1 //! Efficient and customizable data-encoding functions like base64, base32, and hex
2 //!
3 //! This [crate] provides little-endian ASCII base-conversion encodings for
4 //! bases of size 2, 4, 8, 16, 32, and 64. It supports:
5 //!
6 //! - [padding] for streaming
7 //! - canonical encodings (e.g. [trailing bits] are checked)
8 //! - in-place [encoding] and [decoding] functions
9 //! - partial [decoding] functions (e.g. for error recovery)
10 //! - character [translation] (e.g. for case-insensitivity)
11 //! - most and least significant [bit-order]
12 //! - [ignoring] characters when decoding (e.g. for skipping newlines)
13 //! - [wrapping] the output when encoding
14 //! - no-std environments with `default-features = false, features = ["alloc"]`
15 //! - no-alloc environments with `default-features = false`
16 //!
17 //! You may use the [binary] or the [website] to play around.
18 //!
19 //! # Examples
20 //!
21 //! This crate provides predefined encodings as [constants]. These constants are of type
22 //! [`Encoding`]. This type provides encoding and decoding functions with in-place or allocating
23 //! variants. Here is an example using the allocating encoding function of [`BASE64`]:
24 //!
25 //! ```rust
26 //! use data_encoding::BASE64;
27 //! assert_eq!(BASE64.encode(b"Hello world"), "SGVsbG8gd29ybGQ=");
28 //! ```
29 //!
30 //! Here is an example using the in-place decoding function of [`BASE32`]:
31 //!
32 //! ```rust
33 //! use data_encoding::BASE32;
34 //! let input = b"JBSWY3DPEB3W64TMMQ======";
35 //! let mut output = vec![0; BASE32.decode_len(input.len()).unwrap()];
36 //! let len = BASE32.decode_mut(input, &mut output).unwrap();
37 //! assert_eq!(&output[0 .. len], b"Hello world");
38 //! ```
39 //!
40 //! You are not limited to the predefined encodings. You may define your own encodings (with the
41 //! same correctness and performance properties as the predefined ones) using the [`Specification`]
42 //! type:
43 //!
44 //! ```rust
45 //! use data_encoding::Specification;
46 //! let hex = {
47 //!     let mut spec = Specification::new();
48 //!     spec.symbols.push_str("0123456789abcdef");
49 //!     spec.encoding().unwrap()
50 //! };
51 //! assert_eq!(hex.encode(b"hello"), "68656c6c6f");
52 //! ```
53 //!
54 //! You may use the [macro] library to define a compile-time custom encoding:
55 //!
56 //! ```rust,ignore
57 //! use data_encoding::Encoding;
58 //! use data_encoding_macro::new_encoding;
59 //! const HEX: Encoding = new_encoding!{
60 //!     symbols: "0123456789abcdef",
61 //!     translate_from: "ABCDEF",
62 //!     translate_to: "abcdef",
63 //! };
64 //! const BASE64: Encoding = new_encoding!{
65 //!     symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
66 //!     padding: '=',
67 //! };
68 //! ```
69 //!
70 //! # Properties
71 //!
72 //! The [`HEXUPPER`], [`BASE32`], [`BASE32HEX`], [`BASE64`], and [`BASE64URL`] predefined encodings
73 //! conform to [RFC4648].
74 //!
75 //! In general, the encoding and decoding functions satisfy the following properties:
76 //!
77 //! - They are deterministic: their output only depends on their input
78 //! - They have no side-effects: they do not modify any hidden mutable state
79 //! - They are correct: encoding followed by decoding gives the initial data
80 //! - They are canonical (unless [`is_canonical`] returns false): decoding followed by encoding
81 //!   gives the initial data
82 //!
83 //! This last property is usually not satisfied by base64 implementations. This is a matter of
84 //! choice and this crate has made the choice to let the user choose. Support for canonical encoding
85 //! as described by the [RFC][canonical] is provided. But it is also possible to disable checking
86 //! trailing bits, to add characters translation, to decode concatenated padded inputs, and to
87 //! ignore some characters. Note that non-canonical encodings may be an attack vector as described
88 //! in [Base64 Malleability in Practice](https://eprint.iacr.org/2022/361.pdf).
89 //!
90 //! Since the RFC specifies the encoding function on all inputs and the decoding function on all
91 //! possible encoded outputs, the differences between implementations come from the decoding
92 //! function which may be more or less permissive. In this crate, the decoding function of canonical
93 //! encodings rejects all inputs that are not a possible output of the encoding function. Here are
94 //! some concrete examples of decoding differences between this crate, the `base64` crate, and the
95 //! `base64` GNU program:
96 //!
97 //! | Input      | `data-encoding` | `base64`  | GNU `base64`  |
98 //! | ---------- | --------------- | --------- | ------------- |
99 //! | `AAB=`     | `Trailing(2)`   | `Last(2)` | `\x00\x00`    |
100 //! | `AA\nB=`   | `Length(4)`     | `Byte(2)` | `\x00\x00`    |
101 //! | `AAB`      | `Length(0)`     | `Padding` | Invalid input |
102 //! | `AAA`      | `Length(0)`     | `Padding` | Invalid input |
103 //! | `A\rA\nB=` | `Length(4)`     | `Byte(1)` | Invalid input |
104 //! | `-_\r\n`   | `Symbol(0)`     | `Byte(0)` | Invalid input |
105 //! | `AA==AA==` | `[0, 0]`        | `Byte(2)` | `\x00\x00`    |
106 //!
107 //! We can summarize these discrepancies as follows:
108 //!
109 //! | Discrepancy                | `data-encoding` | `base64` | GNU `base64` |
110 //! | -------------------------- | --------------- | -------- | ------------ |
111 //! | Check trailing bits        | Yes             | Yes      | No           |
112 //! | Ignored characters         | None            | None     | `\n`         |
113 //! | Translated characters      | None            | None     | None         |
114 //! | Check padding              | Yes             | No       | Yes          |
115 //! | Support concatenated input | Yes             | No       | Yes          |
116 //!
117 //! This crate permits to disable checking trailing bits. It permits to ignore some characters. It
118 //! permits to translate characters. It permits to use unpadded encodings. However, for padded
119 //! encodings, support for concatenated inputs cannot be disabled. This is simply because it doesn't
120 //! make sense to use padding if it is not to support concatenated inputs.
121 //!
122 //! [RFC4648]: https://tools.ietf.org/html/rfc4648
123 //! [`BASE32HEX`]: constant.BASE32HEX.html
124 //! [`BASE32`]: constant.BASE32.html
125 //! [`BASE64URL`]: constant.BASE64URL.html
126 //! [`BASE64`]: constant.BASE64.html
127 //! [`Encoding`]: struct.Encoding.html
128 //! [`HEXUPPER`]: constant.HEXUPPER.html
129 //! [`Specification`]: struct.Specification.html
130 //! [`is_canonical`]: struct.Encoding.html#method.is_canonical
131 //! [binary]: https://crates.io/crates/data-encoding-bin
132 //! [bit-order]: struct.Specification.html#structfield.bit_order
133 //! [canonical]: https://tools.ietf.org/html/rfc4648#section-3.5
134 //! [constants]: index.html#constants
135 //! [crate]: https://crates.io/crates/data-encoding
136 //! [decoding]: struct.Encoding.html#method.decode_mut
137 //! [encoding]: struct.Encoding.html#method.encode_mut
138 //! [ignoring]: struct.Specification.html#structfield.ignore
139 //! [macro]: https://crates.io/crates/data-encoding-macro
140 //! [padding]: struct.Specification.html#structfield.padding
141 //! [trailing bits]: struct.Specification.html#structfield.check_trailing_bits
142 //! [translation]: struct.Specification.html#structfield.translate
143 //! [website]: https://data-encoding.rs
144 //! [wrapping]: struct.Specification.html#structfield.wrap
145 
146 #![no_std]
147 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
148 // TODO: This list up to warn(clippy::pedantic) should ideally use a lint group.
149 #![warn(elided_lifetimes_in_paths)]
150 // TODO(msrv): #![warn(let_underscore_drop)]
151 #![warn(missing_debug_implementations)]
152 #![warn(missing_docs)]
153 #![warn(unreachable_pub)]
154 // TODO(msrv): #![warn(unsafe_op_in_unsafe_fn)]
155 #![warn(unused_results)]
156 #![allow(unused_unsafe)] // TODO(msrv)
157 #![warn(clippy::pedantic)]
158 #![allow(clippy::assigning_clones)] // TODO(msrv)
159 #![allow(clippy::doc_markdown)]
160 #![allow(clippy::enum_glob_use)]
161 #![allow(clippy::similar_names)]
162 #![allow(clippy::uninlined_format_args)] // TODO(msrv)
163 
164 #[cfg(feature = "alloc")]
165 extern crate alloc;
166 #[cfg(feature = "std")]
167 extern crate std;
168 
169 #[cfg(feature = "alloc")]
170 use alloc::borrow::{Cow, ToOwned};
171 #[cfg(feature = "alloc")]
172 use alloc::string::String;
173 #[cfg(feature = "alloc")]
174 use alloc::vec;
175 #[cfg(feature = "alloc")]
176 use alloc::vec::Vec;
177 use core::convert::TryInto;
178 
179 macro_rules! check {
180     ($e: expr, $c: expr) => {
181         if !$c {
182             return Err($e);
183         }
184     };
185 }
186 
187 trait Static<T: Copy>: Copy {
val(self) -> T188     fn val(self) -> T;
189 }
190 
191 macro_rules! define {
192     ($name: ident: $type: ty = $val: expr) => {
193         #[derive(Copy, Clone)]
194         struct $name;
195         impl Static<$type> for $name {
196             fn val(self) -> $type {
197                 $val
198             }
199         }
200     };
201 }
202 
203 define!(Bf: bool = false);
204 define!(Bt: bool = true);
205 define!(N1: usize = 1);
206 define!(N2: usize = 2);
207 define!(N3: usize = 3);
208 define!(N4: usize = 4);
209 define!(N5: usize = 5);
210 define!(N6: usize = 6);
211 
212 #[derive(Copy, Clone)]
213 struct On;
214 
215 impl<T: Copy> Static<Option<T>> for On {
val(self) -> Option<T>216     fn val(self) -> Option<T> {
217         None
218     }
219 }
220 
221 #[derive(Copy, Clone)]
222 struct Os<T>(T);
223 
224 impl<T: Copy> Static<Option<T>> for Os<T> {
val(self) -> Option<T>225     fn val(self) -> Option<T> {
226         Some(self.0)
227     }
228 }
229 
230 macro_rules! dispatch {
231     (let $var: ident: bool = $val: expr; $($body: tt)*) => {
232         if $val {
233             let $var = Bt; dispatch!($($body)*)
234         } else {
235             let $var = Bf; dispatch!($($body)*)
236         }
237     };
238     (let $var: ident: usize = $val: expr; $($body: tt)*) => {
239         match $val {
240             1 => { let $var = N1; dispatch!($($body)*) },
241             2 => { let $var = N2; dispatch!($($body)*) },
242             3 => { let $var = N3; dispatch!($($body)*) },
243             4 => { let $var = N4; dispatch!($($body)*) },
244             5 => { let $var = N5; dispatch!($($body)*) },
245             6 => { let $var = N6; dispatch!($($body)*) },
246             _ => panic!(),
247         }
248     };
249     (let $var: ident: Option<$type: ty> = $val: expr; $($body: tt)*) => {
250         match $val {
251             None => { let $var = On; dispatch!($($body)*) },
252             Some(x) => { let $var = Os(x); dispatch!($($body)*) },
253         }
254     };
255     ($body: expr) => { $body };
256 }
257 
chunk_unchecked(x: &[u8], n: usize, i: usize) -> &[u8]258 unsafe fn chunk_unchecked(x: &[u8], n: usize, i: usize) -> &[u8] {
259     debug_assert!((i + 1) * n <= x.len());
260     unsafe { core::slice::from_raw_parts(x.as_ptr().add(n * i), n) }
261 }
262 
chunk_mut_unchecked(x: &mut [u8], n: usize, i: usize) -> &mut [u8]263 unsafe fn chunk_mut_unchecked(x: &mut [u8], n: usize, i: usize) -> &mut [u8] {
264     debug_assert!((i + 1) * n <= x.len());
265     unsafe { core::slice::from_raw_parts_mut(x.as_mut_ptr().add(n * i), n) }
266 }
267 
div_ceil(x: usize, m: usize) -> usize268 fn div_ceil(x: usize, m: usize) -> usize {
269     (x + m - 1) / m
270 }
271 
floor(x: usize, m: usize) -> usize272 fn floor(x: usize, m: usize) -> usize {
273     x / m * m
274 }
275 
vectorize<F: FnMut(usize)>(n: usize, bs: usize, mut f: F)276 fn vectorize<F: FnMut(usize)>(n: usize, bs: usize, mut f: F) {
277     for k in 0 .. n / bs {
278         for i in k * bs .. (k + 1) * bs {
279             f(i);
280         }
281     }
282     for i in floor(n, bs) .. n {
283         f(i);
284     }
285 }
286 
287 /// Decoding error kind
288 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
289 pub enum DecodeKind {
290     /// Invalid length
291     Length,
292 
293     /// Invalid symbol
294     Symbol,
295 
296     /// Non-zero trailing bits
297     Trailing,
298 
299     /// Invalid padding length
300     Padding,
301 }
302 
303 impl core::fmt::Display for DecodeKind {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result304     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
305         let description = match self {
306             DecodeKind::Length => "invalid length",
307             DecodeKind::Symbol => "invalid symbol",
308             DecodeKind::Trailing => "non-zero trailing bits",
309             DecodeKind::Padding => "invalid padding length",
310         };
311         write!(f, "{}", description)
312     }
313 }
314 
315 /// Decoding error
316 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
317 pub struct DecodeError {
318     /// Error position
319     ///
320     /// This position is always a valid input position and represents the first encountered error.
321     pub position: usize,
322 
323     /// Error kind
324     pub kind: DecodeKind,
325 }
326 
327 #[cfg(feature = "std")]
328 impl std::error::Error for DecodeError {}
329 
330 impl core::fmt::Display for DecodeError {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result331     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
332         write!(f, "{} at {}", self.kind, self.position)
333     }
334 }
335 
336 /// Decoding error with partial result
337 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
338 pub struct DecodePartial {
339     /// Number of bytes read from input
340     ///
341     /// This number does not exceed the error position: `read <= error.position`.
342     pub read: usize,
343 
344     /// Number of bytes written to output
345     ///
346     /// This number does not exceed the decoded length: `written <= decode_len(read)`.
347     pub written: usize,
348 
349     /// Decoding error
350     pub error: DecodeError,
351 }
352 
353 const INVALID: u8 = 128;
354 const IGNORE: u8 = 129;
355 const PADDING: u8 = 130;
356 
order(msb: bool, n: usize, i: usize) -> usize357 fn order(msb: bool, n: usize, i: usize) -> usize {
358     if msb {
359         n - 1 - i
360     } else {
361         i
362     }
363 }
364 
enc(bit: usize) -> usize365 fn enc(bit: usize) -> usize {
366     match bit {
367         1 | 2 | 4 => 1,
368         3 | 6 => 3,
369         5 => 5,
370         _ => unreachable!(),
371     }
372 }
373 
dec(bit: usize) -> usize374 fn dec(bit: usize) -> usize {
375     enc(bit) * 8 / bit
376 }
377 
encode_len<B: Static<usize>>(bit: B, len: usize) -> usize378 fn encode_len<B: Static<usize>>(bit: B, len: usize) -> usize {
379     div_ceil(8 * len, bit.val())
380 }
381 
encode_block<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8], )382 fn encode_block<B: Static<usize>, M: Static<bool>>(
383     bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8],
384 ) {
385     debug_assert!(input.len() <= enc(bit.val()));
386     debug_assert_eq!(output.len(), encode_len(bit, input.len()));
387     let bit = bit.val();
388     let msb = msb.val();
389     let mut x = 0u64;
390     for (i, input) in input.iter().enumerate() {
391         x |= u64::from(*input) << (8 * order(msb, enc(bit), i));
392     }
393     for (i, output) in output.iter_mut().enumerate() {
394         let y = x >> (bit * order(msb, dec(bit), i));
395         *output = symbols[(y & 0xff) as usize];
396     }
397 }
398 
encode_mut<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8], )399 fn encode_mut<B: Static<usize>, M: Static<bool>>(
400     bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8],
401 ) {
402     debug_assert_eq!(output.len(), encode_len(bit, input.len()));
403     let enc = enc(bit.val());
404     let dec = dec(bit.val());
405     let n = input.len() / enc;
406     let bs = match bit.val() {
407         5 => 2,
408         6 => 4,
409         _ => 1,
410     };
411     vectorize(n, bs, |i| {
412         let input = unsafe { chunk_unchecked(input, enc, i) };
413         let output = unsafe { chunk_mut_unchecked(output, dec, i) };
414         encode_block(bit, msb, symbols, input, output);
415     });
416     encode_block(bit, msb, symbols, &input[enc * n ..], &mut output[dec * n ..]);
417 }
418 
419 // Fails if an input character does not translate to a symbol. The error is the
420 // lowest index of such character. The output is not written to.
decode_block<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, values: &[u8; 256], input: &[u8], output: &mut [u8], ) -> Result<(), usize>421 fn decode_block<B: Static<usize>, M: Static<bool>>(
422     bit: B, msb: M, values: &[u8; 256], input: &[u8], output: &mut [u8],
423 ) -> Result<(), usize> {
424     debug_assert!(output.len() <= enc(bit.val()));
425     debug_assert_eq!(input.len(), encode_len(bit, output.len()));
426     let bit = bit.val();
427     let msb = msb.val();
428     let mut x = 0u64;
429     for j in 0 .. input.len() {
430         let y = values[input[j] as usize];
431         check!(j, y < 1 << bit);
432         x |= u64::from(y) << (bit * order(msb, dec(bit), j));
433     }
434     for (j, output) in output.iter_mut().enumerate() {
435         *output = (x >> (8 * order(msb, enc(bit), j)) & 0xff) as u8;
436     }
437     Ok(())
438 }
439 
440 // Fails if an input character does not translate to a symbol. The error `pos`
441 // is the lowest index of such character. The output is valid up to `pos / dec *
442 // enc` excluded.
decode_mut<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, values: &[u8; 256], input: &[u8], output: &mut [u8], ) -> Result<(), usize>443 fn decode_mut<B: Static<usize>, M: Static<bool>>(
444     bit: B, msb: M, values: &[u8; 256], input: &[u8], output: &mut [u8],
445 ) -> Result<(), usize> {
446     debug_assert_eq!(input.len(), encode_len(bit, output.len()));
447     let enc = enc(bit.val());
448     let dec = dec(bit.val());
449     let n = input.len() / dec;
450     for i in 0 .. n {
451         let input = unsafe { chunk_unchecked(input, dec, i) };
452         let output = unsafe { chunk_mut_unchecked(output, enc, i) };
453         decode_block(bit, msb, values, input, output).map_err(|e| dec * i + e)?;
454     }
455     decode_block(bit, msb, values, &input[dec * n ..], &mut output[enc * n ..])
456         .map_err(|e| dec * n + e)
457 }
458 
459 // Fails if there are non-zero trailing bits.
check_trail<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, ctb: bool, values: &[u8; 256], input: &[u8], ) -> Result<(), ()>460 fn check_trail<B: Static<usize>, M: Static<bool>>(
461     bit: B, msb: M, ctb: bool, values: &[u8; 256], input: &[u8],
462 ) -> Result<(), ()> {
463     if 8 % bit.val() == 0 || !ctb {
464         return Ok(());
465     }
466     let trail = bit.val() * input.len() % 8;
467     if trail == 0 {
468         return Ok(());
469     }
470     let mut mask = (1 << trail) - 1;
471     if !msb.val() {
472         mask <<= bit.val() - trail;
473     }
474     check!((), values[input[input.len() - 1] as usize] & mask == 0);
475     Ok(())
476 }
477 
478 // Fails if the padding length is invalid. The error is the index of the first
479 // padding character.
check_pad<B: Static<usize>>(bit: B, values: &[u8; 256], input: &[u8]) -> Result<usize, usize>480 fn check_pad<B: Static<usize>>(bit: B, values: &[u8; 256], input: &[u8]) -> Result<usize, usize> {
481     let bit = bit.val();
482     debug_assert_eq!(input.len(), dec(bit));
483     let is_pad = |x: &&u8| values[**x as usize] == PADDING;
484     let count = input.iter().rev().take_while(is_pad).count();
485     let len = input.len() - count;
486     check!(len, len > 0 && bit * len % 8 < bit);
487     Ok(len)
488 }
489 
encode_base_len<B: Static<usize>>(bit: B, len: usize) -> usize490 fn encode_base_len<B: Static<usize>>(bit: B, len: usize) -> usize {
491     encode_len(bit, len)
492 }
493 
encode_base<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8], )494 fn encode_base<B: Static<usize>, M: Static<bool>>(
495     bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8],
496 ) {
497     debug_assert_eq!(output.len(), encode_base_len(bit, input.len()));
498     encode_mut(bit, msb, symbols, input, output);
499 }
500 
encode_pad_len<B: Static<usize>, P: Static<Option<u8>>>(bit: B, pad: P, len: usize) -> usize501 fn encode_pad_len<B: Static<usize>, P: Static<Option<u8>>>(bit: B, pad: P, len: usize) -> usize {
502     match pad.val() {
503         None => encode_base_len(bit, len),
504         Some(_) => div_ceil(len, enc(bit.val())) * dec(bit.val()),
505     }
506 }
507 
encode_pad<B: Static<usize>, M: Static<bool>, P: Static<Option<u8>>>( bit: B, msb: M, symbols: &[u8; 256], spad: P, input: &[u8], output: &mut [u8], )508 fn encode_pad<B: Static<usize>, M: Static<bool>, P: Static<Option<u8>>>(
509     bit: B, msb: M, symbols: &[u8; 256], spad: P, input: &[u8], output: &mut [u8],
510 ) {
511     let pad = match spad.val() {
512         None => return encode_base(bit, msb, symbols, input, output),
513         Some(pad) => pad,
514     };
515     debug_assert_eq!(output.len(), encode_pad_len(bit, spad, input.len()));
516     let olen = encode_base_len(bit, input.len());
517     encode_base(bit, msb, symbols, input, &mut output[.. olen]);
518     for output in output.iter_mut().skip(olen) {
519         *output = pad;
520     }
521 }
522 
encode_wrap_len< 'a, B: Static<usize>, P: Static<Option<u8>>, W: Static<Option<(usize, &'a [u8])>>, >( bit: B, pad: P, wrap: W, ilen: usize, ) -> usize523 fn encode_wrap_len<
524     'a,
525     B: Static<usize>,
526     P: Static<Option<u8>>,
527     W: Static<Option<(usize, &'a [u8])>>,
528 >(
529     bit: B, pad: P, wrap: W, ilen: usize,
530 ) -> usize {
531     let olen = encode_pad_len(bit, pad, ilen);
532     match wrap.val() {
533         None => olen,
534         Some((col, end)) => olen + end.len() * div_ceil(olen, col),
535     }
536 }
537 
encode_wrap_mut< 'a, B: Static<usize>, M: Static<bool>, P: Static<Option<u8>>, W: Static<Option<(usize, &'a [u8])>>, >( bit: B, msb: M, symbols: &[u8; 256], pad: P, wrap: W, input: &[u8], output: &mut [u8], )538 fn encode_wrap_mut<
539     'a,
540     B: Static<usize>,
541     M: Static<bool>,
542     P: Static<Option<u8>>,
543     W: Static<Option<(usize, &'a [u8])>>,
544 >(
545     bit: B, msb: M, symbols: &[u8; 256], pad: P, wrap: W, input: &[u8], output: &mut [u8],
546 ) {
547     let (col, end) = match wrap.val() {
548         None => return encode_pad(bit, msb, symbols, pad, input, output),
549         Some((col, end)) => (col, end),
550     };
551     debug_assert_eq!(output.len(), encode_wrap_len(bit, pad, wrap, input.len()));
552     debug_assert_eq!(col % dec(bit.val()), 0);
553     let col = col / dec(bit.val());
554     let enc = col * enc(bit.val());
555     let dec = col * dec(bit.val()) + end.len();
556     let olen = dec - end.len();
557     let n = input.len() / enc;
558     for i in 0 .. n {
559         let input = unsafe { chunk_unchecked(input, enc, i) };
560         let output = unsafe { chunk_mut_unchecked(output, dec, i) };
561         encode_base(bit, msb, symbols, input, &mut output[.. olen]);
562         output[olen ..].copy_from_slice(end);
563     }
564     if input.len() > enc * n {
565         let olen = dec * n + encode_pad_len(bit, pad, input.len() - enc * n);
566         encode_pad(bit, msb, symbols, pad, &input[enc * n ..], &mut output[dec * n .. olen]);
567         output[olen ..].copy_from_slice(end);
568     }
569 }
570 
571 // Returns the longest valid input length and associated output length.
decode_wrap_len<B: Static<usize>, P: Static<bool>>( bit: B, pad: P, len: usize, ) -> (usize, usize)572 fn decode_wrap_len<B: Static<usize>, P: Static<bool>>(
573     bit: B, pad: P, len: usize,
574 ) -> (usize, usize) {
575     let bit = bit.val();
576     if pad.val() {
577         (floor(len, dec(bit)), len / dec(bit) * enc(bit))
578     } else {
579         let trail = bit * len % 8;
580         (len - trail / bit, bit * len / 8)
581     }
582 }
583 
584 // Fails with Length if length is invalid. The error is the largest valid
585 // length.
decode_pad_len<B: Static<usize>, P: Static<bool>>( bit: B, pad: P, len: usize, ) -> Result<usize, DecodeError>586 fn decode_pad_len<B: Static<usize>, P: Static<bool>>(
587     bit: B, pad: P, len: usize,
588 ) -> Result<usize, DecodeError> {
589     let (ilen, olen) = decode_wrap_len(bit, pad, len);
590     check!(DecodeError { position: ilen, kind: DecodeKind::Length }, ilen == len);
591     Ok(olen)
592 }
593 
594 // Fails with Length if length is invalid. The error is the largest valid
595 // length.
decode_base_len<B: Static<usize>>(bit: B, len: usize) -> Result<usize, DecodeError>596 fn decode_base_len<B: Static<usize>>(bit: B, len: usize) -> Result<usize, DecodeError> {
597     decode_pad_len(bit, Bf, len)
598 }
599 
600 // Fails with Symbol if an input character does not translate to a symbol. The
601 // error is the lowest index of such character.
602 // Fails with Trailing if there are non-zero trailing bits.
decode_base_mut<B: Static<usize>, M: Static<bool>>( bit: B, msb: M, ctb: bool, values: &[u8; 256], input: &[u8], output: &mut [u8], ) -> Result<usize, DecodePartial>603 fn decode_base_mut<B: Static<usize>, M: Static<bool>>(
604     bit: B, msb: M, ctb: bool, values: &[u8; 256], input: &[u8], output: &mut [u8],
605 ) -> Result<usize, DecodePartial> {
606     debug_assert_eq!(Ok(output.len()), decode_base_len(bit, input.len()));
607     let fail = |pos, kind| DecodePartial {
608         read: pos / dec(bit.val()) * dec(bit.val()),
609         written: pos / dec(bit.val()) * enc(bit.val()),
610         error: DecodeError { position: pos, kind },
611     };
612     decode_mut(bit, msb, values, input, output).map_err(|pos| fail(pos, DecodeKind::Symbol))?;
613     check_trail(bit, msb, ctb, values, input)
614         .map_err(|()| fail(input.len() - 1, DecodeKind::Trailing))?;
615     Ok(output.len())
616 }
617 
618 // Fails with Symbol if an input character does not translate to a symbol. The
619 // error is the lowest index of such character.
620 // Fails with Padding if some padding length is invalid. The error is the index
621 // of the first padding character of the invalid padding.
622 // Fails with Trailing if there are non-zero trailing bits.
decode_pad_mut<B: Static<usize>, M: Static<bool>, P: Static<bool>>( bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, input: &[u8], output: &mut [u8], ) -> Result<usize, DecodePartial>623 fn decode_pad_mut<B: Static<usize>, M: Static<bool>, P: Static<bool>>(
624     bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, input: &[u8], output: &mut [u8],
625 ) -> Result<usize, DecodePartial> {
626     if !pad.val() {
627         return decode_base_mut(bit, msb, ctb, values, input, output);
628     }
629     debug_assert_eq!(Ok(output.len()), decode_pad_len(bit, pad, input.len()));
630     let enc = enc(bit.val());
631     let dec = dec(bit.val());
632     let mut inpos = 0;
633     let mut outpos = 0;
634     let mut outend = output.len();
635     while inpos < input.len() {
636         match decode_base_mut(
637             bit,
638             msb,
639             ctb,
640             values,
641             &input[inpos ..],
642             &mut output[outpos .. outend],
643         ) {
644             Ok(written) => {
645                 if cfg!(debug_assertions) {
646                     inpos = input.len();
647                 }
648                 outpos += written;
649                 break;
650             }
651             Err(partial) => {
652                 inpos += partial.read;
653                 outpos += partial.written;
654             }
655         }
656         let inlen =
657             check_pad(bit, values, &input[inpos .. inpos + dec]).map_err(|pos| DecodePartial {
658                 read: inpos,
659                 written: outpos,
660                 error: DecodeError { position: inpos + pos, kind: DecodeKind::Padding },
661             })?;
662         let outlen = decode_base_len(bit, inlen).unwrap();
663         let written = decode_base_mut(
664             bit,
665             msb,
666             ctb,
667             values,
668             &input[inpos .. inpos + inlen],
669             &mut output[outpos .. outpos + outlen],
670         )
671         .map_err(|partial| {
672             debug_assert_eq!(partial.read, 0);
673             debug_assert_eq!(partial.written, 0);
674             DecodePartial {
675                 read: inpos,
676                 written: outpos,
677                 error: DecodeError {
678                     position: inpos + partial.error.position,
679                     kind: partial.error.kind,
680                 },
681             }
682         })?;
683         debug_assert_eq!(written, outlen);
684         inpos += dec;
685         outpos += outlen;
686         outend -= enc - outlen;
687     }
688     debug_assert_eq!(inpos, input.len());
689     debug_assert_eq!(outpos, outend);
690     Ok(outend)
691 }
692 
skip_ignore(values: &[u8; 256], input: &[u8], mut inpos: usize) -> usize693 fn skip_ignore(values: &[u8; 256], input: &[u8], mut inpos: usize) -> usize {
694     while inpos < input.len() && values[input[inpos] as usize] == IGNORE {
695         inpos += 1;
696     }
697     inpos
698 }
699 
700 // Returns next input and output position.
701 // Fails with Symbol if an input character does not translate to a symbol. The
702 // error is the lowest index of such character.
703 // Fails with Padding if some padding length is invalid. The error is the index
704 // of the first padding character of the invalid padding.
705 // Fails with Trailing if there are non-zero trailing bits.
decode_wrap_block<B: Static<usize>, M: Static<bool>, P: Static<bool>>( bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, input: &[u8], output: &mut [u8], ) -> Result<(usize, usize), DecodeError>706 fn decode_wrap_block<B: Static<usize>, M: Static<bool>, P: Static<bool>>(
707     bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, input: &[u8], output: &mut [u8],
708 ) -> Result<(usize, usize), DecodeError> {
709     let dec = dec(bit.val());
710     let mut buf = [0u8; 8];
711     let mut shift = [0usize; 8];
712     let mut bufpos = 0;
713     let mut inpos = 0;
714     while bufpos < dec {
715         inpos = skip_ignore(values, input, inpos);
716         if inpos == input.len() {
717             break;
718         }
719         shift[bufpos] = inpos;
720         buf[bufpos] = input[inpos];
721         bufpos += 1;
722         inpos += 1;
723     }
724     let olen = decode_pad_len(bit, pad, bufpos).map_err(|mut e| {
725         e.position = shift[e.position];
726         e
727     })?;
728     let written = decode_pad_mut(bit, msb, ctb, values, pad, &buf[.. bufpos], &mut output[.. olen])
729         .map_err(|partial| {
730             debug_assert_eq!(partial.read, 0);
731             debug_assert_eq!(partial.written, 0);
732             DecodeError { position: shift[partial.error.position], kind: partial.error.kind }
733         })?;
734     Ok((inpos, written))
735 }
736 
737 // Fails with Symbol if an input character does not translate to a symbol. The
738 // error is the lowest index of such character.
739 // Fails with Padding if some padding length is invalid. The error is the index
740 // of the first padding character of the invalid padding.
741 // Fails with Trailing if there are non-zero trailing bits.
742 // Fails with Length if input length (without ignored characters) is invalid.
743 #[allow(clippy::too_many_arguments)]
decode_wrap_mut<B: Static<usize>, M: Static<bool>, P: Static<bool>, I: Static<bool>>( bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, has_ignore: I, input: &[u8], output: &mut [u8], ) -> Result<usize, DecodePartial>744 fn decode_wrap_mut<B: Static<usize>, M: Static<bool>, P: Static<bool>, I: Static<bool>>(
745     bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, has_ignore: I, input: &[u8],
746     output: &mut [u8],
747 ) -> Result<usize, DecodePartial> {
748     if !has_ignore.val() {
749         return decode_pad_mut(bit, msb, ctb, values, pad, input, output);
750     }
751     debug_assert_eq!(output.len(), decode_wrap_len(bit, pad, input.len()).1);
752     let mut inpos = 0;
753     let mut outpos = 0;
754     while inpos < input.len() {
755         let (inlen, outlen) = decode_wrap_len(bit, pad, input.len() - inpos);
756         match decode_pad_mut(
757             bit,
758             msb,
759             ctb,
760             values,
761             pad,
762             &input[inpos .. inpos + inlen],
763             &mut output[outpos .. outpos + outlen],
764         ) {
765             Ok(written) => {
766                 inpos += inlen;
767                 outpos += written;
768                 break;
769             }
770             Err(partial) => {
771                 inpos += partial.read;
772                 outpos += partial.written;
773             }
774         }
775         let (ipos, opos) =
776             decode_wrap_block(bit, msb, ctb, values, pad, &input[inpos ..], &mut output[outpos ..])
777                 .map_err(|mut error| {
778                     error.position += inpos;
779                     DecodePartial { read: inpos, written: outpos, error }
780                 })?;
781         inpos += ipos;
782         outpos += opos;
783     }
784     let inpos = skip_ignore(values, input, inpos);
785     if inpos == input.len() {
786         Ok(outpos)
787     } else {
788         Err(DecodePartial {
789             read: inpos,
790             written: outpos,
791             error: DecodeError { position: inpos, kind: DecodeKind::Length },
792         })
793     }
794 }
795 
796 /// Order in which bits are read from a byte
797 ///
798 /// The base-conversion encoding is always little-endian. This means that the least significant
799 /// **byte** is always first. However, we can still choose whether, within a byte, this is the most
800 /// significant or the least significant **bit** that is first. If the terminology is confusing,
801 /// testing on an asymmetrical example should be enough to choose the correct value.
802 ///
803 /// # Examples
804 ///
805 /// In the following example, we can see that a base with the `MostSignificantFirst` bit-order has
806 /// the most significant bit first in the encoded output. In particular, the output is in the same
807 /// order as the bits in the byte. The opposite happens with the `LeastSignificantFirst` bit-order.
808 /// The least significant bit is first and the output is in the reverse order.
809 ///
810 /// ```rust
811 /// use data_encoding::{BitOrder, Specification};
812 /// let mut spec = Specification::new();
813 /// spec.symbols.push_str("01");
814 /// spec.bit_order = BitOrder::MostSignificantFirst;  // default
815 /// let msb = spec.encoding().unwrap();
816 /// spec.bit_order = BitOrder::LeastSignificantFirst;
817 /// let lsb = spec.encoding().unwrap();
818 /// assert_eq!(msb.encode(&[0b01010011]), "01010011");
819 /// assert_eq!(lsb.encode(&[0b01010011]), "11001010");
820 /// ```
821 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
822 #[cfg(feature = "alloc")]
823 pub enum BitOrder {
824     /// Most significant bit first
825     ///
826     /// This is the most common and most intuitive bit-order. In particular, this is the bit-order
827     /// used by [RFC4648] and thus the usual hexadecimal, base64, base32, base64url, and base32hex
828     /// encodings. This is the default bit-order when [specifying](struct.Specification.html) a
829     /// base.
830     ///
831     /// [RFC4648]: https://tools.ietf.org/html/rfc4648
832     MostSignificantFirst,
833 
834     /// Least significant bit first
835     ///
836     /// # Examples
837     ///
838     /// DNSCurve [base32] uses least significant bit first:
839     ///
840     /// ```rust
841     /// use data_encoding::BASE32_DNSCURVE;
842     /// assert_eq!(BASE32_DNSCURVE.encode(&[0x64, 0x88]), "4321");
843     /// assert_eq!(BASE32_DNSCURVE.decode(b"4321").unwrap(), vec![0x64, 0x88]);
844     /// ```
845     ///
846     /// [base32]: constant.BASE32_DNSCURVE.html
847     LeastSignificantFirst,
848 }
849 #[cfg(feature = "alloc")]
850 use crate::BitOrder::*;
851 
852 #[doc(hidden)]
853 #[cfg(feature = "alloc")]
854 pub type InternalEncoding = Cow<'static, [u8]>;
855 
856 #[doc(hidden)]
857 #[cfg(not(feature = "alloc"))]
858 pub type InternalEncoding = &'static [u8];
859 
860 /// Base-conversion encoding
861 ///
862 /// See [Specification](struct.Specification.html) for technical details or how to define a new one.
863 // Required fields:
864 //   0 - 256 (256) symbols
865 // 256 - 512 (256) values
866 // 512 - 513 (  1) padding
867 // 513 - 514 (  1) reserved(3),ctb(1),msb(1),bit(3)
868 // Optional fields:
869 // 514 - 515 (  1) width
870 // 515 -   * (  N) separator
871 // Invariants:
872 // - symbols is 2^bit unique characters repeated 2^(8-bit) times
873 // - values[128 ..] are INVALID
874 // - values[0 .. 128] are either INVALID, IGNORE, PADDING, or < 2^bit
875 // - padding is either < 128 or INVALID
876 // - values[padding] is PADDING if padding < 128
877 // - values and symbols are inverse
878 // - ctb is true if 8 % bit == 0
879 // - width is present if there is x such that values[x] is IGNORE
880 // - width % dec(bit) == 0
881 // - for all x in separator values[x] is IGNORE
882 #[derive(Debug, Clone, PartialEq, Eq)]
883 pub struct Encoding(#[doc(hidden)] pub InternalEncoding);
884 
885 /// How to translate characters when decoding
886 ///
887 /// The order matters. The first character of the `from` field is translated to the first character
888 /// of the `to` field. The second to the second. Etc.
889 ///
890 /// See [Specification](struct.Specification.html) for more information.
891 #[derive(Debug, Clone)]
892 #[cfg(feature = "alloc")]
893 pub struct Translate {
894     /// Characters to translate from
895     pub from: String,
896 
897     /// Characters to translate to
898     pub to: String,
899 }
900 
901 /// How to wrap the output when encoding
902 ///
903 /// See [Specification](struct.Specification.html) for more information.
904 #[derive(Debug, Clone)]
905 #[cfg(feature = "alloc")]
906 pub struct Wrap {
907     /// Wrapping width
908     ///
909     /// Must be a multiple of:
910     ///
911     /// - 8 for a bit-width of 1 (binary), 3 (octal), and 5 (base32)
912     /// - 4 for a bit-width of 2 (base4) and 6 (base64)
913     /// - 2 for a bit-width of 4 (hexadecimal)
914     ///
915     /// Wrapping is disabled if null.
916     pub width: usize,
917 
918     /// Wrapping characters
919     ///
920     /// Wrapping is disabled if empty.
921     pub separator: String,
922 }
923 
924 /// Base-conversion specification
925 ///
926 /// It is possible to define custom encodings given a specification. To do so, it is important to
927 /// understand the theory first.
928 ///
929 /// # Theory
930 ///
931 /// Each subsection has an equivalent subsection in the [Practice](#practice) section.
932 ///
933 /// ## Basics
934 ///
935 /// The main idea of a [base-conversion] encoding is to see `[u8]` as numbers written in
936 /// little-endian base256 and convert them in another little-endian base. For performance reasons,
937 /// this crate restricts this other base to be of size 2 (binary), 4 (base4), 8 (octal), 16
938 /// (hexadecimal), 32 (base32), or 64 (base64). The converted number is written as `[u8]` although
939 /// it doesn't use all the 256 possible values of `u8`. This crate encodes to ASCII, so only values
940 /// smaller than 128 are allowed.
941 ///
942 /// More precisely, we need the following elements:
943 ///
944 /// - The bit-width N: 1 for binary, 2 for base4, 3 for octal, 4 for hexadecimal, 5 for base32, and
945 ///   6 for base64
946 /// - The [bit-order](enum.BitOrder.html): most or least significant bit first
947 /// - The symbols function S from [0, 2<sup>N</sup>) (called values and written `uN`) to symbols
948 ///   (represented as `u8` although only ASCII symbols are allowed, i.e. smaller than 128)
949 /// - The values partial function V from ASCII to [0, 2<sup>N</sup>), i.e. from `u8` to `uN`
950 /// - Whether trailing bits are checked: trailing bits are leading zeros in theory, but since
951 ///   numbers are little-endian they come last
952 ///
953 /// For the encoding to be correct (i.e. encoding then decoding gives back the initial input),
954 /// V(S(i)) must be defined and equal to i for all i in [0, 2<sup>N</sup>). For the encoding to be
955 /// [canonical][canonical] (i.e. different inputs decode to different outputs, or equivalently,
956 /// decoding then encoding gives back the initial input), trailing bits must be checked and if V(i)
957 /// is defined then S(V(i)) is equal to i for all i.
958 ///
959 /// Encoding and decoding are given by the following pipeline:
960 ///
961 /// ```text
962 /// [u8] <--1--> [[bit; 8]] <--2--> [[bit; N]] <--3--> [uN] <--4--> [u8]
963 /// 1: Map bit-order between each u8 and [bit; 8]
964 /// 2: Base conversion between base 2^8 and base 2^N (check trailing bits)
965 /// 3: Map bit-order between each [bit; N] and uN
966 /// 4: Map symbols/values between each uN and u8 (values must be defined)
967 /// ```
968 ///
969 /// ## Extensions
970 ///
971 /// All these extensions make the encoding not canonical.
972 ///
973 /// ### Padding
974 ///
975 /// Padding is useful if the following conditions are met:
976 ///
977 /// - the bit-width is 3 (octal), 5 (base32), or 6 (base64)
978 /// - the length of the data to encode is not known in advance
979 /// - the data must be sent without buffering
980 ///
981 /// Bases for which the bit-width N does not divide 8 may not concatenate encoded data. This comes
982 /// from the fact that it is not possible to make the difference between trailing bits and encoding
983 /// bits. Padding solves this issue by adding a new character to discriminate between trailing bits
984 /// and encoding bits. The idea is to work by blocks of lcm(8, N) bits, where lcm(8, N) is the least
985 /// common multiple of 8 and N. When such block is not complete, it is padded.
986 ///
987 /// To preserve correctness, the padding character must not be a symbol.
988 ///
989 /// ### Ignore characters when decoding
990 ///
991 /// Ignoring characters when decoding is useful if after encoding some characters are added for
992 /// convenience or any other reason (like wrapping). In that case we want to first ignore those
993 /// characters before decoding.
994 ///
995 /// To preserve correctness, ignored characters must not contain symbols or the padding character.
996 ///
997 /// ### Wrap output when encoding
998 ///
999 /// Wrapping output when encoding is useful if the output is meant to be printed in a document where
1000 /// width is limited (typically 80-columns documents). In that case, the wrapping width and the
1001 /// wrapping separator have to be defined.
1002 ///
1003 /// To preserve correctness, the wrapping separator characters must be ignored (see previous
1004 /// subsection). As such, wrapping separator characters must also not contain symbols or the padding
1005 /// character.
1006 ///
1007 /// ### Translate characters when decoding
1008 ///
1009 /// Translating characters when decoding is useful when encoded data may be copied by a humain
1010 /// instead of a machine. Humans tend to confuse some characters for others. In that case we want to
1011 /// translate those characters before decoding.
1012 ///
1013 /// To preserve correctness, the characters we translate _from_ must not contain symbols or the
1014 /// padding character, and the characters we translate _to_ must only contain symbols or the padding
1015 /// character.
1016 ///
1017 /// # Practice
1018 ///
1019 /// ## Basics
1020 ///
1021 /// ```rust
1022 /// use data_encoding::{Encoding, Specification};
1023 /// fn make_encoding(symbols: &str) -> Encoding {
1024 ///     let mut spec = Specification::new();
1025 ///     spec.symbols.push_str(symbols);
1026 ///     spec.encoding().unwrap()
1027 /// }
1028 /// let binary = make_encoding("01");
1029 /// let octal = make_encoding("01234567");
1030 /// let hexadecimal = make_encoding("0123456789abcdef");
1031 /// assert_eq!(binary.encode(b"Bit"), "010000100110100101110100");
1032 /// assert_eq!(octal.encode(b"Bit"), "20464564");
1033 /// assert_eq!(hexadecimal.encode(b"Bit"), "426974");
1034 /// ```
1035 ///
1036 /// The `binary` base has 2 symbols `0` and `1` with value 0 and 1 respectively. The `octal` base
1037 /// has 8 symbols `0` to `7` with value 0 to 7. The `hexadecimal` base has 16 symbols `0` to `9` and
1038 /// `a` to `f` with value 0 to 15. The following diagram gives the idea of how encoding works in the
1039 /// previous example (note that we can actually write such diagram only because the bit-order is
1040 /// most significant first):
1041 ///
1042 /// ```text
1043 /// [      octal] |  2  :  0  :  4  :  6  :  4  :  5  :  6  :  4  |
1044 /// [     binary] |0 1 0 0 0 0 1 0|0 1 1 0 1 0 0 1|0 1 1 1 0 1 0 0|
1045 /// [hexadecimal] |   4   :   2   |   6   :   9   |   7   :   4   |
1046 ///                ^-- LSB                                       ^-- MSB
1047 /// ```
1048 ///
1049 /// Note that in theory, these little-endian numbers are read from right to left (the most
1050 /// significant bit is at the right). Since leading zeros are meaningless (in our usual decimal
1051 /// notation 0123 is the same as 123), it explains why trailing bits must be zero. Trailing bits may
1052 /// occur when the bit-width of a base does not divide 8. Only binary, base4, and hexadecimal don't
1053 /// have trailing bits issues. So let's consider octal and base64, which have trailing bits in
1054 /// similar circumstances:
1055 ///
1056 /// ```rust
1057 /// use data_encoding::{Specification, BASE64_NOPAD};
1058 /// let octal = {
1059 ///     let mut spec = Specification::new();
1060 ///     spec.symbols.push_str("01234567");
1061 ///     spec.encoding().unwrap()
1062 /// };
1063 /// assert_eq!(BASE64_NOPAD.encode(b"B"), "Qg");
1064 /// assert_eq!(octal.encode(b"B"), "204");
1065 /// ```
1066 ///
1067 /// We have the following diagram, where the base64 values are written between parentheses:
1068 ///
1069 /// ```text
1070 /// [base64] |   Q(16)   :   g(32)   : [has 4 zero trailing bits]
1071 /// [ octal] |  2  :  0  :  4  :       [has 1 zero trailing bit ]
1072 ///          |0 1 0 0 0 0 1 0|0 0 0 0
1073 /// [ ascii] |       B       |
1074 ///                           ^-^-^-^-- leading zeros / trailing bits
1075 /// ```
1076 ///
1077 /// ## Extensions
1078 ///
1079 /// ### Padding
1080 ///
1081 /// For octal and base64, lcm(8, 3) == lcm(8, 6) == 24 bits or 3 bytes. For base32, lcm(8, 5) is 40
1082 /// bits or 5 bytes. Let's consider octal and base64:
1083 ///
1084 /// ```rust
1085 /// use data_encoding::{Specification, BASE64};
1086 /// let octal = {
1087 ///     let mut spec = Specification::new();
1088 ///     spec.symbols.push_str("01234567");
1089 ///     spec.padding = Some('=');
1090 ///     spec.encoding().unwrap()
1091 /// };
1092 /// // We start encoding but we only have "B" for now.
1093 /// assert_eq!(BASE64.encode(b"B"), "Qg==");
1094 /// assert_eq!(octal.encode(b"B"), "204=====");
1095 /// // Now we have "it".
1096 /// assert_eq!(BASE64.encode(b"it"), "aXQ=");
1097 /// assert_eq!(octal.encode(b"it"), "322720==");
1098 /// // By concatenating everything, we may decode the original data.
1099 /// assert_eq!(BASE64.decode(b"Qg==aXQ=").unwrap(), b"Bit");
1100 /// assert_eq!(octal.decode(b"204=====322720==").unwrap(), b"Bit");
1101 /// ```
1102 ///
1103 /// We have the following diagrams:
1104 ///
1105 /// ```text
1106 /// [base64] |   Q(16)   :   g(32)   :     =     :     =     |
1107 /// [ octal] |  2  :  0  :  4  :  =  :  =  :  =  :  =  :  =  |
1108 ///          |0 1 0 0 0 0 1 0|. . . . . . . .|. . . . . . . .|
1109 /// [ ascii] |       B       |        end of block aligned --^
1110 ///          ^-- beginning of block aligned
1111 ///
1112 /// [base64] |   a(26)   :   X(23)   :   Q(16)   :     =     |
1113 /// [ octal] |  3  :  2  :  2  :  7  :  2  :  0  :  =  :  =  |
1114 ///          |0 1 1 0 1 0 0 1|0 1 1 1 0 1 0 0|. . . . . . . .|
1115 /// [ ascii] |       i       |       t       |
1116 /// ```
1117 ///
1118 /// ### Ignore characters when decoding
1119 ///
1120 /// The typical use-case is to ignore newlines (`\r` and `\n`). But to keep the example small, we
1121 /// will ignore spaces.
1122 ///
1123 /// ```rust
1124 /// let mut spec = data_encoding::HEXLOWER.specification();
1125 /// spec.ignore.push_str(" \t");
1126 /// let base = spec.encoding().unwrap();
1127 /// assert_eq!(base.decode(b"42 69 74"), base.decode(b"426974"));
1128 /// ```
1129 ///
1130 /// ### Wrap output when encoding
1131 ///
1132 /// The typical use-case is to wrap after 64 or 76 characters with a newline (`\r\n` or `\n`). But
1133 /// to keep the example small, we will wrap after 8 characters with a space.
1134 ///
1135 /// ```rust
1136 /// let mut spec = data_encoding::BASE64.specification();
1137 /// spec.wrap.width = 8;
1138 /// spec.wrap.separator.push_str(" ");
1139 /// let base64 = spec.encoding().unwrap();
1140 /// assert_eq!(base64.encode(b"Hey you"), "SGV5IHlv dQ== ");
1141 /// ```
1142 ///
1143 /// Note that the output always ends with the separator.
1144 ///
1145 /// ### Translate characters when decoding
1146 ///
1147 /// The typical use-case is to translate lowercase to uppercase or reciprocally, but it is also used
1148 /// for letters that look alike, like `O0` or `Il1`. Let's illustrate both examples.
1149 ///
1150 /// ```rust
1151 /// let mut spec = data_encoding::HEXLOWER.specification();
1152 /// spec.translate.from.push_str("ABCDEFOIl");
1153 /// spec.translate.to.push_str("abcdef011");
1154 /// let base = spec.encoding().unwrap();
1155 /// assert_eq!(base.decode(b"BOIl"), base.decode(b"b011"));
1156 /// ```
1157 ///
1158 /// [base-conversion]: https://en.wikipedia.org/wiki/Positional_notation#Base_conversion
1159 /// [canonical]: https://tools.ietf.org/html/rfc4648#section-3.5
1160 #[derive(Debug, Clone)]
1161 #[cfg(feature = "alloc")]
1162 pub struct Specification {
1163     /// Symbols
1164     ///
1165     /// The number of symbols must be 2, 4, 8, 16, 32, or 64. Symbols must be ASCII characters
1166     /// (smaller than 128) and they must be unique.
1167     pub symbols: String,
1168 
1169     /// Bit-order
1170     ///
1171     /// The default is to use most significant bit first since it is the most common.
1172     pub bit_order: BitOrder,
1173 
1174     /// Check trailing bits
1175     ///
1176     /// The default is to check trailing bits. This field is ignored when unnecessary (i.e. for
1177     /// base2, base4, and base16).
1178     pub check_trailing_bits: bool,
1179 
1180     /// Padding
1181     ///
1182     /// The default is to not use padding. The padding character must be ASCII and must not be a
1183     /// symbol.
1184     pub padding: Option<char>,
1185 
1186     /// Characters to ignore when decoding
1187     ///
1188     /// The default is to not ignore characters when decoding. The characters to ignore must be
1189     /// ASCII and must not be symbols or the padding character.
1190     pub ignore: String,
1191 
1192     /// How to wrap the output when encoding
1193     ///
1194     /// The default is to not wrap the output when encoding. The wrapping characters must be ASCII
1195     /// and must not be symbols or the padding character.
1196     pub wrap: Wrap,
1197 
1198     /// How to translate characters when decoding
1199     ///
1200     /// The default is to not translate characters when decoding. The characters to translate from
1201     /// must be ASCII and must not have already been assigned a semantics. The characters to
1202     /// translate to must be ASCII and must have been assigned a semantics (symbol, padding
1203     /// character, or ignored character).
1204     pub translate: Translate,
1205 }
1206 
1207 #[cfg(feature = "alloc")]
1208 impl Default for Specification {
default() -> Self1209     fn default() -> Self {
1210         Self::new()
1211     }
1212 }
1213 
1214 impl Encoding {
sym(&self) -> &[u8; 256]1215     fn sym(&self) -> &[u8; 256] {
1216         self.0[0 .. 256].try_into().unwrap()
1217     }
1218 
val(&self) -> &[u8; 256]1219     fn val(&self) -> &[u8; 256] {
1220         self.0[256 .. 512].try_into().unwrap()
1221     }
1222 
pad(&self) -> Option<u8>1223     fn pad(&self) -> Option<u8> {
1224         if self.0[512] < 128 {
1225             Some(self.0[512])
1226         } else {
1227             None
1228         }
1229     }
1230 
ctb(&self) -> bool1231     fn ctb(&self) -> bool {
1232         self.0[513] & 0x10 != 0
1233     }
1234 
msb(&self) -> bool1235     fn msb(&self) -> bool {
1236         self.0[513] & 0x8 != 0
1237     }
1238 
bit(&self) -> usize1239     fn bit(&self) -> usize {
1240         (self.0[513] & 0x7) as usize
1241     }
1242 
1243     /// Minimum number of input and output blocks when encoding
block_len(&self) -> (usize, usize)1244     fn block_len(&self) -> (usize, usize) {
1245         let bit = self.bit();
1246         match self.wrap() {
1247             Some((col, end)) => (col / dec(bit) * enc(bit), col + end.len()),
1248             None => (enc(bit), dec(bit)),
1249         }
1250     }
1251 
wrap(&self) -> Option<(usize, &[u8])>1252     fn wrap(&self) -> Option<(usize, &[u8])> {
1253         if self.0.len() <= 515 {
1254             return None;
1255         }
1256         Some((self.0[514] as usize, &self.0[515 ..]))
1257     }
1258 
has_ignore(&self) -> bool1259     fn has_ignore(&self) -> bool {
1260         self.0.len() >= 515
1261     }
1262 
1263     /// Returns the encoded length of an input of length `len`
1264     ///
1265     /// See [`encode_mut`] for when to use it.
1266     ///
1267     /// [`encode_mut`]: struct.Encoding.html#method.encode_mut
1268     #[must_use]
encode_len(&self, len: usize) -> usize1269     pub fn encode_len(&self, len: usize) -> usize {
1270         dispatch! {
1271             let bit: usize = self.bit();
1272             let pad: Option<u8> = self.pad();
1273             let wrap: Option<(usize, &[u8])> = self.wrap();
1274             encode_wrap_len(bit, pad, wrap, len)
1275         }
1276     }
1277 
1278     /// Encodes `input` in `output`
1279     ///
1280     /// # Panics
1281     ///
1282     /// Panics if the `output` length does not match the result of [`encode_len`] for the `input`
1283     /// length.
1284     ///
1285     /// # Examples
1286     ///
1287     /// ```rust
1288     /// use data_encoding::BASE64;
1289     /// # let mut buffer = vec![0; 100];
1290     /// let input = b"Hello world";
1291     /// let output = &mut buffer[0 .. BASE64.encode_len(input.len())];
1292     /// BASE64.encode_mut(input, output);
1293     /// assert_eq!(output, b"SGVsbG8gd29ybGQ=");
1294     /// ```
1295     ///
1296     /// [`encode_len`]: struct.Encoding.html#method.encode_len
1297     #[allow(clippy::cognitive_complexity)]
encode_mut(&self, input: &[u8], output: &mut [u8])1298     pub fn encode_mut(&self, input: &[u8], output: &mut [u8]) {
1299         assert_eq!(output.len(), self.encode_len(input.len()));
1300         dispatch! {
1301             let bit: usize = self.bit();
1302             let msb: bool = self.msb();
1303             let pad: Option<u8> = self.pad();
1304             let wrap: Option<(usize, &[u8])> = self.wrap();
1305             encode_wrap_mut(bit, msb, self.sym(), pad, wrap, input, output)
1306         }
1307     }
1308 
1309     /// Appends the encoding of `input` to `output`
1310     ///
1311     /// # Examples
1312     ///
1313     /// ```rust
1314     /// use data_encoding::BASE64;
1315     /// # let mut buffer = vec![0; 100];
1316     /// let input = b"Hello world";
1317     /// let mut output = "Result: ".to_string();
1318     /// BASE64.encode_append(input, &mut output);
1319     /// assert_eq!(output, "Result: SGVsbG8gd29ybGQ=");
1320     /// ```
1321     #[cfg(feature = "alloc")]
encode_append(&self, input: &[u8], output: &mut String)1322     pub fn encode_append(&self, input: &[u8], output: &mut String) {
1323         let output = unsafe { output.as_mut_vec() };
1324         let output_len = output.len();
1325         output.resize(output_len + self.encode_len(input.len()), 0u8);
1326         self.encode_mut(input, &mut output[output_len ..]);
1327     }
1328 
1329     /// Returns an object to encode a fragmented input and append it to `output`
1330     ///
1331     /// See the documentation of [`Encoder`] for more details and examples.
1332     #[cfg(feature = "alloc")]
new_encoder<'a>(&'a self, output: &'a mut String) -> Encoder<'a>1333     pub fn new_encoder<'a>(&'a self, output: &'a mut String) -> Encoder<'a> {
1334         Encoder::new(self, output)
1335     }
1336 
1337     /// Writes the encoding of `input` to `output`
1338     ///
1339     /// This allocates a buffer of 1024 bytes on the stack. If you want to control the buffer size
1340     /// and location, use [`Encoding::encode_write_buffer()`] instead.
1341     ///
1342     /// # Errors
1343     ///
1344     /// Returns an error when writing to the output fails.
encode_write( &self, input: &[u8], output: &mut impl core::fmt::Write, ) -> core::fmt::Result1345     pub fn encode_write(
1346         &self, input: &[u8], output: &mut impl core::fmt::Write,
1347     ) -> core::fmt::Result {
1348         self.encode_write_buffer(input, output, &mut [0; 1024])
1349     }
1350 
1351     /// Writes the encoding of `input` to `output` using a temporary `buffer`
1352     ///
1353     /// # Panics
1354     ///
1355     /// Panics if the buffer is shorter than 510 bytes.
1356     ///
1357     /// # Errors
1358     ///
1359     /// Returns an error when writing to the output fails.
encode_write_buffer( &self, input: &[u8], output: &mut impl core::fmt::Write, buffer: &mut [u8], ) -> core::fmt::Result1360     pub fn encode_write_buffer(
1361         &self, input: &[u8], output: &mut impl core::fmt::Write, buffer: &mut [u8],
1362     ) -> core::fmt::Result {
1363         assert!(510 <= buffer.len());
1364         let (enc, dec) = self.block_len();
1365         for input in input.chunks(buffer.len() / dec * enc) {
1366             let buffer = &mut buffer[.. self.encode_len(input.len())];
1367             self.encode_mut(input, buffer);
1368             output.write_str(unsafe { core::str::from_utf8_unchecked(buffer) })?;
1369         }
1370         Ok(())
1371     }
1372 
1373     /// Returns encoded `input`
1374     ///
1375     /// # Examples
1376     ///
1377     /// ```rust
1378     /// use data_encoding::BASE64;
1379     /// assert_eq!(BASE64.encode(b"Hello world"), "SGVsbG8gd29ybGQ=");
1380     /// ```
1381     #[cfg(feature = "alloc")]
1382     #[must_use]
encode(&self, input: &[u8]) -> String1383     pub fn encode(&self, input: &[u8]) -> String {
1384         let mut output = vec![0u8; self.encode_len(input.len())];
1385         self.encode_mut(input, &mut output);
1386         unsafe { String::from_utf8_unchecked(output) }
1387     }
1388 
1389     /// Returns the decoded length of an input of length `len`
1390     ///
1391     /// See [`decode_mut`] for when to use it.
1392     ///
1393     /// # Errors
1394     ///
1395     /// Returns an error if `len` is invalid. The error kind is [`Length`] and the [position] is the
1396     /// greatest valid input length.
1397     ///
1398     /// [`decode_mut`]: struct.Encoding.html#method.decode_mut
1399     /// [`Length`]: enum.DecodeKind.html#variant.Length
1400     /// [position]: struct.DecodeError.html#structfield.position
decode_len(&self, len: usize) -> Result<usize, DecodeError>1401     pub fn decode_len(&self, len: usize) -> Result<usize, DecodeError> {
1402         let (ilen, olen) = dispatch! {
1403             let bit: usize = self.bit();
1404             let pad: bool = self.pad().is_some();
1405             decode_wrap_len(bit, pad, len)
1406         };
1407         check!(
1408             DecodeError { position: ilen, kind: DecodeKind::Length },
1409             self.has_ignore() || len == ilen
1410         );
1411         Ok(olen)
1412     }
1413 
1414     /// Decodes `input` in `output`
1415     ///
1416     /// Returns the length of the decoded output. This length may be smaller than the output length
1417     /// if the input contained padding or ignored characters. The output bytes after the returned
1418     /// length are not initialized and should not be read.
1419     ///
1420     /// # Panics
1421     ///
1422     /// Panics if the `output` length does not match the result of [`decode_len`] for the `input`
1423     /// length. Also panics if `decode_len` fails for the `input` length.
1424     ///
1425     /// # Errors
1426     ///
1427     /// Returns an error if `input` is invalid. See [`decode`] for more details. The are two
1428     /// differences though:
1429     ///
1430     /// - [`Length`] may be returned only if the encoding allows ignored characters, because
1431     ///   otherwise this is already checked by [`decode_len`].
1432     /// - The [`read`] first bytes of the input have been successfully decoded to the [`written`]
1433     ///   first bytes of the output.
1434     ///
1435     /// # Examples
1436     ///
1437     /// ```rust
1438     /// use data_encoding::BASE64;
1439     /// # let mut buffer = vec![0; 100];
1440     /// let input = b"SGVsbA==byB3b3JsZA==";
1441     /// let output = &mut buffer[0 .. BASE64.decode_len(input.len()).unwrap()];
1442     /// let len = BASE64.decode_mut(input, output).unwrap();
1443     /// assert_eq!(&output[0 .. len], b"Hello world");
1444     /// ```
1445     ///
1446     /// [`decode_len`]: struct.Encoding.html#method.decode_len
1447     /// [`decode`]: struct.Encoding.html#method.decode
1448     /// [`Length`]: enum.DecodeKind.html#variant.Length
1449     /// [`read`]: struct.DecodePartial.html#structfield.read
1450     /// [`written`]: struct.DecodePartial.html#structfield.written
1451     #[allow(clippy::cognitive_complexity)]
decode_mut(&self, input: &[u8], output: &mut [u8]) -> Result<usize, DecodePartial>1452     pub fn decode_mut(&self, input: &[u8], output: &mut [u8]) -> Result<usize, DecodePartial> {
1453         assert_eq!(Ok(output.len()), self.decode_len(input.len()));
1454         dispatch! {
1455             let bit: usize = self.bit();
1456             let msb: bool = self.msb();
1457             let pad: bool = self.pad().is_some();
1458             let has_ignore: bool = self.has_ignore();
1459             decode_wrap_mut(bit, msb, self.ctb(), self.val(), pad, has_ignore,
1460                             input, output)
1461         }
1462     }
1463 
1464     /// Returns decoded `input`
1465     ///
1466     /// # Errors
1467     ///
1468     /// Returns an error if `input` is invalid. The error kind can be:
1469     ///
1470     /// - [`Length`] if the input length is invalid. The [position] is the greatest valid input
1471     ///   length.
1472     /// - [`Symbol`] if the input contains an invalid character. The [position] is the first invalid
1473     ///   character.
1474     /// - [`Trailing`] if the input has non-zero trailing bits. This is only possible if the
1475     ///   encoding checks trailing bits. The [position] is the first character containing non-zero
1476     ///   trailing bits.
1477     /// - [`Padding`] if the input has an invalid padding length. This is only possible if the
1478     ///   encoding uses padding. The [position] is the first padding character of the first padding
1479     ///   of invalid length.
1480     ///
1481     /// # Examples
1482     ///
1483     /// ```rust
1484     /// use data_encoding::BASE64;
1485     /// assert_eq!(BASE64.decode(b"SGVsbA==byB3b3JsZA==").unwrap(), b"Hello world");
1486     /// ```
1487     ///
1488     /// [`Length`]: enum.DecodeKind.html#variant.Length
1489     /// [`Symbol`]: enum.DecodeKind.html#variant.Symbol
1490     /// [`Trailing`]: enum.DecodeKind.html#variant.Trailing
1491     /// [`Padding`]: enum.DecodeKind.html#variant.Padding
1492     /// [position]: struct.DecodeError.html#structfield.position
1493     #[cfg(feature = "alloc")]
decode(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError>1494     pub fn decode(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError> {
1495         let mut output = vec![0u8; self.decode_len(input.len())?];
1496         let len = self.decode_mut(input, &mut output).map_err(|partial| partial.error)?;
1497         output.truncate(len);
1498         Ok(output)
1499     }
1500 
1501     /// Returns the bit-width
1502     #[must_use]
bit_width(&self) -> usize1503     pub fn bit_width(&self) -> usize {
1504         self.bit()
1505     }
1506 
1507     /// Returns whether the encoding is canonical
1508     ///
1509     /// An encoding is not canonical if one of the following conditions holds:
1510     ///
1511     /// - trailing bits are not checked
1512     /// - padding is used
1513     /// - characters are ignored
1514     /// - characters are translated
1515     #[must_use]
is_canonical(&self) -> bool1516     pub fn is_canonical(&self) -> bool {
1517         if !self.ctb() {
1518             return false;
1519         }
1520         let bit = self.bit();
1521         let sym = self.sym();
1522         let val = self.val();
1523         for i in 0 .. 256 {
1524             if val[i] == INVALID {
1525                 continue;
1526             }
1527             if val[i] >= 1 << bit {
1528                 return false;
1529             }
1530             if sym[val[i] as usize] as usize != i {
1531                 return false;
1532             }
1533         }
1534         true
1535     }
1536 
1537     /// Returns the encoding specification
1538     #[allow(clippy::missing_panics_doc)] // no panic
1539     #[cfg(feature = "alloc")]
1540     #[must_use]
specification(&self) -> Specification1541     pub fn specification(&self) -> Specification {
1542         let mut specification = Specification::new();
1543         specification
1544             .symbols
1545             .push_str(core::str::from_utf8(&self.sym()[0 .. 1 << self.bit()]).unwrap());
1546         specification.bit_order =
1547             if self.msb() { MostSignificantFirst } else { LeastSignificantFirst };
1548         specification.check_trailing_bits = self.ctb();
1549         if let Some(pad) = self.pad() {
1550             specification.padding = Some(pad as char);
1551         }
1552         for i in 0 .. 128u8 {
1553             if self.val()[i as usize] != IGNORE {
1554                 continue;
1555             }
1556             specification.ignore.push(i as char);
1557         }
1558         if let Some((col, end)) = self.wrap() {
1559             specification.wrap.width = col;
1560             specification.wrap.separator = core::str::from_utf8(end).unwrap().to_owned();
1561         }
1562         for i in 0 .. 128u8 {
1563             let canonical = if self.val()[i as usize] < 1 << self.bit() {
1564                 self.sym()[self.val()[i as usize] as usize]
1565             } else if self.val()[i as usize] == PADDING {
1566                 self.pad().unwrap()
1567             } else {
1568                 continue;
1569             };
1570             if i == canonical {
1571                 continue;
1572             }
1573             specification.translate.from.push(i as char);
1574             specification.translate.to.push(canonical as char);
1575         }
1576         specification
1577     }
1578 
1579     #[doc(hidden)]
1580     #[must_use]
internal_new(implementation: &'static [u8]) -> Encoding1581     pub const fn internal_new(implementation: &'static [u8]) -> Encoding {
1582         #[cfg(feature = "alloc")]
1583         let encoding = Encoding(Cow::Borrowed(implementation));
1584         #[cfg(not(feature = "alloc"))]
1585         let encoding = Encoding(implementation);
1586         encoding
1587     }
1588 
1589     #[doc(hidden)]
1590     #[must_use]
internal_implementation(&self) -> &[u8]1591     pub fn internal_implementation(&self) -> &[u8] {
1592         &self.0
1593     }
1594 }
1595 
1596 /// Encodes fragmented input to an output
1597 ///
1598 /// It is equivalent to use an [`Encoder`] with multiple calls to [`Encoder::append()`] than to
1599 /// first concatenate all the input and then use [`Encoding::encode_append()`]. In particular, this
1600 /// function will not introduce padding or wrapping between inputs.
1601 ///
1602 /// # Examples
1603 ///
1604 /// ```rust
1605 /// // This is a bit inconvenient but we can't take a long-term reference to data_encoding::BASE64
1606 /// // because it's a constant. We need to use a static which has an address instead. This will be
1607 /// // fixed in version 3 of the library.
1608 /// static BASE64: data_encoding::Encoding = data_encoding::BASE64;
1609 /// let mut output = String::new();
1610 /// let mut encoder = BASE64.new_encoder(&mut output);
1611 /// encoder.append(b"hello");
1612 /// encoder.append(b"world");
1613 /// encoder.finalize();
1614 /// assert_eq!(output, BASE64.encode(b"helloworld"));
1615 /// ```
1616 #[derive(Debug)]
1617 #[cfg(feature = "alloc")]
1618 pub struct Encoder<'a> {
1619     encoding: &'a Encoding,
1620     output: &'a mut String,
1621     buffer: [u8; 255],
1622     length: u8,
1623 }
1624 
1625 #[cfg(feature = "alloc")]
1626 impl<'a> Drop for Encoder<'a> {
drop(&mut self)1627     fn drop(&mut self) {
1628         self.encoding.encode_append(&self.buffer[.. self.length as usize], self.output);
1629     }
1630 }
1631 
1632 #[cfg(feature = "alloc")]
1633 impl<'a> Encoder<'a> {
new(encoding: &'a Encoding, output: &'a mut String) -> Self1634     fn new(encoding: &'a Encoding, output: &'a mut String) -> Self {
1635         Encoder { encoding, output, buffer: [0; 255], length: 0 }
1636     }
1637 
1638     /// Encodes the provided input fragment and appends the result to the output
append(&mut self, mut input: &[u8])1639     pub fn append(&mut self, mut input: &[u8]) {
1640         #[allow(clippy::cast_possible_truncation)] // no truncation
1641         let max = self.encoding.block_len().0 as u8;
1642         if self.length != 0 {
1643             let len = self.length;
1644             #[allow(clippy::cast_possible_truncation)] // no truncation
1645             let add = core::cmp::min((max - len) as usize, input.len()) as u8;
1646             self.buffer[len as usize ..][.. add as usize].copy_from_slice(&input[.. add as usize]);
1647             self.length += add;
1648             input = &input[add as usize ..];
1649             if self.length != max {
1650                 debug_assert!(self.length < max);
1651                 debug_assert!(input.is_empty());
1652                 return;
1653             }
1654             self.encoding.encode_append(&self.buffer[.. max as usize], self.output);
1655             self.length = 0;
1656         }
1657         let len = floor(input.len(), max as usize);
1658         self.encoding.encode_append(&input[.. len], self.output);
1659         input = &input[len ..];
1660         #[allow(clippy::cast_possible_truncation)] // no truncation
1661         let len = input.len() as u8;
1662         self.buffer[.. len as usize].copy_from_slice(input);
1663         self.length = len;
1664     }
1665 
1666     /// Makes sure all inputs have been encoded and appended to the output
1667     ///
1668     /// This is equivalent to dropping the encoder and required for correctness, otherwise some
1669     /// encoded data may be missing at the end.
finalize(self)1670     pub fn finalize(self) {}
1671 }
1672 
1673 #[derive(Debug, Copy, Clone)]
1674 #[cfg(feature = "alloc")]
1675 enum SpecificationErrorImpl {
1676     BadSize,
1677     NotAscii,
1678     Duplicate(u8),
1679     ExtraPadding,
1680     WrapLength,
1681     WrapWidth(u8),
1682     FromTo,
1683     Undefined(u8),
1684 }
1685 #[cfg(feature = "alloc")]
1686 use crate::SpecificationErrorImpl::*;
1687 
1688 /// Specification error
1689 #[derive(Debug, Copy, Clone)]
1690 #[cfg(feature = "alloc")]
1691 pub struct SpecificationError(SpecificationErrorImpl);
1692 
1693 #[cfg(feature = "alloc")]
1694 impl core::fmt::Display for SpecificationError {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result1695     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1696         match self.0 {
1697             BadSize => write!(f, "invalid number of symbols"),
1698             NotAscii => write!(f, "non-ascii character"),
1699             Duplicate(c) => write!(f, "{:?} has conflicting definitions", c as char),
1700             ExtraPadding => write!(f, "unnecessary padding"),
1701             WrapLength => write!(f, "invalid wrap width or separator length"),
1702             WrapWidth(x) => write!(f, "wrap width not a multiple of {}", x),
1703             FromTo => write!(f, "translate from/to length mismatch"),
1704             Undefined(c) => write!(f, "{:?} is undefined", c as char),
1705         }
1706     }
1707 }
1708 
1709 #[cfg(feature = "std")]
1710 impl std::error::Error for SpecificationError {
description(&self) -> &str1711     fn description(&self) -> &str {
1712         match self.0 {
1713             BadSize => "invalid number of symbols",
1714             NotAscii => "non-ascii character",
1715             Duplicate(_) => "conflicting definitions",
1716             ExtraPadding => "unnecessary padding",
1717             WrapLength => "invalid wrap width or separator length",
1718             WrapWidth(_) => "wrap width not a multiple",
1719             FromTo => "translate from/to length mismatch",
1720             Undefined(_) => "undefined character",
1721         }
1722     }
1723 }
1724 
1725 #[cfg(feature = "alloc")]
1726 impl Specification {
1727     /// Returns a default specification
1728     #[must_use]
new() -> Specification1729     pub fn new() -> Specification {
1730         Specification {
1731             symbols: String::new(),
1732             bit_order: MostSignificantFirst,
1733             check_trailing_bits: true,
1734             padding: None,
1735             ignore: String::new(),
1736             wrap: Wrap { width: 0, separator: String::new() },
1737             translate: Translate { from: String::new(), to: String::new() },
1738         }
1739     }
1740 
1741     /// Returns the specified encoding
1742     ///
1743     /// # Errors
1744     ///
1745     /// Returns an error if the specification is invalid.
encoding(&self) -> Result<Encoding, SpecificationError>1746     pub fn encoding(&self) -> Result<Encoding, SpecificationError> {
1747         let symbols = self.symbols.as_bytes();
1748         let bit: u8 = match symbols.len() {
1749             2 => 1,
1750             4 => 2,
1751             8 => 3,
1752             16 => 4,
1753             32 => 5,
1754             64 => 6,
1755             _ => return Err(SpecificationError(BadSize)),
1756         };
1757         let mut values = [INVALID; 128];
1758         let set = |v: &mut [u8; 128], i: u8, x: u8| {
1759             check!(SpecificationError(NotAscii), i < 128);
1760             if v[i as usize] == x {
1761                 return Ok(());
1762             }
1763             check!(SpecificationError(Duplicate(i)), v[i as usize] == INVALID);
1764             v[i as usize] = x;
1765             Ok(())
1766         };
1767         for (v, symbols) in symbols.iter().enumerate() {
1768             #[allow(clippy::cast_possible_truncation)] // no truncation
1769             set(&mut values, *symbols, v as u8)?;
1770         }
1771         let msb = self.bit_order == MostSignificantFirst;
1772         let ctb = self.check_trailing_bits || 8 % bit == 0;
1773         let pad = match self.padding {
1774             None => None,
1775             Some(pad) => {
1776                 check!(SpecificationError(ExtraPadding), 8 % bit != 0);
1777                 check!(SpecificationError(NotAscii), pad.len_utf8() == 1);
1778                 set(&mut values, pad as u8, PADDING)?;
1779                 Some(pad as u8)
1780             }
1781         };
1782         for i in self.ignore.bytes() {
1783             set(&mut values, i, IGNORE)?;
1784         }
1785         let wrap = if self.wrap.separator.is_empty() || self.wrap.width == 0 {
1786             None
1787         } else {
1788             let col = self.wrap.width;
1789             let end = self.wrap.separator.as_bytes();
1790             check!(SpecificationError(WrapLength), col < 256 && end.len() < 256);
1791             #[allow(clippy::cast_possible_truncation)] // no truncation
1792             let col = col as u8;
1793             #[allow(clippy::cast_possible_truncation)] // no truncation
1794             let dec = dec(bit as usize) as u8;
1795             check!(SpecificationError(WrapWidth(dec)), col % dec == 0);
1796             for &i in end {
1797                 set(&mut values, i, IGNORE)?;
1798             }
1799             Some((col, end))
1800         };
1801         let from = self.translate.from.as_bytes();
1802         let to = self.translate.to.as_bytes();
1803         check!(SpecificationError(FromTo), from.len() == to.len());
1804         for i in 0 .. from.len() {
1805             check!(SpecificationError(NotAscii), to[i] < 128);
1806             let v = values[to[i] as usize];
1807             check!(SpecificationError(Undefined(to[i])), v != INVALID);
1808             set(&mut values, from[i], v)?;
1809         }
1810         let mut encoding = Vec::new();
1811         for _ in 0 .. 256 / symbols.len() {
1812             encoding.extend_from_slice(symbols);
1813         }
1814         encoding.extend_from_slice(&values);
1815         encoding.extend_from_slice(&[INVALID; 128]);
1816         match pad {
1817             None => encoding.push(INVALID),
1818             Some(pad) => encoding.push(pad),
1819         }
1820         encoding.push(bit);
1821         if msb {
1822             encoding[513] |= 0x08;
1823         }
1824         if ctb {
1825             encoding[513] |= 0x10;
1826         }
1827         if let Some((col, end)) = wrap {
1828             encoding.push(col);
1829             encoding.extend_from_slice(end);
1830         } else if values.contains(&IGNORE) {
1831             encoding.push(0);
1832         }
1833         Ok(Encoding(Cow::Owned(encoding)))
1834     }
1835 }
1836 
1837 /// Lowercase hexadecimal encoding
1838 ///
1839 /// This encoding is a static version of:
1840 ///
1841 /// ```rust
1842 /// # use data_encoding::{Specification, HEXLOWER};
1843 /// let mut spec = Specification::new();
1844 /// spec.symbols.push_str("0123456789abcdef");
1845 /// assert_eq!(HEXLOWER, spec.encoding().unwrap());
1846 /// ```
1847 ///
1848 /// # Examples
1849 ///
1850 /// ```rust
1851 /// use data_encoding::HEXLOWER;
1852 /// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef];
1853 /// assert_eq!(HEXLOWER.decode(b"deadbeef").unwrap(), deadbeef);
1854 /// assert_eq!(HEXLOWER.encode(&deadbeef), "deadbeef");
1855 /// ```
1856 pub const HEXLOWER: Encoding = Encoding::internal_new(HEXLOWER_IMPL);
1857 const HEXLOWER_IMPL: &[u8] = &[
1858     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54,
1859     55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100,
1860     101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51,
1861     52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97,
1862     98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48,
1863     49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55,
1864     56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100,
1865     101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51,
1866     52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97,
1867     98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48,
1868     49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55,
1869     56, 57, 97, 98, 99, 100, 101, 102, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1870     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1871     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 1, 2,
1872     3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1873     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1874     128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1875     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1876     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1877     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1878     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1879     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1880     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1881     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1882     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28,
1883 ];
1884 
1885 /// Lowercase hexadecimal encoding with case-insensitive decoding
1886 ///
1887 /// This encoding is a static version of:
1888 ///
1889 /// ```rust
1890 /// # use data_encoding::{Specification, HEXLOWER_PERMISSIVE};
1891 /// let mut spec = Specification::new();
1892 /// spec.symbols.push_str("0123456789abcdef");
1893 /// spec.translate.from.push_str("ABCDEF");
1894 /// spec.translate.to.push_str("abcdef");
1895 /// assert_eq!(HEXLOWER_PERMISSIVE, spec.encoding().unwrap());
1896 /// ```
1897 ///
1898 /// # Examples
1899 ///
1900 /// ```rust
1901 /// use data_encoding::HEXLOWER_PERMISSIVE;
1902 /// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef];
1903 /// assert_eq!(HEXLOWER_PERMISSIVE.decode(b"DeadBeef").unwrap(), deadbeef);
1904 /// assert_eq!(HEXLOWER_PERMISSIVE.encode(&deadbeef), "deadbeef");
1905 /// ```
1906 ///
1907 /// You can also define a shorter name:
1908 ///
1909 /// ```rust
1910 /// use data_encoding::{Encoding, HEXLOWER_PERMISSIVE};
1911 /// const HEX: Encoding = HEXLOWER_PERMISSIVE;
1912 /// ```
1913 pub const HEXLOWER_PERMISSIVE: Encoding = Encoding::internal_new(HEXLOWER_PERMISSIVE_IMPL);
1914 const HEXLOWER_PERMISSIVE_IMPL: &[u8] = &[
1915     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54,
1916     55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100,
1917     101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51,
1918     52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97,
1919     98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48,
1920     49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55,
1921     56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100,
1922     101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51,
1923     52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97,
1924     98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48,
1925     49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55,
1926     56, 57, 97, 98, 99, 100, 101, 102, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1927     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1928     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 1, 2,
1929     3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128,
1930     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1931     128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1932     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1933     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1934     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1935     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1936     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1937     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1938     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1939     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28,
1940 ];
1941 
1942 /// Uppercase hexadecimal encoding
1943 ///
1944 /// This encoding is a static version of:
1945 ///
1946 /// ```rust
1947 /// # use data_encoding::{Specification, HEXUPPER};
1948 /// let mut spec = Specification::new();
1949 /// spec.symbols.push_str("0123456789ABCDEF");
1950 /// assert_eq!(HEXUPPER, spec.encoding().unwrap());
1951 /// ```
1952 ///
1953 /// It is compliant with [RFC4648] and known as "base16" or "hex".
1954 ///
1955 /// # Examples
1956 ///
1957 /// ```rust
1958 /// use data_encoding::HEXUPPER;
1959 /// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef];
1960 /// assert_eq!(HEXUPPER.decode(b"DEADBEEF").unwrap(), deadbeef);
1961 /// assert_eq!(HEXUPPER.encode(&deadbeef), "DEADBEEF");
1962 /// ```
1963 ///
1964 /// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-8
1965 pub const HEXUPPER: Encoding = Encoding::internal_new(HEXUPPER_IMPL);
1966 const HEXUPPER_IMPL: &[u8] = &[
1967     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
1968     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
1969     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
1970     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
1971     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
1972     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
1973     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
1974     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
1975     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
1976     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
1977     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 128, 128, 128, 128, 128, 128,
1978     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1979     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1980     128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11,
1981     12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1982     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1983     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1984     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1985     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1986     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1987     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1988     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1989     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
1990     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28,
1991 ];
1992 
1993 /// Uppercase hexadecimal encoding with case-insensitive decoding
1994 ///
1995 /// This encoding is a static version of:
1996 ///
1997 /// ```rust
1998 /// # use data_encoding::{Specification, HEXUPPER_PERMISSIVE};
1999 /// let mut spec = Specification::new();
2000 /// spec.symbols.push_str("0123456789ABCDEF");
2001 /// spec.translate.from.push_str("abcdef");
2002 /// spec.translate.to.push_str("ABCDEF");
2003 /// assert_eq!(HEXUPPER_PERMISSIVE, spec.encoding().unwrap());
2004 /// ```
2005 ///
2006 /// # Examples
2007 ///
2008 /// ```rust
2009 /// use data_encoding::HEXUPPER_PERMISSIVE;
2010 /// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef];
2011 /// assert_eq!(HEXUPPER_PERMISSIVE.decode(b"DeadBeef").unwrap(), deadbeef);
2012 /// assert_eq!(HEXUPPER_PERMISSIVE.encode(&deadbeef), "DEADBEEF");
2013 /// ```
2014 pub const HEXUPPER_PERMISSIVE: Encoding = Encoding::internal_new(HEXUPPER_PERMISSIVE_IMPL);
2015 const HEXUPPER_PERMISSIVE_IMPL: &[u8] = &[
2016     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
2017     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2018     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
2019     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2020     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
2021     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2022     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
2023     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2024     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55,
2025     56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2026     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 128, 128, 128, 128, 128, 128,
2027     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2028     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2029     128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11,
2030     12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2031     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, 128,
2032     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2033     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2034     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2035     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2036     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2037     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2038     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2039     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28,
2040 ];
2041 
2042 /// Padded base32 encoding
2043 ///
2044 /// This encoding is a static version of:
2045 ///
2046 /// ```rust
2047 /// # use data_encoding::{Specification, BASE32};
2048 /// let mut spec = Specification::new();
2049 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
2050 /// spec.padding = Some('=');
2051 /// assert_eq!(BASE32, spec.encoding().unwrap());
2052 /// ```
2053 ///
2054 /// It conforms to [RFC4648].
2055 ///
2056 /// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-6
2057 pub const BASE32: Encoding = Encoding::internal_new(BASE32_IMPL);
2058 const BASE32_IMPL: &[u8] = &[
2059     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2060     89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
2061     81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72,
2062     73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55,
2063     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2064     89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
2065     81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72,
2066     73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55,
2067     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2068     89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
2069     81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 128, 128, 128, 128, 128, 128,
2070     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2071     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2072     128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 130, 128, 128,
2073     128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
2074     25, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2075     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2076     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2077     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2078     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2079     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2080     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2081     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2082     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 29,
2083 ];
2084 
2085 /// Unpadded base32 encoding
2086 ///
2087 /// This encoding is a static version of:
2088 ///
2089 /// ```rust
2090 /// # use data_encoding::{Specification, BASE32_NOPAD};
2091 /// let mut spec = Specification::new();
2092 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
2093 /// assert_eq!(BASE32_NOPAD, spec.encoding().unwrap());
2094 /// ```
2095 pub const BASE32_NOPAD: Encoding = Encoding::internal_new(BASE32_NOPAD_IMPL);
2096 const BASE32_NOPAD_IMPL: &[u8] = &[
2097     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2098     89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
2099     81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72,
2100     73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55,
2101     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2102     89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
2103     81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72,
2104     73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55,
2105     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2106     89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
2107     81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 128, 128, 128, 128, 128, 128,
2108     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2109     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2110     128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 128,
2111     128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
2112     25, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2113     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2114     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2115     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2116     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2117     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2118     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2119     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2120     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 29,
2121 ];
2122 
2123 /// Padded base32hex encoding
2124 ///
2125 /// This encoding is a static version of:
2126 ///
2127 /// ```rust
2128 /// # use data_encoding::{Specification, BASE32HEX};
2129 /// let mut spec = Specification::new();
2130 /// spec.symbols.push_str("0123456789ABCDEFGHIJKLMNOPQRSTUV");
2131 /// spec.padding = Some('=');
2132 /// assert_eq!(BASE32HEX, spec.encoding().unwrap());
2133 /// ```
2134 ///
2135 /// It conforms to [RFC4648].
2136 ///
2137 /// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-7
2138 pub const BASE32HEX: Encoding = Encoding::internal_new(BASE32HEX_IMPL);
2139 const BASE32HEX_IMPL: &[u8] = &[
2140     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
2141     79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2142     71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55,
2143     56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
2144     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
2145     79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2146     71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55,
2147     56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
2148     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
2149     79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2150     71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 128, 128, 128, 128, 128, 128,
2151     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2152     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2153     128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 130, 128, 128, 128, 10, 11,
2154     12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128,
2155     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2156     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2157     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2158     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2159     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2160     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2161     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2162     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2163     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 29,
2164 ];
2165 
2166 /// Unpadded base32hex encoding
2167 ///
2168 /// This encoding is a static version of:
2169 ///
2170 /// ```rust
2171 /// # use data_encoding::{Specification, BASE32HEX_NOPAD};
2172 /// let mut spec = Specification::new();
2173 /// spec.symbols.push_str("0123456789ABCDEFGHIJKLMNOPQRSTUV");
2174 /// assert_eq!(BASE32HEX_NOPAD, spec.encoding().unwrap());
2175 /// ```
2176 pub const BASE32HEX_NOPAD: Encoding = Encoding::internal_new(BASE32HEX_NOPAD_IMPL);
2177 const BASE32HEX_NOPAD_IMPL: &[u8] = &[
2178     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
2179     79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2180     71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55,
2181     56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
2182     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
2183     79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2184     71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55,
2185     56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
2186     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
2187     79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70,
2188     71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 128, 128, 128, 128, 128, 128,
2189     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2190     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2191     128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11,
2192     12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128,
2193     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2194     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2195     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2196     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2197     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2198     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2199     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2200     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2201     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 29,
2202 ];
2203 
2204 /// DNSSEC base32 encoding
2205 ///
2206 /// This encoding is a static version of:
2207 ///
2208 /// ```rust
2209 /// # use data_encoding::{Specification, BASE32_DNSSEC};
2210 /// let mut spec = Specification::new();
2211 /// spec.symbols.push_str("0123456789abcdefghijklmnopqrstuv");
2212 /// spec.translate.from.push_str("ABCDEFGHIJKLMNOPQRSTUV");
2213 /// spec.translate.to.push_str("abcdefghijklmnopqrstuv");
2214 /// assert_eq!(BASE32_DNSSEC, spec.encoding().unwrap());
2215 /// ```
2216 ///
2217 /// It conforms to [RFC5155]:
2218 ///
2219 /// - It uses a base32 extended hex alphabet.
2220 /// - It is case-insensitive when decoding and uses lowercase when encoding.
2221 /// - It does not use padding.
2222 ///
2223 /// [RFC5155]: https://tools.ietf.org/html/rfc5155
2224 pub const BASE32_DNSSEC: Encoding = Encoding::internal_new(BASE32_DNSSEC_IMPL);
2225 const BASE32_DNSSEC_IMPL: &[u8] = &[
2226     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
2227     108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
2228     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2229     116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104,
2230     105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53,
2231     54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
2232     113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101,
2233     102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49,
2234     50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
2235     110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98,
2236     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2237     118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
2238     107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 128, 128, 128, 128, 128, 128, 128,
2239     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2240     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2241     128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 13,
2242     14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128,
2243     128, 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
2244     26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2245     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2246     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2247     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2248     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2249     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2250     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2251     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 29,
2252 ];
2253 
2254 #[allow(clippy::doc_markdown)]
2255 /// DNSCurve base32 encoding
2256 ///
2257 /// This encoding is a static version of:
2258 ///
2259 /// ```rust
2260 /// # use data_encoding::{BitOrder, Specification, BASE32_DNSCURVE};
2261 /// let mut spec = Specification::new();
2262 /// spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
2263 /// spec.bit_order = BitOrder::LeastSignificantFirst;
2264 /// spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
2265 /// spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
2266 /// assert_eq!(BASE32_DNSCURVE, spec.encoding().unwrap());
2267 /// ```
2268 ///
2269 /// It conforms to [DNSCurve].
2270 ///
2271 /// [DNSCurve]: https://dnscurve.org/in-implement.html
2272 pub const BASE32_DNSCURVE: Encoding = Encoding::internal_new(BASE32_DNSCURVE_IMPL);
2273 const BASE32_DNSCURVE_IMPL: &[u8] = &[
2274     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110,
2275     112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
2276     98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119,
2277     120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107,
2278     108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53,
2279     54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116,
2280     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103,
2281     104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49,
2282     50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113,
2283     114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99,
2284     100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
2285     122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109,
2286     110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 128, 128, 128, 128, 128, 128, 128,
2287     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2288     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2289     128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 128, 10, 11,
2290     12, 128, 13, 14, 15, 128, 16, 17, 18, 19, 20, 128, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
2291     128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 128, 13, 14, 15, 128, 16, 17, 18, 19, 20, 128,
2292     21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2293     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2294     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2295     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2296     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2297     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2298     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2299     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 21,
2300 ];
2301 
2302 /// Padded base64 encoding
2303 ///
2304 /// This encoding is a static version of:
2305 ///
2306 /// ```rust
2307 /// # use data_encoding::{Specification, BASE64};
2308 /// let mut spec = Specification::new();
2309 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2310 /// spec.padding = Some('=');
2311 /// assert_eq!(BASE64, spec.encoding().unwrap());
2312 /// ```
2313 ///
2314 /// It conforms to [RFC4648].
2315 ///
2316 /// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-4
2317 pub const BASE64: Encoding = Encoding::internal_new(BASE64_IMPL);
2318 const BASE64_IMPL: &[u8] = &[
2319     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2320     89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
2321     115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66,
2322     67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
2323     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2324     116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67,
2325     68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97,
2326     98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
2327     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68,
2328     69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98,
2329     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2330     118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128,
2331     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2332     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2333     128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128,
2334     128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
2335     24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
2336     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2337     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2338     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2339     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2340     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2341     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2342     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2343     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30,
2344 ];
2345 
2346 /// Unpadded base64 encoding
2347 ///
2348 /// This encoding is a static version of:
2349 ///
2350 /// ```rust
2351 /// # use data_encoding::{Specification, BASE64_NOPAD};
2352 /// let mut spec = Specification::new();
2353 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2354 /// assert_eq!(BASE64_NOPAD, spec.encoding().unwrap());
2355 /// ```
2356 pub const BASE64_NOPAD: Encoding = Encoding::internal_new(BASE64_NOPAD_IMPL);
2357 const BASE64_NOPAD_IMPL: &[u8] = &[
2358     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2359     89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
2360     115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66,
2361     67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
2362     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2363     116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67,
2364     68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97,
2365     98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
2366     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68,
2367     69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98,
2368     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2369     118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128,
2370     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2371     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2372     128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128,
2373     128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
2374     24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
2375     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2376     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2377     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2378     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2379     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2380     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2381     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2382     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 30,
2383 ];
2384 
2385 /// MIME base64 encoding
2386 ///
2387 /// This encoding is a static version of:
2388 ///
2389 /// ```rust
2390 /// # use data_encoding::{Specification, BASE64_MIME};
2391 /// let mut spec = Specification::new();
2392 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2393 /// spec.padding = Some('=');
2394 /// spec.wrap.width = 76;
2395 /// spec.wrap.separator.push_str("\r\n");
2396 /// assert_eq!(BASE64_MIME, spec.encoding().unwrap());
2397 /// ```
2398 ///
2399 /// It does not exactly conform to [RFC2045] because it does not print the header
2400 /// and does not ignore all characters.
2401 ///
2402 /// [RFC2045]: https://tools.ietf.org/html/rfc2045
2403 pub const BASE64_MIME: Encoding = Encoding::internal_new(BASE64_MIME_IMPL);
2404 const BASE64_MIME_IMPL: &[u8] = &[
2405     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2406     89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
2407     115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66,
2408     67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
2409     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2410     116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67,
2411     68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97,
2412     98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
2413     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68,
2414     69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98,
2415     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2416     118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128,
2417     128, 128, 128, 128, 128, 128, 129, 128, 128, 129, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2418     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2419     128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128,
2420     128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
2421     24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
2422     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2423     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2424     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2425     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2426     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2427     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2428     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2429     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30, 76, 13, 10,
2430 ];
2431 
2432 /// MIME base64 encoding without trailing bits check
2433 ///
2434 /// This encoding is a static version of:
2435 ///
2436 /// ```rust
2437 /// # use data_encoding::{Specification, BASE64_MIME_PERMISSIVE};
2438 /// let mut spec = Specification::new();
2439 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2440 /// spec.padding = Some('=');
2441 /// spec.wrap.width = 76;
2442 /// spec.wrap.separator.push_str("\r\n");
2443 /// spec.check_trailing_bits = false;
2444 /// assert_eq!(BASE64_MIME_PERMISSIVE, spec.encoding().unwrap());
2445 /// ```
2446 ///
2447 /// It does not exactly conform to [RFC2045] because it does not print the header
2448 /// and does not ignore all characters.
2449 ///
2450 /// [RFC2045]: https://tools.ietf.org/html/rfc2045
2451 pub const BASE64_MIME_PERMISSIVE: Encoding = Encoding::internal_new(BASE64_MIME_PERMISSIVE_IMPL);
2452 const BASE64_MIME_PERMISSIVE_IMPL: &[u8] = &[
2453     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2454     89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
2455     115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66,
2456     67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
2457     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2458     116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67,
2459     68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97,
2460     98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
2461     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68,
2462     69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98,
2463     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2464     118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128,
2465     128, 128, 128, 128, 128, 128, 129, 128, 128, 129, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2466     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2467     128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128,
2468     128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
2469     24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
2470     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2471     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2472     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2473     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2474     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2475     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2476     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2477     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 14, 76, 13, 10,
2478 ];
2479 
2480 /// Padded base64url encoding
2481 ///
2482 /// This encoding is a static version of:
2483 ///
2484 /// ```rust
2485 /// # use data_encoding::{Specification, BASE64URL};
2486 /// let mut spec = Specification::new();
2487 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
2488 /// spec.padding = Some('=');
2489 /// assert_eq!(BASE64URL, spec.encoding().unwrap());
2490 /// ```
2491 ///
2492 /// It conforms to [RFC4648].
2493 ///
2494 /// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-5
2495 pub const BASE64URL: Encoding = Encoding::internal_new(BASE64URL_IMPL);
2496 const BASE64URL_IMPL: &[u8] = &[
2497     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2498     89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
2499     115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66,
2500     67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
2501     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2502     116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67,
2503     68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97,
2504     98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
2505     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67, 68,
2506     69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98,
2507     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2508     118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 128, 128, 128, 128,
2509     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2510     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2511     128, 128, 128, 62, 128, 128, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128,
2512     128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
2513     24, 25, 128, 128, 128, 128, 63, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
2514     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2515     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2516     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2517     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2518     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2519     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2520     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2521     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30,
2522 ];
2523 
2524 /// Unpadded base64url encoding
2525 ///
2526 /// This encoding is a static version of:
2527 ///
2528 /// ```rust
2529 /// # use data_encoding::{Specification, BASE64URL_NOPAD};
2530 /// let mut spec = Specification::new();
2531 /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
2532 /// assert_eq!(BASE64URL_NOPAD, spec.encoding().unwrap());
2533 /// ```
2534 pub const BASE64URL_NOPAD: Encoding = Encoding::internal_new(BASE64URL_NOPAD_IMPL);
2535 const BASE64URL_NOPAD_IMPL: &[u8] = &[
2536     65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
2537     89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
2538     115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66,
2539     67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
2540     97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
2541     116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67,
2542     68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97,
2543     98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
2544     117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67, 68,
2545     69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98,
2546     99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
2547     118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 128, 128, 128, 128,
2548     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2549     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2550     128, 128, 128, 62, 128, 128, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128,
2551     128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
2552     24, 25, 128, 128, 128, 128, 63, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
2553     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2554     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2555     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2556     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2557     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2558     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2559     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
2560     128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 30,
2561 ];
2562