| 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_parser.h" | 7 #include "core/fpdfapi/parser/cpdf_parser.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "core/fpdfapi/parser/cpdf_array.h" | 11 #include "core/fpdfapi/parser/cpdf_array.h" |
| 12 #include "core/fpdfapi/parser/cpdf_crypto_handler.h" | 12 #include "core/fpdfapi/parser/cpdf_crypto_handler.h" |
| 13 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 13 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 14 #include "core/fpdfapi/parser/cpdf_document.h" | 14 #include "core/fpdfapi/parser/cpdf_document.h" |
| 15 #include "core/fpdfapi/parser/cpdf_linearized.h" |
| 15 #include "core/fpdfapi/parser/cpdf_number.h" | 16 #include "core/fpdfapi/parser/cpdf_number.h" |
| 16 #include "core/fpdfapi/parser/cpdf_reference.h" | 17 #include "core/fpdfapi/parser/cpdf_reference.h" |
| 17 #include "core/fpdfapi/parser/cpdf_security_handler.h" | 18 #include "core/fpdfapi/parser/cpdf_security_handler.h" |
| 18 #include "core/fpdfapi/parser/cpdf_stream.h" | 19 #include "core/fpdfapi/parser/cpdf_stream.h" |
| 19 #include "core/fpdfapi/parser/cpdf_stream_acc.h" | 20 #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
| 20 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" | 21 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" |
| 21 #include "core/fpdfapi/parser/fpdf_parser_utility.h" | 22 #include "core/fpdfapi/parser/fpdf_parser_utility.h" |
| 22 #include "core/fxcrt/fx_ext.h" | 23 #include "core/fxcrt/fx_ext.h" |
| 23 #include "core/fxcrt/fx_safe_types.h" | 24 #include "core/fxcrt/fx_safe_types.h" |
| 24 #include "third_party/base/stl_util.h" | 25 #include "third_party/base/stl_util.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 47 } // namespace | 48 } // namespace |
| 48 | 49 |
| 49 CPDF_Parser::CPDF_Parser() | 50 CPDF_Parser::CPDF_Parser() |
| 50 : m_pDocument(nullptr), | 51 : m_pDocument(nullptr), |
| 51 m_bHasParsed(false), | 52 m_bHasParsed(false), |
| 52 m_bOwnFileRead(true), | 53 m_bOwnFileRead(true), |
| 53 m_FileVersion(0), | 54 m_FileVersion(0), |
| 54 m_pTrailer(nullptr), | 55 m_pTrailer(nullptr), |
| 55 m_pEncryptDict(nullptr), | 56 m_pEncryptDict(nullptr), |
| 56 m_bVersionUpdated(false), | 57 m_bVersionUpdated(false), |
| 57 m_pLinearized(nullptr), | |
| 58 m_dwFirstPageNo(0), | |
| 59 m_dwXrefStartObjNum(0) { | 58 m_dwXrefStartObjNum(0) { |
| 60 m_pSyntax.reset(new CPDF_SyntaxParser); | 59 m_pSyntax.reset(new CPDF_SyntaxParser); |
| 61 } | 60 } |
| 62 | 61 |
| 63 CPDF_Parser::~CPDF_Parser() { | 62 CPDF_Parser::~CPDF_Parser() { |
| 64 delete m_pTrailer; | 63 delete m_pTrailer; |
| 65 ReleaseEncryptHandler(); | 64 ReleaseEncryptHandler(); |
| 66 SetEncryptDictionary(nullptr); | 65 SetEncryptDictionary(nullptr); |
| 67 | 66 |
| 68 if (m_bOwnFileRead && m_pSyntax->m_pFileAccess) { | 67 if (m_bOwnFileRead && m_pSyntax->m_pFileAccess) { |
| 69 m_pSyntax->m_pFileAccess->Release(); | 68 m_pSyntax->m_pFileAccess->Release(); |
| 70 m_pSyntax->m_pFileAccess = nullptr; | 69 m_pSyntax->m_pFileAccess = nullptr; |
| 71 } | 70 } |
| 72 | 71 |
| 73 for (CPDF_Dictionary* trailer : m_Trailers) | 72 for (CPDF_Dictionary* trailer : m_Trailers) |
| 74 delete trailer; | 73 delete trailer; |
| 75 | |
| 76 delete m_pLinearized; | |
| 77 } | 74 } |
| 78 | 75 |
| 79 uint32_t CPDF_Parser::GetLastObjNum() const { | 76 uint32_t CPDF_Parser::GetLastObjNum() const { |
| 80 return m_ObjectInfo.empty() ? 0 : m_ObjectInfo.rbegin()->first; | 77 return m_ObjectInfo.empty() ? 0 : m_ObjectInfo.rbegin()->first; |
| 81 } | 78 } |
| 82 | 79 |
| 83 bool CPDF_Parser::IsValidObjectNumber(uint32_t objnum) const { | 80 bool CPDF_Parser::IsValidObjectNumber(uint32_t objnum) const { |
| 84 return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first; | 81 return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first; |
| 85 } | 82 } |
| 86 | 83 |
| (...skipping 1308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1395 } | 1392 } |
| 1396 | 1393 |
| 1397 CPDF_Object* pObj = m_pSyntax->GetObjectForStrict(pObjList, objnum, gennum); | 1394 CPDF_Object* pObj = m_pSyntax->GetObjectForStrict(pObjList, objnum, gennum); |
| 1398 if (pResultPos) | 1395 if (pResultPos) |
| 1399 *pResultPos = m_pSyntax->m_Pos; | 1396 *pResultPos = m_pSyntax->m_Pos; |
| 1400 | 1397 |
| 1401 m_pSyntax->RestorePos(SavedPos); | 1398 m_pSyntax->RestorePos(SavedPos); |
| 1402 return pObj; | 1399 return pObj; |
| 1403 } | 1400 } |
| 1404 | 1401 |
| 1402 uint32_t CPDF_Parser::GetFirstPageNo() const { |
| 1403 return m_pLinearized ? m_pLinearized->GetFirstPageNo() : 0; |
| 1404 } |
| 1405 |
| 1405 CPDF_Dictionary* CPDF_Parser::LoadTrailerV4() { | 1406 CPDF_Dictionary* CPDF_Parser::LoadTrailerV4() { |
| 1406 if (m_pSyntax->GetKeyword() != "trailer") | 1407 if (m_pSyntax->GetKeyword() != "trailer") |
| 1407 return nullptr; | 1408 return nullptr; |
| 1408 | 1409 |
| 1409 std::unique_ptr<CPDF_Object> pObj( | 1410 std::unique_ptr<CPDF_Object> pObj( |
| 1410 m_pSyntax->GetObject(m_pDocument, 0, 0, true)); | 1411 m_pSyntax->GetObject(m_pDocument, 0, 0, true)); |
| 1411 if (!ToDictionary(pObj.get())) | 1412 if (!ToDictionary(pObj.get())) |
| 1412 return nullptr; | 1413 return nullptr; |
| 1413 return pObj.release()->AsDictionary(); | 1414 return pObj.release()->AsDictionary(); |
| 1414 } | 1415 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1441 word = m_pSyntax->GetNextWord(&bIsNumber); | 1442 word = m_pSyntax->GetNextWord(&bIsNumber); |
| 1442 if (!bIsNumber) | 1443 if (!bIsNumber) |
| 1443 return false; | 1444 return false; |
| 1444 | 1445 |
| 1445 uint32_t gennum = FXSYS_atoui(word.c_str()); | 1446 uint32_t gennum = FXSYS_atoui(word.c_str()); |
| 1446 if (m_pSyntax->GetKeyword() != "obj") { | 1447 if (m_pSyntax->GetKeyword() != "obj") { |
| 1447 m_pSyntax->RestorePos(SavedPos); | 1448 m_pSyntax->RestorePos(SavedPos); |
| 1448 return false; | 1449 return false; |
| 1449 } | 1450 } |
| 1450 | 1451 |
| 1451 m_pLinearized = m_pSyntax->GetObject(nullptr, objnum, gennum, true); | 1452 m_pLinearized = CPDF_Linearized::CreateForObject( |
| 1453 pdfium::WrapUnique(m_pSyntax->GetObject(nullptr, objnum, gennum, true))); |
| 1452 if (!m_pLinearized) | 1454 if (!m_pLinearized) |
| 1453 return false; | 1455 return false; |
| 1454 | 1456 m_LastXRefOffset = m_pLinearized->GetLastXRefOffset(); |
| 1455 CPDF_Dictionary* pDict = m_pLinearized->GetDict(); | 1457 // Move parser onto first page xref table start. |
| 1456 if (pDict && pDict->GetObjectFor("Linearized")) { | 1458 m_pSyntax->GetNextWord(nullptr); |
| 1457 m_pSyntax->GetNextWord(nullptr); | 1459 return true; |
| 1458 | |
| 1459 CPDF_Object* pLen = pDict->GetObjectFor("L"); | |
| 1460 if (!pLen) { | |
| 1461 delete m_pLinearized; | |
| 1462 m_pLinearized = nullptr; | |
| 1463 return false; | |
| 1464 } | |
| 1465 | |
| 1466 if (pLen->GetInteger() != (int)pFileAccess->GetSize()) | |
| 1467 return false; | |
| 1468 | |
| 1469 if (CPDF_Number* pNo = ToNumber(pDict->GetObjectFor("P"))) | |
| 1470 m_dwFirstPageNo = pNo->GetInteger(); | |
| 1471 | |
| 1472 if (CPDF_Number* pTable = ToNumber(pDict->GetObjectFor("T"))) | |
| 1473 m_LastXRefOffset = pTable->GetInteger(); | |
| 1474 | |
| 1475 return true; | |
| 1476 } | |
| 1477 delete m_pLinearized; | |
| 1478 m_pLinearized = nullptr; | |
| 1479 return false; | |
| 1480 } | 1460 } |
| 1481 | 1461 |
| 1482 CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( | 1462 CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( |
| 1483 IFX_SeekableReadStream* pFileAccess, | 1463 IFX_SeekableReadStream* pFileAccess, |
| 1484 CPDF_Document* pDocument) { | 1464 CPDF_Document* pDocument) { |
| 1485 ASSERT(!m_bHasParsed); | 1465 ASSERT(!m_bHasParsed); |
| 1486 | 1466 |
| 1487 m_bXRefStream = false; | 1467 m_bXRefStream = false; |
| 1488 m_LastXRefOffset = 0; | 1468 m_LastXRefOffset = 0; |
| 1489 m_bOwnFileRead = true; | 1469 m_bOwnFileRead = true; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1518 | 1498 |
| 1519 int32_t xrefsize = GetDirectInteger(m_pTrailer, "Size"); | 1499 int32_t xrefsize = GetDirectInteger(m_pTrailer, "Size"); |
| 1520 if (xrefsize > 0) | 1500 if (xrefsize > 0) |
| 1521 ShrinkObjectMap(xrefsize); | 1501 ShrinkObjectMap(xrefsize); |
| 1522 } | 1502 } |
| 1523 | 1503 |
| 1524 Error eRet = SetEncryptHandler(); | 1504 Error eRet = SetEncryptHandler(); |
| 1525 if (eRet != SUCCESS) | 1505 if (eRet != SUCCESS) |
| 1526 return eRet; | 1506 return eRet; |
| 1527 | 1507 |
| 1528 m_pDocument->LoadLinearizedDoc(m_pLinearized->GetDict()); | 1508 m_pDocument->LoadLinearizedDoc(m_pLinearized.get()); |
| 1529 if (!m_pDocument->GetRoot() || m_pDocument->GetPageCount() == 0) { | 1509 if (!m_pDocument->GetRoot() || m_pDocument->GetPageCount() == 0) { |
| 1530 if (bXRefRebuilt) | 1510 if (bXRefRebuilt) |
| 1531 return FORMAT_ERROR; | 1511 return FORMAT_ERROR; |
| 1532 | 1512 |
| 1533 ReleaseEncryptHandler(); | 1513 ReleaseEncryptHandler(); |
| 1534 if (!RebuildCrossRef()) | 1514 if (!RebuildCrossRef()) |
| 1535 return FORMAT_ERROR; | 1515 return FORMAT_ERROR; |
| 1536 | 1516 |
| 1537 eRet = SetEncryptHandler(); | 1517 eRet = SetEncryptHandler(); |
| 1538 if (eRet != SUCCESS) | 1518 if (eRet != SUCCESS) |
| 1539 return eRet; | 1519 return eRet; |
| 1540 | 1520 |
| 1541 m_pDocument->LoadLinearizedDoc(m_pLinearized->GetDict()); | 1521 m_pDocument->LoadLinearizedDoc(m_pLinearized.get()); |
| 1542 if (!m_pDocument->GetRoot()) | 1522 if (!m_pDocument->GetRoot()) |
| 1543 return FORMAT_ERROR; | 1523 return FORMAT_ERROR; |
| 1544 } | 1524 } |
| 1545 | 1525 |
| 1546 if (GetRootObjNum() == 0) { | 1526 if (GetRootObjNum() == 0) { |
| 1547 ReleaseEncryptHandler(); | 1527 ReleaseEncryptHandler(); |
| 1548 if (!RebuildCrossRef() || GetRootObjNum() == 0) | 1528 if (!RebuildCrossRef() || GetRootObjNum() == 0) |
| 1549 return FORMAT_ERROR; | 1529 return FORMAT_ERROR; |
| 1550 | 1530 |
| 1551 eRet = SetEncryptHandler(); | 1531 eRet = SetEncryptHandler(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1606 if (!LoadLinearizedAllCrossRefV4(m_LastXRefOffset, m_dwXrefStartObjNum) && | 1586 if (!LoadLinearizedAllCrossRefV4(m_LastXRefOffset, m_dwXrefStartObjNum) && |
| 1607 !LoadLinearizedAllCrossRefV5(m_LastXRefOffset)) { | 1587 !LoadLinearizedAllCrossRefV5(m_LastXRefOffset)) { |
| 1608 m_LastXRefOffset = 0; | 1588 m_LastXRefOffset = 0; |
| 1609 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; | 1589 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; |
| 1610 return FORMAT_ERROR; | 1590 return FORMAT_ERROR; |
| 1611 } | 1591 } |
| 1612 | 1592 |
| 1613 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; | 1593 m_pSyntax->m_MetadataObjnum = dwSaveMetadataObjnum; |
| 1614 return SUCCESS; | 1594 return SUCCESS; |
| 1615 } | 1595 } |
| OLD | NEW |