Chromium Code Reviews| 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_linearized.h" | 7 #include "core/fpdfapi/parser/cpdf_linearized_header.h" |
| 8 | 8 |
| 9 #include "core/fpdfapi/parser/cpdf_array.h" | 9 #include "core/fpdfapi/parser/cpdf_array.h" |
| 10 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 10 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 11 #include "core/fpdfapi/parser/cpdf_number.h" | 11 #include "core/fpdfapi/parser/cpdf_number.h" |
| 12 #include "third_party/base/ptr_util.h" | 12 #include "third_party/base/ptr_util.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 template <class T> | 16 template <class T> |
| 17 bool IsValidNumericDictionaryValue(const CPDF_Dictionary* pDict, | 17 bool IsValidNumericDictionaryValue(const CPDF_Dictionary* pDict, |
| 18 const char* key, | 18 const char* key, |
| 19 T min_value, | 19 T min_value, |
| 20 bool must_exist = true) { | 20 bool must_exist = true) { |
| 21 if (!pDict->KeyExist(key)) | 21 if (!pDict->KeyExist(key)) |
| 22 return !must_exist; | 22 return !must_exist; |
| 23 const CPDF_Number* pNum = ToNumber(pDict->GetObjectFor(key)); | 23 const CPDF_Number* pNum = ToNumber(pDict->GetObjectFor(key)); |
| 24 if (!pNum || !pNum->IsInteger()) | 24 if (!pNum || !pNum->IsInteger()) |
| 25 return false; | 25 return false; |
| 26 const int raw_value = pNum->GetInteger(); | 26 const int raw_value = pNum->GetInteger(); |
| 27 if (!pdfium::base::IsValueInRangeForNumericType<T>(raw_value)) | 27 if (!pdfium::base::IsValueInRangeForNumericType<T>(raw_value)) |
| 28 return false; | 28 return false; |
| 29 return static_cast<T>(raw_value) >= min_value; | 29 return static_cast<T>(raw_value) >= min_value; |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 // static | 34 // static |
| 35 std::unique_ptr<CPDF_Linearized> CPDF_Linearized::CreateForObject( | 35 std::unique_ptr<CPDF_LinearizedHeader> CPDF_LinearizedHeader::CreateForObject( |
| 36 std::unique_ptr<CPDF_Object> pObj) { | 36 std::unique_ptr<CPDF_Object> pObj) { |
| 37 auto pDict = ToDictionary(std::move(pObj)); | 37 auto pDict = ToDictionary(std::move(pObj)); |
| 38 if (!pDict || !pDict->KeyExist("Linearized") || | 38 if (!pDict || !pDict->KeyExist("Linearized") || |
| 39 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "L", 1) || | 39 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "L", 1) || |
| 40 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "P", 0, false) || | 40 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "P", 0, false) || |
| 41 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "T", 1) || | 41 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "T", 1) || |
| 42 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "N", 0) || | 42 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "N", 0) || |
| 43 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "E", 1) || | 43 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "E", 1) || |
| 44 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "O", 1)) | 44 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "O", 1)) |
| 45 return nullptr; | 45 return nullptr; |
| 46 return pdfium::WrapUnique(new CPDF_Linearized(pDict.get())); | 46 return pdfium::WrapUnique(new CPDF_LinearizedHeader(pDict.get())); |
| 47 } | 47 } |
| 48 | 48 |
| 49 CPDF_Linearized::CPDF_Linearized(const CPDF_Dictionary* pDict) { | 49 CPDF_LinearizedHeader::CPDF_LinearizedHeader(const CPDF_Dictionary* pDict) { |
| 50 if (!pDict) | |
|
Tom Sepez
2016/11/07 19:27:49
Can't happen given check above and this is now pro
| |
| 51 return; | |
| 52 m_szFileSize = pDict->GetIntegerFor("L"); | 50 m_szFileSize = pDict->GetIntegerFor("L"); |
| 53 m_dwFirstPageNo = pDict->GetIntegerFor("P"); | 51 m_dwFirstPageNo = pDict->GetIntegerFor("P"); |
| 54 m_szLastXRefOffset = pDict->GetIntegerFor("T"); | 52 m_szLastXRefOffset = pDict->GetIntegerFor("T"); |
| 55 m_PageCount = pDict->GetIntegerFor("N"); | 53 m_PageCount = pDict->GetIntegerFor("N"); |
| 56 m_szFirstPageEndOffset = pDict->GetIntegerFor("E"); | 54 m_szFirstPageEndOffset = pDict->GetIntegerFor("E"); |
| 57 m_FirstPageObjNum = pDict->GetIntegerFor("O"); | 55 m_FirstPageObjNum = pDict->GetIntegerFor("O"); |
| 58 const CPDF_Array* pHintStreamRange = pDict->GetArrayFor("H"); | 56 const CPDF_Array* pHintStreamRange = pDict->GetArrayFor("H"); |
| 59 const size_t nHintStreamSize = | 57 const size_t nHintStreamSize = |
| 60 pHintStreamRange ? pHintStreamRange->GetCount() : 0; | 58 pHintStreamRange ? pHintStreamRange->GetCount() : 0; |
| 61 if (nHintStreamSize == 2 || nHintStreamSize == 4) { | 59 if (nHintStreamSize == 2 || nHintStreamSize == 4) { |
| 62 m_szHintStart = std::max(pHintStreamRange->GetIntegerAt(0), 0); | 60 m_szHintStart = std::max(pHintStreamRange->GetIntegerAt(0), 0); |
| 63 m_szHintLength = std::max(pHintStreamRange->GetIntegerAt(1), 0); | 61 m_szHintLength = std::max(pHintStreamRange->GetIntegerAt(1), 0); |
| 64 } | 62 } |
| 65 } | 63 } |
| 66 | 64 |
| 67 CPDF_Linearized::~CPDF_Linearized() {} | 65 CPDF_LinearizedHeader::~CPDF_LinearizedHeader() {} |
| 68 | 66 |
| 69 bool CPDF_Linearized::HasHintTable() const { | 67 bool CPDF_LinearizedHeader::HasHintTable() const { |
| 70 return GetPageCount() > 1 && GetHintStart() > 0 && GetHintLength() > 0; | 68 return GetPageCount() > 1 && GetHintStart() > 0 && GetHintLength() > 0; |
| 71 } | 69 } |
| OLD | NEW |