Crate base122_rs

Source
Expand description

§Base122 Encoding Library

A high-performance Base122 encoding/decoding library for Rust, based on the original kevinAlbs Base122 algorithm.

Base122 is a binary-to-text encoding that is approximately 14% more space-efficient than Base64, making it ideal for data URIs and other space-constrained applications.

§Algorithm Overview

Base122 uses a bitwise approach:

  • Extracts 7-bit chunks from input data
  • Maps safe characters directly to single bytes
  • Encodes “dangerous characters” using UTF-8 multi-byte sequences
  • Achieves ~87% compression efficiency (7 bits input per 8 bits output)

§Dangerous Characters

Six characters are considered “dangerous” for transmission and are specially encoded:

  • \0 (null) - can truncate strings
  • \n (newline) - breaks single-line formats
  • \r (carriage return) - breaks single-line formats
  • " (double quote) - conflicts with JSON/HTML attributes
  • & (ampersand) - conflicts with HTML entities
  • \ (backslash) - conflicts with escape sequences

§Performance

  • Theoretical efficiency: 87.5% (7 bits input / 8 bits output)
  • Actual efficiency: ~87% for large data
  • vs Base64: ~14% smaller output
  • Dependencies: Zero - pure Rust implementation

§Examples

use base122_rs::{encode, decode};

// Basic encoding/decoding
let data = b"Hello, World!";
let encoded = encode(data);
let decoded = decode(&encoded).unwrap();
assert_eq!(data, &decoded[..]);

// Binary data with dangerous characters
let binary_data = vec![0, 10, 13, 34, 38, 92, 65, 66, 67];
let encoded = encode(&binary_data);
let decoded = decode(&encoded).unwrap();
assert_eq!(binary_data, decoded);

Functions§

decode
Decodes Base122-encoded data back to the original binary data.
encode
Encodes binary data using the Base122 algorithm.