| 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/fpdf_parser/include/cpdf_parser.h" | 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 return pObjStream->GetDict()->GetIntegerBy("N"); | 44 return pObjStream->GetDict()->GetIntegerBy("N"); |
| 45 } | 45 } |
| 46 | 46 |
| 47 int32_t GetStreamFirst(CPDF_StreamAcc* pObjStream) { | 47 int32_t GetStreamFirst(CPDF_StreamAcc* pObjStream) { |
| 48 return pObjStream->GetDict()->GetIntegerBy("First"); | 48 return pObjStream->GetDict()->GetIntegerBy("First"); |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 CPDF_Parser::CPDF_Parser() | 53 CPDF_Parser::CPDF_Parser() |
| 54 : m_bOwnFileRead(true), | 54 : m_bHasParsed(false), |
| 55 m_bOwnFileRead(true), |
| 55 m_FileVersion(0), | 56 m_FileVersion(0), |
| 56 m_pTrailer(nullptr), | 57 m_pTrailer(nullptr), |
| 57 m_pEncryptDict(nullptr), | 58 m_pEncryptDict(nullptr), |
| 58 m_bVersionUpdated(false), | 59 m_bVersionUpdated(false), |
| 59 m_pLinearized(nullptr), | 60 m_pLinearized(nullptr), |
| 60 m_dwFirstPageNo(0), | 61 m_dwFirstPageNo(0), |
| 61 m_dwXrefStartObjNum(0) { | 62 m_dwXrefStartObjNum(0) { |
| 62 m_pSyntax.reset(new CPDF_SyntaxParser); | 63 m_pSyntax.reset(new CPDF_SyntaxParser); |
| 63 } | 64 } |
| 64 | 65 |
| 65 CPDF_Parser::~CPDF_Parser() { | 66 CPDF_Parser::~CPDF_Parser() { |
| 66 CloseParser(); | 67 if (m_pTrailer) |
| 68 m_pTrailer->Release(); |
| 69 |
| 70 ReleaseEncryptHandler(); |
| 71 SetEncryptDictionary(nullptr); |
| 72 |
| 73 if (m_bOwnFileRead && m_pSyntax->m_pFileAccess) { |
| 74 m_pSyntax->m_pFileAccess->Release(); |
| 75 m_pSyntax->m_pFileAccess = nullptr; |
| 76 } |
| 77 |
| 78 int32_t iLen = m_Trailers.GetSize(); |
| 79 for (int32_t i = 0; i < iLen; ++i) { |
| 80 if (CPDF_Dictionary* trailer = m_Trailers.GetAt(i)) |
| 81 trailer->Release(); |
| 82 } |
| 83 |
| 84 if (m_pLinearized) |
| 85 m_pLinearized->Release(); |
| 67 } | 86 } |
| 68 | 87 |
| 69 uint32_t CPDF_Parser::GetLastObjNum() const { | 88 uint32_t CPDF_Parser::GetLastObjNum() const { |
| 70 return m_ObjectInfo.empty() ? 0 : m_ObjectInfo.rbegin()->first; | 89 return m_ObjectInfo.empty() ? 0 : m_ObjectInfo.rbegin()->first; |
| 71 } | 90 } |
| 72 | 91 |
| 73 bool CPDF_Parser::IsValidObjectNumber(uint32_t objnum) const { | 92 bool CPDF_Parser::IsValidObjectNumber(uint32_t objnum) const { |
| 74 return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first; | 93 return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first; |
| 75 } | 94 } |
| 76 | 95 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 auto it = m_ObjectInfo.lower_bound(objnum); | 136 auto it = m_ObjectInfo.lower_bound(objnum); |
| 118 while (it != m_ObjectInfo.end()) { | 137 while (it != m_ObjectInfo.end()) { |
| 119 auto saved_it = it++; | 138 auto saved_it = it++; |
| 120 m_ObjectInfo.erase(saved_it); | 139 m_ObjectInfo.erase(saved_it); |
| 121 } | 140 } |
| 122 | 141 |
| 123 if (!pdfium::ContainsKey(m_ObjectInfo, objnum - 1)) | 142 if (!pdfium::ContainsKey(m_ObjectInfo, objnum - 1)) |
| 124 m_ObjectInfo[objnum - 1].pos = 0; | 143 m_ObjectInfo[objnum - 1].pos = 0; |
| 125 } | 144 } |
| 126 | 145 |
| 127 void CPDF_Parser::CloseParser() { | |
| 128 m_bVersionUpdated = false; | |
| 129 m_pDocument = nullptr; | |
| 130 | |
| 131 if (m_pTrailer) { | |
| 132 m_pTrailer->Release(); | |
| 133 m_pTrailer = nullptr; | |
| 134 } | |
| 135 ReleaseEncryptHandler(); | |
| 136 SetEncryptDictionary(nullptr); | |
| 137 | |
| 138 if (m_bOwnFileRead && m_pSyntax->m_pFileAccess) { | |
| 139 m_pSyntax->m_pFileAccess->Release(); | |
| 140 m_pSyntax->m_pFileAccess = nullptr; | |
| 141 } | |
| 142 | |
| 143 m_ObjectStreamMap.clear(); | |
| 144 m_ObjCache.clear(); | |
| 145 m_SortedOffset.clear(); | |
| 146 m_ObjectInfo.clear(); | |
| 147 | |
| 148 int32_t iLen = m_Trailers.GetSize(); | |
| 149 for (int32_t i = 0; i < iLen; ++i) { | |
| 150 if (CPDF_Dictionary* trailer = m_Trailers.GetAt(i)) | |
| 151 trailer->Release(); | |
| 152 } | |
| 153 m_Trailers.RemoveAll(); | |
| 154 | |
| 155 if (m_pLinearized) { | |
| 156 m_pLinearized->Release(); | |
| 157 m_pLinearized = nullptr; | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, | 146 CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, |
| 162 CPDF_Document* pDocument) { | 147 CPDF_Document* pDocument) { |
| 163 CloseParser(); | 148 ASSERT(!m_bHasParsed); |
| 149 m_bHasParsed = true; |
| 164 | 150 |
| 165 m_bXRefStream = FALSE; | 151 m_bXRefStream = FALSE; |
| 166 m_LastXRefOffset = 0; | 152 m_LastXRefOffset = 0; |
| 167 m_bOwnFileRead = true; | 153 m_bOwnFileRead = true; |
| 168 | 154 |
| 169 int32_t offset = GetHeaderOffset(pFileAccess); | 155 int32_t offset = GetHeaderOffset(pFileAccess); |
| 170 if (offset == -1) { | 156 if (offset == -1) { |
| 171 if (pFileAccess) | 157 if (pFileAccess) |
| 172 pFileAccess->Release(); | 158 pFileAccess->Release(); |
| 173 return FORMAT_ERROR; | 159 return FORMAT_ERROR; |
| (...skipping 1369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1543 | 1529 |
| 1544 return TRUE; | 1530 return TRUE; |
| 1545 } | 1531 } |
| 1546 m_pLinearized->Release(); | 1532 m_pLinearized->Release(); |
| 1547 m_pLinearized = nullptr; | 1533 m_pLinearized = nullptr; |
| 1548 return FALSE; | 1534 return FALSE; |
| 1549 } | 1535 } |
| 1550 | 1536 |
| 1551 CPDF_Parser::Error CPDF_Parser::StartLinearizedParse(IFX_FileRead* pFileAccess, | 1537 CPDF_Parser::Error CPDF_Parser::StartLinearizedParse(IFX_FileRead* pFileAccess, |
| 1552 CPDF_Document* pDocument) { | 1538 CPDF_Document* pDocument) { |
| 1553 CloseParser(); | 1539 ASSERT(!m_bHasParsed); |
| 1540 |
| 1554 m_bXRefStream = FALSE; | 1541 m_bXRefStream = FALSE; |
| 1555 m_LastXRefOffset = 0; | 1542 m_LastXRefOffset = 0; |
| 1556 m_bOwnFileRead = true; | 1543 m_bOwnFileRead = true; |
| 1557 | 1544 |
| 1558 int32_t offset = GetHeaderOffset(pFileAccess); | 1545 int32_t offset = GetHeaderOffset(pFileAccess); |
| 1559 if (offset == -1) | 1546 if (offset == -1) |
| 1560 return FORMAT_ERROR; | 1547 return FORMAT_ERROR; |
| 1561 | 1548 |
| 1562 if (!IsLinearizedFile(pFileAccess, offset)) { | 1549 if (!IsLinearizedFile(pFileAccess, offset)) { |
| 1563 m_pSyntax->m_pFileAccess = nullptr; | 1550 m_pSyntax->m_pFileAccess = nullptr; |
| 1564 return StartParse(pFileAccess, std::move(pDocument)); | 1551 return StartParse(pFileAccess, std::move(pDocument)); |
| 1565 } | 1552 } |
| 1553 m_bHasParsed = true; |
| 1554 m_pDocument = pDocument; |
| 1566 | 1555 |
| 1567 m_pDocument = pDocument; | |
| 1568 FX_FILESIZE dwFirstXRefOffset = m_pSyntax->SavePos(); | 1556 FX_FILESIZE dwFirstXRefOffset = m_pSyntax->SavePos(); |
| 1569 | 1557 |
| 1570 FX_BOOL bXRefRebuilt = FALSE; | 1558 FX_BOOL bXRefRebuilt = FALSE; |
| 1571 FX_BOOL bLoadV4 = LoadCrossRefV4(dwFirstXRefOffset, 0, FALSE); | 1559 FX_BOOL bLoadV4 = LoadCrossRefV4(dwFirstXRefOffset, 0, FALSE); |
| 1572 if (!bLoadV4 && !LoadCrossRefV5(&dwFirstXRefOffset, TRUE)) { | 1560 if (!bLoadV4 && !LoadCrossRefV5(&dwFirstXRefOffset, TRUE)) { |
| 1573 if (!RebuildCrossRef()) | 1561 if (!RebuildCrossRef()) |
| 1574 return FORMAT_ERROR; | 1562 return FORMAT_ERROR; |
| 1575 | 1563 |
| 1576 bXRefRebuilt = TRUE; | 1564 bXRefRebuilt = TRUE; |
| 1577 m_LastXRefOffset = 0; | 1565 m_LastXRefOffset = 0; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1673 if (!LoadLinearizedAllCrossRefV4(m_LastXRefOffset, m_dwXrefStartObjNum) && | 1661 if (!LoadLinearizedAllCrossRefV4(m_LastXRefOffset, m_dwXrefStartObjNum) && |
| 1674 !LoadLinearizedAllCrossRefV5(m_LastXRefOffset)) { | 1662 !LoadLinearizedAllCrossRefV5(m_LastXRefOffset)) { |
| 1675 m_LastXRefOffset = 0; | 1663 m_LastXRefOffset = 0; |
| 1676 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; | 1664 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; |
| 1677 return FORMAT_ERROR; | 1665 return FORMAT_ERROR; |
| 1678 } | 1666 } |
| 1679 | 1667 |
| 1680 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; | 1668 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; |
| 1681 return SUCCESS; | 1669 return SUCCESS; |
| 1682 } | 1670 } |
| OLD | NEW |