Repository navigation
feat: accept optional padding in Base32 and Base64 decoding - #21
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
policy::PaddedOptionalandpolicy::UnpaddedOptionalin 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.b"f"asPaddedOptionalZg==Zg,Zg==Zg=,Zg===UnpaddedOptionalZgZg,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 protocolDesign Notes
Each marker specifies
ENCODE_PADDEDfor output and aDECODE_PADDINGrule (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 duringupdateand an unpadded fragment atfinish. Alphabet, case, and trailing-bit checks apply to both input forms.Verified with
cargo x teston Rust 1.88.0 and stable,cargo x check,cargo x lint, andcargo x bench -- --test.Refs #9