| 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_hint_tables.h" | 7 #include "core/fpdfapi/parser/cpdf_hint_tables.h" |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "core/fpdfapi/parser/cpdf_array.h" | 11 #include "core/fpdfapi/parser/cpdf_array.h" |
| 12 #include "core/fpdfapi/parser/cpdf_data_avail.h" | 12 #include "core/fpdfapi/parser/cpdf_data_avail.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" | |
| 16 #include "core/fpdfapi/parser/cpdf_stream.h" | 15 #include "core/fpdfapi/parser/cpdf_stream.h" |
| 17 #include "core/fpdfapi/parser/cpdf_stream_acc.h" | 16 #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
| 18 #include "core/fxcrt/fx_safe_types.h" | 17 #include "core/fxcrt/fx_safe_types.h" |
| 19 #include "third_party/base/numerics/safe_conversions.h" | 18 #include "third_party/base/numerics/safe_conversions.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 bool CanReadFromBitStream(const CFX_BitStream* hStream, | 22 bool CanReadFromBitStream(const CFX_BitStream* hStream, |
| 24 const FX_SAFE_UINT32& bits) { | 23 const FX_SAFE_UINT32& bits) { |
| 25 return bits.IsValid() && hStream->BitsRemaining() >= bits.ValueOrDie(); | 24 return bits.IsValid() && hStream->BitsRemaining() >= bits.ValueOrDie(); |
| 26 } | 25 } |
| 27 | 26 |
| 28 // Sanity check values from the page table header. The note in the PDF 1.7 | 27 // Sanity check values from the page table header. The note in the PDF 1.7 |
| 29 // reference for Table F.3 says the valid range is only 0 through 32. Though 0 | 28 // reference for Table F.3 says the valid range is only 0 through 32. Though 0 |
| 30 // is not useful either. | 29 // is not useful either. |
| 31 bool IsValidPageOffsetHintTableBitCount(uint32_t bits) { | 30 bool IsValidPageOffsetHintTableBitCount(uint32_t bits) { |
| 32 return bits > 0 && bits <= 32; | 31 return bits > 0 && bits <= 32; |
| 33 } | 32 } |
| 34 | 33 |
| 35 } // namespace | 34 } // namespace |
| 36 | 35 |
| 37 CPDF_HintTables::CPDF_HintTables(CPDF_DataAvail* pDataAvail, | 36 CPDF_HintTables::CPDF_HintTables(CPDF_DataAvail* pDataAvail, |
| 38 CPDF_Linearized* pLinearized) | 37 CPDF_Dictionary* pLinearized) |
| 39 : m_pDataAvail(pDataAvail), | 38 : m_pDataAvail(pDataAvail), |
| 40 m_pLinearized(pLinearized), | 39 m_pLinearizedDict(pLinearized), |
| 41 m_nFirstPageSharedObjs(0), | 40 m_nFirstPageSharedObjs(0), |
| 42 m_szFirstPageObjOffset(0) { | 41 m_szFirstPageObjOffset(0) { |
| 43 ASSERT(m_pLinearized); | 42 ASSERT(m_pLinearizedDict); |
| 44 } | 43 } |
| 45 | 44 |
| 46 CPDF_HintTables::~CPDF_HintTables() {} | 45 CPDF_HintTables::~CPDF_HintTables() {} |
| 47 | 46 |
| 48 uint32_t CPDF_HintTables::GetItemLength( | 47 uint32_t CPDF_HintTables::GetItemLength( |
| 49 uint32_t index, | 48 uint32_t index, |
| 50 const std::vector<FX_FILESIZE>& szArray) { | 49 const std::vector<FX_FILESIZE>& szArray) { |
| 51 if (szArray.size() < 2 || index > szArray.size() - 2 || | 50 if (szArray.size() < 2 || index > szArray.size() - 2 || |
| 52 szArray[index] > szArray[index + 1]) { | 51 szArray[index] > szArray[index + 1]) { |
| 53 return 0; | 52 return 0; |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 return false; | 480 return false; |
| 482 } | 481 } |
| 483 | 482 |
| 484 CFX_BitStream bs; | 483 CFX_BitStream bs; |
| 485 bs.Init(acc.GetData(), size); | 484 bs.Init(acc.GetData(), size); |
| 486 return ReadPageHintTable(&bs) && | 485 return ReadPageHintTable(&bs) && |
| 487 ReadSharedObjHintTable(&bs, shared_hint_table_offset); | 486 ReadSharedObjHintTable(&bs, shared_hint_table_offset); |
| 488 } | 487 } |
| 489 | 488 |
| 490 int CPDF_HintTables::GetEndOfFirstPageOffset() const { | 489 int CPDF_HintTables::GetEndOfFirstPageOffset() const { |
| 491 return static_cast<int>(m_pLinearized->GetFirstPageEndOffset()); | 490 CPDF_Object* pOffsetE = m_pLinearizedDict->GetDirectObjectFor("E"); |
| 491 return pOffsetE ? pOffsetE->GetInteger() : -1; |
| 492 } | 492 } |
| 493 | 493 |
| 494 int CPDF_HintTables::GetNumberOfPages() const { | 494 int CPDF_HintTables::GetNumberOfPages() const { |
| 495 return static_cast<int>(m_pLinearized->GetPageCount()); | 495 CPDF_Object* pPageNum = m_pLinearizedDict->GetDirectObjectFor("N"); |
| 496 return pPageNum ? pPageNum->GetInteger() : 0; |
| 496 } | 497 } |
| 497 | 498 |
| 498 int CPDF_HintTables::GetFirstPageObjectNumber() const { | 499 int CPDF_HintTables::GetFirstPageObjectNumber() const { |
| 499 return static_cast<int>(m_pLinearized->GetFirstPageObjNum()); | 500 CPDF_Object* pFirstPageObj = m_pLinearizedDict->GetDirectObjectFor("O"); |
| 501 return pFirstPageObj ? pFirstPageObj->GetInteger() : -1; |
| 500 } | 502 } |
| 501 | 503 |
| 502 int CPDF_HintTables::GetFirstPageNumber() const { | 504 int CPDF_HintTables::GetFirstPageNumber() const { |
| 503 return static_cast<int>(m_pLinearized->GetFirstPageNo()); | 505 CPDF_Object* pFirstPageNum = m_pLinearizedDict->GetDirectObjectFor("P"); |
| 506 return pFirstPageNum ? pFirstPageNum->GetInteger() : 0; |
| 504 } | 507 } |
| 505 | 508 |
| 506 int CPDF_HintTables::ReadPrimaryHintStreamOffset() const { | 509 int CPDF_HintTables::ReadPrimaryHintStreamOffset() const { |
| 507 return static_cast<int>(m_pLinearized->GetHintStart()); | 510 return ReadPrimaryHintStream(0); |
| 508 } | 511 } |
| 509 | 512 |
| 510 int CPDF_HintTables::ReadPrimaryHintStreamLength() const { | 513 int CPDF_HintTables::ReadPrimaryHintStreamLength() const { |
| 511 return static_cast<int>(m_pLinearized->GetHintLength()); | 514 return ReadPrimaryHintStream(1); |
| 512 } | 515 } |
| 516 |
| 517 int CPDF_HintTables::ReadPrimaryHintStream(int index) const { |
| 518 CPDF_Array* pRange = m_pLinearizedDict->GetArrayFor("H"); |
| 519 if (!pRange) |
| 520 return -1; |
| 521 |
| 522 CPDF_Object* pStreamLen = pRange->GetDirectObjectAt(index); |
| 523 return pStreamLen ? pStreamLen->GetInteger() : -1; |
| 524 } |
| OLD | NEW |