saxreader.c 82.8 KB
Newer Older
1 2 3 4
/*
 *    SAX Reader implementation
 *
 * Copyright 2008 Alistair Leslie-Hughes
5
 * Copyright 2008 Piotr Caban
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 */
#define COBJMACROS

#include "config.h"

#include <stdarg.h>
26 27 28 29 30 31 32
#ifdef HAVE_LIBXML2
# include <libxml/parser.h>
# include <libxml/xmlerror.h>
# include <libxml/SAX2.h>
# include <libxml/parserInternals.h>
#endif

33 34 35 36 37
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winnls.h"
#include "ole2.h"
38
#include "msxml6.h"
39 40 41 42 43 44 45 46 47 48 49 50 51
#include "wininet.h"
#include "urlmon.h"
#include "winreg.h"
#include "shlwapi.h"

#include "wine/debug.h"

#include "msxml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(msxml);

#ifdef HAVE_LIBXML2

52 53 54 55 56 57 58
struct bstrpool
{
    BSTR *pool;
    unsigned int index;
    unsigned int len;
};

59 60
typedef struct _saxreader
{
61 62
    IVBSAXXMLReader IVBSAXXMLReader_iface;
    ISAXXMLReader ISAXXMLReader_iface;
63
    LONG ref;
64
    struct ISAXContentHandler *contentHandler;
65
    struct IVBSAXContentHandler *vbcontentHandler;
66
    struct ISAXErrorHandler *errorHandler;
67
    struct IVBSAXErrorHandler *vberrorHandler;
68 69 70 71
    struct ISAXLexicalHandler *lexicalHandler;
    struct IVBSAXLexicalHandler *vblexicalHandler;
    struct ISAXDeclHandler *declHandler;
    struct IVBSAXDeclHandler *vbdeclHandler;
72
    xmlSAXHandler sax;
73
    BOOL isParsing;
74
    struct bstrpool pool;
75 76
} saxreader;

77 78
typedef struct _saxlocator
{
79 80
    IVBSAXLocator IVBSAXLocator_iface;
    ISAXLocator ISAXLocator_iface;
81 82
    LONG ref;
    saxreader *saxreader;
83 84
    HRESULT ret;
    xmlParserCtxtPtr pParserCtxt;
85
    WCHAR *publicId;
86
    WCHAR *systemId;
87 88
    xmlChar *lastCur;
    int line;
89
    int realLine;
90
    int column;
91
    int realColumn;
92
    BOOL vbInterface;
93 94 95
    int nsStackSize;
    int nsStackLast;
    int *nsStack;
96 97
} saxlocator;

98 99
typedef struct _saxattributes
{
100 101
    IVBSAXAttributes IVBSAXAttributes_iface;
    ISAXAttributes ISAXAttributes_iface;
102
    LONG ref;
103 104 105 106
    int nb_attributes;
    BSTR *szLocalname;
    BSTR *szURI;
    BSTR *szValue;
107
    BSTR *szQName;
108 109
} saxattributes;

110 111
static inline saxreader *impl_from_IVBSAXXMLReader( IVBSAXXMLReader *iface )
{
112
    return CONTAINING_RECORD(iface, saxreader, IVBSAXXMLReader_iface);
113
}
114

115 116
static inline saxreader *impl_from_ISAXXMLReader( ISAXXMLReader *iface )
{
117
    return CONTAINING_RECORD(iface, saxreader, ISAXXMLReader_iface);
118 119
}

120 121
static inline saxlocator *impl_from_IVBSAXLocator( IVBSAXLocator *iface )
{
122
    return CONTAINING_RECORD(iface, saxlocator, IVBSAXLocator_iface);
123 124
}

125 126
static inline saxlocator *impl_from_ISAXLocator( ISAXLocator *iface )
{
127
    return CONTAINING_RECORD(iface, saxlocator, ISAXLocator_iface);
128 129
}

130 131
static inline saxattributes *impl_from_IVBSAXAttributes( IVBSAXAttributes *iface )
{
132
    return CONTAINING_RECORD(iface, saxattributes, IVBSAXAttributes_iface);
133 134
}

135 136
static inline saxattributes *impl_from_ISAXAttributes( ISAXAttributes *iface )
{
137
    return CONTAINING_RECORD(iface, saxattributes, ISAXAttributes_iface);
138 139
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* property names */
static const WCHAR PropertyCharsetW[] = {
    'c','h','a','r','s','e','t',0
};
static const WCHAR PropertyDeclHandlerW[] = {
    'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/',
    's','a','x','/','p','r','o','p','e','r','t','i','e','s','/',
    'd','e','c','l','a','r','a','t','i','o','n',
    '-','h','a','n','d','l','e','r',0
};
static const WCHAR PropertyDomNodeW[] = {
    'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/',
    's','a','x','/','p','r','o','p','e','r','t','i','e','s','/',
    'd','o','m','-','n','o','d','e',0
};
static const WCHAR PropertyInputSourceW[] = {
    'i','n','p','u','t','-','s','o','u','r','c','e',0
};
static const WCHAR PropertyLexicalHandlerW[] = {
    'h','t','t','p',':','/','/','x','m','l','.','o','r','g','/',
    's','a','x','/','p','r','o','p','e','r','t','i','e','s','/',
    'l','e','x','i','c','a','l','-','h','a','n','d','l','e','r',0
};
static const WCHAR PropertyMaxElementDepthW[] = {
    'm','a','x','-','e','l','e','m','e','n','t','-','d','e','p','t','h',0
};
static const WCHAR PropertyMaxXMLSizeW[] = {
    'm','a','x','-','x','m','l','-','s','i','z','e',0
};
static const WCHAR PropertySchemaDeclHandlerW[] = {
    's','c','h','e','m','a','-','d','e','c','l','a','r','a','t','i','o','n','-',
    'h','a','n','d','l','e','r',0
};
static const WCHAR PropertyXMLDeclEncodingW[] = {
    'x','m','l','d','e','c','l','-','e','n','c','o','d','i','n','g',0
};
static const WCHAR PropertyXMLDeclStandaloneW[] = {
    'x','m','l','d','e','c','l','-','s','t','a','n','d','a','l','o','n','e',0
};
static const WCHAR PropertyXMLDeclVersionW[] = {
    'x','m','l','d','e','c','l','-','v','e','r','s','i','o','n',0
};

183 184 185 186 187
static inline BOOL has_content_handler(const saxlocator *locator)
{
    return  (locator->vbInterface && locator->saxreader->vbcontentHandler) ||
           (!locator->vbInterface && locator->saxreader->contentHandler);
}
188

189 190 191 192 193 194
static inline BOOL has_error_handler(const saxlocator *locator)
{
    return (locator->vbInterface && locator->saxreader->vberrorHandler) ||
          (!locator->vbInterface && locator->saxreader->errorHandler);
}

195 196 197 198 199 200 201
static HRESULT namespacePush(saxlocator *locator, int ns)
{
    if(locator->nsStackLast>=locator->nsStackSize)
    {
        int *new_stack;

        new_stack = HeapReAlloc(GetProcessHeap(), 0,
202
                locator->nsStack, sizeof(int)*locator->nsStackSize*2);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        if(!new_stack) return E_OUTOFMEMORY;
        locator->nsStack = new_stack;
        locator->nsStackSize *= 2;
    }
    locator->nsStack[locator->nsStackLast++] = ns;

    return S_OK;
}

static int namespacePop(saxlocator *locator)
{
    if(locator->nsStackLast == 0) return 0;
    return locator->nsStack[--locator->nsStackLast];
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
static BOOL bstr_pool_insert(struct bstrpool *pool, BSTR pool_entry)
{
    if (!pool->pool)
    {
        pool->pool = HeapAlloc(GetProcessHeap(), 0, 16 * sizeof(*pool->pool));
        if (!pool->pool)
            return FALSE;

        pool->index = 0;
        pool->len = 16;
    }
    else if (pool->index == pool->len)
    {
        BSTR *realloc = HeapReAlloc(GetProcessHeap(), 0, pool->pool, pool->len * 2 * sizeof(*realloc));

        if (!realloc)
            return FALSE;

        pool->pool = realloc;
        pool->len *= 2;
    }

    pool->pool[pool->index++] = pool_entry;
    return TRUE;
}

static void free_bstr_pool(struct bstrpool *pool)
{
    unsigned int i;

    for (i = 0; i < pool->index; i++)
        SysFreeString(pool->pool[i]);

    HeapFree(GetProcessHeap(), 0, pool->pool);

    pool->pool = NULL;
    pool->index = pool->len = 0;
}

257
static BSTR bstr_from_xmlCharN(const xmlChar *buf, int len)
258 259 260 261 262 263 264 265 266
{
    DWORD dLen;
    BSTR bstr;

    if (!buf)
        return NULL;

    dLen = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, len, NULL, 0);
    if(len != -1) dLen++;
267 268
    bstr = SysAllocStringLen(NULL, dLen-1);
    if (!bstr)
269
        return NULL;
270 271
    MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, len, bstr, dLen);
    if(len != -1) bstr[dLen-1] = '\0';
272 273 274 275

    return bstr;
}

276 277
static BSTR QName_from_xmlChar(const xmlChar *prefix, const xmlChar *name)
{
278
    xmlChar *qname;
279 280 281 282
    BSTR bstr;

    if(!name) return NULL;

283
    if(!prefix || !*prefix)
284 285
        return bstr_from_xmlChar(name);

286 287 288
    qname = xmlBuildQName(name, prefix, NULL, 0);
    bstr = bstr_from_xmlChar(qname);
    xmlFree(qname);
289 290 291 292

    return bstr;
}

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static BSTR pooled_bstr_from_xmlChar(struct bstrpool *pool, const xmlChar *buf)
{
    BSTR pool_entry = bstr_from_xmlChar(buf);

    if (pool_entry && !bstr_pool_insert(pool, pool_entry))
    {
        SysFreeString(pool_entry);
        return NULL;
    }

    return pool_entry;
}

static BSTR pooled_bstr_from_xmlCharN(struct bstrpool *pool, const xmlChar *buf, int len)
{
    BSTR pool_entry = bstr_from_xmlCharN(buf, len);

    if (pool_entry && !bstr_pool_insert(pool, pool_entry))
    {
        SysFreeString(pool_entry);
        return NULL;
    }

    return pool_entry;
}

static BSTR pooled_QName_from_xmlChar(struct bstrpool *pool, const xmlChar *prefix, const xmlChar *name)
{
    BSTR pool_entry = QName_from_xmlChar(prefix, name);

    if (pool_entry && !bstr_pool_insert(pool, pool_entry))
    {
        SysFreeString(pool_entry);
        return NULL;
    }

    return pool_entry;
}

332 333 334 335 336
static void format_error_message_from_id(saxlocator *This, HRESULT hr)
{
    xmlStopParser(This->pParserCtxt);
    This->ret = hr;

337
    if(has_error_handler(This))
338 339 340 341 342 343 344 345 346
    {
        WCHAR msg[1024];
        if(!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
                    NULL, hr, 0, msg, sizeof(msg), NULL))
        {
            FIXME("MSXML errors not yet supported.\n");
            msg[0] = '\0';
        }

347 348 349 350
        if(This->vbInterface)
        {
            BSTR bstrMsg = SysAllocString(msg);
            IVBSAXErrorHandler_fatalError(This->saxreader->vberrorHandler,
351
                    &This->IVBSAXLocator_iface, &bstrMsg, hr);
352
            SysFreeString(bstrMsg);
353 354 355
        }
        else
            ISAXErrorHandler_fatalError(This->saxreader->errorHandler,
356
                    &This->ISAXLocator_iface, msg, hr);
357 358 359
    }
}

360 361 362 363 364
static void update_position(saxlocator *This, xmlChar *end)
{
    if(This->lastCur == NULL)
    {
        This->lastCur = (xmlChar*)This->pParserCtxt->input->base;
365 366
        This->realLine = 1;
        This->realColumn = 1;
367
    }
368 369 370
    else if(This->lastCur < This->pParserCtxt->input->base)
    {
        This->lastCur = (xmlChar*)This->pParserCtxt->input->base;
371 372 373 374 375 376 377 378 379
        This->realLine = 1;
        This->realColumn = 1;
    }

    if(This->pParserCtxt->input->cur<This->lastCur)
    {
        This->lastCur = (xmlChar*)This->pParserCtxt->input->base;
        This->realLine -= 1;
        This->realColumn = 1;
380
    }
381 382 383 384 385 386 387

    if(!end) end = (xmlChar*)This->pParserCtxt->input->cur;

    while(This->lastCur < end)
    {
        if(*(This->lastCur) == '\n')
        {
388 389
            This->realLine++;
            This->realColumn = 1;
390
        }
391 392 393
        else if(*(This->lastCur) == '\r' &&
                (This->lastCur==This->pParserCtxt->input->end ||
                 *(This->lastCur+1)!='\n'))
394
        {
395 396
            This->realLine++;
            This->realColumn = 1;
397
        }
398
        else This->realColumn++;
399 400

        This->lastCur++;
401 402 403

        /* Count multibyte UTF8 encoded characters once */
        while((*(This->lastCur)&0xC0) == 0x80) This->lastCur++;
404
    }
405 406 407

    This->line = This->realLine;
    This->column = This->realColumn;
408 409
}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/*** IVBSAXAttributes interface ***/
/*** IUnknown methods ***/
static HRESULT WINAPI ivbsaxattributes_QueryInterface(
        IVBSAXAttributes* iface,
        REFIID riid,
        void **ppvObject)
{
    saxattributes *This = impl_from_IVBSAXAttributes(iface);

    TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);

    *ppvObject = NULL;

    if (IsEqualGUID(riid, &IID_IUnknown) ||
            IsEqualGUID(riid, &IID_IDispatch) ||
            IsEqualGUID(riid, &IID_IVBSAXAttributes))
    {
        *ppvObject = iface;
    }
    else
    {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    IVBSAXAttributes_AddRef(iface);

    return S_OK;
}

static ULONG WINAPI ivbsaxattributes_AddRef(IVBSAXAttributes* iface)
{
    saxattributes *This = impl_from_IVBSAXAttributes(iface);
443
    return ISAXAttributes_AddRef(&This->ISAXAttributes_iface);
444 445 446 447 448
}

static ULONG WINAPI ivbsaxattributes_Release(IVBSAXAttributes* iface)
{
    saxattributes *This = impl_from_IVBSAXAttributes(iface);
449
    return ISAXAttributes_Release(&This->ISAXAttributes_iface);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
}

/*** IDispatch methods ***/
static HRESULT WINAPI ivbsaxattributes_GetTypeInfoCount( IVBSAXAttributes *iface, UINT* pctinfo )
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;

    return S_OK;
}

static HRESULT WINAPI ivbsaxattributes_GetTypeInfo(
    IVBSAXAttributes *iface,
    UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
    HRESULT hr;

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(IVBSAXAttributes_tid, ppTInfo);

    return hr;
}

static HRESULT WINAPI ivbsaxattributes_GetIDsOfNames(
    IVBSAXAttributes *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId)
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
          lcid, rgDispId);

    if(!rgszNames || cNames == 0 || !rgDispId)
        return E_INVALIDARG;

    hr = get_typeinfo(IVBSAXAttributes_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ivbsaxattributes_Invoke(
    IVBSAXAttributes *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr)
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IVBSAXAttributes_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
527 528
        hr = ITypeInfo_Invoke(typeinfo, &This->IVBSAXAttributes_iface, dispIdMember, wFlags,
                pDispParams, pVarResult, pExcepInfo, puArgErr);
529 530 531 532 533 534 535 536 537 538 539 540
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

/*** IVBSAXAttributes methods ***/
static HRESULT WINAPI ivbsaxattributes_get_length(
        IVBSAXAttributes* iface,
        int *nLength)
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
541
    return ISAXAttributes_getLength(&This->ISAXAttributes_iface, nLength);
542 543 544 545 546 547 548 549 550
}

static HRESULT WINAPI ivbsaxattributes_getURI(
        IVBSAXAttributes* iface,
        int nIndex,
        BSTR *uri)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
551
    return ISAXAttributes_getURI(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)uri, &len);
552 553 554 555 556 557 558 559 560
}

static HRESULT WINAPI ivbsaxattributes_getLocalName(
        IVBSAXAttributes* iface,
        int nIndex,
        BSTR *localName)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
561 562
    return ISAXAttributes_getLocalName(&This->ISAXAttributes_iface, nIndex,
            (const WCHAR**)localName, &len);
563 564 565 566 567 568 569 570 571
}

static HRESULT WINAPI ivbsaxattributes_getQName(
        IVBSAXAttributes* iface,
        int nIndex,
        BSTR *QName)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
572
    return ISAXAttributes_getQName(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)QName, &len);
573 574 575 576 577 578 579 580 581
}

static HRESULT WINAPI ivbsaxattributes_getIndexFromName(
        IVBSAXAttributes* iface,
        BSTR uri,
        BSTR localName,
        int *index)
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
582
    return ISAXAttributes_getIndexFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
583
            localName, SysStringLen(localName), index);
584 585 586 587 588 589 590 591
}

static HRESULT WINAPI ivbsaxattributes_getIndexFromQName(
        IVBSAXAttributes* iface,
        BSTR QName,
        int *index)
{
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
592
    return ISAXAttributes_getIndexFromQName(&This->ISAXAttributes_iface, QName,
593
            SysStringLen(QName), index);
594 595 596 597 598 599 600 601 602
}

static HRESULT WINAPI ivbsaxattributes_getType(
        IVBSAXAttributes* iface,
        int nIndex,
        BSTR *type)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
603
    return ISAXAttributes_getType(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)type, &len);
604 605 606 607 608 609 610 611 612 613
}

static HRESULT WINAPI ivbsaxattributes_getTypeFromName(
        IVBSAXAttributes* iface,
        BSTR uri,
        BSTR localName,
        BSTR *type)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
614
    return ISAXAttributes_getTypeFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
615
            localName, SysStringLen(localName), (const WCHAR**)type, &len);
616 617 618 619 620 621 622 623 624
}

static HRESULT WINAPI ivbsaxattributes_getTypeFromQName(
        IVBSAXAttributes* iface,
        BSTR QName,
        BSTR *type)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
625 626
    return ISAXAttributes_getTypeFromQName(&This->ISAXAttributes_iface, QName, SysStringLen(QName),
            (const WCHAR**)type, &len);
627 628 629 630 631 632 633 634 635
}

static HRESULT WINAPI ivbsaxattributes_getValue(
        IVBSAXAttributes* iface,
        int nIndex,
        BSTR *value)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
636
    return ISAXAttributes_getValue(&This->ISAXAttributes_iface, nIndex, (const WCHAR**)value, &len);
637 638 639 640 641 642 643 644 645 646
}

static HRESULT WINAPI ivbsaxattributes_getValueFromName(
        IVBSAXAttributes* iface,
        BSTR uri,
        BSTR localName,
        BSTR *value)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
647
    return ISAXAttributes_getValueFromName(&This->ISAXAttributes_iface, uri, SysStringLen(uri),
648
            localName, SysStringLen(localName), (const WCHAR**)value, &len);
649 650 651 652 653 654 655 656 657
}

static HRESULT WINAPI ivbsaxattributes_getValueFromQName(
        IVBSAXAttributes* iface,
        BSTR QName,
        BSTR *value)
{
    int len;
    saxattributes *This = impl_from_IVBSAXAttributes( iface );
658
    return ISAXAttributes_getValueFromQName(&This->ISAXAttributes_iface, QName,
659
            SysStringLen(QName), (const WCHAR**)value, &len);
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
}

static const struct IVBSAXAttributesVtbl ivbsaxattributes_vtbl =
{
    ivbsaxattributes_QueryInterface,
    ivbsaxattributes_AddRef,
    ivbsaxattributes_Release,
    ivbsaxattributes_GetTypeInfoCount,
    ivbsaxattributes_GetTypeInfo,
    ivbsaxattributes_GetIDsOfNames,
    ivbsaxattributes_Invoke,
    ivbsaxattributes_get_length,
    ivbsaxattributes_getURI,
    ivbsaxattributes_getLocalName,
    ivbsaxattributes_getQName,
    ivbsaxattributes_getIndexFromName,
    ivbsaxattributes_getIndexFromQName,
    ivbsaxattributes_getType,
    ivbsaxattributes_getTypeFromName,
    ivbsaxattributes_getTypeFromQName,
    ivbsaxattributes_getValue,
    ivbsaxattributes_getValueFromName,
    ivbsaxattributes_getValueFromQName
};

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
/*** ISAXAttributes interface ***/
/*** IUnknown methods ***/
static HRESULT WINAPI isaxattributes_QueryInterface(
        ISAXAttributes* iface,
        REFIID riid,
        void **ppvObject)
{
    saxattributes *This = impl_from_ISAXAttributes(iface);

    TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);

    *ppvObject = NULL;

    if (IsEqualGUID(riid, &IID_IUnknown) ||
            IsEqualGUID(riid, &IID_ISAXAttributes))
    {
        *ppvObject = iface;
    }
    else
    {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    ISAXAttributes_AddRef(iface);

    return S_OK;
}

static ULONG WINAPI isaxattributes_AddRef(ISAXAttributes* iface)
{
    saxattributes *This = impl_from_ISAXAttributes(iface);
    TRACE("%p\n", This);
    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI isaxattributes_Release(ISAXAttributes* iface)
{
    saxattributes *This = impl_from_ISAXAttributes(iface);
    LONG ref;

    TRACE("%p\n", This);

    ref = InterlockedDecrement(&This->ref);
    if (ref==0)
    {
731 732 733 734 735 736
        int index;
        for(index=0; index<This->nb_attributes; index++)
        {
            SysFreeString(This->szLocalname[index]);
            SysFreeString(This->szURI[index]);
            SysFreeString(This->szValue[index]);
737
            SysFreeString(This->szQName[index]);
738 739
        }

740 741 742 743
        heap_free(This->szLocalname);
        heap_free(This->szURI);
        heap_free(This->szValue);
        heap_free(This->szQName);
744

745
        heap_free(This);
746 747 748 749 750 751 752 753 754 755 756 757
    }

    return ref;
}

/*** ISAXAttributes methods ***/
static HRESULT WINAPI isaxattributes_getLength(
        ISAXAttributes* iface,
        int *length)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );

758 759 760
    *length = This->nb_attributes;
    TRACE("Length set to %d\n", *length);
    return S_OK;
761 762 763 764 765 766 767 768 769
}

static HRESULT WINAPI isaxattributes_getURI(
        ISAXAttributes* iface,
        int nIndex,
        const WCHAR **pUrl,
        int *pUriSize)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
770 771 772 773
    TRACE("(%p)->(%d)\n", This, nIndex);

    if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
    if(!pUrl || !pUriSize) return E_POINTER;
774

775 776 777 778
    *pUriSize = SysStringLen(This->szURI[nIndex]);
    *pUrl = This->szURI[nIndex];

    return S_OK;
779 780 781 782 783 784 785 786 787
}

static HRESULT WINAPI isaxattributes_getLocalName(
        ISAXAttributes* iface,
        int nIndex,
        const WCHAR **pLocalName,
        int *pLocalNameLength)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
788
    TRACE("(%p)->(%d)\n", This, nIndex);
789

790 791
    if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
    if(!pLocalName || !pLocalNameLength) return E_POINTER;
792 793 794 795 796

    *pLocalNameLength = SysStringLen(This->szLocalname[nIndex]);
    *pLocalName = This->szLocalname[nIndex];

    return S_OK;
797 798 799 800 801 802 803 804 805
}

static HRESULT WINAPI isaxattributes_getQName(
        ISAXAttributes* iface,
        int nIndex,
        const WCHAR **pQName,
        int *pQNameLength)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
806
    TRACE("(%p)->(%d)\n", This, nIndex);
807

808 809
    if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
    if(!pQName || !pQNameLength) return E_POINTER;
810 811 812 813 814

    *pQNameLength = SysStringLen(This->szQName[nIndex]);
    *pQName = This->szQName[nIndex];

    return S_OK;
815 816 817 818 819 820 821 822 823 824 825 826 827
}

static HRESULT WINAPI isaxattributes_getName(
        ISAXAttributes* iface,
        int nIndex,
        const WCHAR **pUri,
        int *pUriLength,
        const WCHAR **pLocalName,
        int *pLocalNameSize,
        const WCHAR **pQName,
        int *pQNameLength)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
828
    TRACE("(%p)->(%d)\n", This, nIndex);
829

830 831 832 833 834 835 836 837 838 839 840 841
    if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
    if(!pUri || !pUriLength || !pLocalName || !pLocalNameSize
            || !pQName || !pQNameLength) return E_POINTER;

    *pUriLength = SysStringLen(This->szURI[nIndex]);
    *pUri = This->szURI[nIndex];
    *pLocalNameSize = SysStringLen(This->szLocalname[nIndex]);
    *pLocalName = This->szLocalname[nIndex];
    *pQNameLength = SysStringLen(This->szQName[nIndex]);
    *pQName = This->szQName[nIndex];

    return S_OK;
842 843 844 845 846 847 848 849 850 851 852
}

static HRESULT WINAPI isaxattributes_getIndexFromName(
        ISAXAttributes* iface,
        const WCHAR *pUri,
        int cUriLength,
        const WCHAR *pLocalName,
        int cocalNameLength,
        int *index)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
853 854
    int i;
    TRACE("(%p)->(%s, %d, %s, %d)\n", This, debugstr_w(pUri), cUriLength,
855
            debugstr_w(pLocalName), cocalNameLength);
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

    if(!pUri || !pLocalName || !index) return E_POINTER;

    for(i=0; i<This->nb_attributes; i++)
    {
        if(cUriLength!=SysStringLen(This->szURI[i])
                || cocalNameLength!=SysStringLen(This->szLocalname[i]))
            continue;
        if(cUriLength && memcmp(pUri, This->szURI[i],
                    sizeof(WCHAR)*cUriLength))
            continue;
        if(cocalNameLength && memcmp(pLocalName, This->szLocalname[i],
                    sizeof(WCHAR)*cocalNameLength))
            continue;

        *index = i;
        return S_OK;
    }

    return E_INVALIDARG;
876 877 878 879 880 881 882 883 884
}

static HRESULT WINAPI isaxattributes_getIndexFromQName(
        ISAXAttributes* iface,
        const WCHAR *pQName,
        int nQNameLength,
        int *index)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
885 886
    int i;
    TRACE("(%p)->(%s, %d)\n", This, debugstr_w(pQName), nQNameLength);
887

888 889 890 891 892 893 894 895 896 897 898 899 900
    if(!pQName || !index) return E_POINTER;
    if(!nQNameLength) return E_INVALIDARG;

    for(i=0; i<This->nb_attributes; i++)
    {
        if(nQNameLength!=SysStringLen(This->szQName[i])) continue;
        if(memcmp(pQName, This->szQName, sizeof(WCHAR)*nQNameLength)) continue;

        *index = i;
        return S_OK;
    }

    return E_INVALIDARG;
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 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
}

static HRESULT WINAPI isaxattributes_getType(
        ISAXAttributes* iface,
        int nIndex,
        const WCHAR **pType,
        int *pTypeLength)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );

    FIXME("(%p)->(%d) stub\n", This, nIndex);
    return E_NOTIMPL;
}

static HRESULT WINAPI isaxattributes_getTypeFromName(
        ISAXAttributes* iface,
        const WCHAR *pUri,
        int nUri,
        const WCHAR *pLocalName,
        int nLocalName,
        const WCHAR **pType,
        int *nType)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );

    FIXME("(%p)->(%s, %d, %s, %d) stub\n", This, debugstr_w(pUri), nUri,
            debugstr_w(pLocalName), nLocalName);
    return E_NOTIMPL;
}

static HRESULT WINAPI isaxattributes_getTypeFromQName(
        ISAXAttributes* iface,
        const WCHAR *pQName,
        int nQName,
        const WCHAR **pType,
        int *nType)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );

    FIXME("(%p)->(%s, %d) stub\n", This, debugstr_w(pQName), nQName);
    return E_NOTIMPL;
}

static HRESULT WINAPI isaxattributes_getValue(
        ISAXAttributes* iface,
        int nIndex,
        const WCHAR **pValue,
        int *nValue)
{
    saxattributes *This = impl_from_ISAXAttributes( iface );
951
    TRACE("(%p)->(%d)\n", This, nIndex);
952

953 954
    if(nIndex>=This->nb_attributes || nIndex<0) return E_INVALIDARG;
    if(!pValue || !nValue) return E_POINTER;
955 956 957 958 959

    *nValue = SysStringLen(This->szValue[nIndex]);
    *pValue = This->szValue[nIndex];

    return S_OK;
960 961 962 963 964 965 966 967 968 969 970
}

static HRESULT WINAPI isaxattributes_getValueFromName(
        ISAXAttributes* iface,
        const WCHAR *pUri,
        int nUri,
        const WCHAR *pLocalName,
        int nLocalName,
        const WCHAR **pValue,
        int *nValue)
{
971 972
    HRESULT hr;
    int index;
973
    saxattributes *This = impl_from_ISAXAttributes( iface );
974
    TRACE("(%p)->(%s, %d, %s, %d)\n", This, debugstr_w(pUri), nUri,
975
            debugstr_w(pLocalName), nLocalName);
976 977 978 979 980 981

    hr = ISAXAttributes_getIndexFromName(iface,
            pUri, nUri, pLocalName, nLocalName, &index);
    if(hr==S_OK) hr = ISAXAttributes_getValue(iface, index, pValue, nValue);

    return hr;
982 983 984 985 986 987 988 989 990
}

static HRESULT WINAPI isaxattributes_getValueFromQName(
        ISAXAttributes* iface,
        const WCHAR *pQName,
        int nQName,
        const WCHAR **pValue,
        int *nValue)
{
991 992
    HRESULT hr;
    int index;
993
    saxattributes *This = impl_from_ISAXAttributes( iface );
994
    TRACE("(%p)->(%s, %d)\n", This, debugstr_w(pQName), nQName);
995

996 997 998 999
    hr = ISAXAttributes_getIndexFromQName(iface, pQName, nQName, &index);
    if(hr==S_OK) hr = ISAXAttributes_getValue(iface, index, pValue, nValue);

    return hr;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
}

static const struct ISAXAttributesVtbl isaxattributes_vtbl =
{
    isaxattributes_QueryInterface,
    isaxattributes_AddRef,
    isaxattributes_Release,
    isaxattributes_getLength,
    isaxattributes_getURI,
    isaxattributes_getLocalName,
    isaxattributes_getQName,
    isaxattributes_getName,
    isaxattributes_getIndexFromName,
    isaxattributes_getIndexFromQName,
    isaxattributes_getType,
    isaxattributes_getTypeFromName,
    isaxattributes_getTypeFromQName,
    isaxattributes_getValue,
    isaxattributes_getValueFromName,
    isaxattributes_getValueFromQName
};

1022
static HRESULT SAXAttributes_create(saxattributes **attr,
1023
        int nb_namespaces, const xmlChar **xmlNamespaces,
1024
        int nb_attributes, const xmlChar **xmlAttributes)
1025 1026
{
    saxattributes *attributes;
1027
    int index;
1028
    static const xmlChar xmlns[] = "xmlns";
1029

1030
    attributes = heap_alloc(sizeof(*attributes));
1031 1032 1033
    if(!attributes)
        return E_OUTOFMEMORY;

1034 1035
    attributes->IVBSAXAttributes_iface.lpVtbl = &ivbsaxattributes_vtbl;
    attributes->ISAXAttributes_iface.lpVtbl = &isaxattributes_vtbl;
1036 1037
    attributes->ref = 1;

1038
    attributes->nb_attributes = nb_namespaces+nb_attributes;
1039

1040 1041 1042 1043
    attributes->szLocalname = heap_alloc(sizeof(BSTR)*attributes->nb_attributes);
    attributes->szURI = heap_alloc(sizeof(BSTR)*attributes->nb_attributes);
    attributes->szValue = heap_alloc(sizeof(BSTR)*attributes->nb_attributes);
    attributes->szQName = heap_alloc(sizeof(BSTR)*attributes->nb_attributes);
1044

1045 1046
    if(!attributes->szLocalname || !attributes->szURI
            || !attributes->szValue || !attributes->szQName)
1047
    {
1048 1049 1050 1051 1052
        heap_free(attributes->szLocalname);
        heap_free(attributes->szURI);
        heap_free(attributes->szValue);
        heap_free(attributes->szQName);
        heap_free(attributes);
1053 1054 1055
        return E_FAIL;
    }

1056 1057 1058 1059 1060 1061 1062 1063
    for(index=0; index<nb_namespaces; index++)
    {
        attributes->szLocalname[index] = SysAllocStringLen(NULL, 0);
        attributes->szURI[index] = SysAllocStringLen(NULL, 0);
        attributes->szValue[index] = bstr_from_xmlChar(xmlNamespaces[2*index+1]);
        attributes->szQName[index] = QName_from_xmlChar(xmlns, xmlNamespaces[2*index]);
    }

1064 1065
    for(index=0; index<nb_attributes; index++)
    {
1066
        attributes->szLocalname[nb_namespaces+index] =
1067
            bstr_from_xmlChar(xmlAttributes[index*5]);
1068
        attributes->szURI[nb_namespaces+index] =
1069
            bstr_from_xmlChar(xmlAttributes[index*5+2]);
1070
        attributes->szValue[nb_namespaces+index] =
1071 1072
            bstr_from_xmlCharN(xmlAttributes[index*5+3],
                    xmlAttributes[index*5+4]-xmlAttributes[index*5+3]);
1073
        attributes->szQName[nb_namespaces+index] =
1074
            QName_from_xmlChar(xmlAttributes[index*5+1], xmlAttributes[index*5]);
1075 1076 1077 1078 1079
    }

    *attr = attributes;

    TRACE("returning %p\n", *attr);
1080 1081 1082 1083

    return S_OK;
}

1084 1085 1086 1087 1088 1089
/*** LibXML callbacks ***/
static void libxmlStartDocument(void *ctx)
{
    saxlocator *This = ctx;
    HRESULT hr;

1090
    if(has_content_handler(This))
1091
    {
1092 1093 1094 1095 1096
        if(This->vbInterface)
            hr = IVBSAXContentHandler_startDocument(This->saxreader->vbcontentHandler);
        else
            hr = ISAXContentHandler_startDocument(This->saxreader->contentHandler);

1097 1098
        if(hr != S_OK)
            format_error_message_from_id(This, hr);
1099
    }
1100

1101
    update_position(This, NULL);
1102 1103
}

1104 1105 1106 1107 1108
static void libxmlEndDocument(void *ctx)
{
    saxlocator *This = ctx;
    HRESULT hr;

1109 1110
    This->column = 0;
    This->line = 0;
1111

1112 1113
    if(This->ret != S_OK) return;

1114
    if(has_content_handler(This))
1115
    {
1116 1117 1118 1119 1120
        if(This->vbInterface)
            hr = IVBSAXContentHandler_endDocument(This->saxreader->vbcontentHandler);
        else
            hr = ISAXContentHandler_endDocument(This->saxreader->contentHandler);

1121 1122
        if(hr != S_OK)
            format_error_message_from_id(This, hr);
1123 1124 1125
    }
}

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
static void libxmlStartElementNS(
        void *ctx,
        const xmlChar *localname,
        const xmlChar *prefix,
        const xmlChar *URI,
        int nb_namespaces,
        const xmlChar **namespaces,
        int nb_attributes,
        int nb_defaulted,
        const xmlChar **attributes)
{
1137
    BSTR NamespaceUri, LocalName, QName, Prefix, Uri;
1138 1139
    saxlocator *This = ctx;
    HRESULT hr;
1140
    saxattributes *attr;
1141
    int index;
1142

1143 1144 1145 1146
    if(*(This->pParserCtxt->input->cur) == '/')
        update_position(This, (xmlChar*)This->pParserCtxt->input->cur+2);
    else
        update_position(This, (xmlChar*)This->pParserCtxt->input->cur+1);
1147

1148
    hr = namespacePush(This, nb_namespaces);
1149
    if(hr==S_OK && has_content_handler(This))
1150
    {
1151 1152
        for(index=0; index<nb_namespaces; index++)
        {
1153 1154
            Prefix = pooled_bstr_from_xmlChar(&This->saxreader->pool, namespaces[2*index]);
            Uri = pooled_bstr_from_xmlChar(&This->saxreader->pool, namespaces[2*index+1]);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172

            if(This->vbInterface)
                hr = IVBSAXContentHandler_startPrefixMapping(
                        This->saxreader->vbcontentHandler,
                        &Prefix, &Uri);
            else
                hr = ISAXContentHandler_startPrefixMapping(
                        This->saxreader->contentHandler,
                        Prefix, SysStringLen(Prefix),
                        Uri, SysStringLen(Uri));

            if(hr != S_OK)
            {
                format_error_message_from_id(This, hr);
                return;
            }
        }

1173 1174 1175
        NamespaceUri = pooled_bstr_from_xmlChar(&This->saxreader->pool, URI);
        LocalName = pooled_bstr_from_xmlChar(&This->saxreader->pool, localname);
        QName = pooled_QName_from_xmlChar(&This->saxreader->pool, prefix, localname);
1176

1177
        hr = SAXAttributes_create(&attr, nb_namespaces, namespaces, nb_attributes, attributes);
1178 1179
        if(hr == S_OK)
        {
1180
            if(This->vbInterface)
1181 1182
                hr = IVBSAXContentHandler_startElement(This->saxreader->vbcontentHandler,
                        &NamespaceUri, &LocalName, &QName, &attr->IVBSAXAttributes_iface);
1183
            else
1184 1185 1186
                hr = ISAXContentHandler_startElement(This->saxreader->contentHandler, NamespaceUri,
                        SysStringLen(NamespaceUri), LocalName, SysStringLen(LocalName), QName,
                        SysStringLen(QName), &attr->ISAXAttributes_iface);
1187

1188
            ISAXAttributes_Release(&attr->ISAXAttributes_iface);
1189
        }
1190
    }
1191 1192 1193

    if(hr != S_OK)
        format_error_message_from_id(This, hr);
1194 1195
}

1196 1197 1198 1199 1200 1201
static void libxmlEndElementNS(
        void *ctx,
        const xmlChar *localname,
        const xmlChar *prefix,
        const xmlChar *URI)
{
1202
    BSTR NamespaceUri, LocalName, QName, Prefix;
1203 1204
    saxlocator *This = ctx;
    HRESULT hr;
1205
    xmlChar *end;
1206
    int nsNr, index;
1207

1208 1209
    end = (xmlChar*)This->pParserCtxt->input->cur;
    if(*(end-1) != '>' || *(end-2) != '/')
1210 1211
        while(end-2>=This->pParserCtxt->input->base
                && *(end-2)!='<' && *(end-1)!='/') end--;
1212 1213

    update_position(This, end);
1214

1215 1216
    nsNr = namespacePop(This);

1217
    if(has_content_handler(This))
1218
    {
1219 1220 1221
        NamespaceUri = pooled_bstr_from_xmlChar(&This->saxreader->pool, URI);
        LocalName = pooled_bstr_from_xmlChar(&This->saxreader->pool, localname);
        QName = pooled_QName_from_xmlChar(&This->saxreader->pool, prefix, localname);
1222

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
        if(This->vbInterface)
            hr = IVBSAXContentHandler_endElement(
                    This->saxreader->vbcontentHandler,
                    &NamespaceUri, &LocalName, &QName);
        else
            hr = ISAXContentHandler_endElement(
                    This->saxreader->contentHandler,
                    NamespaceUri, SysStringLen(NamespaceUri),
                    LocalName, SysStringLen(LocalName),
                    QName, SysStringLen(QName));
1233

1234
        if(hr != S_OK)
1235
        {
1236
            format_error_message_from_id(This, hr);
1237 1238 1239 1240 1241 1242
            return;
        }

        for(index=This->pParserCtxt->nsNr-2;
                index>=This->pParserCtxt->nsNr-nsNr*2; index-=2)
        {
1243
            Prefix = pooled_bstr_from_xmlChar(&This->saxreader->pool, This->pParserCtxt->nsTab[index]);
1244 1245 1246 1247 1248 1249 1250 1251 1252

            if(This->vbInterface)
                hr = IVBSAXContentHandler_endPrefixMapping(
                        This->saxreader->vbcontentHandler, &Prefix);
            else
                hr = ISAXContentHandler_endPrefixMapping(
                        This->saxreader->contentHandler,
                        Prefix, SysStringLen(Prefix));

1253 1254 1255 1256 1257 1258
            if(hr != S_OK)
            {
                format_error_message_from_id(This, hr);
                return;
            }

1259
        }
1260
    }
1261 1262

    update_position(This, NULL);
1263 1264
}

1265 1266 1267 1268 1269 1270
static void libxmlCharacters(
        void *ctx,
        const xmlChar *ch,
        int len)
{
    saxlocator *This = ctx;
1271
    BSTR Chars;
1272
    HRESULT hr;
1273
    xmlChar *cur;
1274
    xmlChar *end;
1275
    BOOL lastEvent = FALSE;
1276

1277
    if(!(has_content_handler(This))) return;
1278

1279 1280 1281
    cur = (xmlChar*)ch;
    if(*(ch-1)=='\r') cur--;
    end = cur;
1282

1283 1284
    if(ch<This->pParserCtxt->input->base || ch>This->pParserCtxt->input->end)
        This->column++;
1285

1286
    while(1)
1287
    {
1288 1289
        while(end-ch<len && *end!='\r') end++;
        if(end-ch==len)
1290
        {
1291 1292 1293
            end--;
            lastEvent = TRUE;
        }
1294

1295
        if(!lastEvent) *end = '\n';
1296

1297
        Chars = pooled_bstr_from_xmlCharN(&This->saxreader->pool, cur, end-cur+1);
1298 1299 1300 1301 1302 1303 1304
        if(This->vbInterface)
            hr = IVBSAXContentHandler_characters(
                    This->saxreader->vbcontentHandler, &Chars);
        else
            hr = ISAXContentHandler_characters(
                    This->saxreader->contentHandler,
                    Chars, SysStringLen(Chars));
1305

1306 1307 1308 1309 1310 1311
        if(hr != S_OK)
        {
            format_error_message_from_id(This, hr);
            return;
        }

1312
        This->column += end-cur+1;
1313

1314 1315
        if(lastEvent)
            break;
1316

1317 1318 1319 1320 1321 1322
        *end = '\r';
        end++;
        if(*end == '\n')
        {
            end++;
            This->column++;
1323
        }
1324
        cur = end;
1325

1326
        if(end-ch == len) break;
1327
    }
1328 1329 1330 1331

    if(ch<This->pParserCtxt->input->base || ch>This->pParserCtxt->input->end)
        This->column = This->realColumn
            +This->pParserCtxt->input->cur-This->lastCur;
1332 1333
}

1334 1335 1336 1337 1338
static void libxmlSetDocumentLocator(
        void *ctx,
        xmlSAXLocatorPtr loc)
{
    saxlocator *This = ctx;
1339
    HRESULT hr = S_OK;
1340

1341 1342 1343
    if(has_content_handler(This))
    {
        if(This->vbInterface)
1344 1345
            hr = IVBSAXContentHandler_putref_documentLocator(This->saxreader->vbcontentHandler,
                    &This->IVBSAXLocator_iface);
1346
        else
1347 1348
            hr = ISAXContentHandler_putDocumentLocator(This->saxreader->contentHandler,
                    &This->ISAXLocator_iface);
1349
    }
1350 1351

    if(FAILED(hr))
1352 1353 1354
        format_error_message_from_id(This, hr);
}

1355 1356 1357 1358 1359 1360 1361
static void libxmlComment(void *ctx, const xmlChar *value)
{
    saxlocator *This = ctx;
    BSTR bValue;
    HRESULT hr;
    xmlChar *beg = (xmlChar*)This->pParserCtxt->input->cur;

1362 1363
    while(beg-4>=This->pParserCtxt->input->base
            && memcmp(beg-4, "<!--", sizeof(char[4]))) beg--;
1364 1365 1366 1367 1368
    update_position(This, beg);

    if(!This->vbInterface && !This->saxreader->lexicalHandler) return;
    if(This->vbInterface && !This->saxreader->vblexicalHandler) return;

1369
    bValue = pooled_bstr_from_xmlChar(&This->saxreader->pool, value);
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

    if(This->vbInterface)
        hr = IVBSAXLexicalHandler_comment(
                This->saxreader->vblexicalHandler, &bValue);
    else
        hr = ISAXLexicalHandler_comment(
                This->saxreader->lexicalHandler,
                bValue, SysStringLen(bValue));

    if(FAILED(hr))
        format_error_message_from_id(This, hr);

    update_position(This, NULL);
}

1385
static void libxmlFatalError(void *ctx, const char *msg, ...)
1386 1387 1388
{
    saxlocator *This = ctx;
    char message[1024];
1389
    WCHAR *error;
1390 1391 1392 1393 1394 1395 1396
    DWORD len;
    va_list args;

    va_start(args, msg);
    vsprintf(message, msg, args);
    va_end(args);

1397
    len = MultiByteToWideChar(CP_UNIXCP, 0, message, -1, NULL, 0);
1398 1399 1400 1401 1402 1403
    error = heap_alloc(sizeof(WCHAR)*len);
    if(error)
    {
        MultiByteToWideChar(CP_UNIXCP, 0, message, -1, error, len);
        TRACE("fatal error for %p: %s\n", This, debugstr_w(error));
    }
1404

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    if(!has_error_handler(This))
    {
        xmlStopParser(This->pParserCtxt);
        This->ret = E_FAIL;
        heap_free(error);
        return;
    }

    FIXME("Error handling is not compatible.\n");

1415 1416
    if(This->vbInterface)
    {
1417
        BSTR bstrError = SysAllocString(error);
1418 1419
        IVBSAXErrorHandler_fatalError(This->saxreader->vberrorHandler, &This->IVBSAXLocator_iface,
                &bstrError, E_FAIL);
1420
        SysFreeString(bstrError);
1421 1422
    }
    else
1423 1424
        ISAXErrorHandler_fatalError(This->saxreader->errorHandler, &This->ISAXLocator_iface,
                error, E_FAIL);
1425

1426
    heap_free(error);
1427 1428 1429

    xmlStopParser(This->pParserCtxt);
    This->ret = E_FAIL;
1430 1431
}

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
static void libxmlCDataBlock(void *ctx, const xmlChar *value, int len)
{
    saxlocator *This = ctx;
    HRESULT hr = S_OK;
    xmlChar *beg = (xmlChar*)This->pParserCtxt->input->cur-len;
    xmlChar *cur, *end;
    int realLen;
    BSTR Chars;
    BOOL lastEvent = FALSE, change;

1442 1443
    while(beg-9>=This->pParserCtxt->input->base
            && memcmp(beg-9, "<![CDATA[", sizeof(char[9]))) beg--;
1444 1445 1446 1447 1448 1449 1450 1451
    update_position(This, beg);

    if(This->vbInterface && This->saxreader->vblexicalHandler)
        hr = IVBSAXLexicalHandler_startCDATA(This->saxreader->vblexicalHandler);
    if(!This->vbInterface && This->saxreader->lexicalHandler)
        hr = ISAXLexicalHandler_startCDATA(This->saxreader->lexicalHandler);

    if(FAILED(hr))
1452 1453 1454 1455
    {
        format_error_message_from_id(This, hr);
        return;
    }
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476

    realLen = This->pParserCtxt->input->cur-beg-3;
    cur = beg;
    end = beg;

    while(1)
    {
        while(end-beg<realLen && *end!='\r') end++;
        if(end-beg==realLen)
        {
            end--;
            lastEvent = TRUE;
        }
        else if(end-beg==realLen-1 && *end=='\r' && *(end+1)=='\n')
            lastEvent = TRUE;

        if(*end == '\r') change = TRUE;
        else change = FALSE;

        if(change) *end = '\n';

1477
        if(has_content_handler(This))
1478
        {
1479
            Chars = pooled_bstr_from_xmlCharN(&This->saxreader->pool, cur, end-cur+1);
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
            if(This->vbInterface)
                hr = IVBSAXContentHandler_characters(
                        This->saxreader->vbcontentHandler, &Chars);
            else
                hr = ISAXContentHandler_characters(
                        This->saxreader->contentHandler,
                        Chars, SysStringLen(Chars));
        }

        if(change) *end = '\r';

        if(lastEvent)
            break;

        This->column += end-cur+2;
        end += 2;
        cur = end;
    }

    if(This->vbInterface && This->saxreader->vblexicalHandler)
        hr = IVBSAXLexicalHandler_endCDATA(This->saxreader->vblexicalHandler);
    if(!This->vbInterface && This->saxreader->lexicalHandler)
        hr = ISAXLexicalHandler_endCDATA(This->saxreader->lexicalHandler);

    if(FAILED(hr))
        format_error_message_from_id(This, hr);

    This->column += 4+end-cur;
}

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
/*** IVBSAXLocator interface ***/
/*** IUnknown methods ***/
static HRESULT WINAPI ivbsaxlocator_QueryInterface(IVBSAXLocator* iface, REFIID riid, void **ppvObject)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );

    TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject);

    *ppvObject = NULL;

    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
            IsEqualGUID( riid, &IID_IDispatch) ||
            IsEqualGUID( riid, &IID_IVBSAXLocator ))
    {
        *ppvObject = iface;
    }
    else
    {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    IVBSAXLocator_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI ivbsaxlocator_AddRef(IVBSAXLocator* iface)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
    TRACE("%p\n", This );
    return InterlockedIncrement( &This->ref );
}

static ULONG WINAPI ivbsaxlocator_Release(
        IVBSAXLocator* iface)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
1548
    return ISAXLocator_Release((ISAXLocator*)&This->IVBSAXLocator_iface);
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
}

/*** IDispatch methods ***/
static HRESULT WINAPI ivbsaxlocator_GetTypeInfoCount( IVBSAXLocator *iface, UINT* pctinfo )
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;

    return S_OK;
}

static HRESULT WINAPI ivbsaxlocator_GetTypeInfo(
    IVBSAXLocator *iface,
    UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
    HRESULT hr;

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(IVBSAXLocator_tid, ppTInfo);

    return hr;
}

static HRESULT WINAPI ivbsaxlocator_GetIDsOfNames(
    IVBSAXLocator *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
          lcid, rgDispId);

    if(!rgszNames || cNames == 0 || !rgDispId)
        return E_INVALIDARG;

    hr = get_typeinfo(IVBSAXLocator_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ivbsaxlocator_Invoke(
    IVBSAXLocator *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IVBSAXLocator_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
1626 1627
        hr = ITypeInfo_Invoke(typeinfo, &This->IVBSAXLocator_iface, dispIdMember, wFlags,
                pDispParams, pVarResult, pExcepInfo, puArgErr);
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

/*** IVBSAXLocator methods ***/
static HRESULT WINAPI ivbsaxlocator_get_columnNumber(
        IVBSAXLocator* iface,
        int *pnColumn)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
1640
    return ISAXLocator_getColumnNumber((ISAXLocator*)&This->IVBSAXLocator_iface, pnColumn);
1641 1642 1643 1644 1645 1646 1647
}

static HRESULT WINAPI ivbsaxlocator_get_lineNumber(
        IVBSAXLocator* iface,
        int *pnLine)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
1648
    return ISAXLocator_getLineNumber((ISAXLocator*)&This->IVBSAXLocator_iface, pnLine);
1649 1650 1651 1652 1653 1654 1655
}

static HRESULT WINAPI ivbsaxlocator_get_publicId(
        IVBSAXLocator* iface,
        BSTR* publicId)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
1656
    return ISAXLocator_getPublicId((ISAXLocator*)&This->IVBSAXLocator_iface,
1657 1658 1659 1660 1661 1662 1663 1664
            (const WCHAR**)publicId);
}

static HRESULT WINAPI ivbsaxlocator_get_systemId(
        IVBSAXLocator* iface,
        BSTR* systemId)
{
    saxlocator *This = impl_from_IVBSAXLocator( iface );
1665
    return ISAXLocator_getSystemId((ISAXLocator*)&This->IVBSAXLocator_iface,
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
            (const WCHAR**)systemId);
}

static const struct IVBSAXLocatorVtbl ivbsaxlocator_vtbl =
{
    ivbsaxlocator_QueryInterface,
    ivbsaxlocator_AddRef,
    ivbsaxlocator_Release,
    ivbsaxlocator_GetTypeInfoCount,
    ivbsaxlocator_GetTypeInfo,
    ivbsaxlocator_GetIDsOfNames,
    ivbsaxlocator_Invoke,
    ivbsaxlocator_get_columnNumber,
    ivbsaxlocator_get_lineNumber,
    ivbsaxlocator_get_publicId,
    ivbsaxlocator_get_systemId
};

1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
/*** ISAXLocator interface ***/
/*** IUnknown methods ***/
static HRESULT WINAPI isaxlocator_QueryInterface(ISAXLocator* iface, REFIID riid, void **ppvObject)
{
    saxlocator *This = impl_from_ISAXLocator( iface );

    TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );

    *ppvObject = NULL;

    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
            IsEqualGUID( riid, &IID_ISAXLocator ))
    {
        *ppvObject = iface;
    }
    else
    {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    ISAXLocator_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI isaxlocator_AddRef(ISAXLocator* iface)
{
    saxlocator *This = impl_from_ISAXLocator( iface );
    TRACE("%p\n", This );
    return InterlockedIncrement( &This->ref );
}

static ULONG WINAPI isaxlocator_Release(
        ISAXLocator* iface)
{
    saxlocator *This = impl_from_ISAXLocator( iface );
    LONG ref;

    TRACE("%p\n", This );

    ref = InterlockedDecrement( &This->ref );
    if ( ref == 0 )
    {
1728 1729
        SysFreeString(This->publicId);
        SysFreeString(This->systemId);
1730
        heap_free(This->nsStack);
1731

1732
        ISAXXMLReader_Release(&This->saxreader->ISAXXMLReader_iface);
1733
        heap_free( This );
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
    }

    return ref;
}

/*** ISAXLocator methods ***/
static HRESULT WINAPI isaxlocator_getColumnNumber(
        ISAXLocator* iface,
        int *pnColumn)
{
    saxlocator *This = impl_from_ISAXLocator( iface );

1746
    *pnColumn = This->column;
1747
    return S_OK;
1748 1749 1750 1751 1752 1753 1754 1755
}

static HRESULT WINAPI isaxlocator_getLineNumber(
        ISAXLocator* iface,
        int *pnLine)
{
    saxlocator *This = impl_from_ISAXLocator( iface );

1756
    *pnLine = This->line;
1757
    return S_OK;
1758 1759 1760 1761 1762 1763
}

static HRESULT WINAPI isaxlocator_getPublicId(
        ISAXLocator* iface,
        const WCHAR ** ppwchPublicId)
{
1764
    BSTR publicId;
1765 1766
    saxlocator *This = impl_from_ISAXLocator( iface );

1767
    SysFreeString(This->publicId);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779

    publicId = bstr_from_xmlChar(xmlSAX2GetPublicId(This->pParserCtxt));
    if(SysStringLen(publicId))
        This->publicId = (WCHAR*)&publicId;
    else
    {
        SysFreeString(publicId);
        This->publicId = NULL;
    }

    *ppwchPublicId = This->publicId;
    return S_OK;
1780 1781 1782 1783 1784 1785
}

static HRESULT WINAPI isaxlocator_getSystemId(
        ISAXLocator* iface,
        const WCHAR ** ppwchSystemId)
{
1786
    BSTR systemId;
1787 1788
    saxlocator *This = impl_from_ISAXLocator( iface );

1789
    SysFreeString(This->systemId);
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801

    systemId = bstr_from_xmlChar(xmlSAX2GetSystemId(This->pParserCtxt));
    if(SysStringLen(systemId))
        This->systemId = (WCHAR*)&systemId;
    else
    {
        SysFreeString(systemId);
        This->systemId = NULL;
    }

    *ppwchSystemId = This->systemId;
    return S_OK;
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
}

static const struct ISAXLocatorVtbl isaxlocator_vtbl =
{
    isaxlocator_QueryInterface,
    isaxlocator_AddRef,
    isaxlocator_Release,
    isaxlocator_getColumnNumber,
    isaxlocator_getLineNumber,
    isaxlocator_getPublicId,
    isaxlocator_getSystemId
};

1815
static HRESULT SAXLocator_create(saxreader *reader, saxlocator **ppsaxlocator, BOOL vbInterface)
1816 1817 1818
{
    saxlocator *locator;

1819
    locator = heap_alloc( sizeof (*locator) );
1820 1821 1822
    if( !locator )
        return E_OUTOFMEMORY;

1823 1824
    locator->IVBSAXLocator_iface.lpVtbl = &ivbsaxlocator_vtbl;
    locator->ISAXLocator_iface.lpVtbl = &isaxlocator_vtbl;
1825
    locator->ref = 1;
1826
    locator->vbInterface = vbInterface;
1827 1828

    locator->saxreader = reader;
1829
    ISAXXMLReader_AddRef(&reader->ISAXXMLReader_iface);
1830

1831
    locator->pParserCtxt = NULL;
1832
    locator->publicId = NULL;
1833
    locator->systemId = NULL;
1834 1835 1836
    locator->lastCur = NULL;
    locator->line = 0;
    locator->column = 0;
1837
    locator->ret = S_OK;
1838 1839
    locator->nsStackSize = 8;
    locator->nsStackLast = 0;
1840
    locator->nsStack = heap_alloc(sizeof(int)*locator->nsStackSize);
1841 1842
    if(!locator->nsStack)
    {
1843
        ISAXXMLReader_Release(&reader->ISAXXMLReader_iface);
1844
        heap_free(locator);
1845 1846
        return E_OUTOFMEMORY;
    }
1847

1848 1849 1850 1851 1852 1853 1854
    *ppsaxlocator = locator;

    TRACE("returning %p\n", *ppsaxlocator);

    return S_OK;
}

1855 1856 1857
/*** SAXXMLReader internal functions ***/
static HRESULT internal_parseBuffer(saxreader *This, const char *buffer, int size, BOOL vbInterface)
{
1858 1859
    xmlCharEncoding encoding = XML_CHAR_ENCODING_NONE;
    xmlChar *enc_name = NULL;
1860 1861 1862 1863 1864
    saxlocator *locator;
    HRESULT hr;

    hr = SAXLocator_create(This, &locator, vbInterface);
    if(FAILED(hr))
1865
        return hr;
1866

1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
    if (size >= 4)
    {
        const unsigned char *buff = (unsigned char*)buffer;

        encoding = xmlDetectCharEncoding((xmlChar*)buffer, 4);
        enc_name = (xmlChar*)xmlGetCharEncodingName(encoding);
        TRACE("detected encoding: %s\n", enc_name);
        /* skip BOM, parser won't switch encodings and so won't skip it on its own */
        if ((encoding == XML_CHAR_ENCODING_UTF8) &&
            buff[0] == 0xEF && buff[1] == 0xBB && buff[2] == 0xBF)
        {
            buffer += 3;
            size -= 3;
        }
    }

1883 1884 1885
    locator->pParserCtxt = xmlCreateMemoryParserCtxt(buffer, size);
    if(!locator->pParserCtxt)
    {
1886
        ISAXLocator_Release(&locator->ISAXLocator_iface);
1887 1888 1889
        return E_FAIL;
    }

1890 1891 1892
    if (encoding == XML_CHAR_ENCODING_UTF8)
        locator->pParserCtxt->encoding = xmlStrdup(enc_name);

1893
    xmlFree(locator->pParserCtxt->sax);
1894 1895 1896
    locator->pParserCtxt->sax = &locator->saxreader->sax;
    locator->pParserCtxt->userData = locator;

1897
    This->isParsing = TRUE;
1898
    if(xmlParseDocument(locator->pParserCtxt) == -1) hr = E_FAIL;
1899
    else hr = locator->ret;
1900
    This->isParsing = FALSE;
1901 1902 1903 1904 1905 1906 1907 1908

    if(locator->pParserCtxt)
    {
        locator->pParserCtxt->sax = NULL;
        xmlFreeParserCtxt(locator->pParserCtxt);
        locator->pParserCtxt = NULL;
    }

1909
    ISAXLocator_Release(&locator->ISAXLocator_iface);
1910
    return hr;
1911 1912
}

1913 1914 1915 1916 1917 1918
static HRESULT internal_parseStream(saxreader *This, IStream *stream, BOOL vbInterface)
{
    saxlocator *locator;
    HRESULT hr;
    ULONG dataRead;
    char data[1024];
1919
    int ret;
1920

1921
    dataRead = 0;
1922
    hr = IStream_Read(stream, data, sizeof(data), &dataRead);
1923
    if(FAILED(hr)) return hr;
1924 1925

    hr = SAXLocator_create(This, &locator, vbInterface);
1926
    if(FAILED(hr)) return hr;
1927 1928 1929 1930 1931 1932

    locator->pParserCtxt = xmlCreatePushParserCtxt(
            &locator->saxreader->sax, locator,
            data, dataRead, NULL);
    if(!locator->pParserCtxt)
    {
1933
        ISAXLocator_Release(&locator->ISAXLocator_iface);
1934 1935 1936
        return E_FAIL;
    }

1937
    This->isParsing = TRUE;
1938

1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
    if(dataRead != sizeof(data))
    {
        ret = xmlParseChunk(locator->pParserCtxt, data, 0, 1);
        hr = ret != XML_ERR_OK ? E_FAIL : locator->ret;
    }
    else
    {
        while(1)
        {
            dataRead = 0;
            hr = IStream_Read(stream, data, sizeof(data), &dataRead);
            if (FAILED(hr)) break;
1951

1952 1953
            ret = xmlParseChunk(locator->pParserCtxt, data, dataRead, 0);
            hr = ret != XML_ERR_OK ? E_FAIL : locator->ret;
1954

1955
            if (hr != S_OK) break;
1956

1957 1958 1959 1960 1961 1962
            if (dataRead != sizeof(data))
            {
                ret = xmlParseChunk(locator->pParserCtxt, data, 0, 1);
                hr = ret != XML_ERR_OK ? E_FAIL : locator->ret;
                break;
            }
1963 1964
        }
    }
1965

1966
    This->isParsing = FALSE;
1967 1968 1969

    xmlFreeParserCtxt(locator->pParserCtxt);
    locator->pParserCtxt = NULL;
1970
    ISAXLocator_Release(&locator->ISAXLocator_iface);
1971 1972 1973
    return hr;
}

1974
static HRESULT internal_getEntityResolver(
1975 1976 1977 1978 1979 1980 1981 1982
        saxreader *This,
        void *pEntityResolver,
        BOOL vbInterface)
{
    FIXME("(%p)->(%p) stub\n", This, pEntityResolver);
    return E_NOTIMPL;
}

1983
static HRESULT internal_putEntityResolver(
1984 1985 1986 1987 1988 1989 1990 1991
        saxreader *This,
        void *pEntityResolver,
        BOOL vbInterface)
{
    FIXME("(%p)->(%p) stub\n", This, pEntityResolver);
    return E_NOTIMPL;
}

1992
static HRESULT internal_getContentHandler(
1993 1994 1995 1996 1997 1998 1999
        saxreader* This,
        void *pContentHandler,
        BOOL vbInterface)
{
    TRACE("(%p)->(%p)\n", This, pContentHandler);
    if(pContentHandler == NULL)
        return E_POINTER;
2000 2001
    if((vbInterface && This->vbcontentHandler)
            || (!vbInterface && This->contentHandler))
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
    {
        if(vbInterface)
            IVBSAXContentHandler_AddRef(This->vbcontentHandler);
        else
            ISAXContentHandler_AddRef(This->contentHandler);
    }
    if(vbInterface) *(IVBSAXContentHandler**)pContentHandler =
        This->vbcontentHandler;
    else *(ISAXContentHandler**)pContentHandler = This->contentHandler;

    return S_OK;
}

2015
static HRESULT internal_putContentHandler(
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
        saxreader* This,
        void *contentHandler,
        BOOL vbInterface)
{
    TRACE("(%p)->(%p)\n", This, contentHandler);
    if(contentHandler)
    {
        if(vbInterface)
            IVBSAXContentHandler_AddRef((IVBSAXContentHandler*)contentHandler);
        else
            ISAXContentHandler_AddRef((ISAXContentHandler*)contentHandler);
    }
2028 2029
    if((vbInterface && This->vbcontentHandler)
            || (!vbInterface && This->contentHandler))
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
    {
        if(vbInterface)
            IVBSAXContentHandler_Release(This->vbcontentHandler);
        else
            ISAXContentHandler_Release(This->contentHandler);
    }
    if(vbInterface)
        This->vbcontentHandler = contentHandler;
    else
        This->contentHandler = contentHandler;

    return S_OK;
}

2044
static HRESULT internal_getDTDHandler(
2045 2046 2047 2048 2049 2050 2051 2052
        saxreader* This,
        void *pDTDHandler,
        BOOL vbInterface)
{
    FIXME("(%p)->(%p) stub\n", This, pDTDHandler);
    return E_NOTIMPL;
}

2053
static HRESULT internal_putDTDHandler(
2054 2055 2056 2057 2058 2059 2060 2061
        saxreader* This,
        void *pDTDHandler,
        BOOL vbInterface)
{
    FIXME("(%p)->(%p) stub\n", This, pDTDHandler);
    return E_NOTIMPL;
}

2062
static HRESULT internal_getErrorHandler(
2063 2064 2065 2066 2067 2068 2069
        saxreader* This,
        void *pErrorHandler,
        BOOL vbInterface)
{
    TRACE("(%p)->(%p)\n", This, pErrorHandler);
    if(pErrorHandler == NULL)
        return E_POINTER;
2070 2071 2072 2073 2074 2075

    if(vbInterface && This->vberrorHandler)
        IVBSAXErrorHandler_AddRef(This->vberrorHandler);
    else if(!vbInterface && This->errorHandler)
        ISAXErrorHandler_AddRef(This->errorHandler);

2076 2077 2078 2079 2080 2081 2082 2083 2084
    if(vbInterface)
        *(IVBSAXErrorHandler**)pErrorHandler = This->vberrorHandler;
    else
        *(ISAXErrorHandler**)pErrorHandler = This->errorHandler;

    return S_OK;

}

2085
static HRESULT internal_putErrorHandler(
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
        saxreader* This,
        void *errorHandler,
        BOOL vbInterface)
{
    TRACE("(%p)->(%p)\n", This, errorHandler);
    if(errorHandler)
    {
        if(vbInterface)
            IVBSAXErrorHandler_AddRef((IVBSAXErrorHandler*)errorHandler);
        else
            ISAXErrorHandler_AddRef((ISAXErrorHandler*)errorHandler);
    }
2098 2099 2100 2101 2102 2103

    if(vbInterface && This->vberrorHandler)
        IVBSAXErrorHandler_Release(This->vberrorHandler);
    else if(!vbInterface && This->errorHandler)
        ISAXErrorHandler_Release(This->errorHandler);

2104 2105 2106 2107 2108 2109 2110 2111 2112
    if(vbInterface)
        This->vberrorHandler = errorHandler;
    else
        This->errorHandler = errorHandler;

    return S_OK;

}

2113
static HRESULT internal_parse(
2114 2115 2116 2117 2118 2119
        saxreader* This,
        VARIANT varInput,
        BOOL vbInterface)
{
    HRESULT hr;

2120
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&varInput));
2121

2122 2123 2124
    /* Dispose of the BSTRs in the pool from a prior run, if any. */
    free_bstr_pool(&This->pool);

2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
    switch(V_VT(&varInput))
    {
        case VT_BSTR:
            hr = internal_parseBuffer(This, (const char*)V_BSTR(&varInput),
                    SysStringByteLen(V_BSTR(&varInput)), vbInterface);
            break;
        case VT_ARRAY|VT_UI1: {
            void *pSAData;
            LONG lBound, uBound;
            ULONG dataRead;

            hr = SafeArrayGetLBound(V_ARRAY(&varInput), 1, &lBound);
            if(hr != S_OK) break;
            hr = SafeArrayGetUBound(V_ARRAY(&varInput), 1, &uBound);
            if(hr != S_OK) break;
            dataRead = (uBound-lBound)*SafeArrayGetElemsize(V_ARRAY(&varInput));
2141
            hr = SafeArrayAccessData(V_ARRAY(&varInput), &pSAData);
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
            if(hr != S_OK) break;
            hr = internal_parseBuffer(This, pSAData, dataRead, vbInterface);
            SafeArrayUnaccessData(V_ARRAY(&varInput));
            break;
        }
        case VT_UNKNOWN:
        case VT_DISPATCH: {
            IPersistStream *persistStream;
            IStream *stream = NULL;
            IXMLDOMDocument *xmlDoc;

2153 2154 2155 2156 2157 2158 2159 2160 2161
            if(IUnknown_QueryInterface(V_UNKNOWN(&varInput),
                        &IID_IXMLDOMDocument, (void**)&xmlDoc) == S_OK)
            {
                BSTR bstrData;

                IXMLDOMDocument_get_xml(xmlDoc, &bstrData);
                hr = internal_parseBuffer(This, (const char*)bstrData,
                        SysStringByteLen(bstrData), vbInterface);
                IXMLDOMDocument_Release(xmlDoc);
2162
                SysFreeString(bstrData);
2163 2164
                break;
            }
2165

2166 2167 2168
            if(IUnknown_QueryInterface(V_UNKNOWN(&varInput),
                        &IID_IPersistStream, (void**)&persistStream) == S_OK)
            {
2169 2170 2171 2172 2173 2174 2175
                hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
                if(hr != S_OK)
                {
                    IPersistStream_Release(persistStream);
                    return hr;
                }

2176 2177
                hr = IPersistStream_Save(persistStream, stream, TRUE);
                IPersistStream_Release(persistStream);
2178 2179 2180 2181 2182
                if(hr != S_OK)
                {
                    IStream_Release(stream);
                    break;
                }
2183 2184 2185 2186
            }
            if(stream || IUnknown_QueryInterface(V_UNKNOWN(&varInput),
                        &IID_IStream, (void**)&stream) == S_OK)
            {
2187
                hr = internal_parseStream(This, stream, vbInterface);
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
                IStream_Release(stream);
                break;
            }
        }
        default:
            WARN("vt %d not implemented\n", V_VT(&varInput));
            hr = E_INVALIDARG;
    }

    return hr;
}

static HRESULT internal_vbonDataAvailable(void *obj, char *ptr, DWORD len)
{
    saxreader *This = obj;

    return internal_parseBuffer(This, ptr, len, TRUE);
}

static HRESULT internal_onDataAvailable(void *obj, char *ptr, DWORD len)
{
    saxreader *This = obj;

    return internal_parseBuffer(This, ptr, len, FALSE);
}

2214
static HRESULT internal_parseURL(
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
        saxreader* This,
        const WCHAR *url,
        BOOL vbInterface)
{
    bsc_t *bsc;
    HRESULT hr;

    TRACE("(%p)->(%s)\n", This, debugstr_w(url));

    if(vbInterface) hr = bind_url(url, internal_vbonDataAvailable, This, &bsc);
    else hr = bind_url(url, internal_onDataAvailable, This, &bsc);

    if(FAILED(hr))
        return hr;

    detach_bsc(bsc);

    return S_OK;
}

2235
static HRESULT internal_putProperty(
2236
    saxreader* This,
2237
    const WCHAR *prop,
2238 2239 2240
    VARIANT value,
    BOOL vbInterface)
{
2241 2242 2243
    TRACE("(%p)->(%s %s)\n", This, debugstr_w(prop), debugstr_variant(&value));

    if(!memcmp(prop, PropertyDeclHandlerW, sizeof(PropertyDeclHandlerW)))
2244 2245 2246
    {
        if(This->isParsing) return E_FAIL;

2247
        switch (V_VT(&value))
2248
        {
2249 2250 2251 2252 2253 2254 2255 2256 2257
        case VT_EMPTY:
            if (vbInterface)
            {
                if (This->vbdeclHandler)
                {
                    IVBSAXDeclHandler_Release(This->vbdeclHandler);
                    This->vbdeclHandler = NULL;
                }
            }
2258
            else
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
                if (This->declHandler)
                {
                    ISAXDeclHandler_Release(This->declHandler);
                    This->declHandler = NULL;
                }
            break;
        case VT_UNKNOWN:
            if (V_UNKNOWN(&value)) IUnknown_AddRef(V_UNKNOWN(&value));

            if ((vbInterface && This->vbdeclHandler) ||
               (!vbInterface && This->declHandler))
            {
                if (vbInterface)
                    IVBSAXDeclHandler_Release(This->vbdeclHandler);
                else
                    ISAXDeclHandler_Release(This->declHandler);
            }

            if (vbInterface)
                This->vbdeclHandler = (IVBSAXDeclHandler*)V_UNKNOWN(&value);
2279
            else
2280 2281 2282 2283
                This->declHandler = (ISAXDeclHandler*)V_UNKNOWN(&value);
            break;
        default:
            return E_INVALIDARG;
2284
        }
2285

2286 2287 2288
        return S_OK;
    }

2289
    if(!memcmp(prop, PropertyLexicalHandlerW, sizeof(PropertyLexicalHandlerW)))
2290 2291 2292
    {
        if(This->isParsing) return E_FAIL;

2293
        switch (V_VT(&value))
2294
        {
2295 2296 2297 2298 2299 2300 2301 2302 2303
        case VT_EMPTY:
            if (vbInterface)
            {
                if (This->vblexicalHandler)
                {
                    IVBSAXLexicalHandler_Release(This->vblexicalHandler);
                    This->vblexicalHandler = NULL;
                }
            }
2304
            else
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
                if (This->lexicalHandler)
                {
                    ISAXLexicalHandler_Release(This->lexicalHandler);
                    This->lexicalHandler = NULL;
                }
            break;
        case VT_UNKNOWN:
            if (V_UNKNOWN(&value)) IUnknown_AddRef(V_UNKNOWN(&value));

            if ((vbInterface && This->vblexicalHandler) ||
               (!vbInterface && This->lexicalHandler))
            {
                if (vbInterface)
                    IVBSAXLexicalHandler_Release(This->vblexicalHandler);
                else
                    ISAXLexicalHandler_Release(This->lexicalHandler);
            }

            if (vbInterface)
                This->vblexicalHandler = (IVBSAXLexicalHandler*)V_UNKNOWN(&value);
2325
            else
2326 2327 2328 2329
                This->lexicalHandler = (ISAXLexicalHandler*)V_UNKNOWN(&value);
            break;
        default:
            return E_INVALIDARG;
2330
        }
2331

2332 2333 2334
        return S_OK;
    }

2335
    FIXME("(%p)->(%s): unsupported property\n", This, debugstr_w(prop));
2336

2337
    if(!memcmp(prop, PropertyCharsetW, sizeof(PropertyCharsetW)))
2338 2339
        return E_NOTIMPL;

2340
    if(!memcmp(prop, PropertyDomNodeW, sizeof(PropertyDomNodeW)))
2341 2342
        return E_FAIL;

2343
    if(!memcmp(prop, PropertyInputSourceW, sizeof(PropertyInputSourceW)))
2344 2345
        return E_NOTIMPL;

2346
    if(!memcmp(prop, PropertyMaxElementDepthW, sizeof(PropertyMaxElementDepthW)))
2347 2348
        return E_NOTIMPL;

2349
    if(!memcmp(prop, PropertyMaxXMLSizeW, sizeof(PropertyMaxXMLSizeW)))
2350 2351
        return E_NOTIMPL;

2352
    if(!memcmp(prop, PropertySchemaDeclHandlerW, sizeof(PropertySchemaDeclHandlerW)))
2353 2354
        return E_NOTIMPL;

2355
    if(!memcmp(prop, PropertyXMLDeclEncodingW, sizeof(PropertyXMLDeclEncodingW)))
2356 2357
        return E_FAIL;

2358
    if(!memcmp(prop, PropertyXMLDeclStandaloneW, sizeof(PropertyXMLDeclStandaloneW)))
2359 2360
        return E_FAIL;

2361
    if(!memcmp(prop, PropertyXMLDeclVersionW, sizeof(PropertyXMLDeclVersionW)))
2362 2363 2364 2365 2366
        return E_FAIL;

    return E_INVALIDARG;
}

2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
static HRESULT internal_getProperty(const saxreader* This, const WCHAR *prop, VARIANT *value, BOOL vb)
{
    TRACE("(%p)->(%s)\n", This, debugstr_w(prop));

    if (!value) return E_POINTER;

    if (!memcmp(PropertyLexicalHandlerW, prop, sizeof(PropertyLexicalHandlerW)))
    {
        V_VT(value) = VT_UNKNOWN;
        V_UNKNOWN(value) = vb ? (IUnknown*)This->vblexicalHandler : (IUnknown*)This->lexicalHandler;
        if (V_UNKNOWN(value)) IUnknown_AddRef(V_UNKNOWN(value));
2378 2379 2380 2381 2382 2383 2384 2385
        return S_OK;
    }

    if (!memcmp(PropertyDeclHandlerW, prop, sizeof(PropertyDeclHandlerW)))
    {
        V_VT(value) = VT_UNKNOWN;
        V_UNKNOWN(value) = vb ? (IUnknown*)This->vbdeclHandler : (IUnknown*)This->declHandler;
        if (V_UNKNOWN(value)) IUnknown_AddRef(V_UNKNOWN(value));
2386 2387 2388 2389 2390 2391 2392 2393
        return S_OK;
    }

    FIXME("(%p)->(%s) unsupported property\n", This, debugstr_w(prop));

    return E_NOTIMPL;
}

2394
/*** IVBSAXXMLReader interface ***/
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
/*** IUnknown methods ***/
static HRESULT WINAPI saxxmlreader_QueryInterface(IVBSAXXMLReader* iface, REFIID riid, void **ppvObject)
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );

    *ppvObject = NULL;

    if ( IsEqualGUID( riid, &IID_IUnknown ) ||
         IsEqualGUID( riid, &IID_IDispatch ) ||
         IsEqualGUID( riid, &IID_IVBSAXXMLReader ))
    {
        *ppvObject = iface;
    }
2410 2411
    else if( IsEqualGUID( riid, &IID_ISAXXMLReader ))
    {
2412
        *ppvObject = &This->ISAXXMLReader_iface;
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
    else
    {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
        return E_NOINTERFACE;
    }

    IVBSAXXMLReader_AddRef( iface );

    return S_OK;
}

static ULONG WINAPI saxxmlreader_AddRef(IVBSAXXMLReader* iface)
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
    TRACE("%p\n", This );
    return InterlockedIncrement( &This->ref );
}

static ULONG WINAPI saxxmlreader_Release(
    IVBSAXXMLReader* iface)
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
    LONG ref;

    TRACE("%p\n", This );

    ref = InterlockedDecrement( &This->ref );
    if ( ref == 0 )
    {
2443 2444 2445
        if(This->contentHandler)
            ISAXContentHandler_Release(This->contentHandler);

2446 2447 2448
        if(This->vbcontentHandler)
            IVBSAXContentHandler_Release(This->vbcontentHandler);

2449 2450 2451
        if(This->errorHandler)
            ISAXErrorHandler_Release(This->errorHandler);

2452 2453 2454
        if(This->vberrorHandler)
            IVBSAXErrorHandler_Release(This->vberrorHandler);

2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
        if(This->lexicalHandler)
            ISAXLexicalHandler_Release(This->lexicalHandler);

        if(This->vblexicalHandler)
            IVBSAXLexicalHandler_Release(This->vblexicalHandler);

        if(This->declHandler)
            ISAXDeclHandler_Release(This->declHandler);

        if(This->vbdeclHandler)
            IVBSAXDeclHandler_Release(This->vbdeclHandler);

2467 2468
        free_bstr_pool(&This->pool);

2469
        heap_free( This );
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
    }

    return ref;
}
/*** IDispatch ***/
static HRESULT WINAPI saxxmlreader_GetTypeInfoCount( IVBSAXXMLReader *iface, UINT* pctinfo )
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;

    return S_OK;
}

static HRESULT WINAPI saxxmlreader_GetTypeInfo(
    IVBSAXXMLReader *iface,
    UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo )
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
    HRESULT hr;

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(IVBSAXXMLReader_tid, ppTInfo);

    return hr;
}

static HRESULT WINAPI saxxmlreader_GetIDsOfNames(
    IVBSAXXMLReader *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId)
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
          lcid, rgDispId);

    if(!rgszNames || cNames == 0 || !rgDispId)
        return E_INVALIDARG;

    hr = get_typeinfo(IVBSAXXMLReader_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI saxxmlreader_Invoke(
    IVBSAXXMLReader *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr)
{
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IVBSAXXMLReader_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
2549 2550
        hr = ITypeInfo_Invoke(typeinfo, &This->IVBSAXXMLReader_iface, dispIdMember, wFlags,
                pDispParams, pVarResult, pExcepInfo, puArgErr);
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

/*** IVBSAXXMLReader methods ***/
static HRESULT WINAPI saxxmlreader_getFeature(
    IVBSAXXMLReader* iface,
    const WCHAR *pFeature,
    VARIANT_BOOL *pValue)
{
2563 2564 2565
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    FIXME("(%p)->(%s %p) stub\n", This, debugstr_w(pFeature), pValue);
2566 2567 2568 2569 2570 2571 2572 2573
    return E_NOTIMPL;
}

static HRESULT WINAPI saxxmlreader_putFeature(
    IVBSAXXMLReader* iface,
    const WCHAR *pFeature,
    VARIANT_BOOL vfValue)
{
2574 2575 2576
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    FIXME("(%p)->(%s %x) stub\n", This, debugstr_w(pFeature), vfValue);
2577 2578 2579 2580 2581
    return E_NOTIMPL;
}

static HRESULT WINAPI saxxmlreader_getProperty(
    IVBSAXXMLReader* iface,
2582 2583
    const WCHAR *prop,
    VARIANT *value)
2584
{
2585
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2586
    return internal_getProperty(This, prop, value, TRUE);
2587 2588 2589 2590 2591 2592 2593
}

static HRESULT WINAPI saxxmlreader_putProperty(
    IVBSAXXMLReader* iface,
    const WCHAR *pProp,
    VARIANT value)
{
2594
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2595
    return internal_putProperty(This, pProp, value, TRUE);
2596 2597
}

2598
static HRESULT WINAPI saxxmlreader_get_entityResolver(
2599 2600 2601
    IVBSAXXMLReader* iface,
    IVBSAXEntityResolver **pEntityResolver)
{
2602
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2603
    return internal_getEntityResolver(This, pEntityResolver, TRUE);
2604 2605
}

2606
static HRESULT WINAPI saxxmlreader_put_entityResolver(
2607 2608 2609
    IVBSAXXMLReader* iface,
    IVBSAXEntityResolver *pEntityResolver)
{
2610
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2611
    return internal_putEntityResolver(This, pEntityResolver, TRUE);
2612 2613
}

2614
static HRESULT WINAPI saxxmlreader_get_contentHandler(
2615 2616 2617
    IVBSAXXMLReader* iface,
    IVBSAXContentHandler **ppContentHandler)
{
2618
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2619
    return internal_getContentHandler(This, ppContentHandler, TRUE);
2620 2621
}

2622
static HRESULT WINAPI saxxmlreader_put_contentHandler(
2623 2624 2625
    IVBSAXXMLReader* iface,
    IVBSAXContentHandler *contentHandler)
{
2626
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2627
    return internal_putContentHandler(This, contentHandler, TRUE);
2628 2629
}

2630
static HRESULT WINAPI saxxmlreader_get_dtdHandler(
2631 2632 2633
    IVBSAXXMLReader* iface,
    IVBSAXDTDHandler **pDTDHandler)
{
2634
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2635
    return internal_getDTDHandler(This, pDTDHandler, TRUE);
2636 2637
}

2638
static HRESULT WINAPI saxxmlreader_put_dtdHandler(
2639 2640 2641
    IVBSAXXMLReader* iface,
    IVBSAXDTDHandler *pDTDHandler)
{
2642
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2643
    return internal_putDTDHandler(This, pDTDHandler, TRUE);
2644 2645
}

2646
static HRESULT WINAPI saxxmlreader_get_errorHandler(
2647 2648 2649
    IVBSAXXMLReader* iface,
    IVBSAXErrorHandler **pErrorHandler)
{
2650
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2651
    return internal_getErrorHandler(This, pErrorHandler, TRUE);
2652 2653
}

2654
static HRESULT WINAPI saxxmlreader_put_errorHandler(
2655 2656 2657
    IVBSAXXMLReader* iface,
    IVBSAXErrorHandler *errorHandler)
{
2658
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2659
    return internal_putErrorHandler(This, errorHandler, TRUE);
2660 2661
}

2662
static HRESULT WINAPI saxxmlreader_get_baseURL(
2663 2664 2665
    IVBSAXXMLReader* iface,
    const WCHAR **pBaseUrl)
{
2666 2667 2668
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    FIXME("(%p)->(%p) stub\n", This, pBaseUrl);
2669 2670 2671
    return E_NOTIMPL;
}

2672
static HRESULT WINAPI saxxmlreader_put_baseURL(
2673 2674 2675
    IVBSAXXMLReader* iface,
    const WCHAR *pBaseUrl)
{
2676 2677 2678
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    FIXME("(%p)->(%s) stub\n", This, debugstr_w(pBaseUrl));
2679 2680 2681
    return E_NOTIMPL;
}

2682
static HRESULT WINAPI saxxmlreader_get_secureBaseURL(
2683 2684 2685
    IVBSAXXMLReader* iface,
    const WCHAR **pSecureBaseUrl)
{
2686 2687 2688
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    FIXME("(%p)->(%p) stub\n", This, pSecureBaseUrl);
2689 2690 2691 2692
    return E_NOTIMPL;
}


2693
static HRESULT WINAPI saxxmlreader_put_secureBaseURL(
2694 2695 2696
    IVBSAXXMLReader* iface,
    const WCHAR *secureBaseUrl)
{
2697 2698 2699
    saxreader *This = impl_from_IVBSAXXMLReader( iface );

    FIXME("(%p)->(%s) stub\n", This, debugstr_w(secureBaseUrl));
2700 2701 2702 2703 2704 2705 2706
    return E_NOTIMPL;
}

static HRESULT WINAPI saxxmlreader_parse(
    IVBSAXXMLReader* iface,
    VARIANT varInput)
{
2707
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2708
    return internal_parse(This, varInput, TRUE);
2709 2710 2711 2712 2713 2714
}

static HRESULT WINAPI saxxmlreader_parseURL(
    IVBSAXXMLReader* iface,
    const WCHAR *url)
{
2715
    saxreader *This = impl_from_IVBSAXXMLReader( iface );
2716
    return internal_parseURL(This, url, TRUE);
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
}

static const struct IVBSAXXMLReaderVtbl saxreader_vtbl =
{
    saxxmlreader_QueryInterface,
    saxxmlreader_AddRef,
    saxxmlreader_Release,
    saxxmlreader_GetTypeInfoCount,
    saxxmlreader_GetTypeInfo,
    saxxmlreader_GetIDsOfNames,
    saxxmlreader_Invoke,
    saxxmlreader_getFeature,
    saxxmlreader_putFeature,
    saxxmlreader_getProperty,
    saxxmlreader_putProperty,
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
    saxxmlreader_get_entityResolver,
    saxxmlreader_put_entityResolver,
    saxxmlreader_get_contentHandler,
    saxxmlreader_put_contentHandler,
    saxxmlreader_get_dtdHandler,
    saxxmlreader_put_dtdHandler,
    saxxmlreader_get_errorHandler,
    saxxmlreader_put_errorHandler,
    saxxmlreader_get_baseURL,
    saxxmlreader_put_baseURL,
    saxxmlreader_get_secureBaseURL,
    saxxmlreader_put_secureBaseURL,
2744 2745 2746 2747
    saxxmlreader_parse,
    saxxmlreader_parseURL
};

2748 2749 2750 2751 2752
/*** ISAXXMLReader interface ***/
/*** IUnknown methods ***/
static HRESULT WINAPI isaxxmlreader_QueryInterface(ISAXXMLReader* iface, REFIID riid, void **ppvObject)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2753
    return saxxmlreader_QueryInterface(&This->IVBSAXXMLReader_iface, riid, ppvObject);
2754 2755 2756 2757 2758
}

static ULONG WINAPI isaxxmlreader_AddRef(ISAXXMLReader* iface)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2759
    return saxxmlreader_AddRef(&This->IVBSAXXMLReader_iface);
2760 2761 2762 2763 2764
}

static ULONG WINAPI isaxxmlreader_Release(ISAXXMLReader* iface)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2765
    return saxxmlreader_Release(&This->IVBSAXXMLReader_iface);
2766 2767 2768 2769 2770 2771 2772 2773 2774
}

/*** ISAXXMLReader methods ***/
static HRESULT WINAPI isaxxmlreader_getFeature(
        ISAXXMLReader* iface,
        const WCHAR *pFeature,
        VARIANT_BOOL *pValue)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2775
    return IVBSAXXMLReader_getFeature(&This->IVBSAXXMLReader_iface, pFeature, pValue);
2776 2777 2778 2779 2780 2781 2782 2783
}

static HRESULT WINAPI isaxxmlreader_putFeature(
        ISAXXMLReader* iface,
        const WCHAR *pFeature,
        VARIANT_BOOL vfValue)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2784
    return IVBSAXXMLReader_putFeature(&This->IVBSAXXMLReader_iface, pFeature, vfValue);
2785 2786 2787 2788
}

static HRESULT WINAPI isaxxmlreader_getProperty(
        ISAXXMLReader* iface,
2789 2790
        const WCHAR *prop,
        VARIANT *value)
2791 2792
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2793
    return internal_getProperty(This, prop, value, FALSE);
2794 2795 2796 2797 2798 2799 2800 2801
}

static HRESULT WINAPI isaxxmlreader_putProperty(
        ISAXXMLReader* iface,
        const WCHAR *pProp,
        VARIANT value)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2802
    return internal_putProperty(This, pProp, value, FALSE);
2803 2804 2805 2806 2807 2808 2809
}

static HRESULT WINAPI isaxxmlreader_getEntityResolver(
        ISAXXMLReader* iface,
        ISAXEntityResolver **ppEntityResolver)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2810
    return internal_getEntityResolver(This, ppEntityResolver, FALSE);
2811 2812 2813 2814 2815 2816 2817
}

static HRESULT WINAPI isaxxmlreader_putEntityResolver(
        ISAXXMLReader* iface,
        ISAXEntityResolver *pEntityResolver)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2818
    return internal_putEntityResolver(This, pEntityResolver, FALSE);
2819 2820 2821 2822 2823 2824 2825
}

static HRESULT WINAPI isaxxmlreader_getContentHandler(
        ISAXXMLReader* iface,
        ISAXContentHandler **pContentHandler)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2826
    return internal_getContentHandler(This, pContentHandler, FALSE);
2827 2828 2829 2830 2831 2832 2833
}

static HRESULT WINAPI isaxxmlreader_putContentHandler(
        ISAXXMLReader* iface,
        ISAXContentHandler *contentHandler)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2834
    return internal_putContentHandler(This, contentHandler, FALSE);
2835 2836 2837 2838 2839 2840 2841
}

static HRESULT WINAPI isaxxmlreader_getDTDHandler(
        ISAXXMLReader* iface,
        ISAXDTDHandler **pDTDHandler)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2842
    return internal_getDTDHandler(This, pDTDHandler, FALSE);
2843 2844 2845 2846 2847 2848 2849
}

static HRESULT WINAPI isaxxmlreader_putDTDHandler(
        ISAXXMLReader* iface,
        ISAXDTDHandler *pDTDHandler)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2850
    return internal_putDTDHandler(This, pDTDHandler, FALSE);
2851 2852 2853 2854 2855 2856 2857
}

static HRESULT WINAPI isaxxmlreader_getErrorHandler(
        ISAXXMLReader* iface,
        ISAXErrorHandler **pErrorHandler)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2858
    return internal_getErrorHandler(This, pErrorHandler, FALSE);
2859 2860 2861 2862 2863 2864 2865
}

static HRESULT WINAPI isaxxmlreader_putErrorHandler(
        ISAXXMLReader* iface,
        ISAXErrorHandler *errorHandler)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2866
    return internal_putErrorHandler(This, errorHandler, FALSE);
2867 2868 2869 2870 2871 2872 2873
}

static HRESULT WINAPI isaxxmlreader_getBaseURL(
        ISAXXMLReader* iface,
        const WCHAR **pBaseUrl)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2874
    return IVBSAXXMLReader_get_baseURL(&This->IVBSAXXMLReader_iface, pBaseUrl);
2875 2876 2877 2878 2879 2880 2881
}

static HRESULT WINAPI isaxxmlreader_putBaseURL(
        ISAXXMLReader* iface,
        const WCHAR *pBaseUrl)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2882
    return IVBSAXXMLReader_put_baseURL(&This->IVBSAXXMLReader_iface, pBaseUrl);
2883 2884 2885 2886 2887 2888 2889
}

static HRESULT WINAPI isaxxmlreader_getSecureBaseURL(
        ISAXXMLReader* iface,
        const WCHAR **pSecureBaseUrl)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2890
    return IVBSAXXMLReader_get_secureBaseURL(&This->IVBSAXXMLReader_iface, pSecureBaseUrl);
2891 2892 2893 2894 2895 2896 2897
}

static HRESULT WINAPI isaxxmlreader_putSecureBaseURL(
        ISAXXMLReader* iface,
        const WCHAR *secureBaseUrl)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2898
    return IVBSAXXMLReader_put_secureBaseURL(&This->IVBSAXXMLReader_iface, secureBaseUrl);
2899 2900 2901 2902 2903 2904 2905
}

static HRESULT WINAPI isaxxmlreader_parse(
        ISAXXMLReader* iface,
        VARIANT varInput)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2906
    return internal_parse(This, varInput, FALSE);
2907 2908 2909 2910 2911 2912 2913
}

static HRESULT WINAPI isaxxmlreader_parseURL(
        ISAXXMLReader* iface,
        const WCHAR *url)
{
    saxreader *This = impl_from_ISAXXMLReader( iface );
2914
    return internal_parseURL(This, url, FALSE);
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
}

static const struct ISAXXMLReaderVtbl isaxreader_vtbl =
{
    isaxxmlreader_QueryInterface,
    isaxxmlreader_AddRef,
    isaxxmlreader_Release,
    isaxxmlreader_getFeature,
    isaxxmlreader_putFeature,
    isaxxmlreader_getProperty,
    isaxxmlreader_putProperty,
    isaxxmlreader_getEntityResolver,
    isaxxmlreader_putEntityResolver,
    isaxxmlreader_getContentHandler,
    isaxxmlreader_putContentHandler,
    isaxxmlreader_getDTDHandler,
    isaxxmlreader_putDTDHandler,
    isaxxmlreader_getErrorHandler,
    isaxxmlreader_putErrorHandler,
    isaxxmlreader_getBaseURL,
    isaxxmlreader_putBaseURL,
    isaxxmlreader_getSecureBaseURL,
    isaxxmlreader_putSecureBaseURL,
    isaxxmlreader_parse,
    isaxxmlreader_parseURL
};

2942 2943 2944 2945 2946 2947
HRESULT SAXXMLReader_create(IUnknown *pUnkOuter, LPVOID *ppObj)
{
    saxreader *reader;

    TRACE("(%p,%p)\n", pUnkOuter, ppObj);

2948
    reader = heap_alloc( sizeof (*reader) );
2949 2950 2951
    if( !reader )
        return E_OUTOFMEMORY;

2952 2953
    reader->IVBSAXXMLReader_iface.lpVtbl = &saxreader_vtbl;
    reader->ISAXXMLReader_iface.lpVtbl = &isaxreader_vtbl;
2954
    reader->ref = 1;
2955
    reader->contentHandler = NULL;
2956
    reader->vbcontentHandler = NULL;
2957
    reader->errorHandler = NULL;
2958
    reader->vberrorHandler = NULL;
2959 2960 2961 2962 2963
    reader->lexicalHandler = NULL;
    reader->vblexicalHandler = NULL;
    reader->declHandler = NULL;
    reader->vbdeclHandler = NULL;
    reader->isParsing = FALSE;
2964 2965 2966
    reader->pool.pool = NULL;
    reader->pool.index = 0;
    reader->pool.len = 0;
2967

2968 2969
    memset(&reader->sax, 0, sizeof(xmlSAXHandler));
    reader->sax.initialized = XML_SAX2_MAGIC;
2970
    reader->sax.startDocument = libxmlStartDocument;
2971
    reader->sax.endDocument = libxmlEndDocument;
2972
    reader->sax.startElementNs = libxmlStartElementNS;
2973
    reader->sax.endElementNs = libxmlEndElementNS;
2974
    reader->sax.characters = libxmlCharacters;
2975
    reader->sax.setDocumentLocator = libxmlSetDocumentLocator;
2976
    reader->sax.comment = libxmlComment;
2977 2978
    reader->sax.error = libxmlFatalError;
    reader->sax.fatalError = libxmlFatalError;
2979
    reader->sax.cdataBlock = libxmlCDataBlock;
2980

2981
    *ppObj = &reader->IVBSAXXMLReader_iface;
2982

2983
    TRACE("returning iface %p\n", *ppObj);
2984

2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
    return S_OK;
}

#else

HRESULT SAXXMLReader_create(IUnknown *pUnkOuter, LPVOID *ppObj)
{
    MESSAGE("This program tried to use a SAX XML Reader object, but\n"
            "libxml2 support was not present at compile time.\n");
    return E_NOTIMPL;
}

#endif