msxml3: Defer the conversion of XPath expressions to UTF-8.

John Chadwick johnwchadwick at gmail.com
Sat Jul 20 14:28:55 CDT 2013


This patch changes the create_selection interface to accept a wide string
instead of an xmlChar string. This is a transitional change, which will be
needed as the libxml2 XPath parser is phased out.
-------------- next part --------------
From fa013e3dcfa1726e9a216d631a9baf4b71fbc739 Mon Sep 17 00:00:00 2001
From: John Chadwick <johnwchadwick at gmail.com>
Date: Sat, 20 Jul 2013 14:59:10 -0400
Subject: msxml3: Defer the conversion of XPath expressions to UTF-8.

This patch changes the create_selection interface to accept a wide string
instead of an xmlChar string. This is a transitional change, which will be
needed as libxml2's XPath parser is phased out.

The implementation of tagName_to_XPath also had to be changed as it was using
libxml2's UTF-8 string API. libxml2 contained a strcat that performed
reallocation, so it was replaced with a new function that does the same with
heap_realloc and wide strings (heap_strcatW in msxml_private.h.)
---
 dlls/msxml3/domdoc.c        |   39 +++++++++++++++++++++------------------
 dlls/msxml3/element.c       |    4 ++--
 dlls/msxml3/msxml_private.h |   19 +++++++++++++++++--
 dlls/msxml3/node.c          |    5 +----
 dlls/msxml3/selection.c     |    3 ++-
 5 files changed, 43 insertions(+), 27 deletions(-)

diff --git a/dlls/msxml3/domdoc.c b/dlls/msxml3/domdoc.c
index 7eae2d4..8ab6cda 100644
--- a/dlls/msxml3/domdoc.c
+++ b/dlls/msxml3/domdoc.c
@@ -1833,16 +1833,18 @@ static HRESULT WINAPI domdoc_createEntityReference(
     return hr;
 }
 
-xmlChar* tagName_to_XPath(const BSTR tagName)
-{
-    xmlChar *query, *tmp;
-    static const xmlChar mod_pre[] = "*[local-name()='";
-    static const xmlChar mod_post[] = "']";
-    static const xmlChar prefix[] = "descendant::";
+WCHAR *tagName_to_XPath(const BSTR tagName)
+{
+    WCHAR *query, *tmp;
+    static const WCHAR mod_pre[] = {'*','[','l','o','c','a','l','-','n','a','m','e','(',')','=','\'',0};
+    static const WCHAR mod_post[] = {'\'',']',0};
+    static const WCHAR prefix[] = {'d','e','s','c','e','n','d','a','n','t',':',':',0};
+    static const WCHAR slash[] = {'/',0};
+    static const WCHAR wildcard[] = {'*',0};
     const WCHAR *tokBegin, *tokEnd;
     int len;
 
-    query = xmlStrdup(prefix);
+    query = heap_strdupW(prefix);
 
     tokBegin = tagName;
     while (tokBegin && *tokBegin)
@@ -1850,25 +1852,26 @@ xmlChar* tagName_to_XPath(const BSTR tagName)
         switch (*tokBegin)
         {
         case '/':
-            query = xmlStrcat(query, BAD_CAST "/");
+            query = heap_strcatW(query, slash);
             ++tokBegin;
             break;
         case '*':
-            query = xmlStrcat(query, BAD_CAST "*");
+            query = heap_strcatW(query, wildcard);
             ++tokBegin;
             break;
         default:
-            query = xmlStrcat(query, mod_pre);
+            query = heap_strcatW(query, mod_pre);
             tokEnd = tokBegin;
             while (*tokEnd && *tokEnd != '/')
                 ++tokEnd;
-            len = WideCharToMultiByte(CP_UTF8, 0, tokBegin, tokEnd-tokBegin, NULL, 0, NULL, NULL);
-            tmp = xmlMalloc(len);
-            WideCharToMultiByte(CP_UTF8, 0, tokBegin, tokEnd-tokBegin, (char*)tmp, len, NULL, NULL);
-            query = xmlStrncat(query, tmp, len);
-            xmlFree(tmp);
+            len = tokEnd - tokBegin;
+            tmp = heap_alloc((len + 1) * sizeof(WCHAR));
+            memcpy(tmp, tokBegin, len * sizeof(WCHAR));
+            tmp[len] = 0;
+            query = heap_strcatW(query, tmp);
+            heap_free(tmp);
             tokBegin = tokEnd;
-            query = xmlStrcat(query, mod_post);
+            query = heap_strcatW(query, mod_post);
         }
     }
 
@@ -1881,7 +1884,7 @@ static HRESULT WINAPI domdoc_getElementsByTagName(
     IXMLDOMNodeList** resultList )
 {
     domdoc *This = impl_from_IXMLDOMDocument3( iface );
-    xmlChar *query;
+    WCHAR *query;
     HRESULT hr;
     BOOL XPath;
 
@@ -1893,7 +1896,7 @@ static HRESULT WINAPI domdoc_getElementsByTagName(
     This->properties->XPath = TRUE;
     query = tagName_to_XPath(tagName);
     hr = create_selection((xmlNodePtr)get_doc(This), query, resultList);
-    xmlFree(query);
+    heap_free(query);
     This->properties->XPath = XPath;
 
     return hr;
diff --git a/dlls/msxml3/element.c b/dlls/msxml3/element.c
index 5e8822e..4d1a6c9 100644
--- a/dlls/msxml3/element.c
+++ b/dlls/msxml3/element.c
@@ -1472,7 +1472,7 @@ static HRESULT WINAPI domelem_getElementsByTagName(
     BSTR tagName, IXMLDOMNodeList** resultList)
 {
     domelem *This = impl_from_IXMLDOMElement( iface );
-    xmlChar *query;
+    WCHAR *query;
     HRESULT hr;
     BOOL XPath;
 
@@ -1484,7 +1484,7 @@ static HRESULT WINAPI domelem_getElementsByTagName(
     set_xpathmode(get_element(This)->doc, TRUE);
     query = tagName_to_XPath(tagName);
     hr = create_selection(get_element(This), query, resultList);
-    xmlFree(query);
+    heap_free(query);
     set_xpathmode(get_element(This)->doc, XPath);
 
     return hr;
diff --git a/dlls/msxml3/msxml_private.h b/dlls/msxml3/msxml_private.h
index 17e19a7..7b427f5 100644
--- a/dlls/msxml3/msxml_private.h
+++ b/dlls/msxml3/msxml_private.h
@@ -217,6 +217,21 @@ static inline LPSTR heap_strdupWtoA(LPCWSTR str)
     return ret;
 }
 
+static inline WCHAR *heap_strcatW(WCHAR *a, const WCHAR *b)
+{
+    int len = strlenW(a) + strlenW(b);
+    WCHAR *c = heap_realloc(a, (len + 1) * sizeof(WCHAR));
+    WCHAR *ptr = c;
+
+    while(*ptr)
+        ptr++;
+    while(*b)
+        *(ptr++) = *(b++);
+    c[len] = 0;
+
+    return c;
+}
+
 #ifdef HAVE_LIBXML2
 
 extern void schemasInit(void) DECLSPEC_HIDDEN;
@@ -280,7 +295,7 @@ extern IUnknown         *create_doc_Implementation(void) DECLSPEC_HIDDEN;
 extern IUnknown         *create_doc_fragment( xmlNodePtr ) DECLSPEC_HIDDEN;
 extern IUnknown         *create_doc_entity_ref( xmlNodePtr ) DECLSPEC_HIDDEN;
 extern IUnknown         *create_doc_type( xmlNodePtr ) DECLSPEC_HIDDEN;
-extern HRESULT           create_selection( xmlNodePtr, xmlChar*, IXMLDOMNodeList** ) DECLSPEC_HIDDEN;
+extern HRESULT           create_selection( xmlNodePtr, WCHAR*, IXMLDOMNodeList** ) DECLSPEC_HIDDEN;
 extern HRESULT           create_enumvariant( IUnknown*, BOOL, const struct enumvariant_funcs*, IEnumVARIANT**) DECLSPEC_HIDDEN;
 
 /* data accessors */
@@ -371,7 +386,7 @@ extern HRESULT dt_validate(XDR_DT dt, xmlChar const* content) DECLSPEC_HIDDEN;
 
 extern BSTR EnsureCorrectEOL(BSTR) DECLSPEC_HIDDEN;
 
-extern xmlChar* tagName_to_XPath(const BSTR tagName) DECLSPEC_HIDDEN;
+extern WCHAR* tagName_to_XPath(const BSTR tagName) DECLSPEC_HIDDEN;
 
 static inline BSTR bstr_from_xmlChar(const xmlChar *str)
 {
diff --git a/dlls/msxml3/node.c b/dlls/msxml3/node.c
index 79de75c..9a75aa4 100644
--- a/dlls/msxml3/node.c
+++ b/dlls/msxml3/node.c
@@ -1009,14 +1009,11 @@ HRESULT node_transform_node(const xmlnode *This, IXMLDOMNode *stylesheet, BSTR *
 
 HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
 {
-    xmlChar* str;
     HRESULT hr;
 
     if (!query || !nodes) return E_INVALIDARG;
 
-    str = xmlchar_from_wchar(query);
-    hr = create_selection(This->node, str, nodes);
-    heap_free(str);
+    hr = create_selection(This->node, query, nodes);
 
     return hr;
 }
diff --git a/dlls/msxml3/selection.c b/dlls/msxml3/selection.c
index bb0f42e..4777256 100644
--- a/dlls/msxml3/selection.c
+++ b/dlls/msxml3/selection.c
@@ -766,11 +766,12 @@ static void query_serror(void* ctx, xmlErrorPtr err)
     LIBXML2_CALLBACK_SERROR(domselection_create, err);
 }
 
-HRESULT create_selection(xmlNodePtr node, xmlChar* query, IXMLDOMNodeList **out)
+HRESULT create_selection(xmlNodePtr node, WCHAR* str, IXMLDOMNodeList **out)
 {
     domselection *This = heap_alloc(sizeof(domselection));
     xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc);
     HRESULT hr;
+    xmlChar *query = xmlchar_from_wchar(str);
 
     TRACE("(%p, %s, %p)\n", node, debugstr_a((char const*)query), out);
 
-- 
1.7.10.4


More information about the wine-patches mailing list