char legal_letter[128];
char encode_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', '+', '/',};
unsigned char decode_table[256] = {
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,62 ,100,100,100, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,100,100,100, 99,100,100,
100, 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,100,100,100,100,100,
100, 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,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};
/************************************************************
************************************************************/
void init_legal_letter_table()
{
char c = 0;
memset(legal_letter, 0x00, 128);
for( c = 'A'; c <= 'Z'; c++)
{
legal_letter[c] = 1;
}
for( c = 'a'; c <= 'z'; c++)
{
legal_letter[c] = 1;
}
for( c = '0'; c <= '9'; c++)
{
legal_letter[c] = 1;
}
legal_letter['+'] = 1;
legal_letter['/'] = 1;
legal_letter['='] = 1;
}
/************************************************************
************************************************************/
int is_legal(unsigned char *data)
{
int legal = 1;
char c;
init_legal_letter_table();
while( *data != '\0' )
{
c = *data;
if( legal_letter[c] == 0 )
{
printf("%s| Illegal character:[0x%02x|%c].\n", __FUNCTION__, c, c);
legal = 0;
break;
}
data++;
}
return legal;
}
/************************************************************
************************************************************/
int get_parenthesis_number(unsigned char* base64code)
{
int count = 0;
unsigned char *end = NULL;
end = base64code + strlen(base64code) - 1;
while( end >= base64code && *end == '=' )
{
count++;
end--;
}
if( count > 2 )
{
BASE64_DEBUG_PRINT("Error: Not Base64 code.\n");
return -1;
}
return count;
}
/************************************************************
************************************************************/
unsigned char *base64_encode(void *data)
{
unsigned char *original = (unsigned char*)data;
int len = strlen(original);
BASE64_DEBUG_PRINT("Original len:%d\n", len);
int base64_len = (len+2)/3*4;
BASE64_DEBUG_PRINT("After the base64 encode, the len is:%d\n", base64_len);
unsigned char *base64code = (unsigned char*)malloc(sizeof(unsigned char)*base64_len+1);
unsigned char *begin = original;
unsigned char *p = base64code;
unsigned char c[4];
memset(base64code, 0x00, base64_len+1);
while(len - 3 >= 0)
{
memset(c, 0x00, 4);
BASE64_DEBUG_PRINT("%u %u %u\n", begin[0], begin[1], begin[2]);
c[0] = begin[0]>>2;
c[1] = (begin[0]&0x3)<<4|begin[1]>>4;
c[2] = (begin[1]&0xf)<<2|begin[2]>>6;
c[3] = begin[2]&0x3f;
BASE64_DEBUG_PRINT("%u %u %u %u\n", c[0], c[1], c[2], c[3]);
c[0] = encode_table[c[0]];
c[1] = encode_table[c[1]];
c[2] = encode_table[c[2]];
c[3] = encode_table[c[3]];
BASE64_DEBUG_PRINT("%c%c%c%c\n", c[0], c[1], c[2], c[3]);
strncpy(p, c, 4);
len -= 3;
begin += 3;
p += 4;
}
memset(c, 0x00, 4);
if( len == 1 )
{
c[0] = begin[0]>>2;
c[1] = (begin[0]&0x3)<<4;
c[0] = encode_table[c[0]];
c[1] = encode_table[c[1]];
c[2] = c[3] = '=';
}
else if( len == 2 )
{
c[0] = begin[0]>>2;
c[1] = (begin[0]&0x3)<<4|begin[1]>>4;
c[2] = (begin[1]&0xf)<<2;
c[0] = encode_table[c[0]];
c[1] = encode_table[c[1]];
c[2] = encode_table[c[2]];
c[3] = '=';
}
strncpy(p, c, 4);
return base64code;
}
/************************************************************
************************************************************/
unsigned char *base64_decode(unsigned char *base64, int *dlen)
{
int parenthesis = 0;
int base64_len = 0;
int len = 0;
unsigned char *original = NULL;
unsigned char *begin = NULL;
unsigned char c[4];
parenthesis = get_parenthesis_number(base64);
if( parenthesis < 0 )
{
return NULL;
}
base64_len = strlen(base64);
len = base64_len / 4 * 3 - parenthesis;
original = (unsigned char*)malloc(len + 1);
begin = original;
memset(original, 0x00, len+1);
while( base64_len - 4 > 0 || (base64_len - 4 == 0 && parenthesis == 0) )
{
memset(c, 0x00, 4);
c[0] = decode_table[base64[0]];
c[1] = decode_table[base64[1]];
c[2] = decode_table[base64[2]];
c[3] = decode_table[base64[3]];
begin[0] = c[0]<<2 | c[1]>>4;
begin[1] = c[1]<<4 | c[2]>>2;
begin[2] = c[2]<<6 | c[3];
base64_len -= 4;
base64 += 4;
begin += 3;
}
memset(c, 0x00, 4);
if( parenthesis == 1 )
{
c[0] = decode_table[base64[0]];
c[1] = decode_table[base64[1]];
c[2] = decode_table[base64[2]];
begin[0] = c[0]<<2 | c[1]>>4;
begin[1] = c[1]<<4 | c[2]>>2;
}
else if( parenthesis == 2 )
{
c[0] = decode_table[base64[0]];
c[1] = decode_table[base64[1]];
begin[0] = c[0]<<2 | c[1]>>4;
}
*dlen = len;
return original;
}
char encode_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', '+', '/',};
unsigned char decode_table[256] = {
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,62 ,100,100,100, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,100,100,100, 99,100,100,
100, 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,100,100,100,100,100,
100, 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,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};
/************************************************************
************************************************************/
void init_legal_letter_table()
{
char c = 0;
memset(legal_letter, 0x00, 128);
for( c = 'A'; c <= 'Z'; c++)
{
legal_letter[c] = 1;
}
for( c = 'a'; c <= 'z'; c++)
{
legal_letter[c] = 1;
}
for( c = '0'; c <= '9'; c++)
{
legal_letter[c] = 1;
}
legal_letter['+'] = 1;
legal_letter['/'] = 1;
legal_letter['='] = 1;
}
/************************************************************
************************************************************/
int is_legal(unsigned char *data)
{
int legal = 1;
char c;
init_legal_letter_table();
while( *data != '\0' )
{
c = *data;
if( legal_letter[c] == 0 )
{
printf("%s| Illegal character:[0x%02x|%c].\n", __FUNCTION__, c, c);
legal = 0;
break;
}
data++;
}
return legal;
}
/************************************************************
************************************************************/
int get_parenthesis_number(unsigned char* base64code)
{
int count = 0;
unsigned char *end = NULL;
end = base64code + strlen(base64code) - 1;
while( end >= base64code && *end == '=' )
{
count++;
end--;
}
if( count > 2 )
{
BASE64_DEBUG_PRINT("Error: Not Base64 code.\n");
return -1;
}
return count;
}
/************************************************************
************************************************************/
unsigned char *base64_encode(void *data)
{
unsigned char *original = (unsigned char*)data;
int len = strlen(original);
BASE64_DEBUG_PRINT("Original len:%d\n", len);
int base64_len = (len+2)/3*4;
BASE64_DEBUG_PRINT("After the base64 encode, the len is:%d\n", base64_len);
unsigned char *base64code = (unsigned char*)malloc(sizeof(unsigned char)*base64_len+1);
unsigned char *begin = original;
unsigned char *p = base64code;
unsigned char c[4];
memset(base64code, 0x00, base64_len+1);
while(len - 3 >= 0)
{
memset(c, 0x00, 4);
BASE64_DEBUG_PRINT("%u %u %u\n", begin[0], begin[1], begin[2]);
c[0] = begin[0]>>2;
c[1] = (begin[0]&0x3)<<4|begin[1]>>4;
c[2] = (begin[1]&0xf)<<2|begin[2]>>6;
c[3] = begin[2]&0x3f;
BASE64_DEBUG_PRINT("%u %u %u %u\n", c[0], c[1], c[2], c[3]);
c[0] = encode_table[c[0]];
c[1] = encode_table[c[1]];
c[2] = encode_table[c[2]];
c[3] = encode_table[c[3]];
BASE64_DEBUG_PRINT("%c%c%c%c\n", c[0], c[1], c[2], c[3]);
strncpy(p, c, 4);
len -= 3;
begin += 3;
p += 4;
}
memset(c, 0x00, 4);
if( len == 1 )
{
c[0] = begin[0]>>2;
c[1] = (begin[0]&0x3)<<4;
c[0] = encode_table[c[0]];
c[1] = encode_table[c[1]];
c[2] = c[3] = '=';
}
else if( len == 2 )
{
c[0] = begin[0]>>2;
c[1] = (begin[0]&0x3)<<4|begin[1]>>4;
c[2] = (begin[1]&0xf)<<2;
c[0] = encode_table[c[0]];
c[1] = encode_table[c[1]];
c[2] = encode_table[c[2]];
c[3] = '=';
}
strncpy(p, c, 4);
return base64code;
}
/************************************************************
************************************************************/
unsigned char *base64_decode(unsigned char *base64, int *dlen)
{
int parenthesis = 0;
int base64_len = 0;
int len = 0;
unsigned char *original = NULL;
unsigned char *begin = NULL;
unsigned char c[4];
parenthesis = get_parenthesis_number(base64);
if( parenthesis < 0 )
{
return NULL;
}
base64_len = strlen(base64);
len = base64_len / 4 * 3 - parenthesis;
original = (unsigned char*)malloc(len + 1);
begin = original;
memset(original, 0x00, len+1);
while( base64_len - 4 > 0 || (base64_len - 4 == 0 && parenthesis == 0) )
{
memset(c, 0x00, 4);
c[0] = decode_table[base64[0]];
c[1] = decode_table[base64[1]];
c[2] = decode_table[base64[2]];
c[3] = decode_table[base64[3]];
begin[0] = c[0]<<2 | c[1]>>4;
begin[1] = c[1]<<4 | c[2]>>2;
begin[2] = c[2]<<6 | c[3];
base64_len -= 4;
base64 += 4;
begin += 3;
}
memset(c, 0x00, 4);
if( parenthesis == 1 )
{
c[0] = decode_table[base64[0]];
c[1] = decode_table[base64[1]];
c[2] = decode_table[base64[2]];
begin[0] = c[0]<<2 | c[1]>>4;
begin[1] = c[1]<<4 | c[2]>>2;
}
else if( parenthesis == 2 )
{
c[0] = decode_table[base64[0]];
c[1] = decode_table[base64[1]];
begin[0] = c[0]<<2 | c[1]>>4;
}
*dlen = len;
return original;
}