/src/yara/libyara/include/yara/utils.h
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2014. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #ifndef YR_UTILS_H |
31 | | #define YR_UTILS_H |
32 | | |
33 | | #include <limits.h> |
34 | | #include <yara/integers.h> |
35 | | |
36 | | #ifdef _MSC_VER |
37 | | #include <intrin.h> |
38 | | #endif |
39 | | |
40 | | #ifndef NULL |
41 | | #define NULL 0 |
42 | | #endif |
43 | | |
44 | | #if defined(HAVE_STDBOOL_H) || (defined(_MSC_VER) && _MSC_VER >= 1800) |
45 | | #include <stdbool.h> |
46 | | #else |
47 | | #ifndef __cplusplus |
48 | | #define bool _Bool |
49 | | #define true 1 |
50 | | #define false 0 |
51 | | #endif /* __cplusplus */ |
52 | | #endif |
53 | | |
54 | | #ifdef __cplusplus |
55 | | #define EXTERNC extern "C" |
56 | | #else |
57 | | #define EXTERNC |
58 | | #endif |
59 | | |
60 | | #if defined(_WIN32) || defined(__CYGWIN__) |
61 | | #ifdef YR_BUILDING_DLL |
62 | | #ifdef __GNUC__ |
63 | | #define YR_API EXTERNC __attribute__((dllexport)) |
64 | | #define YR_DEPRECATED_API EXTERNC __attribute__((deprecated)) |
65 | | #define YR_DEPRECATED(statement) statement __attribute__((deprecated)) |
66 | | #else |
67 | | #define YR_API EXTERNC __declspec(dllexport) |
68 | | #define YR_DEPRECATED_API EXTERNC __declspec(deprecated) |
69 | | #define YR_DEPRECATED(statement) __declspec(deprecated) statement |
70 | | #endif |
71 | | #elif defined(YR_IMPORTING_DLL) |
72 | | #ifdef __GNUC__ |
73 | | #define YR_API EXTERNC __attribute__((dllimport)) |
74 | | #define YR_DEPRECATED_API EXTERNC __attribute__((deprecated)) |
75 | | #define YR_DEPRECATED(statement) statement __attribute__((deprecated)) |
76 | | #else |
77 | | #define YR_API EXTERNC __declspec(dllimport) |
78 | | #define YR_DEPRECATED_API EXTERNC __declspec(deprecated) |
79 | | #define YR_DEPRECATED(statement) __declspec(deprecated) statement |
80 | | #endif |
81 | | #else |
82 | | #define YR_API EXTERNC |
83 | | #define YR_DEPRECATED_API EXTERNC |
84 | | #define YR_DEPRECATED(statement) statement |
85 | | #endif |
86 | | #else |
87 | | #if __GNUC__ >= 4 |
88 | | #define YR_API EXTERNC __attribute__((visibility("default"))) |
89 | | #define YR_DEPRECATED_API YR_API __attribute__((deprecated)) |
90 | | #define YR_DEPRECATED(statement) statement __attribute__((deprecated)) |
91 | | #else |
92 | | #define YR_API EXTERNC |
93 | | #define YR_DEPRECATED_API EXTERNC |
94 | | #define YR_DEPRECATED(statement) statement |
95 | | #endif |
96 | | #endif |
97 | | |
98 | | #if defined(__GNUC__) |
99 | | #define YR_ALIGN(n) __attribute__((aligned(n))) |
100 | | #elif defined(_MSC_VER) |
101 | | #define YR_ALIGN(n) __declspec(align(n)) |
102 | | #else |
103 | | #define YR_ALIGN(n) |
104 | | #endif |
105 | | |
106 | | #if defined(__GNUC__) |
107 | | #define YR_PRINTF_LIKE(x, y) __attribute__((format(printf, x, y))) |
108 | | #else |
109 | | #define YR_PRINTF_LIKE(x, y) |
110 | | #endif |
111 | | |
112 | 4.87M | #define yr_min(x, y) (((x) < (y)) ? (x) : (y)) |
113 | 4.49M | #define yr_max(x, y) (((x) > (y)) ? (x) : (y)) |
114 | | |
115 | | #define yr_swap(x, y, T) \ |
116 | 0 | do \ |
117 | 0 | { \ |
118 | 0 | T temp = x; \ |
119 | 0 | x = y; \ |
120 | 0 | y = temp; \ |
121 | 0 | } while (0) |
122 | | |
123 | | #ifdef NDEBUG |
124 | | |
125 | | #define assertf(expr, msg, ...) ((void) 0) |
126 | | |
127 | | #else |
128 | | |
129 | | #include <stdlib.h> |
130 | | |
131 | | #define assertf(expr, msg, ...) \ |
132 | 0 | if (!(expr)) \ |
133 | 0 | { \ |
134 | 0 | fprintf(stderr, "%s:%d: " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ |
135 | 0 | abort(); \ |
136 | 0 | } |
137 | | |
138 | | #endif |
139 | | |
140 | | // Set, unset, and test bits in an array of unsigned characters by integer |
141 | | // index. The underlying array must be of type char or unsigned char to |
142 | | // ensure compatibility with the CHAR_BIT constant used in these definitions. |
143 | | |
144 | | #define YR_BITARRAY_SET(uchar_array_base, bitnum) \ |
145 | | (((uchar_array_base)[(bitnum) / CHAR_BIT]) = \ |
146 | | ((uchar_array_base)[(bitnum) / CHAR_BIT] | \ |
147 | | (1 << ((bitnum) % CHAR_BIT)))) |
148 | | |
149 | | #define YR_BITARRAY_UNSET(uchar_array_base, bitnum) \ |
150 | | (((uchar_array_base)[(bitnum) / CHAR_BIT]) = \ |
151 | | ((uchar_array_base)[(bitnum) / CHAR_BIT] & \ |
152 | | (~(1 << ((bitnum) % CHAR_BIT))))) |
153 | | |
154 | | #define YR_BITARRAY_TEST(uchar_array_base, bitnum) \ |
155 | | (((uchar_array_base)[(bitnum) / CHAR_BIT] & (1 << ((bitnum) % CHAR_BIT))) != \ |
156 | | 0) |
157 | | |
158 | | #define YR_BITARRAY_NCHARS(bitnum) (((bitnum) + (CHAR_BIT - 1)) / CHAR_BIT) |
159 | | |
160 | | static inline int yr_popcount64(uint64_t val) |
161 | 10.0k | { |
162 | 10.0k | #if defined(__GNUC__) || defined(__clang__) |
163 | 10.0k | return __builtin_popcountll(val); |
164 | | #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64)) |
165 | | return (int) __popcnt64(val); |
166 | | #else |
167 | | val = val - ((val >> 1) & 0x5555555555555555ULL); |
168 | | val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL); |
169 | | val = (val + (val >> 4)) & 0x0F0F0F0F0F0F0F0FULL; |
170 | | return (int) ((val * 0x0101010101010101ULL) >> 56); |
171 | | #endif |
172 | 10.0k | } Unexecuted instantiation: rules_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: compiler.c:yr_popcount64 Unexecuted instantiation: hash.c:yr_popcount64 Unexecuted instantiation: lexer.c:yr_popcount64 Unexecuted instantiation: libyara.c:yr_popcount64 Unexecuted instantiation: mem.c:yr_popcount64 Unexecuted instantiation: modules.c:yr_popcount64 Unexecuted instantiation: object.c:yr_popcount64 Unexecuted instantiation: rules.c:yr_popcount64 Unexecuted instantiation: scanner.c:yr_popcount64 Unexecuted instantiation: sizedstr.c:yr_popcount64 Unexecuted instantiation: strutils.c:yr_popcount64 Unexecuted instantiation: tests.c:yr_popcount64 Unexecuted instantiation: elf.c:yr_popcount64 Unexecuted instantiation: math.c:yr_popcount64 Unexecuted instantiation: time.c:yr_popcount64 Unexecuted instantiation: pe.c:yr_popcount64 Unexecuted instantiation: pe_utils.c:yr_popcount64 Unexecuted instantiation: console.c:yr_popcount64 Unexecuted instantiation: string.c:yr_popcount64 Line | Count | Source | 161 | 10.0k | { | 162 | 10.0k | #if defined(__GNUC__) || defined(__clang__) | 163 | 10.0k | return __builtin_popcountll(val); | 164 | | #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64)) | 165 | | return (int) __popcnt64(val); | 166 | | #else | 167 | | val = val - ((val >> 1) & 0x5555555555555555ULL); | 168 | | val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL); | 169 | | val = (val + (val >> 4)) & 0x0F0F0F0F0F0F0F0FULL; | 170 | | return (int) ((val * 0x0101010101010101ULL) >> 56); | 171 | | #endif | 172 | 10.0k | } |
Unexecuted instantiation: macho.c:yr_popcount64 Unexecuted instantiation: dex.c:yr_popcount64 Unexecuted instantiation: grammar.c:yr_popcount64 Unexecuted instantiation: ahocorasick.c:yr_popcount64 Unexecuted instantiation: arena.c:yr_popcount64 Unexecuted instantiation: atoms.c:yr_popcount64 Unexecuted instantiation: bitmask.c:yr_popcount64 Unexecuted instantiation: exec.c:yr_popcount64 Unexecuted instantiation: exefiles.c:yr_popcount64 Unexecuted instantiation: filemap.c:yr_popcount64 Unexecuted instantiation: notebook.c:yr_popcount64 Unexecuted instantiation: parser.c:yr_popcount64 Unexecuted instantiation: proc.c:yr_popcount64 Unexecuted instantiation: re.c:yr_popcount64 Unexecuted instantiation: re_lexer.c:yr_popcount64 Unexecuted instantiation: scan.c:yr_popcount64 Unexecuted instantiation: simple_str.c:yr_popcount64 Unexecuted instantiation: stack.c:yr_popcount64 Unexecuted instantiation: linux.c:yr_popcount64 Unexecuted instantiation: base64.c:yr_popcount64 Unexecuted instantiation: hex_lexer.c:yr_popcount64 Unexecuted instantiation: re_grammar.c:yr_popcount64 Unexecuted instantiation: hex_grammar.c:yr_popcount64 Unexecuted instantiation: elf_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: pe_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: dex_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: filemap_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: math_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: dotnet_fuzzer.cc:yr_popcount64(unsigned long) Unexecuted instantiation: macho_fuzzer.cc:yr_popcount64(unsigned long) |
173 | | |
174 | | #endif |