• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PLATFORM_ values;
21 #define _FX_PLATFORM_WINDOWS_ 1
22 #define _FX_PLATFORM_LINUX_ 2
23 #define _FX_PLATFORM_APPLE_ 3
24 
25 #if defined(_WIN32)
26 #define _FX_PLATFORM_ _FX_PLATFORM_WINDOWS_
27 #elif defined(_WIN64)
28 #define _FX_PLATFORM_ _FX_PLATFORM_WINDOWS_
29 #elif defined(__linux__)
30 #define _FX_PLATFORM_ _FX_PLATFORM_LINUX_
31 #elif defined(__APPLE__)
32 #define _FX_PLATFORM_ _FX_PLATFORM_APPLE_
33 #elif defined(__asmjs__) || defined(__wasm__)
34 #define _FX_PLATFORM_ _FX_PLATFORM_LINUX_
35 #endif
36 
37 #if defined(_MSC_VER) && _MSC_VER < 1900
38 #error Sorry, VC++ 2015 or later is required to compile PDFium.
39 #endif  // defined(_MSC_VER) && _MSC_VER < 1900
40 
41 #if defined(__wasm__) && defined(PDF_ENABLE_V8)
42 #error Cannot compile v8 with wasm.
43 #endif  // PDF_ENABLE_V8
44 
45 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
46 #include <windows.h>
47 #include <sal.h>
48 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
49 
50 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
51 #include <Carbon/Carbon.h>
52 #include <libkern/OSAtomic.h>
53 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif  // __cplusplus
58 
59 #define IsFloatZero(f) ((f) < 0.0001 && (f) > -0.0001)
60 #define IsFloatBigger(fa, fb) ((fa) > (fb) && !IsFloatZero((fa) - (fb)))
61 #define IsFloatSmaller(fa, fb) ((fa) < (fb) && !IsFloatZero((fa) - (fb)))
62 #define IsFloatEqual(fa, fb) IsFloatZero((fa) - (fb))
63 
64 // PDFium file sizes match the platform, but PDFium itself does not support
65 // files larger than 2GB even if the platform does. The value must be signed
66 // to support -1 error returns.
67 // TODO(tsepez): support larger files.
68 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
69 #define FX_FILESIZE int32_t
70 #else  // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
71 #define FX_FILESIZE off_t
72 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
73 
74 #ifndef ASSERT
75 #ifndef NDEBUG
76 #define ASSERT assert
77 #else
78 #define ASSERT(a)
79 #endif  // NDEBUG
80 #endif  // ASSERT
81 
82 // M_PI not universally present on all platforms.
83 #define FX_PI 3.1415926535897932384626433832795f
84 #define FX_BEZIER 0.5522847498308f
85 
86 // NOTE: prevent use of the return value from snprintf() since some platforms
87 // have different return values.
88 #define FXSYS_snprintf (void)snprintf
89 #define FXSYS_vsnprintf (void)vsnprintf
90 #define FXSYS_sprintf DO_NOT_USE_SPRINTF_DIE_DIE_DIE
91 #define FXSYS_vsprintf DO_NOT_USE_VSPRINTF_DIE_DIE_DIE
92 
93 #ifdef __cplusplus
94 }  // extern "C"
95 
96 #include "third_party/base/numerics/safe_conversions.h"
97 
98 // Overloaded functions for C++ templates
FXSYS_len(const char * ptr)99 inline size_t FXSYS_len(const char* ptr) {
100   return strlen(ptr);
101 }
102 
FXSYS_len(const wchar_t * ptr)103 inline size_t FXSYS_len(const wchar_t* ptr) {
104   return wcslen(ptr);
105 }
106 
FXSYS_cmp(const char * ptr1,const char * ptr2,size_t len)107 inline int FXSYS_cmp(const char* ptr1, const char* ptr2, size_t len) {
108   return memcmp(ptr1, ptr2, len);
109 }
110 
FXSYS_cmp(const wchar_t * ptr1,const wchar_t * ptr2,size_t len)111 inline int FXSYS_cmp(const wchar_t* ptr1, const wchar_t* ptr2, size_t len) {
112   return wmemcmp(ptr1, ptr2, len);
113 }
114 
FXSYS_chr(const char * ptr,char ch,size_t len)115 inline const char* FXSYS_chr(const char* ptr, char ch, size_t len) {
116   return reinterpret_cast<const char*>(memchr(ptr, ch, len));
117 }
118 
FXSYS_chr(const wchar_t * ptr,wchar_t ch,size_t len)119 inline const wchar_t* FXSYS_chr(const wchar_t* ptr, wchar_t ch, size_t len) {
120   return wmemchr(ptr, ch, len);
121 }
122 
123 extern "C" {
124 #endif  // __cplusplus
125 
126 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
127 #define FXSYS_GetACP GetACP
128 #define FXSYS_itoa _itoa
129 #define FXSYS_WideCharToMultiByte WideCharToMultiByte
130 #define FXSYS_MultiByteToWideChar MultiByteToWideChar
131 #define FXSYS_strlwr _strlwr
132 #define FXSYS_strupr _strupr
133 #define FXSYS_stricmp _stricmp
134 #define FXSYS_wcsicmp _wcsicmp
135 #define FXSYS_wcslwr _wcslwr
136 #define FXSYS_wcsupr _wcsupr
137 #define FXSYS_pow(a, b) (float)powf(a, b)
138 size_t FXSYS_wcsftime(wchar_t* strDest,
139                       size_t maxsize,
140                       const wchar_t* format,
141                       const struct tm* timeptr);
142 #define FXSYS_SetLastError SetLastError
143 #define FXSYS_GetLastError GetLastError
144 #else  // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
145 int FXSYS_GetACP();
146 char* FXSYS_itoa(int value, char* str, int radix);
147 int FXSYS_WideCharToMultiByte(uint32_t codepage,
148                               uint32_t dwFlags,
149                               const wchar_t* wstr,
150                               int wlen,
151                               char* buf,
152                               int buflen,
153                               const char* default_str,
154                               int* pUseDefault);
155 int FXSYS_MultiByteToWideChar(uint32_t codepage,
156                               uint32_t dwFlags,
157                               const char* bstr,
158                               int blen,
159                               wchar_t* buf,
160                               int buflen);
161 char* FXSYS_strlwr(char* str);
162 char* FXSYS_strupr(char* str);
163 int FXSYS_stricmp(const char* str1, const char* str2);
164 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2);
165 wchar_t* FXSYS_wcslwr(wchar_t* str);
166 wchar_t* FXSYS_wcsupr(wchar_t* str);
167 #define FXSYS_pow(a, b) (float)pow(a, b)
168 #define FXSYS_wcsftime wcsftime
169 void FXSYS_SetLastError(uint32_t err);
170 uint32_t FXSYS_GetLastError();
171 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
172 
173 #define FXWORD_GET_LSBFIRST(p)                                \
174   (static_cast<uint16_t>((static_cast<uint16_t>(p[1]) << 8) | \
175                          (static_cast<uint16_t>(p[0]))))
176 #define FXWORD_GET_MSBFIRST(p)                                \
177   (static_cast<uint16_t>((static_cast<uint16_t>(p[0]) << 8) | \
178                          (static_cast<uint16_t>(p[1]))))
179 #define FXDWORD_GET_LSBFIRST(p)                                                \
180   ((static_cast<uint32_t>(p[3]) << 24) | (static_cast<uint32_t>(p[2]) << 16) | \
181    (static_cast<uint32_t>(p[1]) << 8) | (static_cast<uint32_t>(p[0])))
182 #define FXDWORD_GET_MSBFIRST(p)                                                \
183   ((static_cast<uint32_t>(p[0]) << 24) | (static_cast<uint32_t>(p[1]) << 16) | \
184    (static_cast<uint32_t>(p[2]) << 8) | (static_cast<uint32_t>(p[3])))
185 int32_t FXSYS_atoi(const char* str);
186 uint32_t FXSYS_atoui(const char* str);
187 int32_t FXSYS_wtoi(const wchar_t* str);
188 int64_t FXSYS_atoi64(const char* str);
189 const char* FXSYS_i64toa(int64_t value, char* str, int radix);
190 int FXSYS_roundf(float f);
191 int FXSYS_round(double d);
192 #define FXSYS_sqrt2(a, b) (float)sqrt((a) * (a) + (b) * (b))
193 #ifdef __cplusplus
194 }  // extern C
195 #endif  // __cplusplus
196 
197 // To print a size_t value in a portable way:
198 //   size_t size;
199 //   printf("xyz: %" PRIuS, size);
200 // The "u" in the macro corresponds to %u, and S is for "size".
201 #if _FX_PLATFORM_ != _FX_PLATFORM_WINDOWS_
202 
203 #if (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
204 #error "inttypes.h has already been included before this header file, but "
205 #error "without __STDC_FORMAT_MACROS defined."
206 #endif
207 
208 #if !defined(__STDC_FORMAT_MACROS)
209 #define __STDC_FORMAT_MACROS
210 #endif
211 
212 #include <inttypes.h>
213 
214 #if !defined(PRIuS)
215 #define PRIuS "zu"
216 #endif
217 
218 #else  // _FX_PLATFORM_ != _FX_PLATFORM_WINDOWS_
219 
220 #if !defined(PRIuS)
221 #define PRIuS "Iu"
222 #endif
223 
224 #endif  // _FX_PLATFORM_ != _FX_PLATFORM_WINDOWS_
225 
226 #endif  // CORE_FXCRT_FX_SYSTEM_H_
227