Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 7 | #ifndef CORE_FXCRT_XML_CFX_XMLSYNTAXPARSER_H_ |
| 8 | #define CORE_FXCRT_XML_CFX_XMLSYNTAXPARSER_H_ |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 9 | |
| 10 | #include <stack> |
dan sinclair | bf58fbb | 2017-04-06 10:55:39 -0400 | [diff] [blame] | 11 | #include <vector> |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 12 | |
| 13 | #include "core/fxcrt/cfx_blockbuffer.h" |
Dan Sinclair | 3b71d26 | 2017-04-19 08:58:54 -0400 | [diff] [blame] | 14 | #include "core/fxcrt/cfx_seekablestreamproxy.h" |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 15 | #include "core/fxcrt/fx_string.h" |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 16 | #include "core/fxcrt/retain_ptr.h" |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 17 | #include "core/fxcrt/xml/cfx_xmlnode.h" |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 18 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 19 | enum class FX_XmlSyntaxResult { |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 20 | None, |
| 21 | InstructionOpen, |
| 22 | InstructionClose, |
| 23 | ElementOpen, |
| 24 | ElementBreak, |
| 25 | ElementClose, |
| 26 | TargetName, |
| 27 | TagName, |
| 28 | AttriName, |
| 29 | AttriValue, |
| 30 | Text, |
| 31 | CData, |
| 32 | TargetData, |
| 33 | Error, |
| 34 | EndOfString |
| 35 | }; |
| 36 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 37 | class CFX_XMLSyntaxParser { |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 38 | public: |
Tom Sepez | aeee187 | 2017-04-20 14:31:18 -0700 | [diff] [blame] | 39 | static bool IsXMLNameChar(wchar_t ch, bool bFirstChar); |
| 40 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 41 | explicit CFX_XMLSyntaxParser( |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 42 | const RetainPtr<CFX_SeekableStreamProxy>& pStream); |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 43 | ~CFX_XMLSyntaxParser(); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 44 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 45 | FX_XmlSyntaxResult DoSyntaxParse(); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 46 | |
| 47 | int32_t GetStatus() const; |
Nicolas Pena | ad22ac4 | 2017-04-06 12:28:47 -0400 | [diff] [blame] | 48 | FX_FILESIZE GetCurrentPos() const { return m_ParsedChars + m_Start; } |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 49 | FX_FILESIZE GetCurrentBinaryPos() const; |
| 50 | int32_t GetCurrentNodeNumber() const { return m_iCurrentNodeNum; } |
| 51 | int32_t GetLastNodeNumber() const { return m_iLastNodeNum; } |
| 52 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 53 | WideString GetTargetName() const { |
Dan Sinclair | 5f983bb | 2017-04-05 16:33:44 -0400 | [diff] [blame] | 54 | return m_BlockBuffer.GetTextData(0, m_iTextDataLength); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 55 | } |
Dan Sinclair | ee62ba2 | 2017-04-04 15:07:31 -0400 | [diff] [blame] | 56 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 57 | WideString GetTagName() const { |
Dan Sinclair | 5f983bb | 2017-04-05 16:33:44 -0400 | [diff] [blame] | 58 | return m_BlockBuffer.GetTextData(0, m_iTextDataLength); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 59 | } |
Dan Sinclair | ee62ba2 | 2017-04-04 15:07:31 -0400 | [diff] [blame] | 60 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 61 | WideString GetAttributeName() const { |
Dan Sinclair | 5f983bb | 2017-04-05 16:33:44 -0400 | [diff] [blame] | 62 | return m_BlockBuffer.GetTextData(0, m_iTextDataLength); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 63 | } |
Dan Sinclair | ee62ba2 | 2017-04-04 15:07:31 -0400 | [diff] [blame] | 64 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 65 | WideString GetAttributeValue() const { |
Dan Sinclair | 5f983bb | 2017-04-05 16:33:44 -0400 | [diff] [blame] | 66 | return m_BlockBuffer.GetTextData(0, m_iTextDataLength); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 67 | } |
Dan Sinclair | ee62ba2 | 2017-04-04 15:07:31 -0400 | [diff] [blame] | 68 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 69 | WideString GetTextData() const { |
Dan Sinclair | 5f983bb | 2017-04-05 16:33:44 -0400 | [diff] [blame] | 70 | return m_BlockBuffer.GetTextData(0, m_iTextDataLength); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 71 | } |
Dan Sinclair | ee62ba2 | 2017-04-04 15:07:31 -0400 | [diff] [blame] | 72 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 73 | WideString GetTargetData() const { |
Dan Sinclair | 5f983bb | 2017-04-05 16:33:44 -0400 | [diff] [blame] | 74 | return m_BlockBuffer.GetTextData(0, m_iTextDataLength); |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | protected: |
| 78 | enum class FDE_XmlSyntaxState { |
| 79 | Text, |
| 80 | Node, |
| 81 | Target, |
| 82 | Tag, |
| 83 | AttriName, |
| 84 | AttriEqualSign, |
| 85 | AttriQuotation, |
| 86 | AttriValue, |
| 87 | Entity, |
| 88 | EntityDecimal, |
| 89 | EntityHex, |
| 90 | CloseInstruction, |
| 91 | BreakElement, |
| 92 | CloseElement, |
| 93 | SkipDeclNode, |
| 94 | DeclCharData, |
| 95 | SkipComment, |
| 96 | SkipCommentOrDecl, |
| 97 | SkipCData, |
| 98 | TargetData |
| 99 | }; |
| 100 | |
| 101 | void ParseTextChar(wchar_t ch); |
| 102 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 103 | RetainPtr<CFX_SeekableStreamProxy> m_pStream; |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame^] | 104 | size_t m_iXMLPlaneSize; |
Ryan Harrison | 980a3ea | 2017-08-30 10:22:55 -0400 | [diff] [blame] | 105 | FX_FILESIZE m_iCurrentPos; |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 106 | int32_t m_iCurrentNodeNum; |
| 107 | int32_t m_iLastNodeNum; |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 108 | int32_t m_iParsedBytes; |
Nicolas Pena | ad22ac4 | 2017-04-06 12:28:47 -0400 | [diff] [blame] | 109 | FX_FILESIZE m_ParsedChars; |
dan sinclair | bf58fbb | 2017-04-06 10:55:39 -0400 | [diff] [blame] | 110 | std::vector<wchar_t> m_Buffer; |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame^] | 111 | size_t m_iBufferChars; |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 112 | bool m_bEOS; |
Nicolas Pena | ad22ac4 | 2017-04-06 12:28:47 -0400 | [diff] [blame] | 113 | FX_FILESIZE m_Start; // Start position in m_Buffer |
| 114 | FX_FILESIZE m_End; // End position in m_Buffer |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 115 | FX_XMLNODE m_CurNode; |
| 116 | std::stack<FX_XMLNODE> m_XMLNodeStack; |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 117 | CFX_BlockBuffer m_BlockBuffer; |
| 118 | int32_t m_iAllocStep; |
dan sinclair | bf58fbb | 2017-04-06 10:55:39 -0400 | [diff] [blame] | 119 | wchar_t* m_pCurrentBlock; // Pointer into CFX_BlockBuffer |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 120 | int32_t m_iIndexInBlock; |
| 121 | int32_t m_iTextDataLength; |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 122 | FX_XmlSyntaxResult m_syntaxParserResult; |
Dan Sinclair | ac35589 | 2017-04-03 16:46:21 -0400 | [diff] [blame] | 123 | FDE_XmlSyntaxState m_syntaxParserState; |
| 124 | wchar_t m_wQuotationMark; |
| 125 | int32_t m_iEntityStart; |
| 126 | std::stack<wchar_t> m_SkipStack; |
| 127 | wchar_t m_SkipChar; |
| 128 | }; |
| 129 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 130 | #endif // CORE_FXCRT_XML_CFX_XMLSYNTAXPARSER_H_ |