Always define WIDL_C_INLINE_WRAPPERS.
[wine] / dlls / msxml3 / saxreader.c
1 /*
2  *    SAX Reader implementation
3  *
4  * Copyright 2008 Alistair Leslie-Hughes
5  * Copyright 2008 Piotr Caban
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 #define COBJMACROS
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #ifdef HAVE_LIBXML2
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
29 # include <libxml/SAX2.h>
30 # include <libxml/parserInternals.h>
31 #endif
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "ole2.h"
38 #include "msxml6.h"
39 #include "wininet.h"
40 #include "urlmon.h"
41 #include "winreg.h"
42 #include "shlwapi.h"
43
44 #include "wine/debug.h"
45 #include "wine/list.h"
46
47 #include "msxml_private.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
50
51 #ifdef HAVE_LIBXML2
52
53 typedef enum
54 {
55     FeatureUnknown               = 0,
56     ExhaustiveErrors             = 1 << 1,
57     ExternalGeneralEntities      = 1 << 2,
58     ExternalParameterEntities    = 1 << 3,
59     ForcedResync                 = 1 << 4,
60     NamespacePrefixes            = 1 << 5,
61     Namespaces                   = 1 << 6,
62     ParameterEntities            = 1 << 7,
63     PreserveSystemIndentifiers   = 1 << 8,
64     ProhibitDTD                  = 1 << 9,
65     SchemaValidation             = 1 << 10,
66     ServerHttpRequest            = 1 << 11,
67     SuppressValidationfatalError = 1 << 12,
68     UseInlineSchema              = 1 << 13,
69     UseSchemaLocation            = 1 << 14,
70     LexicalHandlerParEntities    = 1 << 15
71 } saxreader_feature;
72
73 /* feature names */
74 static const WCHAR FeatureExternalGeneralEntitiesW[] = {
75     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/','s','a','x','/',
76     'f','e','a','t','u','r','e','s','/','e','x','t','e','r','n','a','l','-','g','e','n','e','r','a','l',
77     '-','e','n','t','i','t','i','e','s',0
78 };
79
80 static const WCHAR FeatureExternalParameterEntitiesW[] = {
81     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/','s','a','x','/','f','e','a','t','u','r','e','s',
82     '/','e','x','t','e','r','n','a','l','-','p','a','r','a','m','e','t','e','r','-','e','n','t','i','t','i','e','s',0
83 };
84
85 static const WCHAR FeatureLexicalHandlerParEntitiesW[] = {
86     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/','s','a','x','/','f','e','a','t','u','r','e','s',
87     '/','l','e','x','i','c','a','l','-','h','a','n','d','l','e','r','/','p','a','r','a','m','e','t','e','r','-','e','n','t','i','t','i','e','s',0
88 };
89
90 static const WCHAR FeatureProhibitDTDW[] = {
91     'p','r','o','h','i','b','i','t','-','d','t','d',0
92 };
93
94 static const WCHAR FeatureNamespacesW[] = {
95     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/','s','a','x','/','f','e','a','t','u','r','e','s',
96     '/','n','a','m','e','s','p','a','c','e','s',0
97 };
98
99 static const WCHAR FeatureNamespacePrefixesW[] = {
100     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/','s','a','x','/','f','e','a','t','u','r','e','s',
101     '/','n','a','m','e','s','p','a','c','e','-','p','r','e','f','i','x','e','s',0
102 };
103
104 struct saxreader_feature_pair
105 {
106     saxreader_feature feature;
107     const WCHAR *name;
108 };
109
110 static const struct saxreader_feature_pair saxreader_feature_map[] = {
111     { ExternalGeneralEntities, FeatureExternalGeneralEntitiesW },
112     { ExternalParameterEntities, FeatureExternalParameterEntitiesW },
113     { LexicalHandlerParEntities, FeatureLexicalHandlerParEntitiesW },
114     { NamespacePrefixes, FeatureNamespacePrefixesW },
115     { Namespaces, FeatureNamespacesW },
116     { ProhibitDTD, FeatureProhibitDTDW }
117 };
118
119 static saxreader_feature get_saxreader_feature(const WCHAR *name)
120 {
121     int min, max, n, c;
122
123     min = 0;
124     max = sizeof(saxreader_feature_map)/sizeof(struct saxreader_feature_pair) - 1;
125
126     while (min <= max)
127     {
128         n = (min+max)/2;
129
130         c = strcmpW(saxreader_feature_map[n].name, name);
131         if (!c)
132             return saxreader_feature_map[n].feature;
133
134         if (c > 0)
135             max = n-1;
136         else
137             min = n+1;
138     }
139
140     return FeatureUnknown;
141 }
142
143 struct bstrpool
144 {
145     BSTR *pool;
146     unsigned int index;
147     unsigned int len;
148 };
149
150 typedef struct
151 {
152     BSTR prefix;
153     BSTR uri;
154 } ns;
155
156 typedef struct
157 {
158     struct list entry;
159     BSTR prefix;
160     BSTR local;
161     BSTR qname;
162     ns *ns; /* namespaces defined in this particular element */
163     int ns_count;
164 } element_entry;
165
166 enum saxhandler_type
167 {
168     SAXContentHandler = 0,
169     SAXDeclHandler,
170     SAXDTDHandler,
171     SAXErrorHandler,
172     SAXLexicalHandler,
173     SAXHandler_Last
174 };
175
176 struct saxhandler_iface
177 {
178     IUnknown *handler;
179     IUnknown *vbhandler;
180 };
181
182 struct saxcontenthandler_iface
183 {
184     ISAXContentHandler *handler;
185     IVBSAXContentHandler *vbhandler;
186 };
187
188 struct saxerrorhandler_iface
189 {
190     ISAXErrorHandler *handler;
191     IVBSAXErrorHandler *vbhandler;
192 };
193
194 struct saxlexicalhandler_iface
195 {
196     ISAXLexicalHandler *handler;
197     IVBSAXLexicalHandler *vbhandler;
198 };
199
200 typedef struct
201 {
202     DispatchEx dispex;
203     IVBSAXXMLReader IVBSAXXMLReader_iface;
204     ISAXXMLReader ISAXXMLReader_iface;
205     LONG ref;
206
207     struct saxhandler_iface saxhandlers[SAXHandler_Last];
208     xmlSAXHandler sax;
209     BOOL isParsing;
210     struct bstrpool pool;
211     saxreader_feature features;
212     BSTR xmldecl_version;
213     MSXML_VERSION version;
214 } saxreader;
215
216 static HRESULT saxreader_put_handler(saxreader *reader, enum saxhandler_type type, void *ptr, BOOL vb)
217 {
218     struct saxhandler_iface *iface = &reader->saxhandlers[type];
219     IUnknown *unk = (IUnknown*)ptr;
220
221     if (unk)
222         IUnknown_AddRef(unk);
223
224     if ((vb && iface->vbhandler) || (!vb && iface->handler))
225         IUnknown_Release(vb ? iface->vbhandler : iface->handler);
226
227     if (vb)
228         iface->vbhandler = unk;
229     else
230         iface->handler = unk;
231
232     return S_OK;
233 }
234
235 static HRESULT saxreader_get_handler(const saxreader *reader, enum saxhandler_type type, BOOL vb, void **ret)
236 {
237     const struct saxhandler_iface *iface = &reader->saxhandlers[type];
238
239     if (!ret) return E_POINTER;
240
241     if ((vb && iface->vbhandler) || (!vb && iface->handler))
242     {
243         if (vb)
244             IUnknown_AddRef(iface->vbhandler);
245         else
246             IUnknown_AddRef(iface->handler);
247     }
248
249     *ret = vb ? iface->vbhandler : iface->handler;
250
251     return S_OK;
252 }
253
254 static struct saxcontenthandler_iface *saxreader_get_contenthandler(saxreader *reader)
255 {
256     return (struct saxcontenthandler_iface*)&reader->saxhandlers[SAXContentHandler];
257 }
258
259 static struct saxerrorhandler_iface *saxreader_get_errorhandler(saxreader *reader)
260 {
261     return (struct saxerrorhandler_iface*)&reader->saxhandlers[SAXErrorHandler];
262 }
263
264 static struct saxlexicalhandler_iface *saxreader_get_lexicalhandler(saxreader *reader)
265 {
266     return (struct saxlexicalhandler_iface*)&reader->saxhandlers[SAXLexicalHandler];
267 }
268
269 typedef struct
270 {
271     IVBSAXLocator IVBSAXLocator_iface;
272     ISAXLocator ISAXLocator_iface;
273     IVBSAXAttributes IVBSAXAttributes_iface;
274     ISAXAttributes ISAXAttributes_iface;
275     LONG ref;
276     saxreader *saxreader;
277     HRESULT ret;
278     xmlParserCtxtPtr pParserCtxt;
279     WCHAR *publicId;
280     WCHAR *systemId;
281     int line;
282     int column;
283     BOOL vbInterface;
284     struct list elements;
285
286     BSTR namespaceUri;
287     int attributesSize;
288     int nb_attributes;
289     struct _attributes
290     {
291         BSTR szLocalname;
292         BSTR szURI;
293         BSTR szValue;
294         BSTR szQName;
295     } *attributes;
296 } saxlocator;
297
298 static inline saxreader *impl_from_IVBSAXXMLReader( IVBSAXXMLReader *iface )
299 {
300     return CONTAINING_RECORD(iface, saxreader, IVBSAXXMLReader_iface);
301 }
302
303 static inline saxreader *impl_from_ISAXXMLReader( ISAXXMLReader *iface )
304 {
305     return CONTAINING_RECORD(iface, saxreader, ISAXXMLReader_iface);
306 }
307
308 static inline saxlocator *impl_from_IVBSAXLocator( IVBSAXLocator *iface )
309 {
310     return CONTAINING_RECORD(iface, saxlocator, IVBSAXLocator_iface);
311 }
312
313 static inline saxlocator *impl_from_ISAXLocator( ISAXLocator *iface )
314 {
315     return CONTAINING_RECORD(iface, saxlocator, ISAXLocator_iface);
316 }
317
318 static inline saxlocator *impl_from_IVBSAXAttributes( IVBSAXAttributes *iface )
319 {
320     return CONTAINING_RECORD(iface, saxlocator, IVBSAXAttributes_iface);
321 }
322
323 static inline saxlocator *impl_from_ISAXAttributes( ISAXAttributes *iface )
324 {
325     return CONTAINING_RECORD(iface, saxlocator, ISAXAttributes_iface);
326 }
327
328 static inline int saxreader_has_handler(const saxlocator *locator, enum saxhandler_type type)
329 {
330     return (locator->vbInterface && locator->saxreader->saxhandlers[type].vbhandler) ||
331           (!locator->vbInterface && locator->saxreader->saxhandlers[type].handler);
332 }
333
334 /* property names */
335 static const WCHAR PropertyCharsetW[] = {
336     'c','h','a','r','s','e','t',0
337 };
338 static const WCHAR PropertyXmlDeclVersionW[] = {
339     'x','m','l','d','e','c','l','-','v','e','r','s','i','o','n',0
340 };
341 static const WCHAR PropertyDeclHandlerW[] = {
342     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/',
343     's','a','x','/','p','r','o','p','e','r','t','i','e','s','/',
344     'd','e','c','l','a','r','a','t','i','o','n',
345     '-','h','a','n','d','l','e','r',0
346 };
347 static const WCHAR PropertyDomNodeW[] = {
348     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/',
349     's','a','x','/','p','r','o','p','e','r','t','i','e','s','/',
350     'd','o','m','-','n','o','d','e',0
351 };
352 static const WCHAR PropertyInputSourceW[] = {
353     'i','n','p','u','t','-','s','o','u','r','c','e',0
354 };
355 static const WCHAR PropertyLexicalHandlerW[] = {
356     'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/',
357     's','a','x','/','p','r','o','p','e','r','t','i','e','s','/',
358     'l','e','x','i','c','a','l','-','h','a','n','d','l','e','r',0
359 };
360 static const WCHAR PropertyMaxElementDepthW[] = {
361     'm','a','x','-','e','l','e','m','e','n','t','-','d','e','p','t','h',0
362 };
363 static const WCHAR PropertyMaxXMLSizeW[] = {
364     'm','a','x','-','x','m','l','-','s','i','z','e',0
365 };
366 static const WCHAR PropertySchemaDeclHandlerW[] = {
367     's','c','h','e','m','a','-','d','e','c','l','a','r','a','t','i','o','n','-',
368     'h','a','n','d','l','e','r',0
369 };
370 static const WCHAR PropertyXMLDeclEncodingW[] = {
371     'x','m','l','d','e','c','l','-','e','n','c','o','d','i','n','g',0
372 };
373 static const WCHAR PropertyXMLDeclStandaloneW[] = {
374     'x','m','l','d','e','c','l','-','s','t','a','n','d','a','l','o','n','e',0
375 };
376 static const WCHAR PropertyXMLDeclVersionW[] = {
377     'x','m','l','d','e','c','l','-','v','e','r','s','i','o','n',0
378 };
379
380 static inline HRESULT set_feature_value(saxreader *reader, saxreader_feature feature, VARIANT_BOOL value)
381 {
382     /* handling of non-VARIANT_* values is version dependent */
383     if ((reader->version <  MSXML4) && (value != VARIANT_TRUE))
384         value = VARIANT_FALSE;
385     if ((reader->version >= MSXML4) && (value != VARIANT_FALSE))
386         value = VARIANT_TRUE;
387
388     if (value == VARIANT_TRUE)
389         reader->features |=  feature;
390     else
391         reader->features &= ~feature;
392
393     return S_OK;
394 }
395
396 static inline HRESULT get_feature_value(const saxreader *reader, saxreader_feature feature, VARIANT_BOOL *value)
397 {
398     *value = reader->features & feature ? VARIANT_TRUE : VARIANT_FALSE;
399     return S_OK;
400 }
401
402 static BOOL is_namespaces_enabled(const saxreader *reader)
403 {
404     return (reader->version < MSXML4) || (reader->features & Namespaces);
405 }
406
407 static BSTR build_qname(BSTR prefix, BSTR local)
408 {
409     if (prefix && *prefix)
410     {
411         BSTR qname = SysAllocStringLen(NULL, SysStringLen(prefix) + SysStringLen(local) + 1);
412         WCHAR *ptr;
413
414         ptr = qname;
415         strcpyW(ptr, prefix);
416         ptr += SysStringLen(prefix);
417         *ptr++ = ':';
418         strcpyW(ptr, local);
419         return qname;
420     }
421     else
422         return SysAllocString(local);
423 }
424
425 static element_entry* alloc_element_entry(const xmlChar *local, const xmlChar *prefix, int nb_ns,
426     const xmlChar **namespaces)
427 {
428     element_entry *ret;
429     int i;
430
431     ret = heap_alloc(sizeof(*ret));
432     if (!ret) return ret;
433
434     ret->local  = bstr_from_xmlChar(local);
435     ret->prefix = bstr_from_xmlChar(prefix);
436     ret->qname  = build_qname(ret->prefix, ret->local);
437     ret->ns = nb_ns ? heap_alloc(nb_ns*sizeof(ns)) : NULL;
438     ret->ns_count = nb_ns;
439
440     for (i=0; i < nb_ns; i++)
441     {
442         ret->ns[i].prefix = bstr_from_xmlChar(namespaces[2*i]);
443         ret->ns[i].uri = bstr_from_xmlChar(namespaces[2*i+1]);
444     }
445
446     return ret;
447 }
448
449 static void free_element_entry(element_entry *element)
450 {
451     int i;
452
453     for (i=0; i<element->ns_count;i++)
454     {
455         SysFreeString(element->ns[i].prefix);
456         SysFreeString(element->ns[i].uri);
457     }
458
459     SysFreeString(element->prefix);
460     SysFreeString(element->local);
461
462     heap_free(element->ns);
463     heap_free(element);
464 }
465
466 static void push_element_ns(saxlocator *locator, element_entry *element)
467 {
468     list_add_head(&locator->elements, &element->entry);
469 }
470
471 static element_entry * pop_element_ns(saxlocator *locator)
472 {
473     element_entry *element = LIST_ENTRY(list_head(&locator->elements), element_entry, entry);
474
475     if (element)
476         list_remove(&element->entry);
477
478     return element;
479 }
480
481 static BSTR find_element_uri(saxlocator *locator, const xmlChar *uri)
482 {
483     element_entry *element;
484     BSTR uriW;
485     int i;
486
487     if (!uri) return NULL;
488
489     uriW = bstr_from_xmlChar(uri);
490
491     LIST_FOR_EACH_ENTRY(element, &locator->elements, element_entry, entry)
492     {
493         for (i=0; i < element->ns_count; i++)
494             if (!strcmpW(uriW, element->ns[i].uri))
495             {
496                 SysFreeString(uriW);
497                 return element->ns[i].uri;
498             }
499     }
500
501     SysFreeString(uriW);
502     ERR("namespace uri not found, %s\n", debugstr_a((char*)uri));
503     return NULL;
504 }
505
506 /* used to localize version dependent error check behaviour */
507 static inline BOOL sax_callback_failed(saxlocator *This, HRESULT hr)
508 {
509     return This->saxreader->version >= MSXML4 ? FAILED(hr) : hr != S_OK;
510 }
511
512 /* index value -1 means it tries to loop for a first time */
513 static inline BOOL iterate_endprefix_index(saxlocator *This, const element_entry *element, int *i)
514 {
515     if (This->saxreader->version >= MSXML4)
516     {
517         if (*i == -1) *i = 0; else ++*i;
518         return *i < element->ns_count;
519     }
520     else
521     {
522         if (*i == -1) *i = element->ns_count-1; else --*i;
523         return *i >= 0;
524     }
525 }
526
527 static BOOL bstr_pool_insert(struct bstrpool *pool, BSTR pool_entry)
528 {
529     if (!pool->pool)
530     {
531         pool->pool = HeapAlloc(GetProcessHeap(), 0, 16 * sizeof(*pool->pool));
532         if (!pool->pool)
533             return FALSE;
534
535         pool->index = 0;
536         pool->len = 16;
537     }
538     else if (pool->index == pool->len)
539     {
540         BSTR *realloc = HeapReAlloc(GetProcessHeap(), 0, pool->pool, pool->len * 2 * sizeof(*realloc));
541
542         if (!realloc)
543             return FALSE;
544
545         pool->pool = realloc;
546         pool->len *= 2;
547     }
548
549     pool->pool[pool->index++] = pool_entry;
550     return TRUE;
551 }
552
553 static void free_bstr_pool(struct bstrpool *pool)
554 {
555     unsigned int i;
556
557     for (i = 0; i < pool->index; i++)
558         SysFreeString(pool->pool[i]);
559
560     HeapFree(GetProcessHeap(), 0, pool->pool);
561
562     pool->pool = NULL;
563     pool->index = pool->len = 0;
564 }
565
566 static BSTR bstr_from_xmlCharN(const xmlChar *buf, int len)
567 {
568     DWORD dLen;
569     BSTR bstr;
570
571     if (!buf)
572         return NULL;
573
574     dLen = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, len, NULL, 0);
575     if(len != -1) dLen++;
576     bstr = SysAllocStringLen(NULL, dLen-1);
577     if (!bstr)
578         return NULL;
579     MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, len, bstr, dLen);
580     if(len != -1) bstr[dLen-1] = '\0';
581
582     return bstr;
583 }
584
585 static BSTR QName_from_xmlChar(const xmlChar *prefix, const xmlChar *name)
586 {
587     xmlChar *qname;
588     BSTR bstr;
589
590     if(!name) return NULL;
591
592     if(!prefix || !*prefix)
593         return bstr_from_xmlChar(name);
594
595     qname = xmlBuildQName(name, prefix, NULL, 0);
596     bstr = bstr_from_xmlChar(qname);
597     xmlFree(qname);
598
599     return bstr;
600 }
601
602 static BSTR pooled_bstr_from_xmlChar(struct bstrpool *pool, const xmlChar *buf)
603 {
604     BSTR pool_entry = bstr_from_xmlChar(buf);
605
606     if (pool_entry && !bstr_pool_insert(pool, pool_entry))
607     {
608         SysFreeString(pool_entry);
609         return NULL;
610     }
611
612     return pool_entry;
613 }
614
615 static BSTR pooled_bstr_from_xmlCharN(struct bstrpool *pool, const xmlChar *buf, int len)
616 {
617     BSTR pool_entry = bstr_from_xmlCharN(buf, len);
618
619     if (pool_entry && !bstr_pool_insert(pool, pool_entry))
620     {
621         SysFreeString(pool_entry);
622         return NULL;
623     }
624
625     return pool_entry;
626 }
627
628 static void format_error_message_from_id(saxlocator *This, HRESULT hr)
629 {
630     struct saxerrorhandler_iface *handler = saxreader_get_errorhandler(This->saxreader);
631     xmlStopParser(This->pParserCtxt);
632     This->ret = hr;
633
634     if (saxreader_has_handler(This, SAXErrorHandler))
635     {
636         WCHAR msg[1024];
637         if(!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
638                     NULL, hr, 0, msg, sizeof(msg), NULL))
639         {
640             FIXME("MSXML errors not yet supported.\n");
641             msg[0] = '\0';
642         }
643
644         if(This->vbInterface)
645         {
646             BSTR bstrMsg = SysAllocString(msg);
647             IVBSAXErrorHandler_fatalError(handler->vbhandler,
648                     &This->IVBSAXLocator_iface, &bstrMsg, hr);
649             SysFreeString(bstrMsg);
650         }
651         else
652             ISAXErrorHandler_fatalError(handler->handler,
653                     &This->ISAXLocator_iface, msg, hr);
654     }
655 }
656
657 static void update_position(saxlocator *This, BOOL fix_column)
658 {
659     const xmlChar *p = This->pParserCtxt->input->cur-1;
660
661     This->line = xmlSAX2GetLineNumber(This->pParserCtxt);
662     if(fix_column)
663     {
664         This->column = 1;
665         for(; *p!='\n' && *p!='\r' && p>=This->pParserCtxt->input->base; p--)
666             This->column++;
667     }
668     else
669     {
670         This->column = xmlSAX2GetColumnNumber(This->pParserCtxt);
671     }
672 }
673
674 /*** IVBSAXAttributes interface ***/
675 /*** IUnknown methods ***/
676 static HRESULT WINAPI ivbsaxattributes_QueryInterface(
677         IVBSAXAttributes* iface,
678         REFIID riid,
679         void **ppvObject)
680 {
681     saxlocator *This = impl_from_IVBSAXAttributes(iface);
682     TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
683     return IVBSAXLocator_QueryInterface(&This->IVBSAXLocator_iface, riid, ppvObject);
684 }
685
686 static ULONG WINAPI ivbsaxattributes_AddRef(IVBSAXAttributes* iface)
687 {
688     saxlocator *This = impl_from_IVBSAXAttributes(iface);
689     return ISAXLocator_AddRef(&This->ISAXLocator_iface);
690 }
691
692 static ULONG WINAPI ivbsaxattributes_Release(IVBSAXAttributes* iface)
693 {
694     saxlocator *This = impl_from_IVBSAXAttributes(iface);
695     return ISAXLocator_Release(&This->ISAXLocator_iface);
696 }
697
698 /*** IDispatch methods ***/
699 static HRESULT WINAPI ivbsaxattributes_GetTypeInfoCount( IVBSAXAttributes *iface, UINT* pctinfo )
700 {
701     saxlocator *This = impl_from_IVBSAXAttributes( iface );
702
703     TRACE("(%p)->(%p)\n", This, pctinfo);
704
705     *pctinfo = 1;
706
707     return S_OK;
708 }
709
710 static HRESULT WINAPI ivbsaxattributes_GetTypeInfo(
711     IVBSAXAttributes *iface,
712     UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
713 {
714     saxlocator *This = impl_from_IVBSAXAttributes( iface );
715     HRESULT hr;
716
717     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
718
719     hr = get_typeinfo(IVBSAXAttributes_tid, ppTInfo);
720
721     return hr;
722 }
723
724 static HRESULT WINAPI ivbsaxattributes_GetIDsOfNames(
725     IVBSAXAttributes *iface,
726     REFIID riid,
727     LPOLESTR* rgszNames,
728     UINT cNames,
729     LCID lcid,
730     DISPID* rgDispId)
731 {
732     saxlocator *This = impl_from_IVBSAXAttributes( iface );
733     ITypeInfo *typeinfo;
734     HRESULT hr;
735
736     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
737           lcid, rgDispId);
738
739     if(!rgszNames || cNames == 0 || !rgDispId)
740         return E_INVALIDARG;
741
742     hr = get_typeinfo(IVBSAXAttributes_tid, &typeinfo);
743     if(SUCCEEDED(hr))
744     {
745         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
746         ITypeInfo_Release(typeinfo);
747     }
748
749     return hr;
750 }
751
752 static HRESULT WINAPI ivbsaxattributes_Invoke(
753     IVBSAXAttributes *iface,
754     DISPID dispIdMember,
755     REFIID riid,
756     LCID lcid,
757     WORD wFlags,
758     DISPPARAMS* pDispParams,
759     VARIANT* pVarResult,
760     EXCEPINFO* pExcepInfo,
761     UINT* puArgErr)
762 {
763     saxlocator *This = impl_from_IVBSAXAttributes( iface );
764     ITypeInfo *typeinfo;
765     HRESULT hr;
766
767     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
768           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
769
770     hr = get_typeinfo(IVBSAXAttributes_tid, &typeinfo);
771     if(SUCCEEDED(hr))
772     {
773         hr = ITypeInfo_Invoke(typeinfo, &This->IVBSAXAttributes_iface, dispIdMember, wFlags,
774                 pDispParams, pVarResult, pExcepInfo, puArgErr);
775         ITypeInfo_Release(typeinfo);
776     }
777
778     return hr;
779 }
780
781 /*** IVBSAXAttributes methods ***/
782 static HRESULT WINAPI ivbsaxattributes_get_length(
783         IVBSAXAttributes* iface,
784         int *nLength)
785 {
786     saxlocator *This = impl_from_IVBSAXAttributes( iface );
787     return ISAXAttributes_getLength(&This->ISAXAttributes_iface, nLength);
788 }
789
790 static HRESULT WINAPI ivbsaxattributes_getURI(
791         IVBSAXAttributes* iface,
792         int nIndex,
793         BSTR *uri)
794 {
795     int len;
796     saxlocator *This = impl_from_IVBSAXAttributes( iface );
797     return ISAXAttributes_getURI(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)uri, &len);
798 }
799
800 static HRESULT WINAPI ivbsaxattributes_getLocalName(
801         IVBSAXAttributes* iface,
802         int nIndex,
803         BSTR *localName)
804 {
805     int len;
806     saxlocator *This = impl_from_IVBSAXAttributes( iface );
807     return ISAXAttributes_getLocalName(&This->ISAXAttributes_iface, nIndex,
808             (const WCHAR**)localName, &len);
809 }
810
811 static HRESULT WINAPI ivbsaxattributes_getQName(
812         IVBSAXAttributes* iface,
813         int nIndex,
814         BSTR *QName)
815 {
816     int len;
817     saxlocator *This = impl_from_IVBSAXAttributes( iface );
818     return ISAXAttributes_getQName(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)QName, &len);
819 }
820
821 static HRESULT WINAPI ivbsaxattributes_getIndexFromName(
822         IVBSAXAttributes* iface,
823         BSTR uri,
824         BSTR localName,
825         int *index)
826 {
827     saxlocator *This = impl_from_IVBSAXAttributes( iface );
828     return ISAXAttributes_getIndexFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
829             localName, SysStringLen(localName), index);
830 }
831
832 static HRESULT WINAPI ivbsaxattributes_getIndexFromQName(
833         IVBSAXAttributes* iface,
834         BSTR QName,
835         int *index)
836 {
837     saxlocator *This = impl_from_IVBSAXAttributes( iface );
838     return ISAXAttributes_getIndexFromQName(&This->ISAXAttributes_iface, QName,
839             SysStringLen(QName), index);
840 }
841
842 static HRESULT WINAPI ivbsaxattributes_getType(
843         IVBSAXAttributes* iface,
844         int nIndex,
845         BSTR *type)
846 {
847     int len;
848     saxlocator *This = impl_from_IVBSAXAttributes( iface );
849     return ISAXAttributes_getType(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)type, &len);
850 }
851
852 static HRESULT WINAPI ivbsaxattributes_getTypeFromName(
853         IVBSAXAttributes* iface,
854         BSTR uri,
855         BSTR localName,
856         BSTR *type)
857 {
858     int len;
859     saxlocator *This = impl_from_IVBSAXAttributes( iface );
860     return ISAXAttributes_getTypeFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
861             localName, SysStringLen(localName), (const WCHAR**)type, &len);
862 }
863
864 static HRESULT WINAPI ivbsaxattributes_getTypeFromQName(
865         IVBSAXAttributes* iface,
866         BSTR QName,
867         BSTR *type)
868 {
869     int len;
870     saxlocator *This = impl_from_IVBSAXAttributes( iface );
871     return ISAXAttributes_getTypeFromQName(&This->ISAXAttributes_iface, QName, SysStringLen(QName),
872             (const WCHAR**)type, &len);
873 }
874
875 static HRESULT WINAPI ivbsaxattributes_getValue(
876         IVBSAXAttributes* iface,
877         int nIndex,
878         BSTR *value)
879 {
880     int len;
881     saxlocator *This = impl_from_IVBSAXAttributes( iface );
882     return ISAXAttributes_getValue(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)value, &len);
883 }
884
885 static HRESULT WINAPI ivbsaxattributes_getValueFromName(
886         IVBSAXAttributes* iface,
887         BSTR uri,
888         BSTR localName,
889         BSTR *value)
890 {
891     int len;
892     saxlocator *This = impl_from_IVBSAXAttributes( iface );
893     return ISAXAttributes_getValueFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
894             localName, SysStringLen(localName), (const WCHAR**)value, &len);
895 }
896
897 static HRESULT WINAPI ivbsaxattributes_getValueFromQName(
898         IVBSAXAttributes* iface,
899         BSTR QName,
900         BSTR *value)
901 {
902     int len;
903     saxlocator *This = impl_from_IVBSAXAttributes( iface );
904     return ISAXAttributes_getValueFromQName(&This->ISAXAttributes_iface, QName,
905             SysStringLen(QName), (const WCHAR**)value, &len);
906 }
907
908 static const struct IVBSAXAttributesVtbl ivbsaxattributes_vtbl =
909 {
910     ivbsaxattributes_QueryInterface,
911     ivbsaxattributes_AddRef,
912     ivbsaxattributes_Release,
913     ivbsaxattributes_GetTypeInfoCount,
914     ivbsaxattributes_GetTypeInfo,
915     ivbsaxattributes_GetIDsOfNames,
916     ivbsaxattributes_Invoke,
917     ivbsaxattributes_get_length,
918     ivbsaxattributes_getURI,
919     ivbsaxattributes_getLocalName,
920     ivbsaxattributes_getQName,
921     ivbsaxattributes_getIndexFromName,
922     ivbsaxattributes_getIndexFromQName,
923     ivbsaxattributes_getType,
924     ivbsaxattributes_getTypeFromName,
925     ivbsaxattributes_getTypeFromQName,
926     ivbsaxattributes_getValue,
927     ivbsaxattributes_getValueFromName,
928     ivbsaxattributes_getValueFromQName
929 };
930
931 /*** ISAXAttributes interface ***/
932 /*** IUnknown methods ***/
933 static HRESULT WINAPI isaxattributes_QueryInterface(
934         ISAXAttributes* iface,
935         REFIID riid,
936         void **ppvObject)
937 {
938     saxlocator *This = impl_from_ISAXAttributes(iface);
939     TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
940     return ISAXLocator_QueryInterface(&This->ISAXLocator_iface, riid, ppvObject);
941 }
942
943 static ULONG WINAPI isaxattributes_AddRef(ISAXAttributes* iface)
944 {
945     saxlocator *This = impl_from_ISAXAttributes(iface);
946     TRACE("%p\n", This);
947     return ISAXLocator_AddRef(&This->ISAXLocator_iface);
948 }
949
950 static ULONG WINAPI isaxattributes_Release(ISAXAttributes* iface)
951 {
952     saxlocator *This = impl_from_ISAXAttributes(iface);
953
954     TRACE("%p\n", This);
955     return ISAXLocator_Release(&This->ISAXLocator_iface);
956 }
957
958 /*** ISAXAttributes methods ***/
959 static HRESULT WINAPI isaxattributes_getLength(
960         ISAXAttributes* iface,
961         int *length)
962 {
963     saxlocator *This = impl_from_ISAXAttributes( iface );
964
965     *length = This->nb_attributes;
966     TRACE("Length set to %d\n", *length);
967     return S_OK;
968 }
969
970 static HRESULT WINAPI isaxattributes_getURI(
971         ISAXAttributes* iface,
972         int index,
973         const WCHAR **url,
974         int *size)
975 {
976     saxlocator *This = impl_from_ISAXAttributes( iface );
977     TRACE("(%p)->(%d)\n", This, index);
978
979     if(index >= This->nb_attributes || index < 0) return E_INVALIDARG;
980     if(!url || !size) return E_POINTER;
981
982     *size = SysStringLen(This->attributes[index].szURI);
983     *url = This->attributes[index].szURI;
984
985     TRACE("(%s:%d)\n", debugstr_w(This->attributes[index].szURI), *size);
986
987     return S_OK;
988 }
989
990 static HRESULT WINAPI isaxattributes_getLocalName(
991         ISAXAttributes* iface,
992         int nIndex,
993         const WCHAR **pLocalName,
994         int *pLocalNameLength)
995 {
996     saxlocator *This = impl_from_ISAXAttributes( iface );
997     TRACE("(%p)->(%d)\n", This, nIndex);
998
999     if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
1000     if(!pLocalName || !pLocalNameLength) return E_POINTER;
1001
1002     *pLocalNameLength = SysStringLen(This->attributes[nIndex].szLocalname);
1003     *pLocalName = This->attributes[nIndex].szLocalname;
1004
1005     return S_OK;
1006 }
1007
1008 static HRESULT WINAPI isaxattributes_getQName(
1009         ISAXAttributes* iface,
1010         int nIndex,
1011         const WCHAR **pQName,
1012         int *pQNameLength)
1013 {
1014     saxlocator *This = impl_from_ISAXAttributes( iface );
1015     TRACE("(%p)->(%d)\n", This, nIndex);
1016
1017     if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
1018     if(!pQName || !pQNameLength) return E_POINTER;
1019
1020     *pQNameLength = SysStringLen(This->attributes[nIndex].szQName);
1021     *pQName = This->attributes[nIndex].szQName;
1022
1023     return S_OK;
1024 }
1025
1026 static HRESULT WINAPI isaxattributes_getName(
1027         ISAXAttributes* iface,
1028         int index,
1029         const WCHAR **uri,
1030         int *pUriLength,
1031         const WCHAR **localName,
1032         int *pLocalNameSize,
1033         const WCHAR **QName,
1034         int *pQNameLength)
1035 {
1036     saxlocator *This = impl_from_ISAXAttributes( iface );
1037     TRACE("(%p)->(%d)\n", This, index);
1038
1039     if(index>=This->nb_attributes || index<0) return E_INVALIDARG;
1040     if(!uri || !pUriLength || !localName || !pLocalNameSize
1041             || !QName || !pQNameLength) return E_POINTER;
1042
1043     *pUriLength = SysStringLen(This->attributes[index].szURI);
1044     *uri = This->attributes[index].szURI;
1045     *pLocalNameSize = SysStringLen(This->attributes[index].szLocalname);
1046     *localName = This->attributes[index].szLocalname;
1047     *pQNameLength = SysStringLen(This->attributes[index].szQName);
1048     *QName = This->attributes[index].szQName;
1049
1050     TRACE("(%s, %s, %s)\n", debugstr_w(*uri), debugstr_w(*localName), debugstr_w(*QName));
1051
1052     return S_OK;
1053 }
1054
1055 static HRESULT WINAPI isaxattributes_getIndexFromName(
1056         ISAXAttributes* iface,
1057         const WCHAR *pUri,
1058         int cUriLength,
1059         const WCHAR *pLocalName,
1060         int cocalNameLength,
1061         int *index)
1062 {
1063     saxlocator *This = impl_from_ISAXAttributes( iface );
1064     int i;
1065     TRACE("(%p)->(%s, %d, %s, %d)\n", This, debugstr_w(pUri), cUriLength,
1066             debugstr_w(pLocalName), cocalNameLength);
1067
1068     if(!pUri || !pLocalName || !index) return E_POINTER;
1069
1070     for(i=0; i<This->nb_attributes; i++)
1071     {
1072         if(cUriLength!=SysStringLen(This->attributes[i].szURI)
1073                 || cocalNameLength!=SysStringLen(This->attributes[i].szLocalname))
1074             continue;
1075         if(cUriLength && memcmp(pUri, This->attributes[i].szURI,
1076                     sizeof(WCHAR)*cUriLength))
1077             continue;
1078         if(cocalNameLength && memcmp(pLocalName, This->attributes[i].szLocalname,
1079                     sizeof(WCHAR)*cocalNameLength))
1080             continue;
1081
1082         *index = i;
1083         return S_OK;
1084     }
1085
1086     return E_INVALIDARG;
1087 }
1088
1089 static HRESULT WINAPI isaxattributes_getIndexFromQName(
1090         ISAXAttributes* iface,
1091         const WCHAR *pQName,
1092         int nQNameLength,
1093         int *index)
1094 {
1095     saxlocator *This = impl_from_ISAXAttributes( iface );
1096     int i;
1097     TRACE("(%p)->(%s, %d)\n", This, debugstr_w(pQName), nQNameLength);
1098
1099     if(!pQName || !index) return E_POINTER;
1100     if(!nQNameLength) return E_INVALIDARG;
1101
1102     for(i=0; i<This->nb_attributes; i++)
1103     {
1104         if(nQNameLength!=SysStringLen(This->attributes[i].szQName)) continue;
1105         if(memcmp(pQName, This->attributes[i].szQName, sizeof(WCHAR)*nQNameLength)) continue;
1106
1107         *index = i;
1108         return S_OK;
1109     }
1110
1111     return E_INVALIDARG;
1112 }
1113
1114 static HRESULT WINAPI isaxattributes_getType(
1115         ISAXAttributes* iface,
1116         int nIndex,
1117         const WCHAR **pType,
1118         int *pTypeLength)
1119 {
1120     saxlocator *This = impl_from_ISAXAttributes( iface );
1121
1122     FIXME("(%p)->(%d) stub\n", This, nIndex);
1123     return E_NOTIMPL;
1124 }
1125
1126 static HRESULT WINAPI isaxattributes_getTypeFromName(
1127         ISAXAttributes* iface,
1128         const WCHAR *pUri,
1129         int nUri,
1130         const WCHAR *pLocalName,
1131         int nLocalName,
1132         const WCHAR **pType,
1133         int *nType)
1134 {
1135     saxlocator *This = impl_from_ISAXAttributes( iface );
1136
1137     FIXME("(%p)->(%s, %d, %s, %d) stub\n", This, debugstr_w(pUri), nUri,
1138             debugstr_w(pLocalName), nLocalName);
1139     return E_NOTIMPL;
1140 }
1141
1142 static HRESULT WINAPI isaxattributes_getTypeFromQName(
1143         ISAXAttributes* iface,
1144         const WCHAR *pQName,
1145         int nQName,
1146         const WCHAR **pType,
1147         int *nType)
1148 {
1149     saxlocator *This = impl_from_ISAXAttributes( iface );
1150
1151     FIXME("(%p)->(%s, %d) stub\n", This, debugstr_w(pQName), nQName);
1152     return E_NOTIMPL;
1153 }
1154
1155 static HRESULT WINAPI isaxattributes_getValue(
1156         ISAXAttributes* iface,
1157         int index,
1158         const WCHAR **value,
1159         int *nValue)
1160 {
1161     saxlocator *This = impl_from_ISAXAttributes( iface );
1162     TRACE("(%p)->(%d)\n", This, index);
1163
1164     if(index>=This->nb_attributes || index<0) return E_INVALIDARG;
1165     if(!value || !nValue) return E_POINTER;
1166
1167     *nValue = SysStringLen(This->attributes[index].szValue);
1168     *value = This->attributes[index].szValue;
1169
1170     TRACE("(%s:%d)\n", debugstr_w(*value), *nValue);
1171
1172     return S_OK;
1173 }
1174
1175 static HRESULT WINAPI isaxattributes_getValueFromName(
1176         ISAXAttributes* iface,
1177         const WCHAR *pUri,
1178         int nUri,
1179         const WCHAR *pLocalName,
1180         int nLocalName,
1181         const WCHAR **pValue,
1182         int *nValue)
1183 {
1184     HRESULT hr;
1185     int index;
1186     saxlocator *This = impl_from_ISAXAttributes( iface );
1187     TRACE("(%p)->(%s, %d, %s, %d)\n", This, debugstr_w(pUri), nUri,
1188             debugstr_w(pLocalName), nLocalName);
1189
1190     hr = ISAXAttributes_getIndexFromName(iface,
1191             pUri, nUri, pLocalName, nLocalName, &index);
1192     if(hr==S_OK) hr = ISAXAttributes_getValue(iface, index, pValue, nValue);
1193
1194     return hr;
1195 }
1196
1197 static HRESULT WINAPI isaxattributes_getValueFromQName(
1198         ISAXAttributes* iface,
1199         const WCHAR *pQName,
1200         int nQName,
1201         const WCHAR **pValue,
1202         int *nValue)
1203 {
1204     HRESULT hr;
1205     int index;
1206     saxlocator *This = impl_from_ISAXAttributes( iface );
1207     TRACE("(%p)->(%s, %d)\n", This, debugstr_w(pQName), nQName);
1208
1209     hr = ISAXAttributes_getIndexFromQName(iface, pQName, nQName, &index);
1210     if(hr==S_OK) hr = ISAXAttributes_getValue(iface, index, pValue, nValue);
1211
1212     return hr;
1213 }
1214
1215 static const struct ISAXAttributesVtbl isaxattributes_vtbl =
1216 {
1217     isaxattributes_QueryInterface,
1218     isaxattributes_AddRef,
1219     isaxattributes_Release,
1220     isaxattributes_getLength,
1221     isaxattributes_getURI,
1222     isaxattributes_getLocalName,
1223     isaxattributes_getQName,
1224     isaxattributes_getName,
1225     isaxattributes_getIndexFromName,
1226     isaxattributes_getIndexFromQName,
1227     isaxattributes_getType,
1228     isaxattributes_getTypeFromName,
1229     isaxattributes_getTypeFromQName,
1230     isaxattributes_getValue,
1231     isaxattributes_getValueFromName,
1232     isaxattributes_getValueFromQName
1233 };
1234
1235 static HRESULT SAXAttributes_populate(saxlocator *locator,
1236         int nb_namespaces, const xmlChar **xmlNamespaces,
1237         int nb_attributes, const xmlChar **xmlAttributes)
1238 {
1239     static const xmlChar xmlns[] = "xmlns";
1240     static const WCHAR xmlnsW[] = { 'x','m','l','n','s',0 };
1241
1242     struct _attributes *attrs;
1243     int i;
1244
1245     /* skip namespace definitions */
1246     if ((locator->saxreader->features & NamespacePrefixes) == 0)
1247         nb_namespaces = 0;
1248
1249     locator->nb_attributes = nb_namespaces + nb_attributes;
1250     if(locator->nb_attributes > locator->attributesSize)
1251     {
1252         attrs = heap_realloc(locator->attributes, sizeof(struct _attributes)*locator->nb_attributes*2);
1253         if(!attrs)
1254         {
1255             locator->nb_attributes = 0;
1256             return E_OUTOFMEMORY;
1257         }
1258         locator->attributes = attrs;
1259     }
1260     else
1261     {
1262         attrs = locator->attributes;
1263     }
1264
1265     for (i = 0; i < nb_namespaces; i++)
1266     {
1267         attrs[nb_attributes+i].szLocalname = SysAllocStringLen(NULL, 0);
1268         attrs[nb_attributes+i].szURI = locator->namespaceUri;
1269         attrs[nb_attributes+i].szValue = bstr_from_xmlChar(xmlNamespaces[2*i+1]);
1270         if(!xmlNamespaces[2*i])
1271             attrs[nb_attributes+i].szQName = SysAllocString(xmlnsW);
1272         else
1273             attrs[nb_attributes+i].szQName = QName_from_xmlChar(xmlns, xmlNamespaces[2*i]);
1274     }
1275
1276     for (i = 0; i < nb_attributes; i++)
1277     {
1278         static const xmlChar xmlA[] = "xml";
1279
1280         if (xmlStrEqual(xmlAttributes[i*5+1], xmlA))
1281             attrs[i].szURI = bstr_from_xmlChar(xmlAttributes[i*5+2]);
1282         else
1283             /* that's an important feature to keep same uri pointer for every reported attribute */
1284             attrs[i].szURI = find_element_uri(locator, xmlAttributes[i*5+2]);
1285
1286         attrs[i].szLocalname = bstr_from_xmlChar(xmlAttributes[i*5]);
1287         attrs[i].szValue = bstr_from_xmlCharN(xmlAttributes[i*5+3],
1288                 xmlAttributes[i*5+4]-xmlAttributes[i*5+3]);
1289         attrs[i].szQName = QName_from_xmlChar(xmlAttributes[i*5+1],
1290                 xmlAttributes[i*5]);
1291     }
1292
1293     return S_OK;
1294 }
1295
1296 /*** LibXML callbacks ***/
1297 static void libxmlStartDocument(void *ctx)
1298 {
1299     saxlocator *This = ctx;
1300     struct saxcontenthandler_iface *handler = saxreader_get_contenthandler(This->saxreader);
1301     HRESULT hr;
1302
1303     if (This->saxreader->version >= MSXML4)
1304     {
1305         const xmlChar *p = This->pParserCtxt->input->cur-1;
1306         update_position(This, FALSE);
1307         while(p>This->pParserCtxt->input->base && *p!='>')
1308         {
1309             if(*p=='\n' || (*p=='\r' && *(p+1)!='\n'))
1310                 This->line--;
1311             p--;
1312         }
1313         This->column = 0;
1314         for(; p>=This->pParserCtxt->input->base && *p!='\n' && *p!='\r'; p--)
1315             This->column++;
1316     }
1317
1318     /* store version value, declaration has to contain version attribute */
1319     if (This->pParserCtxt->standalone != -1)
1320     {
1321         SysFreeString(This->saxreader->xmldecl_version);
1322         This->saxreader->xmldecl_version = bstr_from_xmlChar(This->pParserCtxt->version);
1323     }
1324
1325     if (saxreader_has_handler(This, SAXContentHandler))
1326     {
1327         if(This->vbInterface)
1328             hr = IVBSAXContentHandler_startDocument(handler->vbhandler);
1329         else
1330             hr = ISAXContentHandler_startDocument(handler->handler);
1331
1332         if (sax_callback_failed(This, hr))
1333             format_error_message_from_id(This, hr);
1334     }
1335 }
1336
1337 static void libxmlEndDocument(void *ctx)
1338 {
1339     saxlocator *This = ctx;
1340     struct saxcontenthandler_iface *handler = saxreader_get_contenthandler(This->saxreader);
1341     HRESULT hr;
1342
1343     if (This->saxreader->version >= MSXML4) {
1344         update_position(This, FALSE);
1345         if(This->column > 1)
1346             This->line++;
1347         This->column = 0;
1348     } else {
1349         This->column = 0;
1350         This->line = 0;
1351     }
1352
1353     if(This->ret != S_OK) return;
1354
1355     if (saxreader_has_handler(This, SAXContentHandler))
1356     {
1357         if(This->vbInterface)
1358             hr = IVBSAXContentHandler_endDocument(handler->vbhandler);
1359         else
1360             hr = ISAXContentHandler_endDocument(handler->handler);
1361
1362         if (sax_callback_failed(This, hr))
1363             format_error_message_from_id(This, hr);
1364     }
1365 }
1366
1367 static void libxmlStartElementNS(
1368         void *ctx,
1369         const xmlChar *localname,
1370         const xmlChar *prefix,
1371         const xmlChar *URI,
1372         int nb_namespaces,
1373         const xmlChar **namespaces,
1374         int nb_attributes,
1375         int nb_defaulted,
1376         const xmlChar **attributes)
1377 {
1378     saxlocator *This = ctx;
1379     struct saxcontenthandler_iface *handler = saxreader_get_contenthandler(This->saxreader);
1380     element_entry *element;
1381     HRESULT hr = S_OK;
1382     BSTR uri;
1383
1384     update_position(This, TRUE);
1385     if(*(This->pParserCtxt->input->cur) == '/')
1386         This->column++;
1387     if(This->saxreader->version < MSXML4)
1388         This->column++;
1389
1390     element = alloc_element_entry(localname, prefix, nb_namespaces, namespaces);
1391     push_element_ns(This, element);
1392
1393     if (is_namespaces_enabled(This->saxreader))
1394     {
1395         int i;
1396
1397         for (i = 0; i < nb_namespaces && saxreader_has_handler(This, SAXContentHandler); i++)
1398         {
1399             if (This->vbInterface)
1400                 hr = IVBSAXContentHandler_startPrefixMapping(
1401                         handler->vbhandler,
1402                         &element->ns[i].prefix,
1403                         &element->ns[i].uri);
1404             else
1405                 hr = ISAXContentHandler_startPrefixMapping(
1406                         handler->handler,
1407                         element->ns[i].prefix,
1408                         SysStringLen(element->ns[i].prefix),
1409                         element->ns[i].uri,
1410                         SysStringLen(element->ns[i].uri));
1411
1412             if (sax_callback_failed(This, hr))
1413             {
1414                 format_error_message_from_id(This, hr);
1415                 return;
1416             }
1417         }
1418     }
1419
1420     uri = find_element_uri(This, URI);
1421     hr = SAXAttributes_populate(This, nb_namespaces, namespaces, nb_attributes, attributes);
1422     if (hr == S_OK && saxreader_has_handler(This, SAXContentHandler))
1423     {
1424         BSTR local;
1425
1426         if (is_namespaces_enabled(This->saxreader))
1427             local = element->local;
1428         else
1429             uri = local = NULL;
1430
1431         if (This->vbInterface)
1432             hr = IVBSAXContentHandler_startElement(handler->vbhandler,
1433                     &uri, &local, &element->qname, &This->IVBSAXAttributes_iface);
1434         else
1435             hr = ISAXContentHandler_startElement(handler->handler,
1436                     uri, SysStringLen(uri),
1437                     local, SysStringLen(local),
1438                     element->qname, SysStringLen(element->qname),
1439                     &This->ISAXAttributes_iface);
1440
1441        if (sax_callback_failed(This, hr))
1442            format_error_message_from_id(This, hr);
1443     }
1444 }
1445
1446 static void libxmlEndElementNS(
1447         void *ctx,
1448         const xmlChar *localname,
1449         const xmlChar *prefix,
1450         const xmlChar *URI)
1451 {
1452     saxlocator *This = ctx;
1453     struct saxcontenthandler_iface *handler = saxreader_get_contenthandler(This->saxreader);
1454     element_entry *element;
1455     const xmlChar *p;
1456     BSTR uri, local;
1457     HRESULT hr;
1458
1459     update_position(This, FALSE);
1460     p = This->pParserCtxt->input->cur;
1461
1462     if (This->saxreader->version >= MSXML4)
1463     {
1464         p--;
1465         while(p>This->pParserCtxt->input->base && *p!='>')
1466         {
1467             if(*p=='\n' || (*p=='\r' && *(p+1)!='\n'))
1468                 This->line--;
1469             p--;
1470         }
1471     }
1472     else if(*(p-1)!='>' || *(p-2)!='/')
1473     {
1474         p--;
1475         while(p-2>=This->pParserCtxt->input->base
1476                 && *(p-2)!='<' && *(p-1)!='/')
1477         {
1478             if(*p=='\n' || (*p=='\r' && *(p+1)!='\n'))
1479                 This->line--;
1480             p--;
1481         }
1482     }
1483     This->column = 0;
1484     for(; p>=This->pParserCtxt->input->base && *p!='\n' && *p!='\r'; p--)
1485         This->column++;
1486
1487     uri = find_element_uri(This, URI);
1488     element = pop_element_ns(This);
1489
1490     if (!saxreader_has_handler(This, SAXContentHandler))
1491     {
1492         This->nb_attributes = 0;
1493         free_element_entry(element);
1494         return;
1495     }
1496
1497     if (is_namespaces_enabled(This->saxreader))
1498         local = element->local;
1499     else
1500         uri = local = NULL;
1501
1502     if (This->vbInterface)
1503         hr = IVBSAXContentHandler_endElement(
1504                 handler->vbhandler,
1505                 &uri, &local, &element->qname);
1506     else
1507         hr = ISAXContentHandler_endElement(
1508                 handler->handler,
1509                 uri, SysStringLen(uri),
1510                 local, SysStringLen(local),
1511                 element->qname, SysStringLen(element->qname));
1512
1513     This->nb_attributes = 0;
1514
1515     if (sax_callback_failed(This, hr))
1516     {
1517         format_error_message_from_id(This, hr);
1518         free_element_entry(element);
1519         return;
1520     }
1521
1522     if (is_namespaces_enabled(This->saxreader))
1523     {
1524         int i = -1;
1525         while (iterate_endprefix_index(This, element, &i) && saxreader_has_handler(This, SAXContentHandler))
1526         {
1527             if (This->vbInterface)
1528                 hr = IVBSAXContentHandler_endPrefixMapping(
1529                         handler->vbhandler, &element->ns[i].prefix);
1530             else
1531                 hr = ISAXContentHandler_endPrefixMapping(
1532                         handler->handler, element->ns[i].prefix, SysStringLen(element->ns[i].prefix));
1533
1534             if (sax_callback_failed(This, hr)) break;
1535        }
1536
1537        if (sax_callback_failed(This, hr))
1538            format_error_message_from_id(This, hr);
1539     }
1540
1541     free_element_entry(element);
1542 }
1543
1544 static void libxmlCharacters(
1545         void *ctx,
1546         const xmlChar *ch,
1547         int len)
1548 {
1549     saxlocator *This = ctx;
1550     struct saxcontenthandler_iface *handler = saxreader_get_contenthandler(This->saxreader);
1551     BSTR Chars;
1552     HRESULT hr;
1553     xmlChar *cur, *end;
1554     BOOL lastEvent = FALSE;
1555
1556     if (!saxreader_has_handler(This, SAXContentHandler)) return;
1557
1558     update_position(This, FALSE);
1559     cur = (xmlChar*)This->pParserCtxt->input->cur;
1560     while(cur>=This->pParserCtxt->input->base && *cur!='>')
1561     {
1562         if(*cur=='\n' || (*cur=='\r' && *(cur+1)!='\n'))
1563             This->line--;
1564         cur--;
1565     }
1566     This->column = 1;
1567     for(; cur>=This->pParserCtxt->input->base && *cur!='\n' && *cur!='\r'; cur--)
1568         This->column++;
1569
1570     cur = (xmlChar*)ch;
1571     if(*(ch-1)=='\r') cur--;
1572     end = cur;
1573
1574     while(1)
1575     {
1576         while(end-ch<len && *end!='\r') end++;
1577         if(end-ch==len)
1578         {
1579             lastEvent = TRUE;
1580         }
1581         else
1582         {
1583             *end = '\n';
1584             end++;
1585         }
1586
1587         if (This->saxreader->version >= MSXML4)
1588         {
1589             xmlChar *p;
1590
1591             for(p=cur; p!=end; p++)
1592             {
1593                 if(*p=='\n')
1594                 {
1595                     This->line++;
1596                     This->column = 1;
1597                 }
1598                 else
1599                 {
1600                     This->column++;
1601                 }
1602             }
1603
1604             if(!lastEvent)
1605                 This->column = 0;
1606         }
1607
1608         Chars = pooled_bstr_from_xmlCharN(&This->saxreader->pool, cur, end-cur);
1609         if(This->vbInterface)
1610             hr = IVBSAXContentHandler_characters(handler->vbhandler, &Chars);
1611         else
1612             hr = ISAXContentHandler_characters(handler->handler, Chars, SysStringLen(Chars));
1613
1614         if (sax_callback_failed(This, hr))
1615         {
1616             format_error_message_from_id(This, hr);
1617             return;
1618         }
1619
1620         if (This->saxreader->version < MSXML4)
1621             This->column += end-cur;
1622
1623         if(lastEvent)
1624             break;
1625
1626         *(end-1) = '\r';
1627         if(*end == '\n')
1628         {
1629             end++;
1630             This->column++;
1631         }
1632         cur = end;
1633
1634         if(end-ch == len) break;
1635     }
1636 }
1637
1638 static void libxmlSetDocumentLocator(
1639         void *ctx,
1640         xmlSAXLocatorPtr loc)
1641 {
1642     saxlocator *This = ctx;
1643     struct saxcontenthandler_iface *handler = saxreader_get_contenthandler(This->saxreader);
1644     HRESULT hr = S_OK;
1645
1646     if (saxreader_has_handler(This, SAXContentHandler))
1647     {
1648         if(This->vbInterface)
1649             hr = IVBSAXContentHandler_putref_documentLocator(handler->vbhandler,
1650                     &This->IVBSAXLocator_iface);
1651         else
1652             hr = ISAXContentHandler_putDocumentLocator(handler->handler, &This->ISAXLocator_iface);
1653     }
1654
1655     if(FAILED(hr))
1656         format_error_message_from_id(This, hr);
1657 }
1658
1659 static void libxmlComment(void *ctx, const xmlChar *value)
1660 {
1661     saxlocator *This = ctx;
1662     struct saxlexicalhandler_iface *handler = saxreader_get_lexicalhandler(This->saxreader);
1663     BSTR bValue;
1664     HRESULT hr;
1665     const xmlChar *p = This->pParserCtxt->input->cur;
1666
1667     update_position(This, FALSE);
1668     while(p-4>=This->pParserCtxt->input->base
1669             && memcmp(p-4, "<!--", sizeof(char[4])))
1670     {
1671         if(*p=='\n' || (*p=='\r' && *(p+1)!='\n'))
1672             This->line--;
1673         p--;
1674     }
1675
1676     This->column = 0;
1677     for(; p>=This->pParserCtxt->input->base && *p!='\n' && *p!='\r'; p--)
1678         This->column++;
1679
1680     if (!saxreader_has_handler(This, SAXLexicalHandler)) return;
1681
1682     bValue = pooled_bstr_from_xmlChar(&This->saxreader->pool, value);
1683
1684     if (This->vbInterface)
1685         hr = IVBSAXLexicalHandler_comment(handler->vbhandler, &bValue);
1686     else
1687         hr = ISAXLexicalHandler_comment(handler->handler, bValue, SysStringLen(bValue));
1688
1689     if(FAILED(hr))
1690         format_error_message_from_id(This, hr);
1691 }
1692
1693 static void libxmlFatalError(void *ctx, const char *msg, ...)
1694 {
1695     saxlocator *This = ctx;
1696     struct saxerrorhandler_iface *handler = saxreader_get_errorhandler(This->saxreader);
1697     char message[1024];
1698     WCHAR *error;
1699     DWORD len;
1700     va_list args;
1701
1702     if(This->ret != S_OK) {
1703         xmlStopParser(This->pParserCtxt);
1704         return;
1705     }
1706
1707     va_start(args, msg);
1708     vsprintf(message, msg, args);
1709     va_end(args);
1710
1711     len = MultiByteToWideChar(CP_UNIXCP, 0, message, -1, NULL, 0);
1712     error = heap_alloc(sizeof(WCHAR)*len);
1713     if(error)
1714     {
1715         MultiByteToWideChar(CP_UNIXCP, 0, message, -1, error, len);
1716         TRACE("fatal error for %p: %s\n", This, debugstr_w(error));
1717     }
1718
1719     if (!saxreader_has_handler(This, SAXErrorHandler))
1720     {
1721         xmlStopParser(This->pParserCtxt);
1722         This->ret = E_FAIL;
1723         heap_free(error);
1724         return;
1725     }
1726
1727     FIXME("Error handling is not compatible.\n");
1728
1729     if(This->vbInterface)
1730     {
1731         BSTR bstrError = SysAllocString(error);
1732         IVBSAXErrorHandler_fatalError(handler->vbhandler, &This->IVBSAXLocator_iface,
1733                 &bstrError, E_FAIL);
1734         SysFreeString(bstrError);
1735     }
1736     else
1737         ISAXErrorHandler_fatalError(handler->handler, &This->ISAXLocator_iface, error, E_FAIL);
1738
1739     heap_free(error);
1740
1741     xmlStopParser(This->pParserCtxt);
1742     This->ret = E_FAIL;
1743 }
1744
1745 static void libxmlCDataBlock(void *ctx, const xmlChar *value, int len)
1746 {
1747     saxlocator *This = ctx;
1748     struct saxcontenthandler_iface *content = saxreader_get_contenthandler(This->saxreader);
1749     struct saxlexicalhandler_iface *lexical = saxreader_get_lexicalhandler(This->saxreader);
1750     HRESULT hr = S_OK;
1751     xmlChar *beg = (xmlChar*)This->pParserCtxt->input->cur-len;
1752     xmlChar *cur, *end;
1753     int realLen;
1754     BSTR Chars;
1755     BOOL lastEvent = FALSE, change;
1756
1757     update_position(This, FALSE);
1758     while(beg-9>=This->pParserCtxt->input->base
1759             && memcmp(beg-9, "<![CDATA[", sizeof(char[9])))
1760     {
1761         if(*beg=='\n' || (*beg=='\r' && *(beg+1)!='\n'))
1762             This->line--;
1763         beg--;
1764     }
1765     This->column = 0;
1766     for(; beg>=This->pParserCtxt->input->base && *beg!='\n' && *beg!='\r'; beg--)
1767         This->column++;
1768
1769     if (saxreader_has_handler(This, SAXLexicalHandler))
1770     {
1771        if (This->vbInterface)
1772            hr = IVBSAXLexicalHandler_startCDATA(lexical->vbhandler);
1773        else
1774            hr = ISAXLexicalHandler_startCDATA(lexical->handler);
1775     }
1776
1777     if(FAILED(hr))
1778     {
1779         format_error_message_from_id(This, hr);
1780         return;
1781     }
1782
1783     realLen = This->pParserCtxt->input->cur-beg-3;
1784     cur = beg;
1785     end = beg;
1786
1787     while(1)
1788     {
1789         while(end-beg<realLen && *end!='\r') end++;
1790         if(end-beg==realLen)
1791         {
1792             end--;
1793             lastEvent = TRUE;
1794         }
1795         else if(end-beg==realLen-1 && *end=='\r' && *(end+1)=='\n')
1796             lastEvent = TRUE;
1797
1798         if(*end == '\r') change = TRUE;
1799         else change = FALSE;
1800
1801         if(change) *end = '\n';
1802
1803         if (saxreader_has_handler(This, SAXContentHandler))
1804         {
1805             Chars = pooled_bstr_from_xmlCharN(&This->saxreader->pool, cur, end-cur+1);
1806             if (This->vbInterface)
1807                 hr = IVBSAXContentHandler_characters(content->vbhandler, &Chars);
1808             else
1809                 hr = ISAXContentHandler_characters(content->handler, Chars, SysStringLen(Chars));
1810         }
1811
1812         if(change) *end = '\r';
1813
1814         if(lastEvent)
1815             break;
1816
1817         This->column += end-cur+2;
1818         end += 2;
1819         cur = end;
1820     }
1821
1822     if (saxreader_has_handler(This, SAXLexicalHandler))
1823     {
1824         if (This->vbInterface)
1825             hr = IVBSAXLexicalHandler_endCDATA(lexical->vbhandler);
1826         else
1827             hr = ISAXLexicalHandler_endCDATA(lexical->handler);
1828     }
1829
1830     if(FAILED(hr))
1831         format_error_message_from_id(This, hr);
1832
1833     This->column += 4+end-cur;
1834 }
1835
1836 /*** IVBSAXLocator interface ***/
1837 /*** IUnknown methods ***/
1838 static HRESULT WINAPI ivbsaxlocator_QueryInterface(IVBSAXLocator* iface, REFIID riid, void **ppvObject)
1839 {
1840     saxlocator *This = impl_from_IVBSAXLocator( iface );
1841
1842     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject);
1843
1844     *ppvObject = NULL;
1845
1846     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
1847             IsEqualGUID( riid, &IID_IDispatch) ||
1848             IsEqualGUID( riid, &IID_IVBSAXLocator ))
1849     {
1850         *ppvObject = iface;
1851     }
1852     else if ( IsEqualGUID( riid, &IID_IVBSAXAttributes ))
1853     {
1854         *ppvObject = &This->IVBSAXAttributes_iface;
1855     }
1856     else
1857     {
1858         FIXME("interface %s not implemented\n", debugstr_guid(riid));
1859         return E_NOINTERFACE;
1860     }
1861
1862     IVBSAXLocator_AddRef( iface );
1863
1864     return S_OK;
1865 }
1866
1867 static ULONG WINAPI ivbsaxlocator_AddRef(IVBSAXLocator* iface)
1868 {
1869     saxlocator *This = impl_from_IVBSAXLocator( iface );
1870     TRACE("%p\n", This );
1871     return InterlockedIncrement( &This->ref );
1872 }
1873
1874 static ULONG WINAPI ivbsaxlocator_Release(
1875         IVBSAXLocator* iface)
1876 {
1877     saxlocator *This = impl_from_IVBSAXLocator( iface );
1878     return ISAXLocator_Release((ISAXLocator*)&This->IVBSAXLocator_iface);
1879 }
1880
1881 /*** IDispatch methods ***/
1882 static HRESULT WINAPI ivbsaxlocator_GetTypeInfoCount( IVBSAXLocator *iface, UINT* pctinfo )
1883 {
1884     saxlocator *This = impl_from_IVBSAXLocator( iface );
1885
1886     TRACE("(%p)->(%p)\n", This, pctinfo);
1887
1888     *pctinfo = 1;
1889
1890     return S_OK;
1891 }
1892
1893 static HRESULT WINAPI ivbsaxlocator_GetTypeInfo(
1894     IVBSAXLocator *iface,
1895     UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
1896 {
1897     saxlocator *This = impl_from_IVBSAXLocator( iface );
1898     HRESULT hr;
1899
1900     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1901
1902     hr = get_typeinfo(IVBSAXLocator_tid, ppTInfo);
1903
1904     return hr;
1905 }
1906
1907 static HRESULT WINAPI ivbsaxlocator_GetIDsOfNames(
1908     IVBSAXLocator *iface,
1909     REFIID riid,
1910     LPOLESTR* rgszNames,
1911     UINT cNames,
1912     LCID lcid,
1913     DISPID* rgDispId)
1914 {
1915     saxlocator *This = impl_from_IVBSAXLocator( iface );
1916     ITypeInfo *typeinfo;
1917     HRESULT hr;
1918
1919     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1920           lcid, rgDispId);
1921
1922     if(!rgszNames || cNames == 0 || !rgDispId)
1923         return E_INVALIDARG;
1924
1925     hr = get_typeinfo(IVBSAXLocator_tid, &typeinfo);
1926     if(SUCCEEDED(hr))
1927     {
1928         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1929         ITypeInfo_Release(typeinfo);
1930     }
1931
1932     return hr;
1933 }
1934
1935 static HRESULT WINAPI ivbsaxlocator_Invoke(
1936     IVBSAXLocator *iface,
1937     DISPID dispIdMember,
1938     REFIID riid,
1939     LCID lcid,
1940     WORD wFlags,
1941     DISPPARAMS* pDispParams,
1942     VARIANT* pVarResult,
1943     EXCEPINFO* pExcepInfo,
1944     UINT* puArgErr)
1945 {
1946     saxlocator *This = impl_from_IVBSAXLocator( iface );
1947     ITypeInfo *typeinfo;
1948     HRESULT hr;
1949
1950     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1951           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1952
1953     hr = get_typeinfo(IVBSAXLocator_tid, &typeinfo);
1954     if(SUCCEEDED(hr))
1955     {
1956         hr = ITypeInfo_Invoke(typeinfo, &This->IVBSAXLocator_iface, dispIdMember, wFlags,
1957                 pDispParams, pVarResult, pExcepInfo, puArgErr);
1958         ITypeInfo_Release(typeinfo);
1959     }
1960
1961     return hr;
1962 }
1963
1964 /*** IVBSAXLocator methods ***/
1965 static HRESULT WINAPI ivbsaxlocator_get_columnNumber(
1966         IVBSAXLocator* iface,
1967         int *pnColumn)
1968 {
1969     saxlocator *This = impl_from_IVBSAXLocator( iface );
1970     return ISAXLocator_getColumnNumber((ISAXLocator*)&This->IVBSAXLocator_iface, pnColumn);
1971 }
1972
1973 static HRESULT WINAPI ivbsaxlocator_get_lineNumber(
1974         IVBSAXLocator* iface,
1975         int *pnLine)
1976 {
1977     saxlocator *This = impl_from_IVBSAXLocator( iface );
1978     return ISAXLocator_getLineNumber((ISAXLocator*)&This->IVBSAXLocator_iface, pnLine);
1979 }
1980
1981 static HRESULT WINAPI ivbsaxlocator_get_publicId(
1982         IVBSAXLocator* iface,
1983         BSTR* publicId)
1984 {
1985     saxlocator *This = impl_from_IVBSAXLocator( iface );
1986     return ISAXLocator_getPublicId((ISAXLocator*)&This->IVBSAXLocator_iface,
1987             (const WCHAR**)publicId);
1988 }
1989
1990 static HRESULT WINAPI ivbsaxlocator_get_systemId(
1991         IVBSAXLocator* iface,
1992         BSTR* systemId)
1993 {
1994     saxlocator *This = impl_from_IVBSAXLocator( iface );
1995     return ISAXLocator_getSystemId((ISAXLocator*)&This->IVBSAXLocator_iface,
1996             (const WCHAR**)systemId);
1997 }
1998
1999 static const struct IVBSAXLocatorVtbl VBSAXLocatorVtbl =
2000 {
2001     ivbsaxlocator_QueryInterface,
2002     ivbsaxlocator_AddRef,
2003     ivbsaxlocator_Release,
2004     ivbsaxlocator_GetTypeInfoCount,
2005     ivbsaxlocator_GetTypeInfo,
2006     ivbsaxlocator_GetIDsOfNames,
2007     ivbsaxlocator_Invoke,
2008     ivbsaxlocator_get_columnNumber,
2009     ivbsaxlocator_get_lineNumber,
2010     ivbsaxlocator_get_publicId,
2011     ivbsaxlocator_get_systemId
2012 };
2013
2014 /*** ISAXLocator interface ***/
2015 /*** IUnknown methods ***/
2016 static HRESULT WINAPI isaxlocator_QueryInterface(ISAXLocator* iface, REFIID riid, void **ppvObject)
2017 {
2018     saxlocator *This = impl_from_ISAXLocator( iface );
2019
2020     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
2021
2022     *ppvObject = NULL;
2023
2024     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
2025             IsEqualGUID( riid, &IID_ISAXLocator ))
2026     {
2027         *ppvObject = iface;
2028     }
2029     else if ( IsEqualGUID( riid, &IID_ISAXAttributes ))
2030     {
2031         *ppvObject = &This->ISAXAttributes_iface;
2032     }
2033     else
2034     {
2035         WARN("interface %s not implemented\n", debugstr_guid(riid));
2036         return E_NOINTERFACE;
2037     }
2038
2039     ISAXLocator_AddRef( iface );
2040
2041     return S_OK;
2042 }
2043
2044 static ULONG WINAPI isaxlocator_AddRef(ISAXLocator* iface)
2045 {
2046     saxlocator *This = impl_from_ISAXLocator( iface );
2047     ULONG ref = InterlockedIncrement( &This->ref );
2048     TRACE("(%p)->(%d)\n", This, ref);
2049     return ref;
2050 }
2051
2052 static ULONG WINAPI isaxlocator_Release(
2053         ISAXLocator* iface)
2054 {
2055     saxlocator *This = impl_from_ISAXLocator( iface );
2056     LONG ref = InterlockedDecrement( &This->ref );
2057
2058     TRACE("(%p)->(%d)\n", This, ref );
2059
2060     if (ref == 0)
2061     {
2062         element_entry *element, *element2;
2063         int index;
2064
2065         SysFreeString(This->publicId);
2066         SysFreeString(This->systemId);
2067         SysFreeString(This->namespaceUri);
2068
2069         for(index=0; index<This->nb_attributes; index++)
2070         {
2071             SysFreeString(This->attributes[index].szLocalname);
2072             SysFreeString(This->attributes[index].szValue);
2073             SysFreeString(This->attributes[index].szQName);
2074         }
2075         heap_free(This->attributes);
2076
2077         /* element stack */
2078         LIST_FOR_EACH_ENTRY_SAFE(element, element2, &This->elements, element_entry, entry)
2079         {
2080             list_remove(&element->entry);
2081             free_element_entry(element);
2082         }
2083
2084         ISAXXMLReader_Release(&This->saxreader->ISAXXMLReader_iface);
2085         heap_free( This );
2086     }
2087
2088     return ref;
2089 }
2090
2091 /*** ISAXLocator methods ***/
2092 static HRESULT WINAPI isaxlocator_getColumnNumber(
2093         ISAXLocator* iface,
2094         int *pnColumn)
2095 {
2096     saxlocator *This = impl_from_ISAXLocator( iface );
2097
2098     *pnColumn = This->column;
2099     return S_OK;
2100 }
2101
2102 static HRESULT WINAPI isaxlocator_getLineNumber(
2103         ISAXLocator* iface,
2104         int *pnLine)
2105 {
2106     saxlocator *This = impl_from_ISAXLocator( iface );
2107
2108     *pnLine = This->line;
2109     return S_OK;
2110 }
2111
2112 static HRESULT WINAPI isaxlocator_getPublicId(
2113         ISAXLocator* iface,
2114         const WCHAR ** ppwchPublicId)
2115 {
2116     BSTR publicId;
2117     saxlocator *This = impl_from_ISAXLocator( iface );
2118
2119     SysFreeString(This->publicId);
2120
2121     publicId = bstr_from_xmlChar(xmlSAX2GetPublicId(This->pParserCtxt));
2122     if(SysStringLen(publicId))
2123         This->publicId = (WCHAR*)&publicId;
2124     else
2125     {
2126         SysFreeString(publicId);
2127         This->publicId = NULL;
2128     }
2129
2130     *ppwchPublicId = This->publicId;
2131     return S_OK;
2132 }
2133
2134 static HRESULT WINAPI isaxlocator_getSystemId(
2135         ISAXLocator* iface,
2136         const WCHAR ** ppwchSystemId)
2137 {
2138     BSTR systemId;
2139     saxlocator *This = impl_from_ISAXLocator( iface );
2140
2141     SysFreeString(This->systemId);
2142
2143     systemId = bstr_from_xmlChar(xmlSAX2GetSystemId(This->pParserCtxt));
2144     if(SysStringLen(systemId))
2145         This->systemId = (WCHAR*)&systemId;
2146     else
2147     {
2148         SysFreeString(systemId);
2149         This->systemId = NULL;
2150     }
2151
2152     *ppwchSystemId = This->systemId;
2153     return S_OK;
2154 }
2155
2156 static const struct ISAXLocatorVtbl SAXLocatorVtbl =
2157 {
2158     isaxlocator_QueryInterface,
2159     isaxlocator_AddRef,
2160     isaxlocator_Release,
2161     isaxlocator_getColumnNumber,
2162     isaxlocator_getLineNumber,
2163     isaxlocator_getPublicId,
2164     isaxlocator_getSystemId
2165 };
2166
2167 static HRESULT SAXLocator_create(saxreader *reader, saxlocator **ppsaxlocator, BOOL vbInterface)
2168 {
2169     static const WCHAR w3xmlns[] = { 'h','t','t','p',':','/','/', 'w','w','w','.','w','3','.',
2170         'o','r','g','/','2','0','0','0','/','x','m','l','n','s','/',0 };
2171
2172     saxlocator *locator;
2173
2174     locator = heap_alloc( sizeof (*locator) );
2175     if( !locator )
2176         return E_OUTOFMEMORY;
2177
2178     locator->IVBSAXLocator_iface.lpVtbl = &VBSAXLocatorVtbl;
2179     locator->ISAXLocator_iface.lpVtbl = &SAXLocatorVtbl;
2180     locator->IVBSAXAttributes_iface.lpVtbl = &ivbsaxattributes_vtbl;
2181     locator->ISAXAttributes_iface.lpVtbl = &isaxattributes_vtbl;
2182     locator->ref = 1;
2183     locator->vbInterface = vbInterface;
2184
2185     locator->saxreader = reader;
2186     ISAXXMLReader_AddRef(&reader->ISAXXMLReader_iface);
2187
2188     locator->pParserCtxt = NULL;
2189     locator->publicId = NULL;
2190     locator->systemId = NULL;
2191     locator->line = reader->version < MSXML4 ? 0 : 1;
2192     locator->column = 0;
2193     locator->ret = S_OK;
2194     if (locator->saxreader->version >= MSXML6)
2195         locator->namespaceUri = SysAllocString(w3xmlns);
2196     else
2197         locator->namespaceUri = SysAllocStringLen(NULL, 0);
2198     if(!locator->namespaceUri)
2199     {
2200         ISAXXMLReader_Release(&reader->ISAXXMLReader_iface);
2201         heap_free(locator);
2202         return E_OUTOFMEMORY;
2203     }
2204
2205     locator->attributesSize = 8;
2206     locator->nb_attributes = 0;
2207     locator->attributes = heap_alloc(sizeof(struct _attributes)*locator->attributesSize);
2208     if(!locator->attributes)
2209     {
2210         ISAXXMLReader_Release(&reader->ISAXXMLReader_iface);
2211         SysFreeString(locator->namespaceUri);
2212         heap_free(locator);
2213         return E_OUTOFMEMORY;
2214     }
2215
2216     list_init(&locator->elements);
2217
2218     *ppsaxlocator = locator;
2219
2220     TRACE("returning %p\n", *ppsaxlocator);
2221
2222     return S_OK;
2223 }
2224
2225 /*** SAXXMLReader internal functions ***/
2226 static HRESULT internal_parseBuffer(saxreader *This, const char *buffer, int size, BOOL vbInterface)
2227 {
2228     xmlCharEncoding encoding = XML_CHAR_ENCODING_NONE;
2229     xmlChar *enc_name = NULL;
2230     saxlocator *locator;
2231     HRESULT hr;
2232
2233     TRACE("(%p)->(%p %d)\n", This, buffer, size);
2234
2235     hr = SAXLocator_create(This, &locator, vbInterface);
2236     if (FAILED(hr))
2237         return hr;
2238
2239     if (size >= 4)
2240     {
2241         const unsigned char *buff = (unsigned char*)buffer;
2242
2243         encoding = xmlDetectCharEncoding((xmlChar*)buffer, 4);
2244         enc_name = (xmlChar*)xmlGetCharEncodingName(encoding);
2245         TRACE("detected encoding: %s\n", enc_name);
2246         /* skip BOM, parser won't switch encodings and so won't skip it on its own */
2247         if ((encoding == XML_CHAR_ENCODING_UTF8) &&
2248             buff[0] == 0xEF && buff[1] == 0xBB && buff[2] == 0xBF)
2249         {
2250             buffer += 3;
2251             size -= 3;
2252         }
2253     }
2254
2255     /* if libxml2 detection failed try to guess */
2256     if (encoding == XML_CHAR_ENCODING_NONE)
2257     {
2258         const WCHAR *ptr = (WCHAR*)buffer;
2259         /* xml declaration with possibly specfied encoding will be still handled by parser */
2260         if ((size >= 2) && *ptr == '<' && ptr[1] != '?')
2261         {
2262             enc_name = (xmlChar*)xmlGetCharEncodingName(XML_CHAR_ENCODING_UTF16LE);
2263             encoding = XML_CHAR_ENCODING_UTF16LE;
2264         }
2265     }
2266     else if (encoding == XML_CHAR_ENCODING_UTF8)
2267         enc_name = (xmlChar*)xmlGetCharEncodingName(encoding);
2268     else
2269         enc_name = NULL;
2270
2271     locator->pParserCtxt = xmlCreateMemoryParserCtxt(buffer, size);
2272     if (!locator->pParserCtxt)
2273     {
2274         ISAXLocator_Release(&locator->ISAXLocator_iface);
2275         return E_FAIL;
2276     }
2277
2278     if (enc_name)
2279     {
2280         locator->pParserCtxt->encoding = xmlStrdup(enc_name);
2281         if (encoding == XML_CHAR_ENCODING_UTF16LE) {
2282             TRACE("switching to %s\n", enc_name);
2283             xmlSwitchEncoding(locator->pParserCtxt, encoding);
2284         }
2285     }
2286
2287     xmlFree(locator->pParserCtxt->sax);
2288     locator->pParserCtxt->sax = &locator->saxreader->sax;
2289     locator->pParserCtxt->userData = locator;
2290
2291     This->isParsing = TRUE;
2292     if(xmlParseDocument(locator->pParserCtxt) == -1 && locator->ret == S_OK)
2293         hr = E_FAIL;
2294     else
2295         hr = locator->ret;
2296     This->isParsing = FALSE;
2297
2298     if(locator->pParserCtxt)
2299     {
2300         locator->pParserCtxt->sax = NULL;
2301         xmlFreeParserCtxt(locator->pParserCtxt);
2302         locator->pParserCtxt = NULL;
2303     }
2304
2305     ISAXLocator_Release(&locator->ISAXLocator_iface);
2306     return hr;
2307 }
2308
2309 static HRESULT internal_parseStream(saxreader *This, ISequentialStream *stream, BOOL vbInterface)
2310 {
2311     saxlocator *locator;
2312     HRESULT hr;
2313     ULONG dataRead;
2314     char data[1024];
2315     int ret;
2316
2317     dataRead = 0;
2318     hr = ISequentialStream_Read(stream, data, sizeof(data), &dataRead);
2319     if(FAILED(hr)) return hr;
2320
2321     hr = SAXLocator_create(This, &locator, vbInterface);
2322     if(FAILED(hr)) return hr;
2323
2324     locator->pParserCtxt = xmlCreatePushParserCtxt(
2325             &locator->saxreader->sax, locator,
2326             data, dataRead, NULL);
2327     if(!locator->pParserCtxt)
2328     {
2329         ISAXLocator_Release(&locator->ISAXLocator_iface);
2330         return E_FAIL;
2331     }
2332
2333     This->isParsing = TRUE;
2334
2335     if(dataRead != sizeof(data))
2336     {
2337         ret = xmlParseChunk(locator->pParserCtxt, data, 0, 1);
2338         hr = ret!=XML_ERR_OK && locator->ret==S_OK ? E_FAIL : locator->ret;
2339     }
2340     else
2341     {
2342         while(1)
2343         {
2344             dataRead = 0;
2345             hr = ISequentialStream_Read(stream, data, sizeof(data), &dataRead);
2346             if (FAILED(hr)) break;
2347
2348             ret = xmlParseChunk(locator->pParserCtxt, data, dataRead, 0);
2349             hr = ret!=XML_ERR_OK && locator->ret==S_OK ? E_FAIL : locator->ret;
2350
2351             if (hr != S_OK) break;
2352
2353             if (dataRead != sizeof(data))
2354             {
2355                 ret = xmlParseChunk(locator->pParserCtxt, data, 0, 1);
2356                 hr = ret!=XML_ERR_OK && locator->ret==S_OK ? E_FAIL : locator->ret;
2357                 break;
2358             }
2359         }
2360     }
2361
2362     This->isParsing = FALSE;
2363
2364     xmlFreeParserCtxt(locator->pParserCtxt);
2365     locator->pParserCtxt = NULL;
2366     ISAXLocator_Release(&locator->ISAXLocator_iface);
2367     return hr;
2368 }
2369
2370 static HRESULT internal_getEntityResolver(
2371         saxreader *This,
2372         void *pEntityResolver,
2373         BOOL vbInterface)
2374 {
2375     FIXME("(%p)->(%p) stub\n", This, pEntityResolver);
2376     return E_NOTIMPL;
2377 }
2378
2379 static HRESULT internal_putEntityResolver(
2380         saxreader *This,
2381         void *pEntityResolver,
2382         BOOL vbInterface)
2383 {
2384     FIXME("(%p)->(%p) stub\n", This, pEntityResolver);
2385     return E_NOTIMPL;
2386 }
2387
2388 static HRESULT internal_parse(
2389         saxreader* This,
2390         VARIANT varInput,
2391         BOOL vbInterface)
2392 {
2393     HRESULT hr;
2394
2395     TRACE("(%p)->(%s)\n", This, debugstr_variant(&varInput));
2396
2397     /* Dispose of the BSTRs in the pool from a prior run, if any. */
2398     free_bstr_pool(&This->pool);
2399
2400     switch(V_VT(&varInput))
2401     {
2402         case VT_BSTR:
2403             hr = internal_parseBuffer(This, (const char*)V_BSTR(&varInput),
2404                     strlenW(V_BSTR(&varInput))*sizeof(WCHAR), vbInterface);
2405             break;
2406         case VT_ARRAY|VT_UI1: {
2407             void *pSAData;
2408             LONG lBound, uBound;
2409             ULONG dataRead;
2410
2411             hr = SafeArrayGetLBound(V_ARRAY(&varInput), 1, &lBound);
2412             if(hr != S_OK) break;
2413             hr = SafeArrayGetUBound(V_ARRAY(&varInput), 1, &uBound);
2414             if(hr != S_OK) break;
2415             dataRead = (uBound-lBound)*SafeArrayGetElemsize(V_ARRAY(&varInput));
2416             hr = SafeArrayAccessData(V_ARRAY(&varInput), &pSAData);
2417             if(hr != S_OK) break;
2418             hr = internal_parseBuffer(This, pSAData, dataRead, vbInterface);
2419             SafeArrayUnaccessData(V_ARRAY(&varInput));
2420             break;
2421         }
2422         case VT_UNKNOWN:
2423         case VT_DISPATCH: {
2424             IPersistStream *persistStream;
2425             ISequentialStream *stream = NULL;
2426             IXMLDOMDocument *xmlDoc;
2427
2428             if(IUnknown_QueryInterface(V_UNKNOWN(&varInput),
2429                         &IID_IXMLDOMDocument, (void**)&xmlDoc) == S_OK)
2430             {
2431                 BSTR bstrData;
2432
2433                 IXMLDOMDocument_get_xml(xmlDoc, &bstrData);
2434                 hr = internal_parseBuffer(This, (const char*)bstrData,
2435                         SysStringByteLen(bstrData), vbInterface);
2436                 IXMLDOMDocument_Release(xmlDoc);
2437                 SysFreeString(bstrData);
2438                 break;
2439             }
2440
2441             if(IUnknown_QueryInterface(V_UNKNOWN(&varInput),
2442                         &IID_IPersistStream, (void**)&persistStream) == S_OK)
2443             {
2444                 IStream *stream_copy;
2445
2446                 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream_copy);
2447                 if(hr != S_OK)
2448                 {
2449                     IPersistStream_Release(persistStream);
2450                     return hr;
2451                 }
2452
2453                 hr = IPersistStream_Save(persistStream, stream_copy, TRUE);
2454                 IPersistStream_Release(persistStream);
2455                 if(hr == S_OK)
2456                     IStream_QueryInterface(stream_copy, &IID_ISequentialStream, (void**)&stream);
2457
2458                 IStream_Release(stream_copy);
2459             }
2460
2461             /* try base interface first */
2462             if(!stream)
2463             {
2464                 IUnknown_QueryInterface(V_UNKNOWN(&varInput), &IID_ISequentialStream, (void**)&stream);
2465                 if (!stream)
2466                     /* this should never happen if IStream is implemented properly, but just in case */
2467                     IUnknown_QueryInterface(V_UNKNOWN(&varInput), &IID_IStream, (void**)&stream);
2468             }
2469
2470             if(stream)
2471             {
2472                 hr = internal_parseStream(This, stream, vbInterface);
2473                 ISequentialStream_Release(stream);
2474             }
2475             else
2476             {
2477                 WARN("IUnknown* input doesn't support any of expected interfaces\n");
2478                 hr = E_INVALIDARG;
2479             }
2480
2481             break;
2482         }
2483         default:
2484             WARN("vt %d not implemented\n", V_VT(&varInput));
2485             hr = E_INVALIDARG;
2486     }
2487
2488     return hr;
2489 }
2490
2491 static HRESULT internal_vbonDataAvailable(void *obj, char *ptr, DWORD len)
2492 {
2493     saxreader *This = obj;
2494
2495     return internal_parseBuffer(This, ptr, len, TRUE);
2496 }
2497
2498 static HRESULT internal_onDataAvailable(void *obj, char *ptr, DWORD len)
2499 {
2500     saxreader *This = obj;
2501
2502     return internal_parseBuffer(This, ptr, len, FALSE);
2503 }
2504
2505 static HRESULT internal_parseURL(
2506         saxreader* This,
2507         const WCHAR *url,
2508         BOOL vbInterface)
2509 {
2510     IMoniker *mon;
2511     bsc_t *bsc;
2512     HRESULT hr;
2513
2514     TRACE("(%p)->(%s)\n", This, debugstr_w(url));
2515
2516     hr = create_moniker_from_url(url, &mon);
2517     if(FAILED(hr))
2518         return hr;
2519
2520     if(vbInterface) hr = bind_url(mon, internal_vbonDataAvailable, This, &bsc);
2521     else hr = bind_url(mon, internal_onDataAvailable, This, &bsc);
2522     IMoniker_Release(mon);
2523
2524     if(FAILED(hr))
2525         return hr;
2526
2527     return detach_bsc(bsc);
2528 }
2529
2530 static HRESULT internal_putProperty(
2531     saxreader* This,
2532     const WCHAR *prop,
2533     VARIANT value,
2534     BOOL vbInterface)
2535 {
2536     TRACE("(%p)->(%s %s)\n", This, debugstr_w(prop), debugstr_variant(&value));
2537
2538     if(!memcmp(prop, PropertyDeclHandlerW, sizeof(PropertyDeclHandlerW)))
2539     {
2540         if(This->isParsing) return E_FAIL;
2541
2542         switch (V_VT(&value))
2543         {
2544         case VT_EMPTY:
2545             saxreader_put_handler(This, SAXDeclHandler, NULL, vbInterface);
2546             break;
2547         case VT_UNKNOWN:
2548         {
2549             IUnknown *handler = NULL;
2550
2551             if (V_UNKNOWN(&value))
2552             {
2553                 HRESULT hr;
2554
2555                 if (vbInterface)
2556                     hr = IUnknown_QueryInterface(V_UNKNOWN(&value), &IID_IVBSAXDeclHandler, (void**)&handler);
2557                 else
2558                     hr = IUnknown_QueryInterface(V_UNKNOWN(&value), &IID_ISAXDeclHandler, (void**)&handler);
2559                 if (FAILED(hr)) return hr;
2560             }
2561
2562             saxreader_put_handler(This, SAXDeclHandler, handler, vbInterface);
2563             if (handler) IUnknown_Release(handler);
2564             break;
2565         }
2566         default:
2567             return E_INVALIDARG;
2568         }
2569
2570         return S_OK;
2571     }
2572
2573     if(!memcmp(prop, PropertyLexicalHandlerW, sizeof(PropertyLexicalHandlerW)))
2574     {
2575         if(This->isParsing) return E_FAIL;
2576
2577         switch (V_VT(&value))
2578         {
2579         case VT_EMPTY:
2580             saxreader_put_handler(This, SAXLexicalHandler, NULL, vbInterface);
2581             break;
2582         case VT_UNKNOWN:
2583         {
2584             IUnknown *handler = NULL;
2585
2586             if (V_UNKNOWN(&value))
2587             {
2588                 HRESULT hr;
2589
2590                 if (vbInterface)
2591                     hr = IUnknown_QueryInterface(V_UNKNOWN(&value), &IID_IVBSAXLexicalHandler, (void**)&handler);
2592                 else
2593                     hr = IUnknown_QueryInterface(V_UNKNOWN(&value), &IID_ISAXLexicalHandler, (void**)&handler);
2594                 if (FAILED(hr)) return hr;
2595             }
2596
2597             saxreader_put_handler(This, SAXLexicalHandler, handler, vbInterface);
2598             if (handler) IUnknown_Release(handler);
2599             break;
2600         }
2601         default:
2602             return E_INVALIDARG;
2603         }
2604
2605         return S_OK;
2606     }
2607
2608     if(!memcmp(prop, PropertyMaxXMLSizeW, sizeof(PropertyMaxXMLSizeW)))
2609     {
2610         if (V_VT(&value) == VT_I4 && V_I4(&value) == 0) return S_OK;
2611         FIXME("(%p)->(%s): max-xml-size unsupported\n", This, debugstr_variant(&value));
2612         return E_NOTIMPL;
2613     }
2614
2615     if(!memcmp(prop, PropertyMaxElementDepthW, sizeof(PropertyMaxElementDepthW)))
2616     {
2617         if (V_VT(&value) == VT_I4 && V_I4(&value) == 0) return S_OK;
2618         FIXME("(%p)->(%s): max-element-depth unsupported\n", This, debugstr_variant(&value));
2619         return E_NOTIMPL;
2620     }
2621
2622     FIXME("(%p)->(%s:%s): unsupported property\n", This, debugstr_w(prop), debugstr_variant(&value));
2623
2624     if(!memcmp(prop, PropertyCharsetW, sizeof(PropertyCharsetW)))
2625         return E_NOTIMPL;
2626
2627     if(!memcmp(prop, PropertyDomNodeW, sizeof(PropertyDomNodeW)))
2628         return E_FAIL;
2629
2630     if(!memcmp(prop, PropertyInputSourceW, sizeof(PropertyInputSourceW)))
2631         return E_NOTIMPL;
2632
2633     if(!memcmp(prop, PropertySchemaDeclHandlerW, sizeof(PropertySchemaDeclHandlerW)))
2634         return E_NOTIMPL;
2635
2636     if(!memcmp(prop, PropertyXMLDeclEncodingW, sizeof(PropertyXMLDeclEncodingW)))
2637         return E_FAIL;
2638
2639     if(!memcmp(prop, PropertyXMLDeclStandaloneW, sizeof(PropertyXMLDeclStandaloneW)))
2640         return E_FAIL;
2641
2642     if(!memcmp(prop, PropertyXMLDeclVersionW, sizeof(PropertyXMLDeclVersionW)))
2643         return E_FAIL;
2644
2645     return E_INVALIDARG;
2646 }
2647
2648 static HRESULT internal_getProperty(const saxreader* This, const WCHAR *prop, VARIANT *value, BOOL vb)
2649 {
2650     TRACE("(%p)->(%s)\n", This, debugstr_w(prop));
2651
2652     if (!value) return E_POINTER;
2653
2654     if (!memcmp(PropertyLexicalHandlerW, prop, sizeof(PropertyLexicalHandlerW)))
2655     {
2656         V_VT(value) = VT_UNKNOWN;
2657         saxreader_get_handler(This, SAXLexicalHandler, vb, (void**)&V_UNKNOWN(value));
2658         return S_OK;
2659     }
2660
2661     if (!memcmp(PropertyDeclHandlerW, prop, sizeof(PropertyDeclHandlerW)))
2662     {
2663         V_VT(value) = VT_UNKNOWN;
2664         saxreader_get_handler(This, SAXDeclHandler, vb, (void**)&V_UNKNOWN(value));
2665         return S_OK;
2666     }
2667
2668     if (!memcmp(PropertyXmlDeclVersionW, prop, sizeof(PropertyXmlDeclVersionW)))
2669     {
2670         V_VT(value) = VT_BSTR;
2671         V_BSTR(value) = SysAllocString(This->xmldecl_version);
2672         return S_OK;
2673     }
2674
2675     FIXME("(%p)->(%s) unsupported property\n", This, debugstr_w(prop));
2676
2677     return E_NOTIMPL;
2678 }
2679
2680 /*** IVBSAXXMLReader interface ***/
2681 /*** IUnknown methods ***/
2682 static HRESULT WINAPI saxxmlreader_QueryInterface(IVBSAXXMLReader* iface, REFIID riid, void **ppvObject)
2683 {
2684     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2685
2686     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
2687
2688     *ppvObject = NULL;
2689
2690     if ( IsEqualGUID( riid, &IID_IUnknown ) ||
2691          IsEqualGUID( riid, &IID_IDispatch ) ||
2692          IsEqualGUID( riid, &IID_IVBSAXXMLReader ))
2693     {
2694         *ppvObject = iface;
2695     }
2696     else if( IsEqualGUID( riid, &IID_ISAXXMLReader ))
2697     {
2698         *ppvObject = &This->ISAXXMLReader_iface;
2699     }
2700     else if (dispex_query_interface(&This->dispex, riid, ppvObject))
2701     {
2702         return *ppvObject ? S_OK : E_NOINTERFACE;
2703     }
2704     else
2705     {
2706         FIXME("interface %s not implemented\n", debugstr_guid(riid));
2707         return E_NOINTERFACE;
2708     }
2709
2710     IVBSAXXMLReader_AddRef( iface );
2711
2712     return S_OK;
2713 }
2714
2715 static ULONG WINAPI saxxmlreader_AddRef(IVBSAXXMLReader* iface)
2716 {
2717     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2718     TRACE("%p\n", This );
2719     return InterlockedIncrement( &This->ref );
2720 }
2721
2722 static ULONG WINAPI saxxmlreader_Release(
2723     IVBSAXXMLReader* iface)
2724 {
2725     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2726     LONG ref;
2727
2728     TRACE("%p\n", This );
2729
2730     ref = InterlockedDecrement( &This->ref );
2731     if ( ref == 0 )
2732     {
2733         int i;
2734
2735         for (i = 0; i < SAXHandler_Last; i++)
2736         {
2737             struct saxhandler_iface *iface = &This->saxhandlers[i];
2738
2739             if (iface->handler)
2740                 IUnknown_Release(iface->handler);
2741
2742             if (iface->vbhandler)
2743                 IUnknown_Release(iface->vbhandler);
2744         }
2745
2746         SysFreeString(This->xmldecl_version);
2747         free_bstr_pool(&This->pool);
2748
2749         release_dispex(&This->dispex);
2750         heap_free( This );
2751     }
2752
2753     return ref;
2754 }
2755 /*** IDispatch ***/
2756 static HRESULT WINAPI saxxmlreader_GetTypeInfoCount( IVBSAXXMLReader *iface, UINT* pctinfo )
2757 {
2758     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2759     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
2760 }
2761
2762 static HRESULT WINAPI saxxmlreader_GetTypeInfo(
2763     IVBSAXXMLReader *iface,
2764     UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
2765 {
2766     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2767     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
2768         iTInfo, lcid, ppTInfo);
2769 }
2770
2771 static HRESULT WINAPI saxxmlreader_GetIDsOfNames(
2772     IVBSAXXMLReader *iface,
2773     REFIID riid,
2774     LPOLESTR* rgszNames,
2775     UINT cNames,
2776     LCID lcid,
2777     DISPID* rgDispId)
2778 {
2779     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2780     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
2781         riid, rgszNames, cNames, lcid, rgDispId);
2782 }
2783
2784 static HRESULT WINAPI saxxmlreader_Invoke(
2785     IVBSAXXMLReader *iface,
2786     DISPID dispIdMember,
2787     REFIID riid,
2788     LCID lcid,
2789     WORD wFlags,
2790     DISPPARAMS* pDispParams,
2791     VARIANT* pVarResult,
2792     EXCEPINFO* pExcepInfo,
2793     UINT* puArgErr)
2794 {
2795     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2796     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
2797         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2798 }
2799
2800 /*** IVBSAXXMLReader methods ***/
2801 static HRESULT WINAPI saxxmlreader_getFeature(
2802     IVBSAXXMLReader* iface,
2803     const WCHAR *feature_name,
2804     VARIANT_BOOL *value)
2805 {
2806     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2807     saxreader_feature feature;
2808
2809     TRACE("(%p)->(%s %p)\n", This, debugstr_w(feature_name), value);
2810
2811     feature = get_saxreader_feature(feature_name);
2812     if (feature == Namespaces || feature == NamespacePrefixes)
2813         return get_feature_value(This, feature, value);
2814
2815     FIXME("(%p)->(%s %p) stub\n", This, debugstr_w(feature_name), value);
2816     return E_NOTIMPL;
2817 }
2818
2819 static HRESULT WINAPI saxxmlreader_putFeature(
2820     IVBSAXXMLReader* iface,
2821     const WCHAR *feature_name,
2822     VARIANT_BOOL value)
2823 {
2824     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2825     saxreader_feature feature;
2826
2827     TRACE("(%p)->(%s %x)\n", This, debugstr_w(feature_name), value);
2828
2829     feature = get_saxreader_feature(feature_name);
2830
2831     /* accepted cases */
2832     if ((feature == ExternalGeneralEntities   && value == VARIANT_FALSE) ||
2833         (feature == ExternalParameterEntities && value == VARIANT_FALSE) ||
2834          feature == Namespaces ||
2835          feature == NamespacePrefixes)
2836     {
2837         return set_feature_value(This, feature, value);
2838     }
2839
2840     if (feature == LexicalHandlerParEntities || feature == ProhibitDTD)
2841     {
2842         FIXME("(%p)->(%s %x) stub\n", This, debugstr_w(feature_name), value);
2843         return set_feature_value(This, feature, value);
2844     }
2845
2846     FIXME("(%p)->(%s %x) stub\n", This, debugstr_w(feature_name), value);
2847     return E_NOTIMPL;
2848 }
2849
2850 static HRESULT WINAPI saxxmlreader_getProperty(
2851     IVBSAXXMLReader* iface,
2852     const WCHAR *prop,
2853     VARIANT *value)
2854 {
2855     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2856     return internal_getProperty(This, prop, value, TRUE);
2857 }
2858
2859 static HRESULT WINAPI saxxmlreader_putProperty(
2860     IVBSAXXMLReader* iface,
2861     const WCHAR *pProp,
2862     VARIANT value)
2863 {
2864     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2865     return internal_putProperty(This, pProp, value, TRUE);
2866 }
2867
2868 static HRESULT WINAPI saxxmlreader_get_entityResolver(
2869     IVBSAXXMLReader* iface,
2870     IVBSAXEntityResolver **pEntityResolver)
2871 {
2872     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2873     return internal_getEntityResolver(This, pEntityResolver, TRUE);
2874 }
2875
2876 static HRESULT WINAPI saxxmlreader_put_entityResolver(
2877     IVBSAXXMLReader* iface,
2878     IVBSAXEntityResolver *pEntityResolver)
2879 {
2880     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2881     return internal_putEntityResolver(This, pEntityResolver, TRUE);
2882 }
2883
2884 static HRESULT WINAPI saxxmlreader_get_contentHandler(
2885     IVBSAXXMLReader* iface,
2886     IVBSAXContentHandler **handler)
2887 {
2888     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2889     return saxreader_get_handler(This, SAXContentHandler, TRUE, (void**)handler);
2890 }
2891
2892 static HRESULT WINAPI saxxmlreader_put_contentHandler(
2893     IVBSAXXMLReader* iface,
2894     IVBSAXContentHandler *handler)
2895 {
2896     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2897     return saxreader_put_handler(This, SAXContentHandler, handler, TRUE);
2898 }
2899
2900 static HRESULT WINAPI saxxmlreader_get_dtdHandler(
2901     IVBSAXXMLReader* iface,
2902     IVBSAXDTDHandler **handler)
2903 {
2904     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2905     return saxreader_get_handler(This, SAXDTDHandler, TRUE, (void**)handler);
2906 }
2907
2908 static HRESULT WINAPI saxxmlreader_put_dtdHandler(
2909     IVBSAXXMLReader* iface,
2910     IVBSAXDTDHandler *handler)
2911 {
2912     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2913     return saxreader_put_handler(This, SAXDTDHandler, handler, TRUE);
2914 }
2915
2916 static HRESULT WINAPI saxxmlreader_get_errorHandler(
2917     IVBSAXXMLReader* iface,
2918     IVBSAXErrorHandler **handler)
2919 {
2920     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2921     return saxreader_get_handler(This, SAXErrorHandler, TRUE, (void**)handler);
2922 }
2923
2924 static HRESULT WINAPI saxxmlreader_put_errorHandler(
2925     IVBSAXXMLReader* iface,
2926     IVBSAXErrorHandler *handler)
2927 {
2928     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2929     return saxreader_put_handler(This, SAXErrorHandler, handler, TRUE);
2930 }
2931
2932 static HRESULT WINAPI saxxmlreader_get_baseURL(
2933     IVBSAXXMLReader* iface,
2934     const WCHAR **pBaseUrl)
2935 {
2936     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2937
2938     FIXME("(%p)->(%p) stub\n", This, pBaseUrl);
2939     return E_NOTIMPL;
2940 }
2941
2942 static HRESULT WINAPI saxxmlreader_put_baseURL(
2943     IVBSAXXMLReader* iface,
2944     const WCHAR *pBaseUrl)
2945 {
2946     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2947
2948     FIXME("(%p)->(%s) stub\n", This, debugstr_w(pBaseUrl));
2949     return E_NOTIMPL;
2950 }
2951
2952 static HRESULT WINAPI saxxmlreader_get_secureBaseURL(
2953     IVBSAXXMLReader* iface,
2954     const WCHAR **pSecureBaseUrl)
2955 {
2956     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2957
2958     FIXME("(%p)->(%p) stub\n", This, pSecureBaseUrl);
2959     return E_NOTIMPL;
2960 }
2961
2962
2963 static HRESULT WINAPI saxxmlreader_put_secureBaseURL(
2964     IVBSAXXMLReader* iface,
2965     const WCHAR *secureBaseUrl)
2966 {
2967     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2968
2969     FIXME("(%p)->(%s) stub\n", This, debugstr_w(secureBaseUrl));
2970     return E_NOTIMPL;
2971 }
2972
2973 static HRESULT WINAPI saxxmlreader_parse(
2974     IVBSAXXMLReader* iface,
2975     VARIANT varInput)
2976 {
2977     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2978     return internal_parse(This, varInput, TRUE);
2979 }
2980
2981 static HRESULT WINAPI saxxmlreader_parseURL(
2982     IVBSAXXMLReader* iface,
2983     const WCHAR *url)
2984 {
2985     saxreader *This = impl_from_IVBSAXXMLReader( iface );
2986     return internal_parseURL(This, url, TRUE);
2987 }
2988
2989 static const struct IVBSAXXMLReaderVtbl VBSAXXMLReaderVtbl =
2990 {
2991     saxxmlreader_QueryInterface,
2992     saxxmlreader_AddRef,
2993     saxxmlreader_Release,
2994     saxxmlreader_GetTypeInfoCount,
2995     saxxmlreader_GetTypeInfo,
2996     saxxmlreader_GetIDsOfNames,
2997     saxxmlreader_Invoke,
2998     saxxmlreader_getFeature,
2999     saxxmlreader_putFeature,
3000     saxxmlreader_getProperty,
3001     saxxmlreader_putProperty,
3002     saxxmlreader_get_entityResolver,
3003     saxxmlreader_put_entityResolver,
3004     saxxmlreader_get_contentHandler,
3005     saxxmlreader_put_contentHandler,
3006     saxxmlreader_get_dtdHandler,
3007     saxxmlreader_put_dtdHandler,
3008     saxxmlreader_get_errorHandler,
3009     saxxmlreader_put_errorHandler,
3010     saxxmlreader_get_baseURL,
3011     saxxmlreader_put_baseURL,
3012     saxxmlreader_get_secureBaseURL,
3013     saxxmlreader_put_secureBaseURL,
3014     saxxmlreader_parse,
3015     saxxmlreader_parseURL
3016 };
3017
3018 /*** ISAXXMLReader interface ***/
3019 /*** IUnknown methods ***/
3020 static HRESULT WINAPI isaxxmlreader_QueryInterface(ISAXXMLReader* iface, REFIID riid, void **ppvObject)
3021 {
3022     saxreader *This = impl_from_ISAXXMLReader( iface );
3023     return saxxmlreader_QueryInterface(&This->IVBSAXXMLReader_iface, riid, ppvObject);
3024 }
3025
3026 static ULONG WINAPI isaxxmlreader_AddRef(ISAXXMLReader* iface)
3027 {
3028     saxreader *This = impl_from_ISAXXMLReader( iface );
3029     return saxxmlreader_AddRef(&This->IVBSAXXMLReader_iface);
3030 }
3031
3032 static ULONG WINAPI isaxxmlreader_Release(ISAXXMLReader* iface)
3033 {
3034     saxreader *This = impl_from_ISAXXMLReader( iface );
3035     return saxxmlreader_Release(&This->IVBSAXXMLReader_iface);
3036 }
3037
3038 /*** ISAXXMLReader methods ***/
3039 static HRESULT WINAPI isaxxmlreader_getFeature(
3040         ISAXXMLReader* iface,
3041         const WCHAR *pFeature,
3042         VARIANT_BOOL *pValue)
3043 {
3044     saxreader *This = impl_from_ISAXXMLReader( iface );
3045     return IVBSAXXMLReader_getFeature(&This->IVBSAXXMLReader_iface, pFeature, pValue);
3046 }
3047
3048 static HRESULT WINAPI isaxxmlreader_putFeature(
3049         ISAXXMLReader* iface,
3050         const WCHAR *pFeature,
3051         VARIANT_BOOL vfValue)
3052 {
3053     saxreader *This = impl_from_ISAXXMLReader( iface );
3054     return IVBSAXXMLReader_putFeature(&This->IVBSAXXMLReader_iface, pFeature, vfValue);
3055 }
3056
3057 static HRESULT WINAPI isaxxmlreader_getProperty(
3058         ISAXXMLReader* iface,
3059         const WCHAR *prop,
3060         VARIANT *value)
3061 {
3062     saxreader *This = impl_from_ISAXXMLReader( iface );
3063     return internal_getProperty(This, prop, value, FALSE);
3064 }
3065
3066 static HRESULT WINAPI isaxxmlreader_putProperty(
3067         ISAXXMLReader* iface,
3068         const WCHAR *pProp,
3069         VARIANT value)
3070 {
3071     saxreader *This = impl_from_ISAXXMLReader( iface );
3072     return internal_putProperty(This, pProp, value, FALSE);
3073 }
3074
3075 static HRESULT WINAPI isaxxmlreader_getEntityResolver(
3076         ISAXXMLReader* iface,
3077         ISAXEntityResolver **ppEntityResolver)
3078 {
3079     saxreader *This = impl_from_ISAXXMLReader( iface );
3080     return internal_getEntityResolver(This, ppEntityResolver, FALSE);
3081 }
3082
3083 static HRESULT WINAPI isaxxmlreader_putEntityResolver(
3084         ISAXXMLReader* iface,
3085         ISAXEntityResolver *pEntityResolver)
3086 {
3087     saxreader *This = impl_from_ISAXXMLReader( iface );
3088     return internal_putEntityResolver(This, pEntityResolver, FALSE);
3089 }
3090
3091 static HRESULT WINAPI isaxxmlreader_getContentHandler(
3092         ISAXXMLReader* iface,
3093         ISAXContentHandler **handler)
3094 {
3095     saxreader *This = impl_from_ISAXXMLReader( iface );
3096     return saxreader_get_handler(This, SAXContentHandler, FALSE, (void**)handler);
3097 }
3098
3099 static HRESULT WINAPI isaxxmlreader_putContentHandler(
3100     ISAXXMLReader* iface,
3101     ISAXContentHandler *handler)
3102 {
3103     saxreader *This = impl_from_ISAXXMLReader( iface );
3104     return saxreader_put_handler(This, SAXContentHandler, handler, FALSE);
3105 }
3106
3107 static HRESULT WINAPI isaxxmlreader_getDTDHandler(
3108         ISAXXMLReader* iface,
3109         ISAXDTDHandler **handler)
3110 {
3111     saxreader *This = impl_from_ISAXXMLReader( iface );
3112     return saxreader_get_handler(This, SAXDTDHandler, FALSE, (void**)handler);
3113 }
3114
3115 static HRESULT WINAPI isaxxmlreader_putDTDHandler(
3116         ISAXXMLReader* iface,
3117         ISAXDTDHandler *handler)
3118 {
3119     saxreader *This = impl_from_ISAXXMLReader( iface );
3120     return saxreader_put_handler(This, SAXDTDHandler, handler, FALSE);
3121 }
3122
3123 static HRESULT WINAPI isaxxmlreader_getErrorHandler(
3124         ISAXXMLReader* iface,
3125         ISAXErrorHandler **handler)
3126 {
3127     saxreader *This = impl_from_ISAXXMLReader( iface );
3128     return saxreader_get_handler(This, SAXErrorHandler, FALSE, (void**)handler);
3129 }
3130
3131 static HRESULT WINAPI isaxxmlreader_putErrorHandler(ISAXXMLReader* iface, ISAXErrorHandler *handler)
3132 {
3133     saxreader *This = impl_from_ISAXXMLReader( iface );
3134     return saxreader_put_handler(This, SAXErrorHandler, handler, FALSE);
3135 }
3136
3137 static HRESULT WINAPI isaxxmlreader_getBaseURL(
3138         ISAXXMLReader* iface,
3139         const WCHAR **pBaseUrl)
3140 {
3141     saxreader *This = impl_from_ISAXXMLReader( iface );
3142     return IVBSAXXMLReader_get_baseURL(&This->IVBSAXXMLReader_iface, pBaseUrl);
3143 }
3144
3145 static HRESULT WINAPI isaxxmlreader_putBaseURL(
3146         ISAXXMLReader* iface,
3147         const WCHAR *pBaseUrl)
3148 {
3149     saxreader *This = impl_from_ISAXXMLReader( iface );
3150     return IVBSAXXMLReader_put_baseURL(&This->IVBSAXXMLReader_iface, pBaseUrl);
3151 }
3152
3153 static HRESULT WINAPI isaxxmlreader_getSecureBaseURL(
3154         ISAXXMLReader* iface,
3155         const WCHAR **pSecureBaseUrl)
3156 {
3157     saxreader *This = impl_from_ISAXXMLReader( iface );
3158     return IVBSAXXMLReader_get_secureBaseURL(&This->IVBSAXXMLReader_iface, pSecureBaseUrl);
3159 }
3160
3161 static HRESULT WINAPI isaxxmlreader_putSecureBaseURL(
3162         ISAXXMLReader* iface,
3163         const WCHAR *secureBaseUrl)
3164 {
3165     saxreader *This = impl_from_ISAXXMLReader( iface );
3166     return IVBSAXXMLReader_put_secureBaseURL(&This->IVBSAXXMLReader_iface, secureBaseUrl);
3167 }
3168
3169 static HRESULT WINAPI isaxxmlreader_parse(
3170         ISAXXMLReader* iface,
3171         VARIANT varInput)
3172 {
3173     saxreader *This = impl_from_ISAXXMLReader( iface );
3174     return internal_parse(This, varInput, FALSE);
3175 }
3176
3177 static HRESULT WINAPI isaxxmlreader_parseURL(
3178         ISAXXMLReader* iface,
3179         const WCHAR *url)
3180 {
3181     saxreader *This = impl_from_ISAXXMLReader( iface );
3182     return internal_parseURL(This, url, FALSE);
3183 }
3184
3185 static const struct ISAXXMLReaderVtbl SAXXMLReaderVtbl =
3186 {
3187     isaxxmlreader_QueryInterface,
3188     isaxxmlreader_AddRef,
3189     isaxxmlreader_Release,
3190     isaxxmlreader_getFeature,
3191     isaxxmlreader_putFeature,
3192     isaxxmlreader_getProperty,
3193     isaxxmlreader_putProperty,
3194     isaxxmlreader_getEntityResolver,
3195     isaxxmlreader_putEntityResolver,
3196     isaxxmlreader_getContentHandler,
3197     isaxxmlreader_putContentHandler,
3198     isaxxmlreader_getDTDHandler,
3199     isaxxmlreader_putDTDHandler,
3200     isaxxmlreader_getErrorHandler,
3201     isaxxmlreader_putErrorHandler,
3202     isaxxmlreader_getBaseURL,
3203     isaxxmlreader_putBaseURL,
3204     isaxxmlreader_getSecureBaseURL,
3205     isaxxmlreader_putSecureBaseURL,
3206     isaxxmlreader_parse,
3207     isaxxmlreader_parseURL
3208 };
3209
3210 static const tid_t saxreader_iface_tids[] = {
3211     IVBSAXXMLReader_tid,
3212     0
3213 };
3214 static dispex_static_data_t saxreader_dispex = {
3215     NULL,
3216     IVBSAXXMLReader_tid,
3217     NULL,
3218     saxreader_iface_tids
3219 };
3220
3221 HRESULT SAXXMLReader_create(MSXML_VERSION version, IUnknown *outer, LPVOID *ppObj)
3222 {
3223     saxreader *reader;
3224
3225     TRACE("(%p, %p)\n", outer, ppObj);
3226
3227     reader = heap_alloc( sizeof (*reader) );
3228     if( !reader )
3229         return E_OUTOFMEMORY;
3230
3231     reader->IVBSAXXMLReader_iface.lpVtbl = &VBSAXXMLReaderVtbl;
3232     reader->ISAXXMLReader_iface.lpVtbl = &SAXXMLReaderVtbl;
3233     reader->ref = 1;
3234     memset(reader->saxhandlers, 0, sizeof(reader->saxhandlers));
3235     reader->isParsing = FALSE;
3236     reader->xmldecl_version = NULL;
3237     reader->pool.pool = NULL;
3238     reader->pool.index = 0;
3239     reader->pool.len = 0;
3240     reader->features = Namespaces | NamespacePrefixes;
3241     reader->version = version;
3242
3243     init_dispex(&reader->dispex, (IUnknown*)&reader->IVBSAXXMLReader_iface, &saxreader_dispex);
3244
3245     memset(&reader->sax, 0, sizeof(xmlSAXHandler));
3246     reader->sax.initialized = XML_SAX2_MAGIC;
3247     reader->sax.startDocument = libxmlStartDocument;
3248     reader->sax.endDocument = libxmlEndDocument;
3249     reader->sax.startElementNs = libxmlStartElementNS;
3250     reader->sax.endElementNs = libxmlEndElementNS;
3251     reader->sax.characters = libxmlCharacters;
3252     reader->sax.setDocumentLocator = libxmlSetDocumentLocator;
3253     reader->sax.comment = libxmlComment;
3254     reader->sax.error = libxmlFatalError;
3255     reader->sax.fatalError = libxmlFatalError;
3256     reader->sax.cdataBlock = libxmlCDataBlock;
3257
3258     *ppObj = &reader->IVBSAXXMLReader_iface;
3259
3260     TRACE("returning iface %p\n", *ppObj);
3261
3262     return S_OK;
3263 }
3264
3265 #else
3266
3267 HRESULT SAXXMLReader_create(MSXML_VERSION version, IUnknown *pUnkOuter, LPVOID *ppObj)
3268 {
3269     MESSAGE("This program tried to use a SAX XML Reader object, but\n"
3270             "libxml2 support was not present at compile time.\n");
3271     return E_NOTIMPL;
3272 }
3273
3274 #endif