base_encode/
lib.rs

1//! Functions for encoding data into any base from 2 to 256.
2//!
3//! ## Example
4//!
5//! ```rust
6//! use base_encode::*;
7//!
8//! let data = vec![0x27, 0x10];
9//! encode(&data, 10) // [1, 0, 0, 0, 0]
10//! ```
11//!
12//! Leading zeros are preserved.
13//!
14//! ```rust
15//! encode(&[0, 0, 128], 36) // [0, 0, 3, 14]
16//! ```
17//!
18//! ```rust
19//! decode(&[0, 2, 5, 6], 10) // [0, 1, 0]
20//! ```
21//!
22//! ## Encode / decode strings
23//!
24//! ```rust
25//! from_str("255", 10, b"0123456789").unwrap() // [0xff]
26//!
27//! to_string(&[0xa], 2, b"OX").unwrap() // "XOXO"
28//! ```
29
30pub mod utils;
31
32/// Encodes a `u8` slice to any base.
33///
34/// The `base` must be at least 2 and lower or equal than 256.
35///
36/// The return value contains the digits in the specified base.
37pub fn encode(buf: &[u8], base: u8) -> Vec<u8> {
38    let mut num = utils::from_bytes_be(buf);
39    let mut digits = Vec::new();
40
41    while num.len() > 1 || num[0] != 0 {
42        digits.push(utils::div_rem(&mut num, base as utils::DoubleDigit) as u8);
43    }
44
45    let zeros = buf.iter().take_while(|&x| *x == 0).count();
46
47    digits.resize(digits.len() + zeros, 0);
48    digits.reverse();
49    digits
50}
51
52/// Decodes a base encoded `u8` slice into bytes.
53///
54/// The `base` must be at least 2 and lower or equal than 256.
55/// You must ensure that the values are lower that the base.
56pub fn decode(buf: &[u8], base: u8) -> Option<Vec<u8>> {
57    let mut num = vec![0];
58    let zeros = buf.iter().take_while(|&x| *x == 0).count();
59
60    if zeros == buf.len() {
61        return Some(vec![0; zeros]);
62    }
63
64    for &digit in buf {
65        if digit >= base {
66            return None;
67        }
68        utils::mul(&mut num, base as utils::DoubleDigit);
69        utils::add(&mut num, digit.into());
70    }
71
72    let mut bytes = vec![0; zeros];
73    bytes.append(&mut utils::to_bytes_be(&num));
74    Some(bytes)
75}
76
77/// Converts bytes to a base encoded string using the specified character table.
78pub fn to_string(buf: &[u8], base: u8, chars: &[u8]) -> Option<String> {
79    encode(buf, base)
80        .iter()
81        .map(|&x| chars.get(x as usize).map(|&c| c as char))
82        .collect()
83}
84
85/// Converts a base encoded string to bytes using the specified character table.
86pub fn from_str(string: &str, base: u8, chars: &[u8]) -> Option<Vec<u8>> {
87    decode(
88        &string
89            .chars()
90            .map(|c| chars.iter().position(|&a| a == c as u8).map(|x| x as u8))
91            .collect::<Option<Vec<_>>>()?,
92        base,
93    )
94}