diff options
author | lloyd <[email protected]> | 2011-05-16 16:28:54 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2011-05-16 16:28:54 +0000 |
commit | 3c0e96e6555b0e658684e55f4c23ca21fd6f3a19 (patch) | |
tree | 732cb39ec0c6647d35395ef1890c3d212c7c9258 /src/codec | |
parent | 9e8743ee1d84eb196628ad687b9a87da5d2663a2 (diff) |
Fixes for base64 incremental decoding.
Define the Base64_Decoder filter in terms of base64_decode
Don't use locked memory in the hex or base64 filters.
Diffstat (limited to 'src/codec')
-rw-r--r-- | src/codec/base64/base64.cpp | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/src/codec/base64/base64.cpp b/src/codec/base64/base64.cpp index 27e6853bd..4e3a84dc7 100644 --- a/src/codec/base64/base64.cpp +++ b/src/codec/base64/base64.cpp @@ -140,7 +140,6 @@ size_t base64_decode(byte output[], byte decode_buf[4]; size_t decode_buf_pos = 0; size_t final_truncate = 0; - bool seen_pad = false; clear_mem(output, input_length * 3 / 4); @@ -148,19 +147,13 @@ size_t base64_decode(byte output[], { const byte bin = BASE64_TO_BIN[static_cast<byte>(input[i])]; - if(seen_pad && bin != 0x81) - throw std::invalid_argument("base64_decode: invalid padding"); - if(bin <= 0x3F) { decode_buf[decode_buf_pos] = bin; decode_buf_pos += 1; } - else if(!(final_inputs && bin == 0x81)) + else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) { - if(bin == 0x80 && ignore_ws) - continue; - std::string bad_char(1, input[i]); if(bad_char == "\t") bad_char = "\\t"; @@ -173,12 +166,10 @@ size_t base64_decode(byte output[], } /* - * If we either see a pad character, or we are the the end - * of the input + * If we're at the end of the input, pad with 0s and truncate */ - if(final_inputs && (bin == 0x81 || i == input_length - 1)) + if(final_inputs && (i == input_length - 1)) { - seen_pad = true; if(decode_buf_pos) { for(size_t i = decode_buf_pos; i != 4; ++i) @@ -196,10 +187,16 @@ size_t base64_decode(byte output[], out_ptr += 3; decode_buf_pos = 0; + input_consumed = i+1; } } - input_consumed = input_length - decode_buf_pos; + while(input_consumed < input_length && + BASE64_TO_BIN[static_cast<byte>(input[input_consumed])] == 0x80) + { + ++input_consumed; + } + size_t written = (out_ptr - output) - final_truncate; return written; @@ -231,7 +228,7 @@ SecureVector<byte> base64_decode(const char input[], size_t input_length, bool ignore_ws) { - SecureVector<byte> bin(1 + (input_length * 3) / 4); + SecureVector<byte> bin((round_up<size_t>(input_length, 4) * 3) / 4); size_t written = base64_decode(&bin[0], input, |