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