Skip to content

Commit

Permalink
decode: allow to decode a zero length src
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Mar 2, 2023
1 parent 18d5fbf commit 80a22b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
35 changes: 26 additions & 9 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn hex_decode(src: &[u8], dst: &mut [u8]) -> Result<(), Error> {
}

/// Hex decode src into dst.
/// The length of src must be even and not zero.
/// 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.
/// 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.
Expand All @@ -227,10 +227,6 @@ pub fn hex_decode_with_case(
dst: &mut [u8],
check_case: CheckCase,
) -> Result<(), Error> {
if src.is_empty() {
return Err(Error::InvalidLength(0));
}

if src.len() & 1 != 0 {
return Err(Error::InvalidLength(src.len()));
}
Expand Down Expand Up @@ -448,12 +444,13 @@ mod tests {

#[cfg(all(test, any(target_arch = "x86", target_arch = "x86_64")))]
mod test_sse {
use crate::decode::hex_check_sse;
use crate::decode::CheckCase;
use crate::decode::{
hex_check, hex_check_fallback, hex_check_fallback_with_case, hex_check_sse,
hex_check_sse_with_case, hex_check_with_case, hex_decode, hex_decode_unchecked,
hex_decode_with_case, CheckCase,
};
use proptest::proptest;

use super::hex_check_sse_with_case;

fn _test_check_sse_with_case(s: &String, check_case: CheckCase, expect_result: bool) {
if is_x86_feature_detected!("sse4.1") {
assert_eq!(
Expand Down Expand Up @@ -510,4 +507,24 @@ mod test_sse {
_test_check_sse_with_case(s, CheckCase::Upper, false);
}
}

#[test]
fn test_decode_zero_length_src_should_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!(hex_check(src));
assert!(hex_check_with_case(src, CheckCase::None));
assert!(hex_check_fallback(src));
assert!(hex_check_fallback_with_case(src, CheckCase::None));

if is_x86_feature_detected!("sse4.1") {
assert!(unsafe { hex_check_sse_with_case(src, CheckCase::None) });
assert!(unsafe { hex_check_sse(src) });
}

// this function have no return value, so we just execute it and expect no panic
hex_decode_unchecked(src, &mut dst);
}
}
14 changes: 13 additions & 1 deletion src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ pub fn hex_encode_upper_fallback(src: &[u8], dst: &mut [u8]) {

#[cfg(test)]
mod tests {
use crate::encode::hex_encode_custom_case_fallback;
use crate::encode::{hex_encode, hex_encode_custom_case_fallback};

use crate::hex_encode_fallback;
use core::str;
use proptest::proptest;

Expand All @@ -239,4 +241,14 @@ mod tests {
_test_encode_fallback(s, false);
}
}

#[test]
fn test_encode_zero_length_src_should_be_ok() {
let src = b"";
let mut dst = [0u8; 10];
assert!(hex_encode(src, &mut dst).is_ok());

// this function have no return value, so we just execute it and expect no panic
hex_encode_fallback(src, &mut dst);
}
}

0 comments on commit 80a22b3

Please sign in to comment.