| 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_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
| 8 #include "../../include/fxcrt/fx_ext.h" | 8 #include "../../include/fxcrt/fx_ext.h" |
| 9 #include "extension.h" | 9 #include "extension.h" |
| 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 size_t FX_File_Write(FX_HFILE hFile, const void* pBuffer, size_t szBuffer) | 65 size_t FX_File_Write(FX_HFILE hFile, const void* pBuffer, size_t szBuffer) |
| 66 { | 66 { |
| 67 FXSYS_assert(hFile != NULL); | 67 FXSYS_assert(hFile != NULL); |
| 68 return ((IFXCRT_FileAccess*)hFile)->Write(pBuffer, szBuffer); | 68 return ((IFXCRT_FileAccess*)hFile)->Write(pBuffer, szBuffer); |
| 69 } | 69 } |
| 70 size_t FX_File_WritePos(FX_HFILE hFile, const void* pBuffer, size_t szBuffer, FX
_FILESIZE pos) | 70 size_t FX_File_WritePos(FX_HFILE hFile, const void* pBuffer, size_t szBuffer, FX
_FILESIZE pos) |
| 71 { | 71 { |
| 72 FXSYS_assert(hFile != NULL); | 72 FXSYS_assert(hFile != NULL); |
| 73 return ((IFXCRT_FileAccess*)hFile)->WritePos(pBuffer, szBuffer, pos); | 73 return ((IFXCRT_FileAccess*)hFile)->WritePos(pBuffer, szBuffer, pos); |
| 74 } | 74 } |
| 75 FX_BOOL FX_File_Flush(FX_HFILE hFile) | 75 bool FX_File_Flush(FX_HFILE hFile) |
| 76 { | 76 { |
| 77 FXSYS_assert(hFile != NULL); | 77 FXSYS_assert(hFile != NULL); |
| 78 return ((IFXCRT_FileAccess*)hFile)->Flush(); | 78 return ((IFXCRT_FileAccess*)hFile)->Flush(); |
| 79 } | 79 } |
| 80 FX_BOOL FX_File_Truncate(FX_HFILE hFile, FX_FILESIZE szFile) | 80 bool FX_File_Truncate(FX_HFILE hFile, FX_FILESIZE szFile) |
| 81 { | 81 { |
| 82 FXSYS_assert(hFile != NULL); | 82 FXSYS_assert(hFile != NULL); |
| 83 return ((IFXCRT_FileAccess*)hFile)->Truncate(szFile); | 83 return ((IFXCRT_FileAccess*)hFile)->Truncate(szFile); |
| 84 } | 84 } |
| 85 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) | 85 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) |
| 86 { | 86 { |
| 87 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); | 87 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); |
| 88 if (!pFA) { | 88 if (!pFA) { |
| 89 return NULL; | 89 return NULL; |
| 90 } | 90 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 115 return FX_CreateFileStream(filename, FX_FILEMODE_Truncate); | 115 return FX_CreateFileStream(filename, FX_FILEMODE_Truncate); |
| 116 } | 116 } |
| 117 IFX_FileRead* FX_CreateFileRead(const FX_CHAR* filename) | 117 IFX_FileRead* FX_CreateFileRead(const FX_CHAR* filename) |
| 118 { | 118 { |
| 119 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); | 119 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); |
| 120 } | 120 } |
| 121 IFX_FileRead* FX_CreateFileRead(const FX_WCHAR* filename) | 121 IFX_FileRead* FX_CreateFileRead(const FX_WCHAR* filename) |
| 122 { | 122 { |
| 123 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); | 123 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); |
| 124 } | 124 } |
| 125 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer, size_t dwSize, FX_BOOL
bTakeOver) | 125 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer, size_t dwSize, bool bT
akeOver) |
| 126 { | 126 { |
| 127 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); | 127 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); |
| 128 } | 128 } |
| 129 IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive) | 129 IFX_MemoryStream* FX_CreateMemoryStream(bool bConsecutive) |
| 130 { | 130 { |
| 131 return new CFX_MemoryStream(bConsecutive); | 131 return new CFX_MemoryStream(bConsecutive); |
| 132 } | 132 } |
| 133 #ifdef __cplusplus | 133 #ifdef __cplusplus |
| 134 extern "C" { | 134 extern "C" { |
| 135 #endif | 135 #endif |
| 136 FX_FLOAT FXSYS_tan(FX_FLOAT a) | 136 FX_FLOAT FXSYS_tan(FX_FLOAT a) |
| 137 { | 137 { |
| 138 return (FX_FLOAT)tan(a); | 138 return (FX_FLOAT)tan(a); |
| 139 } | 139 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 153 FX_FLOAT FXSYS_wcstof(const FX_WCHAR* pwsStr, int32_t iLength, int32_t *pUsedLen
) | 153 FX_FLOAT FXSYS_wcstof(const FX_WCHAR* pwsStr, int32_t iLength, int32_t *pUsedLen
) |
| 154 { | 154 { |
| 155 FXSYS_assert(pwsStr != NULL); | 155 FXSYS_assert(pwsStr != NULL); |
| 156 if (iLength < 0) { | 156 if (iLength < 0) { |
| 157 iLength = (int32_t)FXSYS_wcslen(pwsStr); | 157 iLength = (int32_t)FXSYS_wcslen(pwsStr); |
| 158 } | 158 } |
| 159 if (iLength == 0) { | 159 if (iLength == 0) { |
| 160 return 0.0f; | 160 return 0.0f; |
| 161 } | 161 } |
| 162 int32_t iUsedLen = 0; | 162 int32_t iUsedLen = 0; |
| 163 FX_BOOL bNegtive = FALSE; | 163 bool bNegtive = false; |
| 164 switch (pwsStr[iUsedLen]) { | 164 switch (pwsStr[iUsedLen]) { |
| 165 case '-': | 165 case '-': |
| 166 bNegtive = TRUE; | 166 bNegtive = true; |
| 167 case '+': | 167 case '+': |
| 168 iUsedLen++; | 168 iUsedLen++; |
| 169 break; | 169 break; |
| 170 } | 170 } |
| 171 FX_FLOAT fValue = 0.0f; | 171 FX_FLOAT fValue = 0.0f; |
| 172 while (iUsedLen < iLength) { | 172 while (iUsedLen < iLength) { |
| 173 FX_WCHAR wch = pwsStr[iUsedLen]; | 173 FX_WCHAR wch = pwsStr[iUsedLen]; |
| 174 if (wch >= L'0' && wch <= L'9') { | 174 if (wch >= L'0' && wch <= L'9') { |
| 175 fValue = fValue * 10.0f + (wch - L'0'); | 175 fValue = fValue * 10.0f + (wch - L'0'); |
| 176 } else { | 176 } else { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 FX_CHAR ch1 = 0, ch2 = 0; | 223 FX_CHAR ch1 = 0, ch2 = 0; |
| 224 while (count-- > 0) { | 224 while (count-- > 0) { |
| 225 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); | 225 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); |
| 226 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); | 226 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); |
| 227 if (ch1 != ch2) { | 227 if (ch1 != ch2) { |
| 228 break; | 228 break; |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 return ch1 - ch2; | 231 return ch1 - ch2; |
| 232 } | 232 } |
| 233 FX_DWORD FX_HashCode_String_GetA(const FX_CHAR* pStr, int32_t iLength, FX_BOOL b
IgnoreCase) | 233 FX_DWORD FX_HashCode_String_GetA(const FX_CHAR* pStr, int32_t iLength, bool bIgn
oreCase) |
| 234 { | 234 { |
| 235 FXSYS_assert(pStr != NULL); | 235 FXSYS_assert(pStr != NULL); |
| 236 if (iLength < 0) { | 236 if (iLength < 0) { |
| 237 iLength = (int32_t)FXSYS_strlen(pStr); | 237 iLength = (int32_t)FXSYS_strlen(pStr); |
| 238 } | 238 } |
| 239 const FX_CHAR* pStrEnd = pStr + iLength; | 239 const FX_CHAR* pStrEnd = pStr + iLength; |
| 240 FX_DWORD dwHashCode = 0; | 240 FX_DWORD dwHashCode = 0; |
| 241 if (bIgnoreCase) { | 241 if (bIgnoreCase) { |
| 242 while (pStr < pStrEnd) { | 242 while (pStr < pStrEnd) { |
| 243 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); | 243 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); |
| 244 } | 244 } |
| 245 } else { | 245 } else { |
| 246 while (pStr < pStrEnd) { | 246 while (pStr < pStrEnd) { |
| 247 dwHashCode = 31 * dwHashCode + *pStr ++; | 247 dwHashCode = 31 * dwHashCode + *pStr ++; |
| 248 } | 248 } |
| 249 } | 249 } |
| 250 return dwHashCode; | 250 return dwHashCode; |
| 251 } | 251 } |
| 252 FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr, int32_t iLength, FX_BOOL
bIgnoreCase) | 252 FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr, int32_t iLength, bool bIg
noreCase) |
| 253 { | 253 { |
| 254 FXSYS_assert(pStr != NULL); | 254 FXSYS_assert(pStr != NULL); |
| 255 if (iLength < 0) { | 255 if (iLength < 0) { |
| 256 iLength = (int32_t)FXSYS_wcslen(pStr); | 256 iLength = (int32_t)FXSYS_wcslen(pStr); |
| 257 } | 257 } |
| 258 const FX_WCHAR* pStrEnd = pStr + iLength; | 258 const FX_WCHAR* pStrEnd = pStr + iLength; |
| 259 FX_DWORD dwHashCode = 0; | 259 FX_DWORD dwHashCode = 0; |
| 260 if (bIgnoreCase) { | 260 if (bIgnoreCase) { |
| 261 while (pStr < pStrEnd) { | 261 while (pStr < pStrEnd) { |
| 262 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); | 262 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 276 #endif | 276 #endif |
| 277 void* FX_Random_MT_Start(FX_DWORD dwSeed) | 277 void* FX_Random_MT_Start(FX_DWORD dwSeed) |
| 278 { | 278 { |
| 279 FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); | 279 FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); |
| 280 pContext->mt[0] = dwSeed; | 280 pContext->mt[0] = dwSeed; |
| 281 FX_DWORD &i = pContext->mti; | 281 FX_DWORD &i = pContext->mti; |
| 282 FX_DWORD* pBuf = pContext->mt; | 282 FX_DWORD* pBuf = pContext->mt; |
| 283 for (i = 1; i < MT_N; i ++) { | 283 for (i = 1; i < MT_N; i ++) { |
| 284 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); | 284 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); |
| 285 } | 285 } |
| 286 pContext->bHaveSeed = TRUE; | 286 pContext->bHaveSeed = true; |
| 287 return pContext; | 287 return pContext; |
| 288 } | 288 } |
| 289 FX_DWORD FX_Random_MT_Generate(void* pContext) | 289 FX_DWORD FX_Random_MT_Generate(void* pContext) |
| 290 { | 290 { |
| 291 FXSYS_assert(pContext != NULL); | 291 FXSYS_assert(pContext != NULL); |
| 292 FX_LPMTRANDOMCONTEXT pMTC = (FX_LPMTRANDOMCONTEXT)pContext; | 292 FX_LPMTRANDOMCONTEXT pMTC = (FX_LPMTRANDOMCONTEXT)pContext; |
| 293 FX_DWORD v; | 293 FX_DWORD v; |
| 294 static FX_DWORD mag[2] = {0, MT_Matrix_A}; | 294 static FX_DWORD mag[2] = {0, MT_Matrix_A}; |
| 295 FX_DWORD &mti = pMTC->mti; | 295 FX_DWORD &mti = pMTC->mti; |
| 296 FX_DWORD* pBuf = pMTC->mt; | 296 FX_DWORD* pBuf = pMTC->mt; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 FX_Random_MT_Close(pContext); | 340 FX_Random_MT_Close(pContext); |
| 341 } | 341 } |
| 342 void FX_Random_GenerateBase(FX_DWORD* pBuffer, int32_t iCount) | 342 void FX_Random_GenerateBase(FX_DWORD* pBuffer, int32_t iCount) |
| 343 { | 343 { |
| 344 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 344 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 345 SYSTEMTIME st1, st2; | 345 SYSTEMTIME st1, st2; |
| 346 ::GetSystemTime(&st1); | 346 ::GetSystemTime(&st1); |
| 347 do { | 347 do { |
| 348 ::GetSystemTime(&st2); | 348 ::GetSystemTime(&st2); |
| 349 } while (FXSYS_memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); | 349 } while (FXSYS_memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); |
| 350 FX_DWORD dwHash1 = FX_HashCode_String_GetA((const FX_CHAR*)&st1, sizeof(st1)
, TRUE); | 350 FX_DWORD dwHash1 = FX_HashCode_String_GetA((const FX_CHAR*)&st1, sizeof(st1)
, true); |
| 351 FX_DWORD dwHash2 = FX_HashCode_String_GetA((const FX_CHAR*)&st2, sizeof(st2)
, TRUE); | 351 FX_DWORD dwHash2 = FX_HashCode_String_GetA((const FX_CHAR*)&st2, sizeof(st2)
, true); |
| 352 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); | 352 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); |
| 353 #else | 353 #else |
| 354 time_t tmLast = time(NULL), tmCur; | 354 time_t tmLast = time(NULL), tmCur; |
| 355 while ((tmCur = time(NULL)) == tmLast); | 355 while ((tmCur = time(NULL)) == tmLast); |
| 356 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); | 356 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); |
| 357 #endif | 357 #endif |
| 358 while (iCount -- > 0) { | 358 while (iCount -- > 0) { |
| 359 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); | 359 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); |
| 360 } | 360 } |
| 361 } | 361 } |
| 362 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 362 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 363 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount) | 363 bool FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount) |
| 364 { | 364 { |
| 365 HCRYPTPROV hCP = NULL; | 365 HCRYPTPROV hCP = NULL; |
| 366 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { | 366 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { |
| 367 return FALSE; | 367 return false; |
| 368 } | 368 } |
| 369 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (uint8_t*)pBuffer); | 369 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (uint8_t*)pBuffer); |
| 370 ::CryptReleaseContext(hCP, 0); | 370 ::CryptReleaseContext(hCP, 0); |
| 371 return TRUE; | 371 return true; |
| 372 } | 372 } |
| 373 #endif | 373 #endif |
| 374 void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount) | 374 void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount) |
| 375 { | 375 { |
| 376 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 376 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 377 FX_GenerateCryptoRandom(pBuffer, iCount); | 377 FX_GenerateCryptoRandom(pBuffer, iCount); |
| 378 #else | 378 #else |
| 379 FX_Random_GenerateBase(pBuffer, iCount); | 379 FX_Random_GenerateBase(pBuffer, iCount); |
| 380 #endif | 380 #endif |
| 381 } | 381 } |
| 382 #ifdef __cplusplus | 382 #ifdef __cplusplus |
| 383 } | 383 } |
| 384 #endif | 384 #endif |
| OLD | NEW |