OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #ifndef CORE_FXCRT_INCLUDE_FX_SYSTEM_H_ | |
8 #define CORE_FXCRT_INCLUDE_FX_SYSTEM_H_ | |
9 | |
10 #include <assert.h> | |
11 #include <math.h> | |
12 #include <stdarg.h> | |
13 #include <stddef.h> | |
14 #include <stdint.h> | |
15 #include <stdio.h> | |
16 #include <stdlib.h> | |
17 #include <string.h> | |
18 #include <wchar.h> | |
19 | |
20 // _FX_OS_ values: | |
21 #define _FX_WIN32_DESKTOP_ 1 | |
22 #define _FX_WIN64_DESKTOP_ 2 | |
23 #define _FX_LINUX_DESKTOP_ 4 | |
24 #define _FX_MACOSX_ 7 | |
25 #define _FX_ANDROID_ 12 | |
26 | |
27 // _FXM_PLATFORM_ values; | |
28 #define _FXM_PLATFORM_WINDOWS_ 1 // _FX_WIN32_DESKTOP_ or _FX_WIN64_DESKTOP_. | |
29 #define _FXM_PLATFORM_LINUX_ 2 // _FX_LINUX_DESKTOP_ always. | |
30 #define _FXM_PLATFORM_APPLE_ 3 // _FX_MACOSX_ always. | |
31 #define _FXM_PLATFORM_ANDROID_ 4 // _FX_ANDROID_ always. | |
32 | |
33 #ifndef _FX_OS_ | |
34 #if defined(__ANDROID__) | |
35 #define _FX_OS_ _FX_ANDROID_ | |
36 #define _FXM_PLATFORM_ _FXM_PLATFORM_ANDROID_ | |
37 #elif defined(_WIN32) | |
38 #define _FX_OS_ _FX_WIN32_DESKTOP_ | |
39 #define _FXM_PLATFORM_ _FXM_PLATFORM_WINDOWS_ | |
40 #elif defined(_WIN64) | |
41 #define _FX_OS_ _FX_WIN64_DESKTOP_ | |
42 #define _FXM_PLATFORM_ _FXM_PLATFORM_WINDOWS_ | |
43 #elif defined(__linux__) | |
44 #define _FX_OS_ _FX_LINUX_DESKTOP_ | |
45 #define _FXM_PLATFORM_ _FXM_PLATFORM_LINUX_ | |
46 #elif defined(__APPLE__) | |
47 #define _FX_OS_ _FX_MACOSX_ | |
48 #define _FXM_PLATFORM_ _FXM_PLATFORM_APPLE_ | |
49 #endif | |
50 #endif // _FX_OS_ | |
51 | |
52 #if !defined(_FX_OS_) || _FX_OS_ == 0 | |
53 #error Sorry, can not figure out target OS. Please specify _FX_OS_ macro. | |
54 #endif | |
55 | |
56 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
57 #include <windows.h> | |
58 #include <sal.h> | |
59 #endif | |
60 | |
61 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
62 #include <Carbon/Carbon.h> | |
63 #include <libkern/OSAtomic.h> | |
64 #endif | |
65 | |
66 #ifdef __cplusplus | |
67 extern "C" { | |
68 #endif | |
69 typedef void* FX_POSITION; // Keep until fxcrt containers gone | |
70 typedef float FX_FLOAT; // Keep, allow upgrade to doubles. | |
71 typedef double FX_DOUBLE; // Keep, allow downgrade to floats. | |
72 typedef int FX_BOOL; // Keep, sadly not always 0 or 1. | |
73 typedef char FX_CHAR; // Keep, questionable signedness. | |
74 typedef wchar_t FX_WCHAR; // Keep, maybe bad platform wchars. | |
75 | |
76 #define IsFloatZero(f) ((f) < 0.0001 && (f) > -0.0001) | |
77 #define IsFloatBigger(fa, fb) ((fa) > (fb) && !IsFloatZero((fa) - (fb))) | |
78 #define IsFloatSmaller(fa, fb) ((fa) < (fb) && !IsFloatZero((fa) - (fb))) | |
79 #define IsFloatEqual(fa, fb) IsFloatZero(fa - fb) | |
80 | |
81 // PDFium string sizes are limited to 2^31-1, and the value is signed to | |
82 // allow -1 as a placeholder for "unknown". | |
83 // TODO(palmer): it should be a |size_t|, or at least unsigned. | |
84 typedef int FX_STRSIZE; | |
85 | |
86 #ifndef TRUE | |
87 #define TRUE 1 | |
88 #endif | |
89 | |
90 #ifndef FALSE | |
91 #define FALSE 0 | |
92 #endif | |
93 | |
94 #ifdef __cplusplus | |
95 static_assert(TRUE == true, "true_needs_to_be_true"); | |
96 static_assert(FALSE == false, "false_needs_to_be_false"); | |
97 #endif | |
98 | |
99 #ifndef ASSERT | |
100 #ifndef NDEBUG | |
101 #define ASSERT assert | |
102 #else | |
103 #define ASSERT(a) | |
104 #endif | |
105 #endif | |
106 | |
107 #if defined(__clang__) || defined(__GNUC__) | |
108 #define PDFIUM_IMMEDIATE_CRASH() __builtin_trap() | |
109 #else | |
110 #define PDFIUM_IMMEDIATE_CRASH() ((void)(*(volatile char*)0 = 0)) | |
111 #endif | |
112 | |
113 // M_PI not universally present on all platforms. | |
114 #define FX_PI 3.1415926535897932384626433832795f | |
115 #define FX_BEZIER 0.5522847498308f | |
116 | |
117 // NOTE: prevent use of the return value from snprintf() since some platforms | |
118 // have different return values (e.g. windows _vsnprintf()), and provide | |
119 // versions that always NUL-terminate. | |
120 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900 | |
121 void FXSYS_snprintf(char* str, | |
122 size_t size, | |
123 _Printf_format_string_ const char* fmt, | |
124 ...); | |
125 void FXSYS_vsnprintf(char* str, size_t size, const char* fmt, va_list ap); | |
126 #else | |
127 #define FXSYS_snprintf (void) snprintf | |
128 #define FXSYS_vsnprintf (void) vsnprintf | |
129 #endif | |
130 | |
131 #define FXSYS_sprintf DO_NOT_USE_SPRINTF_DIE_DIE_DIE | |
132 #define FXSYS_vsprintf DO_NOT_USE_VSPRINTF_DIE_DIE_DIE | |
133 #define FXSYS_strncmp strncmp | |
134 #define FXSYS_strcmp strcmp | |
135 #define FXSYS_strcpy strcpy | |
136 #define FXSYS_strncpy strncpy | |
137 #define FXSYS_strstr strstr | |
138 #define FXSYS_FILE FILE | |
139 #define FXSYS_fopen fopen | |
140 #define FXSYS_fclose fclose | |
141 #define FXSYS_SEEK_END SEEK_END | |
142 #define FXSYS_SEEK_SET SEEK_SET | |
143 #define FXSYS_fseek fseek | |
144 #define FXSYS_ftell ftell | |
145 #define FXSYS_fread fread | |
146 #define FXSYS_fwrite fwrite | |
147 #define FXSYS_fprintf fprintf | |
148 #define FXSYS_fflush fflush | |
149 | |
150 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
151 #ifdef _NATIVE_WCHAR_T_DEFINED | |
152 #define FXSYS_wfopen(f, m) _wfopen((const wchar_t*)(f), (const wchar_t*)(m)) | |
153 #else | |
154 #define FXSYS_wfopen _wfopen | |
155 #endif | |
156 #else | |
157 FXSYS_FILE* FXSYS_wfopen(const FX_WCHAR* filename, const FX_WCHAR* mode); | |
158 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
159 | |
160 #ifdef __cplusplus | |
161 } // extern "C" | |
162 | |
163 #include "third_party/base/numerics/safe_conversions.h" | |
164 | |
165 #define FXSYS_strlen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(strlen(ptr)) | |
166 #define FXSYS_wcslen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(wcslen(ptr)) | |
167 | |
168 // Overloaded functions for C++ templates | |
169 inline FX_STRSIZE FXSYS_len(const FX_CHAR* ptr) { | |
170 return FXSYS_strlen(ptr); | |
171 } | |
172 | |
173 inline FX_STRSIZE FXSYS_len(const FX_WCHAR* ptr) { | |
174 return FXSYS_wcslen(ptr); | |
175 } | |
176 | |
177 inline int FXSYS_cmp(const FX_CHAR* ptr1, const FX_CHAR* ptr2, size_t len) { | |
178 return memcmp(ptr1, ptr2, len); | |
179 } | |
180 | |
181 inline int FXSYS_cmp(const FX_WCHAR* ptr1, const FX_WCHAR* ptr2, size_t len) { | |
182 return wmemcmp(ptr1, ptr2, len); | |
183 } | |
184 | |
185 inline const FX_CHAR* FXSYS_chr(const FX_CHAR* ptr, FX_CHAR ch, size_t len) { | |
186 return reinterpret_cast<const FX_CHAR*>(memchr(ptr, ch, len)); | |
187 } | |
188 | |
189 inline const FX_WCHAR* FXSYS_chr(const FX_WCHAR* ptr, FX_WCHAR ch, size_t len) { | |
190 return wmemchr(ptr, ch, len); | |
191 } | |
192 | |
193 extern "C" { | |
194 #else | |
195 #define FXSYS_strlen(ptr) ((FX_STRSIZE)strlen(ptr)) | |
196 #define FXSYS_wcslen(ptr) ((FX_STRSIZE)wcslen(ptr)) | |
197 #endif | |
198 | |
199 #define FXSYS_wcscmp wcscmp | |
200 #define FXSYS_wcsstr wcsstr | |
201 #define FXSYS_wcsncmp wcsncmp | |
202 #define FXSYS_vswprintf vswprintf | |
203 #define FXSYS_mbstowcs mbstowcs | |
204 #define FXSYS_wcstombs wcstombs | |
205 #define FXSYS_memcmp memcmp | |
206 #define FXSYS_memcpy memcpy | |
207 #define FXSYS_memmove memmove | |
208 #define FXSYS_memset memset | |
209 #define FXSYS_qsort qsort | |
210 #define FXSYS_bsearch bsearch | |
211 | |
212 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
213 #define FXSYS_GetACP GetACP | |
214 #define FXSYS_itoa _itoa | |
215 #define FXSYS_strlwr _strlwr | |
216 #define FXSYS_strupr _strupr | |
217 #define FXSYS_stricmp _stricmp | |
218 #ifdef _NATIVE_WCHAR_T_DEFINED | |
219 #define FXSYS_wcsicmp(str1, str2) _wcsicmp((wchar_t*)(str1), (wchar_t*)(str2)) | |
220 #define FXSYS_WideCharToMultiByte(p1, p2, p3, p4, p5, p6, p7, p8) \ | |
221 WideCharToMultiByte(p1, p2, (const wchar_t*)(p3), p4, p5, p6, p7, p8) | |
222 #define FXSYS_MultiByteToWideChar(p1, p2, p3, p4, p5, p6) \ | |
223 MultiByteToWideChar(p1, p2, p3, p4, (wchar_t*)(p5), p6) | |
224 #define FXSYS_wcslwr(str) _wcslwr((wchar_t*)(str)) | |
225 #define FXSYS_wcsupr(str) _wcsupr((wchar_t*)(str)) | |
226 #else | |
227 #define FXSYS_wcsicmp _wcsicmp | |
228 #define FXSYS_WideCharToMultiByte WideCharToMultiByte | |
229 #define FXSYS_MultiByteToWideChar MultiByteToWideChar | |
230 #define FXSYS_wcslwr _wcslwr | |
231 #define FXSYS_wcsupr _wcsupr | |
232 #endif | |
233 #define FXSYS_GetFullPathName GetFullPathName | |
234 #define FXSYS_GetModuleFileName GetModuleFileName | |
235 #else | |
236 int FXSYS_GetACP(); | |
237 char* FXSYS_itoa(int value, char* str, int radix); | |
238 int FXSYS_WideCharToMultiByte(uint32_t codepage, | |
239 uint32_t dwFlags, | |
240 const wchar_t* wstr, | |
241 int wlen, | |
242 char* buf, | |
243 int buflen, | |
244 const char* default_str, | |
245 int* pUseDefault); | |
246 int FXSYS_MultiByteToWideChar(uint32_t codepage, | |
247 uint32_t dwFlags, | |
248 const char* bstr, | |
249 int blen, | |
250 wchar_t* buf, | |
251 int buflen); | |
252 uint32_t FXSYS_GetFullPathName(const char* filename, | |
253 uint32_t buflen, | |
254 char* buf, | |
255 char** filepart); | |
256 uint32_t FXSYS_GetModuleFileName(void* hModule, char* buf, uint32_t bufsize); | |
257 char* FXSYS_strlwr(char* str); | |
258 char* FXSYS_strupr(char* str); | |
259 int FXSYS_stricmp(const char*, const char*); | |
260 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2); | |
261 wchar_t* FXSYS_wcslwr(wchar_t* str); | |
262 wchar_t* FXSYS_wcsupr(wchar_t* str); | |
263 #endif // _FXM_PLATFORM == _FXM_PLATFORM_WINDOWS_ | |
264 | |
265 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
266 #define FXSYS_pow(a, b) (FX_FLOAT) powf(a, b) | |
267 #else | |
268 #define FXSYS_pow(a, b) (FX_FLOAT) pow(a, b) | |
269 #endif | |
270 #define FXSYS_sqrt(a) (FX_FLOAT) sqrt(a) | |
271 #define FXSYS_fabs(a) (FX_FLOAT) fabs(a) | |
272 #define FXSYS_atan2(a, b) (FX_FLOAT) atan2(a, b) | |
273 #define FXSYS_ceil(a) (FX_FLOAT) ceil(a) | |
274 #define FXSYS_floor(a) (FX_FLOAT) floor(a) | |
275 #define FXSYS_cos(a) (FX_FLOAT) cos(a) | |
276 #define FXSYS_acos(a) (FX_FLOAT) acos(a) | |
277 #define FXSYS_sin(a) (FX_FLOAT) sin(a) | |
278 #define FXSYS_log(a) (FX_FLOAT) log(a) | |
279 #define FXSYS_log10(a) (FX_FLOAT) log10(a) | |
280 #define FXSYS_fmod(a, b) (FX_FLOAT) fmod(a, b) | |
281 #define FXSYS_abs abs | |
282 #define FXDWORD_GET_LSBFIRST(p) \ | |
283 ((static_cast<uint32_t>(p[3]) << 24) | (static_cast<uint32_t>(p[2]) << 16) | \ | |
284 (static_cast<uint32_t>(p[1]) << 8) | (static_cast<uint32_t>(p[0]))) | |
285 #define FXDWORD_GET_MSBFIRST(p) \ | |
286 ((static_cast<uint32_t>(p[0]) << 24) | (static_cast<uint32_t>(p[1]) << 16) | \ | |
287 (static_cast<uint32_t>(p[2]) << 8) | (static_cast<uint32_t>(p[3]))) | |
288 #define FXSYS_HIBYTE(word) ((uint8_t)((word) >> 8)) | |
289 #define FXSYS_LOBYTE(word) ((uint8_t)(word)) | |
290 #define FXSYS_HIWORD(dword) ((uint16_t)((dword) >> 16)) | |
291 #define FXSYS_LOWORD(dword) ((uint16_t)(dword)) | |
292 int32_t FXSYS_atoi(const FX_CHAR* str); | |
293 uint32_t FXSYS_atoui(const FX_CHAR* str); | |
294 int32_t FXSYS_wtoi(const FX_WCHAR* str); | |
295 int64_t FXSYS_atoi64(const FX_CHAR* str); | |
296 int64_t FXSYS_wtoi64(const FX_WCHAR* str); | |
297 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix); | |
298 int FXSYS_round(FX_FLOAT f); | |
299 #define FXSYS_sqrt2(a, b) (FX_FLOAT) FXSYS_sqrt((a) * (a) + (b) * (b)) | |
300 #ifdef __cplusplus | |
301 }; | |
302 #endif | |
303 | |
304 // To print a size_t value in a portable way: | |
305 // size_t size; | |
306 // printf("xyz: %" PRIuS, size); | |
307 // The "u" in the macro corresponds to %u, and S is for "size". | |
308 | |
309 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | |
310 | |
311 #if (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64) | |
312 #error "inttypes.h has already been included before this header file, but " | |
313 #error "without __STDC_FORMAT_MACROS defined." | |
314 #endif | |
315 | |
316 #if !defined(__STDC_FORMAT_MACROS) | |
317 #define __STDC_FORMAT_MACROS | |
318 #endif | |
319 | |
320 #include <inttypes.h> | |
321 | |
322 #if !defined(PRIuS) | |
323 #define PRIuS "zu" | |
324 #endif | |
325 | |
326 #else // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | |
327 | |
328 #if !defined(PRIuS) | |
329 #define PRIuS "Iu" | |
330 #endif | |
331 | |
332 #endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | |
333 | |
334 // Prevent a function from ever being inlined, typically because we'd | |
335 // like it to appear in stack traces. | |
336 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
337 #define NEVER_INLINE __declspec(noinline) | |
338 #else // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
339 #define NEVER_INLINE __attribute__((__noinline__)) | |
340 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
341 | |
342 #endif // CORE_FXCRT_INCLUDE_FX_SYSTEM_H_ | |
OLD | NEW |