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 = struct {
stream: io::stream,
in: io::handle,
enc: *encoding,
obuf: [3]u8,
ibuf: [4]u8,
iavail: u8,
oavail: u8,
pad: bool,
};
type encoder = struct {
stream: io::stream,
out: io::handle,
enc: *encoding,
ibuf: [3]u8,
obuf: [4]u8,
iavail: u8,
oavail: u8,
};
type encoding = struct {
encmap: [64]u8,
decmap: [128]u8,
};
Globals
const std_encoding: encoding;
const url_encoding: encoding;
Functions
fn decode(in: io::handle, enc: *encoding, buf: []u8) (size | io::EOF | io::error);
fn decodedsize(sz: size) size;
fn decodeslice(enc: *encoding, in: []u8) ([]u8 | errors::invalid);
fn decodestr(enc: *encoding, in: str) ([]u8 | errors::invalid);
fn encode(out: io::handle, enc: *encoding, buf: []u8) (size | io::error);
fn encodedsize(sz: size) size;
fn encodeslice(enc: *encoding, in: []u8) []u8;
fn encodestr(enc: *encoding, in: []u8) str;
fn encoding_init(enc: *encoding, alphabet: str) void;
fn newdecoder(enc: *encoding, in: io::handle) decoder;
fn newencoder(enc: *encoding, out: io::handle) encoder;
Types
type decoder
Show undocumented member
type decoder = struct {
stream: io::stream,
in: io::handle,
enc: *encoding,
obuf: [3]u8,
ibuf: [4]u8,
iavail: u8,
oavail: u8,
pad: bool,
};
type encoder
Show undocumented member
type encoder = struct {
stream: io::stream,
out: io::handle,
enc: *encoding,
ibuf: [3]u8,
obuf: [4]u8,
iavail: u8,
oavail: u8,
};
type encoding
Show undocumented member
type encoding = struct {
encmap: [64]u8,
decmap: [128]u8,
};
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 decodedsize
fn decodedsize(sz: size) size;
Given the size of base64 encoded data, returns maximal length of decoded message. The message may be at most 2 bytes shorter than the returned value. Input size must be a multiple of 4.
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 encodedsize
fn encodedsize(sz: size) size;
Given the length of the message, returns the size of its base64 encoding
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. If a read returns an error, the stream must not be read from again.
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. Afterwards io::close must be called to write any unwritten bytes, in case of padding. Closing this stream will not close the underlying stream. After a write returns an error, the stream must not be written to again or closed.