diff options
author | Nick Mathewson <nickm@torproject.org> | 2020-07-30 14:27:29 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2020-07-30 14:27:29 -0400 |
commit | c2d5ec5e43c2690656ea5a5a384683108782d149 (patch) | |
tree | 0f9f9cd201c2d970170e98a5e94d713737a2e234 /src/lib/encoding/binascii.c | |
parent | c4742b89b23d58958ee0d5ca324dac5948c94bf6 (diff) | |
parent | cdb0e6c2522aac372c9a816300dacfd62856dd48 (diff) | |
download | tor-c2d5ec5e43c2690656ea5a5a384683108782d149.tar.gz tor-c2d5ec5e43c2690656ea5a5a384683108782d149.zip |
Merge branch 'maint-0.4.2' into bug40076_042
Diffstat (limited to 'src/lib/encoding/binascii.c')
-rw-r--r-- | src/lib/encoding/binascii.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/lib/encoding/binascii.c b/src/lib/encoding/binascii.c index bd063440d6..fc64e014e7 100644 --- a/src/lib/encoding/binascii.c +++ b/src/lib/encoding/binascii.c @@ -84,7 +84,7 @@ base32_encode(char *dest, size_t destlen, const char *src, size_t srclen) } /** Implements base32 decoding as in RFC 4648. - * Returns 0 if successful, -1 otherwise. + * Return the number of bytes decoded if successful; -1 otherwise. */ int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen) @@ -147,7 +147,7 @@ base32_decode(char *dest, size_t destlen, const char *src, size_t srclen) memset(tmp, 0, srclen); /* on the heap, this should be safe */ tor_free(tmp); tmp = NULL; - return 0; + return i; } #define BASE64_OPENSSL_LINELEN 64 @@ -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', @@ -309,8 +321,10 @@ base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, return (int) enclen; } -/** As base64_encode, but do not add any internal spaces or external padding - * to the output stream. */ +/** As base64_encode, but do not add any internal spaces, and remove external + * padding from the output stream. + * dest must be at least base64_encode_size(srclen, 0), including space for + * the removed external padding. */ int base64_encode_nopad(char *dest, size_t destlen, const uint8_t *src, size_t srclen) |