Base64.h封装
来源:互联网 发布:南京联迪软件 编辑:程序博客网 时间:2024/04/30 17:26
/**************************************************
@Module : Base64
@FileName : Base64.h
@Releted File :NULL
@Function : class define for base64 encode or decode
@Version : 1.0
@Copyright (C) 2017 - All Rights Reserved by
------------------------------------------------
@Modification:
Date Version Modifier Description
2017/01/18 1.0Creation
***************************************************/
#pragma once
/**
* @brief: class define for base64 encode or decode
*/
class CBase64
{
public:
/**
* @brief: base64 decode
* @aug: base64--const char *, [input aug], need decode content
* bindata--unsigned char *,[input aug], after decoded buffer
* @return: the length of the decoded buffer
*/
static int Base64_decode( const char * base64, unsigned char * bindata );
/**
* @brief: base64 encode
* @aug: bindata--unsigned char *,[input aug], need encod buffer
* binlength--int, [input aug], the length of need encod buffer
* base64--const char *, [input aug], after encoded content
* @return: point to the decoded buffer
*/
static char * Base64_encode( const unsigned char * bindata, int binlength, char * base64 );
};
/////////////////implement//////////////////////////
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int CBase64::Base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[0]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+1] )
temp[1]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+2] )
temp[2]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+3] )
temp[3]= k;
}
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
if ( base64[i+2] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
if ( base64[i+3] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
((unsigned char)(temp[3]&0x3F));
}
return j;
}
char * CBase64::Base64_encode( const unsigned char * bindata, int binlength, char * base64 )
{
int i, j;
unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
}
0 0
- Base64.h封装
- NSData+Base64.h
- base64 js封装类
- 工具类 Base64封装
- h.264视频文件封装
- h.264视频文件封装
- h.264视频文件封装
- RTP封装H.264
- cocos2d-x节点(base64.h)API
- 用C#实现Base64处理封装
- memory.h头文件封装
- H.264的RTP封装
- H.264的RTP封装
- 《ASCE1885的信息安全》のCrypto++用户手册のbase64.h
- 模块封装(H,Lib,Dll)
- H.264的RTP封装(下)
- ps流来封装h.264数据
- sqlite3.h再封装 追求成真版ADO封装效果
- MyEclipse下Tomcat无法部署项目 finish按钮无法点击
- java并发编程
- ES6 Promise
- 调度器启动分析
- gitbook将书籍git同步到github
- Base64.h封装
- 让 select 的 option 标签支持事件监听(如复制操作)
- 关于空值null的排序问题 mysql 和oracle
- [Android] Gallery选中变大效果的实现
- PAT 1115. Counting Nodes in a BST (30)
- ssm整理
- Matlab R2012a和vs 2010混合编程
- ssh框架初步搭载
- 关于修改 标签input file 样式问题!