From 8b73c4041bdb66e323200ce2c299b16614dc4c9b Mon Sep 17 00:00:00 2001 From: Jordan Cristiano Date: Sun, 21 Jun 2015 02:21:33 -0400 Subject: [PATCH] Added calculation for encoded output size based on input size. Calculated size is never more than a byte larger than the actual size. --- external/libb64-1.2.1/include/b64/cencode.h | 1 + external/libb64-1.2.1/src/cencode.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/external/libb64-1.2.1/include/b64/cencode.h b/external/libb64-1.2.1/include/b64/cencode.h index 3db2c61..39e279c 100644 --- a/external/libb64-1.2.1/include/b64/cencode.h +++ b/external/libb64-1.2.1/include/b64/cencode.h @@ -28,6 +28,7 @@ typedef struct void base64_init_encodestate(base64_encodestate* state_in); +int base64_calc_buffer_length(int length_in, const base64_encodestate* state_in); int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in); diff --git a/external/libb64-1.2.1/src/cencode.c b/external/libb64-1.2.1/src/cencode.c index 4cc0cdc..5b4988a 100644 --- a/external/libb64-1.2.1/src/cencode.c +++ b/external/libb64-1.2.1/src/cencode.c @@ -24,6 +24,19 @@ char base64_encode_value(char value_in) return encoding[(int)value_in]; } +/* + Output_size calculation from: + http://stackoverflow.com/questions/1533113/calculate-the-size-to-a-base-64-encoded-message + Never more than a byte larger than the actual encoded size. + Guaranteed equal to or larger than the actual encoded size. +*/ +int base64_calc_buffer_length(int length_in, const base64_encodestate* state_in) +{ + const int blockend_newline = ((BASE64_ENC_NO_NEWLINE_TERM & state_in->flags) == 0); + const int output_length = ((((length_in - 1) / 3) * 4) + 4); + return output_length + blockend_newline + (output_length / CHARS_PER_LINE); +} + int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) { int stepcount = state_in->stepcount;