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.

104 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //*********************************************************************
  2. //* C_Base64 - a simple base64 encoder and decoder.
  3. //*
  4. //* Copyright (c) 1999, Bob Withers - bwit@pobox.com
  5. //*
  6. //* This code may be freely used for any purpose, either personal
  7. //* or commercial, provided the authors copyright notice remains
  8. //* intact.
  9. //*********************************************************************
  10. #ifndef TALK_BASE_BASE64_H__
  11. #define TALK_BASE_BASE64_H__
  12. #include <string>
  13. #include <vector>
  14. namespace talk_base {
  15. class Base64
  16. {
  17. public:
  18. enum DecodeOption {
  19. DO_PARSE_STRICT = 1, // Parse only base64 characters
  20. DO_PARSE_WHITE = 2, // Parse only base64 and whitespace characters
  21. DO_PARSE_ANY = 3, // Parse all characters
  22. DO_PARSE_MASK = 3,
  23. DO_PAD_YES = 4, // Padding is required
  24. DO_PAD_ANY = 8, // Padding is optional
  25. DO_PAD_NO = 12, // Padding is disallowed
  26. DO_PAD_MASK = 12,
  27. DO_TERM_BUFFER = 16, // Must termiante at end of buffer
  28. DO_TERM_CHAR = 32, // May terminate at any character boundary
  29. DO_TERM_ANY = 48, // May terminate at a sub-character bit offset
  30. DO_TERM_MASK = 48,
  31. // Strictest interpretation
  32. DO_STRICT = DO_PARSE_STRICT | DO_PAD_YES | DO_TERM_BUFFER,
  33. DO_LAX = DO_PARSE_ANY | DO_PAD_ANY | DO_TERM_CHAR,
  34. };
  35. typedef int DecodeFlags;
  36. static bool IsBase64Char(char ch);
  37. // Get the char next to the |ch| from the Base64Table.
  38. // If the |ch| is the last one in the Base64Table then returns
  39. // the first one from the table.
  40. // Expects the |ch| be a base64 char.
  41. // The result will be saved in |next_ch|.
  42. // Returns true on success.
  43. static bool GetNextBase64Char(char ch, char* next_ch);
  44. // Determines whether the given string consists entirely of valid base64
  45. // encoded characters.
  46. static bool IsBase64Encoded(const std::string& str);
  47. static void EncodeFromArray(const void* data, size_t len,
  48. std::string* result);
  49. static bool DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
  50. std::string* result, size_t* data_used);
  51. static bool DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
  52. std::vector<char>* result, size_t* data_used);
  53. // Convenience Methods
  54. static inline std::string Encode(const std::string& data) {
  55. std::string result;
  56. EncodeFromArray(data.data(), data.size(), &result);
  57. return result;
  58. }
  59. static inline std::string Decode(const std::string& data, DecodeFlags flags) {
  60. std::string result;
  61. DecodeFromArray(data.data(), data.size(), flags, &result, NULL);
  62. return result;
  63. }
  64. static inline bool Decode(const std::string& data, DecodeFlags flags,
  65. std::string* result, size_t* data_used)
  66. {
  67. return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
  68. }
  69. static inline bool Decode(const std::string& data, DecodeFlags flags,
  70. std::vector<char>* result, size_t* data_used)
  71. {
  72. return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
  73. }
  74. private:
  75. static const char Base64Table[];
  76. static const unsigned char DecodeTable[];
  77. static size_t GetNextQuantum(DecodeFlags parse_flags, bool illegal_pads,
  78. const char* data, size_t len, size_t* dpos,
  79. unsigned char qbuf[4], bool* padded);
  80. template<typename T>
  81. static bool DecodeFromArrayTemplate(const char* data, size_t len,
  82. DecodeFlags flags, T* result,
  83. size_t* data_used);
  84. };
  85. } // namespace talk_base
  86. #endif // TALK_BASE_BASE64_H__