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