| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 extern "C" { | 132 extern "C" { |
| 133 #endif | 133 #endif |
| 134 FX_FLOAT FXSYS_tan(FX_FLOAT a) | 134 FX_FLOAT FXSYS_tan(FX_FLOAT a) |
| 135 { | 135 { |
| 136 return (FX_FLOAT)tan(a); | 136 return (FX_FLOAT)tan(a); |
| 137 } | 137 } |
| 138 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) | 138 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) |
| 139 { | 139 { |
| 140 return FXSYS_log(x) / FXSYS_log(b); | 140 return FXSYS_log(x) / FXSYS_log(b); |
| 141 } | 141 } |
| 142 FX_FLOAT FXSYS_strtof(FX_LPCSTR pcsStr, FX_INT32 iLength, FX_INT32 *pUsedLen) | 142 FX_FLOAT FXSYS_strtof(FX_LPCSTR pcsStr, int32_t iLength, int32_t *pUsedLen) |
| 143 { | 143 { |
| 144 FXSYS_assert(pcsStr != NULL); | 144 FXSYS_assert(pcsStr != NULL); |
| 145 if (iLength < 0) { | 145 if (iLength < 0) { |
| 146 iLength = (FX_INT32)FXSYS_strlen(pcsStr); | 146 iLength = (int32_t)FXSYS_strlen(pcsStr); |
| 147 } | 147 } |
| 148 CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); | 148 CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); |
| 149 return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); | 149 return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); |
| 150 } | 150 } |
| 151 FX_FLOAT FXSYS_wcstof(FX_LPCWSTR pwsStr, FX_INT32 iLength, FX_INT32 *pUsedLen) | 151 FX_FLOAT FXSYS_wcstof(FX_LPCWSTR pwsStr, int32_t iLength, int32_t *pUsedLen) |
| 152 { | 152 { |
| 153 FXSYS_assert(pwsStr != NULL); | 153 FXSYS_assert(pwsStr != NULL); |
| 154 if (iLength < 0) { | 154 if (iLength < 0) { |
| 155 iLength = (FX_INT32)FXSYS_wcslen(pwsStr); | 155 iLength = (int32_t)FXSYS_wcslen(pwsStr); |
| 156 } | 156 } |
| 157 if (iLength == 0) { | 157 if (iLength == 0) { |
| 158 return 0.0f; | 158 return 0.0f; |
| 159 } | 159 } |
| 160 FX_INT32 iUsedLen = 0; | 160 int32_t iUsedLen = 0; |
| 161 FX_BOOL bNegtive = FALSE; | 161 FX_BOOL bNegtive = FALSE; |
| 162 switch (pwsStr[iUsedLen]) { | 162 switch (pwsStr[iUsedLen]) { |
| 163 case '-': | 163 case '-': |
| 164 bNegtive = TRUE; | 164 bNegtive = TRUE; |
| 165 case '+': | 165 case '+': |
| 166 iUsedLen++; | 166 iUsedLen++; |
| 167 break; | 167 break; |
| 168 } | 168 } |
| 169 FX_FLOAT fValue = 0.0f; | 169 FX_FLOAT fValue = 0.0f; |
| 170 while (iUsedLen < iLength) { | 170 while (iUsedLen < iLength) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 195 } | 195 } |
| 196 FX_LPWSTR FXSYS_wcsncpy(FX_LPWSTR dstStr, FX_LPCWSTR srcStr, size_t count) | 196 FX_LPWSTR FXSYS_wcsncpy(FX_LPWSTR dstStr, FX_LPCWSTR srcStr, size_t count) |
| 197 { | 197 { |
| 198 FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); | 198 FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); |
| 199 for (size_t i = 0; i < count; ++i) | 199 for (size_t i = 0; i < count; ++i) |
| 200 if ((dstStr[i] = srcStr[i]) == L'\0') { | 200 if ((dstStr[i] = srcStr[i]) == L'\0') { |
| 201 break; | 201 break; |
| 202 } | 202 } |
| 203 return dstStr; | 203 return dstStr; |
| 204 } | 204 } |
| 205 FX_INT32 FXSYS_wcsnicmp(FX_LPCWSTR s1, FX_LPCWSTR s2, size_t count) | 205 int32_t FXSYS_wcsnicmp(FX_LPCWSTR s1, FX_LPCWSTR s2, size_t count) |
| 206 { | 206 { |
| 207 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); | 207 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); |
| 208 FX_WCHAR wch1 = 0, wch2 = 0; | 208 FX_WCHAR wch1 = 0, wch2 = 0; |
| 209 while (count-- > 0) { | 209 while (count-- > 0) { |
| 210 wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); | 210 wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); |
| 211 wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); | 211 wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); |
| 212 if (wch1 != wch2) { | 212 if (wch1 != wch2) { |
| 213 break; | 213 break; |
| 214 } | 214 } |
| 215 } | 215 } |
| 216 return wch1 - wch2; | 216 return wch1 - wch2; |
| 217 } | 217 } |
| 218 FX_INT32 FXSYS_strnicmp(FX_LPCSTR s1, FX_LPCSTR s2, size_t count) | 218 int32_t FXSYS_strnicmp(FX_LPCSTR s1, FX_LPCSTR s2, size_t count) |
| 219 { | 219 { |
| 220 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); | 220 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); |
| 221 FX_CHAR ch1 = 0, ch2 = 0; | 221 FX_CHAR ch1 = 0, ch2 = 0; |
| 222 while (count-- > 0) { | 222 while (count-- > 0) { |
| 223 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); | 223 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); |
| 224 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); | 224 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); |
| 225 if (ch1 != ch2) { | 225 if (ch1 != ch2) { |
| 226 break; | 226 break; |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 return ch1 - ch2; | 229 return ch1 - ch2; |
| 230 } | 230 } |
| 231 FX_DWORD FX_HashCode_String_GetA(FX_LPCSTR pStr, FX_INT32 iLength, FX_BOOL bIgno
reCase) | 231 FX_DWORD FX_HashCode_String_GetA(FX_LPCSTR pStr, int32_t iLength, FX_BOOL bIgnor
eCase) |
| 232 { | 232 { |
| 233 FXSYS_assert(pStr != NULL); | 233 FXSYS_assert(pStr != NULL); |
| 234 if (iLength < 0) { | 234 if (iLength < 0) { |
| 235 iLength = (FX_INT32)FXSYS_strlen(pStr); | 235 iLength = (int32_t)FXSYS_strlen(pStr); |
| 236 } | 236 } |
| 237 FX_LPCSTR pStrEnd = pStr + iLength; | 237 FX_LPCSTR pStrEnd = pStr + iLength; |
| 238 FX_DWORD dwHashCode = 0; | 238 FX_DWORD dwHashCode = 0; |
| 239 if (bIgnoreCase) { | 239 if (bIgnoreCase) { |
| 240 while (pStr < pStrEnd) { | 240 while (pStr < pStrEnd) { |
| 241 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); | 241 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); |
| 242 } | 242 } |
| 243 } else { | 243 } else { |
| 244 while (pStr < pStrEnd) { | 244 while (pStr < pStrEnd) { |
| 245 dwHashCode = 31 * dwHashCode + *pStr ++; | 245 dwHashCode = 31 * dwHashCode + *pStr ++; |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 return dwHashCode; | 248 return dwHashCode; |
| 249 } | 249 } |
| 250 FX_DWORD FX_HashCode_String_GetW(FX_LPCWSTR pStr, FX_INT32 iLength, FX_BOOL bIgn
oreCase) | 250 FX_DWORD FX_HashCode_String_GetW(FX_LPCWSTR pStr, int32_t iLength, FX_BOOL bIgno
reCase) |
| 251 { | 251 { |
| 252 FXSYS_assert(pStr != NULL); | 252 FXSYS_assert(pStr != NULL); |
| 253 if (iLength < 0) { | 253 if (iLength < 0) { |
| 254 iLength = (FX_INT32)FXSYS_wcslen(pStr); | 254 iLength = (int32_t)FXSYS_wcslen(pStr); |
| 255 } | 255 } |
| 256 FX_LPCWSTR pStrEnd = pStr + iLength; | 256 FX_LPCWSTR pStrEnd = pStr + iLength; |
| 257 FX_DWORD dwHashCode = 0; | 257 FX_DWORD dwHashCode = 0; |
| 258 if (bIgnoreCase) { | 258 if (bIgnoreCase) { |
| 259 while (pStr < pStrEnd) { | 259 while (pStr < pStrEnd) { |
| 260 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); | 260 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); |
| 261 } | 261 } |
| 262 } else { | 262 } else { |
| 263 while (pStr < pStrEnd) { | 263 while (pStr < pStrEnd) { |
| 264 dwHashCode = 1313 * dwHashCode + *pStr ++; | 264 dwHashCode = 1313 * dwHashCode + *pStr ++; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 v ^= (v << 7) & 0x9d2c5680UL; | 314 v ^= (v << 7) & 0x9d2c5680UL; |
| 315 v ^= (v << 15) & 0xefc60000UL; | 315 v ^= (v << 15) & 0xefc60000UL; |
| 316 v ^= (v >> 18); | 316 v ^= (v >> 18); |
| 317 return v; | 317 return v; |
| 318 } | 318 } |
| 319 void FX_Random_MT_Close(FX_LPVOID pContext) | 319 void FX_Random_MT_Close(FX_LPVOID pContext) |
| 320 { | 320 { |
| 321 FXSYS_assert(pContext != NULL); | 321 FXSYS_assert(pContext != NULL); |
| 322 FX_Free(pContext); | 322 FX_Free(pContext); |
| 323 } | 323 } |
| 324 void FX_Random_GenerateMT(FX_LPDWORD pBuffer, FX_INT32 iCount) | 324 void FX_Random_GenerateMT(FX_LPDWORD pBuffer, int32_t iCount) |
| 325 { | 325 { |
| 326 FX_DWORD dwSeed; | 326 FX_DWORD dwSeed; |
| 327 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 327 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 328 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { | 328 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { |
| 329 FX_Random_GenerateBase(&dwSeed, 1); | 329 FX_Random_GenerateBase(&dwSeed, 1); |
| 330 } | 330 } |
| 331 #else | 331 #else |
| 332 FX_Random_GenerateBase(&dwSeed, 1); | 332 FX_Random_GenerateBase(&dwSeed, 1); |
| 333 #endif | 333 #endif |
| 334 FX_LPVOID pContext = FX_Random_MT_Start(dwSeed); | 334 FX_LPVOID pContext = FX_Random_MT_Start(dwSeed); |
| 335 while (iCount -- > 0) { | 335 while (iCount -- > 0) { |
| 336 *pBuffer ++ = FX_Random_MT_Generate(pContext); | 336 *pBuffer ++ = FX_Random_MT_Generate(pContext); |
| 337 } | 337 } |
| 338 FX_Random_MT_Close(pContext); | 338 FX_Random_MT_Close(pContext); |
| 339 } | 339 } |
| 340 void FX_Random_GenerateBase(FX_LPDWORD pBuffer, FX_INT32 iCount) | 340 void FX_Random_GenerateBase(FX_LPDWORD pBuffer, int32_t iCount) |
| 341 { | 341 { |
| 342 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 342 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 343 SYSTEMTIME st1, st2; | 343 SYSTEMTIME st1, st2; |
| 344 ::GetSystemTime(&st1); | 344 ::GetSystemTime(&st1); |
| 345 do { | 345 do { |
| 346 ::GetSystemTime(&st2); | 346 ::GetSystemTime(&st2); |
| 347 } while (FXSYS_memcmp32(&st1, &st2, sizeof(SYSTEMTIME)) == 0); | 347 } while (FXSYS_memcmp32(&st1, &st2, sizeof(SYSTEMTIME)) == 0); |
| 348 FX_DWORD dwHash1 = FX_HashCode_String_GetA((FX_LPCSTR)&st1, sizeof(st1), TRU
E); | 348 FX_DWORD dwHash1 = FX_HashCode_String_GetA((FX_LPCSTR)&st1, sizeof(st1), TRU
E); |
| 349 FX_DWORD dwHash2 = FX_HashCode_String_GetA((FX_LPCSTR)&st2, sizeof(st2), TRU
E); | 349 FX_DWORD dwHash2 = FX_HashCode_String_GetA((FX_LPCSTR)&st2, sizeof(st2), TRU
E); |
| 350 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); | 350 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); |
| 351 #else | 351 #else |
| 352 time_t tmLast = time(NULL), tmCur; | 352 time_t tmLast = time(NULL), tmCur; |
| 353 while ((tmCur = time(NULL)) == tmLast); | 353 while ((tmCur = time(NULL)) == tmLast); |
| 354 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); | 354 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); |
| 355 #endif | 355 #endif |
| 356 while (iCount -- > 0) { | 356 while (iCount -- > 0) { |
| 357 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); | 357 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); |
| 358 } | 358 } |
| 359 } | 359 } |
| 360 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 360 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 361 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, FX_INT32 iCount) | 361 FX_BOOL FX_GenerateCryptoRandom(FX_LPDWORD pBuffer, int32_t iCount) |
| 362 { | 362 { |
| 363 HCRYPTPROV hCP = NULL; | 363 HCRYPTPROV hCP = NULL; |
| 364 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { | 364 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { |
| 365 return FALSE; | 365 return FALSE; |
| 366 } | 366 } |
| 367 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (FX_LPBYTE)pBuffer); | 367 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (FX_LPBYTE)pBuffer); |
| 368 ::CryptReleaseContext(hCP, 0); | 368 ::CryptReleaseContext(hCP, 0); |
| 369 return TRUE; | 369 return TRUE; |
| 370 } | 370 } |
| 371 #endif | 371 #endif |
| 372 void FX_Random_GenerateCrypto(FX_LPDWORD pBuffer, FX_INT32 iCount) | 372 void FX_Random_GenerateCrypto(FX_LPDWORD pBuffer, int32_t iCount) |
| 373 { | 373 { |
| 374 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 374 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 375 FX_GenerateCryptoRandom(pBuffer, iCount); | 375 FX_GenerateCryptoRandom(pBuffer, iCount); |
| 376 #else | 376 #else |
| 377 FX_Random_GenerateBase(pBuffer, iCount); | 377 FX_Random_GenerateBase(pBuffer, iCount); |
| 378 #endif | 378 #endif |
| 379 } | 379 } |
| 380 #ifdef __cplusplus | 380 #ifdef __cplusplus |
| 381 } | 381 } |
| 382 #endif | 382 #endif |
| OLD | NEW |