void test_main(void) { ASSERT(BASE64_ENCODE_LENGTH(0) == 0); /* At most 4 bits */ ASSERT(BASE64_ENCODE_LENGTH(1) == 2); /* At most 12 bits */ ASSERT(BASE64_ENCODE_LENGTH(2) == 3); /* At most 20 bits */ ASSERT(BASE64_ENCODE_LENGTH(3) == 4); /* At most 28 bits */ ASSERT(BASE64_ENCODE_LENGTH(4) == 6); /* At most 36 bits */ ASSERT(BASE64_ENCODE_LENGTH(5) == 7); /* At most 44 bits */ ASSERT(BASE64_ENCODE_LENGTH(12) == 16); /* At most 100 bits */ ASSERT(BASE64_ENCODE_LENGTH(13) == 18); /* At most 108 bits */ ASSERT(BASE64_DECODE_LENGTH(0) == 0); /* At most 6 bits */ ASSERT(BASE64_DECODE_LENGTH(1) == 1); /* At most 12 bits */ ASSERT(BASE64_DECODE_LENGTH(2) == 2); /* At most 18 bits */ ASSERT(BASE64_DECODE_LENGTH(3) == 3); /* At most 24 bits */ ASSERT(BASE64_DECODE_LENGTH(4) == 3); /* At most 30 bits */ test_armor(&nettle_base64, LDATA(""), ""); test_armor(&nettle_base64, LDATA("H"), "SA=="); test_armor(&nettle_base64, LDATA("He"), "SGU="); test_armor(&nettle_base64, LDATA("Hel"), "SGVs"); test_armor(&nettle_base64, LDATA("Hell"), "SGVsbA=="); test_armor(&nettle_base64, LDATA("Hello"), "SGVsbG8="); test_armor(&nettle_base64, LDATA("Hello\0"), "SGVsbG8A"); test_armor(&nettle_base64, LDATA("Hello?>>>"), "SGVsbG8/Pj4+"); test_armor(&nettle_base64, LDATA("\xff\xff\xff\xff"), "/////w=="); test_armor(&nettle_base64url, LDATA(""), ""); test_armor(&nettle_base64url, LDATA("H"), "SA=="); test_armor(&nettle_base64url, LDATA("He"), "SGU="); test_armor(&nettle_base64url, LDATA("Hel"), "SGVs"); test_armor(&nettle_base64url, LDATA("Hell"), "SGVsbA=="); test_armor(&nettle_base64url, LDATA("Hello"), "SGVsbG8="); test_armor(&nettle_base64url, LDATA("Hello\0"), "SGVsbG8A"); test_armor(&nettle_base64url, LDATA("Hello?>>>"), "SGVsbG8_Pj4-"); test_armor(&nettle_base64url, LDATA("\xff\xff\xff\xff"), "_____w=="); { /* Test overlapping areas */ uint8_t buffer[] = "Helloxxxx"; struct base64_decode_ctx ctx; size_t dst_length; ASSERT(BASE64_ENCODE_RAW_LENGTH(5) == 8); base64_encode_raw(buffer, 5, buffer); ASSERT(MEMEQ(9, buffer, "SGVsbG8=x")); base64_decode_init(&ctx); dst_length = 0; /* Output parameter only. */ ASSERT(base64_decode_update(&ctx, &dst_length, buffer, 8, buffer)); ASSERT(dst_length == 5); ASSERT(MEMEQ(9, buffer, "HelloG8=x")); } test_fuzz (); }
_public_ int dnssec_binary_from_base64(const dnssec_binary_t *base64, dnssec_binary_t *binary) { if (!base64 || !binary) { return DNSSEC_EINVAL; } if (base64->size == 0) { clear_struct(binary); return DNSSEC_EOK; } size_t raw_size = BASE64_DECODE_LENGTH(base64->size); uint8_t *raw = malloc(raw_size); if (raw == NULL) { return DNSSEC_ENOMEM; } size_t real_size = base64_decode_raw(base64->data, base64->size, raw, raw_size); if (real_size == 0) { free(raw); return DNSSEC_EINVAL; } binary->data = raw; binary->size = real_size; return DNSSEC_EOK; }
/* decodes data and puts the result into result (locally allocated) * The result_size is the return value */ int _gnutls_base64_decode(const uint8_t * data, size_t data_size, gnutls_datum_t * result) { int ret; size_t size; gnutls_datum_t pdata; struct base64_decode_ctx ctx; ret = cpydata(data, data_size, &pdata); if (ret < 0) { gnutls_assert(); return ret; } base64_decode_init(&ctx); size = BASE64_DECODE_LENGTH(data_size); result->data = gnutls_malloc(size); if (result->data == NULL) return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); ret = base64_decode_update(&ctx, &size, result->data, pdata.size, pdata.data); if (ret == 0) { gnutls_assert(); gnutls_free(result->data); result->data = NULL; ret = GNUTLS_E_PARSING_ERROR; goto cleanup; } ret = base64_decode_final(&ctx); if (ret != 1) return gnutls_assert_val(GNUTLS_E_PARSING_ERROR); result->size = size; ret = size; cleanup: gnutls_free(pdata.data); return ret; }
static size_t base64url_decode_length(size_t length) { return BASE64_DECODE_LENGTH(length); }