# baz64 Public API
A zero-dependency base64 (RFC 4648) encoding/decoding library for Rust.
## Top-level re-exports
### `baz64::encode`
```rust
pub fn encode(input: impl AsRef<[u8]>) -> String
```
Encodes arbitrary bytes into a base64 string with `=` padding. Accepts anything that implements `AsRef<[u8]>` (byte slices, `Vec<u8>`, `String`, etc.).
### `baz64::decode`
```rust
pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError>
```
Decodes a base64 string back into bytes. Input length must be a multiple of 4. Returns `Err(DecodeError)` on invalid input.
### `baz64::encode_url_safe`
```rust
pub fn encode_url_safe(input: impl AsRef<[u8]>) -> String
```
Encodes arbitrary bytes into a URL-safe base64 string (RFC 4648 Section 5). Uses `-` and `_` instead of `+` and `/`, and omits `=` padding.
### `baz64::decode_url_safe`
```rust
pub fn decode_url_safe(input: &str) -> Result<Vec<u8>, DecodeError>
```
Decodes a URL-safe base64 string back into bytes. Accepts input with or without `=` padding. Input length must not be 1 mod 4.
### `baz64::DecodeError`
```rust
#[non_exhaustive]
pub enum DecodeError {
InvalidByte(u8, usize),
InvalidLength(usize),
InvalidPadding,
}
```
Error type returned by `decode` and `decode_url_safe`. Implements `Display` and `std::error::Error`.
| `InvalidByte(byte, pos)` | Non-base64 byte at the given position |
| `InvalidLength(len)` | Input length is not valid for the decoding mode |
| `InvalidPadding` | Padding is malformed or has non-zero trailing bits |
## `baz64::pem` module
Utilities for encoding/decoding PEM (Privacy-Enhanced Mail) blocks.
### `baz64::pem::encode`
```rust
pub fn encode(der: impl AsRef<[u8]>, label: &str) -> String
```
Wraps DER bytes in a PEM block with the given label, using 64-character base64 lines.
```text
-----BEGIN CERTIFICATE-----
<base64>
-----END CERTIFICATE-----
```
### `baz64::pem::decode`
```rust
pub fn decode(pem: &str, label: &str) -> Result<Vec<u8>, PemError>
```
Extracts and decodes the first PEM block matching `label`. Whitespace within the base64 body is ignored.
### `baz64::pem::decode_all`
```rust
pub fn decode_all(pem: &str, label: &str) -> Result<Vec<Vec<u8>>, PemError>
```
Extracts and decodes all PEM blocks matching `label`. Returns an empty `Vec` if no blocks match.
### `baz64::PemError`
```rust
#[non_exhaustive]
pub enum PemError {
NoBlockFound,
MalformedBlock,
InvalidBase64(DecodeError),
}
```
Error type returned by PEM operations. Implements `Display`, `std::error::Error`, and `From<DecodeError>`.
| `NoBlockFound` | No PEM block with the requested label exists (returned by `decode` only) |
| `MalformedBlock` | BEGIN marker found but no matching END marker |
| `InvalidBase64(DecodeError)`| The base64 content within the block is invalid|