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
27 #include "webservices.h"
29 #include "wine/debug.h"
30 #include "wine/heap.h"
31 #include "wine/list.h"
32 #include "webservices_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(webservices
);
36 ULONG
prop_size( const struct prop_desc
*desc
, ULONG count
)
38 ULONG i
, ret
= count
* sizeof(struct prop
);
39 for (i
= 0; i
< count
; i
++) ret
+= desc
[i
].size
;
43 void prop_init( const struct prop_desc
*desc
, ULONG count
, struct prop
*prop
, void *data
)
47 for (i
= 0; i
< count
; i
++)
50 prop
[i
].size
= desc
[i
].size
;
51 prop
[i
].readonly
= desc
[i
].readonly
;
52 prop
[i
].writeonly
= desc
[i
].writeonly
;
57 HRESULT
prop_set( const struct prop
*prop
, ULONG count
, ULONG id
, const void *value
, ULONG size
)
59 if (id
>= count
|| size
!= prop
[id
].size
|| prop
[id
].readonly
) return E_INVALIDARG
;
60 memcpy( prop
[id
].value
, value
, size
);
64 HRESULT
prop_get( const struct prop
*prop
, ULONG count
, ULONG id
, void *buf
, ULONG size
)
66 if (id
>= count
|| size
!= prop
[id
].size
|| prop
[id
].writeonly
) return E_INVALIDARG
;
67 memcpy( buf
, prop
[id
].value
, prop
[id
].size
);
71 struct node
*alloc_node( WS_XML_NODE_TYPE type
)
75 if (!(ret
= heap_alloc_zero( sizeof(*ret
) ))) return NULL
;
76 ret
->hdr
.node
.nodeType
= type
;
77 list_init( &ret
->entry
);
78 list_init( &ret
->children
);
82 void free_attribute( WS_XML_ATTRIBUTE
*attr
)
85 free_xml_string( attr
->prefix
);
86 free_xml_string( attr
->localName
);
87 free_xml_string( attr
->ns
);
88 heap_free( attr
->value
);
92 void free_node( struct node
*node
)
95 switch (node_type( node
))
97 case WS_XML_NODE_TYPE_ELEMENT
:
99 WS_XML_ELEMENT_NODE
*elem
= &node
->hdr
;
102 for (i
= 0; i
< elem
->attributeCount
; i
++) free_attribute( elem
->attributes
[i
] );
103 heap_free( elem
->attributes
);
104 free_xml_string( elem
->prefix
);
105 free_xml_string( elem
->localName
);
106 free_xml_string( elem
->ns
);
109 case WS_XML_NODE_TYPE_TEXT
:
111 WS_XML_TEXT_NODE
*text
= (WS_XML_TEXT_NODE
*)node
;
112 heap_free( text
->text
);
115 case WS_XML_NODE_TYPE_COMMENT
:
117 WS_XML_COMMENT_NODE
*comment
= (WS_XML_COMMENT_NODE
*)node
;
118 heap_free( comment
->value
.bytes
);
121 case WS_XML_NODE_TYPE_CDATA
:
122 case WS_XML_NODE_TYPE_END_CDATA
:
123 case WS_XML_NODE_TYPE_END_ELEMENT
:
124 case WS_XML_NODE_TYPE_EOF
:
125 case WS_XML_NODE_TYPE_BOF
:
129 ERR( "unhandled type %u\n", node_type( node
) );
135 void destroy_nodes( struct node
*node
)
140 while ((ptr
= list_head( &node
->children
)))
142 struct node
*child
= LIST_ENTRY( ptr
, struct node
, entry
);
143 list_remove( &child
->entry
);
144 destroy_nodes( child
);
149 static WS_XML_ATTRIBUTE
*dup_attribute( const WS_XML_ATTRIBUTE
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
151 WS_XML_ATTRIBUTE
*dst
;
154 if (!(dst
= heap_alloc_zero( sizeof(*dst
) ))) return NULL
;
155 dst
->singleQuote
= src
->singleQuote
;
156 dst
->isXmlNs
= src
->isXmlNs
;
158 if (src
->prefix
&& !(dst
->prefix
= dup_xml_string( src
->prefix
, FALSE
))) goto error
;
159 if (src
->localName
&& !(dst
->localName
= dup_xml_string( src
->localName
, FALSE
))) goto error
;
160 if (src
->ns
&& !(dst
->ns
= dup_xml_string( src
->ns
, FALSE
))) goto error
;
166 case WS_XML_WRITER_ENCODING_TYPE_BINARY
:
167 if ((hr
= text_to_text( src
->value
, NULL
, NULL
, &dst
->value
)) != S_OK
) goto error
;
170 case WS_XML_WRITER_ENCODING_TYPE_TEXT
:
171 if ((hr
= text_to_utf8text( src
->value
, NULL
, NULL
, (WS_XML_UTF8_TEXT
**)&dst
->value
)) != S_OK
)
176 ERR( "unhandled encoding %u\n", enc
);
184 free_attribute( dst
);
188 static WS_XML_ATTRIBUTE
**dup_attributes( WS_XML_ATTRIBUTE
* const *src
, ULONG count
,
189 WS_XML_WRITER_ENCODING_TYPE enc
)
191 WS_XML_ATTRIBUTE
**dst
;
194 if (!(dst
= heap_alloc( sizeof(*dst
) * count
))) return NULL
;
195 for (i
= 0; i
< count
; i
++)
197 if (!(dst
[i
] = dup_attribute( src
[i
], enc
)))
199 for (; i
> 0; i
--) free_attribute( dst
[i
- 1] );
207 static struct node
*dup_element_node( const WS_XML_ELEMENT_NODE
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
210 WS_XML_ELEMENT_NODE
*dst
;
211 ULONG count
= src
->attributeCount
;
212 WS_XML_ATTRIBUTE
**attrs
= src
->attributes
;
213 const WS_XML_STRING
*prefix
= (src
->prefix
&& src
->prefix
->length
) ? src
->prefix
: NULL
;
214 const WS_XML_STRING
*localname
= src
->localName
;
215 const WS_XML_STRING
*ns
= src
->ns
;
217 if (!(node
= alloc_node( WS_XML_NODE_TYPE_ELEMENT
))) return NULL
;
220 if (count
&& !(dst
->attributes
= dup_attributes( attrs
, count
, enc
))) goto error
;
221 dst
->attributeCount
= count
;
223 if (prefix
&& !(dst
->prefix
= dup_xml_string( prefix
, FALSE
))) goto error
;
224 if (localname
&& !(dst
->localName
= dup_xml_string( localname
, FALSE
))) goto error
;
225 if (ns
&& !(dst
->ns
= dup_xml_string( ns
, FALSE
))) goto error
;
233 static struct node
*dup_text_node( const WS_XML_TEXT_NODE
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
236 WS_XML_TEXT_NODE
*dst
;
239 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
240 dst
= (WS_XML_TEXT_NODE
*)node
;
241 if (!src
->text
) return node
;
245 case WS_XML_WRITER_ENCODING_TYPE_BINARY
:
246 hr
= text_to_text( src
->text
, NULL
, NULL
, &dst
->text
);
249 case WS_XML_WRITER_ENCODING_TYPE_TEXT
:
250 hr
= text_to_utf8text( src
->text
, NULL
, NULL
, (WS_XML_UTF8_TEXT
**)&dst
->text
);
254 ERR( "unhandled encoding %u\n", enc
);
268 static struct node
*dup_comment_node( const WS_XML_COMMENT_NODE
*src
)
271 WS_XML_COMMENT_NODE
*dst
;
273 if (!(node
= alloc_node( WS_XML_NODE_TYPE_COMMENT
))) return NULL
;
274 dst
= (WS_XML_COMMENT_NODE
*)node
;
276 if (src
->value
.length
&& !(dst
->value
.bytes
= heap_alloc( src
->value
.length
)))
281 memcpy( dst
->value
.bytes
, src
->value
.bytes
, src
->value
.length
);
282 dst
->value
.length
= src
->value
.length
;
286 static struct node
*dup_node( const struct node
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
288 switch (node_type( src
))
290 case WS_XML_NODE_TYPE_ELEMENT
:
291 return dup_element_node( &src
->hdr
, enc
);
293 case WS_XML_NODE_TYPE_TEXT
:
294 return dup_text_node( (const WS_XML_TEXT_NODE
*)src
, enc
);
296 case WS_XML_NODE_TYPE_COMMENT
:
297 return dup_comment_node( (const WS_XML_COMMENT_NODE
*)src
);
299 case WS_XML_NODE_TYPE_CDATA
:
300 case WS_XML_NODE_TYPE_END_CDATA
:
301 case WS_XML_NODE_TYPE_END_ELEMENT
:
302 case WS_XML_NODE_TYPE_EOF
:
303 case WS_XML_NODE_TYPE_BOF
:
304 return alloc_node( node_type( src
) );
307 ERR( "unhandled type %u\n", node_type( src
) );
313 static HRESULT
dup_tree( const struct node
*src
, WS_XML_WRITER_ENCODING_TYPE enc
, struct node
**dst
)
316 const struct node
*child
;
318 if (!*dst
&& !(*dst
= dup_node( src
, enc
))) return E_OUTOFMEMORY
;
321 LIST_FOR_EACH_ENTRY( child
, &src
->children
, struct node
, entry
)
323 HRESULT hr
= E_OUTOFMEMORY
;
324 struct node
*new_child
;
326 if (!(new_child
= dup_node( child
, enc
)) || (hr
= dup_tree( child
, enc
, &new_child
)) != S_OK
)
328 destroy_nodes( *dst
);
331 new_child
->parent
= parent
;
332 list_add_tail( &parent
->children
, &new_child
->entry
);
337 static const struct prop_desc reader_props
[] =
339 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_DEPTH */
340 { sizeof(BOOL
), FALSE
}, /* WS_XML_READER_PROPERTY_ALLOW_FRAGMENT */
341 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_ATTRIBUTES */
342 { sizeof(BOOL
), FALSE
}, /* WS_XML_READER_PROPERTY_READ_DECLARATION */
343 { sizeof(WS_CHARSET
), FALSE
}, /* WS_XML_READER_PROPERTY_CHARSET */
344 { sizeof(ULONGLONG
), TRUE
}, /* WS_XML_READER_PROPERTY_ROW */
345 { sizeof(ULONGLONG
), TRUE
}, /* WS_XML_READER_PROPERTY_COLUMN */
346 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_UTF8_TRIM_SIZE */
347 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_STREAM_BUFFER_SIZE */
348 { sizeof(BOOL
), TRUE
}, /* WS_XML_READER_PROPERTY_IN_ATTRIBUTE */
349 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_STREAM_MAX_ROOT_MIME_PART_SIZE */
350 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_STREAM_MAX_MIME_HEADERS_SIZE */
351 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_MIME_PARTS */
352 { sizeof(BOOL
), FALSE
}, /* WS_XML_READER_PROPERTY_ALLOW_INVALID_CHARACTER_REFERENCES */
353 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_NAMESPACES */
358 READER_STATE_INITIAL
,
360 READER_STATE_STARTELEMENT
,
361 READER_STATE_STARTATTRIBUTE
,
362 READER_STATE_STARTCDATA
,
365 READER_STATE_ENDELEMENT
,
366 READER_STATE_ENDCDATA
,
367 READER_STATE_COMMENT
,
383 const unsigned char *read_bufptr
;
384 enum reader_state state
;
386 struct node
*current
;
389 struct prefix
*prefixes
;
391 ULONG nb_prefixes_allocated
;
392 WS_XML_READER_ENCODING_TYPE input_enc
;
393 WS_CHARSET input_charset
;
394 WS_XML_READER_INPUT_TYPE input_type
;
395 WS_READ_CALLBACK input_cb
;
396 void *input_cb_state
;
397 struct xmlbuf
*input_buf
;
398 unsigned char *input_conv
;
400 ULONG text_conv_offset
;
401 unsigned char *stream_buf
;
402 const WS_XML_DICTIONARY
*dict_static
;
403 WS_XML_DICTIONARY
*dict
;
405 struct prop prop
[ARRAY_SIZE( reader_props
)];
408 #define READER_MAGIC (('R' << 24) | ('E' << 16) | ('A' << 8) | 'D')
410 static struct reader
*alloc_reader(void)
412 static const ULONG count
= ARRAY_SIZE( reader_props
);
414 ULONG size
= sizeof(*ret
) + prop_size( reader_props
, count
);
416 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
417 if (!(ret
->prefixes
= heap_alloc_zero( sizeof(*ret
->prefixes
) )))
422 ret
->nb_prefixes
= ret
->nb_prefixes_allocated
= 1;
424 ret
->magic
= READER_MAGIC
;
425 InitializeCriticalSection( &ret
->cs
);
426 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": reader.cs");
428 prop_init( reader_props
, count
, ret
->prop
, &ret
[1] );
429 ret
->prop_count
= count
;
433 static void clear_prefixes( struct prefix
*prefixes
, ULONG count
)
436 for (i
= 0; i
< count
; i
++)
438 free_xml_string( prefixes
[i
].str
);
439 prefixes
[i
].str
= NULL
;
440 free_xml_string( prefixes
[i
].ns
);
441 prefixes
[i
].ns
= NULL
;
445 static HRESULT
set_prefix( struct prefix
*prefix
, const WS_XML_STRING
*str
, const WS_XML_STRING
*ns
)
449 free_xml_string( prefix
->str
);
450 if (!(prefix
->str
= dup_xml_string( str
, FALSE
))) return E_OUTOFMEMORY
;
452 if (prefix
->ns
) free_xml_string( prefix
->ns
);
453 if (!(prefix
->ns
= dup_xml_string( ns
, FALSE
))) return E_OUTOFMEMORY
;
457 static HRESULT
bind_prefix( struct reader
*reader
, const WS_XML_STRING
*prefix
, const WS_XML_STRING
*ns
)
462 for (i
= 0; i
< reader
->nb_prefixes
; i
++)
464 if (WsXmlStringEquals( prefix
, reader
->prefixes
[i
].str
, NULL
) == S_OK
)
465 return set_prefix( &reader
->prefixes
[i
], NULL
, ns
);
467 if (i
>= reader
->nb_prefixes_allocated
)
469 ULONG new_size
= reader
->nb_prefixes_allocated
* sizeof(*reader
->prefixes
) * 2;
470 struct prefix
*tmp
= heap_realloc_zero( reader
->prefixes
, new_size
);
471 if (!tmp
) return E_OUTOFMEMORY
;
472 reader
->prefixes
= tmp
;
473 reader
->nb_prefixes_allocated
*= 2;
475 if ((hr
= set_prefix( &reader
->prefixes
[i
], prefix
, ns
)) != S_OK
) return hr
;
476 reader
->nb_prefixes
++;
480 static const WS_XML_STRING
*get_namespace( struct reader
*reader
, const WS_XML_STRING
*prefix
)
483 for (i
= 0; i
< reader
->nb_prefixes
; i
++)
485 if (WsXmlStringEquals( prefix
, reader
->prefixes
[i
].str
, NULL
) == S_OK
)
486 return reader
->prefixes
[i
].ns
;
491 static void read_insert_eof( struct reader
*reader
, struct node
*eof
)
493 if (!reader
->root
) reader
->root
= eof
;
496 eof
->parent
= reader
->root
;
497 list_add_tail( &reader
->root
->children
, &eof
->entry
);
499 reader
->current
= reader
->last
= eof
;
502 static void read_insert_bof( struct reader
*reader
, struct node
*bof
)
504 reader
->root
->parent
= bof
;
505 list_add_tail( &bof
->children
, &reader
->root
->entry
);
506 reader
->current
= reader
->last
= reader
->root
= bof
;
509 static void read_insert_node( struct reader
*reader
, struct node
*parent
, struct node
*node
)
511 node
->parent
= parent
;
512 list_add_before( list_tail( &parent
->children
), &node
->entry
);
513 reader
->current
= reader
->last
= node
;
516 static void free_reader( struct reader
*reader
)
518 destroy_nodes( reader
->root
);
519 clear_prefixes( reader
->prefixes
, reader
->nb_prefixes
);
520 heap_free( reader
->prefixes
);
521 heap_free( reader
->stream_buf
);
522 heap_free( reader
->input_conv
);
524 reader
->cs
.DebugInfo
->Spare
[0] = 0;
525 DeleteCriticalSection( &reader
->cs
);
529 static HRESULT
init_reader( struct reader
*reader
)
531 static const WS_XML_STRING empty
= {0, NULL
};
535 reader
->state
= READER_STATE_INITIAL
;
536 destroy_nodes( reader
->root
);
537 reader
->root
= reader
->current
= NULL
;
538 reader
->current_attr
= 0;
539 clear_prefixes( reader
->prefixes
, reader
->nb_prefixes
);
540 reader
->nb_prefixes
= 1;
541 if ((hr
= bind_prefix( reader
, &empty
, &empty
)) != S_OK
) return hr
;
543 if (!(node
= alloc_node( WS_XML_NODE_TYPE_EOF
))) return E_OUTOFMEMORY
;
544 read_insert_eof( reader
, node
);
545 reader
->input_enc
= WS_XML_READER_ENCODING_TYPE_TEXT
;
546 reader
->input_charset
= WS_CHARSET_UTF8
;
547 reader
->dict_static
= NULL
;
552 /**************************************************************************
553 * WsCreateReader [webservices.@]
555 HRESULT WINAPI
WsCreateReader( const WS_XML_READER_PROPERTY
*properties
, ULONG count
,
556 WS_XML_READER
**handle
, WS_ERROR
*error
)
558 struct reader
*reader
;
559 ULONG i
, max_depth
= 32, max_attrs
= 128, max_ns
= 32;
560 BOOL read_decl
= TRUE
;
563 TRACE( "%p %u %p %p\n", properties
, count
, handle
, error
);
564 if (error
) FIXME( "ignoring error parameter\n" );
566 if (!handle
) return E_INVALIDARG
;
567 if (!(reader
= alloc_reader())) return E_OUTOFMEMORY
;
569 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_MAX_DEPTH
, &max_depth
, sizeof(max_depth
) );
570 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_MAX_ATTRIBUTES
, &max_attrs
, sizeof(max_attrs
) );
571 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_READ_DECLARATION
, &read_decl
, sizeof(read_decl
) );
572 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_MAX_NAMESPACES
, &max_ns
, sizeof(max_ns
) );
574 for (i
= 0; i
< count
; i
++)
576 hr
= prop_set( reader
->prop
, reader
->prop_count
, properties
[i
].id
, properties
[i
].value
,
577 properties
[i
].valueSize
);
580 free_reader( reader
);
585 if ((hr
= init_reader( reader
)) != S_OK
)
587 free_reader( reader
);
591 TRACE( "created %p\n", reader
);
592 *handle
= (WS_XML_READER
*)reader
;
596 /**************************************************************************
597 * WsFreeReader [webservices.@]
599 void WINAPI
WsFreeReader( WS_XML_READER
*handle
)
601 struct reader
*reader
= (struct reader
*)handle
;
603 TRACE( "%p\n", handle
);
607 EnterCriticalSection( &reader
->cs
);
609 if (reader
->magic
!= READER_MAGIC
)
611 LeaveCriticalSection( &reader
->cs
);
617 LeaveCriticalSection( &reader
->cs
);
618 free_reader( reader
);
621 static HRESULT
read_more_data( struct reader
*reader
, ULONG min_size
, const WS_ASYNC_CONTEXT
*ctx
,
624 ULONG size
= 0, max_size
;
626 if (reader
->read_size
- reader
->read_pos
>= min_size
) return S_OK
;
627 if (reader
->input_type
!= WS_XML_READER_INPUT_TYPE_STREAM
) return WS_E_INVALID_FORMAT
;
628 if (min_size
> reader
->input_size
) return WS_E_QUOTA_EXCEEDED
;
630 if (reader
->read_pos
)
632 memmove( reader
->stream_buf
, reader
->stream_buf
+ reader
->read_pos
, reader
->read_size
- reader
->read_pos
);
633 reader
->read_size
-= reader
->read_pos
;
634 reader
->read_pos
= 0;
636 max_size
= reader
->input_size
- reader
->read_size
;
638 reader
->input_cb( reader
->input_cb_state
, reader
->stream_buf
+ reader
->read_size
, max_size
, &size
, ctx
, error
);
639 if (size
< min_size
) return WS_E_QUOTA_EXCEEDED
;
640 reader
->read_size
+= size
;
644 /**************************************************************************
645 * WsFillReader [webservices.@]
647 HRESULT WINAPI
WsFillReader( WS_XML_READER
*handle
, ULONG min_size
, const WS_ASYNC_CONTEXT
*ctx
,
650 struct reader
*reader
= (struct reader
*)handle
;
653 TRACE( "%p %u %p %p\n", handle
, min_size
, ctx
, error
);
654 if (error
) FIXME( "ignoring error parameter\n" );
655 if (ctx
) FIXME( "ignoring ctx parameter\n" );
657 if (!reader
) return E_INVALIDARG
;
659 EnterCriticalSection( &reader
->cs
);
661 if (reader
->magic
!= READER_MAGIC
)
663 LeaveCriticalSection( &reader
->cs
);
667 if (reader
->input_type
== WS_XML_READER_INPUT_TYPE_STREAM
)
669 hr
= read_more_data( reader
, min_size
, ctx
, error
);
673 reader
->read_size
= min( min_size
, reader
->input_size
);
674 reader
->read_pos
= 0;
678 LeaveCriticalSection( &reader
->cs
);
679 TRACE( "returning %08x\n", hr
);
683 /**************************************************************************
684 * WsGetNamespaceFromPrefix [webservices.@]
686 HRESULT WINAPI
WsGetNamespaceFromPrefix( WS_XML_READER
*handle
, const WS_XML_STRING
*prefix
,
687 BOOL required
, const WS_XML_STRING
**ns
, WS_ERROR
*error
)
689 static const WS_XML_STRING xml
= {3, (BYTE
*)"xml"};
690 static const WS_XML_STRING xmlns
= {5, (BYTE
*)"xmlns"};
691 static const WS_XML_STRING empty_ns
= {0, NULL
};
692 static const WS_XML_STRING xml_ns
= {36, (BYTE
*)"http://www.w3.org/XML/1998/namespace"};
693 static const WS_XML_STRING xmlns_ns
= {29, (BYTE
*)"http://www.w3.org/2000/xmlns/"};
694 struct reader
*reader
= (struct reader
*)handle
;
698 TRACE( "%p %s %d %p %p\n", handle
, debugstr_xmlstr(prefix
), required
, ns
, error
);
699 if (error
) FIXME( "ignoring error parameter\n" );
701 if (!reader
|| !prefix
|| !ns
) return E_INVALIDARG
;
703 EnterCriticalSection( &reader
->cs
);
705 if (reader
->magic
!= READER_MAGIC
)
707 LeaveCriticalSection( &reader
->cs
);
711 if (reader
->state
!= READER_STATE_STARTELEMENT
) hr
= WS_E_INVALID_OPERATION
;
712 else if (!prefix
->length
)
717 else if (WsXmlStringEquals( prefix
, &xml
, NULL
) == S_OK
)
722 else if (WsXmlStringEquals( prefix
, &xmlns
, NULL
) == S_OK
)
729 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
732 for (i
= 0; i
< elem
->attributeCount
; i
++)
734 if (!elem
->attributes
[i
]->isXmlNs
) continue;
735 if (WsXmlStringEquals( prefix
, elem
->attributes
[i
]->prefix
, NULL
) == S_OK
)
737 *ns
= elem
->attributes
[i
]->ns
;
744 LeaveCriticalSection( &reader
->cs
);
746 if (hr
== S_OK
&& !found
)
748 if (required
) hr
= WS_E_INVALID_FORMAT
;
756 TRACE( "returning %08x\n", hr
);
760 /**************************************************************************
761 * WsGetReaderNode [webservices.@]
763 HRESULT WINAPI
WsGetReaderNode( WS_XML_READER
*handle
, const WS_XML_NODE
**node
,
766 struct reader
*reader
= (struct reader
*)handle
;
769 TRACE( "%p %p %p\n", handle
, node
, error
);
770 if (error
) FIXME( "ignoring error parameter\n" );
772 if (!reader
|| !node
) return E_INVALIDARG
;
774 EnterCriticalSection( &reader
->cs
);
776 if (reader
->magic
!= READER_MAGIC
)
778 LeaveCriticalSection( &reader
->cs
);
782 *node
= &reader
->current
->hdr
.node
;
784 LeaveCriticalSection( &reader
->cs
);
785 TRACE( "returning %08x\n", hr
);
789 static HRESULT
get_charset( struct reader
*reader
, void *buf
, ULONG size
)
791 if (!buf
|| size
!= sizeof(reader
->input_charset
)) return E_INVALIDARG
;
792 if (!reader
->input_charset
) return WS_E_INVALID_FORMAT
;
793 *(WS_CHARSET
*)buf
= reader
->input_charset
;
797 /**************************************************************************
798 * WsGetReaderProperty [webservices.@]
800 HRESULT WINAPI
WsGetReaderProperty( WS_XML_READER
*handle
, WS_XML_READER_PROPERTY_ID id
,
801 void *buf
, ULONG size
, WS_ERROR
*error
)
803 struct reader
*reader
= (struct reader
*)handle
;
806 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
807 if (error
) FIXME( "ignoring error parameter\n" );
809 if (!reader
) return E_INVALIDARG
;
811 EnterCriticalSection( &reader
->cs
);
813 if (reader
->magic
!= READER_MAGIC
)
815 LeaveCriticalSection( &reader
->cs
);
819 if (!reader
->input_type
) hr
= WS_E_INVALID_OPERATION
;
820 else if (id
== WS_XML_READER_PROPERTY_CHARSET
) hr
= get_charset( reader
, buf
, size
);
821 else hr
= prop_get( reader
->prop
, reader
->prop_count
, id
, buf
, size
);
823 LeaveCriticalSection( &reader
->cs
);
824 TRACE( "returning %08x\n", hr
);
828 /**************************************************************************
829 * WsGetXmlAttribute [webservices.@]
831 HRESULT WINAPI
WsGetXmlAttribute( WS_XML_READER
*handle
, const WS_XML_STRING
*attr
,
832 WS_HEAP
*heap
, WCHAR
**str
, ULONG
*len
, WS_ERROR
*error
)
834 FIXME( "%p %s %p %p %p %p: stub\n", handle
, debugstr_xmlstr(attr
), heap
, str
, len
, error
);
838 WS_XML_UTF8_TEXT
*alloc_utf8_text( const BYTE
*data
, ULONG len
)
840 WS_XML_UTF8_TEXT
*ret
;
842 if (!(ret
= heap_alloc( sizeof(*ret
) + len
))) return NULL
;
843 ret
->text
.textType
= WS_XML_TEXT_TYPE_UTF8
;
844 ret
->value
.length
= len
;
845 ret
->value
.bytes
= len
? (BYTE
*)(ret
+ 1) : NULL
;
846 ret
->value
.dictionary
= NULL
;
848 if (data
) memcpy( ret
->value
.bytes
, data
, len
);
852 WS_XML_UTF16_TEXT
*alloc_utf16_text( const BYTE
*data
, ULONG len
)
854 WS_XML_UTF16_TEXT
*ret
;
856 if (!(ret
= heap_alloc( sizeof(*ret
) + len
))) return NULL
;
857 ret
->text
.textType
= WS_XML_TEXT_TYPE_UTF16
;
858 ret
->byteCount
= len
;
859 ret
->bytes
= len
? (BYTE
*)(ret
+ 1) : NULL
;
860 if (data
) memcpy( ret
->bytes
, data
, len
);
864 WS_XML_BASE64_TEXT
*alloc_base64_text( const BYTE
*data
, ULONG len
)
866 WS_XML_BASE64_TEXT
*ret
;
868 if (!(ret
= heap_alloc( sizeof(*ret
) + len
))) return NULL
;
869 ret
->text
.textType
= WS_XML_TEXT_TYPE_BASE64
;
871 ret
->bytes
= len
? (BYTE
*)(ret
+ 1) : NULL
;
872 if (data
) memcpy( ret
->bytes
, data
, len
);
876 WS_XML_BOOL_TEXT
*alloc_bool_text( BOOL value
)
878 WS_XML_BOOL_TEXT
*ret
;
880 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
881 ret
->text
.textType
= WS_XML_TEXT_TYPE_BOOL
;
886 WS_XML_INT32_TEXT
*alloc_int32_text( INT32 value
)
888 WS_XML_INT32_TEXT
*ret
;
890 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
891 ret
->text
.textType
= WS_XML_TEXT_TYPE_INT32
;
896 WS_XML_INT64_TEXT
*alloc_int64_text( INT64 value
)
898 WS_XML_INT64_TEXT
*ret
;
900 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
901 ret
->text
.textType
= WS_XML_TEXT_TYPE_INT64
;
906 WS_XML_UINT64_TEXT
*alloc_uint64_text( UINT64 value
)
908 WS_XML_UINT64_TEXT
*ret
;
910 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
911 ret
->text
.textType
= WS_XML_TEXT_TYPE_UINT64
;
916 WS_XML_FLOAT_TEXT
*alloc_float_text( float value
)
918 WS_XML_FLOAT_TEXT
*ret
;
920 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
921 ret
->text
.textType
= WS_XML_TEXT_TYPE_FLOAT
;
926 WS_XML_DOUBLE_TEXT
*alloc_double_text( double value
)
928 WS_XML_DOUBLE_TEXT
*ret
;
930 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
931 ret
->text
.textType
= WS_XML_TEXT_TYPE_DOUBLE
;
936 WS_XML_GUID_TEXT
*alloc_guid_text( const GUID
*value
)
938 WS_XML_GUID_TEXT
*ret
;
940 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
941 ret
->text
.textType
= WS_XML_TEXT_TYPE_GUID
;
946 WS_XML_UNIQUE_ID_TEXT
*alloc_unique_id_text( const GUID
*value
)
948 WS_XML_UNIQUE_ID_TEXT
*ret
;
950 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
951 ret
->text
.textType
= WS_XML_TEXT_TYPE_UNIQUE_ID
;
956 WS_XML_DATETIME_TEXT
*alloc_datetime_text( const WS_DATETIME
*value
)
958 WS_XML_DATETIME_TEXT
*ret
;
960 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
961 ret
->text
.textType
= WS_XML_TEXT_TYPE_DATETIME
;
966 static inline BOOL
read_end_of_data( struct reader
*reader
)
968 return (read_more_data( reader
, 1, NULL
, NULL
) != S_OK
);
971 static inline const unsigned char *read_current_ptr( struct reader
*reader
)
973 return &reader
->read_bufptr
[reader
->read_pos
];
976 static inline void read_skip( struct reader
*reader
, unsigned int count
)
978 assert( reader
->read_pos
+ count
<= reader
->read_size
);
979 reader
->read_pos
+= count
;
982 static inline HRESULT
read_peek( struct reader
*reader
, unsigned char *bytes
, unsigned int len
)
985 if ((hr
= read_more_data( reader
, len
, NULL
, NULL
)) != S_OK
) return hr
;
986 memcpy( bytes
, read_current_ptr( reader
), len
);
990 static inline HRESULT
read_byte( struct reader
*reader
, unsigned char *byte
)
993 if ((hr
= read_more_data( reader
, 1, NULL
, NULL
)) != S_OK
) return hr
;
994 *byte
= *read_current_ptr( reader
);
995 read_skip( reader
, 1 );
999 static inline HRESULT
read_bytes( struct reader
*reader
, unsigned char *bytes
, unsigned int len
)
1002 if ((hr
= read_more_data( reader
, len
, NULL
, NULL
)) != S_OK
) return hr
;
1003 memcpy( bytes
, read_current_ptr( reader
), len
);
1004 read_skip( reader
, len
);
1008 /* UTF-8 support based on libs/wine/utf8.c */
1010 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
1011 static const char utf8_length
[128] =
1013 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
1014 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
1015 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
1016 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
1017 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
1018 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
1019 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
1020 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
1023 /* first byte mask depending on UTF-8 sequence length */
1024 static const unsigned char utf8_mask
[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
1026 /* minimum Unicode value depending on UTF-8 sequence length */
1027 static const unsigned int utf8_minval
[4] = { 0x0, 0x80, 0x800, 0x10000 };
1029 static inline HRESULT
read_utf8_char( struct reader
*reader
, unsigned int *ret
, unsigned int *skip
)
1033 const unsigned char *end
;
1036 if ((hr
= read_more_data( reader
, 1, NULL
, NULL
)) != S_OK
) return hr
;
1037 ch
= *read_current_ptr( reader
);
1045 len
= utf8_length
[ch
- 0x80];
1046 if ((hr
= read_more_data( reader
, len
, NULL
, NULL
)) != S_OK
) return hr
;
1047 end
= read_current_ptr( reader
) + len
+ 1;
1048 *ret
= ch
& utf8_mask
[len
];
1053 if ((ch
= end
[-3] ^ 0x80) >= 0x40) break;
1054 *ret
= (*ret
<< 6) | ch
;
1056 if ((ch
= end
[-2] ^ 0x80) >= 0x40) break;
1057 *ret
= (*ret
<< 6) | ch
;
1059 if ((ch
= end
[-1] ^ 0x80) >= 0x40) break;
1060 *ret
= (*ret
<< 6) | ch
;
1061 if (*ret
< utf8_minval
[len
]) break;
1066 return WS_E_INVALID_FORMAT
;
1069 static inline BOOL
read_isnamechar( unsigned int ch
)
1071 /* FIXME: incomplete */
1072 return (ch
>= 'A' && ch
<= 'Z') ||
1073 (ch
>= 'a' && ch
<= 'z') ||
1074 (ch
>= '0' && ch
<= '9') ||
1075 ch
== '_' || ch
== '-' || ch
== '.' || ch
== ':';
1078 static inline BOOL
read_isspace( unsigned int ch
)
1080 return ch
== ' ' || ch
== '\t' || ch
== '\r' || ch
== '\n';
1083 static inline void read_skip_whitespace( struct reader
*reader
)
1087 if (read_more_data( reader
, 1, NULL
, NULL
) != S_OK
|| !read_isspace( *read_current_ptr( reader
) )) break;
1088 read_skip( reader
, 1 );
1092 static inline HRESULT
read_cmp( struct reader
*reader
, const char *str
, int len
)
1094 const unsigned char *ptr
;
1097 if (len
< 0) len
= strlen( str
);
1098 if ((hr
= read_more_data( reader
, len
, NULL
, NULL
)) != S_OK
) return hr
;
1100 ptr
= read_current_ptr( reader
);
1103 if (*str
!= *ptr
) return WS_E_INVALID_FORMAT
;
1109 static HRESULT
read_xmldecl( struct reader
*reader
)
1113 if ((hr
= read_more_data( reader
, 1, NULL
, NULL
)) != S_OK
) return hr
;
1114 if (*read_current_ptr( reader
) != '<' || (hr
= read_cmp( reader
, "<?", 2 )) != S_OK
)
1116 reader
->state
= READER_STATE_BOF
;
1119 if ((hr
= read_cmp( reader
, "<?xml ", 6 )) != S_OK
) return hr
;
1120 read_skip( reader
, 6 );
1122 /* FIXME: parse attributes */
1125 if (read_more_data( reader
, 1, NULL
, NULL
) != S_OK
|| *read_current_ptr( reader
) == '?' ) break;
1126 read_skip( reader
, 1 );
1129 if ((hr
= read_cmp( reader
, "?>", 2 )) != S_OK
) return hr
;
1130 read_skip( reader
, 2 );
1132 reader
->state
= READER_STATE_BOF
;
1136 HRESULT
append_attribute( WS_XML_ELEMENT_NODE
*elem
, WS_XML_ATTRIBUTE
*attr
)
1138 if (elem
->attributeCount
)
1140 WS_XML_ATTRIBUTE
**tmp
;
1141 if (!(tmp
= heap_realloc( elem
->attributes
, (elem
->attributeCount
+ 1) * sizeof(attr
) )))
1142 return E_OUTOFMEMORY
;
1143 elem
->attributes
= tmp
;
1145 else if (!(elem
->attributes
= heap_alloc( sizeof(attr
) ))) return E_OUTOFMEMORY
;
1146 elem
->attributes
[elem
->attributeCount
++] = attr
;
1150 static inline void init_xml_string( BYTE
*bytes
, ULONG len
, WS_XML_STRING
*str
)
1154 str
->dictionary
= NULL
;
1158 static HRESULT
split_qname( const BYTE
*str
, ULONG len
, WS_XML_STRING
*prefix
, WS_XML_STRING
*localname
)
1160 BYTE
*prefix_bytes
= NULL
, *localname_bytes
= (BYTE
*)str
, *ptr
= (BYTE
*)str
;
1161 ULONG prefix_len
= 0, localname_len
= len
;
1167 if (ptr
== str
) return WS_E_INVALID_FORMAT
;
1168 prefix_bytes
= (BYTE
*)str
;
1169 prefix_len
= ptr
- str
;
1170 localname_bytes
= ptr
+ 1;
1171 localname_len
= len
;
1176 if (!localname_len
) return WS_E_INVALID_FORMAT
;
1178 init_xml_string( prefix_bytes
, prefix_len
, prefix
);
1179 init_xml_string( localname_bytes
, localname_len
, localname
);
1183 static HRESULT
parse_qname( const BYTE
*str
, ULONG len
, WS_XML_STRING
**prefix_ret
, WS_XML_STRING
**localname_ret
)
1185 WS_XML_STRING prefix
, localname
;
1188 if ((hr
= split_qname( str
, len
, &prefix
, &localname
)) != S_OK
) return hr
;
1189 if (!(*prefix_ret
= alloc_xml_string( NULL
, prefix
.length
))) return E_OUTOFMEMORY
;
1190 if (!(*localname_ret
= dup_xml_string( &localname
, FALSE
)))
1192 free_xml_string( *prefix_ret
);
1193 return E_OUTOFMEMORY
;
1195 memcpy( (*prefix_ret
)->bytes
, prefix
.bytes
, prefix
.length
);
1196 if (prefix
.length
&& add_xml_string( *prefix_ret
) != S_OK
) WARN( "prefix not added to dictionary\n" );
1200 static int codepoint_to_utf8( int cp
, unsigned char *dst
)
1210 dst
[1] = 0x80 | (cp
& 0x3f);
1215 if ((cp
>= 0xd800 && cp
<= 0xdfff) || cp
== 0xfffe || cp
== 0xffff) return -1;
1218 dst
[2] = 0x80 | (cp
& 0x3f);
1220 dst
[1] = 0x80 | (cp
& 0x3f);
1225 if (cp
>= 0x110000) return -1;
1226 dst
[3] = 0x80 | (cp
& 0x3f);
1228 dst
[2] = 0x80 | (cp
& 0x3f);
1230 dst
[1] = 0x80 | (cp
& 0x3f);
1236 static HRESULT
decode_text( const unsigned char *str
, ULONG len
, unsigned char *ret
, ULONG
*ret_len
)
1238 const unsigned char *p
= str
;
1239 unsigned char *q
= ret
;
1247 if (!len
) return WS_E_INVALID_FORMAT
;
1249 if (len
>= 3 && !memcmp( p
, "lt;", 3 ))
1255 else if (len
>= 3 && !memcmp( p
, "gt;", 3 ))
1261 else if (len
>= 5 && !memcmp( p
, "quot;", 5 ))
1267 else if (len
>= 4 && !memcmp( p
, "amp;", 4 ))
1273 else if (len
>= 5 && !memcmp( p
, "apos;", 5 ))
1281 ULONG start
, nb_digits
, i
;
1282 int len_utf8
, cp
= 0;
1285 if (!len
) return WS_E_INVALID_FORMAT
;
1291 while (len
&& isxdigit( *p
)) { p
++; len
--; };
1292 if (!len
) return WS_E_INVALID_FORMAT
;
1294 p
-= nb_digits
= start
- len
;
1295 if (!nb_digits
|| nb_digits
> 6 || p
[nb_digits
] != ';') return WS_E_INVALID_FORMAT
;
1296 for (i
= 0; i
< nb_digits
; i
++)
1299 if (*p
>= '0' && *p
<= '9') cp
+= *p
- '0';
1300 else if (*p
>= 'a' && *p
<= 'f') cp
+= *p
- 'a' + 10;
1301 else cp
+= *p
- 'A' + 10;
1305 else if (isdigit( *p
))
1307 while (len
&& *p
== '0') { p
++; len
--; };
1308 if (!len
) return WS_E_INVALID_FORMAT
;
1311 while (len
&& isdigit( *p
)) { p
++; len
--; };
1312 if (!len
) return WS_E_INVALID_FORMAT
;
1314 p
-= nb_digits
= start
- len
;
1315 if (!nb_digits
|| nb_digits
> 7 || p
[nb_digits
] != ';') return WS_E_INVALID_FORMAT
;
1316 for (i
= 0; i
< nb_digits
; i
++)
1323 else return WS_E_INVALID_FORMAT
;
1325 if ((len_utf8
= codepoint_to_utf8( cp
, q
)) < 0) return WS_E_INVALID_FORMAT
;
1326 *ret_len
+= len_utf8
;
1330 else return WS_E_INVALID_FORMAT
;
1342 static HRESULT
read_attribute_value_text( struct reader
*reader
, WS_XML_ATTRIBUTE
*attr
)
1344 WS_XML_UTF8_TEXT
*utf8
;
1345 unsigned int len
, ch
, skip
, quote
;
1346 const unsigned char *start
;
1349 read_skip_whitespace( reader
);
1350 if ((hr
= read_cmp( reader
, "=", 1 )) != S_OK
) return hr
;
1351 read_skip( reader
, 1 );
1353 read_skip_whitespace( reader
);
1354 if ((hr
= read_cmp( reader
, "\"", 1 )) != S_OK
&& (hr
= read_cmp( reader
, "'", 1 )) != S_OK
) return hr
;
1355 if ((hr
= read_utf8_char( reader
, "e
, &skip
)) != S_OK
) return hr
;
1356 read_skip( reader
, 1 );
1359 start
= read_current_ptr( reader
);
1362 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) return hr
;
1363 if (ch
== quote
) break;
1364 read_skip( reader
, skip
);
1367 read_skip( reader
, 1 );
1371 if (!(attr
->ns
= alloc_xml_string( start
, len
))) return E_OUTOFMEMORY
;
1372 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) return hr
;
1373 if (!(utf8
= alloc_utf8_text( NULL
, 0 ))) return E_OUTOFMEMORY
;
1377 if (!(utf8
= alloc_utf8_text( NULL
, len
))) return E_OUTOFMEMORY
;
1378 if ((hr
= decode_text( start
, len
, utf8
->value
.bytes
, &utf8
->value
.length
)) != S_OK
)
1385 attr
->value
= &utf8
->text
;
1386 attr
->singleQuote
= (quote
== '\'');
1390 static inline BOOL
is_text_type( unsigned char type
)
1392 return (type
>= RECORD_ZERO_TEXT
&& type
<= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT
);
1395 static HRESULT
read_int31( struct reader
*reader
, ULONG
*len
)
1400 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1402 if (!(byte
& 0x80)) return S_OK
;
1404 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1405 *len
+= (byte
& 0x7f) << 7;
1406 if (!(byte
& 0x80)) return S_OK
;
1408 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1409 *len
+= (byte
& 0x7f) << 14;
1410 if (!(byte
& 0x80)) return S_OK
;
1412 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1413 *len
+= (byte
& 0x7f) << 21;
1414 if (!(byte
& 0x80)) return S_OK
;
1416 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1417 *len
+= (byte
& 0x07) << 28;
1421 static HRESULT
read_string( struct reader
*reader
, WS_XML_STRING
**str
)
1425 if ((hr
= read_int31( reader
, &len
)) != S_OK
) return hr
;
1426 if (!(*str
= alloc_xml_string( NULL
, len
))) return E_OUTOFMEMORY
;
1427 if ((hr
= read_bytes( reader
, (*str
)->bytes
, len
)) == S_OK
)
1429 if (add_xml_string( *str
) != S_OK
) WARN( "string not added to dictionary\n" );
1432 free_xml_string( *str
);
1436 static HRESULT
read_dict_string( struct reader
*reader
, WS_XML_STRING
**str
)
1438 const WS_XML_DICTIONARY
*dict
;
1442 if ((hr
= read_int31( reader
, &id
)) != S_OK
) return hr
;
1443 dict
= (id
& 1) ? reader
->dict
: reader
->dict_static
;
1444 if (!dict
|| (id
>>= 1) >= dict
->stringCount
) return WS_E_INVALID_FORMAT
;
1445 if (!(*str
= alloc_xml_string( NULL
, 0 ))) return E_OUTOFMEMORY
;
1446 *(*str
) = dict
->strings
[id
];
1450 static HRESULT
read_datetime( struct reader
*reader
, WS_DATETIME
*ret
)
1455 if ((hr
= read_bytes( reader
, (unsigned char *)&val
, sizeof(val
) )) != S_OK
) return hr
;
1457 if ((val
& 0x03) == 1) ret
->format
= WS_DATETIME_FORMAT_UTC
;
1458 else if ((val
& 0x03) == 2) ret
->format
= WS_DATETIME_FORMAT_LOCAL
;
1459 else ret
->format
= WS_DATETIME_FORMAT_NONE
;
1461 if ((ret
->ticks
= val
>> 2) > TICKS_MAX
) return WS_E_INVALID_FORMAT
;
1465 static HRESULT
lookup_string( struct reader
*reader
, ULONG id
, const WS_XML_STRING
**ret
)
1467 const WS_XML_DICTIONARY
*dict
= (id
& 1) ? reader
->dict
: reader
->dict_static
;
1468 if (!dict
|| (id
>>= 1) >= dict
->stringCount
) return WS_E_INVALID_FORMAT
;
1469 *ret
= &dict
->strings
[id
];
1473 static HRESULT
read_attribute_value_bin( struct reader
*reader
, WS_XML_ATTRIBUTE
*attr
)
1475 WS_XML_UTF8_TEXT
*text_utf8
= NULL
;
1476 WS_XML_BASE64_TEXT
*text_base64
= NULL
;
1477 WS_XML_INT32_TEXT
*text_int32
;
1478 WS_XML_INT64_TEXT
*text_int64
;
1479 WS_XML_BOOL_TEXT
*text_bool
;
1480 const WS_XML_STRING
*str
;
1489 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
1490 if (!is_text_type( type
)) return WS_E_INVALID_FORMAT
;
1494 case RECORD_ZERO_TEXT
:
1496 if (!(text_int32
= alloc_int32_text( 0 ))) return E_OUTOFMEMORY
;
1497 attr
->value
= &text_int32
->text
;
1500 case RECORD_ONE_TEXT
:
1502 if (!(text_int32
= alloc_int32_text( 1 ))) return E_OUTOFMEMORY
;
1503 attr
->value
= &text_int32
->text
;
1506 case RECORD_FALSE_TEXT
:
1508 if (!(text_bool
= alloc_bool_text( FALSE
))) return E_OUTOFMEMORY
;
1509 attr
->value
= &text_bool
->text
;
1512 case RECORD_TRUE_TEXT
:
1514 if (!(text_bool
= alloc_bool_text( TRUE
))) return E_OUTOFMEMORY
;
1515 attr
->value
= &text_bool
->text
;
1518 case RECORD_INT8_TEXT
:
1521 if ((hr
= read_byte( reader
, (unsigned char *)&val_int8
)) != S_OK
) return hr
;
1522 if (!(text_int64
= alloc_int64_text( val_int8
))) return E_OUTOFMEMORY
;
1523 attr
->value
= &text_int64
->text
;
1526 case RECORD_INT16_TEXT
:
1529 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int16
, sizeof(val_int16
) )) != S_OK
) return hr
;
1530 if (!(text_int64
= alloc_int64_text( val_int16
))) return E_OUTOFMEMORY
;
1531 attr
->value
= &text_int64
->text
;
1534 case RECORD_INT32_TEXT
:
1535 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
1536 if (!(text_int64
= alloc_int64_text( val_int32
))) return E_OUTOFMEMORY
;
1537 attr
->value
= &text_int64
->text
;
1540 case RECORD_INT64_TEXT
:
1543 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int64
, sizeof(val_int64
) )) != S_OK
) return hr
;
1544 if (!(text_int64
= alloc_int64_text( val_int64
))) return E_OUTOFMEMORY
;
1545 attr
->value
= &text_int64
->text
;
1548 case RECORD_FLOAT_TEXT
:
1550 WS_XML_FLOAT_TEXT
*text_float
;
1553 if ((hr
= read_bytes( reader
, (unsigned char *)&val_float
, sizeof(val_float
) )) != S_OK
) return hr
;
1554 if (!(text_float
= alloc_float_text( val_float
))) return E_OUTOFMEMORY
;
1555 attr
->value
= &text_float
->text
;
1558 case RECORD_DOUBLE_TEXT
:
1560 WS_XML_DOUBLE_TEXT
*text_double
;
1563 if ((hr
= read_bytes( reader
, (unsigned char *)&val_double
, sizeof(val_double
) )) != S_OK
) return hr
;
1564 if (!(text_double
= alloc_double_text( val_double
))) return E_OUTOFMEMORY
;
1565 attr
->value
= &text_double
->text
;
1568 case RECORD_DATETIME_TEXT
:
1570 WS_XML_DATETIME_TEXT
*text_datetime
;
1571 WS_DATETIME datetime
;
1573 if ((hr
= read_datetime( reader
, &datetime
)) != S_OK
) return hr
;
1574 if (!(text_datetime
= alloc_datetime_text( &datetime
))) return E_OUTOFMEMORY
;
1575 attr
->value
= &text_datetime
->text
;
1578 case RECORD_CHARS8_TEXT
:
1579 if ((hr
= read_byte( reader
, (unsigned char *)&val_uint8
)) != S_OK
) return hr
;
1583 case RECORD_CHARS16_TEXT
:
1584 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint16
, sizeof(val_uint16
) )) != S_OK
) return hr
;
1588 case RECORD_CHARS32_TEXT
:
1589 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
1590 if (val_int32
< 0) return WS_E_INVALID_FORMAT
;
1594 case RECORD_BYTES8_TEXT
:
1595 if ((hr
= read_byte( reader
, (unsigned char *)&val_uint8
)) != S_OK
) return hr
;
1596 if (!(text_base64
= alloc_base64_text( NULL
, val_uint8
))) return E_OUTOFMEMORY
;
1597 if ((hr
= read_bytes( reader
, text_base64
->bytes
, val_uint8
)) != S_OK
)
1599 heap_free( text_base64
);
1604 case RECORD_BYTES16_TEXT
:
1605 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint16
, sizeof(val_uint16
) )) != S_OK
) return hr
;
1606 if (!(text_base64
= alloc_base64_text( NULL
, val_uint16
))) return E_OUTOFMEMORY
;
1607 if ((hr
= read_bytes( reader
, text_base64
->bytes
, val_uint16
)) != S_OK
)
1609 heap_free( text_base64
);
1614 case RECORD_BYTES32_TEXT
:
1615 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
1616 if (val_int32
< 0) return WS_E_INVALID_FORMAT
;
1617 if (!(text_base64
= alloc_base64_text( NULL
, val_int32
))) return E_OUTOFMEMORY
;
1618 if ((hr
= read_bytes( reader
, text_base64
->bytes
, val_int32
)) != S_OK
)
1620 heap_free( text_base64
);
1625 case RECORD_EMPTY_TEXT
:
1628 case RECORD_DICTIONARY_TEXT
:
1629 if ((hr
= read_int31( reader
, &id
)) != S_OK
) return hr
;
1630 if ((hr
= lookup_string( reader
, id
, &str
)) != S_OK
) return hr
;
1631 if (!(text_utf8
= alloc_utf8_text( str
->bytes
, str
->length
))) return E_OUTOFMEMORY
;
1634 case RECORD_UNIQUE_ID_TEXT
:
1636 WS_XML_UNIQUE_ID_TEXT
*text_unique_id
;
1637 if ((hr
= read_bytes( reader
, (unsigned char *)&guid
, sizeof(guid
) )) != S_OK
) return hr
;
1638 if (!(text_unique_id
= alloc_unique_id_text( &guid
))) return E_OUTOFMEMORY
;
1639 attr
->value
= &text_unique_id
->text
;
1642 case RECORD_GUID_TEXT
:
1644 WS_XML_GUID_TEXT
*guid_text
;
1645 if ((hr
= read_bytes( reader
, (unsigned char *)&guid
, sizeof(guid
) )) != S_OK
) return hr
;
1646 if (!(guid_text
= alloc_guid_text( &guid
))) return E_OUTOFMEMORY
;
1647 attr
->value
= &guid_text
->text
;
1650 case RECORD_UINT64_TEXT
:
1652 WS_XML_UINT64_TEXT
*text_uint64
;
1655 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint64
, sizeof(val_uint64
) )) != S_OK
) return hr
;
1656 if (!(text_uint64
= alloc_uint64_text( val_uint64
))) return E_OUTOFMEMORY
;
1657 attr
->value
= &text_uint64
->text
;
1660 case RECORD_BOOL_TEXT
:
1662 WS_XML_BOOL_TEXT
*text_bool
;
1665 if ((hr
= read_bytes( reader
, (unsigned char *)&val_bool
, sizeof(val_bool
) )) != S_OK
) return hr
;
1666 if (!(text_bool
= alloc_bool_text( !!val_bool
))) return E_OUTOFMEMORY
;
1667 attr
->value
= &text_bool
->text
;
1671 ERR( "unhandled record type %02x\n", type
);
1672 return WS_E_NOT_SUPPORTED
;
1675 if (type
>= RECORD_BYTES8_TEXT
&& type
<= RECORD_BYTES32_TEXT
)
1677 attr
->value
= &text_base64
->text
;
1683 if (!(text_utf8
= alloc_utf8_text( NULL
, len
))) return E_OUTOFMEMORY
;
1684 if (!len
) text_utf8
->value
.bytes
= (BYTE
*)(text_utf8
+ 1); /* quirk */
1685 if ((hr
= read_bytes( reader
, text_utf8
->value
.bytes
, len
)) != S_OK
)
1687 heap_free( text_utf8
);
1692 attr
->value
= &text_utf8
->text
;
1696 static HRESULT
read_attribute_text( struct reader
*reader
, WS_XML_ATTRIBUTE
**ret
)
1698 static const WS_XML_STRING xmlns
= {5, (BYTE
*)"xmlns"};
1699 WS_XML_ATTRIBUTE
*attr
;
1700 unsigned int len
= 0, ch
, skip
;
1701 const unsigned char *start
;
1702 WS_XML_STRING
*prefix
, *localname
;
1705 if (!(attr
= heap_alloc_zero( sizeof(*attr
) ))) return E_OUTOFMEMORY
;
1707 start
= read_current_ptr( reader
);
1710 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) goto error
;
1711 if (!read_isnamechar( ch
)) break;
1712 read_skip( reader
, skip
);
1717 hr
= WS_E_INVALID_FORMAT
;
1721 if ((hr
= parse_qname( start
, len
, &prefix
, &localname
)) != S_OK
) goto error
;
1722 if (WsXmlStringEquals( prefix
, &xmlns
, NULL
) == S_OK
)
1724 free_xml_string( prefix
);
1726 if (!(attr
->prefix
= alloc_xml_string( localname
->bytes
, localname
->length
)))
1728 free_xml_string( localname
);
1732 attr
->localName
= localname
;
1734 else if (!prefix
->length
&& WsXmlStringEquals( localname
, &xmlns
, NULL
) == S_OK
)
1737 attr
->prefix
= prefix
;
1738 attr
->localName
= localname
;
1742 attr
->prefix
= prefix
;
1743 attr
->localName
= localname
;
1746 if ((hr
= read_attribute_value_text( reader
, attr
)) != S_OK
) goto error
;
1752 free_attribute( attr
);
1756 static inline BOOL
is_attribute_type( unsigned char type
)
1758 return (type
>= RECORD_SHORT_ATTRIBUTE
&& type
<= RECORD_PREFIX_ATTRIBUTE_Z
);
1761 static WS_XML_STRING
*get_xmlns_localname( struct reader
*reader
, const WS_XML_STRING
*prefix
)
1763 if (!get_namespace( reader
, prefix
)) return alloc_xml_string( NULL
, 0 );
1764 return alloc_xml_string( prefix
->bytes
, prefix
->length
);
1767 static HRESULT
read_attribute_bin( struct reader
*reader
, WS_XML_ATTRIBUTE
**ret
)
1769 WS_XML_UTF8_TEXT
*utf8
;
1770 WS_XML_ATTRIBUTE
*attr
;
1771 unsigned char type
= 0;
1774 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
1775 if (!is_attribute_type( type
)) return WS_E_INVALID_FORMAT
;
1776 if (!(attr
= heap_alloc_zero( sizeof(*attr
) ))) return E_OUTOFMEMORY
;
1778 if (type
>= RECORD_PREFIX_ATTRIBUTE_A
&& type
<= RECORD_PREFIX_ATTRIBUTE_Z
)
1780 unsigned char ch
= type
- RECORD_PREFIX_ATTRIBUTE_A
+ 'a';
1781 if (!(attr
->prefix
= alloc_xml_string( &ch
, 1 )))
1786 if ((hr
= read_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1787 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1789 else if (type
>= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A
&& type
<= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_Z
)
1791 unsigned char ch
= type
- RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A
+ 'a';
1792 if (!(attr
->prefix
= alloc_xml_string( &ch
, 1 )))
1797 if ((hr
= read_dict_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1798 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1804 case RECORD_SHORT_ATTRIBUTE
:
1805 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1810 if ((hr
= read_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1811 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1814 case RECORD_ATTRIBUTE
:
1815 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1816 if ((hr
= read_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1817 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1820 case RECORD_SHORT_DICTIONARY_ATTRIBUTE
:
1821 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1826 if ((hr
= read_dict_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1827 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1830 case RECORD_DICTIONARY_ATTRIBUTE
:
1831 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1832 if ((hr
= read_dict_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1833 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1836 case RECORD_SHORT_XMLNS_ATTRIBUTE
:
1837 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1842 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1847 if ((hr
= read_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1848 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1852 case RECORD_XMLNS_ATTRIBUTE
:
1853 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1854 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1859 if ((hr
= read_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1860 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1864 case RECORD_SHORT_DICTIONARY_XMLNS_ATTRIBUTE
:
1865 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1870 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1875 if ((hr
= read_dict_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1876 if (!(utf8
= alloc_utf8_text( NULL
, 0 )))
1881 attr
->value
= &utf8
->text
;
1882 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1886 case RECORD_DICTIONARY_XMLNS_ATTRIBUTE
:
1887 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1888 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1893 if ((hr
= read_dict_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1894 if (!(utf8
= alloc_utf8_text( NULL
, 0 )))
1899 attr
->value
= &utf8
->text
;
1900 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1905 ERR( "unhandled record type %02x\n", type
);
1906 return WS_E_NOT_SUPPORTED
;
1914 free_attribute( attr
);
1918 static inline struct node
*find_parent( struct reader
*reader
)
1920 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_END_ELEMENT
)
1922 if (is_valid_parent( reader
->current
->parent
->parent
)) return reader
->current
->parent
->parent
;
1925 if (is_valid_parent( reader
->current
)) return reader
->current
;
1926 if (is_valid_parent( reader
->current
->parent
)) return reader
->current
->parent
;
1930 static HRESULT
set_namespaces( struct reader
*reader
, WS_XML_ELEMENT_NODE
*elem
)
1932 static const WS_XML_STRING xml
= {3, (BYTE
*)"xml"};
1933 const WS_XML_STRING
*ns
;
1936 if (!(ns
= get_namespace( reader
, elem
->prefix
))) return WS_E_INVALID_FORMAT
;
1937 if (!(elem
->ns
= dup_xml_string( ns
, FALSE
))) return E_OUTOFMEMORY
;
1939 for (i
= 0; i
< elem
->attributeCount
; i
++)
1941 WS_XML_ATTRIBUTE
*attr
= elem
->attributes
[i
];
1942 if (attr
->isXmlNs
|| WsXmlStringEquals( attr
->prefix
, &xml
, NULL
) == S_OK
) continue;
1943 if (!(ns
= get_namespace( reader
, attr
->prefix
))) return WS_E_INVALID_FORMAT
;
1944 if (!(attr
->ns
= alloc_xml_string( NULL
, ns
->length
))) return E_OUTOFMEMORY
;
1945 if (attr
->ns
->length
) memcpy( attr
->ns
->bytes
, ns
->bytes
, ns
->length
);
1950 static WS_XML_ELEMENT_NODE
*alloc_element_pair(void)
1952 struct node
*node
, *end
;
1953 if (!(node
= alloc_node( WS_XML_NODE_TYPE_ELEMENT
))) return NULL
;
1954 if (!(end
= alloc_node( WS_XML_NODE_TYPE_END_ELEMENT
)))
1959 list_add_tail( &node
->children
, &end
->entry
);
1964 static HRESULT
read_attributes_text( struct reader
*reader
, WS_XML_ELEMENT_NODE
*elem
)
1966 WS_XML_ATTRIBUTE
*attr
;
1969 reader
->current_attr
= 0;
1972 read_skip_whitespace( reader
);
1973 if (read_cmp( reader
, ">", 1 ) == S_OK
|| read_cmp( reader
, "/>", 2 ) == S_OK
) break;
1974 if ((hr
= read_attribute_text( reader
, &attr
)) != S_OK
) return hr
;
1975 if ((hr
= append_attribute( elem
, attr
)) != S_OK
)
1977 free_attribute( attr
);
1980 reader
->current_attr
++;
1985 static HRESULT
read_element_text( struct reader
*reader
)
1987 unsigned int len
= 0, ch
, skip
;
1988 const unsigned char *start
;
1989 unsigned char buf
[2];
1990 struct node
*node
= NULL
, *parent
;
1991 WS_XML_ELEMENT_NODE
*elem
;
1994 if (read_end_of_data( reader
))
1996 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
1997 reader
->last
= reader
->current
;
1998 reader
->state
= READER_STATE_EOF
;
2002 if ((hr
= read_peek( reader
, buf
, 2 )) != S_OK
) return hr
;
2003 if (buf
[0] != '<' || !read_isnamechar( buf
[1] )) return WS_E_INVALID_FORMAT
;
2004 read_skip( reader
, 1 );
2006 if (!(elem
= alloc_element_pair())) return E_OUTOFMEMORY
;
2007 node
= (struct node
*)elem
;
2009 start
= read_current_ptr( reader
);
2012 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) goto error
;
2013 if (!read_isnamechar( ch
)) break;
2014 read_skip( reader
, skip
);
2019 hr
= WS_E_INVALID_FORMAT
;
2023 if (!(parent
= find_parent( reader
))) goto error
;
2024 if ((hr
= parse_qname( start
, len
, &elem
->prefix
, &elem
->localName
)) != S_OK
) goto error
;
2025 if ((hr
= read_attributes_text( reader
, elem
)) != S_OK
) goto error
;
2026 if ((hr
= set_namespaces( reader
, elem
)) != S_OK
) goto error
;
2028 read_insert_node( reader
, parent
, node
);
2029 reader
->state
= READER_STATE_STARTELEMENT
;
2033 destroy_nodes( node
);
2037 static inline BOOL
is_element_type( unsigned char type
)
2039 return (type
>= RECORD_SHORT_ELEMENT
&& type
<= RECORD_PREFIX_ELEMENT_Z
);
2042 static HRESULT
read_attributes_bin( struct reader
*reader
, WS_XML_ELEMENT_NODE
*elem
)
2044 WS_XML_ATTRIBUTE
*attr
;
2048 reader
->current_attr
= 0;
2051 if ((hr
= read_peek( reader
, &type
, 1 )) != S_OK
) return hr
;
2052 if (!is_attribute_type( type
)) break;
2053 if ((hr
= read_attribute_bin( reader
, &attr
)) != S_OK
) return hr
;
2054 if ((hr
= append_attribute( elem
, attr
)) != S_OK
)
2056 free_attribute( attr
);
2059 reader
->current_attr
++;
2064 static HRESULT
read_element_bin( struct reader
*reader
)
2066 struct node
*node
= NULL
, *parent
;
2067 WS_XML_ELEMENT_NODE
*elem
;
2071 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2072 if (!is_element_type( type
)) return WS_E_INVALID_FORMAT
;
2074 if (!(elem
= alloc_element_pair())) return E_OUTOFMEMORY
;
2075 node
= (struct node
*)elem
;
2077 if (type
>= RECORD_PREFIX_ELEMENT_A
&& type
<= RECORD_PREFIX_ELEMENT_Z
)
2079 unsigned char ch
= type
- RECORD_PREFIX_ELEMENT_A
+ 'a';
2080 if (!(elem
->prefix
= alloc_xml_string( &ch
, 1 )))
2085 if ((hr
= read_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2087 else if (type
>= RECORD_PREFIX_DICTIONARY_ELEMENT_A
&& type
<= RECORD_PREFIX_DICTIONARY_ELEMENT_Z
)
2089 unsigned char ch
= type
- RECORD_PREFIX_DICTIONARY_ELEMENT_A
+ 'a';
2090 if (!(elem
->prefix
= alloc_xml_string( &ch
, 1 )))
2095 if ((hr
= read_dict_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2101 case RECORD_SHORT_ELEMENT
:
2102 if (!(elem
->prefix
= alloc_xml_string( NULL
, 0 )))
2107 if ((hr
= read_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2110 case RECORD_ELEMENT
:
2111 if ((hr
= read_string( reader
, &elem
->prefix
)) != S_OK
) goto error
;
2112 if ((hr
= read_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2115 case RECORD_SHORT_DICTIONARY_ELEMENT
:
2116 if (!(elem
->prefix
= alloc_xml_string( NULL
, 0 )))
2121 if ((hr
= read_dict_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2124 case RECORD_DICTIONARY_ELEMENT
:
2125 if ((hr
= read_string( reader
, &elem
->prefix
)) != S_OK
) goto error
;
2126 if ((hr
= read_dict_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2130 ERR( "unhandled record type %02x\n", type
);
2131 return WS_E_NOT_SUPPORTED
;
2135 if (!(parent
= find_parent( reader
)))
2137 hr
= WS_E_INVALID_FORMAT
;
2141 if ((hr
= read_attributes_bin( reader
, elem
)) != S_OK
) goto error
;
2142 if ((hr
= set_namespaces( reader
, elem
)) != S_OK
) goto error
;
2144 read_insert_node( reader
, parent
, node
);
2145 reader
->state
= READER_STATE_STARTELEMENT
;
2149 destroy_nodes( node
);
2153 static HRESULT
read_text_text( struct reader
*reader
)
2155 unsigned int len
= 0, ch
, skip
;
2156 const unsigned char *start
;
2157 struct node
*node
, *parent
;
2158 WS_XML_TEXT_NODE
*text
;
2159 WS_XML_UTF8_TEXT
*utf8
;
2162 start
= read_current_ptr( reader
);
2165 if (read_end_of_data( reader
)) break;
2166 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) return hr
;
2167 if (ch
== '<') break;
2168 read_skip( reader
, skip
);
2172 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2174 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return E_OUTOFMEMORY
;
2175 text
= (WS_XML_TEXT_NODE
*)node
;
2176 if (!(utf8
= alloc_utf8_text( NULL
, len
)))
2179 return E_OUTOFMEMORY
;
2181 if ((hr
= decode_text( start
, len
, utf8
->value
.bytes
, &utf8
->value
.length
)) != S_OK
)
2187 text
->text
= &utf8
->text
;
2189 read_insert_node( reader
, parent
, node
);
2190 reader
->state
= READER_STATE_TEXT
;
2191 reader
->text_conv_offset
= 0;
2195 static struct node
*alloc_utf8_text_node( const BYTE
*data
, ULONG len
, WS_XML_UTF8_TEXT
**ret
)
2198 WS_XML_UTF8_TEXT
*utf8
;
2199 WS_XML_TEXT_NODE
*text
;
2201 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2202 if (!(utf8
= alloc_utf8_text( data
, len
)))
2207 text
= (WS_XML_TEXT_NODE
*)node
;
2208 text
->text
= &utf8
->text
;
2209 if (ret
) *ret
= utf8
;
2213 static struct node
*alloc_base64_text_node( const BYTE
*data
, ULONG len
, WS_XML_BASE64_TEXT
**ret
)
2216 WS_XML_BASE64_TEXT
*base64
;
2217 WS_XML_TEXT_NODE
*text
;
2219 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2220 if (!(base64
= alloc_base64_text( data
, len
)))
2225 text
= (WS_XML_TEXT_NODE
*)node
;
2226 text
->text
= &base64
->text
;
2227 if (ret
) *ret
= base64
;
2231 static struct node
*alloc_bool_text_node( BOOL value
)
2234 WS_XML_BOOL_TEXT
*text_bool
;
2235 WS_XML_TEXT_NODE
*text
;
2237 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2238 if (!(text_bool
= alloc_bool_text( value
)))
2243 text
= (WS_XML_TEXT_NODE
*)node
;
2244 text
->text
= &text_bool
->text
;
2248 static struct node
*alloc_int32_text_node( INT32 value
)
2251 WS_XML_INT32_TEXT
*text_int32
;
2252 WS_XML_TEXT_NODE
*text
;
2254 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2255 if (!(text_int32
= alloc_int32_text( value
)))
2260 text
= (WS_XML_TEXT_NODE
*)node
;
2261 text
->text
= &text_int32
->text
;
2265 static struct node
*alloc_int64_text_node( INT64 value
)
2268 WS_XML_INT64_TEXT
*text_int64
;
2269 WS_XML_TEXT_NODE
*text
;
2271 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2272 if (!(text_int64
= alloc_int64_text( value
)))
2277 text
= (WS_XML_TEXT_NODE
*)node
;
2278 text
->text
= &text_int64
->text
;
2282 static struct node
*alloc_float_text_node( float value
)
2285 WS_XML_FLOAT_TEXT
*text_float
;
2286 WS_XML_TEXT_NODE
*text
;
2288 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2289 if (!(text_float
= alloc_float_text( value
)))
2294 text
= (WS_XML_TEXT_NODE
*)node
;
2295 text
->text
= &text_float
->text
;
2299 static struct node
*alloc_double_text_node( double value
)
2302 WS_XML_DOUBLE_TEXT
*text_double
;
2303 WS_XML_TEXT_NODE
*text
;
2305 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2306 if (!(text_double
= alloc_double_text( value
)))
2311 text
= (WS_XML_TEXT_NODE
*)node
;
2312 text
->text
= &text_double
->text
;
2316 static struct node
*alloc_datetime_text_node( const WS_DATETIME
*value
)
2319 WS_XML_DATETIME_TEXT
*text_datetime
;
2320 WS_XML_TEXT_NODE
*text
;
2322 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2323 if (!(text_datetime
= alloc_datetime_text( value
)))
2328 text
= (WS_XML_TEXT_NODE
*)node
;
2329 text
->text
= &text_datetime
->text
;
2333 static struct node
*alloc_unique_id_text_node( const GUID
*value
)
2336 WS_XML_UNIQUE_ID_TEXT
*text_unique_id
;
2337 WS_XML_TEXT_NODE
*text
;
2339 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2340 if (!(text_unique_id
= alloc_unique_id_text( value
)))
2345 text
= (WS_XML_TEXT_NODE
*)node
;
2346 text
->text
= &text_unique_id
->text
;
2350 static struct node
*alloc_guid_text_node( const GUID
*value
)
2353 WS_XML_GUID_TEXT
*text_guid
;
2354 WS_XML_TEXT_NODE
*text
;
2356 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2357 if (!(text_guid
= alloc_guid_text( value
)))
2362 text
= (WS_XML_TEXT_NODE
*)node
;
2363 text
->text
= &text_guid
->text
;
2367 static struct node
*alloc_uint64_text_node( UINT64 value
)
2370 WS_XML_UINT64_TEXT
*text_uint64
;
2371 WS_XML_TEXT_NODE
*text
;
2373 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2374 if (!(text_uint64
= alloc_uint64_text( value
)))
2379 text
= (WS_XML_TEXT_NODE
*)node
;
2380 text
->text
= &text_uint64
->text
;
2384 static HRESULT
append_text_bytes( struct reader
*reader
, WS_XML_TEXT_NODE
*node
, ULONG len
)
2386 WS_XML_BASE64_TEXT
*new, *old
= (WS_XML_BASE64_TEXT
*)node
->text
;
2389 if (!(new = alloc_base64_text( NULL
, old
->length
+ len
))) return E_OUTOFMEMORY
;
2390 memcpy( new->bytes
, old
->bytes
, old
->length
);
2391 if ((hr
= read_bytes( reader
, new->bytes
+ old
->length
, len
)) != S_OK
) return hr
;
2393 node
->text
= &new->text
;
2397 static HRESULT
read_text_bytes( struct reader
*reader
, unsigned char type
)
2399 struct node
*node
= NULL
, *parent
;
2400 WS_XML_BASE64_TEXT
*base64
;
2404 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2409 case RECORD_BYTES8_TEXT
:
2410 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT
:
2413 if ((hr
= read_byte( reader
, (unsigned char *)&len_uint8
)) != S_OK
) goto error
;
2417 case RECORD_BYTES16_TEXT
:
2418 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT
:
2421 if ((hr
= read_bytes( reader
, (unsigned char *)&len_uint16
, sizeof(len_uint16
) )) != S_OK
) goto error
;
2425 case RECORD_BYTES32_TEXT
:
2426 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT
:
2429 if ((hr
= read_bytes( reader
, (unsigned char *)&len_int32
, sizeof(len_int32
) )) != S_OK
) goto error
;
2432 hr
= WS_E_INVALID_FORMAT
;
2439 ERR( "unexpected type %u\n", type
);
2446 if (!(node
= alloc_base64_text_node( NULL
, len
, &base64
))) return E_OUTOFMEMORY
;
2447 if ((hr
= read_bytes( reader
, base64
->bytes
, len
)) != S_OK
) goto error
;
2449 else if ((hr
= append_text_bytes( reader
, (WS_XML_TEXT_NODE
*)node
, len
)) != S_OK
) goto error
;
2453 node
->flags
|= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
;
2456 if ((hr
= read_peek( reader
, &type
, 1 )) != S_OK
) goto error
;
2457 if (type
< RECORD_BYTES8_TEXT
|| type
> RECORD_BYTES32_TEXT_WITH_ENDELEMENT
) break;
2458 read_skip( reader
, 1 );
2461 read_insert_node( reader
, parent
, node
);
2462 reader
->state
= READER_STATE_TEXT
;
2463 reader
->text_conv_offset
= 0;
2471 static HRESULT
read_text_bin( struct reader
*reader
)
2473 struct node
*node
= NULL
, *parent
;
2475 WS_XML_UTF8_TEXT
*utf8
;
2483 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2484 if (!is_text_type( type
) || !(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2488 case RECORD_ZERO_TEXT
:
2489 case RECORD_ZERO_TEXT_WITH_ENDELEMENT
:
2490 if (!(node
= alloc_int32_text_node( 0 ))) return E_OUTOFMEMORY
;
2493 case RECORD_ONE_TEXT
:
2494 case RECORD_ONE_TEXT_WITH_ENDELEMENT
:
2495 if (!(node
= alloc_int32_text_node( 1 ))) return E_OUTOFMEMORY
;
2498 case RECORD_FALSE_TEXT
:
2499 case RECORD_FALSE_TEXT_WITH_ENDELEMENT
:
2500 if (!(node
= alloc_bool_text_node( FALSE
))) return E_OUTOFMEMORY
;
2503 case RECORD_TRUE_TEXT
:
2504 case RECORD_TRUE_TEXT_WITH_ENDELEMENT
:
2505 if (!(node
= alloc_bool_text_node( TRUE
))) return E_OUTOFMEMORY
;
2508 case RECORD_INT8_TEXT
:
2509 case RECORD_INT8_TEXT_WITH_ENDELEMENT
:
2512 if ((hr
= read_byte( reader
, (unsigned char *)&val_int8
)) != S_OK
) return hr
;
2513 if (!(node
= alloc_int32_text_node( val_int8
))) return E_OUTOFMEMORY
;
2516 case RECORD_INT16_TEXT
:
2517 case RECORD_INT16_TEXT_WITH_ENDELEMENT
:
2520 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int16
, sizeof(val_int16
) )) != S_OK
) return hr
;
2521 if (!(node
= alloc_int32_text_node( val_int16
))) return E_OUTOFMEMORY
;
2524 case RECORD_INT32_TEXT
:
2525 case RECORD_INT32_TEXT_WITH_ENDELEMENT
:
2526 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
2527 if (!(node
= alloc_int32_text_node( val_int32
))) return E_OUTOFMEMORY
;
2530 case RECORD_INT64_TEXT
:
2531 case RECORD_INT64_TEXT_WITH_ENDELEMENT
:
2534 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int64
, sizeof(val_int64
) )) != S_OK
) return hr
;
2535 if (!(node
= alloc_int64_text_node( val_int64
))) return E_OUTOFMEMORY
;
2538 case RECORD_FLOAT_TEXT
:
2539 case RECORD_FLOAT_TEXT_WITH_ENDELEMENT
:
2542 if ((hr
= read_bytes( reader
, (unsigned char *)&val_float
, sizeof(val_float
) )) != S_OK
) return hr
;
2543 if (!(node
= alloc_float_text_node( val_float
))) return E_OUTOFMEMORY
;
2546 case RECORD_DOUBLE_TEXT
:
2547 case RECORD_DOUBLE_TEXT_WITH_ENDELEMENT
:
2550 if ((hr
= read_bytes( reader
, (unsigned char *)&val_double
, sizeof(val_double
) )) != S_OK
) return hr
;
2551 if (!(node
= alloc_double_text_node( val_double
))) return E_OUTOFMEMORY
;
2554 case RECORD_DATETIME_TEXT
:
2555 case RECORD_DATETIME_TEXT_WITH_ENDELEMENT
:
2557 WS_DATETIME datetime
;
2558 if ((hr
= read_datetime( reader
, &datetime
)) != S_OK
) return hr
;
2559 if (!(node
= alloc_datetime_text_node( &datetime
))) return E_OUTOFMEMORY
;
2562 case RECORD_CHARS8_TEXT
:
2563 case RECORD_CHARS8_TEXT_WITH_ENDELEMENT
:
2564 if ((hr
= read_byte( reader
, (unsigned char *)&val_uint8
)) != S_OK
) return hr
;
2568 case RECORD_CHARS16_TEXT
:
2569 case RECORD_CHARS16_TEXT_WITH_ENDELEMENT
:
2570 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint16
, sizeof(val_uint16
) )) != S_OK
) return hr
;
2574 case RECORD_CHARS32_TEXT
:
2575 case RECORD_CHARS32_TEXT_WITH_ENDELEMENT
:
2576 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
2577 if (val_int32
< 0) return WS_E_INVALID_FORMAT
;
2581 case RECORD_BYTES8_TEXT
:
2582 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT
:
2583 case RECORD_BYTES16_TEXT
:
2584 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT
:
2585 case RECORD_BYTES32_TEXT
:
2586 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT
:
2587 return read_text_bytes( reader
, type
);
2589 case RECORD_EMPTY_TEXT
:
2590 case RECORD_EMPTY_TEXT_WITH_ENDELEMENT
:
2594 case RECORD_DICTIONARY_TEXT
:
2595 case RECORD_DICTIONARY_TEXT_WITH_ENDELEMENT
:
2597 const WS_XML_STRING
*str
;
2598 if ((hr
= read_int31( reader
, &id
)) != S_OK
) return hr
;
2599 if ((hr
= lookup_string( reader
, id
, &str
)) != S_OK
) return hr
;
2600 if (!(node
= alloc_utf8_text_node( str
->bytes
, str
->length
, NULL
))) return E_OUTOFMEMORY
;
2603 case RECORD_UNIQUE_ID_TEXT
:
2604 case RECORD_UNIQUE_ID_TEXT_WITH_ENDELEMENT
:
2605 if ((hr
= read_bytes( reader
, (unsigned char *)&uuid
, sizeof(uuid
) )) != S_OK
) return hr
;
2606 if (!(node
= alloc_unique_id_text_node( &uuid
))) return E_OUTOFMEMORY
;
2609 case RECORD_GUID_TEXT
:
2610 case RECORD_GUID_TEXT_WITH_ENDELEMENT
:
2611 if ((hr
= read_bytes( reader
, (unsigned char *)&uuid
, sizeof(uuid
) )) != S_OK
) return hr
;
2612 if (!(node
= alloc_guid_text_node( &uuid
))) return E_OUTOFMEMORY
;
2615 case RECORD_UINT64_TEXT
:
2616 case RECORD_UINT64_TEXT_WITH_ENDELEMENT
:
2619 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint64
, sizeof(val_uint64
) )) != S_OK
) return hr
;
2620 if (!(node
= alloc_uint64_text_node( val_uint64
))) return E_OUTOFMEMORY
;
2623 case RECORD_BOOL_TEXT
:
2624 case RECORD_BOOL_TEXT_WITH_ENDELEMENT
:
2627 if ((hr
= read_bytes( reader
, (unsigned char *)&val_bool
, sizeof(val_bool
) )) != S_OK
) return hr
;
2628 if (!(node
= alloc_bool_text_node( !!val_bool
))) return E_OUTOFMEMORY
;
2632 ERR( "unhandled record type %02x\n", type
);
2633 return WS_E_NOT_SUPPORTED
;
2638 if (!(node
= alloc_utf8_text_node( NULL
, len
, &utf8
))) return E_OUTOFMEMORY
;
2639 if (!len
) utf8
->value
.bytes
= (BYTE
*)(utf8
+ 1); /* quirk */
2640 if ((hr
= read_bytes( reader
, utf8
->value
.bytes
, len
)) != S_OK
)
2647 if (type
& 1) node
->flags
|= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
;
2648 read_insert_node( reader
, parent
, node
);
2649 reader
->state
= READER_STATE_TEXT
;
2650 reader
->text_conv_offset
= 0;
2654 static HRESULT
read_node_text( struct reader
* );
2656 static HRESULT
read_startelement_text( struct reader
*reader
)
2660 if (read_cmp( reader
, "<?", 2 ) == S_OK
)
2662 if ((hr
= read_xmldecl( reader
)) != S_OK
) return hr
;
2664 read_skip_whitespace( reader
);
2665 if (read_cmp( reader
, "<", 1 ) == S_OK
)
2667 if ((hr
= read_element_text( reader
)) != S_OK
) return hr
;
2669 if (read_cmp( reader
, "/>", 2 ) == S_OK
)
2671 read_skip( reader
, 2 );
2672 reader
->current
= LIST_ENTRY( list_tail( &reader
->current
->children
), struct node
, entry
);
2673 reader
->last
= reader
->current
;
2674 reader
->state
= READER_STATE_ENDELEMENT
;
2677 else if (read_cmp( reader
, ">", 1 ) == S_OK
)
2679 read_skip( reader
, 1 );
2680 return read_node_text( reader
);
2682 return WS_E_INVALID_FORMAT
;
2685 static HRESULT
read_node_bin( struct reader
* );
2687 static HRESULT
read_startelement_bin( struct reader
*reader
)
2689 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) return WS_E_INVALID_FORMAT
;
2690 return read_node_bin( reader
);
2693 static HRESULT
read_startelement( struct reader
*reader
)
2695 switch (reader
->input_enc
)
2697 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_startelement_text( reader
);
2698 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_startelement_bin( reader
);
2700 ERR( "unhandled encoding %u\n", reader
->input_enc
);
2701 return WS_E_NOT_SUPPORTED
;
2705 static HRESULT
read_to_startelement_text( struct reader
*reader
, BOOL
*found
)
2709 switch (reader
->state
)
2711 case READER_STATE_INITIAL
:
2712 if ((hr
= read_xmldecl( reader
)) != S_OK
) return hr
;
2715 case READER_STATE_STARTELEMENT
:
2716 if (found
) *found
= TRUE
;
2723 read_skip_whitespace( reader
);
2724 if ((hr
= read_element_text( reader
)) == S_OK
&& found
)
2726 if (reader
->state
== READER_STATE_STARTELEMENT
)
2735 static HRESULT
read_to_startelement_bin( struct reader
*reader
, BOOL
*found
)
2739 if (reader
->state
== READER_STATE_STARTELEMENT
)
2741 if (found
) *found
= TRUE
;
2745 if ((hr
= read_element_bin( reader
)) == S_OK
&& found
)
2747 if (reader
->state
== READER_STATE_STARTELEMENT
)
2756 static HRESULT
read_to_startelement( struct reader
*reader
, BOOL
*found
)
2758 switch (reader
->input_enc
)
2760 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_to_startelement_text( reader
, found
);
2761 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_to_startelement_bin( reader
, found
);
2763 ERR( "unhandled encoding %u\n", reader
->input_enc
);
2764 return WS_E_NOT_SUPPORTED
;
2768 static int cmp_name( const unsigned char *name1
, ULONG len1
, const unsigned char *name2
, ULONG len2
)
2771 if (len1
!= len2
) return 1;
2772 for (i
= 0; i
< len1
; i
++) { if (toupper( name1
[i
] ) != toupper( name2
[i
] )) return 1; }
2776 static struct node
*find_startelement( struct reader
*reader
, const WS_XML_STRING
*prefix
,
2777 const WS_XML_STRING
*localname
)
2779 struct node
*parent
;
2780 const WS_XML_STRING
*str
;
2782 for (parent
= reader
->current
; parent
; parent
= parent
->parent
)
2784 if (node_type( parent
) == WS_XML_NODE_TYPE_ELEMENT
)
2786 str
= parent
->hdr
.prefix
;
2787 if (cmp_name( str
->bytes
, str
->length
, prefix
->bytes
, prefix
->length
)) continue;
2788 str
= parent
->hdr
.localName
;
2789 if (cmp_name( str
->bytes
, str
->length
, localname
->bytes
, localname
->length
)) continue;
2796 static HRESULT
read_endelement_text( struct reader
*reader
)
2798 struct node
*parent
;
2799 unsigned int len
= 0, ch
, skip
;
2800 const unsigned char *start
;
2801 WS_XML_STRING prefix
, localname
;
2804 if ((hr
= read_cmp( reader
, "</", 2 )) != S_OK
) return hr
;
2805 read_skip( reader
, 2 );
2807 start
= read_current_ptr( reader
);
2810 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) return hr
;
2813 read_skip( reader
, 1 );
2816 if (!read_isnamechar( ch
)) return WS_E_INVALID_FORMAT
;
2817 read_skip( reader
, skip
);
2821 if ((hr
= split_qname( start
, len
, &prefix
, &localname
)) != S_OK
) return hr
;
2822 if (!(parent
= find_startelement( reader
, &prefix
, &localname
))) return WS_E_INVALID_FORMAT
;
2824 reader
->current
= LIST_ENTRY( list_tail( &parent
->children
), struct node
, entry
);
2825 reader
->last
= reader
->current
;
2826 reader
->state
= READER_STATE_ENDELEMENT
;
2830 static HRESULT
read_endelement_bin( struct reader
*reader
)
2832 struct node
*parent
;
2836 if (!(reader
->current
->flags
& NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
))
2838 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2839 if (type
!= RECORD_ENDELEMENT
) return WS_E_INVALID_FORMAT
;
2841 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2843 reader
->current
= LIST_ENTRY( list_tail( &parent
->children
), struct node
, entry
);
2844 reader
->last
= reader
->current
;
2845 reader
->state
= READER_STATE_ENDELEMENT
;
2849 static HRESULT
read_endelement( struct reader
*reader
)
2851 if (reader
->state
== READER_STATE_EOF
) return WS_E_INVALID_FORMAT
;
2853 if (read_end_of_data( reader
))
2855 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
2856 reader
->last
= reader
->current
;
2857 reader
->state
= READER_STATE_EOF
;
2861 switch (reader
->input_enc
)
2863 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_endelement_text( reader
);
2864 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_endelement_bin( reader
);
2866 ERR( "unhandled encoding %u\n", reader
->input_enc
);
2867 return WS_E_NOT_SUPPORTED
;
2871 static HRESULT
read_comment_text( struct reader
*reader
)
2873 unsigned int len
= 0, ch
, skip
;
2874 const unsigned char *start
;
2875 struct node
*node
, *parent
;
2876 WS_XML_COMMENT_NODE
*comment
;
2879 if ((hr
= read_cmp( reader
, "<!--", 4 )) != S_OK
) return hr
;
2880 read_skip( reader
, 4 );
2882 start
= read_current_ptr( reader
);
2885 if (read_cmp( reader
, "-->", 3 ) == S_OK
)
2887 read_skip( reader
, 3 );
2890 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) return hr
;
2891 read_skip( reader
, skip
);
2895 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2897 if (!(node
= alloc_node( WS_XML_NODE_TYPE_COMMENT
))) return E_OUTOFMEMORY
;
2898 comment
= (WS_XML_COMMENT_NODE
*)node
;
2899 if (!(comment
->value
.bytes
= heap_alloc( len
)))
2902 return E_OUTOFMEMORY
;
2904 memcpy( comment
->value
.bytes
, start
, len
);
2905 comment
->value
.length
= len
;
2907 read_insert_node( reader
, parent
, node
);
2908 reader
->state
= READER_STATE_COMMENT
;
2912 static HRESULT
read_comment_bin( struct reader
*reader
)
2914 struct node
*node
, *parent
;
2915 WS_XML_COMMENT_NODE
*comment
;
2920 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2921 if (type
!= RECORD_COMMENT
) return WS_E_INVALID_FORMAT
;
2922 if ((hr
= read_int31( reader
, &len
)) != S_OK
) return hr
;
2924 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2926 if (!(node
= alloc_node( WS_XML_NODE_TYPE_COMMENT
))) return E_OUTOFMEMORY
;
2927 comment
= (WS_XML_COMMENT_NODE
*)node
;
2928 if (!(comment
->value
.bytes
= heap_alloc( len
)))
2931 return E_OUTOFMEMORY
;
2933 if ((hr
= read_bytes( reader
, comment
->value
.bytes
, len
)) != S_OK
)
2936 return E_OUTOFMEMORY
;
2938 comment
->value
.length
= len
;
2940 read_insert_node( reader
, parent
, node
);
2941 reader
->state
= READER_STATE_COMMENT
;
2945 static HRESULT
read_startcdata( struct reader
*reader
)
2947 struct node
*node
, *endnode
, *parent
;
2950 if ((hr
= read_cmp( reader
, "<![CDATA[", 9 )) != S_OK
) return hr
;
2951 read_skip( reader
, 9 );
2953 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2955 if (!(node
= alloc_node( WS_XML_NODE_TYPE_CDATA
))) return E_OUTOFMEMORY
;
2956 if (!(endnode
= alloc_node( WS_XML_NODE_TYPE_END_CDATA
)))
2959 return E_OUTOFMEMORY
;
2961 list_add_tail( &node
->children
, &endnode
->entry
);
2962 endnode
->parent
= node
;
2964 read_insert_node( reader
, parent
, node
);
2965 reader
->state
= READER_STATE_STARTCDATA
;
2969 static HRESULT
read_cdata( struct reader
*reader
)
2971 unsigned int len
= 0, ch
, skip
;
2972 const unsigned char *start
;
2974 WS_XML_TEXT_NODE
*text
;
2975 WS_XML_UTF8_TEXT
*utf8
;
2978 start
= read_current_ptr( reader
);
2981 if (read_cmp( reader
, "]]>", 3 ) == S_OK
) break;
2982 if ((hr
= read_utf8_char( reader
, &ch
, &skip
)) != S_OK
) return hr
;
2983 read_skip( reader
, skip
);
2987 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return E_OUTOFMEMORY
;
2988 text
= (WS_XML_TEXT_NODE
*)node
;
2989 if (!(utf8
= alloc_utf8_text( start
, len
)))
2992 return E_OUTOFMEMORY
;
2994 text
->text
= &utf8
->text
;
2996 read_insert_node( reader
, reader
->current
, node
);
2997 reader
->state
= READER_STATE_CDATA
;
3001 static HRESULT
read_endcdata( struct reader
*reader
)
3003 struct node
*parent
;
3006 if ((hr
= read_cmp( reader
, "]]>", 3 )) != S_OK
) return hr
;
3007 read_skip( reader
, 3 );
3009 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
) parent
= reader
->current
->parent
;
3010 else parent
= reader
->current
;
3012 reader
->current
= LIST_ENTRY( list_tail( &parent
->children
), struct node
, entry
);
3013 reader
->last
= reader
->current
;
3014 reader
->state
= READER_STATE_ENDCDATA
;
3018 static HRESULT
read_node_text( struct reader
*reader
)
3024 if (read_end_of_data( reader
))
3026 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
3027 reader
->last
= reader
->current
;
3028 reader
->state
= READER_STATE_EOF
;
3031 if (reader
->state
== READER_STATE_STARTCDATA
) return read_cdata( reader
);
3032 else if (reader
->state
== READER_STATE_CDATA
) return read_endcdata( reader
);
3033 else if (read_cmp( reader
, "<?", 2 ) == S_OK
)
3035 if ((hr
= read_xmldecl( reader
)) != S_OK
) return hr
;
3037 else if (read_cmp( reader
, "</", 2 ) == S_OK
) return read_endelement_text( reader
);
3038 else if (read_cmp( reader
, "<![CDATA[", 9 ) == S_OK
) return read_startcdata( reader
);
3039 else if (read_cmp( reader
, "<!--", 4 ) == S_OK
) return read_comment_text( reader
);
3040 else if (read_cmp( reader
, "<", 1 ) == S_OK
) return read_element_text( reader
);
3041 else if (read_cmp( reader
, "/>", 2 ) == S_OK
|| read_cmp( reader
, ">", 1 ) == S_OK
)
3043 return read_startelement_text( reader
);
3045 else return read_text_text( reader
);
3049 static HRESULT
read_node_bin( struct reader
*reader
)
3054 if (reader
->current
->flags
& NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
)
3056 reader
->current
= LIST_ENTRY( list_tail( &reader
->current
->parent
->children
), struct node
, entry
);
3057 reader
->last
= reader
->current
;
3058 reader
->state
= READER_STATE_ENDELEMENT
;
3061 if (read_end_of_data( reader
))
3063 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
3064 reader
->last
= reader
->current
;
3065 reader
->state
= READER_STATE_EOF
;
3069 if ((hr
= read_peek( reader
, &type
, 1 )) != S_OK
) return hr
;
3070 if (type
== RECORD_ENDELEMENT
)
3072 return read_endelement_bin( reader
);
3074 else if (type
== RECORD_COMMENT
)
3076 return read_comment_bin( reader
);
3078 else if (type
>= RECORD_SHORT_ELEMENT
&& type
<= RECORD_PREFIX_ELEMENT_Z
)
3080 return read_element_bin( reader
);
3082 else if (type
>= RECORD_ZERO_TEXT
&& type
<= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT
)
3084 return read_text_bin( reader
);
3086 FIXME( "unhandled record type %02x\n", type
);
3087 return WS_E_NOT_SUPPORTED
;
3090 static HRESULT
read_node( struct reader
*reader
)
3092 switch (reader
->input_enc
)
3094 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_node_text( reader
);
3095 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_node_bin( reader
);
3097 ERR( "unhandled encoding %u\n", reader
->input_enc
);
3098 return WS_E_NOT_SUPPORTED
;
3102 HRESULT
copy_node( WS_XML_READER
*handle
, WS_XML_WRITER_ENCODING_TYPE enc
, struct node
**node
)
3104 struct reader
*reader
= (struct reader
*)handle
;
3105 const struct list
*ptr
;
3106 const struct node
*start
;
3109 EnterCriticalSection( &reader
->cs
);
3111 if (reader
->magic
!= READER_MAGIC
)
3113 LeaveCriticalSection( &reader
->cs
);
3114 return E_INVALIDARG
;
3117 if (reader
->current
!= reader
->root
) ptr
= &reader
->current
->entry
;
3118 else /* copy whole tree */
3120 if (!read_end_of_data( reader
))
3124 if ((hr
= read_node( reader
)) != S_OK
) goto done
;
3125 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_EOF
) break;
3128 ptr
= list_head( &reader
->root
->children
);
3131 start
= LIST_ENTRY( ptr
, struct node
, entry
);
3132 if (node_type( start
) == WS_XML_NODE_TYPE_EOF
) hr
= WS_E_INVALID_OPERATION
;
3133 else hr
= dup_tree( start
, enc
, node
);
3136 LeaveCriticalSection( &reader
->cs
);
3140 /**************************************************************************
3141 * WsReadEndElement [webservices.@]
3143 HRESULT WINAPI
WsReadEndElement( WS_XML_READER
*handle
, WS_ERROR
*error
)
3145 struct reader
*reader
= (struct reader
*)handle
;
3148 TRACE( "%p %p\n", handle
, error
);
3149 if (error
) FIXME( "ignoring error parameter\n" );
3151 if (!reader
) return E_INVALIDARG
;
3153 EnterCriticalSection( &reader
->cs
);
3155 if (reader
->magic
!= READER_MAGIC
)
3157 LeaveCriticalSection( &reader
->cs
);
3158 return E_INVALIDARG
;
3161 hr
= read_endelement( reader
);
3163 LeaveCriticalSection( &reader
->cs
);
3164 TRACE( "returning %08x\n", hr
);
3168 /**************************************************************************
3169 * WsReadNode [webservices.@]
3171 HRESULT WINAPI
WsReadNode( WS_XML_READER
*handle
, WS_ERROR
*error
)
3173 struct reader
*reader
= (struct reader
*)handle
;
3176 TRACE( "%p %p\n", handle
, error
);
3177 if (error
) FIXME( "ignoring error parameter\n" );
3179 if (!reader
) return E_INVALIDARG
;
3181 EnterCriticalSection( &reader
->cs
);
3183 if (reader
->magic
!= READER_MAGIC
)
3185 LeaveCriticalSection( &reader
->cs
);
3186 return E_INVALIDARG
;
3189 hr
= read_node( reader
);
3191 LeaveCriticalSection( &reader
->cs
);
3192 TRACE( "returning %08x\n", hr
);
3196 static HRESULT
skip_node( struct reader
*reader
)
3198 const struct node
*parent
;
3201 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_EOF
) return WS_E_INVALID_OPERATION
;
3202 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_ELEMENT
) parent
= reader
->current
;
3207 if ((hr
= read_node( reader
)) != S_OK
|| !parent
) break;
3208 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_END_ELEMENT
) continue;
3209 if (reader
->current
->parent
== parent
) return read_node( reader
);
3215 /**************************************************************************
3216 * WsSkipNode [webservices.@]
3218 HRESULT WINAPI
WsSkipNode( WS_XML_READER
*handle
, WS_ERROR
*error
)
3220 struct reader
*reader
= (struct reader
*)handle
;
3223 TRACE( "%p %p\n", handle
, error
);
3224 if (error
) FIXME( "ignoring error parameter\n" );
3226 if (!reader
) return E_INVALIDARG
;
3228 EnterCriticalSection( &reader
->cs
);
3230 if (reader
->magic
!= READER_MAGIC
)
3232 LeaveCriticalSection( &reader
->cs
);
3233 return E_INVALIDARG
;
3236 hr
= skip_node( reader
);
3238 LeaveCriticalSection( &reader
->cs
);
3239 TRACE( "returning %08x\n", hr
);
3243 /**************************************************************************
3244 * WsReadStartElement [webservices.@]
3246 HRESULT WINAPI
WsReadStartElement( WS_XML_READER
*handle
, WS_ERROR
*error
)
3248 struct reader
*reader
= (struct reader
*)handle
;
3251 TRACE( "%p %p\n", handle
, error
);
3252 if (error
) FIXME( "ignoring error parameter\n" );
3254 if (!reader
) return E_INVALIDARG
;
3256 EnterCriticalSection( &reader
->cs
);
3258 if (reader
->magic
!= READER_MAGIC
)
3260 LeaveCriticalSection( &reader
->cs
);
3261 return E_INVALIDARG
;
3264 hr
= read_startelement( reader
);
3266 LeaveCriticalSection( &reader
->cs
);
3267 TRACE( "returning %08x\n", hr
);
3271 /**************************************************************************
3272 * WsReadToStartElement [webservices.@]
3274 HRESULT WINAPI
WsReadToStartElement( WS_XML_READER
*handle
, const WS_XML_STRING
*localname
,
3275 const WS_XML_STRING
*ns
, BOOL
*found
, WS_ERROR
*error
)
3277 struct reader
*reader
= (struct reader
*)handle
;
3280 TRACE( "%p %s %s %p %p\n", handle
, debugstr_xmlstr(localname
), debugstr_xmlstr(ns
), found
, error
);
3281 if (error
) FIXME( "ignoring error parameter\n" );
3283 if (!reader
) return E_INVALIDARG
;
3284 if (localname
|| ns
) FIXME( "name and/or namespace not verified\n" );
3286 EnterCriticalSection( &reader
->cs
);
3288 if (reader
->magic
!= READER_MAGIC
)
3290 LeaveCriticalSection( &reader
->cs
);
3291 return E_INVALIDARG
;
3294 hr
= read_to_startelement( reader
, found
);
3296 LeaveCriticalSection( &reader
->cs
);
3297 TRACE( "returning %08x\n", hr
);
3301 BOOL
move_to_root_element( struct node
*root
, struct node
**current
)
3306 if (!(ptr
= list_head( &root
->children
))) return FALSE
;
3307 node
= LIST_ENTRY( ptr
, struct node
, entry
);
3308 if (node_type( node
) == WS_XML_NODE_TYPE_ELEMENT
)
3313 while ((ptr
= list_next( &root
->children
, &node
->entry
)))
3315 struct node
*next
= LIST_ENTRY( ptr
, struct node
, entry
);
3316 if (node_type( next
) == WS_XML_NODE_TYPE_ELEMENT
)
3326 BOOL
move_to_next_element( struct node
**current
)
3329 struct node
*node
= *current
, *parent
= (*current
)->parent
;
3331 if (!parent
) return FALSE
;
3332 while ((ptr
= list_next( &parent
->children
, &node
->entry
)))
3334 struct node
*next
= LIST_ENTRY( ptr
, struct node
, entry
);
3335 if (node_type( next
) == WS_XML_NODE_TYPE_ELEMENT
)
3345 BOOL
move_to_prev_element( struct node
**current
)
3348 struct node
*node
= *current
, *parent
= (*current
)->parent
;
3350 if (!parent
) return FALSE
;
3351 while ((ptr
= list_prev( &parent
->children
, &node
->entry
)))
3353 struct node
*prev
= LIST_ENTRY( ptr
, struct node
, entry
);
3354 if (node_type( prev
) == WS_XML_NODE_TYPE_ELEMENT
)
3364 BOOL
move_to_child_element( struct node
**current
)
3367 struct node
*child
, *node
= *current
;
3369 if (!(ptr
= list_head( &node
->children
))) return FALSE
;
3370 child
= LIST_ENTRY( ptr
, struct node
, entry
);
3371 if (node_type( child
) == WS_XML_NODE_TYPE_ELEMENT
)
3376 while ((ptr
= list_next( &node
->children
, &child
->entry
)))
3378 struct node
*next
= LIST_ENTRY( ptr
, struct node
, entry
);
3379 if (node_type( next
) == WS_XML_NODE_TYPE_ELEMENT
)
3389 BOOL
move_to_end_element( struct node
**current
)
3392 struct node
*node
= *current
;
3394 if (node_type( node
) != WS_XML_NODE_TYPE_ELEMENT
) return FALSE
;
3396 if ((ptr
= list_tail( &node
->children
)))
3398 struct node
*tail
= LIST_ENTRY( ptr
, struct node
, entry
);
3399 if (node_type( tail
) == WS_XML_NODE_TYPE_END_ELEMENT
)
3408 BOOL
move_to_parent_element( struct node
**current
)
3410 struct node
*parent
= (*current
)->parent
;
3412 if (parent
&& (node_type( parent
) == WS_XML_NODE_TYPE_ELEMENT
||
3413 node_type( parent
) == WS_XML_NODE_TYPE_BOF
))
3421 BOOL
move_to_first_node( struct node
**current
)
3424 struct node
*node
= *current
;
3426 if ((ptr
= list_head( &node
->parent
->children
)))
3428 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3434 BOOL
move_to_next_node( struct node
**current
)
3437 struct node
*node
= *current
;
3439 if ((ptr
= list_next( &node
->parent
->children
, &node
->entry
)))
3441 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3447 BOOL
move_to_prev_node( struct node
**current
)
3450 struct node
*node
= *current
;
3452 if ((ptr
= list_prev( &node
->parent
->children
, &node
->entry
)))
3454 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3460 BOOL
move_to_bof( struct node
*root
, struct node
**current
)
3466 BOOL
move_to_eof( struct node
*root
, struct node
**current
)
3469 if ((ptr
= list_tail( &root
->children
)))
3471 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3477 BOOL
move_to_child_node( struct node
**current
)
3480 struct node
*node
= *current
;
3482 if ((ptr
= list_head( &node
->children
)))
3484 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3490 BOOL
move_to_parent_node( struct node
**current
)
3492 struct node
*parent
= (*current
)->parent
;
3493 if (!parent
) return FALSE
;
3498 static HRESULT
read_move_to( struct reader
*reader
, WS_MOVE_TO move
, BOOL
*found
)
3500 BOOL success
= FALSE
;
3503 if (!read_end_of_data( reader
))
3505 struct node
*saved_current
= reader
->current
;
3506 while (reader
->state
!= READER_STATE_EOF
&& (hr
= read_node( reader
)) == S_OK
) { /* nothing */ };
3507 if (hr
!= S_OK
) return hr
;
3508 reader
->current
= saved_current
;
3512 case WS_MOVE_TO_ROOT_ELEMENT
:
3513 success
= move_to_root_element( reader
->root
, &reader
->current
);
3516 case WS_MOVE_TO_NEXT_ELEMENT
:
3517 success
= move_to_next_element( &reader
->current
);
3520 case WS_MOVE_TO_PREVIOUS_ELEMENT
:
3521 success
= move_to_prev_element( &reader
->current
);
3524 case WS_MOVE_TO_CHILD_ELEMENT
:
3525 success
= move_to_child_element( &reader
->current
);
3528 case WS_MOVE_TO_END_ELEMENT
:
3529 success
= move_to_end_element( &reader
->current
);
3532 case WS_MOVE_TO_PARENT_ELEMENT
:
3533 success
= move_to_parent_element( &reader
->current
);
3536 case WS_MOVE_TO_FIRST_NODE
:
3537 success
= move_to_first_node( &reader
->current
);
3540 case WS_MOVE_TO_NEXT_NODE
:
3541 success
= move_to_next_node( &reader
->current
);
3544 case WS_MOVE_TO_PREVIOUS_NODE
:
3545 success
= move_to_prev_node( &reader
->current
);
3548 case WS_MOVE_TO_CHILD_NODE
:
3549 success
= move_to_child_node( &reader
->current
);
3552 case WS_MOVE_TO_BOF
:
3553 success
= move_to_bof( reader
->root
, &reader
->current
);
3556 case WS_MOVE_TO_EOF
:
3557 success
= move_to_eof( reader
->root
, &reader
->current
);
3561 FIXME( "unhandled move %u\n", move
);
3570 return success
? S_OK
: WS_E_INVALID_FORMAT
;
3573 /**************************************************************************
3574 * WsMoveReader [webservices.@]
3576 HRESULT WINAPI
WsMoveReader( WS_XML_READER
*handle
, WS_MOVE_TO move
, BOOL
*found
, WS_ERROR
*error
)
3578 struct reader
*reader
= (struct reader
*)handle
;
3581 TRACE( "%p %u %p %p\n", handle
, move
, found
, error
);
3582 if (error
) FIXME( "ignoring error parameter\n" );
3584 if (!reader
) return E_INVALIDARG
;
3586 EnterCriticalSection( &reader
->cs
);
3588 if (reader
->magic
!= READER_MAGIC
)
3590 LeaveCriticalSection( &reader
->cs
);
3591 return E_INVALIDARG
;
3594 if (reader
->input_type
!= WS_XML_READER_INPUT_TYPE_BUFFER
) hr
= WS_E_INVALID_OPERATION
;
3595 else hr
= read_move_to( reader
, move
, found
);
3597 LeaveCriticalSection( &reader
->cs
);
3598 TRACE( "returning %08x\n", hr
);
3602 /**************************************************************************
3603 * WsReadStartAttribute [webservices.@]
3605 HRESULT WINAPI
WsReadStartAttribute( WS_XML_READER
*handle
, ULONG index
, WS_ERROR
*error
)
3607 struct reader
*reader
= (struct reader
*)handle
;
3608 const WS_XML_ELEMENT_NODE
*elem
;
3611 TRACE( "%p %u %p\n", handle
, index
, error
);
3612 if (error
) FIXME( "ignoring error parameter\n" );
3614 if (!reader
) return E_INVALIDARG
;
3616 EnterCriticalSection( &reader
->cs
);
3618 if (reader
->magic
!= READER_MAGIC
)
3620 LeaveCriticalSection( &reader
->cs
);
3621 return E_INVALIDARG
;
3624 elem
= &reader
->current
->hdr
;
3625 if (reader
->state
!= READER_STATE_STARTELEMENT
|| index
>= elem
->attributeCount
) hr
= WS_E_INVALID_FORMAT
;
3628 reader
->current_attr
= index
;
3629 reader
->state
= READER_STATE_STARTATTRIBUTE
;
3632 LeaveCriticalSection( &reader
->cs
);
3633 TRACE( "returning %08x\n", hr
);
3637 /**************************************************************************
3638 * WsReadEndAttribute [webservices.@]
3640 HRESULT WINAPI
WsReadEndAttribute( WS_XML_READER
*handle
, WS_ERROR
*error
)
3642 struct reader
*reader
= (struct reader
*)handle
;
3645 TRACE( "%p %p\n", handle
, error
);
3646 if (error
) FIXME( "ignoring error parameter\n" );
3648 if (!reader
) return E_INVALIDARG
;
3650 EnterCriticalSection( &reader
->cs
);
3652 if (reader
->magic
!= READER_MAGIC
)
3654 LeaveCriticalSection( &reader
->cs
);
3655 return E_INVALIDARG
;
3658 if (reader
->state
!= READER_STATE_STARTATTRIBUTE
) hr
= WS_E_INVALID_FORMAT
;
3659 else reader
->state
= READER_STATE_STARTELEMENT
;
3661 LeaveCriticalSection( &reader
->cs
);
3662 TRACE( "returning %08x\n", hr
);
3666 static HRESULT
str_to_bool( const unsigned char *str
, ULONG len
, BOOL
*ret
)
3668 if (len
== 4 && !memcmp( str
, "true", 4 )) *ret
= TRUE
;
3669 else if (len
== 1 && !memcmp( str
, "1", 1 )) *ret
= TRUE
;
3670 else if (len
== 5 && !memcmp( str
, "false", 5 )) *ret
= FALSE
;
3671 else if (len
== 1 && !memcmp( str
, "0", 1 )) *ret
= FALSE
;
3672 else return WS_E_INVALID_FORMAT
;
3676 static HRESULT
str_to_int64( const unsigned char *str
, ULONG len
, INT64 min
, INT64 max
, INT64
*ret
)
3678 BOOL negative
= FALSE
;
3679 const unsigned char *ptr
= str
;
3682 while (len
&& read_isspace( *ptr
)) { ptr
++; len
--; }
3683 while (len
&& read_isspace( ptr
[len
- 1] )) { len
--; }
3684 if (!len
) return WS_E_INVALID_FORMAT
;
3692 if (!len
) return WS_E_INVALID_FORMAT
;
3698 if (!isdigit( *ptr
)) return WS_E_INVALID_FORMAT
;
3700 if (negative
) val
= -val
;
3702 if ((!negative
&& (*ret
> max
/ 10 || *ret
* 10 > max
- val
)) ||
3703 (negative
&& (*ret
< min
/ 10 || *ret
* 10 < min
- val
)))
3705 return WS_E_NUMERIC_OVERFLOW
;
3707 *ret
= *ret
* 10 + val
;
3714 static HRESULT
str_to_uint64( const unsigned char *str
, ULONG len
, UINT64 max
, UINT64
*ret
)
3716 const unsigned char *ptr
= str
;
3719 while (len
&& read_isspace( *ptr
)) { ptr
++; len
--; }
3720 while (len
&& read_isspace( ptr
[len
- 1] )) { len
--; }
3721 if (!len
) return WS_E_INVALID_FORMAT
;
3727 if (!isdigit( *ptr
)) return WS_E_INVALID_FORMAT
;
3730 if ((*ret
> max
/ 10 || *ret
* 10 > max
- val
)) return WS_E_NUMERIC_OVERFLOW
;
3731 *ret
= *ret
* 10 + val
;
3738 static HRESULT
str_to_double( const unsigned char *str
, ULONG len
, double *ret
)
3740 BOOL found_sign
= FALSE
, found_exponent
= FALSE
, found_digit
= FALSE
, found_decimal
= FALSE
;
3741 static const unsigned __int64 nan
= 0xfff8000000000000;
3742 static const unsigned __int64 inf
= 0x7ff0000000000000;
3743 static const unsigned __int64 inf_min
= 0xfff0000000000000;
3744 const char *p
= (const char *)str
;
3748 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3749 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3750 if (!len
) return WS_E_INVALID_FORMAT
;
3752 if (len
== 3 && !memcmp( p
, "NaN", 3 ))
3754 *(unsigned __int64
*)ret
= nan
;
3757 if (len
== 3 && !memcmp( p
, "INF", 3 ))
3759 *(unsigned __int64
*)ret
= inf
;
3762 if (len
== 4 && !memcmp( p
, "-INF", 4 ))
3764 *(unsigned __int64
*)ret
= inf_min
;
3768 for (i
= 0; i
< len
; i
++)
3770 if (p
[i
] >= '0' && p
[i
] <= '9')
3775 if (!found_sign
&& !found_digit
&& (p
[i
] == '+' || p
[i
] == '-'))
3780 if (!found_exponent
&& found_digit
&& (p
[i
] == 'e' || p
[i
] == 'E'))
3782 found_exponent
= found_decimal
= TRUE
;
3783 found_digit
= found_sign
= FALSE
;
3786 if (!found_decimal
&& p
[i
] == '.')
3788 found_decimal
= TRUE
;
3791 return WS_E_INVALID_FORMAT
;
3793 if (!found_digit
&& !found_exponent
)
3799 if (_snscanf_l( p
, len
, "%lf", c_locale
, &tmp
) != 1) return WS_E_INVALID_FORMAT
;
3804 static HRESULT
str_to_float( const unsigned char *str
, ULONG len
, float *ret
)
3806 static const unsigned int inf
= 0x7f800000;
3807 static const unsigned int inf_min
= 0xff800000;
3808 const unsigned char *p
= str
;
3812 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3813 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3814 if (!len
) return WS_E_INVALID_FORMAT
;
3816 if (len
== 3 && !memcmp( p
, "INF", 3 ))
3818 *(unsigned int *)ret
= inf
;
3821 if (len
== 4 && !memcmp( p
, "-INF", 4 ))
3823 *(unsigned int *)ret
= inf_min
;
3827 if ((hr
= str_to_double( p
, len
, &val
)) != S_OK
) return hr
;
3832 HRESULT
str_to_guid( const unsigned char *str
, ULONG len
, GUID
*ret
)
3834 static const unsigned char hex
[] =
3836 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
3837 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
3838 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
3839 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
3840 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
3841 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
3842 0,10,11,12,13,14,15 /* 0x60 */
3844 const unsigned char *p
= str
;
3847 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3848 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3849 if (len
!= 36) return WS_E_INVALID_FORMAT
;
3851 if (p
[8] != '-' || p
[13] != '-' || p
[18] != '-' || p
[23] != '-')
3852 return WS_E_INVALID_FORMAT
;
3854 for (i
= 0; i
< 36; i
++)
3856 if (i
== 8 || i
== 13 || i
== 18 || i
== 23) continue;
3857 if (p
[i
] > 'f' || (!hex
[p
[i
]] && p
[i
] != '0')) return WS_E_INVALID_FORMAT
;
3860 ret
->Data1
= hex
[p
[0]] << 28 | hex
[p
[1]] << 24 | hex
[p
[2]] << 20 | hex
[p
[3]] << 16 |
3861 hex
[p
[4]] << 12 | hex
[p
[5]] << 8 | hex
[p
[6]] << 4 | hex
[p
[7]];
3863 ret
->Data2
= hex
[p
[9]] << 12 | hex
[p
[10]] << 8 | hex
[p
[11]] << 4 | hex
[p
[12]];
3864 ret
->Data3
= hex
[p
[14]] << 12 | hex
[p
[15]] << 8 | hex
[p
[16]] << 4 | hex
[p
[17]];
3866 ret
->Data4
[0] = hex
[p
[19]] << 4 | hex
[p
[20]];
3867 ret
->Data4
[1] = hex
[p
[21]] << 4 | hex
[p
[22]];
3868 ret
->Data4
[2] = hex
[p
[24]] << 4 | hex
[p
[25]];
3869 ret
->Data4
[3] = hex
[p
[26]] << 4 | hex
[p
[27]];
3870 ret
->Data4
[4] = hex
[p
[28]] << 4 | hex
[p
[29]];
3871 ret
->Data4
[5] = hex
[p
[30]] << 4 | hex
[p
[31]];
3872 ret
->Data4
[6] = hex
[p
[32]] << 4 | hex
[p
[33]];
3873 ret
->Data4
[7] = hex
[p
[34]] << 4 | hex
[p
[35]];
3878 static HRESULT
str_to_string( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_STRING
*ret
)
3880 int len_utf16
= MultiByteToWideChar( CP_UTF8
, 0, (const char *)str
, len
, NULL
, 0 );
3881 if (!(ret
->chars
= ws_alloc( heap
, len_utf16
* sizeof(WCHAR
) ))) return WS_E_QUOTA_EXCEEDED
;
3882 MultiByteToWideChar( CP_UTF8
, 0, (const char *)str
, len
, ret
->chars
, len_utf16
);
3883 ret
->length
= len_utf16
;
3887 static HRESULT
str_to_unique_id( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_UNIQUE_ID
*ret
)
3889 if (len
== 45 && !memcmp( str
, "urn:uuid:", 9 ))
3891 ret
->uri
.length
= 0;
3892 ret
->uri
.chars
= NULL
;
3893 return str_to_guid( str
+ 9, len
- 9, &ret
->guid
);
3896 memset( &ret
->guid
, 0, sizeof(ret
->guid
) );
3897 return str_to_string( str
, len
, heap
, &ret
->uri
);
3900 static inline unsigned char decode_char( unsigned char c
)
3902 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
3903 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
3904 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
3905 if (c
== '+') return 62;
3906 if (c
== '/') return 63;
3910 static ULONG
decode_base64( const unsigned char *base64
, ULONG len
, unsigned char *buf
)
3913 unsigned char c0
, c1
, c2
, c3
;
3914 const unsigned char *p
= base64
;
3918 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3919 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3920 if ((c2
= decode_char( p
[2] )) > 63) return 0;
3921 if ((c3
= decode_char( p
[3] )) > 63) return 0;
3922 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
3923 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
3924 buf
[i
+ 2] = (c2
<< 6) | c3
;
3931 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3932 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3933 buf
[i
] = (c0
<< 2) | (c1
>> 4);
3936 else if (p
[3] == '=')
3938 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3939 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3940 if ((c2
= decode_char( p
[2] )) > 63) return 0;
3941 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
3942 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
3947 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3948 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3949 if ((c2
= decode_char( p
[2] )) > 63) return 0;
3950 if ((c3
= decode_char( p
[3] )) > 63) return 0;
3951 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
3952 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
3953 buf
[i
+ 2] = (c2
<< 6) | c3
;
3959 static HRESULT
str_to_bytes( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_BYTES
*ret
)
3961 const unsigned char *p
= str
;
3963 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3964 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3966 if (len
% 4) return WS_E_INVALID_FORMAT
;
3967 if (!(ret
->bytes
= ws_alloc( heap
, len
* 3 / 4 ))) return WS_E_QUOTA_EXCEEDED
;
3968 ret
->length
= decode_base64( p
, len
, ret
->bytes
);
3972 static HRESULT
str_to_xml_string( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_XML_STRING
*ret
)
3974 if (!(ret
->bytes
= ws_alloc( heap
, len
))) return WS_E_QUOTA_EXCEEDED
;
3975 memcpy( ret
->bytes
, str
, len
);
3977 ret
->dictionary
= NULL
;
3982 static HRESULT
copy_xml_string( WS_HEAP
*heap
, const WS_XML_STRING
*src
, WS_XML_STRING
*dst
)
3984 if (!(dst
->bytes
= ws_alloc( heap
, src
->length
))) return WS_E_QUOTA_EXCEEDED
;
3985 memcpy( dst
->bytes
, src
->bytes
, src
->length
);
3986 dst
->length
= src
->length
;
3990 static HRESULT
str_to_qname( struct reader
*reader
, const unsigned char *str
, ULONG len
, WS_HEAP
*heap
,
3991 WS_XML_STRING
*prefix_ret
, WS_XML_STRING
*localname_ret
, WS_XML_STRING
*ns_ret
)
3993 const unsigned char *p
= str
;
3994 WS_XML_STRING prefix
, localname
;
3995 const WS_XML_STRING
*ns
;
3998 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3999 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
4001 if ((hr
= split_qname( p
, len
, &prefix
, &localname
)) != S_OK
) return hr
;
4002 if (!(ns
= get_namespace( reader
, &prefix
))) return WS_E_INVALID_FORMAT
;
4004 if (prefix_ret
&& (hr
= copy_xml_string( heap
, &prefix
, prefix_ret
)) != S_OK
) return hr
;
4005 if ((hr
= copy_xml_string( heap
, &localname
, localname_ret
)) != S_OK
)
4007 ws_free( heap
, prefix_ret
->bytes
, prefix_ret
->length
);
4010 if ((hr
= copy_xml_string( heap
, ns
, ns_ret
)) != S_OK
)
4012 ws_free( heap
, prefix_ret
->bytes
, prefix_ret
->length
);
4013 ws_free( heap
, localname_ret
->bytes
, localname_ret
->length
);
4019 static HRESULT
read_qualified_name( struct reader
*reader
, WS_HEAP
*heap
, WS_XML_STRING
*prefix
,
4020 WS_XML_STRING
*localname
, WS_XML_STRING
*ns
)
4022 const WS_XML_TEXT_NODE
*node
= (const WS_XML_TEXT_NODE
*)reader
->current
;
4023 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)node
->text
;
4024 return str_to_qname( reader
, utf8
->value
.bytes
, utf8
->value
.length
, heap
, prefix
, localname
, ns
);
4027 /**************************************************************************
4028 * WsReadQualifiedName [webservices.@]
4030 HRESULT WINAPI
WsReadQualifiedName( WS_XML_READER
*handle
, WS_HEAP
*heap
, WS_XML_STRING
*prefix
,
4031 WS_XML_STRING
*localname
, WS_XML_STRING
*ns
,
4034 struct reader
*reader
= (struct reader
*)handle
;
4037 TRACE( "%p %p %p %p %p %p\n", handle
, heap
, prefix
, localname
, ns
, error
);
4038 if (error
) FIXME( "ignoring error parameter\n" );
4040 if (!reader
|| !heap
) return E_INVALIDARG
;
4042 EnterCriticalSection( &reader
->cs
);
4044 if (reader
->magic
!= READER_MAGIC
)
4046 LeaveCriticalSection( &reader
->cs
);
4047 return E_INVALIDARG
;
4050 if (!reader
->input_type
) hr
= WS_E_INVALID_OPERATION
;
4051 else if (!localname
) hr
= E_INVALIDARG
;
4052 else if (reader
->state
!= READER_STATE_TEXT
) hr
= WS_E_INVALID_FORMAT
;
4053 else hr
= read_qualified_name( reader
, heap
, prefix
, localname
, ns
);
4055 LeaveCriticalSection( &reader
->cs
);
4056 TRACE( "returning %08x\n", hr
);
4060 static const int month_offsets
[2][12] =
4062 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
4063 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
4066 static inline int valid_day( int year
, int month
, int day
)
4068 return day
> 0 && day
<= month_days
[leap_year( year
)][month
- 1];
4071 static inline int leap_days_before( int year
)
4073 return (year
- 1) / 4 - (year
- 1) / 100 + (year
- 1) / 400;
4076 static HRESULT
str_to_datetime( const unsigned char *bytes
, ULONG len
, WS_DATETIME
*ret
)
4078 const unsigned char *p
= bytes
, *q
;
4079 int year
, month
, day
, hour
, min
, sec
, sec_frac
= 0, tz_hour
, tz_min
, tz_neg
;
4081 while (len
&& read_isspace( *p
)) { p
++; len
--; }
4082 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
4085 while (len
&& isdigit( *q
)) { q
++; len
--; };
4086 if (q
- p
!= 4 || !len
|| *q
!= '-') return WS_E_INVALID_FORMAT
;
4087 year
= (p
[0] - '0') * 1000 + (p
[1] - '0') * 100 + (p
[2] - '0') * 10 + p
[3] - '0';
4088 if (year
< 1) return WS_E_INVALID_FORMAT
;
4091 while (len
&& isdigit( *q
)) { q
++; len
--; };
4092 if (q
- p
!= 2 || !len
|| *q
!= '-') return WS_E_INVALID_FORMAT
;
4093 month
= (p
[0] - '0') * 10 + p
[1] - '0';
4094 if (month
< 1 || month
> 12) return WS_E_INVALID_FORMAT
;
4097 while (len
&& isdigit( *q
)) { q
++; len
--; };
4098 if (q
- p
!= 2 || !len
|| *q
!= 'T') return WS_E_INVALID_FORMAT
;
4099 day
= (p
[0] - '0') * 10 + p
[1] - '0';
4100 if (!valid_day( year
, month
, day
)) return WS_E_INVALID_FORMAT
;
4103 while (len
&& isdigit( *q
)) { q
++; len
--; };
4104 if (q
- p
!= 2 || !len
|| *q
!= ':') return WS_E_INVALID_FORMAT
;
4105 hour
= (p
[0] - '0') * 10 + p
[1] - '0';
4106 if (hour
> 24) return WS_E_INVALID_FORMAT
;
4109 while (len
&& isdigit( *q
)) { q
++; len
--; };
4110 if (q
- p
!= 2 || !len
|| *q
!= ':') return WS_E_INVALID_FORMAT
;
4111 min
= (p
[0] - '0') * 10 + p
[1] - '0';
4112 if (min
> 59 || (min
> 0 && hour
== 24)) return WS_E_INVALID_FORMAT
;
4115 while (len
&& isdigit( *q
)) { q
++; len
--; };
4116 if (q
- p
!= 2 || !len
) return WS_E_INVALID_FORMAT
;
4117 sec
= (p
[0] - '0') * 10 + p
[1] - '0';
4118 if (sec
> 59 || (sec
> 0 && hour
== 24)) return WS_E_INVALID_FORMAT
;
4122 unsigned int i
, nb_digits
, mul
= TICKS_PER_SEC
/ 10;
4124 while (len
&& isdigit( *q
)) { q
++; len
--; };
4126 if (nb_digits
< 1 || nb_digits
> 7) return WS_E_INVALID_FORMAT
;
4127 for (i
= 0; i
< nb_digits
; i
++)
4129 sec_frac
+= (p
[i
] - '0') * mul
;
4135 if (--len
) return WS_E_INVALID_FORMAT
;
4136 tz_hour
= tz_min
= tz_neg
= 0;
4137 ret
->format
= WS_DATETIME_FORMAT_UTC
;
4139 else if (*q
== '+' || *q
== '-')
4141 tz_neg
= (*q
== '-') ? 1 : 0;
4144 while (len
&& isdigit( *q
)) { q
++; len
--; };
4145 if (q
- p
!= 2 || !len
|| *q
!= ':') return WS_E_INVALID_FORMAT
;
4146 tz_hour
= (p
[0] - '0') * 10 + p
[1] - '0';
4147 if (tz_hour
> 14) return WS_E_INVALID_FORMAT
;
4150 while (len
&& isdigit( *q
)) { q
++; len
--; };
4151 if (q
- p
!= 2 || len
) return WS_E_INVALID_FORMAT
;
4152 tz_min
= (p
[0] - '0') * 10 + p
[1] - '0';
4153 if (tz_min
> 59 || (tz_min
> 0 && tz_hour
== 14)) return WS_E_INVALID_FORMAT
;
4155 ret
->format
= WS_DATETIME_FORMAT_LOCAL
;
4157 else return WS_E_INVALID_FORMAT
;
4159 ret
->ticks
= ((year
- 1) * 365 + leap_days_before( year
)) * TICKS_PER_DAY
;
4160 ret
->ticks
+= month_offsets
[leap_year( year
)][month
- 1] * TICKS_PER_DAY
;
4161 ret
->ticks
+= (day
- 1) * TICKS_PER_DAY
;
4162 ret
->ticks
+= hour
* TICKS_PER_HOUR
;
4163 ret
->ticks
+= min
* TICKS_PER_MIN
;
4164 ret
->ticks
+= sec
* TICKS_PER_SEC
;
4165 ret
->ticks
+= sec_frac
;
4169 if (tz_hour
* TICKS_PER_HOUR
+ tz_min
* TICKS_PER_MIN
+ ret
->ticks
> TICKS_MAX
)
4170 return WS_E_INVALID_FORMAT
;
4171 ret
->ticks
+= tz_hour
* TICKS_PER_HOUR
;
4172 ret
->ticks
+= tz_min
* TICKS_PER_MIN
;
4176 if (tz_hour
* TICKS_PER_HOUR
+ tz_min
* TICKS_PER_MIN
> ret
->ticks
)
4177 return WS_E_INVALID_FORMAT
;
4178 ret
->ticks
-= tz_hour
* TICKS_PER_HOUR
;
4179 ret
->ticks
-= tz_min
* TICKS_PER_MIN
;
4185 /**************************************************************************
4186 * WsDateTimeToFileTime [webservices.@]
4188 HRESULT WINAPI
WsDateTimeToFileTime( const WS_DATETIME
*dt
, FILETIME
*ft
, WS_ERROR
*error
)
4190 unsigned __int64 ticks
;
4192 TRACE( "%p %p %p\n", dt
, ft
, error
);
4193 if (error
) FIXME( "ignoring error parameter\n" );
4195 if (!dt
|| !ft
) return E_INVALIDARG
;
4197 if (dt
->ticks
< TICKS_1601_01_01
) return WS_E_INVALID_FORMAT
;
4198 ticks
= dt
->ticks
- TICKS_1601_01_01
;
4199 ft
->dwHighDateTime
= ticks
>> 32;
4200 ft
->dwLowDateTime
= (DWORD
)ticks
;
4204 /**************************************************************************
4205 * WsFileTimeToDateTime [webservices.@]
4207 HRESULT WINAPI
WsFileTimeToDateTime( const FILETIME
*ft
, WS_DATETIME
*dt
, WS_ERROR
*error
)
4209 unsigned __int64 ticks
;
4211 TRACE( "%p %p %p\n", ft
, dt
, error
);
4212 if (error
) FIXME( "ignoring error parameter\n" );
4214 if (!dt
|| !ft
) return E_INVALIDARG
;
4216 ticks
= ((unsigned __int64
)ft
->dwHighDateTime
<< 32) | ft
->dwLowDateTime
;
4217 if (ticks
> MAX_UINT64
- TICKS_1601_01_01
) return WS_E_NUMERIC_OVERFLOW
;
4218 if (ticks
+ TICKS_1601_01_01
> TICKS_MAX
) return WS_E_INVALID_FORMAT
;
4219 dt
->ticks
= ticks
+ TICKS_1601_01_01
;
4220 dt
->format
= WS_DATETIME_FORMAT_UTC
;
4224 static BOOL
find_attribute( struct reader
*reader
, const WS_XML_STRING
*localname
,
4225 const WS_XML_STRING
*ns
, ULONG
*index
)
4228 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
4232 *index
= reader
->current_attr
;
4235 for (i
= 0; i
< elem
->attributeCount
; i
++)
4237 const WS_XML_STRING
*localname2
= elem
->attributes
[i
]->localName
;
4238 const WS_XML_STRING
*ns2
= elem
->attributes
[i
]->ns
;
4240 if (!cmp_name( localname
->bytes
, localname
->length
, localname2
->bytes
, localname2
->length
) &&
4241 !cmp_name( ns
->bytes
, ns
->length
, ns2
->bytes
, ns2
->length
))
4250 /**************************************************************************
4251 * WsFindAttribute [webservices.@]
4253 HRESULT WINAPI
WsFindAttribute( WS_XML_READER
*handle
, const WS_XML_STRING
*localname
,
4254 const WS_XML_STRING
*ns
, BOOL required
, ULONG
*index
,
4257 struct reader
*reader
= (struct reader
*)handle
;
4260 TRACE( "%p %s %s %d %p %p\n", handle
, debugstr_xmlstr(localname
), debugstr_xmlstr(ns
),
4261 required
, index
, error
);
4262 if (error
) FIXME( "ignoring error parameter\n" );
4264 if (!reader
|| !localname
|| !ns
|| !index
) return E_INVALIDARG
;
4266 EnterCriticalSection( &reader
->cs
);
4268 if (reader
->magic
!= READER_MAGIC
)
4270 LeaveCriticalSection( &reader
->cs
);
4271 return E_INVALIDARG
;
4274 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) hr
= WS_E_INVALID_OPERATION
;
4275 else if (!find_attribute( reader
, localname
, ns
, index
))
4277 if (required
) hr
= WS_E_INVALID_FORMAT
;
4285 LeaveCriticalSection( &reader
->cs
);
4286 TRACE( "returning %08x\n", hr
);
4290 static HRESULT
get_node_text( struct reader
*reader
, const WS_XML_TEXT
**ret
)
4292 WS_XML_TEXT_NODE
*node
= (WS_XML_TEXT_NODE
*)&reader
->current
->hdr
.node
;
4297 static HRESULT
get_attribute_text( struct reader
*reader
, ULONG index
, const WS_XML_TEXT
**ret
)
4299 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
4300 *ret
= elem
->attributes
[index
]->value
;
4304 static BOOL
match_element( const struct node
*node
, const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
)
4306 const WS_XML_ELEMENT_NODE
*elem
= (const WS_XML_ELEMENT_NODE
*)node
;
4307 if (node_type( node
) != WS_XML_NODE_TYPE_ELEMENT
) return FALSE
;
4308 return WsXmlStringEquals( localname
, elem
->localName
, NULL
) == S_OK
&&
4309 WsXmlStringEquals( ns
, elem
->ns
, NULL
) == S_OK
;
4312 static HRESULT
read_next_node( struct reader
*reader
)
4314 if (reader
->current
== reader
->last
) return read_node( reader
);
4315 if (move_to_child_node( &reader
->current
)) return S_OK
;
4316 if (move_to_next_node( &reader
->current
)) return S_OK
;
4317 if (!move_to_parent_node( &reader
->current
)) return WS_E_INVALID_FORMAT
;
4318 if (move_to_next_node( &reader
->current
)) return S_OK
;
4319 return WS_E_INVALID_FORMAT
;
4328 static void save_reader_position( const struct reader
*reader
, struct reader_pos
*pos
)
4330 pos
->node
= reader
->current
;
4331 pos
->attr
= reader
->current_attr
;
4334 static void restore_reader_position( struct reader
*reader
, const struct reader_pos
*pos
)
4336 reader
->current
= pos
->node
;
4337 reader
->current_attr
= pos
->attr
;
4340 static HRESULT
get_text( struct reader
*reader
, WS_TYPE_MAPPING mapping
, const WS_XML_STRING
*localname
,
4341 const WS_XML_STRING
*ns
, const WS_XML_TEXT
**ret
, BOOL
*found
)
4345 case WS_ATTRIBUTE_TYPE_MAPPING
:
4348 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
4351 for (i
= 0; i
< elem
->attributeCount
; i
++)
4353 const WS_XML_STRING
*localname2
= elem
->attributes
[i
]->localName
;
4354 const WS_XML_STRING
*ns2
= elem
->attributes
[i
]->ns
;
4356 if (cmp_name( localname
->bytes
, localname
->length
, localname2
->bytes
, localname2
->length
)) continue;
4357 if (!ns
->length
|| !cmp_name( ns
->bytes
, ns
->length
, ns2
->bytes
, ns2
->length
))
4363 if (!*found
) return S_OK
;
4364 return get_attribute_text( reader
, i
, ret
);
4366 case WS_ELEMENT_TYPE_MAPPING
:
4367 case WS_ELEMENT_CONTENT_TYPE_MAPPING
:
4368 case WS_ANY_ELEMENT_TYPE_MAPPING
:
4373 struct reader_pos pos
;
4376 if (!match_element( reader
->current
, localname
, ns
))
4381 save_reader_position( reader
, &pos
);
4382 if ((hr
= read_next_node( reader
)) != S_OK
) return hr
;
4383 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_TEXT
)
4385 restore_reader_position( reader
, &pos
);
4390 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_TEXT
)
4395 return get_node_text( reader
, ret
);
4398 FIXME( "mapping %u not supported\n", mapping
);
4403 static HRESULT
text_to_bool( const WS_XML_TEXT
*text
, BOOL
*val
)
4407 switch (text
->textType
)
4409 case WS_XML_TEXT_TYPE_UTF8
:
4411 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4412 hr
= str_to_bool( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
4415 case WS_XML_TEXT_TYPE_BOOL
:
4417 const WS_XML_BOOL_TEXT
*text_bool
= (const WS_XML_BOOL_TEXT
*)text
;
4418 *val
= text_bool
->value
;
4423 FIXME( "unhandled text type %u\n", text
->textType
);
4430 static HRESULT
read_type_bool( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4431 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4432 const WS_BOOL_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4433 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4435 const WS_XML_TEXT
*text
;
4441 FIXME( "description not supported\n" );
4444 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4445 if (*found
&& (hr
= text_to_bool( text
, &val
)) != S_OK
) return hr
;
4449 case WS_READ_REQUIRED_VALUE
:
4450 if (!*found
) return WS_E_INVALID_FORMAT
;
4453 case WS_READ_NILLABLE_VALUE
:
4454 if (size
!= sizeof(val
)) return E_INVALIDARG
;
4458 case WS_READ_REQUIRED_POINTER
:
4459 if (!*found
) return WS_E_INVALID_FORMAT
;
4462 case WS_READ_OPTIONAL_POINTER
:
4463 case WS_READ_NILLABLE_POINTER
:
4465 BOOL
*heap_val
= NULL
;
4466 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4469 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4472 *(BOOL
**)ret
= heap_val
;
4476 FIXME( "read option %u not supported\n", option
);
4483 static HRESULT
text_to_int8( const WS_XML_TEXT
*text
, INT64
*val
)
4487 switch (text
->textType
)
4489 case WS_XML_TEXT_TYPE_UTF8
:
4491 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4492 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT8
, MAX_INT8
, val
);
4495 case WS_XML_TEXT_TYPE_INT32
:
4497 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4498 assert( text_int32
->value
>= MIN_INT8
);
4499 assert( text_int32
->value
<= MAX_INT8
);
4500 *val
= text_int32
->value
;
4505 FIXME( "unhandled text type %u\n", text
->textType
);
4512 static HRESULT
read_type_int8( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4513 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4514 const WS_INT8_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4515 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4517 const WS_XML_TEXT
*text
;
4523 FIXME( "description not supported\n" );
4526 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4527 if (*found
&& (hr
= text_to_int8( text
, &val
)) != S_OK
) return hr
;
4531 case WS_READ_REQUIRED_VALUE
:
4532 if (!*found
) return WS_E_INVALID_FORMAT
;
4535 case WS_READ_NILLABLE_VALUE
:
4536 if (size
!= sizeof(INT8
)) return E_INVALIDARG
;
4540 case WS_READ_REQUIRED_POINTER
:
4541 if (!*found
) return WS_E_INVALID_FORMAT
;
4544 case WS_READ_OPTIONAL_POINTER
:
4545 case WS_READ_NILLABLE_POINTER
:
4547 INT8
*heap_val
= NULL
;
4548 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4551 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4554 *(INT8
**)ret
= heap_val
;
4558 FIXME( "read option %u not supported\n", option
);
4565 static HRESULT
text_to_int16( const WS_XML_TEXT
*text
, INT64
*val
)
4569 switch (text
->textType
)
4571 case WS_XML_TEXT_TYPE_UTF8
:
4573 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4574 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT16
, MAX_INT16
, val
);
4577 case WS_XML_TEXT_TYPE_INT32
:
4579 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4580 assert( text_int32
->value
>= MIN_INT16
);
4581 assert( text_int32
->value
<= MAX_INT16
);
4582 *val
= text_int32
->value
;
4587 FIXME( "unhandled text type %u\n", text
->textType
);
4594 static HRESULT
read_type_int16( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4595 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4596 const WS_INT16_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4597 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4599 const WS_XML_TEXT
*text
;
4605 FIXME( "description not supported\n" );
4608 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4609 if (*found
&& (hr
= text_to_int16( text
, &val
)) != S_OK
) return hr
;
4613 case WS_READ_REQUIRED_VALUE
:
4614 if (!*found
) return WS_E_INVALID_FORMAT
;
4617 case WS_READ_NILLABLE_VALUE
:
4618 if (size
!= sizeof(INT16
)) return E_INVALIDARG
;
4619 *(INT16
*)ret
= val
;
4622 case WS_READ_REQUIRED_POINTER
:
4623 if (!*found
) return WS_E_INVALID_FORMAT
;
4626 case WS_READ_OPTIONAL_POINTER
:
4627 case WS_READ_NILLABLE_POINTER
:
4629 INT16
*heap_val
= NULL
;
4630 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4633 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4636 *(INT16
**)ret
= heap_val
;
4640 FIXME( "read option %u not supported\n", option
);
4647 static HRESULT
text_to_int32( const WS_XML_TEXT
*text
, INT64
*val
)
4651 switch (text
->textType
)
4653 case WS_XML_TEXT_TYPE_UTF8
:
4655 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4656 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT32
, MAX_INT32
, val
);
4659 case WS_XML_TEXT_TYPE_INT32
:
4661 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4662 *val
= text_int32
->value
;
4667 FIXME( "unhandled text type %u\n", text
->textType
);
4674 static HRESULT
read_type_int32( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4675 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4676 const WS_INT32_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4677 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4679 const WS_XML_TEXT
*text
;
4685 FIXME( "description not supported\n" );
4688 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4689 if (*found
&& (hr
= text_to_int32( text
, &val
)) != S_OK
) return hr
;
4693 case WS_READ_REQUIRED_VALUE
:
4694 if (!*found
) return WS_E_INVALID_FORMAT
;
4697 case WS_READ_NILLABLE_VALUE
:
4698 if (size
!= sizeof(INT32
)) return E_INVALIDARG
;
4699 *(INT32
*)ret
= val
;
4702 case WS_READ_REQUIRED_POINTER
:
4703 if (!*found
) return WS_E_INVALID_FORMAT
;
4706 case WS_READ_OPTIONAL_POINTER
:
4707 case WS_READ_NILLABLE_POINTER
:
4709 INT32
*heap_val
= NULL
;
4710 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4713 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4716 *(INT32
**)ret
= heap_val
;
4720 FIXME( "read option %u not supported\n", option
);
4727 static HRESULT
text_to_int64( const WS_XML_TEXT
*text
, INT64
*val
)
4731 switch (text
->textType
)
4733 case WS_XML_TEXT_TYPE_UTF8
:
4735 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4736 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT64
, MAX_INT64
, val
);
4739 case WS_XML_TEXT_TYPE_INT64
:
4741 const WS_XML_INT64_TEXT
*text_int64
= (const WS_XML_INT64_TEXT
*)text
;
4742 *val
= text_int64
->value
;
4747 FIXME( "unhandled text type %u\n", text
->textType
);
4754 static HRESULT
read_type_int64( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4755 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4756 const WS_INT64_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4757 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4759 const WS_XML_TEXT
*text
;
4765 FIXME( "description not supported\n" );
4768 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4769 if (*found
&& (hr
= text_to_int64( text
, &val
)) != S_OK
) return hr
;
4773 case WS_READ_REQUIRED_VALUE
:
4774 if (!*found
) return WS_E_INVALID_FORMAT
;
4777 case WS_READ_NILLABLE_VALUE
:
4778 if (size
!= sizeof(val
)) return E_INVALIDARG
;
4779 *(INT64
*)ret
= val
;
4782 case WS_READ_REQUIRED_POINTER
:
4783 if (!*found
) return WS_E_INVALID_FORMAT
;
4786 case WS_READ_OPTIONAL_POINTER
:
4787 case WS_READ_NILLABLE_POINTER
:
4789 INT64
*heap_val
= NULL
;
4790 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4793 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4796 *(INT64
**)ret
= heap_val
;
4800 FIXME( "read option %u not supported\n", option
);
4807 static HRESULT
text_to_uint8( const WS_XML_TEXT
*text
, UINT64
*val
)
4811 switch (text
->textType
)
4813 case WS_XML_TEXT_TYPE_UTF8
:
4815 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4816 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT8
, val
);
4819 case WS_XML_TEXT_TYPE_UINT64
:
4821 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
4822 assert( text_uint64
->value
<= MAX_UINT8
);
4823 *val
= text_uint64
->value
;
4828 FIXME( "unhandled text type %u\n", text
->textType
);
4835 static HRESULT
read_type_uint8( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4836 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4837 const WS_UINT8_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4838 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4840 const WS_XML_TEXT
*text
;
4846 FIXME( "description not supported\n" );
4849 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4850 if (*found
&& (hr
= text_to_uint8( text
, &val
)) != S_OK
) return hr
;
4854 case WS_READ_REQUIRED_VALUE
:
4855 if (!*found
) return WS_E_INVALID_FORMAT
;
4858 case WS_READ_NILLABLE_VALUE
:
4859 if (size
!= sizeof(UINT8
)) return E_INVALIDARG
;
4860 *(UINT8
*)ret
= val
;
4863 case WS_READ_REQUIRED_POINTER
:
4864 if (!*found
) return WS_E_INVALID_FORMAT
;
4867 case WS_READ_OPTIONAL_POINTER
:
4868 case WS_READ_NILLABLE_POINTER
:
4870 UINT8
*heap_val
= NULL
;
4871 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4874 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4877 *(UINT8
**)ret
= heap_val
;
4881 FIXME( "read option %u not supported\n", option
);
4888 static HRESULT
text_to_uint16( const WS_XML_TEXT
*text
, UINT64
*val
)
4892 switch (text
->textType
)
4894 case WS_XML_TEXT_TYPE_UTF8
:
4896 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4897 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT16
, val
);
4900 case WS_XML_TEXT_TYPE_INT32
:
4902 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4903 assert( text_int32
->value
>= 0 );
4904 assert( text_int32
->value
<= MAX_UINT16
);
4905 *val
= text_int32
->value
;
4909 case WS_XML_TEXT_TYPE_UINT64
:
4911 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
4912 assert( text_uint64
->value
<= MAX_UINT16
);
4913 *val
= text_uint64
->value
;
4918 FIXME( "unhandled text type %u\n", text
->textType
);
4925 static HRESULT
read_type_uint16( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4926 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4927 const WS_UINT16_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4928 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4930 const WS_XML_TEXT
*text
;
4936 FIXME( "description not supported\n" );
4939 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4940 if (*found
&& (hr
= text_to_uint16( text
, &val
)) != S_OK
) return hr
;
4944 case WS_READ_REQUIRED_VALUE
:
4945 if (!*found
) return WS_E_INVALID_FORMAT
;
4948 case WS_READ_NILLABLE_VALUE
:
4949 if (size
!= sizeof(UINT16
)) return E_INVALIDARG
;
4950 *(UINT16
*)ret
= val
;
4953 case WS_READ_REQUIRED_POINTER
:
4954 if (!*found
) return WS_E_INVALID_FORMAT
;
4957 case WS_READ_OPTIONAL_POINTER
:
4958 case WS_READ_NILLABLE_POINTER
:
4960 UINT16
*heap_val
= NULL
;
4961 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4964 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4967 *(UINT16
**)ret
= heap_val
;
4971 FIXME( "read option %u not supported\n", option
);
4978 static HRESULT
text_to_uint32( const WS_XML_TEXT
*text
, UINT64
*val
)
4982 switch (text
->textType
)
4984 case WS_XML_TEXT_TYPE_UTF8
:
4986 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4987 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT32
, val
);
4990 case WS_XML_TEXT_TYPE_INT32
:
4992 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4993 assert( text_int32
->value
>= 0 );
4994 *val
= text_int32
->value
;
4998 case WS_XML_TEXT_TYPE_UINT64
:
5000 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
5001 assert( text_uint64
->value
<= MAX_UINT32
);
5002 *val
= text_uint64
->value
;
5007 FIXME( "unhandled text type %u\n", text
->textType
);
5014 static HRESULT
read_type_uint32( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5015 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5016 const WS_UINT32_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5017 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5019 const WS_XML_TEXT
*text
;
5025 FIXME( "description not supported\n" );
5028 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5029 if (*found
&& (hr
= text_to_uint32( text
, &val
)) != S_OK
) return hr
;
5033 case WS_READ_REQUIRED_VALUE
:
5034 if (!*found
) return WS_E_INVALID_FORMAT
;
5037 case WS_READ_NILLABLE_VALUE
:
5038 if (size
!= sizeof(UINT32
)) return E_INVALIDARG
;
5039 *(UINT32
*)ret
= val
;
5042 case WS_READ_REQUIRED_POINTER
:
5043 if (!*found
) return WS_E_INVALID_FORMAT
;
5046 case WS_READ_OPTIONAL_POINTER
:
5047 case WS_READ_NILLABLE_POINTER
:
5049 UINT32
*heap_val
= NULL
;
5050 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5053 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5056 *(UINT32
**)ret
= heap_val
;
5060 FIXME( "read option %u not supported\n", option
);
5067 static HRESULT
text_to_uint64( const WS_XML_TEXT
*text
, UINT64
*val
)
5071 switch (text
->textType
)
5073 case WS_XML_TEXT_TYPE_UTF8
:
5075 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5076 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT64
, val
);
5079 case WS_XML_TEXT_TYPE_INT32
:
5081 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
5082 *val
= text_int32
->value
;
5086 case WS_XML_TEXT_TYPE_INT64
:
5088 const WS_XML_INT64_TEXT
*text_int64
= (const WS_XML_INT64_TEXT
*)text
;
5089 *val
= text_int64
->value
;
5093 case WS_XML_TEXT_TYPE_UINT64
:
5095 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
5096 *val
= text_uint64
->value
;
5101 FIXME( "unhandled text type %u\n", text
->textType
);
5108 static HRESULT
read_type_uint64( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5109 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5110 const WS_UINT64_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5111 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5113 const WS_XML_TEXT
*text
;
5119 FIXME( "description not supported\n" );
5122 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5123 if (*found
&& (hr
= text_to_uint64( text
, &val
)) != S_OK
) return hr
;
5127 case WS_READ_REQUIRED_VALUE
:
5128 if (!*found
) return WS_E_INVALID_FORMAT
;
5131 case WS_READ_NILLABLE_VALUE
:
5132 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5133 *(UINT64
*)ret
= val
;
5136 case WS_READ_REQUIRED_POINTER
:
5137 if (!*found
) return WS_E_INVALID_FORMAT
;
5140 case WS_READ_OPTIONAL_POINTER
:
5141 case WS_READ_NILLABLE_POINTER
:
5143 UINT64
*heap_val
= NULL
;
5144 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5147 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5150 *(UINT64
**)ret
= heap_val
;
5154 FIXME( "read option %u not supported\n", option
);
5161 static HRESULT
text_to_float( const WS_XML_TEXT
*text
, float *val
)
5165 switch (text
->textType
)
5167 case WS_XML_TEXT_TYPE_UTF8
:
5169 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5170 hr
= str_to_float( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5173 case WS_XML_TEXT_TYPE_FLOAT
:
5175 const WS_XML_FLOAT_TEXT
*text_float
= (const WS_XML_FLOAT_TEXT
*)text
;
5176 *val
= text_float
->value
;
5181 FIXME( "unhandled text type %u\n", text
->textType
);
5188 static HRESULT
read_type_float( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5189 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5190 const WS_FLOAT_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5191 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5193 const WS_XML_TEXT
*text
;
5197 if (desc
) FIXME( "ignoring description\n" );
5199 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5200 if (*found
&& (hr
= text_to_float( text
, &val
)) != S_OK
) return hr
;
5204 case WS_READ_REQUIRED_VALUE
:
5205 if (!*found
) return WS_E_INVALID_FORMAT
;
5208 case WS_READ_NILLABLE_VALUE
:
5209 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5210 *(float *)ret
= val
;
5213 case WS_READ_REQUIRED_POINTER
:
5214 if (!*found
) return WS_E_INVALID_FORMAT
;
5217 case WS_READ_OPTIONAL_POINTER
:
5218 case WS_READ_NILLABLE_POINTER
:
5220 float *heap_val
= NULL
;
5221 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5224 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5227 *(float **)ret
= heap_val
;
5231 FIXME( "read option %u not supported\n", option
);
5238 static HRESULT
text_to_double( const WS_XML_TEXT
*text
, double *val
)
5242 switch (text
->textType
)
5244 case WS_XML_TEXT_TYPE_UTF8
:
5246 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5247 hr
= str_to_double( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5250 case WS_XML_TEXT_TYPE_DOUBLE
:
5252 const WS_XML_DOUBLE_TEXT
*text_double
= (const WS_XML_DOUBLE_TEXT
*)text
;
5253 *val
= text_double
->value
;
5258 FIXME( "unhandled text type %u\n", text
->textType
);
5265 static HRESULT
read_type_double( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5266 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5267 const WS_DOUBLE_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5268 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5270 const WS_XML_TEXT
*text
;
5274 if (desc
) FIXME( "ignoring description\n" );
5276 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5277 if (*found
&& (hr
= text_to_double( text
, &val
)) != S_OK
) return hr
;
5281 case WS_READ_REQUIRED_VALUE
:
5282 if (!*found
) return WS_E_INVALID_FORMAT
;
5285 case WS_READ_NILLABLE_VALUE
:
5286 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5287 *(double *)ret
= val
;
5290 case WS_READ_REQUIRED_POINTER
:
5291 if (!*found
) return WS_E_INVALID_FORMAT
;
5294 case WS_READ_OPTIONAL_POINTER
:
5295 case WS_READ_NILLABLE_POINTER
:
5297 double *heap_val
= NULL
;
5298 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5301 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5304 *(double **)ret
= heap_val
;
5308 FIXME( "read option %u not supported\n", option
);
5315 static HRESULT
text_to_wsz( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WCHAR
**ret
)
5317 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5320 assert( text
->textType
== WS_XML_TEXT_TYPE_UTF8
);
5321 len
= MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, NULL
, 0 );
5322 if (!(*ret
= ws_alloc( heap
, (len
+ 1) * sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
5323 MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, *ret
, len
);
5328 static HRESULT
read_type_wsz( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5329 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5330 const WS_WSZ_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5331 WS_HEAP
*heap
, WCHAR
**ret
, ULONG size
, BOOL
*found
)
5333 const WS_XML_TEXT
*text
;
5339 FIXME( "description not supported\n" );
5342 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5343 if (*found
&& (hr
= text_to_wsz( text
, heap
, &str
)) != S_OK
) return hr
;
5347 case WS_READ_REQUIRED_POINTER
:
5348 if (!str
&& !(str
= ws_alloc_zero( heap
, sizeof(*str
) ))) return WS_E_QUOTA_EXCEEDED
;
5351 case WS_READ_OPTIONAL_POINTER
:
5352 case WS_READ_NILLABLE_POINTER
:
5353 if (size
!= sizeof(str
)) return E_INVALIDARG
;
5358 FIXME( "read option %u not supported\n", option
);
5365 static HRESULT
get_enum_value( const WS_XML_TEXT
*text
, const WS_ENUM_DESCRIPTION
*desc
, int *ret
)
5367 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5370 assert( text
->textType
== WS_XML_TEXT_TYPE_UTF8
);
5371 for (i
= 0; i
< desc
->valueCount
; i
++)
5373 if (WsXmlStringEquals( &utf8
->value
, desc
->values
[i
].name
, NULL
) == S_OK
)
5375 *ret
= desc
->values
[i
].value
;
5379 return WS_E_INVALID_FORMAT
;
5382 static HRESULT
read_type_enum( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5383 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5384 const WS_ENUM_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5385 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5387 const WS_XML_TEXT
*text
;
5391 if (!desc
) return E_INVALIDARG
;
5393 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5394 if (*found
&& (hr
= get_enum_value( text
, desc
, &val
)) != S_OK
) return hr
;
5398 case WS_READ_REQUIRED_VALUE
:
5399 if (!*found
) return WS_E_INVALID_FORMAT
;
5402 case WS_READ_NILLABLE_VALUE
:
5403 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5407 case WS_READ_REQUIRED_POINTER
:
5408 if (!*found
) return WS_E_INVALID_FORMAT
;
5411 case WS_READ_OPTIONAL_POINTER
:
5412 case WS_READ_NILLABLE_POINTER
:
5414 int *heap_val
= NULL
;
5415 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5418 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5421 *(int **)ret
= heap_val
;
5425 FIXME( "read option %u not supported\n", option
);
5432 static HRESULT
text_to_datetime( const WS_XML_TEXT
*text
, WS_DATETIME
*val
)
5436 switch (text
->textType
)
5438 case WS_XML_TEXT_TYPE_UTF8
:
5440 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5441 hr
= str_to_datetime( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5444 case WS_XML_TEXT_TYPE_DATETIME
:
5446 const WS_XML_DATETIME_TEXT
*text_datetime
= (const WS_XML_DATETIME_TEXT
*)text
;
5447 *val
= text_datetime
->value
;
5452 FIXME( "unhandled text type %u\n", text
->textType
);
5459 static HRESULT
read_type_datetime( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5460 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5461 const WS_DATETIME_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5462 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5464 const WS_XML_TEXT
*text
;
5466 WS_DATETIME val
= {0, WS_DATETIME_FORMAT_UTC
};
5468 if (desc
) FIXME( "ignoring description\n" );
5470 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5471 if (*found
&& (hr
= text_to_datetime( text
, &val
)) != S_OK
) return hr
;
5475 case WS_READ_REQUIRED_VALUE
:
5476 if (!*found
) return WS_E_INVALID_FORMAT
;
5479 case WS_READ_NILLABLE_VALUE
:
5480 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5481 *(WS_DATETIME
*)ret
= val
;
5484 case WS_READ_REQUIRED_POINTER
:
5485 if (!*found
) return WS_E_INVALID_FORMAT
;
5488 case WS_READ_OPTIONAL_POINTER
:
5489 case WS_READ_NILLABLE_POINTER
:
5491 WS_DATETIME
*heap_val
= NULL
;
5492 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5495 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5498 *(WS_DATETIME
**)ret
= heap_val
;
5502 FIXME( "read option %u not supported\n", option
);
5509 static HRESULT
text_to_guid( const WS_XML_TEXT
*text
, GUID
*val
)
5513 switch (text
->textType
)
5515 case WS_XML_TEXT_TYPE_UTF8
:
5517 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5518 hr
= str_to_guid( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5521 case WS_XML_TEXT_TYPE_GUID
:
5523 const WS_XML_GUID_TEXT
*text_guid
= (const WS_XML_GUID_TEXT
*)text
;
5524 *val
= text_guid
->value
;
5529 FIXME( "unhandled text type %u\n", text
->textType
);
5536 static HRESULT
read_type_guid( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5537 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5538 const WS_GUID_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5539 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5541 const WS_XML_TEXT
*text
;
5545 if (desc
) FIXME( "ignoring description\n" );
5547 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5548 if (*found
&& (hr
= text_to_guid( text
, &val
)) != S_OK
) return hr
;
5552 case WS_READ_REQUIRED_VALUE
:
5553 if (!*found
) return WS_E_INVALID_FORMAT
;
5556 case WS_READ_NILLABLE_VALUE
:
5557 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5561 case WS_READ_REQUIRED_POINTER
:
5562 if (!*found
) return WS_E_INVALID_FORMAT
;
5565 case WS_READ_OPTIONAL_POINTER
:
5566 case WS_READ_NILLABLE_POINTER
:
5568 GUID
*heap_val
= NULL
;
5569 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5572 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5575 *(GUID
**)ret
= heap_val
;
5579 FIXME( "read option %u not supported\n", option
);
5586 static HRESULT
text_to_unique_id( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_UNIQUE_ID
*val
)
5590 switch (text
->textType
)
5592 case WS_XML_TEXT_TYPE_UTF8
:
5594 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5595 hr
= str_to_unique_id( text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, val
);
5598 case WS_XML_TEXT_TYPE_UNIQUE_ID
:
5600 const WS_XML_UNIQUE_ID_TEXT
*text_unique_id
= (const WS_XML_UNIQUE_ID_TEXT
*)text
;
5601 val
->guid
= text_unique_id
->value
;
5602 val
->uri
.length
= 0;
5603 val
->uri
.chars
= NULL
;
5608 FIXME( "unhandled text type %u\n", text
->textType
);
5615 static HRESULT
read_type_unique_id( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5616 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5617 const WS_UNIQUE_ID_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5618 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5620 const WS_XML_TEXT
*text
;
5621 WS_UNIQUE_ID val
= {{0}};
5624 if (desc
) FIXME( "ignoring description\n" );
5626 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5627 if (*found
&& (hr
= text_to_unique_id( text
, heap
, &val
)) != S_OK
) return hr
;
5631 case WS_READ_REQUIRED_VALUE
:
5632 if (!*found
) return WS_E_INVALID_FORMAT
;
5635 case WS_READ_NILLABLE_VALUE
:
5636 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5637 *(WS_UNIQUE_ID
*)ret
= val
;
5640 case WS_READ_REQUIRED_POINTER
:
5641 if (!*found
) return WS_E_INVALID_FORMAT
;
5644 case WS_READ_OPTIONAL_POINTER
:
5645 case WS_READ_NILLABLE_POINTER
:
5647 WS_UNIQUE_ID
*heap_val
= NULL
;
5648 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5651 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5654 *(WS_UNIQUE_ID
**)ret
= heap_val
;
5658 FIXME( "read option %u not supported\n", option
);
5665 static HRESULT
text_to_string( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_STRING
*val
)
5669 switch (text
->textType
)
5671 case WS_XML_TEXT_TYPE_UTF8
:
5673 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5674 hr
= str_to_string( text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, val
);
5677 case WS_XML_TEXT_TYPE_UTF16
:
5679 const WS_XML_UTF16_TEXT
*text_utf16
= (const WS_XML_UTF16_TEXT
*)text
;
5680 if (!(val
->chars
= ws_alloc( heap
, text_utf16
->byteCount
))) return WS_E_QUOTA_EXCEEDED
;
5681 memcpy( val
->chars
, text_utf16
->bytes
, text_utf16
->byteCount
);
5682 val
->length
= text_utf16
->byteCount
/ sizeof(WCHAR
);
5687 FIXME( "unhandled text type %u\n", text
->textType
);
5694 static HRESULT
read_type_string( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5695 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5696 const WS_STRING_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5697 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5699 const WS_XML_TEXT
*text
;
5700 WS_STRING val
= {0};
5703 if (desc
) FIXME( "ignoring description\n" );
5705 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5706 if (*found
&& (hr
= text_to_string( text
, heap
, &val
)) != S_OK
) return hr
;
5710 case WS_READ_REQUIRED_VALUE
:
5711 case WS_READ_NILLABLE_VALUE
:
5712 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5713 *(WS_STRING
*)ret
= val
;
5716 case WS_READ_REQUIRED_POINTER
:
5718 WS_STRING
*heap_val
;
5719 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5720 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5722 *(WS_STRING
**)ret
= heap_val
;
5725 case WS_READ_OPTIONAL_POINTER
:
5726 case WS_READ_NILLABLE_POINTER
:
5728 WS_STRING
*heap_val
= NULL
;
5729 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5732 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5735 *(WS_STRING
**)ret
= heap_val
;
5739 FIXME( "read option %u not supported\n", option
);
5746 static HRESULT
text_to_bytes( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_BYTES
*val
)
5750 switch (text
->textType
)
5752 case WS_XML_TEXT_TYPE_UTF8
:
5754 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5755 hr
= str_to_bytes( text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, val
);
5758 case WS_XML_TEXT_TYPE_BASE64
:
5760 const WS_XML_BASE64_TEXT
*text_base64
= (const WS_XML_BASE64_TEXT
*)text
;
5761 if (!(val
->bytes
= ws_alloc( heap
, text_base64
->length
))) return WS_E_QUOTA_EXCEEDED
;
5762 memcpy( val
->bytes
, text_base64
->bytes
, text_base64
->length
);
5763 val
->length
= text_base64
->length
;
5768 FIXME( "unhandled text type %u\n", text
->textType
);
5775 static HRESULT
read_type_bytes( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5776 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5777 const WS_BYTES_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5778 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5780 const WS_XML_TEXT
*text
;
5784 if (desc
) FIXME( "ignoring description\n" );
5786 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5787 if (*found
&& (hr
= text_to_bytes( text
, heap
, &val
)) != S_OK
) return hr
;
5791 case WS_READ_REQUIRED_VALUE
:
5792 case WS_READ_NILLABLE_VALUE
:
5793 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5794 *(WS_BYTES
*)ret
= val
;
5797 case WS_READ_REQUIRED_POINTER
:
5800 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5801 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5803 *(WS_BYTES
**)ret
= heap_val
;
5806 case WS_READ_OPTIONAL_POINTER
:
5807 case WS_READ_NILLABLE_POINTER
:
5809 WS_BYTES
*heap_val
= NULL
;
5810 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5813 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5816 *(WS_BYTES
**)ret
= heap_val
;
5820 FIXME( "read option %u not supported\n", option
);
5827 static HRESULT
text_to_xml_string( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_XML_STRING
*val
)
5829 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5830 assert( text
->textType
== WS_XML_TEXT_TYPE_UTF8
);
5831 return str_to_xml_string( utf8
->value
.bytes
, utf8
->value
.length
, heap
, val
);
5834 static HRESULT
read_type_xml_string( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5835 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5836 const WS_XML_STRING_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5837 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5839 const WS_XML_TEXT
*text
;
5840 WS_XML_STRING val
= {0};
5843 if (desc
) FIXME( "ignoring description\n" );
5845 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5846 if (*found
&& (hr
= text_to_xml_string( text
, heap
, &val
)) != S_OK
) return hr
;
5850 case WS_READ_REQUIRED_VALUE
:
5851 case WS_READ_NILLABLE_VALUE
:
5852 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5853 *(WS_XML_STRING
*)ret
= val
;
5856 case WS_READ_REQUIRED_POINTER
:
5858 WS_XML_STRING
*heap_val
;
5859 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5860 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5862 *(WS_XML_STRING
**)ret
= heap_val
;
5865 case WS_READ_OPTIONAL_POINTER
:
5866 case WS_READ_NILLABLE_POINTER
:
5868 WS_XML_STRING
*heap_val
= NULL
;
5869 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5872 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5875 *(WS_XML_STRING
**)ret
= heap_val
;
5879 FIXME( "read option %u not supported\n", option
);
5886 static HRESULT
text_to_qname( struct reader
*reader
, const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_XML_QNAME
*val
)
5890 switch (text
->textType
)
5892 case WS_XML_TEXT_TYPE_UTF8
:
5894 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5895 hr
= str_to_qname( reader
, text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, NULL
,
5896 &val
->localName
, &val
->ns
);
5899 case WS_XML_TEXT_TYPE_QNAME
:
5901 const WS_XML_QNAME_TEXT
*text_qname
= (const WS_XML_QNAME_TEXT
*)text
;
5902 if ((hr
= copy_xml_string( heap
, text_qname
->localName
, &val
->localName
)) != S_OK
) return hr
;
5903 if ((hr
= copy_xml_string( heap
, text_qname
->ns
, &val
->ns
)) != S_OK
)
5905 ws_free( heap
, val
->localName
.bytes
, val
->localName
.length
);
5911 FIXME( "unhandled text type %u\n", text
->textType
);
5918 static HRESULT
read_type_qname( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5919 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5920 const WS_XML_QNAME_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5921 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5923 const WS_XML_TEXT
*text
;
5924 WS_XML_QNAME val
= {{0}};
5927 if (desc
) FIXME( "ignoring description\n" );
5929 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) return WS_E_INVALID_FORMAT
;
5930 if ((hr
= read_startelement( reader
)) != S_OK
) return hr
;
5931 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_TEXT
) return WS_E_INVALID_FORMAT
;
5933 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5934 if (*found
&& (hr
= text_to_qname( reader
, text
, heap
, &val
)) != S_OK
) return hr
;
5938 case WS_READ_REQUIRED_VALUE
:
5939 if (!*found
) return WS_E_INVALID_FORMAT
;
5942 case WS_READ_NILLABLE_VALUE
:
5943 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5944 *(WS_XML_QNAME
*)ret
= val
;
5947 case WS_READ_REQUIRED_POINTER
:
5948 if (!*found
) return WS_E_INVALID_FORMAT
;
5951 case WS_READ_OPTIONAL_POINTER
:
5952 case WS_READ_NILLABLE_POINTER
:
5954 WS_XML_QNAME
*heap_val
= NULL
;
5955 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5958 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5961 *(WS_XML_QNAME
**)ret
= heap_val
;
5965 FIXME( "read option %u not supported\n", option
);
5972 static HRESULT
read_type_description( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5973 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5974 const WS_STRUCT_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5975 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5979 case WS_READ_REQUIRED_POINTER
:
5980 case WS_READ_OPTIONAL_POINTER
:
5982 if (size
!= sizeof(desc
)) return E_INVALIDARG
;
5983 *(const WS_STRUCT_DESCRIPTION
**)ret
= desc
;
5988 FIXME( "read option %u not supported\n", option
);
5995 static BOOL
is_empty_text_node( const struct node
*node
)
5997 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)node
;
5999 if (node_type( node
) != WS_XML_NODE_TYPE_TEXT
) return FALSE
;
6000 switch (text
->text
->textType
)
6002 case WS_XML_TEXT_TYPE_UTF8
:
6005 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
->text
;
6006 for (i
= 0; i
< utf8
->value
.length
; i
++) if (!read_isspace( utf8
->value
.bytes
[i
] )) return FALSE
;
6009 case WS_XML_TEXT_TYPE_BASE64
:
6011 const WS_XML_BASE64_TEXT
*base64
= (const WS_XML_BASE64_TEXT
*)text
->text
;
6012 return !base64
->length
;
6014 case WS_XML_TEXT_TYPE_BOOL
:
6015 case WS_XML_TEXT_TYPE_INT32
:
6016 case WS_XML_TEXT_TYPE_INT64
:
6017 case WS_XML_TEXT_TYPE_UINT64
:
6018 case WS_XML_TEXT_TYPE_FLOAT
:
6019 case WS_XML_TEXT_TYPE_DOUBLE
:
6020 case WS_XML_TEXT_TYPE_DECIMAL
:
6021 case WS_XML_TEXT_TYPE_GUID
:
6022 case WS_XML_TEXT_TYPE_UNIQUE_ID
:
6023 case WS_XML_TEXT_TYPE_DATETIME
:
6027 ERR( "unhandled text type %u\n", text
->text
->textType
);
6032 /* skips comment and empty text nodes */
6033 static HRESULT
read_type_next_node( struct reader
*reader
)
6038 WS_XML_NODE_TYPE type
;
6040 if ((hr
= read_next_node( reader
)) != S_OK
) return hr
;
6041 type
= node_type( reader
->current
);
6042 if (type
== WS_XML_NODE_TYPE_COMMENT
||
6043 (type
== WS_XML_NODE_TYPE_TEXT
&& is_empty_text_node( reader
->current
))) continue;
6048 static HRESULT
read_type_next_element_node( struct reader
*reader
, const WS_XML_STRING
*localname
,
6049 const WS_XML_STRING
*ns
)
6051 struct reader_pos pos
;
6054 if (!localname
) return S_OK
; /* assume reader is already correctly positioned */
6055 if (reader
->current
== reader
->last
)
6058 if ((hr
= read_to_startelement( reader
, &found
)) != S_OK
) return hr
;
6059 if (!found
) return WS_E_INVALID_FORMAT
;
6061 if (match_element( reader
->current
, localname
, ns
)) return S_OK
;
6063 save_reader_position( reader
, &pos
);
6064 if ((hr
= read_type_next_node( reader
)) != S_OK
) return hr
;
6065 if (match_element( reader
->current
, localname
, ns
)) return S_OK
;
6066 restore_reader_position( reader
, &pos
);
6068 return WS_E_INVALID_FORMAT
;
6071 ULONG
get_type_size( WS_TYPE type
, const void *desc
)
6077 return sizeof(INT8
);
6080 case WS_UINT16_TYPE
:
6081 return sizeof(INT16
);
6085 case WS_UINT32_TYPE
:
6087 return sizeof(INT32
);
6090 case WS_UINT64_TYPE
:
6091 return sizeof(INT64
);
6094 return sizeof(float);
6096 case WS_DOUBLE_TYPE
:
6097 return sizeof(double);
6099 case WS_DATETIME_TYPE
:
6100 return sizeof(WS_DATETIME
);
6103 return sizeof(GUID
);
6105 case WS_UNIQUE_ID_TYPE
:
6106 return sizeof(WS_UNIQUE_ID
);
6108 case WS_STRING_TYPE
:
6109 return sizeof(WS_STRING
);
6112 return sizeof(WCHAR
*);
6115 return sizeof(WS_BYTES
);
6117 case WS_XML_STRING_TYPE
:
6118 return sizeof(WS_XML_STRING
);
6120 case WS_XML_QNAME_TYPE
:
6121 return sizeof(WS_XML_QNAME
);
6123 case WS_DESCRIPTION_TYPE
:
6124 return sizeof(WS_STRUCT_DESCRIPTION
*);
6126 case WS_STRUCT_TYPE
:
6128 const WS_STRUCT_DESCRIPTION
*desc_struct
= desc
;
6129 return desc_struct
->size
;
6133 const WS_UNION_DESCRIPTION
*desc_union
= desc
;
6134 return desc_union
->size
;
6136 case WS_ANY_ATTRIBUTES_TYPE
:
6140 ERR( "unhandled type %u\n", type
);
6145 static WS_READ_OPTION
get_field_read_option( WS_TYPE type
, ULONG options
)
6147 if (options
& WS_FIELD_POINTER
)
6149 if (options
& WS_FIELD_NILLABLE
) return WS_READ_NILLABLE_POINTER
;
6150 if (options
& WS_FIELD_OPTIONAL
) return WS_READ_OPTIONAL_POINTER
;
6151 return WS_READ_REQUIRED_POINTER
;
6162 case WS_UINT16_TYPE
:
6163 case WS_UINT32_TYPE
:
6164 case WS_UINT64_TYPE
:
6166 case WS_DOUBLE_TYPE
:
6167 case WS_DATETIME_TYPE
:
6169 case WS_UNIQUE_ID_TYPE
:
6170 case WS_STRING_TYPE
:
6172 case WS_XML_STRING_TYPE
:
6173 case WS_XML_QNAME_TYPE
:
6174 case WS_STRUCT_TYPE
:
6177 if (options
& (WS_FIELD_OPTIONAL
|WS_FIELD_NILLABLE
)) return WS_READ_NILLABLE_VALUE
;
6178 return WS_READ_REQUIRED_VALUE
;
6181 case WS_DESCRIPTION_TYPE
:
6182 if (options
& WS_FIELD_NILLABLE
) return WS_READ_NILLABLE_POINTER
;
6183 if (options
& WS_FIELD_OPTIONAL
) return WS_READ_OPTIONAL_POINTER
;
6184 return WS_READ_REQUIRED_POINTER
;
6187 FIXME( "unhandled type %u\n", type
);
6192 static HRESULT
read_type_field( struct reader
*, const WS_STRUCT_DESCRIPTION
*, const WS_FIELD_DESCRIPTION
*,
6193 WS_HEAP
*, char *, ULONG
);
6195 static HRESULT
read_type_union( struct reader
*reader
, const WS_UNION_DESCRIPTION
*desc
, WS_HEAP
*heap
, void *ret
,
6196 ULONG size
, BOOL
*found
)
6198 struct reader_pos pos
;
6202 if (size
!= desc
->size
) return E_INVALIDARG
;
6204 save_reader_position( reader
, &pos
);
6205 if ((hr
= read_type_next_node( reader
)) != S_OK
) return hr
;
6207 for (i
= 0; i
< desc
->fieldCount
; i
++)
6209 if ((*found
= match_element( reader
->current
, desc
->fields
[i
]->field
.localName
, desc
->fields
[i
]->field
.ns
)))
6215 *(int *)((char *)ret
+ desc
->enumOffset
) = desc
->noneEnumValue
;
6216 restore_reader_position( reader
, &pos
);
6220 ULONG offset
= desc
->fields
[i
]->field
.offset
;
6221 if ((hr
= read_type_field( reader
, NULL
, &desc
->fields
[i
]->field
, heap
, ret
, offset
)) != S_OK
) return hr
;
6222 *(int *)((char *)ret
+ desc
->enumOffset
) = desc
->fields
[i
]->value
;
6228 static HRESULT
read_type( struct reader
*, WS_TYPE_MAPPING
, WS_TYPE
, const WS_XML_STRING
*,
6229 const WS_XML_STRING
*, const void *, WS_READ_OPTION
, WS_HEAP
*,
6230 void *, ULONG
, BOOL
* );
6232 static HRESULT
read_type_array( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
,
6233 void **ret
, ULONG
*count
)
6236 ULONG item_size
, nb_items
= 0, nb_allocated
= 1, offset
= 0;
6237 WS_READ_OPTION option
;
6241 if (!(option
= get_field_read_option( desc
->type
, desc
->options
))) return E_INVALIDARG
;
6243 /* wrapper element */
6244 if (desc
->localName
&& ((hr
= read_type_next_element_node( reader
, desc
->localName
, desc
->ns
)) != S_OK
))
6247 if (option
== WS_READ_REQUIRED_VALUE
|| option
== WS_READ_NILLABLE_VALUE
)
6248 item_size
= get_type_size( desc
->type
, desc
->typeDescription
);
6250 item_size
= sizeof(void *);
6252 if (!(buf
= ws_alloc_zero( heap
, item_size
))) return WS_E_QUOTA_EXCEEDED
;
6255 if (nb_items
>= nb_allocated
)
6257 SIZE_T old_size
= nb_allocated
* item_size
, new_size
= old_size
* 2;
6258 if (!(buf
= ws_realloc_zero( heap
, buf
, old_size
, new_size
))) return WS_E_QUOTA_EXCEEDED
;
6262 if (desc
->type
== WS_UNION_TYPE
)
6264 hr
= read_type_union( reader
, desc
->typeDescription
, heap
, buf
+ offset
, item_size
, &found
);
6267 ws_free( heap
, buf
, nb_allocated
* item_size
);
6274 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->type
, desc
->itemLocalName
, desc
->itemNs
,
6275 desc
->typeDescription
, option
, heap
, buf
+ offset
, item_size
, &found
);
6276 if (hr
== WS_E_INVALID_FORMAT
) break;
6279 ws_free( heap
, buf
, nb_allocated
* item_size
);
6284 offset
+= item_size
;
6288 if (desc
->localName
&& ((hr
= read_type_next_node( reader
)) != S_OK
)) return hr
;
6290 if (desc
->itemRange
&& (nb_items
< desc
->itemRange
->minItemCount
|| nb_items
> desc
->itemRange
->maxItemCount
))
6292 TRACE( "number of items %u out of range (%u-%u)\n", nb_items
, desc
->itemRange
->minItemCount
,
6293 desc
->itemRange
->maxItemCount
);
6294 ws_free( heap
, buf
, nb_allocated
* item_size
);
6295 return WS_E_INVALID_FORMAT
;
6304 static HRESULT
read_type_text( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
,
6305 WS_READ_OPTION option
, WS_HEAP
*heap
, void *ret
, ULONG size
)
6307 struct reader_pos pos
;
6311 if (reader
->current
== reader
->last
)
6313 if ((hr
= read_to_startelement( reader
, &found
)) != S_OK
) return hr
;
6314 if (!found
) return WS_E_INVALID_FORMAT
;
6317 save_reader_position( reader
, &pos
);
6318 if ((hr
= read_next_node( reader
)) != S_OK
) return hr
;
6319 hr
= read_type( reader
, WS_ANY_ELEMENT_TYPE_MAPPING
, desc
->type
, NULL
, NULL
,
6320 desc
->typeDescription
, option
, heap
, ret
, size
, &found
);
6321 if (hr
== S_OK
&& !found
) restore_reader_position( reader
, &pos
);
6325 static HRESULT
read_type_field( struct reader
*reader
, const WS_STRUCT_DESCRIPTION
*desc_struct
,
6326 const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
, char *buf
, ULONG offset
)
6329 WS_READ_OPTION option
;
6334 if (!desc
) return E_INVALIDARG
;
6335 if (desc
->options
& ~(WS_FIELD_POINTER
|WS_FIELD_OPTIONAL
|WS_FIELD_NILLABLE
|WS_FIELD_NILLABLE_ITEM
))
6337 FIXME( "options %08x not supported\n", desc
->options
);
6340 if (!(option
= get_field_read_option( desc
->type
, desc
->options
))) return E_INVALIDARG
;
6342 if (option
== WS_READ_REQUIRED_VALUE
|| option
== WS_READ_NILLABLE_VALUE
)
6343 size
= get_type_size( desc
->type
, desc
->typeDescription
);
6345 size
= sizeof(void *);
6348 switch (desc
->mapping
)
6350 case WS_TYPE_ATTRIBUTE_FIELD_MAPPING
:
6351 hr
= read_type( reader
, WS_ATTRIBUTE_TYPE_MAPPING
, desc
->type
, desc
->localName
, desc
->ns
,
6352 desc_struct
, option
, heap
, ptr
, size
, &found
);
6355 case WS_ATTRIBUTE_FIELD_MAPPING
:
6356 hr
= read_type( reader
, WS_ATTRIBUTE_TYPE_MAPPING
, desc
->type
, desc
->localName
, desc
->ns
,
6357 desc
->typeDescription
, option
, heap
, ptr
, size
, &found
);
6360 case WS_ELEMENT_FIELD_MAPPING
:
6361 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->type
, desc
->localName
, desc
->ns
,
6362 desc
->typeDescription
, option
, heap
, ptr
, size
, &found
);
6365 case WS_ELEMENT_CHOICE_FIELD_MAPPING
:
6367 if (desc
->type
!= WS_UNION_TYPE
|| !desc
->typeDescription
||
6368 (desc
->options
& (WS_FIELD_POINTER
|WS_FIELD_NILLABLE
))) return E_INVALIDARG
;
6369 hr
= read_type_union( reader
, desc
->typeDescription
, heap
, ptr
, size
, &found
);
6372 case WS_REPEATING_ELEMENT_FIELD_MAPPING
:
6373 case WS_REPEATING_ELEMENT_CHOICE_FIELD_MAPPING
:
6376 hr
= read_type_array( reader
, desc
, heap
, (void **)ptr
, &count
);
6377 if (hr
== S_OK
) *(ULONG
*)(buf
+ desc
->countOffset
) = count
;
6380 case WS_TEXT_FIELD_MAPPING
:
6381 hr
= read_type_text( reader
, desc
, option
, heap
, ptr
, size
);
6385 FIXME( "unhandled field mapping %u\n", desc
->mapping
);
6389 if (hr
== WS_E_INVALID_FORMAT
)
6393 case WS_READ_REQUIRED_VALUE
:
6394 case WS_READ_REQUIRED_POINTER
:
6395 return WS_E_INVALID_FORMAT
;
6397 case WS_READ_NILLABLE_VALUE
:
6398 if (desc
->defaultValue
) memcpy( ptr
, desc
->defaultValue
->value
, desc
->defaultValue
->valueSize
);
6401 case WS_READ_OPTIONAL_POINTER
:
6402 case WS_READ_NILLABLE_POINTER
:
6403 *(void **)ptr
= NULL
;
6407 ERR( "unhandled option %u\n", option
);
6415 static HRESULT
read_type_struct( struct reader
*reader
, WS_TYPE_MAPPING mapping
, const WS_XML_STRING
*localname
,
6416 const WS_XML_STRING
*ns
, const WS_STRUCT_DESCRIPTION
*desc
, WS_READ_OPTION option
,
6417 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
6423 if (!desc
) return E_INVALIDARG
;
6424 if (desc
->structOptions
& ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
)
6426 FIXME( "struct options %08x not supported\n",
6427 desc
->structOptions
& ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
);
6432 case WS_READ_REQUIRED_POINTER
:
6433 case WS_READ_OPTIONAL_POINTER
:
6434 case WS_READ_NILLABLE_POINTER
:
6435 if (size
!= sizeof(void *)) return E_INVALIDARG
;
6436 if (!(buf
= ws_alloc_zero( heap
, desc
->size
))) return WS_E_QUOTA_EXCEEDED
;
6439 case WS_READ_REQUIRED_VALUE
:
6440 case WS_READ_NILLABLE_VALUE
:
6441 if (size
!= desc
->size
) return E_INVALIDARG
;
6446 FIXME( "unhandled read option %u\n", option
);
6450 for (i
= 0; i
< desc
->fieldCount
; i
++)
6452 offset
= desc
->fields
[i
]->offset
;
6453 if ((hr
= read_type_field( reader
, desc
, desc
->fields
[i
], heap
, buf
, offset
)) != S_OK
) break;
6458 case WS_READ_REQUIRED_POINTER
:
6461 ws_free( heap
, buf
, desc
->size
);
6464 *(char **)ret
= buf
;
6467 case WS_READ_OPTIONAL_POINTER
:
6468 case WS_READ_NILLABLE_POINTER
:
6469 if (is_nil_value( buf
, desc
->size
))
6471 ws_free( heap
, buf
, desc
->size
);
6474 *(char **)ret
= buf
;
6477 case WS_READ_REQUIRED_VALUE
:
6478 case WS_READ_NILLABLE_VALUE
:
6479 if (hr
!= S_OK
) return hr
;
6483 ERR( "unhandled read option %u\n", option
);
6487 if (desc
->structOptions
& WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
)
6489 struct node
*parent
= find_parent( reader
);
6490 parent
->flags
|= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT
;
6497 static HRESULT
start_mapping( struct reader
*reader
, WS_TYPE_MAPPING mapping
, const WS_XML_STRING
*localname
,
6498 const WS_XML_STRING
*ns
)
6502 case WS_ELEMENT_TYPE_MAPPING
:
6503 case WS_ELEMENT_CONTENT_TYPE_MAPPING
:
6504 return read_type_next_element_node( reader
, localname
, ns
);
6506 case WS_ANY_ELEMENT_TYPE_MAPPING
:
6507 case WS_ATTRIBUTE_TYPE_MAPPING
:
6511 FIXME( "unhandled mapping %u\n", mapping
);
6516 static HRESULT
read_type_endelement_node( struct reader
*reader
)
6518 const struct node
*parent
= find_parent( reader
);
6523 if ((hr
= read_type_next_node( reader
)) != S_OK
) return hr
;
6524 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_END_ELEMENT
&& reader
->current
->parent
== parent
)
6528 if (read_end_of_data( reader
) || !(parent
->flags
& NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT
)) break;
6531 return WS_E_INVALID_FORMAT
;
6534 static HRESULT
end_mapping( struct reader
*reader
, WS_TYPE_MAPPING mapping
)
6538 case WS_ELEMENT_TYPE_MAPPING
:
6539 return read_type_endelement_node( reader
);
6541 case WS_ELEMENT_CONTENT_TYPE_MAPPING
:
6542 return read_type_next_node( reader
);
6544 case WS_ATTRIBUTE_TYPE_MAPPING
:
6550 static BOOL
is_true_text( const WS_XML_TEXT
*text
)
6552 switch (text
->textType
)
6554 case WS_XML_TEXT_TYPE_UTF8
:
6556 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
6557 if (text_utf8
->value
.length
== 4 && !memcmp( text_utf8
->value
.bytes
, "true", 4 )) return TRUE
;
6560 case WS_XML_TEXT_TYPE_BOOL
:
6562 const WS_XML_BOOL_TEXT
*text_bool
= (const WS_XML_BOOL_TEXT
*)text
;
6563 return text_bool
->value
;
6566 ERR( "unhandled text type %u\n", text
->textType
);
6571 static HRESULT
is_nil_element( const WS_XML_ELEMENT_NODE
*elem
)
6573 static const WS_XML_STRING localname
= {3, (BYTE
*)"nil"};
6574 static const WS_XML_STRING ns
= {41, (BYTE
*)"http://www.w3.org/2001/XMLSchema-instance"};
6577 for (i
= 0; i
< elem
->attributeCount
; i
++)
6579 if (elem
->attributes
[i
]->isXmlNs
) continue;
6580 if (WsXmlStringEquals( elem
->attributes
[i
]->localName
, &localname
, NULL
) == S_OK
&&
6581 WsXmlStringEquals( elem
->attributes
[i
]->ns
, &ns
, NULL
) == S_OK
&&
6582 is_true_text( elem
->attributes
[i
]->value
)) return TRUE
;
6587 static HRESULT
read_type( struct reader
*reader
, WS_TYPE_MAPPING mapping
, WS_TYPE type
,
6588 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
, const void *desc
,
6589 WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
, ULONG size
, BOOL
*found
)
6593 if ((hr
= start_mapping( reader
, mapping
, localname
, ns
)) != S_OK
) return hr
;
6595 if (mapping
== WS_ELEMENT_TYPE_MAPPING
&& is_nil_element( &reader
->current
->hdr
))
6597 if (option
!= WS_READ_NILLABLE_POINTER
&& option
!= WS_READ_NILLABLE_VALUE
) return WS_E_INVALID_FORMAT
;
6599 return end_mapping( reader
, mapping
);
6605 hr
= read_type_bool( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6609 hr
= read_type_int8( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6613 hr
= read_type_int16( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6617 hr
= read_type_int32( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6621 hr
= read_type_int64( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6625 hr
= read_type_uint8( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6628 case WS_UINT16_TYPE
:
6629 hr
= read_type_uint16( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6632 case WS_UINT32_TYPE
:
6633 hr
= read_type_uint32( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6636 case WS_UINT64_TYPE
:
6637 hr
= read_type_uint64( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6641 hr
= read_type_float( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6644 case WS_DOUBLE_TYPE
:
6645 hr
= read_type_double( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6648 case WS_DATETIME_TYPE
:
6649 hr
= read_type_datetime( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6653 hr
= read_type_guid( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6656 case WS_UNIQUE_ID_TYPE
:
6657 hr
= read_type_unique_id( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6660 case WS_STRING_TYPE
:
6661 hr
= read_type_string( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6665 hr
= read_type_wsz( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6669 hr
= read_type_bytes( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6672 case WS_XML_STRING_TYPE
:
6673 hr
= read_type_xml_string( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6676 case WS_XML_QNAME_TYPE
:
6677 hr
= read_type_qname( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6680 case WS_DESCRIPTION_TYPE
:
6681 hr
= read_type_description( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6684 case WS_STRUCT_TYPE
:
6685 hr
= read_type_struct( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6689 hr
= read_type_enum( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6693 FIXME( "type %u not supported\n", type
);
6697 if (hr
!= S_OK
) return hr
;
6698 return end_mapping( reader
, mapping
);
6701 /**************************************************************************
6702 * WsReadType [webservices.@]
6704 HRESULT WINAPI
WsReadType( WS_XML_READER
*handle
, WS_TYPE_MAPPING mapping
, WS_TYPE type
,
6705 const void *desc
, WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
,
6706 ULONG size
, WS_ERROR
*error
)
6708 struct reader
*reader
= (struct reader
*)handle
;
6712 TRACE( "%p %u %u %p %u %p %p %u %p\n", handle
, mapping
, type
, desc
, option
, heap
, value
,
6714 if (error
) FIXME( "ignoring error parameter\n" );
6716 if (!reader
|| !value
) return E_INVALIDARG
;
6718 EnterCriticalSection( &reader
->cs
);
6720 if (reader
->magic
!= READER_MAGIC
)
6722 LeaveCriticalSection( &reader
->cs
);
6723 return E_INVALIDARG
;
6726 if ((hr
= read_type( reader
, mapping
, type
, NULL
, NULL
, desc
, option
, heap
, value
, size
, &found
)) == S_OK
)
6730 case WS_ELEMENT_TYPE_MAPPING
:
6731 hr
= read_node( reader
);
6737 if (hr
== S_OK
&& !read_end_of_data( reader
)) hr
= WS_E_INVALID_FORMAT
;
6740 LeaveCriticalSection( &reader
->cs
);
6741 TRACE( "returning %08x\n", hr
);
6745 HRESULT
read_header( WS_XML_READER
*handle
, const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
6746 WS_TYPE type
, const void *desc
, WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
,
6749 struct reader
*reader
= (struct reader
*)handle
;
6753 EnterCriticalSection( &reader
->cs
);
6755 if (reader
->magic
!= READER_MAGIC
)
6757 LeaveCriticalSection( &reader
->cs
);
6758 return E_INVALIDARG
;
6761 hr
= read_type( reader
, WS_ELEMENT_CONTENT_TYPE_MAPPING
, type
, localname
, ns
, desc
, option
, heap
,
6762 value
, size
, &found
);
6764 LeaveCriticalSection( &reader
->cs
);
6768 /**************************************************************************
6769 * WsReadElement [webservices.@]
6771 HRESULT WINAPI
WsReadElement( WS_XML_READER
*handle
, const WS_ELEMENT_DESCRIPTION
*desc
,
6772 WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
, ULONG size
,
6775 struct reader
*reader
= (struct reader
*)handle
;
6779 TRACE( "%p %p %u %p %p %u %p\n", handle
, desc
, option
, heap
, value
, size
, error
);
6780 if (error
) FIXME( "ignoring error parameter\n" );
6782 if (!reader
|| !desc
|| !value
) return E_INVALIDARG
;
6784 EnterCriticalSection( &reader
->cs
);
6786 if (reader
->magic
!= READER_MAGIC
)
6788 LeaveCriticalSection( &reader
->cs
);
6789 return E_INVALIDARG
;
6792 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->type
, desc
->elementLocalName
,
6793 desc
->elementNs
, desc
->typeDescription
, option
, heap
, value
, size
, &found
);
6795 LeaveCriticalSection( &reader
->cs
);
6796 TRACE( "returning %08x\n", hr
);
6800 /**************************************************************************
6801 * WsReadValue [webservices.@]
6803 HRESULT WINAPI
WsReadValue( WS_XML_READER
*handle
, WS_VALUE_TYPE value_type
, void *value
, ULONG size
,
6806 struct reader
*reader
= (struct reader
*)handle
;
6807 WS_TYPE type
= map_value_type( value_type
);
6811 TRACE( "%p %u %p %u %p\n", handle
, type
, value
, size
, error
);
6812 if (error
) FIXME( "ignoring error parameter\n" );
6814 if (!reader
|| !value
|| type
== ~0u) return E_INVALIDARG
;
6816 EnterCriticalSection( &reader
->cs
);
6818 if (reader
->magic
!= READER_MAGIC
)
6820 LeaveCriticalSection( &reader
->cs
);
6821 return E_INVALIDARG
;
6824 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, type
, NULL
, NULL
, NULL
, WS_READ_REQUIRED_VALUE
,
6825 NULL
, value
, size
, &found
);
6827 LeaveCriticalSection( &reader
->cs
);
6828 TRACE( "returning %08x\n", hr
);
6832 /**************************************************************************
6833 * WsReadAttribute [webservices.@]
6835 HRESULT WINAPI
WsReadAttribute( WS_XML_READER
*handle
, const WS_ATTRIBUTE_DESCRIPTION
*desc
,
6836 WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
, ULONG size
,
6839 struct reader
*reader
= (struct reader
*)handle
;
6843 TRACE( "%p %p %u %p %p %u %p\n", handle
, desc
, option
, heap
, value
, size
, error
);
6844 if (error
) FIXME( "ignoring error parameter\n" );
6846 if (!reader
|| !desc
|| !value
) return E_INVALIDARG
;
6848 EnterCriticalSection( &reader
->cs
);
6850 if (reader
->magic
!= READER_MAGIC
)
6852 LeaveCriticalSection( &reader
->cs
);
6853 return E_INVALIDARG
;
6856 if (!reader
->input_type
) hr
= WS_E_INVALID_OPERATION
;
6857 else hr
= read_type( reader
, WS_ATTRIBUTE_TYPE_MAPPING
, desc
->type
, desc
->attributeLocalName
,
6858 desc
->attributeNs
, desc
->typeDescription
, option
, heap
, value
, size
, &found
);
6860 LeaveCriticalSection( &reader
->cs
);
6861 TRACE( "returning %08x\n", hr
);
6865 static inline BOOL
is_utf8( const unsigned char *data
, ULONG size
, ULONG
*offset
)
6867 static const char bom
[] = {0xef,0xbb,0xbf};
6868 return (size
>= sizeof(bom
) && !memcmp( data
, bom
, sizeof(bom
) ) && (*offset
= sizeof(bom
))) ||
6869 (size
> 2 && !(*offset
= 0));
6872 static inline BOOL
is_utf16le( const unsigned char *data
, ULONG size
, ULONG
*offset
)
6874 static const char bom
[] = {0xff,0xfe};
6875 return (size
>= sizeof(bom
) && !memcmp( data
, bom
, sizeof(bom
) ) && (*offset
= sizeof(bom
))) ||
6876 (size
>= 4 && data
[0] == '<' && !data
[1] && !(*offset
= 0));
6879 static WS_CHARSET
detect_charset( const unsigned char *data
, ULONG size
, ULONG
*offset
)
6883 /* FIXME: parse xml declaration */
6885 if (is_utf16le( data
, size
, offset
)) ret
= WS_CHARSET_UTF16LE
;
6886 else if (is_utf8( data
, size
, offset
)) ret
= WS_CHARSET_UTF8
;
6889 FIXME( "charset not recognized\n" );
6893 TRACE( "detected charset %u\n", ret
);
6897 static HRESULT
utf16le_to_utf8( const unsigned char *data
, ULONG size
, unsigned char **buf
, ULONG
*buflen
)
6899 if (size
% sizeof(WCHAR
)) return E_INVALIDARG
;
6900 *buflen
= WideCharToMultiByte( CP_UTF8
, 0, (const WCHAR
*)data
, size
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
6901 if (!(*buf
= heap_alloc( *buflen
))) return E_OUTOFMEMORY
;
6902 WideCharToMultiByte( CP_UTF8
, 0, (const WCHAR
*)data
, size
/ sizeof(WCHAR
), (char *)*buf
, *buflen
, NULL
, NULL
);
6906 static HRESULT
set_input_buffer( struct reader
*reader
, const unsigned char *data
, ULONG size
)
6908 reader
->input_type
= WS_XML_READER_INPUT_TYPE_BUFFER
;
6909 reader
->input_buf
= NULL
;
6911 if (reader
->input_enc
== WS_XML_READER_ENCODING_TYPE_TEXT
&& reader
->input_charset
== WS_CHARSET_UTF16LE
)
6917 if ((hr
= utf16le_to_utf8( data
, size
, &buf
, &buflen
)) != S_OK
) return hr
;
6918 heap_free( reader
->input_conv
);
6919 reader
->read_bufptr
= reader
->input_conv
= buf
;
6920 reader
->read_size
= reader
->input_size
= buflen
;
6924 reader
->read_bufptr
= data
;
6925 reader
->read_size
= reader
->input_size
= size
;
6928 reader
->read_pos
= 0;
6929 reader
->text_conv_offset
= 0;
6933 static void set_input_stream( struct reader
*reader
, WS_READ_CALLBACK callback
, void *state
)
6935 reader
->input_type
= WS_XML_READER_INPUT_TYPE_STREAM
;
6936 reader
->input_cb
= callback
;
6937 reader
->input_cb_state
= state
;
6938 reader
->input_buf
= NULL
;
6939 reader
->input_size
= STREAM_BUFSIZE
;
6941 if (reader
->read_pos
>= reader
->read_size
) reader
->read_size
= 0;
6944 memmove( reader
->stream_buf
, reader
->stream_buf
+ reader
->read_pos
, reader
->read_size
- reader
->read_pos
);
6945 reader
->read_size
-= reader
->read_pos
;
6947 reader
->read_pos
= 0;
6948 reader
->read_bufptr
= reader
->stream_buf
;
6949 reader
->text_conv_offset
= 0;
6952 /**************************************************************************
6953 * WsSetInput [webservices.@]
6955 HRESULT WINAPI
WsSetInput( WS_XML_READER
*handle
, const WS_XML_READER_ENCODING
*encoding
,
6956 const WS_XML_READER_INPUT
*input
, const WS_XML_READER_PROPERTY
*properties
,
6957 ULONG count
, WS_ERROR
*error
)
6959 struct reader
*reader
= (struct reader
*)handle
;
6961 ULONG i
, offset
= 0;
6964 TRACE( "%p %p %p %p %u %p\n", handle
, encoding
, input
, properties
, count
, error
);
6965 if (error
) FIXME( "ignoring error parameter\n" );
6967 if (!reader
) return E_INVALIDARG
;
6969 EnterCriticalSection( &reader
->cs
);
6971 if (reader
->magic
!= READER_MAGIC
)
6973 LeaveCriticalSection( &reader
->cs
);
6974 return E_INVALIDARG
;
6977 for (i
= 0; i
< count
; i
++)
6979 hr
= prop_set( reader
->prop
, reader
->prop_count
, properties
[i
].id
, properties
[i
].value
,
6980 properties
[i
].valueSize
);
6981 if (hr
!= S_OK
) goto done
;
6984 if ((hr
= init_reader( reader
)) != S_OK
) goto done
;
6986 switch (encoding
->encodingType
)
6988 case WS_XML_READER_ENCODING_TYPE_TEXT
:
6990 if (input
->inputType
== WS_XML_READER_INPUT_TYPE_BUFFER
)
6992 const WS_XML_READER_TEXT_ENCODING
*text
= (const WS_XML_READER_TEXT_ENCODING
*)encoding
;
6993 const WS_XML_READER_BUFFER_INPUT
*buf
= (const WS_XML_READER_BUFFER_INPUT
*)input
;
6994 if (text
->charSet
!= WS_CHARSET_AUTO
) reader
->input_charset
= text
->charSet
;
6995 else reader
->input_charset
= detect_charset( buf
->encodedData
, buf
->encodedDataSize
, &offset
);
6998 reader
->input_enc
= WS_XML_READER_ENCODING_TYPE_TEXT
;
7001 case WS_XML_READER_ENCODING_TYPE_BINARY
:
7003 const WS_XML_READER_BINARY_ENCODING
*bin
= (const WS_XML_READER_BINARY_ENCODING
*)encoding
;
7004 reader
->input_enc
= WS_XML_READER_ENCODING_TYPE_BINARY
;
7005 reader
->input_charset
= 0;
7006 reader
->dict_static
= bin
->staticDictionary
? bin
->staticDictionary
: &dict_builtin_static
.dict
;
7007 reader
->dict
= bin
->dynamicDictionary
? bin
->dynamicDictionary
: &dict_builtin
.dict
;
7011 FIXME( "encoding type %u not supported\n", encoding
->encodingType
);
7016 switch (input
->inputType
)
7018 case WS_XML_READER_INPUT_TYPE_BUFFER
:
7020 const WS_XML_READER_BUFFER_INPUT
*buf
= (const WS_XML_READER_BUFFER_INPUT
*)input
;
7021 hr
= set_input_buffer( reader
, (const unsigned char *)buf
->encodedData
+ offset
, buf
->encodedDataSize
- offset
);
7022 if (hr
!= S_OK
) goto done
;
7025 case WS_XML_READER_INPUT_TYPE_STREAM
:
7027 const WS_XML_READER_STREAM_INPUT
*stream
= (const WS_XML_READER_STREAM_INPUT
*)input
;
7028 if (!reader
->stream_buf
&& !(reader
->stream_buf
= heap_alloc( STREAM_BUFSIZE
)))
7033 set_input_stream( reader
, stream
->readCallback
, stream
->readCallbackState
);
7037 FIXME( "input type %u not supported\n", input
->inputType
);
7042 if (!(node
= alloc_node( WS_XML_NODE_TYPE_BOF
))) hr
= E_OUTOFMEMORY
;
7043 else read_insert_bof( reader
, node
);
7046 LeaveCriticalSection( &reader
->cs
);
7047 TRACE( "returning %08x\n", hr
);
7051 static HRESULT
set_input_xml_buffer( struct reader
*reader
, struct xmlbuf
*xmlbuf
)
7053 reader
->input_type
= WS_XML_READER_INPUT_TYPE_BUFFER
;
7054 reader
->input_buf
= xmlbuf
;
7055 reader
->input_enc
= xmlbuf
->encoding
;
7056 reader
->input_charset
= xmlbuf
->charset
;
7057 reader
->dict_static
= xmlbuf
->dict_static
;
7058 reader
->dict
= xmlbuf
->dict
;
7060 if (reader
->input_enc
== WS_XML_READER_ENCODING_TYPE_TEXT
&& reader
->input_charset
== WS_CHARSET_UTF16LE
)
7066 if ((hr
= utf16le_to_utf8( xmlbuf
->bytes
.bytes
, xmlbuf
->bytes
.length
, &buf
, &buflen
)) != S_OK
) return hr
;
7067 heap_free( reader
->input_conv
);
7068 reader
->read_bufptr
= reader
->input_conv
= buf
;
7069 reader
->read_size
= reader
->input_size
= buflen
;
7073 reader
->read_bufptr
= xmlbuf
->bytes
.bytes
;
7074 reader
->read_size
= reader
->input_size
= xmlbuf
->bytes
.length
;
7077 reader
->read_pos
= 0;
7078 reader
->text_conv_offset
= 0;
7082 /**************************************************************************
7083 * WsSetInputToBuffer [webservices.@]
7085 HRESULT WINAPI
WsSetInputToBuffer( WS_XML_READER
*handle
, WS_XML_BUFFER
*buffer
,
7086 const WS_XML_READER_PROPERTY
*properties
, ULONG count
,
7089 struct reader
*reader
= (struct reader
*)handle
;
7090 struct xmlbuf
*xmlbuf
= (struct xmlbuf
*)buffer
;
7095 TRACE( "%p %p %p %u %p\n", handle
, buffer
, properties
, count
, error
);
7096 if (error
) FIXME( "ignoring error parameter\n" );
7098 if (!reader
|| !xmlbuf
) return E_INVALIDARG
;
7100 EnterCriticalSection( &reader
->cs
);
7102 if (reader
->magic
!= READER_MAGIC
)
7104 LeaveCriticalSection( &reader
->cs
);
7105 return E_INVALIDARG
;
7108 for (i
= 0; i
< count
; i
++)
7110 hr
= prop_set( reader
->prop
, reader
->prop_count
, properties
[i
].id
, properties
[i
].value
,
7111 properties
[i
].valueSize
);
7112 if (hr
!= S_OK
) goto done
;
7115 if ((hr
= init_reader( reader
)) != S_OK
) goto done
;
7116 if ((hr
= set_input_xml_buffer( reader
, xmlbuf
)) != S_OK
) goto done
;
7118 if (!(node
= alloc_node( WS_XML_NODE_TYPE_BOF
))) hr
= E_OUTOFMEMORY
;
7119 else read_insert_bof( reader
, node
);
7122 LeaveCriticalSection( &reader
->cs
);
7123 TRACE( "returning %08x\n", hr
);
7127 /**************************************************************************
7128 * WsGetReaderPosition [webservices.@]
7130 HRESULT WINAPI
WsGetReaderPosition( WS_XML_READER
*handle
, WS_XML_NODE_POSITION
*pos
, WS_ERROR
*error
)
7132 struct reader
*reader
= (struct reader
*)handle
;
7135 TRACE( "%p %p %p\n", handle
, pos
, error
);
7136 if (error
) FIXME( "ignoring error parameter\n" );
7138 if (!reader
|| !pos
) return E_INVALIDARG
;
7140 EnterCriticalSection( &reader
->cs
);
7142 if (reader
->magic
!= READER_MAGIC
)
7144 LeaveCriticalSection( &reader
->cs
);
7145 return E_INVALIDARG
;
7148 if (!reader
->input_buf
) hr
= WS_E_INVALID_OPERATION
;
7151 pos
->buffer
= (WS_XML_BUFFER
*)reader
->input_buf
;
7152 pos
->node
= reader
->current
;
7155 LeaveCriticalSection( &reader
->cs
);
7156 TRACE( "returning %08x\n", hr
);
7160 /**************************************************************************
7161 * WsSetReaderPosition [webservices.@]
7163 HRESULT WINAPI
WsSetReaderPosition( WS_XML_READER
*handle
, const WS_XML_NODE_POSITION
*pos
, WS_ERROR
*error
)
7165 struct reader
*reader
= (struct reader
*)handle
;
7168 TRACE( "%p %p %p\n", handle
, pos
, error
);
7169 if (error
) FIXME( "ignoring error parameter\n" );
7171 if (!reader
|| !pos
) return E_INVALIDARG
;
7173 EnterCriticalSection( &reader
->cs
);
7175 if (reader
->magic
!= READER_MAGIC
|| (struct xmlbuf
*)pos
->buffer
!= reader
->input_buf
)
7177 LeaveCriticalSection( &reader
->cs
);
7178 return E_INVALIDARG
;
7181 if (!reader
->input_buf
) hr
= WS_E_INVALID_OPERATION
;
7182 else reader
->current
= pos
->node
;
7184 LeaveCriticalSection( &reader
->cs
);
7185 TRACE( "returning %08x\n", hr
);
7189 static HRESULT
utf8_to_base64( const WS_XML_UTF8_TEXT
*utf8
, WS_XML_BASE64_TEXT
*base64
)
7191 if (utf8
->value
.length
% 4) return WS_E_INVALID_FORMAT
;
7192 if (!(base64
->bytes
= heap_alloc( utf8
->value
.length
* 3 / 4 ))) return E_OUTOFMEMORY
;
7193 base64
->length
= decode_base64( utf8
->value
.bytes
, utf8
->value
.length
, base64
->bytes
);
7197 /**************************************************************************
7198 * WsReadBytes [webservices.@]
7200 HRESULT WINAPI
WsReadBytes( WS_XML_READER
*handle
, void *bytes
, ULONG max_count
, ULONG
*count
, WS_ERROR
*error
)
7202 struct reader
*reader
= (struct reader
*)handle
;
7205 TRACE( "%p %p %u %p %p\n", handle
, bytes
, max_count
, count
, error
);
7206 if (error
) FIXME( "ignoring error parameter\n" );
7208 if (!reader
) return E_INVALIDARG
;
7210 EnterCriticalSection( &reader
->cs
);
7212 if (reader
->magic
!= READER_MAGIC
)
7214 LeaveCriticalSection( &reader
->cs
);
7215 return E_INVALIDARG
;
7218 if (!reader
->input_type
)
7220 hr
= WS_E_INVALID_OPERATION
;
7230 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
&& bytes
)
7232 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)reader
->current
;
7233 WS_XML_BASE64_TEXT base64
;
7235 if ((hr
= utf8_to_base64( (const WS_XML_UTF8_TEXT
*)text
->text
, &base64
)) != S_OK
) goto done
;
7236 if (reader
->text_conv_offset
== base64
.length
)
7238 heap_free( base64
.bytes
);
7239 hr
= read_node( reader
);
7242 *count
= min( base64
.length
- reader
->text_conv_offset
, max_count
);
7243 memcpy( bytes
, base64
.bytes
+ reader
->text_conv_offset
, *count
);
7244 reader
->text_conv_offset
+= *count
;
7245 heap_free( base64
.bytes
);
7249 LeaveCriticalSection( &reader
->cs
);
7250 TRACE( "returning %08x\n", hr
);
7254 static HRESULT
utf8_to_utf16( const WS_XML_UTF8_TEXT
*utf8
, WS_XML_UTF16_TEXT
*utf16
)
7256 int len
= MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, NULL
, 0 );
7257 if (!(utf16
->bytes
= heap_alloc( len
* sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
7258 MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, (WCHAR
*)utf16
->bytes
, len
);
7259 utf16
->byteCount
= len
* sizeof(WCHAR
);
7263 /**************************************************************************
7264 * WsReadChars [webservices.@]
7266 HRESULT WINAPI
WsReadChars( WS_XML_READER
*handle
, WCHAR
*chars
, ULONG max_count
, ULONG
*count
, WS_ERROR
*error
)
7268 struct reader
*reader
= (struct reader
*)handle
;
7271 TRACE( "%p %p %u %p %p\n", handle
, chars
, max_count
, count
, error
);
7272 if (error
) FIXME( "ignoring error parameter\n" );
7274 if (!reader
) return E_INVALIDARG
;
7276 EnterCriticalSection( &reader
->cs
);
7278 if (reader
->magic
!= READER_MAGIC
)
7280 LeaveCriticalSection( &reader
->cs
);
7281 return E_INVALIDARG
;
7284 if (!reader
->input_type
)
7286 hr
= WS_E_INVALID_OPERATION
;
7296 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
&& chars
)
7298 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)reader
->current
;
7299 WS_XML_UTF16_TEXT utf16
;
7302 if ((hr
= utf8_to_utf16( (const WS_XML_UTF8_TEXT
*)text
->text
, &utf16
)) != S_OK
) goto done
;
7303 if (reader
->text_conv_offset
== utf16
.byteCount
/ sizeof(WCHAR
))
7305 heap_free( utf16
.bytes
);
7306 hr
= read_node( reader
);
7309 *count
= min( utf16
.byteCount
/ sizeof(WCHAR
) - reader
->text_conv_offset
, max_count
);
7310 memcpy( chars
, utf16
.bytes
+ reader
->text_conv_offset
* sizeof(WCHAR
), *count
* sizeof(WCHAR
) );
7311 reader
->text_conv_offset
+= *count
;
7312 heap_free( utf16
.bytes
);
7316 LeaveCriticalSection( &reader
->cs
);
7317 TRACE( "returning %08x\n", hr
);
7321 /**************************************************************************
7322 * WsReadCharsUtf8 [webservices.@]
7324 HRESULT WINAPI
WsReadCharsUtf8( WS_XML_READER
*handle
, BYTE
*bytes
, ULONG max_count
, ULONG
*count
, WS_ERROR
*error
)
7326 struct reader
*reader
= (struct reader
*)handle
;
7329 TRACE( "%p %p %u %p %p\n", handle
, bytes
, max_count
, count
, error
);
7330 if (error
) FIXME( "ignoring error parameter\n" );
7332 if (!reader
) return E_INVALIDARG
;
7334 EnterCriticalSection( &reader
->cs
);
7336 if (reader
->magic
!= READER_MAGIC
)
7338 LeaveCriticalSection( &reader
->cs
);
7339 return E_INVALIDARG
;
7342 if (!reader
->input_type
)
7344 hr
= WS_E_INVALID_OPERATION
;
7354 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
&& bytes
)
7356 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)reader
->current
;
7357 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
->text
;
7359 if (reader
->text_conv_offset
== utf8
->value
.length
)
7361 hr
= read_node( reader
);
7364 *count
= min( utf8
->value
.length
- reader
->text_conv_offset
, max_count
);
7365 memcpy( bytes
, utf8
->value
.bytes
+ reader
->text_conv_offset
, *count
);
7366 reader
->text_conv_offset
+= *count
;
7370 LeaveCriticalSection( &reader
->cs
);
7371 TRACE( "returning %08x\n", hr
);
7375 static HRESULT
move_to_element( struct reader
*reader
)
7378 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_BOF
&&
7379 (hr
= read_move_to( reader
, WS_MOVE_TO_CHILD_NODE
, NULL
)) != S_OK
) return hr
;
7380 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) return E_FAIL
;
7384 static HRESULT
copy_tree( struct reader
*reader
, WS_XML_WRITER
*writer
)
7386 const struct node
*node
, *parent
;
7390 if ((hr
= move_to_element( reader
)) != S_OK
) return hr
;
7391 parent
= reader
->current
;
7394 node
= reader
->current
;
7395 if ((hr
= WsWriteNode( writer
, (const WS_XML_NODE
*)node
, NULL
)) != S_OK
) break;
7396 if (node_type( node
) == WS_XML_NODE_TYPE_END_ELEMENT
&& node
->parent
== parent
) done
= TRUE
;
7397 if ((hr
= read_next_node( reader
)) != S_OK
|| done
) break;
7402 /**************************************************************************
7403 * WsReadXmlBuffer [webservices.@]
7405 HRESULT WINAPI
WsReadXmlBuffer( WS_XML_READER
*handle
, WS_HEAP
*heap
, WS_XML_BUFFER
**ret
, WS_ERROR
*error
)
7407 struct reader
*reader
= (struct reader
*)handle
;
7408 WS_XML_WRITER
*writer
= NULL
;
7409 WS_XML_BUFFER
*buffer
= NULL
;
7412 TRACE( "%p %p %p %p\n", handle
, heap
, ret
, error
);
7413 if (error
) FIXME( "ignoring error parameter\n" );
7415 if (!reader
|| !heap
) return E_INVALIDARG
;
7416 if (!ret
) return E_FAIL
;
7418 EnterCriticalSection( &reader
->cs
);
7420 if (reader
->magic
!= READER_MAGIC
)
7422 LeaveCriticalSection( &reader
->cs
);
7423 return E_INVALIDARG
;
7426 if (!reader
->input_type
) hr
= WS_E_INVALID_OPERATION
;
7429 if ((hr
= WsCreateWriter( NULL
, 0, &writer
, NULL
)) != S_OK
) goto done
;
7430 if ((hr
= WsCreateXmlBuffer( heap
, NULL
, 0, &buffer
, NULL
)) != S_OK
) goto done
;
7431 if ((hr
= WsSetOutputToBuffer( writer
, buffer
, NULL
, 0, NULL
)) != S_OK
) goto done
;
7432 if ((hr
= copy_tree( reader
, writer
)) == S_OK
) *ret
= buffer
;
7436 if (hr
!= S_OK
) free_xmlbuf( (struct xmlbuf
*)buffer
);
7437 WsFreeWriter( writer
);
7438 LeaveCriticalSection( &reader
->cs
);
7439 TRACE( "returning %08x\n", hr
);
7443 HRESULT
create_header_buffer( WS_XML_READER
*handle
, WS_HEAP
*heap
, WS_XML_BUFFER
**ret
)
7445 struct reader
*reader
= (struct reader
*)handle
;
7446 HRESULT hr
= WS_E_QUOTA_EXCEEDED
;
7447 struct xmlbuf
*xmlbuf
;
7449 EnterCriticalSection( &reader
->cs
);
7451 if (reader
->magic
!= READER_MAGIC
)
7453 LeaveCriticalSection( &reader
->cs
);
7454 return E_INVALIDARG
;
7457 if ((xmlbuf
= alloc_xmlbuf( heap
, reader
->read_pos
, reader
->input_enc
, reader
->input_charset
,
7458 reader
->dict_static
, reader
->dict
)))
7460 memcpy( xmlbuf
->bytes
.bytes
, reader
->read_bufptr
, reader
->read_pos
);
7461 xmlbuf
->bytes
.length
= reader
->read_pos
;
7462 *ret
= (WS_XML_BUFFER
*)xmlbuf
;
7466 LeaveCriticalSection( &reader
->cs
);
7470 HRESULT
get_param_desc( const WS_STRUCT_DESCRIPTION
*desc
, USHORT index
, const WS_FIELD_DESCRIPTION
**ret
)
7472 if (index
>= desc
->fieldCount
) return E_INVALIDARG
;
7473 *ret
= desc
->fields
[index
];
7477 static ULONG
get_field_size( const WS_FIELD_DESCRIPTION
*desc
)
7479 if (desc
->options
& WS_FIELD_POINTER
) return sizeof(void *);
7480 return get_type_size( desc
->type
, desc
->typeDescription
);
7483 static HRESULT
read_param( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
, void *ret
)
7485 if (!ret
&& !(ret
= ws_alloc_zero( heap
, get_field_size(desc
) ))) return WS_E_QUOTA_EXCEEDED
;
7486 return read_type_field( reader
, NULL
, desc
, heap
, ret
, 0 );
7489 static HRESULT
read_param_array( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
,
7490 void **ret
, ULONG
*count
)
7492 if (!ret
&& !(ret
= ws_alloc_zero( heap
, sizeof(void **) ))) return WS_E_QUOTA_EXCEEDED
;
7493 return read_type_array( reader
, desc
, heap
, ret
, count
);
7496 static void set_array_len( const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, ULONG index
, ULONG len
,
7500 for (i
= 0; i
< count
; i
++)
7502 if (params
[i
].outputMessageIndex
!= index
|| params
[i
].parameterType
!= WS_PARAMETER_TYPE_ARRAY_COUNT
)
7504 if ((ptr
= *(ULONG
**)args
[i
])) *ptr
= len
;
7509 HRESULT
read_output_params( WS_XML_READER
*handle
, WS_HEAP
*heap
, const WS_ELEMENT_DESCRIPTION
*desc
,
7510 const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
7512 struct reader
*reader
= (struct reader
*)handle
;
7513 const WS_STRUCT_DESCRIPTION
*desc_struct
;
7514 const WS_FIELD_DESCRIPTION
*desc_field
;
7518 if (desc
->type
!= WS_STRUCT_TYPE
|| !(desc_struct
= desc
->typeDescription
)) return E_INVALIDARG
;
7520 EnterCriticalSection( &reader
->cs
);
7522 if (reader
->magic
!= READER_MAGIC
)
7524 LeaveCriticalSection( &reader
->cs
);
7525 return E_INVALIDARG
;
7528 if ((hr
= start_mapping( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->elementLocalName
, desc
->elementNs
)) != S_OK
)
7531 for (i
= 0; i
< count
; i
++)
7533 if (params
[i
].outputMessageIndex
== INVALID_PARAMETER_INDEX
) continue;
7534 if (params
[i
].parameterType
== WS_PARAMETER_TYPE_MESSAGES
)
7536 FIXME( "messages type not supported\n" );
7540 if ((hr
= get_param_desc( desc_struct
, params
[i
].outputMessageIndex
, &desc_field
)) != S_OK
) goto done
;
7541 if (params
[i
].parameterType
== WS_PARAMETER_TYPE_NORMAL
)
7543 void *ptr
= *(void **)args
[i
];
7544 if ((hr
= read_param( reader
, desc_field
, heap
, ptr
)) != S_OK
) goto done
;
7546 else if (params
[i
].parameterType
== WS_PARAMETER_TYPE_ARRAY
)
7548 void **ptr
= *(void ***)args
[i
];
7549 if ((hr
= read_param_array( reader
, desc_field
, heap
, ptr
, &len
)) != S_OK
) goto done
;
7550 set_array_len( params
, count
, params
[i
].outputMessageIndex
, len
, args
);
7554 if (desc_struct
->structOptions
& WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
)
7556 struct node
*parent
= find_parent( reader
);
7557 parent
->flags
|= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT
;
7560 hr
= end_mapping( reader
, WS_ELEMENT_TYPE_MAPPING
);
7563 LeaveCriticalSection( &reader
->cs
);