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 #ifndef CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
8 #define CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
9 
10 #include <ostream>
11 #include <vector>
12 
13 #include "core/fxcrt/fx_string.h"
14 #include "core/fxcrt/retain_ptr.h"
15 #include "third_party/base/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 Optional<FX_FILESIZE> GetHeaderOffset(
46     const RetainPtr<IFX_SeekableReadStream>& pFile);
47 
48 int32_t GetDirectInteger(const CPDF_Dictionary* pDict, const ByteString& key);
49 
50 ByteString PDF_NameDecode(ByteStringView orig);
51 ByteString PDF_NameEncode(const ByteString& orig);
52 
53 // Return |nCount| elements from |pArray| as a vector of floats. |pArray| must
54 // have at least |nCount| elements.
55 std::vector<float> ReadArrayElementsToVector(const CPDF_Array* pArray,
56                                              size_t nCount);
57 
58 // Returns true if |dict| has a /Type name entry that matches |type|.
59 bool ValidateDictType(const CPDF_Dictionary* dict, const ByteString& type);
60 
61 // Returns true if |dict| is non-null and all entries in |dict| are dictionaries
62 // of |type|.
63 bool ValidateDictAllResourcesOfType(const CPDF_Dictionary* dict,
64                                     const ByteString& type);
65 
66 // Shorthand for ValidateDictAllResourcesOfType(dict, "Font").
67 bool ValidateFontResourceDict(const CPDF_Dictionary* dict);
68 
69 std::ostream& operator<<(std::ostream& buf, const CPDF_Object* pObj);
70 
71 #endif  // CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
72