base16-0.2.1/.circleci/config.yml010064400007650000024000000037621351057644100146530ustar0000000000000000version: 2.1 workflows: version: 2 test: jobs: - Run tests coverage: jobs: - Gen Coverage jobs: Run tests: docker: - image: circleci/rust:1.36.0 steps: - checkout - run: name: Version information command: rustc --version; cargo --version; rustup --version - run: name: Test (all features) command: cargo test --all --verbose - run: name: Test (only alloc) # use `--tests` to skip doctests for the `alloc` feature, since they # fail because of https://github.com/rust-lang/rust/issues/54010 command: cargo test --tests --features=alloc --no-default-features - run: name: Test (no std or alloc) command: cargo test --all --no-default-features - run: # TODO: collect benchmark output somehow? name: Build benchmarks command: cargo bench --no-run # TODO: make sure the fuzz tests build and run for like, at least a second # or so. Gen Coverage: machine: true steps: - checkout - run: name: Download rustup command: | wget https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init chmod +x rustup-init - run: name: Install Rust command: | ./rustup-init -y --no-modify-path rm rustup-init echo 'export PATH=$HOME/.cargo/bin:$PATH' >> $BASH_ENV - run: sudo apt-get update - run: sudo apt-get install libssl-dev pkg-config cmake zlib1g-dev - run: name: Install Tarpaulin command: cargo install cargo-tarpaulin environment: RUSTFLAGS: --cfg procmacro2_semver_exempt - run: name: Generate coverage report command: cargo tarpaulin --out Xml --all-features - run: name: Upload to codecov.io command: bash <(curl -s https://codecov.io/bash) -Z -f cobertura.xml base16-0.2.1/.gitignore010064400007650000024000000000371327241665300130130ustar0000000000000000 /target **/*.rs.bk Cargo.lock base16-0.2.1/benches/bench.rs010064400007650000024000000104431351055475700140640ustar0000000000000000#![allow(unknown_lints)] use criterion::{criterion_group, criterion_main, Criterion, BatchSize, Throughput, ParameterizedBenchmark}; use rand::prelude::*; const SIZES: &[usize] = &[3, 16, 64, 256, 1024]; fn rand_enc_input(sz: usize) -> (Vec, base16::EncConfig) { let mut rng = thread_rng(); let mut vec = vec![0u8; sz]; let cfg = if rng.gen::() { base16::EncodeUpper } else { base16::EncodeLower }; rng.fill_bytes(&mut vec); (vec, cfg) } fn batch_size_for_input(i: usize) -> BatchSize { if i < 1024 { BatchSize::SmallInput } else { BatchSize::LargeInput } } fn bench_encode(c: &mut Criterion) { c.bench( "encode to fresh string", ParameterizedBenchmark::new( "encode_config", |b, items| { b.iter_batched( || rand_enc_input(*items), |(input, enc)| base16::encode_config(&input, enc), batch_size_for_input(*items), ) }, SIZES.iter().cloned(), ).throughput(|bytes| Throughput::Bytes(*bytes as u32)), ); c.bench( "encode to preallocated string", ParameterizedBenchmark::new( "encode_config_buf", |b, items| { b.iter_batched( || (rand_enc_input(*items), String::with_capacity(2 * *items)), |((input, enc), mut buf)| { buf.truncate(0); base16::encode_config_buf(&input, enc, &mut buf) }, batch_size_for_input(*items), ) }, SIZES.iter().cloned(), ).throughput(|bytes| Throughput::Bytes(*bytes as u32)), ); c.bench( "encode to slice", ParameterizedBenchmark::new( "encode_config_slice", |b, items| { b.iter_batched( || (rand_enc_input(*items), vec![0u8; 2 * *items]), |((input, enc), mut dst)| { base16::encode_config_slice(&input, enc, &mut dst) }, batch_size_for_input(*items), ) }, SIZES.iter().cloned(), ).throughput(|bytes| Throughput::Bytes(*bytes as u32)), ); } fn rand_hex_string(size: usize) -> String { let mut rng = thread_rng(); let mut s = String::with_capacity(size); let chars: &[u8] = b"0123456789abcdefABCDEF"; while s.len() < size { s.push(*chars.choose(&mut rng).unwrap() as char); } s } fn bench_decode(c: &mut Criterion) { c.bench( "decode to fresh vec", ParameterizedBenchmark::new( "decode", |b, items| { b.iter_batched( || rand_hex_string(*items), |input| base16::decode(&input), batch_size_for_input(*items), ) }, SIZES.iter().cloned(), ).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)), ); c.bench( "decode to preallocated vec", ParameterizedBenchmark::new( "decode_buf", |b, items| { b.iter_batched( || (rand_hex_string(*items), Vec::with_capacity(*items)), |(input, mut buf)| { buf.truncate(0); base16::decode_buf(&input, &mut buf) }, batch_size_for_input(*items), ) }, SIZES.iter().cloned(), ).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)), ); c.bench( "decode to slice", ParameterizedBenchmark::new( "decode_slice", |b, items| { b.iter_batched( || (rand_hex_string(*items), vec![0u8; *items]), |(input, mut buf)| { base16::decode_slice(&input, &mut buf) }, batch_size_for_input(*items), ) }, SIZES.iter().cloned(), ).throughput(|bytes| Throughput::Bytes(*bytes as u32 * 2)), ); } criterion_group!(benches, bench_encode, bench_decode); criterion_main!(benches); base16-0.2.1/Cargo.toml.orig010064400007650000024000000013461351057644500137170ustar0000000000000000[package] name = "base16" version = "0.2.1" authors = ["Thom Chiovoloni "] keywords = ["hex", "base16", "encode", "decode", "no_std"] repository = "https://github.com/thomcc/rust-base16" description = "base16 (hex) encoding and decoding" categories = ["encoding", "no-std"] license = "CC0-1.0" readme = "README.md" edition = "2018" [badges] circle-ci = { repository = "thomcc/rust-base16", branch = "master" } codecov = { repository = "thomcc/rust-base16", branch = "master", service = "github" } [features] std = ["alloc"] alloc = [] default = ["std"] [dependencies] [dev-dependencies] rand = "0.6.5" criterion = "0.2.11" [[bench]] name = "bench" harness = false [package.metadata.docs.rs] all-features = true base16-0.2.1/Cargo.toml0000644000000024170000000000000101560ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "base16" version = "0.2.1" authors = ["Thom Chiovoloni "] description = "base16 (hex) encoding and decoding" readme = "README.md" keywords = ["hex", "base16", "encode", "decode", "no_std"] categories = ["encoding", "no-std"] license = "CC0-1.0" repository = "https://github.com/thomcc/rust-base16" [package.metadata.docs.rs] all-features = true [[bench]] name = "bench" harness = false [dependencies] [dev-dependencies.criterion] version = "0.2.11" [dev-dependencies.rand] version = "0.6.5" [features] alloc = [] default = ["std"] std = ["alloc"] [badges.circle-ci] branch = "master" repository = "thomcc/rust-base16" [badges.codecov] branch = "master" repository = "thomcc/rust-base16" service = "github" base16-0.2.1/CHANGELOG.md010064400007650000024000000025741351057660400126420ustar0000000000000000# 0.2.0 - `encode_byte` now returns `[u8; 2]` instead of `(u8, u8)`, as in practice this tends to be more convenient. - The use of `std` which requires the `alloc` trait has been split into the `alloc` feature. - `base16` has been relicensed as CC0-1.0 from dual MIT/Apache-2.0. # 0.2.1 - Make code more bulletproof in the case of panics when using the `decode_buf` or `encode_config_buf` functions. Previously, if the innermost encode/decode function paniced, code that inspected the contents of the Vec (either in a Drop implementation, or by catching the panic) could read from uninitialized memory. However, I don't believe it is possible for the innermost encode/decode functions to panic, so I don't think there was any risk in previous versions. I don't believe the panic is possible because only two panics exist in the generated assembly (both only in debug configuration, and not in release). The two panics are respectively: - a debug_assert verifying the caller performed a check (which it does). - a usize overflow check on an index variable, which is impossible as we've already tested for that. That said, this is some powerful rationalization, so I'm cutting a new version with this fix anyway. - Additionally, several functions that previously used unsafe internally now either use less unsafe, or are entirely safe. base16-0.2.1/LICENSE-CC0010064400007650000024000000156101351051641700123700ustar0000000000000000Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. base16-0.2.1/README.md010064400007650000024000000047671351057675700123270ustar0000000000000000# [base16](https://crates.io/crates/base16) (hex) encoding for Rust. [![Docs](https://docs.rs/base16/badge.svg)](https://docs.rs/base16) [![CircleCI](https://circleci.com/gh/thomcc/rust-base16.svg?style=svg)](https://circleci.com/gh/thomcc/rust-base16) [![codecov](https://codecov.io/gh/thomcc/rust-base16/branch/master/graph/badge.svg)](https://codecov.io/gh/thomcc/rust-base16) This is a base16 (e.g. hexadecimal) encoding and decoding library which was initially written with an emphasis on performance. This was before Rust added SIMD, and I haven't gotten around to adding that. It's still probably the fastest non-SIMD impl. ## Usage Add `base16 = "0.2"` to Cargo.toml, then: ```rust fn main() { let original_msg = "Foobar"; let hex_string = base16::encode_lower(original_msg); assert_eq!(hex_string, "466f6f626172"); let decoded = base16::decode(&hex_string).unwrap(); assert_eq!(String::from_utf8(decoded).unwrap(), original_msg); } ``` More usage examples in the [docs](https://docs.rs/base16). ## `no_std` Usage This crate supports use in `no_std` configurations using the following knobs. - The `"alloc"` feature, which is on by default, adds a number of helpful functions that require use of the [`alloc`](https://doc.rust-lang.org/alloc/index.html) crate, but not the rest of `std`. This is `no_std` compatible. - Each function documents if it requires use of the `alloc` feature. - The `"std"` feature, which is on by default, enables the `"alloc"` feature, and additionally makes `base16::DecodeError` implement the `std::error::Error` trait. (Frustratingly, this trait is in `std` and not in `core` or `alloc`...) For clarity, this means that by default, we assume you are okay with use of `std`. If you'd like to disable the use of `std`, but are in an environment where you have an allocator (e.g. use of the [`alloc`](https://doc.rust-lang.org/alloc/index.html) crate is acceptable), then you require this as `alloc`-only as follows: ```toml [dependencies] # Turn of use of `std` (but leave use of `alloc`). base16 = { version = "0.2", default-features = false, features = ["alloc"] } ``` If you just want the core `base16` functionality and none of the helpers, then you should turn off all features. ```toml [dependencies] # Turn of use of `std` and `alloc`. base16 = { version = "0.2", default-features = false } ``` Both of these configurations are `no_std` compatible. # License Public domain, as explained [here](https://creativecommons.org/publicdomain/zero/1.0/legalcode) base16-0.2.1/src/lib.rs010064400007650000024000000540311351057644100127260ustar0000000000000000//! This is a base16 (e.g. hexadecimal) encoding and decoding library with an //! emphasis on performance. The API is very similar and inspired by the base64 //! crate's API, however it's less complex (base16 is much more simple than //! base64). //! //! # Encoding //! //! The config options at the moment are limited to the output case (upper vs //! lower). //! //! | Function | Output | Allocates | Requires `alloc` feature | //! | ---------------------------------- | ---------------------------- | ----------------------- | ------------------------ | //! | [`encode_upper`], [`encode_lower`] | Returns a new `String` | Always | Yes | //! | [`encode_config`] | Returns a new `String` | Always | Yes | //! | [`encode_config_buf`] | Appends to provided `String` | If buffer needs to grow | Yes | //! | [`encode_config_slice`] | Writes to provided `&[u8]` | Never | No | //! //! # Decoding //! //! Note that there are no config options (In the future one might be added to //! restrict the input character set, but it's not clear to me that this is //! useful). //! //! | Function | Output | Allocates | Requires `alloc` feature | //! | ----------------- | ----------------------------- | ----------------------- | ------------------------ | //! | [`decode`] | Returns a new `Vec` | Always | Yes | //! | [`decode_slice`] | Writes to provided `&[u8]` | Never | No | //! | [`decode_buf`] | Appends to provided `Vec` | If buffer needs to grow | Yes | //! //! # Features //! //! This crate has two features, both are enabled by default and exist to allow //! users in `no_std` environments to disable various portions of . //! //! - The `"alloc"` feature, which is on by default, adds a number of helpful //! functions that require use of the [`alloc`][alloc_crate] crate, but not the //! rest of `std`. //! - This is `no_std` compatible. //! - Each function should list whether or not it requires this feature //! under the `Availability` of its documentation. //! //! - The `"std"` feature, which is on by default, enables the `"alloc"` //! feature, and additionally makes [`DecodeError`] implement the //! `std::error::Error` trait. //! //! - Frustratingly, this trait is in `std` (and not in `core` or `alloc`), //! but not implementing it would be quite annoying for some users, so //! it's kept, even though it's what prevents us from being `no_std` //! compatible in all configurations. //! //! [alloc_crate]: https://doc.rust-lang.org/alloc/index.html #![cfg_attr(not(feature = "std"), no_std)] #![deny(missing_docs)] #[cfg(feature = "alloc")] extern crate alloc; #[cfg(feature = "alloc")] use alloc::{vec::Vec, string::String}; /// Configuration options for encoding. Just specifies whether or not output /// should be uppercase or lowercase. #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum EncConfig { /// Encode using lower case characters for hex values >= 10 EncodeLower, /// Encode using upper case characters for hex values >= 10 EncodeUpper, } pub use EncConfig::*; #[inline] fn encoded_size(source_len: usize) -> usize { const USIZE_TOP_BIT: usize = 1usize << (core::mem::size_of::() * 8 - 1); if (source_len & USIZE_TOP_BIT) != 0 { usize_overflow(source_len) } source_len << 1 } #[inline] fn encode_slice_raw(src: &[u8], cfg: EncConfig, dst: &mut [u8]) { let lut = if cfg == EncodeLower { HEX_LOWER } else { HEX_UPPER }; debug_assert!(dst.len() == encoded_size(src.len())); dst.chunks_exact_mut(2).zip(src.iter().copied()).for_each(|(d, sb)| { d[0] = lut[(sb >> 4) as usize]; d[1] = lut[(sb & 0xf) as usize]; }) } #[cfg(feature = "alloc")] #[inline] fn encode_to_string(bytes: &[u8], cfg: EncConfig) -> String { let size = encoded_size(bytes.len()); let mut buf: Vec = Vec::with_capacity(size); unsafe { buf.set_len(size); } encode_slice_raw(bytes, cfg, &mut buf); debug_assert!(core::str::from_utf8(&buf).is_ok()); unsafe { String::from_utf8_unchecked(buf) } } #[cfg(feature = "alloc")] #[inline] unsafe fn grow_vec_uninitialized(v: &mut Vec, grow_by: usize) -> usize { v.reserve(grow_by); let initial_len = v.len(); let new_len = initial_len + grow_by; debug_assert!(new_len <= v.capacity()); v.set_len(new_len); initial_len } /// Encode bytes as base16, using lower case characters for nibbles between 10 /// and 15 (`a` through `f`). /// /// This is equivalent to `base16::encode_config(bytes, base16::EncodeUpper)`. /// /// # Example /// /// ``` /// assert_eq!(base16::encode_lower(b"Hello World"), "48656c6c6f20576f726c64"); /// assert_eq!(base16::encode_lower(&[0xff, 0xcc, 0xaa]), "ffccaa"); /// ``` /// /// # Availability /// /// This function is only available when the `alloc` feature is enabled, as it /// needs to produce a String. #[cfg(feature = "alloc")] #[inline] pub fn encode_lower>(input: &T) -> String { encode_to_string(input.as_ref(), EncodeLower) } /// Encode bytes as base16, using upper case characters for nibbles between /// 10 and 15 (`A` through `F`). /// /// This is equivalent to `base16::encode_config(bytes, base16::EncodeUpper)`. /// /// # Example /// /// ``` /// assert_eq!(base16::encode_upper(b"Hello World"), "48656C6C6F20576F726C64"); /// assert_eq!(base16::encode_upper(&[0xff, 0xcc, 0xaa]), "FFCCAA"); /// ``` /// /// # Availability /// /// This function is only available when the `alloc` feature is enabled, as it /// needs to produce a `String`. #[cfg(feature = "alloc")] #[inline] pub fn encode_upper>(input: &T) -> String { encode_to_string(input.as_ref(), EncodeUpper) } /// Encode `input` into a string using the listed config. The resulting string /// contains `input.len() * 2` bytes. /// /// # Example /// /// ``` /// let data = [1, 2, 3, 0xaa, 0xbb, 0xcc]; /// assert_eq!(base16::encode_config(&data, base16::EncodeLower), "010203aabbcc"); /// assert_eq!(base16::encode_config(&data, base16::EncodeUpper), "010203AABBCC"); /// ``` /// /// # Availability /// /// This function is only available when the `alloc` feature is enabled, as it /// needs to produce a `String`. #[cfg(feature = "alloc")] #[inline] pub fn encode_config>(input: &T, cfg: EncConfig) -> String { encode_to_string(input.as_ref(), cfg) } /// Encode `input` into the end of the provided buffer. Returns the number of /// bytes that were written. /// /// Only allocates when `dst.size() + (input.len() * 2) >= dst.capacity()`. /// /// # Example /// /// ``` /// let messages = &["Taako, ", "Merle, ", "Magnus"]; /// let mut buffer = String::new(); /// for msg in messages { /// let bytes_written = base16::encode_config_buf(msg.as_bytes(), /// base16::EncodeUpper, /// &mut buffer); /// assert_eq!(bytes_written, msg.len() * 2); /// } /// assert_eq!(buffer, "5461616B6F2C204D65726C652C204D61676E7573"); /// ``` /// # Availability /// /// This function is only available when the `alloc` feature is enabled, as it /// needs write to a `String`. #[cfg(feature = "alloc")] #[inline] pub fn encode_config_buf>(input: &T, cfg: EncConfig, dst: &mut String) -> usize { let src = input.as_ref(); let bytes_to_write = encoded_size(src.len()); // Swap the string out while we work on it, so that if we panic, we don't // leave behind garbage (we do clear the string if we panic, but that's // better than UB) let mut buf = core::mem::replace(dst, String::new()).into_bytes(); let cur_size = unsafe { grow_vec_uninitialized(&mut buf, bytes_to_write) }; encode_slice_raw(src, cfg, &mut buf[cur_size..]); debug_assert!(core::str::from_utf8(&buf).is_ok()); // Put `buf` back into `dst`. *dst = unsafe { String::from_utf8_unchecked(buf) }; bytes_to_write } /// Write bytes as base16 into the provided output buffer. Never allocates. /// /// This is useful if you wish to avoid allocation entirely (e.g. your /// destination buffer is on the stack), or control it precisely. /// /// # Panics /// /// Panics if the desination buffer is insufficiently large. /// /// # Example /// /// ``` /// # extern crate core as std; /// // Writing to a statically sized buffer on the stack. /// let message = b"Wu-Tang Killa Bees"; /// let mut buffer = [0u8; 1024]; /// /// let wrote = base16::encode_config_slice(message, /// base16::EncodeLower, /// &mut buffer); /// /// assert_eq!(message.len() * 2, wrote); /// assert_eq!(std::str::from_utf8(&buffer[..wrote]).unwrap(), /// "57752d54616e67204b696c6c612042656573"); /// /// // Appending to an existing buffer is possible too. /// let wrote2 = base16::encode_config_slice(b": The Swarm", /// base16::EncodeLower, /// &mut buffer[wrote..]); /// let write_end = wrote + wrote2; /// assert_eq!(std::str::from_utf8(&buffer[..write_end]).unwrap(), /// "57752d54616e67204b696c6c6120426565733a2054686520537761726d"); /// ``` /// # Availability /// /// This function is available whether or not the `alloc` feature is enabled. #[inline] pub fn encode_config_slice>(input: &T, cfg: EncConfig, dst: &mut [u8]) -> usize { let src = input.as_ref(); let need_size = encoded_size(src.len()); if dst.len() < need_size { dest_too_small_enc(dst.len(), need_size); } encode_slice_raw(src, cfg, &mut dst[..need_size]); need_size } /// Encode a single character as hex, returning a tuple containing the two /// encoded bytes in big-endian order -- the order the characters would be in /// when written out (e.g. the top nibble is the first item in the tuple) /// /// # Example /// ``` /// assert_eq!(base16::encode_byte(0xff, base16::EncodeLower), [b'f', b'f']); /// assert_eq!(base16::encode_byte(0xa0, base16::EncodeUpper), [b'A', b'0']); /// assert_eq!(base16::encode_byte(3, base16::EncodeUpper), [b'0', b'3']); /// ``` /// # Availability /// /// This function is available whether or not the `alloc` feature is enabled. #[inline] pub fn encode_byte(byte: u8, cfg: EncConfig) -> [u8; 2] { let lut = if cfg == EncodeLower { HEX_LOWER } else { HEX_UPPER }; let lo = lut[(byte & 15) as usize]; let hi = lut[(byte >> 4) as usize]; [hi, lo] } /// Convenience wrapper for `base16::encode_byte(byte, base16::EncodeLower)` /// /// See also `base16::encode_byte_u`. /// /// # Example /// ``` /// assert_eq!(base16::encode_byte_l(0xff), [b'f', b'f']); /// assert_eq!(base16::encode_byte_l(30), [b'1', b'e']); /// assert_eq!(base16::encode_byte_l(0x2d), [b'2', b'd']); /// ``` /// # Availability /// /// This function is available whether or not the `alloc` feature is enabled. #[inline] pub fn encode_byte_l(byte: u8) -> [u8; 2] { encode_byte(byte, EncodeLower) } /// Convenience wrapper for `base16::encode_byte(byte, base16::EncodeUpper)` /// /// See also `base16::encode_byte_l`. /// /// # Example /// ``` /// assert_eq!(base16::encode_byte_u(0xff), [b'F', b'F']); /// assert_eq!(base16::encode_byte_u(30), [b'1', b'E']); /// assert_eq!(base16::encode_byte_u(0x2d), [b'2', b'D']); /// ``` /// # Availability /// /// This function is available whether or not the `alloc` feature is enabled. #[inline] pub fn encode_byte_u(byte: u8) -> [u8; 2] { encode_byte(byte, EncodeUpper) } /// Represents a problem with the data we want to decode. /// /// This implements `std::error::Error` and `Display` if the `std` /// feature is enabled, but only `Display` if it is not. #[derive(Debug, PartialEq, Eq, Clone)] pub enum DecodeError { /// An invalid byte was found in the input (bytes must be `[0-9a-fA-F]`) InvalidByte { /// The index at which the problematic byte was found. index: usize, /// The byte that we cannot decode. byte: u8 }, /// The length of the input not a multiple of two InvalidLength { /// The input length. length: usize }, } #[cold] fn invalid_length(length: usize) -> DecodeError { DecodeError::InvalidLength { length } } #[cold] fn invalid_byte(index: usize, src: &[u8]) -> DecodeError { DecodeError::InvalidByte { index, byte: src[index] } } impl core::fmt::Display for DecodeError { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match *self { DecodeError::InvalidByte { index, byte } => { write!(f, "Invalid byte `b{:?}`, at index {}.", byte as char, index) } DecodeError::InvalidLength { length } => write!(f, "Base16 data cannot have length {} (must be even)", length), } } } #[cfg(feature = "std")] impl std::error::Error for DecodeError { fn description(&self) -> &str { match *self { DecodeError::InvalidByte { .. } => "Illegal byte in base16 data", DecodeError::InvalidLength { .. } => "Illegal length for base16 data", } } fn cause(&self) -> Option<&dyn std::error::Error> { None } } #[inline] fn decode_slice_raw(src: &[u8], dst: &mut[u8]) -> Result<(), usize> { // checked in caller. debug_assert!(src.len() / 2 == dst.len()); debug_assert!((src.len() & 1) == 0); src.chunks_exact(2).enumerate().zip(dst.iter_mut()).try_for_each(|((si, s), d)| { let r0 = DECODE_LUT[s[0] as usize]; let r1 = DECODE_LUT[s[1] as usize]; if (r0 | r1) >= 0 { *d = ((r0 << 4) | r1) as u8; Ok(()) } else { Err(si * 2) } }).map_err(|bad_idx| raw_decode_err(bad_idx, src)) } #[cold] #[inline(never)] fn raw_decode_err(idx: usize, src: &[u8]) -> usize { let b0 = src[idx]; if decode_byte(b0).is_none() { idx } else { idx + 1 } } /// Decode bytes from base16, and return a new `Vec` containing the results. /// /// # Example /// /// ``` /// assert_eq!(&base16::decode("48656c6c6f20576f726c64".as_bytes()).unwrap(), /// b"Hello World"); /// assert_eq!(&base16::decode(b"deadBEEF").unwrap(), /// &[0xde, 0xad, 0xbe, 0xef]); /// // Error cases: /// assert_eq!(base16::decode(b"Not Hexadecimal!"), /// Err(base16::DecodeError::InvalidByte { byte: b'N', index: 0 })); /// assert_eq!(base16::decode(b"a"), /// Err(base16::DecodeError::InvalidLength { length: 1 })); /// ``` /// # Availability /// /// This function is only available when the `alloc` feature is enabled, as it /// needs to produce a Vec. #[cfg(feature = "alloc")] #[inline] pub fn decode>(input: &T) -> Result, DecodeError> { let src = input.as_ref(); if (src.len() & 1) != 0 { return Err(invalid_length(src.len())); } let need_size = src.len() >> 1; let mut dst = Vec::with_capacity(need_size); unsafe { dst.set_len(need_size); } match decode_slice_raw(src, &mut dst) { Ok(()) => Ok(dst), Err(index) => Err(invalid_byte(index, src)) } } /// Decode bytes from base16, and appends into the provided buffer. Only /// allocates if the buffer could not fit the data. Returns the number of bytes /// written. /// /// In the case of an error, the buffer should remain the same size. /// /// # Example /// /// ``` /// # extern crate core as std; /// # extern crate alloc; /// # use alloc::vec::Vec; /// let mut result = Vec::new(); /// assert_eq!(base16::decode_buf(b"4d61646f6b61", &mut result).unwrap(), 6); /// assert_eq!(base16::decode_buf(b"486F6D757261", &mut result).unwrap(), 6); /// assert_eq!(std::str::from_utf8(&result).unwrap(), "MadokaHomura"); /// ``` /// # Availability /// /// This function is only available when the `alloc` feature is enabled, as it /// needs to write to a Vec. #[cfg(feature = "alloc")] #[inline] pub fn decode_buf>(input: &T, v: &mut Vec) -> Result { let src = input.as_ref(); if (src.len() & 1) != 0 { return Err(invalid_length(src.len())); } // Swap the vec out while we work on it, so that if we panic, we don't leave // behind garbage (this will end up cleared if we panic, but that's better // than UB) let mut work = core::mem::replace(v, Vec::default()); let need_size = src.len() >> 1; let current_size = unsafe { grow_vec_uninitialized(&mut work, need_size) }; match decode_slice_raw(src, &mut work[current_size..]) { Ok(()) => { // Swap back core::mem::swap(v, &mut work); Ok(need_size) } Err(index) => { work.truncate(current_size); // Swap back core::mem::swap(v, &mut work); Err(invalid_byte(index, src)) } } } /// Decode bytes from base16, and write into the provided buffer. Never /// allocates. /// /// In the case of a decoder error, the output is not specified, but in practice /// will remain untouched for an `InvalidLength` error, and will contain the /// decoded input up to the problem byte in the case of an InvalidByte error. /// /// # Panics /// /// Panics if the provided buffer is not large enough for the input. /// /// # Example /// ``` /// let msg = "476f6f642072757374206c6962726172696573207573652073696c6c79206578616d706c6573"; /// let mut buf = [0u8; 1024]; /// assert_eq!(base16::decode_slice(&msg[..], &mut buf).unwrap(), 38); /// assert_eq!(&buf[..38], b"Good rust libraries use silly examples".as_ref()); /// /// let msg2 = b"2E20416C736F2C20616E696D65207265666572656e636573"; /// assert_eq!(base16::decode_slice(&msg2[..], &mut buf[38..]).unwrap(), 24); /// assert_eq!(&buf[38..62], b". Also, anime references".as_ref()); /// ``` /// # Availability /// /// This function is available whether or not the `alloc` feature is enabled. #[inline] pub fn decode_slice>(input: &T, out: &mut [u8]) -> Result { let src = input.as_ref(); if (src.len() & 1) != 0 { return Err(invalid_length(src.len())); } let need_size = src.len() >> 1; if out.len() < need_size { dest_too_small_dec(out.len(), need_size); } match decode_slice_raw(src, &mut out[..need_size]) { Ok(()) => Ok(need_size), Err(index) => Err(invalid_byte(index, src)) } } /// Decode a single character as hex. /// /// Returns `None` for values outside the ASCII hex range. /// /// # Example /// ``` /// assert_eq!(base16::decode_byte(b'a'), Some(10)); /// assert_eq!(base16::decode_byte(b'B'), Some(11)); /// assert_eq!(base16::decode_byte(b'0'), Some(0)); /// assert_eq!(base16::decode_byte(b'q'), None); /// assert_eq!(base16::decode_byte(b'x'), None); /// ``` /// # Availability /// /// This function is available whether or not the `alloc` feature is enabled. #[inline] pub fn decode_byte(c: u8) -> Option { if c.wrapping_sub(b'0') <= 9 { Some(c.wrapping_sub(b'0')) } else if c.wrapping_sub(b'a') < 6 { Some(c.wrapping_sub(b'a') + 10) } else if c.wrapping_sub(b'A') < 6 { Some(c.wrapping_sub(b'A') + 10) } else { None } } static HEX_UPPER: [u8; 16] = *b"0123456789ABCDEF"; static HEX_LOWER: [u8; 16] = *b"0123456789abcdef"; static DECODE_LUT: [i8; 256] = [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ]; // Outlined assertions. #[inline(never)] #[cold] fn usize_overflow(len: usize) -> ! { panic!("usize overflow when computing size of destination: {}", len); } #[cold] #[inline(never)] fn dest_too_small_enc(dst_len: usize, need_size: usize) -> ! { panic!("Destination is not large enough to encode input: {} < {}", dst_len, need_size); } #[cold] #[inline(never)] fn dest_too_small_dec(dst_len: usize, need_size: usize) -> ! { panic!("Destination buffer not large enough for decoded input {} < {}", dst_len, need_size); } // encoded_size smoke tests #[cfg(test)] mod tests { use super::*; #[test] #[should_panic] #[cfg(pointer_size )] fn test_encoded_size_panic_top_bit() { #[cfg(target_pointer_width = "64")] let usz = 0x8000_0000_0000_0000usize; #[cfg(target_pointer_width = "32")] let usz = 0x8000_0000usize; let _ = encoded_size(usz); } #[test] #[should_panic] fn test_encoded_size_panic_max() { let _ = encoded_size(usize::max_value()); } #[test] fn test_encoded_size_allows_almost_max() { #[cfg(target_pointer_width = "64")] let usz = 0x7fff_ffff_ffff_ffffusize; #[cfg(target_pointer_width = "32")] let usz = 0x7fff_ffffusize; assert_eq!(encoded_size(usz), usz * 2); } } base16-0.2.1/tests/doctest_copies.rs010064400007650000024000000114201351057644100155350ustar0000000000000000 #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "alloc")] extern crate alloc; // Can't run doctests for a no_std crate if it uses allocator (e.g. can't run // them if we're using `alloc`), so we duplicate them here... // See https://github.com/rust-lang/rust/issues/54010 #[cfg(feature = "alloc")] #[test] fn test_encode_lower() { assert_eq!(base16::encode_lower(b"Hello World"), "48656c6c6f20576f726c64"); assert_eq!(base16::encode_lower(&[0xff, 0xcc, 0xaa]), "ffccaa"); } #[cfg(feature = "alloc")] #[test] fn test_encode_upper() { assert_eq!(base16::encode_upper(b"Hello World"), "48656C6C6F20576F726C64"); assert_eq!(base16::encode_upper(&[0xff, 0xcc, 0xaa]), "FFCCAA"); } #[cfg(feature = "alloc")] #[test] fn test_encode_config() { let data = [1, 2, 3, 0xaa, 0xbb, 0xcc]; assert_eq!(base16::encode_config(&data, base16::EncodeLower), "010203aabbcc"); assert_eq!(base16::encode_config(&data, base16::EncodeUpper), "010203AABBCC"); } #[cfg(feature = "alloc")] #[test] fn test_encode_config_buf() { use alloc::string::String; let messages = &["Taako, ", "Merle, ", "Magnus"]; let mut buffer = String::new(); for msg in messages { let bytes_written = base16::encode_config_buf(msg.as_bytes(), base16::EncodeUpper, &mut buffer); assert_eq!(bytes_written, msg.len() * 2); } assert_eq!(buffer, "5461616B6F2C204D65726C652C204D61676E7573"); } #[test] fn test_encode_config_slice() { // Writing to a statically sized buffer on the stack. let message = b"Wu-Tang Killa Bees"; let mut buffer = [0u8; 1024]; let wrote = base16::encode_config_slice(message, base16::EncodeLower, &mut buffer); assert_eq!(message.len() * 2, wrote); assert_eq!(core::str::from_utf8(&buffer[..wrote]).unwrap(), "57752d54616e67204b696c6c612042656573"); // Appending to an existing buffer is possible too. let wrote2 = base16::encode_config_slice(b": The Swarm", base16::EncodeLower, &mut buffer[wrote..]); let write_end = wrote + wrote2; assert_eq!(core::str::from_utf8(&buffer[..write_end]).unwrap(), "57752d54616e67204b696c6c6120426565733a2054686520537761726d"); } #[test] fn test_encode_config_byte() { assert_eq!(base16::encode_byte(0xff, base16::EncodeLower), [b'f', b'f']); assert_eq!(base16::encode_byte(0xa0, base16::EncodeUpper), [b'A', b'0']); assert_eq!(base16::encode_byte(3, base16::EncodeUpper), [b'0', b'3']); } #[test] fn test_encode_config_byte_l() { assert_eq!(base16::encode_byte_l(0xff), [b'f', b'f']); assert_eq!(base16::encode_byte_l(30), [b'1', b'e']); assert_eq!(base16::encode_byte_l(0x2d), [b'2', b'd']); } #[test] fn test_encode_config_byte_u() { assert_eq!(base16::encode_byte_u(0xff), [b'F', b'F']); assert_eq!(base16::encode_byte_u(30), [b'1', b'E']); assert_eq!(base16::encode_byte_u(0x2d), [b'2', b'D']); } #[cfg(feature = "alloc")] #[test] fn test_decode() { assert_eq!(&base16::decode("48656c6c6f20576f726c64".as_bytes()).unwrap(), b"Hello World"); assert_eq!(&base16::decode(b"deadBEEF").unwrap(), &[0xde, 0xad, 0xbe, 0xef]); // Error cases: assert_eq!(base16::decode(b"Not Hexadecimal!"), Err(base16::DecodeError::InvalidByte { byte: b'N', index: 0 })); assert_eq!(base16::decode(b"a"), Err(base16::DecodeError::InvalidLength { length: 1 })); } #[cfg(feature = "alloc")] #[test] fn test_decode_buf() { use alloc::vec::Vec; let mut result = Vec::new(); assert_eq!(base16::decode_buf(b"4d61646f6b61", &mut result).unwrap(), 6); assert_eq!(base16::decode_buf(b"486F6D757261", &mut result).unwrap(), 6); assert_eq!(core::str::from_utf8(&result).unwrap(), "MadokaHomura"); } #[test] fn test_decode_slice() { let msg = "476f6f642072757374206c6962726172696573207573652073696c6c79206578616d706c6573"; let mut buf = [0u8; 1024]; assert_eq!(base16::decode_slice(&msg[..], &mut buf).unwrap(), 38); assert_eq!(&buf[..38], b"Good rust libraries use silly examples".as_ref()); let msg2 = b"2E20416C736F2C20616E696D65207265666572656e636573"; assert_eq!(base16::decode_slice(&msg2[..], &mut buf[38..]).unwrap(), 24); assert_eq!(&buf[38..62], b". Also, anime references".as_ref()); } #[test] fn test_decode_byte() { assert_eq!(base16::decode_byte(b'a'), Some(10)); assert_eq!(base16::decode_byte(b'B'), Some(11)); assert_eq!(base16::decode_byte(b'0'), Some(0)); assert_eq!(base16::decode_byte(b'q'), None); assert_eq!(base16::decode_byte(b'x'), None); } base16-0.2.1/tests/tests.rs010064400007650000024000000151571351051641700137000ustar0000000000000000 #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "alloc")] #[macro_use] extern crate alloc; use base16::*; const ALL_LOWER: &[&str] = &[ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff", ]; const ALL_UPPER: &[&str] = &[ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF", ]; #[cfg(feature = "alloc")] #[test] fn test_exhaustive_bytes_encode() { for i in 0..256 { assert_eq!(&encode_lower(&[i as u8]), ALL_LOWER[i]); assert_eq!(&encode_upper(&[i as u8]), ALL_UPPER[i]); } } #[cfg(feature = "alloc")] #[test] fn test_exhaustive_bytes_decode() { for i in 0..16 { for j in 0..16 { let all_cases = format!("{0:x}{1:x}{0:x}{1:X}{0:X}{1:x}{0:X}{1:X}", i, j); let byte = i * 16 + j; let expect = &[byte, byte, byte, byte]; assert_eq!(&decode(&all_cases).unwrap(), expect, "Failed for {}", all_cases); } } for b in 0..256 { let i = b as u8; let expected = match i { b'0' | b'1' | b'2' | b'3' | b'4' | b'5' | b'6' | b'7' | b'8' | b'9' => Ok(vec![i - b'0']), b'a' | b'b' | b'c' | b'd' | b'e' | b'f' => Ok(vec![i - b'a' + 10]), b'A' | b'B' | b'C' | b'D' | b'E' | b'F' => Ok(vec![i - b'A' + 10]), _ => Err(DecodeError::InvalidByte { byte: i, index: 1 }) }; assert_eq!(decode(&[b'0', i]), expected); } } #[cfg(feature = "alloc")] #[test] fn test_decode_errors() { let mut buf = decode(b"686f6d61646f6b61").unwrap(); let orig = buf.clone(); assert_eq!(buf.len(), 8); assert_eq!(decode_buf(b"abc", &mut buf), Err(DecodeError::InvalidLength { length: 3 })); assert_eq!(buf, orig); assert_eq!(decode_buf(b"6d61646f686f6d75g_", &mut buf), Err(DecodeError::InvalidByte { byte: b'g', index: 16 })); assert_eq!(buf, orig); } #[test] #[should_panic] fn test_panic_slice_encode() { let mut slice = [0u8; 8]; encode_config_slice(b"Yuasa", EncodeLower, &mut slice); } #[test] #[should_panic] fn test_panic_slice_decode() { let mut slice = [0u8; 32]; let input = b"4920646f6e277420636172652074686174206d7563682061626f757420504d4d4d20544248"; let _ignore = decode_slice(&input[..], &mut slice); } #[test] fn test_enc_slice_exact_fit() { let mut slice = [0u8; 12]; let res = encode_config_slice(b"abcdef", EncodeLower, &mut slice); assert_eq!(res, 12); assert_eq!(&slice, b"616263646566") } #[test] fn test_exhaustive_encode_byte() { for i in 0..256 { let byte = i as u8; let su = ALL_UPPER[byte as usize].as_bytes(); let sl = ALL_LOWER[byte as usize].as_bytes(); let tu = encode_byte(byte, EncodeUpper); let tl = encode_byte(byte, EncodeLower); assert_eq!(tu[0], su[0]); assert_eq!(tu[1], su[1]); assert_eq!(tl[0], sl[0]); assert_eq!(tl[1], sl[1]); assert_eq!(tu, encode_byte_u(byte)); assert_eq!(tl, encode_byte_l(byte)); } } const HEX_TO_VALUE: &[(u8, u8)] = &[ (b'0', 0x0), (b'1', 0x1), (b'2', 0x2), (b'3', 0x3), (b'4', 0x4), (b'5', 0x5), (b'6', 0x6), (b'7', 0x7), (b'8', 0x8), (b'9', 0x9), (b'a', 0xa), (b'b', 0xb), (b'c', 0xc), (b'd', 0xd), (b'e', 0xe), (b'f', 0xf), (b'A', 0xA), (b'B', 0xB), (b'C', 0xC), (b'D', 0xD), (b'E', 0xE), (b'F', 0xF), ]; #[test] fn test_exhaustive_decode_byte() { let mut expected = [None::; 256]; for &(k, v) in HEX_TO_VALUE { expected[k as usize] = Some(v); } for i in 0..256 { assert_eq!(decode_byte(i as u8), expected[i]); } } base16-0.2.1/.cargo_vcs_info.json0000644000000001120000000000000121460ustar00{ "git": { "sha1": "a5321823aa9361279d12ee460aa99ca1a4e62ddf" } }