Home | History | Annotate | Download | only in fxcrt
      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 <algorithm>
     11 
     12 #include "core/include/fxcrt/fx_stream.h"
     13 
     14 class CFX_UTF8Decoder;
     15 class CXML_Element;
     16 
     17 class CXML_DataBufAcc : public IFX_BufferRead {
     18  public:
     19   CXML_DataBufAcc(const uint8_t* pBuffer, size_t size)
     20       : m_pBuffer(pBuffer), m_dwSize(size), m_dwCurPos(0) {}
     21   ~CXML_DataBufAcc() override {}
     22 
     23   // IFX_BufferRead
     24   void Release() override { delete this; }
     25   FX_BOOL IsEOF() override { return m_dwCurPos >= m_dwSize; }
     26   FX_FILESIZE GetPosition() override { return (FX_FILESIZE)m_dwCurPos; }
     27   size_t ReadBlock(void* buffer, size_t size) override { return 0; }
     28   FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE) override {
     29     if (bRestart) {
     30       m_dwCurPos = 0;
     31     }
     32     if (m_dwCurPos < m_dwSize) {
     33       m_dwCurPos = m_dwSize;
     34       return TRUE;
     35     }
     36     return FALSE;
     37   }
     38   const uint8_t* GetBlockBuffer() override { return m_pBuffer; }
     39   size_t GetBlockSize() override { return m_dwSize; }
     40   FX_FILESIZE GetBlockOffset() override { return 0; }
     41 
     42  protected:
     43   const uint8_t* m_pBuffer;
     44   size_t m_dwSize;
     45   size_t m_dwCurPos;
     46 };
     47 
     48 class CXML_DataStmAcc : public IFX_BufferRead {
     49  public:
     50   explicit CXML_DataStmAcc(IFX_FileRead* pFileRead)
     51       : m_pFileRead(pFileRead), m_pBuffer(NULL), m_nStart(0), m_dwSize(0) {
     52     FXSYS_assert(m_pFileRead);
     53   }
     54   ~CXML_DataStmAcc() override { FX_Free(m_pBuffer); }
     55 
     56   void Release() override { delete this; }
     57   FX_BOOL IsEOF() override {
     58     return m_nStart + (FX_FILESIZE)m_dwSize >= m_pFileRead->GetSize();
     59   }
     60   FX_FILESIZE GetPosition() override {
     61     return m_nStart + (FX_FILESIZE)m_dwSize;
     62   }
     63   size_t ReadBlock(void* buffer, size_t size) override { return 0; }
     64   FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE) override {
     65     if (bRestart) {
     66       m_nStart = 0;
     67     }
     68     FX_FILESIZE nLength = m_pFileRead->GetSize();
     69     m_nStart += (FX_FILESIZE)m_dwSize;
     70     if (m_nStart >= nLength) {
     71       return FALSE;
     72     }
     73     static const FX_FILESIZE FX_XMLDATASTREAM_BufferSize = 32 * 1024;
     74     m_dwSize = static_cast<size_t>(
     75         std::min(FX_XMLDATASTREAM_BufferSize, nLength - m_nStart));
     76     if (!m_pBuffer) {
     77       m_pBuffer = FX_Alloc(uint8_t, m_dwSize);
     78     }
     79     return m_pFileRead->ReadBlock(m_pBuffer, m_nStart, m_dwSize);
     80   }
     81   const uint8_t* GetBlockBuffer() override { return (const uint8_t*)m_pBuffer; }
     82   size_t GetBlockSize() override { return m_dwSize; }
     83   FX_FILESIZE GetBlockOffset() override { return m_nStart; }
     84 
     85  protected:
     86   IFX_FileRead* m_pFileRead;
     87   uint8_t* m_pBuffer;
     88   FX_FILESIZE m_nStart;
     89   size_t m_dwSize;
     90 };
     91 
     92 class CXML_Parser {
     93  public:
     94   ~CXML_Parser();
     95   IFX_BufferRead* m_pDataAcc;
     96   FX_BOOL m_bOwnedStream;
     97   FX_FILESIZE m_nOffset;
     98   FX_BOOL m_bSaveSpaceChars;
     99   const uint8_t* m_pBuffer;
    100   size_t m_dwBufferSize;
    101   FX_FILESIZE m_nBufferOffset;
    102   size_t m_dwIndex;
    103   FX_BOOL Init(uint8_t* pBuffer, size_t size);
    104   FX_BOOL Init(IFX_FileRead* pFileRead);
    105   FX_BOOL Init(IFX_BufferRead* pBuffer);
    106   FX_BOOL Init(FX_BOOL bOwndedStream);
    107   FX_BOOL ReadNextBlock();
    108   FX_BOOL IsEOF();
    109   FX_BOOL HaveAvailData();
    110   void SkipWhiteSpaces();
    111   void GetName(CFX_ByteString& space, CFX_ByteString& name);
    112   void GetAttrValue(CFX_WideString& value);
    113   FX_DWORD GetCharRef();
    114   void GetTagName(CFX_ByteString& space,
    115                   CFX_ByteString& name,
    116                   FX_BOOL& bEndTag,
    117                   FX_BOOL bStartTag = FALSE);
    118   void SkipLiterals(const CFX_ByteStringC& str);
    119   CXML_Element* ParseElement(CXML_Element* pParent, FX_BOOL bStartTag = FALSE);
    120   void InsertContentSegment(FX_BOOL bCDATA,
    121                             const CFX_WideStringC& content,
    122                             CXML_Element* pElement);
    123   void InsertCDATASegment(CFX_UTF8Decoder& decoder, CXML_Element* pElement);
    124 };
    125 
    126 void FX_XML_SplitQualifiedName(const CFX_ByteStringC& bsFullName,
    127                                CFX_ByteStringC& bsSpace,
    128                                CFX_ByteStringC& bsName);
    129 
    130 #endif  // CORE_SRC_FXCRT_XML_INT_H_
    131