Home | History | Annotate | Download | only in fxcrt
      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_INCLUDE_FXCRT_FX_SYSTEM_H_
      8 #define CORE_INCLUDE_FXCRT_FX_SYSTEM_H_
      9 
     10 #define _FX_WIN32_DESKTOP_		1
     11 #define _FX_LINUX_DESKTOP_		4
     12 #define _FX_MACOSX_				7
     13 #define _FX_ANDROID_			12
     14 
     15 #define _FXM_PLATFORM_WINDOWS_		1
     16 #define _FXM_PLATFORM_LINUX_		2
     17 #define _FXM_PLATFORM_APPLE_		3
     18 #define _FXM_PLATFORM_ANDROID_		4
     19 
     20 #ifndef _FX_OS_
     21 #if defined(__ANDROID__)
     22 #define _FX_OS_ _FX_ANDROID_
     23 #define _FXM_PLATFORM_ _FXM_PLATFORM_ANDROID_
     24 #elif defined(_WIN32) || defined(_WIN64)
     25 #define _FX_OS_ _FX_WIN32_DESKTOP_
     26 #define _FXM_PLATFORM_ _FXM_PLATFORM_WINDOWS_
     27 #elif defined(__linux__)
     28 #define _FX_OS_ _FX_LINUX_DESKTOP_
     29 #define _FXM_PLATFORM_ _FXM_PLATFORM_LINUX_
     30 #elif defined(__APPLE__)
     31 #define _FX_OS_ _FX_MACOSX_
     32 #define _FXM_PLATFORM_ _FXM_PLATFORM_APPLE_
     33 #endif
     34 #endif
     35 
     36 #if !defined(_FX_OS_) || _FX_OS_ == 0
     37 #error Sorry, can not figure out what OS you are targeting to. Please specify _FX_OS_ macro.
     38 #endif
     39 
     40 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
     41 #define _CRT_SECURE_NO_WARNINGS
     42 #include <sal.h>
     43 #include <windows.h>
     44 #endif  // _FXM_PLATFORM_WINDOWS_
     45 #define _FX_W32_		1
     46 #define _FX_W64_		2
     47 #ifndef _FX_WORDSIZE_
     48 #if defined(_WIN64) || defined(__arm64) || defined(__arm64__) || defined(_M_AMD64) || defined(_M_X64) || defined(_M_IA64) || defined(__powerpc64__) || defined(__x86_64__) || __WORDSIZE == 64 || defined(__LP64__)
     49 #define _FX_WORDSIZE_	_FX_W64_
     50 #else
     51 #define _FX_WORDSIZE_	_FX_W32_
     52 #endif
     53 #endif
     54 
     55 #include <stddef.h>
     56 #include <stdarg.h>
     57 #include <setjmp.h>
     58 #include <stdlib.h>
     59 #include <stdio.h>
     60 #include <string.h>
     61 #include <assert.h>
     62 #include <wchar.h>
     63 
     64 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
     65 #include <libkern/OSAtomic.h>
     66 #if _FX_OS_ == _FX_MACOSX_
     67 #include <Carbon/Carbon.h>
     68 #elif _FX_OS_ == _FX_IOS_
     69 #include <CoreText/CoreText.h>
     70 #include <CoreGraphics/CoreGraphics.h>
     71 #endif
     72 #endif
     73 
     74 #ifdef __cplusplus
     75 extern "C" {
     76 #endif
     77 typedef void*					FX_LPVOID;
     78 typedef void const*				FX_LPCVOID;
     79 typedef void*					FX_POSITION;
     80 typedef signed char				FX_INT8;
     81 typedef unsigned char			FX_UINT8;
     82 typedef unsigned char			FX_BYTE;
     83 typedef unsigned char*			FX_LPBYTE;
     84 typedef unsigned char const*	FX_LPCBYTE;
     85 typedef short					FX_INT16;
     86 typedef unsigned short			FX_UINT16;
     87 typedef short					FX_SHORT;
     88 typedef unsigned short			FX_WORD;
     89 typedef unsigned short*			FX_LPWORD;
     90 typedef unsigned short const*	FX_LPCWORD;
     91 typedef int						FX_INT32;
     92 typedef float					FX_FLOAT;
     93 typedef int						FX_BOOL;
     94 typedef int						FX_ERR;
     95 #define FX_SUCCEEDED(Status)	((FX_ERR)(Status) >= 0)
     96 #define FX_FAILED(Status)		((FX_ERR)(Status) < 0)
     97 typedef char					FX_CHAR;
     98 typedef char*					FX_LPSTR;
     99 typedef char const*				FX_LPCSTR;
    100 #define FX_DWORD_MAX UINT_MAX
    101 typedef unsigned int		FX_DWORD;
    102 typedef unsigned int*		FX_LPDWORD;
    103 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    104 typedef __int64				FX_INT64;
    105 typedef unsigned __int64	FX_UINT64;
    106 #else
    107 typedef long long int		FX_INT64;
    108 typedef unsigned long long	FX_UINT64;
    109 #endif
    110 #if _FX_WORDSIZE_ == _FX_W64_
    111 typedef FX_INT64			FX_INTPTR;
    112 typedef FX_UINT64			FX_UINTPTR;
    113 #else
    114 typedef int					FX_INTPTR;
    115 typedef unsigned int		FX_UINTPTR;
    116 #endif
    117 typedef wchar_t					FX_WCHAR;
    118 typedef wchar_t*				FX_LPWSTR;
    119 typedef wchar_t const*			FX_LPCWSTR;
    120 typedef FX_DWORD				FX_UINT32;
    121 typedef FX_UINT64				FX_QWORD;
    122 
    123 // PDFium string sizes are limited to 2^31-1, and the value is signed to
    124 // allow -1 as a placeholder for "unknown".
    125 // TODO(palmer): it should be a |size_t|, or at least unsigned.
    126 typedef int FX_STRSIZE;
    127 
    128 #if defined(DEBUG) && !defined(_DEBUG)
    129 #define _DEBUG
    130 #endif
    131 #ifndef TRUE
    132 
    133 #define TRUE	1
    134 #endif
    135 #ifndef FALSE
    136 
    137 #define FALSE	0
    138 #endif
    139 #ifndef NULL
    140 
    141 #define NULL	0
    142 #endif
    143 #define FXSYS_assert assert
    144 #ifndef ASSERT
    145 #ifdef _DEBUG
    146 #define ASSERT FXSYS_assert
    147 #else
    148 
    149 #define ASSERT(a)
    150 #endif
    151 #endif
    152 #define FX_MAX(a, b) (((a) > (b)) ? (a) : (b))
    153 #define FX_MIN(a, b) (((a) < (b)) ? (a) : (b))
    154 #define FX_PI	3.1415926535897932384626433832795f
    155 
    156 // NOTE: prevent use of the return value from snprintf() since some platforms
    157 // have different return values (e.g. windows _vsnprintf()), and provide
    158 // versions that always NUL-terminate.
    159 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900
    160 void FXSYS_snprintf(char *str, size_t size, _Printf_format_string_ const char* fmt, ...);
    161 void FXSYS_vsnprintf(char *str, size_t size, const char* fmt, va_list ap);
    162 #else
    163 #define FXSYS_snprintf	(void) snprintf
    164 #define FXSYS_vsnprintf	(void) vsnprintf
    165 #endif
    166 
    167 #define FXSYS_sprintf	DO_NOT_USE_SPRINTF_DIE_DIE_DIE
    168 #define FXSYS_vsprintf	DO_NOT_USE_VSPRINTF_DIE_DIE_DIE
    169 #define FXSYS_strchr	strchr
    170 #define FXSYS_strncmp	strncmp
    171 #define FXSYS_strcmp	strcmp
    172 #define FXSYS_strcpy	strcpy
    173 #define FXSYS_strncpy	strncpy
    174 #define FXSYS_strstr	strstr
    175 #define FXSYS_FILE		FILE
    176 #define FXSYS_fopen		fopen
    177 #define FXSYS_fclose	fclose
    178 #define FXSYS_SEEK_END	SEEK_END
    179 #define FXSYS_SEEK_SET	SEEK_SET
    180 #define FXSYS_fseek		fseek
    181 #define FXSYS_ftell		ftell
    182 #define FXSYS_fread		fread
    183 #define FXSYS_fwrite	fwrite
    184 #define FXSYS_fprintf	fprintf
    185 #define FXSYS_fflush	fflush
    186 
    187 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    188 #ifdef _NATIVE_WCHAR_T_DEFINED
    189 #define FXSYS_wfopen(f, m) _wfopen((const wchar_t*)(f), (const wchar_t*)(m))
    190 #else
    191 #define FXSYS_wfopen _wfopen
    192 #endif
    193 #else
    194 FXSYS_FILE* FXSYS_wfopen(FX_LPCWSTR filename, FX_LPCWSTR mode);
    195 #endif
    196 
    197 #ifdef __cplusplus
    198 } // extern "C"
    199 #include "../../../third_party/base/numerics/safe_conversions.h"
    200 #define FXSYS_strlen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(strlen(ptr))
    201 #define FXSYS_wcslen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(wcslen(ptr))
    202 extern "C" {
    203 #else
    204 #define FXSYS_strlen(ptr) ((FX_STRSIZE)strlen(ptr))
    205 #define FXSYS_wcslen(ptr) ((FX_STRSIZE)wcslen(ptr))
    206 #endif
    207 
    208 #define FXSYS_wcscmp	wcscmp
    209 #define FXSYS_wcschr	wcschr
    210 #define FXSYS_wcsstr	wcsstr
    211 #define FXSYS_wcsncmp	wcsncmp
    212 #define FXSYS_vswprintf	vswprintf
    213 #define FXSYS_mbstowcs	mbstowcs
    214 #define FXSYS_wcstombs	wcstombs
    215 #define FXSYS_memcmp	memcmp
    216 #define FXSYS_memcpy	memcpy
    217 #define FXSYS_memmove	memmove
    218 #define FXSYS_memset	memset
    219 #define FXSYS_memchr	memchr
    220 #define FXSYS_qsort		qsort
    221 #define FXSYS_bsearch	bsearch
    222 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    223 #define FXSYS_GetACP GetACP
    224 #define FXSYS_itoa _itoa
    225 #define FXSYS_strlwr _strlwr
    226 #define FXSYS_strupr _strupr
    227 #define FXSYS_stricmp _stricmp
    228 #ifdef _NATIVE_WCHAR_T_DEFINED
    229 #define FXSYS_wcsicmp(str1, str2) _wcsicmp((wchar_t*)(str1), (wchar_t*)(str2))
    230 #define FXSYS_WideCharToMultiByte(p1, p2, p3, p4, p5, p6, p7, p8) WideCharToMultiByte(p1, p2, (const wchar_t*)(p3), p4, p5, p6, p7, p8)
    231 #define FXSYS_MultiByteToWideChar(p1, p2, p3, p4, p5, p6) MultiByteToWideChar(p1, p2, p3, p4, (wchar_t*)(p5), p6)
    232 #define FXSYS_wcslwr(str) _wcslwr((wchar_t*)(str))
    233 #define FXSYS_wcsupr(str) _wcsupr((wchar_t*)(str))
    234 #else
    235 #define FXSYS_wcsicmp _wcsicmp
    236 #define FXSYS_WideCharToMultiByte WideCharToMultiByte
    237 #define FXSYS_MultiByteToWideChar MultiByteToWideChar
    238 #define FXSYS_wcslwr _wcslwr
    239 #define FXSYS_wcsupr _wcsupr
    240 #endif
    241 #define FXSYS_GetFullPathName GetFullPathName
    242 #define FXSYS_GetModuleFileName GetModuleFileName
    243 #else
    244 int			FXSYS_GetACP(void);
    245 char*		FXSYS_itoa(int value, char* string, int radix);
    246 int			FXSYS_WideCharToMultiByte(FX_DWORD codepage, FX_DWORD dwFlags, const wchar_t* wstr, int wlen,
    247                                       char* buf, int buflen, const char* default_str, int* pUseDefault);
    248 int			FXSYS_MultiByteToWideChar(FX_DWORD codepage, FX_DWORD dwFlags, const char* bstr, int blen,
    249                                       wchar_t* buf, int buflen);
    250 FX_DWORD	FXSYS_GetFullPathName(const char* filename, FX_DWORD buflen, char* buf, char** filepart);
    251 FX_DWORD	FXSYS_GetModuleFileName(void* hModule, char* buf, FX_DWORD bufsize);
    252 char*		FXSYS_strlwr(char* str);
    253 char*		FXSYS_strupr(char* str);
    254 int			FXSYS_stricmp(const char*, const char*);
    255 int			FXSYS_wcsicmp(const wchar_t *string1, const wchar_t *string2);
    256 wchar_t*	FXSYS_wcslwr(wchar_t* str);
    257 wchar_t*	FXSYS_wcsupr(wchar_t* str);
    258 #endif
    259 #define FXSYS_memcpy32		FXSYS_memcpy
    260 #define FXSYS_memcmp32		FXSYS_memcmp
    261 #define FXSYS_memset32		FXSYS_memset
    262 #define FXSYS_memset8		FXSYS_memset
    263 #define FXSYS_memmove32		FXSYS_memmove
    264 #ifdef __cplusplus
    265 }
    266 #endif
    267 #include <math.h>
    268 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    269 #define FXSYS_pow(a, b)		(FX_FLOAT)powf(a, b)
    270 #else
    271 #define FXSYS_pow(a, b)		(FX_FLOAT)pow(a, b)
    272 #endif
    273 #define FXSYS_sqrt(a)		(FX_FLOAT)sqrt(a)
    274 #define FXSYS_fabs(a)		(FX_FLOAT)fabs(a)
    275 #define FXSYS_atan2(a, b)	(FX_FLOAT)atan2(a, b)
    276 #define FXSYS_ceil(a)		(FX_FLOAT)ceil(a)
    277 #define FXSYS_floor(a)		(FX_FLOAT)floor(a)
    278 #define FXSYS_cos(a)		(FX_FLOAT)cos(a)
    279 #define FXSYS_acos(a)		(FX_FLOAT)acos(a)
    280 #define FXSYS_sin(a)		(FX_FLOAT)sin(a)
    281 #define FXSYS_log(a)		(FX_FLOAT)log(a)
    282 #define FXSYS_log10(a)		(FX_FLOAT)log10(a)
    283 #define FXSYS_fmod(a, b)	(FX_FLOAT)fmod(a, b)
    284 #define FXSYS_abs			abs
    285 #ifdef __cplusplus
    286 extern "C" {
    287 #endif
    288 #define _FX_LSB_FIRST_
    289 #define FXDWORD_FROM_LSBFIRST(i)	(i)
    290 #define FXDWORD_FROM_MSBFIRST(i)	(((FX_BYTE)(i) << 24) | ((FX_BYTE)((i) >> 8) << 16) | ((FX_BYTE)((i) >> 16) << 8) | (FX_BYTE)((i) >> 24))
    291 #define FXDWORD_GET_LSBFIRST(p)		((((FX_LPBYTE)(p))[3] << 24) | (((FX_LPBYTE)(p))[2] << 16) | (((FX_LPBYTE)(p))[1] << 8) | (((FX_LPBYTE)(p))[0]))
    292 #define FXDWORD_GET_MSBFIRST(p) ((((FX_LPBYTE)(p))[0] << 24) | (((FX_LPBYTE)(p))[1] << 16) | (((FX_LPBYTE)(p))[2] << 8) | (((FX_LPBYTE)(p))[3]))
    293 #define FXSYS_HIBYTE(word)	((FX_BYTE)((word) >> 8))
    294 #define FXSYS_LOBYTE(word)	((FX_BYTE)(word))
    295 #define FXSYS_HIWORD(dword)	((FX_WORD)((dword) >> 16))
    296 #define FXSYS_LOWORD(dword)	((FX_WORD)(dword))
    297 FX_INT32	FXSYS_atoi(FX_LPCSTR str);
    298 FX_INT32	FXSYS_wtoi(FX_LPCWSTR str);
    299 FX_INT64	FXSYS_atoi64(FX_LPCSTR str);
    300 FX_INT64	FXSYS_wtoi64(FX_LPCWSTR str);
    301 FX_LPCSTR	FXSYS_i64toa(FX_INT64 value, FX_LPSTR str, int radix);
    302 FX_LPCWSTR	FXSYS_i64tow(FX_INT64 value, FX_LPWSTR str, int radix);
    303 int			FXSYS_round(FX_FLOAT f);
    304 #define		FXSYS_Mul(a, b) ((a) * (b))
    305 #define		FXSYS_Div(a, b) ((a) / (b))
    306 #define		FXSYS_MulDiv(a, b, c) ((a) * (b) / (c))
    307 #define		FXSYS_sqrt2(a, b) (FX_FLOAT)FXSYS_sqrt((a)*(a) + (b)*(b))
    308 #ifdef __cplusplus
    309 };
    310 
    311 #if defined(__clang__) || _MSC_VER >= 1700
    312 #define FX_FINAL final
    313 #define FX_OVERRIDE override
    314 #elif defined(__GNUC__) && __cplusplus >= 201103 && \
    315       (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
    316 #define FX_FINAL final
    317 #define FX_OVERRIDE override
    318 #else
    319 #define FX_FINAL
    320 #define FX_OVERRIDE
    321 #endif
    322 #endif
    323 
    324 // To print a size_t value in a portable way:
    325 //   size_t size;
    326 //   printf("xyz: %" PRIuS, size);
    327 // The "u" in the macro corresponds to %u, and S is for "size".
    328 
    329 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
    330 
    331 #if (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
    332 #error "inttypes.h has already been included before this header file, but "
    333 #error "without __STDC_FORMAT_MACROS defined."
    334 #endif
    335 
    336 #if !defined(__STDC_FORMAT_MACROS)
    337 #define __STDC_FORMAT_MACROS
    338 #endif
    339 
    340 #include <inttypes.h>
    341 
    342 #if !defined(PRIuS)
    343 #define PRIuS "zu"
    344 #endif
    345 
    346 #else  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
    347 
    348 #if !defined(PRIuS)
    349 #define PRIuS "Iu"
    350 #endif
    351 
    352 #endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
    353 
    354 // Prevent a function from ever being inlined, typically because we'd
    355 // like it to appear in stack traces.
    356 #if  _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    357 #define NEVER_INLINE __declspec(noinline)
    358 #else  // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    359 #define NEVER_INLINE __attribute__((__noinline__))
    360 #endif  // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    361 
    362 #endif  // CORE_INCLUDE_FXCRT_FX_SYSTEM_H_
    363