Crate base64

source ·
Expand description

Alphabets

An alphabet::Alphabet defines what ASCII symbols are used to encode to or decode from.

Constants in alphabet like alphabet::STANDARD or alphabet::URL_SAFE provide commonly used alphabets, but you can also build your own custom Alphabet if needed.

Engines

Once you have an Alphabet, you can pick which Engine you want. A few parts of the public API provide a default, but otherwise the user must provide an Engine to use.

See engine::Engine for more on what engine to choose, or use engine::DEFAULT_ENGINE if you just want plain old standard base64 and don’t have other requirements.

Config

In addition to an Alphabet, constructing an Engine also requires an engine::Config. Each Engine has a corresponding Config implementation.

encode() and decode() use the standard alphabet and default engine in an RFC 4648 standard setup.

Encoding

Several different encoding functions are available to you depending on your desire for convenience vs performance.

FunctionOutputAllocates
encodeReturns a new StringAlways
encode_engineReturns a new StringAlways
encode_engine_stringAppends to provided StringOnly if String needs to grow
encode_engine_sliceWrites to provided &[u8]Never - fastest

All of the encoding functions that take an Engine will pad as per the engine’s config.

Decoding

Just as for encoding, there are different decoding functions available.

FunctionOutputAllocates
decodeReturns a new Vec<u8>Always
decode_engineReturns a new Vec<u8>Always
decode_engine_vecAppends to provided Vec<u8>Only if Vec needs to grow
decode_engine_sliceWrites to provided &[u8]Never - fastest

Unlike encoding, where all possible input is valid, decoding can fail (see DecodeError).

Input can be invalid because it has invalid characters or invalid padding. (No padding at all is valid, but excess padding is not.) Whitespace in the input is invalid, just like any other non-base64 byte.

Read and Write

To decode a std::io::Read of b64 bytes, wrap a reader (file, network socket, etc) with read::DecoderReader.

To write raw bytes and have them b64 encoded on the fly, wrap a std::io::Write with write::EncoderWriter.

There is some performance overhead (15% or so) because of the necessary buffer shuffling – still fast enough that almost nobody cares. Also, these implementations do not heap allocate.

Display

See display for how to transparently base64 data via a Display implementation.

Panics

If length calculations result in overflowing usize, a panic will result.

The _slice flavors of encode or decode will panic if the provided output slice is too small.

Modules

Provides Alphabet and constants for alphabets commonly used in the wild.
Enables base64’d output anywhere you might use a Display implementation, like a format string.
Provides the Engine abstraction and out of the box implementations.
Implementations of io::Read to transparently decode base64.
Implementations of io::Write to transparently handle base64.

Enums

Errors that can occur while decoding.

Functions

Decode base64 using the default engine. Returns a Result containing a Vec<u8>.
Decode from string reference as octets using the specified Engine. Returns a Result containing a Vec<u8>.
Decode the input into the provided output slice.
Decode from string reference as octets. Writes into the supplied Vec, which may allocate if its internal buffer isn’t big enough. Returns a Result containing an empty tuple, aka ().
Encode arbitrary octets as base64 using the default engine. Returns a String.
Encode arbitrary octets as base64 using the provided Engine. Returns a String.
Encode arbitrary octets as base64. Writes into the supplied output buffer.
Encode arbitrary octets as base64. Writes into the supplied String, which may allocate if its internal buffer isn’t big enough.
Calculate the base64 encoded length for a given input length, optionally including any appropriate padding bytes.