1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use libc::{c_int, c_void, size_t};
#[no_mangle]
pub extern "C" fn memx_init() {
}
#[no_mangle]
pub extern "C" fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void {
let buf = unsafe { std::slice::from_raw_parts(cx as *const u8, n) };
let c = c as u8;
match ::memx::memchr(buf, c) {
Some(pos) => unsafe { buf.as_ptr().add(pos) as *mut c_void },
None => 0 as *mut c_void,
}
}
#[no_mangle]
pub extern "C" fn memrchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void {
let buf = unsafe { std::slice::from_raw_parts(cx as *const u8, n) };
let c = c as u8;
match ::memx::memrchr(buf, c) {
Some(pos) => unsafe { buf.as_ptr().add(pos) as *mut c_void },
None => 0 as *mut c_void,
}
}
use core::cmp::Ordering;
#[no_mangle]
pub extern "C" fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int {
let a = unsafe { std::slice::from_raw_parts(cx as *const u8, n) };
let b = unsafe { std::slice::from_raw_parts(ct as *const u8, n) };
match ::memx::memcmp(a, b) {
Ordering::Equal => 0 as c_int,
Ordering::Less => -1 as c_int,
Ordering::Greater => 1 as c_int,
}
}
#[no_mangle]
pub extern "C" fn bcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int {
let a = unsafe { std::slice::from_raw_parts(cx as *const u8, n) };
let b = unsafe { std::slice::from_raw_parts(ct as *const u8, n) };
if ::memx::memeq(a, b) {
0 as c_int
} else {
-1 as c_int
}
}
#[no_mangle]
pub extern "C" fn memmem(
haystack: *const c_void,
haystacklen: size_t,
needle: *const c_void,
needlelen: size_t,
) -> *mut c_void {
let haystack = unsafe { std::slice::from_raw_parts(haystack as *const u8, haystacklen) };
let needle = unsafe { std::slice::from_raw_parts(needle as *const u8, needlelen) };
match ::memx::memmem(haystack, needle) {
Some(pos) => unsafe { haystack.as_ptr().add(pos) as *mut c_void },
None => 0 as *mut c_void,
}
}
#[cfg(not(target_env = "musl"))]
#[no_mangle]
pub extern "C" fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void {
let a = unsafe { std::slice::from_raw_parts_mut(dest as *mut u8, n) };
let b = unsafe { std::slice::from_raw_parts(src as *const u8, n) };
let _r = ::memx::memcpy(a, b);
dest
}
#[no_mangle]
pub extern "C" fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void {
let buf = unsafe { std::slice::from_raw_parts_mut(dest as *mut u8, n) };
let c = c as u8;
let _r = ::memx::memset(buf, c);
dest
}