Chromium Code Reviews| 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 "xfa/fgas/crt/fgas_stream.h" | 7 #include "xfa/fgas/crt/fgas_stream.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 virtual ~CFGAS_FileRead(); | 267 virtual ~CFGAS_FileRead(); |
| 268 virtual void Release() { delete this; } | 268 virtual void Release() { delete this; } |
| 269 virtual FX_FILESIZE GetSize(); | 269 virtual FX_FILESIZE GetSize(); |
| 270 virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size); | 270 virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size); |
| 271 | 271 |
| 272 protected: | 272 protected: |
| 273 FX_BOOL m_bReleaseStream; | 273 FX_BOOL m_bReleaseStream; |
| 274 IFX_Stream* m_pStream; | 274 IFX_Stream* m_pStream; |
| 275 }; | 275 }; |
| 276 | 276 |
| 277 class CFX_BufferAccImp : public IFX_FileRead { | 277 int32_t filelength(FXSYS_FILE* file) { |
|
Tom Sepez
2016/05/18 22:01:21
Our naming would have functions starting with a ca
dsinclair
2016/05/19 14:24:44
Done.
| |
| 278 public: | 278 ASSERT(file != NULL); |
| 279 CFX_BufferAccImp(IFX_BufferRead* pBufferRead, | 279 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ |
| 280 FX_FILESIZE iFileSize, | 280 return _filelength(_fileno(file)); |
| 281 FX_BOOL bReleaseStream); | 281 #else |
| 282 virtual ~CFX_BufferAccImp(); | 282 int32_t iPos = FXSYS_ftell(file); |
| 283 virtual void Release() { delete this; } | 283 FXSYS_fseek(file, 0, FXSYS_SEEK_END); |
| 284 virtual FX_FILESIZE GetSize(); | 284 int32_t iLen = FXSYS_ftell(file); |
| 285 virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size); | 285 FXSYS_fseek(file, iPos, FXSYS_SEEK_SET); |
| 286 return iLen; | |
| 287 #endif | |
| 288 } | |
| 286 | 289 |
| 287 protected: | 290 FX_BOOL fsetsize(FXSYS_FILE* file, int32_t size) { |
|
Tom Sepez
2016/05/18 22:01:21
maybe FileSetSize()
dsinclair
2016/05/19 14:24:44
Done.
| |
| 288 IFX_BufferRead* m_pBufferRead; | 291 ASSERT(file != NULL); |
| 289 FX_BOOL m_bReleaseStream; | 292 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ |
| 290 FX_FILESIZE m_iBufSize; | 293 return _chsize(_fileno(file), size) == 0; |
| 291 }; | 294 #elif _FX_OS_ == _FX_WIN32_MOBILE_ |
| 292 | 295 HANDLE hFile = _fileno(file); |
| 293 class CFGAS_FileWrite : public IFX_FileWrite { | 296 uint32_t dwPos = ::SetFilePointer(hFile, 0, 0, FILE_CURRENT); |
| 294 public: | 297 ::SetFilePointer(hFile, size, 0, FILE_BEGIN); |
| 295 CFGAS_FileWrite(IFX_Stream* pStream, FX_BOOL bReleaseStream); | 298 FX_BOOL bRet = ::SetEndOfFile(hFile); |
| 296 virtual ~CFGAS_FileWrite(); | 299 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); |
| 297 virtual void Release() { delete this; } | 300 return bRet; |
| 298 virtual FX_FILESIZE GetSize(); | 301 #else |
| 299 virtual FX_BOOL Flush(); | 302 return FALSE; |
| 300 virtual FX_BOOL WriteBlock(const void* pData, size_t size); | 303 #endif |
| 301 virtual FX_BOOL WriteBlock(const void* pData, | 304 } |
| 302 FX_FILESIZE offset, | |
| 303 size_t size); | |
| 304 | |
| 305 protected: | |
| 306 IFX_Stream* m_pStream; | |
| 307 FX_BOOL m_bReleaseStream; | |
| 308 }; | |
| 309 | 305 |
| 310 } // namespace | 306 } // namespace |
| 311 | 307 |
| 312 IFX_Stream* IFX_Stream::CreateStream(IFX_BufferRead* pBufferRead, | 308 IFX_Stream* IFX_Stream::CreateStream(IFX_BufferRead* pBufferRead, |
| 313 uint32_t dwAccess, | 309 uint32_t dwAccess, |
| 314 int32_t iFileSize, | 310 int32_t iFileSize, |
| 315 FX_BOOL bReleaseBufferRead) { | 311 FX_BOOL bReleaseBufferRead) { |
| 316 CFX_Stream* pSR = new CFX_Stream; | 312 CFX_Stream* pSR = new CFX_Stream; |
| 317 if (!pSR->LoadBufferRead(pBufferRead, iFileSize, dwAccess, | 313 if (!pSR->LoadBufferRead(pBufferRead, iFileSize, dwAccess, |
| 318 bReleaseBufferRead)) { | 314 bReleaseBufferRead)) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 if (dwAccess & FX_STREAMACCESS_Write) { | 402 if (dwAccess & FX_STREAMACCESS_Write) { |
| 407 if (dwAccess & FX_STREAMACCESS_Create) | 403 if (dwAccess & FX_STREAMACCESS_Create) |
| 408 m_hFile = FXSYS_wfopen(pszSrcFileName, L"w+b"); | 404 m_hFile = FXSYS_wfopen(pszSrcFileName, L"w+b"); |
| 409 | 405 |
| 410 if (!m_hFile) { | 406 if (!m_hFile) { |
| 411 m_hFile = FXSYS_wfopen(pszSrcFileName, L"r+b"); | 407 m_hFile = FXSYS_wfopen(pszSrcFileName, L"r+b"); |
| 412 if (!m_hFile) | 408 if (!m_hFile) |
| 413 return FALSE; | 409 return FALSE; |
| 414 | 410 |
| 415 if (dwAccess & FX_STREAMACCESS_Truncate) | 411 if (dwAccess & FX_STREAMACCESS_Truncate) |
| 416 FX_fsetsize(m_hFile, 0); | 412 fsetsize(m_hFile, 0); |
| 417 } | 413 } |
| 418 } else { | 414 } else { |
| 419 return FALSE; | 415 return FALSE; |
| 420 } | 416 } |
| 421 } | 417 } |
| 422 #else | 418 #else |
| 423 const FX_CHAR* wsMode = "rb"; | 419 const FX_CHAR* wsMode = "rb"; |
| 424 if (dwAccess & FX_STREAMACCESS_Write) { | 420 if (dwAccess & FX_STREAMACCESS_Write) { |
| 425 if (dwAccess & FX_STREAMACCESS_Append) { | 421 if (dwAccess & FX_STREAMACCESS_Append) { |
| 426 wsMode = "a+b"; | 422 wsMode = "a+b"; |
| 427 } else if (dwAccess & FX_STREAMACCESS_Truncate) { | 423 } else if (dwAccess & FX_STREAMACCESS_Truncate) { |
| 428 wsMode = "w+b"; | 424 wsMode = "w+b"; |
| 429 } else { | 425 } else { |
| 430 wsMode = "r+b"; | 426 wsMode = "r+b"; |
| 431 } | 427 } |
| 432 } | 428 } |
| 433 CFX_ByteString szFileName = CFX_ByteString::FromUnicode(pszSrcFileName); | 429 CFX_ByteString szFileName = CFX_ByteString::FromUnicode(pszSrcFileName); |
| 434 m_hFile = FXSYS_fopen(szFileName.c_str(), wsMode); | 430 m_hFile = FXSYS_fopen(szFileName.c_str(), wsMode); |
| 435 if (m_hFile == NULL) { | 431 if (m_hFile == NULL) { |
| 436 if (dwAccess & FX_STREAMACCESS_Write) { | 432 if (dwAccess & FX_STREAMACCESS_Write) { |
| 437 if (dwAccess & FX_STREAMACCESS_Create) { | 433 if (dwAccess & FX_STREAMACCESS_Create) { |
| 438 m_hFile = FXSYS_fopen(szFileName.c_str(), "w+b"); | 434 m_hFile = FXSYS_fopen(szFileName.c_str(), "w+b"); |
| 439 } | 435 } |
| 440 if (m_hFile == NULL) { | 436 if (m_hFile == NULL) { |
| 441 m_hFile = FXSYS_fopen(szFileName.c_str(), "r+b"); | 437 m_hFile = FXSYS_fopen(szFileName.c_str(), "r+b"); |
| 442 if (m_hFile == NULL) { | 438 if (m_hFile == NULL) { |
| 443 return FALSE; | 439 return FALSE; |
| 444 } | 440 } |
| 445 if (dwAccess & FX_STREAMACCESS_Truncate) { | 441 if (dwAccess & FX_STREAMACCESS_Truncate) { |
| 446 FX_fsetsize(m_hFile, 0); | 442 fsetsize(m_hFile, 0); |
| 447 } | 443 } |
| 448 } | 444 } |
| 449 } else { | 445 } else { |
| 450 return FALSE; | 446 return FALSE; |
| 451 } | 447 } |
| 452 } | 448 } |
| 453 #endif | 449 #endif |
| 454 m_dwAccess = dwAccess; | 450 m_dwAccess = dwAccess; |
| 455 if ((dwAccess & (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) == | 451 if ((dwAccess & (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) == |
| 456 (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) { | 452 (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) { |
| 457 m_iLength = 0; | 453 m_iLength = 0; |
| 458 } else { | 454 } else { |
| 459 m_iLength = FX_filelength(m_hFile); | 455 m_iLength = filelength(m_hFile); |
| 460 } | 456 } |
| 461 return TRUE; | 457 return TRUE; |
| 462 } | 458 } |
| 463 int32_t CFX_FileStreamImp::GetLength() const { | 459 int32_t CFX_FileStreamImp::GetLength() const { |
| 464 ASSERT(m_hFile != NULL); | 460 ASSERT(m_hFile != NULL); |
| 465 return m_iLength; | 461 return m_iLength; |
| 466 } | 462 } |
| 467 int32_t CFX_FileStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { | 463 int32_t CFX_FileStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { |
| 468 ASSERT(m_hFile != NULL); | 464 ASSERT(m_hFile != NULL); |
| 469 FXSYS_fseek(m_hFile, iOffset, eSeek); | 465 FXSYS_fseek(m_hFile, iOffset, eSeek); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 } | 527 } |
| 532 } | 528 } |
| 533 return iRet; | 529 return iRet; |
| 534 } | 530 } |
| 535 void CFX_FileStreamImp::Flush() { | 531 void CFX_FileStreamImp::Flush() { |
| 536 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); | 532 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); |
| 537 FXSYS_fflush(m_hFile); | 533 FXSYS_fflush(m_hFile); |
| 538 } | 534 } |
| 539 FX_BOOL CFX_FileStreamImp::SetLength(int32_t iLength) { | 535 FX_BOOL CFX_FileStreamImp::SetLength(int32_t iLength) { |
| 540 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); | 536 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); |
| 541 FX_BOOL bRet = FX_fsetsize(m_hFile, iLength); | 537 FX_BOOL bRet = fsetsize(m_hFile, iLength); |
| 542 m_iLength = FX_filelength(m_hFile); | 538 m_iLength = filelength(m_hFile); |
| 543 return bRet; | 539 return bRet; |
| 544 } | 540 } |
| 545 CFX_FileReadStreamImp::CFX_FileReadStreamImp() | 541 CFX_FileReadStreamImp::CFX_FileReadStreamImp() |
| 546 : m_pFileRead(NULL), m_iPosition(0), m_iLength(0) {} | 542 : m_pFileRead(NULL), m_iPosition(0), m_iLength(0) {} |
| 547 FX_BOOL CFX_FileReadStreamImp::LoadFileRead(IFX_FileRead* pFileRead, | 543 FX_BOOL CFX_FileReadStreamImp::LoadFileRead(IFX_FileRead* pFileRead, |
| 548 uint32_t dwAccess) { | 544 uint32_t dwAccess) { |
| 549 ASSERT(m_pFileRead == NULL && pFileRead != NULL); | 545 ASSERT(m_pFileRead == NULL && pFileRead != NULL); |
| 550 if (dwAccess & FX_STREAMACCESS_Write) { | 546 if (dwAccess & FX_STREAMACCESS_Write) { |
| 551 return FALSE; | 547 return FALSE; |
| 552 } | 548 } |
| (...skipping 936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1489 FX_FILESIZE CFGAS_FileRead::GetSize() { | 1485 FX_FILESIZE CFGAS_FileRead::GetSize() { |
| 1490 return (FX_FILESIZE)m_pStream->GetLength(); | 1486 return (FX_FILESIZE)m_pStream->GetLength(); |
| 1491 } | 1487 } |
| 1492 FX_BOOL CFGAS_FileRead::ReadBlock(void* buffer, | 1488 FX_BOOL CFGAS_FileRead::ReadBlock(void* buffer, |
| 1493 FX_FILESIZE offset, | 1489 FX_FILESIZE offset, |
| 1494 size_t size) { | 1490 size_t size) { |
| 1495 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); | 1491 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); |
| 1496 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); | 1492 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); |
| 1497 return iLen == (int32_t)size; | 1493 return iLen == (int32_t)size; |
| 1498 } | 1494 } |
| 1499 | |
| 1500 IFX_FileRead* FX_CreateFileRead(IFX_BufferRead* pBufferRead, | |
| 1501 FX_FILESIZE iFileSize, | |
| 1502 FX_BOOL bReleaseStream) { | |
| 1503 if (!pBufferRead) { | |
| 1504 return NULL; | |
| 1505 } | |
| 1506 return new CFX_BufferAccImp(pBufferRead, iFileSize, bReleaseStream); | |
| 1507 } | |
| 1508 CFX_BufferAccImp::CFX_BufferAccImp(IFX_BufferRead* pBufferRead, | |
| 1509 FX_FILESIZE iFileSize, | |
| 1510 FX_BOOL bReleaseStream) | |
| 1511 : m_pBufferRead(pBufferRead), | |
| 1512 m_bReleaseStream(bReleaseStream), | |
| 1513 m_iBufSize(iFileSize) { | |
| 1514 ASSERT(m_pBufferRead); | |
| 1515 } | |
| 1516 CFX_BufferAccImp::~CFX_BufferAccImp() { | |
| 1517 if (m_bReleaseStream && m_pBufferRead) { | |
| 1518 m_pBufferRead->Release(); | |
| 1519 } | |
| 1520 } | |
| 1521 FX_FILESIZE CFX_BufferAccImp::GetSize() { | |
| 1522 if (!m_pBufferRead) { | |
| 1523 return 0; | |
| 1524 } | |
| 1525 if (m_iBufSize >= 0) { | |
| 1526 return m_iBufSize; | |
| 1527 } | |
| 1528 if (!m_pBufferRead->ReadNextBlock(TRUE)) { | |
| 1529 return 0; | |
| 1530 } | |
| 1531 m_iBufSize = (FX_FILESIZE)m_pBufferRead->GetBlockSize(); | |
| 1532 while (!m_pBufferRead->IsEOF()) { | |
| 1533 m_pBufferRead->ReadNextBlock(FALSE); | |
| 1534 m_iBufSize += (FX_FILESIZE)m_pBufferRead->GetBlockSize(); | |
| 1535 } | |
| 1536 return m_iBufSize; | |
| 1537 } | |
| 1538 FX_BOOL CFX_BufferAccImp::ReadBlock(void* buffer, | |
| 1539 FX_FILESIZE offset, | |
| 1540 size_t size) { | |
| 1541 if (!m_pBufferRead) { | |
| 1542 return FALSE; | |
| 1543 } | |
| 1544 if (!buffer || !size) { | |
| 1545 return TRUE; | |
| 1546 } | |
| 1547 FX_FILESIZE dwBufSize = GetSize(); | |
| 1548 if (offset >= dwBufSize) { | |
| 1549 return FALSE; | |
| 1550 } | |
| 1551 size_t dwBlockSize = m_pBufferRead->GetBlockSize(); | |
| 1552 FX_FILESIZE dwBlockOffset = m_pBufferRead->GetBlockOffset(); | |
| 1553 if (offset < dwBlockOffset) { | |
| 1554 if (!m_pBufferRead->ReadNextBlock(TRUE)) { | |
| 1555 return FALSE; | |
| 1556 } | |
| 1557 dwBlockSize = m_pBufferRead->GetBlockSize(); | |
| 1558 dwBlockOffset = m_pBufferRead->GetBlockOffset(); | |
| 1559 } | |
| 1560 while (offset < dwBlockOffset || | |
| 1561 offset >= (FX_FILESIZE)(dwBlockOffset + dwBlockSize)) { | |
| 1562 if (m_pBufferRead->IsEOF() || !m_pBufferRead->ReadNextBlock(FALSE)) { | |
| 1563 break; | |
| 1564 } | |
| 1565 dwBlockSize = m_pBufferRead->GetBlockSize(); | |
| 1566 dwBlockOffset = m_pBufferRead->GetBlockOffset(); | |
| 1567 } | |
| 1568 if (offset < dwBlockOffset || | |
| 1569 offset >= (FX_FILESIZE)(dwBlockOffset + dwBlockSize)) { | |
| 1570 return FALSE; | |
| 1571 } | |
| 1572 const uint8_t* pBuffer = m_pBufferRead->GetBlockBuffer(); | |
| 1573 const FX_FILESIZE dwOffset = offset - dwBlockOffset; | |
| 1574 size_t dwCopySize = | |
| 1575 std::min(size, static_cast<size_t>(dwBlockSize - dwOffset)); | |
| 1576 FXSYS_memcpy(buffer, pBuffer + dwOffset, dwCopySize); | |
| 1577 offset = dwCopySize; | |
| 1578 size -= dwCopySize; | |
| 1579 while (size) { | |
| 1580 if (!m_pBufferRead->ReadNextBlock(FALSE)) { | |
| 1581 break; | |
| 1582 } | |
| 1583 dwBlockOffset = m_pBufferRead->GetBlockOffset(); | |
| 1584 dwBlockSize = m_pBufferRead->GetBlockSize(); | |
| 1585 pBuffer = m_pBufferRead->GetBlockBuffer(); | |
| 1586 dwCopySize = std::min(size, dwBlockSize); | |
| 1587 FXSYS_memcpy(((uint8_t*)buffer) + offset, pBuffer, dwCopySize); | |
| 1588 offset += dwCopySize; | |
| 1589 size -= dwCopySize; | |
| 1590 } | |
| 1591 return TRUE; | |
| 1592 } | |
| 1593 | |
| 1594 IFX_FileWrite* FX_CreateFileWrite(IFX_Stream* pBaseStream, | |
| 1595 FX_BOOL bReleaseStream) { | |
| 1596 ASSERT(pBaseStream != NULL); | |
| 1597 return new CFGAS_FileWrite(pBaseStream, bReleaseStream); | |
| 1598 } | |
| 1599 | |
| 1600 CFGAS_FileWrite::CFGAS_FileWrite(IFX_Stream* pStream, FX_BOOL bReleaseStream) | |
| 1601 : m_pStream(pStream), m_bReleaseStream(bReleaseStream) { | |
| 1602 ASSERT(m_pStream != NULL); | |
| 1603 } | |
| 1604 CFGAS_FileWrite::~CFGAS_FileWrite() { | |
| 1605 if (m_bReleaseStream) { | |
| 1606 m_pStream->Release(); | |
| 1607 } | |
| 1608 } | |
| 1609 FX_FILESIZE CFGAS_FileWrite::GetSize() { | |
| 1610 return m_pStream->GetLength(); | |
| 1611 } | |
| 1612 FX_BOOL CFGAS_FileWrite::Flush() { | |
| 1613 m_pStream->Flush(); | |
| 1614 return TRUE; | |
| 1615 } | |
| 1616 FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData, size_t size) { | |
| 1617 return m_pStream->WriteData((const uint8_t*)pData, (int32_t)size) == | |
| 1618 (int32_t)size; | |
| 1619 } | |
| 1620 FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData, | |
| 1621 FX_FILESIZE offset, | |
| 1622 size_t size) { | |
| 1623 m_pStream->Seek(FX_STREAMSEEK_Begin, offset); | |
| 1624 int32_t iLen = m_pStream->WriteData((uint8_t*)pData, (int32_t)size); | |
| 1625 return iLen == (int32_t)size; | |
| 1626 } | |
| OLD | NEW |