@@ -30,22 +30,17 @@ pub enum Alphabet {
30
30
/// Whether to use padding.
31
31
padding : bool
32
32
} ,
33
- /// Crockford base32 encoding.
34
- Crockford ,
35
33
}
36
34
37
35
/// RFC4648 base32 encoding with padding.
38
36
const RFC4648_ALPHABET : & ' static [ u8 ] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" ;
39
37
40
- /// Crockford base32 encoding.
41
- const CROCKFORD_ALPHABET : & ' static [ u8 ] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ" ;
42
-
43
38
/// Encode a byte slice into a base32 string.
44
39
pub fn encode ( alphabet : Alphabet , data : & [ u8 ] ) -> String {
45
40
let ( alphabet, padding) = match alphabet {
46
41
Alphabet :: RFC4648 { padding } => ( RFC4648_ALPHABET , padding) ,
47
- Alphabet :: Crockford => ( CROCKFORD_ALPHABET , false ) ,
48
42
} ;
43
+
49
44
let mut ret = Vec :: with_capacity ( ( data. len ( ) + 3 ) / 4 * 5 ) ;
50
45
51
46
for chunk in data. chunks ( 5 ) {
@@ -87,18 +82,11 @@ const RFC4648_INV_ALPHABET: [i8; 43] = [
87
82
9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 ,
88
83
] ;
89
84
90
- /// Inverse Crockford lookup table for decoding.
91
- const CROCKFORD_INV_ALPHABET : [ i8 ; 43 ] = [
92
- 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 ,
93
- 18 , 19 , 1 , 20 , 21 , 0 , 22 , 23 , 24 , 25 , 26 , -1 , 27 , 28 , 29 , 30 , 31 ,
94
- ] ;
95
-
96
85
/// Decode a base32 string into a byte vector.
97
86
pub fn decode ( alphabet : Alphabet , data : & str ) -> Option < Vec < u8 > > {
98
87
let data = data. as_bytes ( ) ;
99
88
let alphabet = match alphabet {
100
89
Alphabet :: RFC4648 { .. } => RFC4648_INV_ALPHABET ,
101
- Alphabet :: Crockford => CROCKFORD_INV_ALPHABET ,
102
90
} ;
103
91
let mut unpadded_data_length = data. len ( ) ;
104
92
data. iter ( ) . rev ( ) . take ( 6 ) . for_each ( |& c| {
@@ -131,9 +119,8 @@ pub fn decode(alphabet: Alphabet, data: &str) -> Option<Vec<u8>> {
131
119
}
132
120
133
121
#[ cfg( test) ]
134
- #[ allow( dead_code, unused_attributes) ]
135
122
mod test {
136
- use super :: Alphabet :: { Crockford , RFC4648 } ;
123
+ use super :: Alphabet :: RFC4648 ;
137
124
use super :: { decode, encode} ;
138
125
use std:: fmt:: { Debug , Error , Formatter } ;
139
126
@@ -148,26 +135,6 @@ mod test {
148
135
}
149
136
}
150
137
151
- #[ test]
152
- fn masks_crockford ( ) {
153
- assert_eq ! (
154
- encode( Crockford , & [ 0xF8 , 0x3E , 0x0F , 0x83 , 0xE0 ] ) ,
155
- "Z0Z0Z0Z0"
156
- ) ;
157
- assert_eq ! (
158
- encode( Crockford , & [ 0x07 , 0xC1 , 0xF0 , 0x7C , 0x1F ] ) ,
159
- "0Z0Z0Z0Z"
160
- ) ;
161
- assert_eq ! (
162
- decode( Crockford , "Z0Z0Z0Z0" ) . unwrap( ) ,
163
- [ 0xF8 , 0x3E , 0x0F , 0x83 , 0xE0 ]
164
- ) ;
165
- assert_eq ! (
166
- decode( Crockford , "0Z0Z0Z0Z" ) . unwrap( ) ,
167
- [ 0x07 , 0xC1 , 0xF0 , 0x7C , 0x1F ]
168
- ) ;
169
- }
170
-
171
138
#[ test]
172
139
fn masks_rfc4648 ( ) {
173
140
assert_eq ! (
@@ -234,17 +201,6 @@ mod test {
234
201
}
235
202
}
236
203
237
- #[ test]
238
- #[ allow( non_snake_case) ]
239
- fn iIlL1_oO0 ( ) {
240
- assert_eq ! ( decode( Crockford , "IiLlOo" ) , decode( Crockford , "111100" ) ) ;
241
- }
242
-
243
- #[ test]
244
- fn invalid_chars_crockford ( ) {
245
- assert_eq ! ( decode( Crockford , "," ) , None )
246
- }
247
-
248
204
#[ test]
249
205
fn invalid_chars_rfc4648 ( ) {
250
206
assert_eq ! ( decode( RFC4648 { padding: true } , "," ) , None )
0 commit comments