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 "core/fxcrt/extension.h" | 7 #include "core/fxcrt/extension.h" |
8 #include "core/fxcrt/include/fx_basic.h" | 8 #include "core/fxcrt/include/fx_basic.h" |
9 #include "core/fxcrt/include/fx_ext.h" | 9 #include "core/fxcrt/include/fx_ext.h" |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 m_pFile->Release(); | 22 m_pFile->Release(); |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 IFX_FileStream* CFX_CRTFileStream::Retain() { | 26 IFX_FileStream* CFX_CRTFileStream::Retain() { |
27 m_dwCount++; | 27 m_dwCount++; |
28 return this; | 28 return this; |
29 } | 29 } |
30 | 30 |
31 void CFX_CRTFileStream::Release() { | 31 void CFX_CRTFileStream::Release() { |
32 FX_DWORD nCount = --m_dwCount; | 32 uint32_t nCount = --m_dwCount; |
33 if (!nCount) { | 33 if (!nCount) { |
34 delete this; | 34 delete this; |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 FX_FILESIZE CFX_CRTFileStream::GetSize() { | 38 FX_FILESIZE CFX_CRTFileStream::GetSize() { |
39 return m_pFile->GetSize(); | 39 return m_pFile->GetSize(); |
40 } | 40 } |
41 | 41 |
42 FX_BOOL CFX_CRTFileStream::IsEOF() { | 42 FX_BOOL CFX_CRTFileStream::IsEOF() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 CFX_CRTFileAccess* pFA = NULL; | 75 CFX_CRTFileAccess* pFA = NULL; |
76 pFA = new CFX_CRTFileAccess; | 76 pFA = new CFX_CRTFileAccess; |
77 if (NULL == pFA) | 77 if (NULL == pFA) |
78 return NULL; | 78 return NULL; |
79 | 79 |
80 pFA->Init(wsPath); | 80 pFA->Init(wsPath); |
81 return pFA; | 81 return pFA; |
82 } | 82 } |
83 #endif // PDF_ENABLE_XFA | 83 #endif // PDF_ENABLE_XFA |
84 | 84 |
85 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) { | 85 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, uint32_t dwModes) { |
86 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); | 86 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); |
87 if (!pFA) { | 87 if (!pFA) { |
88 return NULL; | 88 return NULL; |
89 } | 89 } |
90 if (!pFA->Open(filename, dwModes)) { | 90 if (!pFA->Open(filename, dwModes)) { |
91 pFA->Release(); | 91 pFA->Release(); |
92 return NULL; | 92 return NULL; |
93 } | 93 } |
94 return new CFX_CRTFileStream(pFA); | 94 return new CFX_CRTFileStream(pFA); |
95 } | 95 } |
96 IFX_FileStream* FX_CreateFileStream(const FX_WCHAR* filename, | 96 IFX_FileStream* FX_CreateFileStream(const FX_WCHAR* filename, |
97 FX_DWORD dwModes) { | 97 uint32_t dwModes) { |
98 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); | 98 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); |
99 if (!pFA) { | 99 if (!pFA) { |
100 return NULL; | 100 return NULL; |
101 } | 101 } |
102 if (!pFA->Open(filename, dwModes)) { | 102 if (!pFA->Open(filename, dwModes)) { |
103 pFA->Release(); | 103 pFA->Release(); |
104 return NULL; | 104 return NULL; |
105 } | 105 } |
106 return new CFX_CRTFileStream(pFA); | 106 return new CFX_CRTFileStream(pFA); |
107 } | 107 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 FX_CHAR ch1 = 0, ch2 = 0; | 210 FX_CHAR ch1 = 0, ch2 = 0; |
211 while (count-- > 0) { | 211 while (count-- > 0) { |
212 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); | 212 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); |
213 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); | 213 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); |
214 if (ch1 != ch2) { | 214 if (ch1 != ch2) { |
215 break; | 215 break; |
216 } | 216 } |
217 } | 217 } |
218 return ch1 - ch2; | 218 return ch1 - ch2; |
219 } | 219 } |
220 FX_DWORD FX_HashCode_String_GetA(const FX_CHAR* pStr, | 220 uint32_t FX_HashCode_String_GetA(const FX_CHAR* pStr, |
221 int32_t iLength, | 221 int32_t iLength, |
222 FX_BOOL bIgnoreCase) { | 222 FX_BOOL bIgnoreCase) { |
223 FXSYS_assert(pStr); | 223 FXSYS_assert(pStr); |
224 if (iLength < 0) { | 224 if (iLength < 0) { |
225 iLength = (int32_t)FXSYS_strlen(pStr); | 225 iLength = (int32_t)FXSYS_strlen(pStr); |
226 } | 226 } |
227 const FX_CHAR* pStrEnd = pStr + iLength; | 227 const FX_CHAR* pStrEnd = pStr + iLength; |
228 FX_DWORD dwHashCode = 0; | 228 uint32_t dwHashCode = 0; |
229 if (bIgnoreCase) { | 229 if (bIgnoreCase) { |
230 while (pStr < pStrEnd) { | 230 while (pStr < pStrEnd) { |
231 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); | 231 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); |
232 } | 232 } |
233 } else { | 233 } else { |
234 while (pStr < pStrEnd) { | 234 while (pStr < pStrEnd) { |
235 dwHashCode = 31 * dwHashCode + *pStr++; | 235 dwHashCode = 31 * dwHashCode + *pStr++; |
236 } | 236 } |
237 } | 237 } |
238 return dwHashCode; | 238 return dwHashCode; |
239 } | 239 } |
240 FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr, | 240 uint32_t FX_HashCode_String_GetW(const FX_WCHAR* pStr, |
241 int32_t iLength, | 241 int32_t iLength, |
242 FX_BOOL bIgnoreCase) { | 242 FX_BOOL bIgnoreCase) { |
243 FXSYS_assert(pStr); | 243 FXSYS_assert(pStr); |
244 if (iLength < 0) { | 244 if (iLength < 0) { |
245 iLength = (int32_t)FXSYS_wcslen(pStr); | 245 iLength = (int32_t)FXSYS_wcslen(pStr); |
246 } | 246 } |
247 const FX_WCHAR* pStrEnd = pStr + iLength; | 247 const FX_WCHAR* pStrEnd = pStr + iLength; |
248 FX_DWORD dwHashCode = 0; | 248 uint32_t dwHashCode = 0; |
249 if (bIgnoreCase) { | 249 if (bIgnoreCase) { |
250 while (pStr < pStrEnd) { | 250 while (pStr < pStrEnd) { |
251 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); | 251 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); |
252 } | 252 } |
253 } else { | 253 } else { |
254 while (pStr < pStrEnd) { | 254 while (pStr < pStrEnd) { |
255 dwHashCode = 1313 * dwHashCode + *pStr++; | 255 dwHashCode = 1313 * dwHashCode + *pStr++; |
256 } | 256 } |
257 } | 257 } |
258 return dwHashCode; | 258 return dwHashCode; |
259 } | 259 } |
260 | 260 |
261 void* FX_Random_MT_Start(FX_DWORD dwSeed) { | 261 void* FX_Random_MT_Start(uint32_t dwSeed) { |
262 FX_MTRANDOMCONTEXT* pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); | 262 FX_MTRANDOMCONTEXT* pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); |
263 pContext->mt[0] = dwSeed; | 263 pContext->mt[0] = dwSeed; |
264 FX_DWORD& i = pContext->mti; | 264 uint32_t& i = pContext->mti; |
265 FX_DWORD* pBuf = pContext->mt; | 265 uint32_t* pBuf = pContext->mt; |
266 for (i = 1; i < MT_N; i++) { | 266 for (i = 1; i < MT_N; i++) { |
267 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); | 267 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); |
268 } | 268 } |
269 pContext->bHaveSeed = TRUE; | 269 pContext->bHaveSeed = TRUE; |
270 return pContext; | 270 return pContext; |
271 } | 271 } |
272 FX_DWORD FX_Random_MT_Generate(void* pContext) { | 272 uint32_t FX_Random_MT_Generate(void* pContext) { |
273 FXSYS_assert(pContext); | 273 FXSYS_assert(pContext); |
274 FX_MTRANDOMCONTEXT* pMTC = static_cast<FX_MTRANDOMCONTEXT*>(pContext); | 274 FX_MTRANDOMCONTEXT* pMTC = static_cast<FX_MTRANDOMCONTEXT*>(pContext); |
275 FX_DWORD v; | 275 uint32_t v; |
276 static FX_DWORD mag[2] = {0, MT_Matrix_A}; | 276 static uint32_t mag[2] = {0, MT_Matrix_A}; |
277 FX_DWORD& mti = pMTC->mti; | 277 uint32_t& mti = pMTC->mti; |
278 FX_DWORD* pBuf = pMTC->mt; | 278 uint32_t* pBuf = pMTC->mt; |
279 if ((int)mti < 0 || mti >= MT_N) { | 279 if ((int)mti < 0 || mti >= MT_N) { |
280 if (mti > MT_N && !pMTC->bHaveSeed) { | 280 if (mti > MT_N && !pMTC->bHaveSeed) { |
281 return 0; | 281 return 0; |
282 } | 282 } |
283 FX_DWORD kk; | 283 uint32_t kk; |
284 for (kk = 0; kk < MT_N - MT_M; kk++) { | 284 for (kk = 0; kk < MT_N - MT_M; kk++) { |
285 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); | 285 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); |
286 pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; | 286 pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; |
287 } | 287 } |
288 for (; kk < MT_N - 1; kk++) { | 288 for (; kk < MT_N - 1; kk++) { |
289 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); | 289 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); |
290 pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; | 290 pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; |
291 } | 291 } |
292 v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); | 292 v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); |
293 pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; | 293 pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; |
294 mti = 0; | 294 mti = 0; |
295 } | 295 } |
296 v = pBuf[mti++]; | 296 v = pBuf[mti++]; |
297 v ^= (v >> 11); | 297 v ^= (v >> 11); |
298 v ^= (v << 7) & 0x9d2c5680UL; | 298 v ^= (v << 7) & 0x9d2c5680UL; |
299 v ^= (v << 15) & 0xefc60000UL; | 299 v ^= (v << 15) & 0xefc60000UL; |
300 v ^= (v >> 18); | 300 v ^= (v >> 18); |
301 return v; | 301 return v; |
302 } | 302 } |
303 void FX_Random_MT_Close(void* pContext) { | 303 void FX_Random_MT_Close(void* pContext) { |
304 FXSYS_assert(pContext); | 304 FXSYS_assert(pContext); |
305 FX_Free(pContext); | 305 FX_Free(pContext); |
306 } | 306 } |
307 void FX_Random_GenerateMT(FX_DWORD* pBuffer, int32_t iCount) { | 307 void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount) { |
308 FX_DWORD dwSeed; | 308 uint32_t dwSeed; |
309 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 309 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
310 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { | 310 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { |
311 FX_Random_GenerateBase(&dwSeed, 1); | 311 FX_Random_GenerateBase(&dwSeed, 1); |
312 } | 312 } |
313 #else | 313 #else |
314 FX_Random_GenerateBase(&dwSeed, 1); | 314 FX_Random_GenerateBase(&dwSeed, 1); |
315 #endif | 315 #endif |
316 void* pContext = FX_Random_MT_Start(dwSeed); | 316 void* pContext = FX_Random_MT_Start(dwSeed); |
317 while (iCount-- > 0) { | 317 while (iCount-- > 0) { |
318 *pBuffer++ = FX_Random_MT_Generate(pContext); | 318 *pBuffer++ = FX_Random_MT_Generate(pContext); |
319 } | 319 } |
320 FX_Random_MT_Close(pContext); | 320 FX_Random_MT_Close(pContext); |
321 } | 321 } |
322 void FX_Random_GenerateBase(FX_DWORD* pBuffer, int32_t iCount) { | 322 void FX_Random_GenerateBase(uint32_t* pBuffer, int32_t iCount) { |
323 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 323 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
324 SYSTEMTIME st1, st2; | 324 SYSTEMTIME st1, st2; |
325 ::GetSystemTime(&st1); | 325 ::GetSystemTime(&st1); |
326 do { | 326 do { |
327 ::GetSystemTime(&st2); | 327 ::GetSystemTime(&st2); |
328 } while (FXSYS_memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); | 328 } while (FXSYS_memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); |
329 FX_DWORD dwHash1 = | 329 uint32_t dwHash1 = |
330 FX_HashCode_String_GetA((const FX_CHAR*)&st1, sizeof(st1), TRUE); | 330 FX_HashCode_String_GetA((const FX_CHAR*)&st1, sizeof(st1), TRUE); |
331 FX_DWORD dwHash2 = | 331 uint32_t dwHash2 = |
332 FX_HashCode_String_GetA((const FX_CHAR*)&st2, sizeof(st2), TRUE); | 332 FX_HashCode_String_GetA((const FX_CHAR*)&st2, sizeof(st2), TRUE); |
333 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); | 333 ::srand((dwHash1 << 16) | (uint32_t)dwHash2); |
334 #else | 334 #else |
335 time_t tmLast = time(NULL); | 335 time_t tmLast = time(NULL); |
336 time_t tmCur; | 336 time_t tmCur; |
337 while ((tmCur = time(NULL)) == tmLast) { | 337 while ((tmCur = time(NULL)) == tmLast) { |
338 continue; | 338 continue; |
339 } | 339 } |
340 | 340 |
341 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); | 341 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); |
342 #endif | 342 #endif |
343 while (iCount-- > 0) { | 343 while (iCount-- > 0) { |
344 *pBuffer++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); | 344 *pBuffer++ = (uint32_t)((::rand() << 16) | (::rand() & 0xFFFF)); |
345 } | 345 } |
346 } | 346 } |
347 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 347 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
348 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount) { | 348 FX_BOOL FX_GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount) { |
349 HCRYPTPROV hCP = NULL; | 349 HCRYPTPROV hCP = NULL; |
350 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || !hCP) { | 350 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || !hCP) { |
351 return FALSE; | 351 return FALSE; |
352 } | 352 } |
353 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (uint8_t*)pBuffer); | 353 ::CryptGenRandom(hCP, iCount * sizeof(uint32_t), (uint8_t*)pBuffer); |
354 ::CryptReleaseContext(hCP, 0); | 354 ::CryptReleaseContext(hCP, 0); |
355 return TRUE; | 355 return TRUE; |
356 } | 356 } |
357 #endif | 357 #endif |
358 void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount) { | 358 void FX_Random_GenerateCrypto(uint32_t* pBuffer, int32_t iCount) { |
359 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 359 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
360 FX_GenerateCryptoRandom(pBuffer, iCount); | 360 FX_GenerateCryptoRandom(pBuffer, iCount); |
361 #else | 361 #else |
362 FX_Random_GenerateBase(pBuffer, iCount); | 362 FX_Random_GenerateBase(pBuffer, iCount); |
363 #endif | 363 #endif |
364 } | 364 } |
365 | 365 |
366 #ifdef PDF_ENABLE_XFA | 366 #ifdef PDF_ENABLE_XFA |
367 void FX_GUID_CreateV4(FX_LPGUID pGUID) { | 367 void FX_GUID_CreateV4(FX_LPGUID pGUID) { |
368 FX_Random_GenerateMT((FX_DWORD*)pGUID, 4); | 368 FX_Random_GenerateMT((uint32_t*)pGUID, 4); |
369 uint8_t& b = ((uint8_t*)pGUID)[6]; | 369 uint8_t& b = ((uint8_t*)pGUID)[6]; |
370 b = (b & 0x0F) | 0x40; | 370 b = (b & 0x0F) | 0x40; |
371 } | 371 } |
372 const FX_CHAR* gs_FX_pHexChars = "0123456789ABCDEF"; | 372 const FX_CHAR* gs_FX_pHexChars = "0123456789ABCDEF"; |
373 void FX_GUID_ToString(FX_LPCGUID pGUID, | 373 void FX_GUID_ToString(FX_LPCGUID pGUID, |
374 CFX_ByteString& bsStr, | 374 CFX_ByteString& bsStr, |
375 FX_BOOL bSeparator) { | 375 FX_BOOL bSeparator) { |
376 FX_CHAR* pBuf = bsStr.GetBuffer(40); | 376 FX_CHAR* pBuf = bsStr.GetBuffer(40); |
377 uint8_t b; | 377 uint8_t b; |
378 for (int32_t i = 0; i < 16; i++) { | 378 for (int32_t i = 0; i < 16; i++) { |
379 b = ((const uint8_t*)pGUID)[i]; | 379 b = ((const uint8_t*)pGUID)[i]; |
380 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 380 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
381 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 381 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
382 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 382 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
383 *pBuf++ = L'-'; | 383 *pBuf++ = L'-'; |
384 } | 384 } |
385 } | 385 } |
386 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 386 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
387 } | 387 } |
388 #endif // PDF_ENABLE_XFA | 388 #endif // PDF_ENABLE_XFA |
OLD | NEW |