Cleanup XFA packet code

Remove GetPacketByID, move GetPacketByName to the xfa_utils file.
Cleanup CreateNode to accept the XFA_XDPPACKET instead of the packet
info.

Change-Id: I0f246c84f61b6b4175ca307bdcd125d9bc24bb1e
Reviewed-on: https://pdfium-review.googlesource.com/20010
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
diff --git a/fxjs/cjx_node.cpp b/fxjs/cjx_node.cpp
index 3442b35..cdf8984 100644
--- a/fxjs/cjx_node.cpp
+++ b/fxjs/cjx_node.cpp
@@ -3522,7 +3522,6 @@
 CXFA_Node* CJX_Node::GetProperty(int32_t index,
                                  XFA_Element eProperty,
                                  bool bCreateProperty) {
-  uint32_t dwPacket = GetXFANode()->GetPacketID();
   if (index < 0 || index >= GetXFANode()->PropertyOccuranceCount(eProperty))
     return nullptr;
 
@@ -3548,10 +3547,10 @@
     }
   }
 
-  const XFA_PACKETINFO* pPacket = XFA_GetPacketByID(dwPacket);
   CXFA_Node* pNewNode = nullptr;
-  for (; iCount <= index; iCount++) {
-    pNewNode = GetDocument()->CreateNode(pPacket, eProperty);
+  for (; iCount <= index; ++iCount) {
+    pNewNode =
+        GetDocument()->CreateNode(GetXFANode()->GetPacketID(), eProperty);
     if (!pNewNode)
       return nullptr;
     GetXFANode()->InsertChild(pNewNode, nullptr);
diff --git a/xfa/fxfa/fxfa_basic.h b/xfa/fxfa/fxfa_basic.h
index b8e3574..1f79608 100644
--- a/xfa/fxfa/fxfa_basic.h
+++ b/xfa/fxfa/fxfa_basic.h
@@ -87,6 +87,8 @@
       1 << static_cast<uint8_t>(XFA_PacketType::Stylesheet),
   XFA_XDPPACKET_USER = 1 << static_cast<uint8_t>(XFA_PacketType::User),
   XFA_XDPPACKET_XDP = 1 << static_cast<uint8_t>(XFA_PacketType::Xdp),
+
+  XFA_XDPPACKET_LAST = XFA_XDPPACKET_ConnectionSet + 1
 };
 
 enum XFA_XDPPACKET_FLAGS {
diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp
index 3fc5c39..4e230ee 100644
--- a/xfa/fxfa/parser/cxfa_document.cpp
+++ b/xfa/fxfa/parser/cxfa_document.cpp
@@ -213,17 +213,14 @@
   }
 }
 
-CXFA_Node* CXFA_Document::CreateNode(uint32_t dwPacket, XFA_Element eElement) {
-  return CreateNode(XFA_GetPacketByID(dwPacket), eElement);
-}
-
-CXFA_Node* CXFA_Document::CreateNode(const XFA_PACKETINFO* pPacket,
+CXFA_Node* CXFA_Document::CreateNode(XFA_XDPPACKET packet,
                                      XFA_Element eElement) {
-  if (!pPacket || eElement == XFA_Element::Unknown)
+  if (packet == XFA_XDPPACKET_UNKNOWN || packet >= XFA_XDPPACKET_LAST ||
+      eElement == XFA_Element::Unknown) {
     return nullptr;
+  }
 
-  std::unique_ptr<CXFA_Node> pNode =
-      CXFA_Node::Create(this, eElement, pPacket->eName);
+  std::unique_ptr<CXFA_Node> pNode = CXFA_Node::Create(this, eElement, packet);
   if (!pNode)
     return nullptr;
 
diff --git a/xfa/fxfa/parser/cxfa_document.h b/xfa/fxfa/parser/cxfa_document.h
index 45e9236..82ef661 100644
--- a/xfa/fxfa/parser/cxfa_document.h
+++ b/xfa/fxfa/parser/cxfa_document.h
@@ -87,8 +87,7 @@
   XFA_VERSION GetCurVersionMode() { return m_eCurVersionMode; }
   XFA_VERSION RecognizeXFAVersionNumber(const WideString& wsTemplateNS);
 
-  CXFA_Node* CreateNode(uint32_t dwPacket, XFA_Element eElement);
-  CXFA_Node* CreateNode(const XFA_PACKETINFO* pPacket, XFA_Element eElement);
+  CXFA_Node* CreateNode(XFA_XDPPACKET packet, XFA_Element eElement);
 
   void DoProtoMerge();
   void DoDataMerge();
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index f24bddb..c8e2266 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -157,7 +157,7 @@
 }
 
 CXFA_Node::CXFA_Node(CXFA_Document* pDoc,
-                     uint16_t ePacket,
+                     XFA_XDPPACKET ePacket,
                      uint32_t validPackets,
                      XFA_ObjectType oType,
                      XFA_Element eType,
@@ -429,8 +429,7 @@
   if (!property)
     return nodes;
 
-  const XFA_PACKETINFO* pPacket = XFA_GetPacketByID(GetPacketID());
-  CXFA_Node* pNewNode = m_pDocument->CreateNode(pPacket, *property);
+  CXFA_Node* pNewNode = m_pDocument->CreateNode(GetPacketID(), *property);
   if (pNewNode) {
     InsertChild(pNewNode, nullptr);
     pNewNode->SetFlag(XFA_NodeFlag_Initialized, true);
diff --git a/xfa/fxfa/parser/cxfa_node.h b/xfa/fxfa/parser/cxfa_node.h
index 3e05f36..a2e8b30 100644
--- a/xfa/fxfa/parser/cxfa_node.h
+++ b/xfa/fxfa/parser/cxfa_node.h
@@ -84,7 +84,7 @@
   XFA_Attribute GetAttribute(size_t i) const;
   XFA_AttributeType GetAttributeType(XFA_Attribute type) const;
 
-  uint32_t GetPacketID() const { return m_ePacket; }
+  XFA_XDPPACKET GetPacketID() const { return m_ePacket; }
 
   void SetFlag(uint32_t dwFlag, bool bNotify);
   void ClearFlag(uint32_t dwFlag);
@@ -182,7 +182,7 @@
 
  protected:
   CXFA_Node(CXFA_Document* pDoc,
-            uint16_t ePacket,
+            XFA_XDPPACKET ePacket,
             uint32_t validPackets,
             XFA_ObjectType oType,
             XFA_Element eType,
@@ -200,15 +200,15 @@
   pdfium::Optional<void*> GetDefaultValue(XFA_Attribute attr,
                                           XFA_AttributeType eType) const;
 
-  const PropertyData* m_Properties;
-  const AttributeData* m_Attributes;
-  uint32_t m_ValidPackets;
+  const PropertyData* const m_Properties;
+  const AttributeData* const m_Attributes;
+  const uint32_t m_ValidPackets;
   CXFA_Node* m_pNext;
   CXFA_Node* m_pChild;
   CXFA_Node* m_pLastChild;
   CXFA_Node* m_pParent;
   CFX_XMLNode* m_pXMLNode;
-  uint16_t m_ePacket;
+  const XFA_XDPPACKET m_ePacket;
   uint16_t m_uNodeFlags;
   uint32_t m_dwNameHash;
   CXFA_Node* m_pAuxNode;
diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp
index 453ae8d..5152403 100644
--- a/xfa/fxfa/parser/cxfa_simple_parser.cpp
+++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp
@@ -228,26 +228,6 @@
   }
 }
 
-const XFA_PACKETINFO* GetPacketByName(const WideStringView& wsName) {
-  if (wsName.IsEmpty())
-    return nullptr;
-
-  uint32_t uHash = FX_HashCode_GetW(wsName, false);
-  int32_t iStart = 0;
-  int32_t iEnd = g_iXFAPacketCount - 1;
-  do {
-    int32_t iMid = (iStart + iEnd) / 2;
-    const XFA_PACKETINFO* pInfo = g_XFAPacketData + iMid;
-    if (uHash == pInfo->uHash)
-      return pInfo;
-    if (uHash < pInfo->uHash)
-      iEnd = iMid - 1;
-    else
-      iStart = iMid + 1;
-  } while (iStart <= iEnd);
-  return nullptr;
-}
-
 }  // namespace
 
 bool XFA_RecognizeRichText(CFX_XMLElement* pRichTextXMLNode) {
@@ -508,7 +488,7 @@
     CFX_XMLElement* pElement = reinterpret_cast<CFX_XMLElement*>(pChildItem);
     WideString wsPacketName = pElement->GetLocalTagName();
     const XFA_PACKETINFO* pPacketInfo =
-        GetPacketByName(wsPacketName.AsStringView());
+        XFA_GetPacketByName(wsPacketName.AsStringView());
     if (pPacketInfo && pPacketInfo->pURI) {
       if (!MatchNodeName(pElement, pPacketInfo->pName, pPacketInfo->pURI,
                          pPacketInfo->eFlags)) {
diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp
index e931576..a7b31ae 100644
--- a/xfa/fxfa/parser/xfa_utils.cpp
+++ b/xfa/fxfa/parser/xfa_utils.cpp
@@ -190,14 +190,19 @@
   return g_XFAPacketData + static_cast<uint8_t>(ePacket);
 }
 
-const XFA_PACKETINFO* XFA_GetPacketByID(uint32_t dwPacket) {
-  int32_t iStart = 0, iEnd = g_iXFAPacketCount - 1;
+const XFA_PACKETINFO* XFA_GetPacketByName(const WideStringView& wsName) {
+  if (wsName.IsEmpty())
+    return nullptr;
+
+  uint32_t uHash = FX_HashCode_GetW(wsName, false);
+  int32_t iStart = 0;
+  int32_t iEnd = g_iXFAPacketCount - 1;
   do {
     int32_t iMid = (iStart + iEnd) / 2;
-    uint32_t dwFind = (g_XFAPacketData + iMid)->eName;
-    if (dwPacket == dwFind)
-      return g_XFAPacketData + iMid;
-    if (dwPacket < dwFind)
+    const XFA_PACKETINFO* pInfo = g_XFAPacketData + iMid;
+    if (uHash == pInfo->uHash)
+      return pInfo;
+    if (uHash < pInfo->uHash)
       iEnd = iMid - 1;
     else
       iStart = iMid + 1;
diff --git a/xfa/fxfa/parser/xfa_utils.h b/xfa/fxfa/parser/xfa_utils.h
index 889dcc0..5cd265e 100644
--- a/xfa/fxfa/parser/xfa_utils.h
+++ b/xfa/fxfa/parser/xfa_utils.h
@@ -43,6 +43,6 @@
     const WideStringView& wsAttributeName);
 
 const XFA_PACKETINFO* XFA_GetPacketByIndex(XFA_PacketType ePacket);
-const XFA_PACKETINFO* XFA_GetPacketByID(uint32_t dwPacket);
+const XFA_PACKETINFO* XFA_GetPacketByName(const WideStringView& wsName);
 
 #endif  // XFA_FXFA_PARSER_XFA_UTILS_H_