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