From 1980f10ff2b869f14c409b712eea6744941ebd88 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 30 Nov 2017 22:07:10 +0000 Subject: Simplify XDP parsing code This CL cleans up the XDP packet parsing code. This is followup from a previous CL. Change-Id: Ic4a855e67e1b22d0703e53e9a2a9b4923775b6ad Reviewed-on: https://pdfium-review.googlesource.com/20090 Reviewed-by: Henrique Nakashima Commit-Queue: dsinclair --- xfa/fxfa/parser/cxfa_simple_parser.cpp | 224 ++++++++++++++------------------- xfa/fxfa/parser/cxfa_simple_parser.h | 10 +- 2 files changed, 102 insertions(+), 132 deletions(-) (limited to 'xfa/fxfa/parser') diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp index 18d6006efc..22b02ccdc8 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.cpp +++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp @@ -466,26 +466,25 @@ CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket(CFX_XMLNode* pXMLDocumentNode, case XFA_PacketType::Xdp: return ParseAsXDPPacket_XDP(pXMLDocumentNode); case XFA_PacketType::Config: - return ParseAsXDPPacket_Config(pXMLDocumentNode, XFA_PacketType::Config); + return ParseAsXDPPacket_Config(pXMLDocumentNode); case XFA_PacketType::Template: - return ParseAsXDPPacket_TemplateForm(pXMLDocumentNode, - XFA_PacketType::Template); + return ParseAsXDPPacket_Template(pXMLDocumentNode); case XFA_PacketType::Form: - return ParseAsXDPPacket_TemplateForm(pXMLDocumentNode, - XFA_PacketType::Form); + return ParseAsXDPPacket_Form(pXMLDocumentNode); case XFA_PacketType::Datasets: return ParseAsXDPPacket_Data(pXMLDocumentNode); case XFA_PacketType::Xdc: return ParseAsXDPPacket_Xdc(pXMLDocumentNode); case XFA_PacketType::LocaleSet: return ParseAsXDPPacket_LocaleConnectionSourceSet( - pXMLDocumentNode, XFA_PacketType::LocaleSet); + pXMLDocumentNode, XFA_PacketType::LocaleSet, XFA_Element::LocaleSet); case XFA_PacketType::ConnectionSet: return ParseAsXDPPacket_LocaleConnectionSourceSet( - pXMLDocumentNode, XFA_PacketType::ConnectionSet); + pXMLDocumentNode, XFA_PacketType::ConnectionSet, + XFA_Element::ConnectionSet); case XFA_PacketType::SourceSet: return ParseAsXDPPacket_LocaleConnectionSourceSet( - pXMLDocumentNode, XFA_PacketType::SourceSet); + pXMLDocumentNode, XFA_PacketType::SourceSet, XFA_Element::SourceSet); default: return ParseAsXDPPacket_User(pXMLDocumentNode); } @@ -532,8 +531,7 @@ CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_XDP( return nullptr; pXMLConfigDOMRoot = pChildItem; - pXFAConfigDOMRoot = - ParseAsXDPPacket_Config(pXMLConfigDOMRoot, XFA_PacketType::Config); + pXFAConfigDOMRoot = ParseAsXDPPacket_Config(pXMLConfigDOMRoot); if (pXFAConfigDOMRoot) pXFARootNode->InsertChild(pXFAConfigDOMRoot, nullptr); } @@ -619,8 +617,7 @@ CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_XDP( } CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Config( - CFX_XMLNode* pXMLDocumentNode, - XFA_PacketType ePacketID) { + CFX_XMLNode* pXMLDocumentNode) { const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::Config); if (!MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, packet->flags)) { @@ -632,90 +629,93 @@ CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Config( return nullptr; pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, false); - if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, true)) + if (!NormalLoader(pNode, pXMLDocumentNode, XFA_PacketType::Config, true)) return nullptr; pNode->SetXMLMappingNode(pXMLDocumentNode); return pNode; } -CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_TemplateForm( - CFX_XMLNode* pXMLDocumentNode, - XFA_PacketType ePacketID) { - CXFA_Node* pNode = nullptr; - if (ePacketID == XFA_PacketType::Template) { - const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::Template); - if (MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, - packet->flags)) { - pNode = m_pFactory->CreateNode(XFA_PacketType::Template, - XFA_Element::Template); - if (!pNode) - return nullptr; +CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Template( + CFX_XMLNode* pXMLDocumentNode) { + const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::Template); + if (!MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, + packet->flags)) { + return nullptr; + } - pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, - false); - if (m_bDocumentParser) { - CFX_XMLElement* pXMLDocumentElement = - static_cast(pXMLDocumentNode); - WideString wsNamespaceURI = pXMLDocumentElement->GetNamespaceURI(); - if (wsNamespaceURI.IsEmpty()) - wsNamespaceURI = pXMLDocumentElement->GetString(L"xmlns:xfa"); + CXFA_Node* pNode = + m_pFactory->CreateNode(XFA_PacketType::Template, XFA_Element::Template); + if (!pNode) + return nullptr; - pNode->GetDocument()->RecognizeXFAVersionNumber(wsNamespaceURI); - } - if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, true)) - return nullptr; - } - } else if (ePacketID == XFA_PacketType::Form) { - const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::Form); - if (MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, - packet->flags)) { - CFX_XMLElement* pXMLDocumentElement = - static_cast(pXMLDocumentNode); - WideString wsChecksum = pXMLDocumentElement->GetString(L"checksum"); - if (wsChecksum.GetLength() != 28 || - m_pXMLParser->m_dwCheckStatus != 0x03) { - return nullptr; - } + pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, false); + if (m_bDocumentParser) { + CFX_XMLElement* pXMLDocumentElement = + static_cast(pXMLDocumentNode); + WideString wsNamespaceURI = pXMLDocumentElement->GetNamespaceURI(); + if (wsNamespaceURI.IsEmpty()) + wsNamespaceURI = pXMLDocumentElement->GetString(L"xmlns:xfa"); + + pNode->GetDocument()->RecognizeXFAVersionNumber(wsNamespaceURI); + } + if (!NormalLoader(pNode, pXMLDocumentNode, XFA_PacketType::Template, true)) + return nullptr; - auto pChecksum = pdfium::MakeUnique(); - pChecksum->StartChecksum(); - pChecksum->UpdateChecksum(m_pFileRead, m_pXMLParser->m_nStart[0], - m_pXMLParser->m_nSize[0]); - pChecksum->UpdateChecksum(m_pFileRead, m_pXMLParser->m_nStart[1], - m_pXMLParser->m_nSize[1]); - pChecksum->FinishChecksum(); - ByteString bsCheck = pChecksum->GetChecksum(); - if (bsCheck != wsChecksum.UTF8Encode()) - return nullptr; + pNode->SetXMLMappingNode(pXMLDocumentNode); + return pNode; +} - pNode = m_pFactory->CreateNode(XFA_PacketType::Form, XFA_Element::Form); - if (!pNode) - return nullptr; +CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Form( + CFX_XMLNode* pXMLDocumentNode) { + const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::Form); + if (!MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, + packet->flags)) { + return nullptr; + } - pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, - false); - pNode->JSNode()->SetAttribute(XFA_Attribute::Checksum, - wsChecksum.AsStringView(), false); - CXFA_Node* pTemplateRoot = - m_pRootNode->GetFirstChildByClass(XFA_Element::Template); - CXFA_Node* pTemplateChosen = - pTemplateRoot - ? pTemplateRoot->GetFirstChildByClass(XFA_Element::Subform) - : nullptr; - bool bUseAttribute = true; - if (pTemplateChosen && - pTemplateChosen->JSNode()->GetEnum(XFA_Attribute::RestoreState) != - XFA_AttributeEnum::Auto) { - bUseAttribute = false; - } - if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, bUseAttribute)) - return nullptr; - } + CFX_XMLElement* pXMLDocumentElement = + static_cast(pXMLDocumentNode); + WideString wsChecksum = pXMLDocumentElement->GetString(L"checksum"); + if (wsChecksum.GetLength() != 28 || m_pXMLParser->m_dwCheckStatus != 0x03) { + return nullptr; } - if (pNode) - pNode->SetXMLMappingNode(pXMLDocumentNode); + auto pChecksum = pdfium::MakeUnique(); + pChecksum->StartChecksum(); + pChecksum->UpdateChecksum(m_pFileRead, m_pXMLParser->m_nStart[0], + m_pXMLParser->m_nSize[0]); + pChecksum->UpdateChecksum(m_pFileRead, m_pXMLParser->m_nStart[1], + m_pXMLParser->m_nSize[1]); + pChecksum->FinishChecksum(); + ByteString bsCheck = pChecksum->GetChecksum(); + if (bsCheck != wsChecksum.UTF8Encode()) + return nullptr; + + CXFA_Node* pNode = + m_pFactory->CreateNode(XFA_PacketType::Form, XFA_Element::Form); + if (!pNode) + return nullptr; + + pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, false); + pNode->JSNode()->SetAttribute(XFA_Attribute::Checksum, + wsChecksum.AsStringView(), false); + CXFA_Node* pTemplateRoot = + m_pRootNode->GetFirstChildByClass(XFA_Element::Template); + CXFA_Node* pTemplateChosen = + pTemplateRoot ? pTemplateRoot->GetFirstChildByClass(XFA_Element::Subform) + : nullptr; + bool bUseAttribute = true; + if (pTemplateChosen && + pTemplateChosen->JSNode()->GetEnum(XFA_Attribute::RestoreState) != + XFA_AttributeEnum::Auto) { + bUseAttribute = false; + } + if (!NormalLoader(pNode, pXMLDocumentNode, XFA_PacketType::Form, + bUseAttribute)) + return nullptr; + + pNode->SetXMLMappingNode(pXMLDocumentNode); return pNode; } @@ -782,53 +782,23 @@ CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Data( CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_LocaleConnectionSourceSet( CFX_XMLNode* pXMLDocumentNode, - XFA_PacketType ePacketID) { - CXFA_Node* pNode = nullptr; - if (ePacketID == XFA_PacketType::LocaleSet) { - const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::LocaleSet); - if (MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, - packet->flags)) { - pNode = m_pFactory->CreateNode(XFA_PacketType::LocaleSet, - XFA_Element::LocaleSet); - if (!pNode) - return nullptr; + XFA_PacketType packet_type, + XFA_Element element) { + const PacketInfo* packet = GetPacketByIndex(packet_type); + if (!MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, + packet->flags)) { + return nullptr; + } - pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, - false); - if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, true)) - return nullptr; - } - } else if (ePacketID == XFA_PacketType::ConnectionSet) { - const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::ConnectionSet); - if (MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, - packet->flags)) { - pNode = m_pFactory->CreateNode(XFA_PacketType::ConnectionSet, - XFA_Element::ConnectionSet); - if (!pNode) - return nullptr; + CXFA_Node* pNode = m_pFactory->CreateNode(packet_type, element); + if (!pNode) + return nullptr; - pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, - false); - if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, true)) - return nullptr; - } - } else if (ePacketID == XFA_PacketType::SourceSet) { - const PacketInfo* packet = GetPacketByIndex(XFA_PacketType::SourceSet); - if (MatchNodeName(pXMLDocumentNode, packet->name, packet->uri, - packet->flags)) { - pNode = m_pFactory->CreateNode(XFA_PacketType::SourceSet, - XFA_Element::SourceSet); - if (!pNode) - return nullptr; + pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, false); + if (!NormalLoader(pNode, pXMLDocumentNode, packet_type, true)) + return nullptr; - pNode->JSNode()->SetCData(XFA_Attribute::Name, packet->name, false, - false); - if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, true)) - return nullptr; - } - } - if (pNode) - pNode->SetXMLMappingNode(pXMLDocumentNode); + pNode->SetXMLMappingNode(pXMLDocumentNode); return pNode; } diff --git a/xfa/fxfa/parser/cxfa_simple_parser.h b/xfa/fxfa/parser/cxfa_simple_parser.h index 5ae9b43be3..2ccb2c3b1a 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.h +++ b/xfa/fxfa/parser/cxfa_simple_parser.h @@ -40,14 +40,14 @@ class CXFA_SimpleParser { CXFA_Node* ParseAsXDPPacket(CFX_XMLNode* pXMLDocumentNode, XFA_PacketType ePacketID); CXFA_Node* ParseAsXDPPacket_XDP(CFX_XMLNode* pXMLDocumentNode); - CXFA_Node* ParseAsXDPPacket_Config(CFX_XMLNode* pXMLDocumentNode, - XFA_PacketType ePacketID); - CXFA_Node* ParseAsXDPPacket_TemplateForm(CFX_XMLNode* pXMLDocumentNode, - XFA_PacketType ePacketID); + CXFA_Node* ParseAsXDPPacket_Config(CFX_XMLNode* pXMLDocumentNode); + CXFA_Node* ParseAsXDPPacket_Template(CFX_XMLNode* pXMLDocumentNode); + CXFA_Node* ParseAsXDPPacket_Form(CFX_XMLNode* pXMLDocumentNode); CXFA_Node* ParseAsXDPPacket_Data(CFX_XMLNode* pXMLDocumentNode); CXFA_Node* ParseAsXDPPacket_LocaleConnectionSourceSet( CFX_XMLNode* pXMLDocumentNode, - XFA_PacketType ePacketID); + XFA_PacketType packet_type, + XFA_Element element); CXFA_Node* ParseAsXDPPacket_Xdc(CFX_XMLNode* pXMLDocumentNode); CXFA_Node* ParseAsXDPPacket_User(CFX_XMLNode* pXMLDocumentNode); CXFA_Node* NormalLoader(CXFA_Node* pXFANode, -- cgit v1.2.3