silk/base64
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.
Strict standard-padded Base64 operations for caller-provided byte storage.
When to use
Use Base64 when a protocol requires the RFC 4648 standard alphabet and required padding.
Use encodedLength or decodedLength to calculate exact storage before a write.
Details
encodeInto and decodeInto write to caller-owned slices without allocation. Decoding
rejects whitespace, URL-safe characters, malformed padding, and nonzero unused pad bits. All
failures occur before destination mutation. A successful write does not change the unused suffix.
Gotchas
This module does not implement MIME, PEM whitespace, URL-safe, unpadded, streaming, or allocating variants. Base64 is an encoding, not encryption.
Import as Base64 with import silk.base64 { Base64 }.
Public declarations: 3.
Base64Reason
pub enum Base64ReasonThe reason a Base64 size calculation, validation, or caller-buffer operation failed.
InvalidLength
InvalidLength = 0A nonempty encoded input does not contain a whole four-byte quantum.
InvalidByte
InvalidByte = 1An input byte is outside the standard Base64 alphabet.
InvalidPadding
InvalidPadding = 2A padding byte is misplaced, excessive, or missing from its required final position.
NonCanonicalPadBits
NonCanonicalPadBits = 3Unused bits in the final significant sextet are nonzero.
SizeOverflow
SizeOverflow = 4The encoded result length is not representable as usize for the selected target.
OutputTooSmall
OutputTooSmall = 5The caller-provided output slice cannot hold the complete result.
Base64Error
pub struct Base64ErrorA deterministic Base64 failure with details relevant to its reason.
Details
Validation failures populate offset. OutputTooSmall populates required and available.
SizeOverflow has no additional fields, and unused fields are None.
Field reason
pub reason: Base64ReasonThe stable failure category.
Field offset
pub offset: silk/option.Option<usize>The offending input byte, input length, or final significant sextet position.
Field required
pub required: silk/option.Option<usize>The exact output length needed by OutputTooSmall.
Field available
pub available: silk/option.Option<usize>The supplied output length reported by OutputTooSmall.
Base64
pub struct Base64The owner of strict standard-padded Base64 operations.
Associated function Base64.encodedLength
pub fn encodedLength(inputLength: usize) -> silk/result.Result<usize, silk/base64.Base64Error>Returns the exact standard-padded encoded length without allocation.
Details
Zero returns zero. An unrepresentable result returns SizeOverflow without a trap. All
optional error fields are None for SizeOverflow.
Associated function Base64.decodedLength
pub fn decodedLength<'life0>(input: &'life0 [u8]) -> silk/result.Result<usize, silk/base64.Base64Error>Validates strict standard-padded Base64 and returns its exact decoded byte length.
Details
Empty input returns zero. A nonempty input must contain complete four-byte quanta. Validation checks bytes from left to right and accepts padding only in the final quantum.
Gotchas
The function rejects whitespace, URL-safe bytes, malformed padding, and nonzero unused pad bits. The returned error identifies the first failure offset.
Associated function Base64.encodeInto
pub fn encodeInto<'life0, 'life1>(output: &'life0 mut [u8], input: &'life1 [u8]) -> silk/result.Result<usize, silk/base64.Base64Error>Encodes bytes into a caller-owned output prefix and returns the number of written bytes.
Details
The function checks the exact size and capacity before the first write. A failure does not
change output. A successful write does not change bytes after the returned prefix.
Gotchas
The function returns SizeOverflow for an unrepresentable result. It returns OutputTooSmall
with exact required and available values for insufficient output. It does not add a NUL
terminator.
Associated function Base64.decodeInto
pub fn decodeInto<'life0, 'life1>(output: &'life0 mut [u8], input: &'life1 [u8]) -> silk/result.Result<usize, silk/base64.Base64Error>Decodes strict standard-padded Base64 into an output prefix and returns the written length.
Details
The function completes strict validation and capacity checking before the first write. A
failure does not change output. A successful write does not change the unused suffix.
Gotchas
An input-validation error takes precedence over OutputTooSmall. The function rejects
whitespace, URL-safe bytes, malformed padding, and nonzero unused pad bits.