diff options
author | Jack Lloyd <[email protected]> | 2017-02-05 14:36:56 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-02-05 14:36:56 -0500 |
commit | 147e50f1a5f1058871a77a7aeacde140c5fe4cdf (patch) | |
tree | 32233fc4866154dad9831c752c162a7edd982211 /src | |
parent | 149a1ba8a4eeaf2613c1058a678dc3173a7ad047 (diff) | |
parent | 8b9660ebced19b93afce347937772d3a74530e41 (diff) |
Merge GH #868 Add base64 size helper functions
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/codec/base64/base64.cpp | 13 | ||||
-rw-r--r-- | src/lib/codec/base64/base64.h | 22 |
2 files changed, 29 insertions, 6 deletions
diff --git a/src/lib/codec/base64/base64.cpp b/src/lib/codec/base64/base64.cpp index c854e52b0..88bbcff3a 100644 --- a/src/lib/codec/base64/base64.cpp +++ b/src/lib/codec/base64/base64.cpp @@ -78,7 +78,7 @@ size_t base64_encode(char out[], std::string base64_encode(const uint8_t input[], size_t input_length) { - const size_t output_length = (round_up(input_length, 3) / 3) * 4; + const size_t output_length = base64_encode_max_output(input_length); std::string output(output_length, 0); size_t consumed = 0; @@ -230,7 +230,7 @@ secure_vector<uint8_t> base64_decode(const char input[], size_t input_length, bool ignore_ws) { - const size_t output_length = (round_up(input_length, 4) * 3) / 4; + const size_t output_length = base64_decode_max_output(input_length); secure_vector<uint8_t> bin(output_length); size_t written = base64_decode(bin.data(), @@ -248,5 +248,14 @@ secure_vector<uint8_t> base64_decode(const std::string& input, return base64_decode(input.data(), input.size(), ignore_ws); } +size_t base64_encode_max_output(size_t input_length) + { + return (round_up(input_length, 3) / 3) * 4; + } + +size_t base64_decode_max_output(size_t input_length) + { + return (round_up(input_length, 4) * 3) / 4; + } } diff --git a/src/lib/codec/base64/base64.h b/src/lib/codec/base64/base64.h index 0e78ea3c2..70d806ee8 100644 --- a/src/lib/codec/base64/base64.h +++ b/src/lib/codec/base64/base64.h @@ -15,7 +15,7 @@ namespace Botan { /** * Perform base64 encoding -* @param output an array of at least input_length*4/3 bytes +* @param output an array of at least base64_encode_max_output bytes * @param input is some binary data * @param input_length length of input in bytes * @param input_consumed is an output parameter which says how many @@ -54,7 +54,7 @@ std::string base64_encode(const std::vector<uint8_t, Alloc>& input) /** * Perform base64 decoding -* @param output an array of at least input_length*3/4 bytes +* @param output an array of at least base64_decode_max_output bytes * @param input some base64 input * @param input_length length of input in bytes * @param input_consumed is an output parameter which says how many @@ -76,7 +76,7 @@ size_t BOTAN_DLL base64_decode(uint8_t output[], /** * Perform base64 decoding -* @param output an array of at least input_length*3/4 bytes +* @param output an array of at least base64_decode_max_output bytes * @param input some base64 input * @param input_length length of input in bytes * @param ignore_ws ignore whitespace on input; if false, throw an @@ -90,7 +90,7 @@ size_t BOTAN_DLL base64_decode(uint8_t output[], /** * Perform base64 decoding -* @param output an array of at least input_length/3*4 bytes +* @param output an array of at least base64_decode_max_output bytes * @param input some base64 input * @param ignore_ws ignore whitespace on input; if false, throw an exception if whitespace is encountered @@ -122,6 +122,20 @@ secure_vector<uint8_t> BOTAN_DLL base64_decode(const char input[], secure_vector<uint8_t> BOTAN_DLL base64_decode(const std::string& input, bool ignore_ws = true); +/** +* Calculate the size of output buffer for base64_encode +* @param input_length the length of input in bytes +* @return the size of output buffer in bytes +*/ +size_t BOTAN_DLL base64_encode_max_output(size_t input_length); + +/** +* Calculate the size of output buffer for base64_decode +* @param input_length the length of input in bytes +* @return the size of output buffer in bytes +*/ +size_t BOTAN_DLL base64_decode_max_output(size_t input_length); + } #endif |