From 224fa2a8db5adfd75e2652afd50ba7517d1b9493 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Tue, 26 Oct 2010 23:53:28 +0000 Subject: Fixed #1152 (The base64 decoder should ignore whitespaces in the input). In fact, the base64 decoder now will silently ignore/skip any bad characters. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3356 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib-util/src/pjlib-util-test/encryption.c | 103 ++++++++++++++++++---------- pjlib-util/src/pjlib-util/base64.c | 58 +++++++--------- 2 files changed, 91 insertions(+), 70 deletions(-) (limited to 'pjlib-util') diff --git a/pjlib-util/src/pjlib-util-test/encryption.c b/pjlib-util/src/pjlib-util-test/encryption.c index 3db0d9a0..b0d3d6a8 100644 --- a/pjlib-util/src/pjlib-util-test/encryption.c +++ b/pjlib-util/src/pjlib-util-test/encryption.c @@ -494,6 +494,12 @@ static int crc32_test(void) return 0; } +enum +{ + ENCODE = 1, + DECODE = 2, + ENCODE_DECODE = 3 +}; /* * Base64 test vectors (RFC 4648) @@ -502,48 +508,70 @@ static struct base64_test_vec { const char *base256; const char *base64; + unsigned flag; } base64_test_vec[] = { { "", - "" + "", + ENCODE_DECODE }, { "f", - "Zg==" + "Zg==", + ENCODE_DECODE }, { "fo", - "Zm8=" + "Zm8=", + ENCODE_DECODE }, { "foo", - "Zm9v" + "Zm9v", + ENCODE_DECODE }, { "foob", - "Zm9vYg==" + "Zm9vYg==", + ENCODE_DECODE }, { "fooba", "Zm9vYmE=", + ENCODE_DECODE }, { "foobar", - "Zm9vYmFy" + "Zm9vYmFy", + ENCODE_DECODE }, { "\x14\xfb\x9c\x03\xd9\x7e", - "FPucA9l+" + "FPucA9l+", + ENCODE_DECODE }, { "\x14\xfb\x9c\x03\xd9", - "FPucA9k=" + "FPucA9k=", + ENCODE_DECODE }, { "\x14\xfb\x9c\x03", - "FPucAw==" - } + "FPucAw==", + ENCODE_DECODE + }, + /* with whitespaces */ + { + "foobar", + "Zm9v\r\nYmFy", + DECODE + }, + { + "foobar", + "\nZ\r\nm 9\tv\nYm\nF\ny\n", + DECODE + }, }; @@ -556,38 +584,43 @@ static int base64_test(void) PJ_LOG(3, (THIS_FILE, " base64 test..")); for (i=0; iptr; int len = input->slen; - int i, j; - int c1, c2, c3, c4; + int i, j, k; + int c[4]; PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL); @@ -135,42 +137,28 @@ PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input, PJ_ASSERT_RETURN(*out_len >= PJ_BASE64_TO_BASE256_LEN(len), PJ_ETOOSMALL); - for (i=0, j=0; i+3 < len; i+=4) { - c1 = base256_char(buf[i]); - c2 = base256_char(buf[i+1]); - c3 = base256_char(buf[i+2]); - c4 = base256_char(buf[i+3]); - - out[j++] = (pj_uint8_t)((c1<<2) | ((c2 & 0x30)>>4)); - out[j++] = (pj_uint8_t)(((c2 & 0x0F)<<4) | ((c3 & 0x3C)>>2)); - out[j++] = (pj_uint8_t)(((c3 & 0x03)<<6) | (c4 & 0x3F)); - } - - if (i < len) { - c1 = base256_char(buf[i]); - - if (i+1 < len) - c2 = base256_char(buf[i+1]); - else - c2 = (INV); - - if (i+2 < len) - c3 = base256_char(buf[i+2]); - else - c3 = (INV); - - c4 = (INV); + for (i=0, j=0; i>4)); - if (c3 != INV) { - out[j++] = (pj_uint8_t)(((c2 & 0x0F)<<4) | ((c3 & 0x3C)>>2)); - if (c4 != INV) { - out[j++] = (pj_uint8_t)(((c3 & 0x03)<<6) | (c4 & 0x3F)); + if (k<4) { + if (k > 1) { + out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4)); + if (k > 2) { + out[j++] = (pj_uint8_t) + (((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2)); } } + break; } - + + out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4)); + out[j++] = (pj_uint8_t)(((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2)); + out[j++] = (pj_uint8_t)(((c[2] & 0x03)<<6) | (c[3] & 0x3F)); } pj_assert(j < *out_len); -- cgit v1.2.3