OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "xfa/fde/css/fde_csssyntax.h" | 7 #include "xfa/fde/css/fde_csssyntax.h" |
8 | 8 |
9 #include "xfa/fde/css/fde_cssdatatable.h" | 9 #include "xfa/fde/css/fde_cssdatatable.h" |
10 #include "xfa/fgas/crt/fgas_codepage.h" | 10 #include "xfa/fgas/crt/fgas_codepage.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 m_dwCheck((uint32_t)-1), | 26 m_dwCheck((uint32_t)-1), |
27 m_eMode(FDE_CSSSYNTAXMODE_RuleSet), | 27 m_eMode(FDE_CSSSYNTAXMODE_RuleSet), |
28 m_eStatus(FDE_CSSSYNTAXSTATUS_None), | 28 m_eStatus(FDE_CSSSYNTAXSTATUS_None), |
29 m_ModeStack(100) {} | 29 m_ModeStack(100) {} |
30 | 30 |
31 CFDE_CSSSyntaxParser::~CFDE_CSSSyntaxParser() { | 31 CFDE_CSSSyntaxParser::~CFDE_CSSSyntaxParser() { |
32 m_TextData.Reset(); | 32 m_TextData.Reset(); |
33 m_TextPlane.Reset(); | 33 m_TextPlane.Reset(); |
34 } | 34 } |
35 | 35 |
36 FX_BOOL CFDE_CSSSyntaxParser::Init(IFX_Stream* pStream, | 36 bool CFDE_CSSSyntaxParser::Init(IFX_Stream* pStream, |
37 int32_t iCSSPlaneSize, | 37 int32_t iCSSPlaneSize, |
38 int32_t iTextDataSize, | 38 int32_t iTextDataSize, |
39 FX_BOOL bOnlyDeclaration) { | 39 bool bOnlyDeclaration) { |
40 ASSERT(pStream && iCSSPlaneSize > 0 && iTextDataSize > 0); | 40 ASSERT(pStream && iCSSPlaneSize > 0 && iTextDataSize > 0); |
41 Reset(bOnlyDeclaration); | 41 Reset(bOnlyDeclaration); |
42 if (!m_TextData.EstimateSize(iTextDataSize)) { | 42 if (!m_TextData.EstimateSize(iTextDataSize)) { |
43 return FALSE; | 43 return false; |
44 } | 44 } |
45 uint8_t bom[4]; | 45 uint8_t bom[4]; |
46 m_pStream = pStream; | 46 m_pStream = pStream; |
47 m_iStreamPos = m_pStream->GetBOM(bom); | 47 m_iStreamPos = m_pStream->GetBOM(bom); |
48 m_iPlaneSize = iCSSPlaneSize; | 48 m_iPlaneSize = iCSSPlaneSize; |
49 return TRUE; | 49 return true; |
50 } | 50 } |
51 FX_BOOL CFDE_CSSSyntaxParser::Init(const FX_WCHAR* pBuffer, | 51 bool CFDE_CSSSyntaxParser::Init(const FX_WCHAR* pBuffer, |
52 int32_t iBufferSize, | 52 int32_t iBufferSize, |
53 int32_t iTextDatSize, | 53 int32_t iTextDatSize, |
54 FX_BOOL bOnlyDeclaration) { | 54 bool bOnlyDeclaration) { |
55 ASSERT(pBuffer && iBufferSize > 0 && iTextDatSize > 0); | 55 ASSERT(pBuffer && iBufferSize > 0 && iTextDatSize > 0); |
56 Reset(bOnlyDeclaration); | 56 Reset(bOnlyDeclaration); |
57 if (!m_TextData.EstimateSize(iTextDatSize)) { | 57 if (!m_TextData.EstimateSize(iTextDatSize)) { |
58 return FALSE; | 58 return false; |
59 } | 59 } |
60 return m_TextPlane.AttachBuffer(pBuffer, iBufferSize); | 60 return m_TextPlane.AttachBuffer(pBuffer, iBufferSize); |
61 } | 61 } |
62 void CFDE_CSSSyntaxParser::Reset(FX_BOOL bOnlyDeclaration) { | 62 void CFDE_CSSSyntaxParser::Reset(bool bOnlyDeclaration) { |
63 m_TextPlane.Reset(); | 63 m_TextPlane.Reset(); |
64 m_TextData.Reset(); | 64 m_TextData.Reset(); |
65 m_pStream = nullptr; | 65 m_pStream = nullptr; |
66 m_iStreamPos = 0; | 66 m_iStreamPos = 0; |
67 m_iTextDatLen = 0; | 67 m_iTextDatLen = 0; |
68 m_dwCheck = (uint32_t)-1; | 68 m_dwCheck = (uint32_t)-1; |
69 m_eStatus = FDE_CSSSYNTAXSTATUS_None; | 69 m_eStatus = FDE_CSSSYNTAXSTATUS_None; |
70 m_eMode = bOnlyDeclaration ? FDE_CSSSYNTAXMODE_PropertyName | 70 m_eMode = bOnlyDeclaration ? FDE_CSSSYNTAXMODE_PropertyName |
71 : FDE_CSSSYNTAXMODE_RuleSet; | 71 : FDE_CSSSYNTAXMODE_RuleSet; |
72 } | 72 } |
73 FDE_CSSSYNTAXSTATUS CFDE_CSSSyntaxParser::DoSyntaxParse() { | 73 FDE_CSSSYNTAXSTATUS CFDE_CSSSyntaxParser::DoSyntaxParse() { |
74 while (m_eStatus >= FDE_CSSSYNTAXSTATUS_None) { | 74 while (m_eStatus >= FDE_CSSSYNTAXSTATUS_None) { |
75 if (m_TextPlane.IsEOF()) { | 75 if (m_TextPlane.IsEOF()) { |
76 if (!m_pStream) { | 76 if (!m_pStream) { |
77 if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && | 77 if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && |
78 m_TextData.GetLength() > 0) { | 78 m_TextData.GetLength() > 0) { |
79 SaveTextData(); | 79 SaveTextData(); |
80 return m_eStatus = FDE_CSSSYNTAXSTATUS_PropertyValue; | 80 return m_eStatus = FDE_CSSSYNTAXSTATUS_PropertyValue; |
81 } | 81 } |
82 return m_eStatus = FDE_CSSSYNTAXSTATUS_EOS; | 82 return m_eStatus = FDE_CSSSYNTAXSTATUS_EOS; |
83 } | 83 } |
84 FX_BOOL bEOS; | 84 bool bEOS; |
85 int32_t iLen = m_TextPlane.LoadFromStream(m_pStream, m_iStreamPos, | 85 int32_t iLen = m_TextPlane.LoadFromStream(m_pStream, m_iStreamPos, |
86 m_iPlaneSize, bEOS); | 86 m_iPlaneSize, bEOS); |
87 m_iStreamPos = m_pStream->GetPosition(); | 87 m_iStreamPos = m_pStream->GetPosition(); |
88 if (iLen < 1) { | 88 if (iLen < 1) { |
89 if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && | 89 if (m_eMode == FDE_CSSSYNTAXMODE_PropertyValue && |
90 m_TextData.GetLength() > 0) { | 90 m_TextData.GetLength() > 0) { |
91 SaveTextData(); | 91 SaveTextData(); |
92 return m_eStatus = FDE_CSSSYNTAXSTATUS_PropertyValue; | 92 return m_eStatus = FDE_CSSSYNTAXSTATUS_PropertyValue; |
93 } | 93 } |
94 return m_eStatus = FDE_CSSSYNTAXSTATUS_EOS; | 94 return m_eStatus = FDE_CSSSYNTAXSTATUS_EOS; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); | 244 FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
245 if (!pMode || *pMode != FDE_CSSSYNTAXMODE_Import) | 245 if (!pMode || *pMode != FDE_CSSSYNTAXMODE_Import) |
246 return m_eStatus = FDE_CSSSYNTAXSTATUS_Error; | 246 return m_eStatus = FDE_CSSSYNTAXSTATUS_Error; |
247 | 247 |
248 if (m_TextData.GetLength() > 0) { | 248 if (m_TextData.GetLength() > 0) { |
249 SaveTextData(); | 249 SaveTextData(); |
250 if (IsImportEnabled()) { | 250 if (IsImportEnabled()) { |
251 return FDE_CSSSYNTAXSTATUS_MediaType; | 251 return FDE_CSSSYNTAXSTATUS_MediaType; |
252 } | 252 } |
253 } else { | 253 } else { |
254 FX_BOOL bEnabled = IsImportEnabled(); | 254 bool bEnabled = IsImportEnabled(); |
255 m_TextPlane.MoveNext(); | 255 m_TextPlane.MoveNext(); |
256 m_ModeStack.Pop(); | 256 m_ModeStack.Pop(); |
257 SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); | 257 SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
258 if (bEnabled) { | 258 if (bEnabled) { |
259 DisableImport(); | 259 DisableImport(); |
260 return FDE_CSSSYNTAXSTATUS_ImportClose; | 260 return FDE_CSSSYNTAXSTATUS_ImportClose; |
261 } | 261 } |
262 } | 262 } |
263 } break; | 263 } break; |
264 case '/': | 264 case '/': |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 AppendChar(wch); | 347 AppendChar(wch); |
348 } | 348 } |
349 break; | 349 break; |
350 case FDE_CSSSYNTAXMODE_UnknownRule: | 350 case FDE_CSSSYNTAXMODE_UnknownRule: |
351 if (wch == ';') { | 351 if (wch == ';') { |
352 SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); | 352 SwitchMode(FDE_CSSSYNTAXMODE_RuleSet); |
353 } | 353 } |
354 m_TextPlane.MoveNext(); | 354 m_TextPlane.MoveNext(); |
355 break; | 355 break; |
356 default: | 356 default: |
357 ASSERT(FALSE); | 357 ASSERT(false); |
358 break; | 358 break; |
359 } | 359 } |
360 } | 360 } |
361 } | 361 } |
362 return m_eStatus; | 362 return m_eStatus; |
363 } | 363 } |
364 FX_BOOL CFDE_CSSSyntaxParser::IsImportEnabled() const { | 364 bool CFDE_CSSSyntaxParser::IsImportEnabled() const { |
365 if ((m_dwCheck & FDE_CSSSYNTAXCHECK_AllowImport) == 0) { | 365 if ((m_dwCheck & FDE_CSSSYNTAXCHECK_AllowImport) == 0) { |
366 return FALSE; | 366 return false; |
367 } | 367 } |
368 if (m_ModeStack.GetSize() > 1) { | 368 if (m_ModeStack.GetSize() > 1) { |
369 return FALSE; | 369 return false; |
370 } | 370 } |
371 return TRUE; | 371 return true; |
372 } | 372 } |
373 FX_BOOL CFDE_CSSSyntaxParser::AppendChar(FX_WCHAR wch) { | 373 bool CFDE_CSSSyntaxParser::AppendChar(FX_WCHAR wch) { |
374 m_TextPlane.MoveNext(); | 374 m_TextPlane.MoveNext(); |
375 if (m_TextData.GetLength() > 0 || wch > ' ') { | 375 if (m_TextData.GetLength() > 0 || wch > ' ') { |
376 m_TextData.AppendChar(wch); | 376 m_TextData.AppendChar(wch); |
377 return TRUE; | 377 return true; |
378 } | 378 } |
379 return FALSE; | 379 return false; |
380 } | 380 } |
381 int32_t CFDE_CSSSyntaxParser::SaveTextData() { | 381 int32_t CFDE_CSSSyntaxParser::SaveTextData() { |
382 m_iTextDatLen = m_TextData.TrimEnd(); | 382 m_iTextDatLen = m_TextData.TrimEnd(); |
383 m_TextData.Clear(); | 383 m_TextData.Clear(); |
384 return m_iTextDatLen; | 384 return m_iTextDatLen; |
385 } | 385 } |
386 void CFDE_CSSSyntaxParser::SwitchMode(FDE_CSSSYNTAXMODE eMode) { | 386 void CFDE_CSSSyntaxParser::SwitchMode(FDE_CSSSYNTAXMODE eMode) { |
387 m_eMode = eMode; | 387 m_eMode = eMode; |
388 SaveTextData(); | 388 SaveTextData(); |
389 } | 389 } |
390 int32_t CFDE_CSSSyntaxParser::SwitchToComment() { | 390 int32_t CFDE_CSSSyntaxParser::SwitchToComment() { |
391 int32_t iLength = m_TextData.GetLength(); | 391 int32_t iLength = m_TextData.GetLength(); |
392 m_ModeStack.Push(m_eMode); | 392 m_ModeStack.Push(m_eMode); |
393 SwitchMode(FDE_CSSSYNTAXMODE_Comment); | 393 SwitchMode(FDE_CSSSYNTAXMODE_Comment); |
394 return iLength; | 394 return iLength; |
395 } | 395 } |
396 FX_BOOL CFDE_CSSSyntaxParser::RestoreMode() { | 396 bool CFDE_CSSSyntaxParser::RestoreMode() { |
397 FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); | 397 FDE_CSSSYNTAXMODE* pMode = m_ModeStack.GetTopElement(); |
398 if (!pMode) | 398 if (!pMode) |
399 return FALSE; | 399 return false; |
400 | 400 |
401 SwitchMode(*pMode); | 401 SwitchMode(*pMode); |
402 m_ModeStack.Pop(); | 402 m_ModeStack.Pop(); |
403 return TRUE; | 403 return true; |
404 } | 404 } |
405 const FX_WCHAR* CFDE_CSSSyntaxParser::GetCurrentString(int32_t& iLength) const { | 405 const FX_WCHAR* CFDE_CSSSyntaxParser::GetCurrentString(int32_t& iLength) const { |
406 iLength = m_iTextDatLen; | 406 iLength = m_iTextDatLen; |
407 return m_TextData.GetBuffer(); | 407 return m_TextData.GetBuffer(); |
408 } | 408 } |
409 CFDE_CSSTextBuf::CFDE_CSSTextBuf() | 409 CFDE_CSSTextBuf::CFDE_CSSTextBuf() |
410 : m_bExtBuf(FALSE), | 410 : m_bExtBuf(false), |
411 m_pBuffer(nullptr), | 411 m_pBuffer(nullptr), |
412 m_iBufLen(0), | 412 m_iBufLen(0), |
413 m_iDatLen(0), | 413 m_iDatLen(0), |
414 m_iDatPos(0) {} | 414 m_iDatPos(0) {} |
415 CFDE_CSSTextBuf::~CFDE_CSSTextBuf() { | 415 CFDE_CSSTextBuf::~CFDE_CSSTextBuf() { |
416 Reset(); | 416 Reset(); |
417 } | 417 } |
418 void CFDE_CSSTextBuf::Reset() { | 418 void CFDE_CSSTextBuf::Reset() { |
419 if (!m_bExtBuf) { | 419 if (!m_bExtBuf) { |
420 FX_Free(m_pBuffer); | 420 FX_Free(m_pBuffer); |
421 m_pBuffer = nullptr; | 421 m_pBuffer = nullptr; |
422 } | 422 } |
423 m_iDatPos = m_iDatLen = m_iBufLen; | 423 m_iDatPos = m_iDatLen = m_iBufLen; |
424 } | 424 } |
425 FX_BOOL CFDE_CSSTextBuf::AttachBuffer(const FX_WCHAR* pBuffer, | 425 bool CFDE_CSSTextBuf::AttachBuffer(const FX_WCHAR* pBuffer, int32_t iBufLen) { |
426 int32_t iBufLen) { | |
427 Reset(); | 426 Reset(); |
428 m_pBuffer = const_cast<FX_WCHAR*>(pBuffer); | 427 m_pBuffer = const_cast<FX_WCHAR*>(pBuffer); |
429 m_iDatLen = m_iBufLen = iBufLen; | 428 m_iDatLen = m_iBufLen = iBufLen; |
430 return m_bExtBuf = TRUE; | 429 return m_bExtBuf = true; |
431 } | 430 } |
432 FX_BOOL CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { | 431 bool CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { |
433 ASSERT(iAllocSize > 0); | 432 ASSERT(iAllocSize > 0); |
434 Clear(); | 433 Clear(); |
435 m_bExtBuf = FALSE; | 434 m_bExtBuf = false; |
436 return ExpandBuf(iAllocSize); | 435 return ExpandBuf(iAllocSize); |
437 } | 436 } |
438 int32_t CFDE_CSSTextBuf::LoadFromStream(IFX_Stream* pTxtStream, | 437 int32_t CFDE_CSSTextBuf::LoadFromStream(IFX_Stream* pTxtStream, |
439 int32_t iStreamOffset, | 438 int32_t iStreamOffset, |
440 int32_t iMaxChars, | 439 int32_t iMaxChars, |
441 FX_BOOL& bEOS) { | 440 bool& bEOS) { |
442 ASSERT(iStreamOffset >= 0 && iMaxChars > 0); | 441 ASSERT(iStreamOffset >= 0 && iMaxChars > 0); |
443 Clear(); | 442 Clear(); |
444 m_bExtBuf = FALSE; | 443 m_bExtBuf = false; |
445 if (!ExpandBuf(iMaxChars)) { | 444 if (!ExpandBuf(iMaxChars)) { |
446 return 0; | 445 return 0; |
447 } | 446 } |
448 if (pTxtStream->GetPosition() != iStreamOffset) { | 447 if (pTxtStream->GetPosition() != iStreamOffset) { |
449 pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); | 448 pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); |
450 } | 449 } |
451 m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, bEOS); | 450 m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, bEOS); |
452 return m_iDatLen; | 451 return m_iDatLen; |
453 } | 452 } |
454 FX_BOOL CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { | 453 bool CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { |
455 if (m_bExtBuf) { | 454 if (m_bExtBuf) { |
456 return FALSE; | 455 return false; |
457 } | 456 } |
458 if (!m_pBuffer) { | 457 if (!m_pBuffer) { |
459 m_pBuffer = FX_Alloc(FX_WCHAR, iDesiredSize); | 458 m_pBuffer = FX_Alloc(FX_WCHAR, iDesiredSize); |
460 } else if (m_iBufLen != iDesiredSize) { | 459 } else if (m_iBufLen != iDesiredSize) { |
461 m_pBuffer = FX_Realloc(FX_WCHAR, m_pBuffer, iDesiredSize); | 460 m_pBuffer = FX_Realloc(FX_WCHAR, m_pBuffer, iDesiredSize); |
462 } else { | 461 } else { |
463 return TRUE; | 462 return true; |
464 } | 463 } |
465 if (!m_pBuffer) { | 464 if (!m_pBuffer) { |
466 m_iBufLen = 0; | 465 m_iBufLen = 0; |
467 return FALSE; | 466 return false; |
468 } | 467 } |
469 m_iBufLen = iDesiredSize; | 468 m_iBufLen = iDesiredSize; |
470 return TRUE; | 469 return true; |
471 } | 470 } |
472 void CFDE_CSSTextBuf::Subtract(int32_t iStart, int32_t iLength) { | 471 void CFDE_CSSTextBuf::Subtract(int32_t iStart, int32_t iLength) { |
473 ASSERT(iStart >= 0 && iLength > 0); | 472 ASSERT(iStart >= 0 && iLength > 0); |
474 if (iLength > m_iDatLen - iStart) { | 473 if (iLength > m_iDatLen - iStart) { |
475 iLength = m_iDatLen - iStart; | 474 iLength = m_iDatLen - iStart; |
476 } | 475 } |
477 if (iLength < 0) { | 476 if (iLength < 0) { |
478 iLength = 0; | 477 iLength = 0; |
479 } else { | 478 } else { |
480 FXSYS_memmove(m_pBuffer, m_pBuffer + iStart, iLength * sizeof(FX_WCHAR)); | 479 FXSYS_memmove(m_pBuffer, m_pBuffer + iStart, iLength * sizeof(FX_WCHAR)); |
481 } | 480 } |
482 m_iDatLen = iLength; | 481 m_iDatLen = iLength; |
483 } | 482 } |
OLD | NEW |