1 // Copyright 2017 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 #include "core/fxcrt/fx_system.h"
8
9 #include <math.h>
10
11 #include <limits>
12
13 #include "build/build_config.h"
14 #include "core/fxcrt/compiler_specific.h"
15 #include "core/fxcrt/fx_extension.h"
16
17 namespace {
18
19 #if !BUILDFLAG(IS_WIN)
20 uint32_t g_last_error = 0;
21 #endif
22
23 template <typename IntType, typename CharType>
FXSYS_StrToInt(const CharType * str)24 IntType FXSYS_StrToInt(const CharType* str) {
25 if (!str)
26 return 0;
27
28 // Process the sign.
29 bool neg = *str == '-';
30 if (neg || *str == '+') {
31 // SAFETY: `str` points at the start of the string, which is a character or
32 // a terminating NUL. `*str` is non-NUL from the condition above, so `str`
33 // is pointing inside the string. Afterward, `str` may be pointing at the
34 // terminating NUL.
35 UNSAFE_BUFFERS(str++);
36 }
37
38 IntType num = 0;
39 while (*str && FXSYS_IsDecimalDigit(*str)) {
40 IntType val = FXSYS_DecimalCharToInt(*str);
41 if (num > (std::numeric_limits<IntType>::max() - val) / 10) {
42 if (neg && std::numeric_limits<IntType>::is_signed) {
43 // Return MIN when the represented number is signed type and is smaller
44 // than the min value.
45 return std::numeric_limits<IntType>::min();
46 }
47 // Return MAX when the represented number is signed type and is larger
48 // than the max value, or the number is unsigned type and out of range.
49 return std::numeric_limits<IntType>::max();
50 }
51 num = num * 10 + val;
52
53 // SAFETY: The loop terminates if `str` is ever pointing at the terminating
54 // NUL. `str` is only moved by one character at a time, so inside the loop
55 // `str` always points inside the string.
56 UNSAFE_BUFFERS(str++);
57 }
58 // When it is a negative value, -num should be returned. Since num may be of
59 // unsigned type, use ~num + 1 to avoid the warning of applying unary minus
60 // operator to unsigned type.
61 return neg ? ~num + 1 : num;
62 }
63
64 template <typename T, typename UT, typename STR_T>
FXSYS_IntToStr(T value,STR_T str,int radix)65 STR_T FXSYS_IntToStr(T value, STR_T str, int radix) {
66 if (radix < 2 || radix > 16) {
67 str[0] = 0;
68 return str;
69 }
70 if (value == 0) {
71 str[0] = '0';
72 UNSAFE_TODO(str[1]) = 0;
73 return str;
74 }
75 int i = 0;
76 UT uvalue;
77 if (value < 0) {
78 UNSAFE_TODO(str[i++]) = '-';
79 // Standard trick to avoid undefined behaviour when negating INT_MIN.
80 uvalue = static_cast<UT>(-(value + 1)) + 1;
81 } else {
82 uvalue = value;
83 }
84 int digits = 1;
85 T order = uvalue / radix;
86 while (order > 0) {
87 digits++;
88 order = order / radix;
89 }
90 for (int d = digits - 1; d > -1; d--) {
91 UNSAFE_TODO(str[d + i] = "0123456789abcdef"[uvalue % radix]);
92 uvalue /= radix;
93 }
94 UNSAFE_TODO(str[digits + i]) = 0;
95 return str;
96 }
97
98 } // namespace
99
FXSYS_roundf(float f)100 int FXSYS_roundf(float f) {
101 if (isnan(f))
102 return 0;
103 if (f < static_cast<float>(std::numeric_limits<int>::min()))
104 return std::numeric_limits<int>::min();
105 if (f >= static_cast<float>(std::numeric_limits<int>::max()))
106 return std::numeric_limits<int>::max();
107 return static_cast<int>(round(f));
108 }
109
FXSYS_round(double d)110 int FXSYS_round(double d) {
111 if (isnan(d))
112 return 0;
113 if (d < static_cast<double>(std::numeric_limits<int>::min()))
114 return std::numeric_limits<int>::min();
115 if (d >= static_cast<double>(std::numeric_limits<int>::max()))
116 return std::numeric_limits<int>::max();
117 return static_cast<int>(round(d));
118 }
119
FXSYS_atoi(const char * str)120 int32_t FXSYS_atoi(const char* str) {
121 return FXSYS_StrToInt<int32_t, char>(str);
122 }
FXSYS_atoui(const char * str)123 uint32_t FXSYS_atoui(const char* str) {
124 return FXSYS_StrToInt<uint32_t>(str);
125 }
FXSYS_wtoi(const wchar_t * str)126 int32_t FXSYS_wtoi(const wchar_t* str) {
127 return FXSYS_StrToInt<int32_t, wchar_t>(str);
128 }
FXSYS_atoi64(const char * str)129 int64_t FXSYS_atoi64(const char* str) {
130 return FXSYS_StrToInt<int64_t, char>(str);
131 }
FXSYS_i64toa(int64_t value,char * str,int radix)132 const char* FXSYS_i64toa(int64_t value, char* str, int radix) {
133 return FXSYS_IntToStr<int64_t, uint64_t, char*>(value, str, radix);
134 }
135
136 #if BUILDFLAG(IS_WIN)
137
FXSYS_wcsftime(wchar_t * strDest,size_t maxsize,const wchar_t * format,const struct tm * timeptr)138 size_t FXSYS_wcsftime(wchar_t* strDest,
139 size_t maxsize,
140 const wchar_t* format,
141 const struct tm* timeptr) {
142 // Avoid tripping an invalid parameter handler and crashing process.
143 // Note: leap seconds may cause tm_sec == 60.
144 if (timeptr->tm_year < -1900 || timeptr->tm_year > 8099 ||
145 timeptr->tm_mon < 0 || timeptr->tm_mon > 11 || timeptr->tm_mday < 1 ||
146 timeptr->tm_mday > 31 || timeptr->tm_hour < 0 || timeptr->tm_hour > 23 ||
147 timeptr->tm_min < 0 || timeptr->tm_min > 59 || timeptr->tm_sec < 0 ||
148 timeptr->tm_sec > 60 || timeptr->tm_wday < 0 || timeptr->tm_wday > 6 ||
149 timeptr->tm_yday < 0 || timeptr->tm_yday > 365) {
150 strDest[0] = L'\0';
151 return 0;
152 }
153 return wcsftime(strDest, maxsize, format, timeptr);
154 }
155
FXSYS_stricmp(const char * str1,const char * str2)156 int FXSYS_stricmp(const char* str1, const char* str2) {
157 return _stricmp(str1, str2);
158 }
159
FXSYS_wcsicmp(const wchar_t * str1,const wchar_t * str2)160 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2) {
161 return _wcsicmp(str1, str2);
162 }
163
164 #else // BUILDFLAG(IS_WIN)
165
FXSYS_strlwr(char * str)166 char* FXSYS_strlwr(char* str) {
167 if (!str) {
168 return nullptr;
169 }
170 char* s = str;
171 while (*str) {
172 *str = tolower(*str);
173 UNSAFE_BUFFERS(str++); // SAFETY: NUL check in while condition.
174 }
175 return s;
176 }
177
FXSYS_strupr(char * str)178 char* FXSYS_strupr(char* str) {
179 if (!str) {
180 return nullptr;
181 }
182 char* s = str;
183 while (*str) {
184 *str = toupper(*str);
185 UNSAFE_BUFFERS(str++); // SAFETY: NUL check in while condition.
186 }
187 return s;
188 }
189
FXSYS_wcslwr(wchar_t * str)190 wchar_t* FXSYS_wcslwr(wchar_t* str) {
191 if (!str) {
192 return nullptr;
193 }
194 wchar_t* s = str;
195 while (*str) {
196 *str = FXSYS_towlower(*str);
197 UNSAFE_BUFFERS(str++); // SAFETY: NUL check in while condition.
198 }
199 return s;
200 }
201
FXSYS_wcsupr(wchar_t * str)202 wchar_t* FXSYS_wcsupr(wchar_t* str) {
203 if (!str) {
204 return nullptr;
205 }
206 wchar_t* s = str;
207 while (*str) {
208 *str = FXSYS_towupper(*str);
209 UNSAFE_BUFFERS(str++); // SAFETY: NUL check in while condition.
210 }
211 return s;
212 }
213
FXSYS_stricmp(const char * str1,const char * str2)214 int FXSYS_stricmp(const char* str1, const char* str2) {
215 int f;
216 int l;
217 do {
218 f = toupper(*str1);
219 l = toupper(*str2);
220 // SAFETY: The loop breaks when `*str1` is NUL, so `str1` is always inside
221 // its string.
222 UNSAFE_BUFFERS(++str1);
223 // SAFETY: The loop breaks when `*str1` is non-NUL but `*str2` is NUL (as
224 // checked by `f != l`), so `str2` is always inside its string.
225 UNSAFE_BUFFERS(++str2);
226 } while (f && f == l);
227 return f - l;
228 }
229
FXSYS_wcsicmp(const wchar_t * str1,const wchar_t * str2)230 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2) {
231 wchar_t f;
232 wchar_t l;
233 do {
234 f = FXSYS_towupper(*str1);
235 l = FXSYS_towupper(*str2);
236 // SAFETY: The loop breaks when `*str1` is NUL, so `str1` is always inside
237 // its string.
238 UNSAFE_BUFFERS(++str1);
239 // SAFETY: The loop breaks when `*str1` is non-NUL but `*str2` is NUL (as
240 // checked by `f != l`), so `str2` is always inside its string.
241 UNSAFE_BUFFERS(++str2);
242 } while (f && f == l);
243 return f - l;
244 }
245
FXSYS_itoa(int value,char * str,int radix)246 char* FXSYS_itoa(int value, char* str, int radix) {
247 return FXSYS_IntToStr<int32_t, uint32_t, char*>(value, str, radix);
248 }
249
FXSYS_SetLastError(uint32_t err)250 void FXSYS_SetLastError(uint32_t err) {
251 g_last_error = err;
252 }
253
FXSYS_GetLastError()254 uint32_t FXSYS_GetLastError() {
255 return g_last_error;
256 }
257 #endif // BUILDFLAG(IS_WIN)
258
FXSYS_sqrt2(float a,float b)259 float FXSYS_sqrt2(float a, float b) {
260 return sqrtf(a * a + b * b);
261 }
262