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_FX_SYSTEM_H_
8 #define CORE_FXCRT_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 // __cplusplus
69
70 typedef void* FX_POSITION; // Keep until fxcrt containers gone
71 typedef float FX_FLOAT; // Keep, allow upgrade to doubles.
72 typedef double FX_DOUBLE; // Keep, allow downgrade to floats.
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 ASSERT
87 #ifndef NDEBUG
88 #define ASSERT assert
89 #else
90 #define ASSERT(a)
91 #endif
92 #endif
93
94 #if defined(__clang__) || defined(__GNUC__)
95 #define PDFIUM_IMMEDIATE_CRASH() __builtin_trap()
96 #else
97 #define PDFIUM_IMMEDIATE_CRASH() ((void)(*(volatile char*)0 = 0))
98 #endif
99
100 // M_PI not universally present on all platforms.
101 #define FX_PI 3.1415926535897932384626433832795f
102 #define FX_BEZIER 0.5522847498308f
103
104 // NOTE: prevent use of the return value from snprintf() since some platforms
105 // have different return values (e.g. windows _vsnprintf()), and provide
106 // versions that always NUL-terminate.
107 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900
108 void FXSYS_snprintf(char* str,
109 size_t size,
110 _Printf_format_string_ const char* fmt,
111 ...);
112 void FXSYS_vsnprintf(char* str, size_t size, const char* fmt, va_list ap);
113 #else
114 #define FXSYS_snprintf (void)snprintf
115 #define FXSYS_vsnprintf (void)vsnprintf
116 #endif
117
118 #define FXSYS_sprintf DO_NOT_USE_SPRINTF_DIE_DIE_DIE
119 #define FXSYS_vsprintf DO_NOT_USE_VSPRINTF_DIE_DIE_DIE
120 #define FXSYS_strncmp strncmp
121 #define FXSYS_strcmp strcmp
122 #define FXSYS_strcpy strcpy
123 #define FXSYS_strncpy strncpy
124 #define FXSYS_strstr strstr
125 #define FXSYS_FILE FILE
126 #define FXSYS_fopen fopen
127 #define FXSYS_fclose fclose
128 #define FXSYS_SEEK_END SEEK_END
129 #define FXSYS_SEEK_SET SEEK_SET
130 #define FXSYS_fseek fseek
131 #define FXSYS_ftell ftell
132 #define FXSYS_fread fread
133 #define FXSYS_fwrite fwrite
134 #define FXSYS_fprintf fprintf
135 #define FXSYS_fflush fflush
136
137 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
138 #ifdef _NATIVE_WCHAR_T_DEFINED
139 #define FXSYS_wfopen(f, m) _wfopen((const wchar_t*)(f), (const wchar_t*)(m))
140 #else
141 #define FXSYS_wfopen _wfopen
142 #endif
143 #else
144 FXSYS_FILE* FXSYS_wfopen(const FX_WCHAR* filename, const FX_WCHAR* mode);
145 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
146
147 #ifdef __cplusplus
148 } // extern "C"
149
150 #include "third_party/base/numerics/safe_conversions.h"
151
152 #define FXSYS_strlen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(strlen(ptr))
153 #define FXSYS_wcslen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(wcslen(ptr))
154
155 // Overloaded functions for C++ templates
FXSYS_len(const FX_CHAR * ptr)156 inline FX_STRSIZE FXSYS_len(const FX_CHAR* ptr) {
157 return FXSYS_strlen(ptr);
158 }
159
FXSYS_len(const FX_WCHAR * ptr)160 inline FX_STRSIZE FXSYS_len(const FX_WCHAR* ptr) {
161 return FXSYS_wcslen(ptr);
162 }
163
FXSYS_cmp(const FX_CHAR * ptr1,const FX_CHAR * ptr2,size_t len)164 inline int FXSYS_cmp(const FX_CHAR* ptr1, const FX_CHAR* ptr2, size_t len) {
165 return memcmp(ptr1, ptr2, len);
166 }
167
FXSYS_cmp(const FX_WCHAR * ptr1,const FX_WCHAR * ptr2,size_t len)168 inline int FXSYS_cmp(const FX_WCHAR* ptr1, const FX_WCHAR* ptr2, size_t len) {
169 return wmemcmp(ptr1, ptr2, len);
170 }
171
FXSYS_chr(const FX_CHAR * ptr,FX_CHAR ch,size_t len)172 inline const FX_CHAR* FXSYS_chr(const FX_CHAR* ptr, FX_CHAR ch, size_t len) {
173 return reinterpret_cast<const FX_CHAR*>(memchr(ptr, ch, len));
174 }
175
FXSYS_chr(const FX_WCHAR * ptr,FX_WCHAR ch,size_t len)176 inline const FX_WCHAR* FXSYS_chr(const FX_WCHAR* ptr, FX_WCHAR ch, size_t len) {
177 return wmemchr(ptr, ch, len);
178 }
179
180 extern "C" {
181 #else
182 #define FXSYS_strlen(ptr) ((FX_STRSIZE)strlen(ptr))
183 #define FXSYS_wcslen(ptr) ((FX_STRSIZE)wcslen(ptr))
184 #endif
185
186 #define FXSYS_wcscmp wcscmp
187 #define FXSYS_wcsstr wcsstr
188 #define FXSYS_wcsncmp wcsncmp
189 #define FXSYS_vswprintf vswprintf
190 #define FXSYS_mbstowcs mbstowcs
191 #define FXSYS_wcstombs wcstombs
192 #define FXSYS_memcmp memcmp
193 #define FXSYS_memcpy memcpy
194 #define FXSYS_memmove memmove
195 #define FXSYS_memset memset
196 #define FXSYS_qsort qsort
197 #define FXSYS_bsearch bsearch
198
199 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
200 #define FXSYS_GetACP GetACP
201 #define FXSYS_itoa _itoa
202 #define FXSYS_strlwr _strlwr
203 #define FXSYS_strupr _strupr
204 #define FXSYS_stricmp _stricmp
205 #ifdef _NATIVE_WCHAR_T_DEFINED
206 #define FXSYS_wcsicmp(str1, str2) _wcsicmp((wchar_t*)(str1), (wchar_t*)(str2))
207 #define FXSYS_WideCharToMultiByte(p1, p2, p3, p4, p5, p6, p7, p8) \
208 WideCharToMultiByte(p1, p2, (const wchar_t*)(p3), p4, p5, p6, p7, p8)
209 #define FXSYS_MultiByteToWideChar(p1, p2, p3, p4, p5, p6) \
210 MultiByteToWideChar(p1, p2, p3, p4, (wchar_t*)(p5), p6)
211 #define FXSYS_wcslwr(str) _wcslwr((wchar_t*)(str))
212 #define FXSYS_wcsupr(str) _wcsupr((wchar_t*)(str))
213 #else
214 #define FXSYS_wcsicmp _wcsicmp
215 #define FXSYS_WideCharToMultiByte WideCharToMultiByte
216 #define FXSYS_MultiByteToWideChar MultiByteToWideChar
217 #define FXSYS_wcslwr _wcslwr
218 #define FXSYS_wcsupr _wcsupr
219 #endif
220 #define FXSYS_GetFullPathName GetFullPathName
221 #define FXSYS_GetModuleFileName GetModuleFileName
222 #else
223 int FXSYS_GetACP();
224 char* FXSYS_itoa(int value, char* str, int radix);
225 int FXSYS_WideCharToMultiByte(uint32_t codepage,
226 uint32_t dwFlags,
227 const wchar_t* wstr,
228 int wlen,
229 char* buf,
230 int buflen,
231 const char* default_str,
232 int* pUseDefault);
233 int FXSYS_MultiByteToWideChar(uint32_t codepage,
234 uint32_t dwFlags,
235 const char* bstr,
236 int blen,
237 wchar_t* buf,
238 int buflen);
239 uint32_t FXSYS_GetFullPathName(const char* filename,
240 uint32_t buflen,
241 char* buf,
242 char** filepart);
243 uint32_t FXSYS_GetModuleFileName(void* hModule, char* buf, uint32_t bufsize);
244 char* FXSYS_strlwr(char* str);
245 char* FXSYS_strupr(char* str);
246 int FXSYS_stricmp(const char*, const char*);
247 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2);
248 wchar_t* FXSYS_wcslwr(wchar_t* str);
249 wchar_t* FXSYS_wcsupr(wchar_t* str);
250 #endif // _FXM_PLATFORM == _FXM_PLATFORM_WINDOWS_
251
252 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
253 #define FXSYS_pow(a, b) (FX_FLOAT) powf(a, b)
254 #else
255 #define FXSYS_pow(a, b) (FX_FLOAT) pow(a, b)
256 #endif
257 #define FXSYS_sqrt(a) (FX_FLOAT) sqrt(a)
258 #define FXSYS_fabs(a) (FX_FLOAT) fabs(a)
259 #define FXSYS_atan2(a, b) (FX_FLOAT) atan2(a, b)
260 #define FXSYS_ceil(a) (FX_FLOAT) ceil(a)
261 #define FXSYS_floor(a) (FX_FLOAT) floor(a)
262 #define FXSYS_cos(a) (FX_FLOAT) cos(a)
263 #define FXSYS_acos(a) (FX_FLOAT) acos(a)
264 #define FXSYS_sin(a) (FX_FLOAT) sin(a)
265 #define FXSYS_log(a) (FX_FLOAT) log(a)
266 #define FXSYS_log10(a) (FX_FLOAT) log10(a)
267 #define FXSYS_fmod(a, b) (FX_FLOAT) fmod(a, b)
268 #define FXSYS_abs abs
269 #define FXDWORD_GET_LSBFIRST(p) \
270 ((static_cast<uint32_t>(p[3]) << 24) | (static_cast<uint32_t>(p[2]) << 16) | \
271 (static_cast<uint32_t>(p[1]) << 8) | (static_cast<uint32_t>(p[0])))
272 #define FXDWORD_GET_MSBFIRST(p) \
273 ((static_cast<uint32_t>(p[0]) << 24) | (static_cast<uint32_t>(p[1]) << 16) | \
274 (static_cast<uint32_t>(p[2]) << 8) | (static_cast<uint32_t>(p[3])))
275 #define FXSYS_HIBYTE(word) ((uint8_t)((word) >> 8))
276 #define FXSYS_LOBYTE(word) ((uint8_t)(word))
277 #define FXSYS_HIWORD(dword) ((uint16_t)((dword) >> 16))
278 #define FXSYS_LOWORD(dword) ((uint16_t)(dword))
279 int32_t FXSYS_atoi(const FX_CHAR* str);
280 uint32_t FXSYS_atoui(const FX_CHAR* str);
281 int32_t FXSYS_wtoi(const FX_WCHAR* str);
282 int64_t FXSYS_atoi64(const FX_CHAR* str);
283 int64_t FXSYS_wtoi64(const FX_WCHAR* str);
284 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix);
285 int FXSYS_round(FX_FLOAT f);
286 #define FXSYS_sqrt2(a, b) (FX_FLOAT) FXSYS_sqrt((a) * (a) + (b) * (b))
287 #ifdef __cplusplus
288 };
289 #endif
290
291 // To print a size_t value in a portable way:
292 // size_t size;
293 // printf("xyz: %" PRIuS, size);
294 // The "u" in the macro corresponds to %u, and S is for "size".
295
296 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
297
298 #if (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
299 #error "inttypes.h has already been included before this header file, but "
300 #error "without __STDC_FORMAT_MACROS defined."
301 #endif
302
303 #if !defined(__STDC_FORMAT_MACROS)
304 #define __STDC_FORMAT_MACROS
305 #endif
306
307 #include <inttypes.h>
308
309 #if !defined(PRIuS)
310 #define PRIuS "zu"
311 #endif
312
313 #else // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
314
315 #if !defined(PRIuS)
316 #define PRIuS "Iu"
317 #endif
318
319 #endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
320
321 // Prevent a function from ever being inlined, typically because we'd
322 // like it to appear in stack traces.
323 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
324 #define NEVER_INLINE __declspec(noinline)
325 #else // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
326 #define NEVER_INLINE __attribute__((__noinline__))
327 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
328
329 #endif // CORE_FXCRT_FX_SYSTEM_H_
330