reader.c 223 KB
Newer Older
1
/*
2
 * Copyright 2015, 2016 Hans Leidekker for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
20
#include <assert.h>
21
#include <float.h>
22
#include <locale.h>
23 24 25 26 27 28 29

#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "webservices.h"

#include "wine/debug.h"
30
#include "wine/heap.h"
31
#include "wine/list.h"
32
#include "webservices_private.h"
33 34 35

WINE_DEFAULT_DEBUG_CHANNEL(webservices);

36
ULONG prop_size( const struct prop_desc *desc, ULONG count )
37
{
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    ULONG i, ret = count * sizeof(struct prop);
    for (i = 0; i < count; i++) ret += desc[i].size;
    return ret;
}

void prop_init( const struct prop_desc *desc, ULONG count, struct prop *prop, void *data )
{
    ULONG i;
    char *ptr = data;
    for (i = 0; i < count; i++)
    {
        prop[i].value     = ptr;
        prop[i].size      = desc[i].size;
        prop[i].readonly  = desc[i].readonly;
        prop[i].writeonly = desc[i].writeonly;
        ptr += prop[i].size;
    }
55
}
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

HRESULT prop_set( const struct prop *prop, ULONG count, ULONG id, const void *value, ULONG size )
{
    if (id >= count || size != prop[id].size || prop[id].readonly) return E_INVALIDARG;
    memcpy( prop[id].value, value, size );
    return S_OK;
}

HRESULT prop_get( const struct prop *prop, ULONG count, ULONG id, void *buf, ULONG size )
{
    if (id >= count || size != prop[id].size || prop[id].writeonly) return E_INVALIDARG;
    memcpy( buf, prop[id].value, prop[id].size );
    return S_OK;
}

71
struct node *alloc_node( WS_XML_NODE_TYPE type )
72 73 74 75 76 77
{
    struct node *ret;

    if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
    ret->hdr.node.nodeType = type;
    list_init( &ret->entry );
78
    list_init( &ret->children );
79 80 81
    return ret;
}

82
void free_attribute( WS_XML_ATTRIBUTE *attr )
83 84
{
    if (!attr) return;
85 86 87
    free_xml_string( attr->prefix );
    free_xml_string( attr->localName );
    free_xml_string( attr->ns );
88 89 90 91
    heap_free( attr->value );
    heap_free( attr );
}

92
void free_node( struct node *node )
93 94
{
    if (!node) return;
95
    switch (node_type( node ))
96 97 98
    {
    case WS_XML_NODE_TYPE_ELEMENT:
    {
99
        WS_XML_ELEMENT_NODE *elem = &node->hdr;
100 101 102 103
        ULONG i;

        for (i = 0; i < elem->attributeCount; i++) free_attribute( elem->attributes[i] );
        heap_free( elem->attributes );
104 105 106
        free_xml_string( elem->prefix );
        free_xml_string( elem->localName );
        free_xml_string( elem->ns );
107 108 109 110 111 112 113 114
        break;
    }
    case WS_XML_NODE_TYPE_TEXT:
    {
        WS_XML_TEXT_NODE *text = (WS_XML_TEXT_NODE *)node;
        heap_free( text->text );
        break;
    }
115 116 117 118
    case WS_XML_NODE_TYPE_COMMENT:
    {
        WS_XML_COMMENT_NODE *comment = (WS_XML_COMMENT_NODE *)node;
        heap_free( comment->value.bytes );
119
        break;
120
    }
121 122
    case WS_XML_NODE_TYPE_CDATA:
    case WS_XML_NODE_TYPE_END_CDATA:
123 124 125 126 127 128
    case WS_XML_NODE_TYPE_END_ELEMENT:
    case WS_XML_NODE_TYPE_EOF:
    case WS_XML_NODE_TYPE_BOF:
        break;

    default:
129
        ERR( "unhandled type %u\n", node_type( node ) );
130 131 132 133 134
        break;
    }
    heap_free( node );
}

135
void destroy_nodes( struct node *node )
136 137 138
{
    struct list *ptr;

139 140
    if (!node) return;
    while ((ptr = list_head( &node->children )))
141
    {
142 143 144
        struct node *child = LIST_ENTRY( ptr, struct node, entry );
        list_remove( &child->entry );
        destroy_nodes( child );
145
    }
146
    free_node( node );
147 148
}

149
static WS_XML_ATTRIBUTE *dup_attribute( const WS_XML_ATTRIBUTE *src, WS_XML_WRITER_ENCODING_TYPE enc )
150 151
{
    WS_XML_ATTRIBUTE *dst;
152
    HRESULT hr;
153

154
    if (!(dst = heap_alloc_zero( sizeof(*dst) ))) return NULL;
155 156
    dst->singleQuote = src->singleQuote;
    dst->isXmlNs     = src->isXmlNs;
157

158 159 160
    if (src->prefix && !(dst->prefix = dup_xml_string( src->prefix, FALSE ))) goto error;
    if (src->localName && !(dst->localName = dup_xml_string( src->localName, FALSE ))) goto error;
    if (src->ns && !(dst->ns = dup_xml_string( src->ns, FALSE ))) goto error;
161

162
    if (src->value)
163
    {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        switch (enc)
        {
        case WS_XML_WRITER_ENCODING_TYPE_BINARY:
            if ((hr = text_to_text( src->value, NULL, NULL, &dst->value )) != S_OK) goto error;
            break;

        case WS_XML_WRITER_ENCODING_TYPE_TEXT:
            if ((hr = text_to_utf8text( src->value, NULL, NULL, (WS_XML_UTF8_TEXT **)&dst->value )) != S_OK)
                goto error;
            break;

        default:
            ERR( "unhandled encoding %u\n", enc );
            goto error;
        }
179 180
    }

181 182 183 184 185 186 187
    return dst;

error:
    free_attribute( dst );
    return NULL;
}

188 189
static WS_XML_ATTRIBUTE **dup_attributes( WS_XML_ATTRIBUTE * const *src, ULONG count,
                                          WS_XML_WRITER_ENCODING_TYPE enc )
190 191 192 193 194 195 196
{
    WS_XML_ATTRIBUTE **dst;
    ULONG i;

    if (!(dst = heap_alloc( sizeof(*dst) * count ))) return NULL;
    for (i = 0; i < count; i++)
    {
197
        if (!(dst[i] = dup_attribute( src[i], enc )))
198 199 200 201 202 203 204 205 206
        {
            for (; i > 0; i--) free_attribute( dst[i - 1] );
            heap_free( dst );
            return NULL;
        }
    }
    return dst;
}

207
static struct node *dup_element_node( const WS_XML_ELEMENT_NODE *src, WS_XML_WRITER_ENCODING_TYPE enc )
208 209 210 211 212 213 214 215 216 217 218 219
{
    struct node *node;
    WS_XML_ELEMENT_NODE *dst;
    ULONG count = src->attributeCount;
    WS_XML_ATTRIBUTE **attrs = src->attributes;
    const WS_XML_STRING *prefix = (src->prefix && src->prefix->length) ? src->prefix : NULL;
    const WS_XML_STRING *localname = src->localName;
    const WS_XML_STRING *ns = src->ns;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_ELEMENT ))) return NULL;
    dst = &node->hdr;

220
    if (count && !(dst->attributes = dup_attributes( attrs, count, enc ))) goto error;
221 222
    dst->attributeCount = count;

223 224 225
    if (prefix && !(dst->prefix = dup_xml_string( prefix, FALSE ))) goto error;
    if (localname && !(dst->localName = dup_xml_string( localname, FALSE ))) goto error;
    if (ns && !(dst->ns = dup_xml_string( ns, FALSE ))) goto error;
226 227 228 229 230 231 232
    return node;

error:
    free_node( node );
    return NULL;
}

233
static struct node *dup_text_node( const WS_XML_TEXT_NODE *src, WS_XML_WRITER_ENCODING_TYPE enc )
234 235 236
{
    struct node *node;
    WS_XML_TEXT_NODE *dst;
237
    HRESULT hr;
238 239 240

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    dst = (WS_XML_TEXT_NODE *)node;
241
    if (!src->text) return node;
242

243
    switch (enc)
244
    {
245 246 247 248 249 250 251 252 253 254 255 256
    case WS_XML_WRITER_ENCODING_TYPE_BINARY:
        hr = text_to_text( src->text, NULL, NULL, &dst->text );
        break;

    case WS_XML_WRITER_ENCODING_TYPE_TEXT:
        hr = text_to_utf8text( src->text, NULL, NULL, (WS_XML_UTF8_TEXT **)&dst->text );
        break;

    default:
        ERR( "unhandled encoding %u\n", enc );
        free_node( node );
        return NULL;
257
    }
258 259 260 261 262 263 264

    if (hr != S_OK)
    {
        free_node( node );
        return NULL;
    }

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    return node;
}

static struct node *dup_comment_node( const WS_XML_COMMENT_NODE *src )
{
    struct node *node;
    WS_XML_COMMENT_NODE *dst;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return NULL;
    dst = (WS_XML_COMMENT_NODE *)node;

    if (src->value.length && !(dst->value.bytes = heap_alloc( src->value.length )))
    {
        free_node( node );
        return NULL;
    }
    memcpy( dst->value.bytes, src->value.bytes, src->value.length );
    dst->value.length = src->value.length;
    return node;
}

286
static struct node *dup_node( const struct node *src, WS_XML_WRITER_ENCODING_TYPE enc )
287 288 289 290
{
    switch (node_type( src ))
    {
    case WS_XML_NODE_TYPE_ELEMENT:
291
        return dup_element_node( &src->hdr, enc );
292 293

    case WS_XML_NODE_TYPE_TEXT:
294
        return dup_text_node( (const WS_XML_TEXT_NODE *)src, enc );
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

    case WS_XML_NODE_TYPE_COMMENT:
        return dup_comment_node( (const WS_XML_COMMENT_NODE *)src );

    case WS_XML_NODE_TYPE_CDATA:
    case WS_XML_NODE_TYPE_END_CDATA:
    case WS_XML_NODE_TYPE_END_ELEMENT:
    case WS_XML_NODE_TYPE_EOF:
    case WS_XML_NODE_TYPE_BOF:
        return alloc_node( node_type( src ) );

    default:
        ERR( "unhandled type %u\n", node_type( src ) );
        break;
    }
    return NULL;
}

313
static HRESULT dup_tree( const struct node *src, WS_XML_WRITER_ENCODING_TYPE enc, struct node **dst )
314 315 316 317
{
    struct node *parent;
    const struct node *child;

318
    if (!*dst && !(*dst = dup_node( src, enc ))) return E_OUTOFMEMORY;
319 320 321 322 323 324 325
    parent = *dst;

    LIST_FOR_EACH_ENTRY( child, &src->children, struct node, entry )
    {
        HRESULT hr = E_OUTOFMEMORY;
        struct node *new_child;

326
        if (!(new_child = dup_node( child, enc )) || (hr = dup_tree( child, enc, &new_child )) != S_OK)
327 328 329 330 331 332 333 334 335 336
        {
            destroy_nodes( *dst );
            return hr;
        }
        new_child->parent = parent;
        list_add_tail( &parent->children, &new_child->entry );
    }
    return S_OK;
}

337
static const struct prop_desc reader_props[] =
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
{
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_MAX_DEPTH */
    { sizeof(BOOL), FALSE },       /* WS_XML_READER_PROPERTY_ALLOW_FRAGMENT */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_MAX_ATTRIBUTES */
    { sizeof(BOOL), FALSE },       /* WS_XML_READER_PROPERTY_READ_DECLARATION */
    { sizeof(WS_CHARSET), FALSE }, /* WS_XML_READER_PROPERTY_CHARSET */
    { sizeof(ULONGLONG), TRUE },   /* WS_XML_READER_PROPERTY_ROW */
    { sizeof(ULONGLONG), TRUE },   /* WS_XML_READER_PROPERTY_COLUMN */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_UTF8_TRIM_SIZE */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_STREAM_BUFFER_SIZE */
    { sizeof(BOOL), TRUE },        /* WS_XML_READER_PROPERTY_IN_ATTRIBUTE */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_STREAM_MAX_ROOT_MIME_PART_SIZE */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_STREAM_MAX_MIME_HEADERS_SIZE */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_MAX_MIME_PARTS */
    { sizeof(BOOL), FALSE },       /* WS_XML_READER_PROPERTY_ALLOW_INVALID_CHARACTER_REFERENCES */
    { sizeof(ULONG), FALSE },      /* WS_XML_READER_PROPERTY_MAX_NAMESPACES */
};

356 357 358 359 360
enum reader_state
{
    READER_STATE_INITIAL,
    READER_STATE_BOF,
    READER_STATE_STARTELEMENT,
361
    READER_STATE_STARTATTRIBUTE,
362 363
    READER_STATE_STARTCDATA,
    READER_STATE_CDATA,
364 365
    READER_STATE_TEXT,
    READER_STATE_ENDELEMENT,
366
    READER_STATE_ENDCDATA,
367
    READER_STATE_COMMENT,
368 369 370
    READER_STATE_EOF
};

371 372
struct prefix
{
373 374
    WS_XML_STRING *str;
    WS_XML_STRING *ns;
375 376
};

377 378
struct reader
{
379 380 381 382 383 384 385 386 387 388 389 390 391 392
    ULONG                        magic;
    CRITICAL_SECTION             cs;
    ULONG                        read_size;
    ULONG                        read_pos;
    const unsigned char         *read_bufptr;
    enum reader_state            state;
    struct node                 *root;
    struct node                 *current;
    ULONG                        current_attr;
    struct node                 *last;
    struct prefix               *prefixes;
    ULONG                        nb_prefixes;
    ULONG                        nb_prefixes_allocated;
    WS_XML_READER_ENCODING_TYPE  input_enc;
393
    WS_CHARSET                   input_charset;
394
    WS_XML_READER_INPUT_TYPE     input_type;
395 396
    WS_READ_CALLBACK             input_cb;
    void                        *input_cb_state;
397 398 399 400
    struct xmlbuf               *input_buf;
    const unsigned char         *input_data;
    ULONG                        input_size;
    ULONG                        text_conv_offset;
401
    unsigned char               *stream_buf;
402
    const WS_XML_DICTIONARY     *dict_static;
403
    WS_XML_DICTIONARY           *dict;
404
    ULONG                        prop_count;
405
    struct prop                  prop[ARRAY_SIZE( reader_props )];
406 407
};

408 409
#define READER_MAGIC (('R' << 24) | ('E' << 16) | ('A' << 8) | 'D')

410 411
static struct reader *alloc_reader(void)
{
412
    static const ULONG count = ARRAY_SIZE( reader_props );
413
    struct reader *ret;
414
    ULONG size = sizeof(*ret) + prop_size( reader_props, count );
415 416

    if (!(ret = heap_alloc_zero( size ))) return NULL;
417 418 419 420 421 422 423
    if (!(ret->prefixes = heap_alloc_zero( sizeof(*ret->prefixes) )))
    {
        heap_free( ret );
        return NULL;
    }
    ret->nb_prefixes = ret->nb_prefixes_allocated = 1;

424 425 426 427
    ret->magic       = READER_MAGIC;
    InitializeCriticalSection( &ret->cs );
    ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": reader.cs");

428
    prop_init( reader_props, count, ret->prop, &ret[1] );
429
    ret->prop_count  = count;
430 431 432
    return ret;
}

433 434 435 436 437
static void clear_prefixes( struct prefix *prefixes, ULONG count )
{
    ULONG i;
    for (i = 0; i < count; i++)
    {
438 439 440 441
        free_xml_string( prefixes[i].str );
        prefixes[i].str = NULL;
        free_xml_string( prefixes[i].ns );
        prefixes[i].ns  = NULL;
442 443 444 445 446 447 448
    }
}

static HRESULT set_prefix( struct prefix *prefix, const WS_XML_STRING *str, const WS_XML_STRING *ns )
{
    if (str)
    {
449
        free_xml_string( prefix->str );
450
        if (!(prefix->str = dup_xml_string( str, FALSE ))) return E_OUTOFMEMORY;
451
    }
452
    if (prefix->ns) free_xml_string( prefix->ns );
453
    if (!(prefix->ns = dup_xml_string( ns, FALSE ))) return E_OUTOFMEMORY;
454 455 456 457 458 459 460 461 462 463
    return S_OK;
}

static HRESULT bind_prefix( struct reader *reader, const WS_XML_STRING *prefix, const WS_XML_STRING *ns )
{
    ULONG i;
    HRESULT hr;

    for (i = 0; i < reader->nb_prefixes; i++)
    {
464
        if (WsXmlStringEquals( prefix, reader->prefixes[i].str, NULL ) == S_OK)
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
            return set_prefix( &reader->prefixes[i], NULL, ns );
    }
    if (i >= reader->nb_prefixes_allocated)
    {
        ULONG new_size = reader->nb_prefixes_allocated * sizeof(*reader->prefixes) * 2;
        struct prefix *tmp = heap_realloc_zero( reader->prefixes, new_size  );
        if (!tmp) return E_OUTOFMEMORY;
        reader->prefixes = tmp;
        reader->nb_prefixes_allocated *= 2;
    }
    if ((hr = set_prefix( &reader->prefixes[i], prefix, ns )) != S_OK) return hr;
    reader->nb_prefixes++;
    return S_OK;
}

static const WS_XML_STRING *get_namespace( struct reader *reader, const WS_XML_STRING *prefix )
{
    ULONG i;
    for (i = 0; i < reader->nb_prefixes; i++)
    {
485 486
        if (WsXmlStringEquals( prefix, reader->prefixes[i].str, NULL ) == S_OK)
            return reader->prefixes[i].ns;
487 488 489 490
    }
    return NULL;
}

491 492 493 494 495 496 497 498
static void read_insert_eof( struct reader *reader, struct node *eof )
{
    if (!reader->root) reader->root = eof;
    else
    {
        eof->parent = reader->root;
        list_add_tail( &reader->root->children, &eof->entry );
    }
499
    reader->current = reader->last = eof;
500 501 502 503 504 505
}

static void read_insert_bof( struct reader *reader, struct node *bof )
{
    reader->root->parent = bof;
    list_add_tail( &bof->children, &reader->root->entry );
506
    reader->current = reader->last = reader->root = bof;
507 508 509 510 511
}

static void read_insert_node( struct reader *reader, struct node *parent, struct node *node )
{
    node->parent = parent;
512
    list_add_before( list_tail( &parent->children ), &node->entry );
513
    reader->current = reader->last = node;
514 515
}

516 517 518 519 520
static void free_reader( struct reader *reader )
{
    destroy_nodes( reader->root );
    clear_prefixes( reader->prefixes, reader->nb_prefixes );
    heap_free( reader->prefixes );
521
    heap_free( reader->stream_buf );
522

523 524 525 526 527 528
    reader->cs.DebugInfo->Spare[0] = 0;
    DeleteCriticalSection( &reader->cs );
    heap_free( reader );
}

static HRESULT init_reader( struct reader *reader )
529
{
530
    static const WS_XML_STRING empty = {0, NULL};
531
    struct node *node;
532
    HRESULT hr;
533

534
    reader->state        = READER_STATE_INITIAL;
535
    destroy_nodes( reader->root );
536 537
    reader->root         = reader->current = NULL;
    reader->current_attr = 0;
538
    clear_prefixes( reader->prefixes, reader->nb_prefixes );
539
    reader->nb_prefixes  = 1;
540 541
    if ((hr = bind_prefix( reader, &empty, &empty )) != S_OK) return hr;

542
    if (!(node = alloc_node( WS_XML_NODE_TYPE_EOF ))) return E_OUTOFMEMORY;
543
    read_insert_eof( reader, node );
544 545 546 547
    reader->input_enc     = WS_XML_READER_ENCODING_TYPE_TEXT;
    reader->input_charset = WS_CHARSET_UTF8;
    reader->dict_static   = NULL;
    reader->dict          = NULL;
548 549 550
    return S_OK;
}

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
/**************************************************************************
 *          WsCreateReader		[webservices.@]
 */
HRESULT WINAPI WsCreateReader( const WS_XML_READER_PROPERTY *properties, ULONG count,
                               WS_XML_READER **handle, WS_ERROR *error )
{
    struct reader *reader;
    ULONG i, max_depth = 32, max_attrs = 128, max_ns = 32;
    BOOL read_decl = TRUE;
    HRESULT hr;

    TRACE( "%p %u %p %p\n", properties, count, handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!handle) return E_INVALIDARG;
    if (!(reader = alloc_reader())) return E_OUTOFMEMORY;

568 569 570 571
    prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_DEPTH, &max_depth, sizeof(max_depth) );
    prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_ATTRIBUTES, &max_attrs, sizeof(max_attrs) );
    prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_READ_DECLARATION, &read_decl, sizeof(read_decl) );
    prop_set( reader->prop, reader->prop_count, WS_XML_READER_PROPERTY_MAX_NAMESPACES, &max_ns, sizeof(max_ns) );
572 573 574

    for (i = 0; i < count; i++)
    {
575 576
        hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
                       properties[i].valueSize );
577 578
        if (hr != S_OK)
        {
579
            free_reader( reader );
580 581 582 583
            return hr;
        }
    }

584
    if ((hr = init_reader( reader )) != S_OK)
585
    {
586
        free_reader( reader );
587
        return hr;
588 589
    }

590
    TRACE( "created %p\n", reader );
591 592 593 594 595 596 597 598 599 600 601 602
    *handle = (WS_XML_READER *)reader;
    return S_OK;
}

/**************************************************************************
 *          WsFreeReader		[webservices.@]
 */
void WINAPI WsFreeReader( WS_XML_READER *handle )
{
    struct reader *reader = (struct reader *)handle;

    TRACE( "%p\n", handle );
603 604 605 606 607 608 609 610 611 612 613 614 615 616

    if (!reader) return;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return;
    }

    reader->magic = 0;

    LeaveCriticalSection( &reader->cs );
617
    free_reader( reader );
618 619
}

620 621 622 623 624 625 626 627 628
static HRESULT read_more_data( struct reader *reader, ULONG min_size, const WS_ASYNC_CONTEXT *ctx,
                               WS_ERROR *error )
{
    ULONG size = 0, max_size;

    if (reader->read_size - reader->read_pos >= min_size) return S_OK;
    if (reader->input_type != WS_XML_READER_INPUT_TYPE_STREAM) return WS_E_INVALID_FORMAT;
    if (min_size > reader->input_size) return WS_E_QUOTA_EXCEEDED;

629
    if (reader->read_pos)
630 631 632 633 634 635 636 637 638 639 640 641 642
    {
        memmove( reader->stream_buf, reader->stream_buf + reader->read_pos, reader->read_size - reader->read_pos );
        reader->read_size -= reader->read_pos;
        reader->read_pos = 0;
    }
    max_size = reader->input_size - reader->read_size;

    reader->input_cb( reader->input_cb_state, reader->stream_buf + reader->read_size, max_size, &size, ctx, error );
    if (size < min_size) return WS_E_QUOTA_EXCEEDED;
    reader->read_size += size;
    return S_OK;
}

643 644 645 646 647 648 649
/**************************************************************************
 *          WsFillReader		[webservices.@]
 */
HRESULT WINAPI WsFillReader( WS_XML_READER *handle, ULONG min_size, const WS_ASYNC_CONTEXT *ctx,
                             WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
650
    HRESULT hr;
651 652 653

    TRACE( "%p %u %p %p\n", handle, min_size, ctx, error );
    if (error) FIXME( "ignoring error parameter\n" );
654
    if (ctx) FIXME( "ignoring ctx parameter\n" );
655 656 657

    if (!reader) return E_INVALIDARG;

658 659 660 661 662 663 664 665
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

666 667 668 669 670 671 672 673 674 675
    if (reader->input_type == WS_XML_READER_INPUT_TYPE_STREAM)
    {
        hr = read_more_data( reader, min_size, ctx, error );
    }
    else
    {
        reader->read_size = min( min_size, reader->input_size );
        reader->read_pos = 0;
        hr = S_OK;
    }
676

677
    LeaveCriticalSection( &reader->cs );
678 679
    TRACE( "returning %08x\n", hr );
    return hr;
680 681
}

682 683 684 685 686 687 688 689 690 691 692 693 694
/**************************************************************************
 *          WsGetNamespaceFromPrefix		[webservices.@]
 */
HRESULT WINAPI WsGetNamespaceFromPrefix( WS_XML_READER *handle, const WS_XML_STRING *prefix,
                                         BOOL required, const WS_XML_STRING **ns, WS_ERROR *error )
{
    static const WS_XML_STRING xml = {3, (BYTE *)"xml"};
    static const WS_XML_STRING xmlns = {5, (BYTE *)"xmlns"};
    static const WS_XML_STRING empty_ns = {0, NULL};
    static const WS_XML_STRING xml_ns = {36, (BYTE *)"http://www.w3.org/XML/1998/namespace"};
    static const WS_XML_STRING xmlns_ns = {29, (BYTE *)"http://www.w3.org/2000/xmlns/"};
    struct reader *reader = (struct reader *)handle;
    BOOL found = FALSE;
695
    HRESULT hr = S_OK;
696 697 698 699 700

    TRACE( "%p %s %d %p %p\n", handle, debugstr_xmlstr(prefix), required, ns, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !prefix || !ns) return E_INVALIDARG;
701 702 703 704 705 706 707 708 709

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

710 711
    if (reader->state != READER_STATE_STARTELEMENT) hr = WS_E_INVALID_OPERATION;
    else if (!prefix->length)
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    {
        *ns = &empty_ns;
        found = TRUE;
    }
    else if (WsXmlStringEquals( prefix, &xml, NULL ) == S_OK)
    {
        *ns = &xml_ns;
        found = TRUE;
    }
    else if (WsXmlStringEquals( prefix, &xmlns, NULL ) == S_OK)
    {
        *ns = &xmlns_ns;
        found = TRUE;
    }
    else
    {
        WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
        ULONG i;

        for (i = 0; i < elem->attributeCount; i++)
        {
            if (!elem->attributes[i]->isXmlNs) continue;
            if (WsXmlStringEquals( prefix, elem->attributes[i]->prefix, NULL ) == S_OK)
            {
                *ns = elem->attributes[i]->ns;
                found = TRUE;
                break;
            }
        }
    }

743 744
    LeaveCriticalSection( &reader->cs );

745
    if (hr == S_OK && !found)
746
    {
747 748 749 750 751 752
        if (required) hr = WS_E_INVALID_FORMAT;
        else
        {
            *ns = NULL;
            hr = S_FALSE;
        }
753
    }
754

755 756
    TRACE( "returning %08x\n", hr );
    return hr;
757 758
}

759 760 761 762 763 764 765
/**************************************************************************
 *          WsGetReaderNode		[webservices.@]
 */
HRESULT WINAPI WsGetReaderNode( WS_XML_READER *handle, const WS_XML_NODE **node,
                                WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
766
    HRESULT hr = S_OK;
767 768 769 770 771 772

    TRACE( "%p %p %p\n", handle, node, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !node) return E_INVALIDARG;

773 774 775 776 777 778 779 780
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

781
    *node = &reader->current->hdr.node;
782 783

    LeaveCriticalSection( &reader->cs );
784
    TRACE( "returning %08x\n", hr );
785 786 787
    return S_OK;
}

788 789 790 791 792 793 794 795
static HRESULT get_charset( struct reader *reader, void *buf, ULONG size )
{
    if (!buf || size != sizeof(reader->input_charset)) return E_INVALIDARG;
    if (!reader->input_charset) return WS_E_INVALID_FORMAT;
    *(WS_CHARSET *)buf = reader->input_charset;
    return S_OK;
}

796 797 798 799 800 801 802
/**************************************************************************
 *          WsGetReaderProperty		[webservices.@]
 */
HRESULT WINAPI WsGetReaderProperty( WS_XML_READER *handle, WS_XML_READER_PROPERTY_ID id,
                                    void *buf, ULONG size, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
803
    HRESULT hr;
804 805 806 807

    TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
    if (error) FIXME( "ignoring error parameter\n" );

808 809 810 811 812 813 814 815 816 817
    if (!reader) return E_INVALIDARG;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

818 819
    if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
    else if (id == WS_XML_READER_PROPERTY_CHARSET) hr = get_charset( reader, buf, size );
820 821 822
    else hr = prop_get( reader->prop, reader->prop_count, id, buf, size );

    LeaveCriticalSection( &reader->cs );
823
    TRACE( "returning %08x\n", hr );
824
    return hr;
825 826
}

827 828 829 830 831 832
/**************************************************************************
 *          WsGetXmlAttribute		[webservices.@]
 */
HRESULT WINAPI WsGetXmlAttribute( WS_XML_READER *handle, const WS_XML_STRING *attr,
                                  WS_HEAP *heap, WCHAR **str, ULONG *len, WS_ERROR *error )
{
833
    FIXME( "%p %s %p %p %p %p: stub\n", handle, debugstr_xmlstr(attr), heap, str, len, error );
834 835 836
    return E_NOTIMPL;
}

837
WS_XML_UTF8_TEXT *alloc_utf8_text( const BYTE *data, ULONG len )
838 839 840 841 842 843
{
    WS_XML_UTF8_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
    ret->text.textType    = WS_XML_TEXT_TYPE_UTF8;
    ret->value.length     = len;
844
    ret->value.bytes      = len ? (BYTE *)(ret + 1) : NULL;
845 846 847 848 849 850
    ret->value.dictionary = NULL;
    ret->value.id         = 0;
    if (data) memcpy( ret->value.bytes, data, len );
    return ret;
}

851 852 853 854 855 856 857 858 859 860 861 862
WS_XML_UTF16_TEXT *alloc_utf16_text( const BYTE *data, ULONG len )
{
    WS_XML_UTF16_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_UTF16;
    ret->byteCount     = len;
    ret->bytes         = len ? (BYTE *)(ret + 1) : NULL;
    if (data) memcpy( ret->bytes, data, len );
    return ret;
}

863
WS_XML_BASE64_TEXT *alloc_base64_text( const BYTE *data, ULONG len )
864 865 866 867 868 869 870 871 872 873 874
{
    WS_XML_BASE64_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) + len ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_BASE64;
    ret->length        = len;
    ret->bytes         = len ? (BYTE *)(ret + 1) : NULL;
    if (data) memcpy( ret->bytes, data, len );
    return ret;
}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
WS_XML_BOOL_TEXT *alloc_bool_text( BOOL value )
{
    WS_XML_BOOL_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_BOOL;
    ret->value         = value;
    return ret;
}

WS_XML_INT32_TEXT *alloc_int32_text( INT32 value )
{
    WS_XML_INT32_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_INT32;
    ret->value         = value;
    return ret;
}

WS_XML_INT64_TEXT *alloc_int64_text( INT64 value )
{
    WS_XML_INT64_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_INT64;
    ret->value         = value;
    return ret;
}

WS_XML_UINT64_TEXT *alloc_uint64_text( UINT64 value )
{
    WS_XML_UINT64_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_UINT64;
    ret->value         = value;
    return ret;
}

915 916 917 918 919 920 921 922 923 924
WS_XML_FLOAT_TEXT *alloc_float_text( float value )
{
    WS_XML_FLOAT_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_FLOAT;
    ret->value         = value;
    return ret;
}

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
WS_XML_DOUBLE_TEXT *alloc_double_text( double value )
{
    WS_XML_DOUBLE_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_DOUBLE;
    ret->value         = value;
    return ret;
}

WS_XML_GUID_TEXT *alloc_guid_text( const GUID *value )
{
    WS_XML_GUID_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_GUID;
    ret->value         = *value;
    return ret;
}

WS_XML_UNIQUE_ID_TEXT *alloc_unique_id_text( const GUID *value )
{
    WS_XML_UNIQUE_ID_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_UNIQUE_ID;
    ret->value         = *value;
    return ret;
}

WS_XML_DATETIME_TEXT *alloc_datetime_text( const WS_DATETIME *value )
{
    WS_XML_DATETIME_TEXT *ret;

    if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
    ret->text.textType = WS_XML_TEXT_TYPE_DATETIME;
    ret->value         = *value;
    return ret;
}

965 966
static inline BOOL read_end_of_data( struct reader *reader )
{
967
    return (read_more_data( reader, 1, NULL, NULL ) != S_OK);
968 969
}

970
static inline const unsigned char *read_current_ptr( struct reader *reader )
971 972 973 974
{
    return &reader->read_bufptr[reader->read_pos];
}

975 976 977 978 979 980
static inline void read_skip( struct reader *reader, unsigned int count )
{
    assert( reader->read_pos + count <= reader->read_size );
    reader->read_pos += count;
}

981
static inline HRESULT read_peek( struct reader *reader, unsigned char *bytes, unsigned int len )
982
{
983 984 985
    HRESULT hr;
    if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
    memcpy( bytes, read_current_ptr( reader ), len );
986 987 988 989 990
    return S_OK;
}

static inline HRESULT read_byte( struct reader *reader, unsigned char *byte )
{
991 992 993 994
    HRESULT hr;
    if ((hr = read_more_data( reader, 1, NULL, NULL )) != S_OK) return hr;
    *byte = *read_current_ptr( reader );
    read_skip( reader, 1 );
995 996 997 998 999
    return S_OK;
}

static inline HRESULT read_bytes( struct reader *reader, unsigned char *bytes, unsigned int len )
{
1000 1001 1002 1003
    HRESULT hr;
    if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
    memcpy( bytes, read_current_ptr( reader ), len );
    read_skip( reader, len );
1004 1005 1006
    return S_OK;
}

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
/* UTF-8 support based on libs/wine/utf8.c */

/* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
static const char utf8_length[128] =
{
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
    0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
    3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0  /* 0xf0-0xff */
};

/* first byte mask depending on UTF-8 sequence length */
static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };

/* minimum Unicode value depending on UTF-8 sequence length */
static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };

1028
static inline HRESULT read_utf8_char( struct reader *reader, unsigned int *ret, unsigned int *skip )
1029
{
1030
    unsigned int len;
1031
    unsigned char ch;
1032
    const unsigned char *end;
1033
    HRESULT hr;
1034

1035 1036
    if ((hr = read_more_data( reader, 1, NULL, NULL )) != S_OK) return hr;
    ch = *read_current_ptr( reader );
1037 1038
    if (ch < 0x80)
    {
1039
        *ret = ch;
1040
        *skip = 1;
1041
        return S_OK;
1042
    }
1043

1044
    len = utf8_length[ch - 0x80];
1045 1046
    if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;
    end = read_current_ptr( reader ) + len + 1;
1047
    *ret = ch & utf8_mask[len];
1048 1049 1050 1051 1052

    switch (len)
    {
    case 3:
        if ((ch = end[-3] ^ 0x80) >= 0x40) break;
1053
        *ret = (*ret << 6) | ch;
1054 1055
    case 2:
        if ((ch = end[-2] ^ 0x80) >= 0x40) break;
1056
        *ret = (*ret << 6) | ch;
1057 1058
    case 1:
        if ((ch = end[-1] ^ 0x80) >= 0x40) break;
1059 1060
        *ret = (*ret << 6) | ch;
        if (*ret < utf8_minval[len]) break;
1061
        *skip = len + 1;
1062
        return S_OK;
1063 1064
    }

1065
    return WS_E_INVALID_FORMAT;
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
}

static inline BOOL read_isnamechar( unsigned int ch )
{
    /* FIXME: incomplete */
    return (ch >= 'A' && ch <= 'Z') ||
           (ch >= 'a' && ch <= 'z') ||
           (ch >= '0' && ch <= '9') ||
           ch == '_' || ch == '-' || ch == '.' || ch == ':';
}

static inline BOOL read_isspace( unsigned int ch )
{
    return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
}

static inline void read_skip_whitespace( struct reader *reader )
{
1084 1085 1086 1087 1088
    for (;;)
    {
        if (read_more_data( reader, 1, NULL, NULL ) != S_OK || !read_isspace( *read_current_ptr( reader ) )) break;
        read_skip( reader, 1 );
    }
1089 1090
}

1091
static inline HRESULT read_cmp( struct reader *reader, const char *str, int len )
1092
{
1093 1094
    const unsigned char *ptr;
    HRESULT hr;
1095 1096

    if (len < 0) len = strlen( str );
1097 1098 1099
    if ((hr = read_more_data( reader, len, NULL, NULL )) != S_OK) return hr;

    ptr = read_current_ptr( reader );
1100 1101
    while (len--)
    {
1102
        if (*str != *ptr) return WS_E_INVALID_FORMAT;
1103 1104
        str++; ptr++;
    }
1105
    return S_OK;
1106 1107 1108 1109
}

static HRESULT read_xmldecl( struct reader *reader )
{
1110 1111
    HRESULT hr;

1112
    if ((hr = read_more_data( reader, 1, NULL, NULL )) != S_OK) return hr;
1113
    if (*read_current_ptr( reader ) != '<' || (hr = read_cmp( reader, "<?", 2 )) != S_OK)
1114 1115 1116 1117
    {
        reader->state = READER_STATE_BOF;
        return S_OK;
    }
1118
    if ((hr = read_cmp( reader, "<?xml ", 6 )) != S_OK) return hr;
1119 1120 1121
    read_skip( reader, 6 );

    /* FIXME: parse attributes */
1122 1123 1124 1125 1126
    for (;;)
    {
        if (read_more_data( reader, 1, NULL, NULL ) != S_OK || *read_current_ptr( reader ) == '?' ) break;
        read_skip( reader, 1 );
    }
1127

1128
    if ((hr = read_cmp( reader, "?>", 2 )) != S_OK) return hr;
1129 1130 1131 1132 1133 1134
    read_skip( reader, 2 );

    reader->state = READER_STATE_BOF;
    return S_OK;
}

1135
HRESULT append_attribute( WS_XML_ELEMENT_NODE *elem, WS_XML_ATTRIBUTE *attr )
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
{
    if (elem->attributeCount)
    {
        WS_XML_ATTRIBUTE **tmp;
        if (!(tmp = heap_realloc( elem->attributes, (elem->attributeCount + 1) * sizeof(attr) )))
            return E_OUTOFMEMORY;
        elem->attributes = tmp;
    }
    else if (!(elem->attributes = heap_alloc( sizeof(attr) ))) return E_OUTOFMEMORY;
    elem->attributes[elem->attributeCount++] = attr;
    return S_OK;
}

1149
static inline void init_xml_string( BYTE *bytes, ULONG len, WS_XML_STRING *str )
1150
{
1151 1152 1153 1154 1155
    str->length     = len;
    str->bytes      = bytes;
    str->dictionary = NULL;
    str->id         = 0;
}
1156

1157 1158 1159 1160
static HRESULT split_qname( const BYTE *str, ULONG len, WS_XML_STRING *prefix, WS_XML_STRING *localname )
{
    BYTE *prefix_bytes = NULL, *localname_bytes = (BYTE *)str, *ptr = (BYTE *)str;
    ULONG prefix_len = 0, localname_len = len;
1161 1162

    while (len--)
1163
    {
1164
        if (*ptr == ':')
1165
        {
1166
            if (ptr == str) return WS_E_INVALID_FORMAT;
1167 1168 1169 1170
            prefix_bytes = (BYTE *)str;
            prefix_len   = ptr - str;
            localname_bytes = ptr + 1;
            localname_len   = len;
1171 1172
            break;
        }
1173
        ptr++;
1174
    }
1175 1176 1177 1178
    if (!localname_len) return WS_E_INVALID_FORMAT;

    init_xml_string( prefix_bytes, prefix_len, prefix );
    init_xml_string( localname_bytes, localname_len, localname );
1179 1180 1181
    return S_OK;
}

1182
static HRESULT parse_qname( const BYTE *str, ULONG len, WS_XML_STRING **prefix_ret, WS_XML_STRING **localname_ret )
1183
{
1184
    WS_XML_STRING prefix, localname;
1185 1186
    HRESULT hr;

1187 1188
    if ((hr = split_qname( str, len, &prefix, &localname )) != S_OK) return hr;
    if (!(*prefix_ret = alloc_xml_string( NULL, prefix.length ))) return E_OUTOFMEMORY;
1189
    if (!(*localname_ret = dup_xml_string( &localname, FALSE )))
1190
    {
1191
        free_xml_string( *prefix_ret );
1192 1193
        return E_OUTOFMEMORY;
    }
1194 1195
    memcpy( (*prefix_ret)->bytes, prefix.bytes, prefix.length );
    if (prefix.length && add_xml_string( *prefix_ret ) != S_OK) WARN( "prefix not added to dictionary\n" );
1196 1197 1198
    return S_OK;
}

1199 1200
static int codepoint_to_utf8( int cp, unsigned char *dst )
{
1201
    if (!cp) return -1;
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    if (cp < 0x80)
    {
        *dst = cp;
        return 1;
    }
    if (cp < 0x800)
    {
        dst[1] = 0x80 | (cp & 0x3f);
        cp >>= 6;
        dst[0] = 0xc0 | cp;
        return 2;
    }
    if ((cp >= 0xd800 && cp <= 0xdfff) || cp == 0xfffe || cp == 0xffff) return -1;
    if (cp < 0x10000)
    {
        dst[2] = 0x80 | (cp & 0x3f);
        cp >>= 6;
        dst[1] = 0x80 | (cp & 0x3f);
        cp >>= 6;
        dst[0] = 0xe0 | cp;
        return 3;
    }
1224
    if (cp >= 0x110000) return -1;
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    dst[3] = 0x80 | (cp & 0x3f);
    cp >>= 6;
    dst[2] = 0x80 | (cp & 0x3f);
    cp >>= 6;
    dst[1] = 0x80 | (cp & 0x3f);
    cp >>= 6;
    dst[0] = 0xf0 | cp;
    return 4;
}

static HRESULT decode_text( const unsigned char *str, ULONG len, unsigned char *ret, ULONG *ret_len  )
{
    const unsigned char *p = str;
    unsigned char *q = ret;

    *ret_len = 0;
    while (len)
    {
        if (*p == '&')
        {
            p++; len--;
            if (!len) return WS_E_INVALID_FORMAT;

            if (len >= 3 && !memcmp( p, "lt;", 3 ))
            {
                *q++ = '<';
                p += 3;
                len -= 3;
            }
            else if (len >= 3 && !memcmp( p, "gt;", 3 ))
            {
                *q++ = '>';
                p += 3;
                len -= 3;
            }
            else if (len >= 5 && !memcmp( p, "quot;", 5 ))
            {
                *q++ = '"';
                p += 5;
                len -= 5;
            }
            else if (len >= 4 && !memcmp( p, "amp;", 4 ))
            {
                *q++ = '&';
                p += 4;
                len -= 4;
            }
            else if (len >= 5 && !memcmp( p, "apos;", 5 ))
            {
                *q++ = '\'';
                p += 5;
                len -= 5;
            }
            else if (*p == '#')
            {
                ULONG start, nb_digits, i;
                int len_utf8, cp = 0;

                p++; len--;
                if (!len) return WS_E_INVALID_FORMAT;
1285
                if (*p == 'x')
1286 1287 1288 1289 1290 1291 1292 1293
                {
                    p++; len--;

                    start = len;
                    while (len && isxdigit( *p )) { p++; len--; };
                    if (!len) return WS_E_INVALID_FORMAT;

                    p -= nb_digits = start - len;
1294
                    if (!nb_digits || nb_digits > 6 || p[nb_digits] != ';') return WS_E_INVALID_FORMAT;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
                    for (i = 0; i < nb_digits; i++)
                    {
                        cp *= 16;
                        if (*p >= '0' && *p <= '9') cp += *p - '0';
                        else if (*p >= 'a' && *p <= 'f') cp += *p - 'a' + 10;
                        else cp += *p - 'A' + 10;
                        p++;
                    }
                }
                else if (isdigit( *p ))
1305
                {
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
                    while (len && *p == '0') { p++; len--; };
                    if (!len) return WS_E_INVALID_FORMAT;

                    start = len;
                    while (len && isdigit( *p )) { p++; len--; };
                    if (!len) return WS_E_INVALID_FORMAT;

                    p -= nb_digits = start - len;
                    if (!nb_digits || nb_digits > 7 || p[nb_digits] != ';') return WS_E_INVALID_FORMAT;
                    for (i = 0; i < nb_digits; i++)
                    {
                        cp *= 10;
                        cp += *p - '0';
                        p++;
                    }
1321
                }
1322
                else return WS_E_INVALID_FORMAT;
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
                p++; len--;
                if ((len_utf8 = codepoint_to_utf8( cp, q )) < 0) return WS_E_INVALID_FORMAT;
                *ret_len += len_utf8;
                q += len_utf8;
                continue;
            }
            else return WS_E_INVALID_FORMAT;
        }
        else
        {
            *q++ = *p++;
            len--;
        }
        *ret_len += 1;
    }
    return S_OK;
}

1341 1342
static HRESULT read_attribute_value_text( struct reader *reader, WS_XML_ATTRIBUTE *attr )
{
1343
    WS_XML_UTF8_TEXT *utf8;
1344 1345
    unsigned int len, ch, skip, quote;
    const unsigned char *start;
1346
    HRESULT hr;
1347 1348

    read_skip_whitespace( reader );
1349
    if ((hr = read_cmp( reader, "=", 1 )) != S_OK) return hr;
1350 1351 1352
    read_skip( reader, 1 );

    read_skip_whitespace( reader );
1353
    if ((hr = read_cmp( reader, "\"", 1 )) != S_OK && (hr = read_cmp( reader, "'", 1 )) != S_OK) return hr;
1354
    if ((hr = read_utf8_char( reader, &quote, &skip )) != S_OK) return hr;
1355 1356 1357 1358 1359 1360
    read_skip( reader, 1 );

    len = 0;
    start = read_current_ptr( reader );
    for (;;)
    {
1361
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
1362 1363 1364 1365 1366 1367 1368 1369
        if (ch == quote) break;
        read_skip( reader, skip );
        len += skip;
    }
    read_skip( reader, 1 );

    if (attr->isXmlNs)
    {
1370 1371 1372
        if (!(attr->ns = alloc_xml_string( start, len ))) return E_OUTOFMEMORY;
        if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) return hr;
        if (!(utf8 = alloc_utf8_text( NULL, 0 ))) return E_OUTOFMEMORY;
1373 1374 1375
    }
    else
    {
1376 1377 1378 1379 1380 1381
        if (!(utf8 = alloc_utf8_text( NULL, len ))) return E_OUTOFMEMORY;
        if ((hr = decode_text( start, len, utf8->value.bytes, &utf8->value.length )) != S_OK)
        {
            heap_free( utf8 );
            return hr;
        }
1382 1383 1384 1385 1386 1387 1388
    }

    attr->value = &utf8->text;
    attr->singleQuote = (quote == '\'');
    return S_OK;
}

1389 1390 1391 1392 1393
static inline BOOL is_text_type( unsigned char type )
{
    return (type >= RECORD_ZERO_TEXT && type <= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT);
}

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
static HRESULT read_int31( struct reader *reader, ULONG *len )
{
    unsigned char byte;
    HRESULT hr;

    if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
    *len = byte & 0x7f;
    if (!(byte & 0x80)) return S_OK;

    if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
    *len += (byte & 0x7f) << 7;
    if (!(byte & 0x80)) return S_OK;

    if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
    *len += (byte & 0x7f) << 14;
    if (!(byte & 0x80)) return S_OK;

    if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
    *len += (byte & 0x7f) << 21;
    if (!(byte & 0x80)) return S_OK;

    if ((hr = read_byte( reader, &byte )) != S_OK) return hr;
    *len += (byte & 0x07) << 28;
    return S_OK;
}

static HRESULT read_string( struct reader *reader, WS_XML_STRING **str )
{
    ULONG len;
    HRESULT hr;
    if ((hr = read_int31( reader, &len )) != S_OK) return hr;
    if (!(*str = alloc_xml_string( NULL, len ))) return E_OUTOFMEMORY;
1426 1427 1428 1429 1430 1431
    if ((hr = read_bytes( reader, (*str)->bytes, len )) == S_OK)
    {
        if (add_xml_string( *str ) != S_OK) WARN( "string not added to dictionary\n" );
        return S_OK;
    }
    free_xml_string( *str );
1432 1433 1434
    return hr;
}

1435 1436
static HRESULT read_dict_string( struct reader *reader, WS_XML_STRING **str )
{
1437
    const WS_XML_DICTIONARY *dict;
1438
    HRESULT hr;
1439 1440
    ULONG id;

1441
    if ((hr = read_int31( reader, &id )) != S_OK) return hr;
1442 1443
    dict = (id & 1) ? reader->dict : reader->dict_static;
    if (!dict || (id >>= 1) >= dict->stringCount) return WS_E_INVALID_FORMAT;
1444
    if (!(*str = alloc_xml_string( NULL, 0 ))) return E_OUTOFMEMORY;
1445
    *(*str) = dict->strings[id];
1446 1447 1448
    return S_OK;
}

1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
static HRESULT read_datetime( struct reader *reader, WS_DATETIME *ret )
{
    UINT64 val;
    HRESULT hr;

    if ((hr = read_bytes( reader, (unsigned char *)&val, sizeof(val) )) != S_OK) return hr;

    if ((val & 0x03) == 1) ret->format = WS_DATETIME_FORMAT_UTC;
    else if ((val & 0x03) == 2) ret->format = WS_DATETIME_FORMAT_LOCAL;
    else ret->format = WS_DATETIME_FORMAT_NONE;

    if ((ret->ticks = val >> 2) > TICKS_MAX) return WS_E_INVALID_FORMAT;
    return S_OK;
}

1464 1465 1466 1467 1468 1469 1470 1471
static HRESULT lookup_string( struct reader *reader, ULONG id, const WS_XML_STRING **ret )
{
    const WS_XML_DICTIONARY *dict = (id & 1) ? reader->dict : reader->dict_static;
    if (!dict || (id >>= 1) >= dict->stringCount) return WS_E_INVALID_FORMAT;
    *ret = &dict->strings[id];
    return S_OK;
}

1472 1473
static HRESULT read_attribute_value_bin( struct reader *reader, WS_XML_ATTRIBUTE *attr )
{
1474 1475 1476 1477 1478
    WS_XML_UTF8_TEXT *text_utf8 = NULL;
    WS_XML_BASE64_TEXT *text_base64 = NULL;
    WS_XML_INT32_TEXT *text_int32;
    WS_XML_INT64_TEXT *text_int64;
    WS_XML_BOOL_TEXT *text_bool;
1479
    const WS_XML_STRING *str;
1480
    unsigned char type;
1481 1482
    UINT8 val_uint8;
    UINT16 val_uint16;
1483 1484 1485
    INT32 val_int32;
    ULONG len = 0, id;
    GUID guid;
1486 1487 1488 1489 1490 1491 1492
    HRESULT hr;

    if ((hr = read_byte( reader, &type )) != S_OK) return hr;
    if (!is_text_type( type )) return WS_E_INVALID_FORMAT;

    switch (type)
    {
1493
    case RECORD_ZERO_TEXT:
1494 1495 1496 1497 1498
    {
        if (!(text_int32 = alloc_int32_text( 0 ))) return E_OUTOFMEMORY;
        attr->value = &text_int32->text;
        return S_OK;
    }
1499
    case RECORD_ONE_TEXT:
1500 1501 1502 1503 1504
    {
        if (!(text_int32 = alloc_int32_text( 1 ))) return E_OUTOFMEMORY;
        attr->value = &text_int32->text;
        return S_OK;
    }
1505
    case RECORD_FALSE_TEXT:
1506 1507 1508 1509 1510
    {
        if (!(text_bool = alloc_bool_text( FALSE ))) return E_OUTOFMEMORY;
        attr->value = &text_bool->text;
        return S_OK;
    }
1511
    case RECORD_TRUE_TEXT:
1512 1513 1514 1515 1516
    {
        if (!(text_bool = alloc_bool_text( TRUE ))) return E_OUTOFMEMORY;
        attr->value = &text_bool->text;
        return S_OK;
    }
1517
    case RECORD_INT8_TEXT:
1518 1519
    {
        INT8 val_int8;
1520
        if ((hr = read_byte( reader, (unsigned char *)&val_int8 )) != S_OK) return hr;
1521 1522 1523 1524
        if (!(text_int64 = alloc_int64_text( val_int8 ))) return E_OUTOFMEMORY;
        attr->value = &text_int64->text;
        return S_OK;
    }
1525
    case RECORD_INT16_TEXT:
1526 1527
    {
        INT16 val_int16;
1528
        if ((hr = read_bytes( reader, (unsigned char *)&val_int16, sizeof(val_int16) )) != S_OK) return hr;
1529 1530 1531 1532
        if (!(text_int64 = alloc_int64_text( val_int16 ))) return E_OUTOFMEMORY;
        attr->value = &text_int64->text;
        return S_OK;
    }
1533 1534
    case RECORD_INT32_TEXT:
        if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1535 1536 1537
        if (!(text_int64 = alloc_int64_text( val_int32 ))) return E_OUTOFMEMORY;
        attr->value = &text_int64->text;
        return S_OK;
1538 1539

    case RECORD_INT64_TEXT:
1540 1541
    {
        INT64 val_int64;
1542
        if ((hr = read_bytes( reader, (unsigned char *)&val_int64, sizeof(val_int64) )) != S_OK) return hr;
1543 1544 1545 1546
        if (!(text_int64 = alloc_int64_text( val_int64 ))) return E_OUTOFMEMORY;
        attr->value = &text_int64->text;
        return S_OK;
    }
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
    case RECORD_FLOAT_TEXT:
    {
        WS_XML_FLOAT_TEXT *text_float;
        float val_float;

        if ((hr = read_bytes( reader, (unsigned char *)&val_float, sizeof(val_float) )) != S_OK) return hr;
        if (!(text_float = alloc_float_text( val_float ))) return E_OUTOFMEMORY;
        attr->value = &text_float->text;
        return S_OK;
    }
1557
    case RECORD_DOUBLE_TEXT:
1558 1559 1560
    {
        WS_XML_DOUBLE_TEXT *text_double;
        double val_double;
1561

1562 1563 1564 1565 1566
        if ((hr = read_bytes( reader, (unsigned char *)&val_double, sizeof(val_double) )) != S_OK) return hr;
        if (!(text_double = alloc_double_text( val_double ))) return E_OUTOFMEMORY;
        attr->value = &text_double->text;
        return S_OK;
    }
1567
    case RECORD_DATETIME_TEXT:
1568 1569 1570
    {
        WS_XML_DATETIME_TEXT *text_datetime;
        WS_DATETIME datetime;
1571

1572 1573 1574 1575 1576
        if ((hr = read_datetime( reader, &datetime )) != S_OK) return hr;
        if (!(text_datetime = alloc_datetime_text( &datetime ))) return E_OUTOFMEMORY;
        attr->value = &text_datetime->text;
        return S_OK;
    }
1577
    case RECORD_CHARS8_TEXT:
1578 1579
        if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
        len = val_uint8;
1580
        break;
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594

    case RECORD_CHARS16_TEXT:
        if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
        len = val_uint16;
        break;

    case RECORD_CHARS32_TEXT:
        if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
        if (val_int32 < 0) return WS_E_INVALID_FORMAT;
        len = val_int32;
        break;

    case RECORD_BYTES8_TEXT:
        if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
1595 1596
        if (!(text_base64 = alloc_base64_text( NULL, val_uint8 ))) return E_OUTOFMEMORY;
        if ((hr = read_bytes( reader, text_base64->bytes, val_uint8 )) != S_OK)
1597
        {
1598
            heap_free( text_base64 );
1599 1600 1601 1602 1603
            return hr;
        }
        break;

    case RECORD_BYTES16_TEXT:
1604
        if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
1605 1606
        if (!(text_base64 = alloc_base64_text( NULL, val_uint16 ))) return E_OUTOFMEMORY;
        if ((hr = read_bytes( reader, text_base64->bytes, val_uint16 )) != S_OK)
1607
        {
1608
            heap_free( text_base64 );
1609 1610 1611 1612 1613
            return hr;
        }
        break;

    case RECORD_BYTES32_TEXT:
1614
        if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
1615
        if (val_int32 < 0) return WS_E_INVALID_FORMAT;
1616 1617
        if (!(text_base64 = alloc_base64_text( NULL, val_int32 ))) return E_OUTOFMEMORY;
        if ((hr = read_bytes( reader, text_base64->bytes, val_int32 )) != S_OK)
1618
        {
1619
            heap_free( text_base64 );
1620 1621 1622 1623
            return hr;
        }
        break;

1624 1625 1626 1627 1628
    case RECORD_EMPTY_TEXT:
        break;

    case RECORD_DICTIONARY_TEXT:
        if ((hr = read_int31( reader, &id )) != S_OK) return hr;
1629
        if ((hr = lookup_string( reader, id, &str )) != S_OK) return hr;
1630
        if (!(text_utf8 = alloc_utf8_text( str->bytes, str->length ))) return E_OUTOFMEMORY;
1631
        break;
1632

1633
    case RECORD_UNIQUE_ID_TEXT:
1634 1635 1636 1637 1638 1639 1640
    {
        WS_XML_UNIQUE_ID_TEXT *text_unique_id;
        if ((hr = read_bytes( reader, (unsigned char *)&guid, sizeof(guid) )) != S_OK) return hr;
        if (!(text_unique_id = alloc_unique_id_text( &guid ))) return E_OUTOFMEMORY;
        attr->value = &text_unique_id->text;
        return S_OK;
    }
1641
    case RECORD_GUID_TEXT:
1642 1643 1644 1645 1646 1647 1648
    {
        WS_XML_GUID_TEXT *guid_text;
        if ((hr = read_bytes( reader, (unsigned char *)&guid, sizeof(guid) )) != S_OK) return hr;
        if (!(guid_text = alloc_guid_text( &guid ))) return E_OUTOFMEMORY;
        attr->value = &guid_text->text;
        return S_OK;
    }
1649
    case RECORD_UINT64_TEXT:
1650 1651 1652
    {
        WS_XML_UINT64_TEXT *text_uint64;
        UINT64 val_uint64;
1653

1654 1655 1656 1657 1658
        if ((hr = read_bytes( reader, (unsigned char *)&val_uint64, sizeof(val_uint64) )) != S_OK) return hr;
        if (!(text_uint64 = alloc_uint64_text( val_uint64 ))) return E_OUTOFMEMORY;
        attr->value = &text_uint64->text;
        return S_OK;
    }
1659
    case RECORD_BOOL_TEXT:
1660 1661 1662
    {
        WS_XML_BOOL_TEXT *text_bool;
        BOOL val_bool;
1663

1664 1665 1666 1667 1668
        if ((hr = read_bytes( reader, (unsigned char *)&val_bool, sizeof(val_bool) )) != S_OK) return hr;
        if (!(text_bool = alloc_bool_text( !!val_bool ))) return E_OUTOFMEMORY;
        attr->value = &text_bool->text;
        return S_OK;
    }
1669 1670 1671 1672 1673
    default:
        ERR( "unhandled record type %02x\n", type );
        return WS_E_NOT_SUPPORTED;
    }

1674 1675
    if (type >= RECORD_BYTES8_TEXT && type <= RECORD_BYTES32_TEXT)
    {
1676
        attr->value = &text_base64->text;
1677 1678 1679
        return S_OK;
    }

1680
    if (!text_utf8)
1681
    {
1682 1683 1684
        if (!(text_utf8 = alloc_utf8_text( NULL, len ))) return E_OUTOFMEMORY;
        if (!len) text_utf8->value.bytes = (BYTE *)(text_utf8 + 1); /* quirk */
        if ((hr = read_bytes( reader, text_utf8->value.bytes, len )) != S_OK)
1685
        {
1686
            heap_free( text_utf8 );
1687 1688
            return hr;
        }
1689 1690
    }

1691
    attr->value = &text_utf8->text;
1692 1693 1694
    return S_OK;
}

1695
static HRESULT read_attribute_text( struct reader *reader, WS_XML_ATTRIBUTE **ret )
1696
{
1697
    static const WS_XML_STRING xmlns = {5, (BYTE *)"xmlns"};
1698
    WS_XML_ATTRIBUTE *attr;
1699
    unsigned int len = 0, ch, skip;
1700
    const unsigned char *start;
1701
    WS_XML_STRING *prefix, *localname;
1702
    HRESULT hr;
1703 1704 1705 1706 1707 1708

    if (!(attr = heap_alloc_zero( sizeof(*attr) ))) return E_OUTOFMEMORY;

    start = read_current_ptr( reader );
    for (;;)
    {
1709
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) goto error;
1710 1711 1712 1713
        if (!read_isnamechar( ch )) break;
        read_skip( reader, skip );
        len += skip;
    }
1714 1715 1716 1717 1718
    if (!len)
    {
        hr = WS_E_INVALID_FORMAT;
        goto error;
    }
1719

1720
    if ((hr = parse_qname( start, len, &prefix, &localname )) != S_OK) goto error;
1721 1722
    if (WsXmlStringEquals( prefix, &xmlns, NULL ) == S_OK)
    {
1723
        free_xml_string( prefix );
1724
        attr->isXmlNs   = 1;
1725 1726
        if (!(attr->prefix = alloc_xml_string( localname->bytes, localname->length )))
        {
1727
            free_xml_string( localname );
1728
            hr = E_OUTOFMEMORY;
1729 1730 1731 1732 1733 1734 1735 1736
            goto error;
        }
        attr->localName = localname;
    }
    else if (!prefix->length && WsXmlStringEquals( localname, &xmlns, NULL ) == S_OK)
    {
        attr->isXmlNs   = 1;
        attr->prefix    = prefix;
1737 1738 1739 1740 1741 1742 1743
        attr->localName = localname;
    }
    else
    {
        attr->prefix    = prefix;
        attr->localName = localname;
    }
1744

1745
    if ((hr = read_attribute_value_text( reader, attr )) != S_OK) goto error;
1746 1747 1748 1749 1750 1751 1752 1753 1754

    *ret = attr;
    return S_OK;

error:
    free_attribute( attr );
    return hr;
}

1755 1756 1757 1758 1759
static inline BOOL is_attribute_type( unsigned char type )
{
    return (type >= RECORD_SHORT_ATTRIBUTE && type <= RECORD_PREFIX_ATTRIBUTE_Z);
}

1760 1761 1762 1763 1764 1765
static WS_XML_STRING *get_xmlns_localname( struct reader *reader, const WS_XML_STRING *prefix )
{
    if (!get_namespace( reader, prefix )) return alloc_xml_string( NULL, 0 );
    return alloc_xml_string( prefix->bytes, prefix->length );
}

1766 1767
static HRESULT read_attribute_bin( struct reader *reader, WS_XML_ATTRIBUTE **ret )
{
1768
    WS_XML_UTF8_TEXT *utf8;
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
    WS_XML_ATTRIBUTE *attr;
    unsigned char type = 0;
    HRESULT hr;

    if ((hr = read_byte( reader, &type )) != S_OK) return hr;
    if (!is_attribute_type( type )) return WS_E_INVALID_FORMAT;
    if (!(attr = heap_alloc_zero( sizeof(*attr) ))) return E_OUTOFMEMORY;

    if (type >= RECORD_PREFIX_ATTRIBUTE_A && type <= RECORD_PREFIX_ATTRIBUTE_Z)
    {
        unsigned char ch = type - RECORD_PREFIX_ATTRIBUTE_A + 'a';
        if (!(attr->prefix = alloc_xml_string( &ch, 1 )))
        {
            hr = E_OUTOFMEMORY;
            goto error;
        }
        if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
        if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
    }
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
    else if (type >= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A && type <= RECORD_PREFIX_DICTIONARY_ATTRIBUTE_Z)
    {
        unsigned char ch = type - RECORD_PREFIX_DICTIONARY_ATTRIBUTE_A + 'a';
        if (!(attr->prefix = alloc_xml_string( &ch, 1 )))
        {
            hr = E_OUTOFMEMORY;
            goto error;
        }
        if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
        if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
    }
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
    else
    {
        switch (type)
        {
        case RECORD_SHORT_ATTRIBUTE:
            if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
            if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
            if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
            break;

        case RECORD_ATTRIBUTE:
            if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
            if ((hr = read_string( reader, &attr->localName )) != S_OK) goto error;
            if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
            break;

1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
        case RECORD_SHORT_DICTIONARY_ATTRIBUTE:
            if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
            if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
            if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
            break;

        case RECORD_DICTIONARY_ATTRIBUTE:
            if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
            if ((hr = read_dict_string( reader, &attr->localName )) != S_OK) goto error;
            if ((hr = read_attribute_value_bin( reader, attr )) != S_OK) goto error;
            break;

1835 1836 1837 1838 1839 1840
        case RECORD_SHORT_XMLNS_ATTRIBUTE:
            if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
1841 1842 1843 1844 1845
            if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
1846
            if ((hr = read_string( reader, &attr->ns )) != S_OK) goto error;
1847
            if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
1848 1849 1850 1851 1852
            attr->isXmlNs = 1;
            break;

        case RECORD_XMLNS_ATTRIBUTE:
            if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1853 1854 1855 1856 1857
            if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
1858 1859 1860 1861 1862
            if ((hr = read_string( reader, &attr->ns )) != S_OK) goto error;
            if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
            attr->isXmlNs = 1;
            break;

1863 1864 1865 1866 1867 1868
        case RECORD_SHORT_DICTIONARY_XMLNS_ATTRIBUTE:
            if (!(attr->prefix = alloc_xml_string( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
1869 1870 1871 1872 1873
            if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
1874
            if ((hr = read_dict_string( reader, &attr->ns )) != S_OK) goto error;
1875 1876 1877 1878 1879 1880
            if (!(utf8 = alloc_utf8_text( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
            attr->value = &utf8->text;
1881 1882 1883 1884 1885 1886
            if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
            attr->isXmlNs = 1;
            break;

        case RECORD_DICTIONARY_XMLNS_ATTRIBUTE:
            if ((hr = read_string( reader, &attr->prefix )) != S_OK) goto error;
1887 1888 1889 1890 1891
            if (!(attr->localName = get_xmlns_localname( reader, attr->prefix )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
1892
            if ((hr = read_dict_string( reader, &attr->ns )) != S_OK) goto error;
1893 1894 1895 1896 1897 1898
            if (!(utf8 = alloc_utf8_text( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
            attr->value = &utf8->text;
1899 1900 1901 1902
            if ((hr = bind_prefix( reader, attr->prefix, attr->ns )) != S_OK) goto error;
            attr->isXmlNs = 1;
            break;

1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
        default:
            ERR( "unhandled record type %02x\n", type );
            return WS_E_NOT_SUPPORTED;
        }
    }

    *ret = attr;
    return S_OK;

error:
    free_attribute( attr );
    return hr;
}

1917
static inline struct node *find_parent( struct reader *reader )
1918
{
1919 1920 1921 1922 1923
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_END_ELEMENT)
    {
        if (is_valid_parent( reader->current->parent->parent )) return reader->current->parent->parent;
        return NULL;
    }
1924 1925
    if (is_valid_parent( reader->current )) return reader->current;
    if (is_valid_parent( reader->current->parent )) return reader->current->parent;
1926 1927 1928
    return NULL;
}

1929 1930 1931 1932 1933 1934 1935
static HRESULT set_namespaces( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
{
    static const WS_XML_STRING xml = {3, (BYTE *)"xml"};
    const WS_XML_STRING *ns;
    ULONG i;

    if (!(ns = get_namespace( reader, elem->prefix ))) return WS_E_INVALID_FORMAT;
1936
    if (!(elem->ns = dup_xml_string( ns, FALSE ))) return E_OUTOFMEMORY;
1937 1938 1939 1940 1941 1942

    for (i = 0; i < elem->attributeCount; i++)
    {
        WS_XML_ATTRIBUTE *attr = elem->attributes[i];
        if (attr->isXmlNs || WsXmlStringEquals( attr->prefix, &xml, NULL ) == S_OK) continue;
        if (!(ns = get_namespace( reader, attr->prefix ))) return WS_E_INVALID_FORMAT;
1943
        if (!(attr->ns = alloc_xml_string( NULL, ns->length ))) return E_OUTOFMEMORY;
1944
        if (attr->ns->length) memcpy( attr->ns->bytes, ns->bytes, ns->length );
1945 1946 1947 1948
    }
    return S_OK;
}

1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
static WS_XML_ELEMENT_NODE *alloc_element_pair(void)
{
    struct node *node, *end;
    if (!(node = alloc_node( WS_XML_NODE_TYPE_ELEMENT ))) return NULL;
    if (!(end = alloc_node( WS_XML_NODE_TYPE_END_ELEMENT )))
    {
        free_node( node );
        return NULL;
    }
    list_add_tail( &node->children, &end->entry );
    end->parent = node;
    return &node->hdr;
}

static HRESULT read_attributes_text( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
{
    WS_XML_ATTRIBUTE *attr;
    HRESULT hr;

    reader->current_attr = 0;
    for (;;)
    {
        read_skip_whitespace( reader );
1972
        if (read_cmp( reader, ">", 1 ) == S_OK || read_cmp( reader, "/>", 2 ) == S_OK) break;
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
        if ((hr = read_attribute_text( reader, &attr )) != S_OK) return hr;
        if ((hr = append_attribute( elem, attr )) != S_OK)
        {
            free_attribute( attr );
            return hr;
        }
        reader->current_attr++;
    }
    return S_OK;
}

static HRESULT read_element_text( struct reader *reader )
1985 1986
{
    unsigned int len = 0, ch, skip;
1987
    const unsigned char *start;
1988
    unsigned char buf[2];
1989
    struct node *node = NULL, *parent;
1990
    WS_XML_ELEMENT_NODE *elem;
1991
    HRESULT hr;
1992 1993 1994

    if (read_end_of_data( reader ))
    {
1995 1996
        reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
        reader->last    = reader->current;
1997 1998 1999 2000
        reader->state   = READER_STATE_EOF;
        return S_OK;
    }

2001 2002
    if ((hr = read_peek( reader, buf, 2 )) != S_OK) return hr;
    if (buf[0] != '<' || !read_isnamechar( buf[1] )) return WS_E_INVALID_FORMAT;
2003 2004
    read_skip( reader, 1 );

2005 2006 2007
    if (!(elem = alloc_element_pair())) return E_OUTOFMEMORY;
    node = (struct node *)elem;

2008 2009 2010
    start = read_current_ptr( reader );
    for (;;)
    {
2011
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) goto error;
2012 2013 2014 2015
        if (!read_isnamechar( ch )) break;
        read_skip( reader, skip );
        len += skip;
    }
2016 2017 2018 2019 2020
    if (!len)
    {
        hr = WS_E_INVALID_FORMAT;
        goto error;
    }
2021

2022
    if (!(parent = find_parent( reader ))) goto error;
2023
    if ((hr = parse_qname( start, len, &elem->prefix, &elem->localName )) != S_OK) goto error;
2024 2025
    if ((hr = read_attributes_text( reader, elem )) != S_OK) goto error;
    if ((hr = set_namespaces( reader, elem )) != S_OK) goto error;
2026

2027 2028 2029
    read_insert_node( reader, parent, node );
    reader->state = READER_STATE_STARTELEMENT;
    return S_OK;
2030

2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
error:
    destroy_nodes( node );
    return hr;
}

static inline BOOL is_element_type( unsigned char type )
{
    return (type >= RECORD_SHORT_ELEMENT && type <= RECORD_PREFIX_ELEMENT_Z);
}

2041 2042 2043 2044 2045 2046 2047 2048 2049
static HRESULT read_attributes_bin( struct reader *reader, WS_XML_ELEMENT_NODE *elem )
{
    WS_XML_ATTRIBUTE *attr;
    unsigned char type;
    HRESULT hr;

    reader->current_attr = 0;
    for (;;)
    {
2050
        if ((hr = read_peek( reader, &type, 1 )) != S_OK) return hr;
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
        if (!is_attribute_type( type )) break;
        if ((hr = read_attribute_bin( reader, &attr )) != S_OK) return hr;
        if ((hr = append_attribute( elem, attr )) != S_OK)
        {
            free_attribute( attr );
            return hr;
        }
        reader->current_attr++;
    }
    return S_OK;
}

2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
static HRESULT read_element_bin( struct reader *reader )
{
    struct node *node = NULL, *parent;
    WS_XML_ELEMENT_NODE *elem;
    unsigned char type;
    HRESULT hr;

    if ((hr = read_byte( reader, &type )) != S_OK) return hr;
    if (!is_element_type( type )) return WS_E_INVALID_FORMAT;

    if (!(elem = alloc_element_pair())) return E_OUTOFMEMORY;
    node = (struct node *)elem;

    if (type >= RECORD_PREFIX_ELEMENT_A && type <= RECORD_PREFIX_ELEMENT_Z)
2077
    {
2078 2079
        unsigned char ch = type - RECORD_PREFIX_ELEMENT_A + 'a';
        if (!(elem->prefix = alloc_xml_string( &ch, 1 )))
2080
        {
2081
            hr = E_OUTOFMEMORY;
2082 2083
            goto error;
        }
2084
        if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
2085
    }
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
    else if (type >= RECORD_PREFIX_DICTIONARY_ELEMENT_A && type <= RECORD_PREFIX_DICTIONARY_ELEMENT_Z)
    {
        unsigned char ch = type - RECORD_PREFIX_DICTIONARY_ELEMENT_A + 'a';
        if (!(elem->prefix = alloc_xml_string( &ch, 1 )))
        {
            hr = E_OUTOFMEMORY;
            goto error;
        }
        if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
    }
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
    else
    {
        switch (type)
        {
        case RECORD_SHORT_ELEMENT:
            if (!(elem->prefix = alloc_xml_string( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
            if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
            break;

        case RECORD_ELEMENT:
            if ((hr = read_string( reader, &elem->prefix )) != S_OK) goto error;
            if ((hr = read_string( reader, &elem->localName )) != S_OK) goto error;
            break;

2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
        case RECORD_SHORT_DICTIONARY_ELEMENT:
            if (!(elem->prefix = alloc_xml_string( NULL, 0 )))
            {
                hr = E_OUTOFMEMORY;
                goto error;
            }
            if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
            break;

        case RECORD_DICTIONARY_ELEMENT:
            if ((hr = read_string( reader, &elem->prefix )) != S_OK) goto error;
            if ((hr = read_dict_string( reader, &elem->localName )) != S_OK) goto error;
            break;

2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
        default:
            ERR( "unhandled record type %02x\n", type );
            return WS_E_NOT_SUPPORTED;
        }
    }

    if (!(parent = find_parent( reader )))
    {
        hr = WS_E_INVALID_FORMAT;
        goto error;
    }

2140
    if ((hr = read_attributes_bin( reader, elem )) != S_OK) goto error;
2141
    if ((hr = set_namespaces( reader, elem )) != S_OK) goto error;
2142

2143 2144
    read_insert_node( reader, parent, node );
    reader->state = READER_STATE_STARTELEMENT;
2145 2146 2147
    return S_OK;

error:
2148
    destroy_nodes( node );
2149 2150 2151
    return hr;
}

2152
static HRESULT read_text_text( struct reader *reader )
2153 2154
{
    unsigned int len = 0, ch, skip;
2155
    const unsigned char *start;
2156
    struct node *node, *parent;
2157 2158
    WS_XML_TEXT_NODE *text;
    WS_XML_UTF8_TEXT *utf8;
2159
    HRESULT hr;
2160 2161 2162 2163 2164

    start = read_current_ptr( reader );
    for (;;)
    {
        if (read_end_of_data( reader )) break;
2165
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2166 2167 2168 2169 2170
        if (ch == '<') break;
        read_skip( reader, skip );
        len += skip;
    }

2171
    if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2172

2173 2174
    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return E_OUTOFMEMORY;
    text = (WS_XML_TEXT_NODE *)node;
2175
    if (!(utf8 = alloc_utf8_text( NULL, len )))
2176 2177 2178 2179
    {
        heap_free( node );
        return E_OUTOFMEMORY;
    }
2180 2181 2182 2183 2184 2185
    if ((hr = decode_text( start, len, utf8->value.bytes, &utf8->value.length )) != S_OK)
    {
        heap_free( utf8 );
        heap_free( node );
        return hr;
    }
2186
    text->text = &utf8->text;
2187

2188
    read_insert_node( reader, parent, node );
2189
    reader->state = READER_STATE_TEXT;
2190
    reader->text_conv_offset = 0;
2191 2192 2193
    return S_OK;
}

2194
static struct node *alloc_utf8_text_node( const BYTE *data, ULONG len, WS_XML_UTF8_TEXT **ret )
2195
{
2196
    struct node *node;
2197
    WS_XML_UTF8_TEXT *utf8;
2198 2199 2200 2201 2202 2203 2204 2205
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(utf8 = alloc_utf8_text( data, len )))
    {
        heap_free( node );
        return NULL;
    }
2206
    text = (WS_XML_TEXT_NODE *)node;
2207
    text->text = &utf8->text;
2208
    if (ret) *ret = utf8;
2209 2210 2211
    return node;
}

2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
static struct node *alloc_base64_text_node( const BYTE *data, ULONG len, WS_XML_BASE64_TEXT **ret )
{
    struct node *node;
    WS_XML_BASE64_TEXT *base64;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(base64 = alloc_base64_text( data, len )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &base64->text;
    if (ret) *ret = base64;
    return node;
}

2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
static struct node *alloc_bool_text_node( BOOL value )
{
    struct node *node;
    WS_XML_BOOL_TEXT *text_bool;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_bool = alloc_bool_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_bool->text;
    return node;
}

static struct node *alloc_int32_text_node( INT32 value )
{
    struct node *node;
    WS_XML_INT32_TEXT *text_int32;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_int32 = alloc_int32_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_int32->text;
    return node;
}

static struct node *alloc_int64_text_node( INT64 value )
{
    struct node *node;
    WS_XML_INT64_TEXT *text_int64;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_int64 = alloc_int64_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_int64->text;
    return node;
}

2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
static struct node *alloc_float_text_node( float value )
{
    struct node *node;
    WS_XML_FLOAT_TEXT *text_float;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_float = alloc_float_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_float->text;
    return node;
}

2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
static struct node *alloc_double_text_node( double value )
{
    struct node *node;
    WS_XML_DOUBLE_TEXT *text_double;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_double = alloc_double_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_double->text;
    return node;
}

static struct node *alloc_datetime_text_node( const WS_DATETIME *value )
{
    struct node *node;
    WS_XML_DATETIME_TEXT *text_datetime;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_datetime = alloc_datetime_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_datetime->text;
    return node;
}

static struct node *alloc_unique_id_text_node( const GUID *value )
{
    struct node *node;
    WS_XML_UNIQUE_ID_TEXT *text_unique_id;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_unique_id = alloc_unique_id_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_unique_id->text;
    return node;
}

static struct node *alloc_guid_text_node( const GUID *value )
{
    struct node *node;
    WS_XML_GUID_TEXT *text_guid;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_guid = alloc_guid_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_guid->text;
    return node;
}

static struct node *alloc_uint64_text_node( UINT64 value )
{
    struct node *node;
    WS_XML_UINT64_TEXT *text_uint64;
    WS_XML_TEXT_NODE *text;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return NULL;
    if (!(text_uint64 = alloc_uint64_text( value )))
    {
        heap_free( node );
        return NULL;
    }
    text = (WS_XML_TEXT_NODE *)node;
    text->text = &text_uint64->text;
    return node;
}

2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
static HRESULT append_text_bytes( struct reader *reader, WS_XML_TEXT_NODE *node, ULONG len )
{
    WS_XML_BASE64_TEXT *new, *old = (WS_XML_BASE64_TEXT *)node->text;
    HRESULT hr;

    if (!(new = alloc_base64_text( NULL, old->length + len ))) return E_OUTOFMEMORY;
    memcpy( new->bytes, old->bytes, old->length );
    if ((hr = read_bytes( reader, new->bytes + old->length, len )) != S_OK) return hr;
    heap_free( old );
    node->text = &new->text;
    return S_OK;
}

static HRESULT read_text_bytes( struct reader *reader, unsigned char type )
{
    struct node *node = NULL, *parent;
    WS_XML_BASE64_TEXT *base64;
    HRESULT hr;
    ULONG len;

    if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
    for (;;)
    {
        switch (type)
        {
        case RECORD_BYTES8_TEXT:
        case RECORD_BYTES8_TEXT_WITH_ENDELEMENT:
        {
            UINT8 len_uint8;
            if ((hr = read_byte( reader, (unsigned char *)&len_uint8 )) != S_OK) goto error;
            len = len_uint8;
            break;
        }
        case RECORD_BYTES16_TEXT:
        case RECORD_BYTES16_TEXT_WITH_ENDELEMENT:
        {
            UINT16 len_uint16;
            if ((hr = read_bytes( reader, (unsigned char *)&len_uint16, sizeof(len_uint16) )) != S_OK) goto error;
            len = len_uint16;
            break;
        }
        case RECORD_BYTES32_TEXT:
        case RECORD_BYTES32_TEXT_WITH_ENDELEMENT:
        {
            INT32 len_int32;
            if ((hr = read_bytes( reader, (unsigned char *)&len_int32, sizeof(len_int32) )) != S_OK) goto error;
            if (len_int32 < 0)
            {
                hr = WS_E_INVALID_FORMAT;
                goto error;
            }
            len = len_int32;
            break;
        }
        default:
            ERR( "unexpected type %u\n", type );
            hr = E_INVALIDARG;
            goto error;
        }

        if (!node)
        {
            if (!(node = alloc_base64_text_node( NULL, len, &base64 ))) return E_OUTOFMEMORY;
            if ((hr = read_bytes( reader, base64->bytes, len )) != S_OK) goto error;
        }
        else if ((hr = append_text_bytes( reader, (WS_XML_TEXT_NODE *)node, len )) != S_OK) goto error;

        if (type & 1)
        {
            node->flags |= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT;
            break;
        }
2455
        if ((hr = read_peek( reader, &type, 1 )) != S_OK) goto error;
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
        if (type < RECORD_BYTES8_TEXT || type > RECORD_BYTES32_TEXT_WITH_ENDELEMENT) break;
        read_skip( reader, 1 );
    }

    read_insert_node( reader, parent, node );
    reader->state = READER_STATE_TEXT;
    reader->text_conv_offset = 0;
    return S_OK;

error:
    free_node( node );
    return hr;
}

2470 2471 2472
static HRESULT read_text_bin( struct reader *reader )
{
    struct node *node = NULL, *parent;
2473
    unsigned char type;
2474 2475 2476 2477 2478 2479
    WS_XML_UTF8_TEXT *utf8;
    INT32 val_int32;
    UINT8 val_uint8;
    UINT16 val_uint16;
    ULONG len, id;
    GUID uuid;
2480 2481 2482
    HRESULT hr;

    if ((hr = read_byte( reader, &type )) != S_OK) return hr;
2483
    if (!is_text_type( type ) || !(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2484 2485 2486

    switch (type)
    {
2487 2488
    case RECORD_ZERO_TEXT:
    case RECORD_ZERO_TEXT_WITH_ENDELEMENT:
2489
        if (!(node = alloc_int32_text_node( 0 ))) return E_OUTOFMEMORY;
2490 2491 2492 2493
        break;

    case RECORD_ONE_TEXT:
    case RECORD_ONE_TEXT_WITH_ENDELEMENT:
2494
        if (!(node = alloc_int32_text_node( 1 ))) return E_OUTOFMEMORY;
2495 2496 2497 2498
        break;

    case RECORD_FALSE_TEXT:
    case RECORD_FALSE_TEXT_WITH_ENDELEMENT:
2499
        if (!(node = alloc_bool_text_node( FALSE ))) return E_OUTOFMEMORY;
2500 2501 2502 2503
        break;

    case RECORD_TRUE_TEXT:
    case RECORD_TRUE_TEXT_WITH_ENDELEMENT:
2504
        if (!(node = alloc_bool_text_node( TRUE ))) return E_OUTOFMEMORY;
2505 2506
        break;

2507 2508
    case RECORD_INT8_TEXT:
    case RECORD_INT8_TEXT_WITH_ENDELEMENT:
2509 2510
    {
        INT8 val_int8;
2511
        if ((hr = read_byte( reader, (unsigned char *)&val_int8 )) != S_OK) return hr;
2512
        if (!(node = alloc_int32_text_node( val_int8 ))) return E_OUTOFMEMORY;
2513
        break;
2514
    }
2515 2516
    case RECORD_INT16_TEXT:
    case RECORD_INT16_TEXT_WITH_ENDELEMENT:
2517 2518
    {
        INT16 val_int16;
2519
        if ((hr = read_bytes( reader, (unsigned char *)&val_int16, sizeof(val_int16) )) != S_OK) return hr;
2520
        if (!(node = alloc_int32_text_node( val_int16 ))) return E_OUTOFMEMORY;
2521
        break;
2522
    }
2523 2524 2525
    case RECORD_INT32_TEXT:
    case RECORD_INT32_TEXT_WITH_ENDELEMENT:
        if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
2526
        if (!(node = alloc_int32_text_node( val_int32 ))) return E_OUTOFMEMORY;
2527 2528 2529 2530
        break;

    case RECORD_INT64_TEXT:
    case RECORD_INT64_TEXT_WITH_ENDELEMENT:
2531 2532
    {
        INT64 val_int64;
2533
        if ((hr = read_bytes( reader, (unsigned char *)&val_int64, sizeof(val_int64) )) != S_OK) return hr;
2534
        if (!(node = alloc_int64_text_node( val_int64 ))) return E_OUTOFMEMORY;
2535
        break;
2536
    }
2537 2538 2539 2540 2541 2542 2543 2544
    case RECORD_FLOAT_TEXT:
    case RECORD_FLOAT_TEXT_WITH_ENDELEMENT:
    {
        float val_float;
        if ((hr = read_bytes( reader, (unsigned char *)&val_float, sizeof(val_float) )) != S_OK) return hr;
        if (!(node = alloc_float_text_node( val_float ))) return E_OUTOFMEMORY;
        break;
    }
2545 2546
    case RECORD_DOUBLE_TEXT:
    case RECORD_DOUBLE_TEXT_WITH_ENDELEMENT:
2547 2548
    {
        double val_double;
2549
        if ((hr = read_bytes( reader, (unsigned char *)&val_double, sizeof(val_double) )) != S_OK) return hr;
2550
        if (!(node = alloc_double_text_node( val_double ))) return E_OUTOFMEMORY;
2551
        break;
2552
    }
2553 2554
    case RECORD_DATETIME_TEXT:
    case RECORD_DATETIME_TEXT_WITH_ENDELEMENT:
2555 2556
    {
        WS_DATETIME datetime;
2557
        if ((hr = read_datetime( reader, &datetime )) != S_OK) return hr;
2558
        if (!(node = alloc_datetime_text_node( &datetime ))) return E_OUTOFMEMORY;
2559
        break;
2560
    }
2561
    case RECORD_CHARS8_TEXT:
2562
    case RECORD_CHARS8_TEXT_WITH_ENDELEMENT:
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
        if ((hr = read_byte( reader, (unsigned char *)&val_uint8 )) != S_OK) return hr;
        len = val_uint8;
        break;

    case RECORD_CHARS16_TEXT:
    case RECORD_CHARS16_TEXT_WITH_ENDELEMENT:
        if ((hr = read_bytes( reader, (unsigned char *)&val_uint16, sizeof(val_uint16) )) != S_OK) return hr;
        len = val_uint16;
        break;

    case RECORD_CHARS32_TEXT:
    case RECORD_CHARS32_TEXT_WITH_ENDELEMENT:
        if ((hr = read_bytes( reader, (unsigned char *)&val_int32, sizeof(val_int32) )) != S_OK) return hr;
        if (val_int32 < 0) return WS_E_INVALID_FORMAT;
        len = val_int32;
        break;

    case RECORD_BYTES8_TEXT:
    case RECORD_BYTES8_TEXT_WITH_ENDELEMENT:
    case RECORD_BYTES16_TEXT:
    case RECORD_BYTES16_TEXT_WITH_ENDELEMENT:
    case RECORD_BYTES32_TEXT:
    case RECORD_BYTES32_TEXT_WITH_ENDELEMENT:
2586
        return read_text_bytes( reader, type );
2587 2588 2589 2590 2591 2592 2593 2594

    case RECORD_EMPTY_TEXT:
    case RECORD_EMPTY_TEXT_WITH_ENDELEMENT:
        len = 0;
        break;

    case RECORD_DICTIONARY_TEXT:
    case RECORD_DICTIONARY_TEXT_WITH_ENDELEMENT:
2595 2596
    {
        const WS_XML_STRING *str;
2597
        if ((hr = read_int31( reader, &id )) != S_OK) return hr;
2598
        if ((hr = lookup_string( reader, id, &str )) != S_OK) return hr;
2599
        if (!(node = alloc_utf8_text_node( str->bytes, str->length, NULL ))) return E_OUTOFMEMORY;
2600
        break;
2601
    }
2602 2603
    case RECORD_UNIQUE_ID_TEXT:
    case RECORD_UNIQUE_ID_TEXT_WITH_ENDELEMENT:
2604
        if ((hr = read_bytes( reader, (unsigned char *)&uuid, sizeof(uuid) )) != S_OK) return hr;
2605
        if (!(node = alloc_unique_id_text_node( &uuid ))) return E_OUTOFMEMORY;
2606 2607
        break;

2608 2609
    case RECORD_GUID_TEXT:
    case RECORD_GUID_TEXT_WITH_ENDELEMENT:
2610
        if ((hr = read_bytes( reader, (unsigned char *)&uuid, sizeof(uuid) )) != S_OK) return hr;
2611
        if (!(node = alloc_guid_text_node( &uuid ))) return E_OUTOFMEMORY;
2612 2613 2614 2615
        break;

    case RECORD_UINT64_TEXT:
    case RECORD_UINT64_TEXT_WITH_ENDELEMENT:
2616 2617
    {
        UINT64 val_uint64;
2618
        if ((hr = read_bytes( reader, (unsigned char *)&val_uint64, sizeof(val_uint64) )) != S_OK) return hr;
2619
        if (!(node = alloc_uint64_text_node( val_uint64 ))) return E_OUTOFMEMORY;
2620
        break;
2621
    }
2622 2623
    case RECORD_BOOL_TEXT:
    case RECORD_BOOL_TEXT_WITH_ENDELEMENT:
2624 2625
    {
        BOOL val_bool;
2626
        if ((hr = read_bytes( reader, (unsigned char *)&val_bool, sizeof(val_bool) )) != S_OK) return hr;
2627
        if (!(node = alloc_bool_text_node( !!val_bool ))) return E_OUTOFMEMORY;
2628
        break;
2629
    }
2630 2631 2632 2633 2634
    default:
        ERR( "unhandled record type %02x\n", type );
        return WS_E_NOT_SUPPORTED;
    }

2635
    if (!node)
2636
    {
2637
        if (!(node = alloc_utf8_text_node( NULL, len, &utf8 ))) return E_OUTOFMEMORY;
2638
        if (!len) utf8->value.bytes = (BYTE *)(utf8 + 1); /* quirk */
2639
        if ((hr = read_bytes( reader, utf8->value.bytes, len )) != S_OK)
2640 2641 2642 2643
        {
            free_node( node );
            return hr;
        }
2644 2645
    }

2646
    if (type & 1) node->flags |= NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT;
2647 2648 2649 2650 2651 2652
    read_insert_node( reader, parent, node );
    reader->state = READER_STATE_TEXT;
    reader->text_conv_offset = 0;
    return S_OK;
}

2653
static HRESULT read_node_text( struct reader * );
2654

2655
static HRESULT read_startelement_text( struct reader *reader )
2656
{
2657
    read_skip_whitespace( reader );
2658
    if (read_cmp( reader, "/>", 2 ) == S_OK)
2659 2660
    {
        read_skip( reader, 2 );
2661 2662 2663
        reader->current = LIST_ENTRY( list_tail( &reader->current->children ), struct node, entry );
        reader->last    = reader->current;
        reader->state   = READER_STATE_ENDELEMENT;
2664 2665
        return S_OK;
    }
2666
    else if (read_cmp( reader, ">", 1 ) == S_OK)
2667 2668
    {
        read_skip( reader, 1 );
2669
        return read_node_text( reader );
2670 2671
    }
    return WS_E_INVALID_FORMAT;
2672 2673
}

2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
static HRESULT read_node_bin( struct reader * );

static HRESULT read_startelement_bin( struct reader *reader )
{
    if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return WS_E_INVALID_FORMAT;
    return read_node_bin( reader );
}

static HRESULT read_startelement( struct reader *reader )
{
    switch (reader->input_enc)
    {
    case WS_XML_READER_ENCODING_TYPE_TEXT:   return read_startelement_text( reader );
    case WS_XML_READER_ENCODING_TYPE_BINARY: return read_startelement_bin( reader );
    default:
        ERR( "unhandled encoding %u\n", reader->input_enc );
        return WS_E_NOT_SUPPORTED;
    }
}

static HRESULT read_to_startelement_text( struct reader *reader, BOOL *found )
2695 2696 2697 2698 2699 2700 2701 2702
{
    HRESULT hr;

    switch (reader->state)
    {
    case READER_STATE_INITIAL:
        if ((hr = read_xmldecl( reader )) != S_OK) return hr;
        break;
2703

2704 2705 2706 2707 2708 2709 2710 2711 2712
    case READER_STATE_STARTELEMENT:
        if (found) *found = TRUE;
        return S_OK;

    default:
        break;
    }

    read_skip_whitespace( reader );
2713
    if ((hr = read_element_text( reader )) == S_OK && found)
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
    {
        if (reader->state == READER_STATE_STARTELEMENT)
            *found = TRUE;
        else
            *found = FALSE;
    }

    return hr;
}

2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
static HRESULT read_to_startelement_bin( struct reader *reader, BOOL *found )
{
    HRESULT hr;

    if (reader->state == READER_STATE_STARTELEMENT)
    {
        if (found) *found = TRUE;
        return S_OK;
    }

    if ((hr = read_element_bin( reader )) == S_OK && found)
    {
        if (reader->state == READER_STATE_STARTELEMENT)
            *found = TRUE;
        else
            *found = FALSE;
    }

    return hr;
}

static HRESULT read_to_startelement( struct reader *reader, BOOL *found )
{
    switch (reader->input_enc)
    {
    case WS_XML_READER_ENCODING_TYPE_TEXT:   return read_to_startelement_text( reader, found );
    case WS_XML_READER_ENCODING_TYPE_BINARY: return read_to_startelement_bin( reader, found );
    default:
        ERR( "unhandled encoding %u\n", reader->input_enc );
        return WS_E_NOT_SUPPORTED;
    }
}

2757 2758 2759 2760 2761 2762 2763 2764
static int cmp_name( const unsigned char *name1, ULONG len1, const unsigned char *name2, ULONG len2 )
{
    ULONG i;
    if (len1 != len2) return 1;
    for (i = 0; i < len1; i++) { if (toupper( name1[i] ) != toupper( name2[i] )) return 1; }
    return 0;
}

2765 2766
static struct node *find_startelement( struct reader *reader, const WS_XML_STRING *prefix,
                                       const WS_XML_STRING *localname )
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
{
    struct node *parent;
    const WS_XML_STRING *str;

    for (parent = reader->current; parent; parent = parent->parent)
    {
        if (node_type( parent ) == WS_XML_NODE_TYPE_ELEMENT)
        {
            str = parent->hdr.prefix;
            if (cmp_name( str->bytes, str->length, prefix->bytes, prefix->length )) continue;
            str = parent->hdr.localName;
            if (cmp_name( str->bytes, str->length, localname->bytes, localname->length )) continue;
            return parent;
       }
    }
    return NULL;
}

2785
static HRESULT read_endelement_text( struct reader *reader )
2786
{
2787
    struct node *parent;
2788
    unsigned int len = 0, ch, skip;
2789
    const unsigned char *start;
2790
    WS_XML_STRING prefix, localname;
2791
    HRESULT hr;
2792

2793
    if ((hr = read_cmp( reader, "</", 2 )) != S_OK) return hr;
2794 2795
    read_skip( reader, 2 );

2796
    start = read_current_ptr( reader );
2797 2798
    for (;;)
    {
2799
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2800 2801 2802 2803 2804 2805 2806
        if (ch == '>')
        {
            read_skip( reader, 1 );
            break;
        }
        if (!read_isnamechar( ch )) return WS_E_INVALID_FORMAT;
        read_skip( reader, skip );
2807
        len += skip;
2808
    }
2809

2810 2811
    if ((hr = split_qname( start, len, &prefix, &localname )) != S_OK) return hr;
    if (!(parent = find_startelement( reader, &prefix, &localname ))) return WS_E_INVALID_FORMAT;
2812

2813 2814 2815
    reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
    reader->last    = reader->current;
    reader->state   = READER_STATE_ENDELEMENT;
2816 2817 2818
    return S_OK;
}

2819 2820 2821 2822 2823 2824
static HRESULT read_endelement_bin( struct reader *reader )
{
    struct node *parent;
    unsigned char type;
    HRESULT hr;

2825 2826 2827 2828 2829
    if (!(reader->current->flags & NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT))
    {
        if ((hr = read_byte( reader, &type )) != S_OK) return hr;
        if (type != RECORD_ENDELEMENT) return WS_E_INVALID_FORMAT;
    }
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
    if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;

    reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
    reader->last    = reader->current;
    reader->state   = READER_STATE_ENDELEMENT;
    return S_OK;
}

static HRESULT read_endelement( struct reader *reader )
{
    if (reader->state == READER_STATE_EOF) return WS_E_INVALID_FORMAT;

    if (read_end_of_data( reader ))
    {
        reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
        reader->last    = reader->current;
        reader->state   = READER_STATE_EOF;
        return S_OK;
    }

    switch (reader->input_enc)
    {
    case WS_XML_READER_ENCODING_TYPE_TEXT:   return read_endelement_text( reader );
    case WS_XML_READER_ENCODING_TYPE_BINARY: return read_endelement_bin( reader );
    default:
        ERR( "unhandled encoding %u\n", reader->input_enc );
        return WS_E_NOT_SUPPORTED;
    }
}

2860
static HRESULT read_comment_text( struct reader *reader )
2861 2862
{
    unsigned int len = 0, ch, skip;
2863
    const unsigned char *start;
2864
    struct node *node, *parent;
2865
    WS_XML_COMMENT_NODE *comment;
2866
    HRESULT hr;
2867

2868
    if ((hr = read_cmp( reader, "<!--", 4 )) != S_OK) return hr;
2869 2870 2871 2872 2873
    read_skip( reader, 4 );

    start = read_current_ptr( reader );
    for (;;)
    {
2874
        if (read_cmp( reader, "-->", 3 ) == S_OK)
2875 2876 2877 2878
        {
            read_skip( reader, 3 );
            break;
        }
2879
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2880 2881 2882 2883
        read_skip( reader, skip );
        len += skip;
    }

2884
    if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2885

2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
    if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return E_OUTOFMEMORY;
    comment = (WS_XML_COMMENT_NODE *)node;
    if (!(comment->value.bytes = heap_alloc( len )))
    {
        heap_free( node );
        return E_OUTOFMEMORY;
    }
    memcpy( comment->value.bytes, start, len );
    comment->value.length = len;

2896
    read_insert_node( reader, parent, node );
2897
    reader->state = READER_STATE_COMMENT;
2898 2899 2900
    return S_OK;
}

2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933
static HRESULT read_comment_bin( struct reader *reader )
{
    struct node *node, *parent;
    WS_XML_COMMENT_NODE *comment;
    unsigned char type;
    ULONG len;
    HRESULT hr;

    if ((hr = read_byte( reader, &type )) != S_OK) return hr;
    if (type != RECORD_COMMENT) return WS_E_INVALID_FORMAT;
    if ((hr = read_int31( reader, &len )) != S_OK) return hr;

    if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;

    if (!(node = alloc_node( WS_XML_NODE_TYPE_COMMENT ))) return E_OUTOFMEMORY;
    comment = (WS_XML_COMMENT_NODE *)node;
    if (!(comment->value.bytes = heap_alloc( len )))
    {
        heap_free( node );
        return E_OUTOFMEMORY;
    }
    if ((hr = read_bytes( reader, comment->value.bytes, len )) != S_OK)
    {
        free_node( node );
        return E_OUTOFMEMORY;
    }
    comment->value.length = len;

    read_insert_node( reader, parent, node );
    reader->state = READER_STATE_COMMENT;
    return S_OK;
}

2934 2935
static HRESULT read_startcdata( struct reader *reader )
{
2936
    struct node *node, *endnode, *parent;
2937
    HRESULT hr;
2938

2939
    if ((hr = read_cmp( reader, "<![CDATA[", 9 )) != S_OK) return hr;
2940 2941
    read_skip( reader, 9 );

2942
    if (!(parent = find_parent( reader ))) return WS_E_INVALID_FORMAT;
2943

2944
    if (!(node = alloc_node( WS_XML_NODE_TYPE_CDATA ))) return E_OUTOFMEMORY;
2945 2946 2947 2948 2949 2950 2951 2952
    if (!(endnode = alloc_node( WS_XML_NODE_TYPE_END_CDATA )))
    {
        heap_free( node );
        return E_OUTOFMEMORY;
    }
    list_add_tail( &node->children, &endnode->entry );
    endnode->parent = node;

2953
    read_insert_node( reader, parent, node );
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
    reader->state = READER_STATE_STARTCDATA;
    return S_OK;
}

static HRESULT read_cdata( struct reader *reader )
{
    unsigned int len = 0, ch, skip;
    const unsigned char *start;
    struct node *node;
    WS_XML_TEXT_NODE *text;
    WS_XML_UTF8_TEXT *utf8;
2965
    HRESULT hr;
2966 2967 2968 2969

    start = read_current_ptr( reader );
    for (;;)
    {
2970
        if (read_cmp( reader, "]]>", 3 ) == S_OK) break;
2971
        if ((hr = read_utf8_char( reader, &ch, &skip )) != S_OK) return hr;
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991
        read_skip( reader, skip );
        len += skip;
    }

    if (!(node = alloc_node( WS_XML_NODE_TYPE_TEXT ))) return E_OUTOFMEMORY;
    text = (WS_XML_TEXT_NODE *)node;
    if (!(utf8 = alloc_utf8_text( start, len )))
    {
        heap_free( node );
        return E_OUTOFMEMORY;
    }
    text->text = &utf8->text;

    read_insert_node( reader, reader->current, node );
    reader->state = READER_STATE_CDATA;
    return S_OK;
}

static HRESULT read_endcdata( struct reader *reader )
{
2992
    struct node *parent;
2993
    HRESULT hr;
2994

2995
    if ((hr = read_cmp( reader, "]]>", 3 )) != S_OK) return hr;
2996 2997
    read_skip( reader, 3 );

2998 2999 3000 3001 3002 3003
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT) parent = reader->current->parent;
    else parent = reader->current;

    reader->current = LIST_ENTRY( list_tail( &parent->children ), struct node, entry );
    reader->last    = reader->current;
    reader->state   = READER_STATE_ENDCDATA;
3004 3005 3006
    return S_OK;
}

3007
static HRESULT read_node_text( struct reader *reader )
3008 3009 3010 3011 3012 3013 3014
{
    HRESULT hr;

    for (;;)
    {
        if (read_end_of_data( reader ))
        {
3015 3016
            reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
            reader->last    = reader->current;
3017 3018 3019
            reader->state   = READER_STATE_EOF;
            return S_OK;
        }
3020 3021
        if (reader->state == READER_STATE_STARTCDATA) return read_cdata( reader );
        else if (reader->state == READER_STATE_CDATA) return read_endcdata( reader );
3022
        else if (read_cmp( reader, "<?", 2 ) == S_OK)
3023 3024 3025 3026
        {
            hr = read_xmldecl( reader );
            if (FAILED( hr )) return hr;
        }
3027 3028 3029 3030 3031 3032 3033 3034
        else if (read_cmp( reader, "</", 2 ) == S_OK) return read_endelement_text( reader );
        else if (read_cmp( reader, "<![CDATA[", 9 ) == S_OK) return read_startcdata( reader );
        else if (read_cmp( reader, "<!--", 4 ) == S_OK) return read_comment_text( reader );
        else if (read_cmp( reader, "<", 1 ) == S_OK) return read_element_text( reader );
        else if (read_cmp( reader, "/>", 2 ) == S_OK || read_cmp( reader, ">", 1 ) == S_OK)
        {
            return read_startelement_text( reader );
        }
3035
        else return read_text_text( reader );
3036 3037 3038
    }
}

3039 3040 3041 3042 3043
static HRESULT read_node_bin( struct reader *reader )
{
    unsigned char type;
    HRESULT hr;

3044
    if (reader->current->flags & NODE_FLAG_TEXT_WITH_IMPLICIT_END_ELEMENT)
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
    {
        reader->current = LIST_ENTRY( list_tail( &reader->current->parent->children ), struct node, entry );
        reader->last    = reader->current;
        reader->state   = READER_STATE_ENDELEMENT;
        return S_OK;
    }
    if (read_end_of_data( reader ))
    {
        reader->current = LIST_ENTRY( list_tail( &reader->root->children ), struct node, entry );
        reader->last    = reader->current;
        reader->state   = READER_STATE_EOF;
        return S_OK;
    }

3059
    if ((hr = read_peek( reader, &type, 1 )) != S_OK) return hr;
3060 3061 3062 3063
    if (type == RECORD_ENDELEMENT)
    {
        return read_endelement_bin( reader );
    }
3064 3065 3066 3067
    else if (type == RECORD_COMMENT)
    {
        return read_comment_bin( reader );
    }
3068 3069 3070 3071
    else if (type >= RECORD_SHORT_ELEMENT && type <= RECORD_PREFIX_ELEMENT_Z)
    {
        return read_element_bin( reader );
    }
3072 3073 3074 3075
    else if (type >= RECORD_ZERO_TEXT && type <= RECORD_QNAME_DICTIONARY_TEXT_WITH_ENDELEMENT)
    {
        return read_text_bin( reader );
    }
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
    FIXME( "unhandled record type %02x\n", type );
    return WS_E_NOT_SUPPORTED;
}

static HRESULT read_node( struct reader *reader )
{
    switch (reader->input_enc)
    {
    case WS_XML_READER_ENCODING_TYPE_TEXT:   return read_node_text( reader );
    case WS_XML_READER_ENCODING_TYPE_BINARY: return read_node_bin( reader );
    default:
        ERR( "unhandled encoding %u\n", reader->input_enc );
        return WS_E_NOT_SUPPORTED;
    }
}

3092
HRESULT copy_node( WS_XML_READER *handle, WS_XML_WRITER_ENCODING_TYPE enc, struct node **node )
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
{
    struct reader *reader = (struct reader *)handle;
    const struct list *ptr;
    const struct node *start;
    HRESULT hr;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    if (reader->current != reader->root) ptr = &reader->current->entry;
    else /* copy whole tree */
    {
        if (!read_end_of_data( reader ))
        {
            for (;;)
            {
                if ((hr = read_node( reader )) != S_OK) goto done;
                if (node_type( reader->current ) == WS_XML_NODE_TYPE_EOF) break;
            }
        }
        ptr = list_head( &reader->root->children );
    }

    start = LIST_ENTRY( ptr, struct node, entry );
    if (node_type( start ) == WS_XML_NODE_TYPE_EOF) hr = WS_E_INVALID_OPERATION;
3123
    else hr = dup_tree( start, enc, node );
3124 3125 3126 3127 3128 3129

done:
    LeaveCriticalSection( &reader->cs );
    return hr;
}

3130 3131 3132 3133 3134 3135
/**************************************************************************
 *          WsReadEndElement		[webservices.@]
 */
HRESULT WINAPI WsReadEndElement( WS_XML_READER *handle, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3136
    HRESULT hr;
3137 3138 3139 3140 3141

    TRACE( "%p %p\n", handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_endelement( reader );

    LeaveCriticalSection( &reader->cs );
3154
    TRACE( "returning %08x\n", hr );
3155
    return hr;
3156 3157
}

3158 3159 3160 3161 3162 3163
/**************************************************************************
 *          WsReadNode		[webservices.@]
 */
HRESULT WINAPI WsReadNode( WS_XML_READER *handle, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3164
    HRESULT hr;
3165 3166 3167 3168 3169

    TRACE( "%p %p\n", handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_node( reader );

    LeaveCriticalSection( &reader->cs );
3182
    TRACE( "returning %08x\n", hr );
3183
    return hr;
3184 3185
}

3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
static HRESULT skip_node( struct reader *reader )
{
    const struct node *parent;
    HRESULT hr;

    if (node_type( reader->current ) == WS_XML_NODE_TYPE_EOF) return WS_E_INVALID_OPERATION;
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_ELEMENT) parent = reader->current;
    else parent = NULL;

    for (;;)
    {
3197
        if ((hr = read_node( reader )) != S_OK || !parent) break;
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228
        if (node_type( reader->current ) != WS_XML_NODE_TYPE_END_ELEMENT) continue;
        if (reader->current->parent == parent) return read_node( reader );
    }

    return hr;
}

/**************************************************************************
 *          WsSkipNode		[webservices.@]
 */
HRESULT WINAPI WsSkipNode( WS_XML_READER *handle, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
    HRESULT hr;

    TRACE( "%p %p\n", handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = skip_node( reader );

    LeaveCriticalSection( &reader->cs );
3229
    TRACE( "returning %08x\n", hr );
3230 3231 3232
    return hr;
}

3233 3234 3235 3236 3237 3238
/**************************************************************************
 *          WsReadStartElement		[webservices.@]
 */
HRESULT WINAPI WsReadStartElement( WS_XML_READER *handle, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3239
    HRESULT hr;
3240 3241 3242 3243 3244

    TRACE( "%p %p\n", handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_startelement( reader );

    LeaveCriticalSection( &reader->cs );
3257
    TRACE( "returning %08x\n", hr );
3258
    return hr;
3259 3260
}

3261 3262 3263 3264 3265 3266 3267
/**************************************************************************
 *          WsReadToStartElement		[webservices.@]
 */
HRESULT WINAPI WsReadToStartElement( WS_XML_READER *handle, const WS_XML_STRING *localname,
                                     const WS_XML_STRING *ns, BOOL *found, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3268
    HRESULT hr;
3269 3270 3271 3272 3273 3274 3275

    TRACE( "%p %s %s %p %p\n", handle, debugstr_xmlstr(localname), debugstr_xmlstr(ns), found, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
    if (localname || ns) FIXME( "name and/or namespace not verified\n" );

3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_to_startelement( reader, found );

    LeaveCriticalSection( &reader->cs );
3287
    TRACE( "returning %08x\n", hr );
3288
    return hr;
3289 3290
}

3291
BOOL move_to_root_element( struct node *root, struct node **current )
3292 3293 3294 3295
{
    struct list *ptr;
    struct node *node;

3296
    if (!(ptr = list_head( &root->children ))) return FALSE;
3297
    node = LIST_ENTRY( ptr, struct node, entry );
3298
    if (node_type( node ) == WS_XML_NODE_TYPE_ELEMENT)
3299
    {
3300
        *current = node;
3301 3302
        return TRUE;
    }
3303
    while ((ptr = list_next( &root->children, &node->entry )))
3304 3305
    {
        struct node *next = LIST_ENTRY( ptr, struct node, entry );
3306
        if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3307
        {
3308
            *current = next;
3309 3310 3311 3312 3313 3314 3315
            return TRUE;
        }
        node = next;
    }
    return FALSE;
}

3316
BOOL move_to_next_element( struct node **current )
3317 3318
{
    struct list *ptr;
3319
    struct node *node = *current, *parent = (*current)->parent;
3320

3321 3322
    if (!parent) return FALSE;
    while ((ptr = list_next( &parent->children, &node->entry )))
3323 3324
    {
        struct node *next = LIST_ENTRY( ptr, struct node, entry );
3325
        if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3326
        {
3327
            *current = next;
3328 3329 3330 3331 3332 3333 3334
            return TRUE;
        }
        node = next;
    }
    return FALSE;
}

3335
BOOL move_to_prev_element( struct node **current )
3336 3337
{
    struct list *ptr;
3338
    struct node *node = *current, *parent = (*current)->parent;
3339

3340 3341
    if (!parent) return FALSE;
    while ((ptr = list_prev( &parent->children, &node->entry )))
3342 3343
    {
        struct node *prev = LIST_ENTRY( ptr, struct node, entry );
3344
        if (node_type( prev ) == WS_XML_NODE_TYPE_ELEMENT)
3345
        {
3346
            *current = prev;
3347 3348 3349 3350 3351 3352 3353
            return TRUE;
        }
        node = prev;
    }
    return FALSE;
}

3354
BOOL move_to_child_element( struct node **current )
3355 3356
{
    struct list *ptr;
3357
    struct node *child, *node = *current;
3358

3359 3360 3361
    if (!(ptr = list_head( &node->children ))) return FALSE;
    child = LIST_ENTRY( ptr, struct node, entry );
    if (node_type( child ) == WS_XML_NODE_TYPE_ELEMENT)
3362
    {
3363
        *current = child;
3364 3365
        return TRUE;
    }
3366
    while ((ptr = list_next( &node->children, &child->entry )))
3367 3368
    {
        struct node *next = LIST_ENTRY( ptr, struct node, entry );
3369
        if (node_type( next ) == WS_XML_NODE_TYPE_ELEMENT)
3370
        {
3371
            *current = next;
3372 3373
            return TRUE;
        }
3374
        child = next;
3375 3376 3377 3378
    }
    return FALSE;
}

3379
BOOL move_to_end_element( struct node **current )
3380 3381
{
    struct list *ptr;
3382
    struct node *node = *current;
3383

3384
    if (node_type( node ) != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
3385 3386 3387 3388

    if ((ptr = list_tail( &node->children )))
    {
        struct node *tail = LIST_ENTRY( ptr, struct node, entry );
3389
        if (node_type( tail ) == WS_XML_NODE_TYPE_END_ELEMENT)
3390
        {
3391
            *current = tail;
3392 3393 3394 3395 3396 3397
            return TRUE;
        }
    }
    return FALSE;
}

3398
BOOL move_to_parent_element( struct node **current )
3399
{
3400
    struct node *parent = (*current)->parent;
3401

3402 3403
    if (parent && (node_type( parent ) == WS_XML_NODE_TYPE_ELEMENT ||
                   node_type( parent ) == WS_XML_NODE_TYPE_BOF))
3404
    {
3405
        *current = parent;
3406 3407 3408 3409 3410
        return TRUE;
    }
    return FALSE;
}

3411
BOOL move_to_first_node( struct node **current )
3412 3413
{
    struct list *ptr;
3414 3415 3416
    struct node *node = *current;

    if ((ptr = list_head( &node->parent->children )))
3417
    {
3418
        *current = LIST_ENTRY( ptr, struct node, entry );
3419 3420 3421 3422 3423
        return TRUE;
    }
    return FALSE;
}

3424
BOOL move_to_next_node( struct node **current )
3425 3426
{
    struct list *ptr;
3427 3428 3429
    struct node *node = *current;

    if ((ptr = list_next( &node->parent->children, &node->entry )))
3430
    {
3431
        *current = LIST_ENTRY( ptr, struct node, entry );
3432 3433 3434 3435 3436
        return TRUE;
    }
    return FALSE;
}

3437
BOOL move_to_prev_node( struct node **current )
3438 3439
{
    struct list *ptr;
3440 3441 3442
    struct node *node = *current;

    if ((ptr = list_prev( &node->parent->children, &node->entry )))
3443
    {
3444
        *current = LIST_ENTRY( ptr, struct node, entry );
3445 3446 3447 3448 3449
        return TRUE;
    }
    return FALSE;
}

3450 3451 3452 3453 3454 3455 3456
BOOL move_to_bof( struct node *root, struct node **current )
{
    *current = root;
    return TRUE;
}

BOOL move_to_eof( struct node *root, struct node **current )
3457 3458
{
    struct list *ptr;
3459
    if ((ptr = list_tail( &root->children )))
3460
    {
3461
        *current = LIST_ENTRY( ptr, struct node, entry );
3462 3463 3464 3465 3466
        return TRUE;
    }
    return FALSE;
}

3467
BOOL move_to_child_node( struct node **current )
3468
{
3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
    struct list *ptr;
    struct node *node = *current;

    if ((ptr = list_head( &node->children )))
    {
        *current = LIST_ENTRY( ptr, struct node, entry );
        return TRUE;
    }
    return FALSE;
}

3480
BOOL move_to_parent_node( struct node **current )
3481 3482
{
    struct node *parent = (*current)->parent;
3483
    if (!parent) return FALSE;
3484
    *current = parent;
3485 3486 3487
    return TRUE;
}

3488 3489 3490 3491 3492 3493 3494
static HRESULT read_move_to( struct reader *reader, WS_MOVE_TO move, BOOL *found )
{
    BOOL success = FALSE;
    HRESULT hr = S_OK;

    if (!read_end_of_data( reader ))
    {
3495
        struct node *saved_current = reader->current;
3496 3497
        while (reader->state != READER_STATE_EOF && (hr = read_node( reader )) == S_OK) { /* nothing */ };
        if (hr != S_OK) return hr;
3498
        reader->current = saved_current;
3499 3500 3501
    }
    switch (move)
    {
3502
    case WS_MOVE_TO_ROOT_ELEMENT:
3503
        success = move_to_root_element( reader->root, &reader->current );
3504 3505
        break;

3506
    case WS_MOVE_TO_NEXT_ELEMENT:
3507
        success = move_to_next_element( &reader->current );
3508 3509
        break;

3510
    case WS_MOVE_TO_PREVIOUS_ELEMENT:
3511
        success = move_to_prev_element( &reader->current );
3512 3513
        break;

3514
    case WS_MOVE_TO_CHILD_ELEMENT:
3515
        success = move_to_child_element( &reader->current );
3516 3517
        break;

3518
    case WS_MOVE_TO_END_ELEMENT:
3519
        success = move_to_end_element( &reader->current );
3520 3521
        break;

3522
    case WS_MOVE_TO_PARENT_ELEMENT:
3523
        success = move_to_parent_element( &reader->current );
3524 3525
        break;

3526
    case WS_MOVE_TO_FIRST_NODE:
3527
        success = move_to_first_node( &reader->current );
3528 3529 3530
        break;

    case WS_MOVE_TO_NEXT_NODE:
3531
        success = move_to_next_node( &reader->current );
3532 3533 3534
        break;

    case WS_MOVE_TO_PREVIOUS_NODE:
3535
        success = move_to_prev_node( &reader->current );
3536 3537 3538
        break;

    case WS_MOVE_TO_CHILD_NODE:
3539
        success = move_to_child_node( &reader->current );
3540 3541 3542
        break;

    case WS_MOVE_TO_BOF:
3543
        success = move_to_bof( reader->root, &reader->current );
3544 3545 3546
        break;

    case WS_MOVE_TO_EOF:
3547
        success = move_to_eof( reader->root, &reader->current );
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
        break;

    default:
        FIXME( "unhandled move %u\n", move );
        return E_NOTIMPL;
    }

    if (found)
    {
        *found = success;
        return S_OK;
    }
    return success ? S_OK : WS_E_INVALID_FORMAT;
}

/**************************************************************************
 *          WsMoveReader		[webservices.@]
 */
HRESULT WINAPI WsMoveReader( WS_XML_READER *handle, WS_MOVE_TO move, BOOL *found, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3569
    HRESULT hr;
3570 3571 3572 3573 3574 3575

    TRACE( "%p %u %p %p\n", handle, move, found, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;

3576 3577 3578 3579 3580 3581 3582 3583
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

3584
    if (reader->input_type != WS_XML_READER_INPUT_TYPE_BUFFER) hr = WS_E_INVALID_OPERATION;
3585
    else hr = read_move_to( reader, move, found );
3586 3587

    LeaveCriticalSection( &reader->cs );
3588
    TRACE( "returning %08x\n", hr );
3589
    return hr;
3590 3591
}

3592 3593 3594 3595 3596 3597
/**************************************************************************
 *          WsReadStartAttribute		[webservices.@]
 */
HRESULT WINAPI WsReadStartAttribute( WS_XML_READER *handle, ULONG index, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3598
    const WS_XML_ELEMENT_NODE *elem;
3599
    HRESULT hr = S_OK;
3600 3601 3602 3603 3604 3605

    TRACE( "%p %u %p\n", handle, index, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;

3606 3607 3608 3609 3610 3611 3612 3613
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

3614
    elem = &reader->current->hdr;
3615 3616
    if (reader->state != READER_STATE_STARTELEMENT || index >= elem->attributeCount) hr = WS_E_INVALID_FORMAT;
    else
3617
    {
3618 3619
        reader->current_attr = index;
        reader->state        = READER_STATE_STARTATTRIBUTE;
3620
    }
3621

3622
    LeaveCriticalSection( &reader->cs );
3623
    TRACE( "returning %08x\n", hr );
3624 3625 3626
    return S_OK;
}

3627 3628 3629 3630 3631 3632
/**************************************************************************
 *          WsReadEndAttribute		[webservices.@]
 */
HRESULT WINAPI WsReadEndAttribute( WS_XML_READER *handle, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
3633
    HRESULT hr = S_OK;
3634 3635 3636 3637 3638 3639

    TRACE( "%p %p\n", handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;

3640 3641 3642 3643 3644 3645 3646 3647
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

3648 3649
    if (reader->state != READER_STATE_STARTATTRIBUTE) hr = WS_E_INVALID_FORMAT;
    else reader->state = READER_STATE_STARTELEMENT;
3650 3651

    LeaveCriticalSection( &reader->cs );
3652 3653
    TRACE( "returning %08x\n", hr );
    return hr;
3654 3655
}

3656
static HRESULT str_to_bool( const unsigned char *str, ULONG len, BOOL *ret )
3657
{
3658 3659 3660 3661 3662 3663
    if (len == 4 && !memcmp( str, "true", 4 )) *ret = TRUE;
    else if (len == 1 && !memcmp( str, "1", 1 )) *ret = TRUE;
    else if (len == 5 && !memcmp( str, "false", 5 )) *ret = FALSE;
    else if (len == 1 && !memcmp( str, "0", 1 )) *ret = FALSE;
    else return WS_E_INVALID_FORMAT;
    return S_OK;
3664 3665
}

3666
static HRESULT str_to_int64( const unsigned char *str, ULONG len, INT64 min, INT64 max, INT64 *ret )
3667 3668
{
    BOOL negative = FALSE;
3669
    const unsigned char *ptr = str;
3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703

    *ret = 0;
    while (len && read_isspace( *ptr )) { ptr++; len--; }
    while (len && read_isspace( ptr[len - 1] )) { len--; }
    if (!len) return WS_E_INVALID_FORMAT;

    if (*ptr == '-')
    {
        negative = TRUE;
        ptr++;
        len--;
    }
    if (!len) return WS_E_INVALID_FORMAT;

    while (len--)
    {
        int val;

        if (!isdigit( *ptr )) return WS_E_INVALID_FORMAT;
        val = *ptr - '0';
        if (negative) val = -val;

        if ((!negative && (*ret > max / 10 || *ret * 10 > max - val)) ||
            (negative && (*ret < min / 10 || *ret * 10 < min - val)))
        {
            return WS_E_NUMERIC_OVERFLOW;
        }
        *ret = *ret * 10 + val;
        ptr++;
    }

    return S_OK;
}

3704
static HRESULT str_to_uint64( const unsigned char *str, ULONG len, UINT64 max, UINT64 *ret )
3705
{
3706
    const unsigned char *ptr = str;
3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727

    *ret = 0;
    while (len && read_isspace( *ptr )) { ptr++; len--; }
    while (len && read_isspace( ptr[len - 1] )) { len--; }
    if (!len) return WS_E_INVALID_FORMAT;

    while (len--)
    {
        unsigned int val;

        if (!isdigit( *ptr )) return WS_E_INVALID_FORMAT;
        val = *ptr - '0';

        if ((*ret > max / 10 || *ret * 10 > max - val)) return WS_E_NUMERIC_OVERFLOW;
        *ret = *ret * 10 + val;
        ptr++;
    }

    return S_OK;
}

3728 3729
static HRESULT str_to_double( const unsigned char *str, ULONG len, double *ret )
{
3730
    BOOL found_sign = FALSE, found_exponent = FALSE, found_digit = FALSE, found_decimal = FALSE;
3731 3732 3733
    static const unsigned __int64 nan = 0xfff8000000000000;
    static const unsigned __int64 inf = 0x7ff0000000000000;
    static const unsigned __int64 inf_min = 0xfff0000000000000;
3734 3735 3736
    const char *p = (const char *)str;
    double tmp;
    ULONG i;
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746

    while (len && read_isspace( *p )) { p++; len--; }
    while (len && read_isspace( p[len - 1] )) { len--; }
    if (!len) return WS_E_INVALID_FORMAT;

    if (len == 3 && !memcmp( p, "NaN", 3 ))
    {
        *(unsigned __int64 *)ret = nan;
        return S_OK;
    }
3747
    if (len == 3 && !memcmp( p, "INF", 3 ))
3748 3749 3750 3751
    {
        *(unsigned __int64 *)ret = inf;
        return S_OK;
    }
3752
    if (len == 4 && !memcmp( p, "-INF", 4 ))
3753 3754 3755 3756 3757
    {
        *(unsigned __int64 *)ret = inf_min;
        return S_OK;
    }

3758
    for (i = 0; i < len; i++)
3759
    {
3760
        if (p[i] >= '0' && p[i] <= '9')
3761
        {
3762 3763
            found_digit = TRUE;
            continue;
3764
        }
3765
        if (!found_sign && !found_digit && (p[i] == '+' || p[i] == '-'))
3766
        {
3767 3768
            found_sign = TRUE;
            continue;
3769
        }
3770
        if (!found_exponent && found_digit && (p[i] == 'e' || p[i] == 'E'))
3771
        {
3772 3773 3774 3775 3776 3777 3778 3779
            found_exponent = found_decimal = TRUE;
            found_digit = found_sign = FALSE;
            continue;
        }
        if (!found_decimal && p[i] == '.')
        {
            found_decimal = TRUE;
            continue;
3780
        }
3781
        return WS_E_INVALID_FORMAT;
3782
    }
3783
    if (!found_digit && !found_exponent)
3784
    {
3785 3786
        *ret = 0;
        return S_OK;
3787 3788
    }

3789 3790 3791
    if (_snscanf_l( p, len, "%lf", c_locale, &tmp ) != 1) return WS_E_INVALID_FORMAT;
    *ret = tmp;
    return S_OK;
3792 3793
}

3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
static HRESULT str_to_float( const unsigned char *str, ULONG len, float *ret )
{
    static const unsigned int inf = 0x7f800000;
    static const unsigned int inf_min = 0xff800000;
    const unsigned char *p = str;
    double val;
    HRESULT hr;

    while (len && read_isspace( *p )) { p++; len--; }
    while (len && read_isspace( p[len - 1] )) { len--; }
    if (!len) return WS_E_INVALID_FORMAT;

    if (len == 3 && !memcmp( p, "INF", 3 ))
    {
        *(unsigned int *)ret = inf;
        return S_OK;
    }
    if (len == 4 && !memcmp( p, "-INF", 4 ))
    {
        *(unsigned int *)ret = inf_min;
        return S_OK;
    }

    if ((hr = str_to_double( p, len, &val )) != S_OK) return hr;
    *ret = val;
    return S_OK;
}

3822
HRESULT str_to_guid( const unsigned char *str, ULONG len, GUID *ret )
3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867
{
    static const unsigned char hex[] =
    {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x00 */
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x10 */
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x20 */
        0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,        /* 0x30 */
        0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,  /* 0x40 */
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x50 */
        0,10,11,12,13,14,15                     /* 0x60 */
    };
    const unsigned char *p = str;
    ULONG i;

    while (len && read_isspace( *p )) { p++; len--; }
    while (len && read_isspace( p[len - 1] )) { len--; }
    if (len != 36) return WS_E_INVALID_FORMAT;

    if (p[8] != '-' || p[13] != '-' || p[18] != '-' || p[23] != '-')
        return WS_E_INVALID_FORMAT;

    for (i = 0; i < 36; i++)
    {
        if (i == 8 || i == 13 || i == 18 || i == 23) continue;
        if (p[i] > 'f' || (!hex[p[i]] && p[i] != '0')) return WS_E_INVALID_FORMAT;
    }

    ret->Data1 = hex[p[0]] << 28 | hex[p[1]] << 24 | hex[p[2]] << 20 | hex[p[3]] << 16 |
                 hex[p[4]] << 12 | hex[p[5]] << 8  | hex[p[6]] << 4  | hex[p[7]];

    ret->Data2 = hex[p[9]]  << 12 | hex[p[10]] << 8 | hex[p[11]] << 4 | hex[p[12]];
    ret->Data3 = hex[p[14]] << 12 | hex[p[15]] << 8 | hex[p[16]] << 4 | hex[p[17]];

    ret->Data4[0] = hex[p[19]] << 4 | hex[p[20]];
    ret->Data4[1] = hex[p[21]] << 4 | hex[p[22]];
    ret->Data4[2] = hex[p[24]] << 4 | hex[p[25]];
    ret->Data4[3] = hex[p[26]] << 4 | hex[p[27]];
    ret->Data4[4] = hex[p[28]] << 4 | hex[p[29]];
    ret->Data4[5] = hex[p[30]] << 4 | hex[p[31]];
    ret->Data4[6] = hex[p[32]] << 4 | hex[p[33]];
    ret->Data4[7] = hex[p[34]] << 4 | hex[p[35]];

    return S_OK;
}

3868 3869
static HRESULT str_to_string( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_STRING *ret )
{
3870
    int len_utf16 = MultiByteToWideChar( CP_UTF8, 0, (const char *)str, len, NULL, 0 );
3871
    if (!(ret->chars = ws_alloc( heap, len_utf16 * sizeof(WCHAR) ))) return WS_E_QUOTA_EXCEEDED;
3872
    MultiByteToWideChar( CP_UTF8, 0, (const char *)str, len, ret->chars, len_utf16 );
3873 3874 3875 3876
    ret->length = len_utf16;
    return S_OK;
}

3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889
static HRESULT str_to_unique_id( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_UNIQUE_ID *ret )
{
    if (len == 45 && !memcmp( str, "urn:uuid:", 9 ))
    {
        ret->uri.length = 0;
        ret->uri.chars  = NULL;
        return str_to_guid( str + 9, len - 9, &ret->guid );
    }

    memset( &ret->guid, 0, sizeof(ret->guid) );
    return str_to_string( str, len, heap, &ret->uri );
}

3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961
static inline unsigned char decode_char( unsigned char c )
{
    if (c >= 'A' && c <= 'Z') return c - 'A';
    if (c >= 'a' && c <= 'z') return c - 'a' + 26;
    if (c >= '0' && c <= '9') return c - '0' + 52;
    if (c == '+') return 62;
    if (c == '/') return 63;
    return 64;
}

static ULONG decode_base64( const unsigned char *base64, ULONG len, unsigned char *buf )
{
    ULONG i = 0;
    unsigned char c0, c1, c2, c3;
    const unsigned char *p = base64;

    while (len > 4)
    {
        if ((c0 = decode_char( p[0] )) > 63) return 0;
        if ((c1 = decode_char( p[1] )) > 63) return 0;
        if ((c2 = decode_char( p[2] )) > 63) return 0;
        if ((c3 = decode_char( p[3] )) > 63) return 0;
        buf[i + 0] = (c0 << 2) | (c1 >> 4);
        buf[i + 1] = (c1 << 4) | (c2 >> 2);
        buf[i + 2] = (c2 << 6) |  c3;
        len -= 4;
        i += 3;
        p += 4;
    }
    if (p[2] == '=')
    {
        if ((c0 = decode_char( p[0] )) > 63) return 0;
        if ((c1 = decode_char( p[1] )) > 63) return 0;
        buf[i] = (c0 << 2) | (c1 >> 4);
        i++;
    }
    else if (p[3] == '=')
    {
        if ((c0 = decode_char( p[0] )) > 63) return 0;
        if ((c1 = decode_char( p[1] )) > 63) return 0;
        if ((c2 = decode_char( p[2] )) > 63) return 0;
        buf[i + 0] = (c0 << 2) | (c1 >> 4);
        buf[i + 1] = (c1 << 4) | (c2 >> 2);
        i += 2;
    }
    else
    {
        if ((c0 = decode_char( p[0] )) > 63) return 0;
        if ((c1 = decode_char( p[1] )) > 63) return 0;
        if ((c2 = decode_char( p[2] )) > 63) return 0;
        if ((c3 = decode_char( p[3] )) > 63) return 0;
        buf[i + 0] = (c0 << 2) | (c1 >> 4);
        buf[i + 1] = (c1 << 4) | (c2 >> 2);
        buf[i + 2] = (c2 << 6) |  c3;
        i += 3;
    }
    return i;
}

static HRESULT str_to_bytes( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_BYTES *ret )
{
    const unsigned char *p = str;

    while (len && read_isspace( *p )) { p++; len--; }
    while (len && read_isspace( p[len - 1] )) { len--; }

    if (len % 4) return WS_E_INVALID_FORMAT;
    if (!(ret->bytes = ws_alloc( heap, len * 3 / 4 ))) return WS_E_QUOTA_EXCEEDED;
    ret->length = decode_base64( p, len, ret->bytes );
    return S_OK;
}

3962 3963 3964
static HRESULT str_to_xml_string( const unsigned char *str, ULONG len, WS_HEAP *heap, WS_XML_STRING *ret )
{
    if (!(ret->bytes = ws_alloc( heap, len ))) return WS_E_QUOTA_EXCEEDED;
3965
    memcpy( ret->bytes, str, len );
3966 3967 3968 3969 3970 3971
    ret->length     = len;
    ret->dictionary = NULL;
    ret->id         = 0;
    return S_OK;
}

3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039
static HRESULT copy_xml_string( WS_HEAP *heap, const WS_XML_STRING *src, WS_XML_STRING *dst )
{
    if (!(dst->bytes = ws_alloc( heap, src->length ))) return WS_E_QUOTA_EXCEEDED;
    memcpy( dst->bytes, src->bytes, src->length );
    dst->length = src->length;
    return S_OK;
}

static HRESULT str_to_qname( struct reader *reader, const unsigned char *str, ULONG len, WS_HEAP *heap,
                             WS_XML_STRING *prefix_ret, WS_XML_STRING *localname_ret, WS_XML_STRING *ns_ret )
{
    const unsigned char *p = str;
    WS_XML_STRING prefix, localname;
    const WS_XML_STRING *ns;
    HRESULT hr;

    while (len && read_isspace( *p )) { p++; len--; }
    while (len && read_isspace( p[len - 1] )) { len--; }

    if ((hr = split_qname( p, len, &prefix, &localname )) != S_OK) return hr;
    if (!(ns = get_namespace( reader, &prefix ))) return WS_E_INVALID_FORMAT;

    if (prefix_ret && (hr = copy_xml_string( heap, &prefix, prefix_ret )) != S_OK) return hr;
    if ((hr = copy_xml_string( heap, &localname, localname_ret )) != S_OK)
    {
        ws_free( heap, prefix_ret->bytes, prefix_ret->length );
        return hr;
    }
    if ((hr = copy_xml_string( heap, ns, ns_ret )) != S_OK)
    {
        ws_free( heap, prefix_ret->bytes, prefix_ret->length );
        ws_free( heap, localname_ret->bytes, localname_ret->length );
        return hr;
    }
    return S_OK;
}

static HRESULT read_qualified_name( struct reader *reader, WS_HEAP *heap, WS_XML_STRING *prefix,
                                    WS_XML_STRING *localname, WS_XML_STRING *ns )
{
    const WS_XML_TEXT_NODE *node = (const WS_XML_TEXT_NODE *)reader->current;
    const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)node->text;
    return str_to_qname( reader, utf8->value.bytes, utf8->value.length, heap, prefix, localname, ns );
}

/**************************************************************************
 *          WsReadQualifiedName		[webservices.@]
 */
HRESULT WINAPI WsReadQualifiedName( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_STRING *prefix,
                                    WS_XML_STRING *localname, WS_XML_STRING *ns,
                                    WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
    HRESULT hr;

    TRACE( "%p %p %p %p %p %p\n", handle, heap, prefix, localname, ns, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !heap) return E_INVALIDARG;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

4040 4041 4042 4043
    if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
    else if (!localname) hr = E_INVALIDARG;
    else if (reader->state != READER_STATE_TEXT) hr = WS_E_INVALID_FORMAT;
    else hr = read_qualified_name( reader, heap, prefix, localname, ns );
4044 4045

    LeaveCriticalSection( &reader->cs );
4046
    TRACE( "returning %08x\n", hr );
4047 4048 4049
    return hr;
}

4050 4051 4052 4053 4054 4055 4056 4057
static const int month_offsets[2][12] =
{
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};

static inline int valid_day( int year, int month, int day )
{
4058
    return day > 0 && day <= month_days[leap_year( year )][month - 1];
4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149
}

static inline int leap_days_before( int year )
{
    return (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
}

static HRESULT str_to_datetime( const unsigned char *bytes, ULONG len, WS_DATETIME *ret )
{
    const unsigned char *p = bytes, *q;
    int year, month, day, hour, min, sec, sec_frac = 0, tz_hour, tz_min, tz_neg;

    while (len && read_isspace( *p )) { p++; len--; }
    while (len && read_isspace( p[len - 1] )) { len--; }

    q = p;
    while (len && isdigit( *q )) { q++; len--; };
    if (q - p != 4 || !len || *q != '-') return WS_E_INVALID_FORMAT;
    year = (p[0] - '0') * 1000 + (p[1] - '0') * 100 + (p[2] - '0') * 10 + p[3] - '0';
    if (year < 1) return WS_E_INVALID_FORMAT;

    p = ++q; len--;
    while (len && isdigit( *q )) { q++; len--; };
    if (q - p != 2 || !len || *q != '-') return WS_E_INVALID_FORMAT;
    month = (p[0] - '0') * 10 + p[1] - '0';
    if (month < 1 || month > 12) return WS_E_INVALID_FORMAT;

    p = ++q; len--;
    while (len && isdigit( *q )) { q++; len--; };
    if (q - p != 2 || !len || *q != 'T') return WS_E_INVALID_FORMAT;
    day = (p[0] - '0') * 10 + p[1] - '0';
    if (!valid_day( year, month, day )) return WS_E_INVALID_FORMAT;

    p = ++q; len--;
    while (len && isdigit( *q )) { q++; len--; };
    if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
    hour = (p[0] - '0') * 10 + p[1] - '0';
    if (hour > 24) return WS_E_INVALID_FORMAT;

    p = ++q; len--;
    while (len && isdigit( *q )) { q++; len--; };
    if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
    min = (p[0] - '0') * 10 + p[1] - '0';
    if (min > 59 || (min > 0 && hour == 24)) return WS_E_INVALID_FORMAT;

    p = ++q; len--;
    while (len && isdigit( *q )) { q++; len--; };
    if (q - p != 2 || !len) return WS_E_INVALID_FORMAT;
    sec = (p[0] - '0') * 10 + p[1] - '0';
    if (sec > 59 || (sec > 0 && hour == 24)) return WS_E_INVALID_FORMAT;

    if (*q == '.')
    {
        unsigned int i, nb_digits, mul = TICKS_PER_SEC / 10;
        p = ++q; len--;
        while (len && isdigit( *q )) { q++; len--; };
        nb_digits = q - p;
        if (nb_digits < 1 || nb_digits > 7) return WS_E_INVALID_FORMAT;
        for (i = 0; i < nb_digits; i++)
        {
            sec_frac += (p[i] - '0') * mul;
            mul /= 10;
        }
    }
    if (*q == 'Z')
    {
        if (--len) return WS_E_INVALID_FORMAT;
        tz_hour = tz_min = tz_neg = 0;
        ret->format = WS_DATETIME_FORMAT_UTC;
    }
    else if (*q == '+' || *q == '-')
    {
        tz_neg = (*q == '-') ? 1 : 0;

        p = ++q; len--;
        while (len && isdigit( *q )) { q++; len--; };
        if (q - p != 2 || !len || *q != ':') return WS_E_INVALID_FORMAT;
        tz_hour = (p[0] - '0') * 10 + p[1] - '0';
        if (tz_hour > 14) return WS_E_INVALID_FORMAT;

        p = ++q; len--;
        while (len && isdigit( *q )) { q++; len--; };
        if (q - p != 2 || len) return WS_E_INVALID_FORMAT;
        tz_min = (p[0] - '0') * 10 + p[1] - '0';
        if (tz_min > 59 || (tz_min > 0 && tz_hour == 14)) return WS_E_INVALID_FORMAT;

        ret->format = WS_DATETIME_FORMAT_LOCAL;
    }
    else return WS_E_INVALID_FORMAT;

    ret->ticks = ((year - 1) * 365 + leap_days_before( year )) * TICKS_PER_DAY;
4150
    ret->ticks += month_offsets[leap_year( year )][month - 1] * TICKS_PER_DAY;
4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
    ret->ticks += (day - 1) * TICKS_PER_DAY;
    ret->ticks += hour * TICKS_PER_HOUR;
    ret->ticks += min * TICKS_PER_MIN;
    ret->ticks += sec * TICKS_PER_SEC;
    ret->ticks += sec_frac;

    if (tz_neg)
    {
        if (tz_hour * TICKS_PER_HOUR + tz_min * TICKS_PER_MIN + ret->ticks > TICKS_MAX)
            return WS_E_INVALID_FORMAT;
        ret->ticks += tz_hour * TICKS_PER_HOUR;
        ret->ticks += tz_min * TICKS_PER_MIN;
    }
    else
    {
        if (tz_hour * TICKS_PER_HOUR + tz_min * TICKS_PER_MIN > ret->ticks)
            return WS_E_INVALID_FORMAT;
        ret->ticks -= tz_hour * TICKS_PER_HOUR;
        ret->ticks -= tz_min * TICKS_PER_MIN;
    }

    return S_OK;
4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191
}

/**************************************************************************
 *          WsDateTimeToFileTime               [webservices.@]
 */
HRESULT WINAPI WsDateTimeToFileTime( const WS_DATETIME *dt, FILETIME *ft, WS_ERROR *error )
{
    unsigned __int64 ticks;

    TRACE( "%p %p %p\n", dt, ft, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!dt || !ft) return E_INVALIDARG;

    if (dt->ticks < TICKS_1601_01_01) return WS_E_INVALID_FORMAT;
    ticks = dt->ticks - TICKS_1601_01_01;
    ft->dwHighDateTime = ticks >> 32;
    ft->dwLowDateTime  = (DWORD)ticks;
    return S_OK;
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
}

/**************************************************************************
 *          WsFileTimeToDateTime               [webservices.@]
 */
HRESULT WINAPI WsFileTimeToDateTime( const FILETIME *ft, WS_DATETIME *dt, WS_ERROR *error )
{
    unsigned __int64 ticks;

    TRACE( "%p %p %p\n", ft, dt, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!dt || !ft) return E_INVALIDARG;

    ticks = ((unsigned __int64)ft->dwHighDateTime << 32) | ft->dwLowDateTime;
    if (ticks > MAX_UINT64 - TICKS_1601_01_01) return WS_E_NUMERIC_OVERFLOW;
    if (ticks + TICKS_1601_01_01 > TICKS_MAX) return WS_E_INVALID_FORMAT;
    dt->ticks  = ticks + TICKS_1601_01_01;
    dt->format = WS_DATETIME_FORMAT_UTC;
    return S_OK;
4212 4213
}

4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247
static BOOL find_attribute( struct reader *reader, const WS_XML_STRING *localname,
                            const WS_XML_STRING *ns, ULONG *index )
{
    ULONG i;
    WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;

    if (!localname)
    {
        *index = reader->current_attr;
        return TRUE;
    }
    for (i = 0; i < elem->attributeCount; i++)
    {
        const WS_XML_STRING *localname2 = elem->attributes[i]->localName;
        const WS_XML_STRING *ns2 = elem->attributes[i]->ns;

        if (!cmp_name( localname->bytes, localname->length, localname2->bytes, localname2->length ) &&
            !cmp_name( ns->bytes, ns->length, ns2->bytes, ns2->length ))
        {
            *index = i;
            return TRUE;
        }
    }
    return FALSE;
}

/**************************************************************************
 *          WsFindAttribute		[webservices.@]
 */
HRESULT WINAPI WsFindAttribute( WS_XML_READER *handle, const WS_XML_STRING *localname,
                                const WS_XML_STRING *ns, BOOL required, ULONG *index,
                                WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
4248
    HRESULT hr = S_OK;
4249 4250 4251 4252 4253 4254 4255

    TRACE( "%p %s %s %d %p %p\n", handle, debugstr_xmlstr(localname), debugstr_xmlstr(ns),
           required, index, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !localname || !ns || !index) return E_INVALIDARG;

4256 4257 4258 4259 4260 4261 4262 4263
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

4264 4265
    if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) hr = WS_E_INVALID_OPERATION;
    else if (!find_attribute( reader, localname, ns, index ))
4266
    {
4267 4268 4269 4270 4271 4272
        if (required) hr = WS_E_INVALID_FORMAT;
        else
        {
            *index = ~0u;
            hr = S_FALSE;
        }
4273
    }
4274 4275

    LeaveCriticalSection( &reader->cs );
4276
    TRACE( "returning %08x\n", hr );
4277
    return hr;
4278 4279
}

4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293
static HRESULT get_node_text( struct reader *reader, const WS_XML_TEXT **ret )
{
    WS_XML_TEXT_NODE *node = (WS_XML_TEXT_NODE *)&reader->current->hdr.node;
    *ret = node->text;
    return S_OK;
}

static HRESULT get_attribute_text( struct reader *reader, ULONG index, const WS_XML_TEXT **ret )
{
    WS_XML_ELEMENT_NODE *elem = &reader->current->hdr;
    *ret = elem->attributes[index]->value;
    return S_OK;
}

4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311
static BOOL match_element( const struct node *node, const WS_XML_STRING *localname, const WS_XML_STRING *ns )
{
    const WS_XML_ELEMENT_NODE *elem = (const WS_XML_ELEMENT_NODE *)node;
    if (node_type( node ) != WS_XML_NODE_TYPE_ELEMENT) return FALSE;
    return WsXmlStringEquals( localname, elem->localName, NULL ) == S_OK &&
           WsXmlStringEquals( ns, elem->ns, NULL ) == S_OK;
}

static HRESULT read_next_node( struct reader *reader )
{
    if (reader->current == reader->last) return read_node( reader );
    if (move_to_child_node( &reader->current )) return S_OK;
    if (move_to_next_node( &reader->current )) return S_OK;
    if (!move_to_parent_node( &reader->current )) return WS_E_INVALID_FORMAT;
    if (move_to_next_node( &reader->current )) return S_OK;
    return WS_E_INVALID_FORMAT;
}

4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
struct reader_pos
{
    struct node *node;
    ULONG        attr;
};

static void save_reader_position( const struct reader *reader, struct reader_pos *pos )
{
    pos->node = reader->current;
    pos->attr = reader->current_attr;
}

static void restore_reader_position( struct reader *reader, const struct reader_pos *pos )
{
    reader->current      = pos->node;
    reader->current_attr = pos->attr;
}

4330 4331
static HRESULT get_text( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
                         const WS_XML_STRING *ns, const WS_XML_TEXT **ret, BOOL *found )
4332 4333 4334 4335 4336 4337
{
    switch (mapping)
    {
    case WS_ATTRIBUTE_TYPE_MAPPING:
    {
        ULONG index;
4338
        if (!(*found = find_attribute( reader, localname, ns, &index ))) return S_OK;
4339
        return get_attribute_text( reader, index, ret );
4340 4341 4342
    }
    case WS_ELEMENT_TYPE_MAPPING:
    case WS_ELEMENT_CONTENT_TYPE_MAPPING:
4343
    case WS_ANY_ELEMENT_TYPE_MAPPING:
4344 4345 4346 4347
    {
        *found = TRUE;
        if (localname)
        {
4348
            struct reader_pos pos;
4349
            HRESULT hr;
4350

4351
            if (!match_element( reader->current, localname, ns ))
4352 4353 4354 4355
            {
                *found = FALSE;
                return S_OK;
            }
4356
            save_reader_position( reader, &pos );
4357 4358 4359
            if ((hr = read_next_node( reader )) != S_OK) return hr;
            if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT)
            {
4360
                restore_reader_position( reader, &pos );
4361 4362 4363 4364 4365 4366 4367 4368
                *found = FALSE;
                return S_OK;
            }
        }
        if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT)
        {
            *found = FALSE;
            return S_OK;
4369
        }
4370
        return get_node_text( reader, ret );
4371
    }
4372 4373 4374 4375 4376 4377
    default:
        FIXME( "mapping %u not supported\n", mapping );
        return E_NOTIMPL;
    }
}

4378
static HRESULT text_to_bool( const WS_XML_TEXT *text, BOOL *val )
4379
{
4380
    HRESULT hr;
4381

4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_bool( text_utf8->value.bytes, text_utf8->value.length, val );
        break;
    }
    case WS_XML_TEXT_TYPE_BOOL:
    {
        const WS_XML_BOOL_TEXT *text_bool = (const WS_XML_BOOL_TEXT *)text;
        *val = text_bool->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
4403 4404
}

4405
static HRESULT read_type_bool( struct reader *reader, WS_TYPE_MAPPING mapping,
4406
                               const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4407
                               const WS_BOOL_DESCRIPTION *desc, WS_READ_OPTION option,
4408
                               WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4409
{
4410
    const WS_XML_TEXT *text;
4411
    HRESULT hr;
4412
    BOOL val = FALSE;
4413 4414

    if (desc)
4415
    {
4416 4417 4418
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4419 4420
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_bool( text, &val )) != S_OK) return hr;
4421

4422 4423 4424
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4425
        if (!*found) return WS_E_INVALID_FORMAT;
4426 4427 4428
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4429
        if (size != sizeof(val)) return E_INVALIDARG;
4430 4431 4432 4433
        *(BOOL *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4434
        if (!*found) return WS_E_INVALID_FORMAT;
4435 4436 4437
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4438
    case WS_READ_NILLABLE_POINTER:
4439 4440 4441
    {
        BOOL *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4442
        if (*found)
4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(BOOL **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }
4454 4455 4456 4457

    return S_OK;
}

4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
static HRESULT text_to_int8( const WS_XML_TEXT *text, INT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT8, MAX_INT8, val );
        break;
    }
    case WS_XML_TEXT_TYPE_INT32:
    {
        const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
        assert( text_int32->value >= MIN_INT8 );
        assert( text_int32->value <= MAX_INT8 );
        *val = text_int32->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4487
static HRESULT read_type_int8( struct reader *reader, WS_TYPE_MAPPING mapping,
4488
                               const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4489
                               const WS_INT8_DESCRIPTION *desc, WS_READ_OPTION option,
4490
                               WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4491
{
4492
    const WS_XML_TEXT *text;
4493
    HRESULT hr;
4494
    INT64 val = 0;
4495 4496 4497 4498 4499 4500

    if (desc)
    {
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4501 4502
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_int8( text, &val )) != S_OK) return hr;
4503

4504 4505 4506
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4507
        if (!*found) return WS_E_INVALID_FORMAT;
4508 4509 4510
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4511 4512 4513 4514 4515
        if (size != sizeof(INT8)) return E_INVALIDARG;
        *(INT8 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4516
        if (!*found) return WS_E_INVALID_FORMAT;
4517 4518 4519
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4520
    case WS_READ_NILLABLE_POINTER:
4521 4522 4523
    {
        INT8 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4524
        if (*found)
4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(INT8 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

4537 4538 4539
    return S_OK;
}

4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568
static HRESULT text_to_int16( const WS_XML_TEXT *text, INT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT16, MAX_INT16, val );
        break;
    }
    case WS_XML_TEXT_TYPE_INT32:
    {
        const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
        assert( text_int32->value >= MIN_INT16 );
        assert( text_int32->value <= MAX_INT16 );
        *val = text_int32->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4569
static HRESULT read_type_int16( struct reader *reader, WS_TYPE_MAPPING mapping,
4570
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4571
                                const WS_INT16_DESCRIPTION *desc, WS_READ_OPTION option,
4572
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4573
{
4574
    const WS_XML_TEXT *text;
4575
    HRESULT hr;
4576
    INT64 val = 0;
4577 4578

    if (desc)
4579
    {
4580 4581 4582
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4583 4584
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_int16( text, &val )) != S_OK) return hr;
4585

4586 4587 4588
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4589
        if (!*found) return WS_E_INVALID_FORMAT;
4590 4591 4592
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4593 4594 4595 4596 4597
        if (size != sizeof(INT16)) return E_INVALIDARG;
        *(INT16 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4598
        if (!*found) return WS_E_INVALID_FORMAT;
4599 4600 4601
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4602
    case WS_READ_NILLABLE_POINTER:
4603 4604 4605
    {
        INT16 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4606
        if (*found)
4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(INT16 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

4619 4620 4621
    return S_OK;
}

4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648
static HRESULT text_to_int32( const WS_XML_TEXT *text, INT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT32, MAX_INT32, val );
        break;
    }
    case WS_XML_TEXT_TYPE_INT32:
    {
        const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
        *val = text_int32->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4649
static HRESULT read_type_int32( struct reader *reader, WS_TYPE_MAPPING mapping,
4650
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4651
                                const WS_INT32_DESCRIPTION *desc, WS_READ_OPTION option,
4652
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4653
{
4654
    const WS_XML_TEXT *text;
4655
    HRESULT hr;
4656
    INT64 val = 0;
4657 4658 4659 4660 4661 4662

    if (desc)
    {
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4663 4664
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_int32( text, &val )) != S_OK) return hr;
4665

4666 4667 4668
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4669
        if (!*found) return WS_E_INVALID_FORMAT;
4670 4671 4672
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4673 4674 4675 4676 4677
        if (size != sizeof(INT32)) return E_INVALIDARG;
        *(INT32 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4678
        if (!*found) return WS_E_INVALID_FORMAT;
4679 4680 4681
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4682
    case WS_READ_NILLABLE_POINTER:
4683 4684 4685
    {
        INT32 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4686
        if (*found)
4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(INT32 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

4699 4700 4701
    return S_OK;
}

4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
static HRESULT text_to_int64( const WS_XML_TEXT *text, INT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_int64( text_utf8->value.bytes, text_utf8->value.length, MIN_INT64, MAX_INT64, val );
        break;
    }
    case WS_XML_TEXT_TYPE_INT64:
    {
        const WS_XML_INT64_TEXT *text_int64 = (const WS_XML_INT64_TEXT *)text;
        *val = text_int64->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4729
static HRESULT read_type_int64( struct reader *reader, WS_TYPE_MAPPING mapping,
4730
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4731
                                const WS_INT64_DESCRIPTION *desc, WS_READ_OPTION option,
4732
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4733
{
4734
    const WS_XML_TEXT *text;
4735
    HRESULT hr;
4736
    INT64 val = 0;
4737 4738

    if (desc)
4739
    {
4740 4741 4742
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4743 4744
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_int64( text, &val )) != S_OK) return hr;
4745

4746 4747 4748
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4749
        if (!*found) return WS_E_INVALID_FORMAT;
4750 4751 4752
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4753
        if (size != sizeof(val)) return E_INVALIDARG;
4754 4755 4756 4757
        *(INT64 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4758
        if (!*found) return WS_E_INVALID_FORMAT;
4759 4760 4761
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4762
    case WS_READ_NILLABLE_POINTER:
4763 4764 4765
    {
        INT64 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4766
        if (*found)
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(INT64 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

4779 4780 4781
    return S_OK;
}

4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809
static HRESULT text_to_uint8( const WS_XML_TEXT *text, UINT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT8, val );
        break;
    }
    case WS_XML_TEXT_TYPE_UINT64:
    {
        const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
        assert( text_uint64->value <= MAX_UINT8 );
        *val = text_uint64->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4810
static HRESULT read_type_uint8( struct reader *reader, WS_TYPE_MAPPING mapping,
4811
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4812
                                const WS_UINT8_DESCRIPTION *desc, WS_READ_OPTION option,
4813
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4814
{
4815
    const WS_XML_TEXT *text;
4816
    HRESULT hr;
4817
    UINT64 val = 0;
4818 4819

    if (desc)
4820
    {
4821 4822 4823
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4824 4825
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_uint8( text, &val )) != S_OK) return hr;
4826

4827 4828 4829
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4830
        if (!*found) return WS_E_INVALID_FORMAT;
4831 4832 4833
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4834 4835 4836 4837 4838
        if (size != sizeof(UINT8)) return E_INVALIDARG;
        *(UINT8 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4839
        if (!*found) return WS_E_INVALID_FORMAT;
4840 4841 4842
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4843
    case WS_READ_NILLABLE_POINTER:
4844 4845 4846
    {
        UINT8 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4847
        if (*found)
4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(UINT8 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

4860 4861 4862
    return S_OK;
}

4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899
static HRESULT text_to_uint16( const WS_XML_TEXT *text, UINT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT16, val );
        break;
    }
    case WS_XML_TEXT_TYPE_INT32:
    {
        const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
        assert( text_int32->value >= 0 );
        assert( text_int32->value <= MAX_UINT16 );
        *val = text_int32->value;
        hr = S_OK;
        break;
    }
    case WS_XML_TEXT_TYPE_UINT64:
    {
        const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
        assert( text_uint64->value <= MAX_UINT16 );
        *val = text_uint64->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4900
static HRESULT read_type_uint16( struct reader *reader, WS_TYPE_MAPPING mapping,
4901
                                 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4902
                                 const WS_UINT16_DESCRIPTION *desc, WS_READ_OPTION option,
4903
                                 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4904
{
4905
    const WS_XML_TEXT *text;
4906
    HRESULT hr;
4907
    UINT64 val = 0;
4908 4909

    if (desc)
4910
    {
4911 4912 4913
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
4914 4915
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_uint16( text, &val )) != S_OK) return hr;
4916

4917 4918 4919
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
4920
        if (!*found) return WS_E_INVALID_FORMAT;
4921 4922 4923
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
4924 4925 4926 4927 4928
        if (size != sizeof(UINT16)) return E_INVALIDARG;
        *(UINT16 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
4929
        if (!*found) return WS_E_INVALID_FORMAT;
4930 4931 4932
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
4933
    case WS_READ_NILLABLE_POINTER:
4934 4935 4936
    {
        UINT16 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
4937
        if (*found)
4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(UINT16 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

4950 4951 4952
    return S_OK;
}

4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988
static HRESULT text_to_uint32( const WS_XML_TEXT *text, UINT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT32, val );
        break;
    }
    case WS_XML_TEXT_TYPE_INT32:
    {
        const WS_XML_INT32_TEXT *text_int32 = (const WS_XML_INT32_TEXT *)text;
        assert( text_int32->value >= 0 );
        *val = text_int32->value;
        hr = S_OK;
        break;
    }
    case WS_XML_TEXT_TYPE_UINT64:
    {
        const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
        assert( text_uint64->value <= MAX_UINT32 );
        *val = text_uint64->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

4989
static HRESULT read_type_uint32( struct reader *reader, WS_TYPE_MAPPING mapping,
4990
                                 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
4991
                                 const WS_UINT32_DESCRIPTION *desc, WS_READ_OPTION option,
4992
                                 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
4993
{
4994
    const WS_XML_TEXT *text;
4995
    HRESULT hr;
4996
    UINT64 val = 0;
4997 4998

    if (desc)
4999
    {
5000 5001 5002
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
5003 5004
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_uint32( text, &val )) != S_OK) return hr;
5005

5006 5007 5008
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5009
        if (!*found) return WS_E_INVALID_FORMAT;
5010 5011 5012
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
5013 5014 5015 5016 5017
        if (size != sizeof(UINT32)) return E_INVALIDARG;
        *(UINT32 *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5018
        if (!*found) return WS_E_INVALID_FORMAT;
5019 5020 5021
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
5022
    case WS_READ_NILLABLE_POINTER:
5023 5024 5025
    {
        UINT32 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5026
        if (*found)
5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(UINT32 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

5039 5040 5041
    return S_OK;
}

5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053
static HRESULT text_to_uint64( const WS_XML_TEXT *text, UINT64 *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_uint64( text_utf8->value.bytes, text_utf8->value.length, MAX_UINT64, val );
        break;
    }
5054 5055 5056 5057 5058 5059 5060
    case WS_XML_TEXT_TYPE_INT64:
    {
        const WS_XML_INT64_TEXT *text_int64 = (const WS_XML_INT64_TEXT *)text;
        *val = text_int64->value;
        hr = S_OK;
        break;
    }
5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075
    case WS_XML_TEXT_TYPE_UINT64:
    {
        const WS_XML_UINT64_TEXT *text_uint64 = (const WS_XML_UINT64_TEXT *)text;
        *val = text_uint64->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5076
static HRESULT read_type_uint64( struct reader *reader, WS_TYPE_MAPPING mapping,
5077
                                 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
5078
                                 const WS_UINT64_DESCRIPTION *desc, WS_READ_OPTION option,
5079
                                 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5080
{
5081
    const WS_XML_TEXT *text;
5082
    HRESULT hr;
5083
    UINT64 val = 0;
5084 5085

    if (desc)
5086
    {
5087 5088 5089
        FIXME( "description not supported\n" );
        return E_NOTIMPL;
    }
5090 5091
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_uint64( text, &val )) != S_OK) return hr;
5092

5093 5094 5095
    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5096
        if (!*found) return WS_E_INVALID_FORMAT;
5097 5098 5099
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
5100
        if (size != sizeof(val)) return E_INVALIDARG;
5101 5102
        *(UINT64 *)ret = val;
        break;
5103

5104
    case WS_READ_REQUIRED_POINTER:
5105
        if (!*found) return WS_E_INVALID_FORMAT;
5106
        /* fall through */
5107

5108
    case WS_READ_OPTIONAL_POINTER:
5109
    case WS_READ_NILLABLE_POINTER:
5110
    {
5111 5112
        UINT64 *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5113
        if (*found)
5114 5115 5116 5117 5118 5119 5120 5121 5122
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(UINT64 **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
5123 5124 5125 5126 5127 5128
        return E_NOTIMPL;
    }

    return S_OK;
}

5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158
static HRESULT text_to_float( const WS_XML_TEXT *text, float *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_float( text_utf8->value.bytes, text_utf8->value.length, val );
        break;
    }
    case WS_XML_TEXT_TYPE_FLOAT:
    {
        const WS_XML_FLOAT_TEXT *text_float = (const WS_XML_FLOAT_TEXT *)text;
        *val = text_float->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

static HRESULT read_type_float( struct reader *reader, WS_TYPE_MAPPING mapping,
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                const WS_FLOAT_DESCRIPTION *desc, WS_READ_OPTION option,
5159
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5160 5161 5162 5163 5164 5165 5166
{
    const WS_XML_TEXT *text;
    HRESULT hr;
    float val = 0.0;

    if (desc) FIXME( "ignoring description\n" );

5167 5168
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_float( text, &val )) != S_OK) return hr;
5169 5170 5171 5172

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5173
        if (!*found) return WS_E_INVALID_FORMAT;
5174 5175 5176 5177 5178 5179 5180 5181
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
        if (size != sizeof(val)) return E_INVALIDARG;
        *(float *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5182
        if (!*found) return WS_E_INVALID_FORMAT;
5183 5184 5185 5186 5187 5188 5189
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
    case WS_READ_NILLABLE_POINTER:
    {
        float *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5190
        if (*found)
5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(float **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232
static HRESULT text_to_double( const WS_XML_TEXT *text, double *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_double( text_utf8->value.bytes, text_utf8->value.length, val );
        break;
    }
    case WS_XML_TEXT_TYPE_DOUBLE:
    {
        const WS_XML_DOUBLE_TEXT *text_double = (const WS_XML_DOUBLE_TEXT *)text;
        *val = text_double->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5233 5234 5235
static HRESULT read_type_double( struct reader *reader, WS_TYPE_MAPPING mapping,
                                 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                 const WS_DOUBLE_DESCRIPTION *desc, WS_READ_OPTION option,
5236
                                 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5237
{
5238
    const WS_XML_TEXT *text;
5239 5240 5241 5242 5243
    HRESULT hr;
    double val = 0.0;

    if (desc) FIXME( "ignoring description\n" );

5244 5245
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_double( text, &val )) != S_OK) return hr;
5246 5247 5248 5249

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5250
        if (!*found) return WS_E_INVALID_FORMAT;
5251 5252 5253
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
5254
        if (size != sizeof(val)) return E_INVALIDARG;
5255 5256 5257 5258
        *(double *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5259
        if (!*found) return WS_E_INVALID_FORMAT;
5260 5261 5262
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
5263
    case WS_READ_NILLABLE_POINTER:
5264 5265 5266
    {
        double *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5267
        if (*found)
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(double **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295
static HRESULT text_to_wsz( const WS_XML_TEXT *text, WS_HEAP *heap, WCHAR **ret )
{
    const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
    int len;

    assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
    len = MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, NULL, 0 );
    if (!(*ret = ws_alloc( heap, (len + 1) * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
    MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, *ret, len );
    (*ret)[len] = 0;
    return S_OK;
}

5296 5297 5298
static HRESULT read_type_wsz( struct reader *reader, WS_TYPE_MAPPING mapping,
                              const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                              const WS_WSZ_DESCRIPTION *desc, WS_READ_OPTION option,
5299
                              WS_HEAP *heap, WCHAR **ret, ULONG size, BOOL *found )
5300
{
5301
    const WS_XML_TEXT *text;
5302
    HRESULT hr;
5303
    WCHAR *str = NULL;
5304

5305
    if (desc)
5306
    {
5307
        FIXME( "description not supported\n" );
5308 5309
        return E_NOTIMPL;
    }
5310 5311
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_wsz( text, heap, &str )) != S_OK) return hr;
5312

5313
    switch (option)
5314
    {
5315
    case WS_READ_REQUIRED_POINTER:
5316
        if (!str && !(str = ws_alloc_zero( heap, sizeof(*str) ))) return WS_E_QUOTA_EXCEEDED;
5317
        /* fall through */
5318

5319
    case WS_READ_OPTIONAL_POINTER:
5320
    case WS_READ_NILLABLE_POINTER:
5321 5322
        if (size != sizeof(str)) return E_INVALIDARG;
        *ret = str;
5323
        break;
5324 5325

    default:
5326
        FIXME( "read option %u not supported\n", option );
5327
        return E_NOTIMPL;
5328
    }
5329

5330 5331 5332
    return S_OK;
}

5333
static HRESULT get_enum_value( const WS_XML_TEXT *text, const WS_ENUM_DESCRIPTION *desc, int *ret )
5334
{
5335
    const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
5336
    ULONG i;
5337 5338

    assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
5339 5340
    for (i = 0; i < desc->valueCount; i++)
    {
5341
        if (WsXmlStringEquals( &utf8->value, desc->values[i].name, NULL ) == S_OK)
5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352
        {
            *ret = desc->values[i].value;
            return S_OK;
        }
    }
    return WS_E_INVALID_FORMAT;
}

static HRESULT read_type_enum( struct reader *reader, WS_TYPE_MAPPING mapping,
                               const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                               const WS_ENUM_DESCRIPTION *desc, WS_READ_OPTION option,
5353
                               WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5354
{
5355
    const WS_XML_TEXT *text;
5356 5357 5358 5359 5360
    HRESULT hr;
    int val = 0;

    if (!desc) return E_INVALIDARG;

5361 5362
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = get_enum_value( text, desc, &val )) != S_OK) return hr;
5363 5364 5365 5366

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5367
        if (!*found) return WS_E_INVALID_FORMAT;
5368 5369 5370
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
5371
        if (size != sizeof(val)) return E_INVALIDARG;
5372 5373 5374 5375
        *(int *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5376
        if (!*found) return WS_E_INVALID_FORMAT;
5377 5378 5379
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
5380
    case WS_READ_NILLABLE_POINTER:
5381 5382 5383
    {
        int *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5384
        if (*found)
5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(int **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426
static HRESULT text_to_datetime( const WS_XML_TEXT *text, WS_DATETIME *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_datetime( text_utf8->value.bytes, text_utf8->value.length, val );
        break;
    }
    case WS_XML_TEXT_TYPE_DATETIME:
    {
        const WS_XML_DATETIME_TEXT *text_datetime = (const WS_XML_DATETIME_TEXT *)text;
        *val = text_datetime->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5427 5428 5429
static HRESULT read_type_datetime( struct reader *reader, WS_TYPE_MAPPING mapping,
                                   const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                   const WS_DATETIME_DESCRIPTION *desc, WS_READ_OPTION option,
5430
                                   WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5431
{
5432
    const WS_XML_TEXT *text;
5433 5434 5435 5436 5437
    HRESULT hr;
    WS_DATETIME val = {0, WS_DATETIME_FORMAT_UTC};

    if (desc) FIXME( "ignoring description\n" );

5438 5439
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_datetime( text, &val )) != S_OK) return hr;
5440 5441 5442 5443

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5444
        if (!*found) return WS_E_INVALID_FORMAT;
5445 5446 5447
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
5448
        if (size != sizeof(val)) return E_INVALIDARG;
5449 5450 5451 5452
        *(WS_DATETIME *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5453
        if (!*found) return WS_E_INVALID_FORMAT;
5454 5455 5456
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
5457
    case WS_READ_NILLABLE_POINTER:
5458 5459 5460
    {
        WS_DATETIME *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5461
        if (*found)
5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(WS_DATETIME **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503
static HRESULT text_to_guid( const WS_XML_TEXT *text, GUID *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_guid( text_utf8->value.bytes, text_utf8->value.length, val );
        break;
    }
    case WS_XML_TEXT_TYPE_GUID:
    {
        const WS_XML_GUID_TEXT *text_guid = (const WS_XML_GUID_TEXT *)text;
        *val = text_guid->value;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5504 5505 5506
static HRESULT read_type_guid( struct reader *reader, WS_TYPE_MAPPING mapping,
                               const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                               const WS_GUID_DESCRIPTION *desc, WS_READ_OPTION option,
5507
                               WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5508
{
5509
    const WS_XML_TEXT *text;
5510
    GUID val = {0};
5511 5512 5513 5514
    HRESULT hr;

    if (desc) FIXME( "ignoring description\n" );

5515 5516
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_guid( text, &val )) != S_OK) return hr;
5517 5518 5519 5520

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5521
        if (!*found) return WS_E_INVALID_FORMAT;
5522 5523 5524
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
5525
        if (size != sizeof(val)) return E_INVALIDARG;
5526 5527 5528 5529
        *(GUID *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5530
        if (!*found) return WS_E_INVALID_FORMAT;
5531 5532 5533
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
5534
    case WS_READ_NILLABLE_POINTER:
5535 5536 5537
    {
        GUID *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5538
        if (*found)
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(GUID **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582
static HRESULT text_to_unique_id( const WS_XML_TEXT *text, WS_HEAP *heap, WS_UNIQUE_ID *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_unique_id( text_utf8->value.bytes, text_utf8->value.length, heap, val );
        break;
    }
    case WS_XML_TEXT_TYPE_UNIQUE_ID:
    {
        const WS_XML_UNIQUE_ID_TEXT *text_unique_id = (const WS_XML_UNIQUE_ID_TEXT *)text;
        val->guid       = text_unique_id->value;
        val->uri.length = 0;
        val->uri.chars  = NULL;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5583 5584 5585
static HRESULT read_type_unique_id( struct reader *reader, WS_TYPE_MAPPING mapping,
                                    const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                    const WS_UNIQUE_ID_DESCRIPTION *desc, WS_READ_OPTION option,
5586
                                    WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5587
{
5588
    const WS_XML_TEXT *text;
5589 5590 5591 5592 5593
    WS_UNIQUE_ID val = {{0}};
    HRESULT hr;

    if (desc) FIXME( "ignoring description\n" );

5594 5595
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_unique_id( text, heap, &val )) != S_OK) return hr;
5596 5597 5598 5599

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5600
        if (!*found) return WS_E_INVALID_FORMAT;
5601 5602 5603 5604 5605 5606 5607 5608
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
        if (size != sizeof(val)) return E_INVALIDARG;
        *(WS_UNIQUE_ID *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5609
        if (!*found) return WS_E_INVALID_FORMAT;
5610 5611 5612 5613 5614 5615 5616
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
    case WS_READ_NILLABLE_POINTER:
    {
        WS_UNIQUE_ID *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5617
        if (*found)
5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(WS_UNIQUE_ID **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661
static HRESULT text_to_string( const WS_XML_TEXT *text, WS_HEAP *heap, WS_STRING *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_string( text_utf8->value.bytes, text_utf8->value.length, heap, val );
        break;
    }
    case WS_XML_TEXT_TYPE_UTF16:
    {
        const WS_XML_UTF16_TEXT *text_utf16 = (const WS_XML_UTF16_TEXT *)text;
        if (!(val->chars = ws_alloc( heap, text_utf16->byteCount ))) return WS_E_QUOTA_EXCEEDED;
        memcpy( val->chars, text_utf16->bytes, text_utf16->byteCount );
        val->length = text_utf16->byteCount / sizeof(WCHAR);
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5662 5663 5664
static HRESULT read_type_string( struct reader *reader, WS_TYPE_MAPPING mapping,
                                 const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                 const WS_STRING_DESCRIPTION *desc, WS_READ_OPTION option,
5665
                                 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5666
{
5667
    const WS_XML_TEXT *text;
5668 5669 5670 5671 5672
    WS_STRING val = {0};
    HRESULT hr;

    if (desc) FIXME( "ignoring description\n" );

5673 5674
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_string( text, heap, &val )) != S_OK) return hr;
5675 5676 5677 5678 5679 5680 5681 5682 5683 5684

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
    case WS_READ_NILLABLE_VALUE:
        if (size != sizeof(val)) return E_INVALIDARG;
        *(WS_STRING *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5685 5686 5687 5688 5689 5690 5691 5692
    {
        WS_STRING *heap_val;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
        if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
        *heap_val = val;
        *(WS_STRING **)ret = heap_val;
        break;
    }
5693 5694 5695 5696 5697
    case WS_READ_OPTIONAL_POINTER:
    case WS_READ_NILLABLE_POINTER:
    {
        WS_STRING *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5698
        if (*found)
5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(WS_STRING **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742
static HRESULT text_to_bytes( const WS_XML_TEXT *text, WS_HEAP *heap, WS_BYTES *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_bytes( text_utf8->value.bytes, text_utf8->value.length, heap, val );
        break;
    }
    case WS_XML_TEXT_TYPE_BASE64:
    {
        const WS_XML_BASE64_TEXT *text_base64 = (const WS_XML_BASE64_TEXT *)text;
        if (!(val->bytes = ws_alloc( heap, text_base64->length ))) return WS_E_QUOTA_EXCEEDED;
        memcpy( val->bytes, text_base64->bytes, text_base64->length );
        val->length = text_base64->length;
        hr = S_OK;
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5743 5744 5745
static HRESULT read_type_bytes( struct reader *reader, WS_TYPE_MAPPING mapping,
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                const WS_BYTES_DESCRIPTION *desc, WS_READ_OPTION option,
5746
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5747
{
5748
    const WS_XML_TEXT *text;
5749 5750 5751 5752 5753
    WS_BYTES val = {0};
    HRESULT hr;

    if (desc) FIXME( "ignoring description\n" );

5754 5755
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_bytes( text, heap, &val )) != S_OK) return hr;
5756 5757 5758 5759 5760

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
    case WS_READ_NILLABLE_VALUE:
5761
        if (size != sizeof(val)) return E_INVALIDARG;
5762 5763 5764 5765
        *(WS_BYTES *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5766 5767 5768 5769 5770 5771 5772 5773
    {
        WS_BYTES *heap_val;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
        if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
        *heap_val = val;
        *(WS_BYTES **)ret = heap_val;
        break;
    }
5774 5775 5776 5777 5778
    case WS_READ_OPTIONAL_POINTER:
    case WS_READ_NILLABLE_POINTER:
    {
        WS_BYTES *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5779
        if (*found)
5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(WS_BYTES **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5795 5796 5797 5798 5799 5800 5801
static HRESULT text_to_xml_string( const WS_XML_TEXT *text, WS_HEAP *heap, WS_XML_STRING *val )
{
    const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text;
    assert( text->textType == WS_XML_TEXT_TYPE_UTF8 );
    return str_to_xml_string( utf8->value.bytes, utf8->value.length, heap, val );
}

5802 5803 5804
static HRESULT read_type_xml_string( struct reader *reader, WS_TYPE_MAPPING mapping,
                                     const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                     const WS_XML_STRING_DESCRIPTION *desc, WS_READ_OPTION option,
5805
                                     WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5806
{
5807
    const WS_XML_TEXT *text;
5808 5809 5810 5811 5812
    WS_XML_STRING val = {0};
    HRESULT hr;

    if (desc) FIXME( "ignoring description\n" );

5813 5814
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_xml_string( text, heap, &val )) != S_OK) return hr;
5815 5816 5817 5818 5819 5820 5821 5822 5823 5824

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
    case WS_READ_NILLABLE_VALUE:
        if (size != sizeof(val)) return E_INVALIDARG;
        *(WS_XML_STRING *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5825 5826 5827 5828 5829 5830 5831 5832
    {
        WS_XML_STRING *heap_val;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
        if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
        *heap_val = val;
        *(WS_XML_STRING **)ret = heap_val;
        break;
    }
5833 5834 5835 5836 5837
    case WS_READ_OPTIONAL_POINTER:
    case WS_READ_NILLABLE_POINTER:
    {
        WS_XML_STRING *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5838
        if (*found)
5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(WS_XML_STRING **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885
static HRESULT text_to_qname( struct reader *reader, const WS_XML_TEXT *text, WS_HEAP *heap, WS_XML_QNAME *val )
{
    HRESULT hr;

    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        hr = str_to_qname( reader, text_utf8->value.bytes, text_utf8->value.length, heap, NULL,
                           &val->localName, &val->ns );
        break;
    }
    case WS_XML_TEXT_TYPE_QNAME:
    {
        const WS_XML_QNAME_TEXT *text_qname = (const WS_XML_QNAME_TEXT *)text;
        if ((hr = copy_xml_string( heap, text_qname->localName, &val->localName )) != S_OK) return hr;
        if ((hr = copy_xml_string( heap, text_qname->ns, &val->ns )) != S_OK)
        {
            ws_free( heap, val->localName.bytes, val->localName.length );
            return hr;
        }
        break;
    }
    default:
        FIXME( "unhandled text type %u\n", text->textType );
        return E_NOTIMPL;
    }

    return hr;
}

5886 5887 5888
static HRESULT read_type_qname( struct reader *reader, WS_TYPE_MAPPING mapping,
                                const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                                const WS_XML_QNAME_DESCRIPTION *desc, WS_READ_OPTION option,
5889
                                WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
5890
{
5891
    const WS_XML_TEXT *text;
5892 5893 5894 5895 5896 5897 5898 5899 5900
    WS_XML_QNAME val = {{0}};
    HRESULT hr;

    if (desc) FIXME( "ignoring description\n" );

    if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return WS_E_INVALID_FORMAT;
    if ((hr = read_startelement( reader )) != S_OK) return hr;
    if (node_type( reader->current ) != WS_XML_NODE_TYPE_TEXT) return WS_E_INVALID_FORMAT;

5901 5902
    if ((hr = get_text( reader, mapping, localname, ns, &text, found )) != S_OK) return hr;
    if (*found && (hr = text_to_qname( reader, text, heap, &val )) != S_OK) return hr;
5903 5904 5905 5906

    switch (option)
    {
    case WS_READ_REQUIRED_VALUE:
5907
        if (!*found) return WS_E_INVALID_FORMAT;
5908 5909 5910 5911 5912 5913 5914 5915
        /* fall through */

    case WS_READ_NILLABLE_VALUE:
        if (size != sizeof(val)) return E_INVALIDARG;
        *(WS_XML_QNAME *)ret = val;
        break;

    case WS_READ_REQUIRED_POINTER:
5916
        if (!*found) return WS_E_INVALID_FORMAT;
5917 5918 5919 5920 5921 5922 5923
        /* fall through */

    case WS_READ_OPTIONAL_POINTER:
    case WS_READ_NILLABLE_POINTER:
    {
        WS_XML_QNAME *heap_val = NULL;
        if (size != sizeof(heap_val)) return E_INVALIDARG;
5924
        if (*found)
5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939
        {
            if (!(heap_val = ws_alloc( heap, sizeof(*heap_val) ))) return WS_E_QUOTA_EXCEEDED;
            *heap_val = val;
        }
        *(WS_XML_QNAME **)ret = heap_val;
        break;
    }
    default:
        FIXME( "read option %u not supported\n", option );
        return E_NOTIMPL;
    }

    return S_OK;
}

5940 5941 5942 5943 5944
static BOOL is_empty_text_node( const struct node *node )
{
    const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)node;

    if (node_type( node ) != WS_XML_NODE_TYPE_TEXT) return FALSE;
5945 5946 5947 5948 5949
    switch (text->text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        ULONG i;
5950
        const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;
5951 5952 5953 5954
        for (i = 0; i < utf8->value.length; i++) if (!read_isspace( utf8->value.bytes[i] )) return FALSE;
        return TRUE;
    }
    case WS_XML_TEXT_TYPE_BASE64:
5955
    {
5956
        const WS_XML_BASE64_TEXT *base64 = (const WS_XML_BASE64_TEXT *)text->text;
5957 5958
        return !base64->length;
    }
5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970
    case WS_XML_TEXT_TYPE_BOOL:
    case WS_XML_TEXT_TYPE_INT32:
    case WS_XML_TEXT_TYPE_INT64:
    case WS_XML_TEXT_TYPE_UINT64:
    case WS_XML_TEXT_TYPE_FLOAT:
    case WS_XML_TEXT_TYPE_DOUBLE:
    case WS_XML_TEXT_TYPE_DECIMAL:
    case WS_XML_TEXT_TYPE_GUID:
    case WS_XML_TEXT_TYPE_UNIQUE_ID:
    case WS_XML_TEXT_TYPE_DATETIME:
        return FALSE;

5971
    default:
5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984
        ERR( "unhandled text type %u\n", text->text->textType );
        return FALSE;
    }
}

/* skips comment and empty text nodes */
static HRESULT read_type_next_node( struct reader *reader )
{
    for (;;)
    {
        HRESULT hr;
        WS_XML_NODE_TYPE type;

5985
        if ((hr = read_next_node( reader )) != S_OK) return hr;
5986 5987 5988 5989 5990 5991 5992 5993 5994 5995
        type = node_type( reader->current );
        if (type == WS_XML_NODE_TYPE_COMMENT ||
            (type == WS_XML_NODE_TYPE_TEXT && is_empty_text_node( reader->current ))) continue;
        return S_OK;
    }
}

static HRESULT read_type_next_element_node( struct reader *reader, const WS_XML_STRING *localname,
                                            const WS_XML_STRING *ns )
{
5996
    struct reader_pos pos;
5997 5998 5999
    HRESULT hr;

    if (!localname) return S_OK; /* assume reader is already correctly positioned */
6000 6001 6002 6003 6004 6005
    if (reader->current == reader->last)
    {
        BOOL found;
        if ((hr = read_to_startelement( reader, &found )) != S_OK) return hr;
        if (!found) return WS_E_INVALID_FORMAT;
    }
6006
    if (match_element( reader->current, localname, ns )) return S_OK;
6007

6008
    save_reader_position( reader, &pos );
6009
    if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6010
    if (match_element( reader->current, localname, ns )) return S_OK;
6011
    restore_reader_position( reader, &pos );
6012 6013

    return WS_E_INVALID_FORMAT;
6014 6015
}

6016
ULONG get_type_size( WS_TYPE type, const void *desc )
6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030
{
    switch (type)
    {
    case WS_INT8_TYPE:
    case WS_UINT8_TYPE:
        return sizeof(INT8);

    case WS_INT16_TYPE:
    case WS_UINT16_TYPE:
        return sizeof(INT16);

    case WS_BOOL_TYPE:
    case WS_INT32_TYPE:
    case WS_UINT32_TYPE:
6031
    case WS_ENUM_TYPE:
6032 6033 6034 6035 6036 6037
        return sizeof(INT32);

    case WS_INT64_TYPE:
    case WS_UINT64_TYPE:
        return sizeof(INT64);

6038 6039 6040
    case WS_FLOAT_TYPE:
        return sizeof(float);

6041 6042 6043
    case WS_DOUBLE_TYPE:
        return sizeof(double);

6044 6045 6046
    case WS_DATETIME_TYPE:
        return sizeof(WS_DATETIME);

6047 6048 6049
    case WS_GUID_TYPE:
        return sizeof(GUID);

6050 6051 6052
    case WS_UNIQUE_ID_TYPE:
        return sizeof(WS_UNIQUE_ID);

6053 6054 6055
    case WS_STRING_TYPE:
        return sizeof(WS_STRING);

6056 6057 6058
    case WS_WSZ_TYPE:
        return sizeof(WCHAR *);

6059 6060 6061
    case WS_BYTES_TYPE:
        return sizeof(WS_BYTES);

6062 6063 6064
    case WS_XML_STRING_TYPE:
        return sizeof(WS_XML_STRING);

6065 6066 6067
    case WS_XML_QNAME_TYPE:
        return sizeof(WS_XML_QNAME);

6068 6069 6070
    case WS_DESCRIPTION_TYPE:
        return sizeof(WS_STRUCT_DESCRIPTION *);

6071 6072 6073 6074 6075 6076 6077 6078 6079 6080
    case WS_STRUCT_TYPE:
    {
        const WS_STRUCT_DESCRIPTION *desc_struct = desc;
        return desc_struct->size;
    }
    case WS_UNION_TYPE:
    {
        const WS_UNION_DESCRIPTION *desc_union = desc;
        return desc_union->size;
    }
6081 6082 6083 6084 6085 6086
    default:
        ERR( "unhandled type %u\n", type );
        return 0;
    }
}

6087
static WS_READ_OPTION get_field_read_option( WS_TYPE type, ULONG options )
6088
{
6089 6090 6091 6092 6093 6094 6095
    if (options & WS_FIELD_POINTER)
    {
        if (options & WS_FIELD_NILLABLE) return WS_READ_NILLABLE_POINTER;
        if (options & WS_FIELD_OPTIONAL) return WS_READ_OPTIONAL_POINTER;
        return WS_READ_REQUIRED_POINTER;
    }

6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106
    switch (type)
    {
    case WS_BOOL_TYPE:
    case WS_INT8_TYPE:
    case WS_INT16_TYPE:
    case WS_INT32_TYPE:
    case WS_INT64_TYPE:
    case WS_UINT8_TYPE:
    case WS_UINT16_TYPE:
    case WS_UINT32_TYPE:
    case WS_UINT64_TYPE:
6107
    case WS_FLOAT_TYPE:
6108 6109
    case WS_DOUBLE_TYPE:
    case WS_DATETIME_TYPE:
6110
    case WS_GUID_TYPE:
6111
    case WS_UNIQUE_ID_TYPE:
6112
    case WS_STRING_TYPE:
6113
    case WS_BYTES_TYPE:
6114
    case WS_XML_STRING_TYPE:
6115
    case WS_XML_QNAME_TYPE:
6116
    case WS_STRUCT_TYPE:
6117
    case WS_ENUM_TYPE:
6118
    case WS_UNION_TYPE:
6119
        if (options & (WS_FIELD_OPTIONAL|WS_FIELD_NILLABLE)) return WS_READ_NILLABLE_VALUE;
6120 6121 6122
        return WS_READ_REQUIRED_VALUE;

    case WS_WSZ_TYPE:
6123
    case WS_DESCRIPTION_TYPE:
6124 6125
        if (options & WS_FIELD_NILLABLE) return WS_READ_NILLABLE_POINTER;
        if (options & WS_FIELD_OPTIONAL) return WS_READ_OPTIONAL_POINTER;
6126 6127 6128 6129 6130 6131 6132 6133
        return WS_READ_REQUIRED_POINTER;

    default:
        FIXME( "unhandled type %u\n", type );
        return 0;
    }
}

6134 6135
static HRESULT read_type_field( struct reader *, const WS_FIELD_DESCRIPTION *, WS_HEAP *, char *, ULONG );

6136 6137
static HRESULT read_type_union( struct reader *reader, const WS_UNION_DESCRIPTION *desc, WS_HEAP *heap, void *ret,
                                ULONG size, BOOL *found )
6138
{
6139
    struct reader_pos pos;
6140 6141 6142
    HRESULT hr;
    ULONG i;

6143
    if (size != desc->size) return E_INVALIDARG;
6144

6145
    save_reader_position( reader, &pos );
6146
    if ((hr = read_type_next_node( reader )) != S_OK) return hr;
6147

6148 6149
    for (i = 0; i < desc->fieldCount; i++)
    {
6150
        if ((*found = match_element( reader->current, desc->fields[i]->field.localName, desc->fields[i]->field.ns )))
6151 6152 6153
            break;
    }

6154
    if (!*found)
6155
    {
6156 6157
        *(int *)((char *)ret + desc->enumOffset) = desc->noneEnumValue;
        restore_reader_position( reader, &pos );
6158
    }
6159
    else
6160
    {
6161 6162 6163
        ULONG offset = desc->fields[i]->field.offset;
        if ((hr = read_type_field( reader, &desc->fields[i]->field, heap, ret, offset )) != S_OK) return hr;
        *(int *)((char *)ret + desc->enumOffset) = desc->fields[i]->value;
6164 6165
    }

6166
    return S_OK;
6167 6168
}

6169 6170
static HRESULT read_type( struct reader *, WS_TYPE_MAPPING, WS_TYPE, const WS_XML_STRING *,
                          const WS_XML_STRING *, const void *, WS_READ_OPTION, WS_HEAP *,
6171
                          void *, ULONG, BOOL * );
6172

6173 6174
static HRESULT read_type_array( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap,
                                void **ret, ULONG *count )
6175 6176 6177
{
    HRESULT hr;
    ULONG item_size, nb_items = 0, nb_allocated = 1, offset = 0;
6178
    WS_READ_OPTION option;
6179
    BOOL found;
6180 6181
    char *buf;

6182
    if (!(option = get_field_read_option( desc->type, desc->options ))) return E_INVALIDARG;
6183

6184
    /* wrapper element */
6185 6186
    if (desc->localName && ((hr = read_type_next_element_node( reader, desc->localName, desc->ns )) != S_OK))
        return hr;
6187

6188
    if (option == WS_READ_REQUIRED_VALUE || option == WS_READ_NILLABLE_VALUE)
6189 6190 6191 6192
        item_size = get_type_size( desc->type, desc->typeDescription );
    else
        item_size = sizeof(void *);

6193 6194 6195 6196 6197
    if (!(buf = ws_alloc_zero( heap, item_size ))) return WS_E_QUOTA_EXCEEDED;
    for (;;)
    {
        if (nb_items >= nb_allocated)
        {
6198
            SIZE_T old_size = nb_allocated * item_size, new_size = old_size * 2;
6199
            if (!(buf = ws_realloc_zero( heap, buf, old_size, new_size ))) return WS_E_QUOTA_EXCEEDED;
6200 6201
            nb_allocated *= 2;
        }
6202 6203

        if (desc->type == WS_UNION_TYPE)
6204 6205 6206 6207 6208 6209 6210 6211 6212
        {
            hr = read_type_union( reader, desc->typeDescription, heap, buf + offset, item_size, &found );
            if (hr != S_OK)
            {
                ws_free( heap, buf, nb_allocated * item_size );
                return hr;
            }
            if (!found) break;
        }
6213
        else
6214
        {
6215
            hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->itemLocalName, desc->itemNs,
6216
                            desc->typeDescription, option, heap, buf + offset, item_size, &found );
6217 6218 6219 6220 6221 6222
            if (hr == WS_E_INVALID_FORMAT) break;
            if (hr != S_OK)
            {
                ws_free( heap, buf, nb_allocated * item_size );
                return hr;
            }
6223
        }
6224

6225 6226 6227 6228
        offset += item_size;
        nb_items++;
    }

6229
    if (desc->localName && ((hr = read_type_next_node( reader )) != S_OK)) return hr;
6230

6231
    if (desc->itemRange && (nb_items < desc->itemRange->minItemCount || nb_items > desc->itemRange->maxItemCount))
6232
    {
6233 6234
        TRACE( "number of items %u out of range (%u-%u)\n", nb_items, desc->itemRange->minItemCount,
               desc->itemRange->maxItemCount );
6235
        ws_free( heap, buf, nb_allocated * item_size );
6236
        return WS_E_INVALID_FORMAT;
6237 6238 6239
    }

    *count = nb_items;
6240 6241
    *ret = buf;

6242 6243 6244
    return S_OK;
}

6245 6246 6247
static HRESULT read_type_text( struct reader *reader, const WS_FIELD_DESCRIPTION *desc,
                               WS_READ_OPTION option, WS_HEAP *heap, void *ret, ULONG size )
{
6248 6249
    struct reader_pos pos;
    BOOL found;
6250
    HRESULT hr;
6251

6252 6253
    if (reader->current == reader->last)
    {
6254
        if ((hr = read_to_startelement( reader, &found )) != S_OK) return hr;
6255 6256
        if (!found) return WS_E_INVALID_FORMAT;
    }
6257

6258 6259 6260 6261 6262 6263
    save_reader_position( reader, &pos );
    if ((hr = read_next_node( reader )) != S_OK) return hr;
    hr = read_type( reader, WS_ANY_ELEMENT_TYPE_MAPPING, desc->type, NULL, NULL,
                    desc->typeDescription, option, heap, ret, size, &found );
    if (hr == S_OK && !found) restore_reader_position( reader, &pos );
    return hr;
6264 6265
}

6266 6267
static HRESULT read_type_field( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap, char *buf,
                                ULONG offset )
6268
{
6269
    char *ptr;
6270
    WS_READ_OPTION option;
6271
    ULONG size;
6272
    HRESULT hr;
6273
    BOOL found;
6274

6275
    if (!desc) return E_INVALIDARG;
6276
    if (desc->options & ~(WS_FIELD_POINTER|WS_FIELD_OPTIONAL|WS_FIELD_NILLABLE|WS_FIELD_NILLABLE_ITEM))
6277 6278 6279 6280
    {
        FIXME( "options %08x not supported\n", desc->options );
        return E_NOTIMPL;
    }
6281
    if (!(option = get_field_read_option( desc->type, desc->options ))) return E_INVALIDARG;
6282

6283
    if (option == WS_READ_REQUIRED_VALUE || option == WS_READ_NILLABLE_VALUE)
6284 6285 6286 6287
        size = get_type_size( desc->type, desc->typeDescription );
    else
        size = sizeof(void *);

6288
    ptr = buf + offset;
6289 6290
    switch (desc->mapping)
    {
6291 6292 6293 6294
    case WS_TYPE_ATTRIBUTE_FIELD_MAPPING:
        FIXME( "WS_TYPE_ATTRIBUTE_FIELD_MAPPING not supported\n" );
        return S_OK;

6295 6296
    case WS_ATTRIBUTE_FIELD_MAPPING:
        hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6297
                        desc->typeDescription, option, heap, ptr, size, &found );
6298 6299 6300 6301
        break;

    case WS_ELEMENT_FIELD_MAPPING:
        hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->localName, desc->ns,
6302
                        desc->typeDescription, option, heap, ptr, size, &found );
6303 6304
        break;

6305
    case WS_ELEMENT_CHOICE_FIELD_MAPPING:
6306
    {
6307 6308
        if (desc->type != WS_UNION_TYPE || !desc->typeDescription ||
            (desc->options & (WS_FIELD_POINTER|WS_FIELD_NILLABLE))) return E_INVALIDARG;
6309
        hr = read_type_union( reader, desc->typeDescription, heap, ptr, size, &found );
6310
        break;
6311
    }
6312
    case WS_REPEATING_ELEMENT_FIELD_MAPPING:
6313
    case WS_REPEATING_ELEMENT_CHOICE_FIELD_MAPPING:
6314 6315
    {
        ULONG count;
6316
        hr = read_type_array( reader, desc, heap, (void **)ptr, &count );
6317
        if (hr == S_OK) *(ULONG *)(buf + desc->countOffset) = count;
6318 6319
        break;
    }
6320 6321 6322 6323
    case WS_TEXT_FIELD_MAPPING:
        hr = read_type_text( reader, desc, option, heap, ptr, size );
        break;

6324
    default:
6325
        FIXME( "unhandled field mapping %u\n", desc->mapping );
6326 6327 6328
        return E_NOTIMPL;
    }

6329
    if (hr == WS_E_INVALID_FORMAT)
6330 6331 6332 6333
    {
        switch (option)
        {
        case WS_READ_REQUIRED_VALUE:
6334 6335 6336 6337
        case WS_READ_REQUIRED_POINTER:
            return WS_E_INVALID_FORMAT;

        case WS_READ_NILLABLE_VALUE:
6338 6339 6340
            if (desc->defaultValue) memcpy( ptr, desc->defaultValue->value, desc->defaultValue->valueSize );
            return S_OK;

6341 6342
        case WS_READ_OPTIONAL_POINTER:
        case WS_READ_NILLABLE_POINTER:
6343 6344 6345 6346 6347 6348 6349 6350 6351
            *(void **)ptr = NULL;
            return S_OK;

        default:
            ERR( "unhandled option %u\n", option );
            return E_NOTIMPL;
        }
    }

6352 6353 6354
    return hr;
}

6355 6356
static HRESULT read_type_struct( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
                                 const WS_XML_STRING *ns, const WS_STRUCT_DESCRIPTION *desc, WS_READ_OPTION option,
6357
                                 WS_HEAP *heap, void *ret, ULONG size, BOOL *found )
6358
{
6359
    ULONG i, offset;
6360 6361 6362 6363
    HRESULT hr;
    char *buf;

    if (!desc) return E_INVALIDARG;
6364 6365 6366 6367 6368
    if (desc->structOptions & ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
    {
        FIXME( "struct options %08x not supported\n",
               desc->structOptions & ~WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT );
    }
6369

6370
    switch (option)
6371
    {
6372 6373
    case WS_READ_REQUIRED_POINTER:
    case WS_READ_OPTIONAL_POINTER:
6374
    case WS_READ_NILLABLE_POINTER:
6375
        if (size != sizeof(void *)) return E_INVALIDARG;
6376
        if (!(buf = ws_alloc_zero( heap, desc->size ))) return WS_E_QUOTA_EXCEEDED;
6377
        break;
6378

6379
    case WS_READ_REQUIRED_VALUE:
6380
    case WS_READ_NILLABLE_VALUE:
6381 6382
        if (size != desc->size) return E_INVALIDARG;
        buf = ret;
6383 6384 6385
        break;

    default:
6386
        FIXME( "unhandled read option %u\n", option );
6387
        return E_NOTIMPL;
6388
    }
6389 6390

    for (i = 0; i < desc->fieldCount; i++)
6391
    {
6392
        offset = desc->fields[i]->offset;
6393
        if ((hr = read_type_field( reader, desc->fields[i], heap, buf, offset )) != S_OK) break;
6394 6395 6396 6397 6398 6399
    }

    switch (option)
    {
    case WS_READ_REQUIRED_POINTER:
        if (hr != S_OK)
6400
        {
6401
            ws_free( heap, buf, desc->size );
6402 6403
            return hr;
        }
6404
        *(char **)ret = buf;
6405
        break;
6406 6407

    case WS_READ_OPTIONAL_POINTER:
6408 6409
    case WS_READ_NILLABLE_POINTER:
        if (is_nil_value( buf, desc->size ))
6410
        {
6411
            ws_free( heap, buf, desc->size );
6412 6413 6414
            buf = NULL;
        }
        *(char **)ret = buf;
6415
        break;
6416 6417

    case WS_READ_REQUIRED_VALUE:
6418
    case WS_READ_NILLABLE_VALUE:
6419 6420
        if (hr != S_OK) return hr;
        break;
6421 6422 6423 6424

    default:
        ERR( "unhandled read option %u\n", option );
        return E_NOTIMPL;
6425
    }
6426 6427 6428 6429 6430 6431

    if (desc->structOptions & WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
    {
        struct node *parent = find_parent( reader );
        parent->flags |= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT;
    }
6432 6433

    *found = TRUE;
6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487
    return S_OK;
}

static HRESULT start_mapping( struct reader *reader, WS_TYPE_MAPPING mapping, const WS_XML_STRING *localname,
                              const WS_XML_STRING *ns )
{
    switch (mapping)
    {
    case WS_ELEMENT_TYPE_MAPPING:
    case WS_ELEMENT_CONTENT_TYPE_MAPPING:
        return read_type_next_element_node( reader, localname, ns );

    case WS_ANY_ELEMENT_TYPE_MAPPING:
    case WS_ATTRIBUTE_TYPE_MAPPING:
        return S_OK;

    default:
        FIXME( "unhandled mapping %u\n", mapping );
        return E_NOTIMPL;
    }
}

static HRESULT read_type_endelement_node( struct reader *reader )
{
    const struct node *parent = find_parent( reader );
    HRESULT hr;

    for (;;)
    {
        if ((hr = read_type_next_node( reader )) != S_OK) return hr;
        if (node_type( reader->current ) == WS_XML_NODE_TYPE_END_ELEMENT && reader->current->parent == parent)
        {
            return S_OK;
        }
        if (read_end_of_data( reader ) || !(parent->flags & NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT)) break;
    }

    return WS_E_INVALID_FORMAT;
}

static HRESULT end_mapping( struct reader *reader, WS_TYPE_MAPPING mapping )
{
    switch (mapping)
    {
    case WS_ELEMENT_TYPE_MAPPING:
        return read_type_endelement_node( reader );

    case WS_ELEMENT_CONTENT_TYPE_MAPPING:
        return read_type_next_node( reader );

    case WS_ATTRIBUTE_TYPE_MAPPING:
    default:
        return S_OK;
    }
6488
}
6489

6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510
static BOOL is_true_text( const WS_XML_TEXT *text )
{
    switch (text->textType)
    {
    case WS_XML_TEXT_TYPE_UTF8:
    {
        const WS_XML_UTF8_TEXT *text_utf8 = (const WS_XML_UTF8_TEXT *)text;
        if (text_utf8->value.length == 4 && !memcmp( text_utf8->value.bytes, "true", 4 )) return TRUE;
        return FALSE;
    }
    case WS_XML_TEXT_TYPE_BOOL:
    {
        const WS_XML_BOOL_TEXT *text_bool = (const WS_XML_BOOL_TEXT *)text;
        return text_bool->value;
    }
    default:
        ERR( "unhandled text type %u\n", text->textType );
        return FALSE;
    }
}

6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521
static HRESULT is_nil_element( const WS_XML_ELEMENT_NODE *elem )
{
    static const WS_XML_STRING localname = {3, (BYTE *)"nil"};
    static const WS_XML_STRING ns = {41, (BYTE *)"http://www.w3.org/2001/XMLSchema-instance"};
    ULONG i;

    for (i = 0; i < elem->attributeCount; i++)
    {
        if (elem->attributes[i]->isXmlNs) continue;
        if (WsXmlStringEquals( elem->attributes[i]->localName, &localname, NULL ) == S_OK &&
            WsXmlStringEquals( elem->attributes[i]->ns, &ns, NULL ) == S_OK &&
6522
            is_true_text( elem->attributes[i]->value )) return TRUE;
6523 6524 6525 6526
    }
    return FALSE;
}

6527
static HRESULT read_type( struct reader *reader, WS_TYPE_MAPPING mapping, WS_TYPE type,
6528
                          const WS_XML_STRING *localname, const WS_XML_STRING *ns, const void *desc,
6529
                          WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size, BOOL *found )
6530
{
6531 6532
    HRESULT hr;

6533
    if ((hr = start_mapping( reader, mapping, localname, ns )) != S_OK) return hr;
6534

6535 6536 6537
    if (mapping == WS_ELEMENT_TYPE_MAPPING && is_nil_element( &reader->current->hdr ))
    {
        if (option != WS_READ_NILLABLE_POINTER && option != WS_READ_NILLABLE_VALUE) return WS_E_INVALID_FORMAT;
6538
        *found = TRUE;
6539
        return end_mapping( reader, mapping );
6540 6541
    }

6542 6543 6544
    switch (type)
    {
    case WS_BOOL_TYPE:
6545
        hr = read_type_bool( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6546 6547
        break;

6548
    case WS_INT8_TYPE:
6549
        hr = read_type_int8( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6550 6551
        break;

6552
    case WS_INT16_TYPE:
6553
        hr = read_type_int16( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6554 6555
        break;

6556
    case WS_INT32_TYPE:
6557
        hr = read_type_int32( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6558 6559
        break;

6560
    case WS_INT64_TYPE:
6561
        hr = read_type_int64( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6562 6563
        break;

6564
    case WS_UINT8_TYPE:
6565
        hr = read_type_uint8( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6566 6567
        break;

6568
    case WS_UINT16_TYPE:
6569
        hr = read_type_uint16( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6570 6571
        break;

6572
    case WS_UINT32_TYPE:
6573
        hr = read_type_uint32( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6574 6575
        break;

6576
    case WS_UINT64_TYPE:
6577
        hr = read_type_uint64( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6578 6579
        break;

6580
    case WS_FLOAT_TYPE:
6581
        hr = read_type_float( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6582 6583
        break;

6584
    case WS_DOUBLE_TYPE:
6585
        hr = read_type_double( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6586 6587
        break;

6588
    case WS_DATETIME_TYPE:
6589
        hr = read_type_datetime( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6590 6591 6592
        break;

    case WS_GUID_TYPE:
6593
        hr = read_type_guid( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6594 6595
        break;

6596
    case WS_UNIQUE_ID_TYPE:
6597
        hr = read_type_unique_id( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6598 6599
        break;

6600
    case WS_STRING_TYPE:
6601
        hr = read_type_string( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6602 6603
        break;

6604
    case WS_WSZ_TYPE:
6605
        hr = read_type_wsz( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6606 6607
        break;

6608
    case WS_BYTES_TYPE:
6609
        hr = read_type_bytes( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6610 6611
        break;

6612
    case WS_XML_STRING_TYPE:
6613
        hr = read_type_xml_string( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6614 6615
        break;

6616
    case WS_XML_QNAME_TYPE:
6617
        hr = read_type_qname( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6618 6619
        break;

6620
    case WS_STRUCT_TYPE:
6621
        hr = read_type_struct( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6622 6623
        break;

6624
    case WS_ENUM_TYPE:
6625
        hr = read_type_enum( reader, mapping, localname, ns, desc, option, heap, value, size, found );
6626 6627
        break;

6628 6629 6630 6631
    default:
        FIXME( "type %u not supported\n", type );
        return E_NOTIMPL;
    }
6632

6633
    if (hr != S_OK) return hr;
6634
    return end_mapping( reader, mapping );
6635
}
6636

6637 6638 6639 6640 6641 6642 6643 6644
/**************************************************************************
 *          WsReadType		[webservices.@]
 */
HRESULT WINAPI WsReadType( WS_XML_READER *handle, WS_TYPE_MAPPING mapping, WS_TYPE type,
                           const void *desc, WS_READ_OPTION option, WS_HEAP *heap, void *value,
                           ULONG size, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
6645
    BOOL found;
6646
    HRESULT hr;
6647 6648 6649 6650 6651 6652 6653

    TRACE( "%p %u %u %p %u %p %p %u %p\n", handle, mapping, type, desc, option, heap, value,
           size, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !value) return E_INVALIDARG;

6654 6655 6656 6657 6658 6659 6660 6661
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

6662
    if ((hr = read_type( reader, mapping, type, NULL, NULL, desc, option, heap, value, size, &found )) == S_OK)
6663
    {
6664 6665 6666 6667 6668
        switch (mapping)
        {
        case WS_ELEMENT_TYPE_MAPPING:
            hr = read_node( reader );
            break;
6669

6670 6671 6672 6673
        default:
            break;
        }
        if (hr == S_OK && !read_end_of_data( reader )) hr = WS_E_INVALID_FORMAT;
6674 6675
    }

6676
    LeaveCriticalSection( &reader->cs );
6677
    TRACE( "returning %08x\n", hr );
6678
    return hr;
6679 6680
}

6681 6682 6683 6684 6685
HRESULT read_header( WS_XML_READER *handle, const WS_XML_STRING *localname, const WS_XML_STRING *ns,
                     WS_TYPE type, const void *desc, WS_READ_OPTION option, WS_HEAP *heap, void *value,
                     ULONG size )
{
    struct reader *reader = (struct reader *)handle;
6686
    BOOL found;
6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697
    HRESULT hr;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_type( reader, WS_ELEMENT_CONTENT_TYPE_MAPPING, type, localname, ns, desc, option, heap,
6698
                    value, size, &found );
6699 6700 6701 6702 6703

    LeaveCriticalSection( &reader->cs );
    return hr;
}

6704 6705 6706 6707 6708 6709 6710 6711
/**************************************************************************
 *          WsReadElement		[webservices.@]
 */
HRESULT WINAPI WsReadElement( WS_XML_READER *handle, const WS_ELEMENT_DESCRIPTION *desc,
                              WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size,
                              WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
6712
    BOOL found;
6713
    HRESULT hr;
6714 6715 6716 6717 6718 6719

    TRACE( "%p %p %u %p %p %u %p\n", handle, desc, option, heap, value, size, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !desc || !value) return E_INVALIDARG;

6720 6721 6722 6723 6724 6725 6726 6727 6728
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, desc->type, desc->elementLocalName,
6729
                    desc->elementNs, desc->typeDescription, option, heap, value, size, &found );
6730 6731

    LeaveCriticalSection( &reader->cs );
6732
    TRACE( "returning %08x\n", hr );
6733
    return hr;
6734 6735
}

6736 6737 6738 6739 6740 6741 6742 6743
/**************************************************************************
 *          WsReadValue		[webservices.@]
 */
HRESULT WINAPI WsReadValue( WS_XML_READER *handle, WS_VALUE_TYPE value_type, void *value, ULONG size,
                            WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
    WS_TYPE type = map_value_type( value_type );
6744
    BOOL found;
6745
    HRESULT hr;
6746 6747 6748 6749 6750 6751

    TRACE( "%p %u %p %u %p\n", handle, type, value, size, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !value || type == ~0u) return E_INVALIDARG;

6752 6753 6754 6755 6756 6757 6758 6759 6760
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    hr = read_type( reader, WS_ELEMENT_TYPE_MAPPING, type, NULL, NULL, NULL, WS_READ_REQUIRED_VALUE,
6761
                    NULL, value, size, &found );
6762 6763

    LeaveCriticalSection( &reader->cs );
6764
    TRACE( "returning %08x\n", hr );
6765
    return hr;
6766 6767
}

6768 6769 6770 6771 6772 6773 6774 6775
/**************************************************************************
 *          WsReadAttribute		[webservices.@]
 */
HRESULT WINAPI WsReadAttribute( WS_XML_READER *handle, const WS_ATTRIBUTE_DESCRIPTION *desc,
                                WS_READ_OPTION option, WS_HEAP *heap, void *value, ULONG size,
                                WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
6776
    BOOL found;
6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791
    HRESULT hr;

    TRACE( "%p %p %u %p %p %u %p\n", handle, desc, option, heap, value, size, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !desc || !value) return E_INVALIDARG;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

6792 6793 6794
    if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
    else hr = read_type( reader, WS_ATTRIBUTE_TYPE_MAPPING, desc->type, desc->attributeLocalName,
                         desc->attributeNs, desc->typeDescription, option, heap, value, size, &found );
6795 6796

    LeaveCriticalSection( &reader->cs );
6797
    TRACE( "returning %08x\n", hr );
6798 6799 6800
    return hr;
}

6801 6802 6803
static inline BOOL is_utf8( const unsigned char *data, ULONG size, ULONG *offset )
{
    static const char bom[] = {0xef,0xbb,0xbf};
6804
    return (size >= sizeof(bom) && !memcmp( data, bom, sizeof(bom) ) && (*offset = sizeof(bom))) ||
6805 6806 6807 6808 6809 6810
           (size > 2 && !(*offset = 0));
}

static inline BOOL is_utf16le( const unsigned char *data, ULONG size, ULONG *offset )
{
    static const char bom[] = {0xff,0xfe};
6811 6812
    return (size >= sizeof(bom) && !memcmp( data, bom, sizeof(bom) ) && (*offset = sizeof(bom))) ||
           (size >= 4 && data[0] == '<' && !data[1] && !(*offset = 0));
6813 6814
}

6815
static WS_CHARSET detect_charset( const unsigned char *data, ULONG size, ULONG *offset )
6816
{
6817
    WS_CHARSET ret = 0;
6818 6819 6820

    /* FIXME: parse xml declaration */

6821 6822
    if (is_utf16le( data, size, offset )) ret = WS_CHARSET_UTF16LE;
    else if (is_utf8( data, size, offset )) ret = WS_CHARSET_UTF8;
6823 6824 6825
    else
    {
        FIXME( "charset not recognized\n" );
6826
        return 0;
6827 6828
    }

6829 6830
    TRACE( "detected charset %u\n", ret );
    return ret;
6831 6832
}

6833
static void set_input_buffer( struct reader *reader, const unsigned char *data, ULONG size )
6834 6835
{
    reader->input_type  = WS_XML_READER_INPUT_TYPE_BUFFER;
6836
    reader->input_buf   = NULL;
6837 6838 6839 6840 6841 6842
    reader->input_data  = data;
    reader->input_size  = size;

    reader->read_size   = reader->input_size;
    reader->read_pos    = 0;
    reader->read_bufptr = reader->input_data;
6843 6844

    reader->text_conv_offset = 0;
6845 6846
}

6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867
static void set_input_stream( struct reader *reader, WS_READ_CALLBACK callback, void *state )
{
    reader->input_type     = WS_XML_READER_INPUT_TYPE_STREAM;
    reader->input_cb       = callback;
    reader->input_cb_state = state;
    reader->input_buf      = NULL;
    reader->input_data     = reader->stream_buf;
    reader->input_size     = STREAM_BUFSIZE;

    if (reader->read_pos >= reader->read_size) reader->read_size = 0;
    else
    {
        memmove( reader->stream_buf, reader->stream_buf + reader->read_pos, reader->read_size - reader->read_pos );
        reader->read_size -= reader->read_pos;
    }
    reader->read_pos    = 0;
    reader->read_bufptr = reader->input_data;

    reader->text_conv_offset = 0;
}

6868 6869 6870 6871 6872 6873 6874 6875
/**************************************************************************
 *          WsSetInput		[webservices.@]
 */
HRESULT WINAPI WsSetInput( WS_XML_READER *handle, const WS_XML_READER_ENCODING *encoding,
                           const WS_XML_READER_INPUT *input, const WS_XML_READER_PROPERTY *properties,
                           ULONG count, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
6876
    struct node *node;
6877
    ULONG i, offset = 0;
6878
    HRESULT hr;
6879 6880 6881 6882 6883 6884

    TRACE( "%p %p %p %p %u %p\n", handle, encoding, input, properties, count, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;

6885 6886 6887 6888 6889 6890 6891 6892
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

6893 6894
    for (i = 0; i < count; i++)
    {
6895 6896
        hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
                       properties[i].valueSize );
6897
        if (hr != S_OK) goto done;
6898 6899
    }

6900
    if ((hr = init_reader( reader )) != S_OK) goto done;
6901

6902 6903
    switch (encoding->encodingType)
    {
6904 6905
    case WS_XML_READER_ENCODING_TYPE_TEXT:
    {
6906
        if (input->inputType == WS_XML_READER_INPUT_TYPE_BUFFER)
6907
        {
6908 6909
            const WS_XML_READER_TEXT_ENCODING *text = (const WS_XML_READER_TEXT_ENCODING *)encoding;
            const WS_XML_READER_BUFFER_INPUT *buf = (const WS_XML_READER_BUFFER_INPUT *)input;
6910 6911
            if (text->charSet != WS_CHARSET_AUTO) reader->input_charset = text->charSet;
            else reader->input_charset = detect_charset( buf->encodedData, buf->encodedDataSize, &offset );
6912
        }
6913

6914
        reader->input_enc = WS_XML_READER_ENCODING_TYPE_TEXT;
6915 6916
        break;
    }
6917
    case WS_XML_READER_ENCODING_TYPE_BINARY:
6918
    {
6919
        const WS_XML_READER_BINARY_ENCODING *bin = (const WS_XML_READER_BINARY_ENCODING *)encoding;
6920 6921
        reader->input_enc     = WS_XML_READER_ENCODING_TYPE_BINARY;
        reader->input_charset = 0;
6922 6923
        reader->dict_static   = bin->staticDictionary ? bin->staticDictionary : &dict_builtin_static.dict;
        reader->dict          = bin->dynamicDictionary ? bin->dynamicDictionary : &dict_builtin.dict;
6924
        break;
6925
    }
6926 6927
    default:
        FIXME( "encoding type %u not supported\n", encoding->encodingType );
6928 6929
        hr = E_NOTIMPL;
        goto done;
6930
    }
6931

6932 6933
    switch (input->inputType)
    {
6934 6935
    case WS_XML_READER_INPUT_TYPE_BUFFER:
    {
6936
        const WS_XML_READER_BUFFER_INPUT *buf = (const WS_XML_READER_BUFFER_INPUT *)input;
6937 6938 6939 6940 6941
        set_input_buffer( reader, (const unsigned char *)buf->encodedData + offset, buf->encodedDataSize - offset );
        break;
    }
    case WS_XML_READER_INPUT_TYPE_STREAM:
    {
6942
        const WS_XML_READER_STREAM_INPUT *stream = (const WS_XML_READER_STREAM_INPUT *)input;
6943 6944 6945 6946 6947 6948
        if (!reader->stream_buf && !(reader->stream_buf = heap_alloc( STREAM_BUFSIZE )))
        {
            hr = E_OUTOFMEMORY;
            goto done;
        }
        set_input_stream( reader, stream->readCallback, stream->readCallbackState );
6949 6950 6951 6952
        break;
    }
    default:
        FIXME( "input type %u not supported\n", input->inputType );
6953 6954
        hr = E_NOTIMPL;
        goto done;
6955 6956
    }

6957 6958 6959 6960 6961
    if (!(node = alloc_node( WS_XML_NODE_TYPE_BOF ))) hr = E_OUTOFMEMORY;
    else read_insert_bof( reader, node );

done:
    LeaveCriticalSection( &reader->cs );
6962
    TRACE( "returning %08x\n", hr );
6963
    return hr;
6964 6965
}

6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983
static void set_input_xml_buffer( struct reader *reader, struct xmlbuf *buf )
{
    reader->input_type    = WS_XML_READER_INPUT_TYPE_BUFFER;
    reader->input_buf     = buf;
    reader->input_enc     = buf->encoding;
    reader->input_charset = buf->charset;
    reader->input_data    = buf->bytes.bytes;
    reader->input_size    = buf->bytes.length;
    reader->dict_static   = buf->dict_static;
    reader->dict          = buf->dict;

    reader->read_size   = reader->input_size;
    reader->read_pos    = 0;
    reader->read_bufptr = reader->input_data;

    reader->text_conv_offset = 0;
}

6984 6985 6986 6987 6988 6989 6990 6991 6992 6993
/**************************************************************************
 *          WsSetInputToBuffer		[webservices.@]
 */
HRESULT WINAPI WsSetInputToBuffer( WS_XML_READER *handle, WS_XML_BUFFER *buffer,
                                   const WS_XML_READER_PROPERTY *properties, ULONG count,
                                   WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
    struct xmlbuf *xmlbuf = (struct xmlbuf *)buffer;
    struct node *node;
6994
    HRESULT hr;
6995
    ULONG i;
6996 6997 6998 6999 7000 7001

    TRACE( "%p %p %p %u %p\n", handle, buffer, properties, count, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !xmlbuf) return E_INVALIDARG;

7002 7003 7004 7005 7006 7007 7008 7009
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

7010 7011
    for (i = 0; i < count; i++)
    {
7012 7013
        hr = prop_set( reader->prop, reader->prop_count, properties[i].id, properties[i].value,
                       properties[i].valueSize );
7014
        if (hr != S_OK) goto done;
7015 7016
    }

7017
    if ((hr = init_reader( reader )) != S_OK) goto done;
7018
    set_input_xml_buffer( reader, xmlbuf );
7019

7020 7021 7022 7023 7024
    if (!(node = alloc_node( WS_XML_NODE_TYPE_BOF ))) hr = E_OUTOFMEMORY;
    else read_insert_bof( reader, node );

done:
    LeaveCriticalSection( &reader->cs );
7025
    TRACE( "returning %08x\n", hr );
7026
    return hr;
7027
}
7028

7029 7030 7031 7032 7033 7034
/**************************************************************************
 *          WsGetReaderPosition		[webservices.@]
 */
HRESULT WINAPI WsGetReaderPosition( WS_XML_READER *handle, WS_XML_NODE_POSITION *pos, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
7035
    HRESULT hr = S_OK;
7036 7037 7038 7039 7040

    TRACE( "%p %p %p\n", handle, pos, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !pos) return E_INVALIDARG;
7041 7042 7043 7044 7045 7046 7047 7048 7049

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

7050 7051
    if (!reader->input_buf) hr = WS_E_INVALID_OPERATION;
    else
7052
    {
7053 7054
        pos->buffer = (WS_XML_BUFFER *)reader->input_buf;
        pos->node   = reader->current;
7055
    }
7056

7057
    LeaveCriticalSection( &reader->cs );
7058 7059
    TRACE( "returning %08x\n", hr );
    return hr;
7060
}
7061 7062 7063 7064 7065 7066 7067

/**************************************************************************
 *          WsSetReaderPosition		[webservices.@]
 */
HRESULT WINAPI WsSetReaderPosition( WS_XML_READER *handle, const WS_XML_NODE_POSITION *pos, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
7068
    HRESULT hr = S_OK;
7069 7070 7071 7072

    TRACE( "%p %p %p\n", handle, pos, error );
    if (error) FIXME( "ignoring error parameter\n" );

7073 7074 7075 7076 7077 7078 7079 7080 7081 7082
    if (!reader || !pos) return E_INVALIDARG;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC || (struct xmlbuf *)pos->buffer != reader->input_buf)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

7083 7084
    if (!reader->input_buf) hr = WS_E_INVALID_OPERATION;
    else reader->current = pos->node;
7085 7086

    LeaveCriticalSection( &reader->cs );
7087 7088
    TRACE( "returning %08x\n", hr );
    return hr;
7089
}
7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104

static HRESULT utf8_to_base64( const WS_XML_UTF8_TEXT *utf8, WS_XML_BASE64_TEXT *base64 )
{
    if (utf8->value.length % 4) return WS_E_INVALID_FORMAT;
    if (!(base64->bytes = heap_alloc( utf8->value.length * 3 / 4 ))) return E_OUTOFMEMORY;
    base64->length = decode_base64( utf8->value.bytes, utf8->value.length, base64->bytes );
    return S_OK;
}

/**************************************************************************
 *          WsReadBytes		[webservices.@]
 */
HRESULT WINAPI WsReadBytes( WS_XML_READER *handle, void *bytes, ULONG max_count, ULONG *count, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
7105
    HRESULT hr = S_OK;
7106 7107 7108 7109 7110

    TRACE( "%p %p %u %p %p\n", handle, bytes, max_count, count, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    if (!reader->input_type)
    {
7122 7123
        hr = WS_E_INVALID_OPERATION;
        goto done;
7124 7125 7126
    }
    if (!count)
    {
7127 7128
        hr = E_INVALIDARG;
        goto done;
7129
    }
7130 7131 7132 7133 7134 7135 7136

    *count = 0;
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && bytes)
    {
        const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
        WS_XML_BASE64_TEXT base64;

7137
        if ((hr = utf8_to_base64( (const WS_XML_UTF8_TEXT *)text->text, &base64 )) != S_OK) goto done;
7138 7139 7140
        if (reader->text_conv_offset == base64.length)
        {
            heap_free( base64.bytes );
7141
            hr = read_node( reader );
7142
            goto done;
7143 7144 7145 7146 7147 7148 7149
        }
        *count = min( base64.length - reader->text_conv_offset, max_count );
        memcpy( bytes, base64.bytes + reader->text_conv_offset, *count );
        reader->text_conv_offset += *count;
        heap_free( base64.bytes );
    }

7150
done:
7151
    LeaveCriticalSection( &reader->cs );
7152 7153
    TRACE( "returning %08x\n", hr );
    return hr;
7154
}
7155

7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170
static HRESULT utf8_to_utf16( const WS_XML_UTF8_TEXT *utf8, WS_XML_UTF16_TEXT *utf16 )
{
    int len = MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, NULL, 0 );
    if (!(utf16->bytes = heap_alloc( len * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
    MultiByteToWideChar( CP_UTF8, 0, (char *)utf8->value.bytes, utf8->value.length, (WCHAR *)utf16->bytes, len );
    utf16->byteCount = len * sizeof(WCHAR);
    return S_OK;
}

/**************************************************************************
 *          WsReadChars		[webservices.@]
 */
HRESULT WINAPI WsReadChars( WS_XML_READER *handle, WCHAR *chars, ULONG max_count, ULONG *count, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
7171
    HRESULT hr = S_OK;
7172 7173 7174 7175 7176

    TRACE( "%p %p %u %p %p\n", handle, chars, max_count, count, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    if (!reader->input_type)
    {
7188 7189
        hr = WS_E_INVALID_OPERATION;
        goto done;
7190 7191 7192
    }
    if (!count)
    {
7193 7194
        hr = E_INVALIDARG;
        goto done;
7195
    }
7196 7197 7198 7199 7200 7201 7202 7203

    *count = 0;
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && chars)
    {
        const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
        WS_XML_UTF16_TEXT utf16;
        HRESULT hr;

7204
        if ((hr = utf8_to_utf16( (const WS_XML_UTF8_TEXT *)text->text, &utf16 )) != S_OK) goto done;
7205 7206 7207
        if (reader->text_conv_offset == utf16.byteCount / sizeof(WCHAR))
        {
            heap_free( utf16.bytes );
7208
            hr = read_node( reader );
7209
            goto done;
7210 7211 7212 7213 7214 7215 7216
        }
        *count = min( utf16.byteCount / sizeof(WCHAR) - reader->text_conv_offset, max_count );
        memcpy( chars, utf16.bytes + reader->text_conv_offset * sizeof(WCHAR), *count * sizeof(WCHAR) );
        reader->text_conv_offset += *count;
        heap_free( utf16.bytes );
    }

7217
done:
7218
    LeaveCriticalSection( &reader->cs );
7219 7220
    TRACE( "returning %08x\n", hr );
    return hr;
7221 7222
}

7223 7224 7225 7226 7227 7228
/**************************************************************************
 *          WsReadCharsUtf8		[webservices.@]
 */
HRESULT WINAPI WsReadCharsUtf8( WS_XML_READER *handle, BYTE *bytes, ULONG max_count, ULONG *count, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
7229
    HRESULT hr = S_OK;
7230 7231 7232 7233 7234

    TRACE( "%p %p %u %p %p\n", handle, bytes, max_count, count, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader) return E_INVALIDARG;
7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    if (!reader->input_type)
    {
7246 7247
        hr = WS_E_INVALID_OPERATION;
        goto done;
7248 7249 7250
    }
    if (!count)
    {
7251 7252
        hr = E_INVALIDARG;
        goto done;
7253
    }
7254 7255 7256 7257 7258 7259 7260

    *count = 0;
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_TEXT && bytes)
    {
        const WS_XML_TEXT_NODE *text = (const WS_XML_TEXT_NODE *)reader->current;
        const WS_XML_UTF8_TEXT *utf8 = (const WS_XML_UTF8_TEXT *)text->text;

7261 7262 7263
        if (reader->text_conv_offset == utf8->value.length)
        {
            hr = read_node( reader );
7264
            goto done;
7265
        }
7266 7267 7268 7269 7270
        *count = min( utf8->value.length - reader->text_conv_offset, max_count );
        memcpy( bytes, utf8->value.bytes + reader->text_conv_offset, *count );
        reader->text_conv_offset += *count;
    }

7271
done:
7272
    LeaveCriticalSection( &reader->cs );
7273 7274
    TRACE( "returning %08x\n", hr );
    return hr;
7275
}
7276

7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310
static HRESULT move_to_element( struct reader *reader )
{
    HRESULT hr;
    if (node_type( reader->current ) == WS_XML_NODE_TYPE_BOF &&
        (hr = read_move_to( reader, WS_MOVE_TO_CHILD_NODE, NULL )) != S_OK) return hr;
    if (node_type( reader->current ) != WS_XML_NODE_TYPE_ELEMENT) return E_FAIL;
    return S_OK;
}

static HRESULT copy_tree( struct reader *reader, WS_XML_WRITER *writer )
{
    const struct node *node, *parent;
    BOOL done = FALSE;
    HRESULT hr;

    if ((hr = move_to_element( reader )) != S_OK) return hr;
    parent = reader->current;
    for (;;)
    {
        node = reader->current;
        if ((hr = WsWriteNode( writer, (const WS_XML_NODE *)node, NULL )) != S_OK) break;
        if (node_type( node ) == WS_XML_NODE_TYPE_END_ELEMENT && node->parent == parent) done = TRUE;
        if ((hr = read_next_node( reader )) != S_OK || done) break;
    }
    return hr;
}

/**************************************************************************
 *          WsReadXmlBuffer		[webservices.@]
 */
HRESULT WINAPI WsReadXmlBuffer( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_BUFFER **ret, WS_ERROR *error )
{
    struct reader *reader = (struct reader *)handle;
    WS_XML_WRITER *writer = NULL;
7311
    WS_XML_BUFFER *buffer = NULL;
7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327
    HRESULT hr;

    TRACE( "%p %p %p %p\n", handle, heap, ret, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!reader || !heap) return E_INVALIDARG;
    if (!ret) return E_FAIL;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

7328 7329
    if (!reader->input_type) hr = WS_E_INVALID_OPERATION;
    else
7330
    {
7331 7332 7333 7334
        if ((hr = WsCreateWriter( NULL, 0, &writer, NULL )) != S_OK) goto done;
        if ((hr = WsCreateXmlBuffer( heap, NULL, 0, &buffer, NULL )) != S_OK) goto done;
        if ((hr = WsSetOutputToBuffer( writer, buffer, NULL, 0, NULL )) != S_OK) goto done;
        if ((hr = copy_tree( reader, writer )) == S_OK) *ret = buffer;
7335 7336 7337 7338 7339 7340
    }

done:
    if (hr != S_OK) free_xmlbuf( (struct xmlbuf *)buffer );
    WsFreeWriter( writer );
    LeaveCriticalSection( &reader->cs );
7341
    TRACE( "returning %08x\n", hr );
7342 7343 7344
    return hr;
}

7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371
HRESULT create_header_buffer( WS_XML_READER *handle, WS_HEAP *heap, WS_XML_BUFFER **ret )
{
    struct reader *reader = (struct reader *)handle;
    HRESULT hr = WS_E_QUOTA_EXCEEDED;
    struct xmlbuf *xmlbuf;

    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

    if ((xmlbuf = alloc_xmlbuf( heap, reader->read_pos, reader->input_enc, reader->input_charset,
                                reader->dict_static, reader->dict )))
    {
        memcpy( xmlbuf->bytes.bytes, reader->read_bufptr, reader->read_pos );
        xmlbuf->bytes.length = reader->read_pos;
        *ret = (WS_XML_BUFFER *)xmlbuf;
        hr = S_OK;
    }

    LeaveCriticalSection( &reader->cs );
    return hr;
}

7372 7373 7374 7375 7376 7377 7378 7379 7380
HRESULT get_param_desc( const WS_STRUCT_DESCRIPTION *desc, USHORT index, const WS_FIELD_DESCRIPTION **ret )
{
    if (index >= desc->fieldCount) return E_INVALIDARG;
    *ret = desc->fields[index];
    return S_OK;
}

static ULONG get_field_size( const WS_FIELD_DESCRIPTION *desc )
{
7381 7382
    if (desc->options & WS_FIELD_POINTER) return sizeof(void *);
    return get_type_size( desc->type, desc->typeDescription );
7383 7384 7385 7386 7387
}

static HRESULT read_param( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap, void *ret )
{
    if (!ret && !(ret = ws_alloc_zero( heap, get_field_size(desc) ))) return WS_E_QUOTA_EXCEEDED;
7388
    return read_type_field( reader, desc, heap, ret, 0 );
7389 7390 7391 7392 7393 7394
}

static HRESULT read_param_array( struct reader *reader, const WS_FIELD_DESCRIPTION *desc, WS_HEAP *heap,
                                 void **ret, ULONG *count )
{
    if (!ret && !(ret = ws_alloc_zero( heap, sizeof(void **) ))) return WS_E_QUOTA_EXCEEDED;
7395
    return read_type_array( reader, desc, heap, ret, count );
7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421
}

static void set_array_len( const WS_PARAMETER_DESCRIPTION *params, ULONG count, ULONG index, ULONG len,
                           const void **args )
{
    ULONG i, *ptr;
    for (i = 0; i < count; i++)
    {
        if (params[i].outputMessageIndex != index || params[i].parameterType != WS_PARAMETER_TYPE_ARRAY_COUNT)
            continue;
        if ((ptr = *(ULONG **)args[i])) *ptr = len;
        break;
    }
}

HRESULT read_output_params( WS_XML_READER *handle, WS_HEAP *heap, const WS_ELEMENT_DESCRIPTION *desc,
                            const WS_PARAMETER_DESCRIPTION *params, ULONG count, const void **args )
{
    struct reader *reader = (struct reader *)handle;
    const WS_STRUCT_DESCRIPTION *desc_struct;
    const WS_FIELD_DESCRIPTION *desc_field;
    ULONG i, len;
    HRESULT hr;

    if (desc->type != WS_STRUCT_TYPE || !(desc_struct = desc->typeDescription)) return E_INVALIDARG;

7422 7423 7424 7425 7426 7427 7428 7429
    EnterCriticalSection( &reader->cs );

    if (reader->magic != READER_MAGIC)
    {
        LeaveCriticalSection( &reader->cs );
        return E_INVALIDARG;
    }

7430
    if ((hr = start_mapping( reader, WS_ELEMENT_TYPE_MAPPING, desc->elementLocalName, desc->elementNs )) != S_OK)
7431
        goto done;
7432 7433 7434 7435 7436 7437 7438

    for (i = 0; i < count; i++)
    {
        if (params[i].outputMessageIndex == INVALID_PARAMETER_INDEX) continue;
        if (params[i].parameterType == WS_PARAMETER_TYPE_MESSAGES)
        {
            FIXME( "messages type not supported\n" );
7439 7440
            hr = E_NOTIMPL;
            goto done;
7441
        }
7442
        if ((hr = get_param_desc( desc_struct, params[i].outputMessageIndex, &desc_field )) != S_OK) goto done;
7443 7444 7445
        if (params[i].parameterType == WS_PARAMETER_TYPE_NORMAL)
        {
            void *ptr = *(void **)args[i];
7446
            if ((hr = read_param( reader, desc_field, heap, ptr )) != S_OK) goto done;
7447 7448 7449 7450
        }
        else if (params[i].parameterType == WS_PARAMETER_TYPE_ARRAY)
        {
            void **ptr = *(void ***)args[i];
7451
            if ((hr = read_param_array( reader, desc_field, heap, ptr, &len )) != S_OK) goto done;
7452 7453 7454 7455 7456 7457 7458 7459 7460 7461
            set_array_len( params, count, params[i].outputMessageIndex, len, args );
        }
    }

    if (desc_struct->structOptions & WS_STRUCT_IGNORE_TRAILING_ELEMENT_CONTENT)
    {
        struct node *parent = find_parent( reader );
        parent->flags |= NODE_FLAG_IGNORE_TRAILING_ELEMENT_CONTENT;
    }

7462 7463 7464 7465 7466
    hr = end_mapping( reader, WS_ELEMENT_TYPE_MAPPING );

done:
    LeaveCriticalSection( &reader->cs );
    return hr;
7467
}