关键词搜索

源码搜索 ×
×

tars源码分析之1

发布2022-07-03浏览646次

详情内容

base64是一个可逆的变换算法,算不上加密,也不是哈希,来看看实现。之前自己也写过:

  1. #include "util/tc_base64.h"
  2. namespace tars
  3. {
  4. // Base64编码表:将输入数据流每次取6 bit,用此6 bit的值(0-63)作为索引去查表,输出相应字符。这样,每3个字节将编码为4个字符(3×8 → 4×6);不满4个字符的以'='填充。
  5. const char TC_Base64::EnBase64Tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  6. // Base64解码表:将64个可打印字符的值作为索引,查表得到的值(范围为0-63)依次连起来得到解码结果。
  7. // 解码表size为256,非法字符将被解码为ASCII 0
  8. const char TC_Base64::DeBase64Tab[] =
  9. {
  10. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 62, // '+'
  13. 0, 0, 0,
  14. 63, // '/'
  15. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
  16. 0, 0, 0, 0, 0, 0, 0,
  17. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  18. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
  19. 0, 0, 0, 0, 0, 0,
  20. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  21. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
  22. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  27. };
  28. string TC_Base64::encode(const string &data, bool bChangeLine/* = false*/)
  29. {
  30. if(data.empty())
  31. return "";
  32. //设原始串长度为a,结果串中算上回车换行及'/0',最终长度为(a/3+1)*4+(a/3+1)*4*2/76+1,约为1.369*a+6
  33. char *pDst = NULL;
  34. int iBufSize = (int)(data.size()*1.4) + 6;
  35. pDst = new char[iBufSize];
  36. if(pDst == NULL)
  37. return "";
  38. int iDstLen = encode((unsigned char*)data.c_str(), data.size(), pDst, bChangeLine);
  39. string ret(pDst,iDstLen);
  40. delete [] pDst;
  41. return ret;
  42. }
  43. string TC_Base64::decode(const string &data)
  44. {
  45. if(data.empty())
  46. return "";
  47. unsigned char *pDst = NULL;
  48. pDst = new unsigned char[data.size()];
  49. if(pDst == NULL)
  50. return "";
  51. int iDstLen = decode(data.c_str(), data.size(), pDst);
  52. string ret((char*)pDst,iDstLen);
  53. delete [] pDst;
  54. return ret;
  55. }
  56. int TC_Base64::encode(const unsigned char* pSrc, int nSrcLen, char* pDst, bool bChangeLine/* = false*/)
  57. {
  58. unsigned char c1, c2, c3;
  59. int nDstLen = 0;
  60. int nLineLen = 0;
  61. int nDiv = nSrcLen / 3;
  62. int nMod = nSrcLen % 3;
  63. // 每次取3个字节,编码成4个字符
  64. for (int i = 0; i < nDiv; i ++)
  65. {
  66. c1 = *pSrc++;
  67. c2 = *pSrc++;
  68. c3 = *pSrc++;
  69. *pDst++ = EnBase64Tab[c1 >> 2];
  70. *pDst++ = EnBase64Tab[((c1 << 4) | (c2 >> 4)) & 0x3f];
  71. *pDst++ = EnBase64Tab[((c2 << 2) | (c3 >> 6)) & 0x3f];
  72. *pDst++ = EnBase64Tab[c3 & 0x3f];
  73. nLineLen += 4;
  74. nDstLen += 4;
  75. // 相关RFC中每行超过76字符时需要添加回车换行
  76. if (bChangeLine && nLineLen > 72)
  77. {
  78. *pDst++ = '\r';
  79. *pDst++ = '\n';
  80. nLineLen = 0;
  81. nDstLen += 2;
  82. }
  83. }
  84. // 编码余下的字节
  85. if (nMod == 1)
  86. {
  87. c1 = *pSrc++;
  88. *pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
  89. *pDst++ = EnBase64Tab[((c1 & 0x03) << 4)];
  90. *pDst++ = '=';
  91. *pDst++ = '=';
  92. nLineLen += 4;
  93. nDstLen += 4;
  94. }
  95. else if (nMod == 2)
  96. {
  97. c1 = *pSrc++;
  98. c2 = *pSrc++;
  99. *pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
  100. *pDst++ = EnBase64Tab[((c1 & 0x03) << 4) | ((c2 & 0xf0) >> 4)];
  101. *pDst++ = EnBase64Tab[((c2 & 0x0f) << 2)];
  102. *pDst++ = '=';
  103. nDstLen += 4;
  104. }
  105. // 输出加个结束符
  106. *pDst = '\0';
  107. return nDstLen;
  108. }
  109. int TC_Base64::decode(const char* pSrc, int nSrcLen, unsigned char* pDst)
  110. {
  111. int nDstLen; // 输出的字符计数
  112. int nValue; // 解码用到的整数
  113. int i;
  114. i = 0;
  115. nDstLen = 0;
  116. // 取4个字符,解码到一个长整数,再经过移位得到3个字节
  117. while (i < nSrcLen)
  118. {
  119. // 跳过回车换行
  120. if (*pSrc != '\r' && *pSrc!='\n')
  121. {
  122. if(i + 4 > nSrcLen) //待解码字符串不合法,立即停止解码返回
  123. break;
  124. nValue = DeBase64Tab[int(*pSrc++)] << 18;
  125. nValue += DeBase64Tab[int(*pSrc++)] << 12;
  126. *pDst++ = (nValue & 0x00ff0000) >> 16;
  127. nDstLen++;
  128. if (*pSrc != '=')
  129. {
  130. nValue += DeBase64Tab[int(*pSrc++)] << 6;
  131. *pDst++ = (nValue & 0x0000ff00) >> 8;
  132. nDstLen++;
  133. if (*pSrc != '=')
  134. {
  135. nValue += DeBase64Tab[int(*pSrc++)];
  136. *pDst++ =nValue & 0x000000ff;
  137. nDstLen++;
  138. }
  139. }
  140. i += 4;
  141. }
  142. else
  143. {
  144. pSrc++;
  145. i++;
  146. }
  147. }
  148. // 输出加个结束符
  149. *pDst = '\0';
  150. return nDstLen;
  151. }
  152. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载