Avoid a data copy decoding base64 data.

Use an out parameter to avoid a buffer alloc/copy/free cycle
when returning base64-decoded data.
This commit is contained in:
James Turner
2013-10-09 10:03:30 +02:00
parent 2f023803e7
commit 23140e3bf7
3 changed files with 14 additions and 16 deletions
+10 -9
View File
@@ -165,7 +165,7 @@ namespace {
_ptr = p; _ptr = p;
} }
bool apply(std::vector<char>& output, std::istream& source) bool apply(std::vector<unsigned char>& output, std::istream& source)
{ {
unsigned char* pEnd = _ptr + instructionLength; unsigned char* pEnd = _ptr + instructionLength;
unsigned char* newData = pEnd; unsigned char* newData = pEnd;
@@ -330,11 +330,12 @@ public:
} }
bool decodeTextDelta(const SGPath& outputPath) bool decodeTextDelta(const SGPath& outputPath)
{ {
string decoded = strutils::decodeBase64(txDeltaData); std::vector<unsigned char> output, decoded;
strutils::decodeBase64(txDeltaData, decoded);
size_t bytesToDecode = decoded.size(); size_t bytesToDecode = decoded.size();
std::vector<char> output;
unsigned char* p = (unsigned char*) decoded.data(); unsigned char* p = decoded.data();
if (memcmp(p, "SVN\0", DELTA_HEADER_SIZE) != 0) { if (memcmp(p, "SVN\0", DELTA_HEADER_SIZE) != 0) {
return false; // bad header return false; // bad header
} }
@@ -346,9 +347,9 @@ public:
while (bytesToDecode > 0) { while (bytesToDecode > 0) {
if (!SVNDeltaWindow::isWindowComplete(p, bytesToDecode)) { if (!SVNDeltaWindow::isWindowComplete(p, bytesToDecode)) {
SG_LOG(SG_IO, SG_WARN, "SVN txdelta broken window"); SG_LOG(SG_IO, SG_WARN, "SVN txdelta broken window");
return false; return false;
} }
SVNDeltaWindow window(p); SVNDeltaWindow window(p);
assert(bytesToDecode >= window.size()); assert(bytesToDecode >= window.size());
@@ -362,7 +363,7 @@ public:
std::ofstream f; std::ofstream f;
f.open(outputPath.c_str(), f.open(outputPath.c_str(),
std::ios::out | std::ios::trunc | std::ios::binary); std::ios::out | std::ios::trunc | std::ios::binary);
f.write(output.data(), output.size()); f.write((char*) output.data(), output.size());
// compute MD5 while we have the file in memory // compute MD5 while we have the file in memory
memset(&md5Context, 0, sizeof(SG_MD5_CTX)); memset(&md5Context, 0, sizeof(SG_MD5_CTX));
+3 -6
View File
@@ -370,14 +370,13 @@ static bool is_whitespace(unsigned char c) {
return ((c == ' ') || (c == '\r') || (c == '\n')); return ((c == ' ') || (c == '\r') || (c == '\n'));
} }
std::string decodeBase64(const std::string& encoded_string) void decodeBase64(const std::string& encoded_string, std::vector<unsigned char>& ret)
{ {
int in_len = encoded_string.size(); int in_len = encoded_string.size();
int i = 0; int i = 0;
int j = 0; int j = 0;
int in_ = 0; int in_ = 0;
unsigned char char_array_4[4], char_array_3[3]; unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=')) { while (in_len-- && ( encoded_string[in_] != '=')) {
if (is_whitespace( encoded_string[in_])) { if (is_whitespace( encoded_string[in_])) {
@@ -399,7 +398,7 @@ std::string decodeBase64(const std::string& encoded_string)
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++) for (i = 0; (i < 3); i++)
ret += char_array_3[i]; ret.push_back(char_array_3[i]);
i = 0; i = 0;
} }
} }
@@ -415,10 +414,8 @@ std::string decodeBase64(const std::string& encoded_string)
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 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]; char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
} }
return ret;
} }
const char hexChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; const char hexChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+1 -1
View File
@@ -160,7 +160,7 @@ namespace simgear {
* NULs). Throws an exception if input data is not base64, or is * NULs). Throws an exception if input data is not base64, or is
* malformed * malformed
*/ */
std::string decodeBase64(const std::string& a); void decodeBase64(const std::string& a, std::vector<unsigned char>& output);
/** /**
* convert bytes to hexadecimal equivalent * convert bytes to hexadecimal equivalent