diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs index fd2a3b91187265df9ff6bf6d2691ef9edd0f05cc..8e8169be1a9af9f51a3a28a8cda9a2a6eb9fae8a 100644 --- a/src/header/string/mod.rs +++ b/src/header/string/mod.rs @@ -87,7 +87,16 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: usize) #[no_mangle] pub unsafe extern "C" fn memcpy(s1: *mut c_void, s2: *const c_void, n: usize) -> *mut c_void { - platform::memcpy(s1, s2, n) + let mut i = 0; + while i + 7 < n { + *(s1.offset(i as isize) as *mut u64) = *(s2.offset(i as isize) as *const u64); + i += 8; + } + while i < n { + *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + i += 1; + } + s1 } #[no_mangle] diff --git a/src/header/strings/mod.rs b/src/header/strings/mod.rs index 6965abf03ebaf362c95019efb4d8791b9074801e..66661ce043ebc239ee9ebc4ce0b14b56073f0a6e 100644 --- a/src/header/strings/mod.rs +++ b/src/header/strings/mod.rs @@ -20,38 +20,20 @@ pub unsafe extern "C" fn bcmp(first: *const c_void, second: *const c_void, n: si #[no_mangle] pub unsafe extern "C" fn bcopy(src: *const c_void, dst: *mut c_void, n: size_t) { - let src = src as *mut c_char; - let dst = dst as *mut c_char; - - let mut tmp = Vec::with_capacity(n); - for i in 0..n as isize { - tmp.push(*src.offset(i)); - } - for (i, val) in tmp.into_iter().enumerate() { - *dst.offset(i as isize) = val; - } + ptr::copy(src as *const u8, dst as *mut u8, n); } #[no_mangle] -pub unsafe extern "C" fn bzero(src: *mut c_void, n: size_t) { - let src = src as *mut c_char; - - for i in 0..n as isize { - *src.offset(i) = 0; - } +pub unsafe extern "C" fn bzero(dst: *mut c_void, n: size_t) { + ptr::write_bytes(dst as *mut u8, 0, n); } #[no_mangle] -pub extern "C" fn ffs(mut i: c_int) -> c_int { +pub extern "C" fn ffs(i: c_int) -> c_int { if i == 0 { return 0; } - let mut n = 1; - while i & 1 == 0 { - i >>= 1; - n += 1; - } - n + 1 + i.trailing_zeros() as c_int } #[no_mangle] @@ -89,14 +71,7 @@ pub unsafe extern "C" fn strcasecmp(mut first: *const c_char, mut second: *const let mut i = *first; let mut j = *second; - if i >= b'A' as c_char && i <= b'Z' as c_char { - i += (b'a' - b'A') as c_char; - } - if j >= b'A' as c_char && j <= b'Z' as c_char { - j += (b'a' - b'A') as c_char; - } - - if i != j { + if i & !32 != j & !32 { return -1; } @@ -120,14 +95,7 @@ pub unsafe extern "C" fn strncasecmp( let mut i = *first; let mut j = *second; - if i >= b'A' as c_char && i <= b'Z' as c_char { - i += (b'a' - b'A') as c_char; - } - if j >= b'A' as c_char && j <= b'Z' as c_char { - j += (b'a' - b'A') as c_char; - } - - if i != j { + if i & !32 != j & !32 { return -1; } @@ -136,7 +104,7 @@ pub unsafe extern "C" fn strncasecmp( n -= 1; } // Both strings didn't end with NUL bytes (unless we reached the limit) - if n != 0 && *first != *second { + if n > 0 && *first != *second { return -1; } 0 diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 8d90b31b1cd31e793966478a2e2bbbbecf127164..0d661e83f9b3e584930d9ff379b643d219109e8e 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -43,20 +43,6 @@ pub static mut environ: *mut *mut c_char = ptr::null_mut(); #[allow(non_upper_case_globals)] pub static mut inner_environ: Vec<*mut c_char> = Vec::new(); -// NOTE: defined here rather than in string because memcpy() is useful in multiple crates -pub unsafe fn memcpy(s1: *mut c_void, s2: *const c_void, n: usize) -> *mut c_void { - let mut i = 0; - while i + 7 < n { - *(s1.offset(i as isize) as *mut u64) = *(s2.offset(i as isize) as *const u64); - i += 8; - } - while i < n { - *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); - i += 1; - } - s1 -} - pub trait WriteByte: fmt::Write { fn write_u8(&mut self, byte: u8) -> fmt::Result; } @@ -114,10 +100,10 @@ impl StringWriter { pub unsafe fn write(&mut self, buf: &[u8]) { if self.1 > 1 { let copy_size = buf.len().min(self.1 - 1); - memcpy( - self.0 as *mut c_void, - buf.as_ptr() as *const c_void, - copy_size, + ptr::copy_nonoverlapping( + buf.as_ptr(), + self.0, + copy_size ); self.1 -= copy_size; @@ -145,10 +131,10 @@ pub struct UnsafeStringWriter(pub *mut u8); impl UnsafeStringWriter { pub unsafe fn write(&mut self, buf: &[u8]) { - memcpy( - self.0 as *mut c_void, - buf.as_ptr() as *const c_void, - buf.len(), + ptr::copy_nonoverlapping( + buf.as_ptr(), + self.0, + buf.len() ); *self.0.offset(buf.len() as isize) = b'\0'; self.0 = self.0.offset(buf.len() as isize);