gdi32: Try to parse font names without FreeType.
[wine/zf.git] / dlls / webservices / reader.c
blobffa789fb6e6bfcf56d3ea75bae17be18017650c7
1 /*
2 * Copyright 2015, 2016 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <assert.h>
21 #include <float.h>
22 #include <locale.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "webservices.h"
29 #include "wine/debug.h"
30 #include "wine/heap.h"
31 #include "wine/list.h"
32 #include "webservices_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
36 ULONG prop_size( const struct prop_desc *desc, ULONG count )
38 ULONG i, ret = count * sizeof(struct prop);
39 for (i = 0; i < count; i++) ret += desc[i].size;
40 return ret;
43 void prop_init( const struct prop_desc *desc, ULONG count, struct prop *prop, void *data )
45 ULONG i;
46 char *ptr = data;
47 for (i = 0; i < count; i++)
49 prop[i].value = ptr;
50 prop[i].size = desc[i].size;
51 prop[i].readonly = desc[i].readonly;
52 prop[i].writeonly = desc[i].writeonly;
53 ptr += prop[i].size;
57 HRESULT prop_set( const struct prop *prop, ULONG count, ULONG id, const void *value, ULONG size )
59 if (id >= count || size != prop[id].size || prop[id].readonly) return E_INVALIDARG;
60 memcpy( prop[id].value, value, size );
61 return S_OK;
64 HRESULT prop_get( const struct prop *prop, ULONG count, ULONG id, void *buf, ULONG size )
66 if (id >= count || size != prop[id].size || prop[id].writeonly) return E_INVALIDARG;
67 memcpy( buf, prop[id].value, prop[id].size );
68 return S_OK;
71 struct node *alloc_node( WS_XML_NODE_TYPE type )
73 struct node *ret;
75 if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
76 ret->hdr.node.nodeType = type;
77 list_init( &ret->entry );
78 list_init( &ret->children );
79 return ret;
82 void free_attribute( WS_XML_ATTRIBUTE *attr )
84 if (!attr) return;
85 free_xml_string( attr->prefix );
86 free_xml_string( attr->localName );
87 free_xml_string( attr->ns );
88 heap_free( attr->value );
89 heap_free( attr );
92 void free_node( struct node *node )
94 if (!node) return;
95 switch (node_type( node ))
97 case WS_XML_NODE_TYPE_ELEMENT:
99 WS_XML_ELEMENT_NODE *elem = &node->hdr;
100 ULONG i;
102 for (i = 0; i < elem->attributeCount; i++) free_attribute( elem->attributes[i] );
103 heap_free( elem->attributes );
104 free_xml_string( elem->prefix );
105 free_xml_string( elem->localName );
106 free_xml_string( elem->ns );
107 break;
109 case WS_XML_NODE_TYPE_TEXT:
111 WS_XML_TEXT_NODE *text = (WS_XML_TEXT_NODE *)node;
112 heap_free( text->text );
113 break;
115 case WS_XML_NODE_TYPE_COMMENT:
117 WS_XML_COMMENT_NODE *comment = (WS_XML_COMMENT_NODE *)node;
118 heap_free( comment->value.bytes );
119 break;
121 case WS_XML_NODE_TYPE_CDATA:
122 case WS_XML_NODE_TYPE_END_CDATA:
123 case WS_XML_NODE_TYPE_END_ELEMENT:
124 case WS_XML_NODE_TYPE_EOF:
125 case WS_XML_NODE_TYPE_BOF:
126 break;
128 default:
129 ERR( "unhandled type %u\n", node_type( node ) );
130 break;
132 heap_free( node );
135 void destroy_nodes( struct node *node )
137 struct list *ptr;
139 if (!node) return;
140 while ((ptr = list_head( &node->children )))
142 struct node *child = LIST_ENTRY( ptr, struct node, entry );
143 list_remove( &child->entry );
144 destroy_nodes( child );
146 free_node( node );
149 static WS_XML_ATTRIBUTE *dup_attribute( const WS_XML_ATTRIBUTE *src, WS_XML_WRITER_ENCODING_TYPE enc )
151 WS_XML_ATTRIBUTE *dst;
152 HRESULT hr;
154 if (!(dst = heap_alloc_zero( sizeof(*dst) ))) return NULL;
155 dst->singleQuote = src->singleQuote;
156 dst->isXmlNs = src->isXmlNs;
158 if (src->prefix && !(dst->prefix = dup_xml_string( src->prefix, FALSE ))) goto error;
159 if (src->localName && !(dst->localName = dup_xml_string( src->localName, FALSE ))) goto error;
160 if (src->ns && !(dst->ns = dup_xml_string( src->ns, FALSE ))) goto error;
162 if (src->value)
164 switch (enc)
166 case WS_XML_WRITER_ENCODING_TYPE_BINARY:
167 if ((hr = text_to_text( src->value, NULL, NULL, &dst->value )) != S_OK) goto error;
168 break;
170 case WS_XML_WRITER_ENCODING_TYPE_TEXT:
171 if ((hr = text_to_utf8text( src->value, NULL, NULL, (WS_XML_UTF8_TEXT **)&dst->value )) != S_OK)
172 goto error;
173 break;
175 default:
176 ERR( "unhandled encoding %u\n", enc );
177 goto error;
181 return dst;
183 error:
184 free_attribute( dst );
185 return NULL;
188 static WS_XML_ATTRIBUTE **dup_attributes( WS_XML_ATTRIBUTE * const *src, ULONG count,
189 WS_XML_WRITER_ENCODING_TYPE enc )
191 WS_XML_ATTRIBUTE **dst;
192 ULONG i;
194 if (!(dst = heap_alloc( sizeof(*dst) * count ))) return NULL;
195 for (i = 0; i < count; i++)
197 if (!(dst[i] = dup_attribute( src[i], enc )))
199 for (; i > 0; i--) free_attribute( dst[i - 1] );
200 heap_free( dst );
201 return NULL;
204 return dst;
207 static struct node *dup_element_node( const WS_XML_ELEMENT_NODE *src, WS_XML_WRITER_ENCODING_TYPE enc )
209 struct node *node;
210 WS_XML_ELEMENT_NODE *dst;
211 ULONG count = src->attributeCount;
212 WS_XML_ATTRIBUTE **attrs = src->attributes;
213 const WS_XML_STRING *prefix = (src->prefix && src->prefix->length) ? src->prefix : NULL;
214 const WS_XML_STRING *localname = src->localName;
215 const WS_XML_STRING *ns = src->ns;
217 if (!(node = alloc_node( WS_XML_NODE_TYPE_ELEMENT ))) return NULL;
218 dst = &node->hdr;
220 if (count && !(dst->attributes = dup_attributes( attrs, count, enc ))) goto error;
221 dst->attributeCount = count;
223 if (prefix && !(dst->prefix = dup_xml_string( prefix, FALSE ))) goto error;
224 if (localname && !(dst->localName = dup_xml_string( localname, FALSE ))) goto error;
225 if (ns && !(dst->ns = dup_xml_string( ns, FALSE ))) goto error;
226 return node;
228 error:
229 free_node( node );
230 return NULL;
233 static struct node *dup_text_node( const WS_XML_TEXT_NODE *src, WS_XML_WRITER_ENCODING_TYPE enc )
235 struct node *node;
236 WS_XML_TEXT_NODE *dst;
237 HRESULT hr;
239 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
240 dst = (WS_XML_TEXT_NODE *)node;
241 if (!src->text) return node;
243 switch (enc)
245 case WS_XML_WRITER_ENCODING_TYPE_BINARY:
246 hr = text_to_text( src->text, NULL, NULL, &dst->text );
247 break;
249 case WS_XML_WRITER_ENCODING_TYPE_TEXT:
250 hr = text_to_utf8text( src->text, NULL, NULL, (WS_XML_UTF8_TEXT **)&dst->text );
251 break;
253 default:
254 ERR( "unhandled encoding %u\n", enc );
255 free_node( node );
256 return NULL;
259 if (hr != S_OK)
261 free_node( node );
262 return NULL;
265 return node;
268 static struct node *dup_comment_node( const WS_XML_COMMENT_NODE *src )
270 struct node *node;
271 WS_XML_COMMENT_NODE *dst;
273 if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return NULL;
274 dst = (WS_XML_COMMENT_NODE *)node;
276 if (src->value.length && !(dst->value.bytes = heap_alloc( src->value.length )))
278 free_node( node );
279 return NULL;
281 memcpy( dst->value.bytes, src->value.bytes, src->value.length );
282 dst->value.length = src->value.length;
283 return node;
286 static struct node *dup_node( const struct node *src, WS_XML_WRITER_ENCODING_TYPE enc )
288 switch (node_type( src ))
290 case WS_XML_NODE_TYPE_ELEMENT:
291 return dup_element_node( &src->hdr, enc );
293 case WS_XML_NODE_TYPE_TEXT:
294 return dup_text_node( (const WS_XML_TEXT_NODE *)src, enc );
296 case WS_XML_NODE_TYPE_COMMENT:
297 return dup_comment_node( (const WS_XML_COMMENT_NODE *)src );
299 case WS_XML_NODE_TYPE_CDATA:
300 case WS_XML_NODE_TYPE_END_CDATA:
301 case WS_XML_NODE_TYPE_END_ELEMENT:
302 case WS_XML_NODE_TYPE_EOF:
303 case WS_XML_NODE_TYPE_BOF:
304 return alloc_node( node_type( src ) );
306 default:
307 ERR( "unhandled type %u\n", node_type( src ) );
308 break;
310 return NULL;
313 static HRESULT dup_tree( const struct node *src, WS_XML_WRITER_ENCODING_TYPE enc, struct node **dst )
315 struct node *parent;
316 const struct node *child;
318 if (!*dst && !(*dst = dup_node( src, enc ))) return E_OUTOFMEMORY;
319 parent = *dst;
321 LIST_FOR_EACH_ENTRY( child, &src->children, struct node, entry )
323 HRESULT hr = E_OUTOFMEMORY;
324 struct node *new_child;
326 if (!(new_child = dup_node( child, enc )) || (hr = dup_tree( child, enc, &new_child )) != S_OK)
328 destroy_nodes( *dst );
329 return hr;
331 new_child->parent = parent;
332 list_add_tail( &parent->children, &new_child->entry );
334 return S_OK;
337 static const struct prop_desc reader_props[] =
339 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_DEPTH */
340 { sizeof(BOOL), FALSE }, /* WS_XML_READER_PROPERTY_ALLOW_FRAGMENT */
341 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_ATTRIBUTES */
342 { sizeof(BOOL), FALSE }, /* WS_XML_READER_PROPERTY_READ_DECLARATION */
343 { sizeof(WS_CHARSET), FALSE }, /* WS_XML_READER_PROPERTY_CHARSET */
344 { sizeof(ULONGLONG), TRUE }, /* WS_XML_READER_PROPERTY_ROW */
345 { sizeof(ULONGLONG), TRUE }, /* WS_XML_READER_PROPERTY_COLUMN */
346 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_UTF8_TRIM_SIZE */
347 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_STREAM_BUFFER_SIZE */
348 { sizeof(BOOL), TRUE }, /* WS_XML_READER_PROPERTY_IN_ATTRIBUTE */
349 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_STREAM_MAX_ROOT_MIME_PART_SIZE */
350 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_STREAM_MAX_MIME_HEADERS_SIZE */
351 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_MIME_PARTS */
352 { sizeof(BOOL), FALSE }, /* WS_XML_READER_PROPERTY_ALLOW_INVALID_CHARACTER_REFERENCES */
353 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_NAMESPACES */
356 enum reader_state
358 READER_STATE_INITIAL,
359 READER_STATE_BOF,
360 READER_STATE_STARTELEMENT,
361 READER_STATE_STARTATTRIBUTE,
362 READER_STATE_STARTCDATA,
363 READER_STATE_CDATA,
364 READER_STATE_TEXT,
365 READER_STATE_ENDELEMENT,
366 READER_STATE_ENDCDATA,
367 READER_STATE_COMMENT,
368 READER_STATE_EOF
371 struct prefix
373 WS_XML_STRING *str;
374 WS_XML_STRING *ns;
377 struct reader
379 ULONG magic;
380 CRITICAL_SECTION cs;
381 ULONG read_size;
382 ULONG read_pos;
383 const unsigned char *read_bufptr;
384 enum reader_state state;
385 struct node *root;
386 struct node *current;
387 ULONG current_attr;
388 struct node *last;
389 struct prefix *prefixes;
390 ULONG nb_prefixes;
391 ULONG nb_prefixes_allocated;
392 WS_XML_READER_ENCODING_TYPE input_enc;
393 WS_CHARSET input_charset;
394 WS_XML_READER_INPUT_TYPE input_type;
395 WS_READ_CALLBACK input_cb;
396 void *input_cb_state;
397 struct xmlbuf *input_buf;
398 unsigned char *input_conv;
399 ULONG input_size;
400 ULONG text_conv_offset;
401 unsigned char *stream_buf;
402 const WS_XML_DICTIONARY *dict_static;
403 WS_XML_DICTIONARY *dict;
404 ULONG prop_count;
405 struct prop prop[ARRAY_SIZE( reader_props )];
408 #define READER_MAGIC (('R' << 24) | ('E' << 16) | ('A' << 8) | 'D')
410 static struct reader *alloc_reader(void)
412 static const ULONG count = ARRAY_SIZE( reader_props );
413 struct reader *ret;
414 ULONG size = sizeof(*ret) + prop_size( reader_props, count );
416 if (!(ret = heap_alloc_zero( size ))) return NULL;
417 if (!(ret->prefixes = heap_alloc_zero( sizeof(*ret->prefixes) )))
419 heap_free( ret );
420 return NULL;
422 ret->nb_prefixes = ret->nb_prefixes_allocated = 1;
424 ret->magic = READER_MAGIC;
425 InitializeCriticalSection( &ret->cs );
426 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": reader.cs");
428 prop_init( reader_props, count, ret->prop, &ret[1] );
429 ret->prop_count = count;
430 return ret;
433 static void clear_prefixes( struct prefix *prefixes, ULONG count )
435 ULONG i;
436 for (i = 0; i < count; i++)
438 free_xml_string( prefixes[i].str );
439 prefixes[i].str = NULL;
440 free_xml_string( prefixes[i].ns );
441 prefixes[i].ns = NULL;
445 static HRESULT set_prefix( struct prefix *prefix, const WS_XML_STRING *str, const WS_XML_STRING *ns )
447 if (str)
449 free_xml_string( prefix->str );
450 if (!(prefix->str = dup_xml_string( str, FALSE ))) return E_OUTOFMEMORY;
452 if (prefix->ns) free_xml_string( prefix->ns );
453 if (!(prefix->ns = dup_xml_string( ns, FALSE ))) return E_OUTOFMEMORY;
454 return S_OK;
457 static HRESULT bind_prefix( struct reader *reader, const WS_XML_STRING *prefix, const WS_XML_STRING *ns )
459 ULONG i;
460 HRESULT hr;
462 for (i = 0; i < reader->nb_prefixes; i++)
464 if (WsXmlStringEquals( prefix, reader->prefixes[i].str, NULL ) == S_OK)
465 return set_prefix( &reader->prefixes[i], NULL, ns );
467 if (i >= reader->nb_prefixes_allocated)
469 ULONG new_size = reader->nb_prefixes_allocated * sizeof(*reader->prefixes) * 2;
470 struct prefix *tmp = heap_realloc_zero( reader->prefixes, new_size );
471 if (!tmp) return E_OUTOFMEMORY;
472 reader->prefixes = tmp;
473 reader->nb_prefixes_allocated *= 2;
475 if ((hr = set_prefix( &reader->prefixes[i], prefix, ns )) != S_OK) return hr;
476 reader->nb_prefixes++;
477 return S_OK;
480 static const WS_XML_STRING *get_namespace( struct reader *reader, const WS_XML_STRING *prefix )
482 ULONG i;
483 for (i = 0; i < reader->nb_prefixes; i++)
485 if (WsXmlStringEquals( prefix, reader->prefixes[i].str, NULL ) == S_OK)
486 return reader->prefixes[i].ns;
488 return NULL;
491 static void read_insert_eof( struct reader *reader, struct node *eof )
493 if (!reader->root) reader->root = eof;
494 else
496 eof->parent = reader->root;
497 list_add_tail( &reader->root->children, &eof->entry );
499 reader->current = reader->last = eof;
502 static void read_insert_bof( struct reader *reader, struct node *bof )
504 reader->root->parent = bof;
505 list_add_tail( &bof->children, &reader->root->entry );
506 reader->current = reader->last = reader->root = bof;
509 static void read_insert_node( struct reader *reader, struct node *parent, struct node *node )
511 node->parent = parent;
512 list_add_before( list_tail( &parent->children ), &node->entry );
513 reader->current = reader->last = node;
516 static void free_reader( struct reader *reader )
518 destroy_nodes( reader->root );
519 clear_prefixes( reader->prefixes, reader->nb_prefixes );
520 heap_free( reader->prefixes );
521 heap_free( reader->stream_buf );
522 heap_free( reader->input_conv );
524 reader->cs.DebugInfo->Spare[0] = 0;
525 DeleteCriticalSection( &reader->cs );
526 heap_free( reader );
529 static HRESULT init_reader( struct reader *reader )
531 static const WS_XML_STRING empty = {0, NULL};
532 struct node *node;
533 HRESULT hr;
535 reader->state = READER_STATE_INITIAL;
536 destroy_nodes( reader->root );
537 reader->root = reader->current = NULL;
538 reader->current_attr = 0;
539 clear_prefixes( reader->prefixes, reader->nb_prefixes );
540 reader->nb_prefixes = 1;
541 if ((hr = bind_prefix( reader, &empty, &empty )) != S_OK) return hr;
543 if (!(node = alloc_node( WS_XML_NODE_TYPE_EOF ))) return E_OUTOFMEMORY;
544 read_insert_eof( reader, node );
545 reader->input_enc = WS_XML_READER_ENCODING_TYPE_TEXT;
546 reader->input_charset = WS_CHARSET_UTF8;
547 reader->dict_static = NULL;
548 reader->dict = NULL;
549 return S_OK;
552 /**************************************************************************
553 * WsCreateReader [webservices.@]
555 HRESULT WINAPI WsCreateReader( const WS_XML_READER_PROPERTY *properties, ULONG count,
556 WS_XML_READER **handle, WS_ERROR *error )
558 struct reader *reader;
559 ULONG i, max_depth = 32, max_attrs = 128, max_ns = 32;
560 BOOL read_decl = TRUE;
561 HRESULT hr;
563 TRACE( "%p %u %p %p\n", properties, count, handle, error );
564 if (error) FIXME( "ignoring error parameter\n" );
566 if (!handle) return E_INVALIDARG;
567 if (!(reader = alloc_reader())) return E_OUTOFMEMORY;
569 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_DEPTH, &max_depth, sizeof(max_depth) );
570 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_ATTRIBUTES, &max_attrs, sizeof(max_attrs) );
571 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_READ_DECLARATION, &read_decl, sizeof(read_decl) );
572 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_NAMESPACES, &max_ns, sizeof(max_ns) );
574 for (i = 0; i < count; i++)
576 hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
577 properties[i].valueSize );
578 if (hr != S_OK)
580 free_reader( reader );
581 return hr;
585 if ((hr = init_reader( reader )) != S_OK)
587 free_reader( reader );
588 return hr;
591 TRACE( "created %p\n", reader );
592 *handle = (WS_XML_READER *)reader;
593 return S_OK;
596 /**************************************************************************
597 * WsFreeReader [webservices.@]
599 void WINAPI WsFreeReader( WS_XML_READER *handle )
601 struct reader *reader = (struct reader *)handle;
603 TRACE( "%p\n", handle );
605 if (!reader) return;
607 EnterCriticalSection( &reader->cs );
609 if (reader->magic != READER_MAGIC)
611 LeaveCriticalSection( &reader->cs );
612 return;
615 reader->magic = 0;
617 LeaveCriticalSection( &reader->cs );
618 free_reader( reader );
621 static HRESULT read_more_data( struct reader *reader, ULONG min_size, const WS_ASYNC_CONTEXT *ctx,
622 WS_ERROR *error )
624 ULONG size = 0, max_size;
626 if (reader->read_size - reader->read_pos >= min_size) return S_OK;
627 if (reader->input_type != WS_XML_READER_INPUT_TYPE_STREAM) return WS_E_INVALID_FORMAT;
628 if (min_size > reader->input_size) return WS_E_QUOTA_EXCEEDED;
630 if (reader->read_pos)
632 memmove( reader->stream_buf, reader->stream_buf + reader->read_pos, reader->read_size - reader->read_pos );
633 reader->read_size -= reader->read_pos;
634 reader->read_pos = 0;
636 max_size = reader->input_size - reader->read_size;
638 reader->input_cb( reader->input_cb_state, reader->stream_buf + reader->read_size, max_size, &size, ctx, error );
639 if (size < min_size) return WS_E_QUOTA_EXCEEDED;
640 reader->read_size += size;
641 return S_OK;
644 /**************************************************************************
645 * WsFillReader [webservices.@]
647 HRESULT WINAPI WsFillReader( WS_XML_READER *handle, ULONG min_size, const WS_ASYNC_CONTEXT *ctx,
648 WS_ERROR *error )
650 struct reader *reader = (struct reader *)handle;
651 HRESULT hr;
653 TRACE( "%p %u %p %p\n", handle, min_size, ctx, error );
654 if (error) FIXME( "ignoring error parameter\n" );
655 if (ctx) FIXME( "ignoring ctx parameter\n" );
657 if (!reader) return E_INVALIDARG;
659 EnterCriticalSection( &reader->cs );
661 if (reader->magic != READER_MAGIC)
663 LeaveCriticalSection( &reader->cs );
664 return E_INVALIDARG;
667 if (reader->input_type == WS_XML_READER_INPUT_TYPE_STREAM)
669 hr = read_more_data( reader, min_size, ctx, error );
671 else
673 reader->read_size = min( min_size, reader->input_size );
674 reader->read_pos = 0;
675 hr = S_OK;
678 LeaveCriticalSection( &reader->cs );
679 TRACE( "returning %08x\n", hr );
680 return hr;
683 /**************************************************************************
684 * WsGetNamespaceFromPrefix [webservices.@]
686 HRESULT WINAPI WsGetNamespaceFromPrefix( WS_XML_READER *handle, const WS_XML_STRING *prefix,
687 BOOL required, const WS_XML_STRING **ns, WS_ERROR *error )
689 static const WS_XML_STRING xml = {3, (BYTE *)"xml"};
690 static const WS_XML_STRING xmlns = {5, (BYTE *)"xmlns"};
691 static const WS_XML_STRING empty_ns = {0, NULL};
692 static const WS_XML_STRING xml_ns = {36, (BYTE *)"http://www.w3.org/XML/1998/namespace"};
693 static const WS_XML_STRING xmlns_ns = {29, (BYTE *)"http://www.w3.org/2000/xmlns/"};
694 struct reader *reader = (struct reader *)handle;
695 BOOL found = FALSE;
696 HRESULT hr = S_OK;
698 TRACE( "%p %s %d %p %p\n", handle, debugstr_xmlstr(prefix), required, ns, error );
699 if (error) FIXME( "ignoring error parameter\n" );
701 if (!reader || !prefix || !ns) return E_INVALIDARG;
703 EnterCriticalSection( &reader->cs );
705 if (reader->magic != READER_MAGIC)
707 LeaveCriticalSection( &reader->cs );
708 return E_INVALIDARG;
711 if (reader->state != READER_STATE_STARTELEMENT) hr = WS_E_INVALID_OPERATION;
712 else if (!prefix->length)
714 *ns = &empty_ns;
715 found = TRUE;
717 else if (WsXmlStringEquals( prefix, &xml, NULL ) == S_OK)
719 *ns = &xml_ns;
720 found = TRUE;
722 else if (WsXmlStringEquals( prefix, &xmlns, NULL ) == S_OK)
724 *ns = &xmlns_ns;
725 found = TRUE;
727 else
729 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
730 ULONG i;
732 for (i = 0; i < elem->attributeCount; i++)
734 if (!elem->attributes[i]->isXmlNs) continue;
735 if (WsXmlStringEquals( prefix, elem->attributes[i]->prefix, NULL ) == S_OK)
737 *ns = elem->attributes[i]->ns;
738 found = TRUE;
739 break;
744 LeaveCriticalSection( &reader->cs );
746 if (hr == S_OK && !found)
748 if (required) hr = WS_E_INVALID_FORMAT;
749 else
751 *ns = NULL;
752 hr = S_FALSE;
756 TRACE( "returning %08x\n", hr );
757 return hr;
760 /**************************************************************************
761 * WsGetReaderNode [webservices.@]
763 HRESULT WINAPI WsGetReaderNode( WS_XML_READER *handle, const WS_XML_NODE **node,
764 WS_ERROR *error )
766 struct reader *reader = (struct reader *)handle;
767 HRESULT hr = S_OK;
769 TRACE( "%p %p %p\n", handle, node, error );
770 if (error) FIXME( "ignoring error parameter\n" );
772 if (!reader || !node) return E_INVALIDARG;
774 EnterCriticalSection( &reader->cs );
776 if (reader->magic != READER_MAGIC)
778 LeaveCriticalSection( &reader->cs );
779 return E_INVALIDARG;
782 *node = &reader->current->hdr.node;
784 LeaveCriticalSection( &reader->cs );
785 TRACE( "returning %08x\n", hr );
786 return S_OK;
789 static HRESULT get_charset( struct reader *reader, void *buf, ULONG size )
791 if (!buf || size != sizeof(reader->input_charset)) return E_INVALIDARG;
792 if (!reader->input_charset) return WS_E_INVALID_FORMAT;
793 *(WS_CHARSET *)buf = reader->input_charset;
794 return S_OK;
797 /**************************************************************************
798 * WsGetReaderProperty [webservices.@]
800 HRESULT WINAPI WsGetReaderProperty( WS_XML_READER *handle, WS_XML_READER_PROPERTY_ID id,
801 void *buf, ULONG size, WS_ERROR *error )
803 struct reader *reader = (struct reader *)handle;
804 HRESULT hr;
806 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
807 if (error) FIXME( "ignoring error parameter\n" );
809 if (!reader) return E_INVALIDARG;
811 EnterCriticalSection( &reader->cs );
813 if (reader->magic != READER_MAGIC)
815 LeaveCriticalSection( &reader->cs );
816 return E_INVALIDARG;
819 if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
820 else if (id == WS_XML_READER_PROPERTY_CHARSET) hr = get_charset( reader, buf, size );
821 else hr = prop_get( reader->prop, reader->prop_count, id, buf, size );
823 LeaveCriticalSection( &reader->cs );
824 TRACE( "returning %08x\n", hr );
825 return hr;
828 /**************************************************************************
829 * WsGetXmlAttribute [webservices.@]
831 HRESULT WINAPI WsGetXmlAttribute( WS_XML_READER *handle, const WS_XML_STRING *attr,
832 WS_HEAP *heap, WCHAR **str, ULONG *len, WS_ERROR *error )
834 FIXME( "%p %s %p %p %p %p: stub\n", handle, debugstr_xmlstr(attr), heap, str, len, error );
835 return E_NOTIMPL;
838 WS_XML_UTF8_TEXT *alloc_utf8_text( const BYTE *data, ULONG len )
840 WS_XML_UTF8_TEXT *ret;
842 if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
843 ret->text.textType = WS_XML_TEXT_TYPE_UTF8;
844 ret->value.length = len;
845 ret->value.bytes = len ? (BYTE *)(ret + 1) : NULL;
846 ret->value.dictionary = NULL;
847 ret->value.id = 0;
848 if (data) memcpy( ret->value.bytes, data, len );
849 return ret;
852 WS_XML_UTF16_TEXT *alloc_utf16_text( const BYTE *data, ULONG len )
854 WS_XML_UTF16_TEXT *ret;
856 if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
857 ret->text.textType = WS_XML_TEXT_TYPE_UTF16;
858 ret->byteCount = len;
859 ret->bytes = len ? (BYTE *)(ret + 1) : NULL;
860 if (data) memcpy( ret->bytes, data, len );
861 return ret;
864 WS_XML_BASE64_TEXT *alloc_base64_text( const BYTE *data, ULONG len )
866 WS_XML_BASE64_TEXT *ret;
868 if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
869 ret->text.textType = WS_XML_TEXT_TYPE_BASE64;
870 ret->length = len;
871 ret->bytes = len ? (BYTE *)(ret + 1) : NULL;
872 if (data) memcpy( ret->bytes, data, len );
873 return ret;
876 WS_XML_BOOL_TEXT *alloc_bool_text( BOOL value )
878 WS_XML_BOOL_TEXT *ret;
880 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
881 ret->text.textType = WS_XML_TEXT_TYPE_BOOL;
882 ret->value = value;
883 return ret;
886 WS_XML_INT32_TEXT *alloc_int32_text( INT32 value )
888 WS_XML_INT32_TEXT *ret;
890 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
891 ret->text.textType = WS_XML_TEXT_TYPE_INT32;
892 ret->value = value;
893 return ret;
896 WS_XML_INT64_TEXT *alloc_int64_text( INT64 value )
898 WS_XML_INT64_TEXT *ret;
900 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
901 ret->text.textType = WS_XML_TEXT_TYPE_INT64;
902 ret->value = value;
903 return ret;
906 WS_XML_UINT64_TEXT *alloc_uint64_text( UINT64 value )
908 WS_XML_UINT64_TEXT *ret;
910 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
911 ret->text.textType = WS_XML_TEXT_TYPE_UINT64;
912 ret->value = value;
913 return ret;
916 WS_XML_FLOAT_TEXT *alloc_float_text( float value )
918 WS_XML_FLOAT_TEXT *ret;
920 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
921 ret->text.textType = WS_XML_TEXT_TYPE_FLOAT;
922 ret->value = value;
923 return ret;
926 WS_XML_DOUBLE_TEXT *alloc_double_text( double value )
928 WS_XML_DOUBLE_TEXT *ret;
930 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
931 ret->text.textType = WS_XML_TEXT_TYPE_DOUBLE;
932 ret->value = value;
933 return ret;
936 WS_XML_GUID_TEXT *alloc_guid_text( const GUID *value )
938 WS_XML_GUID_TEXT *ret;
940 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
941 ret->text.textType = WS_XML_TEXT_TYPE_GUID;
942 ret->value = *value;
943 return ret;
946 WS_XML_UNIQUE_ID_TEXT *alloc_unique_id_text( const GUID *value )
948 WS_XML_UNIQUE_ID_TEXT *ret;
950 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
951 ret->text.textType = WS_XML_TEXT_TYPE_UNIQUE_ID;
952 ret->value = *value;
953 return ret;
956 WS_XML_DATETIME_TEXT *alloc_datetime_text( const WS_DATETIME *value )
958 WS_XML_DATETIME_TEXT *ret;
960 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
961 ret->text.textType = WS_XML_TEXT_TYPE_DATETIME;
962 ret->value = *value;
963 return ret;
966 static inline BOOL read_end_of_data( struct reader *reader )
968 return (read_more_data( reader, 1, NULL, NULL ) != S_OK);
971 static inline const unsigned char *read_current_ptr( struct reader *reader )
973 return &reader->read_bufptr[reader->read_pos];
976 static inline void read_skip( struct reader *reader, unsigned int count )
978 assert( reader->read_pos + count <= reader->read_size );
979 reader->read_pos += count;
982 static inline HRESULT read_peek( struct reader *reader, unsigned char *bytes, unsigned int len )
984 HRESULT hr;
985 if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
986 memcpy( bytes, read_current_ptr( reader ), len );
987 return S_OK;
990 static inline HRESULT read_byte( struct reader *reader, unsigned char *byte )
992 HRESULT hr;
993 if ((hr = read_more_data( reader, 1, NULL, NULL )) != S_OK) return hr;
994 *byte = *read_current_ptr( reader );
995 read_skip( reader, 1 );
996 return S_OK;
999 static inline HRESULT read_bytes( struct reader *reader, unsigned char *bytes, unsigned int len )
1001 HRESULT hr;
1002 if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
1003 memcpy( bytes, read_current_ptr( reader ), len );
1004 read_skip( reader, len );
1005 return S_OK;
1008 /* UTF-8 support based on libs/wine/utf8.c */
1010 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
1011 static const char utf8_length[128] =
1013 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
1014 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
1015 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
1016 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
1017 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
1018 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
1019 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
1020 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
1023 /* first byte mask depending on UTF-8 sequence length */
1024 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
1026 /* minimum Unicode value depending on UTF-8 sequence length */
1027 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
1029 static inline HRESULT read_utf8_char( struct reader *reader, unsigned int *ret, unsigned int *skip )
1031 unsigned int len;
1032 unsigned char ch;
1033 const unsigned char *end;
1034 HRESULT hr;
1036 if ((hr = read_more_data( reader, 1, NULL, NULL )) != S_OK) return hr;
1037 ch = *read_current_ptr( reader );
1038 if (ch < 0x80)
1040 *ret = ch;
1041 *skip = 1;
1042 return S_OK;
1045 len = utf8_length[ch - 0x80];
1046 if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
1047 end = read_current_ptr( reader ) + len + 1;
1048 *ret = ch & utf8_mask[len];
1050 switch (len)
1052 case 3:
1053 if ((ch = end[-3] ^ 0x80) >= 0x40) break;
1054 *ret = (*ret << 6) | ch;
1055 case 2:
1056 if ((ch = end[-2] ^ 0x80) >= 0x40) break;
1057 *ret = (*ret << 6) | ch;
1058 case 1:
1059 if ((ch = end[-1] ^ 0x80) >= 0x40) break;
1060 *ret = (*ret << 6) | ch;
1061 if (*ret < utf8_minval[len]) break;
1062 *skip = len + 1;
1063 return S_OK;
1066 return WS_E_INVALID_FORMAT;
1069 static inline BOOL read_isnamechar( unsigned int ch )
1071 /* FIXME: incomplete */
1072 return (ch >= 'A' && ch <= 'Z') ||
1073 (ch >= 'a' && ch <= 'z') ||
1074 (ch >= '0' && ch <= '9') ||
1075 ch == '_' || ch == '-' || ch == '.' || ch == ':';
1078 static inline BOOL read_isspace( unsigned int ch )
1080 return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
1083 static inline void read_skip_whitespace( struct reader *reader )
1085 for (;;)
1087 if (read_more_data( reader, 1, NULL, NULL ) != S_OK || !read_isspace( *read_current_ptr( reader ) )) break;
1088 read_skip( reader, 1 );
1092 static inline HRESULT read_cmp( struct reader *reader, const char *str, int len )
1094 const unsigned char *ptr;
1095 HRESULT hr;
1097 if (len < 0) len = strlen( str );
1098 if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
1100 ptr = read_current_ptr( reader );
1101 while (len--)
1103 if (*str != *ptr) return WS_E_INVALID_FORMAT;
1104 str++; ptr++;
1106 return S_OK;
1109 static HRESULT read_xmldecl( struct reader *reader )
1111 HRESULT hr;
1113 if ((hr = read_more_data( reader, 1, NULL, NULL )) != S_OK) return hr;
1114 if (*read_current_ptr( reader ) != '<' || (hr = read_cmp( reader, "<?", 2 )) != S_OK)
1116 reader->state = READER_STATE_BOF;
1117 return S_OK;
1119 if ((hr = read_cmp( reader, "<?xml ", 6 )) != S_OK) return hr;
1120 read_skip( reader, 6 );
1122 /* FIXME: parse attributes */
1123 for (;;)
1125 if (read_more_data( reader, 1, NULL, NULL ) != S_OK || *read_current_ptr( reader ) == '?' ) break;
1126 read_skip( reader, 1 );
1129 if ((hr = read_cmp( reader, "?>", 2 )) != S_OK) return hr;
1130 read_skip( reader, 2 );
1132 reader->state = READER_STATE_BOF;
1133 return S_OK;
1136 HRESULT append_attribute( WS_XML_ELEMENT_NODE *elem, WS_XML_ATTRIBUTE *attr )
1138 if (elem->attributeCount)
1140 WS_XML_ATTRIBUTE **tmp;
1141 if (!(tmp = heap_realloc( elem->attributes, (elem->attributeCount + 1) * sizeof(attr) )))
1142 return E_OUTOFMEMORY;
1143 elem->attributes = tmp;
1145 else if (!(elem->attributes = heap_alloc( sizeof(attr) ))) return E_OUTOFMEMORY;
1146 elem->attributes[elem->attributeCount++] = attr;
1147 return S_OK;
1150 static inline void init_xml_string( BYTE *bytes, ULONG len, WS_XML_STRING *str )
1152 str->length = len;
1153 str->bytes = bytes;
1154 str->dictionary = NULL;
1155 str->id = 0;
1158 static HRESULT split_qname( const BYTE *str, ULONG len, WS_XML_STRING *prefix, WS_XML_STRING *localname )
1160 BYTE *prefix_bytes = NULL, *localname_bytes = (BYTE *)str, *ptr = (BYTE *)str;
1161 ULONG prefix_len = 0, localname_len = len;
1163 while (len--)
1165 if (*ptr == ':')
1167 if (ptr == str) return WS_E_INVALID_FORMAT;
1168 prefix_bytes = (BYTE *)str;
1169 prefix_len = ptr - str;
1170 localname_bytes = ptr + 1;
1171 localname_len = len;
1172 break;
1174 ptr++;
1176 if (!localname_len) return WS_E_INVALID_FORMAT;
1178 init_xml_string( prefix_bytes, prefix_len, prefix );
1179 init_xml_string( localname_bytes, localname_len, localname );
1180 return S_OK;
1183 static HRESULT parse_qname( const BYTE *str, ULONG len, WS_XML_STRING **prefix_ret, WS_XML_STRING **localname_ret )
1185 WS_XML_STRING prefix, localname;
1186 HRESULT hr;
1188 if ((hr = split_qname( str, len, &prefix, &localname )) != S_OK) return hr;
1189 if (!(*prefix_ret = alloc_xml_string( NULL, prefix.length ))) return E_OUTOFMEMORY;
1190 if (!(*localname_ret = dup_xml_string( &localname, FALSE )))
1192 free_xml_string( *prefix_ret );
1193 return E_OUTOFMEMORY;
1195 memcpy( (*prefix_ret)->bytes, prefix.bytes, prefix.length );
1196 if (prefix.length && add_xml_string( *prefix_ret ) != S_OK) WARN( "prefix not added to dictionary\n" );
1197 return S_OK;
1200 static int codepoint_to_utf8( int cp, unsigned char *dst )
1202 if (!cp) return -1;
1203 if (cp < 0x80)
1205 *dst = cp;
1206 return 1;
1208 if (cp < 0x800)
1210 dst[1] = 0x80 | (cp & 0x3f);
1211 cp >>= 6;
1212 dst[0] = 0xc0 | cp;
1213 return 2;
1215 if ((cp >= 0xd800 && cp <= 0xdfff) || cp == 0xfffe || cp == 0xffff) return -1;
1216 if (cp < 0x10000)
1218 dst[2] = 0x80 | (cp & 0x3f);
1219 cp >>= 6;
1220 dst[1] = 0x80 | (cp & 0x3f);
1221 cp >>= 6;
1222 dst[0] = 0xe0 | cp;
1223 return 3;
1225 if (cp >= 0x110000) return -1;
1226 dst[3] = 0x80 | (cp & 0x3f);
1227 cp >>= 6;
1228 dst[2] = 0x80 | (cp & 0x3f);
1229 cp >>= 6;
1230 dst[1] = 0x80 | (cp & 0x3f);
1231 cp >>= 6;
1232 dst[0] = 0xf0 | cp;
1233 return 4;
1236 static HRESULT decode_text( const unsigned char *str, ULONG len, unsigned char *ret, ULONG *ret_len )
1238 const unsigned char *p = str;
1239 unsigned char *q = ret;
1241 *ret_len = 0;
1242 while (len)
1244 if (*p == '&')
1246 p++; len--;
1247 if (!len) return WS_E_INVALID_FORMAT;
1249 if (len >= 3 && !memcmp( p, "lt;", 3 ))
1251 *q++ = '<';
1252 p += 3;
1253 len -= 3;
1255 else if (len >= 3 && !memcmp( p, "gt;", 3 ))
1257 *q++ = '>';
1258 p += 3;
1259 len -= 3;
1261 else if (len >= 5 && !memcmp( p, "quot;", 5 ))
1263 *q++ = '"';
1264 p += 5;
1265 len -= 5;
1267 else if (len >= 4 && !memcmp( p, "amp;", 4 ))
1269 *q++ = '&';
1270 p += 4;
1271 len -= 4;
1273 else if (len >= 5 && !memcmp( p, "apos;", 5 ))
1275 *q++ = '\'';
1276 p += 5;
1277 len -= 5;
1279 else if (*p == '#')
1281 ULONG start, nb_digits, i;
1282 int len_utf8, cp = 0;
1284 p++; len--;
1285 if (!len) return WS_E_INVALID_FORMAT;
1286 if (*p == 'x')
1288 p++; len--;
1290 start = len;
1291 while (len && isxdigit( *p )) { p++; len--; };
1292 if (!len) return WS_E_INVALID_FORMAT;
1294 p -= nb_digits = start - len;
1295 if (!nb_digits || nb_digits > 6 || p[nb_digits] != ';') return WS_E_INVALID_FORMAT;
1296 for (i = 0; i < nb_digits; i++)
1298 cp *= 16;
1299 if (*p >= '0' && *p <= '9') cp += *p - '0';
1300 else if (*p >= 'a' && *p <= 'f') cp += *p - 'a' + 10;
1301 else cp += *p - 'A' + 10;
1302 p++;
1305 else if (isdigit( *p ))
1307 while (len && *p == '0') { p++; len--; };
1308 if (!len) return WS_E_INVALID_FORMAT;
1310 start = len;
1311 while (len && isdigit( *p )) { p++; len--; };
1312 if (!len) return WS_E_INVALID_FORMAT;
1314 p -= nb_digits = start - len;
1315 if (!nb_digits || nb_digits > 7 || p[nb_digits] != ';') return WS_E_INVALID_FORMAT;
1316 for (i = 0; i < nb_digits; i++)
1318 cp *= 10;
1319 cp += *p - '0';
1320 p++;
1323 else return WS_E_INVALID_FORMAT;
1324 p++; len--;
1325 if ((len_utf8 = codepoint_to_utf8( cp, q )) < 0) return WS_E_INVALID_FORMAT;
1326 *ret_len += len_utf8;
1327 q += len_utf8;
1328 continue;
1330 else return WS_E_INVALID_FORMAT;
1332 else
1334 *q++ = *p++;
1335 len--;
1337 *ret_len += 1;
1339 return S_OK;
1342 static HRESULT read_attribute_value_text( struct reader *reader, WS_XML_ATTRIBUTE *attr )
1344 WS_XML_UTF8_TEXT *utf8;
1345 unsigned int len, ch, skip, quote;
1346 const unsigned char *start;
1347 HRESULT hr;
1349 read_skip_whitespace( reader );
1350 if ((hr = read_cmp( reader, "=", 1 )) != S_OK) return hr;
1351 read_skip( reader, 1 );
1353 read_skip_whitespace( reader );
1354 if ((hr = read_cmp( reader, "\"", 1 )) != S_OK && (hr = read_cmp( reader, "'", 1 )) != S_OK) return hr;
1355 if ((hr = read_utf8_char( reader, &quote, &skip )) != S_OK) return hr;
1356 read_skip( reader, 1 );
1358 len = 0;
1359 start = read_current_ptr( reader );
1360 for (;;)
1362 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
1363 if (ch == quote) break;
1364 read_skip( reader, skip );
1365 len += skip;
1367 read_skip( reader, 1 );
1369 if (attr->isXmlNs)
1371 if (!(attr->ns = alloc_xml_string( start, len ))) return E_OUTOFMEMORY;
1372 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) return hr;
1373 if (!(utf8 = alloc_utf8_text( NULL, 0 ))) return E_OUTOFMEMORY;
1375 else
1377 if (!(utf8 = alloc_utf8_text( NULL, len ))) return E_OUTOFMEMORY;
1378 if ((hr = decode_text( start, len, utf8->value.bytes, &utf8->value.length )) != S_OK)
1380 heap_free( utf8 );
1381 return hr;
1385 attr->value = &utf8->text;
1386 attr->singleQuote = (quote == '\'');
1387 return S_OK;
1390 static inline BOOL is_text_type( unsigned char type )
1392 return (type >= RECORD_ZERO_TEXT && type <= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT);
1395 static HRESULT read_int31( struct reader *reader, ULONG *len )
1397 unsigned char byte;
1398 HRESULT hr;
1400 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1401 *len = byte & 0x7f;
1402 if (!(byte & 0x80)) return S_OK;
1404 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1405 *len += (byte & 0x7f) << 7;
1406 if (!(byte & 0x80)) return S_OK;
1408 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1409 *len += (byte & 0x7f) << 14;
1410 if (!(byte & 0x80)) return S_OK;
1412 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1413 *len += (byte & 0x7f) << 21;
1414 if (!(byte & 0x80)) return S_OK;
1416 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1417 *len += (byte & 0x07) << 28;
1418 return S_OK;
1421 static HRESULT read_string( struct reader *reader, WS_XML_STRING **str )
1423 ULONG len;
1424 HRESULT hr;
1425 if ((hr = read_int31( reader, &len )) != S_OK) return hr;
1426 if (!(*str = alloc_xml_string( NULL, len ))) return E_OUTOFMEMORY;
1427 if ((hr = read_bytes( reader, (*str)->bytes, len )) == S_OK)
1429 if (add_xml_string( *str ) != S_OK) WARN( "string not added to dictionary\n" );
1430 return S_OK;
1432 free_xml_string( *str );
1433 return hr;
1436 static HRESULT read_dict_string( struct reader *reader, WS_XML_STRING **str )
1438 const WS_XML_DICTIONARY *dict;
1439 HRESULT hr;
1440 ULONG id;
1442 if ((hr = read_int31( reader, &id )) != S_OK) return hr;
1443 dict = (id & 1) ? reader->dict : reader->dict_static;
1444 if (!dict || (id >>= 1) >= dict->stringCount) return WS_E_INVALID_FORMAT;
1445 if (!(*str = alloc_xml_string( NULL, 0 ))) return E_OUTOFMEMORY;
1446 *(*str) = dict->strings[id];
1447 return S_OK;
1450 static HRESULT read_datetime( struct reader *reader, WS_DATETIME *ret )
1452 UINT64 val;
1453 HRESULT hr;
1455 if ((hr = read_bytes( reader, (unsigned char *)&val, sizeof(val) )) != S_OK) return hr;
1457 if ((val & 0x03) == 1) ret->format = WS_DATETIME_FORMAT_UTC;
1458 else if ((val & 0x03) == 2) ret->format = WS_DATETIME_FORMAT_LOCAL;
1459 else ret->format = WS_DATETIME_FORMAT_NONE;
1461 if ((ret->ticks = val >> 2) > TICKS_MAX) return WS_E_INVALID_FORMAT;
1462 return S_OK;
1465 static HRESULT lookup_string( struct reader *reader, ULONG id, const WS_XML_STRING **ret )
1467 const WS_XML_DICTIONARY *dict = (id & 1) ? reader->dict : reader->dict_static;
1468 if (!dict || (id >>= 1) >= dict->stringCount) return WS_E_INVALID_FORMAT;
1469 *ret = &dict->strings[id];
1470 return S_OK;
1473 static HRESULT read_attribute_value_bin( struct reader *reader, WS_XML_ATTRIBUTE *attr )
1475 WS_XML_UTF8_TEXT *text_utf8 = NULL;
1476 WS_XML_BASE64_TEXT *text_base64 = NULL;
1477 WS_XML_INT32_TEXT *text_int32;
1478 WS_XML_INT64_TEXT *text_int64;
1479 WS_XML_BOOL_TEXT *text_bool;
1480 const WS_XML_STRING *str;
1481 unsigned char type;
1482 UINT8 val_uint8;
1483 UINT16 val_uint16;
1484 INT32 val_int32;
1485 ULONG len = 0, id;
1486 GUID guid;
1487 HRESULT hr;
1489 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
1490 if (!is_text_type( type )) return WS_E_INVALID_FORMAT;
1492 switch (type)
1494 case RECORD_ZERO_TEXT:
1496 if (!(text_int32 = alloc_int32_text( 0 ))) return E_OUTOFMEMORY;
1497 attr->value = &text_int32->text;
1498 return S_OK;
1500 case RECORD_ONE_TEXT:
1502 if (!(text_int32 = alloc_int32_text( 1 ))) return E_OUTOFMEMORY;
1503 attr->value = &text_int32->text;
1504 return S_OK;
1506 case RECORD_FALSE_TEXT:
1508 if (!(text_bool = alloc_bool_text( FALSE ))) return E_OUTOFMEMORY;
1509 attr->value = &text_bool->text;
1510 return S_OK;
1512 case RECORD_TRUE_TEXT:
1514 if (!(text_bool = alloc_bool_text( TRUE ))) return E_OUTOFMEMORY;
1515 attr->value = &text_bool->text;
1516 return S_OK;
1518 case RECORD_INT8_TEXT:
1520 INT8 val_int8;
1521 if ((hr = read_byte( reader, (unsigned char *)&val_int8 )) != S_OK) return hr;
1522 if (!(text_int64 = alloc_int64_text( val_int8 ))) return E_OUTOFMEMORY;
1523 attr->value = &text_int64->text;
1524 return S_OK;
1526 case RECORD_INT16_TEXT:
1528 INT16 val_int16;
1529 if ((hr = read_bytes( reader, (unsigned char *)&val_int16, sizeof(val_int16) )) != S_OK) return hr;
1530 if (!(text_int64 = alloc_int64_text( val_int16 ))) return E_OUTOFMEMORY;
1531 attr->value = &text_int64->text;
1532 return S_OK;
1534 case RECORD_INT32_TEXT:
1535 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1536 if (!(text_int64 = alloc_int64_text( val_int32 ))) return E_OUTOFMEMORY;
1537 attr->value = &text_int64->text;
1538 return S_OK;
1540 case RECORD_INT64_TEXT:
1542 INT64 val_int64;
1543 if ((hr = read_bytes( reader, (unsigned char *)&val_int64, sizeof(val_int64) )) != S_OK) return hr;
1544 if (!(text_int64 = alloc_int64_text( val_int64 ))) return E_OUTOFMEMORY;
1545 attr->value = &text_int64->text;
1546 return S_OK;
1548 case RECORD_FLOAT_TEXT:
1550 WS_XML_FLOAT_TEXT *text_float;
1551 float val_float;
1553 if ((hr = read_bytes( reader, (unsigned char *)&val_float, sizeof(val_float) )) != S_OK) return hr;
1554 if (!(text_float = alloc_float_text( val_float ))) return E_OUTOFMEMORY;
1555 attr->value = &text_float->text;
1556 return S_OK;
1558 case RECORD_DOUBLE_TEXT:
1560 WS_XML_DOUBLE_TEXT *text_double;
1561 double val_double;
1563 if ((hr = read_bytes( reader, (unsigned char *)&val_double, sizeof(val_double) )) != S_OK) return hr;
1564 if (!(text_double = alloc_double_text( val_double ))) return E_OUTOFMEMORY;
1565 attr->value = &text_double->text;
1566 return S_OK;
1568 case RECORD_DATETIME_TEXT:
1570 WS_XML_DATETIME_TEXT *text_datetime;
1571 WS_DATETIME datetime;
1573 if ((hr = read_datetime( reader, &datetime )) != S_OK) return hr;
1574 if (!(text_datetime = alloc_datetime_text( &datetime ))) return E_OUTOFMEMORY;
1575 attr->value = &text_datetime->text;
1576 return S_OK;
1578 case RECORD_CHARS8_TEXT:
1579 if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
1580 len = val_uint8;
1581 break;
1583 case RECORD_CHARS16_TEXT:
1584 if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
1585 len = val_uint16;
1586 break;
1588 case RECORD_CHARS32_TEXT:
1589 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1590 if (val_int32 < 0) return WS_E_INVALID_FORMAT;
1591 len = val_int32;
1592 break;
1594 case RECORD_BYTES8_TEXT:
1595 if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
1596 if (!(text_base64 = alloc_base64_text( NULL, val_uint8 ))) return E_OUTOFMEMORY;
1597 if ((hr = read_bytes( reader, text_base64->bytes, val_uint8 )) != S_OK)
1599 heap_free( text_base64 );
1600 return hr;
1602 break;
1604 case RECORD_BYTES16_TEXT:
1605 if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
1606 if (!(text_base64 = alloc_base64_text( NULL, val_uint16 ))) return E_OUTOFMEMORY;
1607 if ((hr = read_bytes( reader, text_base64->bytes, val_uint16 )) != S_OK)
1609 heap_free( text_base64 );
1610 return hr;
1612 break;
1614 case RECORD_BYTES32_TEXT:
1615 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1616 if (val_int32 < 0) return WS_E_INVALID_FORMAT;
1617 if (!(text_base64 = alloc_base64_text( NULL, val_int32 ))) return E_OUTOFMEMORY;
1618 if ((hr = read_bytes( reader, text_base64->bytes, val_int32 )) != S_OK)
1620 heap_free( text_base64 );
1621 return hr;
1623 break;
1625 case RECORD_EMPTY_TEXT:
1626 break;
1628 case RECORD_DICTIONARY_TEXT:
1629 if ((hr = read_int31( reader, &id )) != S_OK) return hr;
1630 if ((hr = lookup_string( reader, id, &str )) != S_OK) return hr;
1631 if (!(text_utf8 = alloc_utf8_text( str->bytes, str->length ))) return E_OUTOFMEMORY;
1632 break;
1634 case RECORD_UNIQUE_ID_TEXT:
1636 WS_XML_UNIQUE_ID_TEXT *text_unique_id;
1637 if ((hr = read_bytes( reader, (unsigned char *)&guid, sizeof(guid) )) != S_OK) return hr;
1638 if (!(text_unique_id = alloc_unique_id_text( &guid ))) return E_OUTOFMEMORY;
1639 attr->value = &text_unique_id->text;
1640 return S_OK;
1642 case RECORD_GUID_TEXT:
1644 WS_XML_GUID_TEXT *guid_text;
1645 if ((hr = read_bytes( reader, (unsigned char *)&guid, sizeof(guid) )) != S_OK) return hr;
1646 if (!(guid_text = alloc_guid_text( &guid ))) return E_OUTOFMEMORY;
1647 attr->value = &guid_text->text;
1648 return S_OK;
1650 case RECORD_UINT64_TEXT:
1652 WS_XML_UINT64_TEXT *text_uint64;
1653 UINT64 val_uint64;
1655 if ((hr = read_bytes( reader, (unsigned char *)&val_uint64, sizeof(val_uint64) )) != S_OK) return hr;
1656 if (!(text_uint64 = alloc_uint64_text( val_uint64 ))) return E_OUTOFMEMORY;
1657 attr->value = &text_uint64->text;
1658 return S_OK;
1660 case RECORD_BOOL_TEXT:
1662 WS_XML_BOOL_TEXT *text_bool;
1663 BOOL val_bool;
1665 if ((hr = read_bytes( reader, (unsigned char *)&val_bool, sizeof(val_bool) )) != S_OK) return hr;
1666 if (!(text_bool = alloc_bool_text( !!val_bool ))) return E_OUTOFMEMORY;
1667 attr->value = &text_bool->text;
1668 return S_OK;
1670 default:
1671 ERR( "unhandled record type %02x\n", type );
1672 return WS_E_NOT_SUPPORTED;
1675 if (type >= RECORD_BYTES8_TEXT && type <= RECORD_BYTES32_TEXT)
1677 attr->value = &text_base64->text;
1678 return S_OK;
1681 if (!text_utf8)
1683 if (!(text_utf8 = alloc_utf8_text( NULL, len ))) return E_OUTOFMEMORY;
1684 if (!len) text_utf8->value.bytes = (BYTE *)(text_utf8 + 1); /* quirk */
1685 if ((hr = read_bytes( reader, text_utf8->value.bytes, len )) != S_OK)
1687 heap_free( text_utf8 );
1688 return hr;
1692 attr->value = &text_utf8->text;
1693 return S_OK;
1696 static HRESULT read_attribute_text( struct reader *reader, WS_XML_ATTRIBUTE **ret )
1698 static const WS_XML_STRING xmlns = {5, (BYTE *)"xmlns"};
1699 WS_XML_ATTRIBUTE *attr;
1700 unsigned int len = 0, ch, skip;
1701 const unsigned char *start;
1702 WS_XML_STRING *prefix, *localname;
1703 HRESULT hr;
1705 if (!(attr = heap_alloc_zero( sizeof(*attr) ))) return E_OUTOFMEMORY;
1707 start = read_current_ptr( reader );
1708 for (;;)
1710 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) goto error;
1711 if (!read_isnamechar( ch )) break;
1712 read_skip( reader, skip );
1713 len += skip;
1715 if (!len)
1717 hr = WS_E_INVALID_FORMAT;
1718 goto error;
1721 if ((hr = parse_qname( start, len, &prefix, &localname )) != S_OK) goto error;
1722 if (WsXmlStringEquals( prefix, &xmlns, NULL ) == S_OK)
1724 free_xml_string( prefix );
1725 attr->isXmlNs = 1;
1726 if (!(attr->prefix = alloc_xml_string( localname->bytes, localname->length )))
1728 free_xml_string( localname );
1729 hr = E_OUTOFMEMORY;
1730 goto error;
1732 attr->localName = localname;
1734 else if (!prefix->length && WsXmlStringEquals( localname, &xmlns, NULL ) == S_OK)
1736 attr->isXmlNs = 1;
1737 attr->prefix = prefix;
1738 attr->localName = localname;
1740 else
1742 attr->prefix = prefix;
1743 attr->localName = localname;
1746 if ((hr = read_attribute_value_text( reader, attr )) != S_OK) goto error;
1748 *ret = attr;
1749 return S_OK;
1751 error:
1752 free_attribute( attr );
1753 return hr;
1756 static inline BOOL is_attribute_type( unsigned char type )
1758 return (type >= RECORD_SHORT_ATTRIBUTE && type <= RECORD_PREFIX_ATTRIBUTE_Z);
1761 static WS_XML_STRING *get_xmlns_localname( struct reader *reader, const WS_XML_STRING *prefix )
1763 if (!get_namespace( reader, prefix )) return alloc_xml_string( NULL, 0 );
1764 return alloc_xml_string( prefix->bytes, prefix->length );
1767 static HRESULT read_attribute_bin( struct reader *reader, WS_XML_ATTRIBUTE **ret )
1769 WS_XML_UTF8_TEXT *utf8;
1770 WS_XML_ATTRIBUTE *attr;
1771 unsigned char type = 0;
1772 HRESULT hr;
1774 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
1775 if (!is_attribute_type( type )) return WS_E_INVALID_FORMAT;
1776 if (!(attr = heap_alloc_zero( sizeof(*attr) ))) return E_OUTOFMEMORY;
1778 if (type >= RECORD_PREFIX_ATTRIBUTE_A && type <= RECORD_PREFIX_ATTRIBUTE_Z)
1780 unsigned char ch = type - RECORD_PREFIX_ATTRIBUTE_A + 'a';
1781 if (!(attr->prefix = alloc_xml_string( &ch, 1 )))
1783 hr = E_OUTOFMEMORY;
1784 goto error;
1786 if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
1787 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1789 else if (type >= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A && type <= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_Z)
1791 unsigned char ch = type - RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A + 'a';
1792 if (!(attr->prefix = alloc_xml_string( &ch, 1 )))
1794 hr = E_OUTOFMEMORY;
1795 goto error;
1797 if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
1798 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1800 else
1802 switch (type)
1804 case RECORD_SHORT_ATTRIBUTE:
1805 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1807 hr = E_OUTOFMEMORY;
1808 goto error;
1810 if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
1811 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1812 break;
1814 case RECORD_ATTRIBUTE:
1815 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1816 if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
1817 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1818 break;
1820 case RECORD_SHORT_DICTIONARY_ATTRIBUTE:
1821 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1823 hr = E_OUTOFMEMORY;
1824 goto error;
1826 if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
1827 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1828 break;
1830 case RECORD_DICTIONARY_ATTRIBUTE:
1831 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1832 if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
1833 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1834 break;
1836 case RECORD_SHORT_XMLNS_ATTRIBUTE:
1837 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1839 hr = E_OUTOFMEMORY;
1840 goto error;
1842 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1844 hr = E_OUTOFMEMORY;
1845 goto error;
1847 if ((hr = read_string( reader, &attr->ns )) != S_OK) goto error;
1848 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1849 attr->isXmlNs = 1;
1850 break;
1852 case RECORD_XMLNS_ATTRIBUTE:
1853 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1854 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1856 hr = E_OUTOFMEMORY;
1857 goto error;
1859 if ((hr = read_string( reader, &attr->ns )) != S_OK) goto error;
1860 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1861 attr->isXmlNs = 1;
1862 break;
1864 case RECORD_SHORT_DICTIONARY_XMLNS_ATTRIBUTE:
1865 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1867 hr = E_OUTOFMEMORY;
1868 goto error;
1870 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1872 hr = E_OUTOFMEMORY;
1873 goto error;
1875 if ((hr = read_dict_string( reader, &attr->ns )) != S_OK) goto error;
1876 if (!(utf8 = alloc_utf8_text( NULL, 0 )))
1878 hr = E_OUTOFMEMORY;
1879 goto error;
1881 attr->value = &utf8->text;
1882 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1883 attr->isXmlNs = 1;
1884 break;
1886 case RECORD_DICTIONARY_XMLNS_ATTRIBUTE:
1887 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1888 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1890 hr = E_OUTOFMEMORY;
1891 goto error;
1893 if ((hr = read_dict_string( reader, &attr->ns )) != S_OK) goto error;
1894 if (!(utf8 = alloc_utf8_text( NULL, 0 )))
1896 hr = E_OUTOFMEMORY;
1897 goto error;
1899 attr->value = &utf8->text;
1900 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1901 attr->isXmlNs = 1;
1902 break;
1904 default:
1905 ERR( "unhandled record type %02x\n", type );
1906 return WS_E_NOT_SUPPORTED;
1910 *ret = attr;
1911 return S_OK;
1913 error:
1914 free_attribute( attr );
1915 return hr;
1918 static inline struct node *find_parent( struct reader *reader )
1920 if (node_type( reader->current ) == WS_XML_NODE_TYPE_END_ELEMENT)
1922 if (is_valid_parent( reader->current->parent->parent )) return reader->current->parent->parent;
1923 return NULL;
1925 if (is_valid_parent( reader->current )) return reader->current;
1926 if (is_valid_parent( reader->current->parent )) return reader->current->parent;
1927 return NULL;
1930 static HRESULT set_namespaces( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
1932 static const WS_XML_STRING xml = {3, (BYTE *)"xml"};
1933 const WS_XML_STRING *ns;
1934 ULONG i;
1936 if (!(ns = get_namespace( reader, elem->prefix ))) return WS_E_INVALID_FORMAT;
1937 if (!(elem->ns = dup_xml_string( ns, FALSE ))) return E_OUTOFMEMORY;
1939 for (i = 0; i < elem->attributeCount; i++)
1941 WS_XML_ATTRIBUTE *attr = elem->attributes[i];
1942 if (attr->isXmlNs || WsXmlStringEquals( attr->prefix, &xml, NULL ) == S_OK) continue;
1943 if (!(ns = get_namespace( reader, attr->prefix ))) return WS_E_INVALID_FORMAT;
1944 if (!(attr->ns = alloc_xml_string( NULL, ns->length ))) return E_OUTOFMEMORY;
1945 if (attr->ns->length) memcpy( attr->ns->bytes, ns->bytes, ns->length );
1947 return S_OK;
1950 static WS_XML_ELEMENT_NODE *alloc_element_pair(void)
1952 struct node *node, *end;
1953 if (!(node = alloc_node( WS_XML_NODE_TYPE_ELEMENT ))) return NULL;
1954 if (!(end = alloc_node( WS_XML_NODE_TYPE_END_ELEMENT )))
1956 free_node( node );
1957 return NULL;
1959 list_add_tail( &node->children, &end->entry );
1960 end->parent = node;
1961 return &node->hdr;
1964 static HRESULT read_attributes_text( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
1966 WS_XML_ATTRIBUTE *attr;
1967 HRESULT hr;
1969 reader->current_attr = 0;
1970 for (;;)
1972 read_skip_whitespace( reader );
1973 if (read_cmp( reader, ">", 1 ) == S_OK || read_cmp( reader, "/>", 2 ) == S_OK) break;
1974 if ((hr = read_attribute_text( reader, &attr )) != S_OK) return hr;
1975 if ((hr = append_attribute( elem, attr )) != S_OK)
1977 free_attribute( attr );
1978 return hr;
1980 reader->current_attr++;
1982 return S_OK;
1985 static HRESULT read_element_text( struct reader *reader )
1987 unsigned int len = 0, ch, skip;
1988 const unsigned char *start;
1989 unsigned char buf[2];
1990 struct node *node = NULL, *parent;
1991 WS_XML_ELEMENT_NODE *elem;
1992 HRESULT hr;
1994 if (read_end_of_data( reader ))
1996 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
1997 reader->last = reader->current;
1998 reader->state = READER_STATE_EOF;
1999 return S_OK;
2002 if ((hr = read_peek( reader, buf, 2 )) != S_OK) return hr;
2003 if (buf[0] != '<' || !read_isnamechar( buf[1] )) return WS_E_INVALID_FORMAT;
2004 read_skip( reader, 1 );
2006 if (!(elem = alloc_element_pair())) return E_OUTOFMEMORY;
2007 node = (struct node *)elem;
2009 start = read_current_ptr( reader );
2010 for (;;)
2012 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) goto error;
2013 if (!read_isnamechar( ch )) break;
2014 read_skip( reader, skip );
2015 len += skip;
2017 if (!len)
2019 hr = WS_E_INVALID_FORMAT;
2020 goto error;
2023 if (!(parent = find_parent( reader ))) goto error;
2024 if ((hr = parse_qname( start, len, &elem->prefix, &elem->localName )) != S_OK) goto error;
2025 if ((hr = read_attributes_text( reader, elem )) != S_OK) goto error;
2026 if ((hr = set_namespaces( reader, elem )) != S_OK) goto error;
2028 read_insert_node( reader, parent, node );
2029 reader->state = READER_STATE_STARTELEMENT;
2030 return S_OK;
2032 error:
2033 destroy_nodes( node );
2034 return hr;
2037 static inline BOOL is_element_type( unsigned char type )
2039 return (type >= RECORD_SHORT_ELEMENT && type <= RECORD_PREFIX_ELEMENT_Z);
2042 static HRESULT read_attributes_bin( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
2044 WS_XML_ATTRIBUTE *attr;
2045 unsigned char type;
2046 HRESULT hr;
2048 reader->current_attr = 0;
2049 for (;;)
2051 if ((hr = read_peek( reader, &type, 1 )) != S_OK) return hr;
2052 if (!is_attribute_type( type )) break;
2053 if ((hr = read_attribute_bin( reader, &attr )) != S_OK) return hr;
2054 if ((hr = append_attribute( elem, attr )) != S_OK)
2056 free_attribute( attr );
2057 return hr;
2059 reader->current_attr++;
2061 return S_OK;
2064 static HRESULT read_element_bin( struct reader *reader )
2066 struct node *node = NULL, *parent;
2067 WS_XML_ELEMENT_NODE *elem;
2068 unsigned char type;
2069 HRESULT hr;
2071 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2072 if (!is_element_type( type )) return WS_E_INVALID_FORMAT;
2074 if (!(elem = alloc_element_pair())) return E_OUTOFMEMORY;
2075 node = (struct node *)elem;
2077 if (type >= RECORD_PREFIX_ELEMENT_A && type <= RECORD_PREFIX_ELEMENT_Z)
2079 unsigned char ch = type - RECORD_PREFIX_ELEMENT_A + 'a';
2080 if (!(elem->prefix = alloc_xml_string( &ch, 1 )))
2082 hr = E_OUTOFMEMORY;
2083 goto error;
2085 if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2087 else if (type >= RECORD_PREFIX_DICTIONARY_ELEMENT_A && type <= RECORD_PREFIX_DICTIONARY_ELEMENT_Z)
2089 unsigned char ch = type - RECORD_PREFIX_DICTIONARY_ELEMENT_A + 'a';
2090 if (!(elem->prefix = alloc_xml_string( &ch, 1 )))
2092 hr = E_OUTOFMEMORY;
2093 goto error;
2095 if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
2097 else
2099 switch (type)
2101 case RECORD_SHORT_ELEMENT:
2102 if (!(elem->prefix = alloc_xml_string( NULL, 0 )))
2104 hr = E_OUTOFMEMORY;
2105 goto error;
2107 if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2108 break;
2110 case RECORD_ELEMENT:
2111 if ((hr = read_string( reader, &elem->prefix )) != S_OK) goto error;
2112 if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2113 break;
2115 case RECORD_SHORT_DICTIONARY_ELEMENT:
2116 if (!(elem->prefix = alloc_xml_string( NULL, 0 )))
2118 hr = E_OUTOFMEMORY;
2119 goto error;
2121 if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
2122 break;
2124 case RECORD_DICTIONARY_ELEMENT:
2125 if ((hr = read_string( reader, &elem->prefix )) != S_OK) goto error;
2126 if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
2127 break;
2129 default:
2130 ERR( "unhandled record type %02x\n", type );
2131 return WS_E_NOT_SUPPORTED;
2135 if (!(parent = find_parent( reader )))
2137 hr = WS_E_INVALID_FORMAT;
2138 goto error;
2141 if ((hr = read_attributes_bin( reader, elem )) != S_OK) goto error;
2142 if ((hr = set_namespaces( reader, elem )) != S_OK) goto error;
2144 read_insert_node( reader, parent, node );
2145 reader->state = READER_STATE_STARTELEMENT;
2146 return S_OK;
2148 error:
2149 destroy_nodes( node );
2150 return hr;
2153 static HRESULT read_text_text( struct reader *reader )
2155 unsigned int len = 0, ch, skip;
2156 const unsigned char *start;
2157 struct node *node, *parent;
2158 WS_XML_TEXT_NODE *text;
2159 WS_XML_UTF8_TEXT *utf8;
2160 HRESULT hr;
2162 start = read_current_ptr( reader );
2163 for (;;)
2165 if (read_end_of_data( reader )) break;
2166 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2167 if (ch == '<') break;
2168 read_skip( reader, skip );
2169 len += skip;
2172 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2174 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return E_OUTOFMEMORY;
2175 text = (WS_XML_TEXT_NODE *)node;
2176 if (!(utf8 = alloc_utf8_text( NULL, len )))
2178 heap_free( node );
2179 return E_OUTOFMEMORY;
2181 if ((hr = decode_text( start, len, utf8->value.bytes, &utf8->value.length )) != S_OK)
2183 heap_free( utf8 );
2184 heap_free( node );
2185 return hr;
2187 text->text = &utf8->text;
2189 read_insert_node( reader, parent, node );
2190 reader->state = READER_STATE_TEXT;
2191 reader->text_conv_offset = 0;
2192 return S_OK;
2195 static struct node *alloc_utf8_text_node( const BYTE *data, ULONG len, WS_XML_UTF8_TEXT **ret )
2197 struct node *node;
2198 WS_XML_UTF8_TEXT *utf8;
2199 WS_XML_TEXT_NODE *text;
2201 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2202 if (!(utf8 = alloc_utf8_text( data, len )))
2204 heap_free( node );
2205 return NULL;
2207 text = (WS_XML_TEXT_NODE *)node;
2208 text->text = &utf8->text;
2209 if (ret) *ret = utf8;
2210 return node;
2213 static struct node *alloc_base64_text_node( const BYTE *data, ULONG len, WS_XML_BASE64_TEXT **ret )
2215 struct node *node;
2216 WS_XML_BASE64_TEXT *base64;
2217 WS_XML_TEXT_NODE *text;
2219 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2220 if (!(base64 = alloc_base64_text( data, len )))
2222 heap_free( node );
2223 return NULL;
2225 text = (WS_XML_TEXT_NODE *)node;
2226 text->text = &base64->text;
2227 if (ret) *ret = base64;
2228 return node;
2231 static struct node *alloc_bool_text_node( BOOL value )
2233 struct node *node;
2234 WS_XML_BOOL_TEXT *text_bool;
2235 WS_XML_TEXT_NODE *text;
2237 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2238 if (!(text_bool = alloc_bool_text( value )))
2240 heap_free( node );
2241 return NULL;
2243 text = (WS_XML_TEXT_NODE *)node;
2244 text->text = &text_bool->text;
2245 return node;
2248 static struct node *alloc_int32_text_node( INT32 value )
2250 struct node *node;
2251 WS_XML_INT32_TEXT *text_int32;
2252 WS_XML_TEXT_NODE *text;
2254 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2255 if (!(text_int32 = alloc_int32_text( value )))
2257 heap_free( node );
2258 return NULL;
2260 text = (WS_XML_TEXT_NODE *)node;
2261 text->text = &text_int32->text;
2262 return node;
2265 static struct node *alloc_int64_text_node( INT64 value )
2267 struct node *node;
2268 WS_XML_INT64_TEXT *text_int64;
2269 WS_XML_TEXT_NODE *text;
2271 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2272 if (!(text_int64 = alloc_int64_text( value )))
2274 heap_free( node );
2275 return NULL;
2277 text = (WS_XML_TEXT_NODE *)node;
2278 text->text = &text_int64->text;
2279 return node;
2282 static struct node *alloc_float_text_node( float value )
2284 struct node *node;
2285 WS_XML_FLOAT_TEXT *text_float;
2286 WS_XML_TEXT_NODE *text;
2288 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2289 if (!(text_float = alloc_float_text( value )))
2291 heap_free( node );
2292 return NULL;
2294 text = (WS_XML_TEXT_NODE *)node;
2295 text->text = &text_float->text;
2296 return node;
2299 static struct node *alloc_double_text_node( double value )
2301 struct node *node;
2302 WS_XML_DOUBLE_TEXT *text_double;
2303 WS_XML_TEXT_NODE *text;
2305 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2306 if (!(text_double = alloc_double_text( value )))
2308 heap_free( node );
2309 return NULL;
2311 text = (WS_XML_TEXT_NODE *)node;
2312 text->text = &text_double->text;
2313 return node;
2316 static struct node *alloc_datetime_text_node( const WS_DATETIME *value )
2318 struct node *node;
2319 WS_XML_DATETIME_TEXT *text_datetime;
2320 WS_XML_TEXT_NODE *text;
2322 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2323 if (!(text_datetime = alloc_datetime_text( value )))
2325 heap_free( node );
2326 return NULL;
2328 text = (WS_XML_TEXT_NODE *)node;
2329 text->text = &text_datetime->text;
2330 return node;
2333 static struct node *alloc_unique_id_text_node( const GUID *value )
2335 struct node *node;
2336 WS_XML_UNIQUE_ID_TEXT *text_unique_id;
2337 WS_XML_TEXT_NODE *text;
2339 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2340 if (!(text_unique_id = alloc_unique_id_text( value )))
2342 heap_free( node );
2343 return NULL;
2345 text = (WS_XML_TEXT_NODE *)node;
2346 text->text = &text_unique_id->text;
2347 return node;
2350 static struct node *alloc_guid_text_node( const GUID *value )
2352 struct node *node;
2353 WS_XML_GUID_TEXT *text_guid;
2354 WS_XML_TEXT_NODE *text;
2356 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2357 if (!(text_guid = alloc_guid_text( value )))
2359 heap_free( node );
2360 return NULL;
2362 text = (WS_XML_TEXT_NODE *)node;
2363 text->text = &text_guid->text;
2364 return node;
2367 static struct node *alloc_uint64_text_node( UINT64 value )
2369 struct node *node;
2370 WS_XML_UINT64_TEXT *text_uint64;
2371 WS_XML_TEXT_NODE *text;
2373 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2374 if (!(text_uint64 = alloc_uint64_text( value )))
2376 heap_free( node );
2377 return NULL;
2379 text = (WS_XML_TEXT_NODE *)node;
2380 text->text = &text_uint64->text;
2381 return node;
2384 static HRESULT append_text_bytes( struct reader *reader, WS_XML_TEXT_NODE *node, ULONG len )
2386 WS_XML_BASE64_TEXT *new, *old = (WS_XML_BASE64_TEXT *)node->text;
2387 HRESULT hr;
2389 if (!(new = alloc_base64_text( NULL, old->length + len ))) return E_OUTOFMEMORY;
2390 memcpy( new->bytes, old->bytes, old->length );
2391 if ((hr = read_bytes( reader, new->bytes + old->length, len )) != S_OK) return hr;
2392 heap_free( old );
2393 node->text = &new->text;
2394 return S_OK;
2397 static HRESULT read_text_bytes( struct reader *reader, unsigned char type )
2399 struct node *node = NULL, *parent;
2400 WS_XML_BASE64_TEXT *base64;
2401 HRESULT hr;
2402 ULONG len;
2404 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2405 for (;;)
2407 switch (type)
2409 case RECORD_BYTES8_TEXT:
2410 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT:
2412 UINT8 len_uint8;
2413 if ((hr = read_byte( reader, (unsigned char *)&len_uint8 )) != S_OK) goto error;
2414 len = len_uint8;
2415 break;
2417 case RECORD_BYTES16_TEXT:
2418 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT:
2420 UINT16 len_uint16;
2421 if ((hr = read_bytes( reader, (unsigned char *)&len_uint16, sizeof(len_uint16) )) != S_OK) goto error;
2422 len = len_uint16;
2423 break;
2425 case RECORD_BYTES32_TEXT:
2426 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT:
2428 INT32 len_int32;
2429 if ((hr = read_bytes( reader, (unsigned char *)&len_int32, sizeof(len_int32) )) != S_OK) goto error;
2430 if (len_int32 < 0)
2432 hr = WS_E_INVALID_FORMAT;
2433 goto error;
2435 len = len_int32;
2436 break;
2438 default:
2439 ERR( "unexpected type %u\n", type );
2440 hr = E_INVALIDARG;
2441 goto error;
2444 if (!node)
2446 if (!(node = alloc_base64_text_node( NULL, len, &base64 ))) return E_OUTOFMEMORY;
2447 if ((hr = read_bytes( reader, base64->bytes, len )) != S_OK) goto error;
2449 else if ((hr = append_text_bytes( reader, (WS_XML_TEXT_NODE *)node, len )) != S_OK) goto error;
2451 if (type & 1)
2453 node->flags |= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT;
2454 break;
2456 if ((hr = read_peek( reader, &type, 1 )) != S_OK) goto error;
2457 if (type < RECORD_BYTES8_TEXT || type > RECORD_BYTES32_TEXT_WITH_ENDELEMENT) break;
2458 read_skip( reader, 1 );
2461 read_insert_node( reader, parent, node );
2462 reader->state = READER_STATE_TEXT;
2463 reader->text_conv_offset = 0;
2464 return S_OK;
2466 error:
2467 free_node( node );
2468 return hr;
2471 static HRESULT read_text_bin( struct reader *reader )
2473 struct node *node = NULL, *parent;
2474 unsigned char type;
2475 WS_XML_UTF8_TEXT *utf8;
2476 INT32 val_int32;
2477 UINT8 val_uint8;
2478 UINT16 val_uint16;
2479 ULONG len, id;
2480 GUID uuid;
2481 HRESULT hr;
2483 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2484 if (!is_text_type( type ) || !(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2486 switch (type)
2488 case RECORD_ZERO_TEXT:
2489 case RECORD_ZERO_TEXT_WITH_ENDELEMENT:
2490 if (!(node = alloc_int32_text_node( 0 ))) return E_OUTOFMEMORY;
2491 break;
2493 case RECORD_ONE_TEXT:
2494 case RECORD_ONE_TEXT_WITH_ENDELEMENT:
2495 if (!(node = alloc_int32_text_node( 1 ))) return E_OUTOFMEMORY;
2496 break;
2498 case RECORD_FALSE_TEXT:
2499 case RECORD_FALSE_TEXT_WITH_ENDELEMENT:
2500 if (!(node = alloc_bool_text_node( FALSE ))) return E_OUTOFMEMORY;
2501 break;
2503 case RECORD_TRUE_TEXT:
2504 case RECORD_TRUE_TEXT_WITH_ENDELEMENT:
2505 if (!(node = alloc_bool_text_node( TRUE ))) return E_OUTOFMEMORY;
2506 break;
2508 case RECORD_INT8_TEXT:
2509 case RECORD_INT8_TEXT_WITH_ENDELEMENT:
2511 INT8 val_int8;
2512 if ((hr = read_byte( reader, (unsigned char *)&val_int8 )) != S_OK) return hr;
2513 if (!(node = alloc_int32_text_node( val_int8 ))) return E_OUTOFMEMORY;
2514 break;
2516 case RECORD_INT16_TEXT:
2517 case RECORD_INT16_TEXT_WITH_ENDELEMENT:
2519 INT16 val_int16;
2520 if ((hr = read_bytes( reader, (unsigned char *)&val_int16, sizeof(val_int16) )) != S_OK) return hr;
2521 if (!(node = alloc_int32_text_node( val_int16 ))) return E_OUTOFMEMORY;
2522 break;
2524 case RECORD_INT32_TEXT:
2525 case RECORD_INT32_TEXT_WITH_ENDELEMENT:
2526 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
2527 if (!(node = alloc_int32_text_node( val_int32 ))) return E_OUTOFMEMORY;
2528 break;
2530 case RECORD_INT64_TEXT:
2531 case RECORD_INT64_TEXT_WITH_ENDELEMENT:
2533 INT64 val_int64;
2534 if ((hr = read_bytes( reader, (unsigned char *)&val_int64, sizeof(val_int64) )) != S_OK) return hr;
2535 if (!(node = alloc_int64_text_node( val_int64 ))) return E_OUTOFMEMORY;
2536 break;
2538 case RECORD_FLOAT_TEXT:
2539 case RECORD_FLOAT_TEXT_WITH_ENDELEMENT:
2541 float val_float;
2542 if ((hr = read_bytes( reader, (unsigned char *)&val_float, sizeof(val_float) )) != S_OK) return hr;
2543 if (!(node = alloc_float_text_node( val_float ))) return E_OUTOFMEMORY;
2544 break;
2546 case RECORD_DOUBLE_TEXT:
2547 case RECORD_DOUBLE_TEXT_WITH_ENDELEMENT:
2549 double val_double;
2550 if ((hr = read_bytes( reader, (unsigned char *)&val_double, sizeof(val_double) )) != S_OK) return hr;
2551 if (!(node = alloc_double_text_node( val_double ))) return E_OUTOFMEMORY;
2552 break;
2554 case RECORD_DATETIME_TEXT:
2555 case RECORD_DATETIME_TEXT_WITH_ENDELEMENT:
2557 WS_DATETIME datetime;
2558 if ((hr = read_datetime( reader, &datetime )) != S_OK) return hr;
2559 if (!(node = alloc_datetime_text_node( &datetime ))) return E_OUTOFMEMORY;
2560 break;
2562 case RECORD_CHARS8_TEXT:
2563 case RECORD_CHARS8_TEXT_WITH_ENDELEMENT:
2564 if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
2565 len = val_uint8;
2566 break;
2568 case RECORD_CHARS16_TEXT:
2569 case RECORD_CHARS16_TEXT_WITH_ENDELEMENT:
2570 if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
2571 len = val_uint16;
2572 break;
2574 case RECORD_CHARS32_TEXT:
2575 case RECORD_CHARS32_TEXT_WITH_ENDELEMENT:
2576 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
2577 if (val_int32 < 0) return WS_E_INVALID_FORMAT;
2578 len = val_int32;
2579 break;
2581 case RECORD_BYTES8_TEXT:
2582 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT:
2583 case RECORD_BYTES16_TEXT:
2584 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT:
2585 case RECORD_BYTES32_TEXT:
2586 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT:
2587 return read_text_bytes( reader, type );
2589 case RECORD_EMPTY_TEXT:
2590 case RECORD_EMPTY_TEXT_WITH_ENDELEMENT:
2591 len = 0;
2592 break;
2594 case RECORD_DICTIONARY_TEXT:
2595 case RECORD_DICTIONARY_TEXT_WITH_ENDELEMENT:
2597 const WS_XML_STRING *str;
2598 if ((hr = read_int31( reader, &id )) != S_OK) return hr;
2599 if ((hr = lookup_string( reader, id, &str )) != S_OK) return hr;
2600 if (!(node = alloc_utf8_text_node( str->bytes, str->length, NULL ))) return E_OUTOFMEMORY;
2601 break;
2603 case RECORD_UNIQUE_ID_TEXT:
2604 case RECORD_UNIQUE_ID_TEXT_WITH_ENDELEMENT:
2605 if ((hr = read_bytes( reader, (unsigned char *)&uuid, sizeof(uuid) )) != S_OK) return hr;
2606 if (!(node = alloc_unique_id_text_node( &uuid ))) return E_OUTOFMEMORY;
2607 break;
2609 case RECORD_GUID_TEXT:
2610 case RECORD_GUID_TEXT_WITH_ENDELEMENT:
2611 if ((hr = read_bytes( reader, (unsigned char *)&uuid, sizeof(uuid) )) != S_OK) return hr;
2612 if (!(node = alloc_guid_text_node( &uuid ))) return E_OUTOFMEMORY;
2613 break;
2615 case RECORD_UINT64_TEXT:
2616 case RECORD_UINT64_TEXT_WITH_ENDELEMENT:
2618 UINT64 val_uint64;
2619 if ((hr = read_bytes( reader, (unsigned char *)&val_uint64, sizeof(val_uint64) )) != S_OK) return hr;
2620 if (!(node = alloc_uint64_text_node( val_uint64 ))) return E_OUTOFMEMORY;
2621 break;
2623 case RECORD_BOOL_TEXT:
2624 case RECORD_BOOL_TEXT_WITH_ENDELEMENT:
2626 BOOL val_bool;
2627 if ((hr = read_bytes( reader, (unsigned char *)&val_bool, sizeof(val_bool) )) != S_OK) return hr;
2628 if (!(node = alloc_bool_text_node( !!val_bool ))) return E_OUTOFMEMORY;
2629 break;
2631 default:
2632 ERR( "unhandled record type %02x\n", type );
2633 return WS_E_NOT_SUPPORTED;
2636 if (!node)
2638 if (!(node = alloc_utf8_text_node( NULL, len, &utf8 ))) return E_OUTOFMEMORY;
2639 if (!len) utf8->value.bytes = (BYTE *)(utf8 + 1); /* quirk */
2640 if ((hr = read_bytes( reader, utf8->value.bytes, len )) != S_OK)
2642 free_node( node );
2643 return hr;
2647 if (type & 1) node->flags |= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT;
2648 read_insert_node( reader, parent, node );
2649 reader->state = READER_STATE_TEXT;
2650 reader->text_conv_offset = 0;
2651 return S_OK;
2654 static HRESULT read_node_text( struct reader * );
2656 static HRESULT read_startelement_text( struct reader *reader )
2658 HRESULT hr;
2660 if (read_cmp( reader, "<?", 2 ) == S_OK)
2662 if ((hr = read_xmldecl( reader )) != S_OK) return hr;
2664 read_skip_whitespace( reader );
2665 if (read_cmp( reader, "<", 1 ) == S_OK)
2667 if ((hr = read_element_text( reader )) != S_OK) return hr;
2669 if (read_cmp( reader, "/>", 2 ) == S_OK)
2671 read_skip( reader, 2 );
2672 reader->current = LIST_ENTRY( list_tail( &reader->current->children ), struct node, entry );
2673 reader->last = reader->current;
2674 reader->state = READER_STATE_ENDELEMENT;
2675 return S_OK;
2677 else if (read_cmp( reader, ">", 1 ) == S_OK)
2679 read_skip( reader, 1 );
2680 return read_node_text( reader );
2682 return WS_E_INVALID_FORMAT;
2685 static HRESULT read_node_bin( struct reader * );
2687 static HRESULT read_startelement_bin( struct reader *reader )
2689 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return WS_E_INVALID_FORMAT;
2690 return read_node_bin( reader );
2693 static HRESULT read_startelement( struct reader *reader )
2695 switch (reader->input_enc)
2697 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_startelement_text( reader );
2698 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_startelement_bin( reader );
2699 default:
2700 ERR( "unhandled encoding %u\n", reader->input_enc );
2701 return WS_E_NOT_SUPPORTED;
2705 static HRESULT read_to_startelement_text( struct reader *reader, BOOL *found )
2707 HRESULT hr;
2709 switch (reader->state)
2711 case READER_STATE_INITIAL:
2712 if ((hr = read_xmldecl( reader )) != S_OK) return hr;
2713 break;
2715 case READER_STATE_STARTELEMENT:
2716 if (found) *found = TRUE;
2717 return S_OK;
2719 default:
2720 break;
2723 read_skip_whitespace( reader );
2724 if ((hr = read_element_text( reader )) == S_OK && found)
2726 if (reader->state == READER_STATE_STARTELEMENT)
2727 *found = TRUE;
2728 else
2729 *found = FALSE;
2732 return hr;
2735 static HRESULT read_to_startelement_bin( struct reader *reader, BOOL *found )
2737 HRESULT hr;
2739 if (reader->state == READER_STATE_STARTELEMENT)
2741 if (found) *found = TRUE;
2742 return S_OK;
2745 if ((hr = read_element_bin( reader )) == S_OK && found)
2747 if (reader->state == READER_STATE_STARTELEMENT)
2748 *found = TRUE;
2749 else
2750 *found = FALSE;
2753 return hr;
2756 static HRESULT read_to_startelement( struct reader *reader, BOOL *found )
2758 switch (reader->input_enc)
2760 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_to_startelement_text( reader, found );
2761 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_to_startelement_bin( reader, found );
2762 default:
2763 ERR( "unhandled encoding %u\n", reader->input_enc );
2764 return WS_E_NOT_SUPPORTED;
2768 static int cmp_name( const unsigned char *name1, ULONG len1, const unsigned char *name2, ULONG len2 )
2770 ULONG i;
2771 if (len1 != len2) return 1;
2772 for (i = 0; i < len1; i++) { if (toupper( name1[i] ) != toupper( name2[i] )) return 1; }
2773 return 0;
2776 static struct node *find_startelement( struct reader *reader, const WS_XML_STRING *prefix,
2777 const WS_XML_STRING *localname )
2779 struct node *parent;
2780 const WS_XML_STRING *str;
2782 for (parent = reader->current; parent; parent = parent->parent)
2784 if (node_type( parent ) == WS_XML_NODE_TYPE_ELEMENT)
2786 str = parent->hdr.prefix;
2787 if (cmp_name( str->bytes, str->length, prefix->bytes, prefix->length )) continue;
2788 str = parent->hdr.localName;
2789 if (cmp_name( str->bytes, str->length, localname->bytes, localname->length )) continue;
2790 return parent;
2793 return NULL;
2796 static HRESULT read_endelement_text( struct reader *reader )
2798 struct node *parent;
2799 unsigned int len = 0, ch, skip;
2800 const unsigned char *start;
2801 WS_XML_STRING prefix, localname;
2802 HRESULT hr;
2804 if ((hr = read_cmp( reader, "</", 2 )) != S_OK) return hr;
2805 read_skip( reader, 2 );
2807 start = read_current_ptr( reader );
2808 for (;;)
2810 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2811 if (ch == '>')
2813 read_skip( reader, 1 );
2814 break;
2816 if (!read_isnamechar( ch )) return WS_E_INVALID_FORMAT;
2817 read_skip( reader, skip );
2818 len += skip;
2821 if ((hr = split_qname( start, len, &prefix, &localname )) != S_OK) return hr;
2822 if (!(parent = find_startelement( reader, &prefix, &localname ))) return WS_E_INVALID_FORMAT;
2824 reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
2825 reader->last = reader->current;
2826 reader->state = READER_STATE_ENDELEMENT;
2827 return S_OK;
2830 static HRESULT read_endelement_bin( struct reader *reader )
2832 struct node *parent;
2833 unsigned char type;
2834 HRESULT hr;
2836 if (!(reader->current->flags & NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT))
2838 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2839 if (type != RECORD_ENDELEMENT) return WS_E_INVALID_FORMAT;
2841 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2843 reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
2844 reader->last = reader->current;
2845 reader->state = READER_STATE_ENDELEMENT;
2846 return S_OK;
2849 static HRESULT read_endelement( struct reader *reader )
2851 if (reader->state == READER_STATE_EOF) return WS_E_INVALID_FORMAT;
2853 if (read_end_of_data( reader ))
2855 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
2856 reader->last = reader->current;
2857 reader->state = READER_STATE_EOF;
2858 return S_OK;
2861 switch (reader->input_enc)
2863 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_endelement_text( reader );
2864 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_endelement_bin( reader );
2865 default:
2866 ERR( "unhandled encoding %u\n", reader->input_enc );
2867 return WS_E_NOT_SUPPORTED;
2871 static HRESULT read_comment_text( struct reader *reader )
2873 unsigned int len = 0, ch, skip;
2874 const unsigned char *start;
2875 struct node *node, *parent;
2876 WS_XML_COMMENT_NODE *comment;
2877 HRESULT hr;
2879 if ((hr = read_cmp( reader, "<!--", 4 )) != S_OK) return hr;
2880 read_skip( reader, 4 );
2882 start = read_current_ptr( reader );
2883 for (;;)
2885 if (read_cmp( reader, "-->", 3 ) == S_OK)
2887 read_skip( reader, 3 );
2888 break;
2890 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2891 read_skip( reader, skip );
2892 len += skip;
2895 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2897 if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return E_OUTOFMEMORY;
2898 comment = (WS_XML_COMMENT_NODE *)node;
2899 if (!(comment->value.bytes = heap_alloc( len )))
2901 heap_free( node );
2902 return E_OUTOFMEMORY;
2904 memcpy( comment->value.bytes, start, len );
2905 comment->value.length = len;
2907 read_insert_node( reader, parent, node );
2908 reader->state = READER_STATE_COMMENT;
2909 return S_OK;
2912 static HRESULT read_comment_bin( struct reader *reader )
2914 struct node *node, *parent;
2915 WS_XML_COMMENT_NODE *comment;
2916 unsigned char type;
2917 ULONG len;
2918 HRESULT hr;
2920 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2921 if (type != RECORD_COMMENT) return WS_E_INVALID_FORMAT;
2922 if ((hr = read_int31( reader, &len )) != S_OK) return hr;
2924 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2926 if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return E_OUTOFMEMORY;
2927 comment = (WS_XML_COMMENT_NODE *)node;
2928 if (!(comment->value.bytes = heap_alloc( len )))
2930 heap_free( node );
2931 return E_OUTOFMEMORY;
2933 if ((hr = read_bytes( reader, comment->value.bytes, len )) != S_OK)
2935 free_node( node );
2936 return E_OUTOFMEMORY;
2938 comment->value.length = len;
2940 read_insert_node( reader, parent, node );
2941 reader->state = READER_STATE_COMMENT;
2942 return S_OK;
2945 static HRESULT read_startcdata( struct reader *reader )
2947 struct node *node, *endnode, *parent;
2948 HRESULT hr;
2950 if ((hr = read_cmp( reader, "<![CDATA[", 9 )) != S_OK) return hr;
2951 read_skip( reader, 9 );
2953 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2955 if (!(node = alloc_node( WS_XML_NODE_TYPE_CDATA ))) return E_OUTOFMEMORY;
2956 if (!(endnode = alloc_node( WS_XML_NODE_TYPE_END_CDATA )))
2958 heap_free( node );
2959 return E_OUTOFMEMORY;
2961 list_add_tail( &node->children, &endnode->entry );
2962 endnode->parent = node;
2964 read_insert_node( reader, parent, node );
2965 reader->state = READER_STATE_STARTCDATA;
2966 return S_OK;
2969 static HRESULT read_cdata( struct reader *reader )
2971 unsigned int len = 0, ch, skip;
2972 const unsigned char *start;
2973 struct node *node;
2974 WS_XML_TEXT_NODE *text;
2975 WS_XML_UTF8_TEXT *utf8;
2976 HRESULT hr;
2978 start = read_current_ptr( reader );
2979 for (;;)
2981 if (read_cmp( reader, "]]>", 3 ) == S_OK) break;
2982 if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2983 read_skip( reader, skip );
2984 len += skip;
2987 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return E_OUTOFMEMORY;
2988 text = (WS_XML_TEXT_NODE *)node;
2989 if (!(utf8 = alloc_utf8_text( start, len )))
2991 heap_free( node );
2992 return E_OUTOFMEMORY;
2994 text->text = &utf8->text;
2996 read_insert_node( reader, reader->current, node );
2997 reader->state = READER_STATE_CDATA;
2998 return S_OK;
3001 static HRESULT read_endcdata( struct reader *reader )
3003 struct node *parent;
3004 HRESULT hr;
3006 if ((hr = read_cmp( reader, "]]>", 3 )) != S_OK) return hr;
3007 read_skip( reader, 3 );
3009 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT) parent = reader->current->parent;
3010 else parent = reader->current;
3012 reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
3013 reader->last = reader->current;
3014 reader->state = READER_STATE_ENDCDATA;
3015 return S_OK;
3018 static HRESULT read_node_text( struct reader *reader )
3020 HRESULT hr;
3022 for (;;)
3024 if (read_end_of_data( reader ))
3026 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
3027 reader->last = reader->current;
3028 reader->state = READER_STATE_EOF;
3029 return S_OK;
3031 if (reader->state == READER_STATE_STARTCDATA) return read_cdata( reader );
3032 else if (reader->state == READER_STATE_CDATA) return read_endcdata( reader );
3033 else if (read_cmp( reader, "<?", 2 ) == S_OK)
3035 if ((hr = read_xmldecl( reader )) != S_OK) return hr;
3037 else if (read_cmp( reader, "</", 2 ) == S_OK) return read_endelement_text( reader );
3038 else if (read_cmp( reader, "<![CDATA[", 9 ) == S_OK) return read_startcdata( reader );
3039 else if (read_cmp( reader, "<!--", 4 ) == S_OK) return read_comment_text( reader );
3040 else if (read_cmp( reader, "<", 1 ) == S_OK) return read_element_text( reader );
3041 else if (read_cmp( reader, "/>", 2 ) == S_OK || read_cmp( reader, ">", 1 ) == S_OK)
3043 return read_startelement_text( reader );
3045 else return read_text_text( reader );
3049 static HRESULT read_node_bin( struct reader *reader )
3051 unsigned char type;
3052 HRESULT hr;
3054 if (reader->current->flags & NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT)
3056 reader->current = LIST_ENTRY( list_tail( &reader->current->parent->children ), struct node, entry );
3057 reader->last = reader->current;
3058 reader->state = READER_STATE_ENDELEMENT;
3059 return S_OK;
3061 if (read_end_of_data( reader ))
3063 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
3064 reader->last = reader->current;
3065 reader->state = READER_STATE_EOF;
3066 return S_OK;
3069 if ((hr = read_peek( reader, &type, 1 )) != S_OK) return hr;
3070 if (type == RECORD_ENDELEMENT)
3072 return read_endelement_bin( reader );
3074 else if (type == RECORD_COMMENT)
3076 return read_comment_bin( reader );
3078 else if (type >= RECORD_SHORT_ELEMENT && type <= RECORD_PREFIX_ELEMENT_Z)
3080 return read_element_bin( reader );
3082 else if (type >= RECORD_ZERO_TEXT && type <= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT)
3084 return read_text_bin( reader );
3086 FIXME( "unhandled record type %02x\n", type );
3087 return WS_E_NOT_SUPPORTED;
3090 static HRESULT read_node( struct reader *reader )
3092 switch (reader->input_enc)
3094 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_node_text( reader );
3095 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_node_bin( reader );
3096 default:
3097 ERR( "unhandled encoding %u\n", reader->input_enc );
3098 return WS_E_NOT_SUPPORTED;
3102 HRESULT copy_node( WS_XML_READER *handle, WS_XML_WRITER_ENCODING_TYPE enc, struct node **node )
3104 struct reader *reader = (struct reader *)handle;
3105 const struct list *ptr;
3106 const struct node *start;
3107 HRESULT hr;
3109 EnterCriticalSection( &reader->cs );
3111 if (reader->magic != READER_MAGIC)
3113 LeaveCriticalSection( &reader->cs );
3114 return E_INVALIDARG;
3117 if (reader->current != reader->root) ptr = &reader->current->entry;
3118 else /* copy whole tree */
3120 if (!read_end_of_data( reader ))
3122 for (;;)
3124 if ((hr = read_node( reader )) != S_OK) goto done;
3125 if (node_type( reader->current ) == WS_XML_NODE_TYPE_EOF) break;
3128 ptr = list_head( &reader->root->children );
3131 start = LIST_ENTRY( ptr, struct node, entry );
3132 if (node_type( start ) == WS_XML_NODE_TYPE_EOF) hr = WS_E_INVALID_OPERATION;
3133 else hr = dup_tree( start, enc, node );
3135 done:
3136 LeaveCriticalSection( &reader->cs );
3137 return hr;
3140 /**************************************************************************
3141 * WsReadEndElement [webservices.@]
3143 HRESULT WINAPI WsReadEndElement( WS_XML_READER *handle, WS_ERROR *error )
3145 struct reader *reader = (struct reader *)handle;
3146 HRESULT hr;
3148 TRACE( "%p %p\n", handle, error );
3149 if (error) FIXME( "ignoring error parameter\n" );
3151 if (!reader) return E_INVALIDARG;
3153 EnterCriticalSection( &reader->cs );
3155 if (reader->magic != READER_MAGIC)
3157 LeaveCriticalSection( &reader->cs );
3158 return E_INVALIDARG;
3161 hr = read_endelement( reader );
3163 LeaveCriticalSection( &reader->cs );
3164 TRACE( "returning %08x\n", hr );
3165 return hr;
3168 /**************************************************************************
3169 * WsReadNode [webservices.@]
3171 HRESULT WINAPI WsReadNode( WS_XML_READER *handle, WS_ERROR *error )
3173 struct reader *reader = (struct reader *)handle;
3174 HRESULT hr;
3176 TRACE( "%p %p\n", handle, error );
3177 if (error) FIXME( "ignoring error parameter\n" );
3179 if (!reader) return E_INVALIDARG;
3181 EnterCriticalSection( &reader->cs );
3183 if (reader->magic != READER_MAGIC)
3185 LeaveCriticalSection( &reader->cs );
3186 return E_INVALIDARG;
3189 hr = read_node( reader );
3191 LeaveCriticalSection( &reader->cs );
3192 TRACE( "returning %08x\n", hr );
3193 return hr;
3196 static HRESULT skip_node( struct reader *reader )
3198 const struct node *parent;
3199 HRESULT hr;
3201 if (node_type( reader->current ) == WS_XML_NODE_TYPE_EOF) return WS_E_INVALID_OPERATION;
3202 if (node_type( reader->current ) == WS_XML_NODE_TYPE_ELEMENT) parent = reader->current;
3203 else parent = NULL;
3205 for (;;)
3207 if ((hr = read_node( reader )) != S_OK || !parent) break;
3208 if (node_type( reader->current ) != WS_XML_NODE_TYPE_END_ELEMENT) continue;
3209 if (reader->current->parent == parent) return read_node( reader );
3212 return hr;
3215 /**************************************************************************
3216 * WsSkipNode [webservices.@]
3218 HRESULT WINAPI WsSkipNode( WS_XML_READER *handle, WS_ERROR *error )
3220 struct reader *reader = (struct reader *)handle;
3221 HRESULT hr;
3223 TRACE( "%p %p\n", handle, error );
3224 if (error) FIXME( "ignoring error parameter\n" );
3226 if (!reader) return E_INVALIDARG;
3228 EnterCriticalSection( &reader->cs );
3230 if (reader->magic != READER_MAGIC)
3232 LeaveCriticalSection( &reader->cs );
3233 return E_INVALIDARG;
3236 hr = skip_node( reader );
3238 LeaveCriticalSection( &reader->cs );
3239 TRACE( "returning %08x\n", hr );
3240 return hr;
3243 /**************************************************************************
3244 * WsReadStartElement [webservices.@]
3246 HRESULT WINAPI WsReadStartElement( WS_XML_READER *handle, WS_ERROR *error )
3248 struct reader *reader = (struct reader *)handle;
3249 HRESULT hr;
3251 TRACE( "%p %p\n", handle, error );
3252 if (error) FIXME( "ignoring error parameter\n" );
3254 if (!reader) return E_INVALIDARG;
3256 EnterCriticalSection( &reader->cs );
3258 if (reader->magic != READER_MAGIC)
3260 LeaveCriticalSection( &reader->cs );
3261 return E_INVALIDARG;
3264 hr = read_startelement( reader );
3266 LeaveCriticalSection( &reader->cs );
3267 TRACE( "returning %08x\n", hr );
3268 return hr;
3271 /**************************************************************************
3272 * WsReadToStartElement [webservices.@]
3274 HRESULT WINAPI WsReadToStartElement( WS_XML_READER *handle, const WS_XML_STRING *localname,
3275 const WS_XML_STRING *ns, BOOL *found, WS_ERROR *error )
3277 struct reader *reader = (struct reader *)handle;
3278 HRESULT hr;
3280 TRACE( "%p %s %s %p %p\n", handle, debugstr_xmlstr(localname), debugstr_xmlstr(ns), found, error );
3281 if (error) FIXME( "ignoring error parameter\n" );
3283 if (!reader) return E_INVALIDARG;
3284 if (localname || ns) FIXME( "name and/or namespace not verified\n" );
3286 EnterCriticalSection( &reader->cs );
3288 if (reader->magic != READER_MAGIC)
3290 LeaveCriticalSection( &reader->cs );
3291 return E_INVALIDARG;
3294 hr = read_to_startelement( reader, found );
3296 LeaveCriticalSection( &reader->cs );
3297 TRACE( "returning %08x\n", hr );
3298 return hr;
3301 BOOL move_to_root_element( struct node *root, struct node **current )
3303 struct list *ptr;
3304 struct node *node;
3306 if (!(ptr = list_head( &root->children ))) return FALSE;
3307 node = LIST_ENTRY( ptr, struct node, entry );
3308 if (node_type( node ) == WS_XML_NODE_TYPE_ELEMENT)
3310 *current = node;
3311 return TRUE;
3313 while ((ptr = list_next( &root->children, &node->entry )))
3315 struct node *next = LIST_ENTRY( ptr, struct node, entry );
3316 if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3318 *current = next;
3319 return TRUE;
3321 node = next;
3323 return FALSE;
3326 BOOL move_to_next_element( struct node **current )
3328 struct list *ptr;
3329 struct node *node = *current, *parent = (*current)->parent;
3331 if (!parent) return FALSE;
3332 while ((ptr = list_next( &parent->children, &node->entry )))
3334 struct node *next = LIST_ENTRY( ptr, struct node, entry );
3335 if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3337 *current = next;
3338 return TRUE;
3340 node = next;
3342 return FALSE;
3345 BOOL move_to_prev_element( struct node **current )
3347 struct list *ptr;
3348 struct node *node = *current, *parent = (*current)->parent;
3350 if (!parent) return FALSE;
3351 while ((ptr = list_prev( &parent->children, &node->entry )))
3353 struct node *prev = LIST_ENTRY( ptr, struct node, entry );
3354 if (node_type( prev ) == WS_XML_NODE_TYPE_ELEMENT)
3356 *current = prev;
3357 return TRUE;
3359 node = prev;
3361 return FALSE;
3364 BOOL move_to_child_element( struct node **current )
3366 struct list *ptr;
3367 struct node *child, *node = *current;
3369 if (!(ptr = list_head( &node->children ))) return FALSE;
3370 child = LIST_ENTRY( ptr, struct node, entry );
3371 if (node_type( child ) == WS_XML_NODE_TYPE_ELEMENT)
3373 *current = child;
3374 return TRUE;
3376 while ((ptr = list_next( &node->children, &child->entry )))
3378 struct node *next = LIST_ENTRY( ptr, struct node, entry );
3379 if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3381 *current = next;
3382 return TRUE;
3384 child = next;
3386 return FALSE;
3389 BOOL move_to_end_element( struct node **current )
3391 struct list *ptr;
3392 struct node *node = *current;
3394 if (node_type( node ) != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
3396 if ((ptr = list_tail( &node->children )))
3398 struct node *tail = LIST_ENTRY( ptr, struct node, entry );
3399 if (node_type( tail ) == WS_XML_NODE_TYPE_END_ELEMENT)
3401 *current = tail;
3402 return TRUE;
3405 return FALSE;
3408 BOOL move_to_parent_element( struct node **current )
3410 struct node *parent = (*current)->parent;
3412 if (parent && (node_type( parent ) == WS_XML_NODE_TYPE_ELEMENT ||
3413 node_type( parent ) == WS_XML_NODE_TYPE_BOF))
3415 *current = parent;
3416 return TRUE;
3418 return FALSE;
3421 BOOL move_to_first_node( struct node **current )
3423 struct list *ptr;
3424 struct node *node = *current;
3426 if ((ptr = list_head( &node->parent->children )))
3428 *current = LIST_ENTRY( ptr, struct node, entry );
3429 return TRUE;
3431 return FALSE;
3434 BOOL move_to_next_node( struct node **current )
3436 struct list *ptr;
3437 struct node *node = *current;
3439 if ((ptr = list_next( &node->parent->children, &node->entry )))
3441 *current = LIST_ENTRY( ptr, struct node, entry );
3442 return TRUE;
3444 return FALSE;
3447 BOOL move_to_prev_node( struct node **current )
3449 struct list *ptr;
3450 struct node *node = *current;
3452 if ((ptr = list_prev( &node->parent->children, &node->entry )))
3454 *current = LIST_ENTRY( ptr, struct node, entry );
3455 return TRUE;
3457 return FALSE;
3460 BOOL move_to_bof( struct node *root, struct node **current )
3462 *current = root;
3463 return TRUE;
3466 BOOL move_to_eof( struct node *root, struct node **current )
3468 struct list *ptr;
3469 if ((ptr = list_tail( &root->children )))
3471 *current = LIST_ENTRY( ptr, struct node, entry );
3472 return TRUE;
3474 return FALSE;
3477 BOOL move_to_child_node( struct node **current )
3479 struct list *ptr;
3480 struct node *node = *current;
3482 if ((ptr = list_head( &node->children )))
3484 *current = LIST_ENTRY( ptr, struct node, entry );
3485 return TRUE;
3487 return FALSE;
3490 BOOL move_to_parent_node( struct node **current )
3492 struct node *parent = (*current)->parent;
3493 if (!parent) return FALSE;
3494 *current = parent;
3495 return TRUE;
3498 static HRESULT read_move_to( struct reader *reader, WS_MOVE_TO move, BOOL *found )
3500 BOOL success = FALSE;
3501 HRESULT hr = S_OK;
3503 if (!read_end_of_data( reader ))
3505 struct node *saved_current = reader->current;
3506 while (reader->state != READER_STATE_EOF && (hr = read_node( reader )) == S_OK) { /* nothing */ };
3507 if (hr != S_OK) return hr;
3508 reader->current = saved_current;
3510 switch (move)
3512 case WS_MOVE_TO_ROOT_ELEMENT:
3513 success = move_to_root_element( reader->root, &reader->current );
3514 break;
3516 case WS_MOVE_TO_NEXT_ELEMENT:
3517 success = move_to_next_element( &reader->current );
3518 break;
3520 case WS_MOVE_TO_PREVIOUS_ELEMENT:
3521 success = move_to_prev_element( &reader->current );
3522 break;
3524 case WS_MOVE_TO_CHILD_ELEMENT:
3525 success = move_to_child_element( &reader->current );
3526 break;
3528 case WS_MOVE_TO_END_ELEMENT:
3529 success = move_to_end_element( &reader->current );
3530 break;
3532 case WS_MOVE_TO_PARENT_ELEMENT:
3533 success = move_to_parent_element( &reader->current );
3534 break;
3536 case WS_MOVE_TO_FIRST_NODE:
3537 success = move_to_first_node( &reader->current );
3538 break;
3540 case WS_MOVE_TO_NEXT_NODE:
3541 success = move_to_next_node( &reader->current );
3542 break;
3544 case WS_MOVE_TO_PREVIOUS_NODE:
3545 success = move_to_prev_node( &reader->current );
3546 break;
3548 case WS_MOVE_TO_CHILD_NODE:
3549 success = move_to_child_node( &reader->current );
3550 break;
3552 case WS_MOVE_TO_BOF:
3553 success = move_to_bof( reader->root, &reader->current );
3554 break;
3556 case WS_MOVE_TO_EOF:
3557 success = move_to_eof( reader->root, &reader->current );
3558 break;
3560 default:
3561 FIXME( "unhandled move %u\n", move );
3562 return E_NOTIMPL;
3565 if (found)
3567 *found = success;
3568 return S_OK;
3570 return success ? S_OK : WS_E_INVALID_FORMAT;
3573 /**************************************************************************
3574 * WsMoveReader [webservices.@]
3576 HRESULT WINAPI WsMoveReader( WS_XML_READER *handle, WS_MOVE_TO move, BOOL *found, WS_ERROR *error )
3578 struct reader *reader = (struct reader *)handle;
3579 HRESULT hr;
3581 TRACE( "%p %u %p %p\n", handle, move, found, error );
3582 if (error) FIXME( "ignoring error parameter\n" );
3584 if (!reader) return E_INVALIDARG;
3586 EnterCriticalSection( &reader->cs );
3588 if (reader->magic != READER_MAGIC)
3590 LeaveCriticalSection( &reader->cs );
3591 return E_INVALIDARG;
3594 if (reader->input_type != WS_XML_READER_INPUT_TYPE_BUFFER) hr = WS_E_INVALID_OPERATION;
3595 else hr = read_move_to( reader, move, found );
3597 LeaveCriticalSection( &reader->cs );
3598 TRACE( "returning %08x\n", hr );
3599 return hr;
3602 /**************************************************************************
3603 * WsReadStartAttribute [webservices.@]
3605 HRESULT WINAPI WsReadStartAttribute( WS_XML_READER *handle, ULONG index, WS_ERROR *error )
3607 struct reader *reader = (struct reader *)handle;
3608 const WS_XML_ELEMENT_NODE *elem;
3609 HRESULT hr = S_OK;
3611 TRACE( "%p %u %p\n", handle, index, error );
3612 if (error) FIXME( "ignoring error parameter\n" );
3614 if (!reader) return E_INVALIDARG;
3616 EnterCriticalSection( &reader->cs );
3618 if (reader->magic != READER_MAGIC)
3620 LeaveCriticalSection( &reader->cs );
3621 return E_INVALIDARG;
3624 elem = &reader->current->hdr;
3625 if (reader->state != READER_STATE_STARTELEMENT || index >= elem->attributeCount) hr = WS_E_INVALID_FORMAT;
3626 else
3628 reader->current_attr = index;
3629 reader->state = READER_STATE_STARTATTRIBUTE;
3632 LeaveCriticalSection( &reader->cs );
3633 TRACE( "returning %08x\n", hr );
3634 return S_OK;
3637 /**************************************************************************
3638 * WsReadEndAttribute [webservices.@]
3640 HRESULT WINAPI WsReadEndAttribute( WS_XML_READER *handle, WS_ERROR *error )
3642 struct reader *reader = (struct reader *)handle;
3643 HRESULT hr = S_OK;
3645 TRACE( "%p %p\n", handle, error );
3646 if (error) FIXME( "ignoring error parameter\n" );
3648 if (!reader) return E_INVALIDARG;
3650 EnterCriticalSection( &reader->cs );
3652 if (reader->magic != READER_MAGIC)
3654 LeaveCriticalSection( &reader->cs );
3655 return E_INVALIDARG;
3658 if (reader->state != READER_STATE_STARTATTRIBUTE) hr = WS_E_INVALID_FORMAT;
3659 else reader->state = READER_STATE_STARTELEMENT;
3661 LeaveCriticalSection( &reader->cs );
3662 TRACE( "returning %08x\n", hr );
3663 return hr;
3666 static HRESULT str_to_bool( const unsigned char *str, ULONG len, BOOL *ret )
3668 if (len == 4 && !memcmp( str, "true", 4 )) *ret = TRUE;
3669 else if (len == 1 && !memcmp( str, "1", 1 )) *ret = TRUE;
3670 else if (len == 5 && !memcmp( str, "false", 5 )) *ret = FALSE;
3671 else if (len == 1 && !memcmp( str, "0", 1 )) *ret = FALSE;
3672 else return WS_E_INVALID_FORMAT;
3673 return S_OK;
3676 static HRESULT str_to_int64( const unsigned char *str, ULONG len, INT64 min, INT64 max, INT64 *ret )
3678 BOOL negative = FALSE;
3679 const unsigned char *ptr = str;
3681 *ret = 0;
3682 while (len && read_isspace( *ptr )) { ptr++; len--; }
3683 while (len && read_isspace( ptr[len - 1] )) { len--; }
3684 if (!len) return WS_E_INVALID_FORMAT;
3686 if (*ptr == '-')
3688 negative = TRUE;
3689 ptr++;
3690 len--;
3692 if (!len) return WS_E_INVALID_FORMAT;
3694 while (len--)
3696 int val;
3698 if (!isdigit( *ptr )) return WS_E_INVALID_FORMAT;
3699 val = *ptr - '0';
3700 if (negative) val = -val;
3702 if ((!negative && (*ret > max / 10 || *ret * 10 > max - val)) ||
3703 (negative && (*ret < min / 10 || *ret * 10 < min - val)))
3705 return WS_E_NUMERIC_OVERFLOW;
3707 *ret = *ret * 10 + val;
3708 ptr++;
3711 return S_OK;
3714 static HRESULT str_to_uint64( const unsigned char *str, ULONG len, UINT64 max, UINT64 *ret )
3716 const unsigned char *ptr = str;
3718 *ret = 0;
3719 while (len && read_isspace( *ptr )) { ptr++; len--; }
3720 while (len && read_isspace( ptr[len - 1] )) { len--; }
3721 if (!len) return WS_E_INVALID_FORMAT;
3723 while (len--)
3725 unsigned int val;
3727 if (!isdigit( *ptr )) return WS_E_INVALID_FORMAT;
3728 val = *ptr - '0';
3730 if ((*ret > max / 10 || *ret * 10 > max - val)) return WS_E_NUMERIC_OVERFLOW;
3731 *ret = *ret * 10 + val;
3732 ptr++;
3735 return S_OK;
3738 static HRESULT str_to_double( const unsigned char *str, ULONG len, double *ret )
3740 BOOL found_sign = FALSE, found_exponent = FALSE, found_digit = FALSE, found_decimal = FALSE;
3741 static const unsigned __int64 nan = 0xfff8000000000000;
3742 static const unsigned __int64 inf = 0x7ff0000000000000;
3743 static const unsigned __int64 inf_min = 0xfff0000000000000;
3744 const char *p = (const char *)str;
3745 double tmp;
3746 ULONG i;
3748 while (len && read_isspace( *p )) { p++; len--; }
3749 while (len && read_isspace( p[len - 1] )) { len--; }
3750 if (!len) return WS_E_INVALID_FORMAT;
3752 if (len == 3 && !memcmp( p, "NaN", 3 ))
3754 *(unsigned __int64 *)ret = nan;
3755 return S_OK;
3757 if (len == 3 && !memcmp( p, "INF", 3 ))
3759 *(unsigned __int64 *)ret = inf;
3760 return S_OK;
3762 if (len == 4 && !memcmp( p, "-INF", 4 ))
3764 *(unsigned __int64 *)ret = inf_min;
3765 return S_OK;
3768 for (i = 0; i < len; i++)
3770 if (p[i] >= '0' && p[i] <= '9')
3772 found_digit = TRUE;
3773 continue;
3775 if (!found_sign && !found_digit && (p[i] == '+' || p[i] == '-'))
3777 found_sign = TRUE;
3778 continue;
3780 if (!found_exponent && found_digit && (p[i] == 'e' || p[i] == 'E'))
3782 found_exponent = found_decimal = TRUE;
3783 found_digit = found_sign = FALSE;
3784 continue;
3786 if (!found_decimal && p[i] == '.')
3788 found_decimal = TRUE;
3789 continue;
3791 return WS_E_INVALID_FORMAT;
3793 if (!found_digit && !found_exponent)
3795 *ret = 0;
3796 return S_OK;
3799 if (_snscanf_l( p, len, "%lf", c_locale, &tmp ) != 1) return WS_E_INVALID_FORMAT;
3800 *ret = tmp;
3801 return S_OK;
3804 static HRESULT str_to_float( const unsigned char *str, ULONG len, float *ret )
3806 static const unsigned int inf = 0x7f800000;
3807 static const unsigned int inf_min = 0xff800000;
3808 const unsigned char *p = str;
3809 double val;
3810 HRESULT hr;
3812 while (len && read_isspace( *p )) { p++; len--; }
3813 while (len && read_isspace( p[len - 1] )) { len--; }
3814 if (!len) return WS_E_INVALID_FORMAT;
3816 if (len == 3 && !memcmp( p, "INF", 3 ))
3818 *(unsigned int *)ret = inf;
3819 return S_OK;
3821 if (len == 4 && !memcmp( p, "-INF", 4 ))
3823 *(unsigned int *)ret = inf_min;
3824 return S_OK;
3827 if ((hr = str_to_double( p, len, &val )) != S_OK) return hr;
3828 *ret = val;
3829 return S_OK;
3832 HRESULT str_to_guid( const unsigned char *str, ULONG len, GUID *ret )
3834 static const unsigned char hex[] =
3836 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
3837 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
3838 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
3839 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
3840 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
3841 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
3842 0,10,11,12,13,14,15 /* 0x60 */
3844 const unsigned char *p = str;
3845 ULONG i;
3847 while (len && read_isspace( *p )) { p++; len--; }
3848 while (len && read_isspace( p[len - 1] )) { len--; }
3849 if (len != 36) return WS_E_INVALID_FORMAT;
3851 if (p[8] != '-' || p[13] != '-' || p[18] != '-' || p[23] != '-')
3852 return WS_E_INVALID_FORMAT;
3854 for (i = 0; i < 36; i++)
3856 if (i == 8 || i == 13 || i == 18 || i == 23) continue;
3857 if (p[i] > 'f' || (!hex[p[i]] && p[i] != '0')) return WS_E_INVALID_FORMAT;
3860 ret->Data1 = hex[p[0]] << 28 | hex[p[1]] << 24 | hex[p[2]] << 20 | hex[p[3]] << 16 |
3861 hex[p[4]] << 12 | hex[p[5]] << 8 | hex[p[6]] << 4 | hex[p[7]];
3863 ret->Data2 = hex[p[9]] << 12 | hex[p[10]] << 8 | hex[p[11]] << 4 | hex[p[12]];
3864 ret->Data3 = hex[p[14]] << 12 | hex[p[15]] << 8 | hex[p[16]] << 4 | hex[p[17]];
3866 ret->Data4[0] = hex[p[19]] << 4 | hex[p[20]];
3867 ret->Data4[1] = hex[p[21]] << 4 | hex[p[22]];
3868 ret->Data4[2] = hex[p[24]] << 4 | hex[p[25]];
3869 ret->Data4[3] = hex[p[26]] << 4 | hex[p[27]];
3870 ret->Data4[4] = hex[p[28]] << 4 | hex[p[29]];
3871 ret->Data4[5] = hex[p[30]] << 4 | hex[p[31]];
3872 ret->Data4[6] = hex[p[32]] << 4 | hex[p[33]];
3873 ret->Data4[7] = hex[p[34]] << 4 | hex[p[35]];
3875 return S_OK;
3878 static HRESULT str_to_string( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_STRING *ret )
3880 int len_utf16 = MultiByteToWideChar( CP_UTF8, 0, (const char *)str, len, NULL, 0 );
3881 if (!(ret->chars = ws_alloc( heap, len_utf16 * sizeof(WCHAR) ))) return WS_E_QUOTA_EXCEEDED;
3882 MultiByteToWideChar( CP_UTF8, 0, (const char *)str, len, ret->chars, len_utf16 );
3883 ret->length = len_utf16;
3884 return S_OK;
3887 static HRESULT str_to_unique_id( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_UNIQUE_ID *ret )
3889 if (len == 45 && !memcmp( str, "urn:uuid:", 9 ))
3891 ret->uri.length = 0;
3892 ret->uri.chars = NULL;
3893 return str_to_guid( str + 9, len - 9, &ret->guid );
3896 memset( &ret->guid, 0, sizeof(ret->guid) );
3897 return str_to_string( str, len, heap, &ret->uri );
3900 static inline unsigned char decode_char( unsigned char c )
3902 if (c >= 'A' && c <= 'Z') return c - 'A';
3903 if (c >= 'a' && c <= 'z') return c - 'a' + 26;
3904 if (c >= '0' && c <= '9') return c - '0' + 52;
3905 if (c == '+') return 62;
3906 if (c == '/') return 63;
3907 return 64;
3910 static ULONG decode_base64( const unsigned char *base64, ULONG len, unsigned char *buf )
3912 ULONG i = 0;
3913 unsigned char c0, c1, c2, c3;
3914 const unsigned char *p = base64;
3916 while (len > 4)
3918 if ((c0 = decode_char( p[0] )) > 63) return 0;
3919 if ((c1 = decode_char( p[1] )) > 63) return 0;
3920 if ((c2 = decode_char( p[2] )) > 63) return 0;
3921 if ((c3 = decode_char( p[3] )) > 63) return 0;
3922 buf[i + 0] = (c0 << 2) | (c1 >> 4);
3923 buf[i + 1] = (c1 << 4) | (c2 >> 2);
3924 buf[i + 2] = (c2 << 6) | c3;
3925 len -= 4;
3926 i += 3;
3927 p += 4;
3929 if (p[2] == '=')
3931 if ((c0 = decode_char( p[0] )) > 63) return 0;
3932 if ((c1 = decode_char( p[1] )) > 63) return 0;
3933 buf[i] = (c0 << 2) | (c1 >> 4);
3934 i++;
3936 else if (p[3] == '=')
3938 if ((c0 = decode_char( p[0] )) > 63) return 0;
3939 if ((c1 = decode_char( p[1] )) > 63) return 0;
3940 if ((c2 = decode_char( p[2] )) > 63) return 0;
3941 buf[i + 0] = (c0 << 2) | (c1 >> 4);
3942 buf[i + 1] = (c1 << 4) | (c2 >> 2);
3943 i += 2;
3945 else
3947 if ((c0 = decode_char( p[0] )) > 63) return 0;
3948 if ((c1 = decode_char( p[1] )) > 63) return 0;
3949 if ((c2 = decode_char( p[2] )) > 63) return 0;
3950 if ((c3 = decode_char( p[3] )) > 63) return 0;
3951 buf[i + 0] = (c0 << 2) | (c1 >> 4);
3952 buf[i + 1] = (c1 << 4) | (c2 >> 2);
3953 buf[i + 2] = (c2 << 6) | c3;
3954 i += 3;
3956 return i;
3959 static HRESULT str_to_bytes( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_BYTES *ret )
3961 const unsigned char *p = str;
3963 while (len && read_isspace( *p )) { p++; len--; }
3964 while (len && read_isspace( p[len - 1] )) { len--; }
3966 if (len % 4) return WS_E_INVALID_FORMAT;
3967 if (!(ret->bytes = ws_alloc( heap, len * 3 / 4 ))) return WS_E_QUOTA_EXCEEDED;
3968 ret->length = decode_base64( p, len, ret->bytes );
3969 return S_OK;
3972 static HRESULT str_to_xml_string( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_XML_STRING *ret )
3974 if (!(ret->bytes = ws_alloc( heap, len ))) return WS_E_QUOTA_EXCEEDED;
3975 memcpy( ret->bytes, str, len );
3976 ret->length = len;
3977 ret->dictionary = NULL;
3978 ret->id = 0;
3979 return S_OK;
3982 static HRESULT copy_xml_string( WS_HEAP *heap, const WS_XML_STRING *src, WS_XML_STRING *dst )
3984 if (!(dst->bytes = ws_alloc( heap, src->length ))) return WS_E_QUOTA_EXCEEDED;
3985 memcpy( dst->bytes, src->bytes, src->length );
3986 dst->length = src->length;
3987 return S_OK;
3990 static HRESULT str_to_qname( struct reader *reader, const unsigned char *str, ULONG len, WS_HEAP *heap,
3991 WS_XML_STRING *prefix_ret, WS_XML_STRING *localname_ret, WS_XML_STRING *ns_ret )
3993 const unsigned char *p = str;
3994 WS_XML_STRING prefix, localname;
3995 const WS_XML_STRING *ns;
3996 HRESULT hr;
3998 while (len && read_isspace( *p )) { p++; len--; }
3999 while (len && read_isspace( p[len - 1] )) { len--; }
4001 if ((hr = split_qname( p, len, &prefix, &localname )) != S_OK) return hr;
4002 if (!(ns = get_namespace( reader, &prefix ))) return WS_E_INVALID_FORMAT;
4004 if (prefix_ret && (hr = copy_xml_string( heap, &prefix, prefix_ret )) != S_OK) return hr;
4005 if ((hr = copy_xml_string( heap, &localname, localname_ret )) != S_OK)
4007 ws_free( heap, prefix_ret->bytes, prefix_ret->length );
4008 return hr;
4010 if ((hr = copy_xml_string( heap, ns, ns_ret )) != S_OK)
4012 ws_free( heap, prefix_ret->bytes, prefix_ret->length );
4013 ws_free( heap, localname_ret->bytes, localname_ret->length );
4014 return hr;
4016 return S_OK;
4019 static HRESULT read_qualified_name( struct reader *reader, WS_HEAP *heap, WS_XML_STRING *prefix,
4020 WS_XML_STRING *localname, WS_XML_STRING *ns )
4022 const WS_XML_TEXT_NODE *node = (const WS_XML_TEXT_NODE *)reader->current;
4023 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)node->text;
4024 return str_to_qname( reader, utf8->value.bytes, utf8->value.length, heap, prefix, localname, ns );
4027 /**************************************************************************
4028 * WsReadQualifiedName [webservices.@]
4030 HRESULT WINAPI WsReadQualifiedName( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_STRING *prefix,
4031 WS_XML_STRING *localname, WS_XML_STRING *ns,
4032 WS_ERROR *error )
4034 struct reader *reader = (struct reader *)handle;
4035 HRESULT hr;
4037 TRACE( "%p %p %p %p %p %p\n", handle, heap, prefix, localname, ns, error );
4038 if (error) FIXME( "ignoring error parameter\n" );
4040 if (!reader || !heap) return E_INVALIDARG;
4042 EnterCriticalSection( &reader->cs );
4044 if (reader->magic != READER_MAGIC)
4046 LeaveCriticalSection( &reader->cs );
4047 return E_INVALIDARG;
4050 if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
4051 else if (!localname) hr = E_INVALIDARG;
4052 else if (reader->state != READER_STATE_TEXT) hr = WS_E_INVALID_FORMAT;
4053 else hr = read_qualified_name( reader, heap, prefix, localname, ns );
4055 LeaveCriticalSection( &reader->cs );
4056 TRACE( "returning %08x\n", hr );
4057 return hr;
4060 static const int month_offsets[2][12] =
4062 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
4063 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
4066 static inline int valid_day( int year, int month, int day )
4068 return day > 0 && day <= month_days[leap_year( year )][month - 1];
4071 static inline int leap_days_before( int year )
4073 return (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
4076 static HRESULT str_to_datetime( const unsigned char *bytes, ULONG len, WS_DATETIME *ret )
4078 const unsigned char *p = bytes, *q;
4079 int year, month, day, hour, min, sec, sec_frac = 0, tz_hour, tz_min, tz_neg;
4081 while (len && read_isspace( *p )) { p++; len--; }
4082 while (len && read_isspace( p[len - 1] )) { len--; }
4084 q = p;
4085 while (len && isdigit( *q )) { q++; len--; };
4086 if (q - p != 4 || !len || *q != '-') return WS_E_INVALID_FORMAT;
4087 year = (p[0] - '0') * 1000 + (p[1] - '0') * 100 + (p[2] - '0') * 10 + p[3] - '0';
4088 if (year < 1) return WS_E_INVALID_FORMAT;
4090 p = ++q; len--;
4091 while (len && isdigit( *q )) { q++; len--; };
4092 if (q - p != 2 || !len || *q != '-') return WS_E_INVALID_FORMAT;
4093 month = (p[0] - '0') * 10 + p[1] - '0';
4094 if (month < 1 || month > 12) return WS_E_INVALID_FORMAT;
4096 p = ++q; len--;
4097 while (len && isdigit( *q )) { q++; len--; };
4098 if (q - p != 2 || !len || *q != 'T') return WS_E_INVALID_FORMAT;
4099 day = (p[0] - '0') * 10 + p[1] - '0';
4100 if (!valid_day( year, month, day )) return WS_E_INVALID_FORMAT;
4102 p = ++q; len--;
4103 while (len && isdigit( *q )) { q++; len--; };
4104 if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
4105 hour = (p[0] - '0') * 10 + p[1] - '0';
4106 if (hour > 24) return WS_E_INVALID_FORMAT;
4108 p = ++q; len--;
4109 while (len && isdigit( *q )) { q++; len--; };
4110 if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
4111 min = (p[0] - '0') * 10 + p[1] - '0';
4112 if (min > 59 || (min > 0 && hour == 24)) return WS_E_INVALID_FORMAT;
4114 p = ++q; len--;
4115 while (len && isdigit( *q )) { q++; len--; };
4116 if (q - p != 2 || !len) return WS_E_INVALID_FORMAT;
4117 sec = (p[0] - '0') * 10 + p[1] - '0';
4118 if (sec > 59 || (sec > 0 && hour == 24)) return WS_E_INVALID_FORMAT;
4120 if (*q == '.')
4122 unsigned int i, nb_digits, mul = TICKS_PER_SEC / 10;
4123 p = ++q; len--;
4124 while (len && isdigit( *q )) { q++; len--; };
4125 nb_digits = q - p;
4126 if (nb_digits < 1 || nb_digits > 7) return WS_E_INVALID_FORMAT;
4127 for (i = 0; i < nb_digits; i++)
4129 sec_frac += (p[i] - '0') * mul;
4130 mul /= 10;
4133 if (*q == 'Z')
4135 if (--len) return WS_E_INVALID_FORMAT;
4136 tz_hour = tz_min = tz_neg = 0;
4137 ret->format = WS_DATETIME_FORMAT_UTC;
4139 else if (*q == '+' || *q == '-')
4141 tz_neg = (*q == '-') ? 1 : 0;
4143 p = ++q; len--;
4144 while (len && isdigit( *q )) { q++; len--; };
4145 if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
4146 tz_hour = (p[0] - '0') * 10 + p[1] - '0';
4147 if (tz_hour > 14) return WS_E_INVALID_FORMAT;
4149 p = ++q; len--;
4150 while (len && isdigit( *q )) { q++; len--; };
4151 if (q - p != 2 || len) return WS_E_INVALID_FORMAT;
4152 tz_min = (p[0] - '0') * 10 + p[1] - '0';
4153 if (tz_min > 59 || (tz_min > 0 && tz_hour == 14)) return WS_E_INVALID_FORMAT;
4155 ret->format = WS_DATETIME_FORMAT_LOCAL;
4157 else return WS_E_INVALID_FORMAT;
4159 ret->ticks = ((year - 1) * 365 + leap_days_before( year )) * TICKS_PER_DAY;
4160 ret->ticks += month_offsets[leap_year( year )][month - 1] * TICKS_PER_DAY;
4161 ret->ticks += (day - 1) * TICKS_PER_DAY;
4162 ret->ticks += hour * TICKS_PER_HOUR;
4163 ret->ticks += min * TICKS_PER_MIN;
4164 ret->ticks += sec * TICKS_PER_SEC;
4165 ret->ticks += sec_frac;
4167 if (tz_neg)
4169 if (tz_hour * TICKS_PER_HOUR + tz_min * TICKS_PER_MIN + ret->ticks > TICKS_MAX)
4170 return WS_E_INVALID_FORMAT;
4171 ret->ticks += tz_hour * TICKS_PER_HOUR;
4172 ret->ticks += tz_min * TICKS_PER_MIN;
4174 else
4176 if (tz_hour * TICKS_PER_HOUR + tz_min * TICKS_PER_MIN > ret->ticks)
4177 return WS_E_INVALID_FORMAT;
4178 ret->ticks -= tz_hour * TICKS_PER_HOUR;
4179 ret->ticks -= tz_min * TICKS_PER_MIN;
4182 return S_OK;
4185 /**************************************************************************
4186 * WsDateTimeToFileTime [webservices.@]
4188 HRESULT WINAPI WsDateTimeToFileTime( const WS_DATETIME *dt, FILETIME *ft, WS_ERROR *error )
4190 unsigned __int64 ticks;
4192 TRACE( "%p %p %p\n", dt, ft, error );
4193 if (error) FIXME( "ignoring error parameter\n" );
4195 if (!dt || !ft) return E_INVALIDARG;
4197 if (dt->ticks < TICKS_1601_01_01) return WS_E_INVALID_FORMAT;
4198 ticks = dt->ticks - TICKS_1601_01_01;
4199 ft->dwHighDateTime = ticks >> 32;
4200 ft->dwLowDateTime = (DWORD)ticks;
4201 return S_OK;
4204 /**************************************************************************
4205 * WsFileTimeToDateTime [webservices.@]
4207 HRESULT WINAPI WsFileTimeToDateTime( const FILETIME *ft, WS_DATETIME *dt, WS_ERROR *error )
4209 unsigned __int64 ticks;
4211 TRACE( "%p %p %p\n", ft, dt, error );
4212 if (error) FIXME( "ignoring error parameter\n" );
4214 if (!dt || !ft) return E_INVALIDARG;
4216 ticks = ((unsigned __int64)ft->dwHighDateTime << 32) | ft->dwLowDateTime;
4217 if (ticks > MAX_UINT64 - TICKS_1601_01_01) return WS_E_NUMERIC_OVERFLOW;
4218 if (ticks + TICKS_1601_01_01 > TICKS_MAX) return WS_E_INVALID_FORMAT;
4219 dt->ticks = ticks + TICKS_1601_01_01;
4220 dt->format = WS_DATETIME_FORMAT_UTC;
4221 return S_OK;
4224 static BOOL find_attribute( struct reader *reader, const WS_XML_STRING *localname,
4225 const WS_XML_STRING *ns, ULONG *index )
4227 ULONG i;
4228 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
4230 if (!localname)
4232 *index = reader->current_attr;
4233 return TRUE;
4235 for (i = 0; i < elem->attributeCount; i++)
4237 const WS_XML_STRING *localname2 = elem->attributes[i]->localName;
4238 const WS_XML_STRING *ns2 = elem->attributes[i]->ns;
4240 if (!cmp_name( localname->bytes, localname->length, localname2->bytes, localname2->length ) &&
4241 !cmp_name( ns->bytes, ns->length, ns2->bytes, ns2->length ))
4243 *index = i;
4244 return TRUE;
4247 return FALSE;
4250 /**************************************************************************
4251 * WsFindAttribute [webservices.@]
4253 HRESULT WINAPI WsFindAttribute( WS_XML_READER *handle, const WS_XML_STRING *localname,
4254 const WS_XML_STRING *ns, BOOL required, ULONG *index,
4255 WS_ERROR *error )
4257 struct reader *reader = (struct reader *)handle;
4258 HRESULT hr = S_OK;
4260 TRACE( "%p %s %s %d %p %p\n", handle, debugstr_xmlstr(localname), debugstr_xmlstr(ns),
4261 required, index, error );
4262 if (error) FIXME( "ignoring error parameter\n" );
4264 if (!reader || !localname || !ns || !index) return E_INVALIDARG;
4266 EnterCriticalSection( &reader->cs );
4268 if (reader->magic != READER_MAGIC)
4270 LeaveCriticalSection( &reader->cs );
4271 return E_INVALIDARG;
4274 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) hr = WS_E_INVALID_OPERATION;
4275 else if (!find_attribute( reader, localname, ns, index ))
4277 if (required) hr = WS_E_INVALID_FORMAT;
4278 else
4280 *index = ~0u;
4281 hr = S_FALSE;
4285 LeaveCriticalSection( &reader->cs );
4286 TRACE( "returning %08x\n", hr );
4287 return hr;
4290 static HRESULT get_node_text( struct reader *reader, const WS_XML_TEXT **ret )
4292 WS_XML_TEXT_NODE *node = (WS_XML_TEXT_NODE *)&reader->current->hdr.node;
4293 *ret = node->text;
4294 return S_OK;
4297 static HRESULT get_attribute_text( struct reader *reader, ULONG index, const WS_XML_TEXT **ret )
4299 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
4300 *ret = elem->attributes[index]->value;
4301 return S_OK;
4304 static BOOL match_element( const struct node *node, const WS_XML_STRING *localname, const WS_XML_STRING *ns )
4306 const WS_XML_ELEMENT_NODE *elem = (const WS_XML_ELEMENT_NODE *)node;
4307 if (node_type( node ) != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
4308 return WsXmlStringEquals( localname, elem->localName, NULL ) == S_OK &&
4309 WsXmlStringEquals( ns, elem->ns, NULL ) == S_OK;
4312 static HRESULT read_next_node( struct reader *reader )
4314 if (reader->current == reader->last) return read_node( reader );
4315 if (move_to_child_node( &reader->current )) return S_OK;
4316 if (move_to_next_node( &reader->current )) return S_OK;
4317 if (!move_to_parent_node( &reader->current )) return WS_E_INVALID_FORMAT;
4318 if (move_to_next_node( &reader->current )) return S_OK;
4319 return WS_E_INVALID_FORMAT;
4322 struct reader_pos
4324 struct node *node;
4325 ULONG attr;
4328 static void save_reader_position( const struct reader *reader, struct reader_pos *pos )
4330 pos->node = reader->current;
4331 pos->attr = reader->current_attr;
4334 static void restore_reader_position( struct reader *reader, const struct reader_pos *pos )
4336 reader->current = pos->node;
4337 reader->current_attr = pos->attr;
4340 static HRESULT get_text( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
4341 const WS_XML_STRING *ns, const WS_XML_TEXT **ret, BOOL *found )
4343 switch (mapping)
4345 case WS_ATTRIBUTE_TYPE_MAPPING:
4347 ULONG i;
4348 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
4350 *found = FALSE;
4351 for (i = 0; i < elem->attributeCount; i++)
4353 const WS_XML_STRING *localname2 = elem->attributes[i]->localName;
4354 const WS_XML_STRING *ns2 = elem->attributes[i]->ns;
4356 if (cmp_name( localname->bytes, localname->length, localname2->bytes, localname2->length )) continue;
4357 if (!ns->length || !cmp_name( ns->bytes, ns->length, ns2->bytes, ns2->length ))
4359 *found = TRUE;
4360 break;
4363 if (!*found) return S_OK;
4364 return get_attribute_text( reader, i, ret );
4366 case WS_ELEMENT_TYPE_MAPPING:
4367 case WS_ELEMENT_CONTENT_TYPE_MAPPING:
4368 case WS_ANY_ELEMENT_TYPE_MAPPING:
4370 *found = TRUE;
4371 if (localname)
4373 struct reader_pos pos;
4374 HRESULT hr;
4376 if (!match_element( reader->current, localname, ns ))
4378 *found = FALSE;
4379 return S_OK;
4381 save_reader_position( reader, &pos );
4382 if ((hr = read_next_node( reader )) != S_OK) return hr;
4383 if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT)
4385 restore_reader_position( reader, &pos );
4386 *found = FALSE;
4387 return S_OK;
4390 if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT)
4392 *found = FALSE;
4393 return S_OK;
4395 return get_node_text( reader, ret );
4397 default:
4398 FIXME( "mapping %u not supported\n", mapping );
4399 return E_NOTIMPL;
4403 static HRESULT text_to_bool( const WS_XML_TEXT *text, BOOL *val )
4405 HRESULT hr;
4407 switch (text->textType)
4409 case WS_XML_TEXT_TYPE_UTF8:
4411 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4412 hr = str_to_bool( text_utf8->value.bytes, text_utf8->value.length, val );
4413 break;
4415 case WS_XML_TEXT_TYPE_BOOL:
4417 const WS_XML_BOOL_TEXT *text_bool = (const WS_XML_BOOL_TEXT *)text;
4418 *val = text_bool->value;
4419 hr = S_OK;
4420 break;
4422 default:
4423 FIXME( "unhandled text type %u\n", text->textType );
4424 return E_NOTIMPL;
4427 return hr;
4430 static HRESULT read_type_bool( struct reader *reader, WS_TYPE_MAPPING mapping,
4431 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4432 const WS_BOOL_DESCRIPTION *desc, WS_READ_OPTION option,
4433 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4435 const WS_XML_TEXT *text;
4436 HRESULT hr;
4437 BOOL val = FALSE;
4439 if (desc)
4441 FIXME( "description not supported\n" );
4442 return E_NOTIMPL;
4444 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4445 if (*found && (hr = text_to_bool( text, &val )) != S_OK) return hr;
4447 switch (option)
4449 case WS_READ_REQUIRED_VALUE:
4450 if (!*found) return WS_E_INVALID_FORMAT;
4451 /* fall through */
4453 case WS_READ_NILLABLE_VALUE:
4454 if (size != sizeof(val)) return E_INVALIDARG;
4455 *(BOOL *)ret = val;
4456 break;
4458 case WS_READ_REQUIRED_POINTER:
4459 if (!*found) return WS_E_INVALID_FORMAT;
4460 /* fall through */
4462 case WS_READ_OPTIONAL_POINTER:
4463 case WS_READ_NILLABLE_POINTER:
4465 BOOL *heap_val = NULL;
4466 if (size != sizeof(heap_val)) return E_INVALIDARG;
4467 if (*found)
4469 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4470 *heap_val = val;
4472 *(BOOL **)ret = heap_val;
4473 break;
4475 default:
4476 FIXME( "read option %u not supported\n", option );
4477 return E_NOTIMPL;
4480 return S_OK;
4483 static HRESULT text_to_int8( const WS_XML_TEXT *text, INT64 *val )
4485 HRESULT hr;
4487 switch (text->textType)
4489 case WS_XML_TEXT_TYPE_UTF8:
4491 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4492 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT8, MAX_INT8, val );
4493 break;
4495 case WS_XML_TEXT_TYPE_INT32:
4497 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4498 assert( text_int32->value >= MIN_INT8 );
4499 assert( text_int32->value <= MAX_INT8 );
4500 *val = text_int32->value;
4501 hr = S_OK;
4502 break;
4504 default:
4505 FIXME( "unhandled text type %u\n", text->textType );
4506 return E_NOTIMPL;
4509 return hr;
4512 static HRESULT read_type_int8( struct reader *reader, WS_TYPE_MAPPING mapping,
4513 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4514 const WS_INT8_DESCRIPTION *desc, WS_READ_OPTION option,
4515 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4517 const WS_XML_TEXT *text;
4518 HRESULT hr;
4519 INT64 val = 0;
4521 if (desc)
4523 FIXME( "description not supported\n" );
4524 return E_NOTIMPL;
4526 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4527 if (*found && (hr = text_to_int8( text, &val )) != S_OK) return hr;
4529 switch (option)
4531 case WS_READ_REQUIRED_VALUE:
4532 if (!*found) return WS_E_INVALID_FORMAT;
4533 /* fall through */
4535 case WS_READ_NILLABLE_VALUE:
4536 if (size != sizeof(INT8)) return E_INVALIDARG;
4537 *(INT8 *)ret = val;
4538 break;
4540 case WS_READ_REQUIRED_POINTER:
4541 if (!*found) return WS_E_INVALID_FORMAT;
4542 /* fall through */
4544 case WS_READ_OPTIONAL_POINTER:
4545 case WS_READ_NILLABLE_POINTER:
4547 INT8 *heap_val = NULL;
4548 if (size != sizeof(heap_val)) return E_INVALIDARG;
4549 if (*found)
4551 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4552 *heap_val = val;
4554 *(INT8 **)ret = heap_val;
4555 break;
4557 default:
4558 FIXME( "read option %u not supported\n", option );
4559 return E_NOTIMPL;
4562 return S_OK;
4565 static HRESULT text_to_int16( const WS_XML_TEXT *text, INT64 *val )
4567 HRESULT hr;
4569 switch (text->textType)
4571 case WS_XML_TEXT_TYPE_UTF8:
4573 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4574 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT16, MAX_INT16, val );
4575 break;
4577 case WS_XML_TEXT_TYPE_INT32:
4579 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4580 assert( text_int32->value >= MIN_INT16 );
4581 assert( text_int32->value <= MAX_INT16 );
4582 *val = text_int32->value;
4583 hr = S_OK;
4584 break;
4586 default:
4587 FIXME( "unhandled text type %u\n", text->textType );
4588 return E_NOTIMPL;
4591 return hr;
4594 static HRESULT read_type_int16( struct reader *reader, WS_TYPE_MAPPING mapping,
4595 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4596 const WS_INT16_DESCRIPTION *desc, WS_READ_OPTION option,
4597 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4599 const WS_XML_TEXT *text;
4600 HRESULT hr;
4601 INT64 val = 0;
4603 if (desc)
4605 FIXME( "description not supported\n" );
4606 return E_NOTIMPL;
4608 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4609 if (*found && (hr = text_to_int16( text, &val )) != S_OK) return hr;
4611 switch (option)
4613 case WS_READ_REQUIRED_VALUE:
4614 if (!*found) return WS_E_INVALID_FORMAT;
4615 /* fall through */
4617 case WS_READ_NILLABLE_VALUE:
4618 if (size != sizeof(INT16)) return E_INVALIDARG;
4619 *(INT16 *)ret = val;
4620 break;
4622 case WS_READ_REQUIRED_POINTER:
4623 if (!*found) return WS_E_INVALID_FORMAT;
4624 /* fall through */
4626 case WS_READ_OPTIONAL_POINTER:
4627 case WS_READ_NILLABLE_POINTER:
4629 INT16 *heap_val = NULL;
4630 if (size != sizeof(heap_val)) return E_INVALIDARG;
4631 if (*found)
4633 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4634 *heap_val = val;
4636 *(INT16 **)ret = heap_val;
4637 break;
4639 default:
4640 FIXME( "read option %u not supported\n", option );
4641 return E_NOTIMPL;
4644 return S_OK;
4647 static HRESULT text_to_int32( const WS_XML_TEXT *text, INT64 *val )
4649 HRESULT hr;
4651 switch (text->textType)
4653 case WS_XML_TEXT_TYPE_UTF8:
4655 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4656 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT32, MAX_INT32, val );
4657 break;
4659 case WS_XML_TEXT_TYPE_INT32:
4661 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4662 *val = text_int32->value;
4663 hr = S_OK;
4664 break;
4666 default:
4667 FIXME( "unhandled text type %u\n", text->textType );
4668 return E_NOTIMPL;
4671 return hr;
4674 static HRESULT read_type_int32( struct reader *reader, WS_TYPE_MAPPING mapping,
4675 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4676 const WS_INT32_DESCRIPTION *desc, WS_READ_OPTION option,
4677 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4679 const WS_XML_TEXT *text;
4680 HRESULT hr;
4681 INT64 val = 0;
4683 if (desc)
4685 FIXME( "description not supported\n" );
4686 return E_NOTIMPL;
4688 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4689 if (*found && (hr = text_to_int32( text, &val )) != S_OK) return hr;
4691 switch (option)
4693 case WS_READ_REQUIRED_VALUE:
4694 if (!*found) return WS_E_INVALID_FORMAT;
4695 /* fall through */
4697 case WS_READ_NILLABLE_VALUE:
4698 if (size != sizeof(INT32)) return E_INVALIDARG;
4699 *(INT32 *)ret = val;
4700 break;
4702 case WS_READ_REQUIRED_POINTER:
4703 if (!*found) return WS_E_INVALID_FORMAT;
4704 /* fall through */
4706 case WS_READ_OPTIONAL_POINTER:
4707 case WS_READ_NILLABLE_POINTER:
4709 INT32 *heap_val = NULL;
4710 if (size != sizeof(heap_val)) return E_INVALIDARG;
4711 if (*found)
4713 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4714 *heap_val = val;
4716 *(INT32 **)ret = heap_val;
4717 break;
4719 default:
4720 FIXME( "read option %u not supported\n", option );
4721 return E_NOTIMPL;
4724 return S_OK;
4727 static HRESULT text_to_int64( const WS_XML_TEXT *text, INT64 *val )
4729 HRESULT hr;
4731 switch (text->textType)
4733 case WS_XML_TEXT_TYPE_UTF8:
4735 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4736 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT64, MAX_INT64, val );
4737 break;
4739 case WS_XML_TEXT_TYPE_INT64:
4741 const WS_XML_INT64_TEXT *text_int64 = (const WS_XML_INT64_TEXT *)text;
4742 *val = text_int64->value;
4743 hr = S_OK;
4744 break;
4746 default:
4747 FIXME( "unhandled text type %u\n", text->textType );
4748 return E_NOTIMPL;
4751 return hr;
4754 static HRESULT read_type_int64( struct reader *reader, WS_TYPE_MAPPING mapping,
4755 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4756 const WS_INT64_DESCRIPTION *desc, WS_READ_OPTION option,
4757 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4759 const WS_XML_TEXT *text;
4760 HRESULT hr;
4761 INT64 val = 0;
4763 if (desc)
4765 FIXME( "description not supported\n" );
4766 return E_NOTIMPL;
4768 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4769 if (*found && (hr = text_to_int64( text, &val )) != S_OK) return hr;
4771 switch (option)
4773 case WS_READ_REQUIRED_VALUE:
4774 if (!*found) return WS_E_INVALID_FORMAT;
4775 /* fall through */
4777 case WS_READ_NILLABLE_VALUE:
4778 if (size != sizeof(val)) return E_INVALIDARG;
4779 *(INT64 *)ret = val;
4780 break;
4782 case WS_READ_REQUIRED_POINTER:
4783 if (!*found) return WS_E_INVALID_FORMAT;
4784 /* fall through */
4786 case WS_READ_OPTIONAL_POINTER:
4787 case WS_READ_NILLABLE_POINTER:
4789 INT64 *heap_val = NULL;
4790 if (size != sizeof(heap_val)) return E_INVALIDARG;
4791 if (*found)
4793 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4794 *heap_val = val;
4796 *(INT64 **)ret = heap_val;
4797 break;
4799 default:
4800 FIXME( "read option %u not supported\n", option );
4801 return E_NOTIMPL;
4804 return S_OK;
4807 static HRESULT text_to_uint8( const WS_XML_TEXT *text, UINT64 *val )
4809 HRESULT hr;
4811 switch (text->textType)
4813 case WS_XML_TEXT_TYPE_UTF8:
4815 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4816 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT8, val );
4817 break;
4819 case WS_XML_TEXT_TYPE_UINT64:
4821 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
4822 assert( text_uint64->value <= MAX_UINT8 );
4823 *val = text_uint64->value;
4824 hr = S_OK;
4825 break;
4827 default:
4828 FIXME( "unhandled text type %u\n", text->textType );
4829 return E_NOTIMPL;
4832 return hr;
4835 static HRESULT read_type_uint8( struct reader *reader, WS_TYPE_MAPPING mapping,
4836 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4837 const WS_UINT8_DESCRIPTION *desc, WS_READ_OPTION option,
4838 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4840 const WS_XML_TEXT *text;
4841 HRESULT hr;
4842 UINT64 val = 0;
4844 if (desc)
4846 FIXME( "description not supported\n" );
4847 return E_NOTIMPL;
4849 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4850 if (*found && (hr = text_to_uint8( text, &val )) != S_OK) return hr;
4852 switch (option)
4854 case WS_READ_REQUIRED_VALUE:
4855 if (!*found) return WS_E_INVALID_FORMAT;
4856 /* fall through */
4858 case WS_READ_NILLABLE_VALUE:
4859 if (size != sizeof(UINT8)) return E_INVALIDARG;
4860 *(UINT8 *)ret = val;
4861 break;
4863 case WS_READ_REQUIRED_POINTER:
4864 if (!*found) return WS_E_INVALID_FORMAT;
4865 /* fall through */
4867 case WS_READ_OPTIONAL_POINTER:
4868 case WS_READ_NILLABLE_POINTER:
4870 UINT8 *heap_val = NULL;
4871 if (size != sizeof(heap_val)) return E_INVALIDARG;
4872 if (*found)
4874 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4875 *heap_val = val;
4877 *(UINT8 **)ret = heap_val;
4878 break;
4880 default:
4881 FIXME( "read option %u not supported\n", option );
4882 return E_NOTIMPL;
4885 return S_OK;
4888 static HRESULT text_to_uint16( const WS_XML_TEXT *text, UINT64 *val )
4890 HRESULT hr;
4892 switch (text->textType)
4894 case WS_XML_TEXT_TYPE_UTF8:
4896 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4897 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT16, val );
4898 break;
4900 case WS_XML_TEXT_TYPE_INT32:
4902 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4903 assert( text_int32->value >= 0 );
4904 assert( text_int32->value <= MAX_UINT16 );
4905 *val = text_int32->value;
4906 hr = S_OK;
4907 break;
4909 case WS_XML_TEXT_TYPE_UINT64:
4911 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
4912 assert( text_uint64->value <= MAX_UINT16 );
4913 *val = text_uint64->value;
4914 hr = S_OK;
4915 break;
4917 default:
4918 FIXME( "unhandled text type %u\n", text->textType );
4919 return E_NOTIMPL;
4922 return hr;
4925 static HRESULT read_type_uint16( struct reader *reader, WS_TYPE_MAPPING mapping,
4926 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4927 const WS_UINT16_DESCRIPTION *desc, WS_READ_OPTION option,
4928 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4930 const WS_XML_TEXT *text;
4931 HRESULT hr;
4932 UINT64 val = 0;
4934 if (desc)
4936 FIXME( "description not supported\n" );
4937 return E_NOTIMPL;
4939 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4940 if (*found && (hr = text_to_uint16( text, &val )) != S_OK) return hr;
4942 switch (option)
4944 case WS_READ_REQUIRED_VALUE:
4945 if (!*found) return WS_E_INVALID_FORMAT;
4946 /* fall through */
4948 case WS_READ_NILLABLE_VALUE:
4949 if (size != sizeof(UINT16)) return E_INVALIDARG;
4950 *(UINT16 *)ret = val;
4951 break;
4953 case WS_READ_REQUIRED_POINTER:
4954 if (!*found) return WS_E_INVALID_FORMAT;
4955 /* fall through */
4957 case WS_READ_OPTIONAL_POINTER:
4958 case WS_READ_NILLABLE_POINTER:
4960 UINT16 *heap_val = NULL;
4961 if (size != sizeof(heap_val)) return E_INVALIDARG;
4962 if (*found)
4964 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4965 *heap_val = val;
4967 *(UINT16 **)ret = heap_val;
4968 break;
4970 default:
4971 FIXME( "read option %u not supported\n", option );
4972 return E_NOTIMPL;
4975 return S_OK;
4978 static HRESULT text_to_uint32( const WS_XML_TEXT *text, UINT64 *val )
4980 HRESULT hr;
4982 switch (text->textType)
4984 case WS_XML_TEXT_TYPE_UTF8:
4986 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4987 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT32, val );
4988 break;
4990 case WS_XML_TEXT_TYPE_INT32:
4992 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4993 assert( text_int32->value >= 0 );
4994 *val = text_int32->value;
4995 hr = S_OK;
4996 break;
4998 case WS_XML_TEXT_TYPE_UINT64:
5000 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
5001 assert( text_uint64->value <= MAX_UINT32 );
5002 *val = text_uint64->value;
5003 hr = S_OK;
5004 break;
5006 default:
5007 FIXME( "unhandled text type %u\n", text->textType );
5008 return E_NOTIMPL;
5011 return hr;
5014 static HRESULT read_type_uint32( struct reader *reader, WS_TYPE_MAPPING mapping,
5015 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5016 const WS_UINT32_DESCRIPTION *desc, WS_READ_OPTION option,
5017 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5019 const WS_XML_TEXT *text;
5020 HRESULT hr;
5021 UINT64 val = 0;
5023 if (desc)
5025 FIXME( "description not supported\n" );
5026 return E_NOTIMPL;
5028 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5029 if (*found && (hr = text_to_uint32( text, &val )) != S_OK) return hr;
5031 switch (option)
5033 case WS_READ_REQUIRED_VALUE:
5034 if (!*found) return WS_E_INVALID_FORMAT;
5035 /* fall through */
5037 case WS_READ_NILLABLE_VALUE:
5038 if (size != sizeof(UINT32)) return E_INVALIDARG;
5039 *(UINT32 *)ret = val;
5040 break;
5042 case WS_READ_REQUIRED_POINTER:
5043 if (!*found) return WS_E_INVALID_FORMAT;
5044 /* fall through */
5046 case WS_READ_OPTIONAL_POINTER:
5047 case WS_READ_NILLABLE_POINTER:
5049 UINT32 *heap_val = NULL;
5050 if (size != sizeof(heap_val)) return E_INVALIDARG;
5051 if (*found)
5053 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5054 *heap_val = val;
5056 *(UINT32 **)ret = heap_val;
5057 break;
5059 default:
5060 FIXME( "read option %u not supported\n", option );
5061 return E_NOTIMPL;
5064 return S_OK;
5067 static HRESULT text_to_uint64( const WS_XML_TEXT *text, UINT64 *val )
5069 HRESULT hr;
5071 switch (text->textType)
5073 case WS_XML_TEXT_TYPE_UTF8:
5075 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5076 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT64, val );
5077 break;
5079 case WS_XML_TEXT_TYPE_INT32:
5081 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
5082 *val = text_int32->value;
5083 hr = S_OK;
5084 break;
5086 case WS_XML_TEXT_TYPE_INT64:
5088 const WS_XML_INT64_TEXT *text_int64 = (const WS_XML_INT64_TEXT *)text;
5089 *val = text_int64->value;
5090 hr = S_OK;
5091 break;
5093 case WS_XML_TEXT_TYPE_UINT64:
5095 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
5096 *val = text_uint64->value;
5097 hr = S_OK;
5098 break;
5100 default:
5101 FIXME( "unhandled text type %u\n", text->textType );
5102 return E_NOTIMPL;
5105 return hr;
5108 static HRESULT read_type_uint64( struct reader *reader, WS_TYPE_MAPPING mapping,
5109 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5110 const WS_UINT64_DESCRIPTION *desc, WS_READ_OPTION option,
5111 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5113 const WS_XML_TEXT *text;
5114 HRESULT hr;
5115 UINT64 val = 0;
5117 if (desc)
5119 FIXME( "description not supported\n" );
5120 return E_NOTIMPL;
5122 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5123 if (*found && (hr = text_to_uint64( text, &val )) != S_OK) return hr;
5125 switch (option)
5127 case WS_READ_REQUIRED_VALUE:
5128 if (!*found) return WS_E_INVALID_FORMAT;
5129 /* fall through */
5131 case WS_READ_NILLABLE_VALUE:
5132 if (size != sizeof(val)) return E_INVALIDARG;
5133 *(UINT64 *)ret = val;
5134 break;
5136 case WS_READ_REQUIRED_POINTER:
5137 if (!*found) return WS_E_INVALID_FORMAT;
5138 /* fall through */
5140 case WS_READ_OPTIONAL_POINTER:
5141 case WS_READ_NILLABLE_POINTER:
5143 UINT64 *heap_val = NULL;
5144 if (size != sizeof(heap_val)) return E_INVALIDARG;
5145 if (*found)
5147 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5148 *heap_val = val;
5150 *(UINT64 **)ret = heap_val;
5151 break;
5153 default:
5154 FIXME( "read option %u not supported\n", option );
5155 return E_NOTIMPL;
5158 return S_OK;
5161 static HRESULT text_to_float( const WS_XML_TEXT *text, float *val )
5163 HRESULT hr;
5165 switch (text->textType)
5167 case WS_XML_TEXT_TYPE_UTF8:
5169 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5170 hr = str_to_float( text_utf8->value.bytes, text_utf8->value.length, val );
5171 break;
5173 case WS_XML_TEXT_TYPE_FLOAT:
5175 const WS_XML_FLOAT_TEXT *text_float = (const WS_XML_FLOAT_TEXT *)text;
5176 *val = text_float->value;
5177 hr = S_OK;
5178 break;
5180 default:
5181 FIXME( "unhandled text type %u\n", text->textType );
5182 return E_NOTIMPL;
5185 return hr;
5188 static HRESULT read_type_float( struct reader *reader, WS_TYPE_MAPPING mapping,
5189 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5190 const WS_FLOAT_DESCRIPTION *desc, WS_READ_OPTION option,
5191 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5193 const WS_XML_TEXT *text;
5194 HRESULT hr;
5195 float val = 0.0;
5197 if (desc) FIXME( "ignoring description\n" );
5199 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5200 if (*found && (hr = text_to_float( text, &val )) != S_OK) return hr;
5202 switch (option)
5204 case WS_READ_REQUIRED_VALUE:
5205 if (!*found) return WS_E_INVALID_FORMAT;
5206 /* fall through */
5208 case WS_READ_NILLABLE_VALUE:
5209 if (size != sizeof(val)) return E_INVALIDARG;
5210 *(float *)ret = val;
5211 break;
5213 case WS_READ_REQUIRED_POINTER:
5214 if (!*found) return WS_E_INVALID_FORMAT;
5215 /* fall through */
5217 case WS_READ_OPTIONAL_POINTER:
5218 case WS_READ_NILLABLE_POINTER:
5220 float *heap_val = NULL;
5221 if (size != sizeof(heap_val)) return E_INVALIDARG;
5222 if (*found)
5224 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5225 *heap_val = val;
5227 *(float **)ret = heap_val;
5228 break;
5230 default:
5231 FIXME( "read option %u not supported\n", option );
5232 return E_NOTIMPL;
5235 return S_OK;
5238 static HRESULT text_to_double( const WS_XML_TEXT *text, double *val )
5240 HRESULT hr;
5242 switch (text->textType)
5244 case WS_XML_TEXT_TYPE_UTF8:
5246 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5247 hr = str_to_double( text_utf8->value.bytes, text_utf8->value.length, val );
5248 break;
5250 case WS_XML_TEXT_TYPE_DOUBLE:
5252 const WS_XML_DOUBLE_TEXT *text_double = (const WS_XML_DOUBLE_TEXT *)text;
5253 *val = text_double->value;
5254 hr = S_OK;
5255 break;
5257 default:
5258 FIXME( "unhandled text type %u\n", text->textType );
5259 return E_NOTIMPL;
5262 return hr;
5265 static HRESULT read_type_double( struct reader *reader, WS_TYPE_MAPPING mapping,
5266 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5267 const WS_DOUBLE_DESCRIPTION *desc, WS_READ_OPTION option,
5268 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5270 const WS_XML_TEXT *text;
5271 HRESULT hr;
5272 double val = 0.0;
5274 if (desc) FIXME( "ignoring description\n" );
5276 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5277 if (*found && (hr = text_to_double( text, &val )) != S_OK) return hr;
5279 switch (option)
5281 case WS_READ_REQUIRED_VALUE:
5282 if (!*found) return WS_E_INVALID_FORMAT;
5283 /* fall through */
5285 case WS_READ_NILLABLE_VALUE:
5286 if (size != sizeof(val)) return E_INVALIDARG;
5287 *(double *)ret = val;
5288 break;
5290 case WS_READ_REQUIRED_POINTER:
5291 if (!*found) return WS_E_INVALID_FORMAT;
5292 /* fall through */
5294 case WS_READ_OPTIONAL_POINTER:
5295 case WS_READ_NILLABLE_POINTER:
5297 double *heap_val = NULL;
5298 if (size != sizeof(heap_val)) return E_INVALIDARG;
5299 if (*found)
5301 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5302 *heap_val = val;
5304 *(double **)ret = heap_val;
5305 break;
5307 default:
5308 FIXME( "read option %u not supported\n", option );
5309 return E_NOTIMPL;
5312 return S_OK;
5315 static HRESULT text_to_wsz( const WS_XML_TEXT *text, WS_HEAP *heap, WCHAR **ret )
5317 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5318 int len;
5320 assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5321 len = MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, NULL, 0 );
5322 if (!(*ret = ws_alloc( heap, (len + 1) * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
5323 MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, *ret, len );
5324 (*ret)[len] = 0;
5325 return S_OK;
5328 static HRESULT read_type_wsz( struct reader *reader, WS_TYPE_MAPPING mapping,
5329 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5330 const WS_WSZ_DESCRIPTION *desc, WS_READ_OPTION option,
5331 WS_HEAP *heap, WCHAR **ret, ULONG size, BOOL *found )
5333 const WS_XML_TEXT *text;
5334 HRESULT hr;
5335 WCHAR *str = NULL;
5337 if (desc)
5339 FIXME( "description not supported\n" );
5340 return E_NOTIMPL;
5342 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5343 if (*found && (hr = text_to_wsz( text, heap, &str )) != S_OK) return hr;
5345 switch (option)
5347 case WS_READ_REQUIRED_POINTER:
5348 if (!str && !(str = ws_alloc_zero( heap, sizeof(*str) ))) return WS_E_QUOTA_EXCEEDED;
5349 /* fall through */
5351 case WS_READ_OPTIONAL_POINTER:
5352 case WS_READ_NILLABLE_POINTER:
5353 if (size != sizeof(str)) return E_INVALIDARG;
5354 *ret = str;
5355 break;
5357 default:
5358 FIXME( "read option %u not supported\n", option );
5359 return E_NOTIMPL;
5362 return S_OK;
5365 static HRESULT get_enum_value( const WS_XML_TEXT *text, const WS_ENUM_DESCRIPTION *desc, int *ret )
5367 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5368 ULONG i;
5370 assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5371 for (i = 0; i < desc->valueCount; i++)
5373 if (WsXmlStringEquals( &utf8->value, desc->values[i].name, NULL ) == S_OK)
5375 *ret = desc->values[i].value;
5376 return S_OK;
5379 return WS_E_INVALID_FORMAT;
5382 static HRESULT read_type_enum( struct reader *reader, WS_TYPE_MAPPING mapping,
5383 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5384 const WS_ENUM_DESCRIPTION *desc, WS_READ_OPTION option,
5385 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5387 const WS_XML_TEXT *text;
5388 HRESULT hr;
5389 int val = 0;
5391 if (!desc) return E_INVALIDARG;
5393 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5394 if (*found && (hr = get_enum_value( text, desc, &val )) != S_OK) return hr;
5396 switch (option)
5398 case WS_READ_REQUIRED_VALUE:
5399 if (!*found) return WS_E_INVALID_FORMAT;
5400 /* fall through */
5402 case WS_READ_NILLABLE_VALUE:
5403 if (size != sizeof(val)) return E_INVALIDARG;
5404 *(int *)ret = val;
5405 break;
5407 case WS_READ_REQUIRED_POINTER:
5408 if (!*found) return WS_E_INVALID_FORMAT;
5409 /* fall through */
5411 case WS_READ_OPTIONAL_POINTER:
5412 case WS_READ_NILLABLE_POINTER:
5414 int *heap_val = NULL;
5415 if (size != sizeof(heap_val)) return E_INVALIDARG;
5416 if (*found)
5418 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5419 *heap_val = val;
5421 *(int **)ret = heap_val;
5422 break;
5424 default:
5425 FIXME( "read option %u not supported\n", option );
5426 return E_NOTIMPL;
5429 return S_OK;
5432 static HRESULT text_to_datetime( const WS_XML_TEXT *text, WS_DATETIME *val )
5434 HRESULT hr;
5436 switch (text->textType)
5438 case WS_XML_TEXT_TYPE_UTF8:
5440 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5441 hr = str_to_datetime( text_utf8->value.bytes, text_utf8->value.length, val );
5442 break;
5444 case WS_XML_TEXT_TYPE_DATETIME:
5446 const WS_XML_DATETIME_TEXT *text_datetime = (const WS_XML_DATETIME_TEXT *)text;
5447 *val = text_datetime->value;
5448 hr = S_OK;
5449 break;
5451 default:
5452 FIXME( "unhandled text type %u\n", text->textType );
5453 return E_NOTIMPL;
5456 return hr;
5459 static HRESULT read_type_datetime( struct reader *reader, WS_TYPE_MAPPING mapping,
5460 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5461 const WS_DATETIME_DESCRIPTION *desc, WS_READ_OPTION option,
5462 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5464 const WS_XML_TEXT *text;
5465 HRESULT hr;
5466 WS_DATETIME val = {0, WS_DATETIME_FORMAT_UTC};
5468 if (desc) FIXME( "ignoring description\n" );
5470 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5471 if (*found && (hr = text_to_datetime( text, &val )) != S_OK) return hr;
5473 switch (option)
5475 case WS_READ_REQUIRED_VALUE:
5476 if (!*found) return WS_E_INVALID_FORMAT;
5477 /* fall through */
5479 case WS_READ_NILLABLE_VALUE:
5480 if (size != sizeof(val)) return E_INVALIDARG;
5481 *(WS_DATETIME *)ret = val;
5482 break;
5484 case WS_READ_REQUIRED_POINTER:
5485 if (!*found) return WS_E_INVALID_FORMAT;
5486 /* fall through */
5488 case WS_READ_OPTIONAL_POINTER:
5489 case WS_READ_NILLABLE_POINTER:
5491 WS_DATETIME *heap_val = NULL;
5492 if (size != sizeof(heap_val)) return E_INVALIDARG;
5493 if (*found)
5495 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5496 *heap_val = val;
5498 *(WS_DATETIME **)ret = heap_val;
5499 break;
5501 default:
5502 FIXME( "read option %u not supported\n", option );
5503 return E_NOTIMPL;
5506 return S_OK;
5509 static HRESULT text_to_guid( const WS_XML_TEXT *text, GUID *val )
5511 HRESULT hr;
5513 switch (text->textType)
5515 case WS_XML_TEXT_TYPE_UTF8:
5517 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5518 hr = str_to_guid( text_utf8->value.bytes, text_utf8->value.length, val );
5519 break;
5521 case WS_XML_TEXT_TYPE_GUID:
5523 const WS_XML_GUID_TEXT *text_guid = (const WS_XML_GUID_TEXT *)text;
5524 *val = text_guid->value;
5525 hr = S_OK;
5526 break;
5528 default:
5529 FIXME( "unhandled text type %u\n", text->textType );
5530 return E_NOTIMPL;
5533 return hr;
5536 static HRESULT read_type_guid( struct reader *reader, WS_TYPE_MAPPING mapping,
5537 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5538 const WS_GUID_DESCRIPTION *desc, WS_READ_OPTION option,
5539 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5541 const WS_XML_TEXT *text;
5542 GUID val = {0};
5543 HRESULT hr;
5545 if (desc) FIXME( "ignoring description\n" );
5547 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5548 if (*found && (hr = text_to_guid( text, &val )) != S_OK) return hr;
5550 switch (option)
5552 case WS_READ_REQUIRED_VALUE:
5553 if (!*found) return WS_E_INVALID_FORMAT;
5554 /* fall through */
5556 case WS_READ_NILLABLE_VALUE:
5557 if (size != sizeof(val)) return E_INVALIDARG;
5558 *(GUID *)ret = val;
5559 break;
5561 case WS_READ_REQUIRED_POINTER:
5562 if (!*found) return WS_E_INVALID_FORMAT;
5563 /* fall through */
5565 case WS_READ_OPTIONAL_POINTER:
5566 case WS_READ_NILLABLE_POINTER:
5568 GUID *heap_val = NULL;
5569 if (size != sizeof(heap_val)) return E_INVALIDARG;
5570 if (*found)
5572 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5573 *heap_val = val;
5575 *(GUID **)ret = heap_val;
5576 break;
5578 default:
5579 FIXME( "read option %u not supported\n", option );
5580 return E_NOTIMPL;
5583 return S_OK;
5586 static HRESULT text_to_unique_id( const WS_XML_TEXT *text, WS_HEAP *heap, WS_UNIQUE_ID *val )
5588 HRESULT hr;
5590 switch (text->textType)
5592 case WS_XML_TEXT_TYPE_UTF8:
5594 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5595 hr = str_to_unique_id( text_utf8->value.bytes, text_utf8->value.length, heap, val );
5596 break;
5598 case WS_XML_TEXT_TYPE_UNIQUE_ID:
5600 const WS_XML_UNIQUE_ID_TEXT *text_unique_id = (const WS_XML_UNIQUE_ID_TEXT *)text;
5601 val->guid = text_unique_id->value;
5602 val->uri.length = 0;
5603 val->uri.chars = NULL;
5604 hr = S_OK;
5605 break;
5607 default:
5608 FIXME( "unhandled text type %u\n", text->textType );
5609 return E_NOTIMPL;
5612 return hr;
5615 static HRESULT read_type_unique_id( struct reader *reader, WS_TYPE_MAPPING mapping,
5616 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5617 const WS_UNIQUE_ID_DESCRIPTION *desc, WS_READ_OPTION option,
5618 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5620 const WS_XML_TEXT *text;
5621 WS_UNIQUE_ID val = {{0}};
5622 HRESULT hr;
5624 if (desc) FIXME( "ignoring description\n" );
5626 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5627 if (*found && (hr = text_to_unique_id( text, heap, &val )) != S_OK) return hr;
5629 switch (option)
5631 case WS_READ_REQUIRED_VALUE:
5632 if (!*found) return WS_E_INVALID_FORMAT;
5633 /* fall through */
5635 case WS_READ_NILLABLE_VALUE:
5636 if (size != sizeof(val)) return E_INVALIDARG;
5637 *(WS_UNIQUE_ID *)ret = val;
5638 break;
5640 case WS_READ_REQUIRED_POINTER:
5641 if (!*found) return WS_E_INVALID_FORMAT;
5642 /* fall through */
5644 case WS_READ_OPTIONAL_POINTER:
5645 case WS_READ_NILLABLE_POINTER:
5647 WS_UNIQUE_ID *heap_val = NULL;
5648 if (size != sizeof(heap_val)) return E_INVALIDARG;
5649 if (*found)
5651 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5652 *heap_val = val;
5654 *(WS_UNIQUE_ID **)ret = heap_val;
5655 break;
5657 default:
5658 FIXME( "read option %u not supported\n", option );
5659 return E_NOTIMPL;
5662 return S_OK;
5665 static HRESULT text_to_string( const WS_XML_TEXT *text, WS_HEAP *heap, WS_STRING *val )
5667 HRESULT hr;
5669 switch (text->textType)
5671 case WS_XML_TEXT_TYPE_UTF8:
5673 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5674 hr = str_to_string( text_utf8->value.bytes, text_utf8->value.length, heap, val );
5675 break;
5677 case WS_XML_TEXT_TYPE_UTF16:
5679 const WS_XML_UTF16_TEXT *text_utf16 = (const WS_XML_UTF16_TEXT *)text;
5680 if (!(val->chars = ws_alloc( heap, text_utf16->byteCount ))) return WS_E_QUOTA_EXCEEDED;
5681 memcpy( val->chars, text_utf16->bytes, text_utf16->byteCount );
5682 val->length = text_utf16->byteCount / sizeof(WCHAR);
5683 hr = S_OK;
5684 break;
5686 default:
5687 FIXME( "unhandled text type %u\n", text->textType );
5688 return E_NOTIMPL;
5691 return hr;
5694 static HRESULT read_type_string( struct reader *reader, WS_TYPE_MAPPING mapping,
5695 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5696 const WS_STRING_DESCRIPTION *desc, WS_READ_OPTION option,
5697 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5699 const WS_XML_TEXT *text;
5700 WS_STRING val = {0};
5701 HRESULT hr;
5703 if (desc) FIXME( "ignoring description\n" );
5705 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5706 if (*found && (hr = text_to_string( text, heap, &val )) != S_OK) return hr;
5708 switch (option)
5710 case WS_READ_REQUIRED_VALUE:
5711 case WS_READ_NILLABLE_VALUE:
5712 if (size != sizeof(val)) return E_INVALIDARG;
5713 *(WS_STRING *)ret = val;
5714 break;
5716 case WS_READ_REQUIRED_POINTER:
5718 WS_STRING *heap_val;
5719 if (size != sizeof(heap_val)) return E_INVALIDARG;
5720 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5721 *heap_val = val;
5722 *(WS_STRING **)ret = heap_val;
5723 break;
5725 case WS_READ_OPTIONAL_POINTER:
5726 case WS_READ_NILLABLE_POINTER:
5728 WS_STRING *heap_val = NULL;
5729 if (size != sizeof(heap_val)) return E_INVALIDARG;
5730 if (*found)
5732 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5733 *heap_val = val;
5735 *(WS_STRING **)ret = heap_val;
5736 break;
5738 default:
5739 FIXME( "read option %u not supported\n", option );
5740 return E_NOTIMPL;
5743 return S_OK;
5746 static HRESULT text_to_bytes( const WS_XML_TEXT *text, WS_HEAP *heap, WS_BYTES *val )
5748 HRESULT hr;
5750 switch (text->textType)
5752 case WS_XML_TEXT_TYPE_UTF8:
5754 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5755 hr = str_to_bytes( text_utf8->value.bytes, text_utf8->value.length, heap, val );
5756 break;
5758 case WS_XML_TEXT_TYPE_BASE64:
5760 const WS_XML_BASE64_TEXT *text_base64 = (const WS_XML_BASE64_TEXT *)text;
5761 if (!(val->bytes = ws_alloc( heap, text_base64->length ))) return WS_E_QUOTA_EXCEEDED;
5762 memcpy( val->bytes, text_base64->bytes, text_base64->length );
5763 val->length = text_base64->length;
5764 hr = S_OK;
5765 break;
5767 default:
5768 FIXME( "unhandled text type %u\n", text->textType );
5769 return E_NOTIMPL;
5772 return hr;
5775 static HRESULT read_type_bytes( struct reader *reader, WS_TYPE_MAPPING mapping,
5776 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5777 const WS_BYTES_DESCRIPTION *desc, WS_READ_OPTION option,
5778 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5780 const WS_XML_TEXT *text;
5781 WS_BYTES val = {0};
5782 HRESULT hr;
5784 if (desc) FIXME( "ignoring description\n" );
5786 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5787 if (*found && (hr = text_to_bytes( text, heap, &val )) != S_OK) return hr;
5789 switch (option)
5791 case WS_READ_REQUIRED_VALUE:
5792 case WS_READ_NILLABLE_VALUE:
5793 if (size != sizeof(val)) return E_INVALIDARG;
5794 *(WS_BYTES *)ret = val;
5795 break;
5797 case WS_READ_REQUIRED_POINTER:
5799 WS_BYTES *heap_val;
5800 if (size != sizeof(heap_val)) return E_INVALIDARG;
5801 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5802 *heap_val = val;
5803 *(WS_BYTES **)ret = heap_val;
5804 break;
5806 case WS_READ_OPTIONAL_POINTER:
5807 case WS_READ_NILLABLE_POINTER:
5809 WS_BYTES *heap_val = NULL;
5810 if (size != sizeof(heap_val)) return E_INVALIDARG;
5811 if (*found)
5813 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5814 *heap_val = val;
5816 *(WS_BYTES **)ret = heap_val;
5817 break;
5819 default:
5820 FIXME( "read option %u not supported\n", option );
5821 return E_NOTIMPL;
5824 return S_OK;
5827 static HRESULT text_to_xml_string( const WS_XML_TEXT *text, WS_HEAP *heap, WS_XML_STRING *val )
5829 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5830 assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5831 return str_to_xml_string( utf8->value.bytes, utf8->value.length, heap, val );
5834 static HRESULT read_type_xml_string( struct reader *reader, WS_TYPE_MAPPING mapping,
5835 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5836 const WS_XML_STRING_DESCRIPTION *desc, WS_READ_OPTION option,
5837 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5839 const WS_XML_TEXT *text;
5840 WS_XML_STRING val = {0};
5841 HRESULT hr;
5843 if (desc) FIXME( "ignoring description\n" );
5845 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5846 if (*found && (hr = text_to_xml_string( text, heap, &val )) != S_OK) return hr;
5848 switch (option)
5850 case WS_READ_REQUIRED_VALUE:
5851 case WS_READ_NILLABLE_VALUE:
5852 if (size != sizeof(val)) return E_INVALIDARG;
5853 *(WS_XML_STRING *)ret = val;
5854 break;
5856 case WS_READ_REQUIRED_POINTER:
5858 WS_XML_STRING *heap_val;
5859 if (size != sizeof(heap_val)) return E_INVALIDARG;
5860 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5861 *heap_val = val;
5862 *(WS_XML_STRING **)ret = heap_val;
5863 break;
5865 case WS_READ_OPTIONAL_POINTER:
5866 case WS_READ_NILLABLE_POINTER:
5868 WS_XML_STRING *heap_val = NULL;
5869 if (size != sizeof(heap_val)) return E_INVALIDARG;
5870 if (*found)
5872 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5873 *heap_val = val;
5875 *(WS_XML_STRING **)ret = heap_val;
5876 break;
5878 default:
5879 FIXME( "read option %u not supported\n", option );
5880 return E_NOTIMPL;
5883 return S_OK;
5886 static HRESULT text_to_qname( struct reader *reader, const WS_XML_TEXT *text, WS_HEAP *heap, WS_XML_QNAME *val )
5888 HRESULT hr;
5890 switch (text->textType)
5892 case WS_XML_TEXT_TYPE_UTF8:
5894 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5895 hr = str_to_qname( reader, text_utf8->value.bytes, text_utf8->value.length, heap, NULL,
5896 &val->localName, &val->ns );
5897 break;
5899 case WS_XML_TEXT_TYPE_QNAME:
5901 const WS_XML_QNAME_TEXT *text_qname = (const WS_XML_QNAME_TEXT *)text;
5902 if ((hr = copy_xml_string( heap, text_qname->localName, &val->localName )) != S_OK) return hr;
5903 if ((hr = copy_xml_string( heap, text_qname->ns, &val->ns )) != S_OK)
5905 ws_free( heap, val->localName.bytes, val->localName.length );
5906 return hr;
5908 break;
5910 default:
5911 FIXME( "unhandled text type %u\n", text->textType );
5912 return E_NOTIMPL;
5915 return hr;
5918 static HRESULT read_type_qname( struct reader *reader, WS_TYPE_MAPPING mapping,
5919 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5920 const WS_XML_QNAME_DESCRIPTION *desc, WS_READ_OPTION option,
5921 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5923 const WS_XML_TEXT *text;
5924 WS_XML_QNAME val = {{0}};
5925 HRESULT hr;
5927 if (desc) FIXME( "ignoring description\n" );
5929 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return WS_E_INVALID_FORMAT;
5930 if ((hr = read_startelement( reader )) != S_OK) return hr;
5931 if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT) return WS_E_INVALID_FORMAT;
5933 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5934 if (*found && (hr = text_to_qname( reader, text, heap, &val )) != S_OK) return hr;
5936 switch (option)
5938 case WS_READ_REQUIRED_VALUE:
5939 if (!*found) return WS_E_INVALID_FORMAT;
5940 /* fall through */
5942 case WS_READ_NILLABLE_VALUE:
5943 if (size != sizeof(val)) return E_INVALIDARG;
5944 *(WS_XML_QNAME *)ret = val;
5945 break;
5947 case WS_READ_REQUIRED_POINTER:
5948 if (!*found) return WS_E_INVALID_FORMAT;
5949 /* fall through */
5951 case WS_READ_OPTIONAL_POINTER:
5952 case WS_READ_NILLABLE_POINTER:
5954 WS_XML_QNAME *heap_val = NULL;
5955 if (size != sizeof(heap_val)) return E_INVALIDARG;
5956 if (*found)
5958 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5959 *heap_val = val;
5961 *(WS_XML_QNAME **)ret = heap_val;
5962 break;
5964 default:
5965 FIXME( "read option %u not supported\n", option );
5966 return E_NOTIMPL;
5969 return S_OK;
5972 static HRESULT read_type_description( struct reader *reader, WS_TYPE_MAPPING mapping,
5973 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5974 const WS_STRUCT_DESCRIPTION *desc, WS_READ_OPTION option,
5975 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5977 switch (option)
5979 case WS_READ_REQUIRED_POINTER:
5980 case WS_READ_OPTIONAL_POINTER:
5982 if (size != sizeof(desc)) return E_INVALIDARG;
5983 *(const WS_STRUCT_DESCRIPTION **)ret = desc;
5984 *found = TRUE;
5985 break;
5987 default:
5988 FIXME( "read option %u not supported\n", option );
5989 return E_NOTIMPL;
5992 return S_OK;
5995 static BOOL is_empty_text_node( const struct node *node )
5997 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)node;
5999 if (node_type( node ) != WS_XML_NODE_TYPE_TEXT) return FALSE;
6000 switch (text->text->textType)
6002 case WS_XML_TEXT_TYPE_UTF8:
6004 ULONG i;
6005 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
6006 for (i = 0; i < utf8->value.length; i++) if (!read_isspace( utf8->value.bytes[i] )) return FALSE;
6007 return TRUE;
6009 case WS_XML_TEXT_TYPE_BASE64:
6011 const WS_XML_BASE64_TEXT *base64 = (const WS_XML_BASE64_TEXT *)text->text;
6012 return !base64->length;
6014 case WS_XML_TEXT_TYPE_BOOL:
6015 case WS_XML_TEXT_TYPE_INT32:
6016 case WS_XML_TEXT_TYPE_INT64:
6017 case WS_XML_TEXT_TYPE_UINT64:
6018 case WS_XML_TEXT_TYPE_FLOAT:
6019 case WS_XML_TEXT_TYPE_DOUBLE:
6020 case WS_XML_TEXT_TYPE_DECIMAL:
6021 case WS_XML_TEXT_TYPE_GUID:
6022 case WS_XML_TEXT_TYPE_UNIQUE_ID:
6023 case WS_XML_TEXT_TYPE_DATETIME:
6024 return FALSE;
6026 default:
6027 ERR( "unhandled text type %u\n", text->text->textType );
6028 return FALSE;
6032 /* skips comment and empty text nodes */
6033 static HRESULT read_type_next_node( struct reader *reader )
6035 for (;;)
6037 HRESULT hr;
6038 WS_XML_NODE_TYPE type;
6040 if ((hr = read_next_node( reader )) != S_OK) return hr;
6041 type = node_type( reader->current );
6042 if (type == WS_XML_NODE_TYPE_COMMENT ||
6043 (type == WS_XML_NODE_TYPE_TEXT && is_empty_text_node( reader->current ))) continue;
6044 return S_OK;
6048 static HRESULT read_type_next_element_node( struct reader *reader, const WS_XML_STRING *localname,
6049 const WS_XML_STRING *ns )
6051 struct reader_pos pos;
6052 HRESULT hr;
6054 if (!localname) return S_OK; /* assume reader is already correctly positioned */
6055 if (reader->current == reader->last)
6057 BOOL found;
6058 if ((hr = read_to_startelement( reader, &found )) != S_OK) return hr;
6059 if (!found) return WS_E_INVALID_FORMAT;
6061 if (match_element( reader->current, localname, ns )) return S_OK;
6063 save_reader_position( reader, &pos );
6064 if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6065 if (match_element( reader->current, localname, ns )) return S_OK;
6066 restore_reader_position( reader, &pos );
6068 return WS_E_INVALID_FORMAT;
6071 ULONG get_type_size( WS_TYPE type, const void *desc )
6073 switch (type)
6075 case WS_INT8_TYPE:
6076 case WS_UINT8_TYPE:
6077 return sizeof(INT8);
6079 case WS_INT16_TYPE:
6080 case WS_UINT16_TYPE:
6081 return sizeof(INT16);
6083 case WS_BOOL_TYPE:
6084 case WS_INT32_TYPE:
6085 case WS_UINT32_TYPE:
6086 case WS_ENUM_TYPE:
6087 return sizeof(INT32);
6089 case WS_INT64_TYPE:
6090 case WS_UINT64_TYPE:
6091 return sizeof(INT64);
6093 case WS_FLOAT_TYPE:
6094 return sizeof(float);
6096 case WS_DOUBLE_TYPE:
6097 return sizeof(double);
6099 case WS_DATETIME_TYPE:
6100 return sizeof(WS_DATETIME);
6102 case WS_GUID_TYPE:
6103 return sizeof(GUID);
6105 case WS_UNIQUE_ID_TYPE:
6106 return sizeof(WS_UNIQUE_ID);
6108 case WS_STRING_TYPE:
6109 return sizeof(WS_STRING);
6111 case WS_WSZ_TYPE:
6112 return sizeof(WCHAR *);
6114 case WS_BYTES_TYPE:
6115 return sizeof(WS_BYTES);
6117 case WS_XML_STRING_TYPE:
6118 return sizeof(WS_XML_STRING);
6120 case WS_XML_QNAME_TYPE:
6121 return sizeof(WS_XML_QNAME);
6123 case WS_DESCRIPTION_TYPE:
6124 return sizeof(WS_STRUCT_DESCRIPTION *);
6126 case WS_STRUCT_TYPE:
6128 const WS_STRUCT_DESCRIPTION *desc_struct = desc;
6129 return desc_struct->size;
6131 case WS_UNION_TYPE:
6133 const WS_UNION_DESCRIPTION *desc_union = desc;
6134 return desc_union->size;
6136 case WS_ANY_ATTRIBUTES_TYPE:
6137 return 0;
6139 default:
6140 ERR( "unhandled type %u\n", type );
6141 return 0;
6145 static WS_READ_OPTION get_field_read_option( WS_TYPE type, ULONG options )
6147 if (options & WS_FIELD_POINTER)
6149 if (options & WS_FIELD_NILLABLE) return WS_READ_NILLABLE_POINTER;
6150 if (options & WS_FIELD_OPTIONAL) return WS_READ_OPTIONAL_POINTER;
6151 return WS_READ_REQUIRED_POINTER;
6154 switch (type)
6156 case WS_BOOL_TYPE:
6157 case WS_INT8_TYPE:
6158 case WS_INT16_TYPE:
6159 case WS_INT32_TYPE:
6160 case WS_INT64_TYPE:
6161 case WS_UINT8_TYPE:
6162 case WS_UINT16_TYPE:
6163 case WS_UINT32_TYPE:
6164 case WS_UINT64_TYPE:
6165 case WS_FLOAT_TYPE:
6166 case WS_DOUBLE_TYPE:
6167 case WS_DATETIME_TYPE:
6168 case WS_GUID_TYPE:
6169 case WS_UNIQUE_ID_TYPE:
6170 case WS_STRING_TYPE:
6171 case WS_BYTES_TYPE:
6172 case WS_XML_STRING_TYPE:
6173 case WS_XML_QNAME_TYPE:
6174 case WS_STRUCT_TYPE:
6175 case WS_ENUM_TYPE:
6176 case WS_UNION_TYPE:
6177 if (options & (WS_FIELD_OPTIONAL|WS_FIELD_NILLABLE)) return WS_READ_NILLABLE_VALUE;
6178 return WS_READ_REQUIRED_VALUE;
6180 case WS_WSZ_TYPE:
6181 case WS_DESCRIPTION_TYPE:
6182 if (options & WS_FIELD_NILLABLE) return WS_READ_NILLABLE_POINTER;
6183 if (options & WS_FIELD_OPTIONAL) return WS_READ_OPTIONAL_POINTER;
6184 return WS_READ_REQUIRED_POINTER;
6186 default:
6187 FIXME( "unhandled type %u\n", type );
6188 return 0;
6192 static HRESULT read_type_field( struct reader *, const WS_STRUCT_DESCRIPTION *, const WS_FIELD_DESCRIPTION *,
6193 WS_HEAP *, char *, ULONG );
6195 static HRESULT read_type_union( struct reader *reader, const WS_UNION_DESCRIPTION *desc, WS_HEAP *heap, void *ret,
6196 ULONG size, BOOL *found )
6198 struct reader_pos pos;
6199 HRESULT hr;
6200 ULONG i;
6202 if (size != desc->size) return E_INVALIDARG;
6204 save_reader_position( reader, &pos );
6205 if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6207 for (i = 0; i < desc->fieldCount; i++)
6209 if ((*found = match_element( reader->current, desc->fields[i]->field.localName, desc->fields[i]->field.ns )))
6210 break;
6213 if (!*found)
6215 *(int *)((char *)ret + desc->enumOffset) = desc->noneEnumValue;
6216 restore_reader_position( reader, &pos );
6218 else
6220 ULONG offset = desc->fields[i]->field.offset;
6221 if ((hr = read_type_field( reader, NULL, &desc->fields[i]->field, heap, ret, offset )) != S_OK) return hr;
6222 *(int *)((char *)ret + desc->enumOffset) = desc->fields[i]->value;
6225 return S_OK;
6228 static HRESULT read_type( struct reader *, WS_TYPE_MAPPING, WS_TYPE, const WS_XML_STRING *,
6229 const WS_XML_STRING *, const void *, WS_READ_OPTION, WS_HEAP *,
6230 void *, ULONG, BOOL * );
6232 static HRESULT read_type_array( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap,
6233 void **ret, ULONG *count )
6235 HRESULT hr;
6236 ULONG item_size, nb_items = 0, nb_allocated = 1, offset = 0;
6237 WS_READ_OPTION option;
6238 BOOL found;
6239 char *buf;
6241 if (!(option = get_field_read_option( desc->type, desc->options ))) return E_INVALIDARG;
6243 /* wrapper element */
6244 if (desc->localName && ((hr = read_type_next_element_node( reader, desc->localName, desc->ns )) != S_OK))
6245 return hr;
6247 if (option == WS_READ_REQUIRED_VALUE || option == WS_READ_NILLABLE_VALUE)
6248 item_size = get_type_size( desc->type, desc->typeDescription );
6249 else
6250 item_size = sizeof(void *);
6252 if (!(buf = ws_alloc_zero( heap, item_size ))) return WS_E_QUOTA_EXCEEDED;
6253 for (;;)
6255 if (nb_items >= nb_allocated)
6257 SIZE_T old_size = nb_allocated * item_size, new_size = old_size * 2;
6258 if (!(buf = ws_realloc_zero( heap, buf, old_size, new_size ))) return WS_E_QUOTA_EXCEEDED;
6259 nb_allocated *= 2;
6262 if (desc->type == WS_UNION_TYPE)
6264 hr = read_type_union( reader, desc->typeDescription, heap, buf + offset, item_size, &found );
6265 if (hr != S_OK)
6267 ws_free( heap, buf, nb_allocated * item_size );
6268 return hr;
6270 if (!found) break;
6272 else
6274 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->itemLocalName, desc->itemNs,
6275 desc->typeDescription, option, heap, buf + offset, item_size, &found );
6276 if (hr == WS_E_INVALID_FORMAT) break;
6277 if (hr != S_OK)
6279 ws_free( heap, buf, nb_allocated * item_size );
6280 return hr;
6284 offset += item_size;
6285 nb_items++;
6288 if (desc->localName && ((hr = read_type_next_node( reader )) != S_OK)) return hr;
6290 if (desc->itemRange && (nb_items < desc->itemRange->minItemCount || nb_items > desc->itemRange->maxItemCount))
6292 TRACE( "number of items %u out of range (%u-%u)\n", nb_items, desc->itemRange->minItemCount,
6293 desc->itemRange->maxItemCount );
6294 ws_free( heap, buf, nb_allocated * item_size );
6295 return WS_E_INVALID_FORMAT;
6298 *count = nb_items;
6299 *ret = buf;
6301 return S_OK;
6304 static HRESULT read_type_text( struct reader *reader, const WS_FIELD_DESCRIPTION *desc,
6305 WS_READ_OPTION option, WS_HEAP *heap, void *ret, ULONG size )
6307 struct reader_pos pos;
6308 BOOL found;
6309 HRESULT hr;
6311 if (reader->current == reader->last)
6313 if ((hr = read_to_startelement( reader, &found )) != S_OK) return hr;
6314 if (!found) return WS_E_INVALID_FORMAT;
6317 save_reader_position( reader, &pos );
6318 if ((hr = read_next_node( reader )) != S_OK) return hr;
6319 hr = read_type( reader, WS_ANY_ELEMENT_TYPE_MAPPING, desc->type, NULL, NULL,
6320 desc->typeDescription, option, heap, ret, size, &found );
6321 if (hr == S_OK && !found) restore_reader_position( reader, &pos );
6322 return hr;
6325 static HRESULT read_type_field( struct reader *reader, const WS_STRUCT_DESCRIPTION *desc_struct,
6326 const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap, char *buf, ULONG offset )
6328 char *ptr;
6329 WS_READ_OPTION option;
6330 ULONG size;
6331 HRESULT hr;
6332 BOOL found;
6334 if (!desc) return E_INVALIDARG;
6335 if (desc->options & ~(WS_FIELD_POINTER|WS_FIELD_OPTIONAL|WS_FIELD_NILLABLE|WS_FIELD_NILLABLE_ITEM))
6337 FIXME( "options %08x not supported\n", desc->options );
6338 return E_NOTIMPL;
6340 if (!(option = get_field_read_option( desc->type, desc->options ))) return E_INVALIDARG;
6342 if (option == WS_READ_REQUIRED_VALUE || option == WS_READ_NILLABLE_VALUE)
6343 size = get_type_size( desc->type, desc->typeDescription );
6344 else
6345 size = sizeof(void *);
6347 ptr = buf + offset;
6348 switch (desc->mapping)
6350 case WS_TYPE_ATTRIBUTE_FIELD_MAPPING:
6351 hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6352 desc_struct, option, heap, ptr, size, &found );
6353 break;
6355 case WS_ATTRIBUTE_FIELD_MAPPING:
6356 hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6357 desc->typeDescription, option, heap, ptr, size, &found );
6358 break;
6360 case WS_ELEMENT_FIELD_MAPPING:
6361 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6362 desc->typeDescription, option, heap, ptr, size, &found );
6363 break;
6365 case WS_ELEMENT_CHOICE_FIELD_MAPPING:
6367 if (desc->type != WS_UNION_TYPE || !desc->typeDescription ||
6368 (desc->options & (WS_FIELD_POINTER|WS_FIELD_NILLABLE))) return E_INVALIDARG;
6369 hr = read_type_union( reader, desc->typeDescription, heap, ptr, size, &found );
6370 break;
6372 case WS_REPEATING_ELEMENT_FIELD_MAPPING:
6373 case WS_REPEATING_ELEMENT_CHOICE_FIELD_MAPPING:
6375 ULONG count;
6376 hr = read_type_array( reader, desc, heap, (void **)ptr, &count );
6377 if (hr == S_OK) *(ULONG *)(buf + desc->countOffset) = count;
6378 break;
6380 case WS_TEXT_FIELD_MAPPING:
6381 hr = read_type_text( reader, desc, option, heap, ptr, size );
6382 break;
6384 default:
6385 FIXME( "unhandled field mapping %u\n", desc->mapping );
6386 return E_NOTIMPL;
6389 if (hr == WS_E_INVALID_FORMAT)
6391 switch (option)
6393 case WS_READ_REQUIRED_VALUE:
6394 case WS_READ_REQUIRED_POINTER:
6395 return WS_E_INVALID_FORMAT;
6397 case WS_READ_NILLABLE_VALUE:
6398 if (desc->defaultValue) memcpy( ptr, desc->defaultValue->value, desc->defaultValue->valueSize );
6399 return S_OK;
6401 case WS_READ_OPTIONAL_POINTER:
6402 case WS_READ_NILLABLE_POINTER:
6403 *(void **)ptr = NULL;
6404 return S_OK;
6406 default:
6407 ERR( "unhandled option %u\n", option );
6408 return E_NOTIMPL;
6412 return hr;
6415 static HRESULT read_type_struct( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
6416 const WS_XML_STRING *ns, const WS_STRUCT_DESCRIPTION *desc, WS_READ_OPTION option,
6417 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
6419 ULONG i, offset;
6420 HRESULT hr;
6421 char *buf;
6423 if (!desc) return E_INVALIDARG;
6424 if (desc->structOptions & ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
6426 FIXME( "struct options %08x not supported\n",
6427 desc->structOptions & ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT );
6430 switch (option)
6432 case WS_READ_REQUIRED_POINTER:
6433 case WS_READ_OPTIONAL_POINTER:
6434 case WS_READ_NILLABLE_POINTER:
6435 if (size != sizeof(void *)) return E_INVALIDARG;
6436 if (!(buf = ws_alloc_zero( heap, desc->size ))) return WS_E_QUOTA_EXCEEDED;
6437 break;
6439 case WS_READ_REQUIRED_VALUE:
6440 case WS_READ_NILLABLE_VALUE:
6441 if (size != desc->size) return E_INVALIDARG;
6442 buf = ret;
6443 break;
6445 default:
6446 FIXME( "unhandled read option %u\n", option );
6447 return E_NOTIMPL;
6450 for (i = 0; i < desc->fieldCount; i++)
6452 offset = desc->fields[i]->offset;
6453 if ((hr = read_type_field( reader, desc, desc->fields[i], heap, buf, offset )) != S_OK) break;
6456 switch (option)
6458 case WS_READ_REQUIRED_POINTER:
6459 if (hr != S_OK)
6461 ws_free( heap, buf, desc->size );
6462 return hr;
6464 *(char **)ret = buf;
6465 break;
6467 case WS_READ_OPTIONAL_POINTER:
6468 case WS_READ_NILLABLE_POINTER:
6469 if (is_nil_value( buf, desc->size ))
6471 ws_free( heap, buf, desc->size );
6472 buf = NULL;
6474 *(char **)ret = buf;
6475 break;
6477 case WS_READ_REQUIRED_VALUE:
6478 case WS_READ_NILLABLE_VALUE:
6479 if (hr != S_OK) return hr;
6480 break;
6482 default:
6483 ERR( "unhandled read option %u\n", option );
6484 return E_NOTIMPL;
6487 if (desc->structOptions & WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
6489 struct node *parent = find_parent( reader );
6490 parent->flags |= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT;
6493 *found = TRUE;
6494 return S_OK;
6497 static HRESULT start_mapping( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
6498 const WS_XML_STRING *ns )
6500 switch (mapping)
6502 case WS_ELEMENT_TYPE_MAPPING:
6503 case WS_ELEMENT_CONTENT_TYPE_MAPPING:
6504 return read_type_next_element_node( reader, localname, ns );
6506 case WS_ANY_ELEMENT_TYPE_MAPPING:
6507 case WS_ATTRIBUTE_TYPE_MAPPING:
6508 return S_OK;
6510 default:
6511 FIXME( "unhandled mapping %u\n", mapping );
6512 return E_NOTIMPL;
6516 static HRESULT read_type_endelement_node( struct reader *reader )
6518 const struct node *parent = find_parent( reader );
6519 HRESULT hr;
6521 for (;;)
6523 if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6524 if (node_type( reader->current ) == WS_XML_NODE_TYPE_END_ELEMENT && reader->current->parent == parent)
6526 return S_OK;
6528 if (read_end_of_data( reader ) || !(parent->flags & NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT)) break;
6531 return WS_E_INVALID_FORMAT;
6534 static HRESULT end_mapping( struct reader *reader, WS_TYPE_MAPPING mapping )
6536 switch (mapping)
6538 case WS_ELEMENT_TYPE_MAPPING:
6539 return read_type_endelement_node( reader );
6541 case WS_ELEMENT_CONTENT_TYPE_MAPPING:
6542 return read_type_next_node( reader );
6544 case WS_ATTRIBUTE_TYPE_MAPPING:
6545 default:
6546 return S_OK;
6550 static BOOL is_true_text( const WS_XML_TEXT *text )
6552 switch (text->textType)
6554 case WS_XML_TEXT_TYPE_UTF8:
6556 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
6557 if (text_utf8->value.length == 4 && !memcmp( text_utf8->value.bytes, "true", 4 )) return TRUE;
6558 return FALSE;
6560 case WS_XML_TEXT_TYPE_BOOL:
6562 const WS_XML_BOOL_TEXT *text_bool = (const WS_XML_BOOL_TEXT *)text;
6563 return text_bool->value;
6565 default:
6566 ERR( "unhandled text type %u\n", text->textType );
6567 return FALSE;
6571 static HRESULT is_nil_element( const WS_XML_ELEMENT_NODE *elem )
6573 static const WS_XML_STRING localname = {3, (BYTE *)"nil"};
6574 static const WS_XML_STRING ns = {41, (BYTE *)"http://www.w3.org/2001/XMLSchema-instance"};
6575 ULONG i;
6577 for (i = 0; i < elem->attributeCount; i++)
6579 if (elem->attributes[i]->isXmlNs) continue;
6580 if (WsXmlStringEquals( elem->attributes[i]->localName, &localname, NULL ) == S_OK &&
6581 WsXmlStringEquals( elem->attributes[i]->ns, &ns, NULL ) == S_OK &&
6582 is_true_text( elem->attributes[i]->value )) return TRUE;
6584 return FALSE;
6587 static HRESULT read_type( struct reader *reader, WS_TYPE_MAPPING mapping, WS_TYPE type,
6588 const WS_XML_STRING *localname, const WS_XML_STRING *ns, const void *desc,
6589 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size, BOOL *found )
6591 HRESULT hr;
6593 if ((hr = start_mapping( reader, mapping, localname, ns )) != S_OK) return hr;
6595 if (mapping == WS_ELEMENT_TYPE_MAPPING && is_nil_element( &reader->current->hdr ))
6597 if (option != WS_READ_NILLABLE_POINTER && option != WS_READ_NILLABLE_VALUE) return WS_E_INVALID_FORMAT;
6598 *found = TRUE;
6599 return end_mapping( reader, mapping );
6602 switch (type)
6604 case WS_BOOL_TYPE:
6605 hr = read_type_bool( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6606 break;
6608 case WS_INT8_TYPE:
6609 hr = read_type_int8( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6610 break;
6612 case WS_INT16_TYPE:
6613 hr = read_type_int16( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6614 break;
6616 case WS_INT32_TYPE:
6617 hr = read_type_int32( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6618 break;
6620 case WS_INT64_TYPE:
6621 hr = read_type_int64( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6622 break;
6624 case WS_UINT8_TYPE:
6625 hr = read_type_uint8( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6626 break;
6628 case WS_UINT16_TYPE:
6629 hr = read_type_uint16( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6630 break;
6632 case WS_UINT32_TYPE:
6633 hr = read_type_uint32( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6634 break;
6636 case WS_UINT64_TYPE:
6637 hr = read_type_uint64( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6638 break;
6640 case WS_FLOAT_TYPE:
6641 hr = read_type_float( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6642 break;
6644 case WS_DOUBLE_TYPE:
6645 hr = read_type_double( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6646 break;
6648 case WS_DATETIME_TYPE:
6649 hr = read_type_datetime( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6650 break;
6652 case WS_GUID_TYPE:
6653 hr = read_type_guid( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6654 break;
6656 case WS_UNIQUE_ID_TYPE:
6657 hr = read_type_unique_id( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6658 break;
6660 case WS_STRING_TYPE:
6661 hr = read_type_string( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6662 break;
6664 case WS_WSZ_TYPE:
6665 hr = read_type_wsz( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6666 break;
6668 case WS_BYTES_TYPE:
6669 hr = read_type_bytes( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6670 break;
6672 case WS_XML_STRING_TYPE:
6673 hr = read_type_xml_string( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6674 break;
6676 case WS_XML_QNAME_TYPE:
6677 hr = read_type_qname( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6678 break;
6680 case WS_DESCRIPTION_TYPE:
6681 hr = read_type_description( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6682 break;
6684 case WS_STRUCT_TYPE:
6685 hr = read_type_struct( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6686 break;
6688 case WS_ENUM_TYPE:
6689 hr = read_type_enum( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6690 break;
6692 default:
6693 FIXME( "type %u not supported\n", type );
6694 return E_NOTIMPL;
6697 if (hr != S_OK) return hr;
6698 return end_mapping( reader, mapping );
6701 /**************************************************************************
6702 * WsReadType [webservices.@]
6704 HRESULT WINAPI WsReadType( WS_XML_READER *handle, WS_TYPE_MAPPING mapping, WS_TYPE type,
6705 const void *desc, WS_READ_OPTION option, WS_HEAP *heap, void *value,
6706 ULONG size, WS_ERROR *error )
6708 struct reader *reader = (struct reader *)handle;
6709 BOOL found;
6710 HRESULT hr;
6712 TRACE( "%p %u %u %p %u %p %p %u %p\n", handle, mapping, type, desc, option, heap, value,
6713 size, error );
6714 if (error) FIXME( "ignoring error parameter\n" );
6716 if (!reader || !value) return E_INVALIDARG;
6718 EnterCriticalSection( &reader->cs );
6720 if (reader->magic != READER_MAGIC)
6722 LeaveCriticalSection( &reader->cs );
6723 return E_INVALIDARG;
6726 if ((hr = read_type( reader, mapping, type, NULL, NULL, desc, option, heap, value, size, &found )) == S_OK)
6728 switch (mapping)
6730 case WS_ELEMENT_TYPE_MAPPING:
6731 hr = read_node( reader );
6732 break;
6734 default:
6735 break;
6737 if (hr == S_OK && !read_end_of_data( reader )) hr = WS_E_INVALID_FORMAT;
6740 LeaveCriticalSection( &reader->cs );
6741 TRACE( "returning %08x\n", hr );
6742 return hr;
6745 HRESULT read_header( WS_XML_READER *handle, const WS_XML_STRING *localname, const WS_XML_STRING *ns,
6746 WS_TYPE type, const void *desc, WS_READ_OPTION option, WS_HEAP *heap, void *value,
6747 ULONG size )
6749 struct reader *reader = (struct reader *)handle;
6750 BOOL found;
6751 HRESULT hr;
6753 EnterCriticalSection( &reader->cs );
6755 if (reader->magic != READER_MAGIC)
6757 LeaveCriticalSection( &reader->cs );
6758 return E_INVALIDARG;
6761 hr = read_type( reader, WS_ELEMENT_CONTENT_TYPE_MAPPING, type, localname, ns, desc, option, heap,
6762 value, size, &found );
6764 LeaveCriticalSection( &reader->cs );
6765 return hr;
6768 /**************************************************************************
6769 * WsReadElement [webservices.@]
6771 HRESULT WINAPI WsReadElement( WS_XML_READER *handle, const WS_ELEMENT_DESCRIPTION *desc,
6772 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size,
6773 WS_ERROR *error )
6775 struct reader *reader = (struct reader *)handle;
6776 BOOL found;
6777 HRESULT hr;
6779 TRACE( "%p %p %u %p %p %u %p\n", handle, desc, option, heap, value, size, error );
6780 if (error) FIXME( "ignoring error parameter\n" );
6782 if (!reader || !desc || !value) return E_INVALIDARG;
6784 EnterCriticalSection( &reader->cs );
6786 if (reader->magic != READER_MAGIC)
6788 LeaveCriticalSection( &reader->cs );
6789 return E_INVALIDARG;
6792 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->elementLocalName,
6793 desc->elementNs, desc->typeDescription, option, heap, value, size, &found );
6795 LeaveCriticalSection( &reader->cs );
6796 TRACE( "returning %08x\n", hr );
6797 return hr;
6800 /**************************************************************************
6801 * WsReadValue [webservices.@]
6803 HRESULT WINAPI WsReadValue( WS_XML_READER *handle, WS_VALUE_TYPE value_type, void *value, ULONG size,
6804 WS_ERROR *error )
6806 struct reader *reader = (struct reader *)handle;
6807 WS_TYPE type = map_value_type( value_type );
6808 BOOL found;
6809 HRESULT hr;
6811 TRACE( "%p %u %p %u %p\n", handle, type, value, size, error );
6812 if (error) FIXME( "ignoring error parameter\n" );
6814 if (!reader || !value || type == ~0u) return E_INVALIDARG;
6816 EnterCriticalSection( &reader->cs );
6818 if (reader->magic != READER_MAGIC)
6820 LeaveCriticalSection( &reader->cs );
6821 return E_INVALIDARG;
6824 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, type, NULL, NULL, NULL, WS_READ_REQUIRED_VALUE,
6825 NULL, value, size, &found );
6827 LeaveCriticalSection( &reader->cs );
6828 TRACE( "returning %08x\n", hr );
6829 return hr;
6832 /**************************************************************************
6833 * WsReadAttribute [webservices.@]
6835 HRESULT WINAPI WsReadAttribute( WS_XML_READER *handle, const WS_ATTRIBUTE_DESCRIPTION *desc,
6836 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size,
6837 WS_ERROR *error )
6839 struct reader *reader = (struct reader *)handle;
6840 BOOL found;
6841 HRESULT hr;
6843 TRACE( "%p %p %u %p %p %u %p\n", handle, desc, option, heap, value, size, error );
6844 if (error) FIXME( "ignoring error parameter\n" );
6846 if (!reader || !desc || !value) return E_INVALIDARG;
6848 EnterCriticalSection( &reader->cs );
6850 if (reader->magic != READER_MAGIC)
6852 LeaveCriticalSection( &reader->cs );
6853 return E_INVALIDARG;
6856 if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
6857 else hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->attributeLocalName,
6858 desc->attributeNs, desc->typeDescription, option, heap, value, size, &found );
6860 LeaveCriticalSection( &reader->cs );
6861 TRACE( "returning %08x\n", hr );
6862 return hr;
6865 static inline BOOL is_utf8( const unsigned char *data, ULONG size, ULONG *offset )
6867 static const char bom[] = {0xef,0xbb,0xbf};
6868 return (size >= sizeof(bom) && !memcmp( data, bom, sizeof(bom) ) && (*offset = sizeof(bom))) ||
6869 (size > 2 && !(*offset = 0));
6872 static inline BOOL is_utf16le( const unsigned char *data, ULONG size, ULONG *offset )
6874 static const char bom[] = {0xff,0xfe};
6875 return (size >= sizeof(bom) && !memcmp( data, bom, sizeof(bom) ) && (*offset = sizeof(bom))) ||
6876 (size >= 4 && data[0] == '<' && !data[1] && !(*offset = 0));
6879 static WS_CHARSET detect_charset( const unsigned char *data, ULONG size, ULONG *offset )
6881 WS_CHARSET ret = 0;
6883 /* FIXME: parse xml declaration */
6885 if (is_utf16le( data, size, offset )) ret = WS_CHARSET_UTF16LE;
6886 else if (is_utf8( data, size, offset )) ret = WS_CHARSET_UTF8;
6887 else
6889 FIXME( "charset not recognized\n" );
6890 return 0;
6893 TRACE( "detected charset %u\n", ret );
6894 return ret;
6897 static HRESULT utf16le_to_utf8( const unsigned char *data, ULONG size, unsigned char **buf, ULONG *buflen )
6899 if (size % sizeof(WCHAR)) return E_INVALIDARG;
6900 *buflen = WideCharToMultiByte( CP_UTF8, 0, (const WCHAR *)data, size / sizeof(WCHAR), NULL, 0, NULL, NULL );
6901 if (!(*buf = heap_alloc( *buflen ))) return E_OUTOFMEMORY;
6902 WideCharToMultiByte( CP_UTF8, 0, (const WCHAR *)data, size / sizeof(WCHAR), (char *)*buf, *buflen, NULL, NULL );
6903 return S_OK;
6906 static HRESULT set_input_buffer( struct reader *reader, const unsigned char *data, ULONG size )
6908 reader->input_type = WS_XML_READER_INPUT_TYPE_BUFFER;
6909 reader->input_buf = NULL;
6911 if (reader->input_enc == WS_XML_READER_ENCODING_TYPE_TEXT && reader->input_charset == WS_CHARSET_UTF16LE)
6913 unsigned char *buf;
6914 ULONG buflen;
6915 HRESULT hr;
6917 if ((hr = utf16le_to_utf8( data, size, &buf, &buflen )) != S_OK) return hr;
6918 heap_free( reader->input_conv );
6919 reader->read_bufptr = reader->input_conv = buf;
6920 reader->read_size = reader->input_size = buflen;
6922 else
6924 reader->read_bufptr = data;
6925 reader->read_size = reader->input_size = size;
6928 reader->read_pos = 0;
6929 reader->text_conv_offset = 0;
6930 return S_OK;
6933 static void set_input_stream( struct reader *reader, WS_READ_CALLBACK callback, void *state )
6935 reader->input_type = WS_XML_READER_INPUT_TYPE_STREAM;
6936 reader->input_cb = callback;
6937 reader->input_cb_state = state;
6938 reader->input_buf = NULL;
6939 reader->input_size = STREAM_BUFSIZE;
6941 if (reader->read_pos >= reader->read_size) reader->read_size = 0;
6942 else
6944 memmove( reader->stream_buf, reader->stream_buf + reader->read_pos, reader->read_size - reader->read_pos );
6945 reader->read_size -= reader->read_pos;
6947 reader->read_pos = 0;
6948 reader->read_bufptr = reader->stream_buf;
6949 reader->text_conv_offset = 0;
6952 /**************************************************************************
6953 * WsSetInput [webservices.@]
6955 HRESULT WINAPI WsSetInput( WS_XML_READER *handle, const WS_XML_READER_ENCODING *encoding,
6956 const WS_XML_READER_INPUT *input, const WS_XML_READER_PROPERTY *properties,
6957 ULONG count, WS_ERROR *error )
6959 struct reader *reader = (struct reader *)handle;
6960 struct node *node;
6961 ULONG i, offset = 0;
6962 HRESULT hr;
6964 TRACE( "%p %p %p %p %u %p\n", handle, encoding, input, properties, count, error );
6965 if (error) FIXME( "ignoring error parameter\n" );
6967 if (!reader) return E_INVALIDARG;
6969 EnterCriticalSection( &reader->cs );
6971 if (reader->magic != READER_MAGIC)
6973 LeaveCriticalSection( &reader->cs );
6974 return E_INVALIDARG;
6977 for (i = 0; i < count; i++)
6979 hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
6980 properties[i].valueSize );
6981 if (hr != S_OK) goto done;
6984 if ((hr = init_reader( reader )) != S_OK) goto done;
6986 switch (encoding->encodingType)
6988 case WS_XML_READER_ENCODING_TYPE_TEXT:
6990 if (input->inputType == WS_XML_READER_INPUT_TYPE_BUFFER)
6992 const WS_XML_READER_TEXT_ENCODING *text = (const WS_XML_READER_TEXT_ENCODING *)encoding;
6993 const WS_XML_READER_BUFFER_INPUT *buf = (const WS_XML_READER_BUFFER_INPUT *)input;
6994 if (text->charSet != WS_CHARSET_AUTO) reader->input_charset = text->charSet;
6995 else reader->input_charset = detect_charset( buf->encodedData, buf->encodedDataSize, &offset );
6998 reader->input_enc = WS_XML_READER_ENCODING_TYPE_TEXT;
6999 break;
7001 case WS_XML_READER_ENCODING_TYPE_BINARY:
7003 const WS_XML_READER_BINARY_ENCODING *bin = (const WS_XML_READER_BINARY_ENCODING *)encoding;
7004 reader->input_enc = WS_XML_READER_ENCODING_TYPE_BINARY;
7005 reader->input_charset = 0;
7006 reader->dict_static = bin->staticDictionary ? bin->staticDictionary : &dict_builtin_static.dict;
7007 reader->dict = bin->dynamicDictionary ? bin->dynamicDictionary : &dict_builtin.dict;
7008 break;
7010 default:
7011 FIXME( "encoding type %u not supported\n", encoding->encodingType );
7012 hr = E_NOTIMPL;
7013 goto done;
7016 switch (input->inputType)
7018 case WS_XML_READER_INPUT_TYPE_BUFFER:
7020 const WS_XML_READER_BUFFER_INPUT *buf = (const WS_XML_READER_BUFFER_INPUT *)input;
7021 hr = set_input_buffer( reader, (const unsigned char *)buf->encodedData + offset, buf->encodedDataSize - offset );
7022 if (hr != S_OK) goto done;
7023 break;
7025 case WS_XML_READER_INPUT_TYPE_STREAM:
7027 const WS_XML_READER_STREAM_INPUT *stream = (const WS_XML_READER_STREAM_INPUT *)input;
7028 if (!reader->stream_buf && !(reader->stream_buf = heap_alloc( STREAM_BUFSIZE )))
7030 hr = E_OUTOFMEMORY;
7031 goto done;
7033 set_input_stream( reader, stream->readCallback, stream->readCallbackState );
7034 break;
7036 default:
7037 FIXME( "input type %u not supported\n", input->inputType );
7038 hr = E_NOTIMPL;
7039 goto done;
7042 if (!(node = alloc_node( WS_XML_NODE_TYPE_BOF ))) hr = E_OUTOFMEMORY;
7043 else read_insert_bof( reader, node );
7045 done:
7046 LeaveCriticalSection( &reader->cs );
7047 TRACE( "returning %08x\n", hr );
7048 return hr;
7051 static HRESULT set_input_xml_buffer( struct reader *reader, struct xmlbuf *xmlbuf )
7053 reader->input_type = WS_XML_READER_INPUT_TYPE_BUFFER;
7054 reader->input_buf = xmlbuf;
7055 reader->input_enc = xmlbuf->encoding;
7056 reader->input_charset = xmlbuf->charset;
7057 reader->dict_static = xmlbuf->dict_static;
7058 reader->dict = xmlbuf->dict;
7060 if (reader->input_enc == WS_XML_READER_ENCODING_TYPE_TEXT && reader->input_charset == WS_CHARSET_UTF16LE)
7062 unsigned char *buf;
7063 ULONG buflen;
7064 HRESULT hr;
7066 if ((hr = utf16le_to_utf8( xmlbuf->bytes.bytes, xmlbuf->bytes.length, &buf, &buflen )) != S_OK) return hr;
7067 heap_free( reader->input_conv );
7068 reader->read_bufptr = reader->input_conv = buf;
7069 reader->read_size = reader->input_size = buflen;
7071 else
7073 reader->read_bufptr = xmlbuf->bytes.bytes;
7074 reader->read_size = reader->input_size = xmlbuf->bytes.length;
7077 reader->read_pos = 0;
7078 reader->text_conv_offset = 0;
7079 return S_OK;
7082 /**************************************************************************
7083 * WsSetInputToBuffer [webservices.@]
7085 HRESULT WINAPI WsSetInputToBuffer( WS_XML_READER *handle, WS_XML_BUFFER *buffer,
7086 const WS_XML_READER_PROPERTY *properties, ULONG count,
7087 WS_ERROR *error )
7089 struct reader *reader = (struct reader *)handle;
7090 struct xmlbuf *xmlbuf = (struct xmlbuf *)buffer;
7091 struct node *node;
7092 HRESULT hr;
7093 ULONG i;
7095 TRACE( "%p %p %p %u %p\n", handle, buffer, properties, count, error );
7096 if (error) FIXME( "ignoring error parameter\n" );
7098 if (!reader || !xmlbuf) return E_INVALIDARG;
7100 EnterCriticalSection( &reader->cs );
7102 if (reader->magic != READER_MAGIC)
7104 LeaveCriticalSection( &reader->cs );
7105 return E_INVALIDARG;
7108 for (i = 0; i < count; i++)
7110 hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
7111 properties[i].valueSize );
7112 if (hr != S_OK) goto done;
7115 if ((hr = init_reader( reader )) != S_OK) goto done;
7116 if ((hr = set_input_xml_buffer( reader, xmlbuf )) != S_OK) goto done;
7118 if (!(node = alloc_node( WS_XML_NODE_TYPE_BOF ))) hr = E_OUTOFMEMORY;
7119 else read_insert_bof( reader, node );
7121 done:
7122 LeaveCriticalSection( &reader->cs );
7123 TRACE( "returning %08x\n", hr );
7124 return hr;
7127 /**************************************************************************
7128 * WsGetReaderPosition [webservices.@]
7130 HRESULT WINAPI WsGetReaderPosition( WS_XML_READER *handle, WS_XML_NODE_POSITION *pos, WS_ERROR *error )
7132 struct reader *reader = (struct reader *)handle;
7133 HRESULT hr = S_OK;
7135 TRACE( "%p %p %p\n", handle, pos, error );
7136 if (error) FIXME( "ignoring error parameter\n" );
7138 if (!reader || !pos) return E_INVALIDARG;
7140 EnterCriticalSection( &reader->cs );
7142 if (reader->magic != READER_MAGIC)
7144 LeaveCriticalSection( &reader->cs );
7145 return E_INVALIDARG;
7148 if (!reader->input_buf) hr = WS_E_INVALID_OPERATION;
7149 else
7151 pos->buffer = (WS_XML_BUFFER *)reader->input_buf;
7152 pos->node = reader->current;
7155 LeaveCriticalSection( &reader->cs );
7156 TRACE( "returning %08x\n", hr );
7157 return hr;
7160 /**************************************************************************
7161 * WsSetReaderPosition [webservices.@]
7163 HRESULT WINAPI WsSetReaderPosition( WS_XML_READER *handle, const WS_XML_NODE_POSITION *pos, WS_ERROR *error )
7165 struct reader *reader = (struct reader *)handle;
7166 HRESULT hr = S_OK;
7168 TRACE( "%p %p %p\n", handle, pos, error );
7169 if (error) FIXME( "ignoring error parameter\n" );
7171 if (!reader || !pos) return E_INVALIDARG;
7173 EnterCriticalSection( &reader->cs );
7175 if (reader->magic != READER_MAGIC || (struct xmlbuf *)pos->buffer != reader->input_buf)
7177 LeaveCriticalSection( &reader->cs );
7178 return E_INVALIDARG;
7181 if (!reader->input_buf) hr = WS_E_INVALID_OPERATION;
7182 else reader->current = pos->node;
7184 LeaveCriticalSection( &reader->cs );
7185 TRACE( "returning %08x\n", hr );
7186 return hr;
7189 static HRESULT utf8_to_base64( const WS_XML_UTF8_TEXT *utf8, WS_XML_BASE64_TEXT *base64 )
7191 if (utf8->value.length % 4) return WS_E_INVALID_FORMAT;
7192 if (!(base64->bytes = heap_alloc( utf8->value.length * 3 / 4 ))) return E_OUTOFMEMORY;
7193 base64->length = decode_base64( utf8->value.bytes, utf8->value.length, base64->bytes );
7194 return S_OK;
7197 /**************************************************************************
7198 * WsReadBytes [webservices.@]
7200 HRESULT WINAPI WsReadBytes( WS_XML_READER *handle, void *bytes, ULONG max_count, ULONG *count, WS_ERROR *error )
7202 struct reader *reader = (struct reader *)handle;
7203 HRESULT hr = S_OK;
7205 TRACE( "%p %p %u %p %p\n", handle, bytes, max_count, count, error );
7206 if (error) FIXME( "ignoring error parameter\n" );
7208 if (!reader) return E_INVALIDARG;
7210 EnterCriticalSection( &reader->cs );
7212 if (reader->magic != READER_MAGIC)
7214 LeaveCriticalSection( &reader->cs );
7215 return E_INVALIDARG;
7218 if (!reader->input_type)
7220 hr = WS_E_INVALID_OPERATION;
7221 goto done;
7223 if (!count)
7225 hr = E_INVALIDARG;
7226 goto done;
7229 *count = 0;
7230 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && bytes)
7232 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
7233 WS_XML_BASE64_TEXT base64;
7235 if ((hr = utf8_to_base64( (const WS_XML_UTF8_TEXT *)text->text, &base64 )) != S_OK) goto done;
7236 if (reader->text_conv_offset == base64.length)
7238 heap_free( base64.bytes );
7239 hr = read_node( reader );
7240 goto done;
7242 *count = min( base64.length - reader->text_conv_offset, max_count );
7243 memcpy( bytes, base64.bytes + reader->text_conv_offset, *count );
7244 reader->text_conv_offset += *count;
7245 heap_free( base64.bytes );
7248 done:
7249 LeaveCriticalSection( &reader->cs );
7250 TRACE( "returning %08x\n", hr );
7251 return hr;
7254 static HRESULT utf8_to_utf16( const WS_XML_UTF8_TEXT *utf8, WS_XML_UTF16_TEXT *utf16 )
7256 int len = MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, NULL, 0 );
7257 if (!(utf16->bytes = heap_alloc( len * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
7258 MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, (WCHAR *)utf16->bytes, len );
7259 utf16->byteCount = len * sizeof(WCHAR);
7260 return S_OK;
7263 /**************************************************************************
7264 * WsReadChars [webservices.@]
7266 HRESULT WINAPI WsReadChars( WS_XML_READER *handle, WCHAR *chars, ULONG max_count, ULONG *count, WS_ERROR *error )
7268 struct reader *reader = (struct reader *)handle;
7269 HRESULT hr = S_OK;
7271 TRACE( "%p %p %u %p %p\n", handle, chars, max_count, count, error );
7272 if (error) FIXME( "ignoring error parameter\n" );
7274 if (!reader) return E_INVALIDARG;
7276 EnterCriticalSection( &reader->cs );
7278 if (reader->magic != READER_MAGIC)
7280 LeaveCriticalSection( &reader->cs );
7281 return E_INVALIDARG;
7284 if (!reader->input_type)
7286 hr = WS_E_INVALID_OPERATION;
7287 goto done;
7289 if (!count)
7291 hr = E_INVALIDARG;
7292 goto done;
7295 *count = 0;
7296 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && chars)
7298 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
7299 WS_XML_UTF16_TEXT utf16;
7300 HRESULT hr;
7302 if ((hr = utf8_to_utf16( (const WS_XML_UTF8_TEXT *)text->text, &utf16 )) != S_OK) goto done;
7303 if (reader->text_conv_offset == utf16.byteCount / sizeof(WCHAR))
7305 heap_free( utf16.bytes );
7306 hr = read_node( reader );
7307 goto done;
7309 *count = min( utf16.byteCount / sizeof(WCHAR) - reader->text_conv_offset, max_count );
7310 memcpy( chars, utf16.bytes + reader->text_conv_offset * sizeof(WCHAR), *count * sizeof(WCHAR) );
7311 reader->text_conv_offset += *count;
7312 heap_free( utf16.bytes );
7315 done:
7316 LeaveCriticalSection( &reader->cs );
7317 TRACE( "returning %08x\n", hr );
7318 return hr;
7321 /**************************************************************************
7322 * WsReadCharsUtf8 [webservices.@]
7324 HRESULT WINAPI WsReadCharsUtf8( WS_XML_READER *handle, BYTE *bytes, ULONG max_count, ULONG *count, WS_ERROR *error )
7326 struct reader *reader = (struct reader *)handle;
7327 HRESULT hr = S_OK;
7329 TRACE( "%p %p %u %p %p\n", handle, bytes, max_count, count, error );
7330 if (error) FIXME( "ignoring error parameter\n" );
7332 if (!reader) return E_INVALIDARG;
7334 EnterCriticalSection( &reader->cs );
7336 if (reader->magic != READER_MAGIC)
7338 LeaveCriticalSection( &reader->cs );
7339 return E_INVALIDARG;
7342 if (!reader->input_type)
7344 hr = WS_E_INVALID_OPERATION;
7345 goto done;
7347 if (!count)
7349 hr = E_INVALIDARG;
7350 goto done;
7353 *count = 0;
7354 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && bytes)
7356 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
7357 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
7359 if (reader->text_conv_offset == utf8->value.length)
7361 hr = read_node( reader );
7362 goto done;
7364 *count = min( utf8->value.length - reader->text_conv_offset, max_count );
7365 memcpy( bytes, utf8->value.bytes + reader->text_conv_offset, *count );
7366 reader->text_conv_offset += *count;
7369 done:
7370 LeaveCriticalSection( &reader->cs );
7371 TRACE( "returning %08x\n", hr );
7372 return hr;
7375 static HRESULT move_to_element( struct reader *reader )
7377 HRESULT hr;
7378 if (node_type( reader->current ) == WS_XML_NODE_TYPE_BOF &&
7379 (hr = read_move_to( reader, WS_MOVE_TO_CHILD_NODE, NULL )) != S_OK) return hr;
7380 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return E_FAIL;
7381 return S_OK;
7384 static HRESULT copy_tree( struct reader *reader, WS_XML_WRITER *writer )
7386 const struct node *node, *parent;
7387 BOOL done = FALSE;
7388 HRESULT hr;
7390 if ((hr = move_to_element( reader )) != S_OK) return hr;
7391 parent = reader->current;
7392 for (;;)
7394 node = reader->current;
7395 if ((hr = WsWriteNode( writer, (const WS_XML_NODE *)node, NULL )) != S_OK) break;
7396 if (node_type( node ) == WS_XML_NODE_TYPE_END_ELEMENT && node->parent == parent) done = TRUE;
7397 if ((hr = read_next_node( reader )) != S_OK || done) break;
7399 return hr;
7402 /**************************************************************************
7403 * WsReadXmlBuffer [webservices.@]
7405 HRESULT WINAPI WsReadXmlBuffer( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_BUFFER **ret, WS_ERROR *error )
7407 struct reader *reader = (struct reader *)handle;
7408 WS_XML_WRITER *writer = NULL;
7409 WS_XML_BUFFER *buffer = NULL;
7410 HRESULT hr;
7412 TRACE( "%p %p %p %p\n", handle, heap, ret, error );
7413 if (error) FIXME( "ignoring error parameter\n" );
7415 if (!reader || !heap) return E_INVALIDARG;
7416 if (!ret) return E_FAIL;
7418 EnterCriticalSection( &reader->cs );
7420 if (reader->magic != READER_MAGIC)
7422 LeaveCriticalSection( &reader->cs );
7423 return E_INVALIDARG;
7426 if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
7427 else
7429 if ((hr = WsCreateWriter( NULL, 0, &writer, NULL )) != S_OK) goto done;
7430 if ((hr = WsCreateXmlBuffer( heap, NULL, 0, &buffer, NULL )) != S_OK) goto done;
7431 if ((hr = WsSetOutputToBuffer( writer, buffer, NULL, 0, NULL )) != S_OK) goto done;
7432 if ((hr = copy_tree( reader, writer )) == S_OK) *ret = buffer;
7435 done:
7436 if (hr != S_OK) free_xmlbuf( (struct xmlbuf *)buffer );
7437 WsFreeWriter( writer );
7438 LeaveCriticalSection( &reader->cs );
7439 TRACE( "returning %08x\n", hr );
7440 return hr;
7443 HRESULT create_header_buffer( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_BUFFER **ret )
7445 struct reader *reader = (struct reader *)handle;
7446 HRESULT hr = WS_E_QUOTA_EXCEEDED;
7447 struct xmlbuf *xmlbuf;
7449 EnterCriticalSection( &reader->cs );
7451 if (reader->magic != READER_MAGIC)
7453 LeaveCriticalSection( &reader->cs );
7454 return E_INVALIDARG;
7457 if ((xmlbuf = alloc_xmlbuf( heap, reader->read_pos, reader->input_enc, reader->input_charset,
7458 reader->dict_static, reader->dict )))
7460 memcpy( xmlbuf->bytes.bytes, reader->read_bufptr, reader->read_pos );
7461 xmlbuf->bytes.length = reader->read_pos;
7462 *ret = (WS_XML_BUFFER *)xmlbuf;
7463 hr = S_OK;
7466 LeaveCriticalSection( &reader->cs );
7467 return hr;
7470 HRESULT get_param_desc( const WS_STRUCT_DESCRIPTION *desc, USHORT index, const WS_FIELD_DESCRIPTION **ret )
7472 if (index >= desc->fieldCount) return E_INVALIDARG;
7473 *ret = desc->fields[index];
7474 return S_OK;
7477 static ULONG get_field_size( const WS_FIELD_DESCRIPTION *desc )
7479 if (desc->options & WS_FIELD_POINTER) return sizeof(void *);
7480 return get_type_size( desc->type, desc->typeDescription );
7483 static HRESULT read_param( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap, void *ret )
7485 if (!ret && !(ret = ws_alloc_zero( heap, get_field_size(desc) ))) return WS_E_QUOTA_EXCEEDED;
7486 return read_type_field( reader, NULL, desc, heap, ret, 0 );
7489 static HRESULT read_param_array( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap,
7490 void **ret, ULONG *count )
7492 if (!ret && !(ret = ws_alloc_zero( heap, sizeof(void **) ))) return WS_E_QUOTA_EXCEEDED;
7493 return read_type_array( reader, desc, heap, ret, count );
7496 static void set_array_len( const WS_PARAMETER_DESCRIPTION *params, ULONG count, ULONG index, ULONG len,
7497 const void **args )
7499 ULONG i, *ptr;
7500 for (i = 0; i < count; i++)
7502 if (params[i].outputMessageIndex != index || params[i].parameterType != WS_PARAMETER_TYPE_ARRAY_COUNT)
7503 continue;
7504 if ((ptr = *(ULONG **)args[i])) *ptr = len;
7505 break;
7509 HRESULT read_output_params( WS_XML_READER *handle, WS_HEAP *heap, const WS_ELEMENT_DESCRIPTION *desc,
7510 const WS_PARAMETER_DESCRIPTION *params, ULONG count, const void **args )
7512 struct reader *reader = (struct reader *)handle;
7513 const WS_STRUCT_DESCRIPTION *desc_struct;
7514 const WS_FIELD_DESCRIPTION *desc_field;
7515 ULONG i, len;
7516 HRESULT hr;
7518 if (desc->type != WS_STRUCT_TYPE || !(desc_struct = desc->typeDescription)) return E_INVALIDARG;
7520 EnterCriticalSection( &reader->cs );
7522 if (reader->magic != READER_MAGIC)
7524 LeaveCriticalSection( &reader->cs );
7525 return E_INVALIDARG;
7528 if ((hr = start_mapping( reader, WS_ELEMENT_TYPE_MAPPING, desc->elementLocalName, desc->elementNs )) != S_OK)
7529 goto done;
7531 for (i = 0; i < count; i++)
7533 if (params[i].outputMessageIndex == INVALID_PARAMETER_INDEX) continue;
7534 if (params[i].parameterType == WS_PARAMETER_TYPE_MESSAGES)
7536 FIXME( "messages type not supported\n" );
7537 hr = E_NOTIMPL;
7538 goto done;
7540 if ((hr = get_param_desc( desc_struct, params[i].outputMessageIndex, &desc_field )) != S_OK) goto done;
7541 if (params[i].parameterType == WS_PARAMETER_TYPE_NORMAL)
7543 void *ptr = *(void **)args[i];
7544 if ((hr = read_param( reader, desc_field, heap, ptr )) != S_OK) goto done;
7546 else if (params[i].parameterType == WS_PARAMETER_TYPE_ARRAY)
7548 void **ptr = *(void ***)args[i];
7549 if ((hr = read_param_array( reader, desc_field, heap, ptr, &len )) != S_OK) goto done;
7550 set_array_len( params, count, params[i].outputMessageIndex, len, args );
7554 if (desc_struct->structOptions & WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
7556 struct node *parent = find_parent( reader );
7557 parent->flags |= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT;
7560 hr = end_mapping( reader, WS_ELEMENT_TYPE_MAPPING );
7562 done:
7563 LeaveCriticalSection( &reader->cs );
7564 return hr;