andywith 发表于 2018-7-5 11:43

C/C++ base64 源码 及分析学习

先上源码,源码来源于网络,有部分修改.
网上源码很多,但都是抄来抄去,然后有些小BUG也会一起抄,但很少有人会把BUG修正后再发布出来.

C源码:测试正常

#include <string.h>

// 全局常量定义
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char padding_char = '=';

/*编码代码
* const unsigned char * sourcedata, 源数据
* char * base64 ,编码保存
*/
int base64_encode(const unsigned char * sourcedata, char * base64,int len)
{
int i=0, j=0;
unsigned char trans_index=0; // 索引是8位,但是高两位都为0

//const int datalength = strlen((const char*)sourcedata);
const int datalength =len;

for (; i < datalength; i += 3){
// 每三个一组,进行编码
// 要编码的数字的第一个
trans_index = ((sourcedata >> 2) & 0x3f);
base64 = base64char[(int)trans_index];
// 第二个
trans_index = ((sourcedata << 4) & 0x30);
if (i + 1 < datalength){
trans_index |= ((sourcedata >> 4) & 0x0f);
base64 = base64char[(int)trans_index];
}else{
base64 = base64char[(int)trans_index];

base64 = padding_char;

base64 = padding_char;

break; // 超出总长度,可以直接break
}
// 第三个
trans_index = ((sourcedata << 2) & 0x3c);
if (i + 2 < datalength){ // 有的话需要编码2个
trans_index |= ((sourcedata >> 6) & 0x03);
base64 = base64char[(int)trans_index];

trans_index = sourcedata & 0x3f;
base64 = base64char[(int)trans_index];
}
else{
base64 = base64char[(int)trans_index];

base64 = padding_char;

break;
}
}

base64 = '\0';

return 0;
}



//解码

/** 在字符串中查询特定字符位置索引
* const char *str ,字符串
* char c,要查找的字符
*/
inline int num_strchr(const char *str, char c) //
{
const char *pindex = strchr(str, c);
if (NULL == pindex){
return -1;
}
return pindex - str;
}



/* 解码
* const char * base64 码字
* unsigned char * dedata, 解码恢复的数据
*/
int base64_decode(const char * base64, unsigned char * dedata)
{
int i = 0, j=0;
int trans = {0,0,0,0};
for (;base64!='\0';i+=4){
// 每四个一组,译码成三个字符
trans = num_strchr(base64char, base64);
trans = num_strchr(base64char, base64);
// 1/3
dedata = ((trans << 2) & 0xfc) | ((trans>>4) & 0x03);

if (base64 == '='){
continue;
}
else{
trans = num_strchr(base64char, base64);
}
// 2/3
dedata = ((trans << 4) & 0xf0) | ((trans >> 2) & 0x0f);

if (base64 == '='){
continue;
}
else{
trans = num_strchr(base64char, base64);
}

// 3/3
dedata = ((trans << 6) & 0xc0) | (trans & 0x3f);
}

dedata = '\0';

return j;
}





// C++ 源码:
// ----------------------------------------------------------------------------------------------------------------------------------
//Base64.h

#ifndef _BASE64_HH
#define _BASE64_HH


#ifdef __cplusplus
extern "C" {
#endif

unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros = true);
// returns a newly allocated array - of size "resultSize" - that
// the caller is responsible for delete[]ing.

char* base64Encode(char const* orig, unsigned origLength);
// returns a 0-terminated string that
// the caller is responsible for delete[]ing.


#ifdef __cplusplus
}
#endif

#endif
//----------------------------------------------------------------------------------------------------------------------------------------



//Base64.cpp

#include "stdafx.h"
#include "base64.h"
#include "string.h"

static char base64DecodeTable;



char* strDupSize(char const* str)
{
if (str == NULL) return NULL;
size_t len = strlen(str) + 1;
char* copy = new char;
return copy;
}





//初始化解码表
static void initBase64DecodeTable()
{
int i;
for (i = 0; i < 256; ++i) base64DecodeTable = (char)0x80;
// default value: invalid


for (i = 'A'; i <= 'Z'; ++i) base64DecodeTable = 0 + (i - 'A');
for (i = 'a'; i <= 'z'; ++i) base64DecodeTable = 26 + (i - 'a');
for (i = '0'; i <= '9'; ++i) base64DecodeTable = 52 + (i - '0');
base64DecodeTable[(unsigned char)'+'] = 62;
base64DecodeTable[(unsigned char)'/'] = 63;
base64DecodeTable[(unsigned char)'='] = 0;
}

//base64解码函数 参数1:待解码值,参数2:返回解码的长度,参数3:是否去掉尾部的0
unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros)
{
static bool haveInitedBase64DecodeTable = false;
if (!haveInitedBase64DecodeTable)
{
initBase64DecodeTable();
haveInitedBase64DecodeTable = true;
}


unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
int k = 0;

int const jMax = strlen(in) - 3;
// in case "in" is not a multiple of 4 bytes (although it should be)
// 如果“in”不是4字节的倍数(尽管它应该是)

for (int j = 0; j < jMax; j += 4)
{
char inTmp, outTmp;

//原代码实现有个小BUG:如果编码前的数据中含有0,那么会导致解码后数据长度不正确,这在解码二进制文件数据时尤为重要。

//for (int i = 0; i < 4; ++i)
//{
// inTmp = in;
// outTmp = base64DecodeTable[(unsigned char)inTmp];
// if ((outTmp&0x80) != 0) outTmp = 0; // pretend the input was 'A'
//}
//out = (outTmp<<2) | (outTmp>>4);
//out = (outTmp<<4) | (outTmp>>2);
//out = (outTmp<<6) | outTmp;


//for (int i = 0; i < 2; ++i)
//{
// inTmp = in;
// outTmp = base64DecodeTable[(unsigned char)inTmp];
// if ((outTmp&0x80) != 0) outTmp = 0; // pretend the input was 'A'
//}

outTmp = base64DecodeTable[(unsigned char)in];

outTmp = base64DecodeTable[(unsigned char)in];



out = ((outTmp<<2)& 0xfc) | ((outTmp>>4)& 0x03);

if (in == '='){
continue;
}
else{
outTmp =base64DecodeTable[(unsigned char)in];
}
// 2/3
out = ((outTmp<<4)& 0xf0) | ((outTmp>>2)& 0x0f);

if (in == '='){
continue;
}
else{
outTmp =base64DecodeTable[(unsigned char)in];;
}

// 3/3

out = ((outTmp<<6)& 0xc0) | (outTmp& 0x3f);

}


if (trimTrailingZeros)
{
while (k > 0 && out == '\0') --k;
}
resultSize = k;


unsigned char* result = new unsigned char;
memset(result, 0x0, resultSize + 1);
memmove(result, out, resultSize);
delete[] out;


return result;
}


static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//base64编码函数
char* base64Encode(char const* origSigned, unsigned origLength)
{
unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set
if (orig == NULL) return NULL;


unsigned const numOrig24BitValues = origLength/3;
bool havePadding = origLength > numOrig24BitValues*3;
bool havePadding2 = origLength == numOrig24BitValues*3 + 2;
unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding);
char* result = new char; // allow for trailing '/0'


// Map each full group of 3 input bytes into 4 output base-64 characters:
unsigned i;
for (i = 0; i < numOrig24BitValues; ++i)
{
result = base64Char[(orig>>2)&0x3F];
result = base64Char[(((orig&0x3)<<4) | (orig>>4))&0x3F];
result = base64Char[((orig<<2) | (orig>>6))&0x3F];
result = base64Char&0x3F];
}


// Now, take padding into account. (Note: i == numOrig24BitValues)
if (havePadding)
{
result = base64Char[(orig>>2)&0x3F];
if (havePadding2)
{
result = base64Char[(((orig&0x3)<<4) | (orig>>4))&0x3F];
result = base64Char[(orig<<2)&0x3F];
}
else
{
result = base64Char[((orig&0x3)<<4)&0x3F];
result = '=';
}
result = '=';
}


result = 0x0;
return result;
}



//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// 调用示例:
// C函数调用:
char t1[]={0xa,0xc,0xd,0x0,0x0,0x0}; //待编码字符
char bufencode; //编码后缓存区
base64_encode((unsigned char *)t1,bufencode,5); //指定长度调用

printf("编码测试:%s\n",bufencode);

unsigned char outbuf;
unsigned int len1;
len1=base64_decode(bufencode,outbuf);

printf("解码测试:%d\n",len1);
for (int i=0;i<len1;i++)
{
printf("%x ",outbuf);
}
printf("\n");




//C++函数调用:

char *bufEncode;
unsigned char * outBuf;
unsigned int len2;

bufEncode=base64Encode(t1,5);
printf("编码测试:%s\n",bufEncode);

outBuf = base64Decode(bufEncode , len2,false); //最后一个参数,不消除尾部的0



printf("解码测试:%d\n",len2);
for (unsigned i=0;i<len2;i++)
{
printf("%x ",outBuf);
}

delete[]bufEncode;
delete[]outBuf;

-------------------------------------------------------

andywith 发表于 2018-7-5 11:43

// BASE64 代码分析学习


// Base64是用来将非ASCII字符的数据转换成ASCII字符的一种方法
// BASE64 的原理是把待加密数据每3个(字节)为一组,一个字节是8位,所以3*8=24位,然后按6位一组重新拆分成4个(字节)数据,6*4=28位.
// 拆分成的4个数据,每个数据都作为一个索引值,从定义的编码表中取出对应的字符来表示,未加密前的数据一个字节是8位,要是按照字符来表示或者显示数据的话,
// 很明显有些不能表示或不能显示,我们重新编码后,所有数据都是可以显示的字符,这样方便进行字符传输,用于不能传输二进制的场合.

//编码表
static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

// BASE64 加密函数: 参数1,待加密数据 参数2,待加密数据长度 返回加密后的数据地址 使用注意事项: 对加密后数据使用完毕,记得释放内存
char* base64Encode(char const* origSigned, unsigned origLength)
{

unsigned char const* orig = (unsigned char const*)origSigned; //对输入数据按照无符号字节型进行处理

if (orig == NULL) return NULL; //如果输入为空,则返回为空

//unsigned const numOrig24BitValues = origLength / 3; //对输入数据长度 除3,因为BASE64是每3个数据为一组

unsigned numOrig24BitValues = origLength / 3;

bool havePadding = origLength > numOrig24BitValues * 3; //判断是否整除,如果不整除,则需要额外添加数据,增加一个判断标志
bool havePadding2 = origLength == numOrig24BitValues * 3 + 2; //继续判断数据长度,是否是3的倍数余2,如果余2,只需要添加一个数据
unsigned const numResultBytes = 4 * (numOrig24BitValues + havePadding); //计算需要的目标内存空间
char* result = new char; // allow for trailing '/0' //申请内存空间
//char* result = new char; // allow for trailing '/0'

// Map each full group of 3 input bytes into 4 output base-64 characters:
unsigned i;
for (i = 0; i < numOrig24BitValues; ++i)
{
result = base64Char[(orig >> 2) & 0x3F];
result = base64Char[(((orig & 0x3) << 4) | (orig >> 4)) & 0x3F];
result = base64Char[((orig << 2) | (orig >> 6)) & 0x3F];
result = base64Char & 0x3F];
}

// Now, take padding into account. (Note: i == numOrig24BitValues)
if (havePadding) //处理最后的剩余数据
{
result = base64Char[(orig >> 2) & 0x3F];
if (havePadding2) //如果是余2,只需要添加一个 '=',所以要计算三个数据
{
result = base64Char[(((orig & 0x3) << 4) | (orig >> 4)) & 0x3F];
result = base64Char[(orig << 2) & 0x3F];
}
else //如果是余1,则需要添加两个'=',只需要计算两个数据
{
result = base64Char[((orig & 0x3) << 4) & 0x3F];
result = '=';
}
result = '=';
}

//result = '\0';
result = 0x0; //末尾添加0
return result; //返回目标内存地址
}

wisoft 发表于 2018-7-5 12:00

长期不学习,现在理解起来都费劲啊

hejialong 发表于 2018-7-5 19:27

页: [1]
查看完整版本: C/C++ base64 源码 及分析学习