Skip to content

feat: accept optional padding in Base32 and Base64 decoding - #21

Merged
tisonkun merged 3 commits into
mainfrom
codex/optional-padding
Nov 18, 2026
Merged

tisonkun merged 3 commits into
mainfrom
codex/optional-padding

Conversation

@tisonkun

@tisonkun tisonkun commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add policy::PaddedOptional and policy::UnpaddedOptional in the existing padding parameter of Base32 and Base64. The first word selects the encoder's output. Both decoders accept complete padding or none and reject partial or excess padding.

Standard Base64 policy Encodes b"f" as Accepts Rejects
PaddedOptional Zg== Zg, Zg== Zg=, Zg===
UnpaddedOptional Zg Zg, Zg== Zg=, Zg===

For gRPC binary metadata, use base64::Base64<base64::Standard, policy::UnpaddedOptional> to send unpadded values and accept either form from a peer. gRPC protocol

Design Notes

Each marker specifies ENCODE_PADDED for output and a DECODE_PADDING rule (required, forbidden, or optional) for input. Both optional policies share the strict padded and unpadded validators. A final = selects padded validation for one-shot and in-place decoding. Streaming validates a padded quantum during update and an unpadded fragment at finish. Alphabet, case, and trailing-bit checks apply to both input forms.

Verified with cargo x test on Rust 1.88.0 and stable, cargo x check, cargo x lint, and cargo x bench -- --test.

Refs #9

Protocols such as gRPC binary metadata emit unpadded Base64 but accept
both canonical wire forms. The strict padded and unpadded profiles each
reject one of those forms, so callers have to choose a decoder for each
incoming value.

Add policy::OptionalPadding in the existing padding parameter of both
codec families. It emits unpadded text and selects the padded decode
path when the input ends in '='. Each path validates its own length,
alphabet and trailing bits. Streaming recognizes a padded final quantum
during update and validates an unpadded fragment at finish.

Exercise partial and excess padding across all four alphabets with an
independent encoding oracle. Check in-place output and chunked input,
including the error offset when more data follows a padded quantum.

Refs #9
Base32 and Base64 callers can receive fully padded or unpadded input
while still needing a predictable wire format for outgoing values.
Expose PaddedOptional and UnpaddedOptional in the padding parameter so
the profile name identifies the output form and Optional describes
what the decoder accepts.

Select encoding size and output from the emission policy. Share decode
validation between both optional profiles, including in-place decoding
and streamed tails. Compose the stable profile names from the alphabet,
case and padding suffix so both families use the same naming rules.

Document all four policies side by side. Check both output forms against
an independent encoder across the four alphabets, including streamed
output, and exercise partial and excess padding across chunk boundaries.

Refs #9
Base32 and Base64 each choose an output format and an input padding rule.
Make the two choices explicit on each marker with ENCODE_PADDED and
DECODE_PADDING. The decoder can then distinguish required, forbidden
and optional padding directly.

Use the input rule to select the strict validator for one-shot and
in-place decoding. Match the same rule in the streaming decoder so it
recognizes a padded final quantum even when the encoder omits padding.
This removes the derived ACCEPTS_PADDING constant from both the padding
policy and the codec profile.
@tisonkun
tisonkun merged commit 94be0ba into main Nov 18, 2026
13 checks passed
@tisonkun
tisonkun deleted the codex/optional-padding branch November 18, 2026 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant