/src/wireshark/wsutil/ws_mempbrk.c
Line | Count | Source |
1 | | /* ws_mempbrk.c |
2 | | * |
3 | | * Wireshark - Network traffic analyzer |
4 | | * By Gerald Combs <gerald@wireshark.org> |
5 | | * Copyright 1998 Gerald Combs |
6 | | * |
7 | | * SPDX-License-Identifier: GPL-2.0-or-later |
8 | | */ |
9 | | |
10 | | #include "config.h" |
11 | | |
12 | | /* see bug 10798: there is a bug in the compiler the buildbots use for Mac OSX |
13 | | and SSE4.2, so we're not going to use SSE4.2 with Mac OSX right now, for |
14 | | older Mac OSX compilers. |
15 | | */ |
16 | | #ifdef __APPLE__ |
17 | | #if defined(__clang__) && (__clang_major__ >= 6) |
18 | | /* allow HAVE_SSE4_2 to be used for clang 6.0+ case because we know it works */ |
19 | | #else |
20 | | /* don't allow it otherwise, for Mac OSX */ |
21 | | #undef HAVE_SSE4_2 |
22 | | #endif |
23 | | #endif |
24 | | |
25 | | #include "ws_mempbrk.h" |
26 | | #include "ws_mempbrk_int.h" |
27 | | |
28 | | #include <string.h> |
29 | | |
30 | | void |
31 | | ws_mempbrk_compile(ws_mempbrk_pattern* pattern, const char *needles) |
32 | 307 | { |
33 | 307 | const char *n = needles; |
34 | 307 | memset(pattern->patt, 0, 256); |
35 | 2.03k | while (*n) { |
36 | 1.72k | pattern->patt[(int)*n] = 1; |
37 | 1.72k | n++; |
38 | 1.72k | } |
39 | | |
40 | 307 | #ifdef HAVE_SSE4_2 |
41 | 307 | ws_mempbrk_sse42_compile(pattern, needles); |
42 | 307 | #endif |
43 | 307 | } |
44 | | |
45 | | |
46 | | const uint8_t * |
47 | | ws_mempbrk_portable_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle) |
48 | 1.11M | { |
49 | 1.11M | const uint8_t *haystack_end = haystack + haystacklen; |
50 | | |
51 | 1.66M | while (haystack < haystack_end) { |
52 | 556k | if (pattern->patt[*haystack]) { |
53 | 11.0k | if (found_needle) |
54 | 10.8k | *found_needle = *haystack; |
55 | 11.0k | return haystack; |
56 | 11.0k | } |
57 | 545k | haystack++; |
58 | 545k | } |
59 | | |
60 | 1.10M | return NULL; |
61 | 1.11M | } |
62 | | |
63 | | |
64 | | WS_DLL_PUBLIC const uint8_t * |
65 | | ws_mempbrk_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle) |
66 | 1.13M | { |
67 | 1.13M | #ifdef HAVE_SSE4_2 |
68 | 1.13M | if (haystacklen >= 16 && pattern->use_sse42) |
69 | 26.6k | return (const uint8_t*)ws_mempbrk_sse42_exec((const char*)haystack, haystacklen, pattern, found_needle); |
70 | 1.10M | #endif |
71 | | |
72 | 1.10M | return ws_mempbrk_portable_exec(haystack, haystacklen, pattern, found_needle); |
73 | 1.13M | } |
74 | | |
75 | | WS_DLL_PUBLIC const uint8_t * |
76 | | ws_memrpbrk_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle) |
77 | 0 | { |
78 | 0 | const uint8_t *haystack_end = haystack + haystacklen; |
79 | |
|
80 | 0 | while (haystack_end > haystack) { |
81 | 0 | if (pattern->patt[*(--haystack_end)]) { |
82 | 0 | if (found_needle) |
83 | 0 | *found_needle = *haystack_end; |
84 | 0 | return haystack_end; |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | 0 | return NULL; |
89 | 0 | } |
90 | | |
91 | | /* |
92 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
93 | | * |
94 | | * Local variables: |
95 | | * c-basic-offset: 8 |
96 | | * tab-width: 8 |
97 | | * indent-tabs-mode: t |
98 | | * End: |
99 | | * |
100 | | * vi: set shiftwidth=8 tabstop=8 noexpandtab: |
101 | | * :indentSize=8:tabSize=8:noTabs=false: |
102 | | */ |