Bug 1716518 - Upgrade hex to v0.4.3. r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D117795
This commit is contained in:
Mike Hommey 2021-06-15 21:24:44 +00:00
Родитель e7619f1be9
Коммит ec1c214492
10 изменённых файлов: 140 добавлений и 85 удалений

4
Cargo.lock сгенерированный
Просмотреть файл

@ -2317,9 +2317,9 @@ dependencies = [
[[package]]
name = "hex"
version = "0.4.2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "http"

2
third_party/rust/hex/.cargo-checksum.json поставляемый
Просмотреть файл

@ -1 +1 @@
{"files":{"Cargo.toml":"3b509da1763642f9bd85382ae9d83a5f4355cf201b0fab9de719aa635ea0ff70","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"f7bdb3426d045cd50efd4953026e3eb5a83d0199f458a075602611b9344da5b9","README.md":"4bbf89813cd9d2a04707299f34e1c906ed9e8247b910043f38d47b18a0d0641b","benches/hex.rs":"f1301fdaaf5f31cdd16e6988673201c9ce5b7dc65ae7cb7c69559b7adabf38bc","src/error.rs":"7c8af5efa9135fa24fc37dfcd973112c0766ccc7a4826078edb045df9a8efd79","src/lib.rs":"734506f5d604c7d7575d2c6bc31c75a10f628c7b41daa6be11656bd6f7117191","src/serde.rs":"8fb25650a850ae258ff9178bb8818145101dd3df40aa2725fc11b81bdd5d4567","tests/serde.rs":"28dca8a567daf9861413d1b1b5c2da13472f1c1b9c69db02598674cdbd633be2","tests/version-number.rs":"73301b7bfe500eada5ede66f0dce89bd3e354af50a8e7a123b02931cd5eb8e16"},"package":"644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"}
{"files":{"Cargo.toml":"85a822957796d1aa30bd28d695fae3dc86ca8832d6879a8f71e323cc4a130174","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"f7bdb3426d045cd50efd4953026e3eb5a83d0199f458a075602611b9344da5b9","README.md":"ec487a539f41489876f5f2143e6f2071754ad96b1ca74092bdeab8135f6a819e","benches/hex.rs":"17f5081cc970fc5a0e7bf7e781da066ece95c4453b46ad8c129e665b19733885","src/error.rs":"489873510e45802b3b1858668daf46eb559cf0d0cebb41ca34cf5879dabe730e","src/lib.rs":"bc16c851822629225d2d2653ea080f131b0f2c4704d3ff27fed955c9b8b42f71","src/serde.rs":"d1297456aa4e9e11eb45ac41fb7b9d7d88b7de371a118e4c100bd78fc208ea86","tests/serde.rs":"567b0b58d9d1fac12b4ebb7bff6164bdef01b7c9edc59435a2d7507214c6481b","tests/version-number.rs":"913773b7a6b62c6fbea14f155571eb77c2ae8b0ac811dde8e028dee25a4155be"},"package":"7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"}

17
third_party/rust/hex/Cargo.toml поставляемый
Просмотреть файл

@ -13,7 +13,7 @@
[package]
edition = "2018"
name = "hex"
version = "0.4.2"
version = "0.4.3"
authors = ["KokaKiwi <kokakiwi@kokakiwi.net>"]
description = "Encoding and decoding data into/from hexadecimal representation."
documentation = "https://docs.rs/hex/"
@ -32,17 +32,18 @@ harness = false
[dependencies.serde]
version = "1.0"
optional = true
default-features = false
[dev-dependencies.criterion]
version = "0.3"
[dev-dependencies.faster-hex]
version = "0.4"
version = "0.5"
[dev-dependencies.pretty_assertions]
version = "0.6"
[dev-dependencies.rustc-hex]
version = "2.0"
version = "2.1"
[dev-dependencies.serde]
version = "1.0"
@ -52,11 +53,11 @@ features = ["derive"]
version = "1.0"
[dev-dependencies.version-sync]
version = "0.8"
version = "0.9"
[features]
alloc = []
default = ["std"]
std = []
[badges.travis-ci]
branch = "master"
repository = "KokaKiwi/rust-hex"
std = ["alloc"]
[badges.maintenance]
status = "actively-developed"

18
third_party/rust/hex/README.md поставляемый
Просмотреть файл

@ -1,8 +1,7 @@
hex
===
# hex
[![Crates.io: hex](https://img.shields.io/crates/v/hex.svg)](https://crates.io/crates/hex)
[![Documentation](https://docs.rs/hex/badge.svg)](https://docs.rs/hex)
[![Build Status (Travis)](https://travis-ci.org/KokaKiwi/rust-hex.svg?branch=master)](https://travis-ci.org/KokaKiwi/rust-hex)
[![Build Status (Github Actions)](https://github.com/KokaKiwi/rust-hex/workflows/Test%20hex/badge.svg?master)](https://github.com/KokaKiwi/rust-hex/actions)
Encoding and decoding data into/from hexadecimal representation.
@ -10,6 +9,7 @@ Encoding and decoding data into/from hexadecimal representation.
## Examples
Encoding a `String`
```rust
let hex_string = hex::encode("Hello world!");
@ -17,6 +17,7 @@ println!("{}", hex_string); // Prints "48656c6c6f20776f726c6421"
```
Decoding a `String`
```rust
let decoded_string = hex::decode("48656c6c6f20776f726c6421");
@ -28,6 +29,7 @@ You can find the [documentation](https://docs.rs/hex) here.
## Installation
In order to use this crate, you have to add it under `[dependencies]` to your `Cargo.toml`
```toml
[dependencies]
hex = "0.4"
@ -45,17 +47,17 @@ hex = { version = "0.4", default-features = false }
## Features
- `std`:
Enabled by default. Add support for Rust's libstd types.
Enabled by default. Add support for Rust's libstd types.
- `serde`:
Disabled by default. Add support for `serde` de/serializing library.
See the `serde` module documentation for usage.
Disabled by default. Add support for `serde` de/serializing library.
See the `serde` module documentation for usage.
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.

27
third_party/rust/hex/benches/hex.rs поставляемый
Просмотреть файл

@ -35,34 +35,25 @@ fn bench_decode(c: &mut Criterion) {
c.bench_function("faster_hex_decode", move |b| {
let hex = faster_hex::hex_string(DATA).unwrap();
let len = DATA.len();
b.iter(|| {
let mut dst = Vec::with_capacity(len);
dst.resize(len, 0);
faster_hex::hex_decode(hex.as_bytes(), &mut dst).unwrap();
dst
})
let mut dst = vec![0; len];
b.iter(|| faster_hex::hex_decode(hex.as_bytes(), &mut dst).unwrap())
});
c.bench_function("faster_hex_decode_unchecked", |b| {
let hex = faster_hex::hex_string(DATA).unwrap();
let len = DATA.len();
b.iter(|| {
let mut dst = Vec::with_capacity(len);
dst.resize(len, 0);
faster_hex::hex_decode_unchecked(hex.as_bytes(), &mut dst);
dst
})
let mut dst = vec![0; len];
b.iter(|| faster_hex::hex_decode_unchecked(hex.as_bytes(), &mut dst))
});
c.bench_function("faster_hex_decode_fallback", |b| {
let hex = faster_hex::hex_string(DATA).unwrap();
let len = DATA.len();
b.iter(|| {
let mut dst = Vec::with_capacity(len);
dst.resize(len, 0);
faster_hex::hex_decode_fallback(hex.as_bytes(), &mut dst);
dst
})
let mut dst = vec![0; len];
b.iter(|| faster_hex::hex_decode_fallback(hex.as_bytes(), &mut dst))
});
}

6
third_party/rust/hex/src/error.rs поставляемый
Просмотреть файл

@ -33,13 +33,17 @@ impl fmt::Display for FromHexError {
}
#[cfg(test)]
// this feature flag is here to suppress unused
// warnings of `super::*` and `pretty_assertions::assert_eq`
#[cfg(feature = "alloc")]
mod tests {
use super::*;
#[cfg(not(feature = "std"))]
#[cfg(feature = "alloc")]
use alloc::string::ToString;
use pretty_assertions::assert_eq;
#[test]
#[cfg(feature = "alloc")]
fn test_display() {
assert_eq!(
FromHexError::InvalidHexCharacter { c: '\n', index: 5 }.to_string(),

92
third_party/rust/hex/src/lib.rs поставляемый
Просмотреть файл

@ -15,6 +15,16 @@
//! # Example
//!
//! ```
//! # #[cfg(not(feature = "alloc"))]
//! # let mut output = [0; 0x18];
//! #
//! # #[cfg(not(feature = "alloc"))]
//! # hex::encode_to_slice(b"Hello world!", &mut output).unwrap();
//! #
//! # #[cfg(not(feature = "alloc"))]
//! # let hex_string = ::core::str::from_utf8(&output).unwrap();
//! #
//! # #[cfg(feature = "alloc")]
//! let hex_string = hex::encode("Hello world!");
//!
//! println!("{}", hex_string); // Prints "48656c6c6f20776f726c6421"
@ -22,14 +32,14 @@
//! # assert_eq!(hex_string, "48656c6c6f20776f726c6421");
//! ```
#![doc(html_root_url = "https://docs.rs/hex/0.4.2")]
#![doc(html_root_url = "https://docs.rs/hex/0.4.3")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::unreadable_literal)]
#[cfg(not(feature = "std"))]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(not(feature = "std"))]
#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};
use core::iter;
@ -41,7 +51,9 @@ pub use crate::error::FromHexError;
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub mod serde;
#[cfg(feature = "serde")]
pub use crate::serde::{deserialize, serialize, serialize_upper};
pub use crate::serde::deserialize;
#[cfg(all(feature = "alloc", feature = "serde"))]
pub use crate::serde::{serialize, serialize_upper};
/// Encoding values as hex string.
///
@ -59,11 +71,11 @@ pub use crate::serde::{deserialize, serialize, serialize_upper};
///
/// *Note*: instead of using this trait, you might want to use [`encode()`].
pub trait ToHex {
/// Encode the hex strict representing `self` into the result.. Lower case
/// Encode the hex strict representing `self` into the result. Lower case
/// letters are used (e.g. `f9b4ca`)
fn encode_hex<T: iter::FromIterator<char>>(&self) -> T;
/// Encode the hex strict representing `self` into the result.. Lower case
/// Encode the hex strict representing `self` into the result. Upper case
/// letters are used (e.g. `F9B4CA`)
fn encode_hex_upper<T: iter::FromIterator<char>>(&self) -> T;
}
@ -95,7 +107,7 @@ impl<'a> Iterator for BytesToHexChars<'a> {
Some(current) => Some(current),
None => self.inner.next().map(|byte| {
let current = self.table[(byte >> 4) as usize] as char;
self.next = Some(self.table[(byte & 0xf) as usize] as char);
self.next = Some(self.table[(byte & 0x0F) as usize] as char);
current
}),
}
@ -117,6 +129,7 @@ impl<'a> iter::ExactSizeIterator for BytesToHexChars<'a> {
}
}
#[inline]
fn encode_to_iter<T: iter::FromIterator<char>>(table: &'static [u8; 16], source: &[u8]) -> T {
BytesToHexChars::new(source, table).collect()
}
@ -138,18 +151,15 @@ impl<T: AsRef<[u8]>> ToHex for T {
/// # Example
///
/// ```
/// use core::str;
/// use hex::FromHex;
///
/// match Vec::from_hex("48656c6c6f20776f726c6421") {
/// Ok(vec) => {
/// for b in vec {
/// println!("{}", b as char);
/// }
/// }
/// Err(e) => {
/// // Deal with the error ...
/// }
/// }
/// let buffer = <[u8; 12]>::from_hex("48656c6c6f20776f726c6421")?;
/// let string = str::from_utf8(&buffer).expect("invalid buffer length");
///
/// println!("{}", string); // prints "Hello world!"
/// # assert_eq!("Hello world!", string);
/// # Ok::<(), hex::FromHexError>(())
/// ```
pub trait FromHex: Sized {
type Error;
@ -174,6 +184,7 @@ fn val(c: u8, idx: usize) -> Result<u8, FromHexError> {
}
}
#[cfg(feature = "alloc")]
impl FromHex for Vec<u8> {
type Error = FromHexError;
@ -198,7 +209,7 @@ macro_rules! from_hex_array_impl {
type Error = FromHexError;
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let mut out = [0u8; $len];
let mut out = [0_u8; $len];
decode_to_slice(hex, &mut out as &mut [u8])?;
Ok(out)
}
@ -243,6 +254,8 @@ from_hex_array_impl! {
/// assert_eq!(hex::encode("Hello world!"), "48656c6c6f20776f726c6421");
/// assert_eq!(hex::encode(vec![1, 2, 3, 15, 16]), "0102030f10");
/// ```
#[must_use]
#[cfg(feature = "alloc")]
pub fn encode<T: AsRef<[u8]>>(data: T) -> String {
data.encode_hex()
}
@ -257,6 +270,8 @@ pub fn encode<T: AsRef<[u8]>>(data: T) -> String {
/// assert_eq!(hex::encode_upper("Hello world!"), "48656C6C6F20776F726C6421");
/// assert_eq!(hex::encode_upper(vec![1, 2, 3, 15, 16]), "0102030F10");
/// ```
#[must_use]
#[cfg(feature = "alloc")]
pub fn encode_upper<T: AsRef<[u8]>>(data: T) -> String {
data.encode_hex_upper()
}
@ -277,6 +292,7 @@ pub fn encode_upper<T: AsRef<[u8]>>(data: T) -> String {
/// assert_eq!(hex::decode("123"), Err(hex::FromHexError::OddLength));
/// assert!(hex::decode("foo").is_err());
/// ```
#[cfg(feature = "alloc")]
pub fn decode<T: AsRef<[u8]>>(data: T) -> Result<Vec<u8>, FromHexError> {
FromHex::from_hex(data)
}
@ -316,11 +332,14 @@ pub fn decode_to_slice<T: AsRef<[u8]>>(data: T, out: &mut [u8]) -> Result<(), Fr
// (4, 5)
// (6, 7)
// ...
#[inline]
fn generate_iter(len: usize) -> impl Iterator<Item = (usize, usize)> {
(0..len).step_by(2).zip((0..len).skip(1).step_by(2))
}
// the inverse of `val`.
#[inline]
#[must_use]
fn byte2hex(byte: u8, table: &[u8; 16]) -> (u8, u8) {
let high = table[((byte & 0xf0) >> 4) as usize];
let low = table[(byte & 0x0f) as usize];
@ -350,7 +369,11 @@ pub fn encode_to_slice<T: AsRef<[u8]>>(input: T, output: &mut [u8]) -> Result<()
return Err(FromHexError::InvalidStringLength);
}
for (byte, (i, j)) in input.as_ref().iter().zip(generate_iter(input.as_ref().len() * 2)) {
for (byte, (i, j)) in input
.as_ref()
.iter()
.zip(generate_iter(input.as_ref().len() * 2))
{
let (high, low) = byte2hex(*byte, HEX_CHARS_LOWER);
output[i] = high;
output[j] = low;
@ -362,15 +385,14 @@ pub fn encode_to_slice<T: AsRef<[u8]>>(input: T, output: &mut [u8]) -> Result<()
#[cfg(test)]
mod test {
use super::*;
#[cfg(not(feature = "std"))]
#[cfg(feature = "alloc")]
use alloc::string::ToString;
use pretty_assertions::assert_eq;
#[test]
#[cfg(feature = "alloc")]
fn test_gen_iter() {
let mut result = Vec::new();
result.push((0, 1));
result.push((2, 3));
let result = vec![(0, 1), (2, 3)];
assert_eq!(generate_iter(5).collect::<Vec<_>>(), result);
}
@ -405,38 +427,53 @@ mod test {
let mut output_3 = [0; 4];
assert_eq!(decode_to_slice(b"6", &mut output_3), Err(FromHexError::OddLength));
assert_eq!(
decode_to_slice(b"6", &mut output_3),
Err(FromHexError::OddLength)
);
}
#[test]
#[cfg(feature = "alloc")]
fn test_encode() {
assert_eq!(encode("foobar"), "666f6f626172");
}
#[test]
#[cfg(feature = "alloc")]
fn test_decode() {
assert_eq!(decode("666f6f626172"), Ok(String::from("foobar").into_bytes()));
assert_eq!(
decode("666f6f626172"),
Ok(String::from("foobar").into_bytes())
);
}
#[test]
#[cfg(feature = "alloc")]
pub fn test_from_hex_okay_str() {
assert_eq!(Vec::from_hex("666f6f626172").unwrap(), b"foobar");
assert_eq!(Vec::from_hex("666F6F626172").unwrap(), b"foobar");
}
#[test]
#[cfg(feature = "alloc")]
pub fn test_from_hex_okay_bytes() {
assert_eq!(Vec::from_hex(b"666f6f626172").unwrap(), b"foobar");
assert_eq!(Vec::from_hex(b"666F6F626172").unwrap(), b"foobar");
}
#[test]
#[cfg(feature = "alloc")]
pub fn test_invalid_length() {
assert_eq!(Vec::from_hex("1").unwrap_err(), FromHexError::OddLength);
assert_eq!(Vec::from_hex("666f6f6261721").unwrap_err(), FromHexError::OddLength);
assert_eq!(
Vec::from_hex("666f6f6261721").unwrap_err(),
FromHexError::OddLength
);
}
#[test]
#[cfg(feature = "alloc")]
pub fn test_invalid_char() {
assert_eq!(
Vec::from_hex("66ag").unwrap_err(),
@ -445,11 +482,13 @@ mod test {
}
#[test]
#[cfg(feature = "alloc")]
pub fn test_empty() {
assert_eq!(Vec::from_hex("").unwrap(), b"");
}
#[test]
#[cfg(feature = "alloc")]
pub fn test_from_hex_whitespace() {
assert_eq!(
Vec::from_hex("666f 6f62617").unwrap_err(),
@ -471,6 +510,7 @@ mod test {
}
#[test]
#[cfg(feature = "alloc")]
fn test_to_hex() {
assert_eq!(
[0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72].encode_hex::<String>(),

54
third_party/rust/hex/src/serde.rs поставляемый
Просмотреть файл

@ -1,28 +1,40 @@
//! Hex encoding with `serde`.
//!
//! # Example
//!
//! ```
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize)]
//! struct Foo {
//! #[serde(with = "hex")]
//! bar: Vec<u8>,
//! }
//! ```
//!
#[cfg_attr(
all(feature = "alloc", feature = "serde"),
doc = r##"
# Example
```
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Foo {
#[serde(with = "hex")]
bar: Vec<u8>,
}
```
"##
)]
use serde::de::{Error, Visitor};
use serde::{Deserializer, Serializer};
use serde::Deserializer;
#[cfg(feature = "alloc")]
use serde::Serializer;
use std::fmt;
use std::marker::PhantomData;
#[cfg(feature = "alloc")]
use alloc::string::String;
use crate::{FromHex, ToHex};
use core::fmt;
use core::marker::PhantomData;
use crate::FromHex;
#[cfg(feature = "alloc")]
use crate::ToHex;
/// Serializes `data` as hex string using uppercase characters.
///
/// Apart from the characters' casing, this works exactly like `serialize()`.
#[cfg(feature = "alloc")]
pub fn serialize_upper<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -36,7 +48,9 @@ where
///
/// Lowercase characters are used (e.g. `f9b4ca`). The resulting string's length
/// is always even, each byte in data is always encoded using two hex digits.
/// Thus, the resulting string contains exactly twice as many bytes as the input data.
/// Thus, the resulting string contains exactly twice as many bytes as the input
/// data.
#[cfg(feature = "alloc")]
pub fn serialize<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -73,14 +87,14 @@ where
where
E: Error,
{
FromHex::from_hex(data).map_err(|e| Error::custom(e))
FromHex::from_hex(data).map_err(Error::custom)
}
fn visit_borrowed_str<E>(self, data: &'de str) -> Result<Self::Value, E>
where
E: Error,
{
FromHex::from_hex(data).map_err(|e| Error::custom(e))
FromHex::from_hex(data).map_err(Error::custom)
}
}

3
third_party/rust/hex/tests/serde.rs поставляемый
Просмотреть файл

@ -1,4 +1,5 @@
#![cfg(feature = "serde")]
#![cfg(all(feature = "serde", feature = "alloc"))]
#![allow(clippy::blacklisted_name)]
use serde::{Deserialize, Serialize};

Просмотреть файл

@ -1,3 +1,5 @@
#![allow(non_fmt_panic)]
#[test]
fn test_readme_deps() {
version_sync::assert_markdown_deps_updated!("README.md");