Libb64:c/c++实现的base64编码解码库函数

base64编码解码本身也不复杂,但要自己写,还是得花点时间,如果能找到现成的可靠的代码,抄来最好,节省了测试的时间。
libb64就是实现base64编码解码的开源库,还提供了C++封装,用起来也挺方便

代码下载地址 https://sourceforge.net/projects/libb64/

下面是使用libb64 C++封装接口进行base64编码解码的调用代码

/*
 * b64_wrapper.h
 *  Created on: 2016年3月31日
 *      Author: guyadong
 */

#ifndef INCLUDE_B64_WRAPPER_H_
#define INCLUDE_B64_WRAPPER_H_
#include <vector>
#include <memory>
#include <string>
#include <stdexcept>
#include "b64/encode.h"
#include "b64/decode.h"

#define _DEF_STRING(x) #x
#define DEF_TO_STRING(x) _DEF_STRING(x)
#define SOURCE_AT __FILE__ ":" DEF_TO_STRING(__LINE__)
#define ERROR_STR(msg) std::string(SOURCE_AT ":").append(msg)

#define throw_except_if_msg(except,expression,msg) \
	if(expression)\
		throw except(ERROR_STR(msg));
#define throw_except_if(except,expression) throw_except_if_msg(except,expression,#expression)
#define throw_if_msg(expression,msg) throw_except_if_msg(std::invalid_argument,expression,msg)
#define throw_if(expression) throw_except_if(std::invalid_argument,expression)

namespace base64{
/* b64异常类 */
class b64_exception:public std::logic_error{
	// 继承基类构造函数
	using std::logic_error::logic_error;
};
/* 调用libb64对数据进行base64编码
 * input为nullptr或size为0抛出std::invalid_argument异常
 *  */
inline std::string encode(const void* input,size_t size){
	throw_if(nullptr==input||0==size)
	std::vector<char> buffer(size<<1);
	encoder ec;
	base64_init_encodestate(&ec._state);
	// count为base64编码后的数据长度
	auto count=ec.encode(reinterpret_cast<const char*>(input),int(size),buffer.data());
	count+=ec.encode_end(buffer.data()+count);
	assert(count<=buffer.size());
	return std::string(buffer.data(),buffer.data()+count);
}
/* 对数据对象进行base64编码 */
template<typename T>
inline typename std::enable_if<std::is_class<T>::value,std::string>::type
encode_obj(const T& input){
	return encode(std::addressof(input),sizeof(T));
}
/* 对数组对象进行base64编码 */
template<typename T>
inline std::string encode_vector(const std::vector<T>& input){
	return encode(input.data(),input.size()*sizeof(T));
}

/* 调用libb64对base64编码的字符串进行解码,返回解码后的二进制数组
 * input为空抛出std::invalid_argument异常
 * */
inline std::vector<uint8_t> decode(const std::string&input){
	throw_if(input.empty())
	std::vector<uint8_t> buffer(input.size());
	decoder dc;
	base64_init_decodestate(&dc._state);
	// count为base64解码后的数据长度
	auto count=dc.decode(input.data(),int(input.size()),reinterpret_cast<char*>(buffer.data()));
	assert(count<=buffer.size());
	return std::vector<uint8_t>(buffer.data(),buffer.data()+count);
}
/* 将base64字符串解码为对象数据 */
template<typename T>
inline typename std::enable_if<std::is_class<T>::value,T>::type
decode_obj(const std::string&input){
	 auto decoded_data=decode(input);
	 throw_except_if_msg(b64_exception,decoded_data.size()!=sizeof(T),"decode data is invalid obj for T")
	 return reinterpret_cast<T&>(decoded_data.data());
}
/* 将base64字符串解码为数组 */
template<typename T>
inline std::vector<T> decode_vector(const std::string&input){
	 auto decoded_data=decode(input);
	 throw_except_if_msg(b64_exception,0!=decoded_data.size()%sizeof(T),"decode data is invalid obj for std::vector<T>")
	 auto p=reinterpret_cast<T*>(decoded_data.data());
	 return std::vector<T>(p,p+decoded_data.size()/sizeof(T));
}

} /* namespace base64 */


#endif /* INCLUDE_B64_WRAPPER_H_ */

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值