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 #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 #define _CRT_SECURE_NO_WARNINGS
     58 #include <sal.h>
     59 #include <windows.h>
     60 #endif
     61 
     62 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
     63 #include <libkern/OSAtomic.h>
     64 #include <Carbon/Carbon.h>
     65 #endif
     66 
     67 #ifdef __cplusplus
     68 extern "C" {
     69 #endif
     70 typedef void* FX_POSITION;       // Keep until fxcrt containers gone
     71 typedef unsigned short FX_WORD;  // Keep - "an efficient small type"
     72 typedef unsigned int FX_DWORD;   // Keep - "an efficient type"
     73 typedef float FX_FLOAT;          // Keep, allow upgrade to doubles.
     74 typedef int FX_BOOL;             // Keep, sadly not always 0 or 1.
     75 typedef char FX_CHAR;            // Keep, questionable signedness.
     76 typedef wchar_t FX_WCHAR;        // Keep, maybe bad platform wchars.
     77 
     78 // PDFium string sizes are limited to 2^31-1, and the value is signed to
     79 // allow -1 as a placeholder for "unknown".
     80 // TODO(palmer): it should be a |size_t|, or at least unsigned.
     81 typedef int FX_STRSIZE;
     82 
     83 #if defined(DEBUG) && !defined(_DEBUG)
     84 #define _DEBUG
     85 #endif
     86 
     87 #ifndef TRUE
     88 #define TRUE 1
     89 #endif
     90 
     91 #ifndef FALSE
     92 #define FALSE 0
     93 #endif
     94 
     95 #ifdef __cplusplus
     96 static_assert(TRUE == true, "true_needs_to_be_true");
     97 static_assert(FALSE == false, "false_needs_to_be_false");
     98 #endif
     99 
    100 #ifndef NULL
    101 #define NULL 0
    102 #endif
    103 
    104 #define FXSYS_assert assert
    105 #ifndef ASSERT
    106 #ifdef _DEBUG
    107 #define ASSERT FXSYS_assert
    108 #else
    109 #define ASSERT(a)
    110 #endif
    111 #endif
    112 
    113 #define FX_MIN(a, b) (((a) < (b)) ? (a) : (b))
    114 #define FX_PI 3.1415926535897932384626433832795f
    115 
    116 // NOTE: prevent use of the return value from snprintf() since some platforms
    117 // have different return values (e.g. windows _vsnprintf()), and provide
    118 // versions that always NUL-terminate.
    119 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900
    120 void FXSYS_snprintf(char* str,
    121                     size_t size,
    122                     _Printf_format_string_ const char* fmt,
    123                     ...);
    124 void FXSYS_vsnprintf(char* str, size_t size, const char* fmt, va_list ap);
    125 #else
    126 #define FXSYS_snprintf (void) snprintf
    127 #define FXSYS_vsnprintf (void) vsnprintf
    128 #endif
    129 
    130 #define FXSYS_sprintf DO_NOT_USE_SPRINTF_DIE_DIE_DIE
    131 #define FXSYS_vsprintf DO_NOT_USE_VSPRINTF_DIE_DIE_DIE
    132 #define FXSYS_strchr strchr
    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 #include "third_party/base/numerics/safe_conversions.h"
    163 #define FXSYS_strlen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(strlen(ptr))
    164 #define FXSYS_wcslen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(wcslen(ptr))
    165 extern "C" {
    166 #else
    167 #define FXSYS_strlen(ptr) ((FX_STRSIZE)strlen(ptr))
    168 #define FXSYS_wcslen(ptr) ((FX_STRSIZE)wcslen(ptr))
    169 #endif
    170 
    171 #define FXSYS_wcscmp wcscmp
    172 #define FXSYS_wcschr wcschr
    173 #define FXSYS_wcsstr wcsstr
    174 #define FXSYS_wcsncmp wcsncmp
    175 #define FXSYS_vswprintf vswprintf
    176 #define FXSYS_mbstowcs mbstowcs
    177 #define FXSYS_wcstombs wcstombs
    178 #define FXSYS_memcmp memcmp
    179 #define FXSYS_memcpy memcpy
    180 #define FXSYS_memmove memmove
    181 #define FXSYS_memset memset
    182 #define FXSYS_memchr memchr
    183 #define FXSYS_qsort qsort
    184 #define FXSYS_bsearch bsearch
    185 
    186 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    187 #define FXSYS_GetACP GetACP
    188 #define FXSYS_itoa _itoa
    189 #define FXSYS_strlwr _strlwr
    190 #define FXSYS_strupr _strupr
    191 #define FXSYS_stricmp _stricmp
    192 #ifdef _NATIVE_WCHAR_T_DEFINED
    193 #define FXSYS_wcsicmp(str1, str2) _wcsicmp((wchar_t*)(str1), (wchar_t*)(str2))
    194 #define FXSYS_WideCharToMultiByte(p1, p2, p3, p4, p5, p6, p7, p8) \
    195   WideCharToMultiByte(p1, p2, (const wchar_t*)(p3), p4, p5, p6, p7, p8)
    196 #define FXSYS_MultiByteToWideChar(p1, p2, p3, p4, p5, p6) \
    197   MultiByteToWideChar(p1, p2, p3, p4, (wchar_t*)(p5), p6)
    198 #define FXSYS_wcslwr(str) _wcslwr((wchar_t*)(str))
    199 #define FXSYS_wcsupr(str) _wcsupr((wchar_t*)(str))
    200 #else
    201 #define FXSYS_wcsicmp _wcsicmp
    202 #define FXSYS_WideCharToMultiByte WideCharToMultiByte
    203 #define FXSYS_MultiByteToWideChar MultiByteToWideChar
    204 #define FXSYS_wcslwr _wcslwr
    205 #define FXSYS_wcsupr _wcsupr
    206 #endif
    207 #define FXSYS_GetFullPathName GetFullPathName
    208 #define FXSYS_GetModuleFileName GetModuleFileName
    209 #else
    210 int FXSYS_GetACP(void);
    211 char* FXSYS_itoa(int value, char* string, int radix);
    212 int FXSYS_WideCharToMultiByte(FX_DWORD codepage,
    213                               FX_DWORD dwFlags,
    214                               const wchar_t* wstr,
    215                               int wlen,
    216                               char* buf,
    217                               int buflen,
    218                               const char* default_str,
    219                               int* pUseDefault);
    220 int FXSYS_MultiByteToWideChar(FX_DWORD codepage,
    221                               FX_DWORD dwFlags,
    222                               const char* bstr,
    223                               int blen,
    224                               wchar_t* buf,
    225                               int buflen);
    226 FX_DWORD FXSYS_GetFullPathName(const char* filename,
    227                                FX_DWORD buflen,
    228                                char* buf,
    229                                char** filepart);
    230 FX_DWORD FXSYS_GetModuleFileName(void* hModule, char* buf, FX_DWORD bufsize);
    231 char* FXSYS_strlwr(char* str);
    232 char* FXSYS_strupr(char* str);
    233 int FXSYS_stricmp(const char*, const char*);
    234 int FXSYS_wcsicmp(const wchar_t* string1, const wchar_t* string2);
    235 wchar_t* FXSYS_wcslwr(wchar_t* str);
    236 wchar_t* FXSYS_wcsupr(wchar_t* str);
    237 #endif  // _FXM_PLATFORM == _FXM_PLATFORM_WINDOWS_
    238 
    239 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    240 #define FXSYS_pow(a, b) (FX_FLOAT) powf(a, b)
    241 #else
    242 #define FXSYS_pow(a, b) (FX_FLOAT) pow(a, b)
    243 #endif
    244 #define FXSYS_sqrt(a) (FX_FLOAT) sqrt(a)
    245 #define FXSYS_fabs(a) (FX_FLOAT) fabs(a)
    246 #define FXSYS_atan2(a, b) (FX_FLOAT) atan2(a, b)
    247 #define FXSYS_ceil(a) (FX_FLOAT) ceil(a)
    248 #define FXSYS_floor(a) (FX_FLOAT) floor(a)
    249 #define FXSYS_cos(a) (FX_FLOAT) cos(a)
    250 #define FXSYS_acos(a) (FX_FLOAT) acos(a)
    251 #define FXSYS_sin(a) (FX_FLOAT) sin(a)
    252 #define FXSYS_log(a) (FX_FLOAT) log(a)
    253 #define FXSYS_log10(a) (FX_FLOAT) log10(a)
    254 #define FXSYS_fmod(a, b) (FX_FLOAT) fmod(a, b)
    255 #define FXSYS_abs abs
    256 #define FXDWORD_FROM_LSBFIRST(i) (i)
    257 #define FXDWORD_FROM_MSBFIRST(i)                        \
    258   (((uint8_t)(i) << 24) | ((uint8_t)((i) >> 8) << 16) | \
    259    ((uint8_t)((i) >> 16) << 8) | (uint8_t)((i) >> 24))
    260 #define FXDWORD_GET_LSBFIRST(p) \
    261   ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0]))
    262 #define FXDWORD_GET_MSBFIRST(p) \
    263   ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]))
    264 #define FXSYS_HIBYTE(word) ((uint8_t)((word) >> 8))
    265 #define FXSYS_LOBYTE(word) ((uint8_t)(word))
    266 #define FXSYS_HIWORD(dword) ((FX_WORD)((dword) >> 16))
    267 #define FXSYS_LOWORD(dword) ((FX_WORD)(dword))
    268 int32_t FXSYS_atoi(const FX_CHAR* str);
    269 int32_t FXSYS_wtoi(const FX_WCHAR* str);
    270 int64_t FXSYS_atoi64(const FX_CHAR* str);
    271 int64_t FXSYS_wtoi64(const FX_WCHAR* str);
    272 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix);
    273 int FXSYS_round(FX_FLOAT f);
    274 #define FXSYS_Mul(a, b) ((a) * (b))
    275 #define FXSYS_Div(a, b) ((a) / (b))
    276 #define FXSYS_MulDiv(a, b, c) ((a) * (b) / (c))
    277 #define FXSYS_sqrt2(a, b) (FX_FLOAT) FXSYS_sqrt((a) * (a) + (b) * (b))
    278 #ifdef __cplusplus
    279 };
    280 #endif
    281 
    282 // To print a size_t value in a portable way:
    283 //   size_t size;
    284 //   printf("xyz: %" PRIuS, size);
    285 // The "u" in the macro corresponds to %u, and S is for "size".
    286 
    287 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
    288 
    289 #if (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
    290 #error "inttypes.h has already been included before this header file, but "
    291 #error "without __STDC_FORMAT_MACROS defined."
    292 #endif
    293 
    294 #if !defined(__STDC_FORMAT_MACROS)
    295 #define __STDC_FORMAT_MACROS
    296 #endif
    297 
    298 #include <inttypes.h>
    299 
    300 #if !defined(PRIuS)
    301 #define PRIuS "zu"
    302 #endif
    303 
    304 #else  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
    305 
    306 #if !defined(PRIuS)
    307 #define PRIuS "Iu"
    308 #endif
    309 
    310 #endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
    311 
    312 // Prevent a function from ever being inlined, typically because we'd
    313 // like it to appear in stack traces.
    314 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    315 #define NEVER_INLINE __declspec(noinline)
    316 #else  // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    317 #define NEVER_INLINE __attribute__((__noinline__))
    318 #endif  // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    319 
    320 #endif  // CORE_INCLUDE_FXCRT_FX_SYSTEM_H_
    321