📄 base64t.cpp
字号:
// base64t.cc// code for base64t.h// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "base64t.h" // this module#include "base64.h" // Dan's base64 functions#include "datablok.h" // DataBlock#include "security.h" // xSecurity#include <string.h> // strlen, memcpy#include <limits.h> // INT_MAX// --------- globals ----------Base64Encoder base64encoder;Base64Decoder base64decoder;TransPair base64pair(base64encoder, base64decoder);// ------------------ Base64Encoder ------------------int Base64Encoder::minInputSize() const{ return 0;}int Base64Encoder::maxInputSize() const{ // if max output size is INT_MAX, must account // for the .75 expansion factor (if the numbers are // so close to INT_MAX, high-bit truncation may cause // its own bugs..) return INT_MAX/4*3;}int Base64Encoder::minOutputSize(int inputSize) const{ // exact computed value return 1 + (inputSize+2)/3*4; // for now, loose bounds; min of 1 because of null terminator //return max(inputSize*4/3 - 4, 1);}int Base64Encoder::maxOutputSize(int inputSize) const{ // computed value is exact (it is, however, legal to insert // whitespace into the encoding, so this isn't necessarily // correct for some other implementation) return minOutputSize(inputSize); // for now, loose bounds //return inputSize*4/3 + 4;}void Base64Encoder::trans(DataBlock &data){ // run Dan's routine int destLen = base64enc( data.getData(), // input data.getData(), // output data.getDataLen() // input length ); // just assert that it succeeded, because there // should be no input that can't be base64'd xassert(destLen != BASE64_ERROR); // Dan's routine writes the null terminator, but it doesn't count it data.setDataLen(destLen+1);}// ------------------- Base64Decoder ----------------------int Base64Decoder::minInputSize() const{ // encoder always outputs at least a null terminator return 1;}int Base64Decoder::maxInputSize() const{ return INT_MAX;}int Base64Decoder::minOutputSize(int /*inputSize*/) const{ // it is possible for the encoding to have whitespace; // therefore, from just the length alone, we can say // nothing about the minimum output size return 0; // tight computed lower bound //return max( ((inputSize-1)/4 - 1)*3 + 1, 0 ); // loose bounds (for now) //return max(inputSize*3/4 - 4, 0);}int Base64Decoder::maxOutputSize(int inputSize) const{ // tight computed upper bound return ((inputSize-1)/4 - 1)*3 + 3; // loose bounds (for now) //return inputSize*3/4 + 4;}void Base64Decoder::trans(DataBlock &data){ // call Dan's routine int destLen = base64dec( data.getData(), // input, null-terminated data.getData() // output ); // transmission error, etc., might make this plausible if (destLen == BASE64_ERROR) { THROW(xSecurity("malformed base64 input")); // it would be nice to include the source string that // caused the error, but that isn't available since the // base64dec destroys its original input } data.setDataLen(destLen);}// ------------------ alternate interface ----------------void base64decode(DataBlock &dest, char const *srcEncoded){ // prepare a suitable buffer int srcLen = base64length(srcEncoded); dest.setAllocated( mymax(base64decoder.maxOutputSize(srcLen), srcLen)); memcpy(dest.getData(), srcEncoded, srcLen); dest.setDataLen(srcLen); // decode it base64decoder.trans(dest);}// not very efficient.. copies the data twice, ignoring the// base64 part itselfstring base64encode(DataBlock const &src){ // copy it to a large enough data block int srcLen = src.getDataLen(); DataBlock temp(src, base64encoder.maxOutputSize(srcLen)); // encode it base64encoder.trans(temp); // return it as a string return string((char const*)temp.getDataC(), temp.getDataLen());}int base64length(char const *encoded){ // a base64 string is its contents AND the null terminator return strlen(encoded)+1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -