您的位置:首页 > 编程语言 > C语言/C++

C++实现base64编码(1)

2016-05-30 14:26 387 查看
下面的代码是php里面的base64编码逻辑,确实比我之前的要美观很多,我只是简单的用C++的类进行了一下封装,删除了一些没用的逻辑,基本上还是原来PHP的代码:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

class Base64{
private:
static const char base64_pad = '=';

public:
unsigned char * Encode(const unsigned char *str, int length, int &ret_length);
unsigned char * Decode(const unsigned char *str, int length, int &ret_length);
};

unsigned char * Base64::Encode(const unsigned char *str, int length, int &ret_length) /* {{{ */
{
/* {{{ base64 tables */
const char base64_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
};
const unsigned char *current = str;
unsigned char *p;
unsigned char * result;

result = new unsigned char[(length+2)/3*4];
p = result;

if (length < 0) {
ret_length = 0;
return NULL;
}

while (length > 2) { /* keep going until we have less than 24 bits */
*p++ = base64_table[current[0] >> 2];
*p++= base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
*p++= base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
*p++= base64_table[current[2] & 0x3f];

current += 3;
length -= 3; /* we just handle 3 octets of data */
}

/* now deal with the tail end of things */
if (length != 0) {
*p++= base64_table[current[0] >> 2];
if (length > 1) {
*p++= base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
*p++= base64_table[(current[1] & 0x0f) << 2];
*p++= base64_pad;
} else {
*p++ = base64_table[(current[0] & 0x03) << 4];
*p++ = base64_pad;
*p++ = base64_pad;
}
}
ret_length = (int)(p - result);
*p = '\0';
return result;
}

unsigned char * Base64::Decode(const unsigned char *str, int length, int &ret_length) /* {{{ */
{
const short base64_reverse_table[256] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};
const unsigned char *current = str;
int ch, i = 0, j = 0, k;
/* this sucks for threaded environments */

unsigned char * result;

result = new unsigned char[length];

/* run through the whole string, converting as we go */
while ((ch = *current++) != '\0' && length-- > 0) {
if (ch == base64_pad) {
if (*current != '=' && (i % 4) == 1) {
return NULL;
}
continue;
}
ch = base64_reverse_table[ch];
if (ch < 0 || ch == -1) { /* a space or some other separator character, we simply skip over */
continue;
} else if (ch == -2) {
return NULL;
}

switch(i % 4) {
case 0:
result[j] = ch << 2;
break;
case 1:
result[j++] |= ch >> 4;
result[j] = (ch & 0x0f) << 4;
break;
case 2:
result[j++] |= ch >>2;
result[j] = (ch & 0x03) << 6;
break;
case 3:
result[j++] |= ch;
break;
}
i++;
cout << "result == " << result << endl;
}

k = j;
/* mop things up if we ended on a boundary */
if (ch == base64_pad) {
switch(i % 4) {
case 1:
return NULL;
case 2:
k++;
case 3:
result[k] = 0;
}
}
if(ret_length) {
ret_length = j;
}
result[j] = '\0';
return result;
}
int main()
{
unsigned char str[] = "     aHR0cDo     vL2ltZy52LmNtY20uY29tLzdkNjZkNy0wYzJkLTVhNTgtYTA3Mi03MWY4MjhiOTRjYmNfY3JvcF8yMTZ{4MTUwLmpwZw=   ======";
unsigned char *normal,*encoded;
int i,len = 0,ret_len=0;

cout << "original string : " << str << endl;
len = sizeof(str)-1;
Base64 * base64 = new Base64();
cout << "str = " << str << endl;
normal = base64->Decode(str,len,ret_len);
cout << "base64 decode : " << normal <<endl;
delete [] encoded;
delete [] normal;
return 0;
}


上面的代码对php源码中的逻辑做了优化,删除了decode方法中判断结尾的“=”号时多余的逻辑,以免干扰视线。具体删除的代码参照php源码中ext/standard/base64.c

上一篇php实现base64的代码也进行了调整,提高了容错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: