Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: core/fpdfapi/parser/cpdf_linearized.cpp

Issue 2466023002: Unify some code (Closed)
Patch Set: Rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_linearized.h ('k') | core/fpdfapi/parser/cpdf_parser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/parser/cpdf_linearized.h"
8
9 #include "core/fpdfapi/parser/cpdf_array.h"
10 #include "core/fpdfapi/parser/cpdf_dictionary.h"
11 #include "core/fpdfapi/parser/cpdf_number.h"
12 #include "third_party/base/ptr_util.h"
13
14 namespace {
15
16 template <class T>
17 bool IsValidNumericDictionaryValue(const CPDF_Dictionary* pDict,
18 const char* key,
19 T min_value,
20 bool must_exist = true) {
21 if (!pDict->KeyExist(key))
22 return !must_exist;
23 const CPDF_Number* pNum = ToNumber(pDict->GetObjectFor(key));
24 if (!pNum || !pNum->IsInteger())
25 return false;
26 const int raw_value = pNum->GetInteger();
27 if (!pdfium::base::IsValueInRangeForNumericType<T>(raw_value))
28 return false;
29 return static_cast<T>(raw_value) >= min_value;
30 }
31
32 } // namespace
33
34 // static
35 std::unique_ptr<CPDF_Linearized> CPDF_Linearized::CreateForObject(
36 std::unique_ptr<CPDF_Object> pObj) {
37 auto pDict = ToDictionary(std::move(pObj));
38 if (!pDict || !pDict->KeyExist("Linearized") ||
39 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "L", 1) ||
40 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "P", 0, false) ||
41 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "T", 1) ||
42 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "N", 0) ||
43 !IsValidNumericDictionaryValue<FX_FILESIZE>(pDict.get(), "E", 1) ||
44 !IsValidNumericDictionaryValue<uint32_t>(pDict.get(), "O", 1))
45 return nullptr;
46 return pdfium::WrapUnique(new CPDF_Linearized(pDict.get()));
47 }
48
49 CPDF_Linearized::CPDF_Linearized(const CPDF_Dictionary* pDict) {
50 m_szFileSize = pDict->GetIntegerFor("L");
51 m_dwFirstPageNo = pDict->GetIntegerFor("P");
52 m_szLastXRefOffset = pDict->GetIntegerFor("T");
53 m_PageCount = pDict->GetIntegerFor("N");
54 m_szFirstPageEndOffset = pDict->GetIntegerFor("E");
55 m_FirstPageObjNum = pDict->GetIntegerFor("O");
56 const CPDF_Array* pHintStreamRange = pDict->GetArrayFor("H");
57 const size_t nHintStreamSize =
58 pHintStreamRange ? pHintStreamRange->GetCount() : 0;
59 if (nHintStreamSize == 2 || nHintStreamSize == 4) {
60 m_szHintStart = std::max(pHintStreamRange->GetIntegerAt(0), 0);
61 m_szHintLength = std::max(pHintStreamRange->GetIntegerAt(1), 0);
62 }
63 }
64
65 CPDF_Linearized::~CPDF_Linearized() {}
66
67 bool CPDF_Linearized::HasHintTable() const {
68 return GetPageCount() > 1 && GetHintStart() > 0 && GetHintLength() > 0;
69 }
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_linearized.h ('k') | core/fpdfapi/parser/cpdf_parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698