Browse Source

Extract common &CStr -> &str code and reduce allowed hamming distance

master
Drew Short 5 years ago
parent
commit
f196c3a722
  1. 78
      src/lib.rs

78
src/lib.rs

@ -125,17 +125,7 @@ pub extern "C" fn ext_free(raw_lib: *const libc::c_void) {
pub extern "C" fn ext_get_ahash(lib: &PIHash, path_char: *const libc::c_char) -> u64 { pub extern "C" fn ext_get_ahash(lib: &PIHash, path_char: *const libc::c_char) -> u64 {
unsafe { unsafe {
let path_str = CStr::from_ptr(path_char); let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => {
println!(
"Error: {}. Unable to parse '{}'",
e,
to_hex_string(path_str.to_bytes())
);
panic!("Unable to parse path")
}
};
let image_path = get_str_from_cstr(path_str);
let path = Path::new(&image_path); let path = Path::new(&image_path);
lib.get_ahash(path) lib.get_ahash(path)
} }
@ -145,17 +135,7 @@ pub extern "C" fn ext_get_ahash(lib: &PIHash, path_char: *const libc::c_char) ->
pub extern "C" fn ext_get_dhash(lib: &PIHash, path_char: *const libc::c_char) -> u64 { pub extern "C" fn ext_get_dhash(lib: &PIHash, path_char: *const libc::c_char) -> u64 {
unsafe { unsafe {
let path_str = CStr::from_ptr(path_char); let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => {
println!(
"Error: {}. Unable to parse '{}'",
e,
to_hex_string(path_str.to_bytes())
);
panic!("Unable to parse path")
}
};
let image_path = get_str_from_cstr(path_str);
let path = Path::new(&image_path); let path = Path::new(&image_path);
lib.get_dhash(path) lib.get_dhash(path)
} }
@ -165,17 +145,7 @@ pub extern "C" fn ext_get_dhash(lib: &PIHash, path_char: *const libc::c_char) ->
pub extern "C" fn ext_get_phash(lib: &PIHash, path_char: *const libc::c_char) -> u64 { pub extern "C" fn ext_get_phash(lib: &PIHash, path_char: *const libc::c_char) -> u64 {
unsafe { unsafe {
let path_str = CStr::from_ptr(path_char); let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => {
println!(
"Error: {}. Unable to parse '{}'",
e,
to_hex_string(path_str.to_bytes())
);
panic!("Unable to parse path")
}
};
let image_path = get_str_from_cstr(path_str);
let path = Path::new(&image_path); let path = Path::new(&image_path);
lib.get_phash(path) lib.get_phash(path)
} }
@ -192,17 +162,7 @@ pub struct PIHashes {
pub extern "C" fn ext_get_phashes(lib: &PIHash, path_char: *const libc::c_char) -> *mut PIHashes { pub extern "C" fn ext_get_phashes(lib: &PIHash, path_char: *const libc::c_char) -> *mut PIHashes {
unsafe { unsafe {
let path_str = CStr::from_ptr(path_char); let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => {
println!(
"Error: {}. Unable to parse '{}'",
e,
to_hex_string(path_str.to_bytes())
);
panic!("Unable to parse path")
}
};
let image_path = get_str_from_cstr(path_str);
let path = Path::new(&image_path); let path = Path::new(&image_path);
let phashes = lib.get_phashes(path); let phashes = lib.get_phashes(path);
Box::into_raw(Box::new(PIHashes { Box::into_raw(Box::new(PIHashes {
@ -220,6 +180,20 @@ pub extern "C" fn ext_free_phashes(raw_phashes: *const libc::c_void) {
} }
} }
fn get_str_from_cstr(path_str: &CStr) -> &str {
match path_str.to_str() {
Ok(result) => result,
Err(e) => {
println!(
"Error: {}. Unable to parse '{}'",
e,
to_hex_string(path_str.to_bytes())
);
panic!("Unable to parse path")
}
}
}
fn to_hex_string(bytes: &[u8]) -> String { fn to_hex_string(bytes: &[u8]) -> String {
println!("length: {}", bytes.len()); println!("length: {}", bytes.len());
let mut strs: Vec<String> = Vec::new(); let mut strs: Vec<String> = Vec::new();
@ -292,9 +266,9 @@ mod tests {
image_hashes[index], image_hashes[index],
calculated_hash calculated_hash
); );
assert_eq!(calculated_hash, image_hashes[index]);
hashes[index] = calculated_hash; hashes[index] = calculated_hash;
} }
assert_eq!(hashes, image_hashes);
for index in 0..hashes.len() { for index in 0..hashes.len() {
for index2 in 0..hashes.len() { for index2 in 0..hashes.len() {
@ -328,7 +302,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::AHash, hash::HashType::AHash,
hash::Precision::Medium, hash::Precision::Medium,
1u64,
0u64,
sample_01_images, sample_01_images,
sample_01_hashes, sample_01_hashes,
&lib, &lib,
@ -348,7 +322,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::AHash, hash::HashType::AHash,
hash::Precision::Medium, hash::Precision::Medium,
3u64,
0u64,
sample_02_images, sample_02_images,
sample_02_hashes, sample_02_hashes,
&lib, &lib,
@ -365,7 +339,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::AHash, hash::HashType::AHash,
hash::Precision::Medium, hash::Precision::Medium,
1u64,
0u64,
sample_03_images, sample_03_images,
sample_03_hashes, sample_03_hashes,
&lib, &lib,
@ -471,7 +445,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::DHash, hash::HashType::DHash,
hash::Precision::Medium, hash::Precision::Medium,
1u64,
0u64,
sample_04_images, sample_04_images,
sample_04_hashes, sample_04_hashes,
&lib, &lib,
@ -496,7 +470,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::PHash, hash::HashType::PHash,
hash::Precision::Medium, hash::Precision::Medium,
1u64,
0u64,
sample_01_images, sample_01_images,
sample_01_hashes, sample_01_hashes,
&lib, &lib,
@ -516,7 +490,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::PHash, hash::HashType::PHash,
hash::Precision::Medium, hash::Precision::Medium,
1u64,
0u64,
sample_02_images, sample_02_images,
sample_02_hashes, sample_02_hashes,
&lib, &lib,
@ -556,7 +530,7 @@ mod tests {
test_imageset_hash( test_imageset_hash(
hash::HashType::PHash, hash::HashType::PHash,
hash::Precision::Medium, hash::Precision::Medium,
3u64,
0u64,
sample_04_images, sample_04_images,
sample_04_hashes, sample_04_hashes,
&lib, &lib,

Loading…
Cancel
Save