1 // Copyright 2014 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_SRC_FXCRT_XML_INT_H_
8 #define CORE_SRC_FXCRT_XML_INT_H_
9 
10 class CXML_DataBufAcc : public IFX_BufferRead
11 {
12 public:
CXML_DataBufAcc(FX_LPCBYTE pBuffer,size_t size)13     CXML_DataBufAcc(FX_LPCBYTE pBuffer, size_t size)
14         : m_pBuffer(pBuffer)
15         , m_dwSize(size)
16         , m_dwCurPos(0)
17     {
18     }
~CXML_DataBufAcc()19     virtual ~CXML_DataBufAcc() {}
Release()20     virtual void			Release()
21     {
22         delete this;
23     }
IsEOF()24     virtual FX_BOOL			IsEOF()
25     {
26         return m_dwCurPos >= m_dwSize;
27     }
GetPosition()28     virtual FX_FILESIZE		GetPosition()
29     {
30         return (FX_FILESIZE)m_dwCurPos;
31     }
ReadBlock(void * buffer,size_t size)32     virtual size_t			ReadBlock(void* buffer, size_t size)
33     {
34         return 0;
35     }
36     virtual FX_BOOL			ReadNextBlock(FX_BOOL bRestart = FALSE)
37     {
38         if (bRestart) {
39             m_dwCurPos = 0;
40         }
41         if (m_dwCurPos < m_dwSize) {
42             m_dwCurPos = m_dwSize;
43             return TRUE;
44         }
45         return FALSE;
46     }
GetBlockBuffer()47     virtual FX_LPCBYTE		GetBlockBuffer()
48     {
49         return m_pBuffer;
50     }
GetBlockSize()51     virtual size_t			GetBlockSize()
52     {
53         return m_dwSize;
54     }
GetBlockOffset()55     virtual FX_FILESIZE		GetBlockOffset()
56     {
57         return 0;
58     }
59 protected:
60     FX_LPCBYTE		m_pBuffer;
61     size_t			m_dwSize;
62     size_t			m_dwCurPos;
63 };
64 #define FX_XMLDATASTREAM_BufferSize		(32 * 1024)
65 class CXML_DataStmAcc : public IFX_BufferRead
66 {
67 public:
CXML_DataStmAcc(IFX_FileRead * pFileRead)68     CXML_DataStmAcc(IFX_FileRead *pFileRead)
69         : m_pFileRead(pFileRead)
70         , m_pBuffer(NULL)
71         , m_nStart(0)
72         , m_dwSize(0)
73     {
74         FXSYS_assert(m_pFileRead != NULL);
75     }
~CXML_DataStmAcc()76     virtual ~CXML_DataStmAcc()
77     {
78         if (m_pBuffer) {
79             FX_Free(m_pBuffer);
80         }
81     }
Release()82     virtual void			Release()
83     {
84         delete this;
85     }
IsEOF()86     virtual FX_BOOL			IsEOF()
87     {
88         return m_nStart + (FX_FILESIZE)m_dwSize >= m_pFileRead->GetSize();
89     }
GetPosition()90     virtual FX_FILESIZE		GetPosition()
91     {
92         return m_nStart + (FX_FILESIZE)m_dwSize;
93     }
ReadBlock(void * buffer,size_t size)94     virtual size_t			ReadBlock(void* buffer, size_t size)
95     {
96         return 0;
97     }
98     virtual FX_BOOL			ReadNextBlock(FX_BOOL bRestart = FALSE)
99     {
100         if (bRestart) {
101             m_nStart = 0;
102         }
103         FX_FILESIZE nLength = m_pFileRead->GetSize();
104         m_nStart += (FX_FILESIZE)m_dwSize;
105         if (m_nStart >= nLength) {
106             return FALSE;
107         }
108         m_dwSize = (size_t)FX_MIN(FX_XMLDATASTREAM_BufferSize, nLength - m_nStart);
109         if (!m_pBuffer) {
110             m_pBuffer = FX_Alloc(FX_BYTE, m_dwSize);
111         }
112         return m_pFileRead->ReadBlock(m_pBuffer, m_nStart, m_dwSize);
113     }
GetBlockBuffer()114     virtual FX_LPCBYTE		GetBlockBuffer()
115     {
116         return (FX_LPCBYTE)m_pBuffer;
117     }
GetBlockSize()118     virtual size_t			GetBlockSize()
119     {
120         return m_dwSize;
121     }
GetBlockOffset()122     virtual FX_FILESIZE		GetBlockOffset()
123     {
124         return m_nStart;
125     }
126 protected:
127     IFX_FileRead	*m_pFileRead;
128     FX_LPBYTE		m_pBuffer;
129     FX_FILESIZE		m_nStart;
130     size_t			m_dwSize;
131 };
132 class CXML_Parser
133 {
134 public:
135     ~CXML_Parser();
136     IFX_BufferRead*	m_pDataAcc;
137     FX_BOOL			m_bOwnedStream;
138     FX_FILESIZE		m_nOffset;
139     FX_BOOL			m_bSaveSpaceChars;
140     FX_LPCBYTE		m_pBuffer;
141     size_t			m_dwBufferSize;
142     FX_FILESIZE		m_nBufferOffset;
143     size_t			m_dwIndex;
144     FX_BOOL			Init(FX_LPBYTE pBuffer, size_t size);
145     FX_BOOL			Init(IFX_FileRead *pFileRead);
146     FX_BOOL			Init(IFX_BufferRead *pBuffer);
147     FX_BOOL			Init(FX_BOOL bOwndedStream);
148     FX_BOOL			ReadNextBlock();
149     FX_BOOL			IsEOF();
150     FX_BOOL			HaveAvailData();
151     void			SkipWhiteSpaces();
152     void			GetName(CFX_ByteString& space, CFX_ByteString& name);
153     void			GetAttrValue(CFX_WideString &value);
154     FX_DWORD		GetCharRef();
155     void			GetTagName(CFX_ByteString &space, CFX_ByteString &name, FX_BOOL &bEndTag, FX_BOOL bStartTag = FALSE);
156     void			SkipLiterals(FX_BSTR str);
157     CXML_Element*	ParseElement(CXML_Element* pParent, FX_BOOL bStartTag = FALSE);
158     void			InsertContentSegment(FX_BOOL bCDATA, FX_WSTR content, CXML_Element* pElement);
159     void			InsertCDATASegment(CFX_UTF8Decoder& decoder, CXML_Element* pElement);
160 };
161 void FX_XML_SplitQualifiedName(FX_BSTR bsFullName, CFX_ByteStringC &bsSpace, CFX_ByteStringC &bsName);
162 
163 #endif  // CORE_SRC_FXCRT_XML_INT_H_
164