I have a BASE64 encode string:
static const unsigned char base64_test_enc[] =
"VGVzdCBzdHJpbmcgZm9yIGEgc3RhY2tvdmVyZmxvdy5jb20gcXVlc3Rpb24=";
It does not have CRLF-per-72 characters.
How to calculate a decoded message length?
Well, base64 represents 3 bytes in 4 characters... so to start with you just need to divide by 4 and multiply by 3.
You then need to account for padding:
"=="
you need to subtract 2 bytes (as the last group of 4 characters only represents 1 byte)"="
you need to subtract 1 byte (as the last group of 4 characters represents 2 bytes)Base 64 uses 4 characters per 3 bytes. If it uses padding it always has a multiple of 4 characters.
Furthermore, there are three padding possibilities:
==
for one encoded byte=
for two encoded bytesSo you can simply divide the number of characters by 4, then multiply by 3 and finally subtract the number of padding characters.
Possible C code could be (if I wasn't extremely rusty in C, please adjust):
size_t encoded_base64_bytes(const char *input)
{
size_t len, padlen;
char *last, *first_pad;
len = strlen(input);
if (len == 0) return 0;
last = input + len - 4;
first_pad = strchr(last, '=');
padlen = first_pad == null ? 0 : last - first_pad;
return (len / 4) * 3 - padlen;
}
Note that this code assumes that the input is valid base 64.
A good observer will notice that there are spare bits, usually set to 0 in the final characters if padding is used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With