使用openssl BIO逐块解码base64
base64 decode with openssl BIO block by block
我在使用以下代码解码 base64 编码数据时遇到问题。我有大约 3.5 MB 的数据要解码。它几乎可以正常工作,直到最后缺少某些东西。
以下代码读取100kB的源数据块,写入BIO_s_mem,然后多次读取1024字节的解码数据。当没有更多内容可读取时,将检索并再次使用另一个 100 kB 块。在收到最后一个块之前一切正常。它没有完整的 100 kB(只有 25685 字节)。我将它写入 BIO,得到 18 次解码 1024 字节,然后是另一个 435 字节,仅此而已。但这还不是全部。仍然缺少大约 100 字节的解码数据。
所有编码数据都被正确接收(将它们保存到文件并从 shell 命令手动解码给出完整输出)。我应该如何从 BIO 中获取其余部分?我想我应该以某种方式说这就是全部。例如,我尝试了 BIO_flush 多种方式,一些标志,向编码数据添加换行符,但还没有成功。
BIO *bmem, *b64;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
BIO_push(b64, bmem);
vector<char> buf(1024);
while (!end) {
end = task->nextChunk(chunk, chunkSize); // this reads encoded data block
BIO_write(bmem, (void*)chunk, chunkSize);
cout << "Chunk size: " << chunkSize << endl;
if (end || chunkSize == 0) {
BIO_flush(bmem); // I tried multiple things
BIO_flush(b64); // flush here and there, nothing helped
BIO_set_close(bmem, BIO_CLOSE); // and other things... ;-)
}
string result;
int nread;
while ((nread = BIO_read(b64, buf.data(), buf.size())) > 0) {
cout << "Decoded bytes read: " << nread << endl;
result.append(buf.data(), nread);
}
ret->appendRaw(result.c_str(), result.size()); // store result somehow
}
最后一个输出序列:
Chunk size: 25685
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 435
Chunk size: 0
所以再说一次:解码后的输出是二进制文件。它已正确编码,已正确发送到我的代码,但未解码,最后大约 100 个字节丢失。
感谢您的任何提示。
诀窍是在最后的 BIO_read 序列之前添加以下两行:
BIO_write(bmem, "\n", 1);
BIO_set_mem_eof_return(bmem, BIO_NOCLOSE);
我在使用以下代码解码 base64 编码数据时遇到问题。我有大约 3.5 MB 的数据要解码。它几乎可以正常工作,直到最后缺少某些东西。
以下代码读取100kB的源数据块,写入BIO_s_mem,然后多次读取1024字节的解码数据。当没有更多内容可读取时,将检索并再次使用另一个 100 kB 块。在收到最后一个块之前一切正常。它没有完整的 100 kB(只有 25685 字节)。我将它写入 BIO,得到 18 次解码 1024 字节,然后是另一个 435 字节,仅此而已。但这还不是全部。仍然缺少大约 100 字节的解码数据。
所有编码数据都被正确接收(将它们保存到文件并从 shell 命令手动解码给出完整输出)。我应该如何从 BIO 中获取其余部分?我想我应该以某种方式说这就是全部。例如,我尝试了 BIO_flush 多种方式,一些标志,向编码数据添加换行符,但还没有成功。
BIO *bmem, *b64;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
BIO_push(b64, bmem);
vector<char> buf(1024);
while (!end) {
end = task->nextChunk(chunk, chunkSize); // this reads encoded data block
BIO_write(bmem, (void*)chunk, chunkSize);
cout << "Chunk size: " << chunkSize << endl;
if (end || chunkSize == 0) {
BIO_flush(bmem); // I tried multiple things
BIO_flush(b64); // flush here and there, nothing helped
BIO_set_close(bmem, BIO_CLOSE); // and other things... ;-)
}
string result;
int nread;
while ((nread = BIO_read(b64, buf.data(), buf.size())) > 0) {
cout << "Decoded bytes read: " << nread << endl;
result.append(buf.data(), nread);
}
ret->appendRaw(result.c_str(), result.size()); // store result somehow
}
最后一个输出序列:
Chunk size: 25685
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 1024
Decoded bytes read: 435
Chunk size: 0
所以再说一次:解码后的输出是二进制文件。它已正确编码,已正确发送到我的代码,但未解码,最后大约 100 个字节丢失。 感谢您的任何提示。
诀窍是在最后的 BIO_read 序列之前添加以下两行:
BIO_write(bmem, "\n", 1);
BIO_set_mem_eof_return(bmem, BIO_NOCLOSE);