]> git.ipfire.org Git - people/ms/suricata.git/blame - src/util-memcmp.c
core: Remove unneeded consts
[people/ms/suricata.git] / src / util-memcmp.c
CommitLineData
1859ed54
VJ
1/* Copyright (C) 2007-2010 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Victor Julien <victor@inliniac.net>
22 *
23 * Memcmp implementations.
24 */
25
26#include "suricata-common.h"
27
28#include "util-memcmp.h"
29#include "util-unittest.h"
30
31/* code is implemented in util-memcmp.h as it's all inlined */
32
33/* UNITTESTS */
34#ifdef UNITTESTS
35
8f1d7503
KS
36static int MemcmpTest01 (void)
37{
1859ed54
VJ
38 uint8_t a[] = "abcd";
39 uint8_t b[] = "abcd";
40
41 if (SCMemcmp(a, b, sizeof(a)-1) != 0)
42 return 0;
43
44 return 1;
45}
46
8f1d7503
KS
47static int MemcmpTest02 (void)
48{
1859ed54
VJ
49 uint8_t a[] = "abcdabcdabcdabcd";
50 uint8_t b[] = "abcdabcdabcdabcd";
51
52 if (SCMemcmp(a, b, sizeof(a)-1) != 0)
53 return 0;
54
55 return 1;
56}
57
8f1d7503
KS
58static int MemcmpTest03 (void)
59{
1859ed54
VJ
60 uint8_t a[] = "abcdabcd";
61 uint8_t b[] = "abcdabcd";
62
63 if (SCMemcmp(a, b, sizeof(a)-1) != 0)
64 return 0;
65
66 return 1;
67}
68
8f1d7503
KS
69static int MemcmpTest04 (void)
70{
1859ed54
VJ
71 uint8_t a[] = "abcd";
72 uint8_t b[] = "abcD";
73
091f53ce
VJ
74 int r = SCMemcmp(a, b, sizeof(a)-1);
75 if (r != 1) {
76 printf("%s != %s, but memcmp returned %d: ", a, b, r);
1859ed54 77 return 0;
091f53ce 78 }
1859ed54
VJ
79
80 return 1;
81}
82
8f1d7503
KS
83static int MemcmpTest05 (void)
84{
1859ed54
VJ
85 uint8_t a[] = "abcdabcdabcdabcd";
86 uint8_t b[] = "abcDabcdabcdabcd";
87
88 if (SCMemcmp(a, b, sizeof(a)-1) != 1)
89 return 0;
90
91 return 1;
92}
93
8f1d7503
KS
94static int MemcmpTest06 (void)
95{
1859ed54
VJ
96 uint8_t a[] = "abcdabcd";
97 uint8_t b[] = "abcDabcd";
98
99 if (SCMemcmp(a, b, sizeof(a)-1) != 1)
100 return 0;
101
102 return 1;
103}
104
8f1d7503
KS
105static int MemcmpTest07 (void)
106{
1859ed54
VJ
107 uint8_t a[] = "abcd";
108 uint8_t b[] = "abcde";
109
110 if (SCMemcmp(a, b, sizeof(a)-1) != 0)
111 return 0;
112
113 return 1;
114}
115
8f1d7503
KS
116static int MemcmpTest08 (void)
117{
1859ed54
VJ
118 uint8_t a[] = "abcdabcdabcdabcd";
119 uint8_t b[] = "abcdabcdabcdabcde";
120
121 if (SCMemcmp(a, b, sizeof(a)-1) != 0)
122 return 0;
123
124 return 1;
125}
126
8f1d7503
KS
127static int MemcmpTest09 (void)
128{
1859ed54
VJ
129 uint8_t a[] = "abcdabcd";
130 uint8_t b[] = "abcdabcde";
131
132 if (SCMemcmp(a, b, sizeof(a)-1) != 0)
133 return 0;
134
135 return 1;
136}
137
8f1d7503
KS
138static int MemcmpTest10 (void)
139{
1859ed54
VJ
140 uint8_t a[] = "abcd";
141 uint8_t b[] = "Zbcde";
142
143 if (SCMemcmp(a, b, sizeof(a)-1) != 1)
144 return 0;
145
146 return 1;
147}
148
8f1d7503
KS
149static int MemcmpTest11 (void)
150{
1859ed54
VJ
151 uint8_t a[] = "abcdabcdabcdabcd";
152 uint8_t b[] = "Zbcdabcdabcdabcde";
153
154 if (SCMemcmp(a, b, sizeof(a)-1) != 1)
155 return 0;
156
157 return 1;
158}
159
8f1d7503
KS
160static int MemcmpTest12 (void)
161{
1859ed54
VJ
162 uint8_t a[] = "abcdabcd";
163 uint8_t b[] = "Zbcdabcde";
164
165 if (SCMemcmp(a, b, sizeof(a)-1) != 1)
166 return 0;
167
168 return 1;
169}
170
8f1d7503
KS
171static int MemcmpTest13 (void)
172{
1859ed54
VJ
173 uint8_t a[] = "abcdefgh";
174 uint8_t b[] = "AbCdEfGhIjK";
175
176 if (SCMemcmpLowercase(a, b, sizeof(a)-1) != 0)
177 return 0;
178
179 return 1;
180}
181
962462e4
VJ
182#include "util-cpu.h"
183
184#define TEST_RUNS 1000000
185
8f1d7503
KS
186static int MemcmpTest14 (void)
187{
962462e4
VJ
188#ifdef PROFILING
189 uint64_t ticks_start = 0;
190 uint64_t ticks_end = 0;
ab1200fb
VJ
191 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
192 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
962462e4
VJ
193
194 int t = 0;
195 int i, j;
196 int r1 = 0;
197
198 printf("\n");
199
200 ticks_start = UtilCpuGetTicks();
201 for (t = 0; t < TEST_RUNS; t++) {
202 for (i = 0; a[i] != NULL; i++) {
203 // printf("a[%d] = %s\n", i, a[i]);
204 size_t alen = strlen(a[i]) - 1;
205
206 for (j = 0; b[j] != NULL; j++) {
207 // printf("b[%d] = %s\n", j, b[j]);
208 size_t blen = strlen(b[j]) - 1;
209
210 r1 += (memcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen) ? 1 : 0);
211 }
212 }
213 }
214 ticks_end = UtilCpuGetTicks();
215 printf("memcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
216 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
217
218 printf("r1 %d\n", r1);
219 if (r1 != (51 * TEST_RUNS))
220 return 0;
221#endif
222 return 1;
223}
224
8f1d7503
KS
225static int MemcmpTest15 (void)
226{
962462e4
VJ
227#ifdef PROFILING
228 uint64_t ticks_start = 0;
229 uint64_t ticks_end = 0;
ab1200fb
VJ
230 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
231 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
962462e4
VJ
232
233 int t = 0;
234 int i, j;
235 int r2 = 0;
236
237 printf("\n");
238
239 ticks_start = UtilCpuGetTicks();
240 for (t = 0; t < TEST_RUNS; t++) {
241 for (i = 0; a[i] != NULL; i++) {
242 // printf("a[%d] = %s\n", i, a[i]);
243 size_t alen = strlen(a[i]) - 1;
244
245 for (j = 0; b[j] != NULL; j++) {
246 // printf("b[%d] = %s\n", j, b[j]);
247 size_t blen = strlen(b[j]) - 1;
248
249 r2 += MemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
250 }
251 }
252 }
253 ticks_end = UtilCpuGetTicks();
254 printf("MemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
255 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
256
257 printf("r2 %d\n", r2);
258 if (r2 != (51 * TEST_RUNS))
259 return 0;
260#endif
261 return 1;
262}
263
8f1d7503
KS
264static int MemcmpTest16 (void)
265{
962462e4
VJ
266#ifdef PROFILING
267 uint64_t ticks_start = 0;
268 uint64_t ticks_end = 0;
ab1200fb
VJ
269 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
270 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
962462e4
VJ
271
272 int t = 0;
273 int i, j;
274 int r3 = 0;
275
276 printf("\n");
277
278 ticks_start = UtilCpuGetTicks();
279 for (t = 0; t < TEST_RUNS; t++) {
280 for (i = 0; a[i] != NULL; i++) {
281 // printf("a[%d] = %s\n", i, a[i]);
282 size_t alen = strlen(a[i]) - 1;
283
284 for (j = 0; b[j] != NULL; j++) {
285 // printf("b[%d] = %s\n", j, b[j]);
286 size_t blen = strlen(b[j]) - 1;
287
288 r3 += SCMemcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
289 }
290 }
291 }
292 ticks_end = UtilCpuGetTicks();
293 printf("SCMemcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
294 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
295
296 printf("r3 %d\n", r3);
297 if (r3 != (51 * TEST_RUNS))
298 return 0;
299#endif
300 return 1;
301}
302
8f1d7503
KS
303static int MemcmpTest17 (void)
304{
962462e4
VJ
305#ifdef PROFILING
306 uint64_t ticks_start = 0;
307 uint64_t ticks_end = 0;
ab1200fb
VJ
308 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
309 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
962462e4
VJ
310
311 int t = 0;
312 int i, j;
313 int r4 = 0;
314
315 printf("\n");
316
317 ticks_start = UtilCpuGetTicks();
318 for (t = 0; t < TEST_RUNS; t++) {
319 for (i = 0; a[i] != NULL; i++) {
320 // printf("a[%d] = %s\n", i, a[i]);
321 size_t alen = strlen(a[i]) - 1;
322
323 for (j = 0; b[j] != NULL; j++) {
324 // printf("b[%d] = %s\n", j, b[j]);
325 size_t blen = strlen(b[j]) - 1;
326
327 r4 += SCMemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
328 }
329 }
330 }
331 ticks_end = UtilCpuGetTicks();
332 printf("SCMemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
333 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
334
335 printf("r4 %d\n", r4);
336 if (r4 != (51 * TEST_RUNS))
337 return 0;
338#endif
339 return 1;
340}
341
0d910bed 342struct MemcmpTest18Tests {
ab1200fb
VJ
343 const char *a;
344 const char *b;
0d910bed
VJ
345 int result;
346} memcmp_tests18_tests[] = {
347 { "abcdefgh", "!bcdefgh", 1, },
348 { "?bcdefgh", "!bcdefgh", 1, },
349 { "!bcdefgh", "abcdefgh", 1, },
350 { "!bcdefgh", "?bcdefgh", 1, },
351 { "zbcdefgh", "bbcdefgh", 1, },
352
353 { "abcdefgh12345678", "!bcdefgh12345678", 1, },
354 { "?bcdefgh12345678", "!bcdefgh12345678", 1, },
355 { "!bcdefgh12345678", "abcdefgh12345678", 1, },
356 { "!bcdefgh12345678", "?bcdefgh12345678", 1, },
357 { "bbcdefgh12345678", "zbcdefgh12345678", 1, },
358
359 { "abcdefgh", "abcdefgh", 0, },
360 { "abcdefgh", "Abcdefgh", 0, },
361 { "abcdefgh12345678", "Abcdefgh12345678", 0, },
362
363 { NULL, NULL, 0 },
364
365 };
366
367static int MemcmpTest18 (void)
368{
369 struct MemcmpTest18Tests *t = memcmp_tests18_tests;
370
371 while (t && t->a != NULL) {
372
373 if (SCMemcmpLowercase(t->a, t->b, strlen(t->a)-1) != t->result)
374 return 0;
375 SCLogInfo("ok");
376 t++;
377 }
378
379 return 1;
380}
381
1859ed54
VJ
382#endif /* UNITTESTS */
383
8f1d7503
KS
384void MemcmpRegisterTests(void)
385{
1859ed54 386#ifdef UNITTESTS
796dd522
JI
387 UtRegisterTest("MemcmpTest01", MemcmpTest01);
388 UtRegisterTest("MemcmpTest02", MemcmpTest02);
389 UtRegisterTest("MemcmpTest03", MemcmpTest03);
390 UtRegisterTest("MemcmpTest04", MemcmpTest04);
391 UtRegisterTest("MemcmpTest05", MemcmpTest05);
392 UtRegisterTest("MemcmpTest06", MemcmpTest06);
393 UtRegisterTest("MemcmpTest07", MemcmpTest07);
394 UtRegisterTest("MemcmpTest08", MemcmpTest08);
395 UtRegisterTest("MemcmpTest09", MemcmpTest09);
396 UtRegisterTest("MemcmpTest10", MemcmpTest10);
397 UtRegisterTest("MemcmpTest11", MemcmpTest11);
398 UtRegisterTest("MemcmpTest12", MemcmpTest12);
399 UtRegisterTest("MemcmpTest13", MemcmpTest13);
400 UtRegisterTest("MemcmpTest14", MemcmpTest14);
401 UtRegisterTest("MemcmpTest15", MemcmpTest15);
402 UtRegisterTest("MemcmpTest16", MemcmpTest16);
403 UtRegisterTest("MemcmpTest17", MemcmpTest17);
404 UtRegisterTest("MemcmpTest18", MemcmpTest18);
1859ed54
VJ
405#endif /* UNITTESTS */
406}
407