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