1
0
mirror of https://github.com/yarrick/iodine.git synced 2024-11-26 06:56:05 +03:00

#51 handle one block encode/decode for base32

This commit is contained in:
Erik Ekman 2009-02-17 20:40:40 +00:00
parent 75ffe37863
commit 98d5f61e5e
2 changed files with 49 additions and 3 deletions

View File

@ -103,9 +103,12 @@ base32_encode(char *buf, size_t *buflen, const void *data, size_t size)
memset(buf, 0, *buflen); memset(buf, 0, *buflen);
/* how many chars can we encode within the buf */ /* 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 */ /* 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 the buffer is too small, eat some of the data */
if (*buflen < newsize) { if (*buflen < newsize) {
size = maxsize; size = maxsize;
@ -132,7 +135,7 @@ base32_encode(char *buf, size_t *buflen, const void *data, size_t size)
/* store number of bytes from data that was used */ /* store number of bytes from data that was used */
*buflen = size; *buflen = size;
return strlen(buf) - 1; return strlen(buf);
} }
#define DECODE_ERROR 0xffffffff #define DECODE_ERROR 0xffffffff

View File

@ -85,6 +85,48 @@ START_TEST(test_base32_5to8_8to5)
} }
END_TEST END_TEST
START_TEST(test_base32_blksize)
{
size_t rawlen;
size_t enclen;
char *rawbuf;
char *encbuf;
struct encoder *b32;
int i;
int val;
b32 = get_base32_encoder();
rawlen = b32->blocksize_raw();
enclen = b32->blocksize_encoded();
rawbuf = malloc(rawlen + 16);
encbuf = malloc(enclen + 16);
for (i = 0; i < rawlen; i++) {
rawbuf[i] = 'A';
}
rawbuf[i] = 0;
val = b32->encode(encbuf, &enclen, rawbuf, rawlen);
fail_unless(rawlen == 5, "raw length was %d not 5", rawlen);
fail_unless(enclen == 5, "encoded %d bytes, not 5", enclen);
fail_unless(val == 8, "encoded string %s was length %d", encbuf, val);
memset(rawbuf, 0, rawlen + 16);
enclen = val;
val = b32->decode(rawbuf, &rawlen, encbuf, enclen);
fail_unless(rawlen == 5, "raw length was %d not 5", rawlen);
fail_unless(val == 5, "val was not 5 but %d", val);
for (i = 0; i < rawlen; i++) {
fail_unless(rawbuf[i] == 'A');
}
}
END_TEST
TCase * TCase *
test_base32_create_tests() test_base32_create_tests()
{ {
@ -94,6 +136,7 @@ test_base32_create_tests()
tcase_add_loop_test(tc, test_base32_encode, 0, TUPLES); tcase_add_loop_test(tc, test_base32_encode, 0, TUPLES);
tcase_add_loop_test(tc, test_base32_decode, 0, TUPLES); tcase_add_loop_test(tc, test_base32_decode, 0, TUPLES);
tcase_add_test(tc, test_base32_5to8_8to5); tcase_add_test(tc, test_base32_5to8_8to5);
tcase_add_test(tc, test_base32_blksize);
return tc; return tc;
} }