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 decoded to stdout, are provided in the module stream.
8
9pub mod base16;
10pub mod base32;
11pub mod base45;
12pub mod base64;
13pub mod base85;
14pub mod binarytext;
15pub mod error;
16pub mod int;
17pub mod stream;
18
19#[cfg(test)]
20mod tests {
21    use crate::base16::Base16;
22    use crate::base32::Base32;
23    use crate::base45::Base45;
24    use crate::base64::Base64;
25    use crate::base85::Base85;
26    use crate::binarytext::BinaryText;
27    use crate::int::IntEncoder;
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 = IntEncoder::base36();
81        compare_int(&b, 0u64, "");
82        compare_int(&b, 123u64, "3F");
83        compare_int(&b, 926192u64, "JUNK");
84        compare_int(&b, 9223372036854775807u64, "1Y2P0IJ32E8E7");
85        compare_int(&b, 18537u64, "EAX");
86        compare(&b, "", "");
87        compare(&b, "Hi", "EAX");
88    }
89
90    #[test]
91    fn base45() {
92        let b = Base45::new();
93        //Examples taken from RFC 9285
94        compare(&b, "", "");
95        compare(&b, "AB", "BB8");
96        compare(&b, "base-45", "UJCLQE7W581");
97        compare(&b, "Hello!!", "%69 VD92EX0");
98        compare(&b, "ietf!", "QED8WEX0");
99    }
100
101    #[test]
102    fn base62() {
103        let b = IntEncoder::base62();
104        compare_int(&b, 0u64, "");
105        compare_int(&b, 91574294826053u64, "Q0DRQksv");
106        compare(&b, "SIMPLE", "Q0DRQksv");
107    }
108
109    #[test]
110    fn base64() {
111        let b = Base64::new();
112        compare(&b, "", "");
113        compare(&b, "f", "Zg==");
114        compare(&b, "fo", "Zm8=");
115        compare(&b, "foo", "Zm9v");
116        compare(&b, "foob", "Zm9vYg==");
117        compare(&b, "fooba", "Zm9vYmE=");
118        compare(&b, "foobar", "Zm9vYmFy");
119    }
120
121    #[test]
122    fn base85() {
123        let b = Base85::new();
124        // Examples taken from Wikipedia
125        compare(&b, "", "");
126        compare(&b, "Man ", "9jqo^");
127        compare(&b, "sure", "F*2M7");
128        compare(&b, ".", "/c");
129        compare(
130            &b,
131            "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.",
132            "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",
133        );
134    }
135}