summaryrefslogtreecommitdiffstats
path: root/vendor/faster-hex
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/faster-hex
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/faster-hex')
-rw-r--r--vendor/faster-hex/.cargo-checksum.json2
-rw-r--r--vendor/faster-hex/Cargo.toml2
-rw-r--r--vendor/faster-hex/src/decode.rs67
-rw-r--r--vendor/faster-hex/src/error.rs3
-rw-r--r--vendor/faster-hex/src/lib.rs47
5 files changed, 91 insertions, 30 deletions
diff --git a/vendor/faster-hex/.cargo-checksum.json b/vendor/faster-hex/.cargo-checksum.json
index c1efdc840..8abeaef60 100644
--- a/vendor/faster-hex/.cargo-checksum.json
+++ b/vendor/faster-hex/.cargo-checksum.json
@@ -1 +1 @@
-{"files":{"Cargo.toml":"393bee41f638fa483744cdf8efcaa0d70df5c9917ace40db6d3164411e41fead","LICENSE":"7e4baa202160a035c769bb83bd994222c74f479ec582624e3aedaa8f553481c7","LICENSE-THIRD-PARTY/Rust Project Developers":"29662666b44dff84977b46e05642cdef910bc3a93a17b5fd86e632bafa59cf21","LICENSE-THIRD-PARTY/fast-hex":"7c652d12760975f519e979b099e40f9c0868c1139b231e780cea69b2a3fd6656","README.md":"14bf30faaad09c315642b1913b9ebd9f4ae740bf76773b249e411b45379a6917","src/decode.rs":"93bb5d2a734510aae223f67098abc76ea236496d7e898ede076ada8d40a21e24","src/encode.rs":"0574ec4c39984565472b2e01b1978b6e5472d3459a6917e8723c510bd509a0d7","src/error.rs":"2b3fca5f4ea575a87c81053029f01bac4a334abc3b6f0b84dd210f4a933af894","src/lib.rs":"6b72b5816aaff727bdca4370b11297b54e970d5b18c1ed0e914ca7d6d53beb18","src/serde.rs":"46e123d026d9827ea89cd45825943abda617f18294a8490228d1a628ec750e40"},"package":"239f7bfb930f820ab16a9cd95afc26f88264cf6905c960b340a615384aa3338a"} \ No newline at end of file
+{"files":{"Cargo.toml":"068ac3e1822ee296f629cf38e8d3438f1567f52e2c4ed4a05a89f441fc18e25c","LICENSE":"7e4baa202160a035c769bb83bd994222c74f479ec582624e3aedaa8f553481c7","LICENSE-THIRD-PARTY/Rust Project Developers":"29662666b44dff84977b46e05642cdef910bc3a93a17b5fd86e632bafa59cf21","LICENSE-THIRD-PARTY/fast-hex":"7c652d12760975f519e979b099e40f9c0868c1139b231e780cea69b2a3fd6656","README.md":"14bf30faaad09c315642b1913b9ebd9f4ae740bf76773b249e411b45379a6917","src/decode.rs":"f2f07b1e73b12d8d521ab4455627c844d0d0067f58409077bdc2e292509161dc","src/encode.rs":"0574ec4c39984565472b2e01b1978b6e5472d3459a6917e8723c510bd509a0d7","src/error.rs":"9985f839336831f83acbaae459e17aea95f19ada0c1f3929bf00f53c56908486","src/lib.rs":"457fcaaf74965508a58bf955c7b4676f968f4077f52a4a722fc1ee074e83298a","src/serde.rs":"46e123d026d9827ea89cd45825943abda617f18294a8490228d1a628ec750e40"},"package":"a2a2b11eda1d40935b26cf18f6833c526845ae8c41e58d09af6adeb6f0269183"} \ No newline at end of file
diff --git a/vendor/faster-hex/Cargo.toml b/vendor/faster-hex/Cargo.toml
index 57618bc2d..4fb2f9fc5 100644
--- a/vendor/faster-hex/Cargo.toml
+++ b/vendor/faster-hex/Cargo.toml
@@ -12,7 +12,7 @@
[package]
edition = "2018"
name = "faster-hex"
-version = "0.8.1"
+version = "0.9.0"
authors = ["zhangsoledad <787953403@qq.com>"]
exclude = [
"afl/*",
diff --git a/vendor/faster-hex/src/decode.rs b/vendor/faster-hex/src/decode.rs
index 5b7b440eb..be452f58a 100644
--- a/vendor/faster-hex/src/decode.rs
+++ b/vendor/faster-hex/src/decode.rs
@@ -210,15 +210,15 @@ pub unsafe fn hex_check_sse_with_case(mut src: &[u8], check_case: CheckCase) ->
}
/// Hex decode src into dst.
-/// The length of src must be even and not zero.
-/// The length of dst must be at least src.len() / 2.
+/// The length of src must be even, and it's allowed to decode a zero length src.
+/// The length of dst must be src.len() / 2.
pub fn hex_decode(src: &[u8], dst: &mut [u8]) -> Result<(), Error> {
hex_decode_with_case(src, dst, CheckCase::None)
}
/// Hex decode src into dst.
/// The length of src must be even, and it's allowed to decode a zero length src.
-/// The length of dst must be at least src.len() / 2.
+/// The length of dst must be src.len() / 2.
/// when check_case is CheckCase::Lower, the hex string must be lower case.
/// when check_case is CheckCase::Upper, the hex string must be upper case.
/// when check_case is CheckCase::None, the hex string can be lower case or upper case.
@@ -227,15 +227,11 @@ pub fn hex_decode_with_case(
dst: &mut [u8],
check_case: CheckCase,
) -> Result<(), Error> {
- if src.len() & 1 != 0 {
- return Err(Error::InvalidLength(src.len()));
+ let len = dst.len().checked_mul(2).ok_or(Error::Overflow)?;
+ if src.len() < len || ((src.len() & 1) != 0) {
+ return Err(Error::InvalidLength(len));
}
- let expect_dst_len = src.len().checked_div(2).unwrap();
-
- if dst.len() < expect_dst_len {
- return Err(Error::InvalidLength(dst.len()));
- }
if !hex_check_with_case(src, check_case) {
return Err(Error::InvalidChar);
}
@@ -509,11 +505,15 @@ mod test_sse {
}
#[test]
- fn test_decode_zero_length_src_should_be_ok() {
+ fn test_decode_zero_length_src_should_not_be_ok() {
let src = b"";
let mut dst = [0u8; 10];
- assert!(hex_decode(src, &mut dst).is_ok());
- assert!(hex_decode_with_case(src, &mut dst, CheckCase::None).is_ok());
+ assert!(
+ matches!(hex_decode(src, &mut dst), Err(crate::Error::InvalidLength(len)) if len == 20)
+ );
+ assert!(
+ matches!(hex_decode_with_case(src, &mut dst, CheckCase::None), Err(crate::Error::InvalidLength(len)) if len == 20)
+ );
assert!(hex_check(src));
assert!(hex_check_with_case(src, CheckCase::None));
assert!(hex_check_fallback(src));
@@ -527,4 +527,45 @@ mod test_sse {
// this function have no return value, so we just execute it and expect no panic
hex_decode_unchecked(src, &mut dst);
}
+
+ // If `dst's length` is greater than `src's length * 2`, `hex_decode` should return error
+ #[test]
+ fn test_if_dst_len_gt_expect_len_should_return_error() {
+ let short_str = b"8e40af02265360d59f4ecf9ae9ebf8f00a3118408f5a9cdcbcc9c0f93642f3"; // 62 bytes
+ {
+ let mut dst = [0u8; 31];
+ let result = hex_decode(short_str.as_slice(), &mut dst);
+ assert!(result.is_ok());
+ }
+
+ {
+ let mut dst = [0u8; 32];
+ let result = hex_decode(short_str.as_slice(), &mut dst);
+ assert!(matches!(result, Err(crate::Error::InvalidLength(len)) if len == 64))
+ }
+
+ {
+ let mut dst = [0u8; 33];
+ let result = hex_decode(short_str.as_slice(), &mut dst);
+ assert!(matches!(result, Err(crate::Error::InvalidLength(len)) if len == 66))
+ }
+ }
+
+ // if both `src` and `dst` are empty, it's ok
+ // if `src` is empty, but `dst` is not empty, it should be reported as error
+ #[test]
+ fn test_decode_zero_src() {
+ let zero_src = b"";
+ {
+ let mut zero_dst = [];
+ assert!(hex_decode(zero_src, &mut zero_dst).is_ok());
+ }
+
+ {
+ let mut non_zero_dst = [0u8; 1];
+ assert!(
+ matches!(hex_decode(zero_src, &mut non_zero_dst), Err(crate::Error::InvalidLength(len)) if len == 2)
+ );
+ }
+ }
}
diff --git a/vendor/faster-hex/src/error.rs b/vendor/faster-hex/src/error.rs
index d6cf5f249..5cffac394 100644
--- a/vendor/faster-hex/src/error.rs
+++ b/vendor/faster-hex/src/error.rs
@@ -2,6 +2,7 @@
pub enum Error {
InvalidChar,
InvalidLength(usize),
+ Overflow,
}
impl ::core::fmt::Debug for Error {
@@ -9,6 +10,7 @@ impl ::core::fmt::Debug for Error {
match *self {
Error::InvalidLength(len) => write!(f, "Invalid input length {len}"),
Error::InvalidChar => write!(f, "Invalid character"),
+ Error::Overflow => write!(f, "Overflow"),
}
}
}
@@ -25,6 +27,7 @@ impl ::std::error::Error for Error {
match *self {
Error::InvalidChar => "invalid character",
Error::InvalidLength(_) => "invalid length",
+ Error::Overflow => "overflow",
}
}
}
diff --git a/vendor/faster-hex/src/lib.rs b/vendor/faster-hex/src/lib.rs
index 86a357714..07820175e 100644
--- a/vendor/faster-hex/src/lib.rs
+++ b/vendor/faster-hex/src/lib.rs
@@ -60,7 +60,7 @@ pub(crate) fn vectorization_support() -> Vectorization {
};
}
- let val = unsafe { vectorization_support_no_cache_x86() };
+ let val = vectorization_support_no_cache_x86();
FLAGS.store(val as u8, Ordering::Relaxed);
return val;
@@ -69,15 +69,13 @@ pub(crate) fn vectorization_support() -> Vectorization {
Vectorization::None
}
-// We enable xsave so it can inline the _xgetbv call.
-#[target_feature(enable = "xsave")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cold]
-unsafe fn vectorization_support_no_cache_x86() -> Vectorization {
+fn vectorization_support_no_cache_x86() -> Vectorization {
#[cfg(target_arch = "x86")]
- use core::arch::x86::{__cpuid_count, _xgetbv};
+ use core::arch::x86::__cpuid_count;
#[cfg(target_arch = "x86_64")]
- use core::arch::x86_64::{__cpuid_count, _xgetbv};
+ use core::arch::x86_64::__cpuid_count;
// SGX doesn't support CPUID,
// If there's no SSE there might not be CPUID and there's no SSE4.1/AVX2
@@ -85,29 +83,48 @@ unsafe fn vectorization_support_no_cache_x86() -> Vectorization {
return Vectorization::None;
}
- let proc_info_ecx = __cpuid_count(1, 0).ecx;
+ let proc_info_ecx = unsafe { __cpuid_count(1, 0) }.ecx;
let have_sse4 = (proc_info_ecx >> 19) & 1 == 1;
// If there's no SSE4 there can't be AVX2.
if !have_sse4 {
return Vectorization::None;
}
+
let have_xsave = (proc_info_ecx >> 26) & 1 == 1;
let have_osxsave = (proc_info_ecx >> 27) & 1 == 1;
let have_avx = (proc_info_ecx >> 27) & 1 == 1;
if have_xsave && have_osxsave && have_avx {
- let xcr0 = _xgetbv(0);
- let os_avx_support = xcr0 & 6 == 6;
- if os_avx_support {
- let extended_features_ebx = __cpuid_count(7, 0).ebx;
- let have_avx2 = (extended_features_ebx >> 5) & 1 == 1;
- if have_avx2 {
- return Vectorization::AVX2;
- }
+ // # Safety: We checked that the processor supports xsave
+ if unsafe { avx2_support_no_cache_x86() } {
+ return Vectorization::AVX2;
}
}
Vectorization::SSE41
}
+// We enable xsave so it can inline the _xgetbv call.
+// # Safety: Safe as long it's only called when xsave is supported
+#[target_feature(enable = "xsave")]
+#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
+#[cold]
+unsafe fn avx2_support_no_cache_x86() -> bool {
+ #[cfg(target_arch = "x86")]
+ use core::arch::x86::{__cpuid_count, _xgetbv};
+ #[cfg(target_arch = "x86_64")]
+ use core::arch::x86_64::{__cpuid_count, _xgetbv};
+
+ let xcr0 = _xgetbv(0);
+ let os_avx_support = xcr0 & 6 == 6;
+ if os_avx_support {
+ let extended_features_ebx = __cpuid_count(7, 0).ebx;
+ let have_avx2 = (extended_features_ebx >> 5) & 1 == 1;
+ if have_avx2 {
+ return true;
+ }
+ }
+ false
+}
+
#[cfg(test)]
mod tests {
use crate::decode::{hex_decode, hex_decode_with_case, CheckCase};