Skip to main content

binarytext/
lib.rs

1//! This crate provides different encoders and some of their variants, like Base64 or Base32.
2//! All of them implement the trait BinaryText in the module binarytext and share a common
3//! interface.
4//!
5//! Furthermore error types can be found in the mod error.
6//! Functions for handling encoding / decoding of byte streams from a source to a destination, e.g.
7//! input string to decoded to stdout, are provided in the module stream.
8
9pub mod base16;
10pub mod base32;
11pub mod base36;
12pub mod base45;
13pub mod base64;
14pub mod base85;
15pub mod binarytext;
16pub mod error;
17pub mod stream;
18
19#[cfg(test)]
20mod tests {
21    use crate::base16::Base16;
22    use crate::base32::Base32;
23    use crate::base36::Base36;
24    use crate::base45::Base45;
25    use crate::base64::Base64;
26    use crate::base85::Base85;
27    use crate::binarytext::BinaryText;
28
29    fn compare<B: BinaryText>(b: &B, dec: &str, enc: &str) {
30        assert_eq!(b.decode_from_str(enc).unwrap(), dec.to_string());
31        assert_eq!(b.encode_from_str(dec).unwrap(), enc.to_string());
32    }
33
34    fn compare_int<B: BinaryText>(b: &B, int: u64, s: &str) {
35        assert_eq!(b.decode_u64(s.as_bytes()).unwrap(), int);
36        assert_eq!(b.encode_u64(int).unwrap(), s.to_string());
37        let int128 = int as u128;
38        assert_eq!(b.decode_u128(s.as_bytes()).unwrap(), int128);
39        assert_eq!(b.encode_u128(int128).unwrap(), s.to_string());
40    }
41
42    #[test]
43    fn base16() {
44        let b = Base16::new();
45        compare(&b, "", "");
46        compare(&b, "f", "66");
47        compare(&b, "fo", "666F");
48        compare(&b, "foo", "666F6F");
49        compare(&b, "foob", "666F6F62");
50        compare(&b, "fooba", "666F6F6261");
51        compare(&b, "foobar", "666F6F626172");
52    }
53
54    #[test]
55    fn base32() {
56        let b = Base32::new();
57        compare(&b, "", "");
58        compare(&b, "f", "MY======");
59        compare(&b, "fo", "MZXQ====");
60        compare(&b, "foo", "MZXW6===");
61        compare(&b, "foob", "MZXW6YQ=");
62        compare(&b, "fooba", "MZXW6YTB");
63        compare(&b, "foobar", "MZXW6YTBOI======");
64    }
65
66    #[test]
67    fn base32hex() {
68        let b = Base32::base32hex();
69        compare(&b, "", "");
70        compare(&b, "f", "CO======");
71        compare(&b, "fo", "CPNG====");
72        compare(&b, "foo", "CPNMU===");
73        compare(&b, "foob", "CPNMUOG=");
74        compare(&b, "fooba", "CPNMUOJ1");
75        compare(&b, "foobar", "CPNMUOJ1E8======");
76    }
77
78    #[test]
79    fn base36() {
80        let b = Base36::new();
81        compare_int(&b, 0u64, "");
82        compare_int(&b, 926192u64, "JUNK");
83        compare_int(&b, 9223372036854775807u64, "1Y2P0IJ32E8E7");
84        assert_eq!(
85            b.encode_from_str("9223372036854775807").unwrap(),
86            "1Y2P0IJ32E8E7".to_string()
87        );
88    }
89
90    #[test]
91    fn base45() {
92        let b = Base45::new();
93        compare(&b, "", "");
94        compare(&b, "AB", "BB8");
95        compare(&b, "base-45", "UJCLQE7W581");
96        compare(&b, "Hello!!", "%69 VD92EX0");
97        compare(&b, "ietf!", "QED8WEX0");
98    }
99
100    #[test]
101    fn base64() {
102        let b = Base64::new();
103        compare(&b, "", "");
104        compare(&b, "f", "Zg==");
105        compare(&b, "fo", "Zm8=");
106        compare(&b, "foo", "Zm9v");
107        compare(&b, "foob", "Zm9vYg==");
108        compare(&b, "fooba", "Zm9vYmE=");
109        compare(&b, "foobar", "Zm9vYmFy");
110    }
111
112    #[test]
113    fn base85() {
114        let b = Base85::new();
115        compare(&b, "", "");
116        compare(&b, "Man ", "9jqo^");
117        compare(&b, "sure", "F*2M7");
118        compare(&b, ".", "/c");
119        compare(
120            &b,
121            "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
122            "9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c",
123        );
124    }
125}