kernel32: Don't clear WINEDEBUG in the debugger process if +winedbg is set.
[wine.git] / dlls / webservices / reader.c
blob087ad0a9cc778ad1c672c0c52a963284cc9c3f8f
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>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winnls.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/heap.h"
29 #include "wine/list.h"
30 #include "webservices_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
34 ULONG prop_size( const struct prop_desc *desc, ULONG count )
36 ULONG i, ret = count * sizeof(struct prop);
37 for (i = 0; i < count; i++) ret += desc[i].size;
38 return ret;
41 void prop_init( const struct prop_desc *desc, ULONG count, struct prop *prop, void *data )
43 ULONG i;
44 char *ptr = data;
45 for (i = 0; i < count; i++)
47 prop[i].value = ptr;
48 prop[i].size = desc[i].size;
49 prop[i].readonly = desc[i].readonly;
50 prop[i].writeonly = desc[i].writeonly;
51 ptr += prop[i].size;
55 HRESULT prop_set( const struct prop *prop, ULONG count, ULONG id, const void *value, ULONG size )
57 if (id >= count || size != prop[id].size || prop[id].readonly) return E_INVALIDARG;
58 memcpy( prop[id].value, value, size );
59 return S_OK;
62 HRESULT prop_get( const struct prop *prop, ULONG count, ULONG id, void *buf, ULONG size )
64 if (id >= count || size != prop[id].size || prop[id].writeonly) return E_INVALIDARG;
65 memcpy( buf, prop[id].value, prop[id].size );
66 return S_OK;
69 struct node *alloc_node( WS_XML_NODE_TYPE type )
71 struct node *ret;
73 if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
74 ret->hdr.node.nodeType = type;
75 list_init( &ret->entry );
76 list_init( &ret->children );
77 return ret;
80 void free_attribute( WS_XML_ATTRIBUTE *attr )
82 if (!attr) return;
83 free_xml_string( attr->prefix );
84 free_xml_string( attr->localName );
85 free_xml_string( attr->ns );
86 heap_free( attr->value );
87 heap_free( attr );
90 void free_node( struct node *node )
92 if (!node) return;
93 switch (node_type( node ))
95 case WS_XML_NODE_TYPE_ELEMENT:
97 WS_XML_ELEMENT_NODE *elem = &node->hdr;
98 ULONG i;
100 for (i = 0; i < elem->attributeCount; i++) free_attribute( elem->attributes[i] );
101 heap_free( elem->attributes );
102 free_xml_string( elem->prefix );
103 free_xml_string( elem->localName );
104 free_xml_string( elem->ns );
105 break;
107 case WS_XML_NODE_TYPE_TEXT:
109 WS_XML_TEXT_NODE *text = (WS_XML_TEXT_NODE *)node;
110 heap_free( text->text );
111 break;
113 case WS_XML_NODE_TYPE_COMMENT:
115 WS_XML_COMMENT_NODE *comment = (WS_XML_COMMENT_NODE *)node;
116 heap_free( comment->value.bytes );
117 break;
119 case WS_XML_NODE_TYPE_CDATA:
120 case WS_XML_NODE_TYPE_END_CDATA:
121 case WS_XML_NODE_TYPE_END_ELEMENT:
122 case WS_XML_NODE_TYPE_EOF:
123 case WS_XML_NODE_TYPE_BOF:
124 break;
126 default:
127 ERR( "unhandled type %u\n", node_type( node ) );
128 break;
130 heap_free( node );
133 void destroy_nodes( struct node *node )
135 struct list *ptr;
137 if (!node) return;
138 while ((ptr = list_head( &node->children )))
140 struct node *child = LIST_ENTRY( ptr, struct node, entry );
141 list_remove( &child->entry );
142 destroy_nodes( child );
144 free_node( node );
147 static WS_XML_ATTRIBUTE *dup_attribute( const WS_XML_ATTRIBUTE *src, WS_XML_WRITER_ENCODING_TYPE enc )
149 WS_XML_ATTRIBUTE *dst;
150 HRESULT hr;
152 if (!(dst = heap_alloc_zero( sizeof(*dst) ))) return NULL;
153 dst->singleQuote = src->singleQuote;
154 dst->isXmlNs = src->isXmlNs;
156 if (src->prefix && !(dst->prefix = dup_xml_string( src->prefix, FALSE ))) goto error;
157 if (src->localName && !(dst->localName = dup_xml_string( src->localName, FALSE ))) goto error;
158 if (src->ns && !(dst->ns = dup_xml_string( src->ns, FALSE ))) goto error;
160 if (src->value)
162 switch (enc)
164 case WS_XML_WRITER_ENCODING_TYPE_BINARY:
165 if ((hr = text_to_text( src->value, NULL, NULL, &dst->value )) != S_OK) goto error;
166 break;
168 case WS_XML_WRITER_ENCODING_TYPE_TEXT:
169 if ((hr = text_to_utf8text( src->value, NULL, NULL, (WS_XML_UTF8_TEXT **)&dst->value )) != S_OK)
170 goto error;
171 break;
173 default:
174 ERR( "unhandled encoding %u\n", enc );
175 goto error;
179 return dst;
181 error:
182 free_attribute( dst );
183 return NULL;
186 static WS_XML_ATTRIBUTE **dup_attributes( WS_XML_ATTRIBUTE * const *src, ULONG count,
187 WS_XML_WRITER_ENCODING_TYPE enc )
189 WS_XML_ATTRIBUTE **dst;
190 ULONG i;
192 if (!(dst = heap_alloc( sizeof(*dst) * count ))) return NULL;
193 for (i = 0; i < count; i++)
195 if (!(dst[i] = dup_attribute( src[i], enc )))
197 for (; i > 0; i--) free_attribute( dst[i - 1] );
198 heap_free( dst );
199 return NULL;
202 return dst;
205 static struct node *dup_element_node( const WS_XML_ELEMENT_NODE *src, WS_XML_WRITER_ENCODING_TYPE enc )
207 struct node *node;
208 WS_XML_ELEMENT_NODE *dst;
209 ULONG count = src->attributeCount;
210 WS_XML_ATTRIBUTE **attrs = src->attributes;
211 const WS_XML_STRING *prefix = (src->prefix && src->prefix->length) ? src->prefix : NULL;
212 const WS_XML_STRING *localname = src->localName;
213 const WS_XML_STRING *ns = src->ns;
215 if (!(node = alloc_node( WS_XML_NODE_TYPE_ELEMENT ))) return NULL;
216 dst = &node->hdr;
218 if (count && !(dst->attributes = dup_attributes( attrs, count, enc ))) goto error;
219 dst->attributeCount = count;
221 if (prefix && !(dst->prefix = dup_xml_string( prefix, FALSE ))) goto error;
222 if (localname && !(dst->localName = dup_xml_string( localname, FALSE ))) goto error;
223 if (ns && !(dst->ns = dup_xml_string( ns, FALSE ))) goto error;
224 return node;
226 error:
227 free_node( node );
228 return NULL;
231 static struct node *dup_text_node( const WS_XML_TEXT_NODE *src, WS_XML_WRITER_ENCODING_TYPE enc )
233 struct node *node;
234 WS_XML_TEXT_NODE *dst;
235 HRESULT hr;
237 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
238 dst = (WS_XML_TEXT_NODE *)node;
239 if (!src->text) return node;
241 switch (enc)
243 case WS_XML_WRITER_ENCODING_TYPE_BINARY:
244 hr = text_to_text( src->text, NULL, NULL, &dst->text );
245 break;
247 case WS_XML_WRITER_ENCODING_TYPE_TEXT:
248 hr = text_to_utf8text( src->text, NULL, NULL, (WS_XML_UTF8_TEXT **)&dst->text );
249 break;
251 default:
252 ERR( "unhandled encoding %u\n", enc );
253 free_node( node );
254 return NULL;
257 if (hr != S_OK)
259 free_node( node );
260 return NULL;
263 return node;
266 static struct node *dup_comment_node( const WS_XML_COMMENT_NODE *src )
268 struct node *node;
269 WS_XML_COMMENT_NODE *dst;
271 if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return NULL;
272 dst = (WS_XML_COMMENT_NODE *)node;
274 if (src->value.length && !(dst->value.bytes = heap_alloc( src->value.length )))
276 free_node( node );
277 return NULL;
279 memcpy( dst->value.bytes, src->value.bytes, src->value.length );
280 dst->value.length = src->value.length;
281 return node;
284 static struct node *dup_node( const struct node *src, WS_XML_WRITER_ENCODING_TYPE enc )
286 switch (node_type( src ))
288 case WS_XML_NODE_TYPE_ELEMENT:
289 return dup_element_node( &src->hdr, enc );
291 case WS_XML_NODE_TYPE_TEXT:
292 return dup_text_node( (const WS_XML_TEXT_NODE *)src, enc );
294 case WS_XML_NODE_TYPE_COMMENT:
295 return dup_comment_node( (const WS_XML_COMMENT_NODE *)src );
297 case WS_XML_NODE_TYPE_CDATA:
298 case WS_XML_NODE_TYPE_END_CDATA:
299 case WS_XML_NODE_TYPE_END_ELEMENT:
300 case WS_XML_NODE_TYPE_EOF:
301 case WS_XML_NODE_TYPE_BOF:
302 return alloc_node( node_type( src ) );
304 default:
305 ERR( "unhandled type %u\n", node_type( src ) );
306 break;
308 return NULL;
311 static HRESULT dup_tree( const struct node *src, WS_XML_WRITER_ENCODING_TYPE enc, struct node **dst )
313 struct node *parent;
314 const struct node *child;
316 if (!*dst && !(*dst = dup_node( src, enc ))) return E_OUTOFMEMORY;
317 parent = *dst;
319 LIST_FOR_EACH_ENTRY( child, &src->children, struct node, entry )
321 HRESULT hr = E_OUTOFMEMORY;
322 struct node *new_child;
324 if (!(new_child = dup_node( child, enc )) || (hr = dup_tree( child, enc, &new_child )) != S_OK)
326 destroy_nodes( *dst );
327 return hr;
329 new_child->parent = parent;
330 list_add_tail( &parent->children, &new_child->entry );
332 return S_OK;
335 static const struct prop_desc reader_props[] =
337 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_DEPTH */
338 { sizeof(BOOL), FALSE }, /* WS_XML_READER_PROPERTY_ALLOW_FRAGMENT */
339 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_ATTRIBUTES */
340 { sizeof(BOOL), FALSE }, /* WS_XML_READER_PROPERTY_READ_DECLARATION */
341 { sizeof(WS_CHARSET), FALSE }, /* WS_XML_READER_PROPERTY_CHARSET */
342 { sizeof(ULONGLONG), TRUE }, /* WS_XML_READER_PROPERTY_ROW */
343 { sizeof(ULONGLONG), TRUE }, /* WS_XML_READER_PROPERTY_COLUMN */
344 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_UTF8_TRIM_SIZE */
345 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_STREAM_BUFFER_SIZE */
346 { sizeof(BOOL), TRUE }, /* WS_XML_READER_PROPERTY_IN_ATTRIBUTE */
347 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_STREAM_MAX_ROOT_MIME_PART_SIZE */
348 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_STREAM_MAX_MIME_HEADERS_SIZE */
349 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_MIME_PARTS */
350 { sizeof(BOOL), FALSE }, /* WS_XML_READER_PROPERTY_ALLOW_INVALID_CHARACTER_REFERENCES */
351 { sizeof(ULONG), FALSE }, /* WS_XML_READER_PROPERTY_MAX_NAMESPACES */
354 enum reader_state
356 READER_STATE_INITIAL,
357 READER_STATE_BOF,
358 READER_STATE_STARTELEMENT,
359 READER_STATE_STARTATTRIBUTE,
360 READER_STATE_STARTCDATA,
361 READER_STATE_CDATA,
362 READER_STATE_TEXT,
363 READER_STATE_ENDELEMENT,
364 READER_STATE_ENDCDATA,
365 READER_STATE_COMMENT,
366 READER_STATE_EOF
369 struct prefix
371 WS_XML_STRING *str;
372 WS_XML_STRING *ns;
375 struct reader
377 ULONG magic;
378 CRITICAL_SECTION cs;
379 ULONG read_size;
380 ULONG read_pos;
381 const unsigned char *read_bufptr;
382 enum reader_state state;
383 struct node *root;
384 struct node *current;
385 ULONG current_attr;
386 struct node *last;
387 struct prefix *prefixes;
388 ULONG nb_prefixes;
389 ULONG nb_prefixes_allocated;
390 WS_XML_READER_ENCODING_TYPE input_enc;
391 WS_CHARSET input_charset;
392 WS_XML_READER_INPUT_TYPE input_type;
393 struct xmlbuf *input_buf;
394 const unsigned char *input_data;
395 ULONG input_size;
396 ULONG text_conv_offset;
397 const WS_XML_DICTIONARY *dict_static;
398 WS_XML_DICTIONARY *dict;
399 ULONG prop_count;
400 struct prop prop[sizeof(reader_props)/sizeof(reader_props[0])];
403 #define READER_MAGIC (('R' << 24) | ('E' << 16) | ('A' << 8) | 'D')
405 static struct reader *alloc_reader(void)
407 static const ULONG count = sizeof(reader_props)/sizeof(reader_props[0]);
408 struct reader *ret;
409 ULONG size = sizeof(*ret) + prop_size( reader_props, count );
411 if (!(ret = heap_alloc_zero( size ))) return NULL;
412 if (!(ret->prefixes = heap_alloc_zero( sizeof(*ret->prefixes) )))
414 heap_free( ret );
415 return NULL;
417 ret->nb_prefixes = ret->nb_prefixes_allocated = 1;
419 ret->magic = READER_MAGIC;
420 InitializeCriticalSection( &ret->cs );
421 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": reader.cs");
423 prop_init( reader_props, count, ret->prop, &ret[1] );
424 ret->prop_count = count;
425 return ret;
428 static void clear_prefixes( struct prefix *prefixes, ULONG count )
430 ULONG i;
431 for (i = 0; i < count; i++)
433 free_xml_string( prefixes[i].str );
434 prefixes[i].str = NULL;
435 free_xml_string( prefixes[i].ns );
436 prefixes[i].ns = NULL;
440 static HRESULT set_prefix( struct prefix *prefix, const WS_XML_STRING *str, const WS_XML_STRING *ns )
442 if (str)
444 free_xml_string( prefix->str );
445 if (!(prefix->str = dup_xml_string( str, FALSE ))) return E_OUTOFMEMORY;
447 if (prefix->ns) free_xml_string( prefix->ns );
448 if (!(prefix->ns = dup_xml_string( ns, FALSE ))) return E_OUTOFMEMORY;
449 return S_OK;
452 static HRESULT bind_prefix( struct reader *reader, const WS_XML_STRING *prefix, const WS_XML_STRING *ns )
454 ULONG i;
455 HRESULT hr;
457 for (i = 0; i < reader->nb_prefixes; i++)
459 if (WsXmlStringEquals( prefix, reader->prefixes[i].str, NULL ) == S_OK)
460 return set_prefix( &reader->prefixes[i], NULL, ns );
462 if (i >= reader->nb_prefixes_allocated)
464 ULONG new_size = reader->nb_prefixes_allocated * sizeof(*reader->prefixes) * 2;
465 struct prefix *tmp = heap_realloc_zero( reader->prefixes, new_size );
466 if (!tmp) return E_OUTOFMEMORY;
467 reader->prefixes = tmp;
468 reader->nb_prefixes_allocated *= 2;
470 if ((hr = set_prefix( &reader->prefixes[i], prefix, ns )) != S_OK) return hr;
471 reader->nb_prefixes++;
472 return S_OK;
475 static const WS_XML_STRING *get_namespace( struct reader *reader, const WS_XML_STRING *prefix )
477 ULONG i;
478 for (i = 0; i < reader->nb_prefixes; i++)
480 if (WsXmlStringEquals( prefix, reader->prefixes[i].str, NULL ) == S_OK)
481 return reader->prefixes[i].ns;
483 return NULL;
486 static void read_insert_eof( struct reader *reader, struct node *eof )
488 if (!reader->root) reader->root = eof;
489 else
491 eof->parent = reader->root;
492 list_add_tail( &reader->root->children, &eof->entry );
494 reader->current = reader->last = eof;
497 static void read_insert_bof( struct reader *reader, struct node *bof )
499 reader->root->parent = bof;
500 list_add_tail( &bof->children, &reader->root->entry );
501 reader->current = reader->last = reader->root = bof;
504 static void read_insert_node( struct reader *reader, struct node *parent, struct node *node )
506 node->parent = parent;
507 list_add_before( list_tail( &parent->children ), &node->entry );
508 reader->current = reader->last = node;
511 static void free_reader( struct reader *reader )
513 destroy_nodes( reader->root );
514 clear_prefixes( reader->prefixes, reader->nb_prefixes );
515 heap_free( reader->prefixes );
516 reader->cs.DebugInfo->Spare[0] = 0;
517 DeleteCriticalSection( &reader->cs );
518 heap_free( reader );
521 static HRESULT init_reader( struct reader *reader )
523 static const WS_XML_STRING empty = {0, NULL};
524 struct node *node;
525 HRESULT hr;
527 reader->state = READER_STATE_INITIAL;
528 destroy_nodes( reader->root );
529 reader->root = reader->current = NULL;
530 reader->current_attr = 0;
531 clear_prefixes( reader->prefixes, reader->nb_prefixes );
532 reader->nb_prefixes = 1;
533 if ((hr = bind_prefix( reader, &empty, &empty )) != S_OK) return hr;
535 if (!(node = alloc_node( WS_XML_NODE_TYPE_EOF ))) return E_OUTOFMEMORY;
536 read_insert_eof( reader, node );
537 reader->input_enc = WS_XML_READER_ENCODING_TYPE_TEXT;
538 reader->input_charset = WS_CHARSET_UTF8;
539 reader->dict_static = NULL;
540 reader->dict = NULL;
541 return S_OK;
544 /**************************************************************************
545 * WsCreateReader [webservices.@]
547 HRESULT WINAPI WsCreateReader( const WS_XML_READER_PROPERTY *properties, ULONG count,
548 WS_XML_READER **handle, WS_ERROR *error )
550 struct reader *reader;
551 ULONG i, max_depth = 32, max_attrs = 128, max_ns = 32;
552 BOOL read_decl = TRUE;
553 HRESULT hr;
555 TRACE( "%p %u %p %p\n", properties, count, handle, error );
556 if (error) FIXME( "ignoring error parameter\n" );
558 if (!handle) return E_INVALIDARG;
559 if (!(reader = alloc_reader())) return E_OUTOFMEMORY;
561 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_DEPTH, &max_depth, sizeof(max_depth) );
562 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_ATTRIBUTES, &max_attrs, sizeof(max_attrs) );
563 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_READ_DECLARATION, &read_decl, sizeof(read_decl) );
564 prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_NAMESPACES, &max_ns, sizeof(max_ns) );
566 for (i = 0; i < count; i++)
568 hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
569 properties[i].valueSize );
570 if (hr != S_OK)
572 free_reader( reader );
573 return hr;
577 if ((hr = init_reader( reader )) != S_OK)
579 free_reader( reader );
580 return hr;
583 TRACE( "created %p\n", reader );
584 *handle = (WS_XML_READER *)reader;
585 return S_OK;
588 /**************************************************************************
589 * WsFreeReader [webservices.@]
591 void WINAPI WsFreeReader( WS_XML_READER *handle )
593 struct reader *reader = (struct reader *)handle;
595 TRACE( "%p\n", handle );
597 if (!reader) return;
599 EnterCriticalSection( &reader->cs );
601 if (reader->magic != READER_MAGIC)
603 LeaveCriticalSection( &reader->cs );
604 return;
607 reader->magic = 0;
609 LeaveCriticalSection( &reader->cs );
610 free_reader( reader );
613 /**************************************************************************
614 * WsFillReader [webservices.@]
616 HRESULT WINAPI WsFillReader( WS_XML_READER *handle, ULONG min_size, const WS_ASYNC_CONTEXT *ctx,
617 WS_ERROR *error )
619 struct reader *reader = (struct reader *)handle;
621 TRACE( "%p %u %p %p\n", handle, min_size, ctx, error );
622 if (error) FIXME( "ignoring error parameter\n" );
624 if (!reader) return E_INVALIDARG;
626 EnterCriticalSection( &reader->cs );
628 if (reader->magic != READER_MAGIC)
630 LeaveCriticalSection( &reader->cs );
631 return E_INVALIDARG;
634 /* FIXME: add support for stream input */
635 reader->read_size = min( min_size, reader->input_size );
636 reader->read_pos = 0;
638 LeaveCriticalSection( &reader->cs );
639 return S_OK;
642 /**************************************************************************
643 * WsGetNamespaceFromPrefix [webservices.@]
645 HRESULT WINAPI WsGetNamespaceFromPrefix( WS_XML_READER *handle, const WS_XML_STRING *prefix,
646 BOOL required, const WS_XML_STRING **ns, WS_ERROR *error )
648 static const WS_XML_STRING xml = {3, (BYTE *)"xml"};
649 static const WS_XML_STRING xmlns = {5, (BYTE *)"xmlns"};
650 static const WS_XML_STRING empty_ns = {0, NULL};
651 static const WS_XML_STRING xml_ns = {36, (BYTE *)"http://www.w3.org/XML/1998/namespace"};
652 static const WS_XML_STRING xmlns_ns = {29, (BYTE *)"http://www.w3.org/2000/xmlns/"};
653 struct reader *reader = (struct reader *)handle;
654 BOOL found = FALSE;
656 TRACE( "%p %s %d %p %p\n", handle, debugstr_xmlstr(prefix), required, ns, error );
657 if (error) FIXME( "ignoring error parameter\n" );
659 if (!reader || !prefix || !ns) return E_INVALIDARG;
661 EnterCriticalSection( &reader->cs );
663 if (reader->magic != READER_MAGIC)
665 LeaveCriticalSection( &reader->cs );
666 return E_INVALIDARG;
669 if (reader->state != READER_STATE_STARTELEMENT)
671 LeaveCriticalSection( &reader->cs );
672 return WS_E_INVALID_OPERATION;
675 if (!prefix->length)
677 *ns = &empty_ns;
678 found = TRUE;
680 else if (WsXmlStringEquals( prefix, &xml, NULL ) == S_OK)
682 *ns = &xml_ns;
683 found = TRUE;
685 else if (WsXmlStringEquals( prefix, &xmlns, NULL ) == S_OK)
687 *ns = &xmlns_ns;
688 found = TRUE;
690 else
692 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
693 ULONG i;
695 for (i = 0; i < elem->attributeCount; i++)
697 if (!elem->attributes[i]->isXmlNs) continue;
698 if (WsXmlStringEquals( prefix, elem->attributes[i]->prefix, NULL ) == S_OK)
700 *ns = elem->attributes[i]->ns;
701 found = TRUE;
702 break;
707 LeaveCriticalSection( &reader->cs );
709 if (!found)
711 if (required) return WS_E_INVALID_FORMAT;
712 *ns = NULL;
713 return S_FALSE;
716 return S_OK;
719 /**************************************************************************
720 * WsGetReaderNode [webservices.@]
722 HRESULT WINAPI WsGetReaderNode( WS_XML_READER *handle, const WS_XML_NODE **node,
723 WS_ERROR *error )
725 struct reader *reader = (struct reader *)handle;
727 TRACE( "%p %p %p\n", handle, node, error );
728 if (error) FIXME( "ignoring error parameter\n" );
730 if (!reader || !node) return E_INVALIDARG;
732 EnterCriticalSection( &reader->cs );
734 if (reader->magic != READER_MAGIC)
736 LeaveCriticalSection( &reader->cs );
737 return E_INVALIDARG;
740 *node = &reader->current->hdr.node;
742 LeaveCriticalSection( &reader->cs );
743 return S_OK;
746 static HRESULT get_charset( struct reader *reader, void *buf, ULONG size )
748 if (!buf || size != sizeof(reader->input_charset)) return E_INVALIDARG;
749 if (!reader->input_charset) return WS_E_INVALID_FORMAT;
750 *(WS_CHARSET *)buf = reader->input_charset;
751 return S_OK;
754 /**************************************************************************
755 * WsGetReaderProperty [webservices.@]
757 HRESULT WINAPI WsGetReaderProperty( WS_XML_READER *handle, WS_XML_READER_PROPERTY_ID id,
758 void *buf, ULONG size, WS_ERROR *error )
760 struct reader *reader = (struct reader *)handle;
761 HRESULT hr;
763 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
764 if (error) FIXME( "ignoring error parameter\n" );
766 if (!reader) return E_INVALIDARG;
768 EnterCriticalSection( &reader->cs );
770 if (reader->magic != READER_MAGIC)
772 LeaveCriticalSection( &reader->cs );
773 return E_INVALIDARG;
776 if (!reader->input_type)
778 LeaveCriticalSection( &reader->cs );
779 return WS_E_INVALID_OPERATION;
782 if (id == WS_XML_READER_PROPERTY_CHARSET) hr = get_charset( reader, buf, size );
783 else hr = prop_get( reader->prop, reader->prop_count, id, buf, size );
785 LeaveCriticalSection( &reader->cs );
786 return hr;
789 /**************************************************************************
790 * WsGetXmlAttribute [webservices.@]
792 HRESULT WINAPI WsGetXmlAttribute( WS_XML_READER *handle, const WS_XML_STRING *attr,
793 WS_HEAP *heap, WCHAR **str, ULONG *len, WS_ERROR *error )
795 FIXME( "%p %s %p %p %p %p: stub\n", handle, debugstr_xmlstr(attr), heap, str, len, error );
796 return E_NOTIMPL;
799 WS_XML_UTF8_TEXT *alloc_utf8_text( const BYTE *data, ULONG len )
801 WS_XML_UTF8_TEXT *ret;
803 if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
804 ret->text.textType = WS_XML_TEXT_TYPE_UTF8;
805 ret->value.length = len;
806 ret->value.bytes = len ? (BYTE *)(ret + 1) : NULL;
807 ret->value.dictionary = NULL;
808 ret->value.id = 0;
809 if (data) memcpy( ret->value.bytes, data, len );
810 return ret;
813 WS_XML_UTF16_TEXT *alloc_utf16_text( const BYTE *data, ULONG len )
815 WS_XML_UTF16_TEXT *ret;
817 if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
818 ret->text.textType = WS_XML_TEXT_TYPE_UTF16;
819 ret->byteCount = len;
820 ret->bytes = len ? (BYTE *)(ret + 1) : NULL;
821 if (data) memcpy( ret->bytes, data, len );
822 return ret;
825 WS_XML_BASE64_TEXT *alloc_base64_text( const BYTE *data, ULONG len )
827 WS_XML_BASE64_TEXT *ret;
829 if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
830 ret->text.textType = WS_XML_TEXT_TYPE_BASE64;
831 ret->length = len;
832 ret->bytes = len ? (BYTE *)(ret + 1) : NULL;
833 if (data) memcpy( ret->bytes, data, len );
834 return ret;
837 WS_XML_BOOL_TEXT *alloc_bool_text( BOOL value )
839 WS_XML_BOOL_TEXT *ret;
841 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
842 ret->text.textType = WS_XML_TEXT_TYPE_BOOL;
843 ret->value = value;
844 return ret;
847 WS_XML_INT32_TEXT *alloc_int32_text( INT32 value )
849 WS_XML_INT32_TEXT *ret;
851 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
852 ret->text.textType = WS_XML_TEXT_TYPE_INT32;
853 ret->value = value;
854 return ret;
857 WS_XML_INT64_TEXT *alloc_int64_text( INT64 value )
859 WS_XML_INT64_TEXT *ret;
861 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
862 ret->text.textType = WS_XML_TEXT_TYPE_INT64;
863 ret->value = value;
864 return ret;
867 WS_XML_UINT64_TEXT *alloc_uint64_text( UINT64 value )
869 WS_XML_UINT64_TEXT *ret;
871 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
872 ret->text.textType = WS_XML_TEXT_TYPE_UINT64;
873 ret->value = value;
874 return ret;
877 WS_XML_FLOAT_TEXT *alloc_float_text( float value )
879 WS_XML_FLOAT_TEXT *ret;
881 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
882 ret->text.textType = WS_XML_TEXT_TYPE_FLOAT;
883 ret->value = value;
884 return ret;
887 WS_XML_DOUBLE_TEXT *alloc_double_text( double value )
889 WS_XML_DOUBLE_TEXT *ret;
891 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
892 ret->text.textType = WS_XML_TEXT_TYPE_DOUBLE;
893 ret->value = value;
894 return ret;
897 WS_XML_GUID_TEXT *alloc_guid_text( const GUID *value )
899 WS_XML_GUID_TEXT *ret;
901 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
902 ret->text.textType = WS_XML_TEXT_TYPE_GUID;
903 ret->value = *value;
904 return ret;
907 WS_XML_UNIQUE_ID_TEXT *alloc_unique_id_text( const GUID *value )
909 WS_XML_UNIQUE_ID_TEXT *ret;
911 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
912 ret->text.textType = WS_XML_TEXT_TYPE_UNIQUE_ID;
913 ret->value = *value;
914 return ret;
917 WS_XML_DATETIME_TEXT *alloc_datetime_text( const WS_DATETIME *value )
919 WS_XML_DATETIME_TEXT *ret;
921 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
922 ret->text.textType = WS_XML_TEXT_TYPE_DATETIME;
923 ret->value = *value;
924 return ret;
927 static inline BOOL read_end_of_data( struct reader *reader )
929 return reader->read_pos >= reader->read_size;
932 static inline const unsigned char *read_current_ptr( struct reader *reader )
934 return &reader->read_bufptr[reader->read_pos];
937 static inline HRESULT read_peek( struct reader *reader, unsigned char *byte )
939 if (reader->read_pos >= reader->read_size) return WS_E_INVALID_FORMAT;
940 *byte = reader->read_bufptr[reader->read_pos];
941 return S_OK;
944 static inline HRESULT read_byte( struct reader *reader, unsigned char *byte )
946 if (reader->read_pos >= reader->read_size) return WS_E_INVALID_FORMAT;
947 *byte = reader->read_bufptr[reader->read_pos++];
948 return S_OK;
951 static inline HRESULT read_bytes( struct reader *reader, unsigned char *bytes, unsigned int len )
953 if (reader->read_pos + len > reader->read_size) return WS_E_INVALID_FORMAT;
954 memcpy( bytes, reader->read_bufptr + reader->read_pos, len );
955 reader->read_pos += len;
956 return S_OK;
959 /* UTF-8 support based on libs/wine/utf8.c */
961 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
962 static const char utf8_length[128] =
964 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
965 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
966 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
967 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
968 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
969 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
970 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
971 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
974 /* first byte mask depending on UTF-8 sequence length */
975 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
977 /* minimum Unicode value depending on UTF-8 sequence length */
978 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
980 static inline unsigned int read_utf8_char( struct reader *reader, unsigned int *skip )
982 unsigned int len, res;
983 unsigned char ch = reader->read_bufptr[reader->read_pos];
984 const unsigned char *end;
986 if (reader->read_pos >= reader->read_size) return 0;
988 if (ch < 0x80)
990 *skip = 1;
991 return ch;
993 len = utf8_length[ch - 0x80];
994 if (reader->read_pos + len >= reader->read_size) return 0;
995 end = reader->read_bufptr + reader->read_pos + len + 1;
996 res = ch & utf8_mask[len];
998 switch (len)
1000 case 3:
1001 if ((ch = end[-3] ^ 0x80) >= 0x40) break;
1002 res = (res << 6) | ch;
1003 case 2:
1004 if ((ch = end[-2] ^ 0x80) >= 0x40) break;
1005 res = (res << 6) | ch;
1006 case 1:
1007 if ((ch = end[-1] ^ 0x80) >= 0x40) break;
1008 res = (res << 6) | ch;
1009 if (res < utf8_minval[len]) break;
1010 *skip = len + 1;
1011 return res;
1014 return 0;
1017 static inline void read_skip( struct reader *reader, unsigned int count )
1019 if (reader->read_pos + count > reader->read_size) return;
1020 reader->read_pos += count;
1023 static inline void read_rewind( struct reader *reader, unsigned int count )
1025 reader->read_pos -= count;
1028 static inline BOOL read_isnamechar( unsigned int ch )
1030 /* FIXME: incomplete */
1031 return (ch >= 'A' && ch <= 'Z') ||
1032 (ch >= 'a' && ch <= 'z') ||
1033 (ch >= '0' && ch <= '9') ||
1034 ch == '_' || ch == '-' || ch == '.' || ch == ':';
1037 static inline BOOL read_isspace( unsigned int ch )
1039 return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
1042 static inline void read_skip_whitespace( struct reader *reader )
1044 while (reader->read_pos < reader->read_size && read_isspace( reader->read_bufptr[reader->read_pos] ))
1045 reader->read_pos++;
1048 static inline int read_cmp( struct reader *reader, const char *str, int len )
1050 const unsigned char *ptr = read_current_ptr( reader );
1052 if (len < 0) len = strlen( str );
1053 if (reader->read_pos + len > reader->read_size) return -1;
1054 while (len--)
1056 if (*str != *ptr) return *ptr - *str;
1057 str++; ptr++;
1059 return 0;
1062 static HRESULT read_xmldecl( struct reader *reader )
1064 if (!reader->read_size) return WS_E_INVALID_FORMAT;
1066 if (read_cmp( reader, "<", 1 ) || read_cmp( reader, "<?", 2 ))
1068 reader->state = READER_STATE_BOF;
1069 return S_OK;
1071 if (read_cmp( reader, "<?xml ", 6 )) return WS_E_INVALID_FORMAT;
1072 read_skip( reader, 6 );
1074 /* FIXME: parse attributes */
1075 while (reader->read_pos < reader->read_size && reader->read_bufptr[reader->read_pos] != '?')
1076 reader->read_pos++;
1078 if (read_cmp( reader, "?>", 2 )) return WS_E_INVALID_FORMAT;
1079 read_skip( reader, 2 );
1081 reader->state = READER_STATE_BOF;
1082 return S_OK;
1085 HRESULT append_attribute( WS_XML_ELEMENT_NODE *elem, WS_XML_ATTRIBUTE *attr )
1087 if (elem->attributeCount)
1089 WS_XML_ATTRIBUTE **tmp;
1090 if (!(tmp = heap_realloc( elem->attributes, (elem->attributeCount + 1) * sizeof(attr) )))
1091 return E_OUTOFMEMORY;
1092 elem->attributes = tmp;
1094 else if (!(elem->attributes = heap_alloc( sizeof(attr) ))) return E_OUTOFMEMORY;
1095 elem->attributes[elem->attributeCount++] = attr;
1096 return S_OK;
1099 static inline void init_xml_string( BYTE *bytes, ULONG len, WS_XML_STRING *str )
1101 str->length = len;
1102 str->bytes = bytes;
1103 str->dictionary = NULL;
1104 str->id = 0;
1107 static HRESULT split_qname( const BYTE *str, ULONG len, WS_XML_STRING *prefix, WS_XML_STRING *localname )
1109 BYTE *prefix_bytes = NULL, *localname_bytes = (BYTE *)str, *ptr = (BYTE *)str;
1110 ULONG prefix_len = 0, localname_len = len;
1112 while (len--)
1114 if (*ptr == ':')
1116 if (ptr == str) return WS_E_INVALID_FORMAT;
1117 prefix_bytes = (BYTE *)str;
1118 prefix_len = ptr - str;
1119 localname_bytes = ptr + 1;
1120 localname_len = len;
1121 break;
1123 ptr++;
1125 if (!localname_len) return WS_E_INVALID_FORMAT;
1127 init_xml_string( prefix_bytes, prefix_len, prefix );
1128 init_xml_string( localname_bytes, localname_len, localname );
1129 return S_OK;
1132 static HRESULT parse_qname( const BYTE *str, ULONG len, WS_XML_STRING **prefix_ret, WS_XML_STRING **localname_ret )
1134 WS_XML_STRING prefix, localname;
1135 HRESULT hr;
1137 if ((hr = split_qname( str, len, &prefix, &localname )) != S_OK) return hr;
1138 if (!(*prefix_ret = alloc_xml_string( NULL, prefix.length ))) return E_OUTOFMEMORY;
1139 if (!(*localname_ret = dup_xml_string( &localname, FALSE )))
1141 free_xml_string( *prefix_ret );
1142 return E_OUTOFMEMORY;
1144 memcpy( (*prefix_ret)->bytes, prefix.bytes, prefix.length );
1145 if (prefix.length && add_xml_string( *prefix_ret ) != S_OK) WARN( "prefix not added to dictionary\n" );
1146 return S_OK;
1149 static int codepoint_to_utf8( int cp, unsigned char *dst )
1151 if (!cp) return -1;
1152 if (cp < 0x80)
1154 *dst = cp;
1155 return 1;
1157 if (cp < 0x800)
1159 dst[1] = 0x80 | (cp & 0x3f);
1160 cp >>= 6;
1161 dst[0] = 0xc0 | cp;
1162 return 2;
1164 if ((cp >= 0xd800 && cp <= 0xdfff) || cp == 0xfffe || cp == 0xffff) return -1;
1165 if (cp < 0x10000)
1167 dst[2] = 0x80 | (cp & 0x3f);
1168 cp >>= 6;
1169 dst[1] = 0x80 | (cp & 0x3f);
1170 cp >>= 6;
1171 dst[0] = 0xe0 | cp;
1172 return 3;
1174 if (cp >= 0x110000) return -1;
1175 dst[3] = 0x80 | (cp & 0x3f);
1176 cp >>= 6;
1177 dst[2] = 0x80 | (cp & 0x3f);
1178 cp >>= 6;
1179 dst[1] = 0x80 | (cp & 0x3f);
1180 cp >>= 6;
1181 dst[0] = 0xf0 | cp;
1182 return 4;
1185 static HRESULT decode_text( const unsigned char *str, ULONG len, unsigned char *ret, ULONG *ret_len )
1187 const unsigned char *p = str;
1188 unsigned char *q = ret;
1190 *ret_len = 0;
1191 while (len)
1193 if (*p == '&')
1195 p++; len--;
1196 if (!len) return WS_E_INVALID_FORMAT;
1198 if (len >= 3 && !memcmp( p, "lt;", 3 ))
1200 *q++ = '<';
1201 p += 3;
1202 len -= 3;
1204 else if (len >= 3 && !memcmp( p, "gt;", 3 ))
1206 *q++ = '>';
1207 p += 3;
1208 len -= 3;
1210 else if (len >= 5 && !memcmp( p, "quot;", 5 ))
1212 *q++ = '"';
1213 p += 5;
1214 len -= 5;
1216 else if (len >= 4 && !memcmp( p, "amp;", 4 ))
1218 *q++ = '&';
1219 p += 4;
1220 len -= 4;
1222 else if (len >= 5 && !memcmp( p, "apos;", 5 ))
1224 *q++ = '\'';
1225 p += 5;
1226 len -= 5;
1228 else if (*p == '#')
1230 ULONG start, nb_digits, i;
1231 int len_utf8, cp = 0;
1233 p++; len--;
1234 if (!len) return WS_E_INVALID_FORMAT;
1235 if (*p == 'x')
1237 p++; len--;
1239 start = len;
1240 while (len && isxdigit( *p )) { p++; len--; };
1241 if (!len) return WS_E_INVALID_FORMAT;
1243 p -= nb_digits = start - len;
1244 if (!nb_digits || nb_digits > 6 || p[nb_digits] != ';') return WS_E_INVALID_FORMAT;
1245 for (i = 0; i < nb_digits; i++)
1247 cp *= 16;
1248 if (*p >= '0' && *p <= '9') cp += *p - '0';
1249 else if (*p >= 'a' && *p <= 'f') cp += *p - 'a' + 10;
1250 else cp += *p - 'A' + 10;
1251 p++;
1254 else if (isdigit( *p ))
1256 while (len && *p == '0') { p++; len--; };
1257 if (!len) return WS_E_INVALID_FORMAT;
1259 start = len;
1260 while (len && isdigit( *p )) { p++; len--; };
1261 if (!len) return WS_E_INVALID_FORMAT;
1263 p -= nb_digits = start - len;
1264 if (!nb_digits || nb_digits > 7 || p[nb_digits] != ';') return WS_E_INVALID_FORMAT;
1265 for (i = 0; i < nb_digits; i++)
1267 cp *= 10;
1268 cp += *p - '0';
1269 p++;
1272 else return WS_E_INVALID_FORMAT;
1273 p++; len--;
1274 if ((len_utf8 = codepoint_to_utf8( cp, q )) < 0) return WS_E_INVALID_FORMAT;
1275 *ret_len += len_utf8;
1276 q += len_utf8;
1277 continue;
1279 else return WS_E_INVALID_FORMAT;
1281 else
1283 *q++ = *p++;
1284 len--;
1286 *ret_len += 1;
1288 return S_OK;
1291 static HRESULT read_attribute_value_text( struct reader *reader, WS_XML_ATTRIBUTE *attr )
1293 WS_XML_UTF8_TEXT *utf8 = NULL;
1294 unsigned int len, ch, skip, quote;
1295 const unsigned char *start;
1296 HRESULT hr = E_OUTOFMEMORY;
1298 read_skip_whitespace( reader );
1299 if (read_cmp( reader, "=", 1 )) return WS_E_INVALID_FORMAT;
1300 read_skip( reader, 1 );
1302 read_skip_whitespace( reader );
1303 if (read_cmp( reader, "\"", 1 ) && read_cmp( reader, "'", 1 )) return WS_E_INVALID_FORMAT;
1304 quote = read_utf8_char( reader, &skip );
1305 read_skip( reader, 1 );
1307 len = 0;
1308 start = read_current_ptr( reader );
1309 for (;;)
1311 if (!(ch = read_utf8_char( reader, &skip ))) return WS_E_INVALID_FORMAT;
1312 if (ch == quote) break;
1313 read_skip( reader, skip );
1314 len += skip;
1316 read_skip( reader, 1 );
1318 if (attr->isXmlNs)
1320 if (!(attr->ns = alloc_xml_string( start, len ))) goto error;
1321 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1322 if (!(utf8 = alloc_utf8_text( NULL, 0 )))
1324 hr = E_OUTOFMEMORY;
1325 goto error;
1328 else
1330 if (!(utf8 = alloc_utf8_text( NULL, len ))) goto error;
1331 if ((hr = decode_text( start, len, utf8->value.bytes, &utf8->value.length )) != S_OK) goto error;
1334 attr->value = &utf8->text;
1335 attr->singleQuote = (quote == '\'');
1336 return S_OK;
1338 error:
1339 heap_free( utf8 );
1340 return hr;
1343 static inline BOOL is_text_type( unsigned char type )
1345 return (type >= RECORD_ZERO_TEXT && type <= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT);
1348 static HRESULT read_int31( struct reader *reader, ULONG *len )
1350 unsigned char byte;
1351 HRESULT hr;
1353 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1354 *len = byte & 0x7f;
1355 if (!(byte & 0x80)) return S_OK;
1357 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1358 *len += (byte & 0x7f) << 7;
1359 if (!(byte & 0x80)) return S_OK;
1361 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1362 *len += (byte & 0x7f) << 14;
1363 if (!(byte & 0x80)) return S_OK;
1365 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1366 *len += (byte & 0x7f) << 21;
1367 if (!(byte & 0x80)) return S_OK;
1369 if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
1370 *len += (byte & 0x07) << 28;
1371 return S_OK;
1374 static HRESULT read_string( struct reader *reader, WS_XML_STRING **str )
1376 ULONG len;
1377 HRESULT hr;
1378 if ((hr = read_int31( reader, &len )) != S_OK) return hr;
1379 if (!(*str = alloc_xml_string( NULL, len ))) return E_OUTOFMEMORY;
1380 if ((hr = read_bytes( reader, (*str)->bytes, len )) == S_OK)
1382 if (add_xml_string( *str ) != S_OK) WARN( "string not added to dictionary\n" );
1383 return S_OK;
1385 free_xml_string( *str );
1386 return hr;
1389 static HRESULT read_dict_string( struct reader *reader, WS_XML_STRING **str )
1391 const WS_XML_DICTIONARY *dict;
1392 HRESULT hr;
1393 ULONG id;
1395 if ((hr = read_int31( reader, &id )) != S_OK) return hr;
1396 dict = (id & 1) ? reader->dict : reader->dict_static;
1397 if (!dict || (id >>= 1) >= dict->stringCount) return WS_E_INVALID_FORMAT;
1398 if (!(*str = alloc_xml_string( NULL, 0 ))) return E_OUTOFMEMORY;
1399 *(*str) = dict->strings[id];
1400 return S_OK;
1403 static HRESULT read_datetime( struct reader *reader, WS_DATETIME *ret )
1405 UINT64 val;
1406 HRESULT hr;
1408 if ((hr = read_bytes( reader, (unsigned char *)&val, sizeof(val) )) != S_OK) return hr;
1410 if ((val & 0x03) == 1) ret->format = WS_DATETIME_FORMAT_UTC;
1411 else if ((val & 0x03) == 2) ret->format = WS_DATETIME_FORMAT_LOCAL;
1412 else ret->format = WS_DATETIME_FORMAT_NONE;
1414 if ((ret->ticks = val >> 2) > TICKS_MAX) return WS_E_INVALID_FORMAT;
1415 return S_OK;
1418 static HRESULT lookup_string( struct reader *reader, ULONG id, const WS_XML_STRING **ret )
1420 const WS_XML_DICTIONARY *dict = (id & 1) ? reader->dict : reader->dict_static;
1421 if (!dict || (id >>= 1) >= dict->stringCount) return WS_E_INVALID_FORMAT;
1422 *ret = &dict->strings[id];
1423 return S_OK;
1426 static HRESULT read_attribute_value_bin( struct reader *reader, WS_XML_ATTRIBUTE *attr )
1428 WS_XML_UTF8_TEXT *text_utf8 = NULL;
1429 WS_XML_BASE64_TEXT *text_base64 = NULL;
1430 WS_XML_INT32_TEXT *text_int32;
1431 WS_XML_INT64_TEXT *text_int64;
1432 WS_XML_BOOL_TEXT *text_bool;
1433 const WS_XML_STRING *str;
1434 unsigned char type;
1435 UINT8 val_uint8;
1436 UINT16 val_uint16;
1437 INT32 val_int32;
1438 ULONG len = 0, id;
1439 GUID guid;
1440 HRESULT hr;
1442 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
1443 if (!is_text_type( type )) return WS_E_INVALID_FORMAT;
1445 switch (type)
1447 case RECORD_ZERO_TEXT:
1449 if (!(text_int32 = alloc_int32_text( 0 ))) return E_OUTOFMEMORY;
1450 attr->value = &text_int32->text;
1451 return S_OK;
1453 case RECORD_ONE_TEXT:
1455 if (!(text_int32 = alloc_int32_text( 1 ))) return E_OUTOFMEMORY;
1456 attr->value = &text_int32->text;
1457 return S_OK;
1459 case RECORD_FALSE_TEXT:
1461 if (!(text_bool = alloc_bool_text( FALSE ))) return E_OUTOFMEMORY;
1462 attr->value = &text_bool->text;
1463 return S_OK;
1465 case RECORD_TRUE_TEXT:
1467 if (!(text_bool = alloc_bool_text( TRUE ))) return E_OUTOFMEMORY;
1468 attr->value = &text_bool->text;
1469 return S_OK;
1471 case RECORD_INT8_TEXT:
1473 INT8 val_int8;
1474 if ((hr = read_byte( reader, (unsigned char *)&val_int8 )) != S_OK) return hr;
1475 if (!(text_int64 = alloc_int64_text( val_int8 ))) return E_OUTOFMEMORY;
1476 attr->value = &text_int64->text;
1477 return S_OK;
1479 case RECORD_INT16_TEXT:
1481 INT16 val_int16;
1482 if ((hr = read_bytes( reader, (unsigned char *)&val_int16, sizeof(val_int16) )) != S_OK) return hr;
1483 if (!(text_int64 = alloc_int64_text( val_int16 ))) return E_OUTOFMEMORY;
1484 attr->value = &text_int64->text;
1485 return S_OK;
1487 case RECORD_INT32_TEXT:
1488 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1489 if (!(text_int64 = alloc_int64_text( val_int32 ))) return E_OUTOFMEMORY;
1490 attr->value = &text_int64->text;
1491 return S_OK;
1493 case RECORD_INT64_TEXT:
1495 INT64 val_int64;
1496 if ((hr = read_bytes( reader, (unsigned char *)&val_int64, sizeof(val_int64) )) != S_OK) return hr;
1497 if (!(text_int64 = alloc_int64_text( val_int64 ))) return E_OUTOFMEMORY;
1498 attr->value = &text_int64->text;
1499 return S_OK;
1501 case RECORD_FLOAT_TEXT:
1503 WS_XML_FLOAT_TEXT *text_float;
1504 float val_float;
1506 if ((hr = read_bytes( reader, (unsigned char *)&val_float, sizeof(val_float) )) != S_OK) return hr;
1507 if (!(text_float = alloc_float_text( val_float ))) return E_OUTOFMEMORY;
1508 attr->value = &text_float->text;
1509 return S_OK;
1511 case RECORD_DOUBLE_TEXT:
1513 WS_XML_DOUBLE_TEXT *text_double;
1514 double val_double;
1516 if ((hr = read_bytes( reader, (unsigned char *)&val_double, sizeof(val_double) )) != S_OK) return hr;
1517 if (!(text_double = alloc_double_text( val_double ))) return E_OUTOFMEMORY;
1518 attr->value = &text_double->text;
1519 return S_OK;
1521 case RECORD_DATETIME_TEXT:
1523 WS_XML_DATETIME_TEXT *text_datetime;
1524 WS_DATETIME datetime;
1526 if ((hr = read_datetime( reader, &datetime )) != S_OK) return hr;
1527 if (!(text_datetime = alloc_datetime_text( &datetime ))) return E_OUTOFMEMORY;
1528 attr->value = &text_datetime->text;
1529 return S_OK;
1531 case RECORD_CHARS8_TEXT:
1532 if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
1533 len = val_uint8;
1534 break;
1536 case RECORD_CHARS16_TEXT:
1537 if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
1538 len = val_uint16;
1539 break;
1541 case RECORD_CHARS32_TEXT:
1542 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1543 if (val_int32 < 0) return WS_E_INVALID_FORMAT;
1544 len = val_int32;
1545 break;
1547 case RECORD_BYTES8_TEXT:
1548 if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
1549 if (!(text_base64 = alloc_base64_text( NULL, val_uint8 ))) return E_OUTOFMEMORY;
1550 if ((hr = read_bytes( reader, text_base64->bytes, val_uint8 )) != S_OK)
1552 heap_free( text_base64 );
1553 return hr;
1555 break;
1557 case RECORD_BYTES16_TEXT:
1558 if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
1559 if (!(text_base64 = alloc_base64_text( NULL, val_uint16 ))) return E_OUTOFMEMORY;
1560 if ((hr = read_bytes( reader, text_base64->bytes, val_uint16 )) != S_OK)
1562 heap_free( text_base64 );
1563 return hr;
1565 break;
1567 case RECORD_BYTES32_TEXT:
1568 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1569 if (val_int32 < 0) return WS_E_INVALID_FORMAT;
1570 if (!(text_base64 = alloc_base64_text( NULL, val_int32 ))) return E_OUTOFMEMORY;
1571 if ((hr = read_bytes( reader, text_base64->bytes, val_int32 )) != S_OK)
1573 heap_free( text_base64 );
1574 return hr;
1576 break;
1578 case RECORD_EMPTY_TEXT:
1579 break;
1581 case RECORD_DICTIONARY_TEXT:
1582 if ((hr = read_int31( reader, &id )) != S_OK) return hr;
1583 if ((hr = lookup_string( reader, id, &str )) != S_OK) return hr;
1584 if (!(text_utf8 = alloc_utf8_text( str->bytes, str->length ))) return E_OUTOFMEMORY;
1585 break;
1587 case RECORD_UNIQUE_ID_TEXT:
1589 WS_XML_UNIQUE_ID_TEXT *text_unique_id;
1590 if ((hr = read_bytes( reader, (unsigned char *)&guid, sizeof(guid) )) != S_OK) return hr;
1591 if (!(text_unique_id = alloc_unique_id_text( &guid ))) return E_OUTOFMEMORY;
1592 attr->value = &text_unique_id->text;
1593 return S_OK;
1595 case RECORD_GUID_TEXT:
1597 WS_XML_GUID_TEXT *guid_text;
1598 if ((hr = read_bytes( reader, (unsigned char *)&guid, sizeof(guid) )) != S_OK) return hr;
1599 if (!(guid_text = alloc_guid_text( &guid ))) return E_OUTOFMEMORY;
1600 attr->value = &guid_text->text;
1601 return S_OK;
1603 case RECORD_UINT64_TEXT:
1605 WS_XML_UINT64_TEXT *text_uint64;
1606 UINT64 val_uint64;
1608 if ((hr = read_bytes( reader, (unsigned char *)&val_uint64, sizeof(val_uint64) )) != S_OK) return hr;
1609 if (!(text_uint64 = alloc_uint64_text( val_uint64 ))) return E_OUTOFMEMORY;
1610 attr->value = &text_uint64->text;
1611 return S_OK;
1613 case RECORD_BOOL_TEXT:
1615 WS_XML_BOOL_TEXT *text_bool;
1616 BOOL val_bool;
1618 if ((hr = read_bytes( reader, (unsigned char *)&val_bool, sizeof(val_bool) )) != S_OK) return hr;
1619 if (!(text_bool = alloc_bool_text( !!val_bool ))) return E_OUTOFMEMORY;
1620 attr->value = &text_bool->text;
1621 return S_OK;
1623 default:
1624 ERR( "unhandled record type %02x\n", type );
1625 return WS_E_NOT_SUPPORTED;
1628 if (type >= RECORD_BYTES8_TEXT && type <= RECORD_BYTES32_TEXT)
1630 attr->value = &text_base64->text;
1631 return S_OK;
1634 if (!text_utf8)
1636 if (!(text_utf8 = alloc_utf8_text( NULL, len ))) return E_OUTOFMEMORY;
1637 if (!len) text_utf8->value.bytes = (BYTE *)(text_utf8 + 1); /* quirk */
1638 if ((hr = read_bytes( reader, text_utf8->value.bytes, len )) != S_OK)
1640 heap_free( text_utf8 );
1641 return hr;
1645 attr->value = &text_utf8->text;
1646 return S_OK;
1649 static HRESULT read_attribute_text( struct reader *reader, WS_XML_ATTRIBUTE **ret )
1651 static const WS_XML_STRING xmlns = {5, (BYTE *)"xmlns"};
1652 WS_XML_ATTRIBUTE *attr;
1653 unsigned int len = 0, ch, skip;
1654 const unsigned char *start;
1655 WS_XML_STRING *prefix, *localname;
1656 HRESULT hr = WS_E_INVALID_FORMAT;
1658 if (!(attr = heap_alloc_zero( sizeof(*attr) ))) return E_OUTOFMEMORY;
1660 start = read_current_ptr( reader );
1661 for (;;)
1663 if (!(ch = read_utf8_char( reader, &skip ))) goto error;
1664 if (!read_isnamechar( ch )) break;
1665 read_skip( reader, skip );
1666 len += skip;
1668 if (!len) goto error;
1670 if ((hr = parse_qname( start, len, &prefix, &localname )) != S_OK) goto error;
1671 if (WsXmlStringEquals( prefix, &xmlns, NULL ) == S_OK)
1673 free_xml_string( prefix );
1674 attr->isXmlNs = 1;
1675 if (!(attr->prefix = alloc_xml_string( localname->bytes, localname->length )))
1677 free_xml_string( localname );
1678 hr = E_OUTOFMEMORY;
1679 goto error;
1681 attr->localName = localname;
1683 else if (!prefix->length && WsXmlStringEquals( localname, &xmlns, NULL ) == S_OK)
1685 attr->isXmlNs = 1;
1686 attr->prefix = prefix;
1687 attr->localName = localname;
1689 else
1691 attr->prefix = prefix;
1692 attr->localName = localname;
1695 if ((hr = read_attribute_value_text( reader, attr )) != S_OK) goto error;
1697 *ret = attr;
1698 return S_OK;
1700 error:
1701 free_attribute( attr );
1702 return hr;
1705 static inline BOOL is_attribute_type( unsigned char type )
1707 return (type >= RECORD_SHORT_ATTRIBUTE && type <= RECORD_PREFIX_ATTRIBUTE_Z);
1710 static WS_XML_STRING *get_xmlns_localname( struct reader *reader, const WS_XML_STRING *prefix )
1712 if (!get_namespace( reader, prefix )) return alloc_xml_string( NULL, 0 );
1713 return alloc_xml_string( prefix->bytes, prefix->length );
1716 static HRESULT read_attribute_bin( struct reader *reader, WS_XML_ATTRIBUTE **ret )
1718 WS_XML_UTF8_TEXT *utf8;
1719 WS_XML_ATTRIBUTE *attr;
1720 unsigned char type = 0;
1721 HRESULT hr;
1723 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
1724 if (!is_attribute_type( type )) return WS_E_INVALID_FORMAT;
1725 if (!(attr = heap_alloc_zero( sizeof(*attr) ))) return E_OUTOFMEMORY;
1727 if (type >= RECORD_PREFIX_ATTRIBUTE_A && type <= RECORD_PREFIX_ATTRIBUTE_Z)
1729 unsigned char ch = type - RECORD_PREFIX_ATTRIBUTE_A + 'a';
1730 if (!(attr->prefix = alloc_xml_string( &ch, 1 )))
1732 hr = E_OUTOFMEMORY;
1733 goto error;
1735 if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
1736 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1738 else if (type >= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A && type <= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_Z)
1740 unsigned char ch = type - RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A + 'a';
1741 if (!(attr->prefix = alloc_xml_string( &ch, 1 )))
1743 hr = E_OUTOFMEMORY;
1744 goto error;
1746 if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
1747 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1749 else
1751 switch (type)
1753 case RECORD_SHORT_ATTRIBUTE:
1754 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1756 hr = E_OUTOFMEMORY;
1757 goto error;
1759 if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
1760 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1761 break;
1763 case RECORD_ATTRIBUTE:
1764 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1765 if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
1766 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1767 break;
1769 case RECORD_SHORT_DICTIONARY_ATTRIBUTE:
1770 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1772 hr = E_OUTOFMEMORY;
1773 goto error;
1775 if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
1776 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1777 break;
1779 case RECORD_DICTIONARY_ATTRIBUTE:
1780 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1781 if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
1782 if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
1783 break;
1785 case RECORD_SHORT_XMLNS_ATTRIBUTE:
1786 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1788 hr = E_OUTOFMEMORY;
1789 goto error;
1791 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1793 hr = E_OUTOFMEMORY;
1794 goto error;
1796 if ((hr = read_string( reader, &attr->ns )) != S_OK) goto error;
1797 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1798 attr->isXmlNs = 1;
1799 break;
1801 case RECORD_XMLNS_ATTRIBUTE:
1802 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1803 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1805 hr = E_OUTOFMEMORY;
1806 goto error;
1808 if ((hr = read_string( reader, &attr->ns )) != S_OK) goto error;
1809 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1810 attr->isXmlNs = 1;
1811 break;
1813 case RECORD_SHORT_DICTIONARY_XMLNS_ATTRIBUTE:
1814 if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
1816 hr = E_OUTOFMEMORY;
1817 goto error;
1819 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1821 hr = E_OUTOFMEMORY;
1822 goto error;
1824 if ((hr = read_dict_string( reader, &attr->ns )) != S_OK) goto error;
1825 if (!(utf8 = alloc_utf8_text( NULL, 0 )))
1827 hr = E_OUTOFMEMORY;
1828 goto error;
1830 attr->value = &utf8->text;
1831 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1832 attr->isXmlNs = 1;
1833 break;
1835 case RECORD_DICTIONARY_XMLNS_ATTRIBUTE:
1836 if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1837 if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
1839 hr = E_OUTOFMEMORY;
1840 goto error;
1842 if ((hr = read_dict_string( reader, &attr->ns )) != S_OK) goto error;
1843 if (!(utf8 = alloc_utf8_text( NULL, 0 )))
1845 hr = E_OUTOFMEMORY;
1846 goto error;
1848 attr->value = &utf8->text;
1849 if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1850 attr->isXmlNs = 1;
1851 break;
1853 default:
1854 ERR( "unhandled record type %02x\n", type );
1855 return WS_E_NOT_SUPPORTED;
1859 *ret = attr;
1860 return S_OK;
1862 error:
1863 free_attribute( attr );
1864 return hr;
1867 static inline struct node *find_parent( struct reader *reader )
1869 if (node_type( reader->current ) == WS_XML_NODE_TYPE_END_ELEMENT)
1871 if (is_valid_parent( reader->current->parent->parent )) return reader->current->parent->parent;
1872 return NULL;
1874 if (is_valid_parent( reader->current )) return reader->current;
1875 if (is_valid_parent( reader->current->parent )) return reader->current->parent;
1876 return NULL;
1879 static HRESULT set_namespaces( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
1881 static const WS_XML_STRING xml = {3, (BYTE *)"xml"};
1882 const WS_XML_STRING *ns;
1883 ULONG i;
1885 if (!(ns = get_namespace( reader, elem->prefix ))) return WS_E_INVALID_FORMAT;
1886 if (!(elem->ns = dup_xml_string( ns, FALSE ))) return E_OUTOFMEMORY;
1888 for (i = 0; i < elem->attributeCount; i++)
1890 WS_XML_ATTRIBUTE *attr = elem->attributes[i];
1891 if (attr->isXmlNs || WsXmlStringEquals( attr->prefix, &xml, NULL ) == S_OK) continue;
1892 if (!(ns = get_namespace( reader, attr->prefix ))) return WS_E_INVALID_FORMAT;
1893 if (!(attr->ns = alloc_xml_string( NULL, ns->length ))) return E_OUTOFMEMORY;
1894 if (attr->ns->length) memcpy( attr->ns->bytes, ns->bytes, ns->length );
1896 return S_OK;
1899 static WS_XML_ELEMENT_NODE *alloc_element_pair(void)
1901 struct node *node, *end;
1902 if (!(node = alloc_node( WS_XML_NODE_TYPE_ELEMENT ))) return NULL;
1903 if (!(end = alloc_node( WS_XML_NODE_TYPE_END_ELEMENT )))
1905 free_node( node );
1906 return NULL;
1908 list_add_tail( &node->children, &end->entry );
1909 end->parent = node;
1910 return &node->hdr;
1913 static HRESULT read_attributes_text( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
1915 WS_XML_ATTRIBUTE *attr;
1916 HRESULT hr;
1918 reader->current_attr = 0;
1919 for (;;)
1921 read_skip_whitespace( reader );
1922 if (!read_cmp( reader, ">", 1 ) || !read_cmp( reader, "/>", 2 )) break;
1923 if ((hr = read_attribute_text( reader, &attr )) != S_OK) return hr;
1924 if ((hr = append_attribute( elem, attr )) != S_OK)
1926 free_attribute( attr );
1927 return hr;
1929 reader->current_attr++;
1931 return S_OK;
1934 static HRESULT read_element_text( struct reader *reader )
1936 unsigned int len = 0, ch, skip;
1937 const unsigned char *start;
1938 struct node *node = NULL, *parent;
1939 WS_XML_ELEMENT_NODE *elem;
1940 HRESULT hr = WS_E_INVALID_FORMAT;
1942 if (read_end_of_data( reader ))
1944 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
1945 reader->last = reader->current;
1946 reader->state = READER_STATE_EOF;
1947 return S_OK;
1950 if (read_cmp( reader, "<", 1 )) return WS_E_INVALID_FORMAT;
1951 read_skip( reader, 1 );
1952 if (!read_isnamechar( read_utf8_char( reader, &skip )))
1954 read_rewind( reader, 1 );
1955 return WS_E_INVALID_FORMAT;
1958 if (!(elem = alloc_element_pair())) return E_OUTOFMEMORY;
1959 node = (struct node *)elem;
1961 start = read_current_ptr( reader );
1962 for (;;)
1964 if (!(ch = read_utf8_char( reader, &skip ))) goto error;
1965 if (!read_isnamechar( ch )) break;
1966 read_skip( reader, skip );
1967 len += skip;
1969 if (!len) goto error;
1971 if (!(parent = find_parent( reader ))) goto error;
1972 if ((hr = parse_qname( start, len, &elem->prefix, &elem->localName )) != S_OK) goto error;
1973 if ((hr = read_attributes_text( reader, elem )) != S_OK) goto error;
1974 if ((hr = set_namespaces( reader, elem )) != S_OK) goto error;
1976 read_insert_node( reader, parent, node );
1977 reader->state = READER_STATE_STARTELEMENT;
1978 return S_OK;
1980 error:
1981 destroy_nodes( node );
1982 return hr;
1985 static inline BOOL is_element_type( unsigned char type )
1987 return (type >= RECORD_SHORT_ELEMENT && type <= RECORD_PREFIX_ELEMENT_Z);
1990 static HRESULT read_attributes_bin( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
1992 WS_XML_ATTRIBUTE *attr;
1993 unsigned char type;
1994 HRESULT hr;
1996 reader->current_attr = 0;
1997 for (;;)
1999 if ((hr = read_peek( reader, &type )) != S_OK) return hr;
2000 if (!is_attribute_type( type )) break;
2001 if ((hr = read_attribute_bin( reader, &attr )) != S_OK) return hr;
2002 if ((hr = append_attribute( elem, attr )) != S_OK)
2004 free_attribute( attr );
2005 return hr;
2007 reader->current_attr++;
2009 return S_OK;
2012 static HRESULT read_element_bin( struct reader *reader )
2014 struct node *node = NULL, *parent;
2015 WS_XML_ELEMENT_NODE *elem;
2016 unsigned char type;
2017 HRESULT hr;
2019 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2020 if (!is_element_type( type )) return WS_E_INVALID_FORMAT;
2022 if (!(elem = alloc_element_pair())) return E_OUTOFMEMORY;
2023 node = (struct node *)elem;
2025 if (type >= RECORD_PREFIX_ELEMENT_A && type <= RECORD_PREFIX_ELEMENT_Z)
2027 unsigned char ch = type - RECORD_PREFIX_ELEMENT_A + 'a';
2028 if (!(elem->prefix = alloc_xml_string( &ch, 1 )))
2030 hr = E_OUTOFMEMORY;
2031 goto error;
2033 if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2035 else if (type >= RECORD_PREFIX_DICTIONARY_ELEMENT_A && type <= RECORD_PREFIX_DICTIONARY_ELEMENT_Z)
2037 unsigned char ch = type - RECORD_PREFIX_DICTIONARY_ELEMENT_A + 'a';
2038 if (!(elem->prefix = alloc_xml_string( &ch, 1 )))
2040 hr = E_OUTOFMEMORY;
2041 goto error;
2043 if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
2045 else
2047 switch (type)
2049 case RECORD_SHORT_ELEMENT:
2050 if (!(elem->prefix = alloc_xml_string( NULL, 0 )))
2052 hr = E_OUTOFMEMORY;
2053 goto error;
2055 if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2056 break;
2058 case RECORD_ELEMENT:
2059 if ((hr = read_string( reader, &elem->prefix )) != S_OK) goto error;
2060 if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2061 break;
2063 case RECORD_SHORT_DICTIONARY_ELEMENT:
2064 if (!(elem->prefix = alloc_xml_string( NULL, 0 )))
2066 hr = E_OUTOFMEMORY;
2067 goto error;
2069 if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
2070 break;
2072 case RECORD_DICTIONARY_ELEMENT:
2073 if ((hr = read_string( reader, &elem->prefix )) != S_OK) goto error;
2074 if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
2075 break;
2077 default:
2078 ERR( "unhandled record type %02x\n", type );
2079 return WS_E_NOT_SUPPORTED;
2083 if (!(parent = find_parent( reader )))
2085 hr = WS_E_INVALID_FORMAT;
2086 goto error;
2089 if ((hr = read_attributes_bin( reader, elem )) != S_OK) goto error;
2090 if ((hr = set_namespaces( reader, elem )) != S_OK) goto error;
2092 read_insert_node( reader, parent, node );
2093 reader->state = READER_STATE_STARTELEMENT;
2094 return S_OK;
2096 error:
2097 destroy_nodes( node );
2098 return hr;
2101 static HRESULT read_text_text( struct reader *reader )
2103 unsigned int len = 0, ch, skip;
2104 const unsigned char *start;
2105 struct node *node, *parent;
2106 WS_XML_TEXT_NODE *text;
2107 WS_XML_UTF8_TEXT *utf8;
2108 HRESULT hr;
2110 start = read_current_ptr( reader );
2111 for (;;)
2113 if (read_end_of_data( reader )) break;
2114 if (!(ch = read_utf8_char( reader, &skip ))) return WS_E_INVALID_FORMAT;
2115 if (ch == '<') break;
2116 read_skip( reader, skip );
2117 len += skip;
2120 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2122 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return E_OUTOFMEMORY;
2123 text = (WS_XML_TEXT_NODE *)node;
2124 if (!(utf8 = alloc_utf8_text( NULL, len )))
2126 heap_free( node );
2127 return E_OUTOFMEMORY;
2129 if ((hr = decode_text( start, len, utf8->value.bytes, &utf8->value.length )) != S_OK)
2131 heap_free( utf8 );
2132 heap_free( node );
2133 return hr;
2135 text->text = &utf8->text;
2137 read_insert_node( reader, parent, node );
2138 reader->state = READER_STATE_TEXT;
2139 reader->text_conv_offset = 0;
2140 return S_OK;
2143 static struct node *alloc_utf8_text_node( const BYTE *data, ULONG len, WS_XML_UTF8_TEXT **ret )
2145 struct node *node;
2146 WS_XML_UTF8_TEXT *utf8;
2147 WS_XML_TEXT_NODE *text;
2149 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2150 if (!(utf8 = alloc_utf8_text( data, len )))
2152 heap_free( node );
2153 return NULL;
2155 text = (WS_XML_TEXT_NODE *)node;
2156 text->text = &utf8->text;
2157 if (ret) *ret = utf8;
2158 return node;
2161 static struct node *alloc_base64_text_node( const BYTE *data, ULONG len, WS_XML_BASE64_TEXT **ret )
2163 struct node *node;
2164 WS_XML_BASE64_TEXT *base64;
2165 WS_XML_TEXT_NODE *text;
2167 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2168 if (!(base64 = alloc_base64_text( data, len )))
2170 heap_free( node );
2171 return NULL;
2173 text = (WS_XML_TEXT_NODE *)node;
2174 text->text = &base64->text;
2175 if (ret) *ret = base64;
2176 return node;
2179 static struct node *alloc_bool_text_node( BOOL value )
2181 struct node *node;
2182 WS_XML_BOOL_TEXT *text_bool;
2183 WS_XML_TEXT_NODE *text;
2185 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2186 if (!(text_bool = alloc_bool_text( value )))
2188 heap_free( node );
2189 return NULL;
2191 text = (WS_XML_TEXT_NODE *)node;
2192 text->text = &text_bool->text;
2193 return node;
2196 static struct node *alloc_int32_text_node( INT32 value )
2198 struct node *node;
2199 WS_XML_INT32_TEXT *text_int32;
2200 WS_XML_TEXT_NODE *text;
2202 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2203 if (!(text_int32 = alloc_int32_text( value )))
2205 heap_free( node );
2206 return NULL;
2208 text = (WS_XML_TEXT_NODE *)node;
2209 text->text = &text_int32->text;
2210 return node;
2213 static struct node *alloc_int64_text_node( INT64 value )
2215 struct node *node;
2216 WS_XML_INT64_TEXT *text_int64;
2217 WS_XML_TEXT_NODE *text;
2219 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2220 if (!(text_int64 = alloc_int64_text( value )))
2222 heap_free( node );
2223 return NULL;
2225 text = (WS_XML_TEXT_NODE *)node;
2226 text->text = &text_int64->text;
2227 return node;
2230 static struct node *alloc_float_text_node( float value )
2232 struct node *node;
2233 WS_XML_FLOAT_TEXT *text_float;
2234 WS_XML_TEXT_NODE *text;
2236 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2237 if (!(text_float = alloc_float_text( value )))
2239 heap_free( node );
2240 return NULL;
2242 text = (WS_XML_TEXT_NODE *)node;
2243 text->text = &text_float->text;
2244 return node;
2247 static struct node *alloc_double_text_node( double value )
2249 struct node *node;
2250 WS_XML_DOUBLE_TEXT *text_double;
2251 WS_XML_TEXT_NODE *text;
2253 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2254 if (!(text_double = alloc_double_text( value )))
2256 heap_free( node );
2257 return NULL;
2259 text = (WS_XML_TEXT_NODE *)node;
2260 text->text = &text_double->text;
2261 return node;
2264 static struct node *alloc_datetime_text_node( const WS_DATETIME *value )
2266 struct node *node;
2267 WS_XML_DATETIME_TEXT *text_datetime;
2268 WS_XML_TEXT_NODE *text;
2270 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2271 if (!(text_datetime = alloc_datetime_text( value )))
2273 heap_free( node );
2274 return NULL;
2276 text = (WS_XML_TEXT_NODE *)node;
2277 text->text = &text_datetime->text;
2278 return node;
2281 static struct node *alloc_unique_id_text_node( const GUID *value )
2283 struct node *node;
2284 WS_XML_UNIQUE_ID_TEXT *text_unique_id;
2285 WS_XML_TEXT_NODE *text;
2287 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2288 if (!(text_unique_id = alloc_unique_id_text( value )))
2290 heap_free( node );
2291 return NULL;
2293 text = (WS_XML_TEXT_NODE *)node;
2294 text->text = &text_unique_id->text;
2295 return node;
2298 static struct node *alloc_guid_text_node( const GUID *value )
2300 struct node *node;
2301 WS_XML_GUID_TEXT *text_guid;
2302 WS_XML_TEXT_NODE *text;
2304 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2305 if (!(text_guid = alloc_guid_text( value )))
2307 heap_free( node );
2308 return NULL;
2310 text = (WS_XML_TEXT_NODE *)node;
2311 text->text = &text_guid->text;
2312 return node;
2315 static struct node *alloc_uint64_text_node( UINT64 value )
2317 struct node *node;
2318 WS_XML_UINT64_TEXT *text_uint64;
2319 WS_XML_TEXT_NODE *text;
2321 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
2322 if (!(text_uint64 = alloc_uint64_text( value )))
2324 heap_free( node );
2325 return NULL;
2327 text = (WS_XML_TEXT_NODE *)node;
2328 text->text = &text_uint64->text;
2329 return node;
2332 static HRESULT append_text_bytes( struct reader *reader, WS_XML_TEXT_NODE *node, ULONG len )
2334 WS_XML_BASE64_TEXT *new, *old = (WS_XML_BASE64_TEXT *)node->text;
2335 HRESULT hr;
2337 if (!(new = alloc_base64_text( NULL, old->length + len ))) return E_OUTOFMEMORY;
2338 memcpy( new->bytes, old->bytes, old->length );
2339 if ((hr = read_bytes( reader, new->bytes + old->length, len )) != S_OK) return hr;
2340 heap_free( old );
2341 node->text = &new->text;
2342 return S_OK;
2345 static HRESULT read_text_bytes( struct reader *reader, unsigned char type )
2347 struct node *node = NULL, *parent;
2348 WS_XML_BASE64_TEXT *base64;
2349 HRESULT hr;
2350 ULONG len;
2352 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2353 for (;;)
2355 switch (type)
2357 case RECORD_BYTES8_TEXT:
2358 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT:
2360 UINT8 len_uint8;
2361 if ((hr = read_byte( reader, (unsigned char *)&len_uint8 )) != S_OK) goto error;
2362 len = len_uint8;
2363 break;
2365 case RECORD_BYTES16_TEXT:
2366 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT:
2368 UINT16 len_uint16;
2369 if ((hr = read_bytes( reader, (unsigned char *)&len_uint16, sizeof(len_uint16) )) != S_OK) goto error;
2370 len = len_uint16;
2371 break;
2373 case RECORD_BYTES32_TEXT:
2374 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT:
2376 INT32 len_int32;
2377 if ((hr = read_bytes( reader, (unsigned char *)&len_int32, sizeof(len_int32) )) != S_OK) goto error;
2378 if (len_int32 < 0)
2380 hr = WS_E_INVALID_FORMAT;
2381 goto error;
2383 len = len_int32;
2384 break;
2386 default:
2387 ERR( "unexpected type %u\n", type );
2388 hr = E_INVALIDARG;
2389 goto error;
2392 if (!node)
2394 if (!(node = alloc_base64_text_node( NULL, len, &base64 ))) return E_OUTOFMEMORY;
2395 if ((hr = read_bytes( reader, base64->bytes, len )) != S_OK) goto error;
2397 else if ((hr = append_text_bytes( reader, (WS_XML_TEXT_NODE *)node, len )) != S_OK) goto error;
2399 if (type & 1)
2401 node->flags |= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT;
2402 break;
2404 if ((hr = read_peek( reader, &type )) != S_OK) goto error;
2405 if (type < RECORD_BYTES8_TEXT || type > RECORD_BYTES32_TEXT_WITH_ENDELEMENT) break;
2406 read_skip( reader, 1 );
2409 read_insert_node( reader, parent, node );
2410 reader->state = READER_STATE_TEXT;
2411 reader->text_conv_offset = 0;
2412 return S_OK;
2414 error:
2415 free_node( node );
2416 return hr;
2419 static HRESULT read_text_bin( struct reader *reader )
2421 struct node *node = NULL, *parent;
2422 unsigned char type;
2423 WS_XML_UTF8_TEXT *utf8;
2424 INT32 val_int32;
2425 UINT8 val_uint8;
2426 UINT16 val_uint16;
2427 ULONG len, id;
2428 GUID uuid;
2429 HRESULT hr;
2431 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2432 if (!is_text_type( type ) || !(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2434 switch (type)
2436 case RECORD_ZERO_TEXT:
2437 case RECORD_ZERO_TEXT_WITH_ENDELEMENT:
2438 if (!(node = alloc_int32_text_node( 0 ))) return E_OUTOFMEMORY;
2439 break;
2441 case RECORD_ONE_TEXT:
2442 case RECORD_ONE_TEXT_WITH_ENDELEMENT:
2443 if (!(node = alloc_int32_text_node( 1 ))) return E_OUTOFMEMORY;
2444 break;
2446 case RECORD_FALSE_TEXT:
2447 case RECORD_FALSE_TEXT_WITH_ENDELEMENT:
2448 if (!(node = alloc_bool_text_node( FALSE ))) return E_OUTOFMEMORY;
2449 break;
2451 case RECORD_TRUE_TEXT:
2452 case RECORD_TRUE_TEXT_WITH_ENDELEMENT:
2453 if (!(node = alloc_bool_text_node( TRUE ))) return E_OUTOFMEMORY;
2454 break;
2456 case RECORD_INT8_TEXT:
2457 case RECORD_INT8_TEXT_WITH_ENDELEMENT:
2459 INT8 val_int8;
2460 if ((hr = read_byte( reader, (unsigned char *)&val_int8 )) != S_OK) return hr;
2461 if (!(node = alloc_int32_text_node( val_int8 ))) return E_OUTOFMEMORY;
2462 break;
2464 case RECORD_INT16_TEXT:
2465 case RECORD_INT16_TEXT_WITH_ENDELEMENT:
2467 INT16 val_int16;
2468 if ((hr = read_bytes( reader, (unsigned char *)&val_int16, sizeof(val_int16) )) != S_OK) return hr;
2469 if (!(node = alloc_int32_text_node( val_int16 ))) return E_OUTOFMEMORY;
2470 break;
2472 case RECORD_INT32_TEXT:
2473 case RECORD_INT32_TEXT_WITH_ENDELEMENT:
2474 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
2475 if (!(node = alloc_int32_text_node( val_int32 ))) return E_OUTOFMEMORY;
2476 break;
2478 case RECORD_INT64_TEXT:
2479 case RECORD_INT64_TEXT_WITH_ENDELEMENT:
2481 INT64 val_int64;
2482 if ((hr = read_bytes( reader, (unsigned char *)&val_int64, sizeof(val_int64) )) != S_OK) return hr;
2483 if (!(node = alloc_int64_text_node( val_int64 ))) return E_OUTOFMEMORY;
2484 break;
2486 case RECORD_FLOAT_TEXT:
2487 case RECORD_FLOAT_TEXT_WITH_ENDELEMENT:
2489 float val_float;
2490 if ((hr = read_bytes( reader, (unsigned char *)&val_float, sizeof(val_float) )) != S_OK) return hr;
2491 if (!(node = alloc_float_text_node( val_float ))) return E_OUTOFMEMORY;
2492 break;
2494 case RECORD_DOUBLE_TEXT:
2495 case RECORD_DOUBLE_TEXT_WITH_ENDELEMENT:
2497 double val_double;
2498 if ((hr = read_bytes( reader, (unsigned char *)&val_double, sizeof(val_double) )) != S_OK) return hr;
2499 if (!(node = alloc_double_text_node( val_double ))) return E_OUTOFMEMORY;
2500 break;
2502 case RECORD_DATETIME_TEXT:
2503 case RECORD_DATETIME_TEXT_WITH_ENDELEMENT:
2505 WS_DATETIME datetime;
2506 if ((hr = read_datetime( reader, &datetime )) != S_OK) return hr;
2507 if (!(node = alloc_datetime_text_node( &datetime ))) return E_OUTOFMEMORY;
2508 break;
2510 case RECORD_CHARS8_TEXT:
2511 case RECORD_CHARS8_TEXT_WITH_ENDELEMENT:
2512 if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
2513 len = val_uint8;
2514 break;
2516 case RECORD_CHARS16_TEXT:
2517 case RECORD_CHARS16_TEXT_WITH_ENDELEMENT:
2518 if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
2519 len = val_uint16;
2520 break;
2522 case RECORD_CHARS32_TEXT:
2523 case RECORD_CHARS32_TEXT_WITH_ENDELEMENT:
2524 if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
2525 if (val_int32 < 0) return WS_E_INVALID_FORMAT;
2526 len = val_int32;
2527 break;
2529 case RECORD_BYTES8_TEXT:
2530 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT:
2531 case RECORD_BYTES16_TEXT:
2532 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT:
2533 case RECORD_BYTES32_TEXT:
2534 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT:
2535 return read_text_bytes( reader, type );
2537 case RECORD_EMPTY_TEXT:
2538 case RECORD_EMPTY_TEXT_WITH_ENDELEMENT:
2539 len = 0;
2540 break;
2542 case RECORD_DICTIONARY_TEXT:
2543 case RECORD_DICTIONARY_TEXT_WITH_ENDELEMENT:
2545 const WS_XML_STRING *str;
2546 if ((hr = read_int31( reader, &id )) != S_OK) return hr;
2547 if ((hr = lookup_string( reader, id, &str )) != S_OK) return hr;
2548 if (!(node = alloc_utf8_text_node( str->bytes, str->length, NULL ))) return E_OUTOFMEMORY;
2549 break;
2551 case RECORD_UNIQUE_ID_TEXT:
2552 case RECORD_UNIQUE_ID_TEXT_WITH_ENDELEMENT:
2553 if ((hr = read_bytes( reader, (unsigned char *)&uuid, sizeof(uuid) )) != S_OK) return hr;
2554 if (!(node = alloc_unique_id_text_node( &uuid ))) return E_OUTOFMEMORY;
2555 break;
2557 case RECORD_GUID_TEXT:
2558 case RECORD_GUID_TEXT_WITH_ENDELEMENT:
2559 if ((hr = read_bytes( reader, (unsigned char *)&uuid, sizeof(uuid) )) != S_OK) return hr;
2560 if (!(node = alloc_guid_text_node( &uuid ))) return E_OUTOFMEMORY;
2561 break;
2563 case RECORD_UINT64_TEXT:
2564 case RECORD_UINT64_TEXT_WITH_ENDELEMENT:
2566 UINT64 val_uint64;
2567 if ((hr = read_bytes( reader, (unsigned char *)&val_uint64, sizeof(val_uint64) )) != S_OK) return hr;
2568 if (!(node = alloc_uint64_text_node( val_uint64 ))) return E_OUTOFMEMORY;
2569 break;
2571 case RECORD_BOOL_TEXT:
2572 case RECORD_BOOL_TEXT_WITH_ENDELEMENT:
2574 BOOL val_bool;
2575 if ((hr = read_bytes( reader, (unsigned char *)&val_bool, sizeof(val_bool) )) != S_OK) return hr;
2576 if (!(node = alloc_bool_text_node( !!val_bool ))) return E_OUTOFMEMORY;
2577 break;
2579 default:
2580 ERR( "unhandled record type %02x\n", type );
2581 return WS_E_NOT_SUPPORTED;
2584 if (!node)
2586 if (!(node = alloc_utf8_text_node( NULL, len, &utf8 ))) return E_OUTOFMEMORY;
2587 if (!len) utf8->value.bytes = (BYTE *)(utf8 + 1); /* quirk */
2588 if ((hr = read_bytes( reader, utf8->value.bytes, len )) != S_OK)
2590 free_node( node );
2591 return hr;
2595 if (type & 1) node->flags |= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT;
2596 read_insert_node( reader, parent, node );
2597 reader->state = READER_STATE_TEXT;
2598 reader->text_conv_offset = 0;
2599 return S_OK;
2602 static HRESULT read_node_text( struct reader * );
2604 static HRESULT read_startelement_text( struct reader *reader )
2606 read_skip_whitespace( reader );
2607 if (!read_cmp( reader, "/>", 2 ))
2609 read_skip( reader, 2 );
2610 reader->current = LIST_ENTRY( list_tail( &reader->current->children ), struct node, entry );
2611 reader->last = reader->current;
2612 reader->state = READER_STATE_ENDELEMENT;
2613 return S_OK;
2615 else if (!read_cmp( reader, ">", 1 ))
2617 read_skip( reader, 1 );
2618 return read_node_text( reader );
2620 return WS_E_INVALID_FORMAT;
2623 static HRESULT read_node_bin( struct reader * );
2625 static HRESULT read_startelement_bin( struct reader *reader )
2627 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return WS_E_INVALID_FORMAT;
2628 return read_node_bin( reader );
2631 static HRESULT read_startelement( struct reader *reader )
2633 switch (reader->input_enc)
2635 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_startelement_text( reader );
2636 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_startelement_bin( reader );
2637 default:
2638 ERR( "unhandled encoding %u\n", reader->input_enc );
2639 return WS_E_NOT_SUPPORTED;
2643 static HRESULT read_to_startelement_text( struct reader *reader, BOOL *found )
2645 HRESULT hr;
2647 switch (reader->state)
2649 case READER_STATE_INITIAL:
2650 if ((hr = read_xmldecl( reader )) != S_OK) return hr;
2651 break;
2653 case READER_STATE_STARTELEMENT:
2654 if (found) *found = TRUE;
2655 return S_OK;
2657 default:
2658 break;
2661 read_skip_whitespace( reader );
2662 if ((hr = read_element_text( reader )) == S_OK && found)
2664 if (reader->state == READER_STATE_STARTELEMENT)
2665 *found = TRUE;
2666 else
2667 *found = FALSE;
2670 return hr;
2673 static HRESULT read_to_startelement_bin( struct reader *reader, BOOL *found )
2675 HRESULT hr;
2677 if (reader->state == READER_STATE_STARTELEMENT)
2679 if (found) *found = TRUE;
2680 return S_OK;
2683 if ((hr = read_element_bin( reader )) == S_OK && found)
2685 if (reader->state == READER_STATE_STARTELEMENT)
2686 *found = TRUE;
2687 else
2688 *found = FALSE;
2691 return hr;
2694 static HRESULT read_to_startelement( struct reader *reader, BOOL *found )
2696 switch (reader->input_enc)
2698 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_to_startelement_text( reader, found );
2699 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_to_startelement_bin( reader, found );
2700 default:
2701 ERR( "unhandled encoding %u\n", reader->input_enc );
2702 return WS_E_NOT_SUPPORTED;
2706 static int cmp_name( const unsigned char *name1, ULONG len1, const unsigned char *name2, ULONG len2 )
2708 ULONG i;
2709 if (len1 != len2) return 1;
2710 for (i = 0; i < len1; i++) { if (toupper( name1[i] ) != toupper( name2[i] )) return 1; }
2711 return 0;
2714 static struct node *find_startelement( struct reader *reader, const WS_XML_STRING *prefix,
2715 const WS_XML_STRING *localname )
2717 struct node *parent;
2718 const WS_XML_STRING *str;
2720 for (parent = reader->current; parent; parent = parent->parent)
2722 if (node_type( parent ) == WS_XML_NODE_TYPE_ELEMENT)
2724 str = parent->hdr.prefix;
2725 if (cmp_name( str->bytes, str->length, prefix->bytes, prefix->length )) continue;
2726 str = parent->hdr.localName;
2727 if (cmp_name( str->bytes, str->length, localname->bytes, localname->length )) continue;
2728 return parent;
2731 return NULL;
2734 static HRESULT read_endelement_text( struct reader *reader )
2736 struct node *parent;
2737 unsigned int len = 0, ch, skip;
2738 const unsigned char *start;
2739 WS_XML_STRING prefix, localname;
2740 HRESULT hr;
2742 if (read_cmp( reader, "</", 2 )) return WS_E_INVALID_FORMAT;
2743 read_skip( reader, 2 );
2745 start = read_current_ptr( reader );
2746 for (;;)
2748 if (!(ch = read_utf8_char( reader, &skip ))) return WS_E_INVALID_FORMAT;
2749 if (ch == '>')
2751 read_skip( reader, 1 );
2752 break;
2754 if (!read_isnamechar( ch )) return WS_E_INVALID_FORMAT;
2755 read_skip( reader, skip );
2756 len += skip;
2759 if ((hr = split_qname( start, len, &prefix, &localname )) != S_OK) return hr;
2760 if (!(parent = find_startelement( reader, &prefix, &localname ))) return WS_E_INVALID_FORMAT;
2762 reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
2763 reader->last = reader->current;
2764 reader->state = READER_STATE_ENDELEMENT;
2765 return S_OK;
2768 static HRESULT read_endelement_bin( struct reader *reader )
2770 struct node *parent;
2771 unsigned char type;
2772 HRESULT hr;
2774 if (!(reader->current->flags & NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT))
2776 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2777 if (type != RECORD_ENDELEMENT) return WS_E_INVALID_FORMAT;
2779 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2781 reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
2782 reader->last = reader->current;
2783 reader->state = READER_STATE_ENDELEMENT;
2784 return S_OK;
2787 static HRESULT read_endelement( struct reader *reader )
2789 if (reader->state == READER_STATE_EOF) return WS_E_INVALID_FORMAT;
2791 if (read_end_of_data( reader ))
2793 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
2794 reader->last = reader->current;
2795 reader->state = READER_STATE_EOF;
2796 return S_OK;
2799 switch (reader->input_enc)
2801 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_endelement_text( reader );
2802 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_endelement_bin( reader );
2803 default:
2804 ERR( "unhandled encoding %u\n", reader->input_enc );
2805 return WS_E_NOT_SUPPORTED;
2809 static HRESULT read_comment_text( struct reader *reader )
2811 unsigned int len = 0, ch, skip;
2812 const unsigned char *start;
2813 struct node *node, *parent;
2814 WS_XML_COMMENT_NODE *comment;
2816 if (read_cmp( reader, "<!--", 4 )) return WS_E_INVALID_FORMAT;
2817 read_skip( reader, 4 );
2819 start = read_current_ptr( reader );
2820 for (;;)
2822 if (!read_cmp( reader, "-->", 3 ))
2824 read_skip( reader, 3 );
2825 break;
2827 if (!(ch = read_utf8_char( reader, &skip ))) return WS_E_INVALID_FORMAT;
2828 read_skip( reader, skip );
2829 len += skip;
2832 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2834 if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return E_OUTOFMEMORY;
2835 comment = (WS_XML_COMMENT_NODE *)node;
2836 if (!(comment->value.bytes = heap_alloc( len )))
2838 heap_free( node );
2839 return E_OUTOFMEMORY;
2841 memcpy( comment->value.bytes, start, len );
2842 comment->value.length = len;
2844 read_insert_node( reader, parent, node );
2845 reader->state = READER_STATE_COMMENT;
2846 return S_OK;
2849 static HRESULT read_comment_bin( struct reader *reader )
2851 struct node *node, *parent;
2852 WS_XML_COMMENT_NODE *comment;
2853 unsigned char type;
2854 ULONG len;
2855 HRESULT hr;
2857 if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2858 if (type != RECORD_COMMENT) return WS_E_INVALID_FORMAT;
2859 if ((hr = read_int31( reader, &len )) != S_OK) return hr;
2861 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2863 if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return E_OUTOFMEMORY;
2864 comment = (WS_XML_COMMENT_NODE *)node;
2865 if (!(comment->value.bytes = heap_alloc( len )))
2867 heap_free( node );
2868 return E_OUTOFMEMORY;
2870 if ((hr = read_bytes( reader, comment->value.bytes, len )) != S_OK)
2872 free_node( node );
2873 return E_OUTOFMEMORY;
2875 comment->value.length = len;
2877 read_insert_node( reader, parent, node );
2878 reader->state = READER_STATE_COMMENT;
2879 return S_OK;
2882 static HRESULT read_startcdata( struct reader *reader )
2884 struct node *node, *endnode, *parent;
2886 if (read_cmp( reader, "<![CDATA[", 9 )) return WS_E_INVALID_FORMAT;
2887 read_skip( reader, 9 );
2889 if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2891 if (!(node = alloc_node( WS_XML_NODE_TYPE_CDATA ))) return E_OUTOFMEMORY;
2892 if (!(endnode = alloc_node( WS_XML_NODE_TYPE_END_CDATA )))
2894 heap_free( node );
2895 return E_OUTOFMEMORY;
2897 list_add_tail( &node->children, &endnode->entry );
2898 endnode->parent = node;
2900 read_insert_node( reader, parent, node );
2901 reader->state = READER_STATE_STARTCDATA;
2902 return S_OK;
2905 static HRESULT read_cdata( struct reader *reader )
2907 unsigned int len = 0, ch, skip;
2908 const unsigned char *start;
2909 struct node *node;
2910 WS_XML_TEXT_NODE *text;
2911 WS_XML_UTF8_TEXT *utf8;
2913 start = read_current_ptr( reader );
2914 for (;;)
2916 if (!read_cmp( reader, "]]>", 3 )) break;
2917 if (!(ch = read_utf8_char( reader, &skip ))) return WS_E_INVALID_FORMAT;
2918 read_skip( reader, skip );
2919 len += skip;
2922 if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return E_OUTOFMEMORY;
2923 text = (WS_XML_TEXT_NODE *)node;
2924 if (!(utf8 = alloc_utf8_text( start, len )))
2926 heap_free( node );
2927 return E_OUTOFMEMORY;
2929 text->text = &utf8->text;
2931 read_insert_node( reader, reader->current, node );
2932 reader->state = READER_STATE_CDATA;
2933 return S_OK;
2936 static HRESULT read_endcdata( struct reader *reader )
2938 struct node *parent;
2940 if (read_cmp( reader, "]]>", 3 )) return WS_E_INVALID_FORMAT;
2941 read_skip( reader, 3 );
2943 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT) parent = reader->current->parent;
2944 else parent = reader->current;
2946 reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
2947 reader->last = reader->current;
2948 reader->state = READER_STATE_ENDCDATA;
2949 return S_OK;
2952 static HRESULT read_node_text( struct reader *reader )
2954 HRESULT hr;
2956 for (;;)
2958 if (read_end_of_data( reader ))
2960 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
2961 reader->last = reader->current;
2962 reader->state = READER_STATE_EOF;
2963 return S_OK;
2965 if (reader->state == READER_STATE_STARTCDATA) return read_cdata( reader );
2966 else if (reader->state == READER_STATE_CDATA) return read_endcdata( reader );
2967 else if (!read_cmp( reader, "<?", 2 ))
2969 hr = read_xmldecl( reader );
2970 if (FAILED( hr )) return hr;
2972 else if (!read_cmp( reader, "</", 2 )) return read_endelement_text( reader );
2973 else if (!read_cmp( reader, "<![CDATA[", 9 )) return read_startcdata( reader );
2974 else if (!read_cmp( reader, "<!--", 4 )) return read_comment_text( reader );
2975 else if (!read_cmp( reader, "<", 1 )) return read_element_text( reader );
2976 else if (!read_cmp( reader, "/>", 2 ) || !read_cmp( reader, ">", 1 )) return read_startelement_text( reader );
2977 else return read_text_text( reader );
2981 static HRESULT read_node_bin( struct reader *reader )
2983 unsigned char type;
2984 HRESULT hr;
2986 if (reader->current->flags & NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT)
2988 reader->current = LIST_ENTRY( list_tail( &reader->current->parent->children ), struct node, entry );
2989 reader->last = reader->current;
2990 reader->state = READER_STATE_ENDELEMENT;
2991 return S_OK;
2993 if (read_end_of_data( reader ))
2995 reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
2996 reader->last = reader->current;
2997 reader->state = READER_STATE_EOF;
2998 return S_OK;
3001 if ((hr = read_peek( reader, &type )) != S_OK) return hr;
3002 if (type == RECORD_ENDELEMENT)
3004 return read_endelement_bin( reader );
3006 else if (type == RECORD_COMMENT)
3008 return read_comment_bin( reader );
3010 else if (type >= RECORD_SHORT_ELEMENT && type <= RECORD_PREFIX_ELEMENT_Z)
3012 return read_element_bin( reader );
3014 else if (type >= RECORD_ZERO_TEXT && type <= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT)
3016 return read_text_bin( reader );
3018 FIXME( "unhandled record type %02x\n", type );
3019 return WS_E_NOT_SUPPORTED;
3022 static HRESULT read_node( struct reader *reader )
3024 switch (reader->input_enc)
3026 case WS_XML_READER_ENCODING_TYPE_TEXT: return read_node_text( reader );
3027 case WS_XML_READER_ENCODING_TYPE_BINARY: return read_node_bin( reader );
3028 default:
3029 ERR( "unhandled encoding %u\n", reader->input_enc );
3030 return WS_E_NOT_SUPPORTED;
3034 HRESULT copy_node( WS_XML_READER *handle, WS_XML_WRITER_ENCODING_TYPE enc, struct node **node )
3036 struct reader *reader = (struct reader *)handle;
3037 const struct list *ptr;
3038 const struct node *start;
3039 HRESULT hr;
3041 EnterCriticalSection( &reader->cs );
3043 if (reader->magic != READER_MAGIC)
3045 LeaveCriticalSection( &reader->cs );
3046 return E_INVALIDARG;
3049 if (reader->current != reader->root) ptr = &reader->current->entry;
3050 else /* copy whole tree */
3052 if (!read_end_of_data( reader ))
3054 for (;;)
3056 if ((hr = read_node( reader )) != S_OK) goto done;
3057 if (node_type( reader->current ) == WS_XML_NODE_TYPE_EOF) break;
3060 ptr = list_head( &reader->root->children );
3063 start = LIST_ENTRY( ptr, struct node, entry );
3064 if (node_type( start ) == WS_XML_NODE_TYPE_EOF) hr = WS_E_INVALID_OPERATION;
3065 else hr = dup_tree( start, enc, node );
3067 done:
3068 LeaveCriticalSection( &reader->cs );
3069 return hr;
3072 /**************************************************************************
3073 * WsReadEndElement [webservices.@]
3075 HRESULT WINAPI WsReadEndElement( WS_XML_READER *handle, WS_ERROR *error )
3077 struct reader *reader = (struct reader *)handle;
3078 HRESULT hr;
3080 TRACE( "%p %p\n", handle, error );
3081 if (error) FIXME( "ignoring error parameter\n" );
3083 if (!reader) return E_INVALIDARG;
3085 EnterCriticalSection( &reader->cs );
3087 if (reader->magic != READER_MAGIC)
3089 LeaveCriticalSection( &reader->cs );
3090 return E_INVALIDARG;
3093 hr = read_endelement( reader );
3095 LeaveCriticalSection( &reader->cs );
3096 return hr;
3099 /**************************************************************************
3100 * WsReadNode [webservices.@]
3102 HRESULT WINAPI WsReadNode( WS_XML_READER *handle, WS_ERROR *error )
3104 struct reader *reader = (struct reader *)handle;
3105 HRESULT hr;
3107 TRACE( "%p %p\n", handle, error );
3108 if (error) FIXME( "ignoring error parameter\n" );
3110 if (!reader) return E_INVALIDARG;
3112 EnterCriticalSection( &reader->cs );
3114 if (reader->magic != READER_MAGIC)
3116 LeaveCriticalSection( &reader->cs );
3117 return E_INVALIDARG;
3120 hr = read_node( reader );
3122 LeaveCriticalSection( &reader->cs );
3123 return hr;
3126 static HRESULT skip_node( struct reader *reader )
3128 const struct node *parent;
3129 HRESULT hr;
3131 if (node_type( reader->current ) == WS_XML_NODE_TYPE_EOF) return WS_E_INVALID_OPERATION;
3132 if (node_type( reader->current ) == WS_XML_NODE_TYPE_ELEMENT) parent = reader->current;
3133 else parent = NULL;
3135 for (;;)
3137 if ((hr = read_node( reader )) != S_OK || !parent) break;
3138 if (node_type( reader->current ) != WS_XML_NODE_TYPE_END_ELEMENT) continue;
3139 if (reader->current->parent == parent) return read_node( reader );
3142 return hr;
3145 /**************************************************************************
3146 * WsSkipNode [webservices.@]
3148 HRESULT WINAPI WsSkipNode( WS_XML_READER *handle, WS_ERROR *error )
3150 struct reader *reader = (struct reader *)handle;
3151 HRESULT hr;
3153 TRACE( "%p %p\n", handle, error );
3154 if (error) FIXME( "ignoring error parameter\n" );
3156 if (!reader) return E_INVALIDARG;
3158 EnterCriticalSection( &reader->cs );
3160 if (reader->magic != READER_MAGIC)
3162 LeaveCriticalSection( &reader->cs );
3163 return E_INVALIDARG;
3166 hr = skip_node( reader );
3168 LeaveCriticalSection( &reader->cs );
3169 return hr;
3172 /**************************************************************************
3173 * WsReadStartElement [webservices.@]
3175 HRESULT WINAPI WsReadStartElement( WS_XML_READER *handle, WS_ERROR *error )
3177 struct reader *reader = (struct reader *)handle;
3178 HRESULT hr;
3180 TRACE( "%p %p\n", handle, error );
3181 if (error) FIXME( "ignoring error parameter\n" );
3183 if (!reader) return E_INVALIDARG;
3185 EnterCriticalSection( &reader->cs );
3187 if (reader->magic != READER_MAGIC)
3189 LeaveCriticalSection( &reader->cs );
3190 return E_INVALIDARG;
3193 hr = read_startelement( reader );
3195 LeaveCriticalSection( &reader->cs );
3196 return hr;
3199 /**************************************************************************
3200 * WsReadToStartElement [webservices.@]
3202 HRESULT WINAPI WsReadToStartElement( WS_XML_READER *handle, const WS_XML_STRING *localname,
3203 const WS_XML_STRING *ns, BOOL *found, WS_ERROR *error )
3205 struct reader *reader = (struct reader *)handle;
3206 HRESULT hr;
3208 TRACE( "%p %s %s %p %p\n", handle, debugstr_xmlstr(localname), debugstr_xmlstr(ns), found, error );
3209 if (error) FIXME( "ignoring error parameter\n" );
3211 if (!reader) return E_INVALIDARG;
3212 if (localname || ns) FIXME( "name and/or namespace not verified\n" );
3214 EnterCriticalSection( &reader->cs );
3216 if (reader->magic != READER_MAGIC)
3218 LeaveCriticalSection( &reader->cs );
3219 return E_INVALIDARG;
3222 hr = read_to_startelement( reader, found );
3224 LeaveCriticalSection( &reader->cs );
3225 return hr;
3228 BOOL move_to_root_element( struct node *root, struct node **current )
3230 struct list *ptr;
3231 struct node *node;
3233 if (!(ptr = list_head( &root->children ))) return FALSE;
3234 node = LIST_ENTRY( ptr, struct node, entry );
3235 if (node_type( node ) == WS_XML_NODE_TYPE_ELEMENT)
3237 *current = node;
3238 return TRUE;
3240 while ((ptr = list_next( &root->children, &node->entry )))
3242 struct node *next = LIST_ENTRY( ptr, struct node, entry );
3243 if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3245 *current = next;
3246 return TRUE;
3248 node = next;
3250 return FALSE;
3253 BOOL move_to_next_element( struct node **current )
3255 struct list *ptr;
3256 struct node *node = *current, *parent = (*current)->parent;
3258 if (!parent) return FALSE;
3259 while ((ptr = list_next( &parent->children, &node->entry )))
3261 struct node *next = LIST_ENTRY( ptr, struct node, entry );
3262 if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3264 *current = next;
3265 return TRUE;
3267 node = next;
3269 return FALSE;
3272 BOOL move_to_prev_element( struct node **current )
3274 struct list *ptr;
3275 struct node *node = *current, *parent = (*current)->parent;
3277 if (!parent) return FALSE;
3278 while ((ptr = list_prev( &parent->children, &node->entry )))
3280 struct node *prev = LIST_ENTRY( ptr, struct node, entry );
3281 if (node_type( prev ) == WS_XML_NODE_TYPE_ELEMENT)
3283 *current = prev;
3284 return TRUE;
3286 node = prev;
3288 return FALSE;
3291 BOOL move_to_child_element( struct node **current )
3293 struct list *ptr;
3294 struct node *child, *node = *current;
3296 if (!(ptr = list_head( &node->children ))) return FALSE;
3297 child = LIST_ENTRY( ptr, struct node, entry );
3298 if (node_type( child ) == WS_XML_NODE_TYPE_ELEMENT)
3300 *current = child;
3301 return TRUE;
3303 while ((ptr = list_next( &node->children, &child->entry )))
3305 struct node *next = LIST_ENTRY( ptr, struct node, entry );
3306 if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3308 *current = next;
3309 return TRUE;
3311 child = next;
3313 return FALSE;
3316 BOOL move_to_end_element( struct node **current )
3318 struct list *ptr;
3319 struct node *node = *current;
3321 if (node_type( node ) != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
3323 if ((ptr = list_tail( &node->children )))
3325 struct node *tail = LIST_ENTRY( ptr, struct node, entry );
3326 if (node_type( tail ) == WS_XML_NODE_TYPE_END_ELEMENT)
3328 *current = tail;
3329 return TRUE;
3332 return FALSE;
3335 BOOL move_to_parent_element( struct node **current )
3337 struct node *parent = (*current)->parent;
3339 if (parent && (node_type( parent ) == WS_XML_NODE_TYPE_ELEMENT ||
3340 node_type( parent ) == WS_XML_NODE_TYPE_BOF))
3342 *current = parent;
3343 return TRUE;
3345 return FALSE;
3348 BOOL move_to_first_node( struct node **current )
3350 struct list *ptr;
3351 struct node *node = *current;
3353 if ((ptr = list_head( &node->parent->children )))
3355 *current = LIST_ENTRY( ptr, struct node, entry );
3356 return TRUE;
3358 return FALSE;
3361 BOOL move_to_next_node( struct node **current )
3363 struct list *ptr;
3364 struct node *node = *current;
3366 if ((ptr = list_next( &node->parent->children, &node->entry )))
3368 *current = LIST_ENTRY( ptr, struct node, entry );
3369 return TRUE;
3371 return FALSE;
3374 BOOL move_to_prev_node( struct node **current )
3376 struct list *ptr;
3377 struct node *node = *current;
3379 if ((ptr = list_prev( &node->parent->children, &node->entry )))
3381 *current = LIST_ENTRY( ptr, struct node, entry );
3382 return TRUE;
3384 return FALSE;
3387 BOOL move_to_bof( struct node *root, struct node **current )
3389 *current = root;
3390 return TRUE;
3393 BOOL move_to_eof( struct node *root, struct node **current )
3395 struct list *ptr;
3396 if ((ptr = list_tail( &root->children )))
3398 *current = LIST_ENTRY( ptr, struct node, entry );
3399 return TRUE;
3401 return FALSE;
3404 BOOL move_to_child_node( struct node **current )
3406 struct list *ptr;
3407 struct node *node = *current;
3409 if ((ptr = list_head( &node->children )))
3411 *current = LIST_ENTRY( ptr, struct node, entry );
3412 return TRUE;
3414 return FALSE;
3417 BOOL move_to_parent_node( struct node **current )
3419 struct node *parent = (*current)->parent;
3420 if (!parent) return FALSE;
3421 *current = parent;
3422 return TRUE;
3425 static HRESULT read_move_to( struct reader *reader, WS_MOVE_TO move, BOOL *found )
3427 BOOL success = FALSE;
3428 HRESULT hr = S_OK;
3430 if (!read_end_of_data( reader ))
3432 struct node *saved_current = reader->current;
3433 while (reader->state != READER_STATE_EOF && (hr = read_node( reader )) == S_OK) { /* nothing */ };
3434 if (hr != S_OK) return hr;
3435 reader->current = saved_current;
3437 switch (move)
3439 case WS_MOVE_TO_ROOT_ELEMENT:
3440 success = move_to_root_element( reader->root, &reader->current );
3441 break;
3443 case WS_MOVE_TO_NEXT_ELEMENT:
3444 success = move_to_next_element( &reader->current );
3445 break;
3447 case WS_MOVE_TO_PREVIOUS_ELEMENT:
3448 success = move_to_prev_element( &reader->current );
3449 break;
3451 case WS_MOVE_TO_CHILD_ELEMENT:
3452 success = move_to_child_element( &reader->current );
3453 break;
3455 case WS_MOVE_TO_END_ELEMENT:
3456 success = move_to_end_element( &reader->current );
3457 break;
3459 case WS_MOVE_TO_PARENT_ELEMENT:
3460 success = move_to_parent_element( &reader->current );
3461 break;
3463 case WS_MOVE_TO_FIRST_NODE:
3464 success = move_to_first_node( &reader->current );
3465 break;
3467 case WS_MOVE_TO_NEXT_NODE:
3468 success = move_to_next_node( &reader->current );
3469 break;
3471 case WS_MOVE_TO_PREVIOUS_NODE:
3472 success = move_to_prev_node( &reader->current );
3473 break;
3475 case WS_MOVE_TO_CHILD_NODE:
3476 success = move_to_child_node( &reader->current );
3477 break;
3479 case WS_MOVE_TO_BOF:
3480 success = move_to_bof( reader->root, &reader->current );
3481 break;
3483 case WS_MOVE_TO_EOF:
3484 success = move_to_eof( reader->root, &reader->current );
3485 break;
3487 default:
3488 FIXME( "unhandled move %u\n", move );
3489 return E_NOTIMPL;
3492 if (found)
3494 *found = success;
3495 return S_OK;
3497 return success ? S_OK : WS_E_INVALID_FORMAT;
3500 /**************************************************************************
3501 * WsMoveReader [webservices.@]
3503 HRESULT WINAPI WsMoveReader( WS_XML_READER *handle, WS_MOVE_TO move, BOOL *found, WS_ERROR *error )
3505 struct reader *reader = (struct reader *)handle;
3506 HRESULT hr;
3508 TRACE( "%p %u %p %p\n", handle, move, found, error );
3509 if (error) FIXME( "ignoring error parameter\n" );
3511 if (!reader) return E_INVALIDARG;
3513 EnterCriticalSection( &reader->cs );
3515 if (reader->magic != READER_MAGIC)
3517 LeaveCriticalSection( &reader->cs );
3518 return E_INVALIDARG;
3521 if (!reader->input_type)
3523 LeaveCriticalSection( &reader->cs );
3524 return WS_E_INVALID_OPERATION;
3527 hr = read_move_to( reader, move, found );
3529 LeaveCriticalSection( &reader->cs );
3530 return hr;
3533 /**************************************************************************
3534 * WsReadStartAttribute [webservices.@]
3536 HRESULT WINAPI WsReadStartAttribute( WS_XML_READER *handle, ULONG index, WS_ERROR *error )
3538 struct reader *reader = (struct reader *)handle;
3539 const WS_XML_ELEMENT_NODE *elem;
3541 TRACE( "%p %u %p\n", handle, index, error );
3542 if (error) FIXME( "ignoring error parameter\n" );
3544 if (!reader) return E_INVALIDARG;
3546 EnterCriticalSection( &reader->cs );
3548 if (reader->magic != READER_MAGIC)
3550 LeaveCriticalSection( &reader->cs );
3551 return E_INVALIDARG;
3554 elem = &reader->current->hdr;
3555 if (reader->state != READER_STATE_STARTELEMENT || index >= elem->attributeCount)
3557 LeaveCriticalSection( &reader->cs );
3558 return WS_E_INVALID_FORMAT;
3561 reader->current_attr = index;
3562 reader->state = READER_STATE_STARTATTRIBUTE;
3564 LeaveCriticalSection( &reader->cs );
3565 return S_OK;
3568 /**************************************************************************
3569 * WsReadEndAttribute [webservices.@]
3571 HRESULT WINAPI WsReadEndAttribute( WS_XML_READER *handle, WS_ERROR *error )
3573 struct reader *reader = (struct reader *)handle;
3575 TRACE( "%p %p\n", handle, error );
3576 if (error) FIXME( "ignoring error parameter\n" );
3578 if (!reader) return E_INVALIDARG;
3580 EnterCriticalSection( &reader->cs );
3582 if (reader->magic != READER_MAGIC)
3584 LeaveCriticalSection( &reader->cs );
3585 return E_INVALIDARG;
3588 if (reader->state != READER_STATE_STARTATTRIBUTE)
3590 LeaveCriticalSection( &reader->cs );
3591 return WS_E_INVALID_FORMAT;
3594 reader->state = READER_STATE_STARTELEMENT;
3596 LeaveCriticalSection( &reader->cs );
3597 return S_OK;
3600 static HRESULT str_to_bool( const unsigned char *str, ULONG len, BOOL *ret )
3602 if (len == 4 && !memcmp( str, "true", 4 )) *ret = TRUE;
3603 else if (len == 1 && !memcmp( str, "1", 1 )) *ret = TRUE;
3604 else if (len == 5 && !memcmp( str, "false", 5 )) *ret = FALSE;
3605 else if (len == 1 && !memcmp( str, "0", 1 )) *ret = FALSE;
3606 else return WS_E_INVALID_FORMAT;
3607 return S_OK;
3610 static HRESULT str_to_int64( const unsigned char *str, ULONG len, INT64 min, INT64 max, INT64 *ret )
3612 BOOL negative = FALSE;
3613 const unsigned char *ptr = str;
3615 *ret = 0;
3616 while (len && read_isspace( *ptr )) { ptr++; len--; }
3617 while (len && read_isspace( ptr[len - 1] )) { len--; }
3618 if (!len) return WS_E_INVALID_FORMAT;
3620 if (*ptr == '-')
3622 negative = TRUE;
3623 ptr++;
3624 len--;
3626 if (!len) return WS_E_INVALID_FORMAT;
3628 while (len--)
3630 int val;
3632 if (!isdigit( *ptr )) return WS_E_INVALID_FORMAT;
3633 val = *ptr - '0';
3634 if (negative) val = -val;
3636 if ((!negative && (*ret > max / 10 || *ret * 10 > max - val)) ||
3637 (negative && (*ret < min / 10 || *ret * 10 < min - val)))
3639 return WS_E_NUMERIC_OVERFLOW;
3641 *ret = *ret * 10 + val;
3642 ptr++;
3645 return S_OK;
3648 static HRESULT str_to_uint64( const unsigned char *str, ULONG len, UINT64 max, UINT64 *ret )
3650 const unsigned char *ptr = str;
3652 *ret = 0;
3653 while (len && read_isspace( *ptr )) { ptr++; len--; }
3654 while (len && read_isspace( ptr[len - 1] )) { len--; }
3655 if (!len) return WS_E_INVALID_FORMAT;
3657 while (len--)
3659 unsigned int val;
3661 if (!isdigit( *ptr )) return WS_E_INVALID_FORMAT;
3662 val = *ptr - '0';
3664 if ((*ret > max / 10 || *ret * 10 > max - val)) return WS_E_NUMERIC_OVERFLOW;
3665 *ret = *ret * 10 + val;
3666 ptr++;
3669 return S_OK;
3672 BOOL set_fpword( unsigned short new, unsigned short *old )
3674 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3675 unsigned short fpword;
3677 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
3678 *old = fpword;
3679 fpword = new;
3680 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
3681 return TRUE;
3682 #else
3683 FIXME( "not implemented\n" );
3684 return FALSE;
3685 #endif
3688 void restore_fpword( unsigned short fpword )
3690 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3691 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
3692 #else
3693 FIXME( "not implemented\n" );
3694 #endif
3697 static HRESULT str_to_double( const unsigned char *str, ULONG len, double *ret )
3699 static const unsigned __int64 nan = 0xfff8000000000000;
3700 static const unsigned __int64 inf = 0x7ff0000000000000;
3701 static const unsigned __int64 inf_min = 0xfff0000000000000;
3702 HRESULT hr = WS_E_INVALID_FORMAT;
3703 const unsigned char *p = str, *q;
3704 int sign = 1, exp_sign = 1, exp = 0, exp_tmp = 0, neg_exp, i, nb_digits, have_digits;
3705 unsigned __int64 val = 0, tmp;
3706 long double exp_val = 1.0, exp_mul = 10.0;
3707 unsigned short fpword;
3709 while (len && read_isspace( *p )) { p++; len--; }
3710 while (len && read_isspace( p[len - 1] )) { len--; }
3711 if (!len) return WS_E_INVALID_FORMAT;
3713 if (len == 3 && !memcmp( p, "NaN", 3 ))
3715 *(unsigned __int64 *)ret = nan;
3716 return S_OK;
3718 if (len == 3 && !memcmp( p, "INF", 3 ))
3720 *(unsigned __int64 *)ret = inf;
3721 return S_OK;
3723 if (len == 4 && !memcmp( p, "-INF", 4 ))
3725 *(unsigned __int64 *)ret = inf_min;
3726 return S_OK;
3729 *ret = 0.0;
3730 if (*p == '-')
3732 sign = -1;
3733 p++; len--;
3735 else if (*p == '+') { p++; len--; };
3736 if (!len) return S_OK;
3738 if (!set_fpword( 0x37f, &fpword )) return E_NOTIMPL;
3740 q = p;
3741 while (len && isdigit( *q )) { q++; len--; }
3742 have_digits = nb_digits = q - p;
3743 for (i = 0; i < nb_digits; i++)
3745 tmp = val * 10 + p[i] - '0';
3746 if (val > MAX_UINT64 / 10 || tmp < val)
3748 for (; i < nb_digits; i++) exp++;
3749 break;
3751 val = tmp;
3754 if (len)
3756 if (*q == '.')
3758 p = ++q; len--;
3759 while (len && isdigit( *q )) { q++; len--; };
3760 have_digits |= nb_digits = q - p;
3761 for (i = 0; i < nb_digits; i++)
3763 tmp = val * 10 + p[i] - '0';
3764 if (val > MAX_UINT64 / 10 || tmp < val) break;
3765 val = tmp;
3766 exp--;
3769 if (len > 1 && tolower(*q) == 'e')
3771 if (!have_digits) goto done;
3772 p = ++q; len--;
3773 if (*p == '-')
3775 exp_sign = -1;
3776 p++; len--;
3778 else if (*p == '+') { p++; len--; };
3780 q = p;
3781 while (len && isdigit( *q )) { q++; len--; };
3782 nb_digits = q - p;
3783 if (!nb_digits || len) goto done;
3784 for (i = 0; i < nb_digits; i++)
3786 if (exp_tmp > MAX_INT32 / 10 || (exp_tmp = exp_tmp * 10 + p[i] - '0') < 0)
3787 exp_tmp = MAX_INT32;
3789 exp_tmp *= exp_sign;
3791 if (exp < 0 && exp_tmp < 0 && exp + exp_tmp >= 0) exp = MIN_INT32;
3792 else if (exp > 0 && exp_tmp > 0 && exp + exp_tmp < 0) exp = MAX_INT32;
3793 else exp += exp_tmp;
3796 if (!have_digits || len) goto done;
3798 if ((neg_exp = exp < 0)) exp = -exp;
3799 for (; exp; exp >>= 1)
3801 if (exp & 1) exp_val *= exp_mul;
3802 exp_mul *= exp_mul;
3805 *ret = sign * (neg_exp ? val / exp_val : val * exp_val);
3806 hr = S_OK;
3808 done:
3809 restore_fpword( fpword );
3810 return hr;
3813 static HRESULT str_to_float( const unsigned char *str, ULONG len, float *ret )
3815 static const unsigned int inf = 0x7f800000;
3816 static const unsigned int inf_min = 0xff800000;
3817 const unsigned char *p = str;
3818 double val;
3819 HRESULT hr;
3821 while (len && read_isspace( *p )) { p++; len--; }
3822 while (len && read_isspace( p[len - 1] )) { len--; }
3823 if (!len) return WS_E_INVALID_FORMAT;
3825 if (len == 3 && !memcmp( p, "INF", 3 ))
3827 *(unsigned int *)ret = inf;
3828 return S_OK;
3830 if (len == 4 && !memcmp( p, "-INF", 4 ))
3832 *(unsigned int *)ret = inf_min;
3833 return S_OK;
3836 if ((hr = str_to_double( p, len, &val )) != S_OK) return hr;
3837 *ret = val;
3838 return S_OK;
3841 static HRESULT str_to_guid( const unsigned char *str, ULONG len, GUID *ret )
3843 static const unsigned char hex[] =
3845 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
3846 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
3847 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
3848 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
3849 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
3850 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
3851 0,10,11,12,13,14,15 /* 0x60 */
3853 const unsigned char *p = str;
3854 ULONG i;
3856 while (len && read_isspace( *p )) { p++; len--; }
3857 while (len && read_isspace( p[len - 1] )) { len--; }
3858 if (len != 36) return WS_E_INVALID_FORMAT;
3860 if (p[8] != '-' || p[13] != '-' || p[18] != '-' || p[23] != '-')
3861 return WS_E_INVALID_FORMAT;
3863 for (i = 0; i < 36; i++)
3865 if (i == 8 || i == 13 || i == 18 || i == 23) continue;
3866 if (p[i] > 'f' || (!hex[p[i]] && p[i] != '0')) return WS_E_INVALID_FORMAT;
3869 ret->Data1 = hex[p[0]] << 28 | hex[p[1]] << 24 | hex[p[2]] << 20 | hex[p[3]] << 16 |
3870 hex[p[4]] << 12 | hex[p[5]] << 8 | hex[p[6]] << 4 | hex[p[7]];
3872 ret->Data2 = hex[p[9]] << 12 | hex[p[10]] << 8 | hex[p[11]] << 4 | hex[p[12]];
3873 ret->Data3 = hex[p[14]] << 12 | hex[p[15]] << 8 | hex[p[16]] << 4 | hex[p[17]];
3875 ret->Data4[0] = hex[p[19]] << 4 | hex[p[20]];
3876 ret->Data4[1] = hex[p[21]] << 4 | hex[p[22]];
3877 ret->Data4[2] = hex[p[24]] << 4 | hex[p[25]];
3878 ret->Data4[3] = hex[p[26]] << 4 | hex[p[27]];
3879 ret->Data4[4] = hex[p[28]] << 4 | hex[p[29]];
3880 ret->Data4[5] = hex[p[30]] << 4 | hex[p[31]];
3881 ret->Data4[6] = hex[p[32]] << 4 | hex[p[33]];
3882 ret->Data4[7] = hex[p[34]] << 4 | hex[p[35]];
3884 return S_OK;
3887 static HRESULT str_to_string( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_STRING *ret )
3889 int len_utf16 = MultiByteToWideChar( CP_UTF8, 0, (const char *)str, len, NULL, 0 );
3890 if (!(ret->chars = ws_alloc( heap, len_utf16 * sizeof(WCHAR) ))) return WS_E_QUOTA_EXCEEDED;
3891 MultiByteToWideChar( CP_UTF8, 0, (const char *)str, len, ret->chars, len_utf16 );
3892 ret->length = len_utf16;
3893 return S_OK;
3896 static HRESULT str_to_unique_id( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_UNIQUE_ID *ret )
3898 if (len == 45 && !memcmp( str, "urn:uuid:", 9 ))
3900 ret->uri.length = 0;
3901 ret->uri.chars = NULL;
3902 return str_to_guid( str + 9, len - 9, &ret->guid );
3905 memset( &ret->guid, 0, sizeof(ret->guid) );
3906 return str_to_string( str, len, heap, &ret->uri );
3909 static inline unsigned char decode_char( unsigned char c )
3911 if (c >= 'A' && c <= 'Z') return c - 'A';
3912 if (c >= 'a' && c <= 'z') return c - 'a' + 26;
3913 if (c >= '0' && c <= '9') return c - '0' + 52;
3914 if (c == '+') return 62;
3915 if (c == '/') return 63;
3916 return 64;
3919 static ULONG decode_base64( const unsigned char *base64, ULONG len, unsigned char *buf )
3921 ULONG i = 0;
3922 unsigned char c0, c1, c2, c3;
3923 const unsigned char *p = base64;
3925 while (len > 4)
3927 if ((c0 = decode_char( p[0] )) > 63) return 0;
3928 if ((c1 = decode_char( p[1] )) > 63) return 0;
3929 if ((c2 = decode_char( p[2] )) > 63) return 0;
3930 if ((c3 = decode_char( p[3] )) > 63) return 0;
3931 buf[i + 0] = (c0 << 2) | (c1 >> 4);
3932 buf[i + 1] = (c1 << 4) | (c2 >> 2);
3933 buf[i + 2] = (c2 << 6) | c3;
3934 len -= 4;
3935 i += 3;
3936 p += 4;
3938 if (p[2] == '=')
3940 if ((c0 = decode_char( p[0] )) > 63) return 0;
3941 if ((c1 = decode_char( p[1] )) > 63) return 0;
3942 buf[i] = (c0 << 2) | (c1 >> 4);
3943 i++;
3945 else if (p[3] == '=')
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 buf[i + 0] = (c0 << 2) | (c1 >> 4);
3951 buf[i + 1] = (c1 << 4) | (c2 >> 2);
3952 i += 2;
3954 else
3956 if ((c0 = decode_char( p[0] )) > 63) return 0;
3957 if ((c1 = decode_char( p[1] )) > 63) return 0;
3958 if ((c2 = decode_char( p[2] )) > 63) return 0;
3959 if ((c3 = decode_char( p[3] )) > 63) return 0;
3960 buf[i + 0] = (c0 << 2) | (c1 >> 4);
3961 buf[i + 1] = (c1 << 4) | (c2 >> 2);
3962 buf[i + 2] = (c2 << 6) | c3;
3963 i += 3;
3965 return i;
3968 static HRESULT str_to_bytes( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_BYTES *ret )
3970 const unsigned char *p = str;
3972 while (len && read_isspace( *p )) { p++; len--; }
3973 while (len && read_isspace( p[len - 1] )) { len--; }
3975 if (len % 4) return WS_E_INVALID_FORMAT;
3976 if (!(ret->bytes = ws_alloc( heap, len * 3 / 4 ))) return WS_E_QUOTA_EXCEEDED;
3977 ret->length = decode_base64( p, len, ret->bytes );
3978 return S_OK;
3981 static HRESULT str_to_xml_string( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_XML_STRING *ret )
3983 if (!(ret->bytes = ws_alloc( heap, len ))) return WS_E_QUOTA_EXCEEDED;
3984 memcpy( ret->bytes, str, len );
3985 ret->length = len;
3986 ret->dictionary = NULL;
3987 ret->id = 0;
3988 return S_OK;
3991 static HRESULT copy_xml_string( WS_HEAP *heap, const WS_XML_STRING *src, WS_XML_STRING *dst )
3993 if (!(dst->bytes = ws_alloc( heap, src->length ))) return WS_E_QUOTA_EXCEEDED;
3994 memcpy( dst->bytes, src->bytes, src->length );
3995 dst->length = src->length;
3996 return S_OK;
3999 static HRESULT str_to_qname( struct reader *reader, const unsigned char *str, ULONG len, WS_HEAP *heap,
4000 WS_XML_STRING *prefix_ret, WS_XML_STRING *localname_ret, WS_XML_STRING *ns_ret )
4002 const unsigned char *p = str;
4003 WS_XML_STRING prefix, localname;
4004 const WS_XML_STRING *ns;
4005 HRESULT hr;
4007 while (len && read_isspace( *p )) { p++; len--; }
4008 while (len && read_isspace( p[len - 1] )) { len--; }
4010 if ((hr = split_qname( p, len, &prefix, &localname )) != S_OK) return hr;
4011 if (!(ns = get_namespace( reader, &prefix ))) return WS_E_INVALID_FORMAT;
4013 if (prefix_ret && (hr = copy_xml_string( heap, &prefix, prefix_ret )) != S_OK) return hr;
4014 if ((hr = copy_xml_string( heap, &localname, localname_ret )) != S_OK)
4016 ws_free( heap, prefix_ret->bytes, prefix_ret->length );
4017 return hr;
4019 if ((hr = copy_xml_string( heap, ns, ns_ret )) != S_OK)
4021 ws_free( heap, prefix_ret->bytes, prefix_ret->length );
4022 ws_free( heap, localname_ret->bytes, localname_ret->length );
4023 return hr;
4025 return S_OK;
4028 static HRESULT read_qualified_name( struct reader *reader, WS_HEAP *heap, WS_XML_STRING *prefix,
4029 WS_XML_STRING *localname, WS_XML_STRING *ns )
4031 const WS_XML_TEXT_NODE *node = (const WS_XML_TEXT_NODE *)reader->current;
4032 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)node->text;
4033 return str_to_qname( reader, utf8->value.bytes, utf8->value.length, heap, prefix, localname, ns );
4036 /**************************************************************************
4037 * WsReadQualifiedName [webservices.@]
4039 HRESULT WINAPI WsReadQualifiedName( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_STRING *prefix,
4040 WS_XML_STRING *localname, WS_XML_STRING *ns,
4041 WS_ERROR *error )
4043 struct reader *reader = (struct reader *)handle;
4044 HRESULT hr;
4046 TRACE( "%p %p %p %p %p %p\n", handle, heap, prefix, localname, ns, error );
4047 if (error) FIXME( "ignoring error parameter\n" );
4049 if (!reader || !heap) return E_INVALIDARG;
4051 EnterCriticalSection( &reader->cs );
4053 if (reader->magic != READER_MAGIC)
4055 LeaveCriticalSection( &reader->cs );
4056 return E_INVALIDARG;
4059 if (!reader->input_type)
4061 LeaveCriticalSection( &reader->cs );
4062 return WS_E_INVALID_OPERATION;
4065 if (!localname)
4067 LeaveCriticalSection( &reader->cs );
4068 return E_INVALIDARG;
4071 if (reader->state != READER_STATE_TEXT)
4073 LeaveCriticalSection( &reader->cs );
4074 return WS_E_INVALID_FORMAT;
4077 hr = read_qualified_name( reader, heap, prefix, localname, ns );
4079 LeaveCriticalSection( &reader->cs );
4080 return hr;
4083 static const int month_offsets[2][12] =
4085 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
4086 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
4089 static inline int valid_day( int year, int month, int day )
4091 return day > 0 && day <= month_days[leap_year( year )][month - 1];
4094 static inline int leap_days_before( int year )
4096 return (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
4099 static HRESULT str_to_datetime( const unsigned char *bytes, ULONG len, WS_DATETIME *ret )
4101 const unsigned char *p = bytes, *q;
4102 int year, month, day, hour, min, sec, sec_frac = 0, tz_hour, tz_min, tz_neg;
4104 while (len && read_isspace( *p )) { p++; len--; }
4105 while (len && read_isspace( p[len - 1] )) { len--; }
4107 q = p;
4108 while (len && isdigit( *q )) { q++; len--; };
4109 if (q - p != 4 || !len || *q != '-') return WS_E_INVALID_FORMAT;
4110 year = (p[0] - '0') * 1000 + (p[1] - '0') * 100 + (p[2] - '0') * 10 + p[3] - '0';
4111 if (year < 1) return WS_E_INVALID_FORMAT;
4113 p = ++q; len--;
4114 while (len && isdigit( *q )) { q++; len--; };
4115 if (q - p != 2 || !len || *q != '-') return WS_E_INVALID_FORMAT;
4116 month = (p[0] - '0') * 10 + p[1] - '0';
4117 if (month < 1 || month > 12) return WS_E_INVALID_FORMAT;
4119 p = ++q; len--;
4120 while (len && isdigit( *q )) { q++; len--; };
4121 if (q - p != 2 || !len || *q != 'T') return WS_E_INVALID_FORMAT;
4122 day = (p[0] - '0') * 10 + p[1] - '0';
4123 if (!valid_day( year, month, day )) return WS_E_INVALID_FORMAT;
4125 p = ++q; len--;
4126 while (len && isdigit( *q )) { q++; len--; };
4127 if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
4128 hour = (p[0] - '0') * 10 + p[1] - '0';
4129 if (hour > 24) return WS_E_INVALID_FORMAT;
4131 p = ++q; len--;
4132 while (len && isdigit( *q )) { q++; len--; };
4133 if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
4134 min = (p[0] - '0') * 10 + p[1] - '0';
4135 if (min > 59 || (min > 0 && hour == 24)) return WS_E_INVALID_FORMAT;
4137 p = ++q; len--;
4138 while (len && isdigit( *q )) { q++; len--; };
4139 if (q - p != 2 || !len) return WS_E_INVALID_FORMAT;
4140 sec = (p[0] - '0') * 10 + p[1] - '0';
4141 if (sec > 59 || (sec > 0 && hour == 24)) return WS_E_INVALID_FORMAT;
4143 if (*q == '.')
4145 unsigned int i, nb_digits, mul = TICKS_PER_SEC / 10;
4146 p = ++q; len--;
4147 while (len && isdigit( *q )) { q++; len--; };
4148 nb_digits = q - p;
4149 if (nb_digits < 1 || nb_digits > 7) return WS_E_INVALID_FORMAT;
4150 for (i = 0; i < nb_digits; i++)
4152 sec_frac += (p[i] - '0') * mul;
4153 mul /= 10;
4156 if (*q == 'Z')
4158 if (--len) return WS_E_INVALID_FORMAT;
4159 tz_hour = tz_min = tz_neg = 0;
4160 ret->format = WS_DATETIME_FORMAT_UTC;
4162 else if (*q == '+' || *q == '-')
4164 tz_neg = (*q == '-') ? 1 : 0;
4166 p = ++q; len--;
4167 while (len && isdigit( *q )) { q++; len--; };
4168 if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
4169 tz_hour = (p[0] - '0') * 10 + p[1] - '0';
4170 if (tz_hour > 14) return WS_E_INVALID_FORMAT;
4172 p = ++q; len--;
4173 while (len && isdigit( *q )) { q++; len--; };
4174 if (q - p != 2 || len) return WS_E_INVALID_FORMAT;
4175 tz_min = (p[0] - '0') * 10 + p[1] - '0';
4176 if (tz_min > 59 || (tz_min > 0 && tz_hour == 14)) return WS_E_INVALID_FORMAT;
4178 ret->format = WS_DATETIME_FORMAT_LOCAL;
4180 else return WS_E_INVALID_FORMAT;
4182 ret->ticks = ((year - 1) * 365 + leap_days_before( year )) * TICKS_PER_DAY;
4183 ret->ticks += month_offsets[leap_year( year )][month - 1] * TICKS_PER_DAY;
4184 ret->ticks += (day - 1) * TICKS_PER_DAY;
4185 ret->ticks += hour * TICKS_PER_HOUR;
4186 ret->ticks += min * TICKS_PER_MIN;
4187 ret->ticks += sec * TICKS_PER_SEC;
4188 ret->ticks += sec_frac;
4190 if (tz_neg)
4192 if (tz_hour * TICKS_PER_HOUR + tz_min * TICKS_PER_MIN + ret->ticks > TICKS_MAX)
4193 return WS_E_INVALID_FORMAT;
4194 ret->ticks += tz_hour * TICKS_PER_HOUR;
4195 ret->ticks += tz_min * TICKS_PER_MIN;
4197 else
4199 if (tz_hour * TICKS_PER_HOUR + tz_min * TICKS_PER_MIN > ret->ticks)
4200 return WS_E_INVALID_FORMAT;
4201 ret->ticks -= tz_hour * TICKS_PER_HOUR;
4202 ret->ticks -= tz_min * TICKS_PER_MIN;
4205 return S_OK;
4208 /**************************************************************************
4209 * WsDateTimeToFileTime [webservices.@]
4211 HRESULT WINAPI WsDateTimeToFileTime( const WS_DATETIME *dt, FILETIME *ft, WS_ERROR *error )
4213 unsigned __int64 ticks;
4215 TRACE( "%p %p %p\n", dt, ft, error );
4216 if (error) FIXME( "ignoring error parameter\n" );
4218 if (!dt || !ft) return E_INVALIDARG;
4220 if (dt->ticks < TICKS_1601_01_01) return WS_E_INVALID_FORMAT;
4221 ticks = dt->ticks - TICKS_1601_01_01;
4222 ft->dwHighDateTime = ticks >> 32;
4223 ft->dwLowDateTime = (DWORD)ticks;
4224 return S_OK;
4227 /**************************************************************************
4228 * WsFileTimeToDateTime [webservices.@]
4230 HRESULT WINAPI WsFileTimeToDateTime( const FILETIME *ft, WS_DATETIME *dt, WS_ERROR *error )
4232 unsigned __int64 ticks;
4234 TRACE( "%p %p %p\n", ft, dt, error );
4235 if (error) FIXME( "ignoring error parameter\n" );
4237 if (!dt || !ft) return E_INVALIDARG;
4239 ticks = ((unsigned __int64)ft->dwHighDateTime << 32) | ft->dwLowDateTime;
4240 if (ticks > MAX_UINT64 - TICKS_1601_01_01) return WS_E_NUMERIC_OVERFLOW;
4241 if (ticks + TICKS_1601_01_01 > TICKS_MAX) return WS_E_INVALID_FORMAT;
4242 dt->ticks = ticks + TICKS_1601_01_01;
4243 dt->format = WS_DATETIME_FORMAT_UTC;
4244 return S_OK;
4247 static BOOL find_attribute( struct reader *reader, const WS_XML_STRING *localname,
4248 const WS_XML_STRING *ns, ULONG *index )
4250 ULONG i;
4251 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
4253 if (!localname)
4255 *index = reader->current_attr;
4256 return TRUE;
4258 for (i = 0; i < elem->attributeCount; i++)
4260 const WS_XML_STRING *localname2 = elem->attributes[i]->localName;
4261 const WS_XML_STRING *ns2 = elem->attributes[i]->ns;
4263 if (!cmp_name( localname->bytes, localname->length, localname2->bytes, localname2->length ) &&
4264 !cmp_name( ns->bytes, ns->length, ns2->bytes, ns2->length ))
4266 *index = i;
4267 return TRUE;
4270 return FALSE;
4273 /**************************************************************************
4274 * WsFindAttribute [webservices.@]
4276 HRESULT WINAPI WsFindAttribute( WS_XML_READER *handle, const WS_XML_STRING *localname,
4277 const WS_XML_STRING *ns, BOOL required, ULONG *index,
4278 WS_ERROR *error )
4280 struct reader *reader = (struct reader *)handle;
4281 HRESULT hr = S_OK;
4283 TRACE( "%p %s %s %d %p %p\n", handle, debugstr_xmlstr(localname), debugstr_xmlstr(ns),
4284 required, index, error );
4285 if (error) FIXME( "ignoring error parameter\n" );
4287 if (!reader || !localname || !ns || !index) return E_INVALIDARG;
4289 EnterCriticalSection( &reader->cs );
4291 if (reader->magic != READER_MAGIC)
4293 LeaveCriticalSection( &reader->cs );
4294 return E_INVALIDARG;
4297 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT)
4299 LeaveCriticalSection( &reader->cs );
4300 return WS_E_INVALID_OPERATION;
4303 if (!find_attribute( reader, localname, ns, index ))
4305 if (required) hr = WS_E_INVALID_FORMAT;
4306 else
4308 *index = ~0u;
4309 hr = S_FALSE;
4313 LeaveCriticalSection( &reader->cs );
4314 return hr;
4317 static HRESULT get_node_text( struct reader *reader, const WS_XML_TEXT **ret )
4319 WS_XML_TEXT_NODE *node = (WS_XML_TEXT_NODE *)&reader->current->hdr.node;
4320 *ret = node->text;
4321 return S_OK;
4324 static HRESULT get_attribute_text( struct reader *reader, ULONG index, const WS_XML_TEXT **ret )
4326 WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
4327 *ret = elem->attributes[index]->value;
4328 return S_OK;
4331 static BOOL match_element( const struct node *node, const WS_XML_STRING *localname, const WS_XML_STRING *ns )
4333 const WS_XML_ELEMENT_NODE *elem = (const WS_XML_ELEMENT_NODE *)node;
4334 if (node_type( node ) != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
4335 return WsXmlStringEquals( localname, elem->localName, NULL ) == S_OK &&
4336 WsXmlStringEquals( ns, elem->ns, NULL ) == S_OK;
4339 static HRESULT read_next_node( struct reader *reader )
4341 if (reader->current == reader->last) return read_node( reader );
4342 if (move_to_child_node( &reader->current )) return S_OK;
4343 if (move_to_next_node( &reader->current )) return S_OK;
4344 if (!move_to_parent_node( &reader->current )) return WS_E_INVALID_FORMAT;
4345 if (move_to_next_node( &reader->current )) return S_OK;
4346 return WS_E_INVALID_FORMAT;
4349 struct reader_pos
4351 struct node *node;
4352 ULONG attr;
4355 static void save_reader_position( const struct reader *reader, struct reader_pos *pos )
4357 pos->node = reader->current;
4358 pos->attr = reader->current_attr;
4361 static void restore_reader_position( struct reader *reader, const struct reader_pos *pos )
4363 reader->current = pos->node;
4364 reader->current_attr = pos->attr;
4367 static HRESULT get_text( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
4368 const WS_XML_STRING *ns, const WS_XML_TEXT **ret, BOOL *found )
4370 switch (mapping)
4372 case WS_ATTRIBUTE_TYPE_MAPPING:
4374 ULONG index;
4375 if (!(*found = find_attribute( reader, localname, ns, &index ))) return S_OK;
4376 return get_attribute_text( reader, index, ret );
4378 case WS_ELEMENT_TYPE_MAPPING:
4379 case WS_ELEMENT_CONTENT_TYPE_MAPPING:
4380 case WS_ANY_ELEMENT_TYPE_MAPPING:
4382 *found = TRUE;
4383 if (localname)
4385 struct reader_pos pos;
4386 HRESULT hr;
4388 if (!match_element( reader->current, localname, ns ))
4390 *found = FALSE;
4391 return S_OK;
4393 save_reader_position( reader, &pos );
4394 if ((hr = read_next_node( reader )) != S_OK) return hr;
4395 if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT)
4397 restore_reader_position( reader, &pos );
4398 *found = FALSE;
4399 return S_OK;
4402 if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT)
4404 *found = FALSE;
4405 return S_OK;
4407 return get_node_text( reader, ret );
4409 default:
4410 FIXME( "mapping %u not supported\n", mapping );
4411 return E_NOTIMPL;
4415 static HRESULT text_to_bool( const WS_XML_TEXT *text, BOOL *val )
4417 HRESULT hr;
4419 switch (text->textType)
4421 case WS_XML_TEXT_TYPE_UTF8:
4423 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4424 hr = str_to_bool( text_utf8->value.bytes, text_utf8->value.length, val );
4425 break;
4427 case WS_XML_TEXT_TYPE_BOOL:
4429 const WS_XML_BOOL_TEXT *text_bool = (const WS_XML_BOOL_TEXT *)text;
4430 *val = text_bool->value;
4431 hr = S_OK;
4432 break;
4434 default:
4435 FIXME( "unhandled text type %u\n", text->textType );
4436 return E_NOTIMPL;
4439 return hr;
4442 static HRESULT read_type_bool( struct reader *reader, WS_TYPE_MAPPING mapping,
4443 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4444 const WS_BOOL_DESCRIPTION *desc, WS_READ_OPTION option,
4445 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4447 const WS_XML_TEXT *text;
4448 HRESULT hr;
4449 BOOL val = FALSE;
4451 if (desc)
4453 FIXME( "description not supported\n" );
4454 return E_NOTIMPL;
4456 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4457 if (*found && (hr = text_to_bool( text, &val )) != S_OK) return hr;
4459 switch (option)
4461 case WS_READ_REQUIRED_VALUE:
4462 if (!*found) return WS_E_INVALID_FORMAT;
4463 /* fall through */
4465 case WS_READ_NILLABLE_VALUE:
4466 if (size != sizeof(val)) return E_INVALIDARG;
4467 *(BOOL *)ret = val;
4468 break;
4470 case WS_READ_REQUIRED_POINTER:
4471 if (!*found) return WS_E_INVALID_FORMAT;
4472 /* fall through */
4474 case WS_READ_OPTIONAL_POINTER:
4475 case WS_READ_NILLABLE_POINTER:
4477 BOOL *heap_val = NULL;
4478 if (size != sizeof(heap_val)) return E_INVALIDARG;
4479 if (*found)
4481 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4482 *heap_val = val;
4484 *(BOOL **)ret = heap_val;
4485 break;
4487 default:
4488 FIXME( "read option %u not supported\n", option );
4489 return E_NOTIMPL;
4492 return S_OK;
4495 static HRESULT text_to_int8( const WS_XML_TEXT *text, INT64 *val )
4497 HRESULT hr;
4499 switch (text->textType)
4501 case WS_XML_TEXT_TYPE_UTF8:
4503 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4504 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT8, MAX_INT8, val );
4505 break;
4507 case WS_XML_TEXT_TYPE_INT32:
4509 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4510 assert( text_int32->value >= MIN_INT8 );
4511 assert( text_int32->value <= MAX_INT8 );
4512 *val = text_int32->value;
4513 hr = S_OK;
4514 break;
4516 default:
4517 FIXME( "unhandled text type %u\n", text->textType );
4518 return E_NOTIMPL;
4521 return hr;
4524 static HRESULT read_type_int8( struct reader *reader, WS_TYPE_MAPPING mapping,
4525 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4526 const WS_INT8_DESCRIPTION *desc, WS_READ_OPTION option,
4527 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4529 const WS_XML_TEXT *text;
4530 HRESULT hr;
4531 INT64 val = 0;
4533 if (desc)
4535 FIXME( "description not supported\n" );
4536 return E_NOTIMPL;
4538 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4539 if (*found && (hr = text_to_int8( text, &val )) != S_OK) return hr;
4541 switch (option)
4543 case WS_READ_REQUIRED_VALUE:
4544 if (!*found) return WS_E_INVALID_FORMAT;
4545 /* fall through */
4547 case WS_READ_NILLABLE_VALUE:
4548 if (size != sizeof(INT8)) return E_INVALIDARG;
4549 *(INT8 *)ret = val;
4550 break;
4552 case WS_READ_REQUIRED_POINTER:
4553 if (!*found) return WS_E_INVALID_FORMAT;
4554 /* fall through */
4556 case WS_READ_OPTIONAL_POINTER:
4557 case WS_READ_NILLABLE_POINTER:
4559 INT8 *heap_val = NULL;
4560 if (size != sizeof(heap_val)) return E_INVALIDARG;
4561 if (*found)
4563 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4564 *heap_val = val;
4566 *(INT8 **)ret = heap_val;
4567 break;
4569 default:
4570 FIXME( "read option %u not supported\n", option );
4571 return E_NOTIMPL;
4574 return S_OK;
4577 static HRESULT text_to_int16( const WS_XML_TEXT *text, INT64 *val )
4579 HRESULT hr;
4581 switch (text->textType)
4583 case WS_XML_TEXT_TYPE_UTF8:
4585 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4586 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT16, MAX_INT16, val );
4587 break;
4589 case WS_XML_TEXT_TYPE_INT32:
4591 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4592 assert( text_int32->value >= MIN_INT16 );
4593 assert( text_int32->value <= MAX_INT16 );
4594 *val = text_int32->value;
4595 hr = S_OK;
4596 break;
4598 default:
4599 FIXME( "unhandled text type %u\n", text->textType );
4600 return E_NOTIMPL;
4603 return hr;
4606 static HRESULT read_type_int16( struct reader *reader, WS_TYPE_MAPPING mapping,
4607 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4608 const WS_INT16_DESCRIPTION *desc, WS_READ_OPTION option,
4609 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4611 const WS_XML_TEXT *text;
4612 HRESULT hr;
4613 INT64 val = 0;
4615 if (desc)
4617 FIXME( "description not supported\n" );
4618 return E_NOTIMPL;
4620 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4621 if (*found && (hr = text_to_int16( text, &val )) != S_OK) return hr;
4623 switch (option)
4625 case WS_READ_REQUIRED_VALUE:
4626 if (!*found) return WS_E_INVALID_FORMAT;
4627 /* fall through */
4629 case WS_READ_NILLABLE_VALUE:
4630 if (size != sizeof(INT16)) return E_INVALIDARG;
4631 *(INT16 *)ret = val;
4632 break;
4634 case WS_READ_REQUIRED_POINTER:
4635 if (!*found) return WS_E_INVALID_FORMAT;
4636 /* fall through */
4638 case WS_READ_OPTIONAL_POINTER:
4639 case WS_READ_NILLABLE_POINTER:
4641 INT16 *heap_val = NULL;
4642 if (size != sizeof(heap_val)) return E_INVALIDARG;
4643 if (*found)
4645 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4646 *heap_val = val;
4648 *(INT16 **)ret = heap_val;
4649 break;
4651 default:
4652 FIXME( "read option %u not supported\n", option );
4653 return E_NOTIMPL;
4656 return S_OK;
4659 static HRESULT text_to_int32( const WS_XML_TEXT *text, INT64 *val )
4661 HRESULT hr;
4663 switch (text->textType)
4665 case WS_XML_TEXT_TYPE_UTF8:
4667 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4668 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT32, MAX_INT32, val );
4669 break;
4671 case WS_XML_TEXT_TYPE_INT32:
4673 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4674 *val = text_int32->value;
4675 hr = S_OK;
4676 break;
4678 default:
4679 FIXME( "unhandled text type %u\n", text->textType );
4680 return E_NOTIMPL;
4683 return hr;
4686 static HRESULT read_type_int32( struct reader *reader, WS_TYPE_MAPPING mapping,
4687 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4688 const WS_INT32_DESCRIPTION *desc, WS_READ_OPTION option,
4689 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4691 const WS_XML_TEXT *text;
4692 HRESULT hr;
4693 INT64 val = 0;
4695 if (desc)
4697 FIXME( "description not supported\n" );
4698 return E_NOTIMPL;
4700 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4701 if (*found && (hr = text_to_int32( text, &val )) != S_OK) return hr;
4703 switch (option)
4705 case WS_READ_REQUIRED_VALUE:
4706 if (!*found) return WS_E_INVALID_FORMAT;
4707 /* fall through */
4709 case WS_READ_NILLABLE_VALUE:
4710 if (size != sizeof(INT32)) return E_INVALIDARG;
4711 *(INT32 *)ret = val;
4712 break;
4714 case WS_READ_REQUIRED_POINTER:
4715 if (!*found) return WS_E_INVALID_FORMAT;
4716 /* fall through */
4718 case WS_READ_OPTIONAL_POINTER:
4719 case WS_READ_NILLABLE_POINTER:
4721 INT32 *heap_val = NULL;
4722 if (size != sizeof(heap_val)) return E_INVALIDARG;
4723 if (*found)
4725 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4726 *heap_val = val;
4728 *(INT32 **)ret = heap_val;
4729 break;
4731 default:
4732 FIXME( "read option %u not supported\n", option );
4733 return E_NOTIMPL;
4736 return S_OK;
4739 static HRESULT text_to_int64( const WS_XML_TEXT *text, INT64 *val )
4741 HRESULT hr;
4743 switch (text->textType)
4745 case WS_XML_TEXT_TYPE_UTF8:
4747 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4748 hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT64, MAX_INT64, val );
4749 break;
4751 case WS_XML_TEXT_TYPE_INT64:
4753 const WS_XML_INT64_TEXT *text_int64 = (const WS_XML_INT64_TEXT *)text;
4754 *val = text_int64->value;
4755 hr = S_OK;
4756 break;
4758 default:
4759 FIXME( "unhandled text type %u\n", text->textType );
4760 return E_NOTIMPL;
4763 return hr;
4766 static HRESULT read_type_int64( struct reader *reader, WS_TYPE_MAPPING mapping,
4767 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4768 const WS_INT64_DESCRIPTION *desc, WS_READ_OPTION option,
4769 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4771 const WS_XML_TEXT *text;
4772 HRESULT hr;
4773 INT64 val = 0;
4775 if (desc)
4777 FIXME( "description not supported\n" );
4778 return E_NOTIMPL;
4780 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4781 if (*found && (hr = text_to_int64( text, &val )) != S_OK) return hr;
4783 switch (option)
4785 case WS_READ_REQUIRED_VALUE:
4786 if (!*found) return WS_E_INVALID_FORMAT;
4787 /* fall through */
4789 case WS_READ_NILLABLE_VALUE:
4790 if (size != sizeof(val)) return E_INVALIDARG;
4791 *(INT64 *)ret = val;
4792 break;
4794 case WS_READ_REQUIRED_POINTER:
4795 if (!*found) return WS_E_INVALID_FORMAT;
4796 /* fall through */
4798 case WS_READ_OPTIONAL_POINTER:
4799 case WS_READ_NILLABLE_POINTER:
4801 INT64 *heap_val = NULL;
4802 if (size != sizeof(heap_val)) return E_INVALIDARG;
4803 if (*found)
4805 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4806 *heap_val = val;
4808 *(INT64 **)ret = heap_val;
4809 break;
4811 default:
4812 FIXME( "read option %u not supported\n", option );
4813 return E_NOTIMPL;
4816 return S_OK;
4819 static HRESULT text_to_uint8( const WS_XML_TEXT *text, UINT64 *val )
4821 HRESULT hr;
4823 switch (text->textType)
4825 case WS_XML_TEXT_TYPE_UTF8:
4827 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4828 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT8, val );
4829 break;
4831 case WS_XML_TEXT_TYPE_UINT64:
4833 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
4834 assert( text_uint64->value <= MAX_UINT8 );
4835 *val = text_uint64->value;
4836 hr = S_OK;
4837 break;
4839 default:
4840 FIXME( "unhandled text type %u\n", text->textType );
4841 return E_NOTIMPL;
4844 return hr;
4847 static HRESULT read_type_uint8( struct reader *reader, WS_TYPE_MAPPING mapping,
4848 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4849 const WS_UINT8_DESCRIPTION *desc, WS_READ_OPTION option,
4850 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4852 const WS_XML_TEXT *text;
4853 HRESULT hr;
4854 UINT64 val = 0;
4856 if (desc)
4858 FIXME( "description not supported\n" );
4859 return E_NOTIMPL;
4861 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4862 if (*found && (hr = text_to_uint8( text, &val )) != S_OK) return hr;
4864 switch (option)
4866 case WS_READ_REQUIRED_VALUE:
4867 if (!*found) return WS_E_INVALID_FORMAT;
4868 /* fall through */
4870 case WS_READ_NILLABLE_VALUE:
4871 if (size != sizeof(UINT8)) return E_INVALIDARG;
4872 *(UINT8 *)ret = val;
4873 break;
4875 case WS_READ_REQUIRED_POINTER:
4876 if (!*found) return WS_E_INVALID_FORMAT;
4877 /* fall through */
4879 case WS_READ_OPTIONAL_POINTER:
4880 case WS_READ_NILLABLE_POINTER:
4882 UINT8 *heap_val = NULL;
4883 if (size != sizeof(heap_val)) return E_INVALIDARG;
4884 if (*found)
4886 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4887 *heap_val = val;
4889 *(UINT8 **)ret = heap_val;
4890 break;
4892 default:
4893 FIXME( "read option %u not supported\n", option );
4894 return E_NOTIMPL;
4897 return S_OK;
4900 static HRESULT text_to_uint16( const WS_XML_TEXT *text, UINT64 *val )
4902 HRESULT hr;
4904 switch (text->textType)
4906 case WS_XML_TEXT_TYPE_UTF8:
4908 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4909 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT16, val );
4910 break;
4912 case WS_XML_TEXT_TYPE_INT32:
4914 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
4915 assert( text_int32->value >= 0 );
4916 assert( text_int32->value <= MAX_UINT16 );
4917 *val = text_int32->value;
4918 hr = S_OK;
4919 break;
4921 case WS_XML_TEXT_TYPE_UINT64:
4923 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
4924 assert( text_uint64->value <= MAX_UINT16 );
4925 *val = text_uint64->value;
4926 hr = S_OK;
4927 break;
4929 default:
4930 FIXME( "unhandled text type %u\n", text->textType );
4931 return E_NOTIMPL;
4934 return hr;
4937 static HRESULT read_type_uint16( struct reader *reader, WS_TYPE_MAPPING mapping,
4938 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4939 const WS_UINT16_DESCRIPTION *desc, WS_READ_OPTION option,
4940 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4942 const WS_XML_TEXT *text;
4943 HRESULT hr;
4944 UINT64 val = 0;
4946 if (desc)
4948 FIXME( "description not supported\n" );
4949 return E_NOTIMPL;
4951 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
4952 if (*found && (hr = text_to_uint16( text, &val )) != S_OK) return hr;
4954 switch (option)
4956 case WS_READ_REQUIRED_VALUE:
4957 if (!*found) return WS_E_INVALID_FORMAT;
4958 /* fall through */
4960 case WS_READ_NILLABLE_VALUE:
4961 if (size != sizeof(UINT16)) return E_INVALIDARG;
4962 *(UINT16 *)ret = val;
4963 break;
4965 case WS_READ_REQUIRED_POINTER:
4966 if (!*found) return WS_E_INVALID_FORMAT;
4967 /* fall through */
4969 case WS_READ_OPTIONAL_POINTER:
4970 case WS_READ_NILLABLE_POINTER:
4972 UINT16 *heap_val = NULL;
4973 if (size != sizeof(heap_val)) return E_INVALIDARG;
4974 if (*found)
4976 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
4977 *heap_val = val;
4979 *(UINT16 **)ret = heap_val;
4980 break;
4982 default:
4983 FIXME( "read option %u not supported\n", option );
4984 return E_NOTIMPL;
4987 return S_OK;
4990 static HRESULT text_to_uint32( const WS_XML_TEXT *text, UINT64 *val )
4992 HRESULT hr;
4994 switch (text->textType)
4996 case WS_XML_TEXT_TYPE_UTF8:
4998 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
4999 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT32, val );
5000 break;
5002 case WS_XML_TEXT_TYPE_INT32:
5004 const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
5005 assert( text_int32->value >= 0 );
5006 *val = text_int32->value;
5007 hr = S_OK;
5008 break;
5010 case WS_XML_TEXT_TYPE_UINT64:
5012 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
5013 assert( text_uint64->value <= MAX_UINT32 );
5014 *val = text_uint64->value;
5015 hr = S_OK;
5016 break;
5018 default:
5019 FIXME( "unhandled text type %u\n", text->textType );
5020 return E_NOTIMPL;
5023 return hr;
5026 static HRESULT read_type_uint32( struct reader *reader, WS_TYPE_MAPPING mapping,
5027 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5028 const WS_UINT32_DESCRIPTION *desc, WS_READ_OPTION option,
5029 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5031 const WS_XML_TEXT *text;
5032 HRESULT hr;
5033 UINT64 val = 0;
5035 if (desc)
5037 FIXME( "description not supported\n" );
5038 return E_NOTIMPL;
5040 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5041 if (*found && (hr = text_to_uint32( text, &val )) != S_OK) return hr;
5043 switch (option)
5045 case WS_READ_REQUIRED_VALUE:
5046 if (!*found) return WS_E_INVALID_FORMAT;
5047 /* fall through */
5049 case WS_READ_NILLABLE_VALUE:
5050 if (size != sizeof(UINT32)) return E_INVALIDARG;
5051 *(UINT32 *)ret = val;
5052 break;
5054 case WS_READ_REQUIRED_POINTER:
5055 if (!*found) return WS_E_INVALID_FORMAT;
5056 /* fall through */
5058 case WS_READ_OPTIONAL_POINTER:
5059 case WS_READ_NILLABLE_POINTER:
5061 UINT32 *heap_val = NULL;
5062 if (size != sizeof(heap_val)) return E_INVALIDARG;
5063 if (*found)
5065 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5066 *heap_val = val;
5068 *(UINT32 **)ret = heap_val;
5069 break;
5071 default:
5072 FIXME( "read option %u not supported\n", option );
5073 return E_NOTIMPL;
5076 return S_OK;
5079 static HRESULT text_to_uint64( const WS_XML_TEXT *text, UINT64 *val )
5081 HRESULT hr;
5083 switch (text->textType)
5085 case WS_XML_TEXT_TYPE_UTF8:
5087 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5088 hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT64, val );
5089 break;
5091 case WS_XML_TEXT_TYPE_INT64:
5093 const WS_XML_INT64_TEXT *text_int64 = (const WS_XML_INT64_TEXT *)text;
5094 *val = text_int64->value;
5095 hr = S_OK;
5096 break;
5098 case WS_XML_TEXT_TYPE_UINT64:
5100 const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
5101 *val = text_uint64->value;
5102 hr = S_OK;
5103 break;
5105 default:
5106 FIXME( "unhandled text type %u\n", text->textType );
5107 return E_NOTIMPL;
5110 return hr;
5113 static HRESULT read_type_uint64( struct reader *reader, WS_TYPE_MAPPING mapping,
5114 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5115 const WS_UINT64_DESCRIPTION *desc, WS_READ_OPTION option,
5116 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5118 const WS_XML_TEXT *text;
5119 HRESULT hr;
5120 UINT64 val = 0;
5122 if (desc)
5124 FIXME( "description not supported\n" );
5125 return E_NOTIMPL;
5127 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5128 if (*found && (hr = text_to_uint64( text, &val )) != S_OK) return hr;
5130 switch (option)
5132 case WS_READ_REQUIRED_VALUE:
5133 if (!*found) return WS_E_INVALID_FORMAT;
5134 /* fall through */
5136 case WS_READ_NILLABLE_VALUE:
5137 if (size != sizeof(val)) return E_INVALIDARG;
5138 *(UINT64 *)ret = val;
5139 break;
5141 case WS_READ_REQUIRED_POINTER:
5142 if (!*found) return WS_E_INVALID_FORMAT;
5143 /* fall through */
5145 case WS_READ_OPTIONAL_POINTER:
5146 case WS_READ_NILLABLE_POINTER:
5148 UINT64 *heap_val = NULL;
5149 if (size != sizeof(heap_val)) return E_INVALIDARG;
5150 if (*found)
5152 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5153 *heap_val = val;
5155 *(UINT64 **)ret = heap_val;
5156 break;
5158 default:
5159 FIXME( "read option %u not supported\n", option );
5160 return E_NOTIMPL;
5163 return S_OK;
5166 static HRESULT text_to_float( const WS_XML_TEXT *text, float *val )
5168 HRESULT hr;
5170 switch (text->textType)
5172 case WS_XML_TEXT_TYPE_UTF8:
5174 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5175 hr = str_to_float( text_utf8->value.bytes, text_utf8->value.length, val );
5176 break;
5178 case WS_XML_TEXT_TYPE_FLOAT:
5180 const WS_XML_FLOAT_TEXT *text_float = (const WS_XML_FLOAT_TEXT *)text;
5181 *val = text_float->value;
5182 hr = S_OK;
5183 break;
5185 default:
5186 FIXME( "unhandled text type %u\n", text->textType );
5187 return E_NOTIMPL;
5190 return hr;
5193 static HRESULT read_type_float( struct reader *reader, WS_TYPE_MAPPING mapping,
5194 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5195 const WS_FLOAT_DESCRIPTION *desc, WS_READ_OPTION option,
5196 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5198 const WS_XML_TEXT *text;
5199 HRESULT hr;
5200 float val = 0.0;
5202 if (desc) FIXME( "ignoring description\n" );
5204 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5205 if (*found && (hr = text_to_float( text, &val )) != S_OK) return hr;
5207 switch (option)
5209 case WS_READ_REQUIRED_VALUE:
5210 if (!*found) return WS_E_INVALID_FORMAT;
5211 /* fall through */
5213 case WS_READ_NILLABLE_VALUE:
5214 if (size != sizeof(val)) return E_INVALIDARG;
5215 *(float *)ret = val;
5216 break;
5218 case WS_READ_REQUIRED_POINTER:
5219 if (!*found) return WS_E_INVALID_FORMAT;
5220 /* fall through */
5222 case WS_READ_OPTIONAL_POINTER:
5223 case WS_READ_NILLABLE_POINTER:
5225 float *heap_val = NULL;
5226 if (size != sizeof(heap_val)) return E_INVALIDARG;
5227 if (*found)
5229 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5230 *heap_val = val;
5232 *(float **)ret = heap_val;
5233 break;
5235 default:
5236 FIXME( "read option %u not supported\n", option );
5237 return E_NOTIMPL;
5240 return S_OK;
5243 static HRESULT text_to_double( const WS_XML_TEXT *text, double *val )
5245 HRESULT hr;
5247 switch (text->textType)
5249 case WS_XML_TEXT_TYPE_UTF8:
5251 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5252 hr = str_to_double( text_utf8->value.bytes, text_utf8->value.length, val );
5253 break;
5255 case WS_XML_TEXT_TYPE_DOUBLE:
5257 const WS_XML_DOUBLE_TEXT *text_double = (const WS_XML_DOUBLE_TEXT *)text;
5258 *val = text_double->value;
5259 hr = S_OK;
5260 break;
5262 default:
5263 FIXME( "unhandled text type %u\n", text->textType );
5264 return E_NOTIMPL;
5267 return hr;
5270 static HRESULT read_type_double( struct reader *reader, WS_TYPE_MAPPING mapping,
5271 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5272 const WS_DOUBLE_DESCRIPTION *desc, WS_READ_OPTION option,
5273 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5275 const WS_XML_TEXT *text;
5276 HRESULT hr;
5277 double val = 0.0;
5279 if (desc) FIXME( "ignoring description\n" );
5281 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5282 if (*found && (hr = text_to_double( text, &val )) != S_OK) return hr;
5284 switch (option)
5286 case WS_READ_REQUIRED_VALUE:
5287 if (!*found) return WS_E_INVALID_FORMAT;
5288 /* fall through */
5290 case WS_READ_NILLABLE_VALUE:
5291 if (size != sizeof(val)) return E_INVALIDARG;
5292 *(double *)ret = val;
5293 break;
5295 case WS_READ_REQUIRED_POINTER:
5296 if (!*found) return WS_E_INVALID_FORMAT;
5297 /* fall through */
5299 case WS_READ_OPTIONAL_POINTER:
5300 case WS_READ_NILLABLE_POINTER:
5302 double *heap_val = NULL;
5303 if (size != sizeof(heap_val)) return E_INVALIDARG;
5304 if (*found)
5306 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5307 *heap_val = val;
5309 *(double **)ret = heap_val;
5310 break;
5312 default:
5313 FIXME( "read option %u not supported\n", option );
5314 return E_NOTIMPL;
5317 return S_OK;
5320 static HRESULT text_to_wsz( const WS_XML_TEXT *text, WS_HEAP *heap, WCHAR **ret )
5322 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5323 int len;
5325 assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5326 len = MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, NULL, 0 );
5327 if (!(*ret = ws_alloc( heap, (len + 1) * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
5328 MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, *ret, len );
5329 (*ret)[len] = 0;
5330 return S_OK;
5333 static HRESULT read_type_wsz( struct reader *reader, WS_TYPE_MAPPING mapping,
5334 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5335 const WS_WSZ_DESCRIPTION *desc, WS_READ_OPTION option,
5336 WS_HEAP *heap, WCHAR **ret, ULONG size, BOOL *found )
5338 const WS_XML_TEXT *text;
5339 HRESULT hr;
5340 WCHAR *str = NULL;
5342 if (desc)
5344 FIXME( "description not supported\n" );
5345 return E_NOTIMPL;
5347 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5348 if (*found && (hr = text_to_wsz( text, heap, &str )) != S_OK) return hr;
5350 switch (option)
5352 case WS_READ_REQUIRED_POINTER:
5353 if (!str && !(str = ws_alloc_zero( heap, sizeof(*str) ))) return WS_E_QUOTA_EXCEEDED;
5354 /* fall through */
5356 case WS_READ_OPTIONAL_POINTER:
5357 case WS_READ_NILLABLE_POINTER:
5358 if (size != sizeof(str)) return E_INVALIDARG;
5359 *ret = str;
5360 break;
5362 default:
5363 FIXME( "read option %u not supported\n", option );
5364 return E_NOTIMPL;
5367 return S_OK;
5370 static HRESULT get_enum_value( const WS_XML_TEXT *text, const WS_ENUM_DESCRIPTION *desc, int *ret )
5372 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5373 ULONG i;
5375 assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5376 for (i = 0; i < desc->valueCount; i++)
5378 if (WsXmlStringEquals( &utf8->value, desc->values[i].name, NULL ) == S_OK)
5380 *ret = desc->values[i].value;
5381 return S_OK;
5384 return WS_E_INVALID_FORMAT;
5387 static HRESULT read_type_enum( struct reader *reader, WS_TYPE_MAPPING mapping,
5388 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5389 const WS_ENUM_DESCRIPTION *desc, WS_READ_OPTION option,
5390 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5392 const WS_XML_TEXT *text;
5393 HRESULT hr;
5394 int val = 0;
5396 if (!desc) return E_INVALIDARG;
5398 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5399 if (*found && (hr = get_enum_value( text, desc, &val )) != S_OK) return hr;
5401 switch (option)
5403 case WS_READ_REQUIRED_VALUE:
5404 if (!*found) return WS_E_INVALID_FORMAT;
5405 /* fall through */
5407 case WS_READ_NILLABLE_VALUE:
5408 if (size != sizeof(val)) return E_INVALIDARG;
5409 *(int *)ret = val;
5410 break;
5412 case WS_READ_REQUIRED_POINTER:
5413 if (!*found) return WS_E_INVALID_FORMAT;
5414 /* fall through */
5416 case WS_READ_OPTIONAL_POINTER:
5417 case WS_READ_NILLABLE_POINTER:
5419 int *heap_val = NULL;
5420 if (size != sizeof(heap_val)) return E_INVALIDARG;
5421 if (*found)
5423 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5424 *heap_val = val;
5426 *(int **)ret = heap_val;
5427 break;
5429 default:
5430 FIXME( "read option %u not supported\n", option );
5431 return E_NOTIMPL;
5434 return S_OK;
5437 static HRESULT text_to_datetime( const WS_XML_TEXT *text, WS_DATETIME *val )
5439 HRESULT hr;
5441 switch (text->textType)
5443 case WS_XML_TEXT_TYPE_UTF8:
5445 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5446 hr = str_to_datetime( text_utf8->value.bytes, text_utf8->value.length, val );
5447 break;
5449 case WS_XML_TEXT_TYPE_DATETIME:
5451 const WS_XML_DATETIME_TEXT *text_datetime = (const WS_XML_DATETIME_TEXT *)text;
5452 *val = text_datetime->value;
5453 hr = S_OK;
5454 break;
5456 default:
5457 FIXME( "unhandled text type %u\n", text->textType );
5458 return E_NOTIMPL;
5461 return hr;
5464 static HRESULT read_type_datetime( struct reader *reader, WS_TYPE_MAPPING mapping,
5465 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5466 const WS_DATETIME_DESCRIPTION *desc, WS_READ_OPTION option,
5467 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5469 const WS_XML_TEXT *text;
5470 HRESULT hr;
5471 WS_DATETIME val = {0, WS_DATETIME_FORMAT_UTC};
5473 if (desc) FIXME( "ignoring description\n" );
5475 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5476 if (*found && (hr = text_to_datetime( text, &val )) != S_OK) return hr;
5478 switch (option)
5480 case WS_READ_REQUIRED_VALUE:
5481 if (!*found) return WS_E_INVALID_FORMAT;
5482 /* fall through */
5484 case WS_READ_NILLABLE_VALUE:
5485 if (size != sizeof(val)) return E_INVALIDARG;
5486 *(WS_DATETIME *)ret = val;
5487 break;
5489 case WS_READ_REQUIRED_POINTER:
5490 if (!*found) return WS_E_INVALID_FORMAT;
5491 /* fall through */
5493 case WS_READ_OPTIONAL_POINTER:
5494 case WS_READ_NILLABLE_POINTER:
5496 WS_DATETIME *heap_val = NULL;
5497 if (size != sizeof(heap_val)) return E_INVALIDARG;
5498 if (*found)
5500 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5501 *heap_val = val;
5503 *(WS_DATETIME **)ret = heap_val;
5504 break;
5506 default:
5507 FIXME( "read option %u not supported\n", option );
5508 return E_NOTIMPL;
5511 return S_OK;
5514 static HRESULT text_to_guid( const WS_XML_TEXT *text, GUID *val )
5516 HRESULT hr;
5518 switch (text->textType)
5520 case WS_XML_TEXT_TYPE_UTF8:
5522 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5523 hr = str_to_guid( text_utf8->value.bytes, text_utf8->value.length, val );
5524 break;
5526 case WS_XML_TEXT_TYPE_GUID:
5528 const WS_XML_GUID_TEXT *text_guid = (const WS_XML_GUID_TEXT *)text;
5529 *val = text_guid->value;
5530 hr = S_OK;
5531 break;
5533 default:
5534 FIXME( "unhandled text type %u\n", text->textType );
5535 return E_NOTIMPL;
5538 return hr;
5541 static HRESULT read_type_guid( struct reader *reader, WS_TYPE_MAPPING mapping,
5542 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5543 const WS_GUID_DESCRIPTION *desc, WS_READ_OPTION option,
5544 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5546 const WS_XML_TEXT *text;
5547 GUID val = {0};
5548 HRESULT hr;
5550 if (desc) FIXME( "ignoring description\n" );
5552 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5553 if (*found && (hr = text_to_guid( text, &val )) != S_OK) return hr;
5555 switch (option)
5557 case WS_READ_REQUIRED_VALUE:
5558 if (!*found) return WS_E_INVALID_FORMAT;
5559 /* fall through */
5561 case WS_READ_NILLABLE_VALUE:
5562 if (size != sizeof(val)) return E_INVALIDARG;
5563 *(GUID *)ret = val;
5564 break;
5566 case WS_READ_REQUIRED_POINTER:
5567 if (!*found) return WS_E_INVALID_FORMAT;
5568 /* fall through */
5570 case WS_READ_OPTIONAL_POINTER:
5571 case WS_READ_NILLABLE_POINTER:
5573 GUID *heap_val = NULL;
5574 if (size != sizeof(heap_val)) return E_INVALIDARG;
5575 if (*found)
5577 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5578 *heap_val = val;
5580 *(GUID **)ret = heap_val;
5581 break;
5583 default:
5584 FIXME( "read option %u not supported\n", option );
5585 return E_NOTIMPL;
5588 return S_OK;
5591 static HRESULT text_to_unique_id( const WS_XML_TEXT *text, WS_HEAP *heap, WS_UNIQUE_ID *val )
5593 HRESULT hr;
5595 switch (text->textType)
5597 case WS_XML_TEXT_TYPE_UTF8:
5599 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5600 hr = str_to_unique_id( text_utf8->value.bytes, text_utf8->value.length, heap, val );
5601 break;
5603 case WS_XML_TEXT_TYPE_UNIQUE_ID:
5605 const WS_XML_UNIQUE_ID_TEXT *text_unique_id = (const WS_XML_UNIQUE_ID_TEXT *)text;
5606 val->guid = text_unique_id->value;
5607 val->uri.length = 0;
5608 val->uri.chars = NULL;
5609 hr = S_OK;
5610 break;
5612 default:
5613 FIXME( "unhandled text type %u\n", text->textType );
5614 return E_NOTIMPL;
5617 return hr;
5620 static HRESULT read_type_unique_id( struct reader *reader, WS_TYPE_MAPPING mapping,
5621 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5622 const WS_UNIQUE_ID_DESCRIPTION *desc, WS_READ_OPTION option,
5623 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5625 const WS_XML_TEXT *text;
5626 WS_UNIQUE_ID val = {{0}};
5627 HRESULT hr;
5629 if (desc) FIXME( "ignoring description\n" );
5631 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5632 if (*found && (hr = text_to_unique_id( text, heap, &val )) != S_OK) return hr;
5634 switch (option)
5636 case WS_READ_REQUIRED_VALUE:
5637 if (!*found) return WS_E_INVALID_FORMAT;
5638 /* fall through */
5640 case WS_READ_NILLABLE_VALUE:
5641 if (size != sizeof(val)) return E_INVALIDARG;
5642 *(WS_UNIQUE_ID *)ret = val;
5643 break;
5645 case WS_READ_REQUIRED_POINTER:
5646 if (!*found) return WS_E_INVALID_FORMAT;
5647 /* fall through */
5649 case WS_READ_OPTIONAL_POINTER:
5650 case WS_READ_NILLABLE_POINTER:
5652 WS_UNIQUE_ID *heap_val = NULL;
5653 if (size != sizeof(heap_val)) return E_INVALIDARG;
5654 if (*found)
5656 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5657 *heap_val = val;
5659 *(WS_UNIQUE_ID **)ret = heap_val;
5660 break;
5662 default:
5663 FIXME( "read option %u not supported\n", option );
5664 return E_NOTIMPL;
5667 return S_OK;
5670 static HRESULT text_to_string( const WS_XML_TEXT *text, WS_HEAP *heap, WS_STRING *val )
5672 HRESULT hr;
5674 switch (text->textType)
5676 case WS_XML_TEXT_TYPE_UTF8:
5678 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5679 hr = str_to_string( text_utf8->value.bytes, text_utf8->value.length, heap, val );
5680 break;
5682 case WS_XML_TEXT_TYPE_UTF16:
5684 const WS_XML_UTF16_TEXT *text_utf16 = (const WS_XML_UTF16_TEXT *)text;
5685 if (!(val->chars = ws_alloc( heap, text_utf16->byteCount ))) return WS_E_QUOTA_EXCEEDED;
5686 memcpy( val->chars, text_utf16->bytes, text_utf16->byteCount );
5687 val->length = text_utf16->byteCount / sizeof(WCHAR);
5688 hr = S_OK;
5689 break;
5691 default:
5692 FIXME( "unhandled text type %u\n", text->textType );
5693 return E_NOTIMPL;
5696 return hr;
5699 static HRESULT read_type_string( struct reader *reader, WS_TYPE_MAPPING mapping,
5700 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5701 const WS_STRING_DESCRIPTION *desc, WS_READ_OPTION option,
5702 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5704 const WS_XML_TEXT *text;
5705 WS_STRING val = {0};
5706 HRESULT hr;
5708 if (desc) FIXME( "ignoring description\n" );
5710 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5711 if (*found && (hr = text_to_string( text, heap, &val )) != S_OK) return hr;
5713 switch (option)
5715 case WS_READ_REQUIRED_VALUE:
5716 case WS_READ_NILLABLE_VALUE:
5717 if (size != sizeof(val)) return E_INVALIDARG;
5718 *(WS_STRING *)ret = val;
5719 break;
5721 case WS_READ_REQUIRED_POINTER:
5723 WS_STRING *heap_val;
5724 if (size != sizeof(heap_val)) return E_INVALIDARG;
5725 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5726 *heap_val = val;
5727 *(WS_STRING **)ret = heap_val;
5728 break;
5730 case WS_READ_OPTIONAL_POINTER:
5731 case WS_READ_NILLABLE_POINTER:
5733 WS_STRING *heap_val = NULL;
5734 if (size != sizeof(heap_val)) return E_INVALIDARG;
5735 if (*found)
5737 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5738 *heap_val = val;
5740 *(WS_STRING **)ret = heap_val;
5741 break;
5743 default:
5744 FIXME( "read option %u not supported\n", option );
5745 return E_NOTIMPL;
5748 return S_OK;
5751 static HRESULT text_to_bytes( const WS_XML_TEXT *text, WS_HEAP *heap, WS_BYTES *val )
5753 HRESULT hr;
5755 switch (text->textType)
5757 case WS_XML_TEXT_TYPE_UTF8:
5759 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5760 hr = str_to_bytes( text_utf8->value.bytes, text_utf8->value.length, heap, val );
5761 break;
5763 case WS_XML_TEXT_TYPE_BASE64:
5765 const WS_XML_BASE64_TEXT *text_base64 = (const WS_XML_BASE64_TEXT *)text;
5766 if (!(val->bytes = ws_alloc( heap, text_base64->length ))) return WS_E_QUOTA_EXCEEDED;
5767 memcpy( val->bytes, text_base64->bytes, text_base64->length );
5768 val->length = text_base64->length;
5769 hr = S_OK;
5770 break;
5772 default:
5773 FIXME( "unhandled text type %u\n", text->textType );
5774 return E_NOTIMPL;
5777 return hr;
5780 static HRESULT read_type_bytes( struct reader *reader, WS_TYPE_MAPPING mapping,
5781 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5782 const WS_BYTES_DESCRIPTION *desc, WS_READ_OPTION option,
5783 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5785 const WS_XML_TEXT *text;
5786 WS_BYTES val = {0};
5787 HRESULT hr;
5789 if (desc) FIXME( "ignoring description\n" );
5791 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5792 if (*found && (hr = text_to_bytes( text, heap, &val )) != S_OK) return hr;
5794 switch (option)
5796 case WS_READ_REQUIRED_VALUE:
5797 case WS_READ_NILLABLE_VALUE:
5798 if (size != sizeof(val)) return E_INVALIDARG;
5799 *(WS_BYTES *)ret = val;
5800 break;
5802 case WS_READ_REQUIRED_POINTER:
5804 WS_BYTES *heap_val;
5805 if (size != sizeof(heap_val)) return E_INVALIDARG;
5806 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5807 *heap_val = val;
5808 *(WS_BYTES **)ret = heap_val;
5809 break;
5811 case WS_READ_OPTIONAL_POINTER:
5812 case WS_READ_NILLABLE_POINTER:
5814 WS_BYTES *heap_val = NULL;
5815 if (size != sizeof(heap_val)) return E_INVALIDARG;
5816 if (*found)
5818 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5819 *heap_val = val;
5821 *(WS_BYTES **)ret = heap_val;
5822 break;
5824 default:
5825 FIXME( "read option %u not supported\n", option );
5826 return E_NOTIMPL;
5829 return S_OK;
5832 static HRESULT text_to_xml_string( const WS_XML_TEXT *text, WS_HEAP *heap, WS_XML_STRING *val )
5834 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5835 assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5836 return str_to_xml_string( utf8->value.bytes, utf8->value.length, heap, val );
5839 static HRESULT read_type_xml_string( struct reader *reader, WS_TYPE_MAPPING mapping,
5840 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5841 const WS_XML_STRING_DESCRIPTION *desc, WS_READ_OPTION option,
5842 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5844 const WS_XML_TEXT *text;
5845 WS_XML_STRING val = {0};
5846 HRESULT hr;
5848 if (desc) FIXME( "ignoring description\n" );
5850 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5851 if (*found && (hr = text_to_xml_string( text, heap, &val )) != S_OK) return hr;
5853 switch (option)
5855 case WS_READ_REQUIRED_VALUE:
5856 case WS_READ_NILLABLE_VALUE:
5857 if (size != sizeof(val)) return E_INVALIDARG;
5858 *(WS_XML_STRING *)ret = val;
5859 break;
5861 case WS_READ_REQUIRED_POINTER:
5863 WS_XML_STRING *heap_val;
5864 if (size != sizeof(heap_val)) return E_INVALIDARG;
5865 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5866 *heap_val = val;
5867 *(WS_XML_STRING **)ret = heap_val;
5868 break;
5870 case WS_READ_OPTIONAL_POINTER:
5871 case WS_READ_NILLABLE_POINTER:
5873 WS_XML_STRING *heap_val = NULL;
5874 if (size != sizeof(heap_val)) return E_INVALIDARG;
5875 if (*found)
5877 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5878 *heap_val = val;
5880 *(WS_XML_STRING **)ret = heap_val;
5881 break;
5883 default:
5884 FIXME( "read option %u not supported\n", option );
5885 return E_NOTIMPL;
5888 return S_OK;
5891 static HRESULT text_to_qname( struct reader *reader, const WS_XML_TEXT *text, WS_HEAP *heap, WS_XML_QNAME *val )
5893 HRESULT hr;
5895 switch (text->textType)
5897 case WS_XML_TEXT_TYPE_UTF8:
5899 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
5900 hr = str_to_qname( reader, text_utf8->value.bytes, text_utf8->value.length, heap, NULL,
5901 &val->localName, &val->ns );
5902 break;
5904 case WS_XML_TEXT_TYPE_QNAME:
5906 const WS_XML_QNAME_TEXT *text_qname = (const WS_XML_QNAME_TEXT *)text;
5907 if ((hr = copy_xml_string( heap, text_qname->localName, &val->localName )) != S_OK) return hr;
5908 if ((hr = copy_xml_string( heap, text_qname->ns, &val->ns )) != S_OK)
5910 ws_free( heap, val->localName.bytes, val->localName.length );
5911 return hr;
5913 break;
5915 default:
5916 FIXME( "unhandled text type %u\n", text->textType );
5917 return E_NOTIMPL;
5920 return hr;
5923 static HRESULT read_type_qname( struct reader *reader, WS_TYPE_MAPPING mapping,
5924 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5925 const WS_XML_QNAME_DESCRIPTION *desc, WS_READ_OPTION option,
5926 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5928 const WS_XML_TEXT *text;
5929 WS_XML_QNAME val = {{0}};
5930 HRESULT hr;
5932 if (desc) FIXME( "ignoring description\n" );
5934 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return WS_E_INVALID_FORMAT;
5935 if ((hr = read_startelement( reader )) != S_OK) return hr;
5936 if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT) return WS_E_INVALID_FORMAT;
5938 if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
5939 if (*found && (hr = text_to_qname( reader, text, heap, &val )) != S_OK) return hr;
5941 switch (option)
5943 case WS_READ_REQUIRED_VALUE:
5944 if (!*found) return WS_E_INVALID_FORMAT;
5945 /* fall through */
5947 case WS_READ_NILLABLE_VALUE:
5948 if (size != sizeof(val)) return E_INVALIDARG;
5949 *(WS_XML_QNAME *)ret = val;
5950 break;
5952 case WS_READ_REQUIRED_POINTER:
5953 if (!*found) return WS_E_INVALID_FORMAT;
5954 /* fall through */
5956 case WS_READ_OPTIONAL_POINTER:
5957 case WS_READ_NILLABLE_POINTER:
5959 WS_XML_QNAME *heap_val = NULL;
5960 if (size != sizeof(heap_val)) return E_INVALIDARG;
5961 if (*found)
5963 if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
5964 *heap_val = val;
5966 *(WS_XML_QNAME **)ret = heap_val;
5967 break;
5969 default:
5970 FIXME( "read option %u not supported\n", option );
5971 return E_NOTIMPL;
5974 return S_OK;
5977 static BOOL is_empty_text_node( const struct node *node )
5979 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)node;
5981 if (node_type( node ) != WS_XML_NODE_TYPE_TEXT) return FALSE;
5982 switch (text->text->textType)
5984 case WS_XML_TEXT_TYPE_UTF8:
5986 ULONG i;
5987 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
5988 for (i = 0; i < utf8->value.length; i++) if (!read_isspace( utf8->value.bytes[i] )) return FALSE;
5989 return TRUE;
5991 case WS_XML_TEXT_TYPE_BASE64:
5993 const WS_XML_BASE64_TEXT *base64 = (const WS_XML_BASE64_TEXT *)text->text;
5994 return !base64->length;
5996 case WS_XML_TEXT_TYPE_BOOL:
5997 case WS_XML_TEXT_TYPE_INT32:
5998 case WS_XML_TEXT_TYPE_INT64:
5999 case WS_XML_TEXT_TYPE_UINT64:
6000 case WS_XML_TEXT_TYPE_FLOAT:
6001 case WS_XML_TEXT_TYPE_DOUBLE:
6002 case WS_XML_TEXT_TYPE_DECIMAL:
6003 case WS_XML_TEXT_TYPE_GUID:
6004 case WS_XML_TEXT_TYPE_UNIQUE_ID:
6005 case WS_XML_TEXT_TYPE_DATETIME:
6006 return FALSE;
6008 default:
6009 ERR( "unhandled text type %u\n", text->text->textType );
6010 return FALSE;
6014 /* skips comment and empty text nodes */
6015 static HRESULT read_type_next_node( struct reader *reader )
6017 for (;;)
6019 HRESULT hr;
6020 WS_XML_NODE_TYPE type;
6022 if ((hr = read_next_node( reader )) != S_OK) return hr;
6023 type = node_type( reader->current );
6024 if (type == WS_XML_NODE_TYPE_COMMENT ||
6025 (type == WS_XML_NODE_TYPE_TEXT && is_empty_text_node( reader->current ))) continue;
6026 return S_OK;
6030 static HRESULT read_type_next_element_node( struct reader *reader, const WS_XML_STRING *localname,
6031 const WS_XML_STRING *ns )
6033 struct reader_pos pos;
6034 HRESULT hr;
6036 if (!localname) return S_OK; /* assume reader is already correctly positioned */
6037 if (reader->current == reader->last)
6039 BOOL found;
6040 if ((hr = read_to_startelement( reader, &found )) != S_OK) return hr;
6041 if (!found) return WS_E_INVALID_FORMAT;
6043 if (match_element( reader->current, localname, ns )) return S_OK;
6045 save_reader_position( reader, &pos );
6046 if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6047 if (match_element( reader->current, localname, ns )) return S_OK;
6048 restore_reader_position( reader, &pos );
6050 return WS_E_INVALID_FORMAT;
6053 ULONG get_type_size( WS_TYPE type, const void *desc )
6055 switch (type)
6057 case WS_INT8_TYPE:
6058 case WS_UINT8_TYPE:
6059 return sizeof(INT8);
6061 case WS_INT16_TYPE:
6062 case WS_UINT16_TYPE:
6063 return sizeof(INT16);
6065 case WS_BOOL_TYPE:
6066 case WS_INT32_TYPE:
6067 case WS_UINT32_TYPE:
6068 case WS_ENUM_TYPE:
6069 return sizeof(INT32);
6071 case WS_INT64_TYPE:
6072 case WS_UINT64_TYPE:
6073 return sizeof(INT64);
6075 case WS_FLOAT_TYPE:
6076 return sizeof(float);
6078 case WS_DOUBLE_TYPE:
6079 return sizeof(double);
6081 case WS_DATETIME_TYPE:
6082 return sizeof(WS_DATETIME);
6084 case WS_GUID_TYPE:
6085 return sizeof(GUID);
6087 case WS_UNIQUE_ID_TYPE:
6088 return sizeof(WS_UNIQUE_ID);
6090 case WS_STRING_TYPE:
6091 return sizeof(WS_STRING);
6093 case WS_WSZ_TYPE:
6094 return sizeof(WCHAR *);
6096 case WS_BYTES_TYPE:
6097 return sizeof(WS_BYTES);
6099 case WS_XML_STRING_TYPE:
6100 return sizeof(WS_XML_STRING);
6102 case WS_XML_QNAME_TYPE:
6103 return sizeof(WS_XML_QNAME);
6105 case WS_DESCRIPTION_TYPE:
6106 return sizeof(WS_STRUCT_DESCRIPTION *);
6108 case WS_STRUCT_TYPE:
6110 const WS_STRUCT_DESCRIPTION *desc_struct = desc;
6111 return desc_struct->size;
6113 case WS_UNION_TYPE:
6115 const WS_UNION_DESCRIPTION *desc_union = desc;
6116 return desc_union->size;
6118 default:
6119 ERR( "unhandled type %u\n", type );
6120 return 0;
6124 static WS_READ_OPTION get_field_read_option( WS_TYPE type, ULONG options )
6126 if (options & WS_FIELD_POINTER)
6128 if (options & WS_FIELD_NILLABLE) return WS_READ_NILLABLE_POINTER;
6129 if (options & WS_FIELD_OPTIONAL) return WS_READ_OPTIONAL_POINTER;
6130 return WS_READ_REQUIRED_POINTER;
6133 switch (type)
6135 case WS_BOOL_TYPE:
6136 case WS_INT8_TYPE:
6137 case WS_INT16_TYPE:
6138 case WS_INT32_TYPE:
6139 case WS_INT64_TYPE:
6140 case WS_UINT8_TYPE:
6141 case WS_UINT16_TYPE:
6142 case WS_UINT32_TYPE:
6143 case WS_UINT64_TYPE:
6144 case WS_FLOAT_TYPE:
6145 case WS_DOUBLE_TYPE:
6146 case WS_DATETIME_TYPE:
6147 case WS_GUID_TYPE:
6148 case WS_UNIQUE_ID_TYPE:
6149 case WS_STRING_TYPE:
6150 case WS_BYTES_TYPE:
6151 case WS_XML_STRING_TYPE:
6152 case WS_XML_QNAME_TYPE:
6153 case WS_STRUCT_TYPE:
6154 case WS_ENUM_TYPE:
6155 case WS_UNION_TYPE:
6156 if (options & (WS_FIELD_OPTIONAL|WS_FIELD_NILLABLE)) return WS_READ_NILLABLE_VALUE;
6157 return WS_READ_REQUIRED_VALUE;
6159 case WS_WSZ_TYPE:
6160 case WS_DESCRIPTION_TYPE:
6161 if (options & WS_FIELD_NILLABLE) return WS_READ_NILLABLE_POINTER;
6162 if (options & WS_FIELD_OPTIONAL) return WS_READ_OPTIONAL_POINTER;
6163 return WS_READ_REQUIRED_POINTER;
6165 default:
6166 FIXME( "unhandled type %u\n", type );
6167 return 0;
6171 static HRESULT read_type_field( struct reader *, const WS_FIELD_DESCRIPTION *, WS_HEAP *, char *, ULONG );
6173 static HRESULT read_type_union( struct reader *reader, const WS_UNION_DESCRIPTION *desc, WS_HEAP *heap, void *ret,
6174 ULONG size, BOOL *found )
6176 struct reader_pos pos;
6177 HRESULT hr;
6178 ULONG i;
6180 if (size != desc->size) return E_INVALIDARG;
6182 save_reader_position( reader, &pos );
6183 if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6185 for (i = 0; i < desc->fieldCount; i++)
6187 if ((*found = match_element( reader->current, desc->fields[i]->field.localName, desc->fields[i]->field.ns )))
6188 break;
6191 if (!*found)
6193 *(int *)((char *)ret + desc->enumOffset) = desc->noneEnumValue;
6194 restore_reader_position( reader, &pos );
6196 else
6198 ULONG offset = desc->fields[i]->field.offset;
6199 if ((hr = read_type_field( reader, &desc->fields[i]->field, heap, ret, offset )) != S_OK) return hr;
6200 *(int *)((char *)ret + desc->enumOffset) = desc->fields[i]->value;
6203 return S_OK;
6206 static HRESULT read_type( struct reader *, WS_TYPE_MAPPING, WS_TYPE, const WS_XML_STRING *,
6207 const WS_XML_STRING *, const void *, WS_READ_OPTION, WS_HEAP *,
6208 void *, ULONG, BOOL * );
6210 static HRESULT read_type_array( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap,
6211 void **ret, ULONG *count )
6213 HRESULT hr;
6214 ULONG item_size, nb_items = 0, nb_allocated = 1, offset = 0;
6215 WS_READ_OPTION option;
6216 BOOL found;
6217 char *buf;
6219 if (!(option = get_field_read_option( desc->type, desc->options ))) return E_INVALIDARG;
6221 /* wrapper element */
6222 if (desc->localName && ((hr = read_type_next_element_node( reader, desc->localName, desc->ns )) != S_OK))
6223 return hr;
6225 if (option == WS_READ_REQUIRED_VALUE || option == WS_READ_NILLABLE_VALUE)
6226 item_size = get_type_size( desc->type, desc->typeDescription );
6227 else
6228 item_size = sizeof(void *);
6230 if (!(buf = ws_alloc_zero( heap, item_size ))) return WS_E_QUOTA_EXCEEDED;
6231 for (;;)
6233 if (nb_items >= nb_allocated)
6235 SIZE_T old_size = nb_allocated * item_size, new_size = old_size * 2;
6236 if (!(buf = ws_realloc_zero( heap, buf, old_size, new_size ))) return WS_E_QUOTA_EXCEEDED;
6237 nb_allocated *= 2;
6240 if (desc->type == WS_UNION_TYPE)
6242 hr = read_type_union( reader, desc->typeDescription, heap, buf + offset, item_size, &found );
6243 if (hr != S_OK)
6245 ws_free( heap, buf, nb_allocated * item_size );
6246 return hr;
6248 if (!found) break;
6250 else
6252 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->itemLocalName, desc->itemNs,
6253 desc->typeDescription, option, heap, buf + offset, item_size, &found );
6254 if (hr == WS_E_INVALID_FORMAT) break;
6255 if (hr != S_OK)
6257 ws_free( heap, buf, nb_allocated * item_size );
6258 return hr;
6262 offset += item_size;
6263 nb_items++;
6266 if (desc->localName && ((hr = read_type_next_node( reader )) != S_OK)) return hr;
6268 if (desc->itemRange && (nb_items < desc->itemRange->minItemCount || nb_items > desc->itemRange->maxItemCount))
6270 TRACE( "number of items %u out of range (%u-%u)\n", nb_items, desc->itemRange->minItemCount,
6271 desc->itemRange->maxItemCount );
6272 ws_free( heap, buf, nb_allocated * item_size );
6273 return WS_E_INVALID_FORMAT;
6276 *count = nb_items;
6277 *ret = buf;
6279 return S_OK;
6282 static HRESULT read_type_text( struct reader *reader, const WS_FIELD_DESCRIPTION *desc,
6283 WS_READ_OPTION option, WS_HEAP *heap, void *ret, ULONG size )
6285 struct reader_pos pos;
6286 BOOL found;
6287 HRESULT hr;
6289 if (reader->current == reader->last)
6291 if ((hr = read_to_startelement( reader, &found )) != S_OK) return hr;
6292 if (!found) return WS_E_INVALID_FORMAT;
6295 save_reader_position( reader, &pos );
6296 if ((hr = read_next_node( reader )) != S_OK) return hr;
6297 hr = read_type( reader, WS_ANY_ELEMENT_TYPE_MAPPING, desc->type, NULL, NULL,
6298 desc->typeDescription, option, heap, ret, size, &found );
6299 if (hr == S_OK && !found) restore_reader_position( reader, &pos );
6300 return hr;
6303 static HRESULT read_type_field( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap, char *buf,
6304 ULONG offset )
6306 char *ptr;
6307 WS_READ_OPTION option;
6308 ULONG size;
6309 HRESULT hr;
6310 BOOL found;
6312 if (!desc) return E_INVALIDARG;
6313 if (desc->options & ~(WS_FIELD_POINTER|WS_FIELD_OPTIONAL|WS_FIELD_NILLABLE|WS_FIELD_NILLABLE_ITEM))
6315 FIXME( "options %08x not supported\n", desc->options );
6316 return E_NOTIMPL;
6318 if (!(option = get_field_read_option( desc->type, desc->options ))) return E_INVALIDARG;
6320 if (option == WS_READ_REQUIRED_VALUE || option == WS_READ_NILLABLE_VALUE)
6321 size = get_type_size( desc->type, desc->typeDescription );
6322 else
6323 size = sizeof(void *);
6325 ptr = buf + offset;
6326 switch (desc->mapping)
6328 case WS_TYPE_ATTRIBUTE_FIELD_MAPPING:
6329 FIXME( "WS_TYPE_ATTRIBUTE_FIELD_MAPPING not supported\n" );
6330 return S_OK;
6332 case WS_ATTRIBUTE_FIELD_MAPPING:
6333 hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6334 desc->typeDescription, option, heap, ptr, size, &found );
6335 break;
6337 case WS_ELEMENT_FIELD_MAPPING:
6338 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6339 desc->typeDescription, option, heap, ptr, size, &found );
6340 break;
6342 case WS_ELEMENT_CHOICE_FIELD_MAPPING:
6344 if (desc->type != WS_UNION_TYPE || !desc->typeDescription ||
6345 (desc->options & (WS_FIELD_POINTER|WS_FIELD_NILLABLE))) return E_INVALIDARG;
6346 hr = read_type_union( reader, desc->typeDescription, heap, ptr, size, &found );
6347 break;
6349 case WS_REPEATING_ELEMENT_FIELD_MAPPING:
6350 case WS_REPEATING_ELEMENT_CHOICE_FIELD_MAPPING:
6352 ULONG count;
6353 hr = read_type_array( reader, desc, heap, (void **)ptr, &count );
6354 if (hr == S_OK) *(ULONG *)(buf + desc->countOffset) = count;
6355 break;
6357 case WS_TEXT_FIELD_MAPPING:
6358 hr = read_type_text( reader, desc, option, heap, ptr, size );
6359 break;
6361 default:
6362 FIXME( "unhandled field mapping %u\n", desc->mapping );
6363 return E_NOTIMPL;
6366 if (hr == WS_E_INVALID_FORMAT)
6368 switch (option)
6370 case WS_READ_REQUIRED_VALUE:
6371 case WS_READ_REQUIRED_POINTER:
6372 return WS_E_INVALID_FORMAT;
6374 case WS_READ_NILLABLE_VALUE:
6375 if (desc->defaultValue) memcpy( ptr, desc->defaultValue->value, desc->defaultValue->valueSize );
6376 return S_OK;
6378 case WS_READ_OPTIONAL_POINTER:
6379 case WS_READ_NILLABLE_POINTER:
6380 *(void **)ptr = NULL;
6381 return S_OK;
6383 default:
6384 ERR( "unhandled option %u\n", option );
6385 return E_NOTIMPL;
6389 return hr;
6392 static HRESULT read_type_struct( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
6393 const WS_XML_STRING *ns, const WS_STRUCT_DESCRIPTION *desc, WS_READ_OPTION option,
6394 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
6396 ULONG i, offset;
6397 HRESULT hr;
6398 char *buf;
6400 if (!desc) return E_INVALIDARG;
6401 if (desc->structOptions & ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
6403 FIXME( "struct options %08x not supported\n",
6404 desc->structOptions & ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT );
6407 switch (option)
6409 case WS_READ_REQUIRED_POINTER:
6410 case WS_READ_OPTIONAL_POINTER:
6411 case WS_READ_NILLABLE_POINTER:
6412 if (size != sizeof(void *)) return E_INVALIDARG;
6413 if (!(buf = ws_alloc_zero( heap, desc->size ))) return WS_E_QUOTA_EXCEEDED;
6414 break;
6416 case WS_READ_REQUIRED_VALUE:
6417 case WS_READ_NILLABLE_VALUE:
6418 if (size != desc->size) return E_INVALIDARG;
6419 buf = ret;
6420 break;
6422 default:
6423 FIXME( "unhandled read option %u\n", option );
6424 return E_NOTIMPL;
6427 for (i = 0; i < desc->fieldCount; i++)
6429 offset = desc->fields[i]->offset;
6430 if ((hr = read_type_field( reader, desc->fields[i], heap, buf, offset )) != S_OK) break;
6433 switch (option)
6435 case WS_READ_REQUIRED_POINTER:
6436 if (hr != S_OK)
6438 ws_free( heap, buf, desc->size );
6439 return hr;
6441 *(char **)ret = buf;
6442 break;
6444 case WS_READ_OPTIONAL_POINTER:
6445 case WS_READ_NILLABLE_POINTER:
6446 if (is_nil_value( buf, desc->size ))
6448 ws_free( heap, buf, desc->size );
6449 buf = NULL;
6451 *(char **)ret = buf;
6452 break;
6454 case WS_READ_REQUIRED_VALUE:
6455 case WS_READ_NILLABLE_VALUE:
6456 if (hr != S_OK) return hr;
6457 break;
6459 default:
6460 ERR( "unhandled read option %u\n", option );
6461 return E_NOTIMPL;
6464 if (desc->structOptions & WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
6466 struct node *parent = find_parent( reader );
6467 parent->flags |= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT;
6470 *found = TRUE;
6471 return S_OK;
6474 static HRESULT start_mapping( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
6475 const WS_XML_STRING *ns )
6477 switch (mapping)
6479 case WS_ELEMENT_TYPE_MAPPING:
6480 case WS_ELEMENT_CONTENT_TYPE_MAPPING:
6481 return read_type_next_element_node( reader, localname, ns );
6483 case WS_ANY_ELEMENT_TYPE_MAPPING:
6484 case WS_ATTRIBUTE_TYPE_MAPPING:
6485 return S_OK;
6487 default:
6488 FIXME( "unhandled mapping %u\n", mapping );
6489 return E_NOTIMPL;
6493 static HRESULT read_type_endelement_node( struct reader *reader )
6495 const struct node *parent = find_parent( reader );
6496 HRESULT hr;
6498 for (;;)
6500 if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6501 if (node_type( reader->current ) == WS_XML_NODE_TYPE_END_ELEMENT && reader->current->parent == parent)
6503 return S_OK;
6505 if (read_end_of_data( reader ) || !(parent->flags & NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT)) break;
6508 return WS_E_INVALID_FORMAT;
6511 static HRESULT end_mapping( struct reader *reader, WS_TYPE_MAPPING mapping )
6513 switch (mapping)
6515 case WS_ELEMENT_TYPE_MAPPING:
6516 return read_type_endelement_node( reader );
6518 case WS_ELEMENT_CONTENT_TYPE_MAPPING:
6519 return read_type_next_node( reader );
6521 case WS_ATTRIBUTE_TYPE_MAPPING:
6522 default:
6523 return S_OK;
6527 static BOOL is_true_text( const WS_XML_TEXT *text )
6529 switch (text->textType)
6531 case WS_XML_TEXT_TYPE_UTF8:
6533 const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
6534 if (text_utf8->value.length == 4 && !memcmp( text_utf8->value.bytes, "true", 4 )) return TRUE;
6535 return FALSE;
6537 case WS_XML_TEXT_TYPE_BOOL:
6539 const WS_XML_BOOL_TEXT *text_bool = (const WS_XML_BOOL_TEXT *)text;
6540 return text_bool->value;
6542 default:
6543 ERR( "unhandled text type %u\n", text->textType );
6544 return FALSE;
6548 static HRESULT is_nil_element( const WS_XML_ELEMENT_NODE *elem )
6550 static const WS_XML_STRING localname = {3, (BYTE *)"nil"};
6551 static const WS_XML_STRING ns = {41, (BYTE *)"http://www.w3.org/2001/XMLSchema-instance"};
6552 ULONG i;
6554 for (i = 0; i < elem->attributeCount; i++)
6556 if (elem->attributes[i]->isXmlNs) continue;
6557 if (WsXmlStringEquals( elem->attributes[i]->localName, &localname, NULL ) == S_OK &&
6558 WsXmlStringEquals( elem->attributes[i]->ns, &ns, NULL ) == S_OK &&
6559 is_true_text( elem->attributes[i]->value )) return TRUE;
6561 return FALSE;
6564 static HRESULT read_type( struct reader *reader, WS_TYPE_MAPPING mapping, WS_TYPE type,
6565 const WS_XML_STRING *localname, const WS_XML_STRING *ns, const void *desc,
6566 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size, BOOL *found )
6568 HRESULT hr;
6570 if ((hr = start_mapping( reader, mapping, localname, ns )) != S_OK) return hr;
6572 if (mapping == WS_ELEMENT_TYPE_MAPPING && is_nil_element( &reader->current->hdr ))
6574 if (option != WS_READ_NILLABLE_POINTER && option != WS_READ_NILLABLE_VALUE) return WS_E_INVALID_FORMAT;
6575 *found = TRUE;
6576 return end_mapping( reader, mapping );
6579 switch (type)
6581 case WS_BOOL_TYPE:
6582 hr = read_type_bool( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6583 break;
6585 case WS_INT8_TYPE:
6586 hr = read_type_int8( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6587 break;
6589 case WS_INT16_TYPE:
6590 hr = read_type_int16( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6591 break;
6593 case WS_INT32_TYPE:
6594 hr = read_type_int32( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6595 break;
6597 case WS_INT64_TYPE:
6598 hr = read_type_int64( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6599 break;
6601 case WS_UINT8_TYPE:
6602 hr = read_type_uint8( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6603 break;
6605 case WS_UINT16_TYPE:
6606 hr = read_type_uint16( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6607 break;
6609 case WS_UINT32_TYPE:
6610 hr = read_type_uint32( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6611 break;
6613 case WS_UINT64_TYPE:
6614 hr = read_type_uint64( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6615 break;
6617 case WS_FLOAT_TYPE:
6618 hr = read_type_float( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6619 break;
6621 case WS_DOUBLE_TYPE:
6622 hr = read_type_double( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6623 break;
6625 case WS_DATETIME_TYPE:
6626 hr = read_type_datetime( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6627 break;
6629 case WS_GUID_TYPE:
6630 hr = read_type_guid( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6631 break;
6633 case WS_UNIQUE_ID_TYPE:
6634 hr = read_type_unique_id( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6635 break;
6637 case WS_STRING_TYPE:
6638 hr = read_type_string( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6639 break;
6641 case WS_WSZ_TYPE:
6642 hr = read_type_wsz( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6643 break;
6645 case WS_BYTES_TYPE:
6646 hr = read_type_bytes( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6647 break;
6649 case WS_XML_STRING_TYPE:
6650 hr = read_type_xml_string( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6651 break;
6653 case WS_XML_QNAME_TYPE:
6654 hr = read_type_qname( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6655 break;
6657 case WS_STRUCT_TYPE:
6658 hr = read_type_struct( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6659 break;
6661 case WS_ENUM_TYPE:
6662 hr = read_type_enum( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6663 break;
6665 default:
6666 FIXME( "type %u not supported\n", type );
6667 return E_NOTIMPL;
6670 if (hr != S_OK) return hr;
6671 return end_mapping( reader, mapping );
6674 /**************************************************************************
6675 * WsReadType [webservices.@]
6677 HRESULT WINAPI WsReadType( WS_XML_READER *handle, WS_TYPE_MAPPING mapping, WS_TYPE type,
6678 const void *desc, WS_READ_OPTION option, WS_HEAP *heap, void *value,
6679 ULONG size, WS_ERROR *error )
6681 struct reader *reader = (struct reader *)handle;
6682 BOOL found;
6683 HRESULT hr;
6685 TRACE( "%p %u %u %p %u %p %p %u %p\n", handle, mapping, type, desc, option, heap, value,
6686 size, error );
6687 if (error) FIXME( "ignoring error parameter\n" );
6689 if (!reader || !value) return E_INVALIDARG;
6691 EnterCriticalSection( &reader->cs );
6693 if (reader->magic != READER_MAGIC)
6695 LeaveCriticalSection( &reader->cs );
6696 return E_INVALIDARG;
6699 if ((hr = read_type( reader, mapping, type, NULL, NULL, desc, option, heap, value, size, &found )) != S_OK)
6701 LeaveCriticalSection( &reader->cs );
6702 return hr;
6705 switch (mapping)
6707 case WS_ELEMENT_TYPE_MAPPING:
6708 hr = read_node( reader );
6709 break;
6711 default:
6712 break;
6715 if (hr == S_OK && !read_end_of_data( reader )) hr = WS_E_INVALID_FORMAT;
6717 LeaveCriticalSection( &reader->cs );
6718 return hr;
6721 HRESULT read_header( WS_XML_READER *handle, const WS_XML_STRING *localname, const WS_XML_STRING *ns,
6722 WS_TYPE type, const void *desc, WS_READ_OPTION option, WS_HEAP *heap, void *value,
6723 ULONG size )
6725 struct reader *reader = (struct reader *)handle;
6726 BOOL found;
6727 HRESULT hr;
6729 EnterCriticalSection( &reader->cs );
6731 if (reader->magic != READER_MAGIC)
6733 LeaveCriticalSection( &reader->cs );
6734 return E_INVALIDARG;
6737 hr = read_type( reader, WS_ELEMENT_CONTENT_TYPE_MAPPING, type, localname, ns, desc, option, heap,
6738 value, size, &found );
6740 LeaveCriticalSection( &reader->cs );
6741 return hr;
6744 /**************************************************************************
6745 * WsReadElement [webservices.@]
6747 HRESULT WINAPI WsReadElement( WS_XML_READER *handle, const WS_ELEMENT_DESCRIPTION *desc,
6748 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size,
6749 WS_ERROR *error )
6751 struct reader *reader = (struct reader *)handle;
6752 BOOL found;
6753 HRESULT hr;
6755 TRACE( "%p %p %u %p %p %u %p\n", handle, desc, option, heap, value, size, error );
6756 if (error) FIXME( "ignoring error parameter\n" );
6758 if (!reader || !desc || !value) return E_INVALIDARG;
6760 EnterCriticalSection( &reader->cs );
6762 if (reader->magic != READER_MAGIC)
6764 LeaveCriticalSection( &reader->cs );
6765 return E_INVALIDARG;
6768 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->elementLocalName,
6769 desc->elementNs, desc->typeDescription, option, heap, value, size, &found );
6771 LeaveCriticalSection( &reader->cs );
6772 return hr;
6775 /**************************************************************************
6776 * WsReadValue [webservices.@]
6778 HRESULT WINAPI WsReadValue( WS_XML_READER *handle, WS_VALUE_TYPE value_type, void *value, ULONG size,
6779 WS_ERROR *error )
6781 struct reader *reader = (struct reader *)handle;
6782 WS_TYPE type = map_value_type( value_type );
6783 BOOL found;
6784 HRESULT hr;
6786 TRACE( "%p %u %p %u %p\n", handle, type, value, size, error );
6787 if (error) FIXME( "ignoring error parameter\n" );
6789 if (!reader || !value || type == ~0u) return E_INVALIDARG;
6791 EnterCriticalSection( &reader->cs );
6793 if (reader->magic != READER_MAGIC)
6795 LeaveCriticalSection( &reader->cs );
6796 return E_INVALIDARG;
6799 hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, type, NULL, NULL, NULL, WS_READ_REQUIRED_VALUE,
6800 NULL, value, size, &found );
6802 LeaveCriticalSection( &reader->cs );
6803 return hr;
6806 /**************************************************************************
6807 * WsReadAttribute [webservices.@]
6809 HRESULT WINAPI WsReadAttribute( WS_XML_READER *handle, const WS_ATTRIBUTE_DESCRIPTION *desc,
6810 WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size,
6811 WS_ERROR *error )
6813 struct reader *reader = (struct reader *)handle;
6814 BOOL found;
6815 HRESULT hr;
6817 TRACE( "%p %p %u %p %p %u %p\n", handle, desc, option, heap, value, size, error );
6818 if (error) FIXME( "ignoring error parameter\n" );
6820 if (!reader || !desc || !value) return E_INVALIDARG;
6822 EnterCriticalSection( &reader->cs );
6824 if (reader->magic != READER_MAGIC)
6826 LeaveCriticalSection( &reader->cs );
6827 return E_INVALIDARG;
6830 if (!reader->input_type)
6832 LeaveCriticalSection( &reader->cs );
6833 return WS_E_INVALID_OPERATION;
6836 hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->attributeLocalName,
6837 desc->attributeNs, desc->typeDescription, option, heap, value, size, &found );
6839 LeaveCriticalSection( &reader->cs );
6840 return hr;
6843 static inline BOOL is_utf8( const unsigned char *data, ULONG size, ULONG *offset )
6845 static const char bom[] = {0xef,0xbb,0xbf};
6846 const unsigned char *p = data;
6848 return (size >= sizeof(bom) && !memcmp( p, bom, sizeof(bom) ) && (*offset = sizeof(bom))) ||
6849 (size > 2 && !(*offset = 0));
6852 static inline BOOL is_utf16le( const unsigned char *data, ULONG size, ULONG *offset )
6854 static const char bom[] = {0xff,0xfe};
6855 const unsigned char *p = data;
6857 return (size >= sizeof(bom) && !memcmp( p, bom, sizeof(bom) ) && (*offset = sizeof(bom))) ||
6858 (size >= 4 && p[0] == '<' && !p[1] && !(*offset = 0));
6861 static WS_CHARSET detect_charset( const unsigned char *data, ULONG size, ULONG *offset )
6863 WS_CHARSET ret = 0;
6865 /* FIXME: parse xml declaration */
6867 if (is_utf16le( data, size, offset )) ret = WS_CHARSET_UTF16LE;
6868 else if (is_utf8( data, size, offset )) ret = WS_CHARSET_UTF8;
6869 else
6871 FIXME( "charset not recognized\n" );
6872 return 0;
6875 TRACE( "detected charset %u\n", ret );
6876 return ret;
6879 static void set_input_buffer( struct reader *reader, struct xmlbuf *buf, const unsigned char *data, ULONG size )
6881 reader->input_type = WS_XML_READER_INPUT_TYPE_BUFFER;
6882 reader->input_buf = buf;
6883 reader->input_data = data;
6884 reader->input_size = size;
6886 reader->read_size = reader->input_size;
6887 reader->read_pos = 0;
6888 reader->read_bufptr = reader->input_data;
6890 reader->text_conv_offset = 0;
6893 /**************************************************************************
6894 * WsSetInput [webservices.@]
6896 HRESULT WINAPI WsSetInput( WS_XML_READER *handle, const WS_XML_READER_ENCODING *encoding,
6897 const WS_XML_READER_INPUT *input, const WS_XML_READER_PROPERTY *properties,
6898 ULONG count, WS_ERROR *error )
6900 struct reader *reader = (struct reader *)handle;
6901 struct node *node;
6902 ULONG i, offset = 0;
6903 HRESULT hr;
6905 TRACE( "%p %p %p %p %u %p\n", handle, encoding, input, properties, count, error );
6906 if (error) FIXME( "ignoring error parameter\n" );
6908 if (!reader) return E_INVALIDARG;
6910 EnterCriticalSection( &reader->cs );
6912 if (reader->magic != READER_MAGIC)
6914 LeaveCriticalSection( &reader->cs );
6915 return E_INVALIDARG;
6918 for (i = 0; i < count; i++)
6920 hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
6921 properties[i].valueSize );
6922 if (hr != S_OK) goto done;
6925 if ((hr = init_reader( reader )) != S_OK) goto done;
6927 switch (encoding->encodingType)
6929 case WS_XML_READER_ENCODING_TYPE_TEXT:
6931 WS_XML_READER_TEXT_ENCODING *text = (WS_XML_READER_TEXT_ENCODING *)encoding;
6932 WS_XML_READER_BUFFER_INPUT *buf = (WS_XML_READER_BUFFER_INPUT *)input;
6934 if (input->inputType != WS_XML_READER_INPUT_TYPE_BUFFER)
6936 FIXME( "charset detection on input type %u not supported\n", input->inputType );
6937 hr = E_NOTIMPL;
6938 goto done;
6941 if (text->charSet != WS_CHARSET_AUTO) reader->input_charset = text->charSet;
6942 else reader->input_charset = detect_charset( buf->encodedData, buf->encodedDataSize, &offset );
6944 reader->input_enc = WS_XML_READER_ENCODING_TYPE_TEXT;
6945 break;
6947 case WS_XML_READER_ENCODING_TYPE_BINARY:
6949 WS_XML_READER_BINARY_ENCODING *bin = (WS_XML_READER_BINARY_ENCODING *)encoding;
6950 reader->input_enc = WS_XML_READER_ENCODING_TYPE_BINARY;
6951 reader->input_charset = 0;
6952 reader->dict_static = bin->staticDictionary ? bin->staticDictionary : &dict_builtin_static.dict;
6953 reader->dict = bin->dynamicDictionary ? bin->dynamicDictionary : &dict_builtin.dict;
6954 break;
6956 default:
6957 FIXME( "encoding type %u not supported\n", encoding->encodingType );
6958 hr = E_NOTIMPL;
6959 goto done;
6962 switch (input->inputType)
6964 case WS_XML_READER_INPUT_TYPE_BUFFER:
6966 WS_XML_READER_BUFFER_INPUT *buf = (WS_XML_READER_BUFFER_INPUT *)input;
6967 set_input_buffer( reader, NULL, (const unsigned char *)buf->encodedData + offset,
6968 buf->encodedDataSize - offset );
6969 break;
6971 default:
6972 FIXME( "input type %u not supported\n", input->inputType );
6973 hr = E_NOTIMPL;
6974 goto done;
6977 if (!(node = alloc_node( WS_XML_NODE_TYPE_BOF ))) hr = E_OUTOFMEMORY;
6978 else read_insert_bof( reader, node );
6980 done:
6981 LeaveCriticalSection( &reader->cs );
6982 return hr;
6985 /**************************************************************************
6986 * WsSetInputToBuffer [webservices.@]
6988 HRESULT WINAPI WsSetInputToBuffer( WS_XML_READER *handle, WS_XML_BUFFER *buffer,
6989 const WS_XML_READER_PROPERTY *properties, ULONG count,
6990 WS_ERROR *error )
6992 struct reader *reader = (struct reader *)handle;
6993 struct xmlbuf *xmlbuf = (struct xmlbuf *)buffer;
6994 struct node *node;
6995 HRESULT hr;
6996 ULONG i;
6998 TRACE( "%p %p %p %u %p\n", handle, buffer, properties, count, error );
6999 if (error) FIXME( "ignoring error parameter\n" );
7001 if (!reader || !xmlbuf) return E_INVALIDARG;
7003 EnterCriticalSection( &reader->cs );
7005 if (reader->magic != READER_MAGIC)
7007 LeaveCriticalSection( &reader->cs );
7008 return E_INVALIDARG;
7011 for (i = 0; i < count; i++)
7013 hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
7014 properties[i].valueSize );
7015 if (hr != S_OK) goto done;
7018 if ((hr = init_reader( reader )) != S_OK) goto done;
7020 reader->input_enc = xmlbuf->encoding;
7021 reader->input_charset = xmlbuf->charset;
7022 reader->dict_static = xmlbuf->dict_static;
7023 reader->dict = xmlbuf->dict;
7024 set_input_buffer( reader, xmlbuf, xmlbuf->bytes.bytes, xmlbuf->bytes.length );
7026 if (!(node = alloc_node( WS_XML_NODE_TYPE_BOF ))) hr = E_OUTOFMEMORY;
7027 else read_insert_bof( reader, node );
7029 done:
7030 LeaveCriticalSection( &reader->cs );
7031 return hr;
7034 /**************************************************************************
7035 * WsGetReaderPosition [webservices.@]
7037 HRESULT WINAPI WsGetReaderPosition( WS_XML_READER *handle, WS_XML_NODE_POSITION *pos, WS_ERROR *error )
7039 struct reader *reader = (struct reader *)handle;
7041 TRACE( "%p %p %p\n", handle, pos, error );
7042 if (error) FIXME( "ignoring error parameter\n" );
7044 if (!reader || !pos) return E_INVALIDARG;
7046 EnterCriticalSection( &reader->cs );
7048 if (reader->magic != READER_MAGIC)
7050 LeaveCriticalSection( &reader->cs );
7051 return E_INVALIDARG;
7054 if (!reader->input_buf)
7056 LeaveCriticalSection( &reader->cs );
7057 return WS_E_INVALID_OPERATION;
7060 pos->buffer = (WS_XML_BUFFER *)reader->input_buf;
7061 pos->node = reader->current;
7063 LeaveCriticalSection( &reader->cs );
7064 return S_OK;
7067 /**************************************************************************
7068 * WsSetReaderPosition [webservices.@]
7070 HRESULT WINAPI WsSetReaderPosition( WS_XML_READER *handle, const WS_XML_NODE_POSITION *pos, WS_ERROR *error )
7072 struct reader *reader = (struct reader *)handle;
7074 TRACE( "%p %p %p\n", handle, pos, error );
7075 if (error) FIXME( "ignoring error parameter\n" );
7077 if (!reader || !pos) return E_INVALIDARG;
7079 EnterCriticalSection( &reader->cs );
7081 if (reader->magic != READER_MAGIC || (struct xmlbuf *)pos->buffer != reader->input_buf)
7083 LeaveCriticalSection( &reader->cs );
7084 return E_INVALIDARG;
7087 if (!reader->input_buf)
7089 LeaveCriticalSection( &reader->cs );
7090 return WS_E_INVALID_OPERATION;
7093 reader->current = pos->node;
7095 LeaveCriticalSection( &reader->cs );
7096 return S_OK;
7099 static HRESULT utf8_to_base64( const WS_XML_UTF8_TEXT *utf8, WS_XML_BASE64_TEXT *base64 )
7101 if (utf8->value.length % 4) return WS_E_INVALID_FORMAT;
7102 if (!(base64->bytes = heap_alloc( utf8->value.length * 3 / 4 ))) return E_OUTOFMEMORY;
7103 base64->length = decode_base64( utf8->value.bytes, utf8->value.length, base64->bytes );
7104 return S_OK;
7107 /**************************************************************************
7108 * WsReadBytes [webservices.@]
7110 HRESULT WINAPI WsReadBytes( WS_XML_READER *handle, void *bytes, ULONG max_count, ULONG *count, WS_ERROR *error )
7112 struct reader *reader = (struct reader *)handle;
7113 HRESULT hr;
7115 TRACE( "%p %p %u %p %p\n", handle, bytes, max_count, count, error );
7116 if (error) FIXME( "ignoring error parameter\n" );
7118 if (!reader) return E_INVALIDARG;
7120 EnterCriticalSection( &reader->cs );
7122 if (reader->magic != READER_MAGIC)
7124 LeaveCriticalSection( &reader->cs );
7125 return E_INVALIDARG;
7128 if (!reader->input_type)
7130 LeaveCriticalSection( &reader->cs );
7131 return WS_E_INVALID_OPERATION;
7134 if (!count)
7136 LeaveCriticalSection( &reader->cs );
7137 return E_INVALIDARG;
7140 *count = 0;
7141 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && bytes)
7143 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
7144 WS_XML_BASE64_TEXT base64;
7146 if ((hr = utf8_to_base64( (const WS_XML_UTF8_TEXT *)text->text, &base64 )) != S_OK)
7148 LeaveCriticalSection( &reader->cs );
7149 return hr;
7151 if (reader->text_conv_offset == base64.length)
7153 heap_free( base64.bytes );
7154 hr = read_node( reader );
7155 LeaveCriticalSection( &reader->cs );
7156 return hr;
7158 *count = min( base64.length - reader->text_conv_offset, max_count );
7159 memcpy( bytes, base64.bytes + reader->text_conv_offset, *count );
7160 reader->text_conv_offset += *count;
7161 heap_free( base64.bytes );
7164 LeaveCriticalSection( &reader->cs );
7165 return S_OK;
7168 static HRESULT utf8_to_utf16( const WS_XML_UTF8_TEXT *utf8, WS_XML_UTF16_TEXT *utf16 )
7170 int len = MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, NULL, 0 );
7171 if (!(utf16->bytes = heap_alloc( len * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
7172 MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, (WCHAR *)utf16->bytes, len );
7173 utf16->byteCount = len * sizeof(WCHAR);
7174 return S_OK;
7177 /**************************************************************************
7178 * WsReadChars [webservices.@]
7180 HRESULT WINAPI WsReadChars( WS_XML_READER *handle, WCHAR *chars, ULONG max_count, ULONG *count, WS_ERROR *error )
7182 struct reader *reader = (struct reader *)handle;
7184 TRACE( "%p %p %u %p %p\n", handle, chars, max_count, count, error );
7185 if (error) FIXME( "ignoring error parameter\n" );
7187 if (!reader) return E_INVALIDARG;
7189 EnterCriticalSection( &reader->cs );
7191 if (reader->magic != READER_MAGIC)
7193 LeaveCriticalSection( &reader->cs );
7194 return E_INVALIDARG;
7197 if (!reader->input_type)
7199 LeaveCriticalSection( &reader->cs );
7200 return WS_E_INVALID_OPERATION;
7203 if (!count)
7205 LeaveCriticalSection( &reader->cs );
7206 return E_INVALIDARG;
7209 *count = 0;
7210 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && chars)
7212 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
7213 WS_XML_UTF16_TEXT utf16;
7214 HRESULT hr;
7216 if ((hr = utf8_to_utf16( (const WS_XML_UTF8_TEXT *)text->text, &utf16 )) != S_OK)
7218 LeaveCriticalSection( &reader->cs );
7219 return hr;
7221 if (reader->text_conv_offset == utf16.byteCount / sizeof(WCHAR))
7223 heap_free( utf16.bytes );
7224 hr = read_node( reader );
7225 LeaveCriticalSection( &reader->cs );
7226 return hr;
7228 *count = min( utf16.byteCount / sizeof(WCHAR) - reader->text_conv_offset, max_count );
7229 memcpy( chars, utf16.bytes + reader->text_conv_offset * sizeof(WCHAR), *count * sizeof(WCHAR) );
7230 reader->text_conv_offset += *count;
7231 heap_free( utf16.bytes );
7234 LeaveCriticalSection( &reader->cs );
7235 return S_OK;
7238 /**************************************************************************
7239 * WsReadCharsUtf8 [webservices.@]
7241 HRESULT WINAPI WsReadCharsUtf8( WS_XML_READER *handle, BYTE *bytes, ULONG max_count, ULONG *count, WS_ERROR *error )
7243 struct reader *reader = (struct reader *)handle;
7244 HRESULT hr;
7246 TRACE( "%p %p %u %p %p\n", handle, bytes, max_count, count, error );
7247 if (error) FIXME( "ignoring error parameter\n" );
7249 if (!reader) return E_INVALIDARG;
7251 EnterCriticalSection( &reader->cs );
7253 if (reader->magic != READER_MAGIC)
7255 LeaveCriticalSection( &reader->cs );
7256 return E_INVALIDARG;
7259 if (!reader->input_type)
7261 LeaveCriticalSection( &reader->cs );
7262 return WS_E_INVALID_OPERATION;
7265 if (!count)
7267 LeaveCriticalSection( &reader->cs );
7268 return E_INVALIDARG;
7271 *count = 0;
7272 if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && bytes)
7274 const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
7275 const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
7277 if (reader->text_conv_offset == utf8->value.length)
7279 hr = read_node( reader );
7280 LeaveCriticalSection( &reader->cs );
7281 return hr;
7283 *count = min( utf8->value.length - reader->text_conv_offset, max_count );
7284 memcpy( bytes, utf8->value.bytes + reader->text_conv_offset, *count );
7285 reader->text_conv_offset += *count;
7288 LeaveCriticalSection( &reader->cs );
7289 return S_OK;
7292 static HRESULT move_to_element( struct reader *reader )
7294 HRESULT hr;
7295 if (node_type( reader->current ) == WS_XML_NODE_TYPE_BOF &&
7296 (hr = read_move_to( reader, WS_MOVE_TO_CHILD_NODE, NULL )) != S_OK) return hr;
7297 if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return E_FAIL;
7298 return S_OK;
7301 static HRESULT copy_tree( struct reader *reader, WS_XML_WRITER *writer )
7303 const struct node *node, *parent;
7304 BOOL done = FALSE;
7305 HRESULT hr;
7307 if ((hr = move_to_element( reader )) != S_OK) return hr;
7308 parent = reader->current;
7309 for (;;)
7311 node = reader->current;
7312 if ((hr = WsWriteNode( writer, (const WS_XML_NODE *)node, NULL )) != S_OK) break;
7313 if (node_type( node ) == WS_XML_NODE_TYPE_END_ELEMENT && node->parent == parent) done = TRUE;
7314 if ((hr = read_next_node( reader )) != S_OK || done) break;
7316 return hr;
7319 /**************************************************************************
7320 * WsReadXmlBuffer [webservices.@]
7322 HRESULT WINAPI WsReadXmlBuffer( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_BUFFER **ret, WS_ERROR *error )
7324 struct reader *reader = (struct reader *)handle;
7325 WS_XML_WRITER *writer = NULL;
7326 WS_XML_BUFFER *buffer;
7327 HRESULT hr;
7329 TRACE( "%p %p %p %p\n", handle, heap, ret, error );
7330 if (error) FIXME( "ignoring error parameter\n" );
7332 if (!reader || !heap) return E_INVALIDARG;
7333 if (!ret) return E_FAIL;
7335 EnterCriticalSection( &reader->cs );
7337 if (reader->magic != READER_MAGIC)
7339 LeaveCriticalSection( &reader->cs );
7340 return E_INVALIDARG;
7343 if (!reader->input_type)
7345 LeaveCriticalSection( &reader->cs );
7346 return WS_E_INVALID_OPERATION;
7349 if ((hr = WsCreateWriter( NULL, 0, &writer, NULL )) != S_OK) goto done;
7350 if ((hr = WsCreateXmlBuffer( heap, NULL, 0, &buffer, NULL )) != S_OK) goto done;
7351 if ((hr = WsSetOutputToBuffer( writer, buffer, NULL, 0, NULL )) != S_OK) goto done;
7352 if ((hr = copy_tree( reader, writer )) == S_OK) *ret = buffer;
7354 done:
7355 if (hr != S_OK) free_xmlbuf( (struct xmlbuf *)buffer );
7356 WsFreeWriter( writer );
7357 LeaveCriticalSection( &reader->cs );
7358 return hr;
7361 HRESULT create_header_buffer( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_BUFFER **ret )
7363 struct reader *reader = (struct reader *)handle;
7364 HRESULT hr = WS_E_QUOTA_EXCEEDED;
7365 struct xmlbuf *xmlbuf;
7367 EnterCriticalSection( &reader->cs );
7369 if (reader->magic != READER_MAGIC)
7371 LeaveCriticalSection( &reader->cs );
7372 return E_INVALIDARG;
7375 if ((xmlbuf = alloc_xmlbuf( heap, reader->read_pos, reader->input_enc, reader->input_charset,
7376 reader->dict_static, reader->dict )))
7378 memcpy( xmlbuf->bytes.bytes, reader->read_bufptr, reader->read_pos );
7379 xmlbuf->bytes.length = reader->read_pos;
7380 *ret = (WS_XML_BUFFER *)xmlbuf;
7381 hr = S_OK;
7384 LeaveCriticalSection( &reader->cs );
7385 return hr;
7388 HRESULT get_param_desc( const WS_STRUCT_DESCRIPTION *desc, USHORT index, const WS_FIELD_DESCRIPTION **ret )
7390 if (index >= desc->fieldCount) return E_INVALIDARG;
7391 *ret = desc->fields[index];
7392 return S_OK;
7395 static ULONG get_field_size( const WS_FIELD_DESCRIPTION *desc )
7397 if (desc->options & WS_FIELD_POINTER) return sizeof(void *);
7398 return get_type_size( desc->type, desc->typeDescription );
7401 static HRESULT read_param( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap, void *ret )
7403 if (!ret && !(ret = ws_alloc_zero( heap, get_field_size(desc) ))) return WS_E_QUOTA_EXCEEDED;
7404 return read_type_field( reader, desc, heap, ret, 0 );
7407 static HRESULT read_param_array( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap,
7408 void **ret, ULONG *count )
7410 if (!ret && !(ret = ws_alloc_zero( heap, sizeof(void **) ))) return WS_E_QUOTA_EXCEEDED;
7411 return read_type_array( reader, desc, heap, ret, count );
7414 static void set_array_len( const WS_PARAMETER_DESCRIPTION *params, ULONG count, ULONG index, ULONG len,
7415 const void **args )
7417 ULONG i, *ptr;
7418 for (i = 0; i < count; i++)
7420 if (params[i].outputMessageIndex != index || params[i].parameterType != WS_PARAMETER_TYPE_ARRAY_COUNT)
7421 continue;
7422 if ((ptr = *(ULONG **)args[i])) *ptr = len;
7423 break;
7427 HRESULT read_output_params( WS_XML_READER *handle, WS_HEAP *heap, const WS_ELEMENT_DESCRIPTION *desc,
7428 const WS_PARAMETER_DESCRIPTION *params, ULONG count, const void **args )
7430 struct reader *reader = (struct reader *)handle;
7431 const WS_STRUCT_DESCRIPTION *desc_struct;
7432 const WS_FIELD_DESCRIPTION *desc_field;
7433 ULONG i, len;
7434 HRESULT hr;
7436 if (desc->type != WS_STRUCT_TYPE || !(desc_struct = desc->typeDescription)) return E_INVALIDARG;
7438 EnterCriticalSection( &reader->cs );
7440 if (reader->magic != READER_MAGIC)
7442 LeaveCriticalSection( &reader->cs );
7443 return E_INVALIDARG;
7446 if ((hr = start_mapping( reader, WS_ELEMENT_TYPE_MAPPING, desc->elementLocalName, desc->elementNs )) != S_OK)
7447 goto done;
7449 for (i = 0; i < count; i++)
7451 if (params[i].outputMessageIndex == INVALID_PARAMETER_INDEX) continue;
7452 if (params[i].parameterType == WS_PARAMETER_TYPE_MESSAGES)
7454 FIXME( "messages type not supported\n" );
7455 hr = E_NOTIMPL;
7456 goto done;
7458 if ((hr = get_param_desc( desc_struct, params[i].outputMessageIndex, &desc_field )) != S_OK) goto done;
7459 if (params[i].parameterType == WS_PARAMETER_TYPE_NORMAL)
7461 void *ptr = *(void **)args[i];
7462 if ((hr = read_param( reader, desc_field, heap, ptr )) != S_OK) goto done;
7464 else if (params[i].parameterType == WS_PARAMETER_TYPE_ARRAY)
7466 void **ptr = *(void ***)args[i];
7467 if ((hr = read_param_array( reader, desc_field, heap, ptr, &len )) != S_OK) goto done;
7468 set_array_len( params, count, params[i].outputMessageIndex, len, args );
7472 if (desc_struct->structOptions & WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
7474 struct node *parent = find_parent( reader );
7475 parent->flags |= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT;
7478 hr = end_mapping( reader, WS_ELEMENT_TYPE_MAPPING );
7480 done:
7481 LeaveCriticalSection( &reader->cs );
7482 return hr;