| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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/fpdfapi/parser/cpdf_data_avail.h" | 7 #include "core/fpdfapi/parser/cpdf_data_avail.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "third_party/base/numerics/safe_conversions.h" | 26 #include "third_party/base/numerics/safe_conversions.h" |
| 27 #include "third_party/base/stl_util.h" | 27 #include "third_party/base/stl_util.h" |
| 28 | 28 |
| 29 CPDF_DataAvail::FileAvail::~FileAvail() {} | 29 CPDF_DataAvail::FileAvail::~FileAvail() {} |
| 30 | 30 |
| 31 CPDF_DataAvail::DownloadHints::~DownloadHints() {} | 31 CPDF_DataAvail::DownloadHints::~DownloadHints() {} |
| 32 | 32 |
| 33 // static | 33 // static |
| 34 int CPDF_DataAvail::s_CurrentDataAvailRecursionDepth = 0; | 34 int CPDF_DataAvail::s_CurrentDataAvailRecursionDepth = 0; |
| 35 | 35 |
| 36 CPDF_DataAvail::CPDF_DataAvail(FileAvail* pFileAvail, | 36 CPDF_DataAvail::CPDF_DataAvail( |
| 37 IFX_SeekableReadStream* pFileRead, | 37 FileAvail* pFileAvail, |
| 38 bool bSupportHintTable) | 38 const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 39 bool bSupportHintTable) |
| 39 : m_pFileAvail(pFileAvail), m_pFileRead(pFileRead) { | 40 : m_pFileAvail(pFileAvail), m_pFileRead(pFileRead) { |
| 40 m_Pos = 0; | 41 m_Pos = 0; |
| 41 m_dwFileLen = 0; | 42 m_dwFileLen = 0; |
| 42 if (m_pFileRead) { | 43 if (m_pFileRead) { |
| 43 m_dwFileLen = (uint32_t)m_pFileRead->GetSize(); | 44 m_dwFileLen = (uint32_t)m_pFileRead->GetSize(); |
| 44 } | 45 } |
| 45 m_dwCurrentOffset = 0; | 46 m_dwCurrentOffset = 0; |
| 46 m_dwXRefOffset = 0; | 47 m_dwXRefOffset = 0; |
| 47 m_bufferOffset = 0; | 48 m_bufferOffset = 0; |
| 48 m_bufferSize = 0; | 49 m_bufferSize = 0; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 65 m_bAcroFormLoad = false; | 66 m_bAcroFormLoad = false; |
| 66 m_bPageLoadedOK = false; | 67 m_bPageLoadedOK = false; |
| 67 m_bNeedDownLoadResource = false; | 68 m_bNeedDownLoadResource = false; |
| 68 m_bLinearizedFormParamLoad = false; | 69 m_bLinearizedFormParamLoad = false; |
| 69 m_pTrailer = nullptr; | 70 m_pTrailer = nullptr; |
| 70 m_pCurrentParser = nullptr; | 71 m_pCurrentParser = nullptr; |
| 71 m_pAcroForm = nullptr; | 72 m_pAcroForm = nullptr; |
| 72 m_pPageDict = nullptr; | 73 m_pPageDict = nullptr; |
| 73 m_pPageResource = nullptr; | 74 m_pPageResource = nullptr; |
| 74 m_docStatus = PDF_DATAAVAIL_HEADER; | 75 m_docStatus = PDF_DATAAVAIL_HEADER; |
| 75 m_parser.m_bOwnFileRead = false; | |
| 76 m_bTotalLoadPageTree = false; | 76 m_bTotalLoadPageTree = false; |
| 77 m_bCurPageDictLoadOK = false; | 77 m_bCurPageDictLoadOK = false; |
| 78 m_bLinearedDataOK = false; | 78 m_bLinearedDataOK = false; |
| 79 m_bSupportHintTable = bSupportHintTable; | 79 m_bSupportHintTable = bSupportHintTable; |
| 80 } | 80 } |
| 81 | 81 |
| 82 CPDF_DataAvail::~CPDF_DataAvail() { | 82 CPDF_DataAvail::~CPDF_DataAvail() { |
| 83 m_pHintTables.reset(); | 83 m_pHintTables.reset(); |
| 84 for (CPDF_Object* pObject : m_arrayAcroforms) | 84 for (CPDF_Object* pObject : m_arrayAcroforms) |
| 85 delete pObject; | 85 delete pObject; |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 m_docStatus = PDF_DATAAVAIL_DONE; | 313 m_docStatus = PDF_DATAAVAIL_DONE; |
| 314 return true; | 314 return true; |
| 315 } | 315 } |
| 316 | 316 |
| 317 pHints->AddSegment(0, (uint32_t)m_dwFileLen); | 317 pHints->AddSegment(0, (uint32_t)m_dwFileLen); |
| 318 return false; | 318 return false; |
| 319 } | 319 } |
| 320 | 320 |
| 321 bool CPDF_DataAvail::LoadAllXref(DownloadHints* pHints) { | 321 bool CPDF_DataAvail::LoadAllXref(DownloadHints* pHints) { |
| 322 m_parser.m_pSyntax->InitParser(m_pFileRead, (uint32_t)m_dwHeaderOffset); | 322 m_parser.m_pSyntax->InitParser(m_pFileRead, (uint32_t)m_dwHeaderOffset); |
| 323 m_parser.m_bOwnFileRead = false; | |
| 324 if (!m_parser.LoadAllCrossRefV4(m_dwLastXRefOffset) && | 323 if (!m_parser.LoadAllCrossRefV4(m_dwLastXRefOffset) && |
| 325 !m_parser.LoadAllCrossRefV5(m_dwLastXRefOffset)) { | 324 !m_parser.LoadAllCrossRefV5(m_dwLastXRefOffset)) { |
| 326 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; | 325 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; |
| 327 return false; | 326 return false; |
| 328 } | 327 } |
| 329 | 328 |
| 330 m_dwRootObjNum = m_parser.GetRootObjNum(); | 329 m_dwRootObjNum = m_parser.GetRootObjNum(); |
| 331 m_dwInfoObjNum = m_parser.GetInfoObjNum(); | 330 m_dwInfoObjNum = m_parser.GetInfoObjNum(); |
| 332 m_pCurrentParser = &m_parser; | 331 m_pCurrentParser = &m_parser; |
| 333 m_docStatus = PDF_DATAAVAIL_ROOT; | 332 m_docStatus = PDF_DATAAVAIL_ROOT; |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 } | 710 } |
| 712 | 711 |
| 713 bool CPDF_DataAvail::IsLinearized() { | 712 bool CPDF_DataAvail::IsLinearized() { |
| 714 return !!m_pLinearized; | 713 return !!m_pLinearized; |
| 715 } | 714 } |
| 716 | 715 |
| 717 bool CPDF_DataAvail::IsLinearizedFile(uint8_t* pData, uint32_t dwLen) { | 716 bool CPDF_DataAvail::IsLinearizedFile(uint8_t* pData, uint32_t dwLen) { |
| 718 if (m_pLinearized) | 717 if (m_pLinearized) |
| 719 return true; | 718 return true; |
| 720 | 719 |
| 721 ScopedFileStream file(IFX_MemoryStream::Create(pData, (size_t)dwLen, false)); | 720 CFX_RetainPtr<IFX_MemoryStream> file = |
| 722 int32_t offset = GetHeaderOffset(file.get()); | 721 IFX_MemoryStream::Create(pData, (size_t)dwLen, false); |
| 722 int32_t offset = GetHeaderOffset(file); |
| 723 if (offset == -1) { | 723 if (offset == -1) { |
| 724 m_docStatus = PDF_DATAAVAIL_ERROR; | 724 m_docStatus = PDF_DATAAVAIL_ERROR; |
| 725 return false; | 725 return false; |
| 726 } | 726 } |
| 727 | 727 |
| 728 m_dwHeaderOffset = offset; | 728 m_dwHeaderOffset = offset; |
| 729 m_syntaxParser.InitParser(file.get(), offset); | 729 m_syntaxParser.InitParser(file, offset); |
| 730 m_syntaxParser.RestorePos(m_syntaxParser.m_HeaderOffset + 9); | 730 m_syntaxParser.RestorePos(m_syntaxParser.m_HeaderOffset + 9); |
| 731 | 731 |
| 732 bool bNumber; | 732 bool bNumber; |
| 733 CFX_ByteString wordObjNum = m_syntaxParser.GetNextWord(&bNumber); | 733 CFX_ByteString wordObjNum = m_syntaxParser.GetNextWord(&bNumber); |
| 734 if (!bNumber) | 734 if (!bNumber) |
| 735 return false; | 735 return false; |
| 736 | 736 |
| 737 uint32_t objnum = FXSYS_atoui(wordObjNum.c_str()); | 737 uint32_t objnum = FXSYS_atoui(wordObjNum.c_str()); |
| 738 m_pLinearized = CPDF_LinearizedHeader::CreateForObject( | 738 m_pLinearized = CPDF_LinearizedHeader::CreateForObject( |
| 739 ParseIndirectObjectAt(m_syntaxParser.m_HeaderOffset + 9, objnum)); | 739 ParseIndirectObjectAt(m_syntaxParser.m_HeaderOffset + 9, objnum)); |
| 740 if (!m_pLinearized || | 740 if (!m_pLinearized || |
| 741 m_pLinearized->GetFileSize() != m_pFileRead->GetSize()) { | 741 m_pLinearized->GetFileSize() != m_pFileRead->GetSize()) { |
| 742 m_pLinearized.reset(); | 742 m_pLinearized.reset(); |
| 743 return false; | 743 return false; |
| 744 } | 744 } |
| 745 return true; | 745 return true; |
| 746 } | 746 } |
| 747 | 747 |
| 748 bool CPDF_DataAvail::CheckEnd(DownloadHints* pHints) { | 748 bool CPDF_DataAvail::CheckEnd(DownloadHints* pHints) { |
| 749 uint32_t req_pos = (uint32_t)(m_dwFileLen > 1024 ? m_dwFileLen - 1024 : 0); | 749 uint32_t req_pos = (uint32_t)(m_dwFileLen > 1024 ? m_dwFileLen - 1024 : 0); |
| 750 uint32_t dwSize = (uint32_t)(m_dwFileLen - req_pos); | 750 uint32_t dwSize = (uint32_t)(m_dwFileLen - req_pos); |
| 751 | 751 |
| 752 if (m_pFileAvail->IsDataAvail(req_pos, dwSize)) { | 752 if (m_pFileAvail->IsDataAvail(req_pos, dwSize)) { |
| 753 uint8_t buffer[1024]; | 753 uint8_t buffer[1024]; |
| 754 m_pFileRead->ReadBlock(buffer, req_pos, dwSize); | 754 m_pFileRead->ReadBlock(buffer, req_pos, dwSize); |
| 755 | 755 |
| 756 ScopedFileStream file( | 756 CFX_RetainPtr<IFX_MemoryStream> file = |
| 757 IFX_MemoryStream::Create(buffer, (size_t)dwSize, false)); | 757 IFX_MemoryStream::Create(buffer, (size_t)dwSize, false); |
| 758 m_syntaxParser.InitParser(file.get(), 0); | 758 m_syntaxParser.InitParser(file, 0); |
| 759 m_syntaxParser.RestorePos(dwSize - 1); | 759 m_syntaxParser.RestorePos(dwSize - 1); |
| 760 | 760 |
| 761 if (m_syntaxParser.SearchWord("startxref", true, false, dwSize)) { | 761 if (m_syntaxParser.SearchWord("startxref", true, false, dwSize)) { |
| 762 m_syntaxParser.GetNextWord(nullptr); | 762 m_syntaxParser.GetNextWord(nullptr); |
| 763 | 763 |
| 764 bool bNumber; | 764 bool bNumber; |
| 765 CFX_ByteString xrefpos_str = m_syntaxParser.GetNextWord(&bNumber); | 765 CFX_ByteString xrefpos_str = m_syntaxParser.GetNextWord(&bNumber); |
| 766 if (!bNumber) { | 766 if (!bNumber) { |
| 767 m_docStatus = PDF_DATAAVAIL_ERROR; | 767 m_docStatus = PDF_DATAAVAIL_ERROR; |
| 768 return false; | 768 return false; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 794 uint32_t req_size = | 794 uint32_t req_size = |
| 795 (uint32_t)(m_Pos + 512 > m_dwFileLen ? m_dwFileLen - m_Pos : 512); | 795 (uint32_t)(m_Pos + 512 > m_dwFileLen ? m_dwFileLen - m_Pos : 512); |
| 796 | 796 |
| 797 if (m_pFileAvail->IsDataAvail(m_Pos, req_size)) { | 797 if (m_pFileAvail->IsDataAvail(m_Pos, req_size)) { |
| 798 int32_t iSize = (int32_t)(m_Pos + req_size - m_dwCurrentXRefSteam); | 798 int32_t iSize = (int32_t)(m_Pos + req_size - m_dwCurrentXRefSteam); |
| 799 CFX_BinaryBuf buf(iSize); | 799 CFX_BinaryBuf buf(iSize); |
| 800 uint8_t* pBuf = buf.GetBuffer(); | 800 uint8_t* pBuf = buf.GetBuffer(); |
| 801 | 801 |
| 802 m_pFileRead->ReadBlock(pBuf, m_dwCurrentXRefSteam, iSize); | 802 m_pFileRead->ReadBlock(pBuf, m_dwCurrentXRefSteam, iSize); |
| 803 | 803 |
| 804 ScopedFileStream file(IFX_MemoryStream::Create(pBuf, (size_t)iSize, false)); | 804 CFX_RetainPtr<IFX_MemoryStream> file = |
| 805 m_parser.m_pSyntax->InitParser(file.get(), 0); | 805 IFX_MemoryStream::Create(pBuf, (size_t)iSize, false); |
| 806 m_parser.m_pSyntax->InitParser(file, 0); |
| 806 | 807 |
| 807 bool bNumber; | 808 bool bNumber; |
| 808 CFX_ByteString objnum = m_parser.m_pSyntax->GetNextWord(&bNumber); | 809 CFX_ByteString objnum = m_parser.m_pSyntax->GetNextWord(&bNumber); |
| 809 if (!bNumber) | 810 if (!bNumber) |
| 810 return -1; | 811 return -1; |
| 811 | 812 |
| 812 uint32_t objNum = FXSYS_atoui(objnum.c_str()); | 813 uint32_t objNum = FXSYS_atoui(objnum.c_str()); |
| 813 std::unique_ptr<CPDF_Object> pObj = | 814 std::unique_ptr<CPDF_Object> pObj = |
| 814 m_parser.ParseIndirectObjectAt(nullptr, 0, objNum); | 815 m_parser.ParseIndirectObjectAt(nullptr, 0, objNum); |
| 815 | 816 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 CFX_BinaryBuf buf(iSize); | 1041 CFX_BinaryBuf buf(iSize); |
| 1041 uint8_t* pBuf = buf.GetBuffer(); | 1042 uint8_t* pBuf = buf.GetBuffer(); |
| 1042 if (!pBuf) { | 1043 if (!pBuf) { |
| 1043 m_docStatus = PDF_DATAAVAIL_ERROR; | 1044 m_docStatus = PDF_DATAAVAIL_ERROR; |
| 1044 return false; | 1045 return false; |
| 1045 } | 1046 } |
| 1046 | 1047 |
| 1047 if (!m_pFileRead->ReadBlock(pBuf, m_dwTrailerOffset, iSize)) | 1048 if (!m_pFileRead->ReadBlock(pBuf, m_dwTrailerOffset, iSize)) |
| 1048 return false; | 1049 return false; |
| 1049 | 1050 |
| 1050 ScopedFileStream file(IFX_MemoryStream::Create(pBuf, (size_t)iSize, false)); | 1051 CFX_RetainPtr<IFX_MemoryStream> file = |
| 1051 m_syntaxParser.InitParser(file.get(), 0); | 1052 IFX_MemoryStream::Create(pBuf, (size_t)iSize, false); |
| 1053 m_syntaxParser.InitParser(file, 0); |
| 1052 | 1054 |
| 1053 std::unique_ptr<CPDF_Object> pTrailer( | 1055 std::unique_ptr<CPDF_Object> pTrailer( |
| 1054 m_syntaxParser.GetObject(nullptr, 0, 0, true)); | 1056 m_syntaxParser.GetObject(nullptr, 0, 0, true)); |
| 1055 if (!pTrailer) { | 1057 if (!pTrailer) { |
| 1056 m_Pos += m_syntaxParser.SavePos(); | 1058 m_Pos += m_syntaxParser.SavePos(); |
| 1057 pHints->AddSegment(m_Pos, iTrailerSize); | 1059 pHints->AddSegment(m_Pos, iTrailerSize); |
| 1058 return false; | 1060 return false; |
| 1059 } | 1061 } |
| 1060 | 1062 |
| 1061 if (!pTrailer->IsDictionary()) | 1063 if (!pTrailer->IsDictionary()) |
| (...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1699 return AreObjectsAvailable(obj_array, true, nullptr, dummy); | 1701 return AreObjectsAvailable(obj_array, true, nullptr, dummy); |
| 1700 } | 1702 } |
| 1701 | 1703 |
| 1702 CPDF_DataAvail::PageNode::PageNode() : m_type(PDF_PAGENODE_UNKNOWN) {} | 1704 CPDF_DataAvail::PageNode::PageNode() : m_type(PDF_PAGENODE_UNKNOWN) {} |
| 1703 | 1705 |
| 1704 CPDF_DataAvail::PageNode::~PageNode() { | 1706 CPDF_DataAvail::PageNode::~PageNode() { |
| 1705 for (int32_t i = 0; i < m_childNode.GetSize(); ++i) | 1707 for (int32_t i = 0; i < m_childNode.GetSize(); ++i) |
| 1706 delete m_childNode[i]; | 1708 delete m_childNode[i]; |
| 1707 m_childNode.RemoveAll(); | 1709 m_childNode.RemoveAll(); |
| 1708 } | 1710 } |
| OLD | NEW |