base64

package
v0.1.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package base64 implements RFC 4648 encoding with SIMD acceleration for media payloads. Its Encoding API follows encoding/base64.

Index

Constants

View Source
const (
	StdPadding = std.StdPadding
	NoPadding  = std.NoPadding
)

Variables

View Source
var (
	StdEncoding    = NewEncoding(standardAlphabet)
	URLEncoding    = NewEncoding(urlAlphabet)
	RawStdEncoding = StdEncoding.WithPadding(NoPadding)
	RawURLEncoding = URLEncoding.WithPadding(NoPadding)
)

Functions

func AppendDataURL

func AppendDataURL(dst []byte, mediaType string, src []byte) []byte

AppendDataURL appends a standard padded base64 data URL to dst. mediaType must be a valid MIME type supplied by the caller.

func EncodeDataURL

func EncodeDataURL(mediaType string, src []byte) string

EncodeDataURL returns data:<mediaType>;base64,<payload> in one allocation. mediaType must be a valid MIME type supplied by the caller, such as image/png.

func NewDecoder

func NewDecoder(enc *Encoding, r io.Reader) io.Reader

NewDecoder returns the standard library's streaming decoder for enc.

func NewEncoder

func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser

NewEncoder returns a streaming encoder. Close it to flush the final block. Streaming currently uses the standard library's buffered implementation.

Types

type CorruptInputError

type CorruptInputError = std.CorruptInputError

CorruptInputError reports the byte offset of invalid base64 input.

type Encoding

type Encoding struct {
	// contains filtered or unexported fields
}

Encoding describes a base64 alphabet, padding and strict decoding rules. Configured encodings can be shared by concurrent callers.

func NewEncoding

func NewEncoding(alphabet string) *Encoding

NewEncoding creates an encoding from 64 distinct bytes. It panics for invalid alphabets according to encoding/base64.NewEncoding.

func (*Encoding) AppendDecode

func (e *Encoding) AppendDecode(dst, src []byte) ([]byte, error)

AppendDecode appends decoded bytes to dst, including the valid prefix when the input is corrupt.

func (*Encoding) AppendEncode

func (e *Encoding) AppendEncode(dst, src []byte) []byte

AppendEncode appends the encoding of src to dst.

func (*Encoding) Decode

func (e *Encoding) Decode(dst, src []byte) (int, error)

Decode writes decoded bytes to dst and returns their count. On corrupt input, it returns the valid prefix and a CorruptInputError at the original offset. CR and LF are ignored. dst must have enough space for the decoded output.

func (*Encoding) DecodeString

func (e *Encoding) DecodeString(s string) ([]byte, error)

DecodeString decodes s without making a temporary copy of its input.

func (*Encoding) DecodedLen

func (e *Encoding) DecodedLen(n int) int

DecodedLen returns the maximum decoded length for n encoded bytes.

func (*Encoding) Encode

func (e *Encoding) Encode(dst, src []byte)

Encode writes EncodedLen(len(src)) bytes to dst. A short dst causes a panic.

func (*Encoding) EncodeToString

func (e *Encoding) EncodeToString(src []byte) string

EncodeToString returns an immutable encoding of src using one allocation.

func (*Encoding) EncodedLen

func (e *Encoding) EncodedLen(n int) int

EncodedLen returns the number of bytes needed to encode n source bytes.

func (Encoding) Strict

func (e Encoding) Strict() *Encoding

Strict returns a copy that rejects nonzero trailing padding bits.

func (*Encoding) ValidateString

func (e *Encoding) ValidateString(s string) (decodedLen int, err error)

ValidateString returns the decoded byte count and error that DecodeString would return. On corrupt input, the count covers the successfully decoded prefix and CorruptInputError identifies the original input offset.

On amd64 and arm64, uninterrupted standard and URL input is checked without producing decoded bytes; only the last 64 characters are decoded into stack storage. Large custom alphabets, CR/LF inputs and scalar fallbacks may allocate remaining output.

func (Encoding) WithPadding

func (e Encoding) WithPadding(padding rune) *Encoding

WithPadding returns a copy using padding, or NoPadding for unpadded output.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL