encoding::base64+x86_64 +linux

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

// Undocumented types:
type decoder = struct {
	stream: io::stream,
	in: io::handle,
	enc: *encoding,
	// leftover decoded output
	obuf: [3]u8,
	ibuf: [4]u8,
	iavail: u8,
	oavail: u8,
	// if padding was seen in a previous read
	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[link]

Show undocumented member
type decoder = struct {
	stream: io::stream,
	in: io::handle,
	enc: *encoding,
	// leftover decoded output
	obuf: [3]u8,
	ibuf: [4]u8,
	iavail: u8,
	oavail: u8,
	// if padding was seen in a previous read
	pad: bool,
};

type encoder[link]

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[link]

Show undocumented member
type encoding = struct {
	encmap: [64]u8,
	decmap: [128]u8,
};

Globals

let std_encoding[link]

const std_encoding: encoding;

Represents the standard base-64 encoding alphabet as defined in RFC 4648.

let url_encoding[link]

const url_encoding: encoding;

Represents the "base64url" alphabet as defined in RFC 4648, suitable for use in URLs and file paths.

Functions

fn decode[link]

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[link]

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[link]

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[link]

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[link]

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[link]

fn encodedsize(sz: size) size;

Given the length of the message, returns the size of its base64 encoding

fn encodeslice[link]

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[link]

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[link]

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[link]

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[link]

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.