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.

259 lines
7.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //*********************************************************************
  2. //* 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. //* Enhancements by Stanley Yamane:
  11. //* o reverse lookup table for the decode function
  12. //* o reserve string buffer space in advance
  13. //*
  14. //*********************************************************************
  15. #include "talk/base/base64.h"
  16. #include <string.h>
  17. #include "talk/base/common.h"
  18. using std::string;
  19. using std::vector;
  20. namespace talk_base {
  21. static const char kPad = '=';
  22. static const unsigned char pd = 0xFD; // Padding
  23. static const unsigned char sp = 0xFE; // Whitespace
  24. static const unsigned char il = 0xFF; // Illegal base64 character
  25. const char Base64::Base64Table[] =
  26. // 0000000000111111111122222222223333333333444444444455555555556666
  27. // 0123456789012345678901234567890123456789012345678901234567890123
  28. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  29. // Decode Table gives the index of any valid base64 character in the
  30. // Base64 table
  31. // 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
  32. const unsigned char Base64::DecodeTable[] = {
  33. // 0 1 2 3 4 5 6 7 8 9
  34. il,il,il,il,il,il,il,il,il,sp, // 0 - 9
  35. sp,sp,sp,sp,il,il,il,il,il,il, // 10 - 19
  36. il,il,il,il,il,il,il,il,il,il, // 20 - 29
  37. il,il,sp,il,il,il,il,il,il,il, // 30 - 39
  38. il,il,il,62,il,il,il,63,52,53, // 40 - 49
  39. 54,55,56,57,58,59,60,61,il,il, // 50 - 59
  40. il,pd,il,il,il, 0, 1, 2, 3, 4, // 60 - 69
  41. 5, 6, 7, 8, 9,10,11,12,13,14, // 70 - 79
  42. 15,16,17,18,19,20,21,22,23,24, // 80 - 89
  43. 25,il,il,il,il,il,il,26,27,28, // 90 - 99
  44. 29,30,31,32,33,34,35,36,37,38, // 100 - 109
  45. 39,40,41,42,43,44,45,46,47,48, // 110 - 119
  46. 49,50,51,il,il,il,il,il,il,il, // 120 - 129
  47. il,il,il,il,il,il,il,il,il,il, // 130 - 139
  48. il,il,il,il,il,il,il,il,il,il, // 140 - 149
  49. il,il,il,il,il,il,il,il,il,il, // 150 - 159
  50. il,il,il,il,il,il,il,il,il,il, // 160 - 169
  51. il,il,il,il,il,il,il,il,il,il, // 170 - 179
  52. il,il,il,il,il,il,il,il,il,il, // 180 - 189
  53. il,il,il,il,il,il,il,il,il,il, // 190 - 199
  54. il,il,il,il,il,il,il,il,il,il, // 200 - 209
  55. il,il,il,il,il,il,il,il,il,il, // 210 - 219
  56. il,il,il,il,il,il,il,il,il,il, // 220 - 229
  57. il,il,il,il,il,il,il,il,il,il, // 230 - 239
  58. il,il,il,il,il,il,il,il,il,il, // 240 - 249
  59. il,il,il,il,il,il // 250 - 255
  60. };
  61. bool Base64::IsBase64Char(char ch) {
  62. return (('A' <= ch) && (ch <= 'Z')) ||
  63. (('a' <= ch) && (ch <= 'z')) ||
  64. (('0' <= ch) && (ch <= '9')) ||
  65. (ch == '+') || (ch == '/');
  66. }
  67. bool Base64::GetNextBase64Char(char ch, char* next_ch) {
  68. if (next_ch == NULL) {
  69. return false;
  70. }
  71. const char* p = strchr(Base64Table, ch);
  72. if (!p)
  73. return false;
  74. ++p;
  75. *next_ch = (*p) ? *p : Base64Table[0];
  76. return true;
  77. }
  78. bool Base64::IsBase64Encoded(const std::string& str) {
  79. for (size_t i = 0; i < str.size(); ++i) {
  80. if (!IsBase64Char(str.at(i)))
  81. return false;
  82. }
  83. return true;
  84. }
  85. void Base64::EncodeFromArray(const void* data, size_t len, string* result) {
  86. ASSERT(NULL != result);
  87. result->clear();
  88. result->resize(((len + 2) / 3) * 4);
  89. const unsigned char* byte_data = static_cast<const unsigned char*>(data);
  90. unsigned char c;
  91. size_t i = 0;
  92. size_t dest_ix = 0;
  93. while (i < len) {
  94. c = (byte_data[i] >> 2) & 0x3f;
  95. (*result)[dest_ix++] = Base64Table[c];
  96. c = (byte_data[i] << 4) & 0x3f;
  97. if (++i < len) {
  98. c |= (byte_data[i] >> 4) & 0x0f;
  99. }
  100. (*result)[dest_ix++] = Base64Table[c];
  101. if (i < len) {
  102. c = (byte_data[i] << 2) & 0x3f;
  103. if (++i < len) {
  104. c |= (byte_data[i] >> 6) & 0x03;
  105. }
  106. (*result)[dest_ix++] = Base64Table[c];
  107. } else {
  108. (*result)[dest_ix++] = kPad;
  109. }
  110. if (i < len) {
  111. c = byte_data[i] & 0x3f;
  112. (*result)[dest_ix++] = Base64Table[c];
  113. ++i;
  114. } else {
  115. (*result)[dest_ix++] = kPad;
  116. }
  117. }
  118. }
  119. size_t Base64::GetNextQuantum(DecodeFlags parse_flags, bool illegal_pads,
  120. const char* data, size_t len, size_t* dpos,
  121. unsigned char qbuf[4], bool* padded)
  122. {
  123. size_t byte_len = 0, pad_len = 0, pad_start = 0;
  124. for (; (byte_len < 4) && (*dpos < len); ++*dpos) {
  125. qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])];
  126. if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) {
  127. if (parse_flags != DO_PARSE_ANY)
  128. break;
  129. // Ignore illegal characters
  130. } else if (sp == qbuf[byte_len]) {
  131. if (parse_flags == DO_PARSE_STRICT)
  132. break;
  133. // Ignore spaces
  134. } else if (pd == qbuf[byte_len]) {
  135. if (byte_len < 2) {
  136. if (parse_flags != DO_PARSE_ANY)
  137. break;
  138. // Ignore unexpected padding
  139. } else if (byte_len + pad_len >= 4) {
  140. if (parse_flags != DO_PARSE_ANY)
  141. break;
  142. // Ignore extra pads
  143. } else {
  144. if (1 == ++pad_len) {
  145. pad_start = *dpos;
  146. }
  147. }
  148. } else {
  149. if (pad_len > 0) {
  150. if (parse_flags != DO_PARSE_ANY)
  151. break;
  152. // Ignore pads which are followed by data
  153. pad_len = 0;
  154. }
  155. ++byte_len;
  156. }
  157. }
  158. for (size_t i = byte_len; i < 4; ++i) {
  159. qbuf[i] = 0;
  160. }
  161. if (4 == byte_len + pad_len) {
  162. *padded = true;
  163. } else {
  164. *padded = false;
  165. if (pad_len) {
  166. // Roll back illegal padding
  167. *dpos = pad_start;
  168. }
  169. }
  170. return byte_len;
  171. }
  172. bool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
  173. string* result, size_t* data_used) {
  174. return DecodeFromArrayTemplate<string>(data, len, flags, result, data_used);
  175. }
  176. bool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
  177. vector<char>* result, size_t* data_used) {
  178. return DecodeFromArrayTemplate<vector<char> >(data, len, flags, result,
  179. data_used);
  180. }
  181. template<typename T>
  182. bool Base64::DecodeFromArrayTemplate(const char* data, size_t len,
  183. DecodeFlags flags, T* result,
  184. size_t* data_used)
  185. {
  186. ASSERT(NULL != result);
  187. ASSERT(flags <= (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK));
  188. const DecodeFlags parse_flags = flags & DO_PARSE_MASK;
  189. const DecodeFlags pad_flags = flags & DO_PAD_MASK;
  190. const DecodeFlags term_flags = flags & DO_TERM_MASK;
  191. ASSERT(0 != parse_flags);
  192. ASSERT(0 != pad_flags);
  193. ASSERT(0 != term_flags);
  194. result->clear();
  195. result->reserve(len);
  196. size_t dpos = 0;
  197. bool success = true, padded;
  198. unsigned char c, qbuf[4];
  199. while (dpos < len) {
  200. size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags),
  201. data, len, &dpos, qbuf, &padded);
  202. c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3);
  203. if (qlen >= 2) {
  204. result->push_back(c);
  205. c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf);
  206. if (qlen >= 3) {
  207. result->push_back(c);
  208. c = ((qbuf[2] << 6) & 0xc0) | qbuf[3];
  209. if (qlen >= 4) {
  210. result->push_back(c);
  211. c = 0;
  212. }
  213. }
  214. }
  215. if (qlen < 4) {
  216. if ((DO_TERM_ANY != term_flags) && (0 != c)) {
  217. success = false; // unused bits
  218. }
  219. if ((DO_PAD_YES == pad_flags) && !padded) {
  220. success = false; // expected padding
  221. }
  222. break;
  223. }
  224. }
  225. if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) {
  226. success = false; // unused chars
  227. }
  228. if (data_used) {
  229. *data_used = dpos;
  230. }
  231. return success;
  232. }
  233. } // namespace talk_base