Base64 v0.1.0
Base64Variant
pub enum Base64Variant {
Standard,
StandardUnpadded,
Url,
UrlUnpadded
}
Which Base64 alphabet a text uses, and whether it is padded. Copy, carrying no payload.
Cases
| Name | Description |
|---|---|
Standard | The 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. |
StandardUnpadded | The standard alphabet without padding. |
Url | The URL- and file-name-safe alphabet ending in - and _, padded. RFC 4648 section 5. |
UrlUnpadded | The 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.
Parameters
| Name | Type | Description |
|---|---|---|
length | uint | the number of bytes to encode |
form | Base64Variant | the 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.
| Overload | Summary |
|---|---|
pub func Base64Encode(bytes: byte[..], into: var char8[..], form: Base64Variant) -> uint ! FormatError | Writes bytes as Base64 into into. |
pub func Base64Encode(text: char8[..], into: var char8[..], form: Base64Variant) -> uint ! FormatError | Writes the bytes of text as Base64 into into. |
Parameters
| Name | Type | Description |
|---|---|---|
bytes | byte[..] | the bytes to encode |
into | var char8[..] | the destination, at least Base64EncodedLength(bytes.length, form) characters |
form | Base64Variant | the alphabet and padding to encode with |
text | char8[..] | 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.
Parameters
| Name | Type | Description |
|---|---|---|
text | char8[..] | the Base64 text |
form | Base64Variant | the 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.
Parameters
| Name | Type | Description |
|---|---|---|
text | char8[..] | the Base64 text to decode |
into | var byte[..] | the destination, at least Base64DecodedLength(text, form) bytes |
form | Base64Variant | the 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.
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.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
Returns: success when the whole rendering was accepted
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.
Parameters
| Name | Type | Description |
|---|---|---|
bytes | byte[..] | the bytes to render |
form | Base64Variant | the alphabet and padding to render with |
Returns: a view that renders bytes as Base64