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/strutils.h> 35 36 #ifndef NULL 37 #define NULL 0 38 #endif 39 40 #if defined(HAVE_STDBOOL_H) || (defined(_MSC_VER) && _MSC_VER >= 1800) 41 #include <stdbool.h> 42 #else 43 #ifndef __cplusplus 44 #define bool _Bool 45 #define true 1 46 #define false 0 47 #endif /* __cplusplus */ 48 #endif 49 50 #ifdef __cplusplus 51 #define EXTERNC extern "C" 52 #else 53 #define EXTERNC 54 #endif 55 56 #if defined(_WIN32) || defined(__CYGWIN__) 57 #ifdef YR_BUILDING_DLL 58 #ifdef __GNUC__ 59 #define YR_API EXTERNC __attribute__((dllexport)) 60 #define YR_DEPRECATED_API EXTERNC __attribute__((deprecated)) 61 #define YR_DEPRECATED __attribute__((deprecated)) 62 #else 63 #define YR_API EXTERNC __declspec(dllexport) 64 #define YR_DEPRECATED_API EXTERNC __declspec(deprecated) 65 #define YR_DEPRECATED __declspec(deprecated) 66 #endif 67 #elif defined(YR_IMPORTING_DLL) 68 #ifdef __GNUC__ 69 #define YR_API EXTERNC __attribute__((dllimport)) 70 #define YR_DEPRECATED_API EXTERNC __attribute__((deprecated)) 71 #define YR_DEPRECATED __attribute__((deprecated)) 72 #else 73 #define YR_API EXTERNC __declspec(dllimport) 74 #define YR_DEPRECATED_API EXTERNC __declspec(deprecated) 75 #define YR_DEPRECATED __declspec(deprecated) 76 #endif 77 #else 78 #define YR_API EXTERNC 79 #define YR_DEPRECATED_API EXTERNC 80 #define YR_DEPRECATED 81 #endif 82 #else 83 #if __GNUC__ >= 4 84 #define YR_API EXTERNC __attribute__((visibility("default"))) 85 #define YR_DEPRECATED_API YR_API __attribute__((deprecated)) 86 #define YR_DEPRECATED __attribute__((deprecated)) 87 #else 88 #define YR_API EXTERNC 89 #define YR_DEPRECATED_API EXTERNC 90 #define YR_DEPRECATED 91 #endif 92 #endif 93 94 #if defined(__GNUC__) 95 #define YR_ALIGN(n) __attribute__((aligned(n))) 96 #elif defined(_MSC_VER) 97 #define YR_ALIGN(n) __declspec(align(n)) 98 #else 99 #define YR_ALIGN(n) 100 #endif 101 102 #if defined(__GNUC__) 103 #define YR_PRINTF_LIKE(x, y) __attribute__((format(printf, x, y))) 104 #else 105 #define YR_PRINTF_LIKE(x, y) 106 #endif 107 108 #define yr_min(x, y) (((x) < (y)) ? (x) : (y)) 109 #define yr_max(x, y) (((x) > (y)) ? (x) : (y)) 110 111 #define yr_swap(x, y, T) \ 112 do \ 113 { \ 114 T temp = x; \ 115 x = y; \ 116 y = temp; \ 117 } while (0) 118 119 #ifdef NDEBUG 120 121 #define assertf(expr, msg, ...) ((void) 0) 122 123 #else 124 125 #include <stdlib.h> 126 127 #define assertf(expr, msg, ...) \ 128 if (!(expr)) \ 129 { \ 130 fprintf(stderr, "%s:%d: " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ 131 abort(); \ 132 } 133 134 #endif 135 136 // Set, unset, and test bits in an array of unsigned characters by integer 137 // index. The underlying array must be of type char or unsigned char to 138 // ensure compatibility with the CHAR_BIT constant used in these definitions. 139 140 #define YR_BITARRAY_SET(uchar_array_base, bitnum) \ 141 (((uchar_array_base)[(bitnum) / CHAR_BIT]) = \ 142 ((uchar_array_base)[(bitnum) / CHAR_BIT] | \ 143 (1 << ((bitnum) % CHAR_BIT)))) 144 145 #define YR_BITARRAY_UNSET(uchar_array_base, bitnum) \ 146 (((uchar_array_base)[(bitnum) / CHAR_BIT]) = \ 147 ((uchar_array_base)[(bitnum) / CHAR_BIT] & \ 148 (~(1 << ((bitnum) % CHAR_BIT))))) 149 150 #define YR_BITARRAY_TEST(uchar_array_base, bitnum) \ 151 (((uchar_array_base)[(bitnum) / CHAR_BIT] & (1 << ((bitnum) % CHAR_BIT))) != \ 152 0) 153 154 #define YR_BITARRAY_NCHARS(bitnum) (((bitnum) + (CHAR_BIT - 1)) / CHAR_BIT) 155 156 #endif 157