2 * IXmlReader implementation
4 * Copyright 2010, 2012 Nikolay Sivov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "xmllite_private.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(xmllite);
36 /* not defined in public headers */
37 DEFINE_GUID(IID_IXmlReaderInput, 0x0b3ccc9b, 0x9214, 0x428b, 0xa2, 0xae, 0xef, 0x3a, 0xa8, 0x71, 0xaf, 0xda);
46 static const WCHAR utf16W[] = {'U','T','F','-','1','6',0};
47 static const WCHAR utf8W[] = {'U','T','F','-','8',0};
49 struct xml_encoding_data
56 static const struct xml_encoding_data xml_encoding_map[] = {
57 { utf16W, XmlEncoding_UTF16, ~0 },
58 { utf8W, XmlEncoding_UTF8, CP_UTF8 }
65 unsigned int allocated;
69 typedef struct input_buffer input_buffer;
71 typedef struct _xmlreaderinput
73 IXmlReaderInput IXmlReaderInput_iface;
75 /* reference passed on IXmlReaderInput creation, is kept when input is created */
78 xml_encoding encoding;
81 /* stream reference set after SetInput() call from reader,
82 stored as sequential stream, cause currently
83 optimizations possible with IStream aren't implemented */
84 ISequentialStream *stream;
88 typedef struct _xmlreader
90 IXmlReader IXmlReader_iface;
92 xmlreaderinput *input;
96 DtdProcessing dtdmode;
97 UINT line, pos; /* reader position in XML stream */
102 encoded_buffer utf16;
103 encoded_buffer encoded;
105 xmlreaderinput *input;
108 static inline xmlreader *impl_from_IXmlReader(IXmlReader *iface)
110 return CONTAINING_RECORD(iface, xmlreader, IXmlReader_iface);
113 static inline xmlreaderinput *impl_from_IXmlReaderInput(IXmlReaderInput *iface)
115 return CONTAINING_RECORD(iface, xmlreaderinput, IXmlReaderInput_iface);
118 static inline void *m_alloc(IMalloc *imalloc, size_t len)
121 return IMalloc_Alloc(imalloc, len);
123 return heap_alloc(len);
126 static inline void *m_realloc(IMalloc *imalloc, void *mem, size_t len)
129 return IMalloc_Realloc(imalloc, mem, len);
131 return heap_realloc(mem, len);
134 static inline void m_free(IMalloc *imalloc, void *mem)
137 IMalloc_Free(imalloc, mem);
142 /* reader memory allocation functions */
143 static inline void *reader_alloc(xmlreader *reader, size_t len)
145 return m_alloc(reader->imalloc, len);
148 static inline void reader_free(xmlreader *reader, void *mem)
150 m_free(reader->imalloc, mem);
153 /* reader input memory allocation functions */
154 static inline void *readerinput_alloc(xmlreaderinput *input, size_t len)
156 return m_alloc(input->imalloc, len);
159 static inline void *readerinput_realloc(xmlreaderinput *input, void *mem, size_t len)
161 return m_realloc(input->imalloc, mem, len);
164 static inline void readerinput_free(xmlreaderinput *input, void *mem)
166 m_free(input->imalloc, mem);
169 static inline WCHAR *readerinput_strdupW(xmlreaderinput *input, const WCHAR *str)
176 size = (strlenW(str)+1)*sizeof(WCHAR);
177 ret = readerinput_alloc(input, size);
178 if (ret) memcpy(ret, str, size);
184 static HRESULT init_encoded_buffer(xmlreaderinput *input, encoded_buffer *buffer)
186 const int initial_len = 0x2000;
187 buffer->data = readerinput_alloc(input, initial_len);
188 if (!buffer->data) return E_OUTOFMEMORY;
190 memset(buffer->data, 0, 4);
191 buffer->cur = buffer->data;
192 buffer->allocated = initial_len;
198 static void free_encoded_buffer(xmlreaderinput *input, encoded_buffer *buffer)
200 readerinput_free(input, buffer->data);
203 static HRESULT get_code_page(xml_encoding encoding, UINT *cp)
205 if (encoding == XmlEncoding_Unknown)
207 FIXME("unsupported encoding %d\n", encoding);
211 *cp = xml_encoding_map[encoding].cp;
216 static xml_encoding parse_encoding_name(const WCHAR *name)
220 if (!name) return XmlEncoding_Unknown;
223 max = sizeof(xml_encoding_map)/sizeof(struct xml_encoding_data) - 1;
229 c = strcmpiW(xml_encoding_map[n].name, name);
231 return xml_encoding_map[n].enc;
239 return XmlEncoding_Unknown;
242 static HRESULT alloc_input_buffer(xmlreaderinput *input)
244 input_buffer *buffer;
247 input->buffer = NULL;
249 buffer = readerinput_alloc(input, sizeof(*buffer));
250 if (!buffer) return E_OUTOFMEMORY;
252 buffer->input = input;
253 buffer->code_page = ~0; /* code page is unknown at this point */
254 hr = init_encoded_buffer(input, &buffer->utf16);
256 readerinput_free(input, buffer);
260 hr = init_encoded_buffer(input, &buffer->encoded);
262 free_encoded_buffer(input, &buffer->utf16);
263 readerinput_free(input, buffer);
267 input->buffer = buffer;
271 static void free_input_buffer(input_buffer *buffer)
273 free_encoded_buffer(buffer->input, &buffer->encoded);
274 free_encoded_buffer(buffer->input, &buffer->utf16);
275 readerinput_free(buffer->input, buffer);
278 static void readerinput_release_stream(xmlreaderinput *readerinput)
280 if (readerinput->stream) {
281 ISequentialStream_Release(readerinput->stream);
282 readerinput->stream = NULL;
286 /* Queries already stored interface for IStream/ISequentialStream.
287 Interface supplied on creation will be overwritten */
288 static HRESULT readerinput_query_for_stream(xmlreaderinput *readerinput)
292 readerinput_release_stream(readerinput);
293 hr = IUnknown_QueryInterface(readerinput->input, &IID_IStream, (void**)&readerinput->stream);
295 hr = IUnknown_QueryInterface(readerinput->input, &IID_ISequentialStream, (void**)&readerinput->stream);
300 /* reads a chunk to raw buffer */
301 static HRESULT readerinput_growraw(xmlreaderinput *readerinput)
303 encoded_buffer *buffer = &readerinput->buffer->encoded;
304 ULONG len = buffer->allocated - buffer->written, read;
307 /* always try to get aligned to 4 bytes, so the only case we can get partialy read characters is
308 variable width encodings like UTF-8 */
309 len = (len + 3) & ~3;
310 /* try to use allocated space or grow */
311 if (buffer->allocated - buffer->written < len)
313 buffer->allocated *= 2;
314 buffer->data = readerinput_realloc(readerinput, buffer->data, buffer->allocated);
315 len = buffer->allocated - buffer->written;
318 hr = ISequentialStream_Read(readerinput->stream, buffer->data + buffer->written, len, &read);
319 if (FAILED(hr)) return hr;
320 TRACE("requested %d, read %d, ret 0x%08x\n", len, read, hr);
321 buffer->written += read;
326 /* grows UTF-16 buffer so it has at least 'length' bytes free on return */
327 static void readerinput_grow(xmlreaderinput *readerinput, int length)
329 encoded_buffer *buffer = &readerinput->buffer->utf16;
331 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
332 if (buffer->allocated < buffer->written + length + 4)
334 int grown_size = max(2*buffer->allocated, buffer->allocated + length);
335 buffer->data = readerinput_realloc(readerinput, buffer->data, grown_size);
336 buffer->allocated = grown_size;
340 static HRESULT readerinput_detectencoding(xmlreaderinput *readerinput, xml_encoding *enc)
342 encoded_buffer *buffer = &readerinput->buffer->encoded;
343 static char startA[] = {'<','?','x','m'};
344 static WCHAR startW[] = {'<','?'};
345 static char utf8bom[] = {0xef,0xbb,0xbf};
346 static char utf16lebom[] = {0xff,0xfe};
348 *enc = XmlEncoding_Unknown;
350 if (buffer->written <= 3) return MX_E_INPUTEND;
352 /* try start symbols if we have enough data to do that, input buffer should contain
353 first chunk already */
354 if (!memcmp(buffer->data, startA, sizeof(startA)))
355 *enc = XmlEncoding_UTF8;
356 else if (!memcmp(buffer->data, startW, sizeof(startW)))
357 *enc = XmlEncoding_UTF16;
358 /* try with BOM now */
359 else if (!memcmp(buffer->data, utf8bom, sizeof(utf8bom)))
361 buffer->cur += sizeof(utf8bom);
362 *enc = XmlEncoding_UTF8;
364 else if (!memcmp(buffer->data, utf16lebom, sizeof(utf16lebom)))
366 buffer->cur += sizeof(utf16lebom);
367 *enc = XmlEncoding_UTF16;
373 static int readerinput_get_utf8_convlen(xmlreaderinput *readerinput)
375 encoded_buffer *buffer = &readerinput->buffer->encoded;
376 int len = buffer->written;
378 /* complete single byte char */
379 if (!(buffer->data[len-1] & 0x80)) return len;
381 /* find start byte of multibyte char */
382 while (--len && !(buffer->data[len] & 0xc0))
388 /* returns byte length of complete char sequence for specified code page, */
389 static int readerinput_get_convlen(xmlreaderinput *readerinput, UINT cp)
391 encoded_buffer *buffer = &readerinput->buffer->encoded;
392 int len = buffer->written;
395 len = readerinput_get_utf8_convlen(readerinput);
397 len = buffer->written;
399 return len - (buffer->cur - buffer->data);
402 /* note that raw buffer content is kept */
403 static void readerinput_switchencoding(xmlreaderinput *readerinput, xml_encoding enc)
405 encoded_buffer *src = &readerinput->buffer->encoded;
406 encoded_buffer *dest = &readerinput->buffer->utf16;
411 hr = get_code_page(enc, &cp);
412 if (FAILED(hr)) return;
414 len = readerinput_get_convlen(readerinput, cp);
416 TRACE("switching to cp %d\n", cp);
418 /* just copy in this case */
419 if (enc == XmlEncoding_UTF16)
421 readerinput_grow(readerinput, len);
422 memcpy(dest->data, src->cur, len);
423 readerinput->buffer->code_page = cp;
427 dest_len = MultiByteToWideChar(cp, 0, src->cur, len, NULL, 0);
428 readerinput_grow(readerinput, dest_len);
429 MultiByteToWideChar(cp, 0, src->cur, len, (WCHAR*)dest->data, dest_len);
430 dest->data[dest_len] = 0;
431 readerinput->buffer->code_page = cp;
434 static inline const WCHAR *reader_get_cur(xmlreader *reader)
436 return (WCHAR*)reader->input->buffer->utf16.cur;
439 static int reader_cmp(xmlreader *reader, const WCHAR *str)
441 const WCHAR *ptr = reader_get_cur(reader);
444 return strncmpW(str, ptr, strlenW(str));
447 if (ptr[i] != str[i]) return 0;
454 /* moves cursor n WCHARs forward */
455 static void reader_skipn(xmlreader *reader, int n)
457 encoded_buffer *buffer = &reader->input->buffer->utf16;
458 const WCHAR *ptr = reader_get_cur(reader);
460 while (*ptr++ && n--)
462 buffer->cur += sizeof(WCHAR);
467 /* [3] S ::= (#x20 | #x9 | #xD | #xA)+ */
468 static int reader_skipspaces(xmlreader *reader)
470 encoded_buffer *buffer = &reader->input->buffer->utf16;
471 const WCHAR *ptr = reader_get_cur(reader), *start = ptr;
473 while (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')
475 buffer->cur += sizeof(WCHAR);
478 else if (*ptr == '\n')
491 /* [26] VersionNum ::= '1.' [0-9]+ */
492 static HRESULT reader_parse_versionnum(xmlreader *reader)
494 const WCHAR *ptr, *start = reader_get_cur(reader);
495 static const WCHAR onedotW[] = {'1','.',0};
497 if (reader_cmp(reader, onedotW)) return WC_E_XMLDECL;
499 reader_skipn(reader, 2);
501 ptr = reader_get_cur(reader);
502 while (*ptr >= '0' && *ptr <= '9')
505 if (ptr == start) return WC_E_DIGIT;
506 TRACE("version=%s\n", debugstr_wn(start, ptr-start));
507 reader_skipn(reader, ptr-start);
511 /* [24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"') */
512 static HRESULT reader_parse_versioninfo(xmlreader *reader)
514 static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
515 static const WCHAR dblquoteW[] = {'\"',0};
516 static const WCHAR quoteW[] = {'\'',0};
517 static const WCHAR eqW[] = {'=',0};
520 if (!reader_skipspaces(reader)) return WC_E_WHITESPACE;
522 if (reader_cmp(reader, versionW)) return WC_E_XMLDECL;
524 reader_skipn(reader, 7);
526 if (reader_cmp(reader, eqW)) return WC_E_EQUAL;
528 reader_skipn(reader, 1);
530 if (reader_cmp(reader, quoteW) && reader_cmp(reader, dblquoteW))
533 reader_skipn(reader, 1);
535 hr = reader_parse_versionnum(reader);
536 if (FAILED(hr)) return hr;
538 if (reader_cmp(reader, quoteW) && reader_cmp(reader, dblquoteW))
542 reader_skipn(reader, 1);
547 /* [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' */
548 static HRESULT reader_parse_xmldecl(xmlreader *reader)
550 static const WCHAR xmldeclW[] = {'<','?','x','m','l',0};
553 /* check if we have "<?xml" */
554 if (reader_cmp(reader, xmldeclW)) return S_OK;
556 reader_skipn(reader, 5);
557 hr = reader_parse_versioninfo(reader);
564 static HRESULT WINAPI xmlreader_QueryInterface(IXmlReader *iface, REFIID riid, void** ppvObject)
566 xmlreader *This = impl_from_IXmlReader(iface);
568 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
570 if (IsEqualGUID(riid, &IID_IUnknown) ||
571 IsEqualGUID(riid, &IID_IXmlReader))
577 FIXME("interface %s not implemented\n", debugstr_guid(riid));
578 return E_NOINTERFACE;
581 IXmlReader_AddRef(iface);
586 static ULONG WINAPI xmlreader_AddRef(IXmlReader *iface)
588 xmlreader *This = impl_from_IXmlReader(iface);
589 ULONG ref = InterlockedIncrement(&This->ref);
590 TRACE("(%p)->(%d)\n", This, ref);
594 static ULONG WINAPI xmlreader_Release(IXmlReader *iface)
596 xmlreader *This = impl_from_IXmlReader(iface);
597 LONG ref = InterlockedDecrement(&This->ref);
599 TRACE("(%p)->(%d)\n", This, ref);
603 IMalloc *imalloc = This->imalloc;
604 if (This->input) IUnknown_Release(&This->input->IXmlReaderInput_iface);
605 reader_free(This, This);
606 if (imalloc) IMalloc_Release(imalloc);
612 static HRESULT WINAPI xmlreader_SetInput(IXmlReader* iface, IUnknown *input)
614 xmlreader *This = impl_from_IXmlReader(iface);
617 TRACE("(%p %p)\n", This, input);
621 readerinput_release_stream(This->input);
622 IUnknown_Release(&This->input->IXmlReaderInput_iface);
626 This->line = This->pos = 0;
628 /* just reset current input */
631 This->state = XmlReadState_Initial;
635 /* now try IXmlReaderInput, ISequentialStream, IStream */
636 hr = IUnknown_QueryInterface(input, &IID_IXmlReaderInput, (void**)&This->input);
639 IXmlReaderInput *readerinput;
641 /* create IXmlReaderInput basing on supplied interface */
642 hr = CreateXmlReaderInputWithEncodingName(input,
643 NULL, NULL, FALSE, NULL, &readerinput);
644 if (hr != S_OK) return hr;
645 This->input = impl_from_IXmlReaderInput(readerinput);
648 /* set stream for supplied IXmlReaderInput */
649 hr = readerinput_query_for_stream(This->input);
651 This->state = XmlReadState_Initial;
656 static HRESULT WINAPI xmlreader_GetProperty(IXmlReader* iface, UINT property, LONG_PTR *value)
658 xmlreader *This = impl_from_IXmlReader(iface);
660 TRACE("(%p %u %p)\n", This, property, value);
662 if (!value) return E_INVALIDARG;
666 case XmlReaderProperty_DtdProcessing:
667 *value = This->dtdmode;
669 case XmlReaderProperty_ReadState:
670 *value = This->state;
673 FIXME("Unimplemented property (%u)\n", property);
680 static HRESULT WINAPI xmlreader_SetProperty(IXmlReader* iface, UINT property, LONG_PTR value)
682 xmlreader *This = impl_from_IXmlReader(iface);
684 TRACE("(%p %u %lu)\n", iface, property, value);
688 case XmlReaderProperty_DtdProcessing:
689 if (value < 0 || value > _DtdProcessing_Last) return E_INVALIDARG;
690 This->dtdmode = value;
693 FIXME("Unimplemented property (%u)\n", property);
700 static HRESULT WINAPI xmlreader_Read(IXmlReader* iface, XmlNodeType *node_type)
702 xmlreader *This = impl_from_IXmlReader(iface);
704 FIXME("(%p)->(%p): stub\n", This, node_type);
706 if (This->state == XmlReadState_Closed) return S_FALSE;
708 /* if it's a first call for a new input we need to detect stream encoding */
709 if (This->state == XmlReadState_Initial)
714 hr = readerinput_growraw(This->input);
715 if (FAILED(hr)) return hr;
717 /* try to detect encoding by BOM or data and set input code page */
718 hr = readerinput_detectencoding(This->input, &enc);
719 TRACE("detected encoding %s, 0x%08x\n", debugstr_w(xml_encoding_map[enc].name), hr);
720 if (FAILED(hr)) return hr;
722 /* always switch first time cause we have to put something in */
723 readerinput_switchencoding(This->input, enc);
725 /* parse xml declaration */
726 hr = reader_parse_xmldecl(This);
727 if (FAILED(hr)) return hr;
729 This->state = XmlReadState_Interactive;
735 static HRESULT WINAPI xmlreader_GetNodeType(IXmlReader* iface, XmlNodeType *node_type)
737 xmlreader *This = impl_from_IXmlReader(iface);
738 TRACE("(%p)->(%p)\n", This, node_type);
739 *node_type = This->nodetype;
740 return This->state == XmlReadState_Closed ? S_FALSE : S_OK;
743 static HRESULT WINAPI xmlreader_MoveToFirstAttribute(IXmlReader* iface)
745 FIXME("(%p): stub\n", iface);
749 static HRESULT WINAPI xmlreader_MoveToNextAttribute(IXmlReader* iface)
751 FIXME("(%p): stub\n", iface);
755 static HRESULT WINAPI xmlreader_MoveToAttributeByName(IXmlReader* iface,
757 LPCWSTR namespaceUri)
759 FIXME("(%p %p %p): stub\n", iface, local_name, namespaceUri);
763 static HRESULT WINAPI xmlreader_MoveToElement(IXmlReader* iface)
765 FIXME("(%p): stub\n", iface);
769 static HRESULT WINAPI xmlreader_GetQualifiedName(IXmlReader* iface, LPCWSTR *qualifiedName,
770 UINT *qualifiedName_length)
772 FIXME("(%p %p %p): stub\n", iface, qualifiedName, qualifiedName_length);
776 static HRESULT WINAPI xmlreader_GetNamespaceUri(IXmlReader* iface,
777 LPCWSTR *namespaceUri,
778 UINT *namespaceUri_length)
780 FIXME("(%p %p %p): stub\n", iface, namespaceUri, namespaceUri_length);
784 static HRESULT WINAPI xmlreader_GetLocalName(IXmlReader* iface,
786 UINT *local_name_length)
788 FIXME("(%p %p %p): stub\n", iface, local_name, local_name_length);
792 static HRESULT WINAPI xmlreader_GetPrefix(IXmlReader* iface,
796 FIXME("(%p %p %p): stub\n", iface, prefix, prefix_length);
800 static HRESULT WINAPI xmlreader_GetValue(IXmlReader* iface,
804 FIXME("(%p %p %p): stub\n", iface, value, value_length);
808 static HRESULT WINAPI xmlreader_ReadValueChunk(IXmlReader* iface,
813 FIXME("(%p %p %u %p): stub\n", iface, buffer, chunk_size, read);
817 static HRESULT WINAPI xmlreader_GetBaseUri(IXmlReader* iface,
819 UINT *baseUri_length)
821 FIXME("(%p %p %p): stub\n", iface, baseUri, baseUri_length);
825 static BOOL WINAPI xmlreader_IsDefault(IXmlReader* iface)
827 FIXME("(%p): stub\n", iface);
831 static BOOL WINAPI xmlreader_IsEmptyElement(IXmlReader* iface)
833 FIXME("(%p): stub\n", iface);
837 static HRESULT WINAPI xmlreader_GetLineNumber(IXmlReader* iface, UINT *lineNumber)
839 xmlreader *This = impl_from_IXmlReader(iface);
841 TRACE("(%p %p)\n", This, lineNumber);
843 if (!lineNumber) return E_INVALIDARG;
845 *lineNumber = This->line;
850 static HRESULT WINAPI xmlreader_GetLinePosition(IXmlReader* iface, UINT *linePosition)
852 xmlreader *This = impl_from_IXmlReader(iface);
854 TRACE("(%p %p)\n", This, linePosition);
856 if (!linePosition) return E_INVALIDARG;
858 *linePosition = This->pos;
863 static HRESULT WINAPI xmlreader_GetAttributeCount(IXmlReader* iface, UINT *attributeCount)
865 FIXME("(%p %p): stub\n", iface, attributeCount);
869 static HRESULT WINAPI xmlreader_GetDepth(IXmlReader* iface, UINT *depth)
871 FIXME("(%p %p): stub\n", iface, depth);
875 static BOOL WINAPI xmlreader_IsEOF(IXmlReader* iface)
877 FIXME("(%p): stub\n", iface);
881 static const struct IXmlReaderVtbl xmlreader_vtbl =
883 xmlreader_QueryInterface,
887 xmlreader_GetProperty,
888 xmlreader_SetProperty,
890 xmlreader_GetNodeType,
891 xmlreader_MoveToFirstAttribute,
892 xmlreader_MoveToNextAttribute,
893 xmlreader_MoveToAttributeByName,
894 xmlreader_MoveToElement,
895 xmlreader_GetQualifiedName,
896 xmlreader_GetNamespaceUri,
897 xmlreader_GetLocalName,
900 xmlreader_ReadValueChunk,
901 xmlreader_GetBaseUri,
903 xmlreader_IsEmptyElement,
904 xmlreader_GetLineNumber,
905 xmlreader_GetLinePosition,
906 xmlreader_GetAttributeCount,
911 /** IXmlReaderInput **/
912 static HRESULT WINAPI xmlreaderinput_QueryInterface(IXmlReaderInput *iface, REFIID riid, void** ppvObject)
914 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
916 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
918 if (IsEqualGUID(riid, &IID_IXmlReaderInput) ||
919 IsEqualGUID(riid, &IID_IUnknown))
925 WARN("interface %s not implemented\n", debugstr_guid(riid));
926 return E_NOINTERFACE;
929 IUnknown_AddRef(iface);
934 static ULONG WINAPI xmlreaderinput_AddRef(IXmlReaderInput *iface)
936 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
937 ULONG ref = InterlockedIncrement(&This->ref);
938 TRACE("(%p)->(%d)\n", This, ref);
942 static ULONG WINAPI xmlreaderinput_Release(IXmlReaderInput *iface)
944 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
945 LONG ref = InterlockedDecrement(&This->ref);
947 TRACE("(%p)->(%d)\n", This, ref);
951 IMalloc *imalloc = This->imalloc;
952 if (This->input) IUnknown_Release(This->input);
953 if (This->stream) ISequentialStream_Release(This->stream);
954 if (This->buffer) free_input_buffer(This->buffer);
955 readerinput_free(This, This->baseuri);
956 readerinput_free(This, This);
957 if (imalloc) IMalloc_Release(imalloc);
963 static const struct IUnknownVtbl xmlreaderinput_vtbl =
965 xmlreaderinput_QueryInterface,
966 xmlreaderinput_AddRef,
967 xmlreaderinput_Release
970 HRESULT WINAPI CreateXmlReader(REFIID riid, void **obj, IMalloc *imalloc)
974 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid), obj, imalloc);
976 if (!IsEqualGUID(riid, &IID_IXmlReader))
978 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid));
983 reader = IMalloc_Alloc(imalloc, sizeof(*reader));
985 reader = heap_alloc(sizeof(*reader));
986 if(!reader) return E_OUTOFMEMORY;
988 reader->IXmlReader_iface.lpVtbl = &xmlreader_vtbl;
990 reader->input = NULL;
991 reader->state = XmlReadState_Closed;
992 reader->dtdmode = DtdProcessing_Prohibit;
993 reader->line = reader->pos = 0;
994 reader->imalloc = imalloc;
995 if (imalloc) IMalloc_AddRef(imalloc);
996 reader->nodetype = XmlNodeType_None;
998 *obj = &reader->IXmlReader_iface;
1000 TRACE("returning iface %p\n", *obj);
1005 HRESULT WINAPI CreateXmlReaderInputWithEncodingName(IUnknown *stream,
1010 IXmlReaderInput **ppInput)
1012 xmlreaderinput *readerinput;
1015 TRACE("%p %p %s %d %s %p\n", stream, imalloc, wine_dbgstr_w(encoding),
1016 hint, wine_dbgstr_w(base_uri), ppInput);
1018 if (!stream || !ppInput) return E_INVALIDARG;
1021 readerinput = IMalloc_Alloc(imalloc, sizeof(*readerinput));
1023 readerinput = heap_alloc(sizeof(*readerinput));
1024 if(!readerinput) return E_OUTOFMEMORY;
1026 readerinput->IXmlReaderInput_iface.lpVtbl = &xmlreaderinput_vtbl;
1027 readerinput->ref = 1;
1028 readerinput->imalloc = imalloc;
1029 readerinput->stream = NULL;
1030 if (imalloc) IMalloc_AddRef(imalloc);
1031 readerinput->encoding = parse_encoding_name(encoding);
1032 readerinput->hint = hint;
1033 readerinput->baseuri = readerinput_strdupW(readerinput, base_uri);
1035 hr = alloc_input_buffer(readerinput);
1038 readerinput_free(readerinput, readerinput);
1041 IUnknown_QueryInterface(stream, &IID_IUnknown, (void**)&readerinput->input);
1043 *ppInput = &readerinput->IXmlReaderInput_iface;
1045 TRACE("returning iface %p\n", *ppInput);