免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3877 | 回复: 3
打印 上一主题 下一主题

[C++] memo: Simple implement of base64, without handling splitor [复制链接]

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-01-17 15:09 |只看该作者 |倒序浏览
本帖最后由 folklore 于 2018-01-17 15:11 编辑
  1. #pragma once
  2. #include <string>
  3. #include <vector>

  4. class Base64
  5. {
  6. public:
  7.         Base64();
  8.         ~Base64();
  9.         void SetLastTocken(const char Ch2ndLast, const char ChLast);
  10.         std::string Encode(const char Source[], const size_t nSize);
  11.         std::vector<char> Decode(const char *const lpSource);

  12. private:
  13.         int indexof(char ch);
  14.         char * Base64Decode4Char(const char src[4], char buf[3]);
  15.         static char DefaultEndCodeTable[];
  16.         char EndCodeTable[65];        // 64+1
  17. };

  18. #include "stdafx.h"
  19. #include "Base64.h"

  20. #define CH2ndLast (sizeof(Base64::EndCodeTable) - 3)
  21. #define CHLast (sizeof(Base64::EndCodeTable) - 2)
  22. ///<summary>Base 64 translate table.</summary>
  23. char Base64::DefaultEndCodeTable[] =
  24.         "ABCDEFGH"
  25.         "IJKLMNOP"
  26.         "QRSTUVWX"
  27.         "YZ"
  28.         "abcdefgh"
  29.         "ijklmnop"
  30.         "qrstuvwx"
  31.         "yz"
  32.         "0123456789+/";

  33. Base64::Base64()
  34. {
  35.         strcpy_s(this->EndCodeTable, sizeof(this->EndCodeTable), DefaultEndCodeTable);
  36. }

  37. Base64::~Base64()
  38. {
  39. }

  40. void Base64::SetLastTocken(const char Ch2ndLast, const char ChLast){
  41.         this->EndCodeTable[CH2ndLast] = Ch2ndLast;        // array[62]
  42.         this->EndCodeTable[CHLast] = ChLast;        // array[63]
  43. }

  44. static int* Base64Encode3Byte(const char src[3], int buf[4]){
  45.         int Work = (src[0] >>2) &0x3f;        // Get 6 bits of 1st byte
  46.         buf[0] = Work;
  47.        
  48.         Work = (src[0] & 0x3) <<4;        // Left 2 bits shift to top 6 bit
  49.         Work |= ((src[1] >> 4) & 0x0f);        // Get top 4 bits of 2nd bytes
  50.         buf[1] = Work;
  51.        
  52.         Work = ((src[1] << 2) & 0x3C);        // Get left 4 bits of 2nd bytes
  53.         Work |= ((src[2] >> 6) & 0x3);        // Get top 2 bits of 3rd bytes
  54.         buf[2] = Work;

  55.         Work = (src[2] & 0x3f);       
  56.         buf[3] = Work;
  57.         return buf;
  58. }

  59. std::string Base64::Encode(const char Source[], const size_t nSize){
  60.         std::string rs;

  61.         if (nSize > 0){
  62.                 size_t nResult = (nSize + 2) - (nSize + 2) % 3;
  63.                 rs.reserve(nResult + 1);

  64.                 for (size_t iEncode = 0; iEncode < nResult - 3; iEncode += 3){
  65.                         int buf[4];
  66.                         Base64Encode3Byte(&Source[iEncode], buf);
  67.                         for (char EncodedByte : buf){
  68.                                 rs.push_back(this->EndCodeTable[EncodedByte]);
  69.                         }
  70.                 }

  71.                 // -- Encode left bytes
  72.                 char LastSource[3] = { 0, };        // Clear to 0
  73.                 for (size_t iEncode = nResult - 3, index = 0; iEncode < nSize; iEncode++, index++){
  74.                         LastSource[index] = Source[iEncode];
  75.                 }
  76.                 int buf[4];
  77.                 Base64Encode3Byte(LastSource, buf);

  78.                 int nRealLeft = ((3 - (nResult - nSize)) * 8 + 5) / 6;

  79.                 int index = 0;
  80.                 for (index = 0; index < nRealLeft; index++){
  81.                         rs.push_back(this->EndCodeTable[buf[index]]);
  82.                 }
  83.                 for (; index < 4; index++){
  84.                         rs.push_back('=');
  85.                 }
  86.         }
  87.         return std::move(rs);
  88. }

  89. int Base64::indexof(char ch){
  90.         int rs = 0;
  91.         if (ch >= 'A' &&ch <= 'Z'){
  92.                 rs = ch - 'A';
  93.         }else if (ch >= 'a' &&ch <= 'z'){
  94.                 rs = ch - 'a' + 26;
  95.         }else if (ch >= '0' &&ch <= '9'){
  96.                 rs = ch - '0' + 52;
  97.         }else if (ch == this->EndCodeTable[CH2ndLast]){
  98.                 rs = CH2ndLast;
  99.         }else if (ch == this->EndCodeTable[CHLast]){
  100.                 rs = CHLast;
  101.         }
  102.         return rs;
  103. }

  104. char * Base64::Base64Decode4Char(const char src[4], char buf[3]){
  105.         int Indexies[4];
  106.         for (int iInput = 0; iInput < _countof(Indexies); iInput++){
  107.                 Indexies[iInput] = this->indexof(src[iInput]);
  108.         }

  109.         int Work = Indexies[0] << 2; // 1st 6 bits go top
  110.         Work |= ((Indexies[1] >> 4) & 0x03);        // Append 2 bits of 2nd index
  111.         buf[0] = Work & 0xff;

  112.         Work = (Indexies[1] << 4) & 0xf0;        // left 4 bits of 2nd index go top
  113.         Work |= ((Indexies[2] >> 2) & 0x0f); // Append 4 bits of 3nd index to bottom
  114.         buf[1] = Work & 0xff;

  115.         Work = ((Indexies[2] << 6) & 0xc0);
  116.         Work |= (Indexies[3] & 0x3f);        // Append last 6 bits
  117.         buf[2] = Work&0xff;
  118.         return buf;
  119. }

  120. std::vector<char> Base64::Decode(const char *const lpSource){
  121.         std::vector<char> rs;

  122.         std::string Input(lpSource);

  123.         if (Input.length() > 0){
  124.                 while (*Input.rbegin() == '='){
  125.                         Input.pop_back();
  126.                 }

  127.                 size_t nSize = Input.length();
  128.                 size_t nSizeHead = nSize - nSize % 4;

  129.                 rs.reserve((nSize >> 2) + (nSize >> 1));
  130.                 for (int iOutput = 0; iOutput < nSizeHead; iOutput += 4){
  131.                         char buf[3];
  132.                         this->Base64Decode4Char(Input.c_str() + iOutput, buf);
  133.                         for (char ch : buf){
  134.                                 rs.push_back(ch);
  135.                         }
  136.                 }

  137.                 // -- Cal left byte
  138.                 if (nSize != nSizeHead){
  139.                         int nCharLeft = (nSize - nSizeHead) * 6 / 8;
  140.                         char InputBuf[4] = { 0, };
  141.                         strcpy_s(InputBuf, 4, Input.c_str() + nSizeHead);
  142.                         char OuptBuf[3] = { 0, };
  143.                         this->Base64Decode4Char(InputBuf, OuptBuf);
  144.                         _ASSERT(nCharLeft <3);
  145.                         for (int iLeft = 0; iLeft < nCharLeft; iLeft++){
  146.                                 rs.push_back(OuptBuf[iLeft]);
  147.                         }
  148.                 }
  149.         }
  150.         return std::move(rs);
  151. }
复制代码

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
2 [报告]
发表于 2018-01-17 18:00 |只看该作者
回复 1# folklore
分享是美德,牛B

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
3 [报告]
发表于 2018-01-17 19:37 |只看该作者
回复 1# folklore

谢谢大神,有没有C的代码

论坛徽章:
14
水瓶座
日期:2014-06-10 09:51:0215-16赛季CBA联赛之江苏
日期:2017-11-27 11:42:3515-16赛季CBA联赛之八一
日期:2017-04-12 14:26:2815-16赛季CBA联赛之吉林
日期:2016-08-20 10:43:1215-16赛季CBA联赛之广夏
日期:2016-06-23 09:53:58程序设计版块每日发帖之星
日期:2016-02-11 06:20:00程序设计版块每日发帖之星
日期:2016-02-09 06:20:0015-16赛季CBA联赛之上海
日期:2015-12-25 16:40:3515-16赛季CBA联赛之广夏
日期:2015-12-22 09:39:36程序设计版块每日发帖之星
日期:2015-08-24 06:20:002015亚冠之德黑兰石油
日期:2015-08-07 09:57:302015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2018-01-18 14:23 |只看该作者
libb64不好吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP