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