base64: break lines

This commit is contained in:
Mirek Kratochvil 2013-01-13 12:54:16 +01:00
parent 94af9472e8
commit 49710e16be

View file

@ -26,7 +26,7 @@ void base64_encode (const std::string& in, std::string&out, int cols)
int acc = 0, accbits = 0, idx = 0, idxmax = in.length(), col = 0; int acc = 0, accbits = 0, idx = 0, idxmax = in.length(), col = 0;
out.clear(); out.clear();
out.reserve (idxmax + (4 * idxmax / 10) ); out.reserve (idxmax + (2 * idxmax / 5) ); //reserve around 140%
while (idx < idxmax) { while (idx < idxmax) {
if (accbits < 6) { if (accbits < 6) {
acc = (acc << 8) | in[idx++]; acc = (acc << 8) | in[idx++];
@ -35,6 +35,11 @@ void base64_encode (const std::string& in, std::string&out, int cols)
while (accbits >= 6) { while (accbits >= 6) {
accbits -= 6; accbits -= 6;
out.push_back (b64str[ (acc >> accbits) & 0x3f]); out.push_back (b64str[ (acc >> accbits) & 0x3f]);
if (cols && ( (++col) >= cols) ) {
out.push_back ('\n');
col = 0;
}
} }
} }
if (accbits) { if (accbits) {