encoding::base64
Implementation of the base64 encoding scheme as defined by RFC 4648.
A stream-based encoding and decoding interface is available via newencoder
and newdecoder, which transparently encode or decode bytes to or from base64
when writing to or reading from an underlying I/O handle.
Convenience functions for encoding to or decoding from a byte slice or a string
are also available; see encodeslice, decodeslice, encodestr, and
decodestr. These functions dynamically allocate their return values; use the
stream interface if you require static allocation.
Each function accepts the desired base64 encoding alphabet as its first
argument. std_encoding and url_encoding, as defined by the RFC, are
provided for your convenience, but you may create your own encoding using
encoding_init.
Due to security concerns described by the RFC, this implementation rejects
invalid padding.
https://datatracker.ietf.org/doc/html/rfc4648#section-12
Index
Types
type decoder;
type encoder;
type encoding;
Globals
let std_encoding: encoding;
let url_encoding: encoding;
Functions
fn decode(io::handle, *encoding, []u8) (size | io::EOF | io::error);
fn decodeslice(*encoding, []u8) ([]u8 | errors::invalid);
fn decodestr(*encoding, str) ([]u8 | errors::invalid);
fn encode(io::handle, *encoding, []u8) (size | io::error);
fn encodeslice(*encoding, []u8) []u8;
fn encodestr(*encoding, []u8) str;
fn encoding_init(*encoding, str) void;
fn newdecoder(*encoding, io::handle) decoder;
fn newencoder(*encoding, io::handle) encoder;
Types
type decoder
Show undocumented member
type decoder = struct {
stream: io::stream,
in: io::handle,
enc: *encoding,
avail: []u8,
pad: bool,
state: (void | io::EOF | io::error),
};
type encoder
Show undocumented member
type encoder = struct {
stream: io::stream,
out: io::handle,
enc: *encoding,
buf: [2]u8,
avail: size,
err: (void | io::error),
};
type encoding
Show undocumented member
type encoding = struct {
encmap: [64]u8,
decmap: [256]u8,
valid: [256]bool,
};
Globals
let std_encoding
const std_encoding: encoding;
Represents the standard base-64 encoding alphabet as defined in RFC 4648.
let url_encoding
const url_encoding: encoding;
Represents the "base64url" alphabet as defined in RFC 4648, suitable for use
in URLs and file paths.
Functions
fn decode
fn decode(in: io::handle, enc: *encoding, buf: []u8) (size | io::EOF | io::error);
Decodes base64 data from a stream using the given alphabet, returning the
number of bytes of bytes read (i.e. len(buf)).
fn decodeslice
fn decodeslice(enc: *encoding, in: []u8) ([]u8 | errors::invalid);
Decodes a byte slice of ASCII-encoded base 64 data, using the given encoding,
returning a slice of decoded bytes. The caller must free the return value.
fn decodestr
fn decodestr(enc: *encoding, in: str) ([]u8 | errors::invalid);
Decodes a string of ASCII-encoded base 64 data, using the given encoding,
returning a slice of decoded bytes. The caller must free the return value.
fn encode
fn encode(out: io::handle, enc: *encoding, buf: []u8) (size | io::error);
Encodes base64 data using the given alphabet and writes it to a stream,
returning the number of bytes of data written (i.e. len(buf)).
fn encodeslice
fn encodeslice(enc: *encoding, in: []u8) []u8;
Encodes a byte slice in base 64, using the given encoding, returning a slice
of ASCII bytes. The caller must free the return value.
fn encodestr
fn encodestr(enc: *encoding, in: []u8) str;
Encodes a byte slice in base 64, using the given encoding, returning a
string. The caller must free the return value.
fn encoding_init
fn encoding_init(enc: *encoding, alphabet: str) void;
Initializes a new encoding based on the passed alphabet, which must be a
64-byte ASCII string.
fn newdecoder
fn newdecoder(enc: *encoding, in: io::handle) decoder;
Creates a stream that reads and decodes base 64 data from a secondary stream.
This stream does not need to be closed, and closing it will not close the
underlying stream.
fn newencoder
fn newencoder(enc: *encoding, out: io::handle) encoder;
Creates a stream that encodes writes as base64 before writing them to a
secondary stream. The encoder stream must be closed to finalize any unwritten
bytes. Closing this stream will not close the underlying stream.