OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/fxcrt/fx_ext.h" | 7 #include "../../include/fxcrt/fx_ext.h" |
8 #include "extension.h" | 8 #include "extension.h" |
9 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 9 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
10 #include <wincrypt.h> | 10 #include <wincrypt.h> |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 extern "C" { | 144 extern "C" { |
145 #endif | 145 #endif |
146 FX_FLOAT FXSYS_tan(FX_FLOAT a) | 146 FX_FLOAT FXSYS_tan(FX_FLOAT a) |
147 { | 147 { |
148 return (FX_FLOAT)tan(a); | 148 return (FX_FLOAT)tan(a); |
149 } | 149 } |
150 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) | 150 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) |
151 { | 151 { |
152 return FXSYS_log(x) / FXSYS_log(b); | 152 return FXSYS_log(x) / FXSYS_log(b); |
153 } | 153 } |
154 FX_FLOAT FXSYS_strtof(FX_LPCSTR pcsStr, FX_INT32 iLength, FX_INT32 *pUsedLen) | 154 FX_FLOAT FXSYS_strtof(FX_LPCSTR pcsStr, int32_t iLength, int32_t *pUsedLen) |
155 { | 155 { |
156 FXSYS_assert(pcsStr != NULL); | 156 FXSYS_assert(pcsStr != NULL); |
157 if (iLength < 0) { | 157 if (iLength < 0) { |
158 iLength = (FX_INT32)FXSYS_strlen(pcsStr); | 158 iLength = (int32_t)FXSYS_strlen(pcsStr); |
159 } | 159 } |
160 CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); | 160 CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); |
161 return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); | 161 return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); |
162 } | 162 } |
163 FX_FLOAT FXSYS_wcstof(FX_LPCWSTR pwsStr, FX_INT32 iLength, FX_INT32 *pUsedLen) | 163 FX_FLOAT FXSYS_wcstof(FX_LPCWSTR pwsStr, int32_t iLength, int32_t *pUsedLen) |
164 { | 164 { |
165 FXSYS_assert(pwsStr != NULL); | 165 FXSYS_assert(pwsStr != NULL); |
166 if (iLength < 0) { | 166 if (iLength < 0) { |
167 iLength = (FX_INT32)FXSYS_wcslen(pwsStr); | 167 iLength = (int32_t)FXSYS_wcslen(pwsStr); |
168 } | 168 } |
169 if (iLength == 0) { | 169 if (iLength == 0) { |
170 return 0.0f; | 170 return 0.0f; |
171 } | 171 } |
172 FX_INT32 iUsedLen = 0; | 172 int32_t iUsedLen = 0; |
173 FX_BOOL bNegtive = FALSE; | 173 FX_BOOL bNegtive = FALSE; |
174 switch (pwsStr[iUsedLen]) { | 174 switch (pwsStr[iUsedLen]) { |
175 case '-': | 175 case '-': |
176 bNegtive = TRUE; | 176 bNegtive = TRUE; |
177 case '+': | 177 case '+': |
178 iUsedLen++; | 178 iUsedLen++; |
179 break; | 179 break; |
180 } | 180 } |
181 FX_FLOAT fValue = 0.0f; | 181 FX_FLOAT fValue = 0.0f; |
182 while (iUsedLen < iLength) { | 182 while (iUsedLen < iLength) { |
(...skipping 24 matching lines...) Expand all Loading... |
207 } | 207 } |
208 FX_LPWSTR FXSYS_wcsncpy(FX_LPWSTR dstStr, FX_LPCWSTR srcStr, size_t count) | 208 FX_LPWSTR FXSYS_wcsncpy(FX_LPWSTR dstStr, FX_LPCWSTR srcStr, size_t count) |
209 { | 209 { |
210 FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); | 210 FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); |
211 for (size_t i = 0; i < count; ++i) | 211 for (size_t i = 0; i < count; ++i) |
212 if ((dstStr[i] = srcStr[i]) == L'\0') { | 212 if ((dstStr[i] = srcStr[i]) == L'\0') { |
213 break; | 213 break; |
214 } | 214 } |
215 return dstStr; | 215 return dstStr; |
216 } | 216 } |
217 FX_INT32 FXSYS_wcsnicmp(FX_LPCWSTR s1, FX_LPCWSTR s2, size_t count) | 217 int32_t FXSYS_wcsnicmp(FX_LPCWSTR s1, FX_LPCWSTR s2, size_t count) |
218 { | 218 { |
219 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); | 219 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); |
220 FX_WCHAR wch1 = 0, wch2 = 0; | 220 FX_WCHAR wch1 = 0, wch2 = 0; |
221 while (count-- > 0) { | 221 while (count-- > 0) { |
222 wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); | 222 wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); |
223 wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); | 223 wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); |
224 if (wch1 != wch2) { | 224 if (wch1 != wch2) { |
225 break; | 225 break; |
226 } | 226 } |
227 } | 227 } |
228 return wch1 - wch2; | 228 return wch1 - wch2; |
229 } | 229 } |
230 FX_INT32 FXSYS_strnicmp(FX_LPCSTR s1, FX_LPCSTR s2, size_t count) | 230 int32_t FXSYS_strnicmp(FX_LPCSTR s1, FX_LPCSTR s2, size_t count) |
231 { | 231 { |
232 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); | 232 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); |
233 FX_CHAR ch1 = 0, ch2 = 0; | 233 FX_CHAR ch1 = 0, ch2 = 0; |
234 while (count-- > 0) { | 234 while (count-- > 0) { |
235 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); | 235 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); |
236 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); | 236 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); |
237 if (ch1 != ch2) { | 237 if (ch1 != ch2) { |
238 break; | 238 break; |
239 } | 239 } |
240 } | 240 } |
241 return ch1 - ch2; | 241 return ch1 - ch2; |
242 } | 242 } |
243 FX_DWORD FX_HashCode_String_GetA(FX_LPCSTR pStr, FX_INT32 iLength, FX_BOOL bIgno
reCase) | 243 FX_DWORD FX_HashCode_String_GetA(FX_LPCSTR pStr, int32_t iLength, FX_BOOL bIgnor
eCase) |
244 { | 244 { |
245 FXSYS_assert(pStr != NULL); | 245 FXSYS_assert(pStr != NULL); |
246 if (iLength < 0) { | 246 if (iLength < 0) { |
247 iLength = (FX_INT32)FXSYS_strlen(pStr); | 247 iLength = (int32_t)FXSYS_strlen(pStr); |
248 } | 248 } |
249 FX_LPCSTR pStrEnd = pStr + iLength; | 249 FX_LPCSTR pStrEnd = pStr + iLength; |
250 FX_DWORD dwHashCode = 0; | 250 FX_DWORD dwHashCode = 0; |
251 if (bIgnoreCase) { | 251 if (bIgnoreCase) { |
252 while (pStr < pStrEnd) { | 252 while (pStr < pStrEnd) { |
253 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); | 253 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); |
254 } | 254 } |
255 } else { | 255 } else { |
256 while (pStr < pStrEnd) { | 256 while (pStr < pStrEnd) { |
257 dwHashCode = 31 * dwHashCode + *pStr ++; | 257 dwHashCode = 31 * dwHashCode + *pStr ++; |
258 } | 258 } |
259 } | 259 } |
260 return dwHashCode; | 260 return dwHashCode; |
261 } | 261 } |
262 FX_DWORD FX_HashCode_String_GetW(FX_LPCWSTR pStr, FX_INT32 iLength, FX_BOOL bIgn
oreCase) | 262 FX_DWORD FX_HashCode_String_GetW(FX_LPCWSTR pStr, int32_t iLength, FX_BOOL bIgno
reCase) |
263 { | 263 { |
264 FXSYS_assert(pStr != NULL); | 264 FXSYS_assert(pStr != NULL); |
265 if (iLength < 0) { | 265 if (iLength < 0) { |
266 iLength = (FX_INT32)FXSYS_wcslen(pStr); | 266 iLength = (int32_t)FXSYS_wcslen(pStr); |
267 } | 267 } |
268 FX_LPCWSTR pStrEnd = pStr + iLength; | 268 FX_LPCWSTR pStrEnd = pStr + iLength; |
269 FX_DWORD dwHashCode = 0; | 269 FX_DWORD dwHashCode = 0; |
270 if (bIgnoreCase) { | 270 if (bIgnoreCase) { |
271 while (pStr < pStrEnd) { | 271 while (pStr < pStrEnd) { |
272 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); | 272 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); |
273 } | 273 } |
274 } else { | 274 } else { |
275 while (pStr < pStrEnd) { | 275 while (pStr < pStrEnd) { |
276 dwHashCode = 1313 * dwHashCode + *pStr ++; | 276 dwHashCode = 1313 * dwHashCode + *pStr ++; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 v ^= (v << 7) & 0x9d2c5680UL; | 326 v ^= (v << 7) & 0x9d2c5680UL; |
327 v ^= (v << 15) & 0xefc60000UL; | 327 v ^= (v << 15) & 0xefc60000UL; |
328 v ^= (v >> 18); | 328 v ^= (v >> 18); |
329 return v; | 329 return v; |
330 } | 330 } |
331 void FX_Random_MT_Close(FX_LPVOID pContext) | 331 void FX_Random_MT_Close(FX_LPVOID pContext) |
332 { | 332 { |
333 FXSYS_assert(pContext != NULL); | 333 FXSYS_assert(pContext != NULL); |
334 FX_Free(pContext); | 334 FX_Free(pContext); |
335 } | 335 } |
336 void FX_Random_GenerateMT(FX_LPDWORD pBuffer, FX_INT32 iCount) | 336 void FX_Random_GenerateMT(FX_LPDWORD pBuffer, int32_t iCount) |
337 { | 337 { |
338 FX_DWORD dwSeed; | 338 FX_DWORD dwSeed; |
339 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 339 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
340 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { | 340 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { |
341 FX_Random_GenerateBase(&dwSeed, 1); | 341 FX_Random_GenerateBase(&dwSeed, 1); |
342 } | 342 } |
343 #else | 343 #else |
344 FX_Random_GenerateBase(&dwSeed, 1); | 344 FX_Random_GenerateBase(&dwSeed, 1); |
345 #endif | 345 #endif |
346 FX_LPVOID pContext = FX_Random_MT_Start(dwSeed); | 346 FX_LPVOID pContext = FX_Random_MT_Start(dwSeed); |
347 while (iCount -- > 0) { | 347 while (iCount -- > 0) { |
348 *pBuffer ++ = FX_Random_MT_Generate(pContext); | 348 *pBuffer ++ = FX_Random_MT_Generate(pContext); |
349 } | 349 } |
350 FX_Random_MT_Close(pContext); | 350 FX_Random_MT_Close(pContext); |
351 } | 351 } |
352 void FX_Random_GenerateBase(FX_LPDWORD pBuffer, FX_INT32 iCount) | 352 void FX_Random_GenerateBase(FX_LPDWORD pBuffer, int32_t iCount) |
353 { | 353 { |
354 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 354 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
355 SYSTEMTIME st1, st2; | 355 SYSTEMTIME st1, st2; |
356 ::GetSystemTime(&st1); | 356 ::GetSystemTime(&st1); |
357 do { | 357 do { |
358 ::GetSystemTime(&st2); | 358 ::GetSystemTime(&st2); |
359 } while (FXSYS_memcmp32(&st1, &st2, sizeof(SYSTEMTIME)) == 0); | 359 } while (FXSYS_memcmp32(&st1, &st2, sizeof(SYSTEMTIME)) == 0); |
360 FX_DWORD dwHash1 = FX_HashCode_String_GetA((FX_LPCSTR)&st1, sizeof(st1), TRU
E); | 360 FX_DWORD dwHash1 = FX_HashCode_String_GetA((FX_LPCSTR)&st1, sizeof(st1), TRU
E); |
361 FX_DWORD dwHash2 = FX_HashCode_String_GetA((FX_LPCSTR)&st2, sizeof(st2), TRU
E); | 361 FX_DWORD dwHash2 = FX_HashCode_String_GetA((FX_LPCSTR)&st2, sizeof(st2), TRU
E); |
362 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); | 362 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); |
363 #else | 363 #else |
364 time_t tmLast = time(NULL), tmCur; | 364 time_t tmLast = time(NULL), tmCur; |
365 while ((tmCur = time(NULL)) == tmLast); | 365 while ((tmCur = time(NULL)) == tmLast); |
366 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); | 366 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); |
367 #endif | 367 #endif |
368 while (iCount -- > 0) { | 368 while (iCount -- > 0) { |
369 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); | 369 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); |
370 } | 370 } |
371 } | 371 } |
372 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 372 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
373 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount) | 373 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, int32_t iCount) |
374 { | 374 { |
375 HCRYPTPROV hCP = NULL; | 375 HCRYPTPROV hCP = NULL; |
376 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { | 376 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { |
377 return FALSE; | 377 return FALSE; |
378 } | 378 } |
379 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (FX_LPBYTE)pBuffer); | 379 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (FX_LPBYTE)pBuffer); |
380 ::CryptReleaseContext(hCP, 0); | 380 ::CryptReleaseContext(hCP, 0); |
381 return TRUE; | 381 return TRUE; |
382 } | 382 } |
383 #endif | 383 #endif |
384 void FX_Random_GenerateCrypto(FX_LPDWORD pBuffer, FX_INT32 iCount) | 384 void FX_Random_GenerateCrypto(FX_LPDWORD pBuffer, int32_t iCount) |
385 { | 385 { |
386 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 386 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
387 FX_GenerateCryptoRandom(pBuffer, iCount); | 387 FX_GenerateCryptoRandom(pBuffer, iCount); |
388 #else | 388 #else |
389 FX_Random_GenerateBase(pBuffer, iCount); | 389 FX_Random_GenerateBase(pBuffer, iCount); |
390 #endif | 390 #endif |
391 } | 391 } |
392 #ifdef __cplusplus | 392 #ifdef __cplusplus |
393 } | 393 } |
394 #endif | 394 #endif |
395 #ifdef __cplusplus | 395 #ifdef __cplusplus |
396 extern "C" { | 396 extern "C" { |
397 #endif | 397 #endif |
398 void FX_GUID_CreateV4(FX_LPGUID pGUID) | 398 void FX_GUID_CreateV4(FX_LPGUID pGUID) |
399 { | 399 { |
400 #if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ ==
_FX_WIN64_) | 400 #if (_FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ ==
_FX_WIN64_) |
401 #ifdef _FX_WINAPI_PARTITION_DESKTOP_ | 401 #ifdef _FX_WINAPI_PARTITION_DESKTOP_ |
402 if (!FX_GenerateCryptoRandom((FX_LPDWORD)pGUID, 4)) { | 402 if (!FX_GenerateCryptoRandom((FX_LPDWORD)pGUID, 4)) { |
403 FX_Random_GenerateMT((FX_LPDWORD)pGUID, 4); | 403 FX_Random_GenerateMT((FX_LPDWORD)pGUID, 4); |
404 } | 404 } |
405 #else | 405 #else |
406 FX_Random_GenerateMT((FX_LPDWORD)pGUID, 4); | 406 FX_Random_GenerateMT((FX_LPDWORD)pGUID, 4); |
407 #endif | 407 #endif |
408 #else | 408 #else |
409 FX_Random_GenerateMT((FX_LPDWORD)pGUID, 4); | 409 FX_Random_GenerateMT((FX_LPDWORD)pGUID, 4); |
410 #endif | 410 #endif |
411 FX_BYTE &b = ((FX_LPBYTE)pGUID)[6]; | 411 uint8_t &b = ((FX_LPBYTE)pGUID)[6]; |
412 b = (b & 0x0F) | 0x40; | 412 b = (b & 0x0F) | 0x40; |
413 } | 413 } |
414 FX_LPCSTR gs_FX_pHexChars = "0123456789ABCDEF"; | 414 FX_LPCSTR gs_FX_pHexChars = "0123456789ABCDEF"; |
415 void FX_GUID_ToString(FX_LPCGUID pGUID, CFX_ByteString &bsStr, FX_BOOL bSeparato
r) | 415 void FX_GUID_ToString(FX_LPCGUID pGUID, CFX_ByteString &bsStr, FX_BOOL bSeparato
r) |
416 { | 416 { |
417 FX_LPSTR pBuf = bsStr.GetBuffer(40); | 417 FX_LPSTR pBuf = bsStr.GetBuffer(40); |
418 FX_BYTE b; | 418 uint8_t b; |
419 for (FX_INT32 i = 0; i < 16; i ++) { | 419 for (int32_t i = 0; i < 16; i ++) { |
420 b = ((FX_LPCBYTE)pGUID)[i]; | 420 b = ((FX_LPCBYTE)pGUID)[i]; |
421 *pBuf ++ = gs_FX_pHexChars[b >> 4]; | 421 *pBuf ++ = gs_FX_pHexChars[b >> 4]; |
422 *pBuf ++ = gs_FX_pHexChars[b & 0x0F]; | 422 *pBuf ++ = gs_FX_pHexChars[b & 0x0F]; |
423 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 423 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
424 *pBuf ++ = L'-'; | 424 *pBuf ++ = L'-'; |
425 } | 425 } |
426 } | 426 } |
427 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 427 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
428 } | 428 } |
429 #ifdef __cplusplus | 429 #ifdef __cplusplus |
430 } | 430 } |
431 #endif | 431 #endif |
OLD | NEW |