msxml3: Fixed if nesting / ptr checking problems in xmlnode_transformNode.
[wine] / dlls / msxml3 / nodemap.c
1 /*
2  *    Node map implementation
3  *
4  * Copyright 2005 Mike McCormack
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #define COBJMACROS
24
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "msxml2.h"
32
33 #include "msxml_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
38
39 #ifdef HAVE_LIBXML2
40
41 typedef struct _xmlnodemap
42 {
43     const struct IXMLDOMNamedNodeMapVtbl *lpVtbl;
44     const struct ISupportErrorInfoVtbl *lpSEIVtbl;
45     LONG ref;
46     IXMLDOMNode *node;
47     long iterator;
48 } xmlnodemap;
49
50 static inline xmlnodemap *impl_from_IXMLDOMNamedNodeMap( IXMLDOMNamedNodeMap *iface )
51 {
52     return (xmlnodemap *)((char*)iface - FIELD_OFFSET(xmlnodemap, lpVtbl));
53 }
54
55 static inline xmlnodemap *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
56 {
57     return (xmlnodemap *)((char*)iface - FIELD_OFFSET(xmlnodemap, lpSEIVtbl));
58 }
59
60 static HRESULT WINAPI xmlnodemap_QueryInterface(
61     IXMLDOMNamedNodeMap *iface,
62     REFIID riid, void** ppvObject )
63 {
64     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
65     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
66
67     if( IsEqualGUID( riid, &IID_IUnknown ) ||
68         IsEqualGUID( riid, &IID_IDispatch ) ||
69         IsEqualGUID( riid, &IID_IXMLDOMNamedNodeMap ) )
70     {
71         *ppvObject = iface;
72     }
73     else if( IsEqualGUID( riid, &IID_ISupportErrorInfo ))
74     {
75         *ppvObject = &This->lpSEIVtbl;
76     }
77     else
78     {
79         FIXME("interface %s not implemented\n", debugstr_guid(riid));
80         return E_NOINTERFACE;
81     }
82
83     IXMLDOMElement_AddRef( iface );
84
85     return S_OK;
86 }
87
88 static ULONG WINAPI xmlnodemap_AddRef(
89     IXMLDOMNamedNodeMap *iface )
90 {
91     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
92     return InterlockedIncrement( &This->ref );
93 }
94
95 static ULONG WINAPI xmlnodemap_Release(
96     IXMLDOMNamedNodeMap *iface )
97 {
98     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
99     ULONG ref;
100
101     ref = InterlockedDecrement( &This->ref );
102     if ( ref == 0 )
103     {
104         IXMLDOMNode_Release( This->node );
105         HeapFree( GetProcessHeap(), 0, This );
106     }
107
108     return ref;
109 }
110
111 static HRESULT WINAPI xmlnodemap_GetTypeInfoCount(
112     IXMLDOMNamedNodeMap *iface,
113     UINT* pctinfo )
114 {
115     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
116
117     TRACE("(%p)->(%p)\n", This, pctinfo);
118
119     *pctinfo = 1;
120
121     return S_OK;
122 }
123
124 static HRESULT WINAPI xmlnodemap_GetTypeInfo(
125     IXMLDOMNamedNodeMap *iface,
126     UINT iTInfo, LCID lcid,
127     ITypeInfo** ppTInfo )
128 {
129     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
130     HRESULT hr;
131
132     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
133
134     hr = get_typeinfo(IXMLDOMNamedNodeMap_tid, ppTInfo);
135
136     return hr;
137 }
138
139 static HRESULT WINAPI xmlnodemap_GetIDsOfNames(
140     IXMLDOMNamedNodeMap *iface,
141     REFIID riid, LPOLESTR* rgszNames,
142     UINT cNames, LCID lcid, DISPID* rgDispId )
143 {
144     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
145     ITypeInfo *typeinfo;
146     HRESULT hr;
147
148     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
149           lcid, rgDispId);
150
151     if(!rgszNames || cNames == 0 || !rgDispId)
152         return E_INVALIDARG;
153
154     hr = get_typeinfo(IXMLDOMNamedNodeMap_tid, &typeinfo);
155     if(SUCCEEDED(hr))
156     {
157         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
158         ITypeInfo_Release(typeinfo);
159     }
160
161     return hr;
162 }
163
164 static HRESULT WINAPI xmlnodemap_Invoke(
165     IXMLDOMNamedNodeMap *iface,
166     DISPID dispIdMember, REFIID riid, LCID lcid,
167     WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
168     EXCEPINFO* pExcepInfo, UINT* puArgErr )
169 {
170     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
171     ITypeInfo *typeinfo;
172     HRESULT hr;
173
174     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
175           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
176
177     hr = get_typeinfo(IXMLDOMNamedNodeMap_tid, &typeinfo);
178     if(SUCCEEDED(hr))
179     {
180         hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
181                 pVarResult, pExcepInfo, puArgErr);
182         ITypeInfo_Release(typeinfo);
183     }
184
185     return hr;
186 }
187
188 xmlChar *xmlChar_from_wchar( LPWSTR str )
189 {
190     DWORD len;
191     xmlChar *xmlstr;
192
193     len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
194     xmlstr = HeapAlloc( GetProcessHeap(), 0, len );
195     if ( xmlstr )
196         WideCharToMultiByte( CP_UTF8, 0, str, -1, (LPSTR) xmlstr, len, NULL, NULL );
197     return xmlstr;
198 }
199
200 static HRESULT WINAPI xmlnodemap_getNamedItem(
201     IXMLDOMNamedNodeMap *iface,
202     BSTR name,
203     IXMLDOMNode** namedItem)
204 {
205     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
206     xmlChar *element_name;
207     xmlAttrPtr attr;
208     xmlNodePtr node;
209
210     TRACE("%p %s %p\n", This, debugstr_w(name), namedItem );
211
212     if ( !namedItem )
213         return E_INVALIDARG;
214
215     node = xmlNodePtr_from_domnode( This->node, 0 );
216     if ( !node )
217         return E_FAIL;
218
219     element_name = xmlChar_from_wchar( name );
220     attr = xmlHasNsProp( node, element_name, NULL );
221     HeapFree( GetProcessHeap(), 0, element_name );
222
223     if ( !attr )
224     {
225         *namedItem = NULL;
226         return S_FALSE;
227     }
228
229     *namedItem = create_node( (xmlNodePtr) attr );
230
231     return S_OK;
232 }
233
234 static HRESULT WINAPI xmlnodemap_setNamedItem(
235     IXMLDOMNamedNodeMap *iface,
236     IXMLDOMNode* newItem,
237     IXMLDOMNode** namedItem)
238 {
239     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
240     xmlnode *ThisNew = NULL;
241     xmlNodePtr nodeNew;
242     IXMLDOMNode *pAttr = NULL;
243     xmlNodePtr node;
244
245     TRACE("%p %p %p\n", This, newItem, namedItem );
246
247     if(!newItem)
248         return E_INVALIDARG;
249
250     if(namedItem) *namedItem = NULL;
251
252     node = xmlNodePtr_from_domnode( This->node, 0 );
253     if ( !node )
254         return E_FAIL;
255
256     /* Must be an Attribute */
257     IUnknown_QueryInterface(newItem, &IID_IXMLDOMNode, (LPVOID*)&pAttr);
258     if(pAttr)
259     {
260         ThisNew = impl_from_IXMLDOMNode( pAttr );
261
262         if(ThisNew->node->type != XML_ATTRIBUTE_NODE)
263         {
264             IUnknown_Release(pAttr);
265             return E_FAIL;
266         }
267
268         if(!ThisNew->node->parent)
269             if(xmldoc_remove_orphan(ThisNew->node->doc, ThisNew->node) != S_OK)
270                 WARN("%p is not an orphan of %p\n", ThisNew->node, ThisNew->node->doc);
271
272         nodeNew = xmlAddChild(node, ThisNew->node);
273
274         if(namedItem)
275             *namedItem = create_node( nodeNew );
276
277         IUnknown_Release(pAttr);
278
279         return S_OK;
280     }
281
282     return E_INVALIDARG;
283 }
284
285 static HRESULT WINAPI xmlnodemap_removeNamedItem(
286     IXMLDOMNamedNodeMap *iface,
287     BSTR name,
288     IXMLDOMNode** namedItem)
289 {
290     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
291     xmlChar *element_name;
292     xmlAttrPtr attr, attr_copy;
293     xmlNodePtr node;
294
295     TRACE("%p %s %p\n", This, debugstr_w(name), namedItem );
296
297     if ( !name)
298         return E_INVALIDARG;
299
300     node = xmlNodePtr_from_domnode( This->node, 0 );
301     if ( !node )
302         return E_FAIL;
303
304     element_name = xmlChar_from_wchar( name );
305     attr = xmlHasNsProp( node, element_name, NULL );
306     HeapFree( GetProcessHeap(), 0, element_name );
307
308     if ( !attr )
309     {
310         if( namedItem )
311             *namedItem = NULL;
312         return S_FALSE;
313     }
314
315     if ( namedItem )
316     {
317         attr_copy = xmlCopyProp( NULL, attr );
318         attr_copy->doc = node->doc;
319         /* The cast here is OK, xmlFreeNode handles xmlAttrPtr pointers */
320         xmldoc_add_orphan(attr_copy->doc, (xmlNodePtr) attr_copy);
321         *namedItem = create_node( (xmlNodePtr) attr_copy );
322     }
323     xmlRemoveProp( attr );
324
325     return S_OK;
326 }
327
328 static HRESULT WINAPI xmlnodemap_get_item(
329     IXMLDOMNamedNodeMap *iface,
330     long index,
331     IXMLDOMNode** listItem)
332 {
333     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
334     xmlNodePtr node;
335     xmlAttrPtr curr;
336     long attrIndex;
337
338     TRACE("%p %ld\n", This, index);
339
340     *listItem = NULL;
341
342     if (index < 0)
343         return S_FALSE;
344
345     node = xmlNodePtr_from_domnode( This->node, 0 );
346     curr = node->properties;
347
348     for (attrIndex = 0; attrIndex < index; attrIndex++) {
349         if (curr->next == NULL)
350             return S_FALSE;
351         else
352             curr = curr->next;
353     }
354     
355     *listItem = create_node( (xmlNodePtr) curr );
356
357     return S_OK;
358 }
359
360 static HRESULT WINAPI xmlnodemap_get_length(
361     IXMLDOMNamedNodeMap *iface,
362     long* listLength)
363 {
364     xmlNodePtr node;
365     xmlAttrPtr first;
366     xmlAttrPtr curr;
367     long attrCount;
368
369     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
370
371     TRACE("%p\n", This);
372
373     if( !listLength )
374         return E_INVALIDARG;
375
376     node = xmlNodePtr_from_domnode( This->node, 0 );
377     if ( !node )
378         return E_FAIL;
379
380     first = node->properties;
381     if (first == NULL) {
382         *listLength = 0;
383         return S_OK;
384     }
385
386     curr = first;
387     attrCount = 1;
388     while (curr->next != NULL) {
389         attrCount++;
390         curr = curr->next;
391     }
392     *listLength = attrCount;
393  
394     return S_OK;
395 }
396
397 static HRESULT WINAPI xmlnodemap_getQualifiedItem(
398     IXMLDOMNamedNodeMap *iface,
399     BSTR baseName,
400     BSTR namespaceURI,
401     IXMLDOMNode** qualifiedItem)
402 {
403     FIXME("\n");
404     return E_NOTIMPL;
405 }
406
407 static HRESULT WINAPI xmlnodemap_removeQualifiedItem(
408     IXMLDOMNamedNodeMap *iface,
409     BSTR baseName,
410     BSTR namespaceURI,
411     IXMLDOMNode** qualifiedItem)
412 {
413     FIXME("\n");
414     return E_NOTIMPL;
415 }
416
417 static HRESULT WINAPI xmlnodemap_nextNode(
418     IXMLDOMNamedNodeMap *iface,
419     IXMLDOMNode** nextItem)
420 {
421     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
422     xmlNodePtr node;
423     xmlAttrPtr curr;
424     long attrIndex;
425
426     TRACE("%p %ld\n", This, This->iterator);
427
428     *nextItem = NULL;
429
430     node = xmlNodePtr_from_domnode( This->node, 0 );
431     curr = node->properties;
432
433     for (attrIndex = 0; attrIndex < This->iterator; attrIndex++) {
434         if (curr->next == NULL)
435             return S_FALSE;
436         else
437             curr = curr->next;
438     }
439
440     This->iterator++;
441
442     *nextItem = create_node( (xmlNodePtr) curr );
443
444     return S_OK;
445 }
446
447 static HRESULT WINAPI xmlnodemap_reset(
448     IXMLDOMNamedNodeMap *iface )
449 {
450     xmlnodemap *This = impl_from_IXMLDOMNamedNodeMap( iface );
451
452     TRACE("%p %ld\n", This, This->iterator);
453
454     This->iterator = 0;
455
456     return S_OK;
457 }
458
459 static HRESULT WINAPI xmlnodemap__newEnum(
460     IXMLDOMNamedNodeMap *iface,
461     IUnknown** ppUnk)
462 {
463     FIXME("\n");
464     return E_NOTIMPL;
465 }
466
467 static const struct IXMLDOMNamedNodeMapVtbl xmlnodemap_vtbl =
468 {
469     xmlnodemap_QueryInterface,
470     xmlnodemap_AddRef,
471     xmlnodemap_Release,
472     xmlnodemap_GetTypeInfoCount,
473     xmlnodemap_GetTypeInfo,
474     xmlnodemap_GetIDsOfNames,
475     xmlnodemap_Invoke,
476     xmlnodemap_getNamedItem,
477     xmlnodemap_setNamedItem,
478     xmlnodemap_removeNamedItem,
479     xmlnodemap_get_item,
480     xmlnodemap_get_length,
481     xmlnodemap_getQualifiedItem,
482     xmlnodemap_removeQualifiedItem,
483     xmlnodemap_nextNode,
484     xmlnodemap_reset,
485     xmlnodemap__newEnum,
486 };
487
488 static HRESULT WINAPI support_error_QueryInterface(
489     ISupportErrorInfo *iface,
490     REFIID riid, void** ppvObject )
491 {
492     xmlnodemap *This = impl_from_ISupportErrorInfo( iface );
493     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
494
495     return IXMLDOMNamedNodeMap_QueryInterface((IXMLDOMNamedNodeMap*)&This->lpVtbl, riid, ppvObject);
496 }
497
498 static ULONG WINAPI support_error_AddRef(
499     ISupportErrorInfo *iface )
500 {
501     xmlnodemap *This = impl_from_ISupportErrorInfo( iface );
502     return IXMLDOMNamedNodeMap_AddRef((IXMLDOMNamedNodeMap*)&This->lpVtbl);
503 }
504
505 static ULONG WINAPI support_error_Release(
506     ISupportErrorInfo *iface )
507 {
508     xmlnodemap *This = impl_from_ISupportErrorInfo( iface );
509     return IXMLDOMNamedNodeMap_Release((IXMLDOMNamedNodeMap*)&This->lpVtbl);
510 }
511
512 static HRESULT WINAPI support_error_InterfaceSupportsErrorInfo(
513     ISupportErrorInfo *iface,
514     REFIID riid )
515 {
516     FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
517     return S_FALSE;
518 }
519
520 static const struct ISupportErrorInfoVtbl support_error_vtbl =
521 {
522     support_error_QueryInterface,
523     support_error_AddRef,
524     support_error_Release,
525     support_error_InterfaceSupportsErrorInfo
526 };
527
528 IXMLDOMNamedNodeMap *create_nodemap( IXMLDOMNode *node )
529 {
530     xmlnodemap *nodemap;
531
532     nodemap = HeapAlloc( GetProcessHeap(), 0, sizeof *nodemap );
533     if ( !nodemap )
534         return NULL;
535
536     nodemap->lpVtbl = &xmlnodemap_vtbl;
537     nodemap->lpSEIVtbl = &support_error_vtbl;
538     nodemap->node = node;
539     nodemap->ref = 1;
540     nodemap->iterator = 0;
541
542     IXMLDOMNode_AddRef( node );
543     /* Since we AddRef a node here, we don't need to call xmldoc_add_ref() */
544
545     return (IXMLDOMNamedNodeMap*) &nodemap->lpVtbl;
546 }
547
548 #endif