/*
* base64.h
*
* Created on: 2013-1-15
* Author: hcmfys@163.com
*/
#ifndef _BASE_64_H_
#define _BASE_64_H_
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#ifndef OUT
#define OUT
#endif
#ifndef IN
#define IN
#endif
class Base64
{
public:
Base64()
{
base_table ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
int base64_decode(IN const char*code,OUT string &result)
{
int len=strlen(code);
int size=len /4;
if(len % 4 !=0)
{
cout<<"error data format\n";
return -1;
}
int alloc_size=len /4 *3;
char *buffer=new char[alloc_size+1];
memset(buffer,0,alloc_size+1);
int q=0;
for(int i=0; i<size; i++)
{
unsigned char p0=(unsigned char)code[4*i];
unsigned char p1=(unsigned char)code[4*i+1];
unsigned char p2=(unsigned char)code[4*i+2];
unsigned char p3=(unsigned char)code[4*i+3];
int s1=get_index(p0);
if(s1==-1)
{
cout<<"error data format\n";
return -1;
}
int s2=get_index(p1);
if(s2==-1)
{
cout<<"error data format\n";
return -1;
}
int s3=-2;
int s4=-2;
if(p2 =='=')
{
if(p3 != '=')
{
cout<<"error data format\n";
return -1;
}
s3=-2;
}
else
{
s3=get_index(p2);
if(s3==-1)
{
cout<<"error data format\n";
return -1;
}
}
if(p3=='=')
{
s4=-2;
}
else
{
s4=get_index(p3);
if(s4==-1)
{
cout<<"error data format\n";
return -1;
}
}
if(s3==-2)
{
s2=s2 >>4;
buffer[q]=(s1 << 2) | s2;
}
else
{
//前面前6位和中间2位
buffer[q]=( s1<< 2 ) | ( s2 >> 4 );
q++;
if(s4==-2)
{
s3=s3 >>2;
int low= (s2 & 0xf ) << 4;
buffer[q]= low | s3;
}
else
{
//前面前4位和中间4位
int low=(s2 & 0xf)<< 4 ;
int high=s3 >> 2;
buffer[q]= low | high;
q++;
//中间后2位和最后6位
low=(s3 & 0x03) << 6;
buffer[q]=low | s4;
q++;
}
}
}
result=buffer;
alloc_size=strlen(buffer);
delete[] buffer;
return alloc_size;
}
int base64_encode(IN const char* code, OUT string &result)
{
int len = strlen(code);
int size = len / 3;
int allocat_size=size*4 ;
int index = 0;
for (int i = 0; i < size; i++)
{
//前6位
unsigned char p0=(unsigned char)code[index];
unsigned char p1=(unsigned char)code[index+1];
unsigned char p2=(unsigned char)code[index+2];
int cur_index = p0>> 2;
result += base_table[cur_index];
//前2位中间4位
cur_index = ((p0 & 0x03) << 4) | (p1>> 4);
result += base_table[cur_index];
//中间后4位和最后前2位
cur_index = ((p1 & 0xf) << 2) | (p2 >> 6);
result += base_table[cur_index];
//最后6位
cur_index = (p2 & 0x3f);
result += base_table[cur_index];
index += 3;
}
size = 3-len * 4 % 3 ;
switch(size)
{
case 1:
{
unsigned char p0=(unsigned char)code[index];
unsigned char p1=(unsigned char)code[index+1];
//前6位
int cur_index = (p0 >> 2 & 0x3f);
result += base_table[cur_index];
//前后2位和中间4位
int high = (p0 & 0x03) << 4;
int low = p1 >> 4 & 0x0f;
cur_index = high | low;
result += base_table[cur_index];
//最后4位补1个 00 位
high = p1<< 4 & 0xf0;
high=high>>2;
cur_index=high;
result += base_table[cur_index];
//填充一个 = 号
result += "=";
allocat_size+=4;
}
break;
case 2:
{
unsigned char p0=(unsigned char)code[index];
//前6位
int cur_index = (p0>> 2 & 0x3f);
result += base_table[cur_index];
//前4位补充 2个 00 位
cur_index = p0 & 0x03;
cur_index = cur_index << 4;
result += base_table[cur_index];
result += "=";
result += "=";
allocat_size+=4;
}
break;
default:
break;
}
return allocat_size;
}
private:
char *base_table ;
int get_index(char l)
{
for(int i=0; i<64; i++)
{
if(base_table[i]==l)
return i;
}
return -1;
}
};
#endif
c++ base64
最新推荐文章于 2024-09-05 16:20:01 发布