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