Debian Codegraph @

third_party/rust/data-encoding/src/lib.rs

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