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.
Features
- π High Performance: Bitwise operations for maximum efficiency
- π¦ Zero Dependencies: Pure Rust implementation
- π‘οΈ Memory Safe: No unsafe code
- π― Space Efficient: ~87% compression efficiency vs ~75% for Base64
- π§ Easy to Use: Simple encode/decode API
- π Well Documented: Comprehensive documentation and examples
Algorithm Overview
Base122 uses a sophisticated bitwise approach:
- 7-bit Extraction: Extracts exactly 7 bits at a time from input data
- Smart Character Mapping: Safe characters map directly to single bytes
- UTF-8 Encoding: "Dangerous characters" use multi-byte UTF-8 sequences
- Optimal Efficiency: Achieves ~87% compression efficiency
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
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Basic Example
use ;
// Encode binary data
let data = b"Hello, World!";
let encoded = encode;
println!;
// Decode back to original
let decoded = decode.unwrap;
assert_eq!;
Working with Binary Data
use ;
// Binary data with dangerous characters
let binary_data = vec!;
let encoded = encode;
let decoded = decode.unwrap;
assert_eq!;
Command Line Usage
Build and run the demo:
Performance
Efficiency Comparison
| Encoding | Expansion Ratio | Efficiency | Use Case |
|---|---|---|---|
| Hexadecimal | 2.00x | 50% | Debug output |
| Base64 | 1.33x | 75% | Email, HTTP |
| Base122 | 1.14x | 87% | Data URIs, Space-constrained |
Benchmark Results
Size Encoded Ratio Efficiency vs Base64
--------------------------------------------------------
10 12 1.200 83.3% +16.7%
100 115 1.150 87.0% +13.8%
1000 1143 1.143 87.5% +14.3%
10000 11429 1.143 87.5% +14.3%
When to Use Base122
β Ideal for:
- Data URIs in HTML/CSS
- Space-constrained applications
- Binary data transmission
- JSON payloads with binary content
- Text protocols with size limits
β Consider alternatives for:
- Systems requiring Base64 compatibility
- Environments without UTF-8 support
- Cases where simplicity trumps efficiency
Examples
Data URI Optimization
use encode;
// Image data for CSS/HTML
let image_data = read.unwrap;
let base122_uri = format!;
// ~14% smaller than equivalent Base64 data URI
Binary Protocol
use ;
// Encode binary protocol message
let message = vec!;
let encoded = encode;
// Send over text-based protocol
send_message;
// Decode on receiver
let received = receive_message;
let decoded = decode.unwrap;
Error Handling
The decode function returns a Result<Vec<u8>, String>:
use decode;
match decode
Testing
Run all tests:
Run with output for detailed benchmarks:
Run the example:
Documentation
- API Documentation
- Algorithm Details
- Run
cargo doc --openfor local documentation
Development
Release Management
This project includes automated release scripts for easy version management:
π Full Release Process:
- Interactive guided release with comprehensive checks
- Runs full test suite, format checks, and documentation build
- Updates version, creates commits and tags
- Triggers automated publishing to crates.io
β‘ Quick Patch Release:
- Auto-increments patch version (0.1.0 β 0.1.1)
- Runs basic checks only
- Fast release for bug fixes
π€ Automated Publishing:
- GitHub Actions automatically publishes to crates.io when version tags are pushed
- Creates GitHub releases with detailed changelogs
- Runs comprehensive CI across multiple platforms and Rust versions
Manual Release Steps
- Update version in
Cargo.toml - Commit changes:
git commit -m "chore: bump version to x.y.z" - Create tag:
git tag vx.y.z - Push tag:
git push origin vx.y.z - GitHub Actions handles the rest!
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Based on the original Base122 algorithm by Kevin Albertson
- Inspired by the need for more efficient binary-to-text encoding
- Thanks to the Rust community for excellent tooling and libraries
Languages
See Also
- Base64 - Standard Base64 encoding
- Hex - Hexadecimal encoding
- Original Base122 JavaScript implementation