You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base64codec.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // base64codec.hpp
  2. //
  3. // Copyright (C) 2004-2020 MicroNeil Research Corporation.
  4. //
  5. // This software is released under the MIT license. See LICENSE.TXT.
  6. //
  7. // Tools for encoding and decoding base64 data.
  8. #pragma once
  9. #include <vector>
  10. #include <cstring>
  11. #include <string>
  12. namespace codedweller {
  13. typedef std::vector<unsigned char> base64buffer;
  14. class to_base64 : public base64buffer { // Converts binary data to base 64.
  15. private:
  16. bool BadConversion; // True if something went wrong.
  17. void convert(const unsigned char* bfr, const int len); // Does the actual work.
  18. public:
  19. to_base64(const std::vector<unsigned char>& bfr); // Converts from a base64buffer.
  20. to_base64(const std::vector<char>& bfr); // Converts from a buffer.
  21. to_base64(const std::string& s); // Converts from a c++ string.
  22. to_base64(const char* s); // Converts from a c string.
  23. to_base64(const unsigned char* bfr, const int len); // Converts from a uchar buffer.
  24. to_base64(const char* bfr, const int len); // Converts from a char buffer.
  25. bool Bad();
  26. };
  27. class from_base64 : public base64buffer { // Convert base64 data to binary.
  28. private:
  29. bool BadConversion; // True if the conversion didn't go well.
  30. unsigned char NextSixBits( // Helper for decoding & ingoring spaces.
  31. int& cursor,
  32. const unsigned char* bfr,
  33. const int len);
  34. void convert(const unsigned char* bfr, const int len); // Does the actual work.
  35. public:
  36. from_base64(const std::vector<unsigned char>& bfr); // Converts from a base64buffer.
  37. from_base64(const std::vector<char>& bfr); // Converts from a buffer.
  38. from_base64(const std::string& s); // Converts from a c++ string.
  39. from_base64(const char*); // Converts from a c_string.
  40. bool Bad(); // True if conversion wasn't complete.
  41. };
  42. }