概要
实现base64编码和解码功能,使用了位操作和查找表来实现算法。
设计分析
头文件 (base64.h
)
#ifndef BASE64_H_C0CE2A47_D10E_42C9_A27C_C883944E704A
#define BASE64_H_C0CE2A47_D10E_42C9_A27C_C883944E704A
#include <string>
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
#endif /* BASE64_H_C0CE2A47_D10E_42C9_A27C_C883944E704A */
- 接口: 提供了两个函数
base64_encode
和base64_decode
分别用于编码和解码。 - 输入和输出:
base64_encode
接受原始字节和长度,而base64_decode
接受Base64编码的字符串。 - 依赖: 只包含了
<string>
用于标准字符串操作。
实现文件 (base64.cpp
)
#include "base64.h"
#include <iostream>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string base64_decode(std::string const& encoded_string) {
size_t in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]) & 0xff;
char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = 0; j < i; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]) & 0xff;
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
- 静态常量:
base64_chars
定义了Base64字符集。 - 辅助函数:
is_base64
检查一个字符是否是有效的Base64字符。 - 实现: 使用位操作和查找表实现了高效的编码和解码。
测试程序 (test.cpp
)
#include "base64.h"
#include <iostream>
int main() {
const std::string s =
"René Nyffenegger\n"
"http://www.renenyffenegger.ch\n"
"passion for data\n";
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(s.c_str()), s.length());
std::string decoded = base64_decode(encoded);
std::cout << "encoded: " << std::endl << encoded << std::endl << std::endl;
std::cout << "decoded: " << std::endl << decoded << std::endl;
// Test all possibilites of fill bytes (none, one =, two ==)
// References calculated with: https://www.base64encode.org/
std::string rest0_original = "abc";
std::string rest0_reference = "YWJj";
std::string rest0_encoded = base64_encode(reinterpret_cast<const unsigned char*>(rest0_original.c_str()),
rest0_original.length());
std::string rest0_decoded = base64_decode(rest0_encoded);
std::cout << "encoded: " << rest0_encoded << std::endl;
std::cout << "reference: " << rest0_reference << std::endl;
std::cout << "decoded: " << rest0_decoded << std::endl << std::endl;
std::string rest1_original = "abcd";
std::string rest1_reference = "YWJjZA==";
std::string rest1_encoded = base64_encode(reinterpret_cast<const unsigned char*>(rest1_original.c_str()),
rest1_original.length());
std::string rest1_decoded = base64_decode(rest1_encoded);
std::cout << "encoded: " << rest1_encoded << std::endl;
std::cout << "reference: " << rest1_reference << std::endl;
std::cout << "decoded: " << rest1_decoded << std::endl << std::endl;
std::string rest2_original = "abcde";
std::string rest2_reference = "YWJjZGU=";
std::string rest2_encoded = base64_encode(reinterpret_cast<const unsigned char*>(rest2_original.c_str()),
rest2_original.length());
std::string rest2_decoded = base64_decode(rest2_encoded);
std::cout << "encoded: " << rest2_encoded << std::endl;
std::cout << "reference: " << rest2_reference << std::endl;
std::cout << "decoded: " << rest2_decoded << std::endl << std::endl;
return 0;
}
- 用法: 展示了如何使用
base64_encode
和base64_decode
对示例字符串进行编码和解码。 - 验证: 比较编码和解码后的结果与参考值,确保正确性。
- 覆盖范围: 测试了不同填充情况下的编码结果,如无填充、一个’=‘、两个’=='。
优缺点分析
优点
- 高效性: 使用了高效的位操作和查找表进行编码和解码。
- 全面性: 覆盖了各种情况,包括无填充和不同长度的字符串。
- 清晰的接口: 头文件提供了清晰的接口,便于集成到其他项目中使用。
缺点
- 静态数据: 使用静态表 (
base64_chars
),虽然效率高但占用内存。 - 单线程: 没有显式针对并行处理进行优化,尽管在大多数编解码任务中通常不是问题。