Chromium Code Reviews| Index: core/fpdfapi/parser/cpdf_linearized.cpp |
| diff --git a/core/fpdfapi/parser/cpdf_linearized.cpp b/core/fpdfapi/parser/cpdf_linearized.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..825a96f0514953bce8a976600a31d1f4322b31b7 |
| --- /dev/null |
| +++ b/core/fpdfapi/parser/cpdf_linearized.cpp |
| @@ -0,0 +1,66 @@ |
| +// Copyright 2016 PDFium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| + |
| +#include "core/fpdfapi/parser/cpdf_linearized.h" |
| + |
| +#include "core/fpdfapi/parser/cpdf_array.h" |
| +#include "core/fpdfapi/parser/cpdf_dictionary.h" |
| +#include "core/fpdfapi/parser/cpdf_number.h" |
| +#include "third_party/base/ptr_util.h" |
| + |
| +namespace { |
| + |
| +template <class T> |
| +bool IsValidNumericDictionaryValue(const CPDF_Dictionary* pDict, |
| + const char* key, |
| + T min_value, |
| + bool must_be_exists = true) { |
|
Lei Zhang
2016/11/04 23:02:15
must_exist
snake
2016/11/04 23:27:36
Done.
|
| + const CPDF_Object* pNum = pDict->GetObjectFor(key); |
|
Lei Zhang
2016/11/04 23:02:15
You can write:
const CPDF_Number* pNum = ToNumber
snake
2016/11/04 23:27:36
Done.
|
| + if (!pNum || !pNum->IsNumber()) |
|
Lei Zhang
2016/11/04 23:02:15
Also check IsInteger() ?
snake
2016/11/04 23:27:36
Done.
|
| + return !must_be_exists; |
| + const int raw_value = pNum->AsNumber()->GetInteger(); |
| + if (!pdfium::base::IsValueInRangeForNumericType<T>(raw_value)) |
| + return false; |
| + return static_cast<T>(raw_value) >= min_value; |
| +} |
| +} // namespace |
|
Lei Zhang
2016/11/04 23:02:15
nit: blank line above
snake
2016/11/04 23:27:36
Done.
|
| + |
| +// static |
| +std::unique_ptr<CPDF_Linearized> CPDF_Linearized::CreateForObject( |
| + std::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pObj) { |
| + UniqueDictionary pDict = ToDictionary(std::move(pObj)); |
| + if (!pDict || !pDict->GetObjectFor("Linearized") || |
|
Lei Zhang
2016/11/04 23:02:15
Use KeyExist() instead of GetObjectFor() since you
snake
2016/11/04 23:27:36
Done.
|
| + !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "L", 1) || |
| + !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "P", 0, false) || |
| + !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "T", 1) || |
| + !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "N", 0) || |
| + !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "E", 1) || |
| + !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "O", 1)) |
| + return nullptr; |
| + return pdfium::WrapUnique(new CPDF_Linearized(pDict.get())); |
| +} |
| + |
| +CPDF_Linearized::CPDF_Linearized(const CPDF_Dictionary* pDict) { |
| + m_szFileSize = pDict->GetIntegerFor("L"); |
| + m_dwFirstPageNo = pDict->GetIntegerFor("P"); |
| + m_szLastXRefOffset = pDict->GetIntegerFor("T"); |
| + m_PageCount = pDict->GetIntegerFor("N"); |
| + m_szFirstPageEndOffset = pDict->GetIntegerFor("E"); |
| + m_FirstPageObjNum = pDict->GetIntegerFor("O"); |
| + const CPDF_Array* pHintStreamRange = pDict->GetArrayFor("H"); |
| + const size_t nHintStreamSize = |
| + pHintStreamRange ? pHintStreamRange->GetCount() : 0; |
| + if (nHintStreamSize == 2 || nHintStreamSize == 4) { |
| + m_szHintStart = std::max(pHintStreamRange->GetIntegerAt(0), 0); |
| + m_szHintLength = std::max(pHintStreamRange->GetIntegerAt(1), 0); |
| + } |
| +} |
| + |
| +CPDF_Linearized::~CPDF_Linearized() {} |
| + |
| +bool CPDF_Linearized::HasHintTable() const { |
| + return GetPageCount() > 1 && GetHintStart() > 0 && GetHintLength() > 0; |
| +} |