/src/pycryptodome/src/common.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* =================================================================== |
2 | | * |
3 | | * Copyright (c) 2018, Helder Eijs <helderijs@gmail.com> |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * 2. Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in |
14 | | * the documentation and/or other materials provided with the |
15 | | * distribution. |
16 | | * |
17 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
20 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
21 | | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
23 | | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
27 | | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | | * POSSIBILITY OF SUCH DAMAGE. |
29 | | * =================================================================== |
30 | | */ |
31 | | #ifndef COMMON_H |
32 | | #define COMMON_H |
33 | | |
34 | | #include <stdio.h> |
35 | | #include <stdlib.h> |
36 | | #include <string.h> |
37 | | |
38 | | #include "errors.h" |
39 | | |
40 | | /* |
41 | | * Define STATIC as an empty string to turn certain static functions public |
42 | | */ |
43 | | #ifndef STATIC |
44 | | #define STATIC static inline |
45 | | #endif |
46 | | |
47 | | #define TRUE 1 |
48 | | #define FALSE 0 |
49 | | |
50 | | #ifndef MAX |
51 | | #define MAX(a,b) ((a)>(b)?(a):(b)) |
52 | | #endif |
53 | | |
54 | | #ifndef MIN |
55 | | #define MIN(a,b) ((a)<(b)?(a):(b)) |
56 | | #endif |
57 | | |
58 | 2.26k | #define _PASTE(x,y) x##y |
59 | 2.26k | #define _PASTE2(x,y) _PASTE(x,y) |
60 | | |
61 | | #ifdef HAVE_STDINT_H |
62 | | #include <stdint.h> |
63 | | #else |
64 | | |
65 | | typedef __int8 int8_t; |
66 | | typedef unsigned __int8 uint8_t; |
67 | | typedef __int16 int16_t; |
68 | | typedef unsigned __int16 uint16_t; |
69 | | typedef __int32 int32_t; |
70 | | typedef unsigned __int32 uint32_t; |
71 | | typedef __int64 int64_t; |
72 | | typedef unsigned __int64 uint64_t; |
73 | | |
74 | | #ifndef UINT32_MAX |
75 | | #define UINT32_MAX 0xFFFFFFFFUL |
76 | | #endif |
77 | | |
78 | | #ifndef UINT64_MAX |
79 | | #define UINT64_MAX 0xFFFFFFFFFFFFFFFFUL |
80 | | #endif |
81 | | |
82 | | #endif /* HAVE_STDINT_H */ |
83 | | |
84 | | #ifdef _MSC_VER |
85 | | |
86 | | /** Fix for warning C4668 **/ |
87 | | #define WIN32_LEAN_AND_MEAN |
88 | | |
89 | | #define inline _inline |
90 | | #define RESTRICT __restrict |
91 | | |
92 | | #include <malloc.h> |
93 | | |
94 | | #else /** Not MSC **/ |
95 | | |
96 | | #if __STDC_VERSION__ >= 199901L |
97 | | #define RESTRICT restrict |
98 | | #else |
99 | | #ifdef __GNUC__ |
100 | | #define RESTRICT __restrict |
101 | | #define inline __inline |
102 | | #else |
103 | | #define RESTRICT |
104 | | #define inline |
105 | | #endif |
106 | | #endif |
107 | | |
108 | | #endif |
109 | | |
110 | | /** Force checking of assertions **/ |
111 | | #ifdef NDEBUG |
112 | | #undef NDEBUG |
113 | | #endif |
114 | | #include <assert.h> |
115 | | |
116 | | /* |
117 | | * On Windows, distutils expects that a CPython module always exports the symbol init${MODNAME} |
118 | | */ |
119 | | #if defined(NO_CPYTHON_MODULE) |
120 | | #define FAKE_INIT(x) |
121 | | #elif defined(_MSC_VER) || defined(__MINGW32__) |
122 | | #include <Python.h> |
123 | | #if PY_MAJOR_VERSION >= 3 |
124 | | #define FAKE_INIT(x) PyMODINIT_FUNC _PASTE2(PyInit__,x) (void) { return NULL; } |
125 | | #else |
126 | | #define FAKE_INIT(x) PyMODINIT_FUNC _PASTE2(init_,x) (void) { return; } |
127 | | #endif |
128 | | #else |
129 | | #define FAKE_INIT(x) |
130 | | #endif |
131 | | |
132 | | /* |
133 | | * On Windows, functions must be explicitly marked for export. |
134 | | */ |
135 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
136 | | #define EXPORT_SYM __declspec(dllexport) |
137 | | #else |
138 | | #define EXPORT_SYM |
139 | | #endif |
140 | | |
141 | | /* |
142 | | * Platform specific routine for aligned allocation |
143 | | */ |
144 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
145 | | |
146 | | static inline void* align_alloc(size_t size, unsigned boundary) |
147 | | { |
148 | | return _aligned_malloc(size, boundary); |
149 | | } |
150 | | |
151 | | static inline void align_free(void *mem) |
152 | | { |
153 | | if (mem) { |
154 | | _aligned_free(mem); |
155 | | } |
156 | | } |
157 | | |
158 | | #elif defined(HAVE_POSIX_MEMALIGN) |
159 | | |
160 | | static inline void* align_alloc(size_t size, unsigned boundary) |
161 | | { |
162 | | int result; |
163 | | void *new_mem; |
164 | | result = posix_memalign((void**)&new_mem, boundary, size); |
165 | | return result ? NULL : new_mem; |
166 | | } |
167 | | |
168 | | static inline void align_free(void *mem) |
169 | | { |
170 | | free(mem); |
171 | | } |
172 | | |
173 | | #elif defined(HAVE_MEMALIGN) |
174 | | |
175 | | #include <malloc.h> |
176 | | |
177 | | static inline void* align_alloc(size_t size, unsigned boundary) |
178 | 0 | { |
179 | 0 | return memalign(boundary, size); |
180 | 0 | } |
181 | | |
182 | | static inline void align_free(void *mem) |
183 | 0 | { |
184 | 0 | free(mem); |
185 | 0 | } |
186 | | |
187 | | #else |
188 | | #error No routines for aligned memory |
189 | | #endif |
190 | | |
191 | | /* |
192 | | * Find first character in a string which is not c. |
193 | | */ |
194 | | static inline const uint8_t* memchr_not(const uint8_t* s, int c, size_t n) |
195 | 0 | { |
196 | 0 | size_t i; |
197 | 0 |
|
198 | 0 | for (i=0; i<n; i++, s++) |
199 | 0 | if (*s != c) |
200 | 0 | return s; |
201 | 0 | return NULL; |
202 | 0 | } |
203 | | |
204 | | #endif |