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 _FDE_CSSSYNTAX 8 #define _FDE_CSSSYNTAX 9 class CFDE_CSSTextBuf : public CFX_Target { 10 public: 11 CFDE_CSSTextBuf(); 12 ~CFDE_CSSTextBuf(); 13 FX_BOOL AttachBuffer(const FX_WCHAR* pBuffer, int32_t iBufLen); 14 FX_BOOL EstimateSize(int32_t iAllocSize); 15 int32_t LoadFromStream(IFX_Stream* pTxtStream, 16 int32_t iStreamOffset, 17 int32_t iMaxChars, 18 FX_BOOL& bEOS); AppendChar(FX_WCHAR wch)19 FX_BOOL AppendChar(FX_WCHAR wch) { 20 if (m_iDatLen >= m_iBufLen && !ExpandBuf(m_iBufLen * 2)) { 21 return FALSE; 22 } 23 return (m_pBuffer[m_iDatLen++] = wch), TRUE; 24 } Clear()25 void Clear() { m_iDatPos = m_iDatLen = 0; } 26 void Reset(); TrimEnd()27 int32_t TrimEnd() { 28 while (m_iDatLen > 0 && m_pBuffer[m_iDatLen - 1] <= ' ') { 29 --m_iDatLen; 30 } 31 AppendChar(0); 32 return --m_iDatLen; 33 } 34 void Subtract(int32_t iStart, int32_t iLength); IsEOF()35 FX_BOOL IsEOF() const { return m_iDatPos >= m_iDatLen; } GetAt(int32_t index)36 FX_WCHAR GetAt(int32_t index) const { return m_pBuffer[index]; } GetChar()37 FX_WCHAR GetChar() const { return m_pBuffer[m_iDatPos]; } GetNextChar()38 FX_WCHAR GetNextChar() const { 39 return (m_iDatPos + 1 >= m_iDatLen) ? 0 : m_pBuffer[m_iDatPos + 1]; 40 } MoveNext()41 void MoveNext() { m_iDatPos++; } GetLength()42 int32_t GetLength() const { return m_iDatLen; } GetBuffer()43 const FX_WCHAR* GetBuffer() const { return m_pBuffer; } 44 45 protected: 46 FX_BOOL ExpandBuf(int32_t iDesiredSize); 47 FX_BOOL m_bExtBuf; 48 FX_WCHAR* m_pBuffer; 49 int32_t m_iBufLen; 50 int32_t m_iDatLen; 51 int32_t m_iDatPos; 52 }; 53 #define FDE_CSSSYNTAXCHECK_AllowCharset 1 54 #define FDE_CSSSYNTAXCHECK_AllowImport 2 55 enum FDE_CSSSYNTAXMODE { 56 FDE_CSSSYNTAXMODE_RuleSet, 57 FDE_CSSSYNTAXMODE_Comment, 58 FDE_CSSSYNTAXMODE_AtRule, 59 FDE_CSSSYNTAXMODE_UnknownRule, 60 FDE_CSSSYNTAXMODE_Charset, 61 FDE_CSSSYNTAXMODE_Import, 62 FDE_CSSSYNTAXMODE_MediaRule, 63 FDE_CSSSYNTAXMODE_URI, 64 FDE_CSSSYNTAXMODE_MediaType, 65 FDE_CSSSYNTAXMODE_Selector, 66 FDE_CSSSYNTAXMODE_PropertyName, 67 FDE_CSSSYNTAXMODE_PropertyValue, 68 }; 69 class CFDE_CSSSyntaxParser : public IFDE_CSSSyntaxParser, public CFX_Target { 70 public: 71 CFDE_CSSSyntaxParser(); 72 ~CFDE_CSSSyntaxParser(); Release()73 virtual void Release() { delete this; } 74 virtual FX_BOOL Init(IFX_Stream* pStream, 75 int32_t iCSSPlaneSize, 76 int32_t iTextDataSize = 32, 77 FX_BOOL bOnlyDeclaration = FALSE); 78 virtual FX_BOOL Init(const FX_WCHAR* pBuffer, 79 int32_t iBufferSize, 80 int32_t iTextDatSize = 32, 81 FX_BOOL bOnlyDeclaration = FALSE); 82 virtual FDE_CSSSYNTAXSTATUS DoSyntaxParse(); 83 virtual const FX_WCHAR* GetCurrentString(int32_t& iLength) const; 84 85 protected: 86 void Reset(FX_BOOL bOnlyDeclaration); 87 void SwitchMode(FDE_CSSSYNTAXMODE eMode); 88 int32_t SwitchToComment(); 89 90 FX_BOOL RestoreMode(); 91 FX_BOOL AppendChar(FX_WCHAR wch); 92 int32_t SaveTextData(); IsCharsetEnabled()93 FX_BOOL IsCharsetEnabled() const { 94 return (m_dwCheck & FDE_CSSSYNTAXCHECK_AllowCharset) != 0; 95 } DisableCharset()96 void DisableCharset() { m_dwCheck = FDE_CSSSYNTAXCHECK_AllowImport; } 97 FX_BOOL IsImportEnabled() const; DisableImport()98 void DisableImport() { m_dwCheck = 0; } 99 IFX_Stream* m_pStream; 100 int32_t m_iStreamPos; 101 int32_t m_iPlaneSize; 102 CFDE_CSSTextBuf m_TextData; 103 CFDE_CSSTextBuf m_TextPlane; 104 int32_t m_iTextDatLen; 105 FX_DWORD m_dwCheck; 106 FDE_CSSSYNTAXMODE m_eMode; 107 FDE_CSSSYNTAXSTATUS m_eStatus; 108 CFX_StackTemplate<FDE_CSSSYNTAXMODE> m_ModeStack; 109 }; 110 #endif 111