1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Functions for encoding data into any base from 2 to 256.
//!
//! ## Example
//!
//! ```rust
//! use base_encode::*;
//!
//! let data = vec![0x27, 0x10];
//! encode(&data, 10) // [1, 0, 0, 0, 0]
//! ```
//!
//! Leading zeros are preserved.
//!
//! ```rust
//! encode(&[0, 0, 128], 36) // [0, 0, 3, 14]
//! ```
//!
//! ```rust
//! decode(&[0, 2, 5, 6], 10) // [0, 1, 0]
//! ```

pub mod utils;

/// Encodes a `u8` slice to any base.
///
/// The `base` must be at least 2 and lower or equal than 256.
///
/// The return value contains the digits in the specified base.
pub fn encode(buf: &[u8], base: u16) -> Vec<u8> {
    let mut num = utils::from_bytes_be(buf);
    let mut digits = Vec::new();

    while num.len() > 1 || num[0] != 0 {
        digits.push(utils::div_rem(&mut num, base as utils::DoubleDigit) as u8);
    }

    let zeros = buf.iter().take_while(|&x| *x == 0).count();
    digits.resize(digits.len() + zeros, 0);
    digits.reverse(); digits
}

/// Decodes a base encoded `u8` slice into bytes.
///
/// The `base` must be at least 2 and lower or equal than 256.
/// You must ensure that the values are lower that the base.
pub fn decode(buf: &[u8], base: u16) -> Vec<u8> {
    let mut num = vec![0];
    let zeros = buf.iter().take_while(|&x| *x == 0).count();

    for &digit in buf {
        utils::mul(&mut num, base as utils::DoubleDigit);
        utils::add(&mut num, digit.into());
    }

    let mut bytes = vec![0; zeros];
    bytes.append(&mut utils::to_bytes_be(&num));
    bytes
}