gpt4 book ai didi

c++ - C++ Base64图像文件编码

转载 作者:行者123 更新时间:2023-11-28 04:44:31 25 4
gpt4 key购买 nike

我正在编写一些简单的代码来将文件编码为 base64。我有一个简短的 c++ 代码,可以将文件读入 vector 并将其转换为 unsigned char*。我这样做是为了正确使用我得到的编码函数。

问题:它适用于文本文件(不同大小),但不适用于图像文件。我不知道为什么。给了什么?

对于包含文本 abcd 的简单 text.txt,我的代码和 bash 的输出 $( base64 text.txt ) 是一样的。

另一方面,当我输入图像时,输出类似于 iVBORwOKGgoAAAAAAA......AAA== 或者有时它以 corrupted size vs prev_size Aborted (核心转储),前几个字节是正确的。

代码:

static std::vector<char> readBytes(char const* filename)
{
std::ifstream ifs(filename, std::ios::binary|std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg();
std::vector<char> result(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(&result[0], pos);

return result;
}

static char Base64Digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int ToBase64Simple( const BYTE* pSrc, int nLenSrc, char* pDst, int nLenDst )
{
int nLenOut= 0;
while ( nLenSrc > 0 ) {

if (nLenOut+4 > nLenDst) {
cout << "error\n";
return(0); // error
}

// read three source bytes (24 bits)
BYTE s1= pSrc[0]; // (but avoid reading past the end)
BYTE s2= 0; if (nLenSrc>1) s2=pSrc[1]; //------ corrected, thanks to jprichey
BYTE s3= 0; if (nLenSrc>2) s3=pSrc[2];

DWORD n;
n = s1; // xxx1
n <<= 8; // xx1x
n |= s2; // xx12
n <<= 8; // x12x
n |= s3; // x123

//-------------- get four 6-bit values for lookups
BYTE m4= n & 0x3f; n >>= 6;
BYTE m3= n & 0x3f; n >>= 6;
BYTE m2= n & 0x3f; n >>= 6;
BYTE m1= n & 0x3f;

//------------------ lookup the right digits for output
BYTE b1 = Base64Digits[m1];
BYTE b2 = Base64Digits[m2];
BYTE b3 = Base64Digits[m3];
BYTE b4 = Base64Digits[m4];

//--------- end of input handling
*pDst++ = b1;
*pDst++ = b2;
if ( nLenSrc >= 3 ) { // 24 src bits left to encode, output xxxx
*pDst++ = b3;
*pDst++ = b4;
}
if ( nLenSrc == 2 ) { // 16 src bits left to encode, output xxx=
*pDst++ = b3;
*pDst++ = '=';
}
if ( nLenSrc == 1 ) { // 8 src bits left to encode, output xx==
*pDst++ = '=';
*pDst++ = '=';
}
pSrc += 3;
nLenSrc -= 3;
nLenOut += 4;
}
// Could optionally append a NULL byte like so:
*pDst++= 0; nLenOut++;
return( nLenOut );
}

int main(int argc, char* argv[])
{
std::vector<char> mymsg;
mymsg = readBytes(argv[1]);
char* arr = &mymsg[0];
int len = mymsg.size();
int lendst = ((len+2)/3)*4;
unsigned char* uarr = (unsigned char *) malloc(len*sizeof(unsigned char));
char* dst = (char *) malloc(lendst*sizeof(char));;
mymsg.clear(); //free()

// convert to unsigned char
strncpy((char*)uarr, arr, len);

int lenOut = ToBase64Simple(uarr, len, dst, lendst);
free(uarr);

int cont = 0;
while (cont < lenOut) //(dst[cont] != 0)
cout << dst[cont++];
cout << "\n";
}

欢迎任何见解。

最佳答案

我看到两个问题。

首先,您要在使用完 mymsg vector 之前清除它。这使得 arr 指针悬空(指向不再分配的内存)。当您访问 arr 以获取数据时,您最终会遇到未定义的行为。

然后您使用strncpy 来复制(可能)二进制数据。此复制将在到达文件中的第一个 nul (0) 字节时停止,因此不会复制您的所有数据。您应该改用 memcpy

关于c++ - C++ Base64图像文件编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com