X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=src%2Fsrc%2Fbase64.c;h=e63522ec4d5bc4a002bc8ca84fcd40a63339ddc4;hb=56cc2ec0010e6c917a8f1ba285c86e6dadb6f0d3;hp=ca6466bf22d4ed77f7c05ac15e5e3d3d87f136c1;hpb=3c51463e961cb96df8f624420f833c65505bb451;p=exim.git diff --git a/src/src/base64.c b/src/src/base64.c index ca6466bf2..e63522ec4 100644 --- a/src/src/base64.c +++ b/src/src/base64.c @@ -5,7 +5,7 @@ /* Copyright (c) Tom Kistner 2004, 2015 */ /* License: GPL */ -/* Copyright (c) University of Cambridge 1995 - 2015 */ +/* Copyright (c) University of Cambridge 1995 - 2018 */ /* See the file NOTICE for conditions of use and distribution. */ @@ -129,7 +129,7 @@ compact loop is messy and would probably run more slowly. Arguments: code points to the coded string, zero-terminated ptr where to put the pointer to the result, which is in - dynamic store, and zero-terminated + allocated store, and zero-terminated Returns: the number of bytes in the result, or -1 if the input was malformed @@ -150,12 +150,16 @@ static uschar dec64table[] = { }; int -b64decode(uschar *code, uschar **ptr) +b64decode(const uschar *code, uschar **ptr) { + int x, y; -uschar *result = store_get(3*(Ustrlen(code)/4) + 1); +uschar *result; -*ptr = result; +{ + int l = Ustrlen(code); + *ptr = result = store_get(1 + l/4 * 3 + l%4); +} /* Each cycle of the loop handles a quantum of 4 input bytes. For the last quantum this may decode to 1, 2, or 3 output bytes. */ @@ -163,35 +167,50 @@ quantum this may decode to 1, 2, or 3 output bytes. */ while ((x = *code++) != 0) { if (isspace(x)) continue; + /* debug_printf("b64d: '%c'\n", x); */ if (x > 127 || (x = dec64table[x]) == 255) return -1; while (isspace(y = *code++)) ; - if (y == 0 || (y = dec64table[y]) == 255) + /* debug_printf("b64d: '%c'\n", y); */ + if (y > 127 || (y = dec64table[y]) == 255) return -1; *result++ = (x << 2) | (y >> 4); + /* debug_printf("b64d: -> %02x\n", result[-1]); */ while (isspace(x = *code++)) ; - if (x == '=') + /* debug_printf("b64d: '%c'\n", x); */ + if (x == '=') /* endmarker, but there should be another */ { while (isspace(x = *code++)) ; - if (x != '=' || *code != 0) return -1; + /* debug_printf("b64d: '%c'\n", x); */ + if (x != '=') return -1; + while (isspace(y = *code++)) ; + if (y != 0) return -1; + /* debug_printf("b64d: DONE\n"); */ + break; } else { if (x > 127 || (x = dec64table[x]) == 255) return -1; *result++ = (y << 4) | (x >> 2); + /* debug_printf("b64d: -> %02x\n", result[-1]); */ while (isspace(y = *code++)) ; + /* debug_printf("b64d: '%c'\n", y); */ if (y == '=') { - if (*code != 0) return -1; + while (isspace(y = *code++)) ; + if (y != 0) return -1; + /* debug_printf("b64d: DONE\n"); */ + break; } else { if (y > 127 || (y = dec64table[y]) == 255) return -1; *result++ = (x << 6) | y; + /* debug_printf("b64d: -> %02x\n", result[-1]); */ } } } @@ -201,23 +220,6 @@ return result - *ptr; } -/************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - ************************************************* - *************************************************/ - /************************************************* * Encode byte-string in base 64 * *************************************************/ @@ -247,7 +249,7 @@ uschar *p = code; while (len-- >0) { - register int x, y; + int x, y; x = *clear++; *p++ = enc64table[(x >> 2) & 63];