From 1d58af5082916fece3c6f929aa38c0ba0b9bd0c8 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sat, 3 Jan 2015 21:35:20 +0000 Subject: [PATCH] Increase BASE64 encoding size when using system libb64 Remove BASE64 reference testing as it's only libb64 now. Improve the test to ignore \r and \n when comparing BASE-encoded strings to not fail on system (unpatched) libb64. --- configure.ac | 2 +- libtransmission/CMakeLists.txt | 4 ++ libtransmission/crypto-test.c | 70 ++++++++++------------------------ libtransmission/crypto-utils.c | 13 +++++-- third-party/libb64/cencode.c | 12 ++++-- 5 files changed, 44 insertions(+), 57 deletions(-) diff --git a/configure.ac b/configure.ac index 5afd03fbc..557e92ba5 100644 --- a/configure.ac +++ b/configure.ac @@ -229,7 +229,7 @@ AC_ARG_ENABLE([external-b64], [want_external_b64=${enableval}], [want_external_b64=no]) if test "x$want_external_b64" != "xno" ; then - LIBB64_CFLAGS="" + LIBB64_CFLAGS="-DUSE_SYSTEM_B64" LIBB64_LIBS="-lb64" LIBB64_LIBS_QT="-lb64" build_bundled_b64="no" diff --git a/libtransmission/CMakeLists.txt b/libtransmission/CMakeLists.txt index 853bb3220..6f9760c9b 100644 --- a/libtransmission/CMakeLists.txt +++ b/libtransmission/CMakeLists.txt @@ -159,6 +159,10 @@ if(MINIUPNPC_VERSION VERSION_LESS 1.7) add_definitions(-DMINIUPNPC_API_VERSION=${MINIUPNPC_API_VERSION}) endif() +if(USE_SYSTEM_B64) + add_definitions(-DUSE_SYSTEM_B64) +endif() + include_directories( ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} diff --git a/libtransmission/crypto-test.c b/libtransmission/crypto-test.c index 90eeb4b4c..05296dfd5 100644 --- a/libtransmission/crypto-test.c +++ b/libtransmission/crypto-test.c @@ -193,6 +193,23 @@ test_random (void) return 0; } +static bool +base64_eq (const char * a, + const char * b) +{ + for (; ; ++a, ++b) + { + while (*a == '\r' || *a == '\n') + ++a; + while (*b == '\r' || *b == '\n') + ++b; + if (*a == '\0' || *b == '\0' || *a != *b) + break; + } + + return *a == *b; +} + static int test_base64 (void) { @@ -201,17 +218,8 @@ test_base64 (void) int i; out = tr_base64_encode_str ("YOYO!", &len); - check_int_eq (8, len); - check_streq ("WU9ZTyE=", out); - in = tr_base64_decode_str_ (out, &len); - check_int_eq (5, len); - check_streq ("YOYO!", in); - tr_free (in); - tr_free (out); - - out = tr_base64_encode_str_ ("YOYO!", &len); - check_int_eq (8, len); - check_streq ("WU9ZTyE=", out); + check_int_eq (strlen (out), len); + check (base64_eq ("WU9ZTyE=", out)); in = tr_base64_decode_str (out, &len); check_int_eq (5, len); check_streq ("YOYO!", in); @@ -222,15 +230,6 @@ test_base64 (void) check_int_eq (0, len); check_streq ("", out); tr_free (out); - out = tr_base64_decode_ ("", 0, &len); - check_int_eq (0, len); - check_streq ("", out); - tr_free (out); - - out = tr_base64_encode_ ("", 0, &len); - check_int_eq (0, len); - check_streq ("", out); - tr_free (out); out = tr_base64_decode ("", 0, &len); check_int_eq (0, len); check_streq ("", out); @@ -239,20 +238,9 @@ test_base64 (void) out = tr_base64_encode (NULL, 0, &len); check_int_eq (0, len); check (out == NULL); - tr_free (out); - out = tr_base64_decode_ (NULL, 0, &len); - check_int_eq (0, len); - check (out == NULL); - tr_free (out); - - out = tr_base64_encode_ (NULL, 0, &len); - check_int_eq (0, len); - check (out == NULL); - tr_free (out); out = tr_base64_decode (NULL, 0, &len); check_int_eq (0, len); check (out == NULL); - tr_free (out); #define MAX_BUF_SIZE 1024 @@ -265,15 +253,7 @@ test_base64 (void) buf[j] = tr_rand_int_weak (256); out = tr_base64_encode (buf, j, &len); - check_int_eq ((j + 2) / 3 * 4, len); - in = tr_base64_decode_ (out, len, &len); - check_int_eq (j, len); - check (memcmp (in, buf, len) == 0); - tr_free (in); - tr_free (out); - - out = tr_base64_encode_ (buf, j, &len); - check_int_eq ((j + 2) / 3 * 4, len); + check_int_eq (strlen (out), len); in = tr_base64_decode (out, len, &len); check_int_eq (j, len); check (memcmp (in, buf, len) == 0); @@ -285,15 +265,7 @@ test_base64 (void) buf[j] = '\0'; out = tr_base64_encode_str (buf, &len); - check_int_eq ((j + 2) / 3 * 4, len); - in = tr_base64_decode_str_ (out, &len); - check_int_eq (j, len); - check_streq (in, buf); - tr_free (in); - tr_free (out); - - out = tr_base64_encode_str_ (buf, &len); - check_int_eq ((j + 2) / 3 * 4, len); + check_int_eq (strlen (out), len); in = tr_base64_decode_str (out, &len); check_int_eq (j, len); check_streq (in, buf); diff --git a/libtransmission/crypto-utils.c b/libtransmission/crypto-utils.c index cb31bf7e7..b850d1bb4 100644 --- a/libtransmission/crypto-utils.c +++ b/libtransmission/crypto-utils.c @@ -200,10 +200,15 @@ tr_base64_encode (const void * input, { if (input_length != 0) { - size_t ret_length; + size_t ret_length = 4 * ((input_length + 2) / 3); base64_encodestate state; - ret = tr_new (char, 4 * ((input_length + 2) / 3) + 1); +#ifdef USE_SYSTEM_B64 + /* Additional space is needed for newlines if we're using unpatched libb64 */ + ret_length += ret_length / 72 + 1; +#endif + + ret = tr_new (char, ret_length + 1); base64_init_encodestate (&state); ret_length = base64_encode_block (input, input_length, ret, &state); @@ -248,10 +253,10 @@ tr_base64_decode (const void * input, { if (input_length != 0) { - size_t ret_length; + size_t ret_length = input_length / 4 * 3; base64_decodestate state; - ret = tr_new (char, input_length / 4 * 3 + 1); + ret = tr_new (char, ret_length + 1); base64_init_decodestate (&state); ret_length = base64_decode_block (input, input_length, ret, &state); diff --git a/third-party/libb64/cencode.c b/third-party/libb64/cencode.c index acd976769..de3902f1b 100644 --- a/third-party/libb64/cencode.c +++ b/third-party/libb64/cencode.c @@ -7,7 +7,9 @@ For details, see http://sourceforge.net/projects/libb64 #include +/* const int CHARS_PER_LINE = 72; +*/ void base64_init_encodestate(base64_encodestate* state_in) { @@ -72,12 +74,14 @@ int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, result = (fragment & 0x03f) >> 0; *codechar++ = base64_encode_value(result); - /* ++(state_in->stepcount); + /* + ++(state_in->stepcount); if (state_in->stepcount == CHARS_PER_LINE/4) { *codechar++ = '\n'; state_in->stepcount = 0; - } */ + } + */ } } /* control should not reach here */ @@ -102,7 +106,9 @@ int base64_encode_blockend(char* code_out, base64_encodestate* state_in) case step_A: break; } - /* *codechar++ = '\n'; */ + /* + *codechar++ = '\n'; + */ return codechar - code_out; } -- 2.40.0