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 <stdint.h> 11 12 #include <array> 13 #include <memory> 14 #include <vector> 15 16 #include "core/fxcrt/bytestring.h" 17 #include "core/fxcrt/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); last_operator()98 const std::unique_ptr<CPDF_PSOP>& last_operator() { 99 return m_Operators.back(); 100 } 101 102 private: 103 static constexpr int kMaxDepth = 128; 104 105 void AddOperator(ByteStringView word); 106 107 std::vector<std::unique_ptr<CPDF_PSOP>> m_Operators; 108 }; 109 110 class CPDF_PSEngine { 111 public: 112 CPDF_PSEngine(); 113 ~CPDF_PSEngine(); 114 115 bool Parse(pdfium::span<const uint8_t> input); 116 bool Execute(); 117 bool DoOperator(PDF_PSOP op); Reset()118 void Reset() { m_StackCount = 0; } 119 void Push(float value); 120 float Pop(); 121 int PopInt(); GetStackSize()122 uint32_t GetStackSize() const { return m_StackCount; } 123 124 private: 125 static constexpr uint32_t kPSEngineStackSize = 100; 126 127 uint32_t m_StackCount = 0; 128 CPDF_PSProc m_MainProc; 129 std::array<float, kPSEngineStackSize> m_Stack = {}; 130 }; 131 132 #endif // CORE_FPDFAPI_PAGE_CPDF_PSENGINE_H_ 133