msi: Don't hide failure of script custom actions.
[wine] / dlls / msxml3 / mxwriter.c
1 /*
2  *    MXWriter implementation
3  *
4  * Copyright 2011-2012 Nikolay Sivov for CodeWeavers
5  * Copyright 2011 Thomas Mullaly
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
22 #define COBJMACROS
23 #include "config.h"
24
25 #include <stdarg.h>
26 #ifdef HAVE_LIBXML2
27 # include <libxml/parser.h>
28 #endif
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "ole2.h"
33
34 #include "msxml6.h"
35
36 #include "wine/debug.h"
37
38 #include "msxml_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
41
42 static const WCHAR emptyW[] = {0};
43 static const WCHAR spaceW[] = {' '};
44 static const WCHAR quotW[]  = {'\"'};
45
46 /* should be ordered as encoding names are sorted */
47 typedef enum
48 {
49     XmlEncoding_ISO_8859_1 = 0,
50     XmlEncoding_ISO_8859_13,
51     XmlEncoding_ISO_8859_15,
52     XmlEncoding_ISO_8859_2,
53     XmlEncoding_ISO_8859_3,
54     XmlEncoding_ISO_8859_4,
55     XmlEncoding_ISO_8859_5,
56     XmlEncoding_ISO_8859_7,
57     XmlEncoding_ISO_8859_9,
58     XmlEncoding_UTF16,
59     XmlEncoding_UTF8,
60     XmlEncoding_Unknown
61 } xml_encoding;
62
63 struct xml_encoding_data
64 {
65     const WCHAR *encoding;
66     xml_encoding enc;
67     UINT cp;
68 };
69
70 static const WCHAR iso_8859_1W[] = {'i','s','o','-','8','8','5','9','-','1',0};
71 static const WCHAR iso_8859_2W[] = {'i','s','o','-','8','8','5','9','-','2',0};
72 static const WCHAR iso_8859_3W[] = {'i','s','o','-','8','8','5','9','-','3',0};
73 static const WCHAR iso_8859_4W[] = {'i','s','o','-','8','8','5','9','-','4',0};
74 static const WCHAR iso_8859_5W[] = {'i','s','o','-','8','8','5','9','-','5',0};
75 static const WCHAR iso_8859_7W[] = {'i','s','o','-','8','8','5','9','-','7',0};
76 static const WCHAR iso_8859_9W[] = {'i','s','o','-','8','8','5','9','-','9',0};
77 static const WCHAR iso_8859_13W[] = {'i','s','o','-','8','8','5','9','-','1','3',0};
78 static const WCHAR iso_8859_15W[] = {'i','s','o','-','8','8','5','9','-','1','5',0};
79 static const WCHAR utf16W[] = {'U','T','F','-','1','6',0};
80 static const WCHAR utf8W[] = {'U','T','F','-','8',0};
81
82 static const struct xml_encoding_data xml_encoding_map[] = {
83     { iso_8859_1W,  XmlEncoding_ISO_8859_1,  28591 },
84     { iso_8859_13W, XmlEncoding_ISO_8859_13, 28603 },
85     { iso_8859_15W, XmlEncoding_ISO_8859_15, 28605 },
86     { iso_8859_2W,  XmlEncoding_ISO_8859_2,  28592 },
87     { iso_8859_3W,  XmlEncoding_ISO_8859_3,  28593 },
88     { iso_8859_4W,  XmlEncoding_ISO_8859_4,  28594 },
89     { iso_8859_5W,  XmlEncoding_ISO_8859_5,  28595 },
90     { iso_8859_7W,  XmlEncoding_ISO_8859_7,  28597 },
91     { iso_8859_9W,  XmlEncoding_ISO_8859_9,  28599 },
92     { utf16W,       XmlEncoding_UTF16,          ~0 },
93     { utf8W,        XmlEncoding_UTF8,      CP_UTF8 }
94 };
95
96 typedef enum
97 {
98     OutputBuffer_Native  = 0x001,
99     OutputBuffer_Encoded = 0x010,
100     OutputBuffer_Both    = 0x100
101 } output_mode;
102
103 typedef enum
104 {
105     MXWriter_BOM = 0,
106     MXWriter_DisableEscaping,
107     MXWriter_Indent,
108     MXWriter_OmitXmlDecl,
109     MXWriter_Standalone,
110     MXWriter_LastProp
111 } mxwriter_prop;
112
113 typedef enum
114 {
115     EscapeValue,
116     EscapeText
117 } escape_mode;
118
119 typedef struct
120 {
121     char *data;
122     unsigned int allocated;
123     unsigned int written;
124 } encoded_buffer;
125
126 typedef struct
127 {
128     encoded_buffer utf16;
129     encoded_buffer encoded;
130     UINT code_page;
131 } output_buffer;
132
133 typedef struct
134 {
135     DispatchEx dispex;
136     IMXWriter IMXWriter_iface;
137     ISAXContentHandler ISAXContentHandler_iface;
138     ISAXLexicalHandler ISAXLexicalHandler_iface;
139     ISAXDeclHandler    ISAXDeclHandler_iface;
140
141     LONG ref;
142     MSXML_VERSION class_version;
143
144     VARIANT_BOOL props[MXWriter_LastProp];
145     BOOL prop_changed;
146     BOOL cdata;
147
148     BSTR version;
149
150     BSTR encoding; /* exact property value */
151     xml_encoding xml_enc;
152
153     /* contains a pending (or not closed yet) element name or NULL if
154        we don't have to close */
155     BSTR element;
156
157     IStream *dest;
158     ULONG dest_written;
159
160     output_buffer *buffer;
161 } mxwriter;
162
163 typedef struct
164 {
165     BSTR qname;
166     BSTR local;
167     BSTR uri;
168     BSTR type;
169     BSTR value;
170 } mxattribute;
171
172 typedef struct
173 {
174     DispatchEx dispex;
175     IMXAttributes IMXAttributes_iface;
176     ISAXAttributes ISAXAttributes_iface;
177     IVBSAXAttributes IVBSAXAttributes_iface;
178     LONG ref;
179
180     MSXML_VERSION class_version;
181
182     mxattribute *attr;
183     int length;
184     int allocated;
185 } mxattributes;
186
187 static inline mxattributes *impl_from_IMXAttributes( IMXAttributes *iface )
188 {
189     return CONTAINING_RECORD(iface, mxattributes, IMXAttributes_iface);
190 }
191
192 static inline mxattributes *impl_from_ISAXAttributes( ISAXAttributes *iface )
193 {
194     return CONTAINING_RECORD(iface, mxattributes, ISAXAttributes_iface);
195 }
196
197 static inline mxattributes *impl_from_IVBSAXAttributes( IVBSAXAttributes *iface )
198 {
199     return CONTAINING_RECORD(iface, mxattributes, IVBSAXAttributes_iface);
200 }
201
202 static HRESULT mxattributes_grow(mxattributes *This)
203 {
204     if (This->length < This->allocated) return S_OK;
205
206     This->allocated *= 2;
207     This->attr = heap_realloc(This->attr, This->allocated*sizeof(mxattribute));
208
209     return This->attr ? S_OK : E_OUTOFMEMORY;
210 }
211
212 static xml_encoding parse_encoding_name(const WCHAR *encoding)
213 {
214     int min, max, n, c;
215
216     min = 0;
217     max = sizeof(xml_encoding_map)/sizeof(struct xml_encoding_data) - 1;
218
219     while (min <= max)
220     {
221         n = (min+max)/2;
222
223         c = strcmpiW(xml_encoding_map[n].encoding, encoding);
224         if (!c)
225             return xml_encoding_map[n].enc;
226
227         if (c > 0)
228             max = n-1;
229         else
230             min = n+1;
231     }
232
233     return XmlEncoding_Unknown;
234 }
235
236 static HRESULT init_encoded_buffer(encoded_buffer *buffer)
237 {
238     const int initial_len = 0x2000;
239     buffer->data = heap_alloc(initial_len);
240     if (!buffer->data) return E_OUTOFMEMORY;
241
242     memset(buffer->data, 0, 4);
243     buffer->allocated = initial_len;
244     buffer->written = 0;
245
246     return S_OK;
247 }
248
249 static void free_encoded_buffer(encoded_buffer *buffer)
250 {
251     heap_free(buffer->data);
252 }
253
254 static HRESULT get_code_page(xml_encoding encoding, UINT *cp)
255 {
256     const struct xml_encoding_data *data;
257
258     if (encoding == XmlEncoding_Unknown)
259     {
260         FIXME("unsupported encoding %d\n", encoding);
261         return E_NOTIMPL;
262     }
263
264     data = &xml_encoding_map[encoding];
265     *cp = data->cp;
266
267     return S_OK;
268 }
269
270 static HRESULT alloc_output_buffer(xml_encoding encoding, output_buffer **buffer)
271 {
272     output_buffer *ret;
273     HRESULT hr;
274
275     ret = heap_alloc(sizeof(*ret));
276     if (!ret) return E_OUTOFMEMORY;
277
278     hr = get_code_page(encoding, &ret->code_page);
279     if (hr != S_OK) {
280         heap_free(ret);
281         return hr;
282     }
283
284     hr = init_encoded_buffer(&ret->utf16);
285     if (hr != S_OK) {
286         heap_free(ret);
287         return hr;
288     }
289
290     if (ret->code_page == CP_UTF8) {
291         hr = init_encoded_buffer(&ret->encoded);
292         if (hr != S_OK) {
293             free_encoded_buffer(&ret->utf16);
294             heap_free(ret);
295             return hr;
296         }
297     }
298     else
299         memset(&ret->encoded, 0, sizeof(ret->encoded));
300
301     *buffer = ret;
302
303     return S_OK;
304 }
305
306 static void free_output_buffer(output_buffer *buffer)
307 {
308     free_encoded_buffer(&buffer->encoded);
309     free_encoded_buffer(&buffer->utf16);
310     heap_free(buffer);
311 }
312
313 static void grow_buffer(encoded_buffer *buffer, int length)
314 {
315     /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
316     if (buffer->allocated < buffer->written + length + 4)
317     {
318         int grown_size = max(2*buffer->allocated, buffer->allocated + length);
319         buffer->data = heap_realloc(buffer->data, grown_size);
320         buffer->allocated = grown_size;
321     }
322 }
323
324 static HRESULT write_output_buffer_mode(output_buffer *buffer, output_mode mode, const WCHAR *data, int len)
325 {
326     int length;
327     char *ptr;
328
329     if (mode & (OutputBuffer_Encoded | OutputBuffer_Both)) {
330         if (buffer->code_page != ~0)
331         {
332             length = WideCharToMultiByte(buffer->code_page, 0, data, len, NULL, 0, NULL, NULL);
333             grow_buffer(&buffer->encoded, length);
334             ptr = buffer->encoded.data + buffer->encoded.written;
335             length = WideCharToMultiByte(buffer->code_page, 0, data, len, ptr, length, NULL, NULL);
336             buffer->encoded.written += len == -1 ? length-1 : length;
337         }
338     }
339
340     if (mode & (OutputBuffer_Native | OutputBuffer_Both)) {
341         /* WCHAR data just copied */
342         length = len == -1 ? strlenW(data) : len;
343         if (length)
344         {
345             length *= sizeof(WCHAR);
346
347             grow_buffer(&buffer->utf16, length);
348             ptr = buffer->utf16.data + buffer->utf16.written;
349
350             memcpy(ptr, data, length);
351             buffer->utf16.written += length;
352             ptr += length;
353             /* null termination */
354             memset(ptr, 0, sizeof(WCHAR));
355         }
356     }
357
358     return S_OK;
359 }
360
361 static HRESULT write_output_buffer(output_buffer *buffer, const WCHAR *data, int len)
362 {
363     return write_output_buffer_mode(buffer, OutputBuffer_Both, data, len);
364 }
365
366 static HRESULT write_output_buffer_quoted(output_buffer *buffer, const WCHAR *data, int len)
367 {
368     write_output_buffer(buffer, quotW, 1);
369     write_output_buffer(buffer, data, len);
370     write_output_buffer(buffer, quotW, 1);
371
372     return S_OK;
373 }
374
375 /* frees buffer data, reallocates with a default lengths */
376 static void close_output_buffer(mxwriter *This)
377 {
378     heap_free(This->buffer->utf16.data);
379     heap_free(This->buffer->encoded.data);
380     init_encoded_buffer(&This->buffer->utf16);
381     init_encoded_buffer(&This->buffer->encoded);
382     get_code_page(This->xml_enc, &This->buffer->code_page);
383 }
384
385 /* escapes special characters like:
386    '<' -> "&lt;"
387    '&' -> "&amp;"
388    '"' -> "&quot;"
389    '>' -> "&gt;"
390 */
391 static WCHAR *get_escaped_string(const WCHAR *str, escape_mode mode, int *len)
392 {
393     static const WCHAR ltW[]    = {'&','l','t',';'};
394     static const WCHAR ampW[]   = {'&','a','m','p',';'};
395     static const WCHAR equotW[] = {'&','q','u','o','t',';'};
396     static const WCHAR gtW[]    = {'&','g','t',';'};
397
398     const int default_alloc = 100;
399     const int grow_thresh = 10;
400     int p = *len, conv_len;
401     WCHAR *ptr, *ret;
402
403     /* default buffer size to something if length is unknown */
404     conv_len = *len == -1 ? default_alloc : max(2**len, default_alloc);
405     ptr = ret = heap_alloc(conv_len*sizeof(WCHAR));
406
407     while (*str && p)
408     {
409         if (ptr - ret > conv_len - grow_thresh)
410         {
411             int written = ptr - ret;
412             conv_len *= 2;
413             ptr = ret = heap_realloc(ret, conv_len*sizeof(WCHAR));
414             ptr += written;
415         }
416
417         switch (*str)
418         {
419         case '<':
420             memcpy(ptr, ltW, sizeof(ltW));
421             ptr += sizeof(ltW)/sizeof(WCHAR);
422             break;
423         case '&':
424             memcpy(ptr, ampW, sizeof(ampW));
425             ptr += sizeof(ampW)/sizeof(WCHAR);
426             break;
427         case '>':
428             memcpy(ptr, gtW, sizeof(gtW));
429             ptr += sizeof(gtW)/sizeof(WCHAR);
430             break;
431         case '"':
432             if (mode == EscapeValue)
433             {
434                 memcpy(ptr, equotW, sizeof(equotW));
435                 ptr += sizeof(equotW)/sizeof(WCHAR);
436                 break;
437             }
438             /* fallthrough for text mode */
439         default:
440             *ptr++ = *str;
441             break;
442         }
443
444         str++;
445         if (*len != -1) p--;
446     }
447
448     if (*len != -1) *len = ptr-ret;
449     *++ptr = 0;
450
451     return ret;
452 }
453
454 static void write_prolog_buffer(const mxwriter *This)
455 {
456     static const WCHAR versionW[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','='};
457     static const WCHAR encodingW[] = {' ','e','n','c','o','d','i','n','g','=','\"'};
458     static const WCHAR standaloneW[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
459     static const WCHAR yesW[] = {'y','e','s','\"','?','>'};
460     static const WCHAR noW[] = {'n','o','\"','?','>'};
461     static const WCHAR crlfW[] = {'\r','\n'};
462
463     /* version */
464     write_output_buffer(This->buffer, versionW, sizeof(versionW)/sizeof(WCHAR));
465     write_output_buffer_quoted(This->buffer, This->version, -1);
466
467     /* encoding */
468     write_output_buffer(This->buffer, encodingW, sizeof(encodingW)/sizeof(WCHAR));
469
470     /* always write UTF-16 to WCHAR buffer */
471     write_output_buffer_mode(This->buffer, OutputBuffer_Native, utf16W, sizeof(utf16W)/sizeof(WCHAR) - 1);
472     write_output_buffer_mode(This->buffer, OutputBuffer_Encoded, This->encoding, -1);
473     write_output_buffer(This->buffer, quotW, 1);
474
475     /* standalone */
476     write_output_buffer(This->buffer, standaloneW, sizeof(standaloneW)/sizeof(WCHAR));
477     if (This->props[MXWriter_Standalone] == VARIANT_TRUE)
478         write_output_buffer(This->buffer, yesW, sizeof(yesW)/sizeof(WCHAR));
479     else
480         write_output_buffer(This->buffer, noW, sizeof(noW)/sizeof(WCHAR));
481
482     write_output_buffer(This->buffer, crlfW, sizeof(crlfW)/sizeof(WCHAR));
483 }
484
485 /* Attempts to the write data from the mxwriter's buffer to
486  * the destination stream (if there is one).
487  */
488 static HRESULT write_data_to_stream(mxwriter *This)
489 {
490     encoded_buffer *buffer;
491     ULONG written = 0;
492     HRESULT hr;
493
494     if (!This->dest)
495         return S_OK;
496
497     if (This->xml_enc != XmlEncoding_UTF16)
498         buffer = &This->buffer->encoded;
499     else
500         buffer = &This->buffer->utf16;
501
502     if (This->dest_written > buffer->written) {
503         ERR("Failed sanity check! Not sure what to do... (%d > %d)\n", This->dest_written, buffer->written);
504         return E_FAIL;
505     } else if (This->dest_written == buffer->written && This->xml_enc != XmlEncoding_UTF8)
506         /* Windows seems to make an empty write call when the encoding is UTF-8 and
507          * all the data has been written to the stream. It doesn't seem make this call
508          * for any other encodings.
509          */
510         return S_OK;
511
512     /* Write the current content from the output buffer into 'dest'.
513      * TODO: Check what Windows does if the IStream doesn't write all of
514      *       the data we give it at once.
515      */
516     hr = IStream_Write(This->dest, buffer->data+This->dest_written,
517                          buffer->written-This->dest_written, &written);
518     if (FAILED(hr)) {
519         WARN("Failed to write data to IStream (0x%08x)\n", hr);
520         return hr;
521     }
522
523     This->dest_written += written;
524     return hr;
525 }
526
527 /* Newly added element start tag left unclosed cause for empty elements
528    we have to close it differently. */
529 static void close_element_starttag(const mxwriter *This)
530 {
531     static const WCHAR gtW[] = {'>'};
532     if (!This->element) return;
533     write_output_buffer(This->buffer, gtW, 1);
534 }
535
536 static void set_element_name(mxwriter *This, const WCHAR *name, int len)
537 {
538     SysFreeString(This->element);
539     This->element = name ? SysAllocStringLen(name, len) : NULL;
540 }
541
542 static inline HRESULT flush_output_buffer(mxwriter *This)
543 {
544     close_element_starttag(This);
545     set_element_name(This, NULL, 0);
546     This->cdata = FALSE;
547     return write_data_to_stream(This);
548 }
549
550 /* Resets the mxwriter's output buffer by closing it, then creating a new
551  * output buffer using the given encoding.
552  */
553 static inline void reset_output_buffer(mxwriter *This)
554 {
555     close_output_buffer(This);
556     This->dest_written = 0;
557 }
558
559 static HRESULT writer_set_property(mxwriter *writer, mxwriter_prop property, VARIANT_BOOL value)
560 {
561     writer->props[property] = value;
562     writer->prop_changed = TRUE;
563     return S_OK;
564 }
565
566 static HRESULT writer_get_property(const mxwriter *writer, mxwriter_prop property, VARIANT_BOOL *value)
567 {
568     if (!value) return E_POINTER;
569     *value = writer->props[property];
570     return S_OK;
571 }
572
573 static inline mxwriter *impl_from_IMXWriter(IMXWriter *iface)
574 {
575     return CONTAINING_RECORD(iface, mxwriter, IMXWriter_iface);
576 }
577
578 static inline mxwriter *impl_from_ISAXContentHandler(ISAXContentHandler *iface)
579 {
580     return CONTAINING_RECORD(iface, mxwriter, ISAXContentHandler_iface);
581 }
582
583 static inline mxwriter *impl_from_ISAXLexicalHandler(ISAXLexicalHandler *iface)
584 {
585     return CONTAINING_RECORD(iface, mxwriter, ISAXLexicalHandler_iface);
586 }
587
588 static inline mxwriter *impl_from_ISAXDeclHandler(ISAXDeclHandler *iface)
589 {
590     return CONTAINING_RECORD(iface, mxwriter, ISAXDeclHandler_iface);
591 }
592
593 static HRESULT WINAPI mxwriter_QueryInterface(IMXWriter *iface, REFIID riid, void **obj)
594 {
595     mxwriter *This = impl_from_IMXWriter( iface );
596
597     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
598
599     *obj = NULL;
600
601     if ( IsEqualGUID( riid, &IID_IMXWriter ) ||
602          IsEqualGUID( riid, &IID_IDispatch ) ||
603          IsEqualGUID( riid, &IID_IUnknown ) )
604     {
605         *obj = &This->IMXWriter_iface;
606     }
607     else if ( IsEqualGUID( riid, &IID_ISAXContentHandler ) )
608     {
609         *obj = &This->ISAXContentHandler_iface;
610     }
611     else if ( IsEqualGUID( riid, &IID_ISAXLexicalHandler ) )
612     {
613         *obj = &This->ISAXLexicalHandler_iface;
614     }
615     else if ( IsEqualGUID( riid, &IID_ISAXDeclHandler ) )
616     {
617         *obj = &This->ISAXDeclHandler_iface;
618     }
619     else if (dispex_query_interface(&This->dispex, riid, obj))
620     {
621         return *obj ? S_OK : E_NOINTERFACE;
622     }
623     else
624     {
625         ERR("interface %s not implemented\n", debugstr_guid(riid));
626         *obj = NULL;
627         return E_NOINTERFACE;
628     }
629
630     IMXWriter_AddRef(iface);
631     return S_OK;
632 }
633
634 static ULONG WINAPI mxwriter_AddRef(IMXWriter *iface)
635 {
636     mxwriter *This = impl_from_IMXWriter( iface );
637     LONG ref = InterlockedIncrement(&This->ref);
638
639     TRACE("(%p)->(%d)\n", This, ref);
640
641     return ref;
642 }
643
644 static ULONG WINAPI mxwriter_Release(IMXWriter *iface)
645 {
646     mxwriter *This = impl_from_IMXWriter( iface );
647     ULONG ref = InterlockedDecrement(&This->ref);
648
649     TRACE("(%p)->(%d)\n", This, ref);
650
651     if(!ref)
652     {
653         /* Windows flushes the buffer when the interface is destroyed. */
654         flush_output_buffer(This);
655         free_output_buffer(This->buffer);
656
657         if (This->dest) IStream_Release(This->dest);
658         SysFreeString(This->version);
659         SysFreeString(This->encoding);
660
661         SysFreeString(This->element);
662         release_dispex(&This->dispex);
663         heap_free(This);
664     }
665
666     return ref;
667 }
668
669 static HRESULT WINAPI mxwriter_GetTypeInfoCount(IMXWriter *iface, UINT* pctinfo)
670 {
671     mxwriter *This = impl_from_IMXWriter( iface );
672     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
673 }
674
675 static HRESULT WINAPI mxwriter_GetTypeInfo(
676     IMXWriter *iface,
677     UINT iTInfo, LCID lcid,
678     ITypeInfo** ppTInfo )
679 {
680     mxwriter *This = impl_from_IMXWriter( iface );
681     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
682         iTInfo, lcid, ppTInfo);
683 }
684
685 static HRESULT WINAPI mxwriter_GetIDsOfNames(
686     IMXWriter *iface,
687     REFIID riid, LPOLESTR* rgszNames,
688     UINT cNames, LCID lcid, DISPID* rgDispId )
689 {
690     mxwriter *This = impl_from_IMXWriter( iface );
691     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
692         riid, rgszNames, cNames, lcid, rgDispId);
693 }
694
695 static HRESULT WINAPI mxwriter_Invoke(
696     IMXWriter *iface,
697     DISPID dispIdMember, REFIID riid, LCID lcid,
698     WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
699     EXCEPINFO* pExcepInfo, UINT* puArgErr )
700 {
701     mxwriter *This = impl_from_IMXWriter( iface );
702     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
703         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
704 }
705
706 static HRESULT WINAPI mxwriter_put_output(IMXWriter *iface, VARIANT dest)
707 {
708     mxwriter *This = impl_from_IMXWriter( iface );
709     HRESULT hr;
710
711     TRACE("(%p)->(%s)\n", This, debugstr_variant(&dest));
712
713     hr = flush_output_buffer(This);
714     if (FAILED(hr))
715         return hr;
716
717     switch (V_VT(&dest))
718     {
719     case VT_EMPTY:
720     {
721         if (This->dest) IStream_Release(This->dest);
722         This->dest = NULL;
723         reset_output_buffer(This);
724         break;
725     }
726     case VT_UNKNOWN:
727     {
728         IStream *stream;
729
730         hr = IUnknown_QueryInterface(V_UNKNOWN(&dest), &IID_IStream, (void**)&stream);
731         if (hr == S_OK)
732         {
733             /* Recreate the output buffer to make sure it's using the correct encoding. */
734             reset_output_buffer(This);
735
736             if (This->dest) IStream_Release(This->dest);
737             This->dest = stream;
738             break;
739         }
740
741         FIXME("unhandled interface type for VT_UNKNOWN destination\n");
742         return E_NOTIMPL;
743     }
744     default:
745         FIXME("unhandled destination type %s\n", debugstr_variant(&dest));
746         return E_NOTIMPL;
747     }
748
749     return S_OK;
750 }
751
752 static HRESULT WINAPI mxwriter_get_output(IMXWriter *iface, VARIANT *dest)
753 {
754     mxwriter *This = impl_from_IMXWriter( iface );
755
756     TRACE("(%p)->(%p)\n", This, dest);
757
758     if (!This->dest)
759     {
760         HRESULT hr = flush_output_buffer(This);
761         if (FAILED(hr))
762             return hr;
763
764         V_VT(dest)   = VT_BSTR;
765         V_BSTR(dest) = SysAllocString((WCHAR*)This->buffer->utf16.data);
766
767         return S_OK;
768     }
769     else
770         FIXME("not implemented when stream is set up\n");
771
772     return E_NOTIMPL;
773 }
774
775 static HRESULT WINAPI mxwriter_put_encoding(IMXWriter *iface, BSTR encoding)
776 {
777     mxwriter *This = impl_from_IMXWriter( iface );
778     xml_encoding enc;
779     HRESULT hr;
780
781     TRACE("(%p)->(%s)\n", This, debugstr_w(encoding));
782
783     enc = parse_encoding_name(encoding);
784     if (enc == XmlEncoding_Unknown)
785     {
786         FIXME("unsupported encoding %s\n", debugstr_w(encoding));
787         return E_INVALIDARG;
788     }
789
790     hr = flush_output_buffer(This);
791     if (FAILED(hr))
792         return hr;
793
794     SysReAllocString(&This->encoding, encoding);
795     This->xml_enc = enc;
796
797     TRACE("got encoding %d\n", This->xml_enc);
798     reset_output_buffer(This);
799     return S_OK;
800 }
801
802 static HRESULT WINAPI mxwriter_get_encoding(IMXWriter *iface, BSTR *encoding)
803 {
804     mxwriter *This = impl_from_IMXWriter( iface );
805
806     TRACE("(%p)->(%p)\n", This, encoding);
807
808     if (!encoding) return E_POINTER;
809
810     *encoding = SysAllocString(This->encoding);
811     if (!*encoding) return E_OUTOFMEMORY;
812
813     return S_OK;
814 }
815
816 static HRESULT WINAPI mxwriter_put_byteOrderMark(IMXWriter *iface, VARIANT_BOOL value)
817 {
818     mxwriter *This = impl_from_IMXWriter( iface );
819
820     TRACE("(%p)->(%d)\n", This, value);
821     return writer_set_property(This, MXWriter_BOM, value);
822 }
823
824 static HRESULT WINAPI mxwriter_get_byteOrderMark(IMXWriter *iface, VARIANT_BOOL *value)
825 {
826     mxwriter *This = impl_from_IMXWriter( iface );
827
828     TRACE("(%p)->(%p)\n", This, value);
829     return writer_get_property(This, MXWriter_BOM, value);
830 }
831
832 static HRESULT WINAPI mxwriter_put_indent(IMXWriter *iface, VARIANT_BOOL value)
833 {
834     mxwriter *This = impl_from_IMXWriter( iface );
835
836     TRACE("(%p)->(%d)\n", This, value);
837     return writer_set_property(This, MXWriter_Indent, value);
838 }
839
840 static HRESULT WINAPI mxwriter_get_indent(IMXWriter *iface, VARIANT_BOOL *value)
841 {
842     mxwriter *This = impl_from_IMXWriter( iface );
843
844     TRACE("(%p)->(%p)\n", This, value);
845     return writer_get_property(This, MXWriter_Indent, value);
846 }
847
848 static HRESULT WINAPI mxwriter_put_standalone(IMXWriter *iface, VARIANT_BOOL value)
849 {
850     mxwriter *This = impl_from_IMXWriter( iface );
851
852     TRACE("(%p)->(%d)\n", This, value);
853     return writer_set_property(This, MXWriter_Standalone, value);
854 }
855
856 static HRESULT WINAPI mxwriter_get_standalone(IMXWriter *iface, VARIANT_BOOL *value)
857 {
858     mxwriter *This = impl_from_IMXWriter( iface );
859
860     TRACE("(%p)->(%p)\n", This, value);
861     return writer_get_property(This, MXWriter_Standalone, value);
862 }
863
864 static HRESULT WINAPI mxwriter_put_omitXMLDeclaration(IMXWriter *iface, VARIANT_BOOL value)
865 {
866     mxwriter *This = impl_from_IMXWriter( iface );
867
868     TRACE("(%p)->(%d)\n", This, value);
869     return writer_set_property(This, MXWriter_OmitXmlDecl, value);
870 }
871
872 static HRESULT WINAPI mxwriter_get_omitXMLDeclaration(IMXWriter *iface, VARIANT_BOOL *value)
873 {
874     mxwriter *This = impl_from_IMXWriter( iface );
875
876     TRACE("(%p)->(%p)\n", This, value);
877     return writer_get_property(This, MXWriter_OmitXmlDecl, value);
878 }
879
880 static HRESULT WINAPI mxwriter_put_version(IMXWriter *iface, BSTR version)
881 {
882     mxwriter *This = impl_from_IMXWriter( iface );
883
884     TRACE("(%p)->(%s)\n", This, debugstr_w(version));
885
886     if (!version) return E_INVALIDARG;
887
888     SysFreeString(This->version);
889     This->version = SysAllocString(version);
890
891     return S_OK;
892 }
893
894 static HRESULT WINAPI mxwriter_get_version(IMXWriter *iface, BSTR *version)
895 {
896     mxwriter *This = impl_from_IMXWriter( iface );
897
898     TRACE("(%p)->(%p)\n", This, version);
899
900     if (!version) return E_POINTER;
901
902     return return_bstr(This->version, version);
903 }
904
905 static HRESULT WINAPI mxwriter_put_disableOutputEscaping(IMXWriter *iface, VARIANT_BOOL value)
906 {
907     mxwriter *This = impl_from_IMXWriter( iface );
908
909     TRACE("(%p)->(%d)\n", This, value);
910     return writer_set_property(This, MXWriter_DisableEscaping, value);
911 }
912
913 static HRESULT WINAPI mxwriter_get_disableOutputEscaping(IMXWriter *iface, VARIANT_BOOL *value)
914 {
915     mxwriter *This = impl_from_IMXWriter( iface );
916
917     TRACE("(%p)->(%p)\n", This, value);
918     return writer_get_property(This, MXWriter_DisableEscaping, value);
919 }
920
921 static HRESULT WINAPI mxwriter_flush(IMXWriter *iface)
922 {
923     mxwriter *This = impl_from_IMXWriter( iface );
924     TRACE("(%p)\n", This);
925     return flush_output_buffer(This);
926 }
927
928 static const struct IMXWriterVtbl MXWriterVtbl =
929 {
930     mxwriter_QueryInterface,
931     mxwriter_AddRef,
932     mxwriter_Release,
933     mxwriter_GetTypeInfoCount,
934     mxwriter_GetTypeInfo,
935     mxwriter_GetIDsOfNames,
936     mxwriter_Invoke,
937     mxwriter_put_output,
938     mxwriter_get_output,
939     mxwriter_put_encoding,
940     mxwriter_get_encoding,
941     mxwriter_put_byteOrderMark,
942     mxwriter_get_byteOrderMark,
943     mxwriter_put_indent,
944     mxwriter_get_indent,
945     mxwriter_put_standalone,
946     mxwriter_get_standalone,
947     mxwriter_put_omitXMLDeclaration,
948     mxwriter_get_omitXMLDeclaration,
949     mxwriter_put_version,
950     mxwriter_get_version,
951     mxwriter_put_disableOutputEscaping,
952     mxwriter_get_disableOutputEscaping,
953     mxwriter_flush
954 };
955
956 /*** ISAXContentHandler ***/
957 static HRESULT WINAPI SAXContentHandler_QueryInterface(
958     ISAXContentHandler *iface,
959     REFIID riid,
960     void **obj)
961 {
962     mxwriter *This = impl_from_ISAXContentHandler( iface );
963     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
964 }
965
966 static ULONG WINAPI SAXContentHandler_AddRef(ISAXContentHandler *iface)
967 {
968     mxwriter *This = impl_from_ISAXContentHandler( iface );
969     return IMXWriter_AddRef(&This->IMXWriter_iface);
970 }
971
972 static ULONG WINAPI SAXContentHandler_Release(ISAXContentHandler *iface)
973 {
974     mxwriter *This = impl_from_ISAXContentHandler( iface );
975     return IMXWriter_Release(&This->IMXWriter_iface);
976 }
977
978 static HRESULT WINAPI SAXContentHandler_putDocumentLocator(
979     ISAXContentHandler *iface,
980     ISAXLocator *locator)
981 {
982     mxwriter *This = impl_from_ISAXContentHandler( iface );
983     FIXME("(%p)->(%p)\n", This, locator);
984     return E_NOTIMPL;
985 }
986
987 static HRESULT WINAPI SAXContentHandler_startDocument(ISAXContentHandler *iface)
988 {
989     mxwriter *This = impl_from_ISAXContentHandler( iface );
990
991     TRACE("(%p)\n", This);
992
993     /* If properties have been changed since the last "endDocument" call
994      * we need to reset the output buffer. If we don't the output buffer
995      * could end up with multiple XML documents in it, plus this seems to
996      * be how Windows works.
997      */
998     if (This->prop_changed) {
999         reset_output_buffer(This);
1000         This->prop_changed = FALSE;
1001     }
1002
1003     if (This->props[MXWriter_OmitXmlDecl] == VARIANT_TRUE) return S_OK;
1004
1005     write_prolog_buffer(This);
1006
1007     if (This->dest && This->xml_enc == XmlEncoding_UTF16) {
1008         static const char utf16BOM[] = {0xff,0xfe};
1009
1010         if (This->props[MXWriter_BOM] == VARIANT_TRUE)
1011             /* Windows passes a NULL pointer as the pcbWritten parameter and
1012              * ignores any error codes returned from this Write call.
1013              */
1014             IStream_Write(This->dest, utf16BOM, sizeof(utf16BOM), NULL);
1015     }
1016
1017     return S_OK;
1018 }
1019
1020 static HRESULT WINAPI SAXContentHandler_endDocument(ISAXContentHandler *iface)
1021 {
1022     mxwriter *This = impl_from_ISAXContentHandler( iface );
1023     TRACE("(%p)\n", This);
1024     This->prop_changed = FALSE;
1025     return flush_output_buffer(This);
1026 }
1027
1028 static HRESULT WINAPI SAXContentHandler_startPrefixMapping(
1029     ISAXContentHandler *iface,
1030     const WCHAR *prefix,
1031     int nprefix,
1032     const WCHAR *uri,
1033     int nuri)
1034 {
1035     mxwriter *This = impl_from_ISAXContentHandler( iface );
1036     FIXME("(%p)->(%s %s)\n", This, debugstr_wn(prefix, nprefix), debugstr_wn(uri, nuri));
1037     return E_NOTIMPL;
1038 }
1039
1040 static HRESULT WINAPI SAXContentHandler_endPrefixMapping(
1041     ISAXContentHandler *iface,
1042     const WCHAR *prefix,
1043     int nprefix)
1044 {
1045     mxwriter *This = impl_from_ISAXContentHandler( iface );
1046     FIXME("(%p)->(%s)\n", This, debugstr_wn(prefix, nprefix));
1047     return E_NOTIMPL;
1048 }
1049
1050 static HRESULT WINAPI SAXContentHandler_startElement(
1051     ISAXContentHandler *iface,
1052     const WCHAR *namespaceUri,
1053     int nnamespaceUri,
1054     const WCHAR *local_name,
1055     int nlocal_name,
1056     const WCHAR *QName,
1057     int nQName,
1058     ISAXAttributes *attr)
1059 {
1060     mxwriter *This = impl_from_ISAXContentHandler( iface );
1061     static const WCHAR ltW[] = {'<'};
1062
1063     TRACE("(%p)->(%s %s %s %p)\n", This, debugstr_wn(namespaceUri, nnamespaceUri),
1064         debugstr_wn(local_name, nlocal_name), debugstr_wn(QName, nQName), attr);
1065
1066     if ((!namespaceUri || !local_name || !QName) && This->class_version != MSXML6)
1067         return E_INVALIDARG;
1068
1069     close_element_starttag(This);
1070     set_element_name(This, QName ? QName  : emptyW,
1071                            QName ? nQName : 0);
1072
1073     write_output_buffer(This->buffer, ltW, 1);
1074     write_output_buffer(This->buffer, QName, nQName);
1075
1076     if (attr)
1077     {
1078         int length, i, escape;
1079         HRESULT hr;
1080
1081         hr = ISAXAttributes_getLength(attr, &length);
1082         if (FAILED(hr)) return hr;
1083
1084         escape = This->props[MXWriter_DisableEscaping] == VARIANT_FALSE ||
1085             (This->class_version == MSXML4 || This->class_version == MSXML6);
1086
1087         for (i = 0; i < length; i++)
1088         {
1089             static const WCHAR eqW[] = {'='};
1090             const WCHAR *str;
1091             int len = 0;
1092
1093             hr = ISAXAttributes_getQName(attr, i, &str, &len);
1094             if (FAILED(hr)) return hr;
1095
1096             /* space separator in front of every attribute */
1097             write_output_buffer(This->buffer, spaceW, 1);
1098             write_output_buffer(This->buffer, str, len);
1099
1100             write_output_buffer(This->buffer, eqW, 1);
1101
1102             len = 0;
1103             hr = ISAXAttributes_getValue(attr, i, &str, &len);
1104             if (FAILED(hr)) return hr;
1105
1106             if (escape)
1107             {
1108                 WCHAR *escaped = get_escaped_string(str, EscapeValue, &len);
1109                 write_output_buffer_quoted(This->buffer, escaped, len);
1110                 heap_free(escaped);
1111             }
1112             else
1113                 write_output_buffer_quoted(This->buffer, str, len);
1114         }
1115     }
1116
1117     return S_OK;
1118 }
1119
1120 static HRESULT WINAPI SAXContentHandler_endElement(
1121     ISAXContentHandler *iface,
1122     const WCHAR *namespaceUri,
1123     int nnamespaceUri,
1124     const WCHAR * local_name,
1125     int nlocal_name,
1126     const WCHAR *QName,
1127     int nQName)
1128 {
1129     mxwriter *This = impl_from_ISAXContentHandler( iface );
1130
1131     TRACE("(%p)->(%s:%d %s:%d %s:%d)\n", This, debugstr_wn(namespaceUri, nnamespaceUri), nnamespaceUri,
1132         debugstr_wn(local_name, nlocal_name), nlocal_name, debugstr_wn(QName, nQName), nQName);
1133
1134     if ((!namespaceUri || !local_name || !QName) && This->class_version != MSXML6)
1135         return E_INVALIDARG;
1136
1137     if (This->element && QName && !strncmpW(This->element, QName, nQName))
1138     {
1139         static const WCHAR closeW[] = {'/','>'};
1140
1141         write_output_buffer(This->buffer, closeW, 2);
1142     }
1143     else
1144     {
1145         static const WCHAR closetagW[] = {'<','/'};
1146         static const WCHAR gtW[] = {'>'};
1147
1148         write_output_buffer(This->buffer, closetagW, 2);
1149         write_output_buffer(This->buffer, QName, nQName);
1150         write_output_buffer(This->buffer, gtW, 1);
1151     }
1152
1153     set_element_name(This, NULL, 0);
1154
1155     return S_OK;
1156 }
1157
1158 static HRESULT WINAPI SAXContentHandler_characters(
1159     ISAXContentHandler *iface,
1160     const WCHAR *chars,
1161     int nchars)
1162 {
1163     mxwriter *This = impl_from_ISAXContentHandler( iface );
1164
1165     TRACE("(%p)->(%s:%d)\n", This, debugstr_wn(chars, nchars), nchars);
1166
1167     if (!chars) return E_INVALIDARG;
1168
1169     close_element_starttag(This);
1170     set_element_name(This, NULL, 0);
1171
1172     if (nchars)
1173     {
1174         if (This->cdata || This->props[MXWriter_DisableEscaping] == VARIANT_TRUE)
1175             write_output_buffer(This->buffer, chars, nchars);
1176         else
1177         {
1178             int len = nchars;
1179             WCHAR *escaped;
1180
1181             escaped = get_escaped_string(chars, EscapeText, &len);
1182             write_output_buffer(This->buffer, escaped, len);
1183             heap_free(escaped);
1184         }
1185     }
1186
1187     return S_OK;
1188 }
1189
1190 static HRESULT WINAPI SAXContentHandler_ignorableWhitespace(
1191     ISAXContentHandler *iface,
1192     const WCHAR *chars,
1193     int nchars)
1194 {
1195     mxwriter *This = impl_from_ISAXContentHandler( iface );
1196
1197     TRACE("(%p)->(%s)\n", This, debugstr_wn(chars, nchars));
1198
1199     if (!chars) return E_INVALIDARG;
1200
1201     write_output_buffer(This->buffer, chars, nchars);
1202
1203     return S_OK;
1204 }
1205
1206 static HRESULT WINAPI SAXContentHandler_processingInstruction(
1207     ISAXContentHandler *iface,
1208     const WCHAR *target,
1209     int ntarget,
1210     const WCHAR *data,
1211     int ndata)
1212 {
1213     mxwriter *This = impl_from_ISAXContentHandler( iface );
1214     static const WCHAR openpiW[] = {'<','?'};
1215     static const WCHAR closepiW[] = {'?','>','\r','\n'};
1216
1217     TRACE("(%p)->(%s %s)\n", This, debugstr_wn(target, ntarget), debugstr_wn(data, ndata));
1218
1219     if (!target) return E_INVALIDARG;
1220
1221     write_output_buffer(This->buffer, openpiW, sizeof(openpiW)/sizeof(WCHAR));
1222
1223     if (*target)
1224         write_output_buffer(This->buffer, target, ntarget);
1225
1226     if (data && *data && ndata)
1227     {
1228         write_output_buffer(This->buffer, spaceW, 1);
1229         write_output_buffer(This->buffer, data, ndata);
1230     }
1231
1232     write_output_buffer(This->buffer, closepiW, sizeof(closepiW)/sizeof(WCHAR));
1233
1234     return S_OK;
1235 }
1236
1237 static HRESULT WINAPI SAXContentHandler_skippedEntity(
1238     ISAXContentHandler *iface,
1239     const WCHAR *name,
1240     int nname)
1241 {
1242     mxwriter *This = impl_from_ISAXContentHandler( iface );
1243     FIXME("(%p)->(%s)\n", This, debugstr_wn(name, nname));
1244     return E_NOTIMPL;
1245 }
1246
1247 static const struct ISAXContentHandlerVtbl SAXContentHandlerVtbl =
1248 {
1249     SAXContentHandler_QueryInterface,
1250     SAXContentHandler_AddRef,
1251     SAXContentHandler_Release,
1252     SAXContentHandler_putDocumentLocator,
1253     SAXContentHandler_startDocument,
1254     SAXContentHandler_endDocument,
1255     SAXContentHandler_startPrefixMapping,
1256     SAXContentHandler_endPrefixMapping,
1257     SAXContentHandler_startElement,
1258     SAXContentHandler_endElement,
1259     SAXContentHandler_characters,
1260     SAXContentHandler_ignorableWhitespace,
1261     SAXContentHandler_processingInstruction,
1262     SAXContentHandler_skippedEntity
1263 };
1264
1265 /*** ISAXLexicalHandler ***/
1266 static HRESULT WINAPI SAXLexicalHandler_QueryInterface(ISAXLexicalHandler *iface,
1267     REFIID riid, void **obj)
1268 {
1269     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1270     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
1271 }
1272
1273 static ULONG WINAPI SAXLexicalHandler_AddRef(ISAXLexicalHandler *iface)
1274 {
1275     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1276     return IMXWriter_AddRef(&This->IMXWriter_iface);
1277 }
1278
1279 static ULONG WINAPI SAXLexicalHandler_Release(ISAXLexicalHandler *iface)
1280 {
1281     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1282     return IMXWriter_Release(&This->IMXWriter_iface);
1283 }
1284
1285 static HRESULT WINAPI SAXLexicalHandler_startDTD(ISAXLexicalHandler *iface,
1286     const WCHAR *name, int name_len, const WCHAR *publicId, int publicId_len,
1287     const WCHAR *systemId, int systemId_len)
1288 {
1289     static const WCHAR doctypeW[] = {'<','!','D','O','C','T','Y','P','E',' '};
1290     static const WCHAR openintW[] = {'[','\r','\n'};
1291
1292     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1293
1294     TRACE("(%p)->(%s %s %s)\n", This, debugstr_wn(name, name_len), debugstr_wn(publicId, publicId_len),
1295         debugstr_wn(systemId, systemId_len));
1296
1297     if (!name) return E_INVALIDARG;
1298
1299     write_output_buffer(This->buffer, doctypeW, sizeof(doctypeW)/sizeof(WCHAR));
1300
1301     if (*name)
1302     {
1303         write_output_buffer(This->buffer, name, name_len);
1304         write_output_buffer(This->buffer, spaceW, 1);
1305     }
1306
1307     if (publicId)
1308     {
1309         static const WCHAR publicW[] = {'P','U','B','L','I','C',' '};
1310
1311         write_output_buffer(This->buffer, publicW, sizeof(publicW)/sizeof(WCHAR));
1312         write_output_buffer_quoted(This->buffer, publicId, publicId_len);
1313
1314         if (!systemId) return E_INVALIDARG;
1315
1316         if (*publicId)
1317             write_output_buffer(This->buffer, spaceW, 1);
1318
1319         write_output_buffer_quoted(This->buffer, systemId, systemId_len);
1320
1321         if (*systemId)
1322             write_output_buffer(This->buffer, spaceW, 1);
1323     }
1324     else if (systemId)
1325     {
1326         static const WCHAR systemW[] = {'S','Y','S','T','E','M',' '};
1327
1328         write_output_buffer(This->buffer, systemW, sizeof(systemW)/sizeof(WCHAR));
1329         write_output_buffer_quoted(This->buffer, systemId, systemId_len);
1330         if (*systemId)
1331             write_output_buffer(This->buffer, spaceW, 1);
1332     }
1333
1334     write_output_buffer(This->buffer, openintW, sizeof(openintW)/sizeof(WCHAR));
1335
1336     return S_OK;
1337 }
1338
1339 static HRESULT WINAPI SAXLexicalHandler_endDTD(ISAXLexicalHandler *iface)
1340 {
1341     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1342     static const WCHAR closedtdW[] = {']','>','\r','\n'};
1343
1344     TRACE("(%p)\n", This);
1345
1346     write_output_buffer(This->buffer, closedtdW, sizeof(closedtdW)/sizeof(WCHAR));
1347
1348     return S_OK;
1349 }
1350
1351 static HRESULT WINAPI SAXLexicalHandler_startEntity(ISAXLexicalHandler *iface, const WCHAR *name, int len)
1352 {
1353     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1354     FIXME("(%p)->(%s): stub\n", This, debugstr_wn(name, len));
1355     return E_NOTIMPL;
1356 }
1357
1358 static HRESULT WINAPI SAXLexicalHandler_endEntity(ISAXLexicalHandler *iface, const WCHAR *name, int len)
1359 {
1360     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1361     FIXME("(%p)->(%s): stub\n", This, debugstr_wn(name, len));
1362     return E_NOTIMPL;
1363 }
1364
1365 static HRESULT WINAPI SAXLexicalHandler_startCDATA(ISAXLexicalHandler *iface)
1366 {
1367     static const WCHAR scdataW[] = {'<','!','[','C','D','A','T','A','['};
1368     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1369
1370     TRACE("(%p)\n", This);
1371
1372     write_output_buffer(This->buffer, scdataW, sizeof(scdataW)/sizeof(WCHAR));
1373     This->cdata = TRUE;
1374
1375     return S_OK;
1376 }
1377
1378 static HRESULT WINAPI SAXLexicalHandler_endCDATA(ISAXLexicalHandler *iface)
1379 {
1380     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1381     static const WCHAR ecdataW[] = {']',']','>'};
1382
1383     TRACE("(%p)\n", This);
1384
1385     write_output_buffer(This->buffer, ecdataW, sizeof(ecdataW)/sizeof(WCHAR));
1386     This->cdata = FALSE;
1387
1388     return S_OK;
1389 }
1390
1391 static HRESULT WINAPI SAXLexicalHandler_comment(ISAXLexicalHandler *iface, const WCHAR *chars, int nchars)
1392 {
1393     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
1394     static const WCHAR copenW[] = {'<','!','-','-'};
1395     static const WCHAR ccloseW[] = {'-','-','>','\r','\n'};
1396
1397     TRACE("(%p)->(%s:%d)\n", This, debugstr_wn(chars, nchars), nchars);
1398
1399     if (!chars) return E_INVALIDARG;
1400
1401     close_element_starttag(This);
1402
1403     write_output_buffer(This->buffer, copenW, sizeof(copenW)/sizeof(WCHAR));
1404     if (nchars)
1405         write_output_buffer(This->buffer, chars, nchars);
1406     write_output_buffer(This->buffer, ccloseW, sizeof(ccloseW)/sizeof(WCHAR));
1407
1408     return S_OK;
1409 }
1410
1411 static const struct ISAXLexicalHandlerVtbl SAXLexicalHandlerVtbl =
1412 {
1413     SAXLexicalHandler_QueryInterface,
1414     SAXLexicalHandler_AddRef,
1415     SAXLexicalHandler_Release,
1416     SAXLexicalHandler_startDTD,
1417     SAXLexicalHandler_endDTD,
1418     SAXLexicalHandler_startEntity,
1419     SAXLexicalHandler_endEntity,
1420     SAXLexicalHandler_startCDATA,
1421     SAXLexicalHandler_endCDATA,
1422     SAXLexicalHandler_comment
1423 };
1424
1425 /*** ISAXDeclHandler ***/
1426 static HRESULT WINAPI SAXDeclHandler_QueryInterface(ISAXDeclHandler *iface,
1427     REFIID riid, void **obj)
1428 {
1429     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1430     return IMXWriter_QueryInterface(&This->IMXWriter_iface, riid, obj);
1431 }
1432
1433 static ULONG WINAPI SAXDeclHandler_AddRef(ISAXDeclHandler *iface)
1434 {
1435     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1436     return IMXWriter_AddRef(&This->IMXWriter_iface);
1437 }
1438
1439 static ULONG WINAPI SAXDeclHandler_Release(ISAXDeclHandler *iface)
1440 {
1441     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1442     return IMXWriter_Release(&This->IMXWriter_iface);
1443 }
1444
1445 static HRESULT WINAPI SAXDeclHandler_elementDecl(ISAXDeclHandler *iface,
1446     const WCHAR *name, int n_name, const WCHAR *model, int n_model)
1447 {
1448     static const WCHAR elementW[] = {'<','!','E','L','E','M','E','N','T',' '};
1449     static const WCHAR closeelementW[] = {'>','\r','\n'};
1450     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1451
1452     TRACE("(%p)->(%s:%d %s:%d)\n", This, debugstr_wn(name, n_name), n_name,
1453         debugstr_wn(model, n_model), n_model);
1454
1455     if (!name || !model) return E_INVALIDARG;
1456
1457     write_output_buffer(This->buffer, elementW, sizeof(elementW)/sizeof(WCHAR));
1458     if (n_name) {
1459         write_output_buffer(This->buffer, name, n_name);
1460         write_output_buffer(This->buffer, spaceW, sizeof(spaceW)/sizeof(WCHAR));
1461     }
1462     if (n_model)
1463         write_output_buffer(This->buffer, model, n_model);
1464     write_output_buffer(This->buffer, closeelementW, sizeof(closeelementW)/sizeof(WCHAR));
1465
1466     return S_OK;
1467 }
1468
1469 static HRESULT WINAPI SAXDeclHandler_attributeDecl(ISAXDeclHandler *iface,
1470     const WCHAR *element, int n_element, const WCHAR *attr, int n_attr,
1471     const WCHAR *type, int n_type, const WCHAR *Default, int n_default,
1472     const WCHAR *value, int n_value)
1473 {
1474     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1475     FIXME("(%p)->(%s:%d %s:%d %s:%d %s:%d %s:%d): stub\n", This, debugstr_wn(element, n_element), n_element,
1476         debugstr_wn(attr, n_attr), n_attr, debugstr_wn(type, n_type), n_type, debugstr_wn(Default, n_default), n_default,
1477         debugstr_wn(value, n_value), n_value);
1478     return E_NOTIMPL;
1479 }
1480
1481 static HRESULT WINAPI SAXDeclHandler_internalEntityDecl(ISAXDeclHandler *iface,
1482     const WCHAR *name, int n_name, const WCHAR *value, int n_value)
1483 {
1484     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1485     FIXME("(%p)->(%s:%d %s:%d): stub\n", This, debugstr_wn(name, n_name), n_name,
1486         debugstr_wn(value, n_value), n_value);
1487     return E_NOTIMPL;
1488 }
1489
1490 static HRESULT WINAPI SAXDeclHandler_externalEntityDecl(ISAXDeclHandler *iface,
1491     const WCHAR *name, int n_name, const WCHAR *publicId, int n_publicId,
1492     const WCHAR *systemId, int n_systemId)
1493 {
1494     mxwriter *This = impl_from_ISAXDeclHandler( iface );
1495     FIXME("(%p)->(%s:%d %s:%d %s:%d): stub\n", This, debugstr_wn(name, n_name), n_name,
1496         debugstr_wn(publicId, n_publicId), n_publicId, debugstr_wn(systemId, n_systemId), n_systemId);
1497     return E_NOTIMPL;
1498 }
1499
1500 static const ISAXDeclHandlerVtbl SAXDeclHandlerVtbl = {
1501     SAXDeclHandler_QueryInterface,
1502     SAXDeclHandler_AddRef,
1503     SAXDeclHandler_Release,
1504     SAXDeclHandler_elementDecl,
1505     SAXDeclHandler_attributeDecl,
1506     SAXDeclHandler_internalEntityDecl,
1507     SAXDeclHandler_externalEntityDecl
1508 };
1509
1510 static const tid_t mxwriter_iface_tids[] = {
1511     IMXWriter_tid,
1512     0
1513 };
1514
1515 static dispex_static_data_t mxwriter_dispex = {
1516     NULL,
1517     IMXWriter_tid,
1518     NULL,
1519     mxwriter_iface_tids
1520 };
1521
1522 HRESULT MXWriter_create(MSXML_VERSION version, IUnknown *outer, void **ppObj)
1523 {
1524     static const WCHAR version10W[] = {'1','.','0',0};
1525     mxwriter *This;
1526     HRESULT hr;
1527
1528     TRACE("(%p, %p)\n", outer, ppObj);
1529
1530     if (outer) FIXME("support aggregation, outer\n");
1531
1532     This = heap_alloc( sizeof (*This) );
1533     if(!This)
1534         return E_OUTOFMEMORY;
1535
1536     This->IMXWriter_iface.lpVtbl = &MXWriterVtbl;
1537     This->ISAXContentHandler_iface.lpVtbl = &SAXContentHandlerVtbl;
1538     This->ISAXLexicalHandler_iface.lpVtbl = &SAXLexicalHandlerVtbl;
1539     This->ISAXDeclHandler_iface.lpVtbl = &SAXDeclHandlerVtbl;
1540     This->ref = 1;
1541     This->class_version = version;
1542
1543     This->props[MXWriter_BOM] = VARIANT_TRUE;
1544     This->props[MXWriter_DisableEscaping] = VARIANT_FALSE;
1545     This->props[MXWriter_Indent] = VARIANT_FALSE;
1546     This->props[MXWriter_OmitXmlDecl] = VARIANT_FALSE;
1547     This->props[MXWriter_Standalone] = VARIANT_FALSE;
1548     This->prop_changed = FALSE;
1549     This->encoding = SysAllocString(utf16W);
1550     This->version  = SysAllocString(version10W);
1551     This->xml_enc  = XmlEncoding_UTF16;
1552
1553     This->element = NULL;
1554     This->cdata = FALSE;
1555
1556     This->dest = NULL;
1557     This->dest_written = 0;
1558
1559     hr = alloc_output_buffer(This->xml_enc, &This->buffer);
1560     if (hr != S_OK) {
1561         SysFreeString(This->encoding);
1562         SysFreeString(This->version);
1563         heap_free(This);
1564         return hr;
1565     }
1566
1567     init_dispex(&This->dispex, (IUnknown*)&This->IMXWriter_iface, &mxwriter_dispex);
1568
1569     *ppObj = &This->IMXWriter_iface;
1570
1571     TRACE("returning iface %p\n", *ppObj);
1572
1573     return S_OK;
1574 }
1575
1576 static HRESULT WINAPI MXAttributes_QueryInterface(IMXAttributes *iface, REFIID riid, void **ppObj)
1577 {
1578     mxattributes *This = impl_from_IMXAttributes( iface );
1579
1580     TRACE("(%p)->(%s %p)\n", This, debugstr_guid( riid ), ppObj);
1581
1582     *ppObj = NULL;
1583
1584     if ( IsEqualGUID( riid, &IID_IUnknown )  ||
1585          IsEqualGUID( riid, &IID_IDispatch ) ||
1586          IsEqualGUID( riid, &IID_IMXAttributes ))
1587     {
1588         *ppObj = iface;
1589     }
1590     else if ( IsEqualGUID( riid, &IID_ISAXAttributes ))
1591     {
1592         *ppObj = &This->ISAXAttributes_iface;
1593     }
1594     else if ( IsEqualGUID( riid, &IID_IVBSAXAttributes ))
1595     {
1596         *ppObj = &This->IVBSAXAttributes_iface;
1597     }
1598     else if (dispex_query_interface(&This->dispex, riid, ppObj))
1599     {
1600         return *ppObj ? S_OK : E_NOINTERFACE;
1601     }
1602     else
1603     {
1604         FIXME("interface %s not implemented\n", debugstr_guid(riid));
1605         return E_NOINTERFACE;
1606     }
1607
1608     IMXAttributes_AddRef( iface );
1609
1610     return S_OK;
1611 }
1612
1613 static ULONG WINAPI MXAttributes_AddRef(IMXAttributes *iface)
1614 {
1615     mxattributes *This = impl_from_IMXAttributes( iface );
1616     ULONG ref = InterlockedIncrement( &This->ref );
1617     TRACE("(%p)->(%d)\n", This, ref );
1618     return ref;
1619 }
1620
1621 static ULONG WINAPI MXAttributes_Release(IMXAttributes *iface)
1622 {
1623     mxattributes *This = impl_from_IMXAttributes( iface );
1624     LONG ref = InterlockedDecrement( &This->ref );
1625
1626     TRACE("(%p)->(%d)\n", This, ref);
1627
1628     if (ref == 0)
1629     {
1630         int i;
1631
1632         for (i = 0; i < This->length; i++)
1633         {
1634             SysFreeString(This->attr[i].qname);
1635             SysFreeString(This->attr[i].local);
1636             SysFreeString(This->attr[i].uri);
1637             SysFreeString(This->attr[i].type);
1638             SysFreeString(This->attr[i].value);
1639         }
1640
1641         release_dispex(&This->dispex);
1642         heap_free(This->attr);
1643         heap_free(This);
1644     }
1645
1646     return ref;
1647 }
1648
1649 static HRESULT WINAPI MXAttributes_GetTypeInfoCount(IMXAttributes *iface, UINT* pctinfo)
1650 {
1651     mxattributes *This = impl_from_IMXAttributes( iface );
1652     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1653 }
1654
1655 static HRESULT WINAPI MXAttributes_GetTypeInfo(IMXAttributes *iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1656 {
1657     mxattributes *This = impl_from_IMXAttributes( iface );
1658     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1659 }
1660
1661 static HRESULT WINAPI MXAttributes_GetIDsOfNames(
1662     IMXAttributes *iface,
1663     REFIID riid,
1664     LPOLESTR* rgszNames,
1665     UINT cNames,
1666     LCID lcid,
1667     DISPID* rgDispId)
1668 {
1669     mxattributes *This = impl_from_IMXAttributes( iface );
1670     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
1671         riid, rgszNames, cNames, lcid, rgDispId);
1672 }
1673
1674 static HRESULT WINAPI MXAttributes_Invoke(
1675     IMXAttributes *iface,
1676     DISPID dispIdMember,
1677     REFIID riid,
1678     LCID lcid,
1679     WORD wFlags,
1680     DISPPARAMS* pDispParams,
1681     VARIANT* pVarResult,
1682     EXCEPINFO* pExcepInfo,
1683     UINT* puArgErr)
1684 {
1685     mxattributes *This = impl_from_IMXAttributes( iface );
1686     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
1687         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1688 }
1689
1690 static HRESULT WINAPI MXAttributes_addAttribute(IMXAttributes *iface,
1691     BSTR uri, BSTR localName, BSTR QName, BSTR type, BSTR value)
1692 {
1693     mxattributes *This = impl_from_IMXAttributes( iface );
1694     mxattribute *attr;
1695     HRESULT hr;
1696
1697     TRACE("(%p)->(%s %s %s %s %s)\n", This, debugstr_w(uri), debugstr_w(localName),
1698         debugstr_w(QName), debugstr_w(type), debugstr_w(value));
1699
1700     if ((!uri || !localName || !QName || !type || !value) && This->class_version != MSXML6)
1701         return E_INVALIDARG;
1702
1703     /* ensure array is large enough */
1704     hr = mxattributes_grow(This);
1705     if (hr != S_OK) return hr;
1706
1707     attr = &This->attr[This->length];
1708
1709     attr->qname = SysAllocString(QName);
1710     attr->local = SysAllocString(localName);
1711     attr->uri   = SysAllocString(uri);
1712     attr->type  = SysAllocString(type ? type : emptyW);
1713     attr->value = SysAllocString(value);
1714     This->length++;
1715
1716     return S_OK;
1717 }
1718
1719 static HRESULT WINAPI MXAttributes_addAttributeFromIndex(IMXAttributes *iface,
1720     VARIANT atts, int index)
1721 {
1722     mxattributes *This = impl_from_IMXAttributes( iface );
1723     FIXME("(%p)->(%s %d): stub\n", This, debugstr_variant(&atts), index);
1724     return E_NOTIMPL;
1725 }
1726
1727 static HRESULT WINAPI MXAttributes_clear(IMXAttributes *iface)
1728 {
1729     mxattributes *This = impl_from_IMXAttributes( iface );
1730     int i;
1731
1732     TRACE("(%p)\n", This);
1733
1734     for (i = 0; i < This->length; i++)
1735     {
1736         SysFreeString(This->attr[i].qname);
1737         SysFreeString(This->attr[i].local);
1738         SysFreeString(This->attr[i].uri);
1739         SysFreeString(This->attr[i].type);
1740         SysFreeString(This->attr[i].value);
1741         memset(&This->attr[i], 0, sizeof(mxattribute));
1742     }
1743
1744     This->length = 0;
1745
1746     return S_OK;
1747 }
1748
1749 static HRESULT WINAPI MXAttributes_removeAttribute(IMXAttributes *iface, int index)
1750 {
1751     mxattributes *This = impl_from_IMXAttributes( iface );
1752     FIXME("(%p)->(%d): stub\n", This, index);
1753     return E_NOTIMPL;
1754 }
1755
1756 static HRESULT WINAPI MXAttributes_setAttribute(IMXAttributes *iface, int index,
1757     BSTR uri, BSTR localName, BSTR QName, BSTR type, BSTR value)
1758 {
1759     mxattributes *This = impl_from_IMXAttributes( iface );
1760     FIXME("(%p)->(%d %s %s %s %s %s): stub\n", This, index, debugstr_w(uri),
1761         debugstr_w(localName), debugstr_w(QName), debugstr_w(type), debugstr_w(value));
1762     return E_NOTIMPL;
1763 }
1764
1765 static HRESULT WINAPI MXAttributes_setAttributes(IMXAttributes *iface, VARIANT atts)
1766 {
1767     mxattributes *This = impl_from_IMXAttributes( iface );
1768     FIXME("(%p)->(%s): stub\n", This, debugstr_variant(&atts));
1769     return E_NOTIMPL;
1770 }
1771
1772 static HRESULT WINAPI MXAttributes_setLocalName(IMXAttributes *iface, int index,
1773     BSTR localName)
1774 {
1775     mxattributes *This = impl_from_IMXAttributes( iface );
1776     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(localName));
1777     return E_NOTIMPL;
1778 }
1779
1780 static HRESULT WINAPI MXAttributes_setQName(IMXAttributes *iface, int index, BSTR QName)
1781 {
1782     mxattributes *This = impl_from_IMXAttributes( iface );
1783     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(QName));
1784     return E_NOTIMPL;
1785 }
1786
1787 static HRESULT WINAPI MXAttributes_setURI(IMXAttributes *iface, int index, BSTR uri)
1788 {
1789     mxattributes *This = impl_from_IMXAttributes( iface );
1790     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(uri));
1791     return E_NOTIMPL;
1792 }
1793
1794 static HRESULT WINAPI MXAttributes_setValue(IMXAttributes *iface, int index, BSTR value)
1795 {
1796     mxattributes *This = impl_from_IMXAttributes( iface );
1797     FIXME("(%p)->(%d %s): stub\n", This, index, debugstr_w(value));
1798     return E_NOTIMPL;
1799 }
1800
1801 static const IMXAttributesVtbl MXAttributesVtbl = {
1802     MXAttributes_QueryInterface,
1803     MXAttributes_AddRef,
1804     MXAttributes_Release,
1805     MXAttributes_GetTypeInfoCount,
1806     MXAttributes_GetTypeInfo,
1807     MXAttributes_GetIDsOfNames,
1808     MXAttributes_Invoke,
1809     MXAttributes_addAttribute,
1810     MXAttributes_addAttributeFromIndex,
1811     MXAttributes_clear,
1812     MXAttributes_removeAttribute,
1813     MXAttributes_setAttribute,
1814     MXAttributes_setAttributes,
1815     MXAttributes_setLocalName,
1816     MXAttributes_setQName,
1817     MXAttributes_setURI,
1818     MXAttributes_setValue
1819 };
1820
1821 static HRESULT WINAPI SAXAttributes_QueryInterface(ISAXAttributes *iface, REFIID riid, void **ppObj)
1822 {
1823     mxattributes *This = impl_from_ISAXAttributes( iface );
1824     return IMXAttributes_QueryInterface(&This->IMXAttributes_iface, riid, ppObj);
1825 }
1826
1827 static ULONG WINAPI SAXAttributes_AddRef(ISAXAttributes *iface)
1828 {
1829     mxattributes *This = impl_from_ISAXAttributes( iface );
1830     return IMXAttributes_AddRef(&This->IMXAttributes_iface);
1831 }
1832
1833 static ULONG WINAPI SAXAttributes_Release(ISAXAttributes *iface)
1834 {
1835     mxattributes *This = impl_from_ISAXAttributes( iface );
1836     return IMXAttributes_Release(&This->IMXAttributes_iface);
1837 }
1838
1839 static HRESULT WINAPI SAXAttributes_getLength(ISAXAttributes *iface, int *length)
1840 {
1841     mxattributes *This = impl_from_ISAXAttributes( iface );
1842     TRACE("(%p)->(%p)\n", This, length);
1843
1844     if (!length && (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3))
1845        return E_POINTER;
1846
1847     *length = This->length;
1848
1849     return S_OK;
1850 }
1851
1852 static HRESULT WINAPI SAXAttributes_getURI(ISAXAttributes *iface, int index, const WCHAR **uri,
1853     int *len)
1854 {
1855     mxattributes *This = impl_from_ISAXAttributes( iface );
1856
1857     TRACE("(%p)->(%d %p %p)\n", This, index, uri, len);
1858
1859     if (index >= This->length || index < 0) return E_INVALIDARG;
1860     if (!uri || !len) return E_POINTER;
1861
1862     *len = SysStringLen(This->attr[index].uri);
1863     *uri = This->attr[index].uri;
1864
1865     return S_OK;
1866 }
1867
1868 static HRESULT WINAPI SAXAttributes_getLocalName(ISAXAttributes *iface, int index, const WCHAR **name,
1869     int *len)
1870 {
1871     mxattributes *This = impl_from_ISAXAttributes( iface );
1872
1873     TRACE("(%p)->(%d %p %p)\n", This, index, name, len);
1874
1875     if (index >= This->length || index < 0) return E_INVALIDARG;
1876     if (!name || !len) return E_POINTER;
1877
1878     *len = SysStringLen(This->attr[index].local);
1879     *name = This->attr[index].local;
1880
1881     return S_OK;
1882 }
1883
1884 static HRESULT WINAPI SAXAttributes_getQName(ISAXAttributes *iface, int index, const WCHAR **qname, int *length)
1885 {
1886     mxattributes *This = impl_from_ISAXAttributes( iface );
1887
1888     TRACE("(%p)->(%d %p %p)\n", This, index, qname, length);
1889
1890     if (index >= This->length) return E_INVALIDARG;
1891     if (!qname || !length) return E_POINTER;
1892
1893     *qname = This->attr[index].qname;
1894     *length = SysStringLen(This->attr[index].qname);
1895
1896     return S_OK;
1897 }
1898
1899 static HRESULT WINAPI SAXAttributes_getName(ISAXAttributes *iface, int index, const WCHAR **uri, int *uri_len,
1900     const WCHAR **local, int *local_len, const WCHAR **qname, int *qname_len)
1901 {
1902     mxattributes *This = impl_from_ISAXAttributes( iface );
1903
1904     TRACE("(%p)->(%d %p %p %p %p %p %p)\n", This, index, uri, uri_len, local, local_len, qname, qname_len);
1905
1906     if (index >= This->length || index < 0)
1907         return E_INVALIDARG;
1908
1909     if (!uri || !uri_len || !local || !local_len || !qname || !qname_len)
1910         return E_POINTER;
1911
1912     *uri_len = SysStringLen(This->attr[index].uri);
1913     *uri = This->attr[index].uri;
1914
1915     *local_len = SysStringLen(This->attr[index].local);
1916     *local = This->attr[index].local;
1917
1918     *qname_len = SysStringLen(This->attr[index].qname);
1919     *qname = This->attr[index].qname;
1920
1921     TRACE("(%s, %s, %s)\n", debugstr_w(*uri), debugstr_w(*local), debugstr_w(*qname));
1922
1923     return S_OK;
1924 }
1925
1926 static HRESULT WINAPI SAXAttributes_getIndexFromName(ISAXAttributes *iface, const WCHAR *uri, int uri_len,
1927     const WCHAR *name, int len, int *index)
1928 {
1929     mxattributes *This = impl_from_ISAXAttributes( iface );
1930     int i;
1931
1932     TRACE("(%p)->(%s:%d %s:%d %p)\n", This, debugstr_wn(uri, uri_len), uri_len,
1933         debugstr_wn(name, len), len, index);
1934
1935     if (!index && (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3))
1936         return E_POINTER;
1937
1938     if (!uri || !name || !index) return E_INVALIDARG;
1939
1940     for (i = 0; i < This->length; i++)
1941     {
1942         if (uri_len != SysStringLen(This->attr[i].uri)) continue;
1943         if (strncmpW(uri, This->attr[i].uri, uri_len)) continue;
1944
1945         if (len != SysStringLen(This->attr[i].local)) continue;
1946         if (strncmpW(name, This->attr[i].local, len)) continue;
1947
1948         *index = i;
1949         return S_OK;
1950     }
1951
1952     return E_INVALIDARG;
1953 }
1954
1955 static HRESULT WINAPI SAXAttributes_getIndexFromQName(ISAXAttributes *iface, const WCHAR *qname,
1956     int len, int *index)
1957 {
1958     mxattributes *This = impl_from_ISAXAttributes( iface );
1959     int i;
1960
1961     TRACE("(%p)->(%s:%d %p)\n", This, debugstr_wn(qname, len), len, index);
1962
1963     if (!index && (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3))
1964         return E_POINTER;
1965
1966     if (!qname || !index || !len) return E_INVALIDARG;
1967
1968     for (i = 0; i < This->length; i++)
1969     {
1970         if (len != SysStringLen(This->attr[i].qname)) continue;
1971         if (strncmpW(qname, This->attr[i].qname, len)) continue;
1972
1973         *index = i;
1974         return S_OK;
1975     }
1976
1977     return E_INVALIDARG;
1978 }
1979
1980 static HRESULT WINAPI SAXAttributes_getType(ISAXAttributes *iface, int index, const WCHAR **type,
1981     int *len)
1982 {
1983     mxattributes *This = impl_from_ISAXAttributes( iface );
1984
1985     TRACE("(%p)->(%d %p %p)\n", This, index, type, len);
1986
1987     if (index >= This->length) return E_INVALIDARG;
1988
1989     if ((!type || !len) && (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3))
1990        return E_POINTER;
1991
1992     *type = This->attr[index].type;
1993     *len = SysStringLen(This->attr[index].type);
1994
1995     return S_OK;
1996 }
1997
1998 static HRESULT WINAPI SAXAttributes_getTypeFromName(ISAXAttributes *iface, const WCHAR * pUri, int nUri,
1999     const WCHAR * pLocalName, int nLocalName, const WCHAR ** pType, int * nType)
2000 {
2001     mxattributes *This = impl_from_ISAXAttributes( iface );
2002     FIXME("(%p)->(%s:%d %s:%d %p %p): stub\n", This, debugstr_wn(pUri, nUri), nUri,
2003         debugstr_wn(pLocalName, nLocalName), nLocalName, pType, nType);
2004     return E_NOTIMPL;
2005 }
2006
2007 static HRESULT WINAPI SAXAttributes_getTypeFromQName(ISAXAttributes *iface, const WCHAR * pQName,
2008     int nQName, const WCHAR ** pType, int * nType)
2009 {
2010     mxattributes *This = impl_from_ISAXAttributes( iface );
2011     FIXME("(%p)->(%s:%d %p %p): stub\n", This, debugstr_wn(pQName, nQName), nQName, pType, nType);
2012     return E_NOTIMPL;
2013 }
2014
2015 static HRESULT WINAPI SAXAttributes_getValue(ISAXAttributes *iface, int index, const WCHAR **value,
2016     int *len)
2017 {
2018     mxattributes *This = impl_from_ISAXAttributes( iface );
2019
2020     TRACE("(%p)->(%d %p %p)\n", This, index, value, len);
2021
2022     if (index >= This->length) return E_INVALIDARG;
2023
2024     if ((!value || !len) && (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3))
2025        return E_POINTER;
2026
2027     *value = This->attr[index].value;
2028     *len = SysStringLen(This->attr[index].value);
2029
2030     return S_OK;
2031 }
2032
2033 static HRESULT WINAPI SAXAttributes_getValueFromName(ISAXAttributes *iface, const WCHAR *uri,
2034     int uri_len, const WCHAR *name, int name_len, const WCHAR **value, int *value_len)
2035 {
2036     mxattributes *This = impl_from_ISAXAttributes( iface );
2037     HRESULT hr;
2038     int index;
2039
2040     TRACE("(%p)->(%s:%d %s:%d %p %p)\n", This, debugstr_wn(uri, uri_len), uri_len,
2041         debugstr_wn(name, name_len), name_len, value, value_len);
2042
2043     if (!uri || !name || !value || !value_len)
2044         return (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3) ? E_POINTER : E_INVALIDARG;
2045
2046     hr = ISAXAttributes_getIndexFromName(iface, uri, uri_len, name, name_len, &index);
2047     if (hr == S_OK)
2048         hr = ISAXAttributes_getValue(iface, index, value, value_len);
2049
2050     return hr;
2051 }
2052
2053 static HRESULT WINAPI SAXAttributes_getValueFromQName(ISAXAttributes *iface, const WCHAR *qname,
2054     int qname_len, const WCHAR **value, int *value_len)
2055 {
2056     mxattributes *This = impl_from_ISAXAttributes( iface );
2057     HRESULT hr;
2058     int index;
2059
2060     TRACE("(%p)->(%s:%d %p %p)\n", This, debugstr_wn(qname, qname_len), qname_len, value, value_len);
2061
2062     if (!qname || !value || !value_len)
2063         return (This->class_version == MSXML_DEFAULT || This->class_version == MSXML3) ? E_POINTER : E_INVALIDARG;
2064
2065     hr = ISAXAttributes_getIndexFromQName(iface, qname, qname_len, &index);
2066     if (hr == S_OK)
2067         hr = ISAXAttributes_getValue(iface, index, value, value_len);
2068
2069     return hr;
2070 }
2071
2072 static const ISAXAttributesVtbl SAXAttributesVtbl = {
2073     SAXAttributes_QueryInterface,
2074     SAXAttributes_AddRef,
2075     SAXAttributes_Release,
2076     SAXAttributes_getLength,
2077     SAXAttributes_getURI,
2078     SAXAttributes_getLocalName,
2079     SAXAttributes_getQName,
2080     SAXAttributes_getName,
2081     SAXAttributes_getIndexFromName,
2082     SAXAttributes_getIndexFromQName,
2083     SAXAttributes_getType,
2084     SAXAttributes_getTypeFromName,
2085     SAXAttributes_getTypeFromQName,
2086     SAXAttributes_getValue,
2087     SAXAttributes_getValueFromName,
2088     SAXAttributes_getValueFromQName
2089 };
2090
2091 static HRESULT WINAPI VBSAXAttributes_QueryInterface(
2092         IVBSAXAttributes* iface,
2093         REFIID riid,
2094         void **ppvObject)
2095 {
2096     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2097     TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
2098     return ISAXAttributes_QueryInterface(&This->ISAXAttributes_iface, riid, ppvObject);
2099 }
2100
2101 static ULONG WINAPI VBSAXAttributes_AddRef(IVBSAXAttributes* iface)
2102 {
2103     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2104     return ISAXAttributes_AddRef(&This->ISAXAttributes_iface);
2105 }
2106
2107 static ULONG WINAPI VBSAXAttributes_Release(IVBSAXAttributes* iface)
2108 {
2109     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2110     return ISAXAttributes_Release(&This->ISAXAttributes_iface);
2111 }
2112
2113 static HRESULT WINAPI VBSAXAttributes_GetTypeInfoCount( IVBSAXAttributes *iface, UINT* pctinfo )
2114 {
2115     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2116
2117     TRACE("(%p)->(%p)\n", This, pctinfo);
2118
2119     *pctinfo = 1;
2120
2121     return S_OK;
2122 }
2123
2124 static HRESULT WINAPI VBSAXAttributes_GetTypeInfo(
2125     IVBSAXAttributes *iface,
2126     UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
2127 {
2128     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2129     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
2130     return get_typeinfo(IVBSAXAttributes_tid, ppTInfo);
2131 }
2132
2133 static HRESULT WINAPI VBSAXAttributes_GetIDsOfNames(
2134     IVBSAXAttributes *iface,
2135     REFIID riid,
2136     LPOLESTR* rgszNames,
2137     UINT cNames,
2138     LCID lcid,
2139     DISPID* rgDispId)
2140 {
2141     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2142     ITypeInfo *typeinfo;
2143     HRESULT hr;
2144
2145     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
2146           lcid, rgDispId);
2147
2148     if(!rgszNames || cNames == 0 || !rgDispId)
2149         return E_INVALIDARG;
2150
2151     hr = get_typeinfo(IVBSAXAttributes_tid, &typeinfo);
2152     if(SUCCEEDED(hr))
2153     {
2154         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
2155         ITypeInfo_Release(typeinfo);
2156     }
2157
2158     return hr;
2159 }
2160
2161 static HRESULT WINAPI VBSAXAttributes_Invoke(
2162     IVBSAXAttributes *iface,
2163     DISPID dispIdMember,
2164     REFIID riid,
2165     LCID lcid,
2166     WORD wFlags,
2167     DISPPARAMS* pDispParams,
2168     VARIANT* pVarResult,
2169     EXCEPINFO* pExcepInfo,
2170     UINT* puArgErr)
2171 {
2172     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2173     ITypeInfo *typeinfo;
2174     HRESULT hr;
2175
2176     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
2177           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2178
2179     hr = get_typeinfo(IVBSAXAttributes_tid, &typeinfo);
2180     if(SUCCEEDED(hr))
2181     {
2182         hr = ITypeInfo_Invoke(typeinfo, &This->IVBSAXAttributes_iface, dispIdMember, wFlags,
2183                 pDispParams, pVarResult, pExcepInfo, puArgErr);
2184         ITypeInfo_Release(typeinfo);
2185     }
2186
2187     return hr;
2188 }
2189
2190 static HRESULT WINAPI VBSAXAttributes_get_length(IVBSAXAttributes* iface, int *len)
2191 {
2192     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2193     return ISAXAttributes_getLength(&This->ISAXAttributes_iface, len);
2194 }
2195
2196 static HRESULT WINAPI VBSAXAttributes_getURI(IVBSAXAttributes* iface, int index, BSTR *uri)
2197 {
2198     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2199     int len;
2200
2201     return ISAXAttributes_getURI(&This->ISAXAttributes_iface, index, (const WCHAR**)uri, &len);
2202 }
2203
2204 static HRESULT WINAPI VBSAXAttributes_getLocalName(IVBSAXAttributes* iface, int index, BSTR *name)
2205 {
2206     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2207     int len;
2208
2209     return ISAXAttributes_getLocalName(&This->ISAXAttributes_iface, index, (const WCHAR**)name, &len);
2210 }
2211
2212 static HRESULT WINAPI VBSAXAttributes_getQName(IVBSAXAttributes* iface, int index, BSTR *qname)
2213 {
2214     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2215     int len;
2216
2217     return ISAXAttributes_getQName(&This->ISAXAttributes_iface, index, (const WCHAR**)qname, &len);
2218 }
2219
2220 static HRESULT WINAPI VBSAXAttributes_getIndexFromName(IVBSAXAttributes* iface, BSTR uri, BSTR name, int *index)
2221 {
2222     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2223     return ISAXAttributes_getIndexFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
2224             name, SysStringLen(name), index);
2225 }
2226
2227 static HRESULT WINAPI VBSAXAttributes_getIndexFromQName(IVBSAXAttributes* iface, BSTR qname, int *index)
2228 {
2229     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2230     return ISAXAttributes_getIndexFromQName(&This->ISAXAttributes_iface, qname,
2231             SysStringLen(qname), index);
2232 }
2233
2234 static HRESULT WINAPI VBSAXAttributes_getType(IVBSAXAttributes* iface, int index,BSTR *type)
2235 {
2236     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2237     int len;
2238
2239     return ISAXAttributes_getType(&This->ISAXAttributes_iface, index, (const WCHAR**)type, &len);
2240 }
2241
2242 static HRESULT WINAPI VBSAXAttributes_getTypeFromName(IVBSAXAttributes* iface, BSTR uri,
2243     BSTR name, BSTR *type)
2244 {
2245     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2246     int len;
2247
2248     return ISAXAttributes_getTypeFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
2249             name, SysStringLen(name), (const WCHAR**)type, &len);
2250 }
2251
2252 static HRESULT WINAPI VBSAXAttributes_getTypeFromQName(IVBSAXAttributes* iface, BSTR qname, BSTR *type)
2253 {
2254     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2255     int len;
2256
2257     return ISAXAttributes_getTypeFromQName(&This->ISAXAttributes_iface, qname, SysStringLen(qname),
2258             (const WCHAR**)type, &len);
2259 }
2260
2261 static HRESULT WINAPI VBSAXAttributes_getValue(IVBSAXAttributes* iface, int index, BSTR *value)
2262 {
2263     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2264     int len;
2265
2266     return ISAXAttributes_getValue(&This->ISAXAttributes_iface, index, (const WCHAR**)value, &len);
2267 }
2268
2269 static HRESULT WINAPI VBSAXAttributes_getValueFromName(IVBSAXAttributes* iface, BSTR uri, BSTR name,
2270     BSTR *value)
2271 {
2272     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2273     int len;
2274
2275     return ISAXAttributes_getValueFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
2276             name, SysStringLen(name), (const WCHAR**)value, &len);
2277 }
2278
2279 static HRESULT WINAPI VBSAXAttributes_getValueFromQName(IVBSAXAttributes* iface, BSTR qname, BSTR *value)
2280 {
2281     mxattributes *This = impl_from_IVBSAXAttributes( iface );
2282     int len;
2283
2284     return ISAXAttributes_getValueFromQName(&This->ISAXAttributes_iface, qname, SysStringLen(qname),
2285         (const WCHAR**)value, &len);
2286 }
2287
2288 static const struct IVBSAXAttributesVtbl VBSAXAttributesVtbl =
2289 {
2290     VBSAXAttributes_QueryInterface,
2291     VBSAXAttributes_AddRef,
2292     VBSAXAttributes_Release,
2293     VBSAXAttributes_GetTypeInfoCount,
2294     VBSAXAttributes_GetTypeInfo,
2295     VBSAXAttributes_GetIDsOfNames,
2296     VBSAXAttributes_Invoke,
2297     VBSAXAttributes_get_length,
2298     VBSAXAttributes_getURI,
2299     VBSAXAttributes_getLocalName,
2300     VBSAXAttributes_getQName,
2301     VBSAXAttributes_getIndexFromName,
2302     VBSAXAttributes_getIndexFromQName,
2303     VBSAXAttributes_getType,
2304     VBSAXAttributes_getTypeFromName,
2305     VBSAXAttributes_getTypeFromQName,
2306     VBSAXAttributes_getValue,
2307     VBSAXAttributes_getValueFromName,
2308     VBSAXAttributes_getValueFromQName
2309 };
2310
2311 static const tid_t mxattrs_iface_tids[] = {
2312     IMXAttributes_tid,
2313     0
2314 };
2315
2316 static dispex_static_data_t mxattrs_dispex = {
2317     NULL,
2318     IMXAttributes_tid,
2319     NULL,
2320     mxattrs_iface_tids
2321 };
2322
2323 HRESULT SAXAttributes_create(MSXML_VERSION version, IUnknown *outer, void **ppObj)
2324 {
2325     static const int default_count = 10;
2326     mxattributes *This;
2327
2328     TRACE("(%p, %p)\n", outer, ppObj);
2329
2330     This = heap_alloc( sizeof (*This) );
2331     if( !This )
2332         return E_OUTOFMEMORY;
2333
2334     This->IMXAttributes_iface.lpVtbl = &MXAttributesVtbl;
2335     This->ISAXAttributes_iface.lpVtbl = &SAXAttributesVtbl;
2336     This->IVBSAXAttributes_iface.lpVtbl = &VBSAXAttributesVtbl;
2337     This->ref = 1;
2338
2339     This->class_version = version;
2340
2341     This->attr = heap_alloc(default_count*sizeof(mxattribute));
2342     This->length = 0;
2343     This->allocated = default_count;
2344
2345     *ppObj = &This->IMXAttributes_iface;
2346
2347     init_dispex(&This->dispex, (IUnknown*)&This->IMXAttributes_iface, &mxattrs_dispex);
2348
2349     TRACE("returning iface %p\n", *ppObj);
2350
2351     return S_OK;
2352 }