encoding::base32+x86_64 +linux

Implementation of the base32 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 base32 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 base32 encoding alphabet as its first argument. std_encoding and hex_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
	avail: []u8,
	// if padding was seen in a previous read
	pad: bool,
	state: (void | io::EOF | io::error),
};
type encoder = struct {
	stream: io::stream,
	out: io::handle,
	enc: *encoding,
	// leftover input
	buf: [4]u8,
	// bytes available in buf
	avail: size,
	err: (void | io::error),
};
type encoding = struct {
	encmap: [32]u8,
	decmap: [256]u8,
	valid: [256]bool,
};

Globals

const hex_encoding: encoding;
const std_encoding: encoding;

Functions

fn decodeslice(enc: *encoding, in: []u8) ([]u8 | errors::invalid);
fn decodestr(enc: *encoding, in: str) ([]u8 | errors::invalid);
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
	avail: []u8,
	// if padding was seen in a previous read
	pad: bool,
	state: (void | io::EOF | io::error),
};

type encoder[link]

Show undocumented member
type encoder = struct {
	stream: io::stream,
	out: io::handle,
	enc: *encoding,
	// leftover input
	buf: [4]u8,
	// bytes available in buf
	avail: size,
	err: (void | io::error),
};

type encoding[link]

Show undocumented member
type encoding = struct {
	encmap: [32]u8,
	decmap: [256]u8,
	valid: [256]bool,
};

Globals

let hex_encoding[link]

const hex_encoding: encoding;

Represents the "base32hex" alphabet as defined in RFC 4648.

let std_encoding[link]

const std_encoding: encoding;

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

Functions

fn decodeslice[link]

fn decodeslice(enc: *encoding, in: []u8) ([]u8 | errors::invalid);

Decodes a byte slice of ASCII-encoded base-32 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-32 data, using the given encoding, returning a slice of decoded bytes. The caller must free the return value.

fn encodeslice[link]

fn encodeslice(enc: *encoding, in: []u8) []u8;

Encodes a byte slice in base-32, 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-32, 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 32-byte ASCII string.

fn newdecoder[link]

fn newdecoder(enc: *encoding, in: io::handle) decoder;

Creates a stream that reads and decodes base-32 data from a secondary stream. This stream does not need to be closed, and closing it will not close the underlying stream.

fn newencoder[link]

fn newencoder(enc: *encoding, out: io::handle) encoder;

Creates a stream that encodes writes as base-32 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.