data_encoding/
lib.rs

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