OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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/fxfa/parser/xfa_parser_imp.h" | 7 #include "xfa/fxfa/parser/cxfa_simple_parser.h" |
8 | 8 |
9 #include <memory> | |
10 | |
11 #include "xfa/fde/xml/fde_xml_imp.h" | |
12 #include "xfa/fgas/crt/fgas_codepage.h" | 9 #include "xfa/fgas/crt/fgas_codepage.h" |
| 10 #include "xfa/fxfa/include/fxfa.h" |
13 #include "xfa/fxfa/include/xfa_checksum.h" | 11 #include "xfa/fxfa/include/xfa_checksum.h" |
14 #include "xfa/fxfa/parser/xfa_basic_imp.h" | 12 #include "xfa/fxfa/parser/cxfa_xml_parser.h" |
15 #include "xfa/fxfa/parser/xfa_doclayout.h" | |
16 #include "xfa/fxfa/parser/xfa_document.h" | 13 #include "xfa/fxfa/parser/xfa_document.h" |
17 #include "xfa/fxfa/parser/xfa_localemgr.h" | |
18 #include "xfa/fxfa/parser/xfa_object.h" | |
19 #include "xfa/fxfa/parser/xfa_script.h" | |
20 #include "xfa/fxfa/parser/xfa_utils.h" | |
21 | 14 |
22 CXFA_SimpleParser::CXFA_SimpleParser(CXFA_Document* pFactory, | 15 CXFA_SimpleParser::CXFA_SimpleParser(CXFA_Document* pFactory, |
23 bool bDocumentParser) | 16 bool bDocumentParser) |
24 : m_pXMLParser(nullptr), | 17 : m_pXMLParser(nullptr), |
25 m_pXMLDoc(nullptr), | 18 m_pXMLDoc(nullptr), |
26 m_pStream(nullptr), | 19 m_pStream(nullptr), |
27 m_pFileRead(nullptr), | 20 m_pFileRead(nullptr), |
28 m_pFactory(pFactory), | 21 m_pFactory(pFactory), |
29 m_pRootNode(nullptr), | 22 m_pRootNode(nullptr), |
30 m_ePacketID(XFA_XDPPACKET_UNKNOWN), | 23 m_ePacketID(XFA_XDPPACKET_UNKNOWN), |
(...skipping 1285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1316 void CXFA_SimpleParser::CloseParser() { | 1309 void CXFA_SimpleParser::CloseParser() { |
1317 if (m_pXMLDoc) { | 1310 if (m_pXMLDoc) { |
1318 m_pXMLDoc->Release(); | 1311 m_pXMLDoc->Release(); |
1319 m_pXMLDoc = nullptr; | 1312 m_pXMLDoc = nullptr; |
1320 } | 1313 } |
1321 if (m_pStream) { | 1314 if (m_pStream) { |
1322 m_pStream->Release(); | 1315 m_pStream->Release(); |
1323 m_pStream = nullptr; | 1316 m_pStream = nullptr; |
1324 } | 1317 } |
1325 } | 1318 } |
1326 | |
1327 CXFA_DocumentParser::CXFA_DocumentParser(CXFA_FFNotify* pNotify) | |
1328 : m_nodeParser(nullptr, TRUE), m_pNotify(pNotify), m_pDocument(nullptr) {} | |
1329 | |
1330 CXFA_DocumentParser::~CXFA_DocumentParser() { | |
1331 CloseParser(); | |
1332 } | |
1333 | |
1334 int32_t CXFA_DocumentParser::StartParse(IFX_FileRead* pStream, | |
1335 XFA_XDPPACKET ePacketID) { | |
1336 CloseParser(); | |
1337 int32_t nRetStatus = m_nodeParser.StartParse(pStream, ePacketID); | |
1338 if (nRetStatus == XFA_PARSESTATUS_Ready) { | |
1339 m_pDocument.reset(new CXFA_Document(this)); | |
1340 m_nodeParser.SetFactory(m_pDocument.get()); | |
1341 } | |
1342 return nRetStatus; | |
1343 } | |
1344 | |
1345 int32_t CXFA_DocumentParser::DoParse(IFX_Pause* pPause) { | |
1346 int32_t nRetStatus = m_nodeParser.DoParse(pPause); | |
1347 if (nRetStatus >= XFA_PARSESTATUS_Done) { | |
1348 ASSERT(m_pDocument); | |
1349 m_pDocument->SetRoot(m_nodeParser.GetRootNode()); | |
1350 } | |
1351 return nRetStatus; | |
1352 } | |
1353 | |
1354 CFDE_XMLDoc* CXFA_DocumentParser::GetXMLDoc() const { | |
1355 return m_nodeParser.GetXMLDoc(); | |
1356 } | |
1357 | |
1358 CXFA_FFNotify* CXFA_DocumentParser::GetNotify() const { | |
1359 return m_pNotify; | |
1360 } | |
1361 | |
1362 CXFA_Document* CXFA_DocumentParser::GetDocument() const { | |
1363 return m_pDocument.get(); | |
1364 } | |
1365 | |
1366 void CXFA_DocumentParser::CloseParser() { | |
1367 m_pDocument.reset(); | |
1368 m_nodeParser.CloseParser(); | |
1369 } | |
1370 | |
1371 CXFA_XMLParser::CXFA_XMLParser(CFDE_XMLNode* pRoot, IFX_Stream* pStream) | |
1372 : m_nElementStart(0), | |
1373 m_dwCheckStatus(0), | |
1374 m_dwCurrentCheckStatus(0), | |
1375 m_pRoot(pRoot), | |
1376 m_pStream(pStream), | |
1377 m_pParser(nullptr), | |
1378 m_pParent(pRoot), | |
1379 m_pChild(nullptr), | |
1380 m_NodeStack(16), | |
1381 m_syntaxParserResult(FDE_XmlSyntaxResult::None) { | |
1382 ASSERT(m_pParent && m_pStream); | |
1383 m_NodeStack.Push(m_pParent); | |
1384 m_pParser = new CFDE_XMLSyntaxParser; | |
1385 m_pParser->Init(m_pStream, 32 * 1024, 1024 * 1024); | |
1386 } | |
1387 | |
1388 CXFA_XMLParser::~CXFA_XMLParser() { | |
1389 if (m_pParser) { | |
1390 m_pParser->Release(); | |
1391 } | |
1392 m_NodeStack.RemoveAll(); | |
1393 m_ws1.clear(); | |
1394 m_ws2.clear(); | |
1395 } | |
1396 | |
1397 void CXFA_XMLParser::Release() { | |
1398 delete this; | |
1399 } | |
1400 | |
1401 int32_t CXFA_XMLParser::DoParser(IFX_Pause* pPause) { | |
1402 if (m_syntaxParserResult == FDE_XmlSyntaxResult::Error) | |
1403 return -1; | |
1404 if (m_syntaxParserResult == FDE_XmlSyntaxResult::EndOfString) | |
1405 return 100; | |
1406 | |
1407 int32_t iCount = 0; | |
1408 while (TRUE) { | |
1409 m_syntaxParserResult = m_pParser->DoSyntaxParse(); | |
1410 switch (m_syntaxParserResult) { | |
1411 case FDE_XmlSyntaxResult::InstructionOpen: | |
1412 break; | |
1413 case FDE_XmlSyntaxResult::InstructionClose: | |
1414 if (m_pChild) { | |
1415 if (m_pChild->GetType() != FDE_XMLNODE_Instruction) { | |
1416 m_syntaxParserResult = FDE_XmlSyntaxResult::Error; | |
1417 break; | |
1418 } | |
1419 } | |
1420 m_pChild = m_pParent; | |
1421 break; | |
1422 case FDE_XmlSyntaxResult::ElementOpen: | |
1423 if (m_dwCheckStatus != 0x03 && m_NodeStack.GetSize() == 2) { | |
1424 m_nElementStart = m_pParser->GetCurrentPos() - 1; | |
1425 } | |
1426 break; | |
1427 case FDE_XmlSyntaxResult::ElementBreak: | |
1428 break; | |
1429 case FDE_XmlSyntaxResult::ElementClose: | |
1430 if (m_pChild->GetType() != FDE_XMLNODE_Element) { | |
1431 m_syntaxParserResult = FDE_XmlSyntaxResult::Error; | |
1432 break; | |
1433 } | |
1434 m_pParser->GetTagName(m_ws1); | |
1435 static_cast<CFDE_XMLElement*>(m_pChild)->GetTagName(m_ws2); | |
1436 if (m_ws1.GetLength() > 0 && m_ws1 != m_ws2) { | |
1437 m_syntaxParserResult = FDE_XmlSyntaxResult::Error; | |
1438 break; | |
1439 } | |
1440 m_NodeStack.Pop(); | |
1441 if (m_NodeStack.GetSize() < 1) { | |
1442 m_syntaxParserResult = FDE_XmlSyntaxResult::Error; | |
1443 break; | |
1444 } else if (m_dwCurrentCheckStatus != 0 && m_NodeStack.GetSize() == 2) { | |
1445 m_nSize[m_dwCurrentCheckStatus - 1] = | |
1446 m_pParser->GetCurrentBinaryPos() - | |
1447 m_nStart[m_dwCurrentCheckStatus - 1]; | |
1448 m_dwCurrentCheckStatus = 0; | |
1449 } | |
1450 | |
1451 m_pParent = static_cast<CFDE_XMLNode*>(*m_NodeStack.GetTopElement()); | |
1452 m_pChild = m_pParent; | |
1453 iCount++; | |
1454 break; | |
1455 case FDE_XmlSyntaxResult::TargetName: | |
1456 m_pParser->GetTargetName(m_ws1); | |
1457 if (m_ws1 == FX_WSTRC(L"originalXFAVersion") || | |
1458 m_ws1 == FX_WSTRC(L"acrobat")) { | |
1459 m_pChild = new CFDE_XMLInstruction(m_ws1); | |
1460 m_pParent->InsertChildNode(m_pChild); | |
1461 } else { | |
1462 m_pChild = nullptr; | |
1463 } | |
1464 m_ws1.clear(); | |
1465 break; | |
1466 case FDE_XmlSyntaxResult::TagName: | |
1467 m_pParser->GetTagName(m_ws1); | |
1468 m_pChild = new CFDE_XMLElement(m_ws1); | |
1469 m_pParent->InsertChildNode(m_pChild); | |
1470 m_NodeStack.Push(m_pChild); | |
1471 m_pParent = m_pChild; | |
1472 | |
1473 if (m_dwCheckStatus != 0x03 && m_NodeStack.GetSize() == 3) { | |
1474 CFX_WideString wsTag; | |
1475 static_cast<CFDE_XMLElement*>(m_pChild)->GetLocalTagName(wsTag); | |
1476 if (wsTag == FX_WSTRC(L"template")) { | |
1477 m_dwCheckStatus |= 0x01; | |
1478 m_dwCurrentCheckStatus = 0x01; | |
1479 m_nStart[0] = m_pParser->GetCurrentBinaryPos() - | |
1480 (m_pParser->GetCurrentPos() - m_nElementStart); | |
1481 } else if (wsTag == FX_WSTRC(L"datasets")) { | |
1482 m_dwCheckStatus |= 0x02; | |
1483 m_dwCurrentCheckStatus = 0x02; | |
1484 m_nStart[1] = m_pParser->GetCurrentBinaryPos() - | |
1485 (m_pParser->GetCurrentPos() - m_nElementStart); | |
1486 } | |
1487 } | |
1488 break; | |
1489 case FDE_XmlSyntaxResult::AttriName: | |
1490 m_pParser->GetAttributeName(m_ws1); | |
1491 break; | |
1492 case FDE_XmlSyntaxResult::AttriValue: | |
1493 if (m_pChild) { | |
1494 m_pParser->GetAttributeName(m_ws2); | |
1495 if (m_pChild->GetType() == FDE_XMLNODE_Element) { | |
1496 static_cast<CFDE_XMLElement*>(m_pChild)->SetString(m_ws1, m_ws2); | |
1497 } | |
1498 } | |
1499 m_ws1.clear(); | |
1500 break; | |
1501 case FDE_XmlSyntaxResult::Text: | |
1502 m_pParser->GetTextData(m_ws1); | |
1503 m_pChild = new CFDE_XMLText(m_ws1); | |
1504 m_pParent->InsertChildNode(m_pChild); | |
1505 m_pChild = m_pParent; | |
1506 break; | |
1507 case FDE_XmlSyntaxResult::CData: | |
1508 m_pParser->GetTextData(m_ws1); | |
1509 m_pChild = new CFDE_XMLCharData(m_ws1); | |
1510 m_pParent->InsertChildNode(m_pChild); | |
1511 m_pChild = m_pParent; | |
1512 break; | |
1513 case FDE_XmlSyntaxResult::TargetData: | |
1514 if (m_pChild) { | |
1515 if (m_pChild->GetType() != FDE_XMLNODE_Instruction) { | |
1516 m_syntaxParserResult = FDE_XmlSyntaxResult::Error; | |
1517 break; | |
1518 } | |
1519 if (!m_ws1.IsEmpty()) { | |
1520 static_cast<CFDE_XMLInstruction*>(m_pChild)->AppendData(m_ws1); | |
1521 } | |
1522 m_pParser->GetTargetData(m_ws1); | |
1523 static_cast<CFDE_XMLInstruction*>(m_pChild)->AppendData(m_ws1); | |
1524 } | |
1525 m_ws1.clear(); | |
1526 break; | |
1527 default: | |
1528 break; | |
1529 } | |
1530 if (m_syntaxParserResult == FDE_XmlSyntaxResult::Error || | |
1531 m_syntaxParserResult == FDE_XmlSyntaxResult::EndOfString) { | |
1532 break; | |
1533 } | |
1534 if (pPause && iCount > 500 && pPause->NeedToPauseNow()) { | |
1535 break; | |
1536 } | |
1537 } | |
1538 return (m_syntaxParserResult == FDE_XmlSyntaxResult::Error || | |
1539 m_NodeStack.GetSize() != 1) | |
1540 ? -1 | |
1541 : m_pParser->GetStatus(); | |
1542 } | |
OLD | NEW |