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
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/heap.h"
29 #include "wine/list.h"
30 #include "webservices_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(webservices
);
34 ULONG
prop_size( const struct prop_desc
*desc
, ULONG count
)
36 ULONG i
, ret
= count
* sizeof(struct prop
);
37 for (i
= 0; i
< count
; i
++) ret
+= desc
[i
].size
;
41 void prop_init( const struct prop_desc
*desc
, ULONG count
, struct prop
*prop
, void *data
)
45 for (i
= 0; i
< count
; i
++)
48 prop
[i
].size
= desc
[i
].size
;
49 prop
[i
].readonly
= desc
[i
].readonly
;
50 prop
[i
].writeonly
= desc
[i
].writeonly
;
55 HRESULT
prop_set( const struct prop
*prop
, ULONG count
, ULONG id
, const void *value
, ULONG size
)
57 if (id
>= count
|| size
!= prop
[id
].size
|| prop
[id
].readonly
) return E_INVALIDARG
;
58 memcpy( prop
[id
].value
, value
, size
);
62 HRESULT
prop_get( const struct prop
*prop
, ULONG count
, ULONG id
, void *buf
, ULONG size
)
64 if (id
>= count
|| size
!= prop
[id
].size
|| prop
[id
].writeonly
) return E_INVALIDARG
;
65 memcpy( buf
, prop
[id
].value
, prop
[id
].size
);
69 struct node
*alloc_node( WS_XML_NODE_TYPE type
)
73 if (!(ret
= heap_alloc_zero( sizeof(*ret
) ))) return NULL
;
74 ret
->hdr
.node
.nodeType
= type
;
75 list_init( &ret
->entry
);
76 list_init( &ret
->children
);
80 void free_attribute( WS_XML_ATTRIBUTE
*attr
)
83 free_xml_string( attr
->prefix
);
84 free_xml_string( attr
->localName
);
85 free_xml_string( attr
->ns
);
86 heap_free( attr
->value
);
90 void free_node( struct node
*node
)
93 switch (node_type( node
))
95 case WS_XML_NODE_TYPE_ELEMENT
:
97 WS_XML_ELEMENT_NODE
*elem
= &node
->hdr
;
100 for (i
= 0; i
< elem
->attributeCount
; i
++) free_attribute( elem
->attributes
[i
] );
101 heap_free( elem
->attributes
);
102 free_xml_string( elem
->prefix
);
103 free_xml_string( elem
->localName
);
104 free_xml_string( elem
->ns
);
107 case WS_XML_NODE_TYPE_TEXT
:
109 WS_XML_TEXT_NODE
*text
= (WS_XML_TEXT_NODE
*)node
;
110 heap_free( text
->text
);
113 case WS_XML_NODE_TYPE_COMMENT
:
115 WS_XML_COMMENT_NODE
*comment
= (WS_XML_COMMENT_NODE
*)node
;
116 heap_free( comment
->value
.bytes
);
119 case WS_XML_NODE_TYPE_CDATA
:
120 case WS_XML_NODE_TYPE_END_CDATA
:
121 case WS_XML_NODE_TYPE_END_ELEMENT
:
122 case WS_XML_NODE_TYPE_EOF
:
123 case WS_XML_NODE_TYPE_BOF
:
127 ERR( "unhandled type %u\n", node_type( node
) );
133 void destroy_nodes( struct node
*node
)
138 while ((ptr
= list_head( &node
->children
)))
140 struct node
*child
= LIST_ENTRY( ptr
, struct node
, entry
);
141 list_remove( &child
->entry
);
142 destroy_nodes( child
);
147 static WS_XML_ATTRIBUTE
*dup_attribute( const WS_XML_ATTRIBUTE
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
149 WS_XML_ATTRIBUTE
*dst
;
152 if (!(dst
= heap_alloc_zero( sizeof(*dst
) ))) return NULL
;
153 dst
->singleQuote
= src
->singleQuote
;
154 dst
->isXmlNs
= src
->isXmlNs
;
156 if (src
->prefix
&& !(dst
->prefix
= dup_xml_string( src
->prefix
, FALSE
))) goto error
;
157 if (src
->localName
&& !(dst
->localName
= dup_xml_string( src
->localName
, FALSE
))) goto error
;
158 if (src
->ns
&& !(dst
->ns
= dup_xml_string( src
->ns
, FALSE
))) goto error
;
164 case WS_XML_WRITER_ENCODING_TYPE_BINARY
:
165 if ((hr
= text_to_text( src
->value
, NULL
, NULL
, &dst
->value
)) != S_OK
) goto error
;
168 case WS_XML_WRITER_ENCODING_TYPE_TEXT
:
169 if ((hr
= text_to_utf8text( src
->value
, NULL
, NULL
, (WS_XML_UTF8_TEXT
**)&dst
->value
)) != S_OK
)
174 ERR( "unhandled encoding %u\n", enc
);
182 free_attribute( dst
);
186 static WS_XML_ATTRIBUTE
**dup_attributes( WS_XML_ATTRIBUTE
* const *src
, ULONG count
,
187 WS_XML_WRITER_ENCODING_TYPE enc
)
189 WS_XML_ATTRIBUTE
**dst
;
192 if (!(dst
= heap_alloc( sizeof(*dst
) * count
))) return NULL
;
193 for (i
= 0; i
< count
; i
++)
195 if (!(dst
[i
] = dup_attribute( src
[i
], enc
)))
197 for (; i
> 0; i
--) free_attribute( dst
[i
- 1] );
205 static struct node
*dup_element_node( const WS_XML_ELEMENT_NODE
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
208 WS_XML_ELEMENT_NODE
*dst
;
209 ULONG count
= src
->attributeCount
;
210 WS_XML_ATTRIBUTE
**attrs
= src
->attributes
;
211 const WS_XML_STRING
*prefix
= (src
->prefix
&& src
->prefix
->length
) ? src
->prefix
: NULL
;
212 const WS_XML_STRING
*localname
= src
->localName
;
213 const WS_XML_STRING
*ns
= src
->ns
;
215 if (!(node
= alloc_node( WS_XML_NODE_TYPE_ELEMENT
))) return NULL
;
218 if (count
&& !(dst
->attributes
= dup_attributes( attrs
, count
, enc
))) goto error
;
219 dst
->attributeCount
= count
;
221 if (prefix
&& !(dst
->prefix
= dup_xml_string( prefix
, FALSE
))) goto error
;
222 if (localname
&& !(dst
->localName
= dup_xml_string( localname
, FALSE
))) goto error
;
223 if (ns
&& !(dst
->ns
= dup_xml_string( ns
, FALSE
))) goto error
;
231 static struct node
*dup_text_node( const WS_XML_TEXT_NODE
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
234 WS_XML_TEXT_NODE
*dst
;
237 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
238 dst
= (WS_XML_TEXT_NODE
*)node
;
239 if (!src
->text
) return node
;
243 case WS_XML_WRITER_ENCODING_TYPE_BINARY
:
244 hr
= text_to_text( src
->text
, NULL
, NULL
, &dst
->text
);
247 case WS_XML_WRITER_ENCODING_TYPE_TEXT
:
248 hr
= text_to_utf8text( src
->text
, NULL
, NULL
, (WS_XML_UTF8_TEXT
**)&dst
->text
);
252 ERR( "unhandled encoding %u\n", enc
);
266 static struct node
*dup_comment_node( const WS_XML_COMMENT_NODE
*src
)
269 WS_XML_COMMENT_NODE
*dst
;
271 if (!(node
= alloc_node( WS_XML_NODE_TYPE_COMMENT
))) return NULL
;
272 dst
= (WS_XML_COMMENT_NODE
*)node
;
274 if (src
->value
.length
&& !(dst
->value
.bytes
= heap_alloc( src
->value
.length
)))
279 memcpy( dst
->value
.bytes
, src
->value
.bytes
, src
->value
.length
);
280 dst
->value
.length
= src
->value
.length
;
284 static struct node
*dup_node( const struct node
*src
, WS_XML_WRITER_ENCODING_TYPE enc
)
286 switch (node_type( src
))
288 case WS_XML_NODE_TYPE_ELEMENT
:
289 return dup_element_node( &src
->hdr
, enc
);
291 case WS_XML_NODE_TYPE_TEXT
:
292 return dup_text_node( (const WS_XML_TEXT_NODE
*)src
, enc
);
294 case WS_XML_NODE_TYPE_COMMENT
:
295 return dup_comment_node( (const WS_XML_COMMENT_NODE
*)src
);
297 case WS_XML_NODE_TYPE_CDATA
:
298 case WS_XML_NODE_TYPE_END_CDATA
:
299 case WS_XML_NODE_TYPE_END_ELEMENT
:
300 case WS_XML_NODE_TYPE_EOF
:
301 case WS_XML_NODE_TYPE_BOF
:
302 return alloc_node( node_type( src
) );
305 ERR( "unhandled type %u\n", node_type( src
) );
311 static HRESULT
dup_tree( const struct node
*src
, WS_XML_WRITER_ENCODING_TYPE enc
, struct node
**dst
)
314 const struct node
*child
;
316 if (!*dst
&& !(*dst
= dup_node( src
, enc
))) return E_OUTOFMEMORY
;
319 LIST_FOR_EACH_ENTRY( child
, &src
->children
, struct node
, entry
)
321 HRESULT hr
= E_OUTOFMEMORY
;
322 struct node
*new_child
;
324 if (!(new_child
= dup_node( child
, enc
)) || (hr
= dup_tree( child
, enc
, &new_child
)) != S_OK
)
326 destroy_nodes( *dst
);
329 new_child
->parent
= parent
;
330 list_add_tail( &parent
->children
, &new_child
->entry
);
335 static const struct prop_desc reader_props
[] =
337 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_DEPTH */
338 { sizeof(BOOL
), FALSE
}, /* WS_XML_READER_PROPERTY_ALLOW_FRAGMENT */
339 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_ATTRIBUTES */
340 { sizeof(BOOL
), FALSE
}, /* WS_XML_READER_PROPERTY_READ_DECLARATION */
341 { sizeof(WS_CHARSET
), FALSE
}, /* WS_XML_READER_PROPERTY_CHARSET */
342 { sizeof(ULONGLONG
), TRUE
}, /* WS_XML_READER_PROPERTY_ROW */
343 { sizeof(ULONGLONG
), TRUE
}, /* WS_XML_READER_PROPERTY_COLUMN */
344 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_UTF8_TRIM_SIZE */
345 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_STREAM_BUFFER_SIZE */
346 { sizeof(BOOL
), TRUE
}, /* WS_XML_READER_PROPERTY_IN_ATTRIBUTE */
347 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_STREAM_MAX_ROOT_MIME_PART_SIZE */
348 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_STREAM_MAX_MIME_HEADERS_SIZE */
349 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_MIME_PARTS */
350 { sizeof(BOOL
), FALSE
}, /* WS_XML_READER_PROPERTY_ALLOW_INVALID_CHARACTER_REFERENCES */
351 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_NAMESPACES */
356 READER_STATE_INITIAL
,
358 READER_STATE_STARTELEMENT
,
359 READER_STATE_STARTATTRIBUTE
,
360 READER_STATE_STARTCDATA
,
363 READER_STATE_ENDELEMENT
,
364 READER_STATE_ENDCDATA
,
365 READER_STATE_COMMENT
,
381 const unsigned char *read_bufptr
;
382 enum reader_state state
;
384 struct node
*current
;
387 struct prefix
*prefixes
;
389 ULONG nb_prefixes_allocated
;
390 WS_XML_READER_ENCODING_TYPE input_enc
;
391 WS_CHARSET input_charset
;
392 WS_XML_READER_INPUT_TYPE input_type
;
393 struct xmlbuf
*input_buf
;
394 const unsigned char *input_data
;
396 ULONG text_conv_offset
;
397 const WS_XML_DICTIONARY
*dict_static
;
398 WS_XML_DICTIONARY
*dict
;
400 struct prop prop
[sizeof(reader_props
)/sizeof(reader_props
[0])];
403 #define READER_MAGIC (('R' << 24) | ('E' << 16) | ('A' << 8) | 'D')
405 static struct reader
*alloc_reader(void)
407 static const ULONG count
= sizeof(reader_props
)/sizeof(reader_props
[0]);
409 ULONG size
= sizeof(*ret
) + prop_size( reader_props
, count
);
411 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
412 if (!(ret
->prefixes
= heap_alloc_zero( sizeof(*ret
->prefixes
) )))
417 ret
->nb_prefixes
= ret
->nb_prefixes_allocated
= 1;
419 ret
->magic
= READER_MAGIC
;
420 InitializeCriticalSection( &ret
->cs
);
421 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": reader.cs");
423 prop_init( reader_props
, count
, ret
->prop
, &ret
[1] );
424 ret
->prop_count
= count
;
428 static void clear_prefixes( struct prefix
*prefixes
, ULONG count
)
431 for (i
= 0; i
< count
; i
++)
433 free_xml_string( prefixes
[i
].str
);
434 prefixes
[i
].str
= NULL
;
435 free_xml_string( prefixes
[i
].ns
);
436 prefixes
[i
].ns
= NULL
;
440 static HRESULT
set_prefix( struct prefix
*prefix
, const WS_XML_STRING
*str
, const WS_XML_STRING
*ns
)
444 free_xml_string( prefix
->str
);
445 if (!(prefix
->str
= dup_xml_string( str
, FALSE
))) return E_OUTOFMEMORY
;
447 if (prefix
->ns
) free_xml_string( prefix
->ns
);
448 if (!(prefix
->ns
= dup_xml_string( ns
, FALSE
))) return E_OUTOFMEMORY
;
452 static HRESULT
bind_prefix( struct reader
*reader
, const WS_XML_STRING
*prefix
, const WS_XML_STRING
*ns
)
457 for (i
= 0; i
< reader
->nb_prefixes
; i
++)
459 if (WsXmlStringEquals( prefix
, reader
->prefixes
[i
].str
, NULL
) == S_OK
)
460 return set_prefix( &reader
->prefixes
[i
], NULL
, ns
);
462 if (i
>= reader
->nb_prefixes_allocated
)
464 ULONG new_size
= reader
->nb_prefixes_allocated
* sizeof(*reader
->prefixes
) * 2;
465 struct prefix
*tmp
= heap_realloc_zero( reader
->prefixes
, new_size
);
466 if (!tmp
) return E_OUTOFMEMORY
;
467 reader
->prefixes
= tmp
;
468 reader
->nb_prefixes_allocated
*= 2;
470 if ((hr
= set_prefix( &reader
->prefixes
[i
], prefix
, ns
)) != S_OK
) return hr
;
471 reader
->nb_prefixes
++;
475 static const WS_XML_STRING
*get_namespace( struct reader
*reader
, const WS_XML_STRING
*prefix
)
478 for (i
= 0; i
< reader
->nb_prefixes
; i
++)
480 if (WsXmlStringEquals( prefix
, reader
->prefixes
[i
].str
, NULL
) == S_OK
)
481 return reader
->prefixes
[i
].ns
;
486 static void read_insert_eof( struct reader
*reader
, struct node
*eof
)
488 if (!reader
->root
) reader
->root
= eof
;
491 eof
->parent
= reader
->root
;
492 list_add_tail( &reader
->root
->children
, &eof
->entry
);
494 reader
->current
= reader
->last
= eof
;
497 static void read_insert_bof( struct reader
*reader
, struct node
*bof
)
499 reader
->root
->parent
= bof
;
500 list_add_tail( &bof
->children
, &reader
->root
->entry
);
501 reader
->current
= reader
->last
= reader
->root
= bof
;
504 static void read_insert_node( struct reader
*reader
, struct node
*parent
, struct node
*node
)
506 node
->parent
= parent
;
507 list_add_before( list_tail( &parent
->children
), &node
->entry
);
508 reader
->current
= reader
->last
= node
;
511 static void free_reader( struct reader
*reader
)
513 destroy_nodes( reader
->root
);
514 clear_prefixes( reader
->prefixes
, reader
->nb_prefixes
);
515 heap_free( reader
->prefixes
);
516 reader
->cs
.DebugInfo
->Spare
[0] = 0;
517 DeleteCriticalSection( &reader
->cs
);
521 static HRESULT
init_reader( struct reader
*reader
)
523 static const WS_XML_STRING empty
= {0, NULL
};
527 reader
->state
= READER_STATE_INITIAL
;
528 destroy_nodes( reader
->root
);
529 reader
->root
= reader
->current
= NULL
;
530 reader
->current_attr
= 0;
531 clear_prefixes( reader
->prefixes
, reader
->nb_prefixes
);
532 reader
->nb_prefixes
= 1;
533 if ((hr
= bind_prefix( reader
, &empty
, &empty
)) != S_OK
) return hr
;
535 if (!(node
= alloc_node( WS_XML_NODE_TYPE_EOF
))) return E_OUTOFMEMORY
;
536 read_insert_eof( reader
, node
);
537 reader
->input_enc
= WS_XML_READER_ENCODING_TYPE_TEXT
;
538 reader
->input_charset
= WS_CHARSET_UTF8
;
539 reader
->dict_static
= NULL
;
544 /**************************************************************************
545 * WsCreateReader [webservices.@]
547 HRESULT WINAPI
WsCreateReader( const WS_XML_READER_PROPERTY
*properties
, ULONG count
,
548 WS_XML_READER
**handle
, WS_ERROR
*error
)
550 struct reader
*reader
;
551 ULONG i
, max_depth
= 32, max_attrs
= 128, max_ns
= 32;
552 BOOL read_decl
= TRUE
;
555 TRACE( "%p %u %p %p\n", properties
, count
, handle
, error
);
556 if (error
) FIXME( "ignoring error parameter\n" );
558 if (!handle
) return E_INVALIDARG
;
559 if (!(reader
= alloc_reader())) return E_OUTOFMEMORY
;
561 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_MAX_DEPTH
, &max_depth
, sizeof(max_depth
) );
562 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_MAX_ATTRIBUTES
, &max_attrs
, sizeof(max_attrs
) );
563 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_READ_DECLARATION
, &read_decl
, sizeof(read_decl
) );
564 prop_set( reader
->prop
, reader
->prop_count
, WS_XML_READER_PROPERTY_MAX_NAMESPACES
, &max_ns
, sizeof(max_ns
) );
566 for (i
= 0; i
< count
; i
++)
568 hr
= prop_set( reader
->prop
, reader
->prop_count
, properties
[i
].id
, properties
[i
].value
,
569 properties
[i
].valueSize
);
572 free_reader( reader
);
577 if ((hr
= init_reader( reader
)) != S_OK
)
579 free_reader( reader
);
583 TRACE( "created %p\n", reader
);
584 *handle
= (WS_XML_READER
*)reader
;
588 /**************************************************************************
589 * WsFreeReader [webservices.@]
591 void WINAPI
WsFreeReader( WS_XML_READER
*handle
)
593 struct reader
*reader
= (struct reader
*)handle
;
595 TRACE( "%p\n", handle
);
599 EnterCriticalSection( &reader
->cs
);
601 if (reader
->magic
!= READER_MAGIC
)
603 LeaveCriticalSection( &reader
->cs
);
609 LeaveCriticalSection( &reader
->cs
);
610 free_reader( reader
);
613 /**************************************************************************
614 * WsFillReader [webservices.@]
616 HRESULT WINAPI
WsFillReader( WS_XML_READER
*handle
, ULONG min_size
, const WS_ASYNC_CONTEXT
*ctx
,
619 struct reader
*reader
= (struct reader
*)handle
;
621 TRACE( "%p %u %p %p\n", handle
, min_size
, ctx
, error
);
622 if (error
) FIXME( "ignoring error parameter\n" );
624 if (!reader
) return E_INVALIDARG
;
626 EnterCriticalSection( &reader
->cs
);
628 if (reader
->magic
!= READER_MAGIC
)
630 LeaveCriticalSection( &reader
->cs
);
634 /* FIXME: add support for stream input */
635 reader
->read_size
= min( min_size
, reader
->input_size
);
636 reader
->read_pos
= 0;
638 LeaveCriticalSection( &reader
->cs
);
642 /**************************************************************************
643 * WsGetNamespaceFromPrefix [webservices.@]
645 HRESULT WINAPI
WsGetNamespaceFromPrefix( WS_XML_READER
*handle
, const WS_XML_STRING
*prefix
,
646 BOOL required
, const WS_XML_STRING
**ns
, WS_ERROR
*error
)
648 static const WS_XML_STRING xml
= {3, (BYTE
*)"xml"};
649 static const WS_XML_STRING xmlns
= {5, (BYTE
*)"xmlns"};
650 static const WS_XML_STRING empty_ns
= {0, NULL
};
651 static const WS_XML_STRING xml_ns
= {36, (BYTE
*)"http://www.w3.org/XML/1998/namespace"};
652 static const WS_XML_STRING xmlns_ns
= {29, (BYTE
*)"http://www.w3.org/2000/xmlns/"};
653 struct reader
*reader
= (struct reader
*)handle
;
656 TRACE( "%p %s %d %p %p\n", handle
, debugstr_xmlstr(prefix
), required
, ns
, error
);
657 if (error
) FIXME( "ignoring error parameter\n" );
659 if (!reader
|| !prefix
|| !ns
) return E_INVALIDARG
;
661 EnterCriticalSection( &reader
->cs
);
663 if (reader
->magic
!= READER_MAGIC
)
665 LeaveCriticalSection( &reader
->cs
);
669 if (reader
->state
!= READER_STATE_STARTELEMENT
)
671 LeaveCriticalSection( &reader
->cs
);
672 return WS_E_INVALID_OPERATION
;
680 else if (WsXmlStringEquals( prefix
, &xml
, NULL
) == S_OK
)
685 else if (WsXmlStringEquals( prefix
, &xmlns
, NULL
) == S_OK
)
692 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
695 for (i
= 0; i
< elem
->attributeCount
; i
++)
697 if (!elem
->attributes
[i
]->isXmlNs
) continue;
698 if (WsXmlStringEquals( prefix
, elem
->attributes
[i
]->prefix
, NULL
) == S_OK
)
700 *ns
= elem
->attributes
[i
]->ns
;
707 LeaveCriticalSection( &reader
->cs
);
711 if (required
) return WS_E_INVALID_FORMAT
;
719 /**************************************************************************
720 * WsGetReaderNode [webservices.@]
722 HRESULT WINAPI
WsGetReaderNode( WS_XML_READER
*handle
, const WS_XML_NODE
**node
,
725 struct reader
*reader
= (struct reader
*)handle
;
727 TRACE( "%p %p %p\n", handle
, node
, error
);
728 if (error
) FIXME( "ignoring error parameter\n" );
730 if (!reader
|| !node
) return E_INVALIDARG
;
732 EnterCriticalSection( &reader
->cs
);
734 if (reader
->magic
!= READER_MAGIC
)
736 LeaveCriticalSection( &reader
->cs
);
740 *node
= &reader
->current
->hdr
.node
;
742 LeaveCriticalSection( &reader
->cs
);
746 static HRESULT
get_charset( struct reader
*reader
, void *buf
, ULONG size
)
748 if (!buf
|| size
!= sizeof(reader
->input_charset
)) return E_INVALIDARG
;
749 if (!reader
->input_charset
) return WS_E_INVALID_FORMAT
;
750 *(WS_CHARSET
*)buf
= reader
->input_charset
;
754 /**************************************************************************
755 * WsGetReaderProperty [webservices.@]
757 HRESULT WINAPI
WsGetReaderProperty( WS_XML_READER
*handle
, WS_XML_READER_PROPERTY_ID id
,
758 void *buf
, ULONG size
, WS_ERROR
*error
)
760 struct reader
*reader
= (struct reader
*)handle
;
763 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
764 if (error
) FIXME( "ignoring error parameter\n" );
766 if (!reader
) return E_INVALIDARG
;
768 EnterCriticalSection( &reader
->cs
);
770 if (reader
->magic
!= READER_MAGIC
)
772 LeaveCriticalSection( &reader
->cs
);
776 if (!reader
->input_type
)
778 LeaveCriticalSection( &reader
->cs
);
779 return WS_E_INVALID_OPERATION
;
782 if (id
== WS_XML_READER_PROPERTY_CHARSET
) hr
= get_charset( reader
, buf
, size
);
783 else hr
= prop_get( reader
->prop
, reader
->prop_count
, id
, buf
, size
);
785 LeaveCriticalSection( &reader
->cs
);
789 /**************************************************************************
790 * WsGetXmlAttribute [webservices.@]
792 HRESULT WINAPI
WsGetXmlAttribute( WS_XML_READER
*handle
, const WS_XML_STRING
*attr
,
793 WS_HEAP
*heap
, WCHAR
**str
, ULONG
*len
, WS_ERROR
*error
)
795 FIXME( "%p %s %p %p %p %p: stub\n", handle
, debugstr_xmlstr(attr
), heap
, str
, len
, error
);
799 WS_XML_UTF8_TEXT
*alloc_utf8_text( const BYTE
*data
, ULONG len
)
801 WS_XML_UTF8_TEXT
*ret
;
803 if (!(ret
= heap_alloc( sizeof(*ret
) + len
))) return NULL
;
804 ret
->text
.textType
= WS_XML_TEXT_TYPE_UTF8
;
805 ret
->value
.length
= len
;
806 ret
->value
.bytes
= len
? (BYTE
*)(ret
+ 1) : NULL
;
807 ret
->value
.dictionary
= NULL
;
809 if (data
) memcpy( ret
->value
.bytes
, data
, len
);
813 WS_XML_UTF16_TEXT
*alloc_utf16_text( const BYTE
*data
, ULONG len
)
815 WS_XML_UTF16_TEXT
*ret
;
817 if (!(ret
= heap_alloc( sizeof(*ret
) + len
))) return NULL
;
818 ret
->text
.textType
= WS_XML_TEXT_TYPE_UTF16
;
819 ret
->byteCount
= len
;
820 ret
->bytes
= len
? (BYTE
*)(ret
+ 1) : NULL
;
821 if (data
) memcpy( ret
->bytes
, data
, len
);
825 WS_XML_BASE64_TEXT
*alloc_base64_text( const BYTE
*data
, ULONG len
)
827 WS_XML_BASE64_TEXT
*ret
;
829 if (!(ret
= heap_alloc( sizeof(*ret
) + len
))) return NULL
;
830 ret
->text
.textType
= WS_XML_TEXT_TYPE_BASE64
;
832 ret
->bytes
= len
? (BYTE
*)(ret
+ 1) : NULL
;
833 if (data
) memcpy( ret
->bytes
, data
, len
);
837 WS_XML_BOOL_TEXT
*alloc_bool_text( BOOL value
)
839 WS_XML_BOOL_TEXT
*ret
;
841 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
842 ret
->text
.textType
= WS_XML_TEXT_TYPE_BOOL
;
847 WS_XML_INT32_TEXT
*alloc_int32_text( INT32 value
)
849 WS_XML_INT32_TEXT
*ret
;
851 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
852 ret
->text
.textType
= WS_XML_TEXT_TYPE_INT32
;
857 WS_XML_INT64_TEXT
*alloc_int64_text( INT64 value
)
859 WS_XML_INT64_TEXT
*ret
;
861 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
862 ret
->text
.textType
= WS_XML_TEXT_TYPE_INT64
;
867 WS_XML_UINT64_TEXT
*alloc_uint64_text( UINT64 value
)
869 WS_XML_UINT64_TEXT
*ret
;
871 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
872 ret
->text
.textType
= WS_XML_TEXT_TYPE_UINT64
;
877 WS_XML_FLOAT_TEXT
*alloc_float_text( float value
)
879 WS_XML_FLOAT_TEXT
*ret
;
881 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
882 ret
->text
.textType
= WS_XML_TEXT_TYPE_FLOAT
;
887 WS_XML_DOUBLE_TEXT
*alloc_double_text( double value
)
889 WS_XML_DOUBLE_TEXT
*ret
;
891 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
892 ret
->text
.textType
= WS_XML_TEXT_TYPE_DOUBLE
;
897 WS_XML_GUID_TEXT
*alloc_guid_text( const GUID
*value
)
899 WS_XML_GUID_TEXT
*ret
;
901 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
902 ret
->text
.textType
= WS_XML_TEXT_TYPE_GUID
;
907 WS_XML_UNIQUE_ID_TEXT
*alloc_unique_id_text( const GUID
*value
)
909 WS_XML_UNIQUE_ID_TEXT
*ret
;
911 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
912 ret
->text
.textType
= WS_XML_TEXT_TYPE_UNIQUE_ID
;
917 WS_XML_DATETIME_TEXT
*alloc_datetime_text( const WS_DATETIME
*value
)
919 WS_XML_DATETIME_TEXT
*ret
;
921 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
922 ret
->text
.textType
= WS_XML_TEXT_TYPE_DATETIME
;
927 static inline BOOL
read_end_of_data( struct reader
*reader
)
929 return reader
->read_pos
>= reader
->read_size
;
932 static inline const unsigned char *read_current_ptr( struct reader
*reader
)
934 return &reader
->read_bufptr
[reader
->read_pos
];
937 static inline HRESULT
read_peek( struct reader
*reader
, unsigned char *byte
)
939 if (reader
->read_pos
>= reader
->read_size
) return WS_E_INVALID_FORMAT
;
940 *byte
= reader
->read_bufptr
[reader
->read_pos
];
944 static inline HRESULT
read_byte( struct reader
*reader
, unsigned char *byte
)
946 if (reader
->read_pos
>= reader
->read_size
) return WS_E_INVALID_FORMAT
;
947 *byte
= reader
->read_bufptr
[reader
->read_pos
++];
951 static inline HRESULT
read_bytes( struct reader
*reader
, unsigned char *bytes
, unsigned int len
)
953 if (reader
->read_pos
+ len
> reader
->read_size
) return WS_E_INVALID_FORMAT
;
954 memcpy( bytes
, reader
->read_bufptr
+ reader
->read_pos
, len
);
955 reader
->read_pos
+= len
;
959 /* UTF-8 support based on libs/wine/utf8.c */
961 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
962 static const char utf8_length
[128] =
964 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
965 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
966 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
967 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
968 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
969 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
970 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
971 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
974 /* first byte mask depending on UTF-8 sequence length */
975 static const unsigned char utf8_mask
[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
977 /* minimum Unicode value depending on UTF-8 sequence length */
978 static const unsigned int utf8_minval
[4] = { 0x0, 0x80, 0x800, 0x10000 };
980 static inline unsigned int read_utf8_char( struct reader
*reader
, unsigned int *skip
)
982 unsigned int len
, res
;
983 unsigned char ch
= reader
->read_bufptr
[reader
->read_pos
];
984 const unsigned char *end
;
986 if (reader
->read_pos
>= reader
->read_size
) return 0;
993 len
= utf8_length
[ch
- 0x80];
994 if (reader
->read_pos
+ len
>= reader
->read_size
) return 0;
995 end
= reader
->read_bufptr
+ reader
->read_pos
+ len
+ 1;
996 res
= ch
& utf8_mask
[len
];
1001 if ((ch
= end
[-3] ^ 0x80) >= 0x40) break;
1002 res
= (res
<< 6) | ch
;
1004 if ((ch
= end
[-2] ^ 0x80) >= 0x40) break;
1005 res
= (res
<< 6) | ch
;
1007 if ((ch
= end
[-1] ^ 0x80) >= 0x40) break;
1008 res
= (res
<< 6) | ch
;
1009 if (res
< utf8_minval
[len
]) break;
1017 static inline void read_skip( struct reader
*reader
, unsigned int count
)
1019 if (reader
->read_pos
+ count
> reader
->read_size
) return;
1020 reader
->read_pos
+= count
;
1023 static inline void read_rewind( struct reader
*reader
, unsigned int count
)
1025 reader
->read_pos
-= count
;
1028 static inline BOOL
read_isnamechar( unsigned int ch
)
1030 /* FIXME: incomplete */
1031 return (ch
>= 'A' && ch
<= 'Z') ||
1032 (ch
>= 'a' && ch
<= 'z') ||
1033 (ch
>= '0' && ch
<= '9') ||
1034 ch
== '_' || ch
== '-' || ch
== '.' || ch
== ':';
1037 static inline BOOL
read_isspace( unsigned int ch
)
1039 return ch
== ' ' || ch
== '\t' || ch
== '\r' || ch
== '\n';
1042 static inline void read_skip_whitespace( struct reader
*reader
)
1044 while (reader
->read_pos
< reader
->read_size
&& read_isspace( reader
->read_bufptr
[reader
->read_pos
] ))
1048 static inline int read_cmp( struct reader
*reader
, const char *str
, int len
)
1050 const unsigned char *ptr
= read_current_ptr( reader
);
1052 if (len
< 0) len
= strlen( str
);
1053 if (reader
->read_pos
+ len
> reader
->read_size
) return -1;
1056 if (*str
!= *ptr
) return *ptr
- *str
;
1062 static HRESULT
read_xmldecl( struct reader
*reader
)
1064 if (!reader
->read_size
) return WS_E_INVALID_FORMAT
;
1066 if (read_cmp( reader
, "<", 1 ) || read_cmp( reader
, "<?", 2 ))
1068 reader
->state
= READER_STATE_BOF
;
1071 if (read_cmp( reader
, "<?xml ", 6 )) return WS_E_INVALID_FORMAT
;
1072 read_skip( reader
, 6 );
1074 /* FIXME: parse attributes */
1075 while (reader
->read_pos
< reader
->read_size
&& reader
->read_bufptr
[reader
->read_pos
] != '?')
1078 if (read_cmp( reader
, "?>", 2 )) return WS_E_INVALID_FORMAT
;
1079 read_skip( reader
, 2 );
1081 reader
->state
= READER_STATE_BOF
;
1085 HRESULT
append_attribute( WS_XML_ELEMENT_NODE
*elem
, WS_XML_ATTRIBUTE
*attr
)
1087 if (elem
->attributeCount
)
1089 WS_XML_ATTRIBUTE
**tmp
;
1090 if (!(tmp
= heap_realloc( elem
->attributes
, (elem
->attributeCount
+ 1) * sizeof(attr
) )))
1091 return E_OUTOFMEMORY
;
1092 elem
->attributes
= tmp
;
1094 else if (!(elem
->attributes
= heap_alloc( sizeof(attr
) ))) return E_OUTOFMEMORY
;
1095 elem
->attributes
[elem
->attributeCount
++] = attr
;
1099 static inline void init_xml_string( BYTE
*bytes
, ULONG len
, WS_XML_STRING
*str
)
1103 str
->dictionary
= NULL
;
1107 static HRESULT
split_qname( const BYTE
*str
, ULONG len
, WS_XML_STRING
*prefix
, WS_XML_STRING
*localname
)
1109 BYTE
*prefix_bytes
= NULL
, *localname_bytes
= (BYTE
*)str
, *ptr
= (BYTE
*)str
;
1110 ULONG prefix_len
= 0, localname_len
= len
;
1116 if (ptr
== str
) return WS_E_INVALID_FORMAT
;
1117 prefix_bytes
= (BYTE
*)str
;
1118 prefix_len
= ptr
- str
;
1119 localname_bytes
= ptr
+ 1;
1120 localname_len
= len
;
1125 if (!localname_len
) return WS_E_INVALID_FORMAT
;
1127 init_xml_string( prefix_bytes
, prefix_len
, prefix
);
1128 init_xml_string( localname_bytes
, localname_len
, localname
);
1132 static HRESULT
parse_qname( const BYTE
*str
, ULONG len
, WS_XML_STRING
**prefix_ret
, WS_XML_STRING
**localname_ret
)
1134 WS_XML_STRING prefix
, localname
;
1137 if ((hr
= split_qname( str
, len
, &prefix
, &localname
)) != S_OK
) return hr
;
1138 if (!(*prefix_ret
= alloc_xml_string( NULL
, prefix
.length
))) return E_OUTOFMEMORY
;
1139 if (!(*localname_ret
= dup_xml_string( &localname
, FALSE
)))
1141 free_xml_string( *prefix_ret
);
1142 return E_OUTOFMEMORY
;
1144 memcpy( (*prefix_ret
)->bytes
, prefix
.bytes
, prefix
.length
);
1145 if (prefix
.length
&& add_xml_string( *prefix_ret
) != S_OK
) WARN( "prefix not added to dictionary\n" );
1149 static int codepoint_to_utf8( int cp
, unsigned char *dst
)
1159 dst
[1] = 0x80 | (cp
& 0x3f);
1164 if ((cp
>= 0xd800 && cp
<= 0xdfff) || cp
== 0xfffe || cp
== 0xffff) return -1;
1167 dst
[2] = 0x80 | (cp
& 0x3f);
1169 dst
[1] = 0x80 | (cp
& 0x3f);
1174 if (cp
>= 0x110000) return -1;
1175 dst
[3] = 0x80 | (cp
& 0x3f);
1177 dst
[2] = 0x80 | (cp
& 0x3f);
1179 dst
[1] = 0x80 | (cp
& 0x3f);
1185 static HRESULT
decode_text( const unsigned char *str
, ULONG len
, unsigned char *ret
, ULONG
*ret_len
)
1187 const unsigned char *p
= str
;
1188 unsigned char *q
= ret
;
1196 if (!len
) return WS_E_INVALID_FORMAT
;
1198 if (len
>= 3 && !memcmp( p
, "lt;", 3 ))
1204 else if (len
>= 3 && !memcmp( p
, "gt;", 3 ))
1210 else if (len
>= 5 && !memcmp( p
, "quot;", 5 ))
1216 else if (len
>= 4 && !memcmp( p
, "amp;", 4 ))
1222 else if (len
>= 5 && !memcmp( p
, "apos;", 5 ))
1230 ULONG start
, nb_digits
, i
;
1231 int len_utf8
, cp
= 0;
1234 if (!len
) return WS_E_INVALID_FORMAT
;
1240 while (len
&& isxdigit( *p
)) { p
++; len
--; };
1241 if (!len
) return WS_E_INVALID_FORMAT
;
1243 p
-= nb_digits
= start
- len
;
1244 if (!nb_digits
|| nb_digits
> 6 || p
[nb_digits
] != ';') return WS_E_INVALID_FORMAT
;
1245 for (i
= 0; i
< nb_digits
; i
++)
1248 if (*p
>= '0' && *p
<= '9') cp
+= *p
- '0';
1249 else if (*p
>= 'a' && *p
<= 'f') cp
+= *p
- 'a' + 10;
1250 else cp
+= *p
- 'A' + 10;
1254 else if (isdigit( *p
))
1256 while (len
&& *p
== '0') { p
++; len
--; };
1257 if (!len
) return WS_E_INVALID_FORMAT
;
1260 while (len
&& isdigit( *p
)) { p
++; len
--; };
1261 if (!len
) return WS_E_INVALID_FORMAT
;
1263 p
-= nb_digits
= start
- len
;
1264 if (!nb_digits
|| nb_digits
> 7 || p
[nb_digits
] != ';') return WS_E_INVALID_FORMAT
;
1265 for (i
= 0; i
< nb_digits
; i
++)
1272 else return WS_E_INVALID_FORMAT
;
1274 if ((len_utf8
= codepoint_to_utf8( cp
, q
)) < 0) return WS_E_INVALID_FORMAT
;
1275 *ret_len
+= len_utf8
;
1279 else return WS_E_INVALID_FORMAT
;
1291 static HRESULT
read_attribute_value_text( struct reader
*reader
, WS_XML_ATTRIBUTE
*attr
)
1293 WS_XML_UTF8_TEXT
*utf8
= NULL
;
1294 unsigned int len
, ch
, skip
, quote
;
1295 const unsigned char *start
;
1296 HRESULT hr
= E_OUTOFMEMORY
;
1298 read_skip_whitespace( reader
);
1299 if (read_cmp( reader
, "=", 1 )) return WS_E_INVALID_FORMAT
;
1300 read_skip( reader
, 1 );
1302 read_skip_whitespace( reader
);
1303 if (read_cmp( reader
, "\"", 1 ) && read_cmp( reader
, "'", 1 )) return WS_E_INVALID_FORMAT
;
1304 quote
= read_utf8_char( reader
, &skip
);
1305 read_skip( reader
, 1 );
1308 start
= read_current_ptr( reader
);
1311 if (!(ch
= read_utf8_char( reader
, &skip
))) return WS_E_INVALID_FORMAT
;
1312 if (ch
== quote
) break;
1313 read_skip( reader
, skip
);
1316 read_skip( reader
, 1 );
1320 if (!(attr
->ns
= alloc_xml_string( start
, len
))) goto error
;
1321 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1322 if (!(utf8
= alloc_utf8_text( NULL
, 0 )))
1330 if (!(utf8
= alloc_utf8_text( NULL
, len
))) goto error
;
1331 if ((hr
= decode_text( start
, len
, utf8
->value
.bytes
, &utf8
->value
.length
)) != S_OK
) goto error
;
1334 attr
->value
= &utf8
->text
;
1335 attr
->singleQuote
= (quote
== '\'');
1343 static inline BOOL
is_text_type( unsigned char type
)
1345 return (type
>= RECORD_ZERO_TEXT
&& type
<= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT
);
1348 static HRESULT
read_int31( struct reader
*reader
, ULONG
*len
)
1353 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1355 if (!(byte
& 0x80)) return S_OK
;
1357 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1358 *len
+= (byte
& 0x7f) << 7;
1359 if (!(byte
& 0x80)) return S_OK
;
1361 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1362 *len
+= (byte
& 0x7f) << 14;
1363 if (!(byte
& 0x80)) return S_OK
;
1365 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1366 *len
+= (byte
& 0x7f) << 21;
1367 if (!(byte
& 0x80)) return S_OK
;
1369 if ((hr
= read_byte( reader
, &byte
)) != S_OK
) return hr
;
1370 *len
+= (byte
& 0x07) << 28;
1374 static HRESULT
read_string( struct reader
*reader
, WS_XML_STRING
**str
)
1378 if ((hr
= read_int31( reader
, &len
)) != S_OK
) return hr
;
1379 if (!(*str
= alloc_xml_string( NULL
, len
))) return E_OUTOFMEMORY
;
1380 if ((hr
= read_bytes( reader
, (*str
)->bytes
, len
)) == S_OK
)
1382 if (add_xml_string( *str
) != S_OK
) WARN( "string not added to dictionary\n" );
1385 free_xml_string( *str
);
1389 static HRESULT
read_dict_string( struct reader
*reader
, WS_XML_STRING
**str
)
1391 const WS_XML_DICTIONARY
*dict
;
1395 if ((hr
= read_int31( reader
, &id
)) != S_OK
) return hr
;
1396 dict
= (id
& 1) ? reader
->dict
: reader
->dict_static
;
1397 if (!dict
|| (id
>>= 1) >= dict
->stringCount
) return WS_E_INVALID_FORMAT
;
1398 if (!(*str
= alloc_xml_string( NULL
, 0 ))) return E_OUTOFMEMORY
;
1399 *(*str
) = dict
->strings
[id
];
1403 static HRESULT
read_datetime( struct reader
*reader
, WS_DATETIME
*ret
)
1408 if ((hr
= read_bytes( reader
, (unsigned char *)&val
, sizeof(val
) )) != S_OK
) return hr
;
1410 if ((val
& 0x03) == 1) ret
->format
= WS_DATETIME_FORMAT_UTC
;
1411 else if ((val
& 0x03) == 2) ret
->format
= WS_DATETIME_FORMAT_LOCAL
;
1412 else ret
->format
= WS_DATETIME_FORMAT_NONE
;
1414 if ((ret
->ticks
= val
>> 2) > TICKS_MAX
) return WS_E_INVALID_FORMAT
;
1418 static HRESULT
lookup_string( struct reader
*reader
, ULONG id
, const WS_XML_STRING
**ret
)
1420 const WS_XML_DICTIONARY
*dict
= (id
& 1) ? reader
->dict
: reader
->dict_static
;
1421 if (!dict
|| (id
>>= 1) >= dict
->stringCount
) return WS_E_INVALID_FORMAT
;
1422 *ret
= &dict
->strings
[id
];
1426 static HRESULT
read_attribute_value_bin( struct reader
*reader
, WS_XML_ATTRIBUTE
*attr
)
1428 WS_XML_UTF8_TEXT
*text_utf8
= NULL
;
1429 WS_XML_BASE64_TEXT
*text_base64
= NULL
;
1430 WS_XML_INT32_TEXT
*text_int32
;
1431 WS_XML_INT64_TEXT
*text_int64
;
1432 WS_XML_BOOL_TEXT
*text_bool
;
1433 const WS_XML_STRING
*str
;
1442 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
1443 if (!is_text_type( type
)) return WS_E_INVALID_FORMAT
;
1447 case RECORD_ZERO_TEXT
:
1449 if (!(text_int32
= alloc_int32_text( 0 ))) return E_OUTOFMEMORY
;
1450 attr
->value
= &text_int32
->text
;
1453 case RECORD_ONE_TEXT
:
1455 if (!(text_int32
= alloc_int32_text( 1 ))) return E_OUTOFMEMORY
;
1456 attr
->value
= &text_int32
->text
;
1459 case RECORD_FALSE_TEXT
:
1461 if (!(text_bool
= alloc_bool_text( FALSE
))) return E_OUTOFMEMORY
;
1462 attr
->value
= &text_bool
->text
;
1465 case RECORD_TRUE_TEXT
:
1467 if (!(text_bool
= alloc_bool_text( TRUE
))) return E_OUTOFMEMORY
;
1468 attr
->value
= &text_bool
->text
;
1471 case RECORD_INT8_TEXT
:
1474 if ((hr
= read_byte( reader
, (unsigned char *)&val_int8
)) != S_OK
) return hr
;
1475 if (!(text_int64
= alloc_int64_text( val_int8
))) return E_OUTOFMEMORY
;
1476 attr
->value
= &text_int64
->text
;
1479 case RECORD_INT16_TEXT
:
1482 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int16
, sizeof(val_int16
) )) != S_OK
) return hr
;
1483 if (!(text_int64
= alloc_int64_text( val_int16
))) return E_OUTOFMEMORY
;
1484 attr
->value
= &text_int64
->text
;
1487 case RECORD_INT32_TEXT
:
1488 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
1489 if (!(text_int64
= alloc_int64_text( val_int32
))) return E_OUTOFMEMORY
;
1490 attr
->value
= &text_int64
->text
;
1493 case RECORD_INT64_TEXT
:
1496 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int64
, sizeof(val_int64
) )) != S_OK
) return hr
;
1497 if (!(text_int64
= alloc_int64_text( val_int64
))) return E_OUTOFMEMORY
;
1498 attr
->value
= &text_int64
->text
;
1501 case RECORD_FLOAT_TEXT
:
1503 WS_XML_FLOAT_TEXT
*text_float
;
1506 if ((hr
= read_bytes( reader
, (unsigned char *)&val_float
, sizeof(val_float
) )) != S_OK
) return hr
;
1507 if (!(text_float
= alloc_float_text( val_float
))) return E_OUTOFMEMORY
;
1508 attr
->value
= &text_float
->text
;
1511 case RECORD_DOUBLE_TEXT
:
1513 WS_XML_DOUBLE_TEXT
*text_double
;
1516 if ((hr
= read_bytes( reader
, (unsigned char *)&val_double
, sizeof(val_double
) )) != S_OK
) return hr
;
1517 if (!(text_double
= alloc_double_text( val_double
))) return E_OUTOFMEMORY
;
1518 attr
->value
= &text_double
->text
;
1521 case RECORD_DATETIME_TEXT
:
1523 WS_XML_DATETIME_TEXT
*text_datetime
;
1524 WS_DATETIME datetime
;
1526 if ((hr
= read_datetime( reader
, &datetime
)) != S_OK
) return hr
;
1527 if (!(text_datetime
= alloc_datetime_text( &datetime
))) return E_OUTOFMEMORY
;
1528 attr
->value
= &text_datetime
->text
;
1531 case RECORD_CHARS8_TEXT
:
1532 if ((hr
= read_byte( reader
, (unsigned char *)&val_uint8
)) != S_OK
) return hr
;
1536 case RECORD_CHARS16_TEXT
:
1537 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint16
, sizeof(val_uint16
) )) != S_OK
) return hr
;
1541 case RECORD_CHARS32_TEXT
:
1542 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
1543 if (val_int32
< 0) return WS_E_INVALID_FORMAT
;
1547 case RECORD_BYTES8_TEXT
:
1548 if ((hr
= read_byte( reader
, (unsigned char *)&val_uint8
)) != S_OK
) return hr
;
1549 if (!(text_base64
= alloc_base64_text( NULL
, val_uint8
))) return E_OUTOFMEMORY
;
1550 if ((hr
= read_bytes( reader
, text_base64
->bytes
, val_uint8
)) != S_OK
)
1552 heap_free( text_base64
);
1557 case RECORD_BYTES16_TEXT
:
1558 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint16
, sizeof(val_uint16
) )) != S_OK
) return hr
;
1559 if (!(text_base64
= alloc_base64_text( NULL
, val_uint16
))) return E_OUTOFMEMORY
;
1560 if ((hr
= read_bytes( reader
, text_base64
->bytes
, val_uint16
)) != S_OK
)
1562 heap_free( text_base64
);
1567 case RECORD_BYTES32_TEXT
:
1568 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
1569 if (val_int32
< 0) return WS_E_INVALID_FORMAT
;
1570 if (!(text_base64
= alloc_base64_text( NULL
, val_int32
))) return E_OUTOFMEMORY
;
1571 if ((hr
= read_bytes( reader
, text_base64
->bytes
, val_int32
)) != S_OK
)
1573 heap_free( text_base64
);
1578 case RECORD_EMPTY_TEXT
:
1581 case RECORD_DICTIONARY_TEXT
:
1582 if ((hr
= read_int31( reader
, &id
)) != S_OK
) return hr
;
1583 if ((hr
= lookup_string( reader
, id
, &str
)) != S_OK
) return hr
;
1584 if (!(text_utf8
= alloc_utf8_text( str
->bytes
, str
->length
))) return E_OUTOFMEMORY
;
1587 case RECORD_UNIQUE_ID_TEXT
:
1589 WS_XML_UNIQUE_ID_TEXT
*text_unique_id
;
1590 if ((hr
= read_bytes( reader
, (unsigned char *)&guid
, sizeof(guid
) )) != S_OK
) return hr
;
1591 if (!(text_unique_id
= alloc_unique_id_text( &guid
))) return E_OUTOFMEMORY
;
1592 attr
->value
= &text_unique_id
->text
;
1595 case RECORD_GUID_TEXT
:
1597 WS_XML_GUID_TEXT
*guid_text
;
1598 if ((hr
= read_bytes( reader
, (unsigned char *)&guid
, sizeof(guid
) )) != S_OK
) return hr
;
1599 if (!(guid_text
= alloc_guid_text( &guid
))) return E_OUTOFMEMORY
;
1600 attr
->value
= &guid_text
->text
;
1603 case RECORD_UINT64_TEXT
:
1605 WS_XML_UINT64_TEXT
*text_uint64
;
1608 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint64
, sizeof(val_uint64
) )) != S_OK
) return hr
;
1609 if (!(text_uint64
= alloc_uint64_text( val_uint64
))) return E_OUTOFMEMORY
;
1610 attr
->value
= &text_uint64
->text
;
1613 case RECORD_BOOL_TEXT
:
1615 WS_XML_BOOL_TEXT
*text_bool
;
1618 if ((hr
= read_bytes( reader
, (unsigned char *)&val_bool
, sizeof(val_bool
) )) != S_OK
) return hr
;
1619 if (!(text_bool
= alloc_bool_text( !!val_bool
))) return E_OUTOFMEMORY
;
1620 attr
->value
= &text_bool
->text
;
1624 ERR( "unhandled record type %02x\n", type
);
1625 return WS_E_NOT_SUPPORTED
;
1628 if (type
>= RECORD_BYTES8_TEXT
&& type
<= RECORD_BYTES32_TEXT
)
1630 attr
->value
= &text_base64
->text
;
1636 if (!(text_utf8
= alloc_utf8_text( NULL
, len
))) return E_OUTOFMEMORY
;
1637 if (!len
) text_utf8
->value
.bytes
= (BYTE
*)(text_utf8
+ 1); /* quirk */
1638 if ((hr
= read_bytes( reader
, text_utf8
->value
.bytes
, len
)) != S_OK
)
1640 heap_free( text_utf8
);
1645 attr
->value
= &text_utf8
->text
;
1649 static HRESULT
read_attribute_text( struct reader
*reader
, WS_XML_ATTRIBUTE
**ret
)
1651 static const WS_XML_STRING xmlns
= {5, (BYTE
*)"xmlns"};
1652 WS_XML_ATTRIBUTE
*attr
;
1653 unsigned int len
= 0, ch
, skip
;
1654 const unsigned char *start
;
1655 WS_XML_STRING
*prefix
, *localname
;
1656 HRESULT hr
= WS_E_INVALID_FORMAT
;
1658 if (!(attr
= heap_alloc_zero( sizeof(*attr
) ))) return E_OUTOFMEMORY
;
1660 start
= read_current_ptr( reader
);
1663 if (!(ch
= read_utf8_char( reader
, &skip
))) goto error
;
1664 if (!read_isnamechar( ch
)) break;
1665 read_skip( reader
, skip
);
1668 if (!len
) goto error
;
1670 if ((hr
= parse_qname( start
, len
, &prefix
, &localname
)) != S_OK
) goto error
;
1671 if (WsXmlStringEquals( prefix
, &xmlns
, NULL
) == S_OK
)
1673 free_xml_string( prefix
);
1675 if (!(attr
->prefix
= alloc_xml_string( localname
->bytes
, localname
->length
)))
1677 free_xml_string( localname
);
1681 attr
->localName
= localname
;
1683 else if (!prefix
->length
&& WsXmlStringEquals( localname
, &xmlns
, NULL
) == S_OK
)
1686 attr
->prefix
= prefix
;
1687 attr
->localName
= localname
;
1691 attr
->prefix
= prefix
;
1692 attr
->localName
= localname
;
1695 if ((hr
= read_attribute_value_text( reader
, attr
)) != S_OK
) goto error
;
1701 free_attribute( attr
);
1705 static inline BOOL
is_attribute_type( unsigned char type
)
1707 return (type
>= RECORD_SHORT_ATTRIBUTE
&& type
<= RECORD_PREFIX_ATTRIBUTE_Z
);
1710 static WS_XML_STRING
*get_xmlns_localname( struct reader
*reader
, const WS_XML_STRING
*prefix
)
1712 if (!get_namespace( reader
, prefix
)) return alloc_xml_string( NULL
, 0 );
1713 return alloc_xml_string( prefix
->bytes
, prefix
->length
);
1716 static HRESULT
read_attribute_bin( struct reader
*reader
, WS_XML_ATTRIBUTE
**ret
)
1718 WS_XML_UTF8_TEXT
*utf8
;
1719 WS_XML_ATTRIBUTE
*attr
;
1720 unsigned char type
= 0;
1723 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
1724 if (!is_attribute_type( type
)) return WS_E_INVALID_FORMAT
;
1725 if (!(attr
= heap_alloc_zero( sizeof(*attr
) ))) return E_OUTOFMEMORY
;
1727 if (type
>= RECORD_PREFIX_ATTRIBUTE_A
&& type
<= RECORD_PREFIX_ATTRIBUTE_Z
)
1729 unsigned char ch
= type
- RECORD_PREFIX_ATTRIBUTE_A
+ 'a';
1730 if (!(attr
->prefix
= alloc_xml_string( &ch
, 1 )))
1735 if ((hr
= read_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1736 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1738 else if (type
>= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A
&& type
<= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_Z
)
1740 unsigned char ch
= type
- RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A
+ 'a';
1741 if (!(attr
->prefix
= alloc_xml_string( &ch
, 1 )))
1746 if ((hr
= read_dict_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1747 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1753 case RECORD_SHORT_ATTRIBUTE
:
1754 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1759 if ((hr
= read_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1760 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1763 case RECORD_ATTRIBUTE
:
1764 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1765 if ((hr
= read_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1766 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1769 case RECORD_SHORT_DICTIONARY_ATTRIBUTE
:
1770 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1775 if ((hr
= read_dict_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1776 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1779 case RECORD_DICTIONARY_ATTRIBUTE
:
1780 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1781 if ((hr
= read_dict_string( reader
, &attr
->localName
)) != S_OK
) goto error
;
1782 if ((hr
= read_attribute_value_bin( reader
, attr
)) != S_OK
) goto error
;
1785 case RECORD_SHORT_XMLNS_ATTRIBUTE
:
1786 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1791 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1796 if ((hr
= read_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1797 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1801 case RECORD_XMLNS_ATTRIBUTE
:
1802 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1803 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1808 if ((hr
= read_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1809 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1813 case RECORD_SHORT_DICTIONARY_XMLNS_ATTRIBUTE
:
1814 if (!(attr
->prefix
= alloc_xml_string( NULL
, 0 )))
1819 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1824 if ((hr
= read_dict_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1825 if (!(utf8
= alloc_utf8_text( NULL
, 0 )))
1830 attr
->value
= &utf8
->text
;
1831 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1835 case RECORD_DICTIONARY_XMLNS_ATTRIBUTE
:
1836 if ((hr
= read_string( reader
, &attr
->prefix
)) != S_OK
) goto error
;
1837 if (!(attr
->localName
= get_xmlns_localname( reader
, attr
->prefix
)))
1842 if ((hr
= read_dict_string( reader
, &attr
->ns
)) != S_OK
) goto error
;
1843 if (!(utf8
= alloc_utf8_text( NULL
, 0 )))
1848 attr
->value
= &utf8
->text
;
1849 if ((hr
= bind_prefix( reader
, attr
->prefix
, attr
->ns
)) != S_OK
) goto error
;
1854 ERR( "unhandled record type %02x\n", type
);
1855 return WS_E_NOT_SUPPORTED
;
1863 free_attribute( attr
);
1867 static inline struct node
*find_parent( struct reader
*reader
)
1869 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_END_ELEMENT
)
1871 if (is_valid_parent( reader
->current
->parent
->parent
)) return reader
->current
->parent
->parent
;
1874 if (is_valid_parent( reader
->current
)) return reader
->current
;
1875 if (is_valid_parent( reader
->current
->parent
)) return reader
->current
->parent
;
1879 static HRESULT
set_namespaces( struct reader
*reader
, WS_XML_ELEMENT_NODE
*elem
)
1881 static const WS_XML_STRING xml
= {3, (BYTE
*)"xml"};
1882 const WS_XML_STRING
*ns
;
1885 if (!(ns
= get_namespace( reader
, elem
->prefix
))) return WS_E_INVALID_FORMAT
;
1886 if (!(elem
->ns
= dup_xml_string( ns
, FALSE
))) return E_OUTOFMEMORY
;
1888 for (i
= 0; i
< elem
->attributeCount
; i
++)
1890 WS_XML_ATTRIBUTE
*attr
= elem
->attributes
[i
];
1891 if (attr
->isXmlNs
|| WsXmlStringEquals( attr
->prefix
, &xml
, NULL
) == S_OK
) continue;
1892 if (!(ns
= get_namespace( reader
, attr
->prefix
))) return WS_E_INVALID_FORMAT
;
1893 if (!(attr
->ns
= alloc_xml_string( NULL
, ns
->length
))) return E_OUTOFMEMORY
;
1894 if (attr
->ns
->length
) memcpy( attr
->ns
->bytes
, ns
->bytes
, ns
->length
);
1899 static WS_XML_ELEMENT_NODE
*alloc_element_pair(void)
1901 struct node
*node
, *end
;
1902 if (!(node
= alloc_node( WS_XML_NODE_TYPE_ELEMENT
))) return NULL
;
1903 if (!(end
= alloc_node( WS_XML_NODE_TYPE_END_ELEMENT
)))
1908 list_add_tail( &node
->children
, &end
->entry
);
1913 static HRESULT
read_attributes_text( struct reader
*reader
, WS_XML_ELEMENT_NODE
*elem
)
1915 WS_XML_ATTRIBUTE
*attr
;
1918 reader
->current_attr
= 0;
1921 read_skip_whitespace( reader
);
1922 if (!read_cmp( reader
, ">", 1 ) || !read_cmp( reader
, "/>", 2 )) break;
1923 if ((hr
= read_attribute_text( reader
, &attr
)) != S_OK
) return hr
;
1924 if ((hr
= append_attribute( elem
, attr
)) != S_OK
)
1926 free_attribute( attr
);
1929 reader
->current_attr
++;
1934 static HRESULT
read_element_text( struct reader
*reader
)
1936 unsigned int len
= 0, ch
, skip
;
1937 const unsigned char *start
;
1938 struct node
*node
= NULL
, *parent
;
1939 WS_XML_ELEMENT_NODE
*elem
;
1940 HRESULT hr
= WS_E_INVALID_FORMAT
;
1942 if (read_end_of_data( reader
))
1944 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
1945 reader
->last
= reader
->current
;
1946 reader
->state
= READER_STATE_EOF
;
1950 if (read_cmp( reader
, "<", 1 )) return WS_E_INVALID_FORMAT
;
1951 read_skip( reader
, 1 );
1952 if (!read_isnamechar( read_utf8_char( reader
, &skip
)))
1954 read_rewind( reader
, 1 );
1955 return WS_E_INVALID_FORMAT
;
1958 if (!(elem
= alloc_element_pair())) return E_OUTOFMEMORY
;
1959 node
= (struct node
*)elem
;
1961 start
= read_current_ptr( reader
);
1964 if (!(ch
= read_utf8_char( reader
, &skip
))) goto error
;
1965 if (!read_isnamechar( ch
)) break;
1966 read_skip( reader
, skip
);
1969 if (!len
) goto error
;
1971 if (!(parent
= find_parent( reader
))) goto error
;
1972 if ((hr
= parse_qname( start
, len
, &elem
->prefix
, &elem
->localName
)) != S_OK
) goto error
;
1973 if ((hr
= read_attributes_text( reader
, elem
)) != S_OK
) goto error
;
1974 if ((hr
= set_namespaces( reader
, elem
)) != S_OK
) goto error
;
1976 read_insert_node( reader
, parent
, node
);
1977 reader
->state
= READER_STATE_STARTELEMENT
;
1981 destroy_nodes( node
);
1985 static inline BOOL
is_element_type( unsigned char type
)
1987 return (type
>= RECORD_SHORT_ELEMENT
&& type
<= RECORD_PREFIX_ELEMENT_Z
);
1990 static HRESULT
read_attributes_bin( struct reader
*reader
, WS_XML_ELEMENT_NODE
*elem
)
1992 WS_XML_ATTRIBUTE
*attr
;
1996 reader
->current_attr
= 0;
1999 if ((hr
= read_peek( reader
, &type
)) != S_OK
) return hr
;
2000 if (!is_attribute_type( type
)) break;
2001 if ((hr
= read_attribute_bin( reader
, &attr
)) != S_OK
) return hr
;
2002 if ((hr
= append_attribute( elem
, attr
)) != S_OK
)
2004 free_attribute( attr
);
2007 reader
->current_attr
++;
2012 static HRESULT
read_element_bin( struct reader
*reader
)
2014 struct node
*node
= NULL
, *parent
;
2015 WS_XML_ELEMENT_NODE
*elem
;
2019 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2020 if (!is_element_type( type
)) return WS_E_INVALID_FORMAT
;
2022 if (!(elem
= alloc_element_pair())) return E_OUTOFMEMORY
;
2023 node
= (struct node
*)elem
;
2025 if (type
>= RECORD_PREFIX_ELEMENT_A
&& type
<= RECORD_PREFIX_ELEMENT_Z
)
2027 unsigned char ch
= type
- RECORD_PREFIX_ELEMENT_A
+ 'a';
2028 if (!(elem
->prefix
= alloc_xml_string( &ch
, 1 )))
2033 if ((hr
= read_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2035 else if (type
>= RECORD_PREFIX_DICTIONARY_ELEMENT_A
&& type
<= RECORD_PREFIX_DICTIONARY_ELEMENT_Z
)
2037 unsigned char ch
= type
- RECORD_PREFIX_DICTIONARY_ELEMENT_A
+ 'a';
2038 if (!(elem
->prefix
= alloc_xml_string( &ch
, 1 )))
2043 if ((hr
= read_dict_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2049 case RECORD_SHORT_ELEMENT
:
2050 if (!(elem
->prefix
= alloc_xml_string( NULL
, 0 )))
2055 if ((hr
= read_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2058 case RECORD_ELEMENT
:
2059 if ((hr
= read_string( reader
, &elem
->prefix
)) != S_OK
) goto error
;
2060 if ((hr
= read_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2063 case RECORD_SHORT_DICTIONARY_ELEMENT
:
2064 if (!(elem
->prefix
= alloc_xml_string( NULL
, 0 )))
2069 if ((hr
= read_dict_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2072 case RECORD_DICTIONARY_ELEMENT
:
2073 if ((hr
= read_string( reader
, &elem
->prefix
)) != S_OK
) goto error
;
2074 if ((hr
= read_dict_string( reader
, &elem
->localName
)) != S_OK
) goto error
;
2078 ERR( "unhandled record type %02x\n", type
);
2079 return WS_E_NOT_SUPPORTED
;
2083 if (!(parent
= find_parent( reader
)))
2085 hr
= WS_E_INVALID_FORMAT
;
2089 if ((hr
= read_attributes_bin( reader
, elem
)) != S_OK
) goto error
;
2090 if ((hr
= set_namespaces( reader
, elem
)) != S_OK
) goto error
;
2092 read_insert_node( reader
, parent
, node
);
2093 reader
->state
= READER_STATE_STARTELEMENT
;
2097 destroy_nodes( node
);
2101 static HRESULT
read_text_text( struct reader
*reader
)
2103 unsigned int len
= 0, ch
, skip
;
2104 const unsigned char *start
;
2105 struct node
*node
, *parent
;
2106 WS_XML_TEXT_NODE
*text
;
2107 WS_XML_UTF8_TEXT
*utf8
;
2110 start
= read_current_ptr( reader
);
2113 if (read_end_of_data( reader
)) break;
2114 if (!(ch
= read_utf8_char( reader
, &skip
))) return WS_E_INVALID_FORMAT
;
2115 if (ch
== '<') break;
2116 read_skip( reader
, skip
);
2120 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2122 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return E_OUTOFMEMORY
;
2123 text
= (WS_XML_TEXT_NODE
*)node
;
2124 if (!(utf8
= alloc_utf8_text( NULL
, len
)))
2127 return E_OUTOFMEMORY
;
2129 if ((hr
= decode_text( start
, len
, utf8
->value
.bytes
, &utf8
->value
.length
)) != S_OK
)
2135 text
->text
= &utf8
->text
;
2137 read_insert_node( reader
, parent
, node
);
2138 reader
->state
= READER_STATE_TEXT
;
2139 reader
->text_conv_offset
= 0;
2143 static struct node
*alloc_utf8_text_node( const BYTE
*data
, ULONG len
, WS_XML_UTF8_TEXT
**ret
)
2146 WS_XML_UTF8_TEXT
*utf8
;
2147 WS_XML_TEXT_NODE
*text
;
2149 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2150 if (!(utf8
= alloc_utf8_text( data
, len
)))
2155 text
= (WS_XML_TEXT_NODE
*)node
;
2156 text
->text
= &utf8
->text
;
2157 if (ret
) *ret
= utf8
;
2161 static struct node
*alloc_base64_text_node( const BYTE
*data
, ULONG len
, WS_XML_BASE64_TEXT
**ret
)
2164 WS_XML_BASE64_TEXT
*base64
;
2165 WS_XML_TEXT_NODE
*text
;
2167 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2168 if (!(base64
= alloc_base64_text( data
, len
)))
2173 text
= (WS_XML_TEXT_NODE
*)node
;
2174 text
->text
= &base64
->text
;
2175 if (ret
) *ret
= base64
;
2179 static struct node
*alloc_bool_text_node( BOOL value
)
2182 WS_XML_BOOL_TEXT
*text_bool
;
2183 WS_XML_TEXT_NODE
*text
;
2185 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2186 if (!(text_bool
= alloc_bool_text( value
)))
2191 text
= (WS_XML_TEXT_NODE
*)node
;
2192 text
->text
= &text_bool
->text
;
2196 static struct node
*alloc_int32_text_node( INT32 value
)
2199 WS_XML_INT32_TEXT
*text_int32
;
2200 WS_XML_TEXT_NODE
*text
;
2202 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2203 if (!(text_int32
= alloc_int32_text( value
)))
2208 text
= (WS_XML_TEXT_NODE
*)node
;
2209 text
->text
= &text_int32
->text
;
2213 static struct node
*alloc_int64_text_node( INT64 value
)
2216 WS_XML_INT64_TEXT
*text_int64
;
2217 WS_XML_TEXT_NODE
*text
;
2219 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2220 if (!(text_int64
= alloc_int64_text( value
)))
2225 text
= (WS_XML_TEXT_NODE
*)node
;
2226 text
->text
= &text_int64
->text
;
2230 static struct node
*alloc_float_text_node( float value
)
2233 WS_XML_FLOAT_TEXT
*text_float
;
2234 WS_XML_TEXT_NODE
*text
;
2236 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2237 if (!(text_float
= alloc_float_text( value
)))
2242 text
= (WS_XML_TEXT_NODE
*)node
;
2243 text
->text
= &text_float
->text
;
2247 static struct node
*alloc_double_text_node( double value
)
2250 WS_XML_DOUBLE_TEXT
*text_double
;
2251 WS_XML_TEXT_NODE
*text
;
2253 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2254 if (!(text_double
= alloc_double_text( value
)))
2259 text
= (WS_XML_TEXT_NODE
*)node
;
2260 text
->text
= &text_double
->text
;
2264 static struct node
*alloc_datetime_text_node( const WS_DATETIME
*value
)
2267 WS_XML_DATETIME_TEXT
*text_datetime
;
2268 WS_XML_TEXT_NODE
*text
;
2270 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2271 if (!(text_datetime
= alloc_datetime_text( value
)))
2276 text
= (WS_XML_TEXT_NODE
*)node
;
2277 text
->text
= &text_datetime
->text
;
2281 static struct node
*alloc_unique_id_text_node( const GUID
*value
)
2284 WS_XML_UNIQUE_ID_TEXT
*text_unique_id
;
2285 WS_XML_TEXT_NODE
*text
;
2287 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2288 if (!(text_unique_id
= alloc_unique_id_text( value
)))
2293 text
= (WS_XML_TEXT_NODE
*)node
;
2294 text
->text
= &text_unique_id
->text
;
2298 static struct node
*alloc_guid_text_node( const GUID
*value
)
2301 WS_XML_GUID_TEXT
*text_guid
;
2302 WS_XML_TEXT_NODE
*text
;
2304 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2305 if (!(text_guid
= alloc_guid_text( value
)))
2310 text
= (WS_XML_TEXT_NODE
*)node
;
2311 text
->text
= &text_guid
->text
;
2315 static struct node
*alloc_uint64_text_node( UINT64 value
)
2318 WS_XML_UINT64_TEXT
*text_uint64
;
2319 WS_XML_TEXT_NODE
*text
;
2321 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return NULL
;
2322 if (!(text_uint64
= alloc_uint64_text( value
)))
2327 text
= (WS_XML_TEXT_NODE
*)node
;
2328 text
->text
= &text_uint64
->text
;
2332 static HRESULT
append_text_bytes( struct reader
*reader
, WS_XML_TEXT_NODE
*node
, ULONG len
)
2334 WS_XML_BASE64_TEXT
*new, *old
= (WS_XML_BASE64_TEXT
*)node
->text
;
2337 if (!(new = alloc_base64_text( NULL
, old
->length
+ len
))) return E_OUTOFMEMORY
;
2338 memcpy( new->bytes
, old
->bytes
, old
->length
);
2339 if ((hr
= read_bytes( reader
, new->bytes
+ old
->length
, len
)) != S_OK
) return hr
;
2341 node
->text
= &new->text
;
2345 static HRESULT
read_text_bytes( struct reader
*reader
, unsigned char type
)
2347 struct node
*node
= NULL
, *parent
;
2348 WS_XML_BASE64_TEXT
*base64
;
2352 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2357 case RECORD_BYTES8_TEXT
:
2358 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT
:
2361 if ((hr
= read_byte( reader
, (unsigned char *)&len_uint8
)) != S_OK
) goto error
;
2365 case RECORD_BYTES16_TEXT
:
2366 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT
:
2369 if ((hr
= read_bytes( reader
, (unsigned char *)&len_uint16
, sizeof(len_uint16
) )) != S_OK
) goto error
;
2373 case RECORD_BYTES32_TEXT
:
2374 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT
:
2377 if ((hr
= read_bytes( reader
, (unsigned char *)&len_int32
, sizeof(len_int32
) )) != S_OK
) goto error
;
2380 hr
= WS_E_INVALID_FORMAT
;
2387 ERR( "unexpected type %u\n", type
);
2394 if (!(node
= alloc_base64_text_node( NULL
, len
, &base64
))) return E_OUTOFMEMORY
;
2395 if ((hr
= read_bytes( reader
, base64
->bytes
, len
)) != S_OK
) goto error
;
2397 else if ((hr
= append_text_bytes( reader
, (WS_XML_TEXT_NODE
*)node
, len
)) != S_OK
) goto error
;
2401 node
->flags
|= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
;
2404 if ((hr
= read_peek( reader
, &type
)) != S_OK
) goto error
;
2405 if (type
< RECORD_BYTES8_TEXT
|| type
> RECORD_BYTES32_TEXT_WITH_ENDELEMENT
) break;
2406 read_skip( reader
, 1 );
2409 read_insert_node( reader
, parent
, node
);
2410 reader
->state
= READER_STATE_TEXT
;
2411 reader
->text_conv_offset
= 0;
2419 static HRESULT
read_text_bin( struct reader
*reader
)
2421 struct node
*node
= NULL
, *parent
;
2423 WS_XML_UTF8_TEXT
*utf8
;
2431 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2432 if (!is_text_type( type
) || !(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2436 case RECORD_ZERO_TEXT
:
2437 case RECORD_ZERO_TEXT_WITH_ENDELEMENT
:
2438 if (!(node
= alloc_int32_text_node( 0 ))) return E_OUTOFMEMORY
;
2441 case RECORD_ONE_TEXT
:
2442 case RECORD_ONE_TEXT_WITH_ENDELEMENT
:
2443 if (!(node
= alloc_int32_text_node( 1 ))) return E_OUTOFMEMORY
;
2446 case RECORD_FALSE_TEXT
:
2447 case RECORD_FALSE_TEXT_WITH_ENDELEMENT
:
2448 if (!(node
= alloc_bool_text_node( FALSE
))) return E_OUTOFMEMORY
;
2451 case RECORD_TRUE_TEXT
:
2452 case RECORD_TRUE_TEXT_WITH_ENDELEMENT
:
2453 if (!(node
= alloc_bool_text_node( TRUE
))) return E_OUTOFMEMORY
;
2456 case RECORD_INT8_TEXT
:
2457 case RECORD_INT8_TEXT_WITH_ENDELEMENT
:
2460 if ((hr
= read_byte( reader
, (unsigned char *)&val_int8
)) != S_OK
) return hr
;
2461 if (!(node
= alloc_int32_text_node( val_int8
))) return E_OUTOFMEMORY
;
2464 case RECORD_INT16_TEXT
:
2465 case RECORD_INT16_TEXT_WITH_ENDELEMENT
:
2468 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int16
, sizeof(val_int16
) )) != S_OK
) return hr
;
2469 if (!(node
= alloc_int32_text_node( val_int16
))) return E_OUTOFMEMORY
;
2472 case RECORD_INT32_TEXT
:
2473 case RECORD_INT32_TEXT_WITH_ENDELEMENT
:
2474 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
2475 if (!(node
= alloc_int32_text_node( val_int32
))) return E_OUTOFMEMORY
;
2478 case RECORD_INT64_TEXT
:
2479 case RECORD_INT64_TEXT_WITH_ENDELEMENT
:
2482 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int64
, sizeof(val_int64
) )) != S_OK
) return hr
;
2483 if (!(node
= alloc_int64_text_node( val_int64
))) return E_OUTOFMEMORY
;
2486 case RECORD_FLOAT_TEXT
:
2487 case RECORD_FLOAT_TEXT_WITH_ENDELEMENT
:
2490 if ((hr
= read_bytes( reader
, (unsigned char *)&val_float
, sizeof(val_float
) )) != S_OK
) return hr
;
2491 if (!(node
= alloc_float_text_node( val_float
))) return E_OUTOFMEMORY
;
2494 case RECORD_DOUBLE_TEXT
:
2495 case RECORD_DOUBLE_TEXT_WITH_ENDELEMENT
:
2498 if ((hr
= read_bytes( reader
, (unsigned char *)&val_double
, sizeof(val_double
) )) != S_OK
) return hr
;
2499 if (!(node
= alloc_double_text_node( val_double
))) return E_OUTOFMEMORY
;
2502 case RECORD_DATETIME_TEXT
:
2503 case RECORD_DATETIME_TEXT_WITH_ENDELEMENT
:
2505 WS_DATETIME datetime
;
2506 if ((hr
= read_datetime( reader
, &datetime
)) != S_OK
) return hr
;
2507 if (!(node
= alloc_datetime_text_node( &datetime
))) return E_OUTOFMEMORY
;
2510 case RECORD_CHARS8_TEXT
:
2511 case RECORD_CHARS8_TEXT_WITH_ENDELEMENT
:
2512 if ((hr
= read_byte( reader
, (unsigned char *)&val_uint8
)) != S_OK
) return hr
;
2516 case RECORD_CHARS16_TEXT
:
2517 case RECORD_CHARS16_TEXT_WITH_ENDELEMENT
:
2518 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint16
, sizeof(val_uint16
) )) != S_OK
) return hr
;
2522 case RECORD_CHARS32_TEXT
:
2523 case RECORD_CHARS32_TEXT_WITH_ENDELEMENT
:
2524 if ((hr
= read_bytes( reader
, (unsigned char *)&val_int32
, sizeof(val_int32
) )) != S_OK
) return hr
;
2525 if (val_int32
< 0) return WS_E_INVALID_FORMAT
;
2529 case RECORD_BYTES8_TEXT
:
2530 case RECORD_BYTES8_TEXT_WITH_ENDELEMENT
:
2531 case RECORD_BYTES16_TEXT
:
2532 case RECORD_BYTES16_TEXT_WITH_ENDELEMENT
:
2533 case RECORD_BYTES32_TEXT
:
2534 case RECORD_BYTES32_TEXT_WITH_ENDELEMENT
:
2535 return read_text_bytes( reader
, type
);
2537 case RECORD_EMPTY_TEXT
:
2538 case RECORD_EMPTY_TEXT_WITH_ENDELEMENT
:
2542 case RECORD_DICTIONARY_TEXT
:
2543 case RECORD_DICTIONARY_TEXT_WITH_ENDELEMENT
:
2545 const WS_XML_STRING
*str
;
2546 if ((hr
= read_int31( reader
, &id
)) != S_OK
) return hr
;
2547 if ((hr
= lookup_string( reader
, id
, &str
)) != S_OK
) return hr
;
2548 if (!(node
= alloc_utf8_text_node( str
->bytes
, str
->length
, NULL
))) return E_OUTOFMEMORY
;
2551 case RECORD_UNIQUE_ID_TEXT
:
2552 case RECORD_UNIQUE_ID_TEXT_WITH_ENDELEMENT
:
2553 if ((hr
= read_bytes( reader
, (unsigned char *)&uuid
, sizeof(uuid
) )) != S_OK
) return hr
;
2554 if (!(node
= alloc_unique_id_text_node( &uuid
))) return E_OUTOFMEMORY
;
2557 case RECORD_GUID_TEXT
:
2558 case RECORD_GUID_TEXT_WITH_ENDELEMENT
:
2559 if ((hr
= read_bytes( reader
, (unsigned char *)&uuid
, sizeof(uuid
) )) != S_OK
) return hr
;
2560 if (!(node
= alloc_guid_text_node( &uuid
))) return E_OUTOFMEMORY
;
2563 case RECORD_UINT64_TEXT
:
2564 case RECORD_UINT64_TEXT_WITH_ENDELEMENT
:
2567 if ((hr
= read_bytes( reader
, (unsigned char *)&val_uint64
, sizeof(val_uint64
) )) != S_OK
) return hr
;
2568 if (!(node
= alloc_uint64_text_node( val_uint64
))) return E_OUTOFMEMORY
;
2571 case RECORD_BOOL_TEXT
:
2572 case RECORD_BOOL_TEXT_WITH_ENDELEMENT
:
2575 if ((hr
= read_bytes( reader
, (unsigned char *)&val_bool
, sizeof(val_bool
) )) != S_OK
) return hr
;
2576 if (!(node
= alloc_bool_text_node( !!val_bool
))) return E_OUTOFMEMORY
;
2580 ERR( "unhandled record type %02x\n", type
);
2581 return WS_E_NOT_SUPPORTED
;
2586 if (!(node
= alloc_utf8_text_node( NULL
, len
, &utf8
))) return E_OUTOFMEMORY
;
2587 if (!len
) utf8
->value
.bytes
= (BYTE
*)(utf8
+ 1); /* quirk */
2588 if ((hr
= read_bytes( reader
, utf8
->value
.bytes
, len
)) != S_OK
)
2595 if (type
& 1) node
->flags
|= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
;
2596 read_insert_node( reader
, parent
, node
);
2597 reader
->state
= READER_STATE_TEXT
;
2598 reader
->text_conv_offset
= 0;
2602 static HRESULT
read_node_text( struct reader
* );
2604 static HRESULT
read_startelement_text( struct reader
*reader
)
2606 read_skip_whitespace( reader
);
2607 if (!read_cmp( reader
, "/>", 2 ))
2609 read_skip( reader
, 2 );
2610 reader
->current
= LIST_ENTRY( list_tail( &reader
->current
->children
), struct node
, entry
);
2611 reader
->last
= reader
->current
;
2612 reader
->state
= READER_STATE_ENDELEMENT
;
2615 else if (!read_cmp( reader
, ">", 1 ))
2617 read_skip( reader
, 1 );
2618 return read_node_text( reader
);
2620 return WS_E_INVALID_FORMAT
;
2623 static HRESULT
read_node_bin( struct reader
* );
2625 static HRESULT
read_startelement_bin( struct reader
*reader
)
2627 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) return WS_E_INVALID_FORMAT
;
2628 return read_node_bin( reader
);
2631 static HRESULT
read_startelement( struct reader
*reader
)
2633 switch (reader
->input_enc
)
2635 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_startelement_text( reader
);
2636 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_startelement_bin( reader
);
2638 ERR( "unhandled encoding %u\n", reader
->input_enc
);
2639 return WS_E_NOT_SUPPORTED
;
2643 static HRESULT
read_to_startelement_text( struct reader
*reader
, BOOL
*found
)
2647 switch (reader
->state
)
2649 case READER_STATE_INITIAL
:
2650 if ((hr
= read_xmldecl( reader
)) != S_OK
) return hr
;
2653 case READER_STATE_STARTELEMENT
:
2654 if (found
) *found
= TRUE
;
2661 read_skip_whitespace( reader
);
2662 if ((hr
= read_element_text( reader
)) == S_OK
&& found
)
2664 if (reader
->state
== READER_STATE_STARTELEMENT
)
2673 static HRESULT
read_to_startelement_bin( struct reader
*reader
, BOOL
*found
)
2677 if (reader
->state
== READER_STATE_STARTELEMENT
)
2679 if (found
) *found
= TRUE
;
2683 if ((hr
= read_element_bin( reader
)) == S_OK
&& found
)
2685 if (reader
->state
== READER_STATE_STARTELEMENT
)
2694 static HRESULT
read_to_startelement( struct reader
*reader
, BOOL
*found
)
2696 switch (reader
->input_enc
)
2698 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_to_startelement_text( reader
, found
);
2699 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_to_startelement_bin( reader
, found
);
2701 ERR( "unhandled encoding %u\n", reader
->input_enc
);
2702 return WS_E_NOT_SUPPORTED
;
2706 static int cmp_name( const unsigned char *name1
, ULONG len1
, const unsigned char *name2
, ULONG len2
)
2709 if (len1
!= len2
) return 1;
2710 for (i
= 0; i
< len1
; i
++) { if (toupper( name1
[i
] ) != toupper( name2
[i
] )) return 1; }
2714 static struct node
*find_startelement( struct reader
*reader
, const WS_XML_STRING
*prefix
,
2715 const WS_XML_STRING
*localname
)
2717 struct node
*parent
;
2718 const WS_XML_STRING
*str
;
2720 for (parent
= reader
->current
; parent
; parent
= parent
->parent
)
2722 if (node_type( parent
) == WS_XML_NODE_TYPE_ELEMENT
)
2724 str
= parent
->hdr
.prefix
;
2725 if (cmp_name( str
->bytes
, str
->length
, prefix
->bytes
, prefix
->length
)) continue;
2726 str
= parent
->hdr
.localName
;
2727 if (cmp_name( str
->bytes
, str
->length
, localname
->bytes
, localname
->length
)) continue;
2734 static HRESULT
read_endelement_text( struct reader
*reader
)
2736 struct node
*parent
;
2737 unsigned int len
= 0, ch
, skip
;
2738 const unsigned char *start
;
2739 WS_XML_STRING prefix
, localname
;
2742 if (read_cmp( reader
, "</", 2 )) return WS_E_INVALID_FORMAT
;
2743 read_skip( reader
, 2 );
2745 start
= read_current_ptr( reader
);
2748 if (!(ch
= read_utf8_char( reader
, &skip
))) return WS_E_INVALID_FORMAT
;
2751 read_skip( reader
, 1 );
2754 if (!read_isnamechar( ch
)) return WS_E_INVALID_FORMAT
;
2755 read_skip( reader
, skip
);
2759 if ((hr
= split_qname( start
, len
, &prefix
, &localname
)) != S_OK
) return hr
;
2760 if (!(parent
= find_startelement( reader
, &prefix
, &localname
))) return WS_E_INVALID_FORMAT
;
2762 reader
->current
= LIST_ENTRY( list_tail( &parent
->children
), struct node
, entry
);
2763 reader
->last
= reader
->current
;
2764 reader
->state
= READER_STATE_ENDELEMENT
;
2768 static HRESULT
read_endelement_bin( struct reader
*reader
)
2770 struct node
*parent
;
2774 if (!(reader
->current
->flags
& NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
))
2776 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2777 if (type
!= RECORD_ENDELEMENT
) return WS_E_INVALID_FORMAT
;
2779 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2781 reader
->current
= LIST_ENTRY( list_tail( &parent
->children
), struct node
, entry
);
2782 reader
->last
= reader
->current
;
2783 reader
->state
= READER_STATE_ENDELEMENT
;
2787 static HRESULT
read_endelement( struct reader
*reader
)
2789 if (reader
->state
== READER_STATE_EOF
) return WS_E_INVALID_FORMAT
;
2791 if (read_end_of_data( reader
))
2793 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
2794 reader
->last
= reader
->current
;
2795 reader
->state
= READER_STATE_EOF
;
2799 switch (reader
->input_enc
)
2801 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_endelement_text( reader
);
2802 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_endelement_bin( reader
);
2804 ERR( "unhandled encoding %u\n", reader
->input_enc
);
2805 return WS_E_NOT_SUPPORTED
;
2809 static HRESULT
read_comment_text( struct reader
*reader
)
2811 unsigned int len
= 0, ch
, skip
;
2812 const unsigned char *start
;
2813 struct node
*node
, *parent
;
2814 WS_XML_COMMENT_NODE
*comment
;
2816 if (read_cmp( reader
, "<!--", 4 )) return WS_E_INVALID_FORMAT
;
2817 read_skip( reader
, 4 );
2819 start
= read_current_ptr( reader
);
2822 if (!read_cmp( reader
, "-->", 3 ))
2824 read_skip( reader
, 3 );
2827 if (!(ch
= read_utf8_char( reader
, &skip
))) return WS_E_INVALID_FORMAT
;
2828 read_skip( reader
, skip
);
2832 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2834 if (!(node
= alloc_node( WS_XML_NODE_TYPE_COMMENT
))) return E_OUTOFMEMORY
;
2835 comment
= (WS_XML_COMMENT_NODE
*)node
;
2836 if (!(comment
->value
.bytes
= heap_alloc( len
)))
2839 return E_OUTOFMEMORY
;
2841 memcpy( comment
->value
.bytes
, start
, len
);
2842 comment
->value
.length
= len
;
2844 read_insert_node( reader
, parent
, node
);
2845 reader
->state
= READER_STATE_COMMENT
;
2849 static HRESULT
read_comment_bin( struct reader
*reader
)
2851 struct node
*node
, *parent
;
2852 WS_XML_COMMENT_NODE
*comment
;
2857 if ((hr
= read_byte( reader
, &type
)) != S_OK
) return hr
;
2858 if (type
!= RECORD_COMMENT
) return WS_E_INVALID_FORMAT
;
2859 if ((hr
= read_int31( reader
, &len
)) != S_OK
) return hr
;
2861 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2863 if (!(node
= alloc_node( WS_XML_NODE_TYPE_COMMENT
))) return E_OUTOFMEMORY
;
2864 comment
= (WS_XML_COMMENT_NODE
*)node
;
2865 if (!(comment
->value
.bytes
= heap_alloc( len
)))
2868 return E_OUTOFMEMORY
;
2870 if ((hr
= read_bytes( reader
, comment
->value
.bytes
, len
)) != S_OK
)
2873 return E_OUTOFMEMORY
;
2875 comment
->value
.length
= len
;
2877 read_insert_node( reader
, parent
, node
);
2878 reader
->state
= READER_STATE_COMMENT
;
2882 static HRESULT
read_startcdata( struct reader
*reader
)
2884 struct node
*node
, *endnode
, *parent
;
2886 if (read_cmp( reader
, "<![CDATA[", 9 )) return WS_E_INVALID_FORMAT
;
2887 read_skip( reader
, 9 );
2889 if (!(parent
= find_parent( reader
))) return WS_E_INVALID_FORMAT
;
2891 if (!(node
= alloc_node( WS_XML_NODE_TYPE_CDATA
))) return E_OUTOFMEMORY
;
2892 if (!(endnode
= alloc_node( WS_XML_NODE_TYPE_END_CDATA
)))
2895 return E_OUTOFMEMORY
;
2897 list_add_tail( &node
->children
, &endnode
->entry
);
2898 endnode
->parent
= node
;
2900 read_insert_node( reader
, parent
, node
);
2901 reader
->state
= READER_STATE_STARTCDATA
;
2905 static HRESULT
read_cdata( struct reader
*reader
)
2907 unsigned int len
= 0, ch
, skip
;
2908 const unsigned char *start
;
2910 WS_XML_TEXT_NODE
*text
;
2911 WS_XML_UTF8_TEXT
*utf8
;
2913 start
= read_current_ptr( reader
);
2916 if (!read_cmp( reader
, "]]>", 3 )) break;
2917 if (!(ch
= read_utf8_char( reader
, &skip
))) return WS_E_INVALID_FORMAT
;
2918 read_skip( reader
, skip
);
2922 if (!(node
= alloc_node( WS_XML_NODE_TYPE_TEXT
))) return E_OUTOFMEMORY
;
2923 text
= (WS_XML_TEXT_NODE
*)node
;
2924 if (!(utf8
= alloc_utf8_text( start
, len
)))
2927 return E_OUTOFMEMORY
;
2929 text
->text
= &utf8
->text
;
2931 read_insert_node( reader
, reader
->current
, node
);
2932 reader
->state
= READER_STATE_CDATA
;
2936 static HRESULT
read_endcdata( struct reader
*reader
)
2938 struct node
*parent
;
2940 if (read_cmp( reader
, "]]>", 3 )) return WS_E_INVALID_FORMAT
;
2941 read_skip( reader
, 3 );
2943 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
) parent
= reader
->current
->parent
;
2944 else parent
= reader
->current
;
2946 reader
->current
= LIST_ENTRY( list_tail( &parent
->children
), struct node
, entry
);
2947 reader
->last
= reader
->current
;
2948 reader
->state
= READER_STATE_ENDCDATA
;
2952 static HRESULT
read_node_text( struct reader
*reader
)
2958 if (read_end_of_data( reader
))
2960 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
2961 reader
->last
= reader
->current
;
2962 reader
->state
= READER_STATE_EOF
;
2965 if (reader
->state
== READER_STATE_STARTCDATA
) return read_cdata( reader
);
2966 else if (reader
->state
== READER_STATE_CDATA
) return read_endcdata( reader
);
2967 else if (!read_cmp( reader
, "<?", 2 ))
2969 hr
= read_xmldecl( reader
);
2970 if (FAILED( hr
)) return hr
;
2972 else if (!read_cmp( reader
, "</", 2 )) return read_endelement_text( reader
);
2973 else if (!read_cmp( reader
, "<![CDATA[", 9 )) return read_startcdata( reader
);
2974 else if (!read_cmp( reader
, "<!--", 4 )) return read_comment_text( reader
);
2975 else if (!read_cmp( reader
, "<", 1 )) return read_element_text( reader
);
2976 else if (!read_cmp( reader
, "/>", 2 ) || !read_cmp( reader
, ">", 1 )) return read_startelement_text( reader
);
2977 else return read_text_text( reader
);
2981 static HRESULT
read_node_bin( struct reader
*reader
)
2986 if (reader
->current
->flags
& NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT
)
2988 reader
->current
= LIST_ENTRY( list_tail( &reader
->current
->parent
->children
), struct node
, entry
);
2989 reader
->last
= reader
->current
;
2990 reader
->state
= READER_STATE_ENDELEMENT
;
2993 if (read_end_of_data( reader
))
2995 reader
->current
= LIST_ENTRY( list_tail( &reader
->root
->children
), struct node
, entry
);
2996 reader
->last
= reader
->current
;
2997 reader
->state
= READER_STATE_EOF
;
3001 if ((hr
= read_peek( reader
, &type
)) != S_OK
) return hr
;
3002 if (type
== RECORD_ENDELEMENT
)
3004 return read_endelement_bin( reader
);
3006 else if (type
== RECORD_COMMENT
)
3008 return read_comment_bin( reader
);
3010 else if (type
>= RECORD_SHORT_ELEMENT
&& type
<= RECORD_PREFIX_ELEMENT_Z
)
3012 return read_element_bin( reader
);
3014 else if (type
>= RECORD_ZERO_TEXT
&& type
<= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT
)
3016 return read_text_bin( reader
);
3018 FIXME( "unhandled record type %02x\n", type
);
3019 return WS_E_NOT_SUPPORTED
;
3022 static HRESULT
read_node( struct reader
*reader
)
3024 switch (reader
->input_enc
)
3026 case WS_XML_READER_ENCODING_TYPE_TEXT
: return read_node_text( reader
);
3027 case WS_XML_READER_ENCODING_TYPE_BINARY
: return read_node_bin( reader
);
3029 ERR( "unhandled encoding %u\n", reader
->input_enc
);
3030 return WS_E_NOT_SUPPORTED
;
3034 HRESULT
copy_node( WS_XML_READER
*handle
, WS_XML_WRITER_ENCODING_TYPE enc
, struct node
**node
)
3036 struct reader
*reader
= (struct reader
*)handle
;
3037 const struct list
*ptr
;
3038 const struct node
*start
;
3041 EnterCriticalSection( &reader
->cs
);
3043 if (reader
->magic
!= READER_MAGIC
)
3045 LeaveCriticalSection( &reader
->cs
);
3046 return E_INVALIDARG
;
3049 if (reader
->current
!= reader
->root
) ptr
= &reader
->current
->entry
;
3050 else /* copy whole tree */
3052 if (!read_end_of_data( reader
))
3056 if ((hr
= read_node( reader
)) != S_OK
) goto done
;
3057 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_EOF
) break;
3060 ptr
= list_head( &reader
->root
->children
);
3063 start
= LIST_ENTRY( ptr
, struct node
, entry
);
3064 if (node_type( start
) == WS_XML_NODE_TYPE_EOF
) hr
= WS_E_INVALID_OPERATION
;
3065 else hr
= dup_tree( start
, enc
, node
);
3068 LeaveCriticalSection( &reader
->cs
);
3072 /**************************************************************************
3073 * WsReadEndElement [webservices.@]
3075 HRESULT WINAPI
WsReadEndElement( WS_XML_READER
*handle
, WS_ERROR
*error
)
3077 struct reader
*reader
= (struct reader
*)handle
;
3080 TRACE( "%p %p\n", handle
, error
);
3081 if (error
) FIXME( "ignoring error parameter\n" );
3083 if (!reader
) return E_INVALIDARG
;
3085 EnterCriticalSection( &reader
->cs
);
3087 if (reader
->magic
!= READER_MAGIC
)
3089 LeaveCriticalSection( &reader
->cs
);
3090 return E_INVALIDARG
;
3093 hr
= read_endelement( reader
);
3095 LeaveCriticalSection( &reader
->cs
);
3099 /**************************************************************************
3100 * WsReadNode [webservices.@]
3102 HRESULT WINAPI
WsReadNode( WS_XML_READER
*handle
, WS_ERROR
*error
)
3104 struct reader
*reader
= (struct reader
*)handle
;
3107 TRACE( "%p %p\n", handle
, error
);
3108 if (error
) FIXME( "ignoring error parameter\n" );
3110 if (!reader
) return E_INVALIDARG
;
3112 EnterCriticalSection( &reader
->cs
);
3114 if (reader
->magic
!= READER_MAGIC
)
3116 LeaveCriticalSection( &reader
->cs
);
3117 return E_INVALIDARG
;
3120 hr
= read_node( reader
);
3122 LeaveCriticalSection( &reader
->cs
);
3126 static HRESULT
skip_node( struct reader
*reader
)
3128 const struct node
*parent
;
3131 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_EOF
) return WS_E_INVALID_OPERATION
;
3132 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_ELEMENT
) parent
= reader
->current
;
3137 if ((hr
= read_node( reader
)) != S_OK
|| !parent
) break;
3138 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_END_ELEMENT
) continue;
3139 if (reader
->current
->parent
== parent
) return read_node( reader
);
3145 /**************************************************************************
3146 * WsSkipNode [webservices.@]
3148 HRESULT WINAPI
WsSkipNode( WS_XML_READER
*handle
, WS_ERROR
*error
)
3150 struct reader
*reader
= (struct reader
*)handle
;
3153 TRACE( "%p %p\n", handle
, error
);
3154 if (error
) FIXME( "ignoring error parameter\n" );
3156 if (!reader
) return E_INVALIDARG
;
3158 EnterCriticalSection( &reader
->cs
);
3160 if (reader
->magic
!= READER_MAGIC
)
3162 LeaveCriticalSection( &reader
->cs
);
3163 return E_INVALIDARG
;
3166 hr
= skip_node( reader
);
3168 LeaveCriticalSection( &reader
->cs
);
3172 /**************************************************************************
3173 * WsReadStartElement [webservices.@]
3175 HRESULT WINAPI
WsReadStartElement( WS_XML_READER
*handle
, WS_ERROR
*error
)
3177 struct reader
*reader
= (struct reader
*)handle
;
3180 TRACE( "%p %p\n", handle
, error
);
3181 if (error
) FIXME( "ignoring error parameter\n" );
3183 if (!reader
) return E_INVALIDARG
;
3185 EnterCriticalSection( &reader
->cs
);
3187 if (reader
->magic
!= READER_MAGIC
)
3189 LeaveCriticalSection( &reader
->cs
);
3190 return E_INVALIDARG
;
3193 hr
= read_startelement( reader
);
3195 LeaveCriticalSection( &reader
->cs
);
3199 /**************************************************************************
3200 * WsReadToStartElement [webservices.@]
3202 HRESULT WINAPI
WsReadToStartElement( WS_XML_READER
*handle
, const WS_XML_STRING
*localname
,
3203 const WS_XML_STRING
*ns
, BOOL
*found
, WS_ERROR
*error
)
3205 struct reader
*reader
= (struct reader
*)handle
;
3208 TRACE( "%p %s %s %p %p\n", handle
, debugstr_xmlstr(localname
), debugstr_xmlstr(ns
), found
, error
);
3209 if (error
) FIXME( "ignoring error parameter\n" );
3211 if (!reader
) return E_INVALIDARG
;
3212 if (localname
|| ns
) FIXME( "name and/or namespace not verified\n" );
3214 EnterCriticalSection( &reader
->cs
);
3216 if (reader
->magic
!= READER_MAGIC
)
3218 LeaveCriticalSection( &reader
->cs
);
3219 return E_INVALIDARG
;
3222 hr
= read_to_startelement( reader
, found
);
3224 LeaveCriticalSection( &reader
->cs
);
3228 BOOL
move_to_root_element( struct node
*root
, struct node
**current
)
3233 if (!(ptr
= list_head( &root
->children
))) return FALSE
;
3234 node
= LIST_ENTRY( ptr
, struct node
, entry
);
3235 if (node_type( node
) == WS_XML_NODE_TYPE_ELEMENT
)
3240 while ((ptr
= list_next( &root
->children
, &node
->entry
)))
3242 struct node
*next
= LIST_ENTRY( ptr
, struct node
, entry
);
3243 if (node_type( next
) == WS_XML_NODE_TYPE_ELEMENT
)
3253 BOOL
move_to_next_element( struct node
**current
)
3256 struct node
*node
= *current
, *parent
= (*current
)->parent
;
3258 if (!parent
) return FALSE
;
3259 while ((ptr
= list_next( &parent
->children
, &node
->entry
)))
3261 struct node
*next
= LIST_ENTRY( ptr
, struct node
, entry
);
3262 if (node_type( next
) == WS_XML_NODE_TYPE_ELEMENT
)
3272 BOOL
move_to_prev_element( struct node
**current
)
3275 struct node
*node
= *current
, *parent
= (*current
)->parent
;
3277 if (!parent
) return FALSE
;
3278 while ((ptr
= list_prev( &parent
->children
, &node
->entry
)))
3280 struct node
*prev
= LIST_ENTRY( ptr
, struct node
, entry
);
3281 if (node_type( prev
) == WS_XML_NODE_TYPE_ELEMENT
)
3291 BOOL
move_to_child_element( struct node
**current
)
3294 struct node
*child
, *node
= *current
;
3296 if (!(ptr
= list_head( &node
->children
))) return FALSE
;
3297 child
= LIST_ENTRY( ptr
, struct node
, entry
);
3298 if (node_type( child
) == WS_XML_NODE_TYPE_ELEMENT
)
3303 while ((ptr
= list_next( &node
->children
, &child
->entry
)))
3305 struct node
*next
= LIST_ENTRY( ptr
, struct node
, entry
);
3306 if (node_type( next
) == WS_XML_NODE_TYPE_ELEMENT
)
3316 BOOL
move_to_end_element( struct node
**current
)
3319 struct node
*node
= *current
;
3321 if (node_type( node
) != WS_XML_NODE_TYPE_ELEMENT
) return FALSE
;
3323 if ((ptr
= list_tail( &node
->children
)))
3325 struct node
*tail
= LIST_ENTRY( ptr
, struct node
, entry
);
3326 if (node_type( tail
) == WS_XML_NODE_TYPE_END_ELEMENT
)
3335 BOOL
move_to_parent_element( struct node
**current
)
3337 struct node
*parent
= (*current
)->parent
;
3339 if (parent
&& (node_type( parent
) == WS_XML_NODE_TYPE_ELEMENT
||
3340 node_type( parent
) == WS_XML_NODE_TYPE_BOF
))
3348 BOOL
move_to_first_node( struct node
**current
)
3351 struct node
*node
= *current
;
3353 if ((ptr
= list_head( &node
->parent
->children
)))
3355 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3361 BOOL
move_to_next_node( struct node
**current
)
3364 struct node
*node
= *current
;
3366 if ((ptr
= list_next( &node
->parent
->children
, &node
->entry
)))
3368 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3374 BOOL
move_to_prev_node( struct node
**current
)
3377 struct node
*node
= *current
;
3379 if ((ptr
= list_prev( &node
->parent
->children
, &node
->entry
)))
3381 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3387 BOOL
move_to_bof( struct node
*root
, struct node
**current
)
3393 BOOL
move_to_eof( struct node
*root
, struct node
**current
)
3396 if ((ptr
= list_tail( &root
->children
)))
3398 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3404 BOOL
move_to_child_node( struct node
**current
)
3407 struct node
*node
= *current
;
3409 if ((ptr
= list_head( &node
->children
)))
3411 *current
= LIST_ENTRY( ptr
, struct node
, entry
);
3417 BOOL
move_to_parent_node( struct node
**current
)
3419 struct node
*parent
= (*current
)->parent
;
3420 if (!parent
) return FALSE
;
3425 static HRESULT
read_move_to( struct reader
*reader
, WS_MOVE_TO move
, BOOL
*found
)
3427 BOOL success
= FALSE
;
3430 if (!read_end_of_data( reader
))
3432 struct node
*saved_current
= reader
->current
;
3433 while (reader
->state
!= READER_STATE_EOF
&& (hr
= read_node( reader
)) == S_OK
) { /* nothing */ };
3434 if (hr
!= S_OK
) return hr
;
3435 reader
->current
= saved_current
;
3439 case WS_MOVE_TO_ROOT_ELEMENT
:
3440 success
= move_to_root_element( reader
->root
, &reader
->current
);
3443 case WS_MOVE_TO_NEXT_ELEMENT
:
3444 success
= move_to_next_element( &reader
->current
);
3447 case WS_MOVE_TO_PREVIOUS_ELEMENT
:
3448 success
= move_to_prev_element( &reader
->current
);
3451 case WS_MOVE_TO_CHILD_ELEMENT
:
3452 success
= move_to_child_element( &reader
->current
);
3455 case WS_MOVE_TO_END_ELEMENT
:
3456 success
= move_to_end_element( &reader
->current
);
3459 case WS_MOVE_TO_PARENT_ELEMENT
:
3460 success
= move_to_parent_element( &reader
->current
);
3463 case WS_MOVE_TO_FIRST_NODE
:
3464 success
= move_to_first_node( &reader
->current
);
3467 case WS_MOVE_TO_NEXT_NODE
:
3468 success
= move_to_next_node( &reader
->current
);
3471 case WS_MOVE_TO_PREVIOUS_NODE
:
3472 success
= move_to_prev_node( &reader
->current
);
3475 case WS_MOVE_TO_CHILD_NODE
:
3476 success
= move_to_child_node( &reader
->current
);
3479 case WS_MOVE_TO_BOF
:
3480 success
= move_to_bof( reader
->root
, &reader
->current
);
3483 case WS_MOVE_TO_EOF
:
3484 success
= move_to_eof( reader
->root
, &reader
->current
);
3488 FIXME( "unhandled move %u\n", move
);
3497 return success
? S_OK
: WS_E_INVALID_FORMAT
;
3500 /**************************************************************************
3501 * WsMoveReader [webservices.@]
3503 HRESULT WINAPI
WsMoveReader( WS_XML_READER
*handle
, WS_MOVE_TO move
, BOOL
*found
, WS_ERROR
*error
)
3505 struct reader
*reader
= (struct reader
*)handle
;
3508 TRACE( "%p %u %p %p\n", handle
, move
, found
, error
);
3509 if (error
) FIXME( "ignoring error parameter\n" );
3511 if (!reader
) return E_INVALIDARG
;
3513 EnterCriticalSection( &reader
->cs
);
3515 if (reader
->magic
!= READER_MAGIC
)
3517 LeaveCriticalSection( &reader
->cs
);
3518 return E_INVALIDARG
;
3521 if (!reader
->input_type
)
3523 LeaveCriticalSection( &reader
->cs
);
3524 return WS_E_INVALID_OPERATION
;
3527 hr
= read_move_to( reader
, move
, found
);
3529 LeaveCriticalSection( &reader
->cs
);
3533 /**************************************************************************
3534 * WsReadStartAttribute [webservices.@]
3536 HRESULT WINAPI
WsReadStartAttribute( WS_XML_READER
*handle
, ULONG index
, WS_ERROR
*error
)
3538 struct reader
*reader
= (struct reader
*)handle
;
3539 const WS_XML_ELEMENT_NODE
*elem
;
3541 TRACE( "%p %u %p\n", handle
, index
, error
);
3542 if (error
) FIXME( "ignoring error parameter\n" );
3544 if (!reader
) return E_INVALIDARG
;
3546 EnterCriticalSection( &reader
->cs
);
3548 if (reader
->magic
!= READER_MAGIC
)
3550 LeaveCriticalSection( &reader
->cs
);
3551 return E_INVALIDARG
;
3554 elem
= &reader
->current
->hdr
;
3555 if (reader
->state
!= READER_STATE_STARTELEMENT
|| index
>= elem
->attributeCount
)
3557 LeaveCriticalSection( &reader
->cs
);
3558 return WS_E_INVALID_FORMAT
;
3561 reader
->current_attr
= index
;
3562 reader
->state
= READER_STATE_STARTATTRIBUTE
;
3564 LeaveCriticalSection( &reader
->cs
);
3568 /**************************************************************************
3569 * WsReadEndAttribute [webservices.@]
3571 HRESULT WINAPI
WsReadEndAttribute( WS_XML_READER
*handle
, WS_ERROR
*error
)
3573 struct reader
*reader
= (struct reader
*)handle
;
3575 TRACE( "%p %p\n", handle
, error
);
3576 if (error
) FIXME( "ignoring error parameter\n" );
3578 if (!reader
) return E_INVALIDARG
;
3580 EnterCriticalSection( &reader
->cs
);
3582 if (reader
->magic
!= READER_MAGIC
)
3584 LeaveCriticalSection( &reader
->cs
);
3585 return E_INVALIDARG
;
3588 if (reader
->state
!= READER_STATE_STARTATTRIBUTE
)
3590 LeaveCriticalSection( &reader
->cs
);
3591 return WS_E_INVALID_FORMAT
;
3594 reader
->state
= READER_STATE_STARTELEMENT
;
3596 LeaveCriticalSection( &reader
->cs
);
3600 static HRESULT
str_to_bool( const unsigned char *str
, ULONG len
, BOOL
*ret
)
3602 if (len
== 4 && !memcmp( str
, "true", 4 )) *ret
= TRUE
;
3603 else if (len
== 1 && !memcmp( str
, "1", 1 )) *ret
= TRUE
;
3604 else if (len
== 5 && !memcmp( str
, "false", 5 )) *ret
= FALSE
;
3605 else if (len
== 1 && !memcmp( str
, "0", 1 )) *ret
= FALSE
;
3606 else return WS_E_INVALID_FORMAT
;
3610 static HRESULT
str_to_int64( const unsigned char *str
, ULONG len
, INT64 min
, INT64 max
, INT64
*ret
)
3612 BOOL negative
= FALSE
;
3613 const unsigned char *ptr
= str
;
3616 while (len
&& read_isspace( *ptr
)) { ptr
++; len
--; }
3617 while (len
&& read_isspace( ptr
[len
- 1] )) { len
--; }
3618 if (!len
) return WS_E_INVALID_FORMAT
;
3626 if (!len
) return WS_E_INVALID_FORMAT
;
3632 if (!isdigit( *ptr
)) return WS_E_INVALID_FORMAT
;
3634 if (negative
) val
= -val
;
3636 if ((!negative
&& (*ret
> max
/ 10 || *ret
* 10 > max
- val
)) ||
3637 (negative
&& (*ret
< min
/ 10 || *ret
* 10 < min
- val
)))
3639 return WS_E_NUMERIC_OVERFLOW
;
3641 *ret
= *ret
* 10 + val
;
3648 static HRESULT
str_to_uint64( const unsigned char *str
, ULONG len
, UINT64 max
, UINT64
*ret
)
3650 const unsigned char *ptr
= str
;
3653 while (len
&& read_isspace( *ptr
)) { ptr
++; len
--; }
3654 while (len
&& read_isspace( ptr
[len
- 1] )) { len
--; }
3655 if (!len
) return WS_E_INVALID_FORMAT
;
3661 if (!isdigit( *ptr
)) return WS_E_INVALID_FORMAT
;
3664 if ((*ret
> max
/ 10 || *ret
* 10 > max
- val
)) return WS_E_NUMERIC_OVERFLOW
;
3665 *ret
= *ret
* 10 + val
;
3672 BOOL
set_fpword( unsigned short new, unsigned short *old
)
3674 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3675 unsigned short fpword
;
3677 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
3680 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
3683 FIXME( "not implemented\n" );
3688 void restore_fpword( unsigned short fpword
)
3690 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3691 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
3693 FIXME( "not implemented\n" );
3697 static HRESULT
str_to_double( const unsigned char *str
, ULONG len
, double *ret
)
3699 static const unsigned __int64 nan
= 0xfff8000000000000;
3700 static const unsigned __int64 inf
= 0x7ff0000000000000;
3701 static const unsigned __int64 inf_min
= 0xfff0000000000000;
3702 HRESULT hr
= WS_E_INVALID_FORMAT
;
3703 const unsigned char *p
= str
, *q
;
3704 int sign
= 1, exp_sign
= 1, exp
= 0, exp_tmp
= 0, neg_exp
, i
, nb_digits
, have_digits
;
3705 unsigned __int64 val
= 0, tmp
;
3706 long double exp_val
= 1.0, exp_mul
= 10.0;
3707 unsigned short fpword
;
3709 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3710 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3711 if (!len
) return WS_E_INVALID_FORMAT
;
3713 if (len
== 3 && !memcmp( p
, "NaN", 3 ))
3715 *(unsigned __int64
*)ret
= nan
;
3718 if (len
== 3 && !memcmp( p
, "INF", 3 ))
3720 *(unsigned __int64
*)ret
= inf
;
3723 if (len
== 4 && !memcmp( p
, "-INF", 4 ))
3725 *(unsigned __int64
*)ret
= inf_min
;
3735 else if (*p
== '+') { p
++; len
--; };
3736 if (!len
) return S_OK
;
3738 if (!set_fpword( 0x37f, &fpword
)) return E_NOTIMPL
;
3741 while (len
&& isdigit( *q
)) { q
++; len
--; }
3742 have_digits
= nb_digits
= q
- p
;
3743 for (i
= 0; i
< nb_digits
; i
++)
3745 tmp
= val
* 10 + p
[i
] - '0';
3746 if (val
> MAX_UINT64
/ 10 || tmp
< val
)
3748 for (; i
< nb_digits
; i
++) exp
++;
3759 while (len
&& isdigit( *q
)) { q
++; len
--; };
3760 have_digits
|= nb_digits
= q
- p
;
3761 for (i
= 0; i
< nb_digits
; i
++)
3763 tmp
= val
* 10 + p
[i
] - '0';
3764 if (val
> MAX_UINT64
/ 10 || tmp
< val
) break;
3769 if (len
> 1 && tolower(*q
) == 'e')
3771 if (!have_digits
) goto done
;
3778 else if (*p
== '+') { p
++; len
--; };
3781 while (len
&& isdigit( *q
)) { q
++; len
--; };
3783 if (!nb_digits
|| len
) goto done
;
3784 for (i
= 0; i
< nb_digits
; i
++)
3786 if (exp_tmp
> MAX_INT32
/ 10 || (exp_tmp
= exp_tmp
* 10 + p
[i
] - '0') < 0)
3787 exp_tmp
= MAX_INT32
;
3789 exp_tmp
*= exp_sign
;
3791 if (exp
< 0 && exp_tmp
< 0 && exp
+ exp_tmp
>= 0) exp
= MIN_INT32
;
3792 else if (exp
> 0 && exp_tmp
> 0 && exp
+ exp_tmp
< 0) exp
= MAX_INT32
;
3793 else exp
+= exp_tmp
;
3796 if (!have_digits
|| len
) goto done
;
3798 if ((neg_exp
= exp
< 0)) exp
= -exp
;
3799 for (; exp
; exp
>>= 1)
3801 if (exp
& 1) exp_val
*= exp_mul
;
3805 *ret
= sign
* (neg_exp
? val
/ exp_val
: val
* exp_val
);
3809 restore_fpword( fpword
);
3813 static HRESULT
str_to_float( const unsigned char *str
, ULONG len
, float *ret
)
3815 static const unsigned int inf
= 0x7f800000;
3816 static const unsigned int inf_min
= 0xff800000;
3817 const unsigned char *p
= str
;
3821 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3822 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3823 if (!len
) return WS_E_INVALID_FORMAT
;
3825 if (len
== 3 && !memcmp( p
, "INF", 3 ))
3827 *(unsigned int *)ret
= inf
;
3830 if (len
== 4 && !memcmp( p
, "-INF", 4 ))
3832 *(unsigned int *)ret
= inf_min
;
3836 if ((hr
= str_to_double( p
, len
, &val
)) != S_OK
) return hr
;
3841 static HRESULT
str_to_guid( const unsigned char *str
, ULONG len
, GUID
*ret
)
3843 static const unsigned char hex
[] =
3845 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
3846 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
3847 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
3848 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
3849 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
3850 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
3851 0,10,11,12,13,14,15 /* 0x60 */
3853 const unsigned char *p
= str
;
3856 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3857 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3858 if (len
!= 36) return WS_E_INVALID_FORMAT
;
3860 if (p
[8] != '-' || p
[13] != '-' || p
[18] != '-' || p
[23] != '-')
3861 return WS_E_INVALID_FORMAT
;
3863 for (i
= 0; i
< 36; i
++)
3865 if (i
== 8 || i
== 13 || i
== 18 || i
== 23) continue;
3866 if (p
[i
] > 'f' || (!hex
[p
[i
]] && p
[i
] != '0')) return WS_E_INVALID_FORMAT
;
3869 ret
->Data1
= hex
[p
[0]] << 28 | hex
[p
[1]] << 24 | hex
[p
[2]] << 20 | hex
[p
[3]] << 16 |
3870 hex
[p
[4]] << 12 | hex
[p
[5]] << 8 | hex
[p
[6]] << 4 | hex
[p
[7]];
3872 ret
->Data2
= hex
[p
[9]] << 12 | hex
[p
[10]] << 8 | hex
[p
[11]] << 4 | hex
[p
[12]];
3873 ret
->Data3
= hex
[p
[14]] << 12 | hex
[p
[15]] << 8 | hex
[p
[16]] << 4 | hex
[p
[17]];
3875 ret
->Data4
[0] = hex
[p
[19]] << 4 | hex
[p
[20]];
3876 ret
->Data4
[1] = hex
[p
[21]] << 4 | hex
[p
[22]];
3877 ret
->Data4
[2] = hex
[p
[24]] << 4 | hex
[p
[25]];
3878 ret
->Data4
[3] = hex
[p
[26]] << 4 | hex
[p
[27]];
3879 ret
->Data4
[4] = hex
[p
[28]] << 4 | hex
[p
[29]];
3880 ret
->Data4
[5] = hex
[p
[30]] << 4 | hex
[p
[31]];
3881 ret
->Data4
[6] = hex
[p
[32]] << 4 | hex
[p
[33]];
3882 ret
->Data4
[7] = hex
[p
[34]] << 4 | hex
[p
[35]];
3887 static HRESULT
str_to_string( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_STRING
*ret
)
3889 int len_utf16
= MultiByteToWideChar( CP_UTF8
, 0, (const char *)str
, len
, NULL
, 0 );
3890 if (!(ret
->chars
= ws_alloc( heap
, len_utf16
* sizeof(WCHAR
) ))) return WS_E_QUOTA_EXCEEDED
;
3891 MultiByteToWideChar( CP_UTF8
, 0, (const char *)str
, len
, ret
->chars
, len_utf16
);
3892 ret
->length
= len_utf16
;
3896 static HRESULT
str_to_unique_id( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_UNIQUE_ID
*ret
)
3898 if (len
== 45 && !memcmp( str
, "urn:uuid:", 9 ))
3900 ret
->uri
.length
= 0;
3901 ret
->uri
.chars
= NULL
;
3902 return str_to_guid( str
+ 9, len
- 9, &ret
->guid
);
3905 memset( &ret
->guid
, 0, sizeof(ret
->guid
) );
3906 return str_to_string( str
, len
, heap
, &ret
->uri
);
3909 static inline unsigned char decode_char( unsigned char c
)
3911 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
3912 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
3913 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
3914 if (c
== '+') return 62;
3915 if (c
== '/') return 63;
3919 static ULONG
decode_base64( const unsigned char *base64
, ULONG len
, unsigned char *buf
)
3922 unsigned char c0
, c1
, c2
, c3
;
3923 const unsigned char *p
= base64
;
3927 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3928 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3929 if ((c2
= decode_char( p
[2] )) > 63) return 0;
3930 if ((c3
= decode_char( p
[3] )) > 63) return 0;
3931 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
3932 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
3933 buf
[i
+ 2] = (c2
<< 6) | c3
;
3940 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3941 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3942 buf
[i
] = (c0
<< 2) | (c1
>> 4);
3945 else if (p
[3] == '=')
3947 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3948 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3949 if ((c2
= decode_char( p
[2] )) > 63) return 0;
3950 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
3951 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
3956 if ((c0
= decode_char( p
[0] )) > 63) return 0;
3957 if ((c1
= decode_char( p
[1] )) > 63) return 0;
3958 if ((c2
= decode_char( p
[2] )) > 63) return 0;
3959 if ((c3
= decode_char( p
[3] )) > 63) return 0;
3960 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
3961 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
3962 buf
[i
+ 2] = (c2
<< 6) | c3
;
3968 static HRESULT
str_to_bytes( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_BYTES
*ret
)
3970 const unsigned char *p
= str
;
3972 while (len
&& read_isspace( *p
)) { p
++; len
--; }
3973 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
3975 if (len
% 4) return WS_E_INVALID_FORMAT
;
3976 if (!(ret
->bytes
= ws_alloc( heap
, len
* 3 / 4 ))) return WS_E_QUOTA_EXCEEDED
;
3977 ret
->length
= decode_base64( p
, len
, ret
->bytes
);
3981 static HRESULT
str_to_xml_string( const unsigned char *str
, ULONG len
, WS_HEAP
*heap
, WS_XML_STRING
*ret
)
3983 if (!(ret
->bytes
= ws_alloc( heap
, len
))) return WS_E_QUOTA_EXCEEDED
;
3984 memcpy( ret
->bytes
, str
, len
);
3986 ret
->dictionary
= NULL
;
3991 static HRESULT
copy_xml_string( WS_HEAP
*heap
, const WS_XML_STRING
*src
, WS_XML_STRING
*dst
)
3993 if (!(dst
->bytes
= ws_alloc( heap
, src
->length
))) return WS_E_QUOTA_EXCEEDED
;
3994 memcpy( dst
->bytes
, src
->bytes
, src
->length
);
3995 dst
->length
= src
->length
;
3999 static HRESULT
str_to_qname( struct reader
*reader
, const unsigned char *str
, ULONG len
, WS_HEAP
*heap
,
4000 WS_XML_STRING
*prefix_ret
, WS_XML_STRING
*localname_ret
, WS_XML_STRING
*ns_ret
)
4002 const unsigned char *p
= str
;
4003 WS_XML_STRING prefix
, localname
;
4004 const WS_XML_STRING
*ns
;
4007 while (len
&& read_isspace( *p
)) { p
++; len
--; }
4008 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
4010 if ((hr
= split_qname( p
, len
, &prefix
, &localname
)) != S_OK
) return hr
;
4011 if (!(ns
= get_namespace( reader
, &prefix
))) return WS_E_INVALID_FORMAT
;
4013 if (prefix_ret
&& (hr
= copy_xml_string( heap
, &prefix
, prefix_ret
)) != S_OK
) return hr
;
4014 if ((hr
= copy_xml_string( heap
, &localname
, localname_ret
)) != S_OK
)
4016 ws_free( heap
, prefix_ret
->bytes
, prefix_ret
->length
);
4019 if ((hr
= copy_xml_string( heap
, ns
, ns_ret
)) != S_OK
)
4021 ws_free( heap
, prefix_ret
->bytes
, prefix_ret
->length
);
4022 ws_free( heap
, localname_ret
->bytes
, localname_ret
->length
);
4028 static HRESULT
read_qualified_name( struct reader
*reader
, WS_HEAP
*heap
, WS_XML_STRING
*prefix
,
4029 WS_XML_STRING
*localname
, WS_XML_STRING
*ns
)
4031 const WS_XML_TEXT_NODE
*node
= (const WS_XML_TEXT_NODE
*)reader
->current
;
4032 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)node
->text
;
4033 return str_to_qname( reader
, utf8
->value
.bytes
, utf8
->value
.length
, heap
, prefix
, localname
, ns
);
4036 /**************************************************************************
4037 * WsReadQualifiedName [webservices.@]
4039 HRESULT WINAPI
WsReadQualifiedName( WS_XML_READER
*handle
, WS_HEAP
*heap
, WS_XML_STRING
*prefix
,
4040 WS_XML_STRING
*localname
, WS_XML_STRING
*ns
,
4043 struct reader
*reader
= (struct reader
*)handle
;
4046 TRACE( "%p %p %p %p %p %p\n", handle
, heap
, prefix
, localname
, ns
, error
);
4047 if (error
) FIXME( "ignoring error parameter\n" );
4049 if (!reader
|| !heap
) return E_INVALIDARG
;
4051 EnterCriticalSection( &reader
->cs
);
4053 if (reader
->magic
!= READER_MAGIC
)
4055 LeaveCriticalSection( &reader
->cs
);
4056 return E_INVALIDARG
;
4059 if (!reader
->input_type
)
4061 LeaveCriticalSection( &reader
->cs
);
4062 return WS_E_INVALID_OPERATION
;
4067 LeaveCriticalSection( &reader
->cs
);
4068 return E_INVALIDARG
;
4071 if (reader
->state
!= READER_STATE_TEXT
)
4073 LeaveCriticalSection( &reader
->cs
);
4074 return WS_E_INVALID_FORMAT
;
4077 hr
= read_qualified_name( reader
, heap
, prefix
, localname
, ns
);
4079 LeaveCriticalSection( &reader
->cs
);
4083 static const int month_offsets
[2][12] =
4085 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
4086 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
4089 static inline int valid_day( int year
, int month
, int day
)
4091 return day
> 0 && day
<= month_days
[leap_year( year
)][month
- 1];
4094 static inline int leap_days_before( int year
)
4096 return (year
- 1) / 4 - (year
- 1) / 100 + (year
- 1) / 400;
4099 static HRESULT
str_to_datetime( const unsigned char *bytes
, ULONG len
, WS_DATETIME
*ret
)
4101 const unsigned char *p
= bytes
, *q
;
4102 int year
, month
, day
, hour
, min
, sec
, sec_frac
= 0, tz_hour
, tz_min
, tz_neg
;
4104 while (len
&& read_isspace( *p
)) { p
++; len
--; }
4105 while (len
&& read_isspace( p
[len
- 1] )) { len
--; }
4108 while (len
&& isdigit( *q
)) { q
++; len
--; };
4109 if (q
- p
!= 4 || !len
|| *q
!= '-') return WS_E_INVALID_FORMAT
;
4110 year
= (p
[0] - '0') * 1000 + (p
[1] - '0') * 100 + (p
[2] - '0') * 10 + p
[3] - '0';
4111 if (year
< 1) return WS_E_INVALID_FORMAT
;
4114 while (len
&& isdigit( *q
)) { q
++; len
--; };
4115 if (q
- p
!= 2 || !len
|| *q
!= '-') return WS_E_INVALID_FORMAT
;
4116 month
= (p
[0] - '0') * 10 + p
[1] - '0';
4117 if (month
< 1 || month
> 12) return WS_E_INVALID_FORMAT
;
4120 while (len
&& isdigit( *q
)) { q
++; len
--; };
4121 if (q
- p
!= 2 || !len
|| *q
!= 'T') return WS_E_INVALID_FORMAT
;
4122 day
= (p
[0] - '0') * 10 + p
[1] - '0';
4123 if (!valid_day( year
, month
, day
)) return WS_E_INVALID_FORMAT
;
4126 while (len
&& isdigit( *q
)) { q
++; len
--; };
4127 if (q
- p
!= 2 || !len
|| *q
!= ':') return WS_E_INVALID_FORMAT
;
4128 hour
= (p
[0] - '0') * 10 + p
[1] - '0';
4129 if (hour
> 24) return WS_E_INVALID_FORMAT
;
4132 while (len
&& isdigit( *q
)) { q
++; len
--; };
4133 if (q
- p
!= 2 || !len
|| *q
!= ':') return WS_E_INVALID_FORMAT
;
4134 min
= (p
[0] - '0') * 10 + p
[1] - '0';
4135 if (min
> 59 || (min
> 0 && hour
== 24)) return WS_E_INVALID_FORMAT
;
4138 while (len
&& isdigit( *q
)) { q
++; len
--; };
4139 if (q
- p
!= 2 || !len
) return WS_E_INVALID_FORMAT
;
4140 sec
= (p
[0] - '0') * 10 + p
[1] - '0';
4141 if (sec
> 59 || (sec
> 0 && hour
== 24)) return WS_E_INVALID_FORMAT
;
4145 unsigned int i
, nb_digits
, mul
= TICKS_PER_SEC
/ 10;
4147 while (len
&& isdigit( *q
)) { q
++; len
--; };
4149 if (nb_digits
< 1 || nb_digits
> 7) return WS_E_INVALID_FORMAT
;
4150 for (i
= 0; i
< nb_digits
; i
++)
4152 sec_frac
+= (p
[i
] - '0') * mul
;
4158 if (--len
) return WS_E_INVALID_FORMAT
;
4159 tz_hour
= tz_min
= tz_neg
= 0;
4160 ret
->format
= WS_DATETIME_FORMAT_UTC
;
4162 else if (*q
== '+' || *q
== '-')
4164 tz_neg
= (*q
== '-') ? 1 : 0;
4167 while (len
&& isdigit( *q
)) { q
++; len
--; };
4168 if (q
- p
!= 2 || !len
|| *q
!= ':') return WS_E_INVALID_FORMAT
;
4169 tz_hour
= (p
[0] - '0') * 10 + p
[1] - '0';
4170 if (tz_hour
> 14) return WS_E_INVALID_FORMAT
;
4173 while (len
&& isdigit( *q
)) { q
++; len
--; };
4174 if (q
- p
!= 2 || len
) return WS_E_INVALID_FORMAT
;
4175 tz_min
= (p
[0] - '0') * 10 + p
[1] - '0';
4176 if (tz_min
> 59 || (tz_min
> 0 && tz_hour
== 14)) return WS_E_INVALID_FORMAT
;
4178 ret
->format
= WS_DATETIME_FORMAT_LOCAL
;
4180 else return WS_E_INVALID_FORMAT
;
4182 ret
->ticks
= ((year
- 1) * 365 + leap_days_before( year
)) * TICKS_PER_DAY
;
4183 ret
->ticks
+= month_offsets
[leap_year( year
)][month
- 1] * TICKS_PER_DAY
;
4184 ret
->ticks
+= (day
- 1) * TICKS_PER_DAY
;
4185 ret
->ticks
+= hour
* TICKS_PER_HOUR
;
4186 ret
->ticks
+= min
* TICKS_PER_MIN
;
4187 ret
->ticks
+= sec
* TICKS_PER_SEC
;
4188 ret
->ticks
+= sec_frac
;
4192 if (tz_hour
* TICKS_PER_HOUR
+ tz_min
* TICKS_PER_MIN
+ ret
->ticks
> TICKS_MAX
)
4193 return WS_E_INVALID_FORMAT
;
4194 ret
->ticks
+= tz_hour
* TICKS_PER_HOUR
;
4195 ret
->ticks
+= tz_min
* TICKS_PER_MIN
;
4199 if (tz_hour
* TICKS_PER_HOUR
+ tz_min
* TICKS_PER_MIN
> ret
->ticks
)
4200 return WS_E_INVALID_FORMAT
;
4201 ret
->ticks
-= tz_hour
* TICKS_PER_HOUR
;
4202 ret
->ticks
-= tz_min
* TICKS_PER_MIN
;
4208 /**************************************************************************
4209 * WsDateTimeToFileTime [webservices.@]
4211 HRESULT WINAPI
WsDateTimeToFileTime( const WS_DATETIME
*dt
, FILETIME
*ft
, WS_ERROR
*error
)
4213 unsigned __int64 ticks
;
4215 TRACE( "%p %p %p\n", dt
, ft
, error
);
4216 if (error
) FIXME( "ignoring error parameter\n" );
4218 if (!dt
|| !ft
) return E_INVALIDARG
;
4220 if (dt
->ticks
< TICKS_1601_01_01
) return WS_E_INVALID_FORMAT
;
4221 ticks
= dt
->ticks
- TICKS_1601_01_01
;
4222 ft
->dwHighDateTime
= ticks
>> 32;
4223 ft
->dwLowDateTime
= (DWORD
)ticks
;
4227 /**************************************************************************
4228 * WsFileTimeToDateTime [webservices.@]
4230 HRESULT WINAPI
WsFileTimeToDateTime( const FILETIME
*ft
, WS_DATETIME
*dt
, WS_ERROR
*error
)
4232 unsigned __int64 ticks
;
4234 TRACE( "%p %p %p\n", ft
, dt
, error
);
4235 if (error
) FIXME( "ignoring error parameter\n" );
4237 if (!dt
|| !ft
) return E_INVALIDARG
;
4239 ticks
= ((unsigned __int64
)ft
->dwHighDateTime
<< 32) | ft
->dwLowDateTime
;
4240 if (ticks
> MAX_UINT64
- TICKS_1601_01_01
) return WS_E_NUMERIC_OVERFLOW
;
4241 if (ticks
+ TICKS_1601_01_01
> TICKS_MAX
) return WS_E_INVALID_FORMAT
;
4242 dt
->ticks
= ticks
+ TICKS_1601_01_01
;
4243 dt
->format
= WS_DATETIME_FORMAT_UTC
;
4247 static BOOL
find_attribute( struct reader
*reader
, const WS_XML_STRING
*localname
,
4248 const WS_XML_STRING
*ns
, ULONG
*index
)
4251 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
4255 *index
= reader
->current_attr
;
4258 for (i
= 0; i
< elem
->attributeCount
; i
++)
4260 const WS_XML_STRING
*localname2
= elem
->attributes
[i
]->localName
;
4261 const WS_XML_STRING
*ns2
= elem
->attributes
[i
]->ns
;
4263 if (!cmp_name( localname
->bytes
, localname
->length
, localname2
->bytes
, localname2
->length
) &&
4264 !cmp_name( ns
->bytes
, ns
->length
, ns2
->bytes
, ns2
->length
))
4273 /**************************************************************************
4274 * WsFindAttribute [webservices.@]
4276 HRESULT WINAPI
WsFindAttribute( WS_XML_READER
*handle
, const WS_XML_STRING
*localname
,
4277 const WS_XML_STRING
*ns
, BOOL required
, ULONG
*index
,
4280 struct reader
*reader
= (struct reader
*)handle
;
4283 TRACE( "%p %s %s %d %p %p\n", handle
, debugstr_xmlstr(localname
), debugstr_xmlstr(ns
),
4284 required
, index
, error
);
4285 if (error
) FIXME( "ignoring error parameter\n" );
4287 if (!reader
|| !localname
|| !ns
|| !index
) return E_INVALIDARG
;
4289 EnterCriticalSection( &reader
->cs
);
4291 if (reader
->magic
!= READER_MAGIC
)
4293 LeaveCriticalSection( &reader
->cs
);
4294 return E_INVALIDARG
;
4297 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
)
4299 LeaveCriticalSection( &reader
->cs
);
4300 return WS_E_INVALID_OPERATION
;
4303 if (!find_attribute( reader
, localname
, ns
, index
))
4305 if (required
) hr
= WS_E_INVALID_FORMAT
;
4313 LeaveCriticalSection( &reader
->cs
);
4317 static HRESULT
get_node_text( struct reader
*reader
, const WS_XML_TEXT
**ret
)
4319 WS_XML_TEXT_NODE
*node
= (WS_XML_TEXT_NODE
*)&reader
->current
->hdr
.node
;
4324 static HRESULT
get_attribute_text( struct reader
*reader
, ULONG index
, const WS_XML_TEXT
**ret
)
4326 WS_XML_ELEMENT_NODE
*elem
= &reader
->current
->hdr
;
4327 *ret
= elem
->attributes
[index
]->value
;
4331 static BOOL
match_element( const struct node
*node
, const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
)
4333 const WS_XML_ELEMENT_NODE
*elem
= (const WS_XML_ELEMENT_NODE
*)node
;
4334 if (node_type( node
) != WS_XML_NODE_TYPE_ELEMENT
) return FALSE
;
4335 return WsXmlStringEquals( localname
, elem
->localName
, NULL
) == S_OK
&&
4336 WsXmlStringEquals( ns
, elem
->ns
, NULL
) == S_OK
;
4339 static HRESULT
read_next_node( struct reader
*reader
)
4341 if (reader
->current
== reader
->last
) return read_node( reader
);
4342 if (move_to_child_node( &reader
->current
)) return S_OK
;
4343 if (move_to_next_node( &reader
->current
)) return S_OK
;
4344 if (!move_to_parent_node( &reader
->current
)) return WS_E_INVALID_FORMAT
;
4345 if (move_to_next_node( &reader
->current
)) return S_OK
;
4346 return WS_E_INVALID_FORMAT
;
4355 static void save_reader_position( const struct reader
*reader
, struct reader_pos
*pos
)
4357 pos
->node
= reader
->current
;
4358 pos
->attr
= reader
->current_attr
;
4361 static void restore_reader_position( struct reader
*reader
, const struct reader_pos
*pos
)
4363 reader
->current
= pos
->node
;
4364 reader
->current_attr
= pos
->attr
;
4367 static HRESULT
get_text( struct reader
*reader
, WS_TYPE_MAPPING mapping
, const WS_XML_STRING
*localname
,
4368 const WS_XML_STRING
*ns
, const WS_XML_TEXT
**ret
, BOOL
*found
)
4372 case WS_ATTRIBUTE_TYPE_MAPPING
:
4375 if (!(*found
= find_attribute( reader
, localname
, ns
, &index
))) return S_OK
;
4376 return get_attribute_text( reader
, index
, ret
);
4378 case WS_ELEMENT_TYPE_MAPPING
:
4379 case WS_ELEMENT_CONTENT_TYPE_MAPPING
:
4380 case WS_ANY_ELEMENT_TYPE_MAPPING
:
4385 struct reader_pos pos
;
4388 if (!match_element( reader
->current
, localname
, ns
))
4393 save_reader_position( reader
, &pos
);
4394 if ((hr
= read_next_node( reader
)) != S_OK
) return hr
;
4395 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_TEXT
)
4397 restore_reader_position( reader
, &pos
);
4402 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_TEXT
)
4407 return get_node_text( reader
, ret
);
4410 FIXME( "mapping %u not supported\n", mapping
);
4415 static HRESULT
text_to_bool( const WS_XML_TEXT
*text
, BOOL
*val
)
4419 switch (text
->textType
)
4421 case WS_XML_TEXT_TYPE_UTF8
:
4423 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4424 hr
= str_to_bool( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
4427 case WS_XML_TEXT_TYPE_BOOL
:
4429 const WS_XML_BOOL_TEXT
*text_bool
= (const WS_XML_BOOL_TEXT
*)text
;
4430 *val
= text_bool
->value
;
4435 FIXME( "unhandled text type %u\n", text
->textType
);
4442 static HRESULT
read_type_bool( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4443 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4444 const WS_BOOL_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4445 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4447 const WS_XML_TEXT
*text
;
4453 FIXME( "description not supported\n" );
4456 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4457 if (*found
&& (hr
= text_to_bool( text
, &val
)) != S_OK
) return hr
;
4461 case WS_READ_REQUIRED_VALUE
:
4462 if (!*found
) return WS_E_INVALID_FORMAT
;
4465 case WS_READ_NILLABLE_VALUE
:
4466 if (size
!= sizeof(val
)) return E_INVALIDARG
;
4470 case WS_READ_REQUIRED_POINTER
:
4471 if (!*found
) return WS_E_INVALID_FORMAT
;
4474 case WS_READ_OPTIONAL_POINTER
:
4475 case WS_READ_NILLABLE_POINTER
:
4477 BOOL
*heap_val
= NULL
;
4478 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4481 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4484 *(BOOL
**)ret
= heap_val
;
4488 FIXME( "read option %u not supported\n", option
);
4495 static HRESULT
text_to_int8( const WS_XML_TEXT
*text
, INT64
*val
)
4499 switch (text
->textType
)
4501 case WS_XML_TEXT_TYPE_UTF8
:
4503 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4504 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT8
, MAX_INT8
, val
);
4507 case WS_XML_TEXT_TYPE_INT32
:
4509 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4510 assert( text_int32
->value
>= MIN_INT8
);
4511 assert( text_int32
->value
<= MAX_INT8
);
4512 *val
= text_int32
->value
;
4517 FIXME( "unhandled text type %u\n", text
->textType
);
4524 static HRESULT
read_type_int8( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4525 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4526 const WS_INT8_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4527 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4529 const WS_XML_TEXT
*text
;
4535 FIXME( "description not supported\n" );
4538 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4539 if (*found
&& (hr
= text_to_int8( text
, &val
)) != S_OK
) return hr
;
4543 case WS_READ_REQUIRED_VALUE
:
4544 if (!*found
) return WS_E_INVALID_FORMAT
;
4547 case WS_READ_NILLABLE_VALUE
:
4548 if (size
!= sizeof(INT8
)) return E_INVALIDARG
;
4552 case WS_READ_REQUIRED_POINTER
:
4553 if (!*found
) return WS_E_INVALID_FORMAT
;
4556 case WS_READ_OPTIONAL_POINTER
:
4557 case WS_READ_NILLABLE_POINTER
:
4559 INT8
*heap_val
= NULL
;
4560 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4563 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4566 *(INT8
**)ret
= heap_val
;
4570 FIXME( "read option %u not supported\n", option
);
4577 static HRESULT
text_to_int16( const WS_XML_TEXT
*text
, INT64
*val
)
4581 switch (text
->textType
)
4583 case WS_XML_TEXT_TYPE_UTF8
:
4585 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4586 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT16
, MAX_INT16
, val
);
4589 case WS_XML_TEXT_TYPE_INT32
:
4591 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4592 assert( text_int32
->value
>= MIN_INT16
);
4593 assert( text_int32
->value
<= MAX_INT16
);
4594 *val
= text_int32
->value
;
4599 FIXME( "unhandled text type %u\n", text
->textType
);
4606 static HRESULT
read_type_int16( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4607 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4608 const WS_INT16_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4609 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4611 const WS_XML_TEXT
*text
;
4617 FIXME( "description not supported\n" );
4620 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4621 if (*found
&& (hr
= text_to_int16( text
, &val
)) != S_OK
) return hr
;
4625 case WS_READ_REQUIRED_VALUE
:
4626 if (!*found
) return WS_E_INVALID_FORMAT
;
4629 case WS_READ_NILLABLE_VALUE
:
4630 if (size
!= sizeof(INT16
)) return E_INVALIDARG
;
4631 *(INT16
*)ret
= val
;
4634 case WS_READ_REQUIRED_POINTER
:
4635 if (!*found
) return WS_E_INVALID_FORMAT
;
4638 case WS_READ_OPTIONAL_POINTER
:
4639 case WS_READ_NILLABLE_POINTER
:
4641 INT16
*heap_val
= NULL
;
4642 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4645 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4648 *(INT16
**)ret
= heap_val
;
4652 FIXME( "read option %u not supported\n", option
);
4659 static HRESULT
text_to_int32( const WS_XML_TEXT
*text
, INT64
*val
)
4663 switch (text
->textType
)
4665 case WS_XML_TEXT_TYPE_UTF8
:
4667 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4668 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT32
, MAX_INT32
, val
);
4671 case WS_XML_TEXT_TYPE_INT32
:
4673 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4674 *val
= text_int32
->value
;
4679 FIXME( "unhandled text type %u\n", text
->textType
);
4686 static HRESULT
read_type_int32( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4687 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4688 const WS_INT32_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4689 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4691 const WS_XML_TEXT
*text
;
4697 FIXME( "description not supported\n" );
4700 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4701 if (*found
&& (hr
= text_to_int32( text
, &val
)) != S_OK
) return hr
;
4705 case WS_READ_REQUIRED_VALUE
:
4706 if (!*found
) return WS_E_INVALID_FORMAT
;
4709 case WS_READ_NILLABLE_VALUE
:
4710 if (size
!= sizeof(INT32
)) return E_INVALIDARG
;
4711 *(INT32
*)ret
= val
;
4714 case WS_READ_REQUIRED_POINTER
:
4715 if (!*found
) return WS_E_INVALID_FORMAT
;
4718 case WS_READ_OPTIONAL_POINTER
:
4719 case WS_READ_NILLABLE_POINTER
:
4721 INT32
*heap_val
= NULL
;
4722 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4725 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4728 *(INT32
**)ret
= heap_val
;
4732 FIXME( "read option %u not supported\n", option
);
4739 static HRESULT
text_to_int64( const WS_XML_TEXT
*text
, INT64
*val
)
4743 switch (text
->textType
)
4745 case WS_XML_TEXT_TYPE_UTF8
:
4747 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4748 hr
= str_to_int64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MIN_INT64
, MAX_INT64
, val
);
4751 case WS_XML_TEXT_TYPE_INT64
:
4753 const WS_XML_INT64_TEXT
*text_int64
= (const WS_XML_INT64_TEXT
*)text
;
4754 *val
= text_int64
->value
;
4759 FIXME( "unhandled text type %u\n", text
->textType
);
4766 static HRESULT
read_type_int64( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4767 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4768 const WS_INT64_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4769 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4771 const WS_XML_TEXT
*text
;
4777 FIXME( "description not supported\n" );
4780 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4781 if (*found
&& (hr
= text_to_int64( text
, &val
)) != S_OK
) return hr
;
4785 case WS_READ_REQUIRED_VALUE
:
4786 if (!*found
) return WS_E_INVALID_FORMAT
;
4789 case WS_READ_NILLABLE_VALUE
:
4790 if (size
!= sizeof(val
)) return E_INVALIDARG
;
4791 *(INT64
*)ret
= val
;
4794 case WS_READ_REQUIRED_POINTER
:
4795 if (!*found
) return WS_E_INVALID_FORMAT
;
4798 case WS_READ_OPTIONAL_POINTER
:
4799 case WS_READ_NILLABLE_POINTER
:
4801 INT64
*heap_val
= NULL
;
4802 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4805 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4808 *(INT64
**)ret
= heap_val
;
4812 FIXME( "read option %u not supported\n", option
);
4819 static HRESULT
text_to_uint8( const WS_XML_TEXT
*text
, UINT64
*val
)
4823 switch (text
->textType
)
4825 case WS_XML_TEXT_TYPE_UTF8
:
4827 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4828 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT8
, val
);
4831 case WS_XML_TEXT_TYPE_UINT64
:
4833 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
4834 assert( text_uint64
->value
<= MAX_UINT8
);
4835 *val
= text_uint64
->value
;
4840 FIXME( "unhandled text type %u\n", text
->textType
);
4847 static HRESULT
read_type_uint8( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4848 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4849 const WS_UINT8_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4850 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4852 const WS_XML_TEXT
*text
;
4858 FIXME( "description not supported\n" );
4861 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4862 if (*found
&& (hr
= text_to_uint8( text
, &val
)) != S_OK
) return hr
;
4866 case WS_READ_REQUIRED_VALUE
:
4867 if (!*found
) return WS_E_INVALID_FORMAT
;
4870 case WS_READ_NILLABLE_VALUE
:
4871 if (size
!= sizeof(UINT8
)) return E_INVALIDARG
;
4872 *(UINT8
*)ret
= val
;
4875 case WS_READ_REQUIRED_POINTER
:
4876 if (!*found
) return WS_E_INVALID_FORMAT
;
4879 case WS_READ_OPTIONAL_POINTER
:
4880 case WS_READ_NILLABLE_POINTER
:
4882 UINT8
*heap_val
= NULL
;
4883 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4886 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4889 *(UINT8
**)ret
= heap_val
;
4893 FIXME( "read option %u not supported\n", option
);
4900 static HRESULT
text_to_uint16( const WS_XML_TEXT
*text
, UINT64
*val
)
4904 switch (text
->textType
)
4906 case WS_XML_TEXT_TYPE_UTF8
:
4908 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4909 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT16
, val
);
4912 case WS_XML_TEXT_TYPE_INT32
:
4914 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
4915 assert( text_int32
->value
>= 0 );
4916 assert( text_int32
->value
<= MAX_UINT16
);
4917 *val
= text_int32
->value
;
4921 case WS_XML_TEXT_TYPE_UINT64
:
4923 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
4924 assert( text_uint64
->value
<= MAX_UINT16
);
4925 *val
= text_uint64
->value
;
4930 FIXME( "unhandled text type %u\n", text
->textType
);
4937 static HRESULT
read_type_uint16( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
4938 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
4939 const WS_UINT16_DESCRIPTION
*desc
, WS_READ_OPTION option
,
4940 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
4942 const WS_XML_TEXT
*text
;
4948 FIXME( "description not supported\n" );
4951 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
4952 if (*found
&& (hr
= text_to_uint16( text
, &val
)) != S_OK
) return hr
;
4956 case WS_READ_REQUIRED_VALUE
:
4957 if (!*found
) return WS_E_INVALID_FORMAT
;
4960 case WS_READ_NILLABLE_VALUE
:
4961 if (size
!= sizeof(UINT16
)) return E_INVALIDARG
;
4962 *(UINT16
*)ret
= val
;
4965 case WS_READ_REQUIRED_POINTER
:
4966 if (!*found
) return WS_E_INVALID_FORMAT
;
4969 case WS_READ_OPTIONAL_POINTER
:
4970 case WS_READ_NILLABLE_POINTER
:
4972 UINT16
*heap_val
= NULL
;
4973 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
4976 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
4979 *(UINT16
**)ret
= heap_val
;
4983 FIXME( "read option %u not supported\n", option
);
4990 static HRESULT
text_to_uint32( const WS_XML_TEXT
*text
, UINT64
*val
)
4994 switch (text
->textType
)
4996 case WS_XML_TEXT_TYPE_UTF8
:
4998 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
4999 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT32
, val
);
5002 case WS_XML_TEXT_TYPE_INT32
:
5004 const WS_XML_INT32_TEXT
*text_int32
= (const WS_XML_INT32_TEXT
*)text
;
5005 assert( text_int32
->value
>= 0 );
5006 *val
= text_int32
->value
;
5010 case WS_XML_TEXT_TYPE_UINT64
:
5012 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
5013 assert( text_uint64
->value
<= MAX_UINT32
);
5014 *val
= text_uint64
->value
;
5019 FIXME( "unhandled text type %u\n", text
->textType
);
5026 static HRESULT
read_type_uint32( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5027 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5028 const WS_UINT32_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5029 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5031 const WS_XML_TEXT
*text
;
5037 FIXME( "description not supported\n" );
5040 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5041 if (*found
&& (hr
= text_to_uint32( text
, &val
)) != S_OK
) return hr
;
5045 case WS_READ_REQUIRED_VALUE
:
5046 if (!*found
) return WS_E_INVALID_FORMAT
;
5049 case WS_READ_NILLABLE_VALUE
:
5050 if (size
!= sizeof(UINT32
)) return E_INVALIDARG
;
5051 *(UINT32
*)ret
= val
;
5054 case WS_READ_REQUIRED_POINTER
:
5055 if (!*found
) return WS_E_INVALID_FORMAT
;
5058 case WS_READ_OPTIONAL_POINTER
:
5059 case WS_READ_NILLABLE_POINTER
:
5061 UINT32
*heap_val
= NULL
;
5062 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5065 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5068 *(UINT32
**)ret
= heap_val
;
5072 FIXME( "read option %u not supported\n", option
);
5079 static HRESULT
text_to_uint64( const WS_XML_TEXT
*text
, UINT64
*val
)
5083 switch (text
->textType
)
5085 case WS_XML_TEXT_TYPE_UTF8
:
5087 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5088 hr
= str_to_uint64( text_utf8
->value
.bytes
, text_utf8
->value
.length
, MAX_UINT64
, val
);
5091 case WS_XML_TEXT_TYPE_INT64
:
5093 const WS_XML_INT64_TEXT
*text_int64
= (const WS_XML_INT64_TEXT
*)text
;
5094 *val
= text_int64
->value
;
5098 case WS_XML_TEXT_TYPE_UINT64
:
5100 const WS_XML_UINT64_TEXT
*text_uint64
= (const WS_XML_UINT64_TEXT
*)text
;
5101 *val
= text_uint64
->value
;
5106 FIXME( "unhandled text type %u\n", text
->textType
);
5113 static HRESULT
read_type_uint64( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5114 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5115 const WS_UINT64_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5116 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5118 const WS_XML_TEXT
*text
;
5124 FIXME( "description not supported\n" );
5127 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5128 if (*found
&& (hr
= text_to_uint64( text
, &val
)) != S_OK
) return hr
;
5132 case WS_READ_REQUIRED_VALUE
:
5133 if (!*found
) return WS_E_INVALID_FORMAT
;
5136 case WS_READ_NILLABLE_VALUE
:
5137 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5138 *(UINT64
*)ret
= val
;
5141 case WS_READ_REQUIRED_POINTER
:
5142 if (!*found
) return WS_E_INVALID_FORMAT
;
5145 case WS_READ_OPTIONAL_POINTER
:
5146 case WS_READ_NILLABLE_POINTER
:
5148 UINT64
*heap_val
= NULL
;
5149 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5152 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5155 *(UINT64
**)ret
= heap_val
;
5159 FIXME( "read option %u not supported\n", option
);
5166 static HRESULT
text_to_float( const WS_XML_TEXT
*text
, float *val
)
5170 switch (text
->textType
)
5172 case WS_XML_TEXT_TYPE_UTF8
:
5174 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5175 hr
= str_to_float( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5178 case WS_XML_TEXT_TYPE_FLOAT
:
5180 const WS_XML_FLOAT_TEXT
*text_float
= (const WS_XML_FLOAT_TEXT
*)text
;
5181 *val
= text_float
->value
;
5186 FIXME( "unhandled text type %u\n", text
->textType
);
5193 static HRESULT
read_type_float( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5194 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5195 const WS_FLOAT_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5196 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5198 const WS_XML_TEXT
*text
;
5202 if (desc
) FIXME( "ignoring description\n" );
5204 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5205 if (*found
&& (hr
= text_to_float( text
, &val
)) != S_OK
) return hr
;
5209 case WS_READ_REQUIRED_VALUE
:
5210 if (!*found
) return WS_E_INVALID_FORMAT
;
5213 case WS_READ_NILLABLE_VALUE
:
5214 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5215 *(float *)ret
= val
;
5218 case WS_READ_REQUIRED_POINTER
:
5219 if (!*found
) return WS_E_INVALID_FORMAT
;
5222 case WS_READ_OPTIONAL_POINTER
:
5223 case WS_READ_NILLABLE_POINTER
:
5225 float *heap_val
= NULL
;
5226 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5229 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5232 *(float **)ret
= heap_val
;
5236 FIXME( "read option %u not supported\n", option
);
5243 static HRESULT
text_to_double( const WS_XML_TEXT
*text
, double *val
)
5247 switch (text
->textType
)
5249 case WS_XML_TEXT_TYPE_UTF8
:
5251 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5252 hr
= str_to_double( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5255 case WS_XML_TEXT_TYPE_DOUBLE
:
5257 const WS_XML_DOUBLE_TEXT
*text_double
= (const WS_XML_DOUBLE_TEXT
*)text
;
5258 *val
= text_double
->value
;
5263 FIXME( "unhandled text type %u\n", text
->textType
);
5270 static HRESULT
read_type_double( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5271 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5272 const WS_DOUBLE_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5273 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5275 const WS_XML_TEXT
*text
;
5279 if (desc
) FIXME( "ignoring description\n" );
5281 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5282 if (*found
&& (hr
= text_to_double( text
, &val
)) != S_OK
) return hr
;
5286 case WS_READ_REQUIRED_VALUE
:
5287 if (!*found
) return WS_E_INVALID_FORMAT
;
5290 case WS_READ_NILLABLE_VALUE
:
5291 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5292 *(double *)ret
= val
;
5295 case WS_READ_REQUIRED_POINTER
:
5296 if (!*found
) return WS_E_INVALID_FORMAT
;
5299 case WS_READ_OPTIONAL_POINTER
:
5300 case WS_READ_NILLABLE_POINTER
:
5302 double *heap_val
= NULL
;
5303 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5306 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5309 *(double **)ret
= heap_val
;
5313 FIXME( "read option %u not supported\n", option
);
5320 static HRESULT
text_to_wsz( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WCHAR
**ret
)
5322 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5325 assert( text
->textType
== WS_XML_TEXT_TYPE_UTF8
);
5326 len
= MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, NULL
, 0 );
5327 if (!(*ret
= ws_alloc( heap
, (len
+ 1) * sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
5328 MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, *ret
, len
);
5333 static HRESULT
read_type_wsz( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5334 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5335 const WS_WSZ_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5336 WS_HEAP
*heap
, WCHAR
**ret
, ULONG size
, BOOL
*found
)
5338 const WS_XML_TEXT
*text
;
5344 FIXME( "description not supported\n" );
5347 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5348 if (*found
&& (hr
= text_to_wsz( text
, heap
, &str
)) != S_OK
) return hr
;
5352 case WS_READ_REQUIRED_POINTER
:
5353 if (!str
&& !(str
= ws_alloc_zero( heap
, sizeof(*str
) ))) return WS_E_QUOTA_EXCEEDED
;
5356 case WS_READ_OPTIONAL_POINTER
:
5357 case WS_READ_NILLABLE_POINTER
:
5358 if (size
!= sizeof(str
)) return E_INVALIDARG
;
5363 FIXME( "read option %u not supported\n", option
);
5370 static HRESULT
get_enum_value( const WS_XML_TEXT
*text
, const WS_ENUM_DESCRIPTION
*desc
, int *ret
)
5372 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5375 assert( text
->textType
== WS_XML_TEXT_TYPE_UTF8
);
5376 for (i
= 0; i
< desc
->valueCount
; i
++)
5378 if (WsXmlStringEquals( &utf8
->value
, desc
->values
[i
].name
, NULL
) == S_OK
)
5380 *ret
= desc
->values
[i
].value
;
5384 return WS_E_INVALID_FORMAT
;
5387 static HRESULT
read_type_enum( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5388 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5389 const WS_ENUM_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5390 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5392 const WS_XML_TEXT
*text
;
5396 if (!desc
) return E_INVALIDARG
;
5398 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5399 if (*found
&& (hr
= get_enum_value( text
, desc
, &val
)) != S_OK
) return hr
;
5403 case WS_READ_REQUIRED_VALUE
:
5404 if (!*found
) return WS_E_INVALID_FORMAT
;
5407 case WS_READ_NILLABLE_VALUE
:
5408 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5412 case WS_READ_REQUIRED_POINTER
:
5413 if (!*found
) return WS_E_INVALID_FORMAT
;
5416 case WS_READ_OPTIONAL_POINTER
:
5417 case WS_READ_NILLABLE_POINTER
:
5419 int *heap_val
= NULL
;
5420 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5423 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5426 *(int **)ret
= heap_val
;
5430 FIXME( "read option %u not supported\n", option
);
5437 static HRESULT
text_to_datetime( const WS_XML_TEXT
*text
, WS_DATETIME
*val
)
5441 switch (text
->textType
)
5443 case WS_XML_TEXT_TYPE_UTF8
:
5445 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5446 hr
= str_to_datetime( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5449 case WS_XML_TEXT_TYPE_DATETIME
:
5451 const WS_XML_DATETIME_TEXT
*text_datetime
= (const WS_XML_DATETIME_TEXT
*)text
;
5452 *val
= text_datetime
->value
;
5457 FIXME( "unhandled text type %u\n", text
->textType
);
5464 static HRESULT
read_type_datetime( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5465 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5466 const WS_DATETIME_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5467 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5469 const WS_XML_TEXT
*text
;
5471 WS_DATETIME val
= {0, WS_DATETIME_FORMAT_UTC
};
5473 if (desc
) FIXME( "ignoring description\n" );
5475 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5476 if (*found
&& (hr
= text_to_datetime( text
, &val
)) != S_OK
) return hr
;
5480 case WS_READ_REQUIRED_VALUE
:
5481 if (!*found
) return WS_E_INVALID_FORMAT
;
5484 case WS_READ_NILLABLE_VALUE
:
5485 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5486 *(WS_DATETIME
*)ret
= val
;
5489 case WS_READ_REQUIRED_POINTER
:
5490 if (!*found
) return WS_E_INVALID_FORMAT
;
5493 case WS_READ_OPTIONAL_POINTER
:
5494 case WS_READ_NILLABLE_POINTER
:
5496 WS_DATETIME
*heap_val
= NULL
;
5497 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5500 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5503 *(WS_DATETIME
**)ret
= heap_val
;
5507 FIXME( "read option %u not supported\n", option
);
5514 static HRESULT
text_to_guid( const WS_XML_TEXT
*text
, GUID
*val
)
5518 switch (text
->textType
)
5520 case WS_XML_TEXT_TYPE_UTF8
:
5522 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5523 hr
= str_to_guid( text_utf8
->value
.bytes
, text_utf8
->value
.length
, val
);
5526 case WS_XML_TEXT_TYPE_GUID
:
5528 const WS_XML_GUID_TEXT
*text_guid
= (const WS_XML_GUID_TEXT
*)text
;
5529 *val
= text_guid
->value
;
5534 FIXME( "unhandled text type %u\n", text
->textType
);
5541 static HRESULT
read_type_guid( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5542 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5543 const WS_GUID_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5544 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5546 const WS_XML_TEXT
*text
;
5550 if (desc
) FIXME( "ignoring description\n" );
5552 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5553 if (*found
&& (hr
= text_to_guid( text
, &val
)) != S_OK
) return hr
;
5557 case WS_READ_REQUIRED_VALUE
:
5558 if (!*found
) return WS_E_INVALID_FORMAT
;
5561 case WS_READ_NILLABLE_VALUE
:
5562 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5566 case WS_READ_REQUIRED_POINTER
:
5567 if (!*found
) return WS_E_INVALID_FORMAT
;
5570 case WS_READ_OPTIONAL_POINTER
:
5571 case WS_READ_NILLABLE_POINTER
:
5573 GUID
*heap_val
= NULL
;
5574 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5577 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5580 *(GUID
**)ret
= heap_val
;
5584 FIXME( "read option %u not supported\n", option
);
5591 static HRESULT
text_to_unique_id( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_UNIQUE_ID
*val
)
5595 switch (text
->textType
)
5597 case WS_XML_TEXT_TYPE_UTF8
:
5599 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5600 hr
= str_to_unique_id( text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, val
);
5603 case WS_XML_TEXT_TYPE_UNIQUE_ID
:
5605 const WS_XML_UNIQUE_ID_TEXT
*text_unique_id
= (const WS_XML_UNIQUE_ID_TEXT
*)text
;
5606 val
->guid
= text_unique_id
->value
;
5607 val
->uri
.length
= 0;
5608 val
->uri
.chars
= NULL
;
5613 FIXME( "unhandled text type %u\n", text
->textType
);
5620 static HRESULT
read_type_unique_id( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5621 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5622 const WS_UNIQUE_ID_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5623 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5625 const WS_XML_TEXT
*text
;
5626 WS_UNIQUE_ID val
= {{0}};
5629 if (desc
) FIXME( "ignoring description\n" );
5631 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5632 if (*found
&& (hr
= text_to_unique_id( text
, heap
, &val
)) != S_OK
) return hr
;
5636 case WS_READ_REQUIRED_VALUE
:
5637 if (!*found
) return WS_E_INVALID_FORMAT
;
5640 case WS_READ_NILLABLE_VALUE
:
5641 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5642 *(WS_UNIQUE_ID
*)ret
= val
;
5645 case WS_READ_REQUIRED_POINTER
:
5646 if (!*found
) return WS_E_INVALID_FORMAT
;
5649 case WS_READ_OPTIONAL_POINTER
:
5650 case WS_READ_NILLABLE_POINTER
:
5652 WS_UNIQUE_ID
*heap_val
= NULL
;
5653 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5656 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5659 *(WS_UNIQUE_ID
**)ret
= heap_val
;
5663 FIXME( "read option %u not supported\n", option
);
5670 static HRESULT
text_to_string( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_STRING
*val
)
5674 switch (text
->textType
)
5676 case WS_XML_TEXT_TYPE_UTF8
:
5678 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5679 hr
= str_to_string( text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, val
);
5682 case WS_XML_TEXT_TYPE_UTF16
:
5684 const WS_XML_UTF16_TEXT
*text_utf16
= (const WS_XML_UTF16_TEXT
*)text
;
5685 if (!(val
->chars
= ws_alloc( heap
, text_utf16
->byteCount
))) return WS_E_QUOTA_EXCEEDED
;
5686 memcpy( val
->chars
, text_utf16
->bytes
, text_utf16
->byteCount
);
5687 val
->length
= text_utf16
->byteCount
/ sizeof(WCHAR
);
5692 FIXME( "unhandled text type %u\n", text
->textType
);
5699 static HRESULT
read_type_string( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5700 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5701 const WS_STRING_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5702 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5704 const WS_XML_TEXT
*text
;
5705 WS_STRING val
= {0};
5708 if (desc
) FIXME( "ignoring description\n" );
5710 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5711 if (*found
&& (hr
= text_to_string( text
, heap
, &val
)) != S_OK
) return hr
;
5715 case WS_READ_REQUIRED_VALUE
:
5716 case WS_READ_NILLABLE_VALUE
:
5717 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5718 *(WS_STRING
*)ret
= val
;
5721 case WS_READ_REQUIRED_POINTER
:
5723 WS_STRING
*heap_val
;
5724 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5725 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5727 *(WS_STRING
**)ret
= heap_val
;
5730 case WS_READ_OPTIONAL_POINTER
:
5731 case WS_READ_NILLABLE_POINTER
:
5733 WS_STRING
*heap_val
= NULL
;
5734 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5737 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5740 *(WS_STRING
**)ret
= heap_val
;
5744 FIXME( "read option %u not supported\n", option
);
5751 static HRESULT
text_to_bytes( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_BYTES
*val
)
5755 switch (text
->textType
)
5757 case WS_XML_TEXT_TYPE_UTF8
:
5759 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5760 hr
= str_to_bytes( text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, val
);
5763 case WS_XML_TEXT_TYPE_BASE64
:
5765 const WS_XML_BASE64_TEXT
*text_base64
= (const WS_XML_BASE64_TEXT
*)text
;
5766 if (!(val
->bytes
= ws_alloc( heap
, text_base64
->length
))) return WS_E_QUOTA_EXCEEDED
;
5767 memcpy( val
->bytes
, text_base64
->bytes
, text_base64
->length
);
5768 val
->length
= text_base64
->length
;
5773 FIXME( "unhandled text type %u\n", text
->textType
);
5780 static HRESULT
read_type_bytes( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5781 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5782 const WS_BYTES_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5783 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5785 const WS_XML_TEXT
*text
;
5789 if (desc
) FIXME( "ignoring description\n" );
5791 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5792 if (*found
&& (hr
= text_to_bytes( text
, heap
, &val
)) != S_OK
) return hr
;
5796 case WS_READ_REQUIRED_VALUE
:
5797 case WS_READ_NILLABLE_VALUE
:
5798 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5799 *(WS_BYTES
*)ret
= val
;
5802 case WS_READ_REQUIRED_POINTER
:
5805 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5806 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5808 *(WS_BYTES
**)ret
= heap_val
;
5811 case WS_READ_OPTIONAL_POINTER
:
5812 case WS_READ_NILLABLE_POINTER
:
5814 WS_BYTES
*heap_val
= NULL
;
5815 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5818 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5821 *(WS_BYTES
**)ret
= heap_val
;
5825 FIXME( "read option %u not supported\n", option
);
5832 static HRESULT
text_to_xml_string( const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_XML_STRING
*val
)
5834 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5835 assert( text
->textType
== WS_XML_TEXT_TYPE_UTF8
);
5836 return str_to_xml_string( utf8
->value
.bytes
, utf8
->value
.length
, heap
, val
);
5839 static HRESULT
read_type_xml_string( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5840 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5841 const WS_XML_STRING_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5842 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5844 const WS_XML_TEXT
*text
;
5845 WS_XML_STRING val
= {0};
5848 if (desc
) FIXME( "ignoring description\n" );
5850 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5851 if (*found
&& (hr
= text_to_xml_string( text
, heap
, &val
)) != S_OK
) return hr
;
5855 case WS_READ_REQUIRED_VALUE
:
5856 case WS_READ_NILLABLE_VALUE
:
5857 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5858 *(WS_XML_STRING
*)ret
= val
;
5861 case WS_READ_REQUIRED_POINTER
:
5863 WS_XML_STRING
*heap_val
;
5864 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5865 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5867 *(WS_XML_STRING
**)ret
= heap_val
;
5870 case WS_READ_OPTIONAL_POINTER
:
5871 case WS_READ_NILLABLE_POINTER
:
5873 WS_XML_STRING
*heap_val
= NULL
;
5874 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5877 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5880 *(WS_XML_STRING
**)ret
= heap_val
;
5884 FIXME( "read option %u not supported\n", option
);
5891 static HRESULT
text_to_qname( struct reader
*reader
, const WS_XML_TEXT
*text
, WS_HEAP
*heap
, WS_XML_QNAME
*val
)
5895 switch (text
->textType
)
5897 case WS_XML_TEXT_TYPE_UTF8
:
5899 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
5900 hr
= str_to_qname( reader
, text_utf8
->value
.bytes
, text_utf8
->value
.length
, heap
, NULL
,
5901 &val
->localName
, &val
->ns
);
5904 case WS_XML_TEXT_TYPE_QNAME
:
5906 const WS_XML_QNAME_TEXT
*text_qname
= (const WS_XML_QNAME_TEXT
*)text
;
5907 if ((hr
= copy_xml_string( heap
, text_qname
->localName
, &val
->localName
)) != S_OK
) return hr
;
5908 if ((hr
= copy_xml_string( heap
, text_qname
->ns
, &val
->ns
)) != S_OK
)
5910 ws_free( heap
, val
->localName
.bytes
, val
->localName
.length
);
5916 FIXME( "unhandled text type %u\n", text
->textType
);
5923 static HRESULT
read_type_qname( struct reader
*reader
, WS_TYPE_MAPPING mapping
,
5924 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
5925 const WS_XML_QNAME_DESCRIPTION
*desc
, WS_READ_OPTION option
,
5926 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
5928 const WS_XML_TEXT
*text
;
5929 WS_XML_QNAME val
= {{0}};
5932 if (desc
) FIXME( "ignoring description\n" );
5934 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) return WS_E_INVALID_FORMAT
;
5935 if ((hr
= read_startelement( reader
)) != S_OK
) return hr
;
5936 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_TEXT
) return WS_E_INVALID_FORMAT
;
5938 if ((hr
= get_text( reader
, mapping
, localname
, ns
, &text
, found
)) != S_OK
) return hr
;
5939 if (*found
&& (hr
= text_to_qname( reader
, text
, heap
, &val
)) != S_OK
) return hr
;
5943 case WS_READ_REQUIRED_VALUE
:
5944 if (!*found
) return WS_E_INVALID_FORMAT
;
5947 case WS_READ_NILLABLE_VALUE
:
5948 if (size
!= sizeof(val
)) return E_INVALIDARG
;
5949 *(WS_XML_QNAME
*)ret
= val
;
5952 case WS_READ_REQUIRED_POINTER
:
5953 if (!*found
) return WS_E_INVALID_FORMAT
;
5956 case WS_READ_OPTIONAL_POINTER
:
5957 case WS_READ_NILLABLE_POINTER
:
5959 WS_XML_QNAME
*heap_val
= NULL
;
5960 if (size
!= sizeof(heap_val
)) return E_INVALIDARG
;
5963 if (!(heap_val
= ws_alloc( heap
, sizeof(*heap_val
) ))) return WS_E_QUOTA_EXCEEDED
;
5966 *(WS_XML_QNAME
**)ret
= heap_val
;
5970 FIXME( "read option %u not supported\n", option
);
5977 static BOOL
is_empty_text_node( const struct node
*node
)
5979 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)node
;
5981 if (node_type( node
) != WS_XML_NODE_TYPE_TEXT
) return FALSE
;
5982 switch (text
->text
->textType
)
5984 case WS_XML_TEXT_TYPE_UTF8
:
5987 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
->text
;
5988 for (i
= 0; i
< utf8
->value
.length
; i
++) if (!read_isspace( utf8
->value
.bytes
[i
] )) return FALSE
;
5991 case WS_XML_TEXT_TYPE_BASE64
:
5993 const WS_XML_BASE64_TEXT
*base64
= (const WS_XML_BASE64_TEXT
*)text
->text
;
5994 return !base64
->length
;
5996 case WS_XML_TEXT_TYPE_BOOL
:
5997 case WS_XML_TEXT_TYPE_INT32
:
5998 case WS_XML_TEXT_TYPE_INT64
:
5999 case WS_XML_TEXT_TYPE_UINT64
:
6000 case WS_XML_TEXT_TYPE_FLOAT
:
6001 case WS_XML_TEXT_TYPE_DOUBLE
:
6002 case WS_XML_TEXT_TYPE_DECIMAL
:
6003 case WS_XML_TEXT_TYPE_GUID
:
6004 case WS_XML_TEXT_TYPE_UNIQUE_ID
:
6005 case WS_XML_TEXT_TYPE_DATETIME
:
6009 ERR( "unhandled text type %u\n", text
->text
->textType
);
6014 /* skips comment and empty text nodes */
6015 static HRESULT
read_type_next_node( struct reader
*reader
)
6020 WS_XML_NODE_TYPE type
;
6022 if ((hr
= read_next_node( reader
)) != S_OK
) return hr
;
6023 type
= node_type( reader
->current
);
6024 if (type
== WS_XML_NODE_TYPE_COMMENT
||
6025 (type
== WS_XML_NODE_TYPE_TEXT
&& is_empty_text_node( reader
->current
))) continue;
6030 static HRESULT
read_type_next_element_node( struct reader
*reader
, const WS_XML_STRING
*localname
,
6031 const WS_XML_STRING
*ns
)
6033 struct reader_pos pos
;
6036 if (!localname
) return S_OK
; /* assume reader is already correctly positioned */
6037 if (reader
->current
== reader
->last
)
6040 if ((hr
= read_to_startelement( reader
, &found
)) != S_OK
) return hr
;
6041 if (!found
) return WS_E_INVALID_FORMAT
;
6043 if (match_element( reader
->current
, localname
, ns
)) return S_OK
;
6045 save_reader_position( reader
, &pos
);
6046 if ((hr
= read_type_next_node( reader
)) != S_OK
) return hr
;
6047 if (match_element( reader
->current
, localname
, ns
)) return S_OK
;
6048 restore_reader_position( reader
, &pos
);
6050 return WS_E_INVALID_FORMAT
;
6053 ULONG
get_type_size( WS_TYPE type
, const void *desc
)
6059 return sizeof(INT8
);
6062 case WS_UINT16_TYPE
:
6063 return sizeof(INT16
);
6067 case WS_UINT32_TYPE
:
6069 return sizeof(INT32
);
6072 case WS_UINT64_TYPE
:
6073 return sizeof(INT64
);
6076 return sizeof(float);
6078 case WS_DOUBLE_TYPE
:
6079 return sizeof(double);
6081 case WS_DATETIME_TYPE
:
6082 return sizeof(WS_DATETIME
);
6085 return sizeof(GUID
);
6087 case WS_UNIQUE_ID_TYPE
:
6088 return sizeof(WS_UNIQUE_ID
);
6090 case WS_STRING_TYPE
:
6091 return sizeof(WS_STRING
);
6094 return sizeof(WCHAR
*);
6097 return sizeof(WS_BYTES
);
6099 case WS_XML_STRING_TYPE
:
6100 return sizeof(WS_XML_STRING
);
6102 case WS_XML_QNAME_TYPE
:
6103 return sizeof(WS_XML_QNAME
);
6105 case WS_DESCRIPTION_TYPE
:
6106 return sizeof(WS_STRUCT_DESCRIPTION
*);
6108 case WS_STRUCT_TYPE
:
6110 const WS_STRUCT_DESCRIPTION
*desc_struct
= desc
;
6111 return desc_struct
->size
;
6115 const WS_UNION_DESCRIPTION
*desc_union
= desc
;
6116 return desc_union
->size
;
6119 ERR( "unhandled type %u\n", type
);
6124 static WS_READ_OPTION
get_field_read_option( WS_TYPE type
, ULONG options
)
6126 if (options
& WS_FIELD_POINTER
)
6128 if (options
& WS_FIELD_NILLABLE
) return WS_READ_NILLABLE_POINTER
;
6129 if (options
& WS_FIELD_OPTIONAL
) return WS_READ_OPTIONAL_POINTER
;
6130 return WS_READ_REQUIRED_POINTER
;
6141 case WS_UINT16_TYPE
:
6142 case WS_UINT32_TYPE
:
6143 case WS_UINT64_TYPE
:
6145 case WS_DOUBLE_TYPE
:
6146 case WS_DATETIME_TYPE
:
6148 case WS_UNIQUE_ID_TYPE
:
6149 case WS_STRING_TYPE
:
6151 case WS_XML_STRING_TYPE
:
6152 case WS_XML_QNAME_TYPE
:
6153 case WS_STRUCT_TYPE
:
6156 if (options
& (WS_FIELD_OPTIONAL
|WS_FIELD_NILLABLE
)) return WS_READ_NILLABLE_VALUE
;
6157 return WS_READ_REQUIRED_VALUE
;
6160 case WS_DESCRIPTION_TYPE
:
6161 if (options
& WS_FIELD_NILLABLE
) return WS_READ_NILLABLE_POINTER
;
6162 if (options
& WS_FIELD_OPTIONAL
) return WS_READ_OPTIONAL_POINTER
;
6163 return WS_READ_REQUIRED_POINTER
;
6166 FIXME( "unhandled type %u\n", type
);
6171 static HRESULT
read_type_field( struct reader
*, const WS_FIELD_DESCRIPTION
*, WS_HEAP
*, char *, ULONG
);
6173 static HRESULT
read_type_union( struct reader
*reader
, const WS_UNION_DESCRIPTION
*desc
, WS_HEAP
*heap
, void *ret
,
6174 ULONG size
, BOOL
*found
)
6176 struct reader_pos pos
;
6180 if (size
!= desc
->size
) return E_INVALIDARG
;
6182 save_reader_position( reader
, &pos
);
6183 if ((hr
= read_type_next_node( reader
)) != S_OK
) return hr
;
6185 for (i
= 0; i
< desc
->fieldCount
; i
++)
6187 if ((*found
= match_element( reader
->current
, desc
->fields
[i
]->field
.localName
, desc
->fields
[i
]->field
.ns
)))
6193 *(int *)((char *)ret
+ desc
->enumOffset
) = desc
->noneEnumValue
;
6194 restore_reader_position( reader
, &pos
);
6198 ULONG offset
= desc
->fields
[i
]->field
.offset
;
6199 if ((hr
= read_type_field( reader
, &desc
->fields
[i
]->field
, heap
, ret
, offset
)) != S_OK
) return hr
;
6200 *(int *)((char *)ret
+ desc
->enumOffset
) = desc
->fields
[i
]->value
;
6206 static HRESULT
read_type( struct reader
*, WS_TYPE_MAPPING
, WS_TYPE
, const WS_XML_STRING
*,
6207 const WS_XML_STRING
*, const void *, WS_READ_OPTION
, WS_HEAP
*,
6208 void *, ULONG
, BOOL
* );
6210 static HRESULT
read_type_array( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
,
6211 void **ret
, ULONG
*count
)
6214 ULONG item_size
, nb_items
= 0, nb_allocated
= 1, offset
= 0;
6215 WS_READ_OPTION option
;
6219 if (!(option
= get_field_read_option( desc
->type
, desc
->options
))) return E_INVALIDARG
;
6221 /* wrapper element */
6222 if (desc
->localName
&& ((hr
= read_type_next_element_node( reader
, desc
->localName
, desc
->ns
)) != S_OK
))
6225 if (option
== WS_READ_REQUIRED_VALUE
|| option
== WS_READ_NILLABLE_VALUE
)
6226 item_size
= get_type_size( desc
->type
, desc
->typeDescription
);
6228 item_size
= sizeof(void *);
6230 if (!(buf
= ws_alloc_zero( heap
, item_size
))) return WS_E_QUOTA_EXCEEDED
;
6233 if (nb_items
>= nb_allocated
)
6235 SIZE_T old_size
= nb_allocated
* item_size
, new_size
= old_size
* 2;
6236 if (!(buf
= ws_realloc_zero( heap
, buf
, old_size
, new_size
))) return WS_E_QUOTA_EXCEEDED
;
6240 if (desc
->type
== WS_UNION_TYPE
)
6242 hr
= read_type_union( reader
, desc
->typeDescription
, heap
, buf
+ offset
, item_size
, &found
);
6245 ws_free( heap
, buf
, nb_allocated
* item_size
);
6252 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->type
, desc
->itemLocalName
, desc
->itemNs
,
6253 desc
->typeDescription
, option
, heap
, buf
+ offset
, item_size
, &found
);
6254 if (hr
== WS_E_INVALID_FORMAT
) break;
6257 ws_free( heap
, buf
, nb_allocated
* item_size
);
6262 offset
+= item_size
;
6266 if (desc
->localName
&& ((hr
= read_type_next_node( reader
)) != S_OK
)) return hr
;
6268 if (desc
->itemRange
&& (nb_items
< desc
->itemRange
->minItemCount
|| nb_items
> desc
->itemRange
->maxItemCount
))
6270 TRACE( "number of items %u out of range (%u-%u)\n", nb_items
, desc
->itemRange
->minItemCount
,
6271 desc
->itemRange
->maxItemCount
);
6272 ws_free( heap
, buf
, nb_allocated
* item_size
);
6273 return WS_E_INVALID_FORMAT
;
6282 static HRESULT
read_type_text( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
,
6283 WS_READ_OPTION option
, WS_HEAP
*heap
, void *ret
, ULONG size
)
6285 struct reader_pos pos
;
6289 if (reader
->current
== reader
->last
)
6291 if ((hr
= read_to_startelement( reader
, &found
)) != S_OK
) return hr
;
6292 if (!found
) return WS_E_INVALID_FORMAT
;
6295 save_reader_position( reader
, &pos
);
6296 if ((hr
= read_next_node( reader
)) != S_OK
) return hr
;
6297 hr
= read_type( reader
, WS_ANY_ELEMENT_TYPE_MAPPING
, desc
->type
, NULL
, NULL
,
6298 desc
->typeDescription
, option
, heap
, ret
, size
, &found
);
6299 if (hr
== S_OK
&& !found
) restore_reader_position( reader
, &pos
);
6303 static HRESULT
read_type_field( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
, char *buf
,
6307 WS_READ_OPTION option
;
6312 if (!desc
) return E_INVALIDARG
;
6313 if (desc
->options
& ~(WS_FIELD_POINTER
|WS_FIELD_OPTIONAL
|WS_FIELD_NILLABLE
|WS_FIELD_NILLABLE_ITEM
))
6315 FIXME( "options %08x not supported\n", desc
->options
);
6318 if (!(option
= get_field_read_option( desc
->type
, desc
->options
))) return E_INVALIDARG
;
6320 if (option
== WS_READ_REQUIRED_VALUE
|| option
== WS_READ_NILLABLE_VALUE
)
6321 size
= get_type_size( desc
->type
, desc
->typeDescription
);
6323 size
= sizeof(void *);
6326 switch (desc
->mapping
)
6328 case WS_TYPE_ATTRIBUTE_FIELD_MAPPING
:
6329 FIXME( "WS_TYPE_ATTRIBUTE_FIELD_MAPPING not supported\n" );
6332 case WS_ATTRIBUTE_FIELD_MAPPING
:
6333 hr
= read_type( reader
, WS_ATTRIBUTE_TYPE_MAPPING
, desc
->type
, desc
->localName
, desc
->ns
,
6334 desc
->typeDescription
, option
, heap
, ptr
, size
, &found
);
6337 case WS_ELEMENT_FIELD_MAPPING
:
6338 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->type
, desc
->localName
, desc
->ns
,
6339 desc
->typeDescription
, option
, heap
, ptr
, size
, &found
);
6342 case WS_ELEMENT_CHOICE_FIELD_MAPPING
:
6344 if (desc
->type
!= WS_UNION_TYPE
|| !desc
->typeDescription
||
6345 (desc
->options
& (WS_FIELD_POINTER
|WS_FIELD_NILLABLE
))) return E_INVALIDARG
;
6346 hr
= read_type_union( reader
, desc
->typeDescription
, heap
, ptr
, size
, &found
);
6349 case WS_REPEATING_ELEMENT_FIELD_MAPPING
:
6350 case WS_REPEATING_ELEMENT_CHOICE_FIELD_MAPPING
:
6353 hr
= read_type_array( reader
, desc
, heap
, (void **)ptr
, &count
);
6354 if (hr
== S_OK
) *(ULONG
*)(buf
+ desc
->countOffset
) = count
;
6357 case WS_TEXT_FIELD_MAPPING
:
6358 hr
= read_type_text( reader
, desc
, option
, heap
, ptr
, size
);
6362 FIXME( "unhandled field mapping %u\n", desc
->mapping
);
6366 if (hr
== WS_E_INVALID_FORMAT
)
6370 case WS_READ_REQUIRED_VALUE
:
6371 case WS_READ_REQUIRED_POINTER
:
6372 return WS_E_INVALID_FORMAT
;
6374 case WS_READ_NILLABLE_VALUE
:
6375 if (desc
->defaultValue
) memcpy( ptr
, desc
->defaultValue
->value
, desc
->defaultValue
->valueSize
);
6378 case WS_READ_OPTIONAL_POINTER
:
6379 case WS_READ_NILLABLE_POINTER
:
6380 *(void **)ptr
= NULL
;
6384 ERR( "unhandled option %u\n", option
);
6392 static HRESULT
read_type_struct( struct reader
*reader
, WS_TYPE_MAPPING mapping
, const WS_XML_STRING
*localname
,
6393 const WS_XML_STRING
*ns
, const WS_STRUCT_DESCRIPTION
*desc
, WS_READ_OPTION option
,
6394 WS_HEAP
*heap
, void *ret
, ULONG size
, BOOL
*found
)
6400 if (!desc
) return E_INVALIDARG
;
6401 if (desc
->structOptions
& ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
)
6403 FIXME( "struct options %08x not supported\n",
6404 desc
->structOptions
& ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
);
6409 case WS_READ_REQUIRED_POINTER
:
6410 case WS_READ_OPTIONAL_POINTER
:
6411 case WS_READ_NILLABLE_POINTER
:
6412 if (size
!= sizeof(void *)) return E_INVALIDARG
;
6413 if (!(buf
= ws_alloc_zero( heap
, desc
->size
))) return WS_E_QUOTA_EXCEEDED
;
6416 case WS_READ_REQUIRED_VALUE
:
6417 case WS_READ_NILLABLE_VALUE
:
6418 if (size
!= desc
->size
) return E_INVALIDARG
;
6423 FIXME( "unhandled read option %u\n", option
);
6427 for (i
= 0; i
< desc
->fieldCount
; i
++)
6429 offset
= desc
->fields
[i
]->offset
;
6430 if ((hr
= read_type_field( reader
, desc
->fields
[i
], heap
, buf
, offset
)) != S_OK
) break;
6435 case WS_READ_REQUIRED_POINTER
:
6438 ws_free( heap
, buf
, desc
->size
);
6441 *(char **)ret
= buf
;
6444 case WS_READ_OPTIONAL_POINTER
:
6445 case WS_READ_NILLABLE_POINTER
:
6446 if (is_nil_value( buf
, desc
->size
))
6448 ws_free( heap
, buf
, desc
->size
);
6451 *(char **)ret
= buf
;
6454 case WS_READ_REQUIRED_VALUE
:
6455 case WS_READ_NILLABLE_VALUE
:
6456 if (hr
!= S_OK
) return hr
;
6460 ERR( "unhandled read option %u\n", option
);
6464 if (desc
->structOptions
& WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
)
6466 struct node
*parent
= find_parent( reader
);
6467 parent
->flags
|= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT
;
6474 static HRESULT
start_mapping( struct reader
*reader
, WS_TYPE_MAPPING mapping
, const WS_XML_STRING
*localname
,
6475 const WS_XML_STRING
*ns
)
6479 case WS_ELEMENT_TYPE_MAPPING
:
6480 case WS_ELEMENT_CONTENT_TYPE_MAPPING
:
6481 return read_type_next_element_node( reader
, localname
, ns
);
6483 case WS_ANY_ELEMENT_TYPE_MAPPING
:
6484 case WS_ATTRIBUTE_TYPE_MAPPING
:
6488 FIXME( "unhandled mapping %u\n", mapping
);
6493 static HRESULT
read_type_endelement_node( struct reader
*reader
)
6495 const struct node
*parent
= find_parent( reader
);
6500 if ((hr
= read_type_next_node( reader
)) != S_OK
) return hr
;
6501 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_END_ELEMENT
&& reader
->current
->parent
== parent
)
6505 if (read_end_of_data( reader
) || !(parent
->flags
& NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT
)) break;
6508 return WS_E_INVALID_FORMAT
;
6511 static HRESULT
end_mapping( struct reader
*reader
, WS_TYPE_MAPPING mapping
)
6515 case WS_ELEMENT_TYPE_MAPPING
:
6516 return read_type_endelement_node( reader
);
6518 case WS_ELEMENT_CONTENT_TYPE_MAPPING
:
6519 return read_type_next_node( reader
);
6521 case WS_ATTRIBUTE_TYPE_MAPPING
:
6527 static BOOL
is_true_text( const WS_XML_TEXT
*text
)
6529 switch (text
->textType
)
6531 case WS_XML_TEXT_TYPE_UTF8
:
6533 const WS_XML_UTF8_TEXT
*text_utf8
= (const WS_XML_UTF8_TEXT
*)text
;
6534 if (text_utf8
->value
.length
== 4 && !memcmp( text_utf8
->value
.bytes
, "true", 4 )) return TRUE
;
6537 case WS_XML_TEXT_TYPE_BOOL
:
6539 const WS_XML_BOOL_TEXT
*text_bool
= (const WS_XML_BOOL_TEXT
*)text
;
6540 return text_bool
->value
;
6543 ERR( "unhandled text type %u\n", text
->textType
);
6548 static HRESULT
is_nil_element( const WS_XML_ELEMENT_NODE
*elem
)
6550 static const WS_XML_STRING localname
= {3, (BYTE
*)"nil"};
6551 static const WS_XML_STRING ns
= {41, (BYTE
*)"http://www.w3.org/2001/XMLSchema-instance"};
6554 for (i
= 0; i
< elem
->attributeCount
; i
++)
6556 if (elem
->attributes
[i
]->isXmlNs
) continue;
6557 if (WsXmlStringEquals( elem
->attributes
[i
]->localName
, &localname
, NULL
) == S_OK
&&
6558 WsXmlStringEquals( elem
->attributes
[i
]->ns
, &ns
, NULL
) == S_OK
&&
6559 is_true_text( elem
->attributes
[i
]->value
)) return TRUE
;
6564 static HRESULT
read_type( struct reader
*reader
, WS_TYPE_MAPPING mapping
, WS_TYPE type
,
6565 const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
, const void *desc
,
6566 WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
, ULONG size
, BOOL
*found
)
6570 if ((hr
= start_mapping( reader
, mapping
, localname
, ns
)) != S_OK
) return hr
;
6572 if (mapping
== WS_ELEMENT_TYPE_MAPPING
&& is_nil_element( &reader
->current
->hdr
))
6574 if (option
!= WS_READ_NILLABLE_POINTER
&& option
!= WS_READ_NILLABLE_VALUE
) return WS_E_INVALID_FORMAT
;
6576 return end_mapping( reader
, mapping
);
6582 hr
= read_type_bool( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6586 hr
= read_type_int8( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6590 hr
= read_type_int16( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6594 hr
= read_type_int32( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6598 hr
= read_type_int64( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6602 hr
= read_type_uint8( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6605 case WS_UINT16_TYPE
:
6606 hr
= read_type_uint16( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6609 case WS_UINT32_TYPE
:
6610 hr
= read_type_uint32( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6613 case WS_UINT64_TYPE
:
6614 hr
= read_type_uint64( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6618 hr
= read_type_float( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6621 case WS_DOUBLE_TYPE
:
6622 hr
= read_type_double( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6625 case WS_DATETIME_TYPE
:
6626 hr
= read_type_datetime( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6630 hr
= read_type_guid( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6633 case WS_UNIQUE_ID_TYPE
:
6634 hr
= read_type_unique_id( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6637 case WS_STRING_TYPE
:
6638 hr
= read_type_string( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6642 hr
= read_type_wsz( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6646 hr
= read_type_bytes( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6649 case WS_XML_STRING_TYPE
:
6650 hr
= read_type_xml_string( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6653 case WS_XML_QNAME_TYPE
:
6654 hr
= read_type_qname( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6657 case WS_STRUCT_TYPE
:
6658 hr
= read_type_struct( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6662 hr
= read_type_enum( reader
, mapping
, localname
, ns
, desc
, option
, heap
, value
, size
, found
);
6666 FIXME( "type %u not supported\n", type
);
6670 if (hr
!= S_OK
) return hr
;
6671 return end_mapping( reader
, mapping
);
6674 /**************************************************************************
6675 * WsReadType [webservices.@]
6677 HRESULT WINAPI
WsReadType( WS_XML_READER
*handle
, WS_TYPE_MAPPING mapping
, WS_TYPE type
,
6678 const void *desc
, WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
,
6679 ULONG size
, WS_ERROR
*error
)
6681 struct reader
*reader
= (struct reader
*)handle
;
6685 TRACE( "%p %u %u %p %u %p %p %u %p\n", handle
, mapping
, type
, desc
, option
, heap
, value
,
6687 if (error
) FIXME( "ignoring error parameter\n" );
6689 if (!reader
|| !value
) return E_INVALIDARG
;
6691 EnterCriticalSection( &reader
->cs
);
6693 if (reader
->magic
!= READER_MAGIC
)
6695 LeaveCriticalSection( &reader
->cs
);
6696 return E_INVALIDARG
;
6699 if ((hr
= read_type( reader
, mapping
, type
, NULL
, NULL
, desc
, option
, heap
, value
, size
, &found
)) != S_OK
)
6701 LeaveCriticalSection( &reader
->cs
);
6707 case WS_ELEMENT_TYPE_MAPPING
:
6708 hr
= read_node( reader
);
6715 if (hr
== S_OK
&& !read_end_of_data( reader
)) hr
= WS_E_INVALID_FORMAT
;
6717 LeaveCriticalSection( &reader
->cs
);
6721 HRESULT
read_header( WS_XML_READER
*handle
, const WS_XML_STRING
*localname
, const WS_XML_STRING
*ns
,
6722 WS_TYPE type
, const void *desc
, WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
,
6725 struct reader
*reader
= (struct reader
*)handle
;
6729 EnterCriticalSection( &reader
->cs
);
6731 if (reader
->magic
!= READER_MAGIC
)
6733 LeaveCriticalSection( &reader
->cs
);
6734 return E_INVALIDARG
;
6737 hr
= read_type( reader
, WS_ELEMENT_CONTENT_TYPE_MAPPING
, type
, localname
, ns
, desc
, option
, heap
,
6738 value
, size
, &found
);
6740 LeaveCriticalSection( &reader
->cs
);
6744 /**************************************************************************
6745 * WsReadElement [webservices.@]
6747 HRESULT WINAPI
WsReadElement( WS_XML_READER
*handle
, const WS_ELEMENT_DESCRIPTION
*desc
,
6748 WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
, ULONG size
,
6751 struct reader
*reader
= (struct reader
*)handle
;
6755 TRACE( "%p %p %u %p %p %u %p\n", handle
, desc
, option
, heap
, value
, size
, error
);
6756 if (error
) FIXME( "ignoring error parameter\n" );
6758 if (!reader
|| !desc
|| !value
) return E_INVALIDARG
;
6760 EnterCriticalSection( &reader
->cs
);
6762 if (reader
->magic
!= READER_MAGIC
)
6764 LeaveCriticalSection( &reader
->cs
);
6765 return E_INVALIDARG
;
6768 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->type
, desc
->elementLocalName
,
6769 desc
->elementNs
, desc
->typeDescription
, option
, heap
, value
, size
, &found
);
6771 LeaveCriticalSection( &reader
->cs
);
6775 /**************************************************************************
6776 * WsReadValue [webservices.@]
6778 HRESULT WINAPI
WsReadValue( WS_XML_READER
*handle
, WS_VALUE_TYPE value_type
, void *value
, ULONG size
,
6781 struct reader
*reader
= (struct reader
*)handle
;
6782 WS_TYPE type
= map_value_type( value_type
);
6786 TRACE( "%p %u %p %u %p\n", handle
, type
, value
, size
, error
);
6787 if (error
) FIXME( "ignoring error parameter\n" );
6789 if (!reader
|| !value
|| type
== ~0u) return E_INVALIDARG
;
6791 EnterCriticalSection( &reader
->cs
);
6793 if (reader
->magic
!= READER_MAGIC
)
6795 LeaveCriticalSection( &reader
->cs
);
6796 return E_INVALIDARG
;
6799 hr
= read_type( reader
, WS_ELEMENT_TYPE_MAPPING
, type
, NULL
, NULL
, NULL
, WS_READ_REQUIRED_VALUE
,
6800 NULL
, value
, size
, &found
);
6802 LeaveCriticalSection( &reader
->cs
);
6806 /**************************************************************************
6807 * WsReadAttribute [webservices.@]
6809 HRESULT WINAPI
WsReadAttribute( WS_XML_READER
*handle
, const WS_ATTRIBUTE_DESCRIPTION
*desc
,
6810 WS_READ_OPTION option
, WS_HEAP
*heap
, void *value
, ULONG size
,
6813 struct reader
*reader
= (struct reader
*)handle
;
6817 TRACE( "%p %p %u %p %p %u %p\n", handle
, desc
, option
, heap
, value
, size
, error
);
6818 if (error
) FIXME( "ignoring error parameter\n" );
6820 if (!reader
|| !desc
|| !value
) return E_INVALIDARG
;
6822 EnterCriticalSection( &reader
->cs
);
6824 if (reader
->magic
!= READER_MAGIC
)
6826 LeaveCriticalSection( &reader
->cs
);
6827 return E_INVALIDARG
;
6830 if (!reader
->input_type
)
6832 LeaveCriticalSection( &reader
->cs
);
6833 return WS_E_INVALID_OPERATION
;
6836 hr
= read_type( reader
, WS_ATTRIBUTE_TYPE_MAPPING
, desc
->type
, desc
->attributeLocalName
,
6837 desc
->attributeNs
, desc
->typeDescription
, option
, heap
, value
, size
, &found
);
6839 LeaveCriticalSection( &reader
->cs
);
6843 static inline BOOL
is_utf8( const unsigned char *data
, ULONG size
, ULONG
*offset
)
6845 static const char bom
[] = {0xef,0xbb,0xbf};
6846 const unsigned char *p
= data
;
6848 return (size
>= sizeof(bom
) && !memcmp( p
, bom
, sizeof(bom
) ) && (*offset
= sizeof(bom
))) ||
6849 (size
> 2 && !(*offset
= 0));
6852 static inline BOOL
is_utf16le( const unsigned char *data
, ULONG size
, ULONG
*offset
)
6854 static const char bom
[] = {0xff,0xfe};
6855 const unsigned char *p
= data
;
6857 return (size
>= sizeof(bom
) && !memcmp( p
, bom
, sizeof(bom
) ) && (*offset
= sizeof(bom
))) ||
6858 (size
>= 4 && p
[0] == '<' && !p
[1] && !(*offset
= 0));
6861 static WS_CHARSET
detect_charset( const unsigned char *data
, ULONG size
, ULONG
*offset
)
6865 /* FIXME: parse xml declaration */
6867 if (is_utf16le( data
, size
, offset
)) ret
= WS_CHARSET_UTF16LE
;
6868 else if (is_utf8( data
, size
, offset
)) ret
= WS_CHARSET_UTF8
;
6871 FIXME( "charset not recognized\n" );
6875 TRACE( "detected charset %u\n", ret
);
6879 static void set_input_buffer( struct reader
*reader
, struct xmlbuf
*buf
, const unsigned char *data
, ULONG size
)
6881 reader
->input_type
= WS_XML_READER_INPUT_TYPE_BUFFER
;
6882 reader
->input_buf
= buf
;
6883 reader
->input_data
= data
;
6884 reader
->input_size
= size
;
6886 reader
->read_size
= reader
->input_size
;
6887 reader
->read_pos
= 0;
6888 reader
->read_bufptr
= reader
->input_data
;
6890 reader
->text_conv_offset
= 0;
6893 /**************************************************************************
6894 * WsSetInput [webservices.@]
6896 HRESULT WINAPI
WsSetInput( WS_XML_READER
*handle
, const WS_XML_READER_ENCODING
*encoding
,
6897 const WS_XML_READER_INPUT
*input
, const WS_XML_READER_PROPERTY
*properties
,
6898 ULONG count
, WS_ERROR
*error
)
6900 struct reader
*reader
= (struct reader
*)handle
;
6902 ULONG i
, offset
= 0;
6905 TRACE( "%p %p %p %p %u %p\n", handle
, encoding
, input
, properties
, count
, error
);
6906 if (error
) FIXME( "ignoring error parameter\n" );
6908 if (!reader
) return E_INVALIDARG
;
6910 EnterCriticalSection( &reader
->cs
);
6912 if (reader
->magic
!= READER_MAGIC
)
6914 LeaveCriticalSection( &reader
->cs
);
6915 return E_INVALIDARG
;
6918 for (i
= 0; i
< count
; i
++)
6920 hr
= prop_set( reader
->prop
, reader
->prop_count
, properties
[i
].id
, properties
[i
].value
,
6921 properties
[i
].valueSize
);
6922 if (hr
!= S_OK
) goto done
;
6925 if ((hr
= init_reader( reader
)) != S_OK
) goto done
;
6927 switch (encoding
->encodingType
)
6929 case WS_XML_READER_ENCODING_TYPE_TEXT
:
6931 WS_XML_READER_TEXT_ENCODING
*text
= (WS_XML_READER_TEXT_ENCODING
*)encoding
;
6932 WS_XML_READER_BUFFER_INPUT
*buf
= (WS_XML_READER_BUFFER_INPUT
*)input
;
6934 if (input
->inputType
!= WS_XML_READER_INPUT_TYPE_BUFFER
)
6936 FIXME( "charset detection on input type %u not supported\n", input
->inputType
);
6941 if (text
->charSet
!= WS_CHARSET_AUTO
) reader
->input_charset
= text
->charSet
;
6942 else reader
->input_charset
= detect_charset( buf
->encodedData
, buf
->encodedDataSize
, &offset
);
6944 reader
->input_enc
= WS_XML_READER_ENCODING_TYPE_TEXT
;
6947 case WS_XML_READER_ENCODING_TYPE_BINARY
:
6949 WS_XML_READER_BINARY_ENCODING
*bin
= (WS_XML_READER_BINARY_ENCODING
*)encoding
;
6950 reader
->input_enc
= WS_XML_READER_ENCODING_TYPE_BINARY
;
6951 reader
->input_charset
= 0;
6952 reader
->dict_static
= bin
->staticDictionary
? bin
->staticDictionary
: &dict_builtin_static
.dict
;
6953 reader
->dict
= bin
->dynamicDictionary
? bin
->dynamicDictionary
: &dict_builtin
.dict
;
6957 FIXME( "encoding type %u not supported\n", encoding
->encodingType
);
6962 switch (input
->inputType
)
6964 case WS_XML_READER_INPUT_TYPE_BUFFER
:
6966 WS_XML_READER_BUFFER_INPUT
*buf
= (WS_XML_READER_BUFFER_INPUT
*)input
;
6967 set_input_buffer( reader
, NULL
, (const unsigned char *)buf
->encodedData
+ offset
,
6968 buf
->encodedDataSize
- offset
);
6972 FIXME( "input type %u not supported\n", input
->inputType
);
6977 if (!(node
= alloc_node( WS_XML_NODE_TYPE_BOF
))) hr
= E_OUTOFMEMORY
;
6978 else read_insert_bof( reader
, node
);
6981 LeaveCriticalSection( &reader
->cs
);
6985 /**************************************************************************
6986 * WsSetInputToBuffer [webservices.@]
6988 HRESULT WINAPI
WsSetInputToBuffer( WS_XML_READER
*handle
, WS_XML_BUFFER
*buffer
,
6989 const WS_XML_READER_PROPERTY
*properties
, ULONG count
,
6992 struct reader
*reader
= (struct reader
*)handle
;
6993 struct xmlbuf
*xmlbuf
= (struct xmlbuf
*)buffer
;
6998 TRACE( "%p %p %p %u %p\n", handle
, buffer
, properties
, count
, error
);
6999 if (error
) FIXME( "ignoring error parameter\n" );
7001 if (!reader
|| !xmlbuf
) return E_INVALIDARG
;
7003 EnterCriticalSection( &reader
->cs
);
7005 if (reader
->magic
!= READER_MAGIC
)
7007 LeaveCriticalSection( &reader
->cs
);
7008 return E_INVALIDARG
;
7011 for (i
= 0; i
< count
; i
++)
7013 hr
= prop_set( reader
->prop
, reader
->prop_count
, properties
[i
].id
, properties
[i
].value
,
7014 properties
[i
].valueSize
);
7015 if (hr
!= S_OK
) goto done
;
7018 if ((hr
= init_reader( reader
)) != S_OK
) goto done
;
7020 reader
->input_enc
= xmlbuf
->encoding
;
7021 reader
->input_charset
= xmlbuf
->charset
;
7022 reader
->dict_static
= xmlbuf
->dict_static
;
7023 reader
->dict
= xmlbuf
->dict
;
7024 set_input_buffer( reader
, xmlbuf
, xmlbuf
->bytes
.bytes
, xmlbuf
->bytes
.length
);
7026 if (!(node
= alloc_node( WS_XML_NODE_TYPE_BOF
))) hr
= E_OUTOFMEMORY
;
7027 else read_insert_bof( reader
, node
);
7030 LeaveCriticalSection( &reader
->cs
);
7034 /**************************************************************************
7035 * WsGetReaderPosition [webservices.@]
7037 HRESULT WINAPI
WsGetReaderPosition( WS_XML_READER
*handle
, WS_XML_NODE_POSITION
*pos
, WS_ERROR
*error
)
7039 struct reader
*reader
= (struct reader
*)handle
;
7041 TRACE( "%p %p %p\n", handle
, pos
, error
);
7042 if (error
) FIXME( "ignoring error parameter\n" );
7044 if (!reader
|| !pos
) return E_INVALIDARG
;
7046 EnterCriticalSection( &reader
->cs
);
7048 if (reader
->magic
!= READER_MAGIC
)
7050 LeaveCriticalSection( &reader
->cs
);
7051 return E_INVALIDARG
;
7054 if (!reader
->input_buf
)
7056 LeaveCriticalSection( &reader
->cs
);
7057 return WS_E_INVALID_OPERATION
;
7060 pos
->buffer
= (WS_XML_BUFFER
*)reader
->input_buf
;
7061 pos
->node
= reader
->current
;
7063 LeaveCriticalSection( &reader
->cs
);
7067 /**************************************************************************
7068 * WsSetReaderPosition [webservices.@]
7070 HRESULT WINAPI
WsSetReaderPosition( WS_XML_READER
*handle
, const WS_XML_NODE_POSITION
*pos
, WS_ERROR
*error
)
7072 struct reader
*reader
= (struct reader
*)handle
;
7074 TRACE( "%p %p %p\n", handle
, pos
, error
);
7075 if (error
) FIXME( "ignoring error parameter\n" );
7077 if (!reader
|| !pos
) return E_INVALIDARG
;
7079 EnterCriticalSection( &reader
->cs
);
7081 if (reader
->magic
!= READER_MAGIC
|| (struct xmlbuf
*)pos
->buffer
!= reader
->input_buf
)
7083 LeaveCriticalSection( &reader
->cs
);
7084 return E_INVALIDARG
;
7087 if (!reader
->input_buf
)
7089 LeaveCriticalSection( &reader
->cs
);
7090 return WS_E_INVALID_OPERATION
;
7093 reader
->current
= pos
->node
;
7095 LeaveCriticalSection( &reader
->cs
);
7099 static HRESULT
utf8_to_base64( const WS_XML_UTF8_TEXT
*utf8
, WS_XML_BASE64_TEXT
*base64
)
7101 if (utf8
->value
.length
% 4) return WS_E_INVALID_FORMAT
;
7102 if (!(base64
->bytes
= heap_alloc( utf8
->value
.length
* 3 / 4 ))) return E_OUTOFMEMORY
;
7103 base64
->length
= decode_base64( utf8
->value
.bytes
, utf8
->value
.length
, base64
->bytes
);
7107 /**************************************************************************
7108 * WsReadBytes [webservices.@]
7110 HRESULT WINAPI
WsReadBytes( WS_XML_READER
*handle
, void *bytes
, ULONG max_count
, ULONG
*count
, WS_ERROR
*error
)
7112 struct reader
*reader
= (struct reader
*)handle
;
7115 TRACE( "%p %p %u %p %p\n", handle
, bytes
, max_count
, count
, error
);
7116 if (error
) FIXME( "ignoring error parameter\n" );
7118 if (!reader
) return E_INVALIDARG
;
7120 EnterCriticalSection( &reader
->cs
);
7122 if (reader
->magic
!= READER_MAGIC
)
7124 LeaveCriticalSection( &reader
->cs
);
7125 return E_INVALIDARG
;
7128 if (!reader
->input_type
)
7130 LeaveCriticalSection( &reader
->cs
);
7131 return WS_E_INVALID_OPERATION
;
7136 LeaveCriticalSection( &reader
->cs
);
7137 return E_INVALIDARG
;
7141 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
&& bytes
)
7143 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)reader
->current
;
7144 WS_XML_BASE64_TEXT base64
;
7146 if ((hr
= utf8_to_base64( (const WS_XML_UTF8_TEXT
*)text
->text
, &base64
)) != S_OK
)
7148 LeaveCriticalSection( &reader
->cs
);
7151 if (reader
->text_conv_offset
== base64
.length
)
7153 heap_free( base64
.bytes
);
7154 hr
= read_node( reader
);
7155 LeaveCriticalSection( &reader
->cs
);
7158 *count
= min( base64
.length
- reader
->text_conv_offset
, max_count
);
7159 memcpy( bytes
, base64
.bytes
+ reader
->text_conv_offset
, *count
);
7160 reader
->text_conv_offset
+= *count
;
7161 heap_free( base64
.bytes
);
7164 LeaveCriticalSection( &reader
->cs
);
7168 static HRESULT
utf8_to_utf16( const WS_XML_UTF8_TEXT
*utf8
, WS_XML_UTF16_TEXT
*utf16
)
7170 int len
= MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, NULL
, 0 );
7171 if (!(utf16
->bytes
= heap_alloc( len
* sizeof(WCHAR
) ))) return E_OUTOFMEMORY
;
7172 MultiByteToWideChar( CP_UTF8
, 0, (char *)utf8
->value
.bytes
, utf8
->value
.length
, (WCHAR
*)utf16
->bytes
, len
);
7173 utf16
->byteCount
= len
* sizeof(WCHAR
);
7177 /**************************************************************************
7178 * WsReadChars [webservices.@]
7180 HRESULT WINAPI
WsReadChars( WS_XML_READER
*handle
, WCHAR
*chars
, ULONG max_count
, ULONG
*count
, WS_ERROR
*error
)
7182 struct reader
*reader
= (struct reader
*)handle
;
7184 TRACE( "%p %p %u %p %p\n", handle
, chars
, max_count
, count
, error
);
7185 if (error
) FIXME( "ignoring error parameter\n" );
7187 if (!reader
) return E_INVALIDARG
;
7189 EnterCriticalSection( &reader
->cs
);
7191 if (reader
->magic
!= READER_MAGIC
)
7193 LeaveCriticalSection( &reader
->cs
);
7194 return E_INVALIDARG
;
7197 if (!reader
->input_type
)
7199 LeaveCriticalSection( &reader
->cs
);
7200 return WS_E_INVALID_OPERATION
;
7205 LeaveCriticalSection( &reader
->cs
);
7206 return E_INVALIDARG
;
7210 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
&& chars
)
7212 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)reader
->current
;
7213 WS_XML_UTF16_TEXT utf16
;
7216 if ((hr
= utf8_to_utf16( (const WS_XML_UTF8_TEXT
*)text
->text
, &utf16
)) != S_OK
)
7218 LeaveCriticalSection( &reader
->cs
);
7221 if (reader
->text_conv_offset
== utf16
.byteCount
/ sizeof(WCHAR
))
7223 heap_free( utf16
.bytes
);
7224 hr
= read_node( reader
);
7225 LeaveCriticalSection( &reader
->cs
);
7228 *count
= min( utf16
.byteCount
/ sizeof(WCHAR
) - reader
->text_conv_offset
, max_count
);
7229 memcpy( chars
, utf16
.bytes
+ reader
->text_conv_offset
* sizeof(WCHAR
), *count
* sizeof(WCHAR
) );
7230 reader
->text_conv_offset
+= *count
;
7231 heap_free( utf16
.bytes
);
7234 LeaveCriticalSection( &reader
->cs
);
7238 /**************************************************************************
7239 * WsReadCharsUtf8 [webservices.@]
7241 HRESULT WINAPI
WsReadCharsUtf8( WS_XML_READER
*handle
, BYTE
*bytes
, ULONG max_count
, ULONG
*count
, WS_ERROR
*error
)
7243 struct reader
*reader
= (struct reader
*)handle
;
7246 TRACE( "%p %p %u %p %p\n", handle
, bytes
, max_count
, count
, error
);
7247 if (error
) FIXME( "ignoring error parameter\n" );
7249 if (!reader
) return E_INVALIDARG
;
7251 EnterCriticalSection( &reader
->cs
);
7253 if (reader
->magic
!= READER_MAGIC
)
7255 LeaveCriticalSection( &reader
->cs
);
7256 return E_INVALIDARG
;
7259 if (!reader
->input_type
)
7261 LeaveCriticalSection( &reader
->cs
);
7262 return WS_E_INVALID_OPERATION
;
7267 LeaveCriticalSection( &reader
->cs
);
7268 return E_INVALIDARG
;
7272 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_TEXT
&& bytes
)
7274 const WS_XML_TEXT_NODE
*text
= (const WS_XML_TEXT_NODE
*)reader
->current
;
7275 const WS_XML_UTF8_TEXT
*utf8
= (const WS_XML_UTF8_TEXT
*)text
->text
;
7277 if (reader
->text_conv_offset
== utf8
->value
.length
)
7279 hr
= read_node( reader
);
7280 LeaveCriticalSection( &reader
->cs
);
7283 *count
= min( utf8
->value
.length
- reader
->text_conv_offset
, max_count
);
7284 memcpy( bytes
, utf8
->value
.bytes
+ reader
->text_conv_offset
, *count
);
7285 reader
->text_conv_offset
+= *count
;
7288 LeaveCriticalSection( &reader
->cs
);
7292 static HRESULT
move_to_element( struct reader
*reader
)
7295 if (node_type( reader
->current
) == WS_XML_NODE_TYPE_BOF
&&
7296 (hr
= read_move_to( reader
, WS_MOVE_TO_CHILD_NODE
, NULL
)) != S_OK
) return hr
;
7297 if (node_type( reader
->current
) != WS_XML_NODE_TYPE_ELEMENT
) return E_FAIL
;
7301 static HRESULT
copy_tree( struct reader
*reader
, WS_XML_WRITER
*writer
)
7303 const struct node
*node
, *parent
;
7307 if ((hr
= move_to_element( reader
)) != S_OK
) return hr
;
7308 parent
= reader
->current
;
7311 node
= reader
->current
;
7312 if ((hr
= WsWriteNode( writer
, (const WS_XML_NODE
*)node
, NULL
)) != S_OK
) break;
7313 if (node_type( node
) == WS_XML_NODE_TYPE_END_ELEMENT
&& node
->parent
== parent
) done
= TRUE
;
7314 if ((hr
= read_next_node( reader
)) != S_OK
|| done
) break;
7319 /**************************************************************************
7320 * WsReadXmlBuffer [webservices.@]
7322 HRESULT WINAPI
WsReadXmlBuffer( WS_XML_READER
*handle
, WS_HEAP
*heap
, WS_XML_BUFFER
**ret
, WS_ERROR
*error
)
7324 struct reader
*reader
= (struct reader
*)handle
;
7325 WS_XML_WRITER
*writer
= NULL
;
7326 WS_XML_BUFFER
*buffer
;
7329 TRACE( "%p %p %p %p\n", handle
, heap
, ret
, error
);
7330 if (error
) FIXME( "ignoring error parameter\n" );
7332 if (!reader
|| !heap
) return E_INVALIDARG
;
7333 if (!ret
) return E_FAIL
;
7335 EnterCriticalSection( &reader
->cs
);
7337 if (reader
->magic
!= READER_MAGIC
)
7339 LeaveCriticalSection( &reader
->cs
);
7340 return E_INVALIDARG
;
7343 if (!reader
->input_type
)
7345 LeaveCriticalSection( &reader
->cs
);
7346 return WS_E_INVALID_OPERATION
;
7349 if ((hr
= WsCreateWriter( NULL
, 0, &writer
, NULL
)) != S_OK
) goto done
;
7350 if ((hr
= WsCreateXmlBuffer( heap
, NULL
, 0, &buffer
, NULL
)) != S_OK
) goto done
;
7351 if ((hr
= WsSetOutputToBuffer( writer
, buffer
, NULL
, 0, NULL
)) != S_OK
) goto done
;
7352 if ((hr
= copy_tree( reader
, writer
)) == S_OK
) *ret
= buffer
;
7355 if (hr
!= S_OK
) free_xmlbuf( (struct xmlbuf
*)buffer
);
7356 WsFreeWriter( writer
);
7357 LeaveCriticalSection( &reader
->cs
);
7361 HRESULT
create_header_buffer( WS_XML_READER
*handle
, WS_HEAP
*heap
, WS_XML_BUFFER
**ret
)
7363 struct reader
*reader
= (struct reader
*)handle
;
7364 HRESULT hr
= WS_E_QUOTA_EXCEEDED
;
7365 struct xmlbuf
*xmlbuf
;
7367 EnterCriticalSection( &reader
->cs
);
7369 if (reader
->magic
!= READER_MAGIC
)
7371 LeaveCriticalSection( &reader
->cs
);
7372 return E_INVALIDARG
;
7375 if ((xmlbuf
= alloc_xmlbuf( heap
, reader
->read_pos
, reader
->input_enc
, reader
->input_charset
,
7376 reader
->dict_static
, reader
->dict
)))
7378 memcpy( xmlbuf
->bytes
.bytes
, reader
->read_bufptr
, reader
->read_pos
);
7379 xmlbuf
->bytes
.length
= reader
->read_pos
;
7380 *ret
= (WS_XML_BUFFER
*)xmlbuf
;
7384 LeaveCriticalSection( &reader
->cs
);
7388 HRESULT
get_param_desc( const WS_STRUCT_DESCRIPTION
*desc
, USHORT index
, const WS_FIELD_DESCRIPTION
**ret
)
7390 if (index
>= desc
->fieldCount
) return E_INVALIDARG
;
7391 *ret
= desc
->fields
[index
];
7395 static ULONG
get_field_size( const WS_FIELD_DESCRIPTION
*desc
)
7397 if (desc
->options
& WS_FIELD_POINTER
) return sizeof(void *);
7398 return get_type_size( desc
->type
, desc
->typeDescription
);
7401 static HRESULT
read_param( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
, void *ret
)
7403 if (!ret
&& !(ret
= ws_alloc_zero( heap
, get_field_size(desc
) ))) return WS_E_QUOTA_EXCEEDED
;
7404 return read_type_field( reader
, desc
, heap
, ret
, 0 );
7407 static HRESULT
read_param_array( struct reader
*reader
, const WS_FIELD_DESCRIPTION
*desc
, WS_HEAP
*heap
,
7408 void **ret
, ULONG
*count
)
7410 if (!ret
&& !(ret
= ws_alloc_zero( heap
, sizeof(void **) ))) return WS_E_QUOTA_EXCEEDED
;
7411 return read_type_array( reader
, desc
, heap
, ret
, count
);
7414 static void set_array_len( const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, ULONG index
, ULONG len
,
7418 for (i
= 0; i
< count
; i
++)
7420 if (params
[i
].outputMessageIndex
!= index
|| params
[i
].parameterType
!= WS_PARAMETER_TYPE_ARRAY_COUNT
)
7422 if ((ptr
= *(ULONG
**)args
[i
])) *ptr
= len
;
7427 HRESULT
read_output_params( WS_XML_READER
*handle
, WS_HEAP
*heap
, const WS_ELEMENT_DESCRIPTION
*desc
,
7428 const WS_PARAMETER_DESCRIPTION
*params
, ULONG count
, const void **args
)
7430 struct reader
*reader
= (struct reader
*)handle
;
7431 const WS_STRUCT_DESCRIPTION
*desc_struct
;
7432 const WS_FIELD_DESCRIPTION
*desc_field
;
7436 if (desc
->type
!= WS_STRUCT_TYPE
|| !(desc_struct
= desc
->typeDescription
)) return E_INVALIDARG
;
7438 EnterCriticalSection( &reader
->cs
);
7440 if (reader
->magic
!= READER_MAGIC
)
7442 LeaveCriticalSection( &reader
->cs
);
7443 return E_INVALIDARG
;
7446 if ((hr
= start_mapping( reader
, WS_ELEMENT_TYPE_MAPPING
, desc
->elementLocalName
, desc
->elementNs
)) != S_OK
)
7449 for (i
= 0; i
< count
; i
++)
7451 if (params
[i
].outputMessageIndex
== INVALID_PARAMETER_INDEX
) continue;
7452 if (params
[i
].parameterType
== WS_PARAMETER_TYPE_MESSAGES
)
7454 FIXME( "messages type not supported\n" );
7458 if ((hr
= get_param_desc( desc_struct
, params
[i
].outputMessageIndex
, &desc_field
)) != S_OK
) goto done
;
7459 if (params
[i
].parameterType
== WS_PARAMETER_TYPE_NORMAL
)
7461 void *ptr
= *(void **)args
[i
];
7462 if ((hr
= read_param( reader
, desc_field
, heap
, ptr
)) != S_OK
) goto done
;
7464 else if (params
[i
].parameterType
== WS_PARAMETER_TYPE_ARRAY
)
7466 void **ptr
= *(void ***)args
[i
];
7467 if ((hr
= read_param_array( reader
, desc_field
, heap
, ptr
, &len
)) != S_OK
) goto done
;
7468 set_array_len( params
, count
, params
[i
].outputMessageIndex
, len
, args
);
7472 if (desc_struct
->structOptions
& WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT
)
7474 struct node
*parent
= find_parent( reader
);
7475 parent
->flags
|= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT
;
7478 hr
= end_mapping( reader
, WS_ELEMENT_TYPE_MAPPING
);
7481 LeaveCriticalSection( &reader
->cs
);