| 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 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 void CFX_CRTFileAccess::GetPath(CFX_WideString& wsPath) { | 36 void CFX_CRTFileAccess::GetPath(CFX_WideString& wsPath) { |
| 37 wsPath = m_path; | 37 wsPath = m_path; |
| 38 } | 38 } |
| 39 | 39 |
| 40 IFX_SeekableStream* CFX_CRTFileAccess::CreateFileStream(uint32_t dwModes) { | 40 IFX_SeekableStream* CFX_CRTFileAccess::CreateFileStream(uint32_t dwModes) { |
| 41 return FX_CreateFileStream(m_path.c_str(), dwModes); | 41 return FX_CreateFileStream(m_path.c_str(), dwModes); |
| 42 } | 42 } |
| 43 | 43 |
| 44 FX_BOOL CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { | 44 bool CFX_CRTFileAccess::Init(const CFX_WideStringC& wsPath) { |
| 45 m_path = wsPath; | 45 m_path = wsPath; |
| 46 m_RefCount = 1; | 46 m_RefCount = 1; |
| 47 return TRUE; | 47 return true; |
| 48 } | 48 } |
| 49 | 49 |
| 50 #endif // PDF_ENABLE_XFA | 50 #endif // PDF_ENABLE_XFA |
| 51 | 51 |
| 52 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) | 52 CFX_CRTFileStream::CFX_CRTFileStream(std::unique_ptr<IFXCRT_FileAccess> pFA) |
| 53 : m_pFile(std::move(pFA)), m_dwCount(1) {} | 53 : m_pFile(std::move(pFA)), m_dwCount(1) {} |
| 54 | 54 |
| 55 CFX_CRTFileStream::~CFX_CRTFileStream() {} | 55 CFX_CRTFileStream::~CFX_CRTFileStream() {} |
| 56 | 56 |
| 57 CFX_MemoryStream::CFX_MemoryStream(FX_BOOL bConsecutive) | 57 CFX_MemoryStream::CFX_MemoryStream(bool bConsecutive) |
| 58 : m_dwCount(1), | 58 : m_dwCount(1), |
| 59 m_nTotalSize(0), | 59 m_nTotalSize(0), |
| 60 m_nCurSize(0), | 60 m_nCurSize(0), |
| 61 m_nCurPos(0), | 61 m_nCurPos(0), |
| 62 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 62 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
| 63 m_dwFlags = | 63 m_dwFlags = |
| 64 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); | 64 FX_MEMSTREAM_TakeOver | (bConsecutive ? FX_MEMSTREAM_Consecutive : 0); |
| 65 } | 65 } |
| 66 | 66 |
| 67 CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, | 67 CFX_MemoryStream::CFX_MemoryStream(uint8_t* pBuffer, |
| 68 size_t nSize, | 68 size_t nSize, |
| 69 FX_BOOL bTakeOver) | 69 bool bTakeOver) |
| 70 : m_dwCount(1), | 70 : m_dwCount(1), |
| 71 m_nTotalSize(nSize), | 71 m_nTotalSize(nSize), |
| 72 m_nCurSize(nSize), | 72 m_nCurSize(nSize), |
| 73 m_nCurPos(0), | 73 m_nCurPos(0), |
| 74 m_nGrowSize(FX_MEMSTREAM_BlockSize) { | 74 m_nGrowSize(FX_MEMSTREAM_BlockSize) { |
| 75 m_Blocks.Add(pBuffer); | 75 m_Blocks.Add(pBuffer); |
| 76 m_dwFlags = | 76 m_dwFlags = |
| 77 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); | 77 FX_MEMSTREAM_Consecutive | (bTakeOver ? FX_MEMSTREAM_TakeOver : 0); |
| 78 } | 78 } |
| 79 | 79 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); | 185 FXSYS_memcpy(m_Blocks[0] + (size_t)offset, buffer, size); |
| 186 if (m_nCurSize < m_nCurPos) { | 186 if (m_nCurSize < m_nCurPos) { |
| 187 m_nCurSize = m_nCurPos; | 187 m_nCurSize = m_nCurPos; |
| 188 } | 188 } |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| 191 | 191 |
| 192 FX_SAFE_SIZE_T newPos = size; | 192 FX_SAFE_SIZE_T newPos = size; |
| 193 newPos += offset; | 193 newPos += offset; |
| 194 if (!newPos.IsValid()) { | 194 if (!newPos.IsValid()) { |
| 195 return FALSE; | 195 return false; |
| 196 } | 196 } |
| 197 | 197 |
| 198 if (!ExpandBlocks(newPos.ValueOrDie())) { | 198 if (!ExpandBlocks(newPos.ValueOrDie())) { |
| 199 return FALSE; | 199 return false; |
| 200 } | 200 } |
| 201 m_nCurPos = newPos.ValueOrDie(); | 201 m_nCurPos = newPos.ValueOrDie(); |
| 202 size_t nStartBlock = (size_t)offset / m_nGrowSize; | 202 size_t nStartBlock = (size_t)offset / m_nGrowSize; |
| 203 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); | 203 offset -= (FX_FILESIZE)(nStartBlock * m_nGrowSize); |
| 204 while (size) { | 204 while (size) { |
| 205 size_t nWrite = m_nGrowSize - (size_t)offset; | 205 size_t nWrite = m_nGrowSize - (size_t)offset; |
| 206 if (nWrite > size) { | 206 if (nWrite > size) { |
| 207 nWrite = size; | 207 nWrite = size; |
| 208 } | 208 } |
| 209 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); | 209 FXSYS_memcpy(m_Blocks[(int)nStartBlock] + (size_t)offset, buffer, nWrite); |
| 210 buffer = ((uint8_t*)buffer) + nWrite; | 210 buffer = ((uint8_t*)buffer) + nWrite; |
| 211 size -= nWrite; | 211 size -= nWrite; |
| 212 nStartBlock++; | 212 nStartBlock++; |
| 213 offset = 0; | 213 offset = 0; |
| 214 } | 214 } |
| 215 return TRUE; | 215 return true; |
| 216 } | 216 } |
| 217 | 217 |
| 218 bool CFX_MemoryStream::Flush() { | 218 bool CFX_MemoryStream::Flush() { |
| 219 return true; | 219 return true; |
| 220 } | 220 } |
| 221 | 221 |
| 222 bool CFX_MemoryStream::IsConsecutive() const { | 222 bool CFX_MemoryStream::IsConsecutive() const { |
| 223 return !!(m_dwFlags & FX_MEMSTREAM_Consecutive); | 223 return !!(m_dwFlags & FX_MEMSTREAM_Consecutive); |
| 224 } | 224 } |
| 225 | 225 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 256 | 256 |
| 257 void CFX_MemoryStream::DetachBuffer() { | 257 void CFX_MemoryStream::DetachBuffer() { |
| 258 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { | 258 if (!(m_dwFlags & FX_MEMSTREAM_Consecutive)) { |
| 259 return; | 259 return; |
| 260 } | 260 } |
| 261 m_Blocks.RemoveAll(); | 261 m_Blocks.RemoveAll(); |
| 262 m_nTotalSize = m_nCurSize = m_nCurPos = 0; | 262 m_nTotalSize = m_nCurSize = m_nCurPos = 0; |
| 263 m_dwFlags = FX_MEMSTREAM_TakeOver; | 263 m_dwFlags = FX_MEMSTREAM_TakeOver; |
| 264 } | 264 } |
| 265 | 265 |
| 266 FX_BOOL CFX_MemoryStream::ExpandBlocks(size_t size) { | 266 bool CFX_MemoryStream::ExpandBlocks(size_t size) { |
| 267 if (m_nCurSize < size) { | 267 if (m_nCurSize < size) { |
| 268 m_nCurSize = size; | 268 m_nCurSize = size; |
| 269 } | 269 } |
| 270 if (size <= m_nTotalSize) { | 270 if (size <= m_nTotalSize) { |
| 271 return TRUE; | 271 return true; |
| 272 } | 272 } |
| 273 int32_t iCount = m_Blocks.GetSize(); | 273 int32_t iCount = m_Blocks.GetSize(); |
| 274 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; | 274 size = (size - m_nTotalSize + m_nGrowSize - 1) / m_nGrowSize; |
| 275 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); | 275 m_Blocks.SetSize(m_Blocks.GetSize() + (int32_t)size); |
| 276 while (size--) { | 276 while (size--) { |
| 277 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); | 277 uint8_t* pBlock = FX_Alloc(uint8_t, m_nGrowSize); |
| 278 m_Blocks.SetAt(iCount++, pBlock); | 278 m_Blocks.SetAt(iCount++, pBlock); |
| 279 m_nTotalSize += m_nGrowSize; | 279 m_nTotalSize += m_nGrowSize; |
| 280 } | 280 } |
| 281 return TRUE; | 281 return true; |
| 282 } | 282 } |
| 283 | 283 |
| 284 IFX_SeekableStream* CFX_CRTFileStream::Retain() { | 284 IFX_SeekableStream* CFX_CRTFileStream::Retain() { |
| 285 m_dwCount++; | 285 m_dwCount++; |
| 286 return this; | 286 return this; |
| 287 } | 287 } |
| 288 | 288 |
| 289 void CFX_CRTFileStream::Release() { | 289 void CFX_CRTFileStream::Release() { |
| 290 uint32_t nCount = --m_dwCount; | 290 uint32_t nCount = --m_dwCount; |
| 291 if (!nCount) { | 291 if (!nCount) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 int32_t iLength, | 387 int32_t iLength, |
| 388 int32_t* pUsedLen) { | 388 int32_t* pUsedLen) { |
| 389 ASSERT(pwsStr); | 389 ASSERT(pwsStr); |
| 390 if (iLength < 0) { | 390 if (iLength < 0) { |
| 391 iLength = (int32_t)FXSYS_wcslen(pwsStr); | 391 iLength = (int32_t)FXSYS_wcslen(pwsStr); |
| 392 } | 392 } |
| 393 if (iLength == 0) { | 393 if (iLength == 0) { |
| 394 return 0.0f; | 394 return 0.0f; |
| 395 } | 395 } |
| 396 int32_t iUsedLen = 0; | 396 int32_t iUsedLen = 0; |
| 397 FX_BOOL bNegtive = FALSE; | 397 bool bNegtive = false; |
| 398 switch (pwsStr[iUsedLen]) { | 398 switch (pwsStr[iUsedLen]) { |
| 399 case '-': | 399 case '-': |
| 400 bNegtive = TRUE; | 400 bNegtive = true; |
| 401 case '+': | 401 case '+': |
| 402 iUsedLen++; | 402 iUsedLen++; |
| 403 break; | 403 break; |
| 404 } | 404 } |
| 405 FX_FLOAT fValue = 0.0f; | 405 FX_FLOAT fValue = 0.0f; |
| 406 while (iUsedLen < iLength) { | 406 while (iUsedLen < iLength) { |
| 407 FX_WCHAR wch = pwsStr[iUsedLen]; | 407 FX_WCHAR wch = pwsStr[iUsedLen]; |
| 408 if (wch >= L'0' && wch <= L'9') { | 408 if (wch >= L'0' && wch <= L'9') { |
| 409 fValue = fValue * 10.0f + (wch - L'0'); | 409 fValue = fValue * 10.0f + (wch - L'0'); |
| 410 } else { | 410 } else { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 } | 489 } |
| 490 | 490 |
| 491 void* FX_Random_MT_Start(uint32_t dwSeed) { | 491 void* FX_Random_MT_Start(uint32_t dwSeed) { |
| 492 FX_MTRANDOMCONTEXT* pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); | 492 FX_MTRANDOMCONTEXT* pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); |
| 493 pContext->mt[0] = dwSeed; | 493 pContext->mt[0] = dwSeed; |
| 494 uint32_t& i = pContext->mti; | 494 uint32_t& i = pContext->mti; |
| 495 uint32_t* pBuf = pContext->mt; | 495 uint32_t* pBuf = pContext->mt; |
| 496 for (i = 1; i < MT_N; i++) { | 496 for (i = 1; i < MT_N; i++) { |
| 497 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); | 497 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); |
| 498 } | 498 } |
| 499 pContext->bHaveSeed = TRUE; | 499 pContext->bHaveSeed = true; |
| 500 return pContext; | 500 return pContext; |
| 501 } | 501 } |
| 502 uint32_t FX_Random_MT_Generate(void* pContext) { | 502 uint32_t FX_Random_MT_Generate(void* pContext) { |
| 503 ASSERT(pContext); | 503 ASSERT(pContext); |
| 504 FX_MTRANDOMCONTEXT* pMTC = static_cast<FX_MTRANDOMCONTEXT*>(pContext); | 504 FX_MTRANDOMCONTEXT* pMTC = static_cast<FX_MTRANDOMCONTEXT*>(pContext); |
| 505 uint32_t v; | 505 uint32_t v; |
| 506 static uint32_t mag[2] = {0, MT_Matrix_A}; | 506 static uint32_t mag[2] = {0, MT_Matrix_A}; |
| 507 uint32_t& mti = pMTC->mti; | 507 uint32_t& mti = pMTC->mti; |
| 508 uint32_t* pBuf = pMTC->mt; | 508 uint32_t* pBuf = pMTC->mt; |
| 509 if ((int)mti < 0 || mti >= MT_N) { | 509 if ((int)mti < 0 || mti >= MT_N) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 continue; | 568 continue; |
| 569 } | 569 } |
| 570 | 570 |
| 571 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); | 571 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); |
| 572 #endif | 572 #endif |
| 573 while (iCount-- > 0) { | 573 while (iCount-- > 0) { |
| 574 *pBuffer++ = (uint32_t)((::rand() << 16) | (::rand() & 0xFFFF)); | 574 *pBuffer++ = (uint32_t)((::rand() << 16) | (::rand() & 0xFFFF)); |
| 575 } | 575 } |
| 576 } | 576 } |
| 577 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 577 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 578 FX_BOOL FX_GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount) { | 578 bool FX_GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount) { |
| 579 HCRYPTPROV hCP = 0; | 579 HCRYPTPROV hCP = 0; |
| 580 if (!::CryptAcquireContext(&hCP, nullptr, nullptr, PROV_RSA_FULL, 0) || | 580 if (!::CryptAcquireContext(&hCP, nullptr, nullptr, PROV_RSA_FULL, 0) || |
| 581 !hCP) { | 581 !hCP) { |
| 582 return FALSE; | 582 return false; |
| 583 } | 583 } |
| 584 ::CryptGenRandom(hCP, iCount * sizeof(uint32_t), (uint8_t*)pBuffer); | 584 ::CryptGenRandom(hCP, iCount * sizeof(uint32_t), (uint8_t*)pBuffer); |
| 585 ::CryptReleaseContext(hCP, 0); | 585 ::CryptReleaseContext(hCP, 0); |
| 586 return TRUE; | 586 return true; |
| 587 } | 587 } |
| 588 #endif | 588 #endif |
| 589 void FX_Random_GenerateCrypto(uint32_t* pBuffer, int32_t iCount) { | 589 void FX_Random_GenerateCrypto(uint32_t* pBuffer, int32_t iCount) { |
| 590 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 590 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 591 FX_GenerateCryptoRandom(pBuffer, iCount); | 591 FX_GenerateCryptoRandom(pBuffer, iCount); |
| 592 #else | 592 #else |
| 593 FX_Random_GenerateBase(pBuffer, iCount); | 593 FX_Random_GenerateBase(pBuffer, iCount); |
| 594 #endif | 594 #endif |
| 595 } | 595 } |
| 596 | 596 |
| 597 #ifdef PDF_ENABLE_XFA | 597 #ifdef PDF_ENABLE_XFA |
| 598 void FX_GUID_CreateV4(FX_LPGUID pGUID) { | 598 void FX_GUID_CreateV4(FX_LPGUID pGUID) { |
| 599 FX_Random_GenerateMT((uint32_t*)pGUID, 4); | 599 FX_Random_GenerateMT((uint32_t*)pGUID, 4); |
| 600 uint8_t& b = ((uint8_t*)pGUID)[6]; | 600 uint8_t& b = ((uint8_t*)pGUID)[6]; |
| 601 b = (b & 0x0F) | 0x40; | 601 b = (b & 0x0F) | 0x40; |
| 602 } | 602 } |
| 603 const FX_CHAR* gs_FX_pHexChars = "0123456789ABCDEF"; | 603 const FX_CHAR* gs_FX_pHexChars = "0123456789ABCDEF"; |
| 604 void FX_GUID_ToString(FX_LPCGUID pGUID, | 604 void FX_GUID_ToString(FX_LPCGUID pGUID, |
| 605 CFX_ByteString& bsStr, | 605 CFX_ByteString& bsStr, |
| 606 FX_BOOL bSeparator) { | 606 bool bSeparator) { |
| 607 FX_CHAR* pBuf = bsStr.GetBuffer(40); | 607 FX_CHAR* pBuf = bsStr.GetBuffer(40); |
| 608 uint8_t b; | 608 uint8_t b; |
| 609 for (int32_t i = 0; i < 16; i++) { | 609 for (int32_t i = 0; i < 16; i++) { |
| 610 b = ((const uint8_t*)pGUID)[i]; | 610 b = ((const uint8_t*)pGUID)[i]; |
| 611 *pBuf++ = gs_FX_pHexChars[b >> 4]; | 611 *pBuf++ = gs_FX_pHexChars[b >> 4]; |
| 612 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; | 612 *pBuf++ = gs_FX_pHexChars[b & 0x0F]; |
| 613 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { | 613 if (bSeparator && (i == 3 || i == 5 || i == 7 || i == 9)) { |
| 614 *pBuf++ = L'-'; | 614 *pBuf++ = L'-'; |
| 615 } | 615 } |
| 616 } | 616 } |
| 617 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); | 617 bsStr.ReleaseBuffer(bSeparator ? 36 : 32); |
| 618 } | 618 } |
| 619 #endif // PDF_ENABLE_XFA | 619 #endif // PDF_ENABLE_XFA |
| OLD | NEW |