Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
cpdf_linearized_header.cpp
Go to the documentation of this file.
1
// Copyright 2016 The PDFium Authors
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_header.h"
8
9
#
include
<
algorithm
>
10
#
include
<
limits
>
11
#
include
<
utility
>
12
13
#
include
"core/fpdfapi/parser/cpdf_array.h"
14
#
include
"core/fpdfapi/parser/cpdf_dictionary.h"
15
#
include
"core/fpdfapi/parser/cpdf_number.h"
16
#
include
"core/fpdfapi/parser/cpdf_parser.h"
17
#
include
"core/fpdfapi/parser/cpdf_syntax_parser.h"
18
#
include
"core/fxcrt/check.h"
19
#
include
"core/fxcrt/fx_safe_types.h"
20
#
include
"core/fxcrt/ptr_util.h"
21
22
namespace
{
23
24
constexpr
FX_FILESIZE
kLinearizedHeaderOffset = 9;
25
constexpr
size_t kMaxInt =
static_cast
<size_t>(
std
::numeric_limits<
int
>::max());
26
27
template
<
class
T>
28
bool
IsValidNumericDictionaryValue(
const
CPDF_Dictionary
* pDict,
29
const
ByteString
& key,
30
T min_value,
31
bool
must_exist =
true
) {
32
if
(!pDict
->
KeyExist
(
key
)
)
33
return
!must_exist;
34
RetainPtr
<
const
CPDF_Number> pNum = pDict->GetNumberFor(key);
35
if
(!pNum || !pNum->IsInteger())
36
return
false
;
37
const
int
raw_value = pNum->GetInteger();
38
if
(!
pdfium
::IsValueInRangeForNumericType<T>(raw_value)) {
39
return
false
;
40
}
41
return
static_cast
<T>(raw_value) >= min_value;
42
}
43
44
bool
IsLinearizedHeaderValid(
const
CPDF_LinearizedHeader
* header,
45
FX_FILESIZE
document_size) {
46
DCHECK
(header);
47
return
header
->
GetFileSize
(
)
== document_size &&
48
header
->
GetFirstPageNo
(
)
< kMaxInt &&
49
header
->
GetFirstPageNo
(
)
< header
->
GetPageCount
(
)
&&
50
header
->
GetMainXRefTableFirstEntryOffset
(
)
< document_size &&
51
header
->
GetFirstPageEndOffset
(
)
< document_size &&
52
header
->
GetFirstPageObjNum
(
)
<
CPDF_Parser
::
kMaxObjectNumber
&&
53
header
->
GetLastXRefOffset
(
)
< document_size &&
54
header
->
GetHintStart
(
)
< document_size;
55
}
56
57
}
// namespace
58
59
// static
60
std::unique_ptr<CPDF_LinearizedHeader>
CPDF_LinearizedHeader
::Parse(
61
CPDF_SyntaxParser
* parser) {
62
parser
->
SetPos
(
kLinearizedHeaderOffset
)
;
63
64
const
auto
pDict = ToDictionary(
65
parser
->
GetIndirectObject
(
nullptr
,
CPDF_SyntaxParser
::
ParseType
::
kLoose
));
66
67
if
(!pDict || !pDict->KeyExist(
"Linearized"
) ||
68
!IsValidNumericDictionaryValue<
FX_FILESIZE
>(pDict.Get(),
"L"
, 1) ||
69
!IsValidNumericDictionaryValue<uint32_t>(pDict.Get(),
"P"
, 0,
false
) ||
70
!IsValidNumericDictionaryValue<
FX_FILESIZE
>(pDict.Get(),
"T"
, 1) ||
71
!IsValidNumericDictionaryValue<uint32_t>(pDict.Get(),
"N"
, 1) ||
72
!IsValidNumericDictionaryValue<
FX_FILESIZE
>(pDict.Get(),
"E"
, 1) ||
73
!IsValidNumericDictionaryValue<uint32_t>(pDict.Get(),
"O"
, 1)) {
74
return
nullptr
;
75
}
76
// Move parser to the start of the xref table for the documents first page.
77
// (skpping endobj keyword)
78
if
(parser
->
GetNextWord
(
)
.word !=
"endobj"
)
79
return
nullptr
;
80
81
auto
result =
pdfium
::WrapUnique(
82
new
CPDF_LinearizedHeader
(pDict.Get(), parser
->
GetPos
(
)
));
83
84
if
(!IsLinearizedHeaderValid(result.get(), parser
->
GetDocumentSize
(
)
))
85
return
nullptr
;
86
87
return
result;
88
}
89
90
CPDF_LinearizedHeader
::
CPDF_LinearizedHeader
(
const
CPDF_Dictionary
* pDict,
91
FX_FILESIZE
szLastXRefOffset)
92
: m_szFileSize(pDict
->
GetIntegerFor
(
"L"
)
),
93
m_dwFirstPageNo(pDict
->
GetIntegerFor
(
"P"
)
),
94
m_szMainXRefTableFirstEntryOffset(pDict
->
GetIntegerFor
(
"T"
)
),
95
m_PageCount(pDict
->
GetIntegerFor
(
"N"
)
),
96
m_szFirstPageEndOffset(pDict
->
GetIntegerFor
(
"E"
)
),
97
m_FirstPageObjNum(pDict
->
GetIntegerFor
(
"O"
)
),
98
m_szLastXRefOffset(szLastXRefOffset) {
99
RetainPtr
<
const
CPDF_Array
> pHintStreamRange = pDict->GetArrayFor(
"H"
);
100
const
size_t nHintStreamSize =
101
pHintStreamRange ? pHintStreamRange->size() : 0;
102
if
(nHintStreamSize == 2 || nHintStreamSize == 4) {
103
m_szHintStart =
std
::max(pHintStreamRange->GetIntegerAt(0), 0);
104
const
FX_SAFE_UINT32
safe_hint_length = pHintStreamRange->GetIntegerAt(1);
105
if
(safe_hint_length.IsValid())
106
m_HintLength = safe_hint_length.ValueOrDie();
107
}
108
}
109
110
CPDF_LinearizedHeader
::~
CPDF_LinearizedHeader
() =
default
;
111
112
bool
CPDF_LinearizedHeader
::
HasHintTable
()
const
{
113
return
GetPageCount
(
)
> 1 &&
GetHintStart
(
)
> 0 &&
GetHintLength
(
)
> 0;
114
}
ByteString
fxcrt::ByteString ByteString
Definition
bytestring.h:180
DCHECK
#define DCHECK
Definition
check.h:33
CPDF_Array::const_iterator
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition
cpdf_array.h:29
CPDF_Dictionary::KeyExist
bool KeyExist(const ByteString &key) const
Definition
cpdf_dictionary.cpp:255
CPDF_Dictionary::GetIntegerFor
int GetIntegerFor(const ByteString &key) const
Definition
cpdf_dictionary.cpp:133
CPDF_Dictionary::DictMap
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
Definition
cpdf_dictionary.h:31
CPDF_LinearizedHeader
Definition
cpdf_linearized_header.h:19
CPDF_LinearizedHeader::GetHintStart
FX_FILESIZE GetHintStart() const
Definition
cpdf_linearized_header.h:42
CPDF_LinearizedHeader::GetPageCount
uint32_t GetPageCount() const
Definition
cpdf_linearized_header.h:32
CPDF_LinearizedHeader::HasHintTable
bool HasHintTable() const
Definition
cpdf_linearized_header.cpp:112
CPDF_LinearizedHeader::GetFirstPageObjNum
uint32_t GetFirstPageObjNum() const
Definition
cpdf_linearized_header.h:36
CPDF_LinearizedHeader::GetHintLength
uint32_t GetHintLength() const
Definition
cpdf_linearized_header.h:43
CPDF_LinearizedHeader::~CPDF_LinearizedHeader
~CPDF_LinearizedHeader()
CPDF_LinearizedHeader::GetFirstPageNo
uint32_t GetFirstPageNo() const
Definition
cpdf_linearized_header.h:27
CPDF_LinearizedHeader::GetLastXRefOffset
FX_FILESIZE GetLastXRefOffset() const
Definition
cpdf_linearized_header.h:38
CPDF_LinearizedHeader::GetFirstPageEndOffset
FX_FILESIZE GetFirstPageEndOffset() const
Definition
cpdf_linearized_header.h:34
CPDF_LinearizedHeader::GetMainXRefTableFirstEntryOffset
FX_FILESIZE GetMainXRefTableFirstEntryOffset() const
Definition
cpdf_linearized_header.h:29
CPDF_LinearizedHeader::GetFileSize
FX_FILESIZE GetFileSize() const
Definition
cpdf_linearized_header.h:26
CPDF_LinearizedHeader::CPDF_LinearizedHeader
CPDF_LinearizedHeader(const CPDF_Dictionary *pDict, FX_FILESIZE szLastXRefOffset)
Definition
cpdf_linearized_header.cpp:90
CPDF_Parser
Definition
cpdf_parser.h:37
CPDF_Parser::kMaxObjectNumber
static constexpr uint32_t kMaxObjectNumber
Definition
cpdf_parser.h:57
CPDF_SyntaxParser
Definition
cpdf_syntax_parser.h:32
CPDF_SyntaxParser::ParseType
ParseType
Definition
cpdf_syntax_parser.h:34
CPDF_SyntaxParser::ParseType::kLoose
@ kLoose
Definition
cpdf_syntax_parser.h:34
CPDF_SyntaxParser::GetIndirectObject
RetainPtr< CPDF_Object > GetIndirectObject(CPDF_IndirectObjectHolder *pObjList, ParseType parse_type)
Definition
cpdf_syntax_parser.cpp:617
CPDF_SyntaxParser::GetPos
FX_FILESIZE GetPos() const
Definition
cpdf_syntax_parser.h:54
CPDF_SyntaxParser::GetNextWord
WordResult GetNextWord()
Definition
cpdf_syntax_parser.cpp:469
CPDF_SyntaxParser::GetDocumentSize
FX_FILESIZE GetDocumentSize() const
Definition
cpdf_syntax_parser.cpp:148
CPDF_SyntaxParser::SetPos
void SetPos(FX_FILESIZE pos)
Definition
cpdf_syntax_parser.cpp:488
fxcrt::RetainPtr
Definition
retain_ptr.h:28
pdfium::CFDETextOutLargeBitmapTest
Definition
cfde_textout_unittest.cpp:117
FX_SAFE_UINT32
pdfium::CheckedNumeric< uint32_t > FX_SAFE_UINT32
Definition
fx_safe_types.h:14
FX_FILESIZE
#define FX_FILESIZE
Definition
fx_types.h:19
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtwebengine
src
3rdparty
chromium
third_party
pdfium
core
fpdfapi
parser
cpdf_linearized_header.cpp
Generated on
for Qt by
1.18.0