winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / xmllite / writer.c
blobabbe07deb60d6be8b80d6b10c4fefaa20da193c6
1 /*
2 * IXmlWriter implementation
4 * Copyright 2011 Alistair Leslie-Hughes
5 * Copyright 2014 Nikolay Sivov for CodeWeavers
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.
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.
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
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "objbase.h"
27 #include "xmllite.h"
28 #include "xmllite_private.h"
29 #include "initguid.h"
31 #include "wine/debug.h"
32 #include "wine/list.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(xmllite);
37 /* not defined in public headers */
38 DEFINE_GUID(IID_IXmlWriterOutput, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
42 static const WCHAR closeelementW[] = {'<','/'};
43 static const WCHAR closepiW[] = {'?','>'};
44 static const WCHAR ltW[] = {'<'};
45 static const WCHAR gtW[] = {'>'};
47 struct output_buffer
49 char *data;
50 unsigned int allocated;
51 unsigned int written;
52 UINT codepage;
55 typedef enum
57 XmlWriterState_Initial, /* output is not set yet */
58 XmlWriterState_Ready, /* SetOutput() was called, ready to start */
59 XmlWriterState_PIDocStarted, /* document was started with manually added 'xml' PI */
60 XmlWriterState_DocStarted, /* document was started with WriteStartDocument() */
61 XmlWriterState_ElemStarted, /* writing element */
62 XmlWriterState_Content /* content is accepted at this point */
63 } XmlWriterState;
65 typedef struct
67 IXmlWriterOutput IXmlWriterOutput_iface;
68 LONG ref;
69 IUnknown *output;
70 ISequentialStream *stream;
71 IMalloc *imalloc;
72 xml_encoding encoding;
73 struct output_buffer buffer;
74 } xmlwriteroutput;
76 static const struct IUnknownVtbl xmlwriteroutputvtbl;
78 struct element
80 struct list entry;
81 WCHAR *qname;
82 unsigned int len; /* qname length in chars */
85 typedef struct _xmlwriter
87 IXmlWriter IXmlWriter_iface;
88 LONG ref;
89 IMalloc *imalloc;
90 xmlwriteroutput *output;
91 BOOL indent;
92 BOOL bom;
93 BOOL omitxmldecl;
94 XmlConformanceLevel conformance;
95 XmlWriterState state;
96 BOOL bomwritten;
97 BOOL starttagopen;
98 struct list elements;
99 } xmlwriter;
101 static inline xmlwriter *impl_from_IXmlWriter(IXmlWriter *iface)
103 return CONTAINING_RECORD(iface, xmlwriter, IXmlWriter_iface);
106 static inline xmlwriteroutput *impl_from_IXmlWriterOutput(IXmlWriterOutput *iface)
108 return CONTAINING_RECORD(iface, xmlwriteroutput, IXmlWriterOutput_iface);
111 static const char *debugstr_writer_prop(XmlWriterProperty prop)
113 static const char * const prop_names[] =
115 "MultiLanguage",
116 "Indent",
117 "ByteOrderMark",
118 "OmitXmlDeclaration",
119 "ConformanceLevel"
122 if (prop > _XmlWriterProperty_Last)
123 return wine_dbg_sprintf("unknown property=%d", prop);
125 return prop_names[prop];
128 /* writer output memory allocation functions */
129 static inline void *writeroutput_alloc(xmlwriteroutput *output, size_t len)
131 return m_alloc(output->imalloc, len);
134 static inline void writeroutput_free(xmlwriteroutput *output, void *mem)
136 m_free(output->imalloc, mem);
139 static inline void *writeroutput_realloc(xmlwriteroutput *output, void *mem, size_t len)
141 return m_realloc(output->imalloc, mem, len);
144 /* writer memory allocation functions */
145 static inline void *writer_alloc(xmlwriter *writer, size_t len)
147 return m_alloc(writer->imalloc, len);
150 static inline void writer_free(xmlwriter *writer, void *mem)
152 m_free(writer->imalloc, mem);
155 static struct element *alloc_element(xmlwriter *writer, const WCHAR *prefix, const WCHAR *local)
157 struct element *ret;
158 int len;
160 ret = writer_alloc(writer, sizeof(*ret));
161 if (!ret) return ret;
163 len = prefix ? strlenW(prefix) + 1 /* ':' */ : 0;
164 len += strlenW(local);
166 ret->qname = writer_alloc(writer, (len + 1)*sizeof(WCHAR));
167 ret->len = len;
168 if (prefix) {
169 static const WCHAR colonW[] = {':',0};
170 strcpyW(ret->qname, prefix);
171 strcatW(ret->qname, colonW);
173 else
174 ret->qname[0] = 0;
175 strcatW(ret->qname, local);
177 return ret;
180 static void free_element(xmlwriter *writer, struct element *element)
182 writer_free(writer, element->qname);
183 writer_free(writer, element);
186 static void push_element(xmlwriter *writer, struct element *element)
188 list_add_head(&writer->elements, &element->entry);
191 static struct element *pop_element(xmlwriter *writer)
193 struct element *element = LIST_ENTRY(list_head(&writer->elements), struct element, entry);
195 if (element)
196 list_remove(&element->entry);
198 return element;
201 static HRESULT init_output_buffer(xmlwriteroutput *output)
203 struct output_buffer *buffer = &output->buffer;
204 const int initial_len = 0x2000;
205 HRESULT hr;
206 UINT cp;
208 hr = get_code_page(output->encoding, &cp);
209 if (FAILED(hr)) return hr;
211 buffer->data = writeroutput_alloc(output, initial_len);
212 if (!buffer->data) return E_OUTOFMEMORY;
214 memset(buffer->data, 0, 4);
215 buffer->allocated = initial_len;
216 buffer->written = 0;
217 buffer->codepage = cp;
219 return S_OK;
222 static void free_output_buffer(xmlwriteroutput *output)
224 struct output_buffer *buffer = &output->buffer;
225 writeroutput_free(output, buffer->data);
226 buffer->data = NULL;
227 buffer->allocated = 0;
228 buffer->written = 0;
231 static HRESULT grow_output_buffer(xmlwriteroutput *output, int length)
233 struct output_buffer *buffer = &output->buffer;
234 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
235 if (buffer->allocated < buffer->written + length + 4) {
236 int grown_size = max(2*buffer->allocated, buffer->allocated + length);
237 char *ptr = writeroutput_realloc(output, buffer->data, grown_size);
238 if (!ptr) return E_OUTOFMEMORY;
239 buffer->data = ptr;
240 buffer->allocated = grown_size;
243 return S_OK;
246 static HRESULT write_output_buffer(xmlwriteroutput *output, const WCHAR *data, int len)
248 struct output_buffer *buffer = &output->buffer;
249 int length;
250 HRESULT hr;
251 char *ptr;
253 if (buffer->codepage != ~0) {
254 length = WideCharToMultiByte(buffer->codepage, 0, data, len, NULL, 0, NULL, NULL);
255 hr = grow_output_buffer(output, length);
256 if (FAILED(hr)) return hr;
257 ptr = buffer->data + buffer->written;
258 length = WideCharToMultiByte(buffer->codepage, 0, data, len, ptr, length, NULL, NULL);
259 buffer->written += len == -1 ? length-1 : length;
261 else {
262 /* WCHAR data just copied */
263 length = len == -1 ? strlenW(data) : len;
264 if (length) {
265 length *= sizeof(WCHAR);
267 hr = grow_output_buffer(output, length);
268 if (FAILED(hr)) return hr;
269 ptr = buffer->data + buffer->written;
271 memcpy(ptr, data, length);
272 buffer->written += length;
273 ptr += length;
274 /* null termination */
275 *(WCHAR*)ptr = 0;
279 return S_OK;
282 static HRESULT write_output_buffer_quoted(xmlwriteroutput *output, const WCHAR *data, int len)
284 static const WCHAR quoteW[] = {'"'};
285 write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
286 write_output_buffer(output, data, len);
287 write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
288 return S_OK;
291 /* TODO: test if we need to validate char range */
292 static HRESULT write_output_qname(xmlwriteroutput *output, const WCHAR *prefix, const WCHAR *local_name)
294 if (prefix) {
295 static const WCHAR colW[] = {':'};
296 write_output_buffer(output, prefix, -1);
297 write_output_buffer(output, colW, ARRAY_SIZE(colW));
300 write_output_buffer(output, local_name, -1);
302 return S_OK;
305 static void writeroutput_release_stream(xmlwriteroutput *writeroutput)
307 if (writeroutput->stream) {
308 ISequentialStream_Release(writeroutput->stream);
309 writeroutput->stream = NULL;
313 static inline HRESULT writeroutput_query_for_stream(xmlwriteroutput *writeroutput)
315 HRESULT hr;
317 writeroutput_release_stream(writeroutput);
318 hr = IUnknown_QueryInterface(writeroutput->output, &IID_IStream, (void**)&writeroutput->stream);
319 if (hr != S_OK)
320 hr = IUnknown_QueryInterface(writeroutput->output, &IID_ISequentialStream, (void**)&writeroutput->stream);
322 return hr;
325 static HRESULT writeroutput_flush_stream(xmlwriteroutput *output)
327 struct output_buffer *buffer;
328 ULONG written, offset = 0;
329 HRESULT hr;
331 if (!output || !output->stream)
332 return S_OK;
334 buffer = &output->buffer;
336 /* It will loop forever until everything is written or an error occurred. */
337 do {
338 written = 0;
339 hr = ISequentialStream_Write(output->stream, buffer->data + offset, buffer->written, &written);
340 if (FAILED(hr)) {
341 WARN("write to stream failed (0x%08x)\n", hr);
342 buffer->written = 0;
343 return hr;
346 offset += written;
347 buffer->written -= written;
348 } while (buffer->written > 0);
350 return S_OK;
353 static HRESULT write_encoding_bom(xmlwriter *writer)
355 if (!writer->bom || writer->bomwritten) return S_OK;
357 if (writer->output->encoding == XmlEncoding_UTF16) {
358 static const char utf16bom[] = {0xff, 0xfe};
359 struct output_buffer *buffer = &writer->output->buffer;
360 int len = sizeof(utf16bom);
361 HRESULT hr;
363 hr = grow_output_buffer(writer->output, len);
364 if (FAILED(hr)) return hr;
365 memcpy(buffer->data + buffer->written, utf16bom, len);
366 buffer->written += len;
369 writer->bomwritten = TRUE;
370 return S_OK;
373 static HRESULT writer_close_starttag(xmlwriter *writer)
375 HRESULT hr;
377 if (!writer->starttagopen) return S_OK;
378 hr = write_output_buffer(writer->output, gtW, ARRAY_SIZE(gtW));
379 writer->starttagopen = FALSE;
380 writer->state = XmlWriterState_Content;
381 return hr;
384 static HRESULT WINAPI xmlwriter_QueryInterface(IXmlWriter *iface, REFIID riid, void **ppvObject)
386 xmlwriter *This = impl_from_IXmlWriter(iface);
388 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
390 if (IsEqualGUID(riid, &IID_IUnknown) ||
391 IsEqualGUID(riid, &IID_IXmlWriter))
393 *ppvObject = iface;
396 IXmlWriter_AddRef(iface);
398 return S_OK;
401 static ULONG WINAPI xmlwriter_AddRef(IXmlWriter *iface)
403 xmlwriter *This = impl_from_IXmlWriter(iface);
404 TRACE("%p\n", This);
405 return InterlockedIncrement(&This->ref);
408 static ULONG WINAPI xmlwriter_Release(IXmlWriter *iface)
410 xmlwriter *This = impl_from_IXmlWriter(iface);
411 LONG ref;
413 TRACE("%p\n", This);
415 ref = InterlockedDecrement(&This->ref);
416 if (ref == 0) {
417 struct element *element, *element2;
418 IMalloc *imalloc = This->imalloc;
420 IXmlWriter_Flush(iface);
421 if (This->output) IUnknown_Release(&This->output->IXmlWriterOutput_iface);
423 /* element stack */
424 LIST_FOR_EACH_ENTRY_SAFE(element, element2, &This->elements, struct element, entry) {
425 list_remove(&element->entry);
426 free_element(This, element);
429 writer_free(This, This);
430 if (imalloc) IMalloc_Release(imalloc);
433 return ref;
436 /*** IXmlWriter methods ***/
437 static HRESULT WINAPI xmlwriter_SetOutput(IXmlWriter *iface, IUnknown *output)
439 xmlwriter *This = impl_from_IXmlWriter(iface);
440 IXmlWriterOutput *writeroutput;
441 HRESULT hr;
443 TRACE("(%p)->(%p)\n", This, output);
445 if (This->output) {
446 writeroutput_release_stream(This->output);
447 IUnknown_Release(&This->output->IXmlWriterOutput_iface);
448 This->output = NULL;
449 This->bomwritten = FALSE;
452 /* just reset current output */
453 if (!output) {
454 This->state = XmlWriterState_Initial;
455 return S_OK;
458 /* now try IXmlWriterOutput, ISequentialStream, IStream */
459 hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&writeroutput);
460 if (hr == S_OK) {
461 if (writeroutput->lpVtbl == &xmlwriteroutputvtbl)
462 This->output = impl_from_IXmlWriterOutput(writeroutput);
463 else {
464 ERR("got external IXmlWriterOutput implementation: %p, vtbl=%p\n",
465 writeroutput, writeroutput->lpVtbl);
466 IUnknown_Release(writeroutput);
467 return E_FAIL;
472 if (hr != S_OK || !writeroutput) {
473 /* create IXmlWriterOutput basing on supplied interface */
474 hr = CreateXmlWriterOutputWithEncodingName(output, This->imalloc, NULL, &writeroutput);
475 if (hr != S_OK) return hr;
476 This->output = impl_from_IXmlWriterOutput(writeroutput);
479 This->state = XmlWriterState_Ready;
480 return writeroutput_query_for_stream(This->output);
483 static HRESULT WINAPI xmlwriter_GetProperty(IXmlWriter *iface, UINT property, LONG_PTR *value)
485 xmlwriter *This = impl_from_IXmlWriter(iface);
487 TRACE("(%p)->(%s %p)\n", This, debugstr_writer_prop(property), value);
489 if (!value) return E_INVALIDARG;
491 switch (property)
493 case XmlWriterProperty_Indent:
494 *value = This->indent;
495 break;
496 case XmlWriterProperty_ByteOrderMark:
497 *value = This->bom;
498 break;
499 case XmlWriterProperty_OmitXmlDeclaration:
500 *value = This->omitxmldecl;
501 break;
502 case XmlWriterProperty_ConformanceLevel:
503 *value = This->conformance;
504 break;
505 default:
506 FIXME("Unimplemented property (%u)\n", property);
507 return E_NOTIMPL;
510 return S_OK;
513 static HRESULT WINAPI xmlwriter_SetProperty(IXmlWriter *iface, UINT property, LONG_PTR value)
515 xmlwriter *This = impl_from_IXmlWriter(iface);
517 TRACE("(%p)->(%s %lu)\n", This, debugstr_writer_prop(property), value);
519 switch (property)
521 case XmlWriterProperty_ByteOrderMark:
522 This->bom = !!value;
523 break;
524 case XmlWriterProperty_OmitXmlDeclaration:
525 This->omitxmldecl = !!value;
526 break;
527 default:
528 FIXME("Unimplemented property (%u)\n", property);
529 return E_NOTIMPL;
532 return S_OK;
535 static HRESULT WINAPI xmlwriter_WriteAttributes(IXmlWriter *iface, IXmlReader *pReader,
536 BOOL fWriteDefaultAttributes)
538 xmlwriter *This = impl_from_IXmlWriter(iface);
540 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
542 return E_NOTIMPL;
545 static HRESULT WINAPI xmlwriter_WriteAttributeString(IXmlWriter *iface, LPCWSTR pwszPrefix,
546 LPCWSTR pwszLocalName, LPCWSTR pwszNamespaceUri,
547 LPCWSTR pwszValue)
549 xmlwriter *This = impl_from_IXmlWriter(iface);
551 FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszPrefix), wine_dbgstr_w(pwszLocalName),
552 wine_dbgstr_w(pwszNamespaceUri), wine_dbgstr_w(pwszValue));
554 return E_NOTIMPL;
557 static HRESULT WINAPI xmlwriter_WriteCData(IXmlWriter *iface, LPCWSTR pwszText)
559 xmlwriter *This = impl_from_IXmlWriter(iface);
561 FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
563 return E_NOTIMPL;
566 static HRESULT WINAPI xmlwriter_WriteCharEntity(IXmlWriter *iface, WCHAR wch)
568 return E_NOTIMPL;
571 static HRESULT WINAPI xmlwriter_WriteChars(IXmlWriter *iface, const WCHAR *pwch, UINT cwch)
573 xmlwriter *This = impl_from_IXmlWriter(iface);
575 FIXME("%p %s %d\n", This, wine_dbgstr_w(pwch), cwch);
577 return E_NOTIMPL;
580 static HRESULT WINAPI xmlwriter_WriteComment(IXmlWriter *iface, LPCWSTR pwszComment)
582 return E_NOTIMPL;
585 static HRESULT WINAPI xmlwriter_WriteDocType(IXmlWriter *iface, LPCWSTR pwszName, LPCWSTR pwszPublicId,
586 LPCWSTR pwszSystemId, LPCWSTR pwszSubset)
588 xmlwriter *This = impl_from_IXmlWriter(iface);
590 FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszName), wine_dbgstr_w(pwszPublicId),
591 wine_dbgstr_w(pwszSystemId), wine_dbgstr_w(pwszSubset));
593 return E_NOTIMPL;
596 static HRESULT WINAPI xmlwriter_WriteElementString(IXmlWriter *iface, LPCWSTR prefix,
597 LPCWSTR local_name, LPCWSTR uri, LPCWSTR value)
599 xmlwriter *This = impl_from_IXmlWriter(iface);
601 TRACE("(%p)->(%s %s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name),
602 wine_dbgstr_w(uri), wine_dbgstr_w(value));
604 switch (This->state)
606 case XmlWriterState_Initial:
607 return E_UNEXPECTED;
608 case XmlWriterState_ElemStarted:
609 writer_close_starttag(This);
610 break;
611 default:
615 write_encoding_bom(This);
616 write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
617 write_output_qname(This->output, prefix, local_name);
618 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
620 if (value)
621 write_output_buffer(This->output, value, -1);
623 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
624 write_output_qname(This->output, prefix, local_name);
625 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
626 This->state = XmlWriterState_Content;
628 return S_OK;
631 static HRESULT WINAPI xmlwriter_WriteEndDocument(IXmlWriter *iface)
633 xmlwriter *This = impl_from_IXmlWriter(iface);
635 FIXME("%p\n", This);
637 return E_NOTIMPL;
640 static HRESULT WINAPI xmlwriter_WriteEndElement(IXmlWriter *iface)
642 xmlwriter *This = impl_from_IXmlWriter(iface);
643 struct element *element;
645 TRACE("%p\n", This);
647 element = pop_element(This);
648 if (!element)
649 return WR_E_INVALIDACTION;
651 if (This->starttagopen) {
652 static WCHAR closetagW[] = {' ','/','>'};
653 write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW));
654 This->starttagopen = FALSE;
656 else {
657 /* write full end tag */
658 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
659 write_output_buffer(This->output, element->qname, element->len);
660 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
663 return S_OK;
666 static HRESULT WINAPI xmlwriter_WriteEntityRef(IXmlWriter *iface, LPCWSTR pwszName)
668 xmlwriter *This = impl_from_IXmlWriter(iface);
670 FIXME("%p %s\n", This, wine_dbgstr_w(pwszName));
672 return E_NOTIMPL;
675 static HRESULT WINAPI xmlwriter_WriteFullEndElement(IXmlWriter *iface)
677 xmlwriter *This = impl_from_IXmlWriter(iface);
678 struct element *element;
680 TRACE("%p\n", This);
682 element = pop_element(This);
683 if (!element)
684 return WR_E_INVALIDACTION;
686 /* write full end tag */
687 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
688 write_output_buffer(This->output, element->qname, element->len);
689 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
690 This->starttagopen = FALSE;
692 return S_OK;
695 static HRESULT WINAPI xmlwriter_WriteName(IXmlWriter *iface, LPCWSTR pwszName)
697 xmlwriter *This = impl_from_IXmlWriter(iface);
699 FIXME("%p %s\n", This, wine_dbgstr_w(pwszName));
701 return E_NOTIMPL;
704 static HRESULT WINAPI xmlwriter_WriteNmToken(IXmlWriter *iface, LPCWSTR pwszNmToken)
706 xmlwriter *This = impl_from_IXmlWriter(iface);
708 FIXME("%p %s\n", This, wine_dbgstr_w(pwszNmToken));
710 return E_NOTIMPL;
713 static HRESULT WINAPI xmlwriter_WriteNode(IXmlWriter *iface, IXmlReader *pReader,
714 BOOL fWriteDefaultAttributes)
716 xmlwriter *This = impl_from_IXmlWriter(iface);
718 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
720 return E_NOTIMPL;
723 static HRESULT WINAPI xmlwriter_WriteNodeShallow(IXmlWriter *iface, IXmlReader *pReader,
724 BOOL fWriteDefaultAttributes)
726 xmlwriter *This = impl_from_IXmlWriter(iface);
728 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
730 return E_NOTIMPL;
733 static HRESULT WINAPI xmlwriter_WriteProcessingInstruction(IXmlWriter *iface, LPCWSTR name,
734 LPCWSTR text)
736 xmlwriter *This = impl_from_IXmlWriter(iface);
737 static const WCHAR xmlW[] = {'x','m','l',0};
738 static const WCHAR openpiW[] = {'<','?'};
739 static const WCHAR spaceW[] = {' '};
741 TRACE("(%p)->(%s %s)\n", This, wine_dbgstr_w(name), wine_dbgstr_w(text));
743 switch (This->state)
745 case XmlWriterState_Initial:
746 return E_UNEXPECTED;
747 case XmlWriterState_DocStarted:
748 if (!strcmpW(name, xmlW))
749 return WR_E_INVALIDACTION;
750 break;
751 case XmlWriterState_ElemStarted:
752 return WR_E_INVALIDACTION;
753 default:
757 write_encoding_bom(This);
758 write_output_buffer(This->output, openpiW, ARRAY_SIZE(openpiW));
759 write_output_buffer(This->output, name, -1);
760 write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
761 write_output_buffer(This->output, text, -1);
762 write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
764 if (!strcmpW(name, xmlW))
765 This->state = XmlWriterState_PIDocStarted;
767 return S_OK;
770 static HRESULT WINAPI xmlwriter_WriteQualifiedName(IXmlWriter *iface, LPCWSTR pwszLocalName,
771 LPCWSTR pwszNamespaceUri)
773 xmlwriter *This = impl_from_IXmlWriter(iface);
775 FIXME("%p %s %s\n", This, wine_dbgstr_w(pwszLocalName), wine_dbgstr_w(pwszNamespaceUri));
777 return E_NOTIMPL;
780 static HRESULT WINAPI xmlwriter_WriteRaw(IXmlWriter *iface, LPCWSTR pwszData)
782 xmlwriter *This = impl_from_IXmlWriter(iface);
784 FIXME("%p %s\n", This, wine_dbgstr_w(pwszData));
786 return E_NOTIMPL;
789 static HRESULT WINAPI xmlwriter_WriteRawChars(IXmlWriter *iface, const WCHAR *pwch, UINT cwch)
791 xmlwriter *This = impl_from_IXmlWriter(iface);
793 FIXME("%p %s %d\n", This, wine_dbgstr_w(pwch), cwch);
795 return E_NOTIMPL;
798 static HRESULT WINAPI xmlwriter_WriteStartDocument(IXmlWriter *iface, XmlStandalone standalone)
800 static const WCHAR versionW[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"'};
801 static const WCHAR encodingW[] = {' ','e','n','c','o','d','i','n','g','='};
802 xmlwriter *This = impl_from_IXmlWriter(iface);
804 TRACE("(%p)->(%d)\n", This, standalone);
806 switch (This->state)
808 case XmlWriterState_Initial:
809 return E_UNEXPECTED;
810 case XmlWriterState_PIDocStarted:
811 This->state = XmlWriterState_DocStarted;
812 return S_OK;
813 case XmlWriterState_DocStarted:
814 case XmlWriterState_ElemStarted:
815 return WR_E_INVALIDACTION;
816 default:
820 write_encoding_bom(This);
821 This->state = XmlWriterState_DocStarted;
822 if (This->omitxmldecl) return S_OK;
824 /* version */
825 write_output_buffer(This->output, versionW, ARRAY_SIZE(versionW));
827 /* encoding */
828 write_output_buffer(This->output, encodingW, ARRAY_SIZE(encodingW));
829 write_output_buffer_quoted(This->output, get_encoding_name(This->output->encoding), -1);
831 /* standalone */
832 if (standalone == XmlStandalone_Omit)
833 write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
834 else {
835 static const WCHAR standaloneW[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
836 static const WCHAR yesW[] = {'y','e','s','\"','?','>'};
837 static const WCHAR noW[] = {'n','o','\"','?','>'};
839 write_output_buffer(This->output, standaloneW, ARRAY_SIZE(standaloneW));
840 if (standalone == XmlStandalone_Yes)
841 write_output_buffer(This->output, yesW, ARRAY_SIZE(yesW));
842 else
843 write_output_buffer(This->output, noW, ARRAY_SIZE(noW));
846 return S_OK;
849 static HRESULT WINAPI xmlwriter_WriteStartElement(IXmlWriter *iface, LPCWSTR prefix, LPCWSTR local_name, LPCWSTR uri)
851 xmlwriter *This = impl_from_IXmlWriter(iface);
852 struct element *element;
854 TRACE("(%p)->(%s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name), wine_dbgstr_w(uri));
856 if (This->state == XmlWriterState_Initial)
857 return E_UNEXPECTED;
859 if (!local_name)
860 return E_INVALIDARG;
862 /* close pending element */
863 if (This->starttagopen)
864 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
866 element = alloc_element(This, prefix, local_name);
867 if (!element)
868 return E_OUTOFMEMORY;
870 write_encoding_bom(This);
871 This->state = XmlWriterState_ElemStarted;
872 This->starttagopen = TRUE;
874 push_element(This, element);
876 write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
877 write_output_qname(This->output, prefix, local_name);
879 return S_OK;
882 static HRESULT WINAPI xmlwriter_WriteString(IXmlWriter *iface, LPCWSTR pwszText)
884 xmlwriter *This = impl_from_IXmlWriter(iface);
886 FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
888 return E_NOTIMPL;
891 static HRESULT WINAPI xmlwriter_WriteSurrogateCharEntity(IXmlWriter *iface, WCHAR wchLow, WCHAR wchHigh)
893 xmlwriter *This = impl_from_IXmlWriter(iface);
895 FIXME("%p %d %d\n", This, wchLow, wchHigh);
897 return E_NOTIMPL;
900 static HRESULT WINAPI xmlwriter_WriteWhitespace(IXmlWriter *iface, LPCWSTR pwszWhitespace)
902 xmlwriter *This = impl_from_IXmlWriter(iface);
904 FIXME("%p %s\n", This, wine_dbgstr_w(pwszWhitespace));
906 return E_NOTIMPL;
909 static HRESULT WINAPI xmlwriter_Flush(IXmlWriter *iface)
911 xmlwriter *This = impl_from_IXmlWriter(iface);
913 TRACE("%p\n", This);
915 return writeroutput_flush_stream(This->output);
918 static const struct IXmlWriterVtbl xmlwriter_vtbl =
920 xmlwriter_QueryInterface,
921 xmlwriter_AddRef,
922 xmlwriter_Release,
923 xmlwriter_SetOutput,
924 xmlwriter_GetProperty,
925 xmlwriter_SetProperty,
926 xmlwriter_WriteAttributes,
927 xmlwriter_WriteAttributeString,
928 xmlwriter_WriteCData,
929 xmlwriter_WriteCharEntity,
930 xmlwriter_WriteChars,
931 xmlwriter_WriteComment,
932 xmlwriter_WriteDocType,
933 xmlwriter_WriteElementString,
934 xmlwriter_WriteEndDocument,
935 xmlwriter_WriteEndElement,
936 xmlwriter_WriteEntityRef,
937 xmlwriter_WriteFullEndElement,
938 xmlwriter_WriteName,
939 xmlwriter_WriteNmToken,
940 xmlwriter_WriteNode,
941 xmlwriter_WriteNodeShallow,
942 xmlwriter_WriteProcessingInstruction,
943 xmlwriter_WriteQualifiedName,
944 xmlwriter_WriteRaw,
945 xmlwriter_WriteRawChars,
946 xmlwriter_WriteStartDocument,
947 xmlwriter_WriteStartElement,
948 xmlwriter_WriteString,
949 xmlwriter_WriteSurrogateCharEntity,
950 xmlwriter_WriteWhitespace,
951 xmlwriter_Flush
954 /** IXmlWriterOutput **/
955 static HRESULT WINAPI xmlwriteroutput_QueryInterface(IXmlWriterOutput *iface, REFIID riid, void** ppvObject)
957 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
959 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
961 if (IsEqualGUID(riid, &IID_IXmlWriterOutput) ||
962 IsEqualGUID(riid, &IID_IUnknown))
964 *ppvObject = iface;
966 else
968 WARN("interface %s not implemented\n", debugstr_guid(riid));
969 *ppvObject = NULL;
970 return E_NOINTERFACE;
973 IUnknown_AddRef(iface);
975 return S_OK;
978 static ULONG WINAPI xmlwriteroutput_AddRef(IXmlWriterOutput *iface)
980 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
981 ULONG ref = InterlockedIncrement(&This->ref);
982 TRACE("(%p)->(%d)\n", This, ref);
983 return ref;
986 static ULONG WINAPI xmlwriteroutput_Release(IXmlWriterOutput *iface)
988 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
989 LONG ref = InterlockedDecrement(&This->ref);
991 TRACE("(%p)->(%d)\n", This, ref);
993 if (ref == 0)
995 IMalloc *imalloc = This->imalloc;
996 if (This->output) IUnknown_Release(This->output);
997 if (This->stream) ISequentialStream_Release(This->stream);
998 free_output_buffer(This);
999 writeroutput_free(This, This);
1000 if (imalloc) IMalloc_Release(imalloc);
1003 return ref;
1006 static const struct IUnknownVtbl xmlwriteroutputvtbl =
1008 xmlwriteroutput_QueryInterface,
1009 xmlwriteroutput_AddRef,
1010 xmlwriteroutput_Release
1013 HRESULT WINAPI CreateXmlWriter(REFIID riid, void **obj, IMalloc *imalloc)
1015 xmlwriter *writer;
1017 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid), obj, imalloc);
1019 if (!IsEqualGUID(riid, &IID_IXmlWriter))
1021 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid));
1022 return E_FAIL;
1025 if (imalloc)
1026 writer = IMalloc_Alloc(imalloc, sizeof(*writer));
1027 else
1028 writer = heap_alloc(sizeof(*writer));
1029 if(!writer) return E_OUTOFMEMORY;
1031 writer->IXmlWriter_iface.lpVtbl = &xmlwriter_vtbl;
1032 writer->ref = 1;
1033 writer->imalloc = imalloc;
1034 if (imalloc) IMalloc_AddRef(imalloc);
1035 writer->output = NULL;
1036 writer->indent = FALSE;
1037 writer->bom = TRUE;
1038 writer->omitxmldecl = FALSE;
1039 writer->conformance = XmlConformanceLevel_Document;
1040 writer->state = XmlWriterState_Initial;
1041 writer->bomwritten = FALSE;
1042 writer->starttagopen = FALSE;
1043 list_init(&writer->elements);
1045 *obj = &writer->IXmlWriter_iface;
1047 TRACE("returning iface %p\n", *obj);
1049 return S_OK;
1052 HRESULT WINAPI CreateXmlWriterOutputWithEncodingName(IUnknown *stream,
1053 IMalloc *imalloc,
1054 LPCWSTR encoding,
1055 IXmlWriterOutput **output)
1057 static const WCHAR utf8W[] = {'U','T','F','-','8',0};
1058 xmlwriteroutput *writeroutput;
1059 HRESULT hr;
1061 TRACE("%p %p %s %p\n", stream, imalloc, debugstr_w(encoding), output);
1063 if (!stream || !output) return E_INVALIDARG;
1065 *output = NULL;
1067 if (imalloc)
1068 writeroutput = IMalloc_Alloc(imalloc, sizeof(*writeroutput));
1069 else
1070 writeroutput = heap_alloc(sizeof(*writeroutput));
1071 if(!writeroutput) return E_OUTOFMEMORY;
1073 writeroutput->IXmlWriterOutput_iface.lpVtbl = &xmlwriteroutputvtbl;
1074 writeroutput->ref = 1;
1075 writeroutput->imalloc = imalloc;
1076 if (imalloc) IMalloc_AddRef(imalloc);
1077 writeroutput->encoding = parse_encoding_name(encoding ? encoding : utf8W, -1);
1078 writeroutput->stream = NULL;
1079 hr = init_output_buffer(writeroutput);
1080 if (FAILED(hr)) {
1081 IUnknown_Release(&writeroutput->IXmlWriterOutput_iface);
1082 return hr;
1085 IUnknown_QueryInterface(stream, &IID_IUnknown, (void**)&writeroutput->output);
1087 *output = &writeroutput->IXmlWriterOutput_iface;
1089 TRACE("returning iface %p\n", *output);
1091 return S_OK;