1 // Copyright 2014 The PDFium Authors
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 <stddef.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <wchar.h>
16
17 #include "build/build_config.h"
18 #include "core/fxcrt/fx_types.h"
19
20 #if defined(_MSC_VER) && _MSC_VER < 1900
21 #error Sorry, VC++ 2015 or later is required to compile PDFium.
22 #endif // defined(_MSC_VER) && _MSC_VER < 1900
23
24 #if defined(__wasm__) && defined(PDF_ENABLE_V8)
25 #error Cannot compile v8 with wasm.
26 #endif // PDF_ENABLE_V8
27
28 #if BUILDFLAG(IS_WIN)
29 #include <windows.h>
30 #endif // BUILDFLAG(IS_WIN)
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif // __cplusplus
35
36 #define FXSYS_IsFloatZero(f) ((f) < 0.0001 && (f) > -0.0001)
37 #define FXSYS_IsFloatBigger(fa, fb) \
38 ((fa) > (fb) && !FXSYS_IsFloatZero((fa) - (fb)))
39 #define FXSYS_IsFloatSmaller(fa, fb) \
40 ((fa) < (fb) && !FXSYS_IsFloatZero((fa) - (fb)))
41 #define FXSYS_IsFloatEqual(fa, fb) FXSYS_IsFloatZero((fa) - (fb))
42
43 // M_PI not universally present on all platforms.
44 #define FXSYS_PI 3.1415926535897932384626433832795f
45 #define FXSYS_BEZIER 0.5522847498308f
46
47 // NOTE: prevent use of the return value from snprintf() since some platforms
48 // have different return values.
49 #define FXSYS_snprintf (void)snprintf
50 #define FXSYS_vsnprintf (void)vsnprintf
51 #define FXSYS_sprintf DO_NOT_USE_SPRINTF_DIE_DIE_DIE
52 #define FXSYS_vsprintf DO_NOT_USE_VSPRINTF_DIE_DIE_DIE
53
54 #if BUILDFLAG(IS_WIN)
55 #define FXSYS_itoa _itoa
56 #define FXSYS_strlwr _strlwr
57 #define FXSYS_strupr _strupr
58 #define FXSYS_stricmp _stricmp
59 #define FXSYS_wcsicmp _wcsicmp
60 #define FXSYS_wcslwr _wcslwr
61 #define FXSYS_wcsupr _wcsupr
62 size_t FXSYS_wcsftime(wchar_t* strDest,
63 size_t maxsize,
64 const wchar_t* format,
65 const struct tm* timeptr);
66 #define FXSYS_SetLastError SetLastError
67 #define FXSYS_GetLastError GetLastError
68 #else // BUILDFLAG(IS_WIN)
69 char* FXSYS_itoa(int value, char* str, int radix);
70 char* FXSYS_strlwr(char* str);
71 char* FXSYS_strupr(char* str);
72 int FXSYS_stricmp(const char* str1, const char* str2);
73 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2);
74 wchar_t* FXSYS_wcslwr(wchar_t* str);
75 wchar_t* FXSYS_wcsupr(wchar_t* str);
76 #define FXSYS_wcsftime wcsftime
77 void FXSYS_SetLastError(uint32_t err);
78 uint32_t FXSYS_GetLastError();
79 #endif // BUILDFLAG(IS_WIN)
80
81 int32_t FXSYS_atoi(const char* str);
82 uint32_t FXSYS_atoui(const char* str);
83 int32_t FXSYS_wtoi(const wchar_t* str);
84 int64_t FXSYS_atoi64(const char* str);
85 const char* FXSYS_i64toa(int64_t value, char* str, int radix);
86 int FXSYS_roundf(float f);
87 int FXSYS_round(double d);
88 float FXSYS_sqrt2(float a, float b);
89
90 #ifdef __cplusplus
91 } // extern "C"
92
93 // C++-only section
94
95 // Overloaded functions for C++ templates
FXSYS_len(const char * ptr)96 inline size_t FXSYS_len(const char* ptr) {
97 return strlen(ptr);
98 }
99
FXSYS_len(const wchar_t * ptr)100 inline size_t FXSYS_len(const wchar_t* ptr) {
101 return wcslen(ptr);
102 }
103
FXSYS_cmp(const char * ptr1,const char * ptr2,size_t len)104 inline int FXSYS_cmp(const char* ptr1, const char* ptr2, size_t len) {
105 return memcmp(ptr1, ptr2, len);
106 }
107
FXSYS_cmp(const wchar_t * ptr1,const wchar_t * ptr2,size_t len)108 inline int FXSYS_cmp(const wchar_t* ptr1, const wchar_t* ptr2, size_t len) {
109 return wmemcmp(ptr1, ptr2, len);
110 }
111
FXSYS_chr(const char * ptr,char ch,size_t len)112 inline const char* FXSYS_chr(const char* ptr, char ch, size_t len) {
113 return reinterpret_cast<const char*>(memchr(ptr, ch, len));
114 }
115
FXSYS_chr(const wchar_t * ptr,wchar_t ch,size_t len)116 inline const wchar_t* FXSYS_chr(const wchar_t* ptr, wchar_t ch, size_t len) {
117 return wmemchr(ptr, ch, len);
118 }
119
120 // Could be C, but uses C++-style casting.
121 #define FXSYS_UINT16_GET_LSBFIRST(p) \
122 (static_cast<uint16_t>( \
123 (static_cast<uint32_t>(static_cast<uint8_t>((p)[1])) << 8) | \
124 (static_cast<uint32_t>(static_cast<uint8_t>((p)[0])))))
125 #define FXSYS_UINT16_GET_MSBFIRST(p) \
126 (static_cast<uint16_t>( \
127 (static_cast<uint32_t>(static_cast<uint8_t>((p)[0])) << 8) | \
128 (static_cast<uint32_t>(static_cast<uint8_t>((p)[1])))))
129 #define FXSYS_UINT32_GET_LSBFIRST(p) \
130 ((static_cast<uint32_t>(static_cast<uint8_t>((p)[3])) << 24) | \
131 (static_cast<uint32_t>(static_cast<uint8_t>((p)[2])) << 16) | \
132 (static_cast<uint32_t>(static_cast<uint8_t>((p)[1])) << 8) | \
133 (static_cast<uint32_t>(static_cast<uint8_t>((p)[0]))))
134 #define FXSYS_UINT32_GET_MSBFIRST(p) \
135 ((static_cast<uint32_t>(static_cast<uint8_t>((p)[0])) << 24) | \
136 (static_cast<uint32_t>(static_cast<uint8_t>((p)[1])) << 16) | \
137 (static_cast<uint32_t>(static_cast<uint8_t>((p)[2])) << 8) | \
138 (static_cast<uint32_t>(static_cast<uint8_t>((p)[3]))))
139 #endif // __cplusplus
140
141 #endif // CORE_FXCRT_FX_SYSTEM_H_
142