Base64.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /************************************************
  2. * *
  3. * CBase64.cpp *
  4. * Base 64 de- and encoding class *
  5. * *
  6. * ============================================ *
  7. * *
  8. * This class was written on 28.05.2003 *
  9. * by Jan Raddatz [jan-raddatz@web.de] *
  10. * *
  11. * ============================================ *
  12. * *
  13. * Copyright (c) by Jan Raddatz *
  14. * This class was published @ codeguru.com *
  15. * 28.05.2003 *
  16. * *
  17. ************************************************/
  18. #include "pch.h"
  19. #include "Base64.h"
  20. char CBase64Coder::ch64[] = {
  21. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  22. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  23. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  24. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  25. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '='
  26. };
  27. CBase64Coder::CBase64Coder()
  28. {
  29. buf = NULL;
  30. size = 0;
  31. }
  32. CBase64Coder::~CBase64Coder()
  33. {
  34. if (buf)
  35. {
  36. free(buf);
  37. buf = NULL;
  38. }
  39. }
  40. void CBase64Coder::allocMem(int NewSize)
  41. {
  42. if (buf)
  43. buf = (char*)realloc(buf, NewSize);
  44. else
  45. buf = (char*)malloc(NewSize);
  46. memset(buf, 0, NewSize);
  47. }
  48. const char* CBase64Coder::encode(const string& buffer)
  49. {
  50. return encode(buffer.c_str(), buffer.length());
  51. }
  52. const char* CBase64Coder::encode(const char* buffer, int buflen)
  53. {
  54. #ifdef _DEBUG
  55. DWORD start, finish;
  56. start = GetTickCount();
  57. #endif
  58. int nLeft = 3 - buflen % 3;
  59. //根据BASE64算法,总长度会变成原来的4/3倍
  60. //所以内存分配=length*4/3并加1位作为结束符号(0)
  61. size = calc_base64_len(buflen);
  62. allocMem(size + 1);
  63. //临时变量,
  64. int index = 0;
  65. char *pOut = buf;
  66. while (buflen - index >= 3)
  67. {
  68. const char * p1 = buffer + index;
  69. const char * p2 = p1 + 1;
  70. const char * p3 = p2 + 1;
  71. char c1 = ((*p1) & (0xFC)) >> 2;
  72. char c2 = ((*p1) & (0x03)) << 4 | ((*p2) & 0xF0) >> 4;
  73. char c3 = ((*p2) & (0x0F)) << 2 | ((*p3) & 0xC0) >> 6;
  74. char c4 = (*p3) & (0x3F);
  75. *(pOut++) = _getBase64Char(c1);
  76. *(pOut++) = _getBase64Char(c2);
  77. *(pOut++) = _getBase64Char(c3);
  78. *(pOut++) = _getBase64Char(c4);
  79. index += 3;
  80. }
  81. int last = buflen - index;
  82. if (last == 1)
  83. {
  84. const char * p1 = buffer + index;
  85. char c1 = ((*p1) & (0xFC)) >> 2;
  86. char c2 = ((*p1) & (0x03)) << 4;
  87. *(pOut++) = _getBase64Char(c1);
  88. *(pOut++) = _getBase64Char(c2);
  89. *(pOut++) = '=';
  90. *(pOut++) = '=';
  91. }
  92. else if (last == 2)
  93. {
  94. const char * p1 = buffer + index;
  95. const char * p2 = p1 + 1;
  96. char c1 = ((*p1) & (0xFC)) >> 2;
  97. char c2 = ((*p1) & (0x03)) << 4 | ((*p2) & 0xF0) >> 4;
  98. char c3 = ((*p2) & (0x0F)) << 2;
  99. *(pOut++) = _getBase64Char(c1);
  100. *(pOut++) = _getBase64Char(c2);
  101. *(pOut++) = _getBase64Char(c3);
  102. *(pOut++) = '=';
  103. //*(pOut++)='=';
  104. }
  105. #ifdef _DEBUG
  106. finish = GetTickCount();
  107. //TRACE("func Base64Encode spend %d ms\n", finish - start);
  108. #endif
  109. return buf;
  110. }
  111. const char* CBase64Coder::decode(const char* buffer, int Length)
  112. {
  113. #ifdef _DEBUG
  114. DWORD start, finish;
  115. start = GetTickCount();
  116. #endif
  117. int length = Length;
  118. if (length % 4 != 0)
  119. return NULL;
  120. int nLen = calc_data_len(buffer,Length);
  121. size = nLen;
  122. allocMem(size + 1);
  123. int dec = 0;
  124. while (buffer[Length - 1 - dec] == '=')
  125. {
  126. dec++;
  127. }
  128. int real_len = length - dec;
  129. int index = 0;
  130. char *pOut = buf;
  131. while (real_len - index >= 4)
  132. {
  133. const char * p1 = buffer + index;
  134. const char * p2 = p1 + 1;
  135. const char * p3 = p2 + 1;
  136. const char * p4 = p3 + 1;
  137. char c1 = BinSearch(*p1);
  138. if (c1 == -1)
  139. {
  140. return NULL;
  141. }
  142. char c2 = BinSearch(*p2);
  143. if (c2 == -1)
  144. {
  145. return NULL;
  146. }
  147. char c3 = BinSearch(*p3);
  148. if (c3 == -1)
  149. {
  150. return NULL;
  151. }
  152. char c4 = BinSearch(*p4);
  153. if (c4 == -1)
  154. {
  155. return NULL;
  156. }
  157. *(pOut++) = (c1) << 2 | ((c2)&(0x30)) >> 4;
  158. *(pOut++) = ((c2)&(0xF)) << 4 | ((c3)&(0x3C)) >> 2;
  159. *(pOut++) = ((c3)&(0x03)) << 6 | (c4)&(0x3F);
  160. index += 4;
  161. }
  162. int last = real_len - index;
  163. if (last == 2)
  164. {
  165. const char * p1 = buffer + index;
  166. const char * p2 = p1 + 1;
  167. char c1 = BinSearch(*p1);
  168. char c2 = BinSearch(*p2);
  169. *(pOut++) = (c1) << 2 | ((c2)&(0x30)) >> 4;
  170. }
  171. else if (last == 3)
  172. {
  173. const char * p1 = buffer + index;
  174. const char * p2 = p1 + 1;
  175. const char * p3 = p2 + 1;
  176. char c1 = BinSearch(*p1);
  177. char c2 = BinSearch(*p2);
  178. char c3 = BinSearch(*p3);
  179. *(pOut++) = (c1) << 2 | ((c2)&(0x30)) >> 4;
  180. *(pOut++) = ((c2)&(0x0F)) << 4 | ((c3)&(0x30)) >> 2;
  181. }
  182. #ifdef _DEBUG
  183. finish = GetTickCount();
  184. TRACE("func Base64Decode spend %d ms\n", finish - start);
  185. #endif
  186. return buf;
  187. }
  188. //采用二分法查找p在ch64数组中的位置,并返回。如果找不到则返回-1
  189. int CBase64Coder::BinSearch(char p)
  190. {
  191. /*if (p >= 'A' && p <= 'Z')
  192. return (p - 'A');
  193. else if (p >= 'a' && p <= 'z')
  194. return (p - 'a' + 26);
  195. else if (p >= '0' && p <= '9')
  196. return (p - '0' + 26 + 26);
  197. else if (p == '+')
  198. return 62;
  199. else if (p == '/')
  200. return 63;
  201. else if (p == '=')
  202. return 64;*/
  203. switch (p)
  204. {
  205. case 'A':
  206. case 'B':
  207. case 'C':
  208. case 'D':
  209. case 'E':
  210. case 'F':
  211. case 'G':
  212. case 'H':
  213. case 'I':
  214. case 'J':
  215. case 'K':
  216. case 'L':
  217. case 'M':
  218. case 'N':
  219. case 'O':
  220. case 'P':
  221. case 'Q':
  222. case 'R':
  223. case 'S':
  224. case 'T':
  225. case 'U':
  226. case 'V':
  227. case 'W':
  228. case 'X':
  229. case 'Y':
  230. case 'Z':
  231. return p - 'A';
  232. case 'a':
  233. case 'b':
  234. case 'c':
  235. case 'd':
  236. case 'e':
  237. case 'f':
  238. case 'g':
  239. case 'h':
  240. case 'i':
  241. case 'j':
  242. case 'k':
  243. case 'l':
  244. case 'm':
  245. case 'n':
  246. case 'o':
  247. case 'p':
  248. case 'q':
  249. case 'r':
  250. case 's':
  251. case 't':
  252. case 'u':
  253. case 'v':
  254. case 'w':
  255. case 'x':
  256. case 'y':
  257. case 'z':
  258. return p - 'a' + 26;
  259. case '0':
  260. case '1':
  261. case '2':
  262. case '3':
  263. case '4':
  264. case '5':
  265. case '6':
  266. case '7':
  267. case '8':
  268. case '9':
  269. return p - '0' + 52;
  270. case '+':
  271. return 62;
  272. case '/':
  273. return 63;
  274. case '=':
  275. return 64;
  276. }
  277. return -1;
  278. }
  279. int CBase64Coder::calc_base64_len(int data_len)
  280. {
  281. int last = data_len * 8 % 6;
  282. if (last == 0)
  283. {
  284. return data_len * 8 / 6;
  285. }
  286. else
  287. {
  288. int base64_len = data_len * 8 / 6 + 1;
  289. base64_len += (last == 2 ? 2 : 1);
  290. return base64_len;
  291. }
  292. }
  293. int CBase64Coder::calc_data_len(const char * base64, int base64_len)
  294. {
  295. int dec = 0;
  296. while (base64[base64_len - 1 - dec] == '=')
  297. {
  298. dec++;
  299. }
  300. return ((base64_len - dec) * 6) / 8;
  301. }
  302. char CBase64Coder::_getBase64Char(char c)
  303. {
  304. return ch64[c];
  305. }