Loading src/lib/encoding/binascii.c +12 −0 Original line number Diff line number Diff line Loading @@ -179,6 +179,18 @@ base64_encode_size(size_t srclen, int flags) return enclen; } /** Return an upper bound on the number of bytes that might be needed to hold * the data from decoding the base64 string <b>srclen</b>. This is only an * upper bound, since some part of the base64 string might be padding or * space. */ size_t base64_decode_maxsize(size_t srclen) { tor_assert(srclen < INT_MAX / 3); return CEIL_DIV(srclen * 3, 4); } /** Internal table mapping 6 bit values to the Base64 alphabet. */ static const char base64_encode_table[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', Loading src/lib/encoding/binascii.h +1 −0 Original line number Diff line number Diff line Loading @@ -42,6 +42,7 @@ const char *hex_str(const char *from, size_t fromlen); #define BASE64_ENCODE_MULTILINE 1 size_t base64_encode_size(size_t srclen, int flags); size_t base64_decode_maxsize(size_t srclen); int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags); int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen); Loading src/test/test_util_format.c +3 −1 Original line number Diff line number Diff line Loading @@ -392,10 +392,13 @@ test_util_format_encoded_size(void *arg) base64_encode(outbuf, sizeof(outbuf), (char *)inbuf, i, 0); tt_int_op(strlen(outbuf), OP_EQ, base64_encode_size(i, 0)); tt_int_op(i, OP_LE, base64_decode_maxsize(strlen(outbuf))); base64_encode(outbuf, sizeof(outbuf), (char *)inbuf, i, BASE64_ENCODE_MULTILINE); tt_int_op(strlen(outbuf), OP_EQ, base64_encode_size(i, BASE64_ENCODE_MULTILINE)); tt_int_op(i, OP_LE, base64_decode_maxsize(strlen(outbuf))); } done: Loading @@ -417,4 +420,3 @@ struct testcase_t util_format_tests[] = { { "encoded_size", test_util_format_encoded_size, 0, NULL, NULL }, END_OF_TESTCASES }; Loading
src/lib/encoding/binascii.c +12 −0 Original line number Diff line number Diff line Loading @@ -179,6 +179,18 @@ base64_encode_size(size_t srclen, int flags) return enclen; } /** Return an upper bound on the number of bytes that might be needed to hold * the data from decoding the base64 string <b>srclen</b>. This is only an * upper bound, since some part of the base64 string might be padding or * space. */ size_t base64_decode_maxsize(size_t srclen) { tor_assert(srclen < INT_MAX / 3); return CEIL_DIV(srclen * 3, 4); } /** Internal table mapping 6 bit values to the Base64 alphabet. */ static const char base64_encode_table[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', Loading
src/lib/encoding/binascii.h +1 −0 Original line number Diff line number Diff line Loading @@ -42,6 +42,7 @@ const char *hex_str(const char *from, size_t fromlen); #define BASE64_ENCODE_MULTILINE 1 size_t base64_encode_size(size_t srclen, int flags); size_t base64_decode_maxsize(size_t srclen); int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags); int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen); Loading
src/test/test_util_format.c +3 −1 Original line number Diff line number Diff line Loading @@ -392,10 +392,13 @@ test_util_format_encoded_size(void *arg) base64_encode(outbuf, sizeof(outbuf), (char *)inbuf, i, 0); tt_int_op(strlen(outbuf), OP_EQ, base64_encode_size(i, 0)); tt_int_op(i, OP_LE, base64_decode_maxsize(strlen(outbuf))); base64_encode(outbuf, sizeof(outbuf), (char *)inbuf, i, BASE64_ENCODE_MULTILINE); tt_int_op(strlen(outbuf), OP_EQ, base64_encode_size(i, BASE64_ENCODE_MULTILINE)); tt_int_op(i, OP_LE, base64_decode_maxsize(strlen(outbuf))); } done: Loading @@ -417,4 +420,3 @@ struct testcase_t util_format_tests[] = { { "encoded_size", test_util_format_encoded_size, 0, NULL, NULL }, END_OF_TESTCASES };