Base64 v0.1.0

Source
Base64 in its standard and URL-safe alphabets, with or without padding, and a view that prints it.

Base64Variant

pub enum Base64Variant {
    Standard,
    StandardUnpadded,
    Url,
    UrlUnpadded
}

Which Base64 alphabet a text uses, and whether it is padded. Copy, carrying no payload.

Source

Cases

NameDescription
StandardThe standard alphabet ending in + and /, padded with = to a multiple of four characters. RFC 4648 section 4, and what MIME, PEM and most protocols mean by Base64.
StandardUnpaddedThe standard alphabet without padding.
UrlThe URL- and file-name-safe alphabet ending in - and _, padded. RFC 4648 section 5.
UrlUnpaddedThe URL-safe alphabet without padding, which is what JSON Web Tokens and most URL parameters use.

Base64EncodedLength

pub func Base64EncodedLength(
    length: uint,
    form: Base64Variant
) -> uint

How many characters the Base64 form of length bytes occupies in form. Four characters for every three bytes, with a final partial group taking two or three characters, or four with padding. Cannot fail for any length a slice in memory can have; runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
lengthuintthe number of bytes to encode
formBase64Variantthe alphabet and padding to encode with

Returns: the number of characters Base64Encode writes

Base64Encode

pub func Base64Encode(
    bytes: byte[..],
    into: var char8[..],
    form: Base64Variant
) -> uint ! FormatError

pub func Base64Encode(
    text: char8[..],
    into: var char8[..],
    form: Base64Variant
) -> uint ! FormatError

Writes bytes as Base64 into into. Runs in time that depends only on the length of bytes. Reports InsufficientCapacity when into is shorter than Base64EncodedLength(bytes.length, form), leaving it exactly as it was; room past the encoded form is not touched.

OverloadSummary
pub func Base64Encode(bytes: byte[..], into: var char8[..], form: Base64Variant) -> uint ! FormatErrorWrites bytes as Base64 into into.
pub func Base64Encode(text: char8[..], into: var char8[..], form: Base64Variant) -> uint ! FormatErrorWrites the bytes of text as Base64 into into.

Source

Parameters

NameTypeDescription
bytesbyte[..]the bytes to encode
intovar char8[..]the destination, at least Base64EncodedLength(bytes.length, form) characters
formBase64Variantthe alphabet and padding to encode with
textchar8[..]the text whose bytes to encode

Returns: the number of characters written

Base64DecodedLength

pub func Base64DecodedLength(
    text: char8[..],
    form: Base64Variant
) -> uint ! DecodeError

How many bytes the Base64 text decodes to, after checking its length and the placement of its padding. Only the shape is checked; the characters are checked by Base64Decode. A padded variant needs a multiple of four characters with at most two = at the end; an unpadded one any length except one more than a multiple of four, and no = at all. Reports InvalidLength at the end of the text, or InvalidPadding at the first = that is out of place. Runs in time linear in the length and allocates nothing.

Source

Parameters

NameTypeDescription
textchar8[..]the Base64 text
formBase64Variantthe alphabet and padding the text is expected to use

Returns: the number of bytes Base64Decode writes for it

Base64Decode

pub func Base64Decode(
    text: char8[..],
    into: var byte[..],
    form: Base64Variant
) -> uint ! DecodeError

Reads Base64 text in form into into. Runs in time that depends only on the length of text when the text is valid. Reports, in this order of precedence, InvalidLength or InvalidPadding for a text of the wrong shape, InvalidCharacter at the first character outside the variant's alphabet, NonCanonical at a last character whose unused bits are not zero, and InsufficientCapacity with the length needed when into is too short. Every refusal leaves into untouched, because the whole text is checked before anything is written.

Source

Parameters

NameTypeDescription
textchar8[..]the Base64 text to decode
intovar byte[..]the destination, at least Base64DecodedLength(text, form) bytes
formBase64Variantthe alphabet and padding the text is expected to use

Returns: the number of bytes written

Base64View

pub struct Base64View {
    // private fields
}

A borrowed byte sequence that renders as Base64 under a format placeholder. Lets a caller write Render("{}", ViewBase64(key)) instead of sizing a buffer first. The view borrows the bytes and must not outlive them. Copy, owning nothing.

Source

Implements Display

Base64View conforms to Display.

WriteDisplay

pub func WriteDisplay(
    self: &Base64View,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

Writes the bytes as Base64, padded to the placeholder's width. The text is produced in blocks, so rendering allocates nothing however long the bytes are. Reports whatever the writer reported, and UnsupportedRequest for a precision or a style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole rendering was accepted

Source

ViewBase64

pub func ViewBase64(
    bytes: byte[..],
    form: Base64Variant
) -> Base64View

Borrows bytes for rendering as Base64. Cannot fail and allocates nothing; the view reads bytes each time it is rendered, so it must not outlive them.

Source

Parameters

NameTypeDescription
bytesbyte[..]the bytes to render
formBase64Variantthe alphabet and padding to render with

Returns: a view that renders bytes as Base64