From eed52b783f962b93d049ee58a851dad7d7310289 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Tue, 17 Feb 2009 20:22:24 +0000 Subject: [PATCH] #51 handle one block encode/decode for base64 --- src/base64.c | 10 +++++++--- src/iodine.c | 2 +- tests/base64.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/base64.c b/src/base64.c index 803d938..9889fb9 100644 --- a/src/base64.c +++ b/src/base64.c @@ -95,9 +95,13 @@ base64_encode(char *buf, size_t *buflen, const void *data, size_t size) } /* how many chars can we encode within the buf */ - maxsize = BLKSIZE_RAW * (*buflen / BLKSIZE_ENC - 1) - 1; + maxsize = BLKSIZE_RAW * (*buflen / BLKSIZE_ENC); /* how big will the encoded data be */ - newsize = BLKSIZE_ENC * (size / BLKSIZE_RAW + 1) + 1; + newsize = BLKSIZE_ENC * (size / BLKSIZE_RAW); + if (size % BLKSIZE_RAW) { + newsize += BLKSIZE_ENC; + } + /* if the buffer is too small, eat some of the data */ if (*buflen < newsize) { size = maxsize; @@ -120,7 +124,7 @@ base64_encode(char *buf, size_t *buflen, const void *data, size_t size) /* store number of bytes from data that was used */ *buflen = size; - return strlen(buf) - 1; + return strlen(buf); } #define DECODE_ERROR 0xffffffff diff --git a/src/iodine.c b/src/iodine.c index cd12d0d..80e7222 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -140,7 +140,7 @@ build_hostname(char *buf, size_t buflen, size_t space; char *b; - space = MIN(0xFF, buflen) - strlen(topdomain) - 5; + space = MIN(0xFF, buflen) - strlen(topdomain) - 7; if (!encoder->places_dots()) space -= (space / 57); /* space for dots */ diff --git a/tests/base64.c b/tests/base64.c index ccd71c4..bd0e9ce 100644 --- a/tests/base64.c +++ b/tests/base64.c @@ -99,6 +99,48 @@ START_TEST(test_base64_decode) } END_TEST +START_TEST(test_base64_blksize) +{ + size_t rawlen; + size_t enclen; + char *rawbuf; + char *encbuf; + struct encoder *b64; + int i; + int val; + + b64 = get_base64_encoder(); + + rawlen = b64->blocksize_raw(); + enclen = b64->blocksize_encoded(); + + rawbuf = malloc(rawlen + 16); + encbuf = malloc(enclen + 16); + + for (i = 0; i < rawlen; i++) { + rawbuf[i] = 'A'; + } + rawbuf[i] = 0; + + val = b64->encode(encbuf, &enclen, rawbuf, rawlen); + + fail_unless(rawlen == 3, "raw length was %d not 3", rawlen); + fail_unless(enclen == 3, "encoded %d bytes, not 3", enclen); + fail_unless(val == 4, "encoded string %s was length %d", encbuf, val); + + memset(rawbuf, 0, rawlen + 16); + + enclen = val; + val = b64->decode(rawbuf, &rawlen, encbuf, enclen); + + fail_unless(rawlen == 3, "raw length was %d not 3", rawlen); + fail_unless(val == 3); + for (i = 0; i < rawlen; i++) { + fail_unless(rawbuf[i] == 'A'); + } +} +END_TEST + TCase * test_base64_create_tests() { @@ -107,6 +149,7 @@ test_base64_create_tests() tc = tcase_create("Base64"); tcase_add_loop_test(tc, test_base64_encode, 0, TUPLES); tcase_add_loop_test(tc, test_base64_decode, 0, TUPLES); + tcase_add_test(tc, test_base64_blksize); return tc; }