encoded_base64_length#

Return the length encode_base64 would produce, without doing the encoding.

Synopsis#

use MIME::Base64 qw(encoded_base64_length);
my $n = encoded_base64_length($bytes);          # wrapping with "\n"
my $m = encoded_base64_length($bytes, "");      # no wrapping
my $k = encoded_base64_length($bytes, "\r\n");  # CRLF wrapping

What you get back#

An integer: the exact number of bytes encode_base64($bytes, $eol) would produce, including every end-of-line sequence. Equivalent to length(encode_base64($bytes, $eol)) but avoids allocating and building the encoded string — preferred when you need to pre-size a buffer or report progress on a large payload.

Examples#

encoded_base64_length("");                 # 0
encoded_base64_length("A");                # 5   ("QQ==\n")
encoded_base64_length("A", "");            # 4   ("QQ==")
encoded_base64_length("A" x 57);           # 77  (one full 76-char line + "\n")
encoded_base64_length("A" x 57, "\r\n");   # 78  (one full 76-char line + "\r\n")

Edge cases#

  • Empty input returns 0 regardless of the $eol argument.

  • The formula is ceil(n/3) * 4 + lines * length($eol) where lines = ceil(base_len / 76).

  • An undefined or missing $eol counts as "\n" (length 1) to match encode_base64’s default.

Differences from upstream#

Fully compatible with upstream MIME::Base64 3.16.

See also#

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

  • decoded_base64_length — the companion predictor for the decoder.