base64.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_
  11. #define RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_
  12. #include <string>
  13. #include <vector>
  14. #include "rtc_base/system/rtc_export.h"
  15. namespace rtc {
  16. class Base64 {
  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. RTC_EXPORT static void EncodeFromArray(const void* data,
  48. size_t len,
  49. std::string* result);
  50. RTC_EXPORT static bool DecodeFromArray(const char* data,
  51. size_t len,
  52. DecodeFlags flags,
  53. std::string* result,
  54. size_t* data_used);
  55. static bool DecodeFromArray(const char* data,
  56. size_t len,
  57. DecodeFlags flags,
  58. std::vector<char>* result,
  59. size_t* data_used);
  60. static bool DecodeFromArray(const char* data,
  61. size_t len,
  62. DecodeFlags flags,
  63. std::vector<uint8_t>* result,
  64. size_t* data_used);
  65. // Convenience Methods
  66. static inline std::string Encode(const std::string& data) {
  67. std::string result;
  68. EncodeFromArray(data.data(), data.size(), &result);
  69. return result;
  70. }
  71. static inline std::string Decode(const std::string& data, DecodeFlags flags) {
  72. std::string result;
  73. DecodeFromArray(data.data(), data.size(), flags, &result, nullptr);
  74. return result;
  75. }
  76. static inline bool Decode(const std::string& data,
  77. DecodeFlags flags,
  78. std::string* result,
  79. size_t* data_used) {
  80. return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
  81. }
  82. static inline bool Decode(const std::string& data,
  83. DecodeFlags flags,
  84. std::vector<char>* result,
  85. size_t* data_used) {
  86. return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
  87. }
  88. private:
  89. static const char Base64Table[];
  90. static const unsigned char DecodeTable[];
  91. static size_t GetNextQuantum(DecodeFlags parse_flags,
  92. bool illegal_pads,
  93. const char* data,
  94. size_t len,
  95. size_t* dpos,
  96. unsigned char qbuf[4],
  97. bool* padded);
  98. template <typename T>
  99. static bool DecodeFromArrayTemplate(const char* data,
  100. size_t len,
  101. DecodeFlags flags,
  102. T* result,
  103. size_t* data_used);
  104. };
  105. } // namespace rtc
  106. #endif /* RTC_BASE_THIRD_PARTY_BASE64_BASE64_H_ */