Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | #include "xfa/fde/css/fde_csssyntax.h" |
| 8 | |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 11 | #include "xfa/fde/css/fde_cssdatatable.h" |
| 12 | #include "xfa/fgas/crt/fgas_codepage.h" |
| 13 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 14 | namespace { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 15 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 16 | bool FDE_IsSelectorStart(FX_WCHAR wch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 17 | return wch == '.' || wch == '#' || wch == '*' || (wch >= 'a' && wch <= 'z') || |
| 18 | (wch >= 'A' && wch <= 'Z'); |
| 19 | } |
| 20 | |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 21 | } // namespace |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 22 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | CFDE_CSSSyntaxParser::CFDE_CSSSyntaxParser() |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 24 | : m_pStream(nullptr), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 25 | m_iStreamPos(0), |
| 26 | m_iPlaneSize(0), |
| 27 | m_iTextDatLen(0), |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 28 | m_dwCheck((uint32_t)-1), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 29 | m_eMode(FDE_CSSSYNTAXMODE_RuleSet), |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 30 | m_eStatus(FDE_CSSSyntaxStatus::None), |
dsinclair | 3496545 | 2016-07-18 13:14:49 -0700 | [diff] [blame] | 31 | m_ModeStack(100) {} |
| 32 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 33 | CFDE_CSSSyntaxParser::~CFDE_CSSSyntaxParser() { |
| 34 | m_TextData.Reset(); |
| 35 | m_TextPlane.Reset(); |
| 36 | } |
dsinclair | 3496545 | 2016-07-18 13:14:49 -0700 | [diff] [blame] | 37 | |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 38 | bool CFDE_CSSSyntaxParser::Init(const CFX_RetainPtr<IFGAS_Stream>& pStream, |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 39 | int32_t iCSSPlaneSize, |
| 40 | int32_t iTextDataSize, |
| 41 | bool bOnlyDeclaration) { |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 42 | ASSERT(pStream && iCSSPlaneSize > 0 && iTextDataSize > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | Reset(bOnlyDeclaration); |
| 44 | if (!m_TextData.EstimateSize(iTextDataSize)) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 45 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 46 | } |
| 47 | uint8_t bom[4]; |
| 48 | m_pStream = pStream; |
| 49 | m_iStreamPos = m_pStream->GetBOM(bom); |
| 50 | m_iPlaneSize = iCSSPlaneSize; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 51 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 52 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 53 | bool CFDE_CSSSyntaxParser::Init(const FX_WCHAR* pBuffer, |
| 54 | int32_t iBufferSize, |
| 55 | int32_t iTextDatSize, |
| 56 | bool bOnlyDeclaration) { |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 57 | ASSERT(pBuffer && iBufferSize > 0 && iTextDatSize > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 58 | Reset(bOnlyDeclaration); |
| 59 | if (!m_TextData.EstimateSize(iTextDatSize)) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 60 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 61 | } |
| 62 | return m_TextPlane.AttachBuffer(pBuffer, iBufferSize); |
| 63 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 64 | void CFDE_CSSSyntaxParser::Reset(bool bOnlyDeclaration) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 65 | m_TextPlane.Reset(); |
| 66 | m_TextData.Reset(); |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 67 | m_pStream = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 68 | m_iStreamPos = 0; |
| 69 | m_iTextDatLen = 0; |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 70 | m_dwCheck = (uint32_t)-1; |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 71 | m_eStatus = FDE_CSSSyntaxStatus::None; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 72 | m_eMode = bOnlyDeclaration ? FDE_CSSSYNTAXMODE_PropertyName |
| 73 | : FDE_CSSSYNTAXMODE_RuleSet; |
| 74 | } |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 75 | FDE_CSSSyntaxStatus CFDE_CSSSyntaxParser::DoSyntaxParse() { |
| 76 | while (m_eStatus >= FDE_CSSSyntaxStatus::None) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | if (m_TextPlane.IsEOF()) { |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 78 | if (!m_pStream) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 79 | if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && |
| 80 | m_TextData.GetLength() > 0) { |
| 81 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 82 | m_eStatus = FDE_CSSSyntaxStatus::PropertyValue; |
| 83 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 84 | } |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 85 | m_eStatus = FDE_CSSSyntaxStatus::EOS; |
| 86 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 87 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 88 | bool bEOS; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 89 | int32_t iLen = m_TextPlane.LoadFromStream(m_pStream, m_iStreamPos, |
| 90 | m_iPlaneSize, bEOS); |
| 91 | m_iStreamPos = m_pStream->GetPosition(); |
| 92 | if (iLen < 1) { |
| 93 | if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && |
| 94 | m_TextData.GetLength() > 0) { |
| 95 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 96 | m_eStatus = FDE_CSSSyntaxStatus::PropertyValue; |
| 97 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 98 | } |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 99 | m_eStatus = FDE_CSSSyntaxStatus::EOS; |
| 100 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | FX_WCHAR wch; |
| 104 | while (!m_TextPlane.IsEOF()) { |
| 105 | wch = m_TextPlane.GetChar(); |
| 106 | switch (m_eMode) { |
| 107 | case FDE_CSSSYNTAXMODE_RuleSet: |
| 108 | switch (wch) { |
| 109 | case '@': |
| 110 | m_TextPlane.MoveNext(); |
| 111 | SwitchMode(FDE_CSSSYNTAXMODE_AtRule); |
| 112 | break; |
| 113 | case '}': |
| 114 | m_TextPlane.MoveNext(); |
| 115 | if (RestoreMode()) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 116 | return FDE_CSSSyntaxStatus::DeclClose; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 117 | } else { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 118 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 119 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 120 | } |
| 121 | break; |
| 122 | case '/': |
| 123 | if (m_TextPlane.GetNextChar() == '*') { |
| 124 | m_ModeStack.Push(m_eMode); |
| 125 | SwitchMode(FDE_CSSSYNTAXMODE_Comment); |
| 126 | break; |
| 127 | } |
| 128 | default: |
| 129 | if (wch <= ' ') { |
| 130 | m_TextPlane.MoveNext(); |
| 131 | } else if (FDE_IsSelectorStart(wch)) { |
| 132 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 133 | return FDE_CSSSyntaxStatus::StyleRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 134 | } else { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 135 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 136 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 137 | } |
| 138 | break; |
| 139 | } |
| 140 | break; |
| 141 | case FDE_CSSSYNTAXMODE_Selector: |
| 142 | switch (wch) { |
| 143 | case ',': |
| 144 | m_TextPlane.MoveNext(); |
| 145 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
| 146 | if (m_iTextDatLen > 0) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 147 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 148 | } |
| 149 | break; |
| 150 | case '{': |
| 151 | if (m_TextData.GetLength() > 0) { |
| 152 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 153 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 154 | } else { |
| 155 | m_TextPlane.MoveNext(); |
| 156 | m_ModeStack.Push(FDE_CSSSYNTAXMODE_RuleSet); |
| 157 | SwitchMode(FDE_CSSSYNTAXMODE_PropertyName); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 158 | return FDE_CSSSyntaxStatus::DeclOpen; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 159 | } |
| 160 | break; |
| 161 | case '/': |
| 162 | if (m_TextPlane.GetNextChar() == '*') { |
| 163 | if (SwitchToComment() > 0) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 164 | return FDE_CSSSyntaxStatus::Selector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 165 | } |
| 166 | break; |
| 167 | } |
| 168 | default: |
| 169 | AppendChar(wch); |
| 170 | break; |
| 171 | } |
| 172 | break; |
| 173 | case FDE_CSSSYNTAXMODE_PropertyName: |
| 174 | switch (wch) { |
| 175 | case ':': |
| 176 | m_TextPlane.MoveNext(); |
| 177 | SwitchMode(FDE_CSSSYNTAXMODE_PropertyValue); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 178 | return FDE_CSSSyntaxStatus::PropertyName; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 179 | case '}': |
| 180 | m_TextPlane.MoveNext(); |
| 181 | if (RestoreMode()) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 182 | return FDE_CSSSyntaxStatus::DeclClose; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 183 | } else { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 184 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 185 | return m_eStatus; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 186 | } |
| 187 | break; |
| 188 | case '/': |
| 189 | if (m_TextPlane.GetNextChar() == '*') { |
| 190 | if (SwitchToComment() > 0) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 191 | return FDE_CSSSyntaxStatus::PropertyName; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 192 | } |
| 193 | break; |
| 194 | } |
| 195 | default: |
| 196 | AppendChar(wch); |
| 197 | break; |
| 198 | } |
| 199 | break; |
| 200 | case FDE_CSSSYNTAXMODE_PropertyValue: |
| 201 | switch (wch) { |
| 202 | case ';': |
| 203 | m_TextPlane.MoveNext(); |
| 204 | case '}': |
| 205 | SwitchMode(FDE_CSSSYNTAXMODE_PropertyName); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 206 | return FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 207 | case '/': |
| 208 | if (m_TextPlane.GetNextChar() == '*') { |
| 209 | if (SwitchToComment() > 0) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 210 | return FDE_CSSSyntaxStatus::PropertyValue; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 211 | } |
| 212 | break; |
| 213 | } |
| 214 | default: |
| 215 | AppendChar(wch); |
| 216 | break; |
| 217 | } |
| 218 | break; |
| 219 | case FDE_CSSSYNTAXMODE_Comment: |
| 220 | if (wch == '/' && m_TextData.GetLength() > 0 && |
| 221 | m_TextData.GetAt(m_TextData.GetLength() - 1) == '*') { |
| 222 | RestoreMode(); |
| 223 | } else { |
| 224 | m_TextData.AppendChar(wch); |
| 225 | } |
| 226 | m_TextPlane.MoveNext(); |
| 227 | break; |
| 228 | case FDE_CSSSYNTAXMODE_MediaType: |
| 229 | switch (wch) { |
| 230 | case ',': |
| 231 | m_TextPlane.MoveNext(); |
| 232 | SwitchMode(FDE_CSSSYNTAXMODE_MediaType); |
| 233 | if (m_iTextDatLen > 0) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 234 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | case '{': { |
| 238 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 239 | if (!pMode || *pMode != FDE_CSSSYNTAXMODE_MediaRule) { |
| 240 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 241 | return m_eStatus; |
| 242 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 243 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 244 | if (m_TextData.GetLength() > 0) { |
| 245 | SaveTextData(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 246 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 247 | } else { |
| 248 | m_TextPlane.MoveNext(); |
| 249 | *pMode = FDE_CSSSYNTAXMODE_RuleSet; |
| 250 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 251 | return FDE_CSSSyntaxStatus::DeclOpen; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 252 | } |
| 253 | } break; |
| 254 | case ';': { |
| 255 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 256 | if (!pMode || *pMode != FDE_CSSSYNTAXMODE_Import) { |
| 257 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 258 | return m_eStatus; |
| 259 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 260 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 261 | if (m_TextData.GetLength() > 0) { |
| 262 | SaveTextData(); |
| 263 | if (IsImportEnabled()) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 264 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 265 | } |
| 266 | } else { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 267 | bool bEnabled = IsImportEnabled(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 268 | m_TextPlane.MoveNext(); |
| 269 | m_ModeStack.Pop(); |
| 270 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
| 271 | if (bEnabled) { |
| 272 | DisableImport(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 273 | return FDE_CSSSyntaxStatus::ImportClose; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } break; |
| 277 | case '/': |
| 278 | if (m_TextPlane.GetNextChar() == '*') { |
| 279 | if (SwitchToComment() > 0) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 280 | return FDE_CSSSyntaxStatus::MediaType; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 281 | } |
| 282 | break; |
| 283 | } |
| 284 | default: |
| 285 | AppendChar(wch); |
| 286 | break; |
| 287 | } |
| 288 | break; |
| 289 | case FDE_CSSSYNTAXMODE_URI: { |
| 290 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 291 | if (!pMode || *pMode != FDE_CSSSYNTAXMODE_Import) { |
| 292 | m_eStatus = FDE_CSSSyntaxStatus::Error; |
| 293 | return m_eStatus; |
| 294 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 295 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 296 | if (wch <= ' ' || wch == ';') { |
| 297 | int32_t iURIStart, iURILength = m_TextData.GetLength(); |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 298 | if (iURILength > 0 && FDE_ParseCSSURI(m_TextData.GetBuffer(), |
| 299 | &iURIStart, &iURILength)) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 300 | m_TextData.Subtract(iURIStart, iURILength); |
| 301 | SwitchMode(FDE_CSSSYNTAXMODE_MediaType); |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 302 | if (IsImportEnabled()) |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 303 | return FDE_CSSSyntaxStatus::URI; |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 304 | break; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | AppendChar(wch); |
| 308 | } break; |
| 309 | case FDE_CSSSYNTAXMODE_AtRule: |
| 310 | if (wch > ' ') { |
| 311 | AppendChar(wch); |
| 312 | } else { |
| 313 | int32_t iLen = m_TextData.GetLength(); |
| 314 | const FX_WCHAR* psz = m_TextData.GetBuffer(); |
| 315 | if (FXSYS_wcsncmp(L"charset", psz, iLen) == 0) { |
| 316 | SwitchMode(FDE_CSSSYNTAXMODE_Charset); |
| 317 | } else if (FXSYS_wcsncmp(L"import", psz, iLen) == 0) { |
| 318 | m_ModeStack.Push(FDE_CSSSYNTAXMODE_Import); |
| 319 | SwitchMode(FDE_CSSSYNTAXMODE_URI); |
| 320 | if (IsImportEnabled()) { |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 321 | return FDE_CSSSyntaxStatus::ImportRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 322 | } else { |
| 323 | break; |
| 324 | } |
| 325 | } else if (FXSYS_wcsncmp(L"media", psz, iLen) == 0) { |
| 326 | m_ModeStack.Push(FDE_CSSSYNTAXMODE_MediaRule); |
| 327 | SwitchMode(FDE_CSSSYNTAXMODE_MediaType); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 328 | return FDE_CSSSyntaxStatus::MediaRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 329 | } else if (FXSYS_wcsncmp(L"font-face", psz, iLen) == 0) { |
| 330 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 331 | return FDE_CSSSyntaxStatus::FontFaceRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 332 | } else if (FXSYS_wcsncmp(L"page", psz, iLen) == 0) { |
| 333 | SwitchMode(FDE_CSSSYNTAXMODE_Selector); |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 334 | return FDE_CSSSyntaxStatus::PageRule; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 335 | } else { |
| 336 | SwitchMode(FDE_CSSSYNTAXMODE_UnknownRule); |
| 337 | } |
| 338 | } |
| 339 | break; |
| 340 | case FDE_CSSSYNTAXMODE_Charset: |
| 341 | if (wch == ';') { |
| 342 | m_TextPlane.MoveNext(); |
| 343 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
| 344 | if (IsCharsetEnabled()) { |
| 345 | DisableCharset(); |
| 346 | if (m_iTextDatLen > 0) { |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 347 | if (m_pStream) { |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 348 | uint16_t wCodePage = FX_GetCodePageFromStringW( |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 349 | m_TextData.GetBuffer(), m_iTextDatLen); |
| 350 | if (wCodePage < 0xFFFF && |
| 351 | m_pStream->GetCodePage() != wCodePage) { |
| 352 | m_pStream->SetCodePage(wCodePage); |
| 353 | } |
| 354 | } |
Dan Sinclair | 96f482c | 2017-01-11 16:31:27 -0500 | [diff] [blame] | 355 | return FDE_CSSSyntaxStatus::Charset; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } else { |
| 359 | AppendChar(wch); |
| 360 | } |
| 361 | break; |
| 362 | case FDE_CSSSYNTAXMODE_UnknownRule: |
| 363 | if (wch == ';') { |
| 364 | SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
| 365 | } |
| 366 | m_TextPlane.MoveNext(); |
| 367 | break; |
| 368 | default: |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 369 | ASSERT(false); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | return m_eStatus; |
| 375 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 376 | bool CFDE_CSSSyntaxParser::IsImportEnabled() const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 377 | if ((m_dwCheck & FDE_CSSSYNTAXCHECK_AllowImport) == 0) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 378 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 379 | } |
| 380 | if (m_ModeStack.GetSize() > 1) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 381 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 382 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 383 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 384 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 385 | bool CFDE_CSSSyntaxParser::AppendChar(FX_WCHAR wch) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 386 | m_TextPlane.MoveNext(); |
| 387 | if (m_TextData.GetLength() > 0 || wch > ' ') { |
| 388 | m_TextData.AppendChar(wch); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 389 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 390 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 391 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 392 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 393 | int32_t CFDE_CSSSyntaxParser::SaveTextData() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 394 | m_iTextDatLen = m_TextData.TrimEnd(); |
| 395 | m_TextData.Clear(); |
| 396 | return m_iTextDatLen; |
| 397 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 398 | void CFDE_CSSSyntaxParser::SwitchMode(FDE_CSSSYNTAXMODE eMode) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 399 | m_eMode = eMode; |
| 400 | SaveTextData(); |
| 401 | } |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 402 | int32_t CFDE_CSSSyntaxParser::SwitchToComment() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 403 | int32_t iLength = m_TextData.GetLength(); |
| 404 | m_ModeStack.Push(m_eMode); |
| 405 | SwitchMode(FDE_CSSSYNTAXMODE_Comment); |
| 406 | return iLength; |
| 407 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 408 | bool CFDE_CSSSyntaxParser::RestoreMode() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 409 | FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 410 | if (!pMode) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 411 | return false; |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 412 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 413 | SwitchMode(*pMode); |
| 414 | m_ModeStack.Pop(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 415 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 416 | } |
| 417 | const FX_WCHAR* CFDE_CSSSyntaxParser::GetCurrentString(int32_t& iLength) const { |
| 418 | iLength = m_iTextDatLen; |
| 419 | return m_TextData.GetBuffer(); |
| 420 | } |
| 421 | CFDE_CSSTextBuf::CFDE_CSSTextBuf() |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 422 | : m_bExtBuf(false), |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 423 | m_pBuffer(nullptr), |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 424 | m_iBufLen(0), |
| 425 | m_iDatLen(0), |
| 426 | m_iDatPos(0) {} |
| 427 | CFDE_CSSTextBuf::~CFDE_CSSTextBuf() { |
| 428 | Reset(); |
| 429 | } |
| 430 | void CFDE_CSSTextBuf::Reset() { |
| 431 | if (!m_bExtBuf) { |
| 432 | FX_Free(m_pBuffer); |
thestig | cfb77cc | 2016-06-10 12:21:53 -0700 | [diff] [blame] | 433 | m_pBuffer = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 434 | } |
| 435 | m_iDatPos = m_iDatLen = m_iBufLen; |
| 436 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 437 | bool CFDE_CSSTextBuf::AttachBuffer(const FX_WCHAR* pBuffer, int32_t iBufLen) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 438 | Reset(); |
brucedawson | 2f109ab | 2016-05-27 16:13:13 -0700 | [diff] [blame] | 439 | m_pBuffer = const_cast<FX_WCHAR*>(pBuffer); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 440 | m_iDatLen = m_iBufLen = iBufLen; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 441 | return m_bExtBuf = true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 442 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 443 | bool CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 444 | ASSERT(iAllocSize > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 445 | Clear(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 446 | m_bExtBuf = false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 447 | return ExpandBuf(iAllocSize); |
| 448 | } |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 449 | |
| 450 | int32_t CFDE_CSSTextBuf::LoadFromStream( |
| 451 | const CFX_RetainPtr<IFGAS_Stream>& pTxtStream, |
| 452 | int32_t iStreamOffset, |
| 453 | int32_t iMaxChars, |
| 454 | bool& bEOS) { |
dsinclair | 43854a5 | 2016-04-27 12:26:00 -0700 | [diff] [blame] | 455 | ASSERT(iStreamOffset >= 0 && iMaxChars > 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 456 | Clear(); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 457 | m_bExtBuf = false; |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 458 | if (!ExpandBuf(iMaxChars)) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 459 | return 0; |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 460 | |
| 461 | if (pTxtStream->GetPosition() != iStreamOffset) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 462 | pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 463 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 464 | m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, bEOS); |
| 465 | return m_iDatLen; |
| 466 | } |
tsepez | 7cda31a | 2016-12-07 12:10:20 -0800 | [diff] [blame] | 467 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 468 | bool CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 469 | if (m_bExtBuf) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 470 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 471 | } |
| 472 | if (!m_pBuffer) { |
| 473 | m_pBuffer = FX_Alloc(FX_WCHAR, iDesiredSize); |
| 474 | } else if (m_iBufLen != iDesiredSize) { |
| 475 | m_pBuffer = FX_Realloc(FX_WCHAR, m_pBuffer, iDesiredSize); |
| 476 | } else { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 477 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 478 | } |
| 479 | if (!m_pBuffer) { |
| 480 | m_iBufLen = 0; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 481 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 482 | } |
| 483 | m_iBufLen = iDesiredSize; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 484 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 485 | } |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 486 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 487 | void CFDE_CSSTextBuf::Subtract(int32_t iStart, int32_t iLength) { |
npm | 2399152 | 2016-11-28 12:49:29 -0800 | [diff] [blame] | 488 | ASSERT(iStart >= 0 && iLength >= 0); |
| 489 | iLength = std::max(std::min(iLength, m_iDatLen - iStart), 0); |
| 490 | FXSYS_memmove(m_pBuffer, m_pBuffer + iStart, iLength * sizeof(FX_WCHAR)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 491 | m_iDatLen = iLength; |
| 492 | } |