|
20 | 20 | // SOFTWARE.
|
21 | 21 | // (reference https://github.com/andreasots/base32/blob/master/LICENSE-MIT)
|
22 | 22 |
|
| 23 | +/// Alphabet used for encoding and decoding. |
23 | 24 | #[derive(Copy, Clone)]
|
24 | 25 | pub enum Alphabet {
|
25 |
| - // RFC4648 |
| 26 | + /// RFC4648 base32 encoding. padding: bool indicates whether to use padding. |
26 | 27 | RFC4648 { padding: bool },
|
27 |
| - // Crockford |
| 28 | + /// Crockford base32 encoding. |
28 | 29 | Crockford,
|
29 | 30 | }
|
30 | 31 |
|
| 32 | +/// RFC4648 base32 encoding with padding. |
31 | 33 | const RFC4648_ALPHABET: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
| 34 | + |
| 35 | +/// Crockford base32 encoding. |
32 | 36 | const CROCKFORD_ALPHABET: &'static [u8] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
33 | 37 |
|
34 |
| -// Encode a byte slice into a base32 string. |
| 38 | +/// Encode a byte slice into a base32 string. |
35 | 39 | pub fn encode(alphabet: Alphabet, data: &[u8]) -> String {
|
36 | 40 | let (alphabet, padding) = match alphabet {
|
37 | 41 | Alphabet::RFC4648 { padding } => (RFC4648_ALPHABET, padding),
|
@@ -72,10 +76,13 @@ pub fn encode(alphabet: Alphabet, data: &[u8]) -> String {
|
72 | 76 | String::from_utf8(ret).unwrap()
|
73 | 77 | }
|
74 | 78 |
|
| 79 | +/// Inverse RFC4648 lookup table for decoding. |
75 | 80 | const RFC4648_INV_ALPHABET: [i8; 43] = [
|
76 | 81 | -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
|
77 | 82 | 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
78 | 83 | ];
|
| 84 | + |
| 85 | +/// Inverse Crockford lookup table for decoding. |
79 | 86 | const CROCKFORD_INV_ALPHABET: [i8; 43] = [
|
80 | 87 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 1,
|
81 | 88 | 18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 31,
|
|
0 commit comments