诸暨麻将添加redis
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

303 líneas
8.0 KiB

  1. #include "StdAfx.h"
  2. #include "ZBase64.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <ctime>
  6. static const CHAR* DATA_BIN2ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  7. static const std::string base64_chars =
  8. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. "abcdefghijklmnopqrstuvwxyz"
  10. "0123456789+/";
  11. static inline bool is_base64(unsigned char c) {
  12. return (isalnum(c) || (c == '+') || (c == '/'));
  13. }
  14. INT BASE64_Encode(const BYTE* inputBuffer, INT inputCount, TCHAR* outputBuffer)
  15. {
  16. INT i;
  17. BYTE b0, b1, b2;
  18. if ((inputBuffer == NULL) || (inputCount < 0))
  19. {
  20. return -1; // 参数错误
  21. }
  22. if (outputBuffer != NULL)
  23. {
  24. for (i = inputCount; i > 0; i -= 3)
  25. {
  26. if (i >= 3)
  27. { // 将3字节数据转换成4个ASCII字符
  28. b0 = *inputBuffer++;
  29. b1 = *inputBuffer++;
  30. b2 = *inputBuffer++;
  31. *outputBuffer++ = DATA_BIN2ASCII[b0 >> 2];
  32. *outputBuffer++ = DATA_BIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F];
  33. *outputBuffer++ = DATA_BIN2ASCII[((b1 << 2) | (b2 >> 6)) & 0x3F];
  34. *outputBuffer++ = DATA_BIN2ASCII[b2 & 0x3F];
  35. }
  36. else
  37. {
  38. b0 = *inputBuffer++;
  39. if (i == 2)b1 = *inputBuffer++; else b1 = 0;
  40. *outputBuffer++ = DATA_BIN2ASCII[b0 >> 2];
  41. *outputBuffer++ = DATA_BIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F];
  42. *outputBuffer++ = (i == 1) ? TEXT('=') : DATA_BIN2ASCII[(b1 << 2) & 0x3F];
  43. *outputBuffer++ = TEXT('=');
  44. }
  45. } // End for i
  46. *outputBuffer++ = TEXT('/0'); // 添加字符串结束标记
  47. }
  48. return ((inputCount + 2) / 3) * 4; // 返回有效字符个数
  49. }
  50. #define B64_EOLN 0xF0 // 换行/n
  51. #define B64_CR 0xF1 // 回车/r
  52. #define B64_EOF 0xF2 // 连字符-
  53. #define B64_WS 0xE0 // 跳格或者空格(/t、space)
  54. #define B64_ERROR 0xFF // 错误字符
  55. #define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3)
  56. static const BYTE DATA_ASCII2BIN[128] = {
  57. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0xF0, 0xFF, 0xFF, 0xF1, 0xFF, 0xFF,
  58. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  59. 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xF2, 0xFF, 0x3F,
  60. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF,
  61. 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
  62. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  63. 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  64. 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  65. };
  66. INT BASE64_Decode(const TCHAR* inputBuffer, INT inputCount, BYTE* outputBuffer)
  67. {
  68. INT i, j;
  69. BYTE b[4];
  70. TCHAR ch;
  71. if ((inputBuffer == NULL) || (inputCount < 0))
  72. {
  73. return -1; // 参数错误
  74. }
  75. // 去除头部空白字符
  76. while (inputCount > 0)
  77. {
  78. ch = *inputBuffer;
  79. if ((ch < 0) || (ch >= 0x80))
  80. {
  81. return -2; // 数据错误,不在ASCII字符编码范围内
  82. }
  83. else
  84. {
  85. if (DATA_ASCII2BIN[ch] == B64_WS)
  86. {
  87. inputBuffer++;
  88. inputCount--;
  89. }
  90. else
  91. {
  92. break;
  93. }
  94. }
  95. }
  96. // 去除尾部的空白字符、回车换行字符、连字符
  97. while (inputCount >= 4)
  98. {
  99. ch = inputBuffer[inputCount - 1];
  100. if ((ch < 0) || (ch >= 0x80))
  101. {
  102. return -2; // 数据错误,不在ASCII字符编码范围内
  103. }
  104. else
  105. {
  106. if (B64_NOT_BASE64(DATA_ASCII2BIN[ch]))
  107. {
  108. inputCount--;
  109. }
  110. else
  111. {
  112. break;
  113. }
  114. }
  115. }
  116. // 字符串长度必须为4的倍数
  117. if ((inputCount % 4) != 0)
  118. {
  119. return -2; // 数据错误
  120. }
  121. if (outputBuffer != NULL)
  122. {
  123. for (i = 0; i < inputCount; i += 4)
  124. {
  125. for (j = 0; j < 4; j++)
  126. {
  127. ch = *inputBuffer++;
  128. if ((ch < 0) || (ch >= 0x80))
  129. {
  130. return -2; // 数据错误,不在ASCII字符编码范围内
  131. }
  132. else
  133. {
  134. if (ch == '=') // 发现BASE64编码中的填充字符
  135. {
  136. break;
  137. }
  138. else
  139. {
  140. b[j] = DATA_ASCII2BIN[ch];
  141. if (b[j] & 0x80)
  142. {
  143. return -2; // 数据错误,无效的Base64编码字符
  144. }
  145. }
  146. }
  147. } // End for j
  148. if (j == 4)
  149. {
  150. *outputBuffer++ = (b[0] << 2) | (b[1] >> 4);
  151. *outputBuffer++ = (b[1] << 4) | (b[2] >> 2);
  152. *outputBuffer++ = (b[2] << 6) | b[3];
  153. }
  154. else if (j == 3)
  155. { // 有1个填充字节
  156. *outputBuffer++ = (b[0] << 2) | (b[1] >> 4);
  157. *outputBuffer++ = (b[1] << 4) | (b[2] >> 2);
  158. return (i >> 2) * 3 + 2;
  159. }
  160. else if (j == 2)
  161. { // 有2个填充字节
  162. *outputBuffer++ = (b[0] << 2) | (b[1] >> 4);
  163. return (i >> 2) * 3 + 1;
  164. }
  165. else
  166. {
  167. return -2; // 数据错误,无效的Base64编码字符
  168. }
  169. } // End for i
  170. }
  171. return (inputCount >> 2) * 3;
  172. }
  173. std::string base64_decode(std::string const& encoded_string) {
  174. size_t in_len = encoded_string.size();
  175. int i = 0;
  176. int j = 0;
  177. int in_ = 0;
  178. unsigned char char_array_4[4], char_array_3[3];
  179. std::string ret;
  180. while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  181. char_array_4[i++] = encoded_string[in_]; in_++;
  182. if (i == 4) {
  183. for (i = 0; i < 4; i++)
  184. char_array_4[i] = base64_chars.find(char_array_4[i]);
  185. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  186. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  187. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  188. for (i = 0; (i < 3); i++)
  189. ret += char_array_3[i];
  190. i = 0;
  191. }
  192. }
  193. if (i) {
  194. for (j = i; j < 4; j++)
  195. char_array_4[j] = 0;
  196. for (j = 0; j < 4; j++)
  197. char_array_4[j] = base64_chars.find(char_array_4[j]);
  198. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  199. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  200. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  201. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  202. }
  203. return ret;
  204. }
  205. std::string b64decodestring(const std::string& strString)
  206. {
  207. int nByteSrc = strString.length();
  208. std::string pszSource = strString;
  209. const int dekey[] = {
  210. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  211. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  212. 62, // '+'
  213. -1, -1, -1,
  214. 63, // '/'
  215. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
  216. -1, -1, -1, -1, -1, -1, -1,
  217. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  218. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
  219. -1, -1, -1, -1, -1, -1,
  220. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  221. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
  222. };
  223. if (nByteSrc % 4 != 0)
  224. throw "bad base64 string";
  225. std::string pszDecode(nByteSrc * 3 / 4 + 4, '\0');
  226. int nLoop = pszSource[nByteSrc - 1] == '=' ? nByteSrc - 4 : nByteSrc;
  227. int b[4];
  228. int i = 0, n = 0;
  229. for (i = 0; i < nLoop; i += 4)
  230. {
  231. b[0] = dekey[pszSource[i]]; b[1] = dekey[pszSource[i + 1]];
  232. b[2] = dekey[pszSource[i + 2]]; b[3] = dekey[pszSource[i + 3]];
  233. if (b[0] == -1 || b[1] == -1 || b[2] == -1 || b[3] == -1)
  234. throw "bad base64 string";
  235. pszDecode[n] = (b[0] << 2) | ((b[1] & 0x30) >> 4);
  236. pszDecode[n + 1] = ((b[1] & 0xf) << 4) | ((b[2] & 0x3c) >> 2);
  237. pszDecode[n + 2] = ((b[2] & 0x3) << 6) | b[3];
  238. n += 3;
  239. }
  240. if (pszSource[nByteSrc - 1] == '=' && pszSource[nByteSrc - 2] == '=')
  241. {
  242. b[0] = dekey[pszSource[i]]; b[1] = dekey[pszSource[i + 1]];
  243. if (b[0] == -1 || b[1] == -1)
  244. throw "bad base64 string";
  245. pszDecode[n] = (b[0] << 2) | ((b[1] & 0x30) >> 4);
  246. pszDecode[n + 1] = '\0';
  247. }
  248. if (pszSource[nByteSrc - 1] == '=' && pszSource[nByteSrc - 2] != '=')
  249. {
  250. b[0] = dekey[pszSource[i]]; b[1] = dekey[pszSource[i + 1]];
  251. b[2] = dekey[pszSource[i + 2]];
  252. if (b[0] == -1 || b[1] == -1 || b[2] == -1)
  253. throw "bad base64 string";
  254. pszDecode[n] = (b[0] << 2) | ((b[1] & 0x30) >> 4);
  255. pszDecode[n + 1] = ((b[1] & 0xf) << 4) | ((b[2] & 0x3c) >> 2);
  256. pszDecode[n + 2] = '\0';
  257. }
  258. if (pszSource[nByteSrc - 1] != '=' && pszSource[nByteSrc - 2] != '=')
  259. pszDecode[n] = '\0';
  260. return pszDecode;
  261. }