decoded_base64_length#

Return the length decode_base64 would produce, without doing the decoding.

Synopsis#

use MIME::Base64 qw(decoded_base64_length);
my $n = decoded_base64_length($encoded);

What you get back#

An integer: the exact number of bytes decode_base64($encoded) would produce. Equivalent to length(decode_base64($encoded)) but avoids building the decoded string. Useful when validating that a payload fits a size limit, or pre-sizing a destination buffer.

The same leniency rules as decode_base64 apply: stray whitespace and non-alphabet characters are ignored, and the count stops at the first = padding group.

Examples#

decoded_base64_length("");                            # 0
decoded_base64_length("QQ==");                        # 1
decoded_base64_length("QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); # 19
decoded_base64_length("QWxh\nZGRp\nbjpv\ncGVu\n");    # 12 (newlines ignored)

Edge cases#

  • Empty input, or input with no alphabet characters, returns 0.

  • A lone trailing character that would decode to no output (a group of exactly 1 valid character) contributes 0 bytes.

  • Characters after the first = are not counted.

Differences from upstream#

Fully compatible with upstream MIME::Base64 3.16.

See also#

  • decode_base64 — the function whose output length is being computed.

  • encoded_base64_length — the companion predictor for the encoder.