PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfTokenizer.h
1// SPDX-FileCopyrightText: 2006 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2020 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_TOKENIZER_H
6#define PDF_TOKENIZER_H
7
8#include "PdfDeclarations.h"
9#include <podofo/auxiliary/InputDevice.h>
10#include "PdfStatefulEncrypt.h"
11
12#include <deque>
13
14namespace PoDoFo {
15
16class PdfVariant;
17
18enum class PdfPostScriptLanguageLevel : uint8_t
19{
20 L1 = 1,
21 L2 = 2,
22};
23
25{
26 None = 0,
27 StrictParsing = 1,
28 SkipReferences = 2,
30};
31
32struct PODOFO_API PdfTokenizerParams final
33{
34 PdfPostScriptLanguageLevel LanguageLevel = PdfPostScriptLanguageLevel::L2;
35 PdfTokenizerFlags Flags = PdfTokenizerFlags::None;
36};
37
39struct PODOFO_API PdfTokenizerOptions final
40{
41 PdfPostScriptLanguageLevel LanguageLevel = PdfPostScriptLanguageLevel::L2;
42 bool ReadReferences = true;
43};
44
46class PODOFO_API PdfTokenizer
47{
48 friend class PdfParser;
49 friend class PdfPostScriptTokenizer;
50 PODOFO_PRIVATE_FRIEND(class PdfParserObject);
51
52public:
53 static constexpr unsigned BufferSize = 4096;
54 static constexpr size_t MaxStringLength = 64 * 1024 * 1024; // 64 MiB
55
56public:
58 PdfTokenizer(std::shared_ptr<charbuff> buffer);
59
60 [[deprecated("Use the overloads with PdfTokenizerParams instead")]]
62
63 [[deprecated("Use the overloads with PdfTokenizerParams instead")]]
64 PdfTokenizer(std::shared_ptr<charbuff> buffer, const PdfTokenizerOptions& options);
65
83 bool TryReadNextToken(InputStreamDevice& device, std::string_view& token, PdfTokenType& tokenType);
84
90 bool TryPeekNextToken(InputStreamDevice& device, std::string_view& token, PdfTokenType& tokenType);
91
100 int64_t ReadNextNumber(InputStreamDevice& device);
101 bool TryReadNextNumber(InputStreamDevice& device, int64_t& value);
102
111 void ReadNextVariant(InputStreamDevice& device, PdfVariant& variant, const PdfStatefulEncrypt* encrypt = { });
112 bool TryReadNextVariant(InputStreamDevice& device, PdfVariant& variant, const PdfStatefulEncrypt* encrypt = { });
113
114 void Reset();
115
116 void SetParameters(const PdfTokenizerParams& params) { m_Params = params; }
117
118 const PdfTokenizerParams& GetParameters() const { return m_Params; }
119
120protected:
121 // This enum differs from regular PdfDataType in the sense
122 // it enumerates only data types that can be determined literally
123 // by the tokenization and specify better if the strings literals
124 // are regular or hex strings
125 enum class PdfLiteralDataType : uint8_t
126 {
127 Unknown = 0,
128 Bool,
129 Number,
130 Real,
131 String,
132 HexString,
133 Name,
134 Array,
136 Null,
137 Reference,
138 };
139
143 {
147 bool ThrowOnError : 1;
151 bool Strict : 1;
152 };
153
154protected:
164 void ReadNextVariant(InputStreamDevice& device, const std::string_view& token, PdfTokenType tokenType, PdfVariant& variant, const PdfStatefulEncrypt* encrypt);
165 bool TryReadNextVariant(InputStreamDevice& device, const std::string_view& token, PdfTokenType tokenType, PdfVariant& variant, const PdfStatefulEncrypt* encrypt);
166
175 void EnqueueToken(const std::string_view& token, PdfTokenType type);
176
184 bool ReadDictionary(InputStreamDevice& device, PdfVariant& variant, const PdfStatefulEncrypt* encrypt, ParsingOptions opts);
185
193 bool ReadArray(InputStreamDevice& device, PdfVariant& variant, const PdfStatefulEncrypt* encrypt, ParsingOptions opts);
194
202 bool ReadString(InputStreamDevice& device, PdfVariant& variant, const PdfStatefulEncrypt* encrypt, ParsingOptions opts);
203
211 bool ReadHexString(InputStreamDevice& device, PdfVariant& variant, const PdfStatefulEncrypt* encrypt, ParsingOptions opts);
212
223
231 PdfLiteralDataType DetermineDataType(InputStreamDevice& device, const std::string_view& token, PdfTokenType tokenType, PdfVariant& variant, ParsingOptions opts);
232
233private:
234 PdfTokenizer(std::in_place_t, std::shared_ptr<charbuff>&& buffer);
235 bool readDataType(InputStreamDevice& device, PdfLiteralDataType dataType, PdfVariant& variant, const PdfStatefulEncrypt* encrypt, ParsingOptions opts);
241 bool drainContainer(InputStreamDevice& device, PdfTokenType endToken);
242
246 bool handleContainerTruncation(ParsingOptions opts, PdfErrorCode code, std::string_view msg);
247
252 bool handleContainerInnerError(InputStreamDevice& device, PdfTokenType endDelim, ParsingOptions opts,
253 PdfErrorCode code, std::string_view msg);
254
255private:
256 using TokenizerPair = std::pair<std::string, PdfTokenType>;
257 using TokenizerQueue = std::deque<TokenizerPair>;
258
259private:
260 std::shared_ptr<charbuff> m_buffer;
261 PdfTokenizerParams m_Params;
262 TokenizerQueue m_tokenQueue;
263 charbuff m_charBuffer;
264};
265
266};
267
268ENABLE_BITMASK_OPERATORS(PoDoFo::PdfTokenizerFlags);
269
270#endif // PDF_TOKENIZER_H
This file should be included as the FIRST file in every header of PoDoFo lib.
This class represents an input device It optionally supports peeking.
Definition InputDevice.h:19
This class is a parser for general PostScript content in PDF documents.
Definition PdfPostScriptTokenizer.h:25
A simple tokenizer for PDF files and PDF content streams.
Definition PdfTokenizer.h:47
bool TryReadNextToken(InputStreamDevice &device, std::string_view &token)
Reads the next token from the current file position ignoring all comments.
bool TryPeekNextToken(InputStreamDevice &device, std::string_view &token)
Try peek the next token from the current file position ignoring all comments, without actually consum...
void ReadNextVariant(InputStreamDevice &device, const std::string_view &token, PdfTokenType tokenType, PdfVariant &variant, const PdfStatefulEncrypt *encrypt)
Read the next variant from the current file position ignoring all comments.
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:29
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfTokenizerFlags
Definition PdfTokenizer.h:25
@ SkipReferences
Don't read indirect references in the form "1 0 R".
@ String
String datatype in PDF file. Strings have the form (Hallo World!) in PDF files.
@ Dictionary
A dictionary associates keys with values. A key can have another dictionary as value.
@ Array
An array of other PDF data types.
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
@ Reference
The reference datatype contains references to PDF objects in the PDF file of the form 4 0 R.
@ Real
Real datatype for floating point numbers.
@ Number
Number datatype for integer values.
@ Null
The null datatype is always null.
@ Bool
Boolean datatype: Accepts the values "true" and "false".
PdfErrorCode
Error Code enum values which are used in PdfError to describe the error.
Definition PdfError.h:22
@ StrictParsing
Load object streams immediately after parsing cross references sections.
@ None
Do not add a default appearance.
Definition PdfTokenizer.h:40
Definition PdfTokenizer.h:143
bool Strict
PdfTokenizerFlags::StrictParsing at the time of the entry-point call so internal paths can react to s...
Definition PdfTokenizer.h:151
bool ThrowOnError
If set, malformed content raises an exception in the current call.
Definition PdfTokenizer.h:147