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