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