• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PAGE_CPDF_PSENGINE_H_
8 #define CORE_FPDFAPI_PAGE_CPDF_PSENGINE_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include <memory>
14 #include <vector>
15 
16 #include "core/fxcrt/bytestring.h"
17 #include "third_party/base/span.h"
18 
19 class CPDF_PSEngine;
20 class CPDF_PSProc;
21 class CPDF_SimpleParser;
22 
23 enum PDF_PSOP : uint8_t {
24   PSOP_ADD,
25   PSOP_SUB,
26   PSOP_MUL,
27   PSOP_DIV,
28   PSOP_IDIV,
29   PSOP_MOD,
30   PSOP_NEG,
31   PSOP_ABS,
32   PSOP_CEILING,
33   PSOP_FLOOR,
34   PSOP_ROUND,
35   PSOP_TRUNCATE,
36   PSOP_SQRT,
37   PSOP_SIN,
38   PSOP_COS,
39   PSOP_ATAN,
40   PSOP_EXP,
41   PSOP_LN,
42   PSOP_LOG,
43   PSOP_CVI,
44   PSOP_CVR,
45   PSOP_EQ,
46   PSOP_NE,
47   PSOP_GT,
48   PSOP_GE,
49   PSOP_LT,
50   PSOP_LE,
51   PSOP_AND,
52   PSOP_OR,
53   PSOP_XOR,
54   PSOP_NOT,
55   PSOP_BITSHIFT,
56   PSOP_TRUE,
57   PSOP_FALSE,
58   PSOP_IF,
59   PSOP_IFELSE,
60   PSOP_POP,
61   PSOP_EXCH,
62   PSOP_DUP,
63   PSOP_COPY,
64   PSOP_INDEX,
65   PSOP_ROLL,
66   PSOP_PROC,
67   PSOP_CONST
68 };
69 
70 class CPDF_PSOP {
71  public:
72   CPDF_PSOP();
73   explicit CPDF_PSOP(PDF_PSOP op);
74   explicit CPDF_PSOP(float value);
75   ~CPDF_PSOP();
76 
77   bool Parse(CPDF_SimpleParser* parser, int depth);
78   void Execute(CPDF_PSEngine* pEngine);
79   float GetFloatValue() const;
GetOp()80   PDF_PSOP GetOp() const { return m_op; }
81 
82  private:
83   const PDF_PSOP m_op;
84   const float m_value;
85   std::unique_ptr<CPDF_PSProc> m_proc;
86 };
87 
88 class CPDF_PSProc {
89  public:
90   CPDF_PSProc();
91   ~CPDF_PSProc();
92 
93   bool Parse(CPDF_SimpleParser* parser, int depth);
94   bool Execute(CPDF_PSEngine* pEngine);
95 
96   // These methods are exposed for testing.
97   void AddOperatorForTesting(ByteStringView word);
num_operators()98   size_t num_operators() const { return m_Operators.size(); }
last_operator()99   const std::unique_ptr<CPDF_PSOP>& last_operator() {
100     return m_Operators.back();
101   }
102 
103  private:
104   static constexpr int kMaxDepth = 128;
105 
106   void AddOperator(ByteStringView word);
107 
108   std::vector<std::unique_ptr<CPDF_PSOP>> m_Operators;
109 };
110 
111 class CPDF_PSEngine {
112  public:
113   CPDF_PSEngine();
114   ~CPDF_PSEngine();
115 
116   bool Parse(pdfium::span<const uint8_t> input);
117   bool Execute();
118   bool DoOperator(PDF_PSOP op);
Reset()119   void Reset() { m_StackCount = 0; }
120   void Push(float value);
121   float Pop();
122   int PopInt();
GetStackSize()123   uint32_t GetStackSize() const { return m_StackCount; }
124 
125  private:
126   static constexpr uint32_t kPSEngineStackSize = 100;
127 
128   uint32_t m_StackCount = 0;
129   CPDF_PSProc m_MainProc;
130   float m_Stack[kPSEngineStackSize] = {};
131 };
132 
133 #endif  // CORE_FPDFAPI_PAGE_CPDF_PSENGINE_H_
134