]> git.proxmox.com Git - cargo.git/blob - vendor/openssl-0.9.19/src/memcmp.rs
New upstream version 0.23.0
[cargo.git] / vendor / openssl-0.9.19 / src / memcmp.rs
1 use libc::size_t;
2 use ffi;
3
4 /// Returns `true` iff `a` and `b` contain the same bytes.
5 ///
6 /// This operation takes an amount of time dependent on the length of the two
7 /// arrays given, but is independent of the contents of a and b.
8 ///
9 /// # Panics
10 ///
11 /// This function will panic the current task if `a` and `b` do not have the same
12 /// length.
13 pub fn eq(a: &[u8], b: &[u8]) -> bool {
14 assert!(a.len() == b.len());
15 let ret = unsafe {
16 ffi::CRYPTO_memcmp(
17 a.as_ptr() as *const _,
18 b.as_ptr() as *const _,
19 a.len() as size_t,
20 )
21 };
22 ret == 0
23 }
24
25 #[cfg(test)]
26 mod tests {
27 use super::eq;
28
29 #[test]
30 fn test_eq() {
31 assert!(eq(&[], &[]));
32 assert!(eq(&[1], &[1]));
33 assert!(!eq(&[1, 2, 3], &[1, 2, 4]));
34 }
35
36 #[test]
37 #[should_panic]
38 fn test_diff_lens() {
39 eq(&[], &[1]);
40 }
41 }