PageRenderTime 121ms CodeModel.GetById 39ms app.highlight 64ms RepoModel.GetById 1ms app.codeStats 1ms

/Modules/expat/xmlparse.c

http://unladen-swallow.googlecode.com/
C | 6268 lines | 5616 code | 340 blank | 312 comment | 1247 complexity | 26b29043af5e2a1b99cb05d74184f45b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
   2   See the file COPYING for copying permission.
   3*/
   4
   5#define XML_BUILDING_EXPAT 1
   6
   7#ifdef COMPILED_FROM_DSP
   8#include "winconfig.h"
   9#elif defined(MACOS_CLASSIC)
  10#include "macconfig.h"
  11#elif defined(__amigaos4__)
  12#include "amigaconfig.h"
  13#elif defined(HAVE_EXPAT_CONFIG_H)
  14#include <expat_config.h>
  15#endif /* ndef COMPILED_FROM_DSP */
  16
  17#include <stddef.h>
  18#include <string.h>                     /* memset(), memcpy() */
  19#include <assert.h>
  20
  21#include "expat.h"
  22
  23#ifdef XML_UNICODE
  24#define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
  25#define XmlConvert XmlUtf16Convert
  26#define XmlGetInternalEncoding XmlGetUtf16InternalEncoding
  27#define XmlGetInternalEncodingNS XmlGetUtf16InternalEncodingNS
  28#define XmlEncode XmlUtf16Encode
  29#define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((unsigned long)s) & 1))
  30typedef unsigned short ICHAR;
  31#else
  32#define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
  33#define XmlConvert XmlUtf8Convert
  34#define XmlGetInternalEncoding XmlGetUtf8InternalEncoding
  35#define XmlGetInternalEncodingNS XmlGetUtf8InternalEncodingNS
  36#define XmlEncode XmlUtf8Encode
  37#define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
  38typedef char ICHAR;
  39#endif
  40
  41
  42#ifndef XML_NS
  43
  44#define XmlInitEncodingNS XmlInitEncoding
  45#define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
  46#undef XmlGetInternalEncodingNS
  47#define XmlGetInternalEncodingNS XmlGetInternalEncoding
  48#define XmlParseXmlDeclNS XmlParseXmlDecl
  49
  50#endif
  51
  52#ifdef XML_UNICODE
  53
  54#ifdef XML_UNICODE_WCHAR_T
  55#define XML_T(x) (const wchar_t)x
  56#define XML_L(x) L ## x
  57#else
  58#define XML_T(x) (const unsigned short)x
  59#define XML_L(x) x
  60#endif
  61
  62#else
  63
  64#define XML_T(x) x
  65#define XML_L(x) x
  66
  67#endif
  68
  69/* Round up n to be a multiple of sz, where sz is a power of 2. */
  70#define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
  71
  72/* Handle the case where memmove() doesn't exist. */
  73#ifndef HAVE_MEMMOVE
  74#ifdef HAVE_BCOPY
  75#define memmove(d,s,l) bcopy((s),(d),(l))
  76#else
  77#error memmove does not exist on this platform, nor is a substitute available
  78#endif /* HAVE_BCOPY */
  79#endif /* HAVE_MEMMOVE */
  80
  81#include "internal.h"
  82#include "xmltok.h"
  83#include "xmlrole.h"
  84
  85typedef const XML_Char *KEY;
  86
  87typedef struct {
  88  KEY name;
  89} NAMED;
  90
  91typedef struct {
  92  NAMED **v;
  93  unsigned char power;
  94  size_t size;
  95  size_t used;
  96  const XML_Memory_Handling_Suite *mem;
  97} HASH_TABLE;
  98
  99/* Basic character hash algorithm, taken from Python's string hash:
 100   h = h * 1000003 ^ character, the constant being a prime number.
 101
 102*/
 103#ifdef XML_UNICODE
 104#define CHAR_HASH(h, c) \
 105  (((h) * 0xF4243) ^ (unsigned short)(c))
 106#else
 107#define CHAR_HASH(h, c) \
 108  (((h) * 0xF4243) ^ (unsigned char)(c))
 109#endif
 110
 111/* For probing (after a collision) we need a step size relative prime
 112   to the hash table size, which is a power of 2. We use double-hashing,
 113   since we can calculate a second hash value cheaply by taking those bits
 114   of the first hash value that were discarded (masked out) when the table
 115   index was calculated: index = hash & mask, where mask = table->size - 1.
 116   We limit the maximum step size to table->size / 4 (mask >> 2) and make
 117   it odd, since odd numbers are always relative prime to a power of 2.
 118*/
 119#define SECOND_HASH(hash, mask, power) \
 120  ((((hash) & ~(mask)) >> ((power) - 1)) & ((mask) >> 2))
 121#define PROBE_STEP(hash, mask, power) \
 122  ((unsigned char)((SECOND_HASH(hash, mask, power)) | 1))
 123
 124typedef struct {
 125  NAMED **p;
 126  NAMED **end;
 127} HASH_TABLE_ITER;
 128
 129#define INIT_TAG_BUF_SIZE 32  /* must be a multiple of sizeof(XML_Char) */
 130#define INIT_DATA_BUF_SIZE 1024
 131#define INIT_ATTS_SIZE 16
 132#define INIT_ATTS_VERSION 0xFFFFFFFF
 133#define INIT_BLOCK_SIZE 1024
 134#define INIT_BUFFER_SIZE 1024
 135
 136#define EXPAND_SPARE 24
 137
 138typedef struct binding {
 139  struct prefix *prefix;
 140  struct binding *nextTagBinding;
 141  struct binding *prevPrefixBinding;
 142  const struct attribute_id *attId;
 143  XML_Char *uri;
 144  int uriLen;
 145  int uriAlloc;
 146} BINDING;
 147
 148typedef struct prefix {
 149  const XML_Char *name;
 150  BINDING *binding;
 151} PREFIX;
 152
 153typedef struct {
 154  const XML_Char *str;
 155  const XML_Char *localPart;
 156  const XML_Char *prefix;
 157  int strLen;
 158  int uriLen;
 159  int prefixLen;
 160} TAG_NAME;
 161
 162/* TAG represents an open element.
 163   The name of the element is stored in both the document and API
 164   encodings.  The memory buffer 'buf' is a separately-allocated
 165   memory area which stores the name.  During the XML_Parse()/
 166   XMLParseBuffer() when the element is open, the memory for the 'raw'
 167   version of the name (in the document encoding) is shared with the
 168   document buffer.  If the element is open across calls to
 169   XML_Parse()/XML_ParseBuffer(), the buffer is re-allocated to
 170   contain the 'raw' name as well.
 171
 172   A parser re-uses these structures, maintaining a list of allocated
 173   TAG objects in a free list.
 174*/
 175typedef struct tag {
 176  struct tag *parent;           /* parent of this element */
 177  const char *rawName;          /* tagName in the original encoding */
 178  int rawNameLength;
 179  TAG_NAME name;                /* tagName in the API encoding */
 180  char *buf;                    /* buffer for name components */
 181  char *bufEnd;                 /* end of the buffer */
 182  BINDING *bindings;
 183} TAG;
 184
 185typedef struct {
 186  const XML_Char *name;
 187  const XML_Char *textPtr;
 188  int textLen;                  /* length in XML_Chars */
 189  int processed;                /* # of processed bytes - when suspended */
 190  const XML_Char *systemId;
 191  const XML_Char *base;
 192  const XML_Char *publicId;
 193  const XML_Char *notation;
 194  XML_Bool open;
 195  XML_Bool is_param;
 196  XML_Bool is_internal; /* true if declared in internal subset outside PE */
 197} ENTITY;
 198
 199typedef struct {
 200  enum XML_Content_Type         type;
 201  enum XML_Content_Quant        quant;
 202  const XML_Char *              name;
 203  int                           firstchild;
 204  int                           lastchild;
 205  int                           childcnt;
 206  int                           nextsib;
 207} CONTENT_SCAFFOLD;
 208
 209#define INIT_SCAFFOLD_ELEMENTS 32
 210
 211typedef struct block {
 212  struct block *next;
 213  int size;
 214  XML_Char s[1];
 215} BLOCK;
 216
 217typedef struct {
 218  BLOCK *blocks;
 219  BLOCK *freeBlocks;
 220  const XML_Char *end;
 221  XML_Char *ptr;
 222  XML_Char *start;
 223  const XML_Memory_Handling_Suite *mem;
 224} STRING_POOL;
 225
 226/* The XML_Char before the name is used to determine whether
 227   an attribute has been specified. */
 228typedef struct attribute_id {
 229  XML_Char *name;
 230  PREFIX *prefix;
 231  XML_Bool maybeTokenized;
 232  XML_Bool xmlns;
 233} ATTRIBUTE_ID;
 234
 235typedef struct {
 236  const ATTRIBUTE_ID *id;
 237  XML_Bool isCdata;
 238  const XML_Char *value;
 239} DEFAULT_ATTRIBUTE;
 240
 241typedef struct {
 242  unsigned long version;
 243  unsigned long hash;
 244  const XML_Char *uriName;
 245} NS_ATT;
 246
 247typedef struct {
 248  const XML_Char *name;
 249  PREFIX *prefix;
 250  const ATTRIBUTE_ID *idAtt;
 251  int nDefaultAtts;
 252  int allocDefaultAtts;
 253  DEFAULT_ATTRIBUTE *defaultAtts;
 254} ELEMENT_TYPE;
 255
 256typedef struct {
 257  HASH_TABLE generalEntities;
 258  HASH_TABLE elementTypes;
 259  HASH_TABLE attributeIds;
 260  HASH_TABLE prefixes;
 261  STRING_POOL pool;
 262  STRING_POOL entityValuePool;
 263  /* false once a parameter entity reference has been skipped */
 264  XML_Bool keepProcessing;
 265  /* true once an internal or external PE reference has been encountered;
 266     this includes the reference to an external subset */
 267  XML_Bool hasParamEntityRefs;
 268  XML_Bool standalone;
 269#ifdef XML_DTD
 270  /* indicates if external PE has been read */
 271  XML_Bool paramEntityRead;
 272  HASH_TABLE paramEntities;
 273#endif /* XML_DTD */
 274  PREFIX defaultPrefix;
 275  /* === scaffolding for building content model === */
 276  XML_Bool in_eldecl;
 277  CONTENT_SCAFFOLD *scaffold;
 278  unsigned contentStringLen;
 279  unsigned scaffSize;
 280  unsigned scaffCount;
 281  int scaffLevel;
 282  int *scaffIndex;
 283} DTD;
 284
 285typedef struct open_internal_entity {
 286  const char *internalEventPtr;
 287  const char *internalEventEndPtr;
 288  struct open_internal_entity *next;
 289  ENTITY *entity;
 290  int startTagLevel;
 291  XML_Bool betweenDecl; /* WFC: PE Between Declarations */
 292} OPEN_INTERNAL_ENTITY;
 293
 294typedef enum XML_Error PTRCALL Processor(XML_Parser parser,
 295                                         const char *start,
 296                                         const char *end,
 297                                         const char **endPtr);
 298
 299static Processor prologProcessor;
 300static Processor prologInitProcessor;
 301static Processor contentProcessor;
 302static Processor cdataSectionProcessor;
 303#ifdef XML_DTD
 304static Processor ignoreSectionProcessor;
 305static Processor externalParEntProcessor;
 306static Processor externalParEntInitProcessor;
 307static Processor entityValueProcessor;
 308static Processor entityValueInitProcessor;
 309#endif /* XML_DTD */
 310static Processor epilogProcessor;
 311static Processor errorProcessor;
 312static Processor externalEntityInitProcessor;
 313static Processor externalEntityInitProcessor2;
 314static Processor externalEntityInitProcessor3;
 315static Processor externalEntityContentProcessor;
 316static Processor internalEntityProcessor;
 317
 318static enum XML_Error
 319handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName);
 320static enum XML_Error
 321processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
 322               const char *s, const char *next);
 323static enum XML_Error
 324initializeEncoding(XML_Parser parser);
 325static enum XML_Error
 326doProlog(XML_Parser parser, const ENCODING *enc, const char *s, 
 327         const char *end, int tok, const char *next, const char **nextPtr, 
 328         XML_Bool haveMore);
 329static enum XML_Error
 330processInternalEntity(XML_Parser parser, ENTITY *entity, 
 331                      XML_Bool betweenDecl);
 332static enum XML_Error
 333doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc,
 334          const char *start, const char *end, const char **endPtr, 
 335          XML_Bool haveMore);
 336static enum XML_Error
 337doCdataSection(XML_Parser parser, const ENCODING *, const char **startPtr,
 338               const char *end, const char **nextPtr, XML_Bool haveMore);
 339#ifdef XML_DTD
 340static enum XML_Error
 341doIgnoreSection(XML_Parser parser, const ENCODING *, const char **startPtr,
 342                const char *end, const char **nextPtr, XML_Bool haveMore);
 343#endif /* XML_DTD */
 344
 345static enum XML_Error
 346storeAtts(XML_Parser parser, const ENCODING *, const char *s,
 347          TAG_NAME *tagNamePtr, BINDING **bindingsPtr);
 348static enum XML_Error
 349addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
 350           const XML_Char *uri, BINDING **bindingsPtr);
 351static int
 352defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *, XML_Bool isCdata, 
 353                XML_Bool isId, const XML_Char *dfltValue, XML_Parser parser);
 354static enum XML_Error
 355storeAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
 356                    const char *, const char *, STRING_POOL *);
 357static enum XML_Error
 358appendAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
 359                     const char *, const char *, STRING_POOL *);
 360static ATTRIBUTE_ID *
 361getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start,
 362               const char *end);
 363static int
 364setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *);
 365static enum XML_Error
 366storeEntityValue(XML_Parser parser, const ENCODING *enc, const char *start,
 367                 const char *end);
 368static int
 369reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
 370                            const char *start, const char *end);
 371static int
 372reportComment(XML_Parser parser, const ENCODING *enc, const char *start,
 373              const char *end);
 374static void
 375reportDefault(XML_Parser parser, const ENCODING *enc, const char *start,
 376              const char *end);
 377
 378static const XML_Char * getContext(XML_Parser parser);
 379static XML_Bool
 380setContext(XML_Parser parser, const XML_Char *context);
 381
 382static void FASTCALL normalizePublicId(XML_Char *s);
 383
 384static DTD * dtdCreate(const XML_Memory_Handling_Suite *ms);
 385/* do not call if parentParser != NULL */
 386static void dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms);
 387static void
 388dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms);
 389static int
 390dtdCopy(DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms);
 391static int
 392copyEntityTable(HASH_TABLE *, STRING_POOL *, const HASH_TABLE *);
 393
 394static NAMED *
 395lookup(HASH_TABLE *table, KEY name, size_t createSize);
 396static void FASTCALL
 397hashTableInit(HASH_TABLE *, const XML_Memory_Handling_Suite *ms);
 398static void FASTCALL hashTableClear(HASH_TABLE *);
 399static void FASTCALL hashTableDestroy(HASH_TABLE *);
 400static void FASTCALL
 401hashTableIterInit(HASH_TABLE_ITER *, const HASH_TABLE *);
 402static NAMED * FASTCALL hashTableIterNext(HASH_TABLE_ITER *);
 403
 404static void FASTCALL
 405poolInit(STRING_POOL *, const XML_Memory_Handling_Suite *ms);
 406static void FASTCALL poolClear(STRING_POOL *);
 407static void FASTCALL poolDestroy(STRING_POOL *);
 408static XML_Char *
 409poolAppend(STRING_POOL *pool, const ENCODING *enc,
 410           const char *ptr, const char *end);
 411static XML_Char *
 412poolStoreString(STRING_POOL *pool, const ENCODING *enc,
 413                const char *ptr, const char *end);
 414static XML_Bool FASTCALL poolGrow(STRING_POOL *pool);
 415static const XML_Char * FASTCALL
 416poolCopyString(STRING_POOL *pool, const XML_Char *s);
 417static const XML_Char *
 418poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n);
 419static const XML_Char * FASTCALL
 420poolAppendString(STRING_POOL *pool, const XML_Char *s);
 421
 422static int FASTCALL nextScaffoldPart(XML_Parser parser);
 423static XML_Content * build_model(XML_Parser parser);
 424static ELEMENT_TYPE *
 425getElementType(XML_Parser parser, const ENCODING *enc,
 426               const char *ptr, const char *end);
 427
 428static XML_Parser
 429parserCreate(const XML_Char *encodingName,
 430             const XML_Memory_Handling_Suite *memsuite,
 431             const XML_Char *nameSep,
 432             DTD *dtd);
 433static void
 434parserInit(XML_Parser parser, const XML_Char *encodingName);
 435
 436#define poolStart(pool) ((pool)->start)
 437#define poolEnd(pool) ((pool)->ptr)
 438#define poolLength(pool) ((pool)->ptr - (pool)->start)
 439#define poolChop(pool) ((void)--(pool->ptr))
 440#define poolLastChar(pool) (((pool)->ptr)[-1])
 441#define poolDiscard(pool) ((pool)->ptr = (pool)->start)
 442#define poolFinish(pool) ((pool)->start = (pool)->ptr)
 443#define poolAppendChar(pool, c) \
 444  (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
 445   ? 0 \
 446   : ((*((pool)->ptr)++ = c), 1))
 447
 448struct XML_ParserStruct {
 449  /* The first member must be userData so that the XML_GetUserData
 450     macro works. */
 451  void *m_userData;
 452  void *m_handlerArg;
 453  char *m_buffer;
 454  const XML_Memory_Handling_Suite m_mem;
 455  /* first character to be parsed */
 456  const char *m_bufferPtr;
 457  /* past last character to be parsed */
 458  char *m_bufferEnd;
 459  /* allocated end of buffer */
 460  const char *m_bufferLim;
 461  XML_Index m_parseEndByteIndex;
 462  const char *m_parseEndPtr;
 463  XML_Char *m_dataBuf;
 464  XML_Char *m_dataBufEnd;
 465  XML_StartElementHandler m_startElementHandler;
 466  XML_EndElementHandler m_endElementHandler;
 467  XML_CharacterDataHandler m_characterDataHandler;
 468  XML_ProcessingInstructionHandler m_processingInstructionHandler;
 469  XML_CommentHandler m_commentHandler;
 470  XML_StartCdataSectionHandler m_startCdataSectionHandler;
 471  XML_EndCdataSectionHandler m_endCdataSectionHandler;
 472  XML_DefaultHandler m_defaultHandler;
 473  XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
 474  XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
 475  XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
 476  XML_NotationDeclHandler m_notationDeclHandler;
 477  XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
 478  XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
 479  XML_NotStandaloneHandler m_notStandaloneHandler;
 480  XML_ExternalEntityRefHandler m_externalEntityRefHandler;
 481  XML_Parser m_externalEntityRefHandlerArg;
 482  XML_SkippedEntityHandler m_skippedEntityHandler;
 483  XML_UnknownEncodingHandler m_unknownEncodingHandler;
 484  XML_ElementDeclHandler m_elementDeclHandler;
 485  XML_AttlistDeclHandler m_attlistDeclHandler;
 486  XML_EntityDeclHandler m_entityDeclHandler;
 487  XML_XmlDeclHandler m_xmlDeclHandler;
 488  const ENCODING *m_encoding;
 489  INIT_ENCODING m_initEncoding;
 490  const ENCODING *m_internalEncoding;
 491  const XML_Char *m_protocolEncodingName;
 492  XML_Bool m_ns;
 493  XML_Bool m_ns_triplets;
 494  void *m_unknownEncodingMem;
 495  void *m_unknownEncodingData;
 496  void *m_unknownEncodingHandlerData;
 497  void (XMLCALL *m_unknownEncodingRelease)(void *);
 498  PROLOG_STATE m_prologState;
 499  Processor *m_processor;
 500  enum XML_Error m_errorCode;
 501  const char *m_eventPtr;
 502  const char *m_eventEndPtr;
 503  const char *m_positionPtr;
 504  OPEN_INTERNAL_ENTITY *m_openInternalEntities;
 505  OPEN_INTERNAL_ENTITY *m_freeInternalEntities;
 506  XML_Bool m_defaultExpandInternalEntities;
 507  int m_tagLevel;
 508  ENTITY *m_declEntity;
 509  const XML_Char *m_doctypeName;
 510  const XML_Char *m_doctypeSysid;
 511  const XML_Char *m_doctypePubid;
 512  const XML_Char *m_declAttributeType;
 513  const XML_Char *m_declNotationName;
 514  const XML_Char *m_declNotationPublicId;
 515  ELEMENT_TYPE *m_declElementType;
 516  ATTRIBUTE_ID *m_declAttributeId;
 517  XML_Bool m_declAttributeIsCdata;
 518  XML_Bool m_declAttributeIsId;
 519  DTD *m_dtd;
 520  const XML_Char *m_curBase;
 521  TAG *m_tagStack;
 522  TAG *m_freeTagList;
 523  BINDING *m_inheritedBindings;
 524  BINDING *m_freeBindingList;
 525  int m_attsSize;
 526  int m_nSpecifiedAtts;
 527  int m_idAttIndex;
 528  ATTRIBUTE *m_atts;
 529  NS_ATT *m_nsAtts;
 530  unsigned long m_nsAttsVersion;
 531  unsigned char m_nsAttsPower;
 532  POSITION m_position;
 533  STRING_POOL m_tempPool;
 534  STRING_POOL m_temp2Pool;
 535  char *m_groupConnector;
 536  unsigned int m_groupSize;
 537  XML_Char m_namespaceSeparator;
 538  XML_Parser m_parentParser;
 539  XML_ParsingStatus m_parsingStatus;
 540#ifdef XML_DTD
 541  XML_Bool m_isParamEntity;
 542  XML_Bool m_useForeignDTD;
 543  enum XML_ParamEntityParsing m_paramEntityParsing;
 544#endif
 545};
 546
 547#define MALLOC(s) (parser->m_mem.malloc_fcn((s)))
 548#define REALLOC(p,s) (parser->m_mem.realloc_fcn((p),(s)))
 549#define FREE(p) (parser->m_mem.free_fcn((p)))
 550
 551#define userData (parser->m_userData)
 552#define handlerArg (parser->m_handlerArg)
 553#define startElementHandler (parser->m_startElementHandler)
 554#define endElementHandler (parser->m_endElementHandler)
 555#define characterDataHandler (parser->m_characterDataHandler)
 556#define processingInstructionHandler \
 557        (parser->m_processingInstructionHandler)
 558#define commentHandler (parser->m_commentHandler)
 559#define startCdataSectionHandler \
 560        (parser->m_startCdataSectionHandler)
 561#define endCdataSectionHandler (parser->m_endCdataSectionHandler)
 562#define defaultHandler (parser->m_defaultHandler)
 563#define startDoctypeDeclHandler (parser->m_startDoctypeDeclHandler)
 564#define endDoctypeDeclHandler (parser->m_endDoctypeDeclHandler)
 565#define unparsedEntityDeclHandler \
 566        (parser->m_unparsedEntityDeclHandler)
 567#define notationDeclHandler (parser->m_notationDeclHandler)
 568#define startNamespaceDeclHandler \
 569        (parser->m_startNamespaceDeclHandler)
 570#define endNamespaceDeclHandler (parser->m_endNamespaceDeclHandler)
 571#define notStandaloneHandler (parser->m_notStandaloneHandler)
 572#define externalEntityRefHandler \
 573        (parser->m_externalEntityRefHandler)
 574#define externalEntityRefHandlerArg \
 575        (parser->m_externalEntityRefHandlerArg)
 576#define internalEntityRefHandler \
 577        (parser->m_internalEntityRefHandler)
 578#define skippedEntityHandler (parser->m_skippedEntityHandler)
 579#define unknownEncodingHandler (parser->m_unknownEncodingHandler)
 580#define elementDeclHandler (parser->m_elementDeclHandler)
 581#define attlistDeclHandler (parser->m_attlistDeclHandler)
 582#define entityDeclHandler (parser->m_entityDeclHandler)
 583#define xmlDeclHandler (parser->m_xmlDeclHandler)
 584#define encoding (parser->m_encoding)
 585#define initEncoding (parser->m_initEncoding)
 586#define internalEncoding (parser->m_internalEncoding)
 587#define unknownEncodingMem (parser->m_unknownEncodingMem)
 588#define unknownEncodingData (parser->m_unknownEncodingData)
 589#define unknownEncodingHandlerData \
 590  (parser->m_unknownEncodingHandlerData)
 591#define unknownEncodingRelease (parser->m_unknownEncodingRelease)
 592#define protocolEncodingName (parser->m_protocolEncodingName)
 593#define ns (parser->m_ns)
 594#define ns_triplets (parser->m_ns_triplets)
 595#define prologState (parser->m_prologState)
 596#define processor (parser->m_processor)
 597#define errorCode (parser->m_errorCode)
 598#define eventPtr (parser->m_eventPtr)
 599#define eventEndPtr (parser->m_eventEndPtr)
 600#define positionPtr (parser->m_positionPtr)
 601#define position (parser->m_position)
 602#define openInternalEntities (parser->m_openInternalEntities)
 603#define freeInternalEntities (parser->m_freeInternalEntities)
 604#define defaultExpandInternalEntities \
 605        (parser->m_defaultExpandInternalEntities)
 606#define tagLevel (parser->m_tagLevel)
 607#define buffer (parser->m_buffer)
 608#define bufferPtr (parser->m_bufferPtr)
 609#define bufferEnd (parser->m_bufferEnd)
 610#define parseEndByteIndex (parser->m_parseEndByteIndex)
 611#define parseEndPtr (parser->m_parseEndPtr)
 612#define bufferLim (parser->m_bufferLim)
 613#define dataBuf (parser->m_dataBuf)
 614#define dataBufEnd (parser->m_dataBufEnd)
 615#define _dtd (parser->m_dtd)
 616#define curBase (parser->m_curBase)
 617#define declEntity (parser->m_declEntity)
 618#define doctypeName (parser->m_doctypeName)
 619#define doctypeSysid (parser->m_doctypeSysid)
 620#define doctypePubid (parser->m_doctypePubid)
 621#define declAttributeType (parser->m_declAttributeType)
 622#define declNotationName (parser->m_declNotationName)
 623#define declNotationPublicId (parser->m_declNotationPublicId)
 624#define declElementType (parser->m_declElementType)
 625#define declAttributeId (parser->m_declAttributeId)
 626#define declAttributeIsCdata (parser->m_declAttributeIsCdata)
 627#define declAttributeIsId (parser->m_declAttributeIsId)
 628#define freeTagList (parser->m_freeTagList)
 629#define freeBindingList (parser->m_freeBindingList)
 630#define inheritedBindings (parser->m_inheritedBindings)
 631#define tagStack (parser->m_tagStack)
 632#define atts (parser->m_atts)
 633#define attsSize (parser->m_attsSize)
 634#define nSpecifiedAtts (parser->m_nSpecifiedAtts)
 635#define idAttIndex (parser->m_idAttIndex)
 636#define nsAtts (parser->m_nsAtts)
 637#define nsAttsVersion (parser->m_nsAttsVersion)
 638#define nsAttsPower (parser->m_nsAttsPower)
 639#define tempPool (parser->m_tempPool)
 640#define temp2Pool (parser->m_temp2Pool)
 641#define groupConnector (parser->m_groupConnector)
 642#define groupSize (parser->m_groupSize)
 643#define namespaceSeparator (parser->m_namespaceSeparator)
 644#define parentParser (parser->m_parentParser)
 645#define ps_parsing (parser->m_parsingStatus.parsing)
 646#define ps_finalBuffer (parser->m_parsingStatus.finalBuffer)
 647#ifdef XML_DTD
 648#define isParamEntity (parser->m_isParamEntity)
 649#define useForeignDTD (parser->m_useForeignDTD)
 650#define paramEntityParsing (parser->m_paramEntityParsing)
 651#endif /* XML_DTD */
 652
 653XML_Parser XMLCALL
 654XML_ParserCreate(const XML_Char *encodingName)
 655{
 656  return XML_ParserCreate_MM(encodingName, NULL, NULL);
 657}
 658
 659XML_Parser XMLCALL
 660XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
 661{
 662  XML_Char tmp[2];
 663  *tmp = nsSep;
 664  return XML_ParserCreate_MM(encodingName, NULL, tmp);
 665}
 666
 667static const XML_Char implicitContext[] = {
 668  'x', 'm', 'l', '=', 'h', 't', 't', 'p', ':', '/', '/',
 669  'w', 'w', 'w', '.', 'w', '3', '.', 'o', 'r', 'g', '/',
 670  'X', 'M', 'L', '/', '1', '9', '9', '8', '/',
 671  'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\0'
 672};
 673
 674XML_Parser XMLCALL
 675XML_ParserCreate_MM(const XML_Char *encodingName,
 676                    const XML_Memory_Handling_Suite *memsuite,
 677                    const XML_Char *nameSep)
 678{
 679  XML_Parser parser = parserCreate(encodingName, memsuite, nameSep, NULL);
 680  if (parser != NULL && ns) {
 681    /* implicit context only set for root parser, since child
 682       parsers (i.e. external entity parsers) will inherit it
 683    */
 684    if (!setContext(parser, implicitContext)) {
 685      XML_ParserFree(parser);
 686      return NULL;
 687    }
 688  }
 689  return parser;
 690}
 691
 692static XML_Parser
 693parserCreate(const XML_Char *encodingName,
 694             const XML_Memory_Handling_Suite *memsuite,
 695             const XML_Char *nameSep,
 696             DTD *dtd)
 697{
 698  XML_Parser parser;
 699
 700  if (memsuite) {
 701    XML_Memory_Handling_Suite *mtemp;
 702    parser = (XML_Parser)
 703      memsuite->malloc_fcn(sizeof(struct XML_ParserStruct));
 704    if (parser != NULL) {
 705      mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
 706      mtemp->malloc_fcn = memsuite->malloc_fcn;
 707      mtemp->realloc_fcn = memsuite->realloc_fcn;
 708      mtemp->free_fcn = memsuite->free_fcn;
 709    }
 710  }
 711  else {
 712    XML_Memory_Handling_Suite *mtemp;
 713    parser = (XML_Parser)malloc(sizeof(struct XML_ParserStruct));
 714    if (parser != NULL) {
 715      mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
 716      mtemp->malloc_fcn = malloc;
 717      mtemp->realloc_fcn = realloc;
 718      mtemp->free_fcn = free;
 719    }
 720  }
 721
 722  if (!parser)
 723    return parser;
 724
 725  buffer = NULL;
 726  bufferLim = NULL;
 727
 728  attsSize = INIT_ATTS_SIZE;
 729  atts = (ATTRIBUTE *)MALLOC(attsSize * sizeof(ATTRIBUTE));
 730  if (atts == NULL) {
 731    FREE(parser);
 732    return NULL;
 733  }
 734  dataBuf = (XML_Char *)MALLOC(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
 735  if (dataBuf == NULL) {
 736    FREE(atts);
 737    FREE(parser);
 738    return NULL;
 739  }
 740  dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
 741
 742  if (dtd)
 743    _dtd = dtd;
 744  else {
 745    _dtd = dtdCreate(&parser->m_mem);
 746    if (_dtd == NULL) {
 747      FREE(dataBuf);
 748      FREE(atts);
 749      FREE(parser);
 750      return NULL;
 751    }
 752  }
 753
 754  freeBindingList = NULL;
 755  freeTagList = NULL;
 756  freeInternalEntities = NULL;
 757
 758  groupSize = 0;
 759  groupConnector = NULL;
 760
 761  unknownEncodingHandler = NULL;
 762  unknownEncodingHandlerData = NULL;
 763
 764  namespaceSeparator = '!';
 765  ns = XML_FALSE;
 766  ns_triplets = XML_FALSE;
 767
 768  nsAtts = NULL;
 769  nsAttsVersion = 0;
 770  nsAttsPower = 0;
 771
 772  poolInit(&tempPool, &(parser->m_mem));
 773  poolInit(&temp2Pool, &(parser->m_mem));
 774  parserInit(parser, encodingName);
 775
 776  if (encodingName && !protocolEncodingName) {
 777    XML_ParserFree(parser);
 778    return NULL;
 779  }
 780
 781  if (nameSep) {
 782    ns = XML_TRUE;
 783    internalEncoding = XmlGetInternalEncodingNS();
 784    namespaceSeparator = *nameSep;
 785  }
 786  else {
 787    internalEncoding = XmlGetInternalEncoding();
 788  }
 789
 790  return parser;
 791}
 792
 793static void
 794parserInit(XML_Parser parser, const XML_Char *encodingName)
 795{
 796  processor = prologInitProcessor;
 797  XmlPrologStateInit(&prologState);
 798  protocolEncodingName = (encodingName != NULL
 799                          ? poolCopyString(&tempPool, encodingName)
 800                          : NULL);
 801  curBase = NULL;
 802  XmlInitEncoding(&initEncoding, &encoding, 0);
 803  userData = NULL;
 804  handlerArg = NULL;
 805  startElementHandler = NULL;
 806  endElementHandler = NULL;
 807  characterDataHandler = NULL;
 808  processingInstructionHandler = NULL;
 809  commentHandler = NULL;
 810  startCdataSectionHandler = NULL;
 811  endCdataSectionHandler = NULL;
 812  defaultHandler = NULL;
 813  startDoctypeDeclHandler = NULL;
 814  endDoctypeDeclHandler = NULL;
 815  unparsedEntityDeclHandler = NULL;
 816  notationDeclHandler = NULL;
 817  startNamespaceDeclHandler = NULL;
 818  endNamespaceDeclHandler = NULL;
 819  notStandaloneHandler = NULL;
 820  externalEntityRefHandler = NULL;
 821  externalEntityRefHandlerArg = parser;
 822  skippedEntityHandler = NULL;
 823  elementDeclHandler = NULL;
 824  attlistDeclHandler = NULL;
 825  entityDeclHandler = NULL;
 826  xmlDeclHandler = NULL;
 827  bufferPtr = buffer;
 828  bufferEnd = buffer;
 829  parseEndByteIndex = 0;
 830  parseEndPtr = NULL;
 831  declElementType = NULL;
 832  declAttributeId = NULL;
 833  declEntity = NULL;
 834  doctypeName = NULL;
 835  doctypeSysid = NULL;
 836  doctypePubid = NULL;
 837  declAttributeType = NULL;
 838  declNotationName = NULL;
 839  declNotationPublicId = NULL;
 840  declAttributeIsCdata = XML_FALSE;
 841  declAttributeIsId = XML_FALSE;
 842  memset(&position, 0, sizeof(POSITION));
 843  errorCode = XML_ERROR_NONE;
 844  eventPtr = NULL;
 845  eventEndPtr = NULL;
 846  positionPtr = NULL;
 847  openInternalEntities = NULL;
 848  defaultExpandInternalEntities = XML_TRUE;
 849  tagLevel = 0;
 850  tagStack = NULL;
 851  inheritedBindings = NULL;
 852  nSpecifiedAtts = 0;
 853  unknownEncodingMem = NULL;
 854  unknownEncodingRelease = NULL;
 855  unknownEncodingData = NULL;
 856  parentParser = NULL;
 857  ps_parsing = XML_INITIALIZED;
 858#ifdef XML_DTD
 859  isParamEntity = XML_FALSE;
 860  useForeignDTD = XML_FALSE;
 861  paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
 862#endif
 863}
 864
 865/* moves list of bindings to freeBindingList */
 866static void FASTCALL
 867moveToFreeBindingList(XML_Parser parser, BINDING *bindings)
 868{
 869  while (bindings) {
 870    BINDING *b = bindings;
 871    bindings = bindings->nextTagBinding;
 872    b->nextTagBinding = freeBindingList;
 873    freeBindingList = b;
 874  }
 875}
 876
 877XML_Bool XMLCALL
 878XML_ParserReset(XML_Parser parser, const XML_Char *encodingName)
 879{
 880  TAG *tStk;
 881  OPEN_INTERNAL_ENTITY *openEntityList;
 882  if (parentParser)
 883    return XML_FALSE;
 884  /* move tagStack to freeTagList */
 885  tStk = tagStack;
 886  while (tStk) {
 887    TAG *tag = tStk;
 888    tStk = tStk->parent;
 889    tag->parent = freeTagList;
 890    moveToFreeBindingList(parser, tag->bindings);
 891    tag->bindings = NULL;
 892    freeTagList = tag;
 893  }
 894  /* move openInternalEntities to freeInternalEntities */
 895  openEntityList = openInternalEntities;
 896  while (openEntityList) {
 897    OPEN_INTERNAL_ENTITY *openEntity = openEntityList;
 898    openEntityList = openEntity->next;
 899    openEntity->next = freeInternalEntities;
 900    freeInternalEntities = openEntity;
 901  }
 902  moveToFreeBindingList(parser, inheritedBindings);
 903  FREE(unknownEncodingMem);
 904  if (unknownEncodingRelease)
 905    unknownEncodingRelease(unknownEncodingData);
 906  poolClear(&tempPool);
 907  poolClear(&temp2Pool);
 908  parserInit(parser, encodingName);
 909  dtdReset(_dtd, &parser->m_mem);
 910  return setContext(parser, implicitContext);
 911}
 912
 913enum XML_Status XMLCALL
 914XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
 915{
 916  /* Block after XML_Parse()/XML_ParseBuffer() has been called.
 917     XXX There's no way for the caller to determine which of the
 918     XXX possible error cases caused the XML_STATUS_ERROR return.
 919  */
 920  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
 921    return XML_STATUS_ERROR;
 922  if (encodingName == NULL)
 923    protocolEncodingName = NULL;
 924  else {
 925    protocolEncodingName = poolCopyString(&tempPool, encodingName);
 926    if (!protocolEncodingName)
 927      return XML_STATUS_ERROR;
 928  }
 929  return XML_STATUS_OK;
 930}
 931
 932XML_Parser XMLCALL
 933XML_ExternalEntityParserCreate(XML_Parser oldParser,
 934                               const XML_Char *context,
 935                               const XML_Char *encodingName)
 936{
 937  XML_Parser parser = oldParser;
 938  DTD *newDtd = NULL;
 939  DTD *oldDtd = _dtd;
 940  XML_StartElementHandler oldStartElementHandler = startElementHandler;
 941  XML_EndElementHandler oldEndElementHandler = endElementHandler;
 942  XML_CharacterDataHandler oldCharacterDataHandler = characterDataHandler;
 943  XML_ProcessingInstructionHandler oldProcessingInstructionHandler
 944      = processingInstructionHandler;
 945  XML_CommentHandler oldCommentHandler = commentHandler;
 946  XML_StartCdataSectionHandler oldStartCdataSectionHandler
 947      = startCdataSectionHandler;
 948  XML_EndCdataSectionHandler oldEndCdataSectionHandler
 949      = endCdataSectionHandler;
 950  XML_DefaultHandler oldDefaultHandler = defaultHandler;
 951  XML_UnparsedEntityDeclHandler oldUnparsedEntityDeclHandler
 952      = unparsedEntityDeclHandler;
 953  XML_NotationDeclHandler oldNotationDeclHandler = notationDeclHandler;
 954  XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler
 955      = startNamespaceDeclHandler;
 956  XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler
 957      = endNamespaceDeclHandler;
 958  XML_NotStandaloneHandler oldNotStandaloneHandler = notStandaloneHandler;
 959  XML_ExternalEntityRefHandler oldExternalEntityRefHandler
 960      = externalEntityRefHandler;
 961  XML_SkippedEntityHandler oldSkippedEntityHandler = skippedEntityHandler;
 962  XML_UnknownEncodingHandler oldUnknownEncodingHandler
 963      = unknownEncodingHandler;
 964  XML_ElementDeclHandler oldElementDeclHandler = elementDeclHandler;
 965  XML_AttlistDeclHandler oldAttlistDeclHandler = attlistDeclHandler;
 966  XML_EntityDeclHandler oldEntityDeclHandler = entityDeclHandler;
 967  XML_XmlDeclHandler oldXmlDeclHandler = xmlDeclHandler;
 968  ELEMENT_TYPE * oldDeclElementType = declElementType;
 969
 970  void *oldUserData = userData;
 971  void *oldHandlerArg = handlerArg;
 972  XML_Bool oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
 973  XML_Parser oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
 974#ifdef XML_DTD
 975  enum XML_ParamEntityParsing oldParamEntityParsing = paramEntityParsing;
 976  int oldInEntityValue = prologState.inEntityValue;
 977#endif
 978  XML_Bool oldns_triplets = ns_triplets;
 979
 980#ifdef XML_DTD
 981  if (!context)
 982    newDtd = oldDtd;
 983#endif /* XML_DTD */
 984
 985  /* Note that the magical uses of the pre-processor to make field
 986     access look more like C++ require that `parser' be overwritten
 987     here.  This makes this function more painful to follow than it
 988     would be otherwise.
 989  */
 990  if (ns) {
 991    XML_Char tmp[2];
 992    *tmp = namespaceSeparator;
 993    parser = parserCreate(encodingName, &parser->m_mem, tmp, newDtd);
 994  }
 995  else {
 996    parser = parserCreate(encodingName, &parser->m_mem, NULL, newDtd);
 997  }
 998
 999  if (!parser)
1000    return NULL;
1001
1002  startElementHandler = oldStartElementHandler;
1003  endElementHandler = oldEndElementHandler;
1004  characterDataHandler = oldCharacterDataHandler;
1005  processingInstructionHandler = oldProcessingInstructionHandler;
1006  commentHandler = oldCommentHandler;
1007  startCdataSectionHandler = oldStartCdataSectionHandler;
1008  endCdataSectionHandler = oldEndCdataSectionHandler;
1009  defaultHandler = oldDefaultHandler;
1010  unparsedEntityDeclHandler = oldUnparsedEntityDeclHandler;
1011  notationDeclHandler = oldNotationDeclHandler;
1012  startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
1013  endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
1014  notStandaloneHandler = oldNotStandaloneHandler;
1015  externalEntityRefHandler = oldExternalEntityRefHandler;
1016  skippedEntityHandler = oldSkippedEntityHandler;
1017  unknownEncodingHandler = oldUnknownEncodingHandler;
1018  elementDeclHandler = oldElementDeclHandler;
1019  attlistDeclHandler = oldAttlistDeclHandler;
1020  entityDeclHandler = oldEntityDeclHandler;
1021  xmlDeclHandler = oldXmlDeclHandler;
1022  declElementType = oldDeclElementType;
1023  userData = oldUserData;
1024  if (oldUserData == oldHandlerArg)
1025    handlerArg = userData;
1026  else
1027    handlerArg = parser;
1028  if (oldExternalEntityRefHandlerArg != oldParser)
1029    externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
1030  defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
1031  ns_triplets = oldns_triplets;
1032  parentParser = oldParser;
1033#ifdef XML_DTD
1034  paramEntityParsing = oldParamEntityParsing;
1035  prologState.inEntityValue = oldInEntityValue;
1036  if (context) {
1037#endif /* XML_DTD */
1038    if (!dtdCopy(_dtd, oldDtd, &parser->m_mem)
1039      || !setContext(parser, context)) {
1040      XML_ParserFree(parser);
1041      return NULL;
1042    }
1043    processor = externalEntityInitProcessor;
1044#ifdef XML_DTD
1045  }
1046  else {
1047    /* The DTD instance referenced by _dtd is shared between the document's
1048       root parser and external PE parsers, therefore one does not need to
1049       call setContext. In addition, one also *must* not call setContext,
1050       because this would overwrite existing prefix->binding pointers in
1051       _dtd with ones that get destroyed with the external PE parser.
1052       This would leave those prefixes with dangling pointers.
1053    */
1054    isParamEntity = XML_TRUE;
1055    XmlPrologStateInitExternalEntity(&prologState);
1056    processor = externalParEntInitProcessor;
1057  }
1058#endif /* XML_DTD */
1059  return parser;
1060}
1061
1062static void FASTCALL
1063destroyBindings(BINDING *bindings, XML_Parser parser)
1064{
1065  for (;;) {
1066    BINDING *b = bindings;
1067    if (!b)
1068      break;
1069    bindings = b->nextTagBinding;
1070    FREE(b->uri);
1071    FREE(b);
1072  }
1073}
1074
1075void XMLCALL
1076XML_ParserFree(XML_Parser parser)
1077{
1078  TAG *tagList;
1079  OPEN_INTERNAL_ENTITY *entityList;
1080  if (parser == NULL)
1081    return;
1082  /* free tagStack and freeTagList */
1083  tagList = tagStack;
1084  for (;;) {
1085    TAG *p;
1086    if (tagList == NULL) {
1087      if (freeTagList == NULL)
1088        break;
1089      tagList = freeTagList;
1090      freeTagList = NULL;
1091    }
1092    p = tagList;
1093    tagList = tagList->parent;
1094    FREE(p->buf);
1095    destroyBindings(p->bindings, parser);
1096    FREE(p);
1097  }
1098  /* free openInternalEntities and freeInternalEntities */
1099  entityList = openInternalEntities;
1100  for (;;) {
1101    OPEN_INTERNAL_ENTITY *openEntity;
1102    if (entityList == NULL) {
1103      if (freeInternalEntities == NULL)
1104        break;
1105      entityList = freeInternalEntities;
1106      freeInternalEntities = NULL;
1107    }
1108    openEntity = entityList;
1109    entityList = entityList->next;
1110    FREE(openEntity);
1111  }
1112
1113  destroyBindings(freeBindingList, parser);
1114  destroyBindings(inheritedBindings, parser);
1115  poolDestroy(&tempPool);
1116  poolDestroy(&temp2Pool);
1117#ifdef XML_DTD
1118  /* external parameter entity parsers share the DTD structure
1119     parser->m_dtd with the root parser, so we must not destroy it
1120  */
1121  if (!isParamEntity && _dtd)
1122#else
1123  if (_dtd)
1124#endif /* XML_DTD */
1125    dtdDestroy(_dtd, (XML_Bool)!parentParser, &parser->m_mem);
1126  FREE((void *)atts);
1127  FREE(groupConnector);
1128  FREE(buffer);
1129  FREE(dataBuf);
1130  FREE(nsAtts);
1131  FREE(unknownEncodingMem);
1132  if (unknownEncodingRelease)
1133    unknownEncodingRelease(unknownEncodingData);
1134  FREE(parser);
1135}
1136
1137void XMLCALL
1138XML_UseParserAsHandlerArg(XML_Parser parser)
1139{
1140  handlerArg = parser;
1141}
1142
1143enum XML_Error XMLCALL
1144XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD)
1145{
1146#ifdef XML_DTD
1147  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1148  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1149    return XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING;
1150  useForeignDTD = useDTD;
1151  return XML_ERROR_NONE;
1152#else
1153  return XML_ERROR_FEATURE_REQUIRES_XML_DTD;
1154#endif
1155}
1156
1157void XMLCALL
1158XML_SetReturnNSTriplet(XML_Parser parser, int do_nst)
1159{
1160  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1161  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1162    return;
1163  ns_triplets = do_nst ? XML_TRUE : XML_FALSE;
1164}
1165
1166void XMLCALL
1167XML_SetUserData(XML_Parser parser, void *p)
1168{
1169  if (handlerArg == userData)
1170    handlerArg = userData = p;
1171  else
1172    userData = p;
1173}
1174
1175enum XML_Status XMLCALL
1176XML_SetBase(XML_Parser parser, const XML_Char *p)
1177{
1178  if (p) {
1179    p = poolCopyString(&_dtd->pool, p);
1180    if (!p)
1181      return XML_STATUS_ERROR;
1182    curBase = p;
1183  }
1184  else
1185    curBase = NULL;
1186  return XML_STATUS_OK;
1187}
1188
1189const XML_Char * XMLCALL
1190XML_GetBase(XML_Parser parser)
1191{
1192  return curBase;
1193}
1194
1195int XMLCALL
1196XML_GetSpecifiedAttributeCount(XML_Parser parser)
1197{
1198  return nSpecifiedAtts;
1199}
1200
1201int XMLCALL
1202XML_GetIdAttributeIndex(XML_Parser parser)
1203{
1204  return idAttIndex;
1205}
1206
1207void XMLCALL
1208XML_SetElementHandler(XML_Parser parser,
1209                      XML_StartElementHandler start,
1210                      XML_EndElementHandler end)
1211{
1212  startElementHandler = start;
1213  endElementHandler = end;
1214}
1215
1216void XMLCALL
1217XML_SetStartElementHandler(XML_Parser parser,
1218                           XML_StartElementHandler start) {
1219  startElementHandler = start;
1220}
1221
1222void XMLCALL
1223XML_SetEndElementHandler(XML_Parser parser,
1224                         XML_EndElementHandler end) {
1225  endElementHandler = end;
1226}
1227
1228void XMLCALL
1229XML_SetCharacterDataHandler(XML_Parser parser,
1230                            XML_CharacterDataHandler handler)
1231{
1232  characterDataHandler = handler;
1233}
1234
1235void XMLCALL
1236XML_SetProcessingInstructionHandler(XML_Parser parser,
1237                                    XML_ProcessingInstructionHandler handler)
1238{
1239  processingInstructionHandler = handler;
1240}
1241
1242void XMLCALL
1243XML_SetCommentHandler(XML_Parser parser,
1244                      XML_CommentHandler handler)
1245{
1246  commentHandler = handler;
1247}
1248
1249void XMLCALL
1250XML_SetCdataSectionHandler(XML_Parser parser,
1251                           XML_StartCdataSectionHandler start,
1252                           XML_EndCdataSectionHandler end)
1253{
1254  startCdataSectionHandler = start;
1255  endCdataSectionHandler = end;
1256}
1257
1258void XMLCALL
1259XML_SetStartCdataSectionHandler(XML_Parser parser,
1260                                XML_StartCdataSectionHandler start) {
1261  startCdataSectionHandler = start;
1262}
1263
1264void XMLCALL
1265XML_SetEndCdataSectionHandler(XML_Parser parser,
1266                              XML_EndCdataSectionHandler end) {
1267  endCdataSectionHandler = end;
1268}
1269
1270void XMLCALL
1271XML_SetDefaultHandler(XML_Parser parser,
1272                      XML_DefaultHandler handler)
1273{
1274  defaultHandler = handler;
1275  defaultExpandInternalEntities = XML_FALSE;
1276}
1277
1278void XMLCALL
1279XML_SetDefaultHandlerExpand(XML_Parser parser,
1280                            XML_DefaultHandler handler)
1281{
1282  defaultHandler = handler;
1283  defaultExpandInternalEntities = XML_TRUE;
1284}
1285
1286void XMLCALL
1287XML_SetDoctypeDeclHandler(XML_Parser parser,
1288                          XML_StartDoctypeDeclHandler start,
1289                          XML_EndDoctypeDeclHandler end)
1290{
1291  startDoctypeDeclHandler = start;
1292  endDoctypeDeclHandler = end;
1293}
1294
1295void XMLCALL
1296XML_SetStartDoctypeDeclHandler(XML_Parser parser,
1297                               XML_StartDoctypeDeclHandler start) {
1298  startDoctypeDeclHandler = start;
1299}
1300
1301void XMLCALL
1302XML_SetEndDoctypeDeclHandler(XML_Parser parser,
1303                             XML_EndDoctypeDeclHandler end) {
1304  endDoctypeDeclHandler = end;
1305}
1306
1307void XMLCALL
1308XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
1309                                 XML_UnparsedEntityDeclHandler handler)
1310{
1311  unparsedEntityDeclHandler = handler;
1312}
1313
1314void XMLCALL
1315XML_SetNotationDeclHandler(XML_Parser parser,
1316                           XML_NotationDeclHandler handler)
1317{
1318  notationDeclHandler = handler;
1319}
1320
1321void XMLCALL
1322XML_SetNamespaceDeclHandler(XML_Parser parser,
1323                            XML_StartNamespaceDeclHandler start,
1324                            XML_EndNamespaceDeclHandler end)
1325{
1326  startNamespaceDeclHandler = start;
1327  endNamespaceDeclHandler = end;
1328}
1329
1330void XMLCALL
1331XML_SetStartNamespaceDeclHandler(XML_Parser parser,
1332                                 XML_StartNamespaceDeclHandler start) {
1333  startNamespaceDeclHandler = start;
1334}
1335
1336void XMLCALL
1337XML_SetEndNamespaceDeclHandler(XML_Parser parser,
1338                               XML_EndNamespaceDeclHandler end) {
1339  endNamespaceDeclHandler = end;
1340}
1341
1342void XMLCALL
1343XML_SetNotStandaloneHandler(XML_Parser parser,
1344                            XML_NotStandaloneHandler handler)
1345{
1346  notStandaloneHandler = handler;
1347}
1348
1349void XMLCALL
1350XML_SetExternalEntityRefHandler(XML_Parser parser,
1351                                XML_ExternalEntityRefHandler handler)
1352{
1353  externalEntityRefHandler = handler;
1354}
1355
1356void XMLCALL
1357XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
1358{
1359  if (arg)
1360    externalEntityRefHandlerArg = (XML_Parser)arg;
1361  else
1362    externalEntityRefHandlerArg = parser;
1363}
1364
1365void XMLCALL
1366XML_SetSkippedEntityHandler(XML_Parser parser,
1367                            XML_SkippedEntityHandler handler)
1368{
1369  skippedEntityHandler = handler;
1370}
1371
1372void XMLCALL
1373XML_SetUnknownEncodingHandler(XML_Parser parser,
1374                              XML_UnknownEncodingHandler handler,
1375                              void *data)
1376{
1377  unknownEncodingHandler = handler;
1378  unknownEncodingHandlerData = data;
1379}
1380
1381void XMLCALL
1382XML_SetElementDeclHandler(XML_Parser parser,
1383                          XML_ElementDeclHandler eldecl)
1384{
1385  elementDeclHandler = eldecl;
1386}
1387
1388void XMLCALL
1389XML_SetAttlistDeclHandler(XML_Parser parser,
1390                          XML_AttlistDeclHandler attdecl)
1391{
1392  attlistDeclHandler = attdecl;
1393}
1394
1395void XMLCALL
1396XML_SetEntityDeclHandler(XML_Parser parser,
1397                         XML_EntityDeclHandler handler)
1398{
1399  entityDeclHandler = handler;
1400}
1401
1402void XMLCALL
1403XML_SetXmlDeclHandler(XML_Parser parser,
1404                      XML_XmlDeclHandler handler) {
1405  xmlDeclHandler = handler;
1406}
1407
1408int XMLCALL
1409XML_SetParamEntityParsing(XML_Parser parser,
1410                          enum XML_ParamEntityParsing peParsing)
1411{
1412  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1413  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1414    return 0;
1415#ifdef XML_DTD
1416  paramEntityParsing = peParsing;
1417  return 1;
1418#else
1419  return peParsing == XML_PARAM_ENTITY_PARSING_NEVER;
1420#endif
1421}
1422
1423enum XML_Status XMLCALL
1424XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
1425{
1426  switch (ps_parsing) {
1427  case XML_SUSPENDED:
1428    errorCode = XML_ERROR_SUSPENDED;
1429    return XML_STATUS_ERROR;
1430  case XML_FINISHED:
1431    errorCode = XML_ERROR_FINISHED;
1432    return XML_STATUS_ERROR;
1433  default:
1434    ps_parsing = XML_PARSING;
1435  }
1436
1437  if (len == 0) {
1438    ps_finalBuffer = (XML_Bool)isFinal;
1439    if (!isFinal)
1440      return XML_STATUS_OK;
1441    positionPtr = bufferPtr;
1442    parseEndPtr = bufferEnd;
1443
1444    /* If data are left over from last buffer, and we now know that these
1445       data are the final chunk of input, then we have to check them again
1446       to detect errors based on that fact.
1447    */
1448    errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
1449
1450    if (errorCode == XML_ERROR_NONE) {
1451      switch (ps_parsing) {
1452      case XML_SUSPENDED:
1453        XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
1454        positionPtr = bufferPtr;
1455        return XML_STATUS_SUSPENDED;
1456      case XML_INITIALIZED: 
1457      case XML_PARSING:
1458        ps_parsing = XML_FINISHED;
1459        /* fall through */
1460      default:
1461        return XML_STATUS_OK;
1462      }
1463    }
1464    eventEndPtr = eventPtr;
1465    processor = errorProcessor;
1466    return XML_STATUS_ERROR;
1467  }
1468#ifndef XML_CONTEXT_BYTES
1469  else if (bufferPtr == bufferEnd) {
1470    const char *end;
1471    int nLeftOver;
1472    enum XML_Error result;
1473    parseEndByteIndex += len;
1474    positionPtr = s;
1475    ps_finalBuffer = (XML_Bool)isFinal;
1476
1477    errorCode = processor(parser, s, parseEndPtr = s + len, &end);
1478
1479    if (errorCode != XML_ERROR_NONE) {
1480      eventEndPtr = eventPtr;
1481      processor = errorProcessor;
1482      return XML_STATUS_ERROR;
1483    }
1484    else {
1485      switch (ps_parsing) {
1486      case XML_SUSPENDED:
1487        result = XML_STATUS_SUSPENDED;
1488        break;
1489      case XML_INITIALIZED:
1490      case XML_PARSING:
1491        result = XML_STATUS_OK;
1492        if (isFinal) {
1493          ps_parsing = XML_FINISHED;
1494          return result;
1495        }
1496      }
1497    }
1498
1499    XmlUpdatePosition(encoding, positionPtr, end, &position);
1500    nLeftOver = s + len - end;
1501    if (nLeftOver) {
1502      if (buffer == NULL || nLeftOver > bufferLim - buffer) {
1503        /* FIXME avoid integer overflow */
1504        char *temp;
1505        temp = (buffer == NULL
1506                ? (char *)MALLOC(len * 2)
1507                : (char *)REALLOC(buffer, len * 2));
1508        if (temp == NULL) {
1509          errorCode = XML_ERROR_NO_MEMORY;
1510          return XML_STATUS_ERROR;
1511        }
1512        buffer = temp;
1513        if (!buffer) {
1514          errorCode = XML_ERROR_NO_MEMORY;
1515          eventPtr = eventEndPtr = NULL;
1516          processor = errorProcessor;
1517          return XML_STATUS_ERROR;
1518        }
1519        bufferLim = buffer + len * 2;
1520      }
1521      memcpy(buffer, end, nLeftOver);
1522    }
1523    bufferPtr = buffer;
1524    bufferEnd = buffer + nLeftOver;
1525    positionPtr = bufferPtr;
1526    parseEndPtr = bufferEnd;
1527    eventPtr = bufferPtr;
1528    eventEndPtr = bufferPtr;
1529    return result;
1530  }
1531#endif  /* not defined XML_CONTEXT_BYTES */
1532  else {
1533    void *buff = XML_GetBuffer(parser, len);
1534    if (buff == NULL)
1535      return XML_STATUS_ERROR;
1536    else {
1537      memcpy(buff, s, len);
1538      return XML_ParseBuffer(parser, len, isFinal);
1539    }
1540  }
1541}
1542
1543enum XML_Status XMLCALL
1544XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
1545{
1546  const char *start;
1547  enum XML_Status result = XML_STATUS_OK;
1548
1549  switch (ps_parsing) {
1550  case XML_SUSPENDED:
1551    errorCode = XML_ERROR_SUSPENDED;
1552    return XML_STATUS_ERROR;
1553  case XML_FINISHED:
1554    errorCode = XML_ERROR_FINISHED;
1555    return XML_STATUS_ERROR;
1556  default:
1557    ps_parsing = XML_PARSING;
1558  }
1559
1560  start = bufferPtr;
1561  positionPtr = start;
1562  bufferEnd += len;
1563  parseEndPtr = bufferEnd;
1564  parseEndByteIndex += len;
1565  ps_finalBuffer = (XML_Bool)isFinal;
1566
1567  errorCode = processor(parser, start, parseEndPtr, &bufferPtr);
1568
1569  if (errorCode != XML_ERROR_NONE) {
1570    eventEndPtr = eventPtr;
1571    processor = errorProcessor;
1572    return XML_STATUS_ERROR;
1573  }
1574  else {
1575    switch (ps_parsing) {
1576    case XML_SUSPENDED:
1577      result = XML_STATUS_SUSPENDED;
1578      break;
1579    case XML_INITIALIZED: 
1580    case XML_PARSING:
1581      if (isFinal) {
1582        ps_parsing = XML_FINISHED;
1583        return result;
1584      }
1585    default: ;  /* should not happen */
1586    }
1587  }
1588
1589  XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
1590  positionPtr = bufferPtr;
1591  return result;
1592}
1593
1594void * XMLCALL
1595XML_GetBuffer(XML_Parser parser, int len)
1596{
1597  switch (ps_parsing) {
1598  case XML_SUSPENDED:
1599    errorCode = XML_ERROR_SUSPENDED;
1600    return NULL;
1601  case XML_FINISHED:
1602    errorCode = XML_ERROR_FINISHED;
1603    return NULL;
1604  default: ;
1605  }
1606
1607  if (len > bufferLim - bufferEnd) {
1608    /* FIXME avoid integer overflow */
1609    int neededSize = len + (int)(bufferEnd - bufferPtr);
1610#ifdef XML_CONTEXT_BYTES
1611    int keep = (int)(bufferPtr - buffer);
1612
1613    if (keep > XML_CONTEXT_BYTES)
1614      keep = XML_CONTEXT_BYTES;
1615    neededSize += keep;
1616#endif  /* defined XML_CONTEXT_BYTES */
1617    if (neededSize  <= bufferLim - buffer) {
1618#ifdef XML_CONTEXT_BYTES
1619      if (keep < bufferPtr - buffer) {
1620        int offset = (int)(bufferPtr - buffer) - keep;
1621        memmove(buffer, &buffer[offset], bufferEnd - bufferPtr + keep);
1622        bufferEnd -= offset;
1623        bufferPtr -= offset;
1624      }
1625#else
1626      memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
1627      bufferEnd = buffer + (bufferEnd - bufferPtr);
1628      bufferPtr = buffer;
1629#endif  /* not defined XML_CONTEXT_BYTES */
1630    }
1631    else {
1632      char *newBuf;
1633      int bufferSize = (int)(bufferLim - bufferPtr);
1634      if (bufferSize == 0)
1635        bufferSize = INIT_BUFFER_SIZE;
1636      do {
1637        bufferSize *= 2;
1638      } while (bufferSize < neededSize);
1639      newBuf = (char *)MALLOC(bufferSize);
1640      if (newBuf == 0) {
1641        errorCode = XML_ERROR_NO_MEMORY;
1642        return NULL;
1643      }
1644      bufferLim = newBuf + bufferSize;
1645#ifdef XML_CONTEXT_BYTES
1646      if (bufferPtr) {
1647        int keep = (int)(bufferPtr - buffer);
1648        if (keep > XML_CONTEXT_BYTES)
1649          keep = XML_CONTEXT_BYTES;
1650        memcpy(newBuf, &bufferPtr[-keep], bufferEnd - bufferPtr + keep);
1651        FREE(buffer);
1652        buffer = newBuf;
1653        bufferEnd = buffer + (bufferEnd - bufferPtr) + keep;
1654        buffer

Large files files are truncated, but you can click here to view the full file