Base64.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "StdAfx.h"
  2. #include "Base64.h"
  3. CBase64::CBase64(void)
  4. {
  5. }
  6. CBase64::~CBase64(void)
  7. {
  8. }
  9. const char CBase64::sm_base64digits[65] =
  10. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  11. const char CBase64::sm_base64val[128] = {
  12. BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
  13. BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
  14. BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
  15. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
  16. BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  17. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,BAD, BAD,BAD,BAD,BAD,
  18. BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  19. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,BAD, BAD,BAD,BAD,BAD
  20. };
  21. /************************************************************************/
  22. /* 函数:[6/2/2016 IT];
  23. /* 描述:将字节转换为Base64字符;
  24. /* 参数:;
  25. /* [IN] pbinary: 要转换成Base64字符的二进制字节数组;
  26. /* [IN] nbinaryLen: pbinary所指向的缓存大小;
  27. /* [OUT] pOutBase64: 返回转换后的Base64字符,以'\0'结束;
  28. /* 返回:返回转换成功后的Base64字符数;
  29. /* 注意:注意参数1是字节BYTE,返回的pOutBase64是人为加上'\0'结束符;
  30. /* 示例:;
  31. /*
  32. /* 修改:;
  33. /* 日期:;
  34. /* 内容:;
  35. /************************************************************************/
  36. void CBase64::binToBase64(IN const unsigned char *pbinary, IN size_t nbinaryLen, OUT char *pOutBase64)
  37. {
  38. for ( ; nbinaryLen >= 3; nbinaryLen -= 3, pbinary += 3)
  39. {
  40. *pOutBase64++ = sm_base64digits[pbinary[0] >> 2];
  41. *pOutBase64++ = sm_base64digits[((pbinary[0] << 4) & 0x30) | (pbinary[1] >> 4)];
  42. *pOutBase64++ = sm_base64digits[((pbinary[1] << 2) & 0x3c) | (pbinary[2] >> 6)];
  43. *pOutBase64++ = sm_base64digits[pbinary[2] & 0x3f];
  44. }
  45. if (nbinaryLen > 0)
  46. {
  47. unsigned char fragment;
  48. *pOutBase64++ = sm_base64digits[pbinary[0] >> 2];
  49. fragment = (pbinary[0] << 4) & 0x30;
  50. if (nbinaryLen > 1)
  51. fragment |= pbinary[1] >> 4;
  52. *pOutBase64++ = sm_base64digits[fragment];
  53. *pOutBase64++ = (nbinaryLen < 2) ? '=' : sm_base64digits[(pbinary[1] << 2) & 0x3c];
  54. *pOutBase64++ = '=';
  55. }
  56. *pOutBase64 = '\0';
  57. }
  58. /************************************************************************/
  59. /* 函数:[6/2/2016 IT];
  60. /* 描述:将Base64字符串转为化二进制字节字符串;
  61. /* 参数:;
  62. /* [IN] pBase64: 要转化成二进制字节字符串的Base64字符;
  63. /* [OUT] pbinary: 返回转化成二进制字节的字符串;
  64. /* [IN] maxLen: pbinary指向的缓存大小;
  65. /* 返回:返回转化成二进制字节字符数;
  66. /* 注意:;
  67. /* 示例:;
  68. /*
  69. /* 修改:;
  70. /* 日期:;
  71. /* 内容:;
  72. /************************************************************************/
  73. int CBase64::base64ToBin(IN const char *pBase64, OUT unsigned char *pbinary, IN size_t maxLen)
  74. {
  75. size_t len = 0;
  76. unsigned char digit1, digit2, digit3, digit4;
  77. if (pBase64[0] == '+' && pBase64[1] == ' ')
  78. pBase64 += 2;
  79. if (pBase64[0] == '\r')
  80. return 0;
  81. while (*pBase64 && *pBase64 != '\r' /*&& digit4 != '='*/)
  82. {
  83. digit1 = pBase64[0];
  84. if (decode64(digit1) == BAD)
  85. return -1;
  86. digit2 = pBase64[1];
  87. if (decode64(digit2) == BAD)
  88. return -1;
  89. digit3 = pBase64[2];
  90. if (digit3 != '=' && decode64(digit3) == BAD)
  91. return -1;
  92. digit4 = pBase64[3];
  93. if (digit4 != '=' && decode64(digit4) == BAD)
  94. return -1;
  95. pBase64 += 4;
  96. ++len;
  97. if (maxLen && len > maxLen)
  98. return -1;
  99. *(pbinary++) = (decode64(digit1) << 2) | (decode64(digit2) >> 4);
  100. if (digit3 != '=')
  101. {
  102. ++len;
  103. if (maxLen && len > maxLen)
  104. return -1;
  105. *(pbinary++) = ((decode64(digit2) << 4) & 0xf0) | (decode64(digit3) >> 2);
  106. if (digit4 != '=')
  107. {
  108. ++len;
  109. if (maxLen && len > maxLen)
  110. return -1;
  111. *(pbinary++) = ((decode64(digit3) << 6) & 0xc0) | decode64(digit4);
  112. } // if
  113. } // if
  114. } // while
  115. return len;
  116. }
  117. #ifndef VC60
  118. /************************************************************************/
  119. /* 函数:StringToBase64[1/14/2017 IT];
  120. /* 描述:将指定字符串转成Base64字符串;
  121. /* 参数:;
  122. /* [IN] :;
  123. /* [OUT] :;
  124. /* [IN/OUT] :;
  125. /* 返回:void;
  126. /* 注意:;
  127. /* 示例:;
  128. /*
  129. /* 修改:;
  130. /* 日期:;
  131. /* 内容:;
  132. /************************************************************************/
  133. CStringA CBase64::StringToBase64A(IN CStringA strSource)
  134. {
  135. CStringA strBase64 = "";
  136. // 计算出Base64字符长度;
  137. INT nBase64Len = CalcBase64Len(strSource.GetLength());
  138. CHAR* pBase64 = new CHAR[nBase64Len+1];
  139. memset(pBase64, 0, nBase64Len+1);
  140. binToBase64((const unsigned char*)strSource.GetString(), strSource.GetLength(), pBase64);
  141. strBase64 = pBase64;
  142. if (pBase64) delete []pBase64;
  143. return strBase64;
  144. }
  145. CStringW CBase64::StringToBase64W(IN CStringW strSource)
  146. {
  147. CStringW strBase64 = L"";
  148. // 获取多字节字符字节数;
  149. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, strSource, -1, NULL, 0, NULL, NULL);
  150. if ( cbMultiByte == 0 )
  151. return L"";
  152. // 转换成多字节字符;
  153. CHAR *pMultiByteStr = new CHAR[cbMultiByte+1];
  154. memset(pMultiByteStr, 0, cbMultiByte + 1);
  155. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, strSource, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
  156. if (nWriteNum != cbMultiByte)
  157. {
  158. if (pMultiByteStr)
  159. delete []pMultiByteStr;
  160. return L"";
  161. }
  162. // 计算出Base64字符长度;
  163. INT nBase64Len = CalcBase64Len(cbMultiByte);
  164. CHAR *pBase64 = new CHAR[nBase64Len+1];
  165. memset(pBase64, 0, nBase64Len+1);
  166. binToBase64((const unsigned char*)pMultiByteStr, cbMultiByte, pBase64);
  167. strBase64 = pBase64;
  168. if (pBase64) delete []pBase64;
  169. if (pMultiByteStr) delete []pMultiByteStr;
  170. return strBase64;
  171. }
  172. /************************************************************************/
  173. /* 函数:[1/14/2017 IT];
  174. /* 描述:;
  175. /* 参数:;
  176. /* [IN] :;
  177. /* [OUT] :;
  178. /* [IN/OUT] :;
  179. /* 返回:void;
  180. /* 注意:;
  181. /* 示例:;
  182. /*
  183. /* 修改:;
  184. /* 日期:;
  185. /* 内容:;
  186. /************************************************************************/
  187. CStringA CBase64::Base64ToStringA(IN CStringA strBase64)
  188. {
  189. CStringA strResult = "";
  190. // 计算出结果长度;
  191. INT nStrlen = CalcBinLen(strBase64.GetLength());
  192. BYTE *pString = new BYTE[nStrlen+1];
  193. memset(pString, 0, nStrlen+1);
  194. base64ToBin(strBase64, pString, nStrlen);
  195. strResult = (char*)pString;
  196. if ( pString) delete []pString;
  197. return strResult;
  198. }
  199. CStringW CBase64::Base64ToStringW(IN CStringW strBase64)
  200. {
  201. CStringW strResult = L"";
  202. // 获取多字节字符字节数;
  203. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, strBase64, -1, NULL, 0, NULL, NULL);
  204. if ( cbMultiByte == 0 )
  205. return L"";
  206. // 转换成多字节字符;
  207. CHAR *pMultiByteStr = new CHAR[cbMultiByte+1];
  208. memset(pMultiByteStr, 0, cbMultiByte + 1);
  209. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, strBase64, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
  210. if (nWriteNum != cbMultiByte)
  211. {
  212. if (pMultiByteStr)
  213. delete []pMultiByteStr;
  214. return L"";
  215. }
  216. // 计算出Bin字符长度;
  217. INT nStrlen = CalcBinLen(cbMultiByte);
  218. CHAR *pString = new CHAR[nStrlen+1];
  219. memset(pString, 0, nStrlen+1);
  220. base64ToBin(pMultiByteStr, (unsigned char*)pString, nStrlen );
  221. // 获取宽字符字节数;
  222. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, pString, -1, NULL, 0);
  223. if ( cchWideChar == 0 )
  224. return L"";
  225. // 转换成宽字符串;
  226. WCHAR *pResult = new WCHAR[cchWideChar];
  227. memset(pResult, 0 , sizeof(WCHAR)*cchWideChar);
  228. nWriteNum = MultiByteToWideChar(CP_ACP, 0, pString, -1, pResult, cchWideChar );
  229. if ( nWriteNum != cchWideChar)
  230. return L"";
  231. strResult = pResult;
  232. if ( pResult ) delete[] pResult;
  233. if (pString) delete []pString;
  234. if (pMultiByteStr) delete []pMultiByteStr;
  235. return strResult;
  236. }
  237. #endif