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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// pub const ALPHABET: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
pub const BITCOIN:  &[u8; 58] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
pub const RIPPLE:   &[u8; 58] = b"rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
pub const FLICKR:   &[u8; 58] = b"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
pub const SKYWELL:  &[u8; 58] = b"jpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65rkm8oFqi1tuvAxyz";

mod traits;
pub use traits::{Encode, Decode};
use traits::generate_alpha_map;

pub struct BaseX <'a> {
    pub alphabet: &'a [u8],
}

impl <'a> BaseX <'a> {
    pub fn new(alphabet: &'a [u8]) -> Self {
        BaseX {
            alphabet: alphabet,
        }
    }
}

impl <'a> Encode for BaseX <'a>{
    type Output = String;
    fn encode(&self, source: &[u8]) -> String {

        let base = self.alphabet.len() as u16;

        let mut digits: Vec<u16> = vec![0u16; 1];

        let mut i = 0;
        while i < source.len() {

            let mut j = 0;
            let mut carry: u16 = source[i] as u16;

            let digits_len = digits.len();
            while j < digits_len {
                carry += digits.as_slice()[j] << 8;

                digits.as_mut_slice()[j] = carry % (base as u16);

                carry = (carry / (base as u16)) | 0;

                j += 1;
            }

            while carry > 0 {
                digits.push(carry % (base as u16));
                carry = (carry / base) | 0;
            }

            i += 1;
        }

        let mut string = "".to_string();

        // deal with leading zeros
        let mut k = 0;
        while source[k] == 0 && k < source.len() - 1 {

            string.push(self.alphabet[0] as char);

            k += 1;
        }

        // convert digits to a string
        let mut q: i32 = (digits.len() - 1) as i32;
        while q >= 0 {

            let uu: u8 = self.alphabet[digits[q as usize] as usize];
            let xx = uu as char;

            string.push( xx );

            q -= 1;
        }

        string
    }
}

impl <'a> Decode for BaseX <'a> {
    type Output = Option<Vec<u8>>;
    fn decode(&self, string: String) -> Option<Vec<u8>> {
        if string.len() == 0 { return None; }

        let alphabet_map = generate_alpha_map(self.alphabet);
        let base = self.alphabet.len() as u16;
        let ledger = self.alphabet[0] as char;

        let mut bytes: Vec<u8> = vec![];
        let mut i = 0;
        while i < string.len() {
            let c = string.as_str().chars().nth(i).unwrap();
            let val = alphabet_map.get(&c);
            if val.is_none() {
                return None;
            }

            let mut j = 0;
            let mut carry: u16 = *val.unwrap() as u16;
            while j < bytes.len() {
                carry += bytes[j] as u16 * base;
                bytes[j] = (carry as u8) & 0xff;
                carry >>= 8;

                j += 1;
            }

            while carry > 0 {
                bytes.push((carry as u8) & 0xff );
                carry >>= 8;
            }

            i += 1;
        }

        // deal with leading zeros
        let mut k = 0;
        while string.as_str().chars().nth(k).unwrap() == ledger && k < string.len() - 1 {
            bytes.push(0);
            k += 1;
        }

        bytes.as_mut_slice().reverse();

        Some(bytes)
    }
}

#[cfg(test)]
mod test_set {
    use super::*;

    #[test]
    fn encode_test() {
        let src = vec![28, 215, 33, 155];
        let encoded = BaseX::new(BITCOIN).encode(&src);
        assert_eq!(encoded, "jkuzA".to_string());
    }

    #[test]
    fn decode_test() {
        let src = "jkuzA".to_string();
        let decoded = BaseX::new(BITCOIN).decode(src);
        assert_eq!(decoded, Some(vec![28, 215, 33, 155]));
    }
}