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 #ifndef CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
8 #define CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
9
10 #include <iosfwd>
11 #include <vector>
12
13 #include "core/fxcrt/bytestring.h"
14 #include "core/fxcrt/retain_ptr.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16
17 class CPDF_Array;
18 class CPDF_Dictionary;
19 class CPDF_Object;
20 class IFX_SeekableReadStream;
21
22 // Use the accessors below instead of directly accessing PDF_CharType.
23 extern const char PDF_CharType[256];
24
PDFCharIsWhitespace(uint8_t c)25 inline bool PDFCharIsWhitespace(uint8_t c) {
26 return PDF_CharType[c] == 'W';
27 }
PDFCharIsNumeric(uint8_t c)28 inline bool PDFCharIsNumeric(uint8_t c) {
29 return PDF_CharType[c] == 'N';
30 }
PDFCharIsDelimiter(uint8_t c)31 inline bool PDFCharIsDelimiter(uint8_t c) {
32 return PDF_CharType[c] == 'D';
33 }
PDFCharIsOther(uint8_t c)34 inline bool PDFCharIsOther(uint8_t c) {
35 return PDF_CharType[c] == 'R';
36 }
37
PDFCharIsLineEnding(uint8_t c)38 inline bool PDFCharIsLineEnding(uint8_t c) {
39 return c == '\r' || c == '\n';
40 }
41
42 // On success, return a positive offset value to the PDF header. If the header
43 // cannot be found, or if there is an error reading from |pFile|, then return
44 // nullopt.
45 absl::optional<FX_FILESIZE> GetHeaderOffset(
46 const RetainPtr<IFX_SeekableReadStream>& pFile);
47
48 ByteString PDF_NameDecode(ByteStringView orig);
49 ByteString PDF_NameEncode(const ByteString& orig);
50
51 // Return |nCount| elements from |pArray| as a vector of floats. |pArray| must
52 // have at least |nCount| elements.
53 std::vector<float> ReadArrayElementsToVector(const CPDF_Array* pArray,
54 size_t nCount);
55
56 // Returns true if |dict| is non-null and has a /Type name entry that matches
57 // |type|.
58 bool ValidateDictType(const CPDF_Dictionary* dict, ByteStringView type);
59
60 // Returns true if |dict| is non-null and all entries in |dict| are dictionaries
61 // of |type|.
62 bool ValidateDictAllResourcesOfType(const CPDF_Dictionary* dict,
63 ByteStringView type);
64
65 // Shorthand for ValidateDictAllResourcesOfType(dict, "Font").
66 bool ValidateFontResourceDict(const CPDF_Dictionary* dict);
67
68 // Like ValidateDictType(), but /Type can also not exist.
69 bool ValidateDictOptionalType(const CPDF_Dictionary* dict, ByteStringView type);
70
71 std::ostream& operator<<(std::ostream& buf, const CPDF_Object* pObj);
72
73 #endif // CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
74