好友
阅读权限 30
听众
最后登录 1970-1-1
先上源码,源码来源于网络,有部分修改.
网上源码很多,但都是抄来抄去,然后有些小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[j++] = base64char[(int)trans_index];
// 第二个
trans_index = ((sourcedata << 4) & 0x30);
if (i + 1 < datalength){
trans_index |= ((sourcedata[i + 1] >> 4) & 0x0f);
base64[j++] = base64char[(int)trans_index];
}else{
base64[j++] = base64char[(int)trans_index];
base64[j++] = padding_char;
base64[j++] = padding_char;
break; // 超出总长度,可以直接break
}
// 第三个
trans_index = ((sourcedata[i + 1] << 2) & 0x3c);
if (i + 2 < datalength){ // 有的话需要编码2个
trans_index |= ((sourcedata[i + 2] >> 6) & 0x03);
base64[j++] = base64char[(int)trans_index];
trans_index = sourcedata[i + 2] & 0x3f;
base64[j++] = base64char[(int)trans_index];
}
else{
base64[j++] = base64char[(int)trans_index];
base64[j++] = padding_char;
break;
}
}
base64[j] = '\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[4] = {0,0,0,0};
for (;base64!='\0';i+=4){
// 每四个一组,译码成三个字符
trans[0] = num_strchr(base64char, base64);
trans[1] = num_strchr(base64char, base64[i+1]);
// 1/3
dedata[j++] = ((trans[0] << 2) & 0xfc) | ((trans[1]>>4) & 0x03);
if (base64[i+2] == '='){
continue;
}
else{
trans[2] = num_strchr(base64char, base64[i + 2]);
}
// 2/3
dedata[j++] = ((trans[1] << 4) & 0xf0) | ((trans[2] >> 2) & 0x0f);
if (base64[i + 3] == '='){
continue;
}
else{
trans[3] = num_strchr(base64char, base64[i + 3]);
}
// 3/3
dedata[j++] = ((trans[2] << 6) & 0xc0) | (trans[3] & 0x3f);
}
dedata[j] = '\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[256];
char* strDupSize(char const* str)
{
if (str == NULL) return NULL;
size_t len = strlen(str) + 1;
char* copy = new char[len];
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[4], outTmp[4];
//原代码实现有个小BUG:如果编码前的数据中含有0,那么会导致解码后数据长度不正确,这在解码二进制文件数据时尤为重要。
//for (int i = 0; i < 4; ++i)
//{
// inTmp = in[i+j];
// outTmp = base64DecodeTable[(unsigned char)inTmp];
// if ((outTmp&0x80) != 0) outTmp = 0; // pretend the input was 'A'
//}
//out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);
//out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2);
//out[k++] = (outTmp[2]<<6) | outTmp[3];
//for (int i = 0; i < 2; ++i)
//{
// inTmp = in[i+j];
// outTmp = base64DecodeTable[(unsigned char)inTmp];
// if ((outTmp&0x80) != 0) outTmp = 0; // pretend the input was 'A'
//}
outTmp[0] = base64DecodeTable[(unsigned char)in[j+0]];
outTmp[1] = base64DecodeTable[(unsigned char)in[j+1]];
out[k++] = ((outTmp[0]<<2)& 0xfc) | ((outTmp[1]>>4)& 0x03);
if (in[j+2] == '='){
continue;
}
else{
outTmp[2] =base64DecodeTable[(unsigned char)in[j+2]];
}
// 2/3
out[k++] = ((outTmp[1]<<4)& 0xf0) | ((outTmp[2]>>2)& 0x0f);
if (in[j + 3] == '='){
continue;
}
else{
outTmp[3] =base64DecodeTable[(unsigned char)in[j+3]];;
}
// 3/3
out[k++] = ((outTmp[2]<<6)& 0xc0) | (outTmp[3]& 0x3f);
}
if (trimTrailingZeros)
{
while (k > 0 && out[k-1] == '\0') --k;
}
resultSize = k;
unsigned char* result = new unsigned char[resultSize +1];
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[numResultBytes+1]; // 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[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
result[4*i+2] = base64Char[((orig[3*i+1]<<2) | (orig[3*i+2]>>6))&0x3F];
result[4*i+3] = base64Char[orig[3*i+2]&0x3F];
}
// Now, take padding into account. (Note: i == numOrig24BitValues)
if (havePadding)
{
result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
if (havePadding2)
{
result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F];
}
else
{
result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F];
result[4*i+2] = '=';
}
result[4*i+3] = '=';
}
result[numResultBytes] = 0x0;
return result;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// 调用示例:
// C函数调用:
char t1[]={0xa,0xc,0xd,0x0,0x0,0x0}; //待编码字符
char bufencode[800]; //编码后缓存区
base64_encode((unsigned char *)t1,bufencode,5); //指定长度调用
printf("编码测试:%s\n",bufencode);
unsigned char outbuf[800];
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;
-------------------------------------------------------