Home | History | Annotate | Line # | Download | only in lib
xmlparse.c revision 1.2
      1  1.1  tron /* Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
      2  1.1  tron    See the file COPYING for copying permission.
      3  1.1  tron */
      4  1.1  tron 
      5  1.1  tron #include <stddef.h>
      6  1.1  tron #include <string.h>                     /* memset(), memcpy() */
      7  1.1  tron #include <assert.h>
      8  1.1  tron 
      9  1.1  tron #define XML_BUILDING_EXPAT 1
     10  1.1  tron 
     11  1.1  tron #ifdef COMPILED_FROM_DSP
     12  1.1  tron #include "winconfig.h"
     13  1.1  tron #elif defined(MACOS_CLASSIC)
     14  1.1  tron #include "macconfig.h"
     15  1.1  tron #elif defined(__amigaos4__)
     16  1.1  tron #include "amigaconfig.h"
     17  1.1  tron #elif defined(__WATCOMC__)
     18  1.1  tron #include "watcomconfig.h"
     19  1.1  tron #elif defined(HAVE_EXPAT_CONFIG_H)
     20  1.1  tron #include <expat_config.h>
     21  1.1  tron #endif /* ndef COMPILED_FROM_DSP */
     22  1.1  tron 
     23  1.1  tron #include "ascii.h"
     24  1.1  tron #include "expat.h"
     25  1.1  tron 
     26  1.1  tron #ifdef XML_UNICODE
     27  1.1  tron #define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
     28  1.1  tron #define XmlConvert XmlUtf16Convert
     29  1.1  tron #define XmlGetInternalEncoding XmlGetUtf16InternalEncoding
     30  1.1  tron #define XmlGetInternalEncodingNS XmlGetUtf16InternalEncodingNS
     31  1.1  tron #define XmlEncode XmlUtf16Encode
     32  1.1  tron /* Using pointer subtraction to convert to integer type. */
     33  1.1  tron #define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((char *)(s) - (char *)NULL) & 1))
     34  1.1  tron typedef unsigned short ICHAR;
     35  1.1  tron #else
     36  1.1  tron #define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
     37  1.1  tron #define XmlConvert XmlUtf8Convert
     38  1.1  tron #define XmlGetInternalEncoding XmlGetUtf8InternalEncoding
     39  1.1  tron #define XmlGetInternalEncodingNS XmlGetUtf8InternalEncodingNS
     40  1.1  tron #define XmlEncode XmlUtf8Encode
     41  1.1  tron #define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
     42  1.1  tron typedef char ICHAR;
     43  1.1  tron #endif
     44  1.1  tron 
     45  1.1  tron 
     46  1.1  tron #ifndef XML_NS
     47  1.1  tron 
     48  1.1  tron #define XmlInitEncodingNS XmlInitEncoding
     49  1.1  tron #define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
     50  1.1  tron #undef XmlGetInternalEncodingNS
     51  1.1  tron #define XmlGetInternalEncodingNS XmlGetInternalEncoding
     52  1.1  tron #define XmlParseXmlDeclNS XmlParseXmlDecl
     53  1.1  tron 
     54  1.1  tron #endif
     55  1.1  tron 
     56  1.1  tron #ifdef XML_UNICODE
     57  1.1  tron 
     58  1.1  tron #ifdef XML_UNICODE_WCHAR_T
     59  1.1  tron #define XML_T(x) (const wchar_t)x
     60  1.1  tron #define XML_L(x) L ## x
     61  1.1  tron #else
     62  1.1  tron #define XML_T(x) (const unsigned short)x
     63  1.1  tron #define XML_L(x) x
     64  1.1  tron #endif
     65  1.1  tron 
     66  1.1  tron #else
     67  1.1  tron 
     68  1.1  tron #define XML_T(x) x
     69  1.1  tron #define XML_L(x) x
     70  1.1  tron 
     71  1.1  tron #endif
     72  1.1  tron 
     73  1.1  tron /* Round up n to be a multiple of sz, where sz is a power of 2. */
     74  1.1  tron #define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
     75  1.1  tron 
     76  1.1  tron /* Handle the case where memmove() doesn't exist. */
     77  1.1  tron #ifndef HAVE_MEMMOVE
     78  1.1  tron #ifdef HAVE_BCOPY
     79  1.1  tron #define memmove(d,s,l) bcopy((s),(d),(l))
     80  1.1  tron #else
     81  1.1  tron #error memmove does not exist on this platform, nor is a substitute available
     82  1.1  tron #endif /* HAVE_BCOPY */
     83  1.1  tron #endif /* HAVE_MEMMOVE */
     84  1.1  tron 
     85  1.1  tron #include "internal.h"
     86  1.1  tron #include "xmltok.h"
     87  1.1  tron #include "xmlrole.h"
     88  1.1  tron 
     89  1.1  tron typedef const XML_Char *KEY;
     90  1.1  tron 
     91  1.1  tron typedef struct {
     92  1.1  tron   KEY name;
     93  1.1  tron } NAMED;
     94  1.1  tron 
     95  1.1  tron typedef struct {
     96  1.1  tron   NAMED **v;
     97  1.1  tron   unsigned char power;
     98  1.1  tron   size_t size;
     99  1.1  tron   size_t used;
    100  1.1  tron   const XML_Memory_Handling_Suite *mem;
    101  1.1  tron } HASH_TABLE;
    102  1.1  tron 
    103  1.1  tron /* Basic character hash algorithm, taken from Python's string hash:
    104  1.1  tron    h = h * 1000003 ^ character, the constant being a prime number.
    105  1.1  tron 
    106  1.1  tron */
    107  1.1  tron #ifdef XML_UNICODE
    108  1.1  tron #define CHAR_HASH(h, c) \
    109  1.1  tron   (((h) * 0xF4243) ^ (unsigned short)(c))
    110  1.1  tron #else
    111  1.1  tron #define CHAR_HASH(h, c) \
    112  1.1  tron   (((h) * 0xF4243) ^ (unsigned char)(c))
    113  1.1  tron #endif
    114  1.1  tron 
    115  1.1  tron /* For probing (after a collision) we need a step size relative prime
    116  1.1  tron    to the hash table size, which is a power of 2. We use double-hashing,
    117  1.1  tron    since we can calculate a second hash value cheaply by taking those bits
    118  1.1  tron    of the first hash value that were discarded (masked out) when the table
    119  1.1  tron    index was calculated: index = hash & mask, where mask = table->size - 1.
    120  1.1  tron    We limit the maximum step size to table->size / 4 (mask >> 2) and make
    121  1.1  tron    it odd, since odd numbers are always relative prime to a power of 2.
    122  1.1  tron */
    123  1.1  tron #define SECOND_HASH(hash, mask, power) \
    124  1.1  tron   ((((hash) & ~(mask)) >> ((power) - 1)) & ((mask) >> 2))
    125  1.1  tron #define PROBE_STEP(hash, mask, power) \
    126  1.1  tron   ((unsigned char)((SECOND_HASH(hash, mask, power)) | 1))
    127  1.1  tron 
    128  1.1  tron typedef struct {
    129  1.1  tron   NAMED **p;
    130  1.1  tron   NAMED **end;
    131  1.1  tron } HASH_TABLE_ITER;
    132  1.1  tron 
    133  1.1  tron #define INIT_TAG_BUF_SIZE 32  /* must be a multiple of sizeof(XML_Char) */
    134  1.1  tron #define INIT_DATA_BUF_SIZE 1024
    135  1.1  tron #define INIT_ATTS_SIZE 16
    136  1.1  tron #define INIT_ATTS_VERSION 0xFFFFFFFF
    137  1.1  tron #define INIT_BLOCK_SIZE 1024
    138  1.1  tron #define INIT_BUFFER_SIZE 1024
    139  1.1  tron 
    140  1.1  tron #define EXPAND_SPARE 24
    141  1.1  tron 
    142  1.1  tron typedef struct binding {
    143  1.1  tron   struct prefix *prefix;
    144  1.1  tron   struct binding *nextTagBinding;
    145  1.1  tron   struct binding *prevPrefixBinding;
    146  1.1  tron   const struct attribute_id *attId;
    147  1.1  tron   XML_Char *uri;
    148  1.1  tron   int uriLen;
    149  1.1  tron   int uriAlloc;
    150  1.1  tron } BINDING;
    151  1.1  tron 
    152  1.1  tron typedef struct prefix {
    153  1.1  tron   const XML_Char *name;
    154  1.1  tron   BINDING *binding;
    155  1.1  tron } PREFIX;
    156  1.1  tron 
    157  1.1  tron typedef struct {
    158  1.1  tron   const XML_Char *str;
    159  1.1  tron   const XML_Char *localPart;
    160  1.1  tron   const XML_Char *prefix;
    161  1.1  tron   int strLen;
    162  1.1  tron   int uriLen;
    163  1.1  tron   int prefixLen;
    164  1.1  tron } TAG_NAME;
    165  1.1  tron 
    166  1.1  tron /* TAG represents an open element.
    167  1.1  tron    The name of the element is stored in both the document and API
    168  1.1  tron    encodings.  The memory buffer 'buf' is a separately-allocated
    169  1.1  tron    memory area which stores the name.  During the XML_Parse()/
    170  1.1  tron    XMLParseBuffer() when the element is open, the memory for the 'raw'
    171  1.1  tron    version of the name (in the document encoding) is shared with the
    172  1.1  tron    document buffer.  If the element is open across calls to
    173  1.1  tron    XML_Parse()/XML_ParseBuffer(), the buffer is re-allocated to
    174  1.1  tron    contain the 'raw' name as well.
    175  1.1  tron 
    176  1.1  tron    A parser re-uses these structures, maintaining a list of allocated
    177  1.1  tron    TAG objects in a free list.
    178  1.1  tron */
    179  1.1  tron typedef struct tag {
    180  1.1  tron   struct tag *parent;           /* parent of this element */
    181  1.1  tron   const char *rawName;          /* tagName in the original encoding */
    182  1.1  tron   int rawNameLength;
    183  1.1  tron   TAG_NAME name;                /* tagName in the API encoding */
    184  1.1  tron   char *buf;                    /* buffer for name components */
    185  1.1  tron   char *bufEnd;                 /* end of the buffer */
    186  1.1  tron   BINDING *bindings;
    187  1.1  tron } TAG;
    188  1.1  tron 
    189  1.1  tron typedef struct {
    190  1.1  tron   const XML_Char *name;
    191  1.1  tron   const XML_Char *textPtr;
    192  1.1  tron   int textLen;                  /* length in XML_Chars */
    193  1.1  tron   int processed;                /* # of processed bytes - when suspended */
    194  1.1  tron   const XML_Char *systemId;
    195  1.1  tron   const XML_Char *base;
    196  1.1  tron   const XML_Char *publicId;
    197  1.1  tron   const XML_Char *notation;
    198  1.1  tron   XML_Bool open;
    199  1.1  tron   XML_Bool is_param;
    200  1.1  tron   XML_Bool is_internal; /* true if declared in internal subset outside PE */
    201  1.1  tron } ENTITY;
    202  1.1  tron 
    203  1.1  tron typedef struct {
    204  1.1  tron   enum XML_Content_Type         type;
    205  1.1  tron   enum XML_Content_Quant        quant;
    206  1.1  tron   const XML_Char *              name;
    207  1.1  tron   int                           firstchild;
    208  1.1  tron   int                           lastchild;
    209  1.1  tron   int                           childcnt;
    210  1.1  tron   int                           nextsib;
    211  1.1  tron } CONTENT_SCAFFOLD;
    212  1.1  tron 
    213  1.1  tron #define INIT_SCAFFOLD_ELEMENTS 32
    214  1.1  tron 
    215  1.1  tron typedef struct block {
    216  1.1  tron   struct block *next;
    217  1.1  tron   int size;
    218  1.1  tron   XML_Char s[1];
    219  1.1  tron } BLOCK;
    220  1.1  tron 
    221  1.1  tron typedef struct {
    222  1.1  tron   BLOCK *blocks;
    223  1.1  tron   BLOCK *freeBlocks;
    224  1.1  tron   const XML_Char *end;
    225  1.1  tron   XML_Char *ptr;
    226  1.1  tron   XML_Char *start;
    227  1.1  tron   const XML_Memory_Handling_Suite *mem;
    228  1.1  tron } STRING_POOL;
    229  1.1  tron 
    230  1.1  tron /* The XML_Char before the name is used to determine whether
    231  1.1  tron    an attribute has been specified. */
    232  1.1  tron typedef struct attribute_id {
    233  1.1  tron   XML_Char *name;
    234  1.1  tron   PREFIX *prefix;
    235  1.1  tron   XML_Bool maybeTokenized;
    236  1.1  tron   XML_Bool xmlns;
    237  1.1  tron } ATTRIBUTE_ID;
    238  1.1  tron 
    239  1.1  tron typedef struct {
    240  1.1  tron   const ATTRIBUTE_ID *id;
    241  1.1  tron   XML_Bool isCdata;
    242  1.1  tron   const XML_Char *value;
    243  1.1  tron } DEFAULT_ATTRIBUTE;
    244  1.1  tron 
    245  1.1  tron typedef struct {
    246  1.1  tron   unsigned long version;
    247  1.1  tron   unsigned long hash;
    248  1.1  tron   const XML_Char *uriName;
    249  1.1  tron } NS_ATT;
    250  1.1  tron 
    251  1.1  tron typedef struct {
    252  1.1  tron   const XML_Char *name;
    253  1.1  tron   PREFIX *prefix;
    254  1.1  tron   const ATTRIBUTE_ID *idAtt;
    255  1.1  tron   int nDefaultAtts;
    256  1.1  tron   int allocDefaultAtts;
    257  1.1  tron   DEFAULT_ATTRIBUTE *defaultAtts;
    258  1.1  tron } ELEMENT_TYPE;
    259  1.1  tron 
    260  1.1  tron typedef struct {
    261  1.1  tron   HASH_TABLE generalEntities;
    262  1.1  tron   HASH_TABLE elementTypes;
    263  1.1  tron   HASH_TABLE attributeIds;
    264  1.1  tron   HASH_TABLE prefixes;
    265  1.1  tron   STRING_POOL pool;
    266  1.1  tron   STRING_POOL entityValuePool;
    267  1.1  tron   /* false once a parameter entity reference has been skipped */
    268  1.1  tron   XML_Bool keepProcessing;
    269  1.1  tron   /* true once an internal or external PE reference has been encountered;
    270  1.1  tron      this includes the reference to an external subset */
    271  1.1  tron   XML_Bool hasParamEntityRefs;
    272  1.1  tron   XML_Bool standalone;
    273  1.1  tron #ifdef XML_DTD
    274  1.1  tron   /* indicates if external PE has been read */
    275  1.1  tron   XML_Bool paramEntityRead;
    276  1.1  tron   HASH_TABLE paramEntities;
    277  1.1  tron #endif /* XML_DTD */
    278  1.1  tron   PREFIX defaultPrefix;
    279  1.1  tron   /* === scaffolding for building content model === */
    280  1.1  tron   XML_Bool in_eldecl;
    281  1.1  tron   CONTENT_SCAFFOLD *scaffold;
    282  1.1  tron   unsigned contentStringLen;
    283  1.1  tron   unsigned scaffSize;
    284  1.1  tron   unsigned scaffCount;
    285  1.1  tron   int scaffLevel;
    286  1.1  tron   int *scaffIndex;
    287  1.1  tron } DTD;
    288  1.1  tron 
    289  1.1  tron typedef struct open_internal_entity {
    290  1.1  tron   const char *internalEventPtr;
    291  1.1  tron   const char *internalEventEndPtr;
    292  1.1  tron   struct open_internal_entity *next;
    293  1.1  tron   ENTITY *entity;
    294  1.1  tron   int startTagLevel;
    295  1.1  tron   XML_Bool betweenDecl; /* WFC: PE Between Declarations */
    296  1.1  tron } OPEN_INTERNAL_ENTITY;
    297  1.1  tron 
    298  1.1  tron typedef enum XML_Error PTRCALL Processor(XML_Parser parser,
    299  1.1  tron                                          const char *start,
    300  1.1  tron                                          const char *end,
    301  1.1  tron                                          const char **endPtr);
    302  1.1  tron 
    303  1.1  tron static Processor prologProcessor;
    304  1.1  tron static Processor prologInitProcessor;
    305  1.1  tron static Processor contentProcessor;
    306  1.1  tron static Processor cdataSectionProcessor;
    307  1.1  tron #ifdef XML_DTD
    308  1.1  tron static Processor ignoreSectionProcessor;
    309  1.1  tron static Processor externalParEntProcessor;
    310  1.1  tron static Processor externalParEntInitProcessor;
    311  1.1  tron static Processor entityValueProcessor;
    312  1.1  tron static Processor entityValueInitProcessor;
    313  1.1  tron #endif /* XML_DTD */
    314  1.1  tron static Processor epilogProcessor;
    315  1.1  tron static Processor errorProcessor;
    316  1.1  tron static Processor externalEntityInitProcessor;
    317  1.1  tron static Processor externalEntityInitProcessor2;
    318  1.1  tron static Processor externalEntityInitProcessor3;
    319  1.1  tron static Processor externalEntityContentProcessor;
    320  1.1  tron static Processor internalEntityProcessor;
    321  1.1  tron 
    322  1.1  tron static enum XML_Error
    323  1.1  tron handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName);
    324  1.1  tron static enum XML_Error
    325  1.1  tron processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
    326  1.1  tron                const char *s, const char *next);
    327  1.1  tron static enum XML_Error
    328  1.1  tron initializeEncoding(XML_Parser parser);
    329  1.1  tron static enum XML_Error
    330  1.1  tron doProlog(XML_Parser parser, const ENCODING *enc, const char *s,
    331  1.1  tron          const char *end, int tok, const char *next, const char **nextPtr,
    332  1.1  tron          XML_Bool haveMore);
    333  1.1  tron static enum XML_Error
    334  1.1  tron processInternalEntity(XML_Parser parser, ENTITY *entity,
    335  1.1  tron                       XML_Bool betweenDecl);
    336  1.1  tron static enum XML_Error
    337  1.1  tron doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc,
    338  1.1  tron           const char *start, const char *end, const char **endPtr,
    339  1.1  tron           XML_Bool haveMore);
    340  1.1  tron static enum XML_Error
    341  1.1  tron doCdataSection(XML_Parser parser, const ENCODING *, const char **startPtr,
    342  1.1  tron                const char *end, const char **nextPtr, XML_Bool haveMore);
    343  1.1  tron #ifdef XML_DTD
    344  1.1  tron static enum XML_Error
    345  1.1  tron doIgnoreSection(XML_Parser parser, const ENCODING *, const char **startPtr,
    346  1.1  tron                 const char *end, const char **nextPtr, XML_Bool haveMore);
    347  1.1  tron #endif /* XML_DTD */
    348  1.1  tron 
    349  1.1  tron static enum XML_Error
    350  1.1  tron storeAtts(XML_Parser parser, const ENCODING *, const char *s,
    351  1.1  tron           TAG_NAME *tagNamePtr, BINDING **bindingsPtr);
    352  1.1  tron static enum XML_Error
    353  1.1  tron addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
    354  1.1  tron            const XML_Char *uri, BINDING **bindingsPtr);
    355  1.1  tron static int
    356  1.1  tron defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *, XML_Bool isCdata,
    357  1.1  tron                 XML_Bool isId, const XML_Char *dfltValue, XML_Parser parser);
    358  1.1  tron static enum XML_Error
    359  1.1  tron storeAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
    360  1.1  tron                     const char *, const char *, STRING_POOL *);
    361  1.1  tron static enum XML_Error
    362  1.1  tron appendAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
    363  1.1  tron                      const char *, const char *, STRING_POOL *);
    364  1.1  tron static ATTRIBUTE_ID *
    365  1.1  tron getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start,
    366  1.1  tron                const char *end);
    367  1.1  tron static int
    368  1.1  tron setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *);
    369  1.1  tron static enum XML_Error
    370  1.1  tron storeEntityValue(XML_Parser parser, const ENCODING *enc, const char *start,
    371  1.1  tron                  const char *end);
    372  1.1  tron static int
    373  1.1  tron reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
    374  1.1  tron                             const char *start, const char *end);
    375  1.1  tron static int
    376  1.1  tron reportComment(XML_Parser parser, const ENCODING *enc, const char *start,
    377  1.1  tron               const char *end);
    378  1.1  tron static void
    379  1.1  tron reportDefault(XML_Parser parser, const ENCODING *enc, const char *start,
    380  1.1  tron               const char *end);
    381  1.1  tron 
    382  1.1  tron static const XML_Char * getContext(XML_Parser parser);
    383  1.1  tron static XML_Bool
    384  1.1  tron setContext(XML_Parser parser, const XML_Char *context);
    385  1.1  tron 
    386  1.1  tron static void FASTCALL normalizePublicId(XML_Char *s);
    387  1.1  tron 
    388  1.1  tron static DTD * dtdCreate(const XML_Memory_Handling_Suite *ms);
    389  1.1  tron /* do not call if parentParser != NULL */
    390  1.1  tron static void dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms);
    391  1.1  tron static void
    392  1.1  tron dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms);
    393  1.1  tron static int
    394  1.1  tron dtdCopy(DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms);
    395  1.1  tron static int
    396  1.1  tron copyEntityTable(HASH_TABLE *, STRING_POOL *, const HASH_TABLE *);
    397  1.1  tron 
    398  1.1  tron static NAMED *
    399  1.1  tron lookup(HASH_TABLE *table, KEY name, size_t createSize);
    400  1.1  tron static void FASTCALL
    401  1.1  tron hashTableInit(HASH_TABLE *, const XML_Memory_Handling_Suite *ms);
    402  1.1  tron static void FASTCALL hashTableClear(HASH_TABLE *);
    403  1.1  tron static void FASTCALL hashTableDestroy(HASH_TABLE *);
    404  1.1  tron static void FASTCALL
    405  1.1  tron hashTableIterInit(HASH_TABLE_ITER *, const HASH_TABLE *);
    406  1.1  tron static NAMED * FASTCALL hashTableIterNext(HASH_TABLE_ITER *);
    407  1.1  tron 
    408  1.1  tron static void FASTCALL
    409  1.1  tron poolInit(STRING_POOL *, const XML_Memory_Handling_Suite *ms);
    410  1.1  tron static void FASTCALL poolClear(STRING_POOL *);
    411  1.1  tron static void FASTCALL poolDestroy(STRING_POOL *);
    412  1.1  tron static XML_Char *
    413  1.1  tron poolAppend(STRING_POOL *pool, const ENCODING *enc,
    414  1.1  tron            const char *ptr, const char *end);
    415  1.1  tron static XML_Char *
    416  1.1  tron poolStoreString(STRING_POOL *pool, const ENCODING *enc,
    417  1.1  tron                 const char *ptr, const char *end);
    418  1.1  tron static XML_Bool FASTCALL poolGrow(STRING_POOL *pool);
    419  1.1  tron static const XML_Char * FASTCALL
    420  1.1  tron poolCopyString(STRING_POOL *pool, const XML_Char *s);
    421  1.1  tron static const XML_Char *
    422  1.1  tron poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n);
    423  1.1  tron static const XML_Char * FASTCALL
    424  1.1  tron poolAppendString(STRING_POOL *pool, const XML_Char *s);
    425  1.1  tron 
    426  1.1  tron static int FASTCALL nextScaffoldPart(XML_Parser parser);
    427  1.1  tron static XML_Content * build_model(XML_Parser parser);
    428  1.1  tron static ELEMENT_TYPE *
    429  1.1  tron getElementType(XML_Parser parser, const ENCODING *enc,
    430  1.1  tron                const char *ptr, const char *end);
    431  1.1  tron 
    432  1.1  tron static XML_Parser
    433  1.1  tron parserCreate(const XML_Char *encodingName,
    434  1.1  tron              const XML_Memory_Handling_Suite *memsuite,
    435  1.1  tron              const XML_Char *nameSep,
    436  1.1  tron              DTD *dtd);
    437  1.1  tron static void
    438  1.1  tron parserInit(XML_Parser parser, const XML_Char *encodingName);
    439  1.1  tron 
    440  1.1  tron #define poolStart(pool) ((pool)->start)
    441  1.1  tron #define poolEnd(pool) ((pool)->ptr)
    442  1.1  tron #define poolLength(pool) ((pool)->ptr - (pool)->start)
    443  1.1  tron #define poolChop(pool) ((void)--(pool->ptr))
    444  1.1  tron #define poolLastChar(pool) (((pool)->ptr)[-1])
    445  1.1  tron #define poolDiscard(pool) ((pool)->ptr = (pool)->start)
    446  1.1  tron #define poolFinish(pool) ((pool)->start = (pool)->ptr)
    447  1.1  tron #define poolAppendChar(pool, c) \
    448  1.1  tron   (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
    449  1.1  tron    ? 0 \
    450  1.1  tron    : ((*((pool)->ptr)++ = c), 1))
    451  1.1  tron 
    452  1.1  tron struct XML_ParserStruct {
    453  1.1  tron   /* The first member must be userData so that the XML_GetUserData
    454  1.1  tron      macro works. */
    455  1.1  tron   void *m_userData;
    456  1.1  tron   void *m_handlerArg;
    457  1.1  tron   char *m_buffer;
    458  1.1  tron   const XML_Memory_Handling_Suite m_mem;
    459  1.1  tron   /* first character to be parsed */
    460  1.1  tron   const char *m_bufferPtr;
    461  1.1  tron   /* past last character to be parsed */
    462  1.1  tron   char *m_bufferEnd;
    463  1.1  tron   /* allocated end of buffer */
    464  1.1  tron   const char *m_bufferLim;
    465  1.1  tron   XML_Index m_parseEndByteIndex;
    466  1.1  tron   const char *m_parseEndPtr;
    467  1.1  tron   XML_Char *m_dataBuf;
    468  1.1  tron   XML_Char *m_dataBufEnd;
    469  1.1  tron   XML_StartElementHandler m_startElementHandler;
    470  1.1  tron   XML_EndElementHandler m_endElementHandler;
    471  1.1  tron   XML_CharacterDataHandler m_characterDataHandler;
    472  1.1  tron   XML_ProcessingInstructionHandler m_processingInstructionHandler;
    473  1.1  tron   XML_CommentHandler m_commentHandler;
    474  1.1  tron   XML_StartCdataSectionHandler m_startCdataSectionHandler;
    475  1.1  tron   XML_EndCdataSectionHandler m_endCdataSectionHandler;
    476  1.1  tron   XML_DefaultHandler m_defaultHandler;
    477  1.1  tron   XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
    478  1.1  tron   XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
    479  1.1  tron   XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
    480  1.1  tron   XML_NotationDeclHandler m_notationDeclHandler;
    481  1.1  tron   XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
    482  1.1  tron   XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
    483  1.1  tron   XML_NotStandaloneHandler m_notStandaloneHandler;
    484  1.1  tron   XML_ExternalEntityRefHandler m_externalEntityRefHandler;
    485  1.1  tron   XML_Parser m_externalEntityRefHandlerArg;
    486  1.1  tron   XML_SkippedEntityHandler m_skippedEntityHandler;
    487  1.1  tron   XML_UnknownEncodingHandler m_unknownEncodingHandler;
    488  1.1  tron   XML_ElementDeclHandler m_elementDeclHandler;
    489  1.1  tron   XML_AttlistDeclHandler m_attlistDeclHandler;
    490  1.1  tron   XML_EntityDeclHandler m_entityDeclHandler;
    491  1.1  tron   XML_XmlDeclHandler m_xmlDeclHandler;
    492  1.1  tron   const ENCODING *m_encoding;
    493  1.1  tron   INIT_ENCODING m_initEncoding;
    494  1.1  tron   const ENCODING *m_internalEncoding;
    495  1.1  tron   const XML_Char *m_protocolEncodingName;
    496  1.1  tron   XML_Bool m_ns;
    497  1.1  tron   XML_Bool m_ns_triplets;
    498  1.1  tron   void *m_unknownEncodingMem;
    499  1.1  tron   void *m_unknownEncodingData;
    500  1.1  tron   void *m_unknownEncodingHandlerData;
    501  1.1  tron   void (XMLCALL *m_unknownEncodingRelease)(void *);
    502  1.1  tron   PROLOG_STATE m_prologState;
    503  1.1  tron   Processor *m_processor;
    504  1.1  tron   enum XML_Error m_errorCode;
    505  1.1  tron   const char *m_eventPtr;
    506  1.1  tron   const char *m_eventEndPtr;
    507  1.1  tron   const char *m_positionPtr;
    508  1.1  tron   OPEN_INTERNAL_ENTITY *m_openInternalEntities;
    509  1.1  tron   OPEN_INTERNAL_ENTITY *m_freeInternalEntities;
    510  1.1  tron   XML_Bool m_defaultExpandInternalEntities;
    511  1.1  tron   int m_tagLevel;
    512  1.1  tron   ENTITY *m_declEntity;
    513  1.1  tron   const XML_Char *m_doctypeName;
    514  1.1  tron   const XML_Char *m_doctypeSysid;
    515  1.1  tron   const XML_Char *m_doctypePubid;
    516  1.1  tron   const XML_Char *m_declAttributeType;
    517  1.1  tron   const XML_Char *m_declNotationName;
    518  1.1  tron   const XML_Char *m_declNotationPublicId;
    519  1.1  tron   ELEMENT_TYPE *m_declElementType;
    520  1.1  tron   ATTRIBUTE_ID *m_declAttributeId;
    521  1.1  tron   XML_Bool m_declAttributeIsCdata;
    522  1.1  tron   XML_Bool m_declAttributeIsId;
    523  1.1  tron   DTD *m_dtd;
    524  1.1  tron   const XML_Char *m_curBase;
    525  1.1  tron   TAG *m_tagStack;
    526  1.1  tron   TAG *m_freeTagList;
    527  1.1  tron   BINDING *m_inheritedBindings;
    528  1.1  tron   BINDING *m_freeBindingList;
    529  1.1  tron   int m_attsSize;
    530  1.1  tron   int m_nSpecifiedAtts;
    531  1.1  tron   int m_idAttIndex;
    532  1.1  tron   ATTRIBUTE *m_atts;
    533  1.1  tron   NS_ATT *m_nsAtts;
    534  1.1  tron   unsigned long m_nsAttsVersion;
    535  1.1  tron   unsigned char m_nsAttsPower;
    536  1.1  tron   POSITION m_position;
    537  1.1  tron   STRING_POOL m_tempPool;
    538  1.1  tron   STRING_POOL m_temp2Pool;
    539  1.1  tron   char *m_groupConnector;
    540  1.1  tron   unsigned int m_groupSize;
    541  1.1  tron   XML_Char m_namespaceSeparator;
    542  1.1  tron   XML_Parser m_parentParser;
    543  1.1  tron   XML_ParsingStatus m_parsingStatus;
    544  1.1  tron #ifdef XML_DTD
    545  1.1  tron   XML_Bool m_isParamEntity;
    546  1.1  tron   XML_Bool m_useForeignDTD;
    547  1.1  tron   enum XML_ParamEntityParsing m_paramEntityParsing;
    548  1.1  tron #endif
    549  1.1  tron };
    550  1.1  tron 
    551  1.1  tron #define MALLOC(s) (parser->m_mem.malloc_fcn((s)))
    552  1.1  tron #define REALLOC(p,s) (parser->m_mem.realloc_fcn((p),(s)))
    553  1.1  tron #define FREE(p) (parser->m_mem.free_fcn((p)))
    554  1.1  tron 
    555  1.1  tron #define userData (parser->m_userData)
    556  1.1  tron #define handlerArg (parser->m_handlerArg)
    557  1.1  tron #define startElementHandler (parser->m_startElementHandler)
    558  1.1  tron #define endElementHandler (parser->m_endElementHandler)
    559  1.1  tron #define characterDataHandler (parser->m_characterDataHandler)
    560  1.1  tron #define processingInstructionHandler \
    561  1.1  tron         (parser->m_processingInstructionHandler)
    562  1.1  tron #define commentHandler (parser->m_commentHandler)
    563  1.1  tron #define startCdataSectionHandler \
    564  1.1  tron         (parser->m_startCdataSectionHandler)
    565  1.1  tron #define endCdataSectionHandler (parser->m_endCdataSectionHandler)
    566  1.1  tron #define defaultHandler (parser->m_defaultHandler)
    567  1.1  tron #define startDoctypeDeclHandler (parser->m_startDoctypeDeclHandler)
    568  1.1  tron #define endDoctypeDeclHandler (parser->m_endDoctypeDeclHandler)
    569  1.1  tron #define unparsedEntityDeclHandler \
    570  1.1  tron         (parser->m_unparsedEntityDeclHandler)
    571  1.1  tron #define notationDeclHandler (parser->m_notationDeclHandler)
    572  1.1  tron #define startNamespaceDeclHandler \
    573  1.1  tron         (parser->m_startNamespaceDeclHandler)
    574  1.1  tron #define endNamespaceDeclHandler (parser->m_endNamespaceDeclHandler)
    575  1.1  tron #define notStandaloneHandler (parser->m_notStandaloneHandler)
    576  1.1  tron #define externalEntityRefHandler \
    577  1.1  tron         (parser->m_externalEntityRefHandler)
    578  1.1  tron #define externalEntityRefHandlerArg \
    579  1.1  tron         (parser->m_externalEntityRefHandlerArg)
    580  1.1  tron #define internalEntityRefHandler \
    581  1.1  tron         (parser->m_internalEntityRefHandler)
    582  1.1  tron #define skippedEntityHandler (parser->m_skippedEntityHandler)
    583  1.1  tron #define unknownEncodingHandler (parser->m_unknownEncodingHandler)
    584  1.1  tron #define elementDeclHandler (parser->m_elementDeclHandler)
    585  1.1  tron #define attlistDeclHandler (parser->m_attlistDeclHandler)
    586  1.1  tron #define entityDeclHandler (parser->m_entityDeclHandler)
    587  1.1  tron #define xmlDeclHandler (parser->m_xmlDeclHandler)
    588  1.1  tron #define encoding (parser->m_encoding)
    589  1.1  tron #define initEncoding (parser->m_initEncoding)
    590  1.1  tron #define internalEncoding (parser->m_internalEncoding)
    591  1.1  tron #define unknownEncodingMem (parser->m_unknownEncodingMem)
    592  1.1  tron #define unknownEncodingData (parser->m_unknownEncodingData)
    593  1.1  tron #define unknownEncodingHandlerData \
    594  1.1  tron   (parser->m_unknownEncodingHandlerData)
    595  1.1  tron #define unknownEncodingRelease (parser->m_unknownEncodingRelease)
    596  1.1  tron #define protocolEncodingName (parser->m_protocolEncodingName)
    597  1.1  tron #define ns (parser->m_ns)
    598  1.1  tron #define ns_triplets (parser->m_ns_triplets)
    599  1.1  tron #define prologState (parser->m_prologState)
    600  1.1  tron #define processor (parser->m_processor)
    601  1.1  tron #define errorCode (parser->m_errorCode)
    602  1.1  tron #define eventPtr (parser->m_eventPtr)
    603  1.1  tron #define eventEndPtr (parser->m_eventEndPtr)
    604  1.1  tron #define positionPtr (parser->m_positionPtr)
    605  1.1  tron #define position (parser->m_position)
    606  1.1  tron #define openInternalEntities (parser->m_openInternalEntities)
    607  1.1  tron #define freeInternalEntities (parser->m_freeInternalEntities)
    608  1.1  tron #define defaultExpandInternalEntities \
    609  1.1  tron         (parser->m_defaultExpandInternalEntities)
    610  1.1  tron #define tagLevel (parser->m_tagLevel)
    611  1.1  tron #define buffer (parser->m_buffer)
    612  1.1  tron #define bufferPtr (parser->m_bufferPtr)
    613  1.1  tron #define bufferEnd (parser->m_bufferEnd)
    614  1.1  tron #define parseEndByteIndex (parser->m_parseEndByteIndex)
    615  1.1  tron #define parseEndPtr (parser->m_parseEndPtr)
    616  1.1  tron #define bufferLim (parser->m_bufferLim)
    617  1.1  tron #define dataBuf (parser->m_dataBuf)
    618  1.1  tron #define dataBufEnd (parser->m_dataBufEnd)
    619  1.1  tron #define _dtd (parser->m_dtd)
    620  1.1  tron #define curBase (parser->m_curBase)
    621  1.1  tron #define declEntity (parser->m_declEntity)
    622  1.1  tron #define doctypeName (parser->m_doctypeName)
    623  1.1  tron #define doctypeSysid (parser->m_doctypeSysid)
    624  1.1  tron #define doctypePubid (parser->m_doctypePubid)
    625  1.1  tron #define declAttributeType (parser->m_declAttributeType)
    626  1.1  tron #define declNotationName (parser->m_declNotationName)
    627  1.1  tron #define declNotationPublicId (parser->m_declNotationPublicId)
    628  1.1  tron #define declElementType (parser->m_declElementType)
    629  1.1  tron #define declAttributeId (parser->m_declAttributeId)
    630  1.1  tron #define declAttributeIsCdata (parser->m_declAttributeIsCdata)
    631  1.1  tron #define declAttributeIsId (parser->m_declAttributeIsId)
    632  1.1  tron #define freeTagList (parser->m_freeTagList)
    633  1.1  tron #define freeBindingList (parser->m_freeBindingList)
    634  1.1  tron #define inheritedBindings (parser->m_inheritedBindings)
    635  1.1  tron #define tagStack (parser->m_tagStack)
    636  1.1  tron #define atts (parser->m_atts)
    637  1.1  tron #define attsSize (parser->m_attsSize)
    638  1.1  tron #define nSpecifiedAtts (parser->m_nSpecifiedAtts)
    639  1.1  tron #define idAttIndex (parser->m_idAttIndex)
    640  1.1  tron #define nsAtts (parser->m_nsAtts)
    641  1.1  tron #define nsAttsVersion (parser->m_nsAttsVersion)
    642  1.1  tron #define nsAttsPower (parser->m_nsAttsPower)
    643  1.1  tron #define tempPool (parser->m_tempPool)
    644  1.1  tron #define temp2Pool (parser->m_temp2Pool)
    645  1.1  tron #define groupConnector (parser->m_groupConnector)
    646  1.1  tron #define groupSize (parser->m_groupSize)
    647  1.1  tron #define namespaceSeparator (parser->m_namespaceSeparator)
    648  1.1  tron #define parentParser (parser->m_parentParser)
    649  1.1  tron #define ps_parsing (parser->m_parsingStatus.parsing)
    650  1.1  tron #define ps_finalBuffer (parser->m_parsingStatus.finalBuffer)
    651  1.1  tron #ifdef XML_DTD
    652  1.1  tron #define isParamEntity (parser->m_isParamEntity)
    653  1.1  tron #define useForeignDTD (parser->m_useForeignDTD)
    654  1.1  tron #define paramEntityParsing (parser->m_paramEntityParsing)
    655  1.1  tron #endif /* XML_DTD */
    656  1.1  tron 
    657  1.1  tron XML_Parser XMLCALL
    658  1.1  tron XML_ParserCreate(const XML_Char *encodingName)
    659  1.1  tron {
    660  1.1  tron   return XML_ParserCreate_MM(encodingName, NULL, NULL);
    661  1.1  tron }
    662  1.1  tron 
    663  1.1  tron XML_Parser XMLCALL
    664  1.1  tron XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
    665  1.1  tron {
    666  1.1  tron   XML_Char tmp[2];
    667  1.1  tron   *tmp = nsSep;
    668  1.1  tron   return XML_ParserCreate_MM(encodingName, NULL, tmp);
    669  1.1  tron }
    670  1.1  tron 
    671  1.1  tron static const XML_Char implicitContext[] = {
    672  1.1  tron   ASCII_x, ASCII_m, ASCII_l, ASCII_EQUALS, ASCII_h, ASCII_t, ASCII_t, ASCII_p,
    673  1.1  tron   ASCII_COLON, ASCII_SLASH, ASCII_SLASH, ASCII_w, ASCII_w, ASCII_w,
    674  1.1  tron   ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD, ASCII_o, ASCII_r, ASCII_g,
    675  1.1  tron   ASCII_SLASH, ASCII_X, ASCII_M, ASCII_L, ASCII_SLASH, ASCII_1, ASCII_9,
    676  1.1  tron   ASCII_9, ASCII_8, ASCII_SLASH, ASCII_n, ASCII_a, ASCII_m, ASCII_e,
    677  1.1  tron   ASCII_s, ASCII_p, ASCII_a, ASCII_c, ASCII_e, '\0'
    678  1.1  tron };
    679  1.1  tron 
    680  1.1  tron XML_Parser XMLCALL
    681  1.1  tron XML_ParserCreate_MM(const XML_Char *encodingName,
    682  1.1  tron                     const XML_Memory_Handling_Suite *memsuite,
    683  1.1  tron                     const XML_Char *nameSep)
    684  1.1  tron {
    685  1.1  tron   XML_Parser parser = parserCreate(encodingName, memsuite, nameSep, NULL);
    686  1.1  tron   if (parser != NULL && ns) {
    687  1.1  tron     /* implicit context only set for root parser, since child
    688  1.1  tron        parsers (i.e. external entity parsers) will inherit it
    689  1.1  tron     */
    690  1.1  tron     if (!setContext(parser, implicitContext)) {
    691  1.1  tron       XML_ParserFree(parser);
    692  1.1  tron       return NULL;
    693  1.1  tron     }
    694  1.1  tron   }
    695  1.1  tron   return parser;
    696  1.1  tron }
    697  1.1  tron 
    698  1.1  tron static XML_Parser
    699  1.1  tron parserCreate(const XML_Char *encodingName,
    700  1.1  tron              const XML_Memory_Handling_Suite *memsuite,
    701  1.1  tron              const XML_Char *nameSep,
    702  1.1  tron              DTD *dtd)
    703  1.1  tron {
    704  1.1  tron   XML_Parser parser;
    705  1.1  tron 
    706  1.1  tron   if (memsuite) {
    707  1.1  tron     XML_Memory_Handling_Suite *mtemp;
    708  1.1  tron     parser = (XML_Parser)
    709  1.1  tron       memsuite->malloc_fcn(sizeof(struct XML_ParserStruct));
    710  1.1  tron     if (parser != NULL) {
    711  1.1  tron       mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
    712  1.1  tron       mtemp->malloc_fcn = memsuite->malloc_fcn;
    713  1.1  tron       mtemp->realloc_fcn = memsuite->realloc_fcn;
    714  1.1  tron       mtemp->free_fcn = memsuite->free_fcn;
    715  1.1  tron     }
    716  1.1  tron   }
    717  1.1  tron   else {
    718  1.1  tron     XML_Memory_Handling_Suite *mtemp;
    719  1.1  tron     parser = (XML_Parser)malloc(sizeof(struct XML_ParserStruct));
    720  1.1  tron     if (parser != NULL) {
    721  1.1  tron       mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
    722  1.1  tron       mtemp->malloc_fcn = malloc;
    723  1.1  tron       mtemp->realloc_fcn = realloc;
    724  1.1  tron       mtemp->free_fcn = free;
    725  1.1  tron     }
    726  1.1  tron   }
    727  1.1  tron 
    728  1.1  tron   if (!parser)
    729  1.1  tron     return parser;
    730  1.1  tron 
    731  1.1  tron   buffer = NULL;
    732  1.1  tron   bufferLim = NULL;
    733  1.1  tron 
    734  1.1  tron   attsSize = INIT_ATTS_SIZE;
    735  1.1  tron   atts = (ATTRIBUTE *)MALLOC(attsSize * sizeof(ATTRIBUTE));
    736  1.1  tron   if (atts == NULL) {
    737  1.1  tron     FREE(parser);
    738  1.1  tron     return NULL;
    739  1.1  tron   }
    740  1.1  tron   dataBuf = (XML_Char *)MALLOC(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
    741  1.1  tron   if (dataBuf == NULL) {
    742  1.1  tron     FREE(atts);
    743  1.1  tron     FREE(parser);
    744  1.1  tron     return NULL;
    745  1.1  tron   }
    746  1.1  tron   dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
    747  1.1  tron 
    748  1.1  tron   if (dtd)
    749  1.1  tron     _dtd = dtd;
    750  1.1  tron   else {
    751  1.1  tron     _dtd = dtdCreate(&parser->m_mem);
    752  1.1  tron     if (_dtd == NULL) {
    753  1.1  tron       FREE(dataBuf);
    754  1.1  tron       FREE(atts);
    755  1.1  tron       FREE(parser);
    756  1.1  tron       return NULL;
    757  1.1  tron     }
    758  1.1  tron   }
    759  1.1  tron 
    760  1.1  tron   freeBindingList = NULL;
    761  1.1  tron   freeTagList = NULL;
    762  1.1  tron   freeInternalEntities = NULL;
    763  1.1  tron 
    764  1.1  tron   groupSize = 0;
    765  1.1  tron   groupConnector = NULL;
    766  1.1  tron 
    767  1.1  tron   unknownEncodingHandler = NULL;
    768  1.1  tron   unknownEncodingHandlerData = NULL;
    769  1.1  tron 
    770  1.1  tron   namespaceSeparator = ASCII_EXCL;
    771  1.1  tron   ns = XML_FALSE;
    772  1.1  tron   ns_triplets = XML_FALSE;
    773  1.1  tron 
    774  1.1  tron   nsAtts = NULL;
    775  1.1  tron   nsAttsVersion = 0;
    776  1.1  tron   nsAttsPower = 0;
    777  1.1  tron 
    778  1.1  tron   poolInit(&tempPool, &(parser->m_mem));
    779  1.1  tron   poolInit(&temp2Pool, &(parser->m_mem));
    780  1.1  tron   parserInit(parser, encodingName);
    781  1.1  tron 
    782  1.1  tron   if (encodingName && !protocolEncodingName) {
    783  1.1  tron     XML_ParserFree(parser);
    784  1.1  tron     return NULL;
    785  1.1  tron   }
    786  1.1  tron 
    787  1.1  tron   if (nameSep) {
    788  1.1  tron     ns = XML_TRUE;
    789  1.1  tron     internalEncoding = XmlGetInternalEncodingNS();
    790  1.1  tron     namespaceSeparator = *nameSep;
    791  1.1  tron   }
    792  1.1  tron   else {
    793  1.1  tron     internalEncoding = XmlGetInternalEncoding();
    794  1.1  tron   }
    795  1.1  tron 
    796  1.1  tron   return parser;
    797  1.1  tron }
    798  1.1  tron 
    799  1.1  tron static void
    800  1.1  tron parserInit(XML_Parser parser, const XML_Char *encodingName)
    801  1.1  tron {
    802  1.1  tron   processor = prologInitProcessor;
    803  1.1  tron   XmlPrologStateInit(&prologState);
    804  1.1  tron   protocolEncodingName = (encodingName != NULL
    805  1.1  tron                           ? poolCopyString(&tempPool, encodingName)
    806  1.1  tron                           : NULL);
    807  1.1  tron   curBase = NULL;
    808  1.1  tron   XmlInitEncoding(&initEncoding, &encoding, 0);
    809  1.1  tron   userData = NULL;
    810  1.1  tron   handlerArg = NULL;
    811  1.1  tron   startElementHandler = NULL;
    812  1.1  tron   endElementHandler = NULL;
    813  1.1  tron   characterDataHandler = NULL;
    814  1.1  tron   processingInstructionHandler = NULL;
    815  1.1  tron   commentHandler = NULL;
    816  1.1  tron   startCdataSectionHandler = NULL;
    817  1.1  tron   endCdataSectionHandler = NULL;
    818  1.1  tron   defaultHandler = NULL;
    819  1.1  tron   startDoctypeDeclHandler = NULL;
    820  1.1  tron   endDoctypeDeclHandler = NULL;
    821  1.1  tron   unparsedEntityDeclHandler = NULL;
    822  1.1  tron   notationDeclHandler = NULL;
    823  1.1  tron   startNamespaceDeclHandler = NULL;
    824  1.1  tron   endNamespaceDeclHandler = NULL;
    825  1.1  tron   notStandaloneHandler = NULL;
    826  1.1  tron   externalEntityRefHandler = NULL;
    827  1.1  tron   externalEntityRefHandlerArg = parser;
    828  1.1  tron   skippedEntityHandler = NULL;
    829  1.1  tron   elementDeclHandler = NULL;
    830  1.1  tron   attlistDeclHandler = NULL;
    831  1.1  tron   entityDeclHandler = NULL;
    832  1.1  tron   xmlDeclHandler = NULL;
    833  1.1  tron   bufferPtr = buffer;
    834  1.1  tron   bufferEnd = buffer;
    835  1.1  tron   parseEndByteIndex = 0;
    836  1.1  tron   parseEndPtr = NULL;
    837  1.1  tron   declElementType = NULL;
    838  1.1  tron   declAttributeId = NULL;
    839  1.1  tron   declEntity = NULL;
    840  1.1  tron   doctypeName = NULL;
    841  1.1  tron   doctypeSysid = NULL;
    842  1.1  tron   doctypePubid = NULL;
    843  1.1  tron   declAttributeType = NULL;
    844  1.1  tron   declNotationName = NULL;
    845  1.1  tron   declNotationPublicId = NULL;
    846  1.1  tron   declAttributeIsCdata = XML_FALSE;
    847  1.1  tron   declAttributeIsId = XML_FALSE;
    848  1.1  tron   memset(&position, 0, sizeof(POSITION));
    849  1.1  tron   errorCode = XML_ERROR_NONE;
    850  1.1  tron   eventPtr = NULL;
    851  1.1  tron   eventEndPtr = NULL;
    852  1.1  tron   positionPtr = NULL;
    853  1.1  tron   openInternalEntities = NULL;
    854  1.1  tron   defaultExpandInternalEntities = XML_TRUE;
    855  1.1  tron   tagLevel = 0;
    856  1.1  tron   tagStack = NULL;
    857  1.1  tron   inheritedBindings = NULL;
    858  1.1  tron   nSpecifiedAtts = 0;
    859  1.1  tron   unknownEncodingMem = NULL;
    860  1.1  tron   unknownEncodingRelease = NULL;
    861  1.1  tron   unknownEncodingData = NULL;
    862  1.1  tron   parentParser = NULL;
    863  1.1  tron   ps_parsing = XML_INITIALIZED;
    864  1.1  tron #ifdef XML_DTD
    865  1.1  tron   isParamEntity = XML_FALSE;
    866  1.1  tron   useForeignDTD = XML_FALSE;
    867  1.1  tron   paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
    868  1.1  tron #endif
    869  1.1  tron }
    870  1.1  tron 
    871  1.1  tron /* moves list of bindings to freeBindingList */
    872  1.1  tron static void FASTCALL
    873  1.1  tron moveToFreeBindingList(XML_Parser parser, BINDING *bindings)
    874  1.1  tron {
    875  1.1  tron   while (bindings) {
    876  1.1  tron     BINDING *b = bindings;
    877  1.1  tron     bindings = bindings->nextTagBinding;
    878  1.1  tron     b->nextTagBinding = freeBindingList;
    879  1.1  tron     freeBindingList = b;
    880  1.1  tron   }
    881  1.1  tron }
    882  1.1  tron 
    883  1.1  tron XML_Bool XMLCALL
    884  1.1  tron XML_ParserReset(XML_Parser parser, const XML_Char *encodingName)
    885  1.1  tron {
    886  1.1  tron   TAG *tStk;
    887  1.1  tron   OPEN_INTERNAL_ENTITY *openEntityList;
    888  1.1  tron   if (parentParser)
    889  1.1  tron     return XML_FALSE;
    890  1.1  tron   /* move tagStack to freeTagList */
    891  1.1  tron   tStk = tagStack;
    892  1.1  tron   while (tStk) {
    893  1.1  tron     TAG *tag = tStk;
    894  1.1  tron     tStk = tStk->parent;
    895  1.1  tron     tag->parent = freeTagList;
    896  1.1  tron     moveToFreeBindingList(parser, tag->bindings);
    897  1.1  tron     tag->bindings = NULL;
    898  1.1  tron     freeTagList = tag;
    899  1.1  tron   }
    900  1.1  tron   /* move openInternalEntities to freeInternalEntities */
    901  1.1  tron   openEntityList = openInternalEntities;
    902  1.1  tron   while (openEntityList) {
    903  1.1  tron     OPEN_INTERNAL_ENTITY *openEntity = openEntityList;
    904  1.1  tron     openEntityList = openEntity->next;
    905  1.1  tron     openEntity->next = freeInternalEntities;
    906  1.1  tron     freeInternalEntities = openEntity;
    907  1.1  tron   }
    908  1.1  tron   moveToFreeBindingList(parser, inheritedBindings);
    909  1.1  tron   FREE(unknownEncodingMem);
    910  1.1  tron   if (unknownEncodingRelease)
    911  1.1  tron     unknownEncodingRelease(unknownEncodingData);
    912  1.1  tron   poolClear(&tempPool);
    913  1.1  tron   poolClear(&temp2Pool);
    914  1.1  tron   parserInit(parser, encodingName);
    915  1.1  tron   dtdReset(_dtd, &parser->m_mem);
    916  1.1  tron   return setContext(parser, implicitContext);
    917  1.1  tron }
    918  1.1  tron 
    919  1.1  tron enum XML_Status XMLCALL
    920  1.1  tron XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
    921  1.1  tron {
    922  1.1  tron   /* Block after XML_Parse()/XML_ParseBuffer() has been called.
    923  1.1  tron      XXX There's no way for the caller to determine which of the
    924  1.1  tron      XXX possible error cases caused the XML_STATUS_ERROR return.
    925  1.1  tron   */
    926  1.1  tron   if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
    927  1.1  tron     return XML_STATUS_ERROR;
    928  1.1  tron   if (encodingName == NULL)
    929  1.1  tron     protocolEncodingName = NULL;
    930  1.1  tron   else {
    931  1.1  tron     protocolEncodingName = poolCopyString(&tempPool, encodingName);
    932  1.1  tron     if (!protocolEncodingName)
    933  1.1  tron       return XML_STATUS_ERROR;
    934  1.1  tron   }
    935  1.1  tron   return XML_STATUS_OK;
    936  1.1  tron }
    937  1.1  tron 
    938  1.1  tron XML_Parser XMLCALL
    939  1.1  tron XML_ExternalEntityParserCreate(XML_Parser oldParser,
    940  1.1  tron                                const XML_Char *context,
    941  1.1  tron                                const XML_Char *encodingName)
    942  1.1  tron {
    943  1.1  tron   XML_Parser parser = oldParser;
    944  1.1  tron   DTD *newDtd = NULL;
    945  1.1  tron   DTD *oldDtd = _dtd;
    946  1.1  tron   XML_StartElementHandler oldStartElementHandler = startElementHandler;
    947  1.1  tron   XML_EndElementHandler oldEndElementHandler = endElementHandler;
    948  1.1  tron   XML_CharacterDataHandler oldCharacterDataHandler = characterDataHandler;
    949  1.1  tron   XML_ProcessingInstructionHandler oldProcessingInstructionHandler
    950  1.1  tron       = processingInstructionHandler;
    951  1.1  tron   XML_CommentHandler oldCommentHandler = commentHandler;
    952  1.1  tron   XML_StartCdataSectionHandler oldStartCdataSectionHandler
    953  1.1  tron       = startCdataSectionHandler;
    954  1.1  tron   XML_EndCdataSectionHandler oldEndCdataSectionHandler
    955  1.1  tron       = endCdataSectionHandler;
    956  1.1  tron   XML_DefaultHandler oldDefaultHandler = defaultHandler;
    957  1.1  tron   XML_UnparsedEntityDeclHandler oldUnparsedEntityDeclHandler
    958  1.1  tron       = unparsedEntityDeclHandler;
    959  1.1  tron   XML_NotationDeclHandler oldNotationDeclHandler = notationDeclHandler;
    960  1.1  tron   XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler
    961  1.1  tron       = startNamespaceDeclHandler;
    962  1.1  tron   XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler
    963  1.1  tron       = endNamespaceDeclHandler;
    964  1.1  tron   XML_NotStandaloneHandler oldNotStandaloneHandler = notStandaloneHandler;
    965  1.1  tron   XML_ExternalEntityRefHandler oldExternalEntityRefHandler
    966  1.1  tron       = externalEntityRefHandler;
    967  1.1  tron   XML_SkippedEntityHandler oldSkippedEntityHandler = skippedEntityHandler;
    968  1.1  tron   XML_UnknownEncodingHandler oldUnknownEncodingHandler
    969  1.1  tron       = unknownEncodingHandler;
    970  1.1  tron   XML_ElementDeclHandler oldElementDeclHandler = elementDeclHandler;
    971  1.1  tron   XML_AttlistDeclHandler oldAttlistDeclHandler = attlistDeclHandler;
    972  1.1  tron   XML_EntityDeclHandler oldEntityDeclHandler = entityDeclHandler;
    973  1.1  tron   XML_XmlDeclHandler oldXmlDeclHandler = xmlDeclHandler;
    974  1.1  tron   ELEMENT_TYPE * oldDeclElementType = declElementType;
    975  1.1  tron 
    976  1.1  tron   void *oldUserData = userData;
    977  1.1  tron   void *oldHandlerArg = handlerArg;
    978  1.1  tron   XML_Bool oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
    979  1.1  tron   XML_Parser oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
    980  1.1  tron #ifdef XML_DTD
    981  1.1  tron   enum XML_ParamEntityParsing oldParamEntityParsing = paramEntityParsing;
    982  1.1  tron   int oldInEntityValue = prologState.inEntityValue;
    983  1.1  tron #endif
    984  1.1  tron   XML_Bool oldns_triplets = ns_triplets;
    985  1.1  tron 
    986  1.1  tron #ifdef XML_DTD
    987  1.1  tron   if (!context)
    988  1.1  tron     newDtd = oldDtd;
    989  1.1  tron #endif /* XML_DTD */
    990  1.1  tron 
    991  1.1  tron   /* Note that the magical uses of the pre-processor to make field
    992  1.1  tron      access look more like C++ require that `parser' be overwritten
    993  1.1  tron      here.  This makes this function more painful to follow than it
    994  1.1  tron      would be otherwise.
    995  1.1  tron   */
    996  1.1  tron   if (ns) {
    997  1.1  tron     XML_Char tmp[2];
    998  1.1  tron     *tmp = namespaceSeparator;
    999  1.1  tron     parser = parserCreate(encodingName, &parser->m_mem, tmp, newDtd);
   1000  1.1  tron   }
   1001  1.1  tron   else {
   1002  1.1  tron     parser = parserCreate(encodingName, &parser->m_mem, NULL, newDtd);
   1003  1.1  tron   }
   1004  1.1  tron 
   1005  1.1  tron   if (!parser)
   1006  1.1  tron     return NULL;
   1007  1.1  tron 
   1008  1.1  tron   startElementHandler = oldStartElementHandler;
   1009  1.1  tron   endElementHandler = oldEndElementHandler;
   1010  1.1  tron   characterDataHandler = oldCharacterDataHandler;
   1011  1.1  tron   processingInstructionHandler = oldProcessingInstructionHandler;
   1012  1.1  tron   commentHandler = oldCommentHandler;
   1013  1.1  tron   startCdataSectionHandler = oldStartCdataSectionHandler;
   1014  1.1  tron   endCdataSectionHandler = oldEndCdataSectionHandler;
   1015  1.1  tron   defaultHandler = oldDefaultHandler;
   1016  1.1  tron   unparsedEntityDeclHandler = oldUnparsedEntityDeclHandler;
   1017  1.1  tron   notationDeclHandler = oldNotationDeclHandler;
   1018  1.1  tron   startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
   1019  1.1  tron   endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
   1020  1.1  tron   notStandaloneHandler = oldNotStandaloneHandler;
   1021  1.1  tron   externalEntityRefHandler = oldExternalEntityRefHandler;
   1022  1.1  tron   skippedEntityHandler = oldSkippedEntityHandler;
   1023  1.1  tron   unknownEncodingHandler = oldUnknownEncodingHandler;
   1024  1.1  tron   elementDeclHandler = oldElementDeclHandler;
   1025  1.1  tron   attlistDeclHandler = oldAttlistDeclHandler;
   1026  1.1  tron   entityDeclHandler = oldEntityDeclHandler;
   1027  1.1  tron   xmlDeclHandler = oldXmlDeclHandler;
   1028  1.1  tron   declElementType = oldDeclElementType;
   1029  1.1  tron   userData = oldUserData;
   1030  1.1  tron   if (oldUserData == oldHandlerArg)
   1031  1.1  tron     handlerArg = userData;
   1032  1.1  tron   else
   1033  1.1  tron     handlerArg = parser;
   1034  1.1  tron   if (oldExternalEntityRefHandlerArg != oldParser)
   1035  1.1  tron     externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
   1036  1.1  tron   defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
   1037  1.1  tron   ns_triplets = oldns_triplets;
   1038  1.1  tron   parentParser = oldParser;
   1039  1.1  tron #ifdef XML_DTD
   1040  1.1  tron   paramEntityParsing = oldParamEntityParsing;
   1041  1.1  tron   prologState.inEntityValue = oldInEntityValue;
   1042  1.1  tron   if (context) {
   1043  1.1  tron #endif /* XML_DTD */
   1044  1.1  tron     if (!dtdCopy(_dtd, oldDtd, &parser->m_mem)
   1045  1.1  tron       || !setContext(parser, context)) {
   1046  1.1  tron       XML_ParserFree(parser);
   1047  1.1  tron       return NULL;
   1048  1.1  tron     }
   1049  1.1  tron     processor = externalEntityInitProcessor;
   1050  1.1  tron #ifdef XML_DTD
   1051  1.1  tron   }
   1052  1.1  tron   else {
   1053  1.1  tron     /* The DTD instance referenced by _dtd is shared between the document's
   1054  1.1  tron        root parser and external PE parsers, therefore one does not need to
   1055  1.1  tron        call setContext. In addition, one also *must* not call setContext,
   1056  1.1  tron        because this would overwrite existing prefix->binding pointers in
   1057  1.1  tron        _dtd with ones that get destroyed with the external PE parser.
   1058  1.1  tron        This would leave those prefixes with dangling pointers.
   1059  1.1  tron     */
   1060  1.1  tron     isParamEntity = XML_TRUE;
   1061  1.1  tron     XmlPrologStateInitExternalEntity(&prologState);
   1062  1.1  tron     processor = externalParEntInitProcessor;
   1063  1.1  tron   }
   1064  1.1  tron #endif /* XML_DTD */
   1065  1.1  tron   return parser;
   1066  1.1  tron }
   1067  1.1  tron 
   1068  1.1  tron static void FASTCALL
   1069  1.1  tron destroyBindings(BINDING *bindings, XML_Parser parser)
   1070  1.1  tron {
   1071  1.1  tron   for (;;) {
   1072  1.1  tron     BINDING *b = bindings;
   1073  1.1  tron     if (!b)
   1074  1.1  tron       break;
   1075  1.1  tron     bindings = b->nextTagBinding;
   1076  1.1  tron     FREE(b->uri);
   1077  1.1  tron     FREE(b);
   1078  1.1  tron   }
   1079  1.1  tron }
   1080  1.1  tron 
   1081  1.1  tron void XMLCALL
   1082  1.1  tron XML_ParserFree(XML_Parser parser)
   1083  1.1  tron {
   1084  1.1  tron   TAG *tagList;
   1085  1.1  tron   OPEN_INTERNAL_ENTITY *entityList;
   1086  1.1  tron   if (parser == NULL)
   1087  1.1  tron     return;
   1088  1.1  tron   /* free tagStack and freeTagList */
   1089  1.1  tron   tagList = tagStack;
   1090  1.1  tron   for (;;) {
   1091  1.1  tron     TAG *p;
   1092  1.1  tron     if (tagList == NULL) {
   1093  1.1  tron       if (freeTagList == NULL)
   1094  1.1  tron         break;
   1095  1.1  tron       tagList = freeTagList;
   1096  1.1  tron       freeTagList = NULL;
   1097  1.1  tron     }
   1098  1.1  tron     p = tagList;
   1099  1.1  tron     tagList = tagList->parent;
   1100  1.1  tron     FREE(p->buf);
   1101  1.1  tron     destroyBindings(p->bindings, parser);
   1102  1.1  tron     FREE(p);
   1103  1.1  tron   }
   1104  1.1  tron   /* free openInternalEntities and freeInternalEntities */
   1105  1.1  tron   entityList = openInternalEntities;
   1106  1.1  tron   for (;;) {
   1107  1.1  tron     OPEN_INTERNAL_ENTITY *openEntity;
   1108  1.1  tron     if (entityList == NULL) {
   1109  1.1  tron       if (freeInternalEntities == NULL)
   1110  1.1  tron         break;
   1111  1.1  tron       entityList = freeInternalEntities;
   1112  1.1  tron       freeInternalEntities = NULL;
   1113  1.1  tron     }
   1114  1.1  tron     openEntity = entityList;
   1115  1.1  tron     entityList = entityList->next;
   1116  1.1  tron     FREE(openEntity);
   1117  1.1  tron   }
   1118  1.1  tron 
   1119  1.1  tron   destroyBindings(freeBindingList, parser);
   1120  1.1  tron   destroyBindings(inheritedBindings, parser);
   1121  1.1  tron   poolDestroy(&tempPool);
   1122  1.1  tron   poolDestroy(&temp2Pool);
   1123  1.1  tron #ifdef XML_DTD
   1124  1.1  tron   /* external parameter entity parsers share the DTD structure
   1125  1.1  tron      parser->m_dtd with the root parser, so we must not destroy it
   1126  1.1  tron   */
   1127  1.1  tron   if (!isParamEntity && _dtd)
   1128  1.1  tron #else
   1129  1.1  tron   if (_dtd)
   1130  1.1  tron #endif /* XML_DTD */
   1131  1.1  tron     dtdDestroy(_dtd, (XML_Bool)!parentParser, &parser->m_mem);
   1132  1.1  tron   FREE((void *)atts);
   1133  1.1  tron   FREE(groupConnector);
   1134  1.1  tron   FREE(buffer);
   1135  1.1  tron   FREE(dataBuf);
   1136  1.1  tron   FREE(nsAtts);
   1137  1.1  tron   FREE(unknownEncodingMem);
   1138  1.1  tron   if (unknownEncodingRelease)
   1139  1.1  tron     unknownEncodingRelease(unknownEncodingData);
   1140  1.1  tron   FREE(parser);
   1141  1.1  tron }
   1142  1.1  tron 
   1143  1.1  tron void XMLCALL
   1144  1.1  tron XML_UseParserAsHandlerArg(XML_Parser parser)
   1145  1.1  tron {
   1146  1.1  tron   handlerArg = parser;
   1147  1.1  tron }
   1148  1.1  tron 
   1149  1.1  tron enum XML_Error XMLCALL
   1150  1.1  tron XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD)
   1151  1.1  tron {
   1152  1.1  tron #ifdef XML_DTD
   1153  1.1  tron   /* block after XML_Parse()/XML_ParseBuffer() has been called */
   1154  1.1  tron   if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
   1155  1.1  tron     return XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING;
   1156  1.1  tron   useForeignDTD = useDTD;
   1157  1.1  tron   return XML_ERROR_NONE;
   1158  1.1  tron #else
   1159  1.1  tron   return XML_ERROR_FEATURE_REQUIRES_XML_DTD;
   1160  1.1  tron #endif
   1161  1.1  tron }
   1162  1.1  tron 
   1163  1.1  tron void XMLCALL
   1164  1.1  tron XML_SetReturnNSTriplet(XML_Parser parser, int do_nst)
   1165  1.1  tron {
   1166  1.1  tron   /* block after XML_Parse()/XML_ParseBuffer() has been called */
   1167  1.1  tron   if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
   1168  1.1  tron     return;
   1169  1.1  tron   ns_triplets = do_nst ? XML_TRUE : XML_FALSE;
   1170  1.1  tron }
   1171  1.1  tron 
   1172  1.1  tron void XMLCALL
   1173  1.1  tron XML_SetUserData(XML_Parser parser, void *p)
   1174  1.1  tron {
   1175  1.1  tron   if (handlerArg == userData)
   1176  1.1  tron     handlerArg = userData = p;
   1177  1.1  tron   else
   1178  1.1  tron     userData = p;
   1179  1.1  tron }
   1180  1.1  tron 
   1181  1.1  tron enum XML_Status XMLCALL
   1182  1.1  tron XML_SetBase(XML_Parser parser, const XML_Char *p)
   1183  1.1  tron {
   1184  1.1  tron   if (p) {
   1185  1.1  tron     p = poolCopyString(&_dtd->pool, p);
   1186  1.1  tron     if (!p)
   1187  1.1  tron       return XML_STATUS_ERROR;
   1188  1.1  tron     curBase = p;
   1189  1.1  tron   }
   1190  1.1  tron   else
   1191  1.1  tron     curBase = NULL;
   1192  1.1  tron   return XML_STATUS_OK;
   1193  1.1  tron }
   1194  1.1  tron 
   1195  1.1  tron const XML_Char * XMLCALL
   1196  1.1  tron XML_GetBase(XML_Parser parser)
   1197  1.1  tron {
   1198  1.1  tron   return curBase;
   1199  1.1  tron }
   1200  1.1  tron 
   1201  1.1  tron int XMLCALL
   1202  1.1  tron XML_GetSpecifiedAttributeCount(XML_Parser parser)
   1203  1.1  tron {
   1204  1.1  tron   return nSpecifiedAtts;
   1205  1.1  tron }
   1206  1.1  tron 
   1207  1.1  tron int XMLCALL
   1208  1.1  tron XML_GetIdAttributeIndex(XML_Parser parser)
   1209  1.1  tron {
   1210  1.1  tron   return idAttIndex;
   1211  1.1  tron }
   1212  1.1  tron 
   1213  1.1  tron void XMLCALL
   1214  1.1  tron XML_SetElementHandler(XML_Parser parser,
   1215  1.1  tron                       XML_StartElementHandler start,
   1216  1.1  tron                       XML_EndElementHandler end)
   1217  1.1  tron {
   1218  1.1  tron   startElementHandler = start;
   1219  1.1  tron   endElementHandler = end;
   1220  1.1  tron }
   1221  1.1  tron 
   1222  1.1  tron void XMLCALL
   1223  1.1  tron XML_SetStartElementHandler(XML_Parser parser,
   1224  1.1  tron                            XML_StartElementHandler start) {
   1225  1.1  tron   startElementHandler = start;
   1226  1.1  tron }
   1227  1.1  tron 
   1228  1.1  tron void XMLCALL
   1229  1.1  tron XML_SetEndElementHandler(XML_Parser parser,
   1230  1.1  tron                          XML_EndElementHandler end) {
   1231  1.1  tron   endElementHandler = end;
   1232  1.1  tron }
   1233  1.1  tron 
   1234  1.1  tron void XMLCALL
   1235  1.1  tron XML_SetCharacterDataHandler(XML_Parser parser,
   1236  1.1  tron                             XML_CharacterDataHandler handler)
   1237  1.1  tron {
   1238  1.1  tron   characterDataHandler = handler;
   1239  1.1  tron }
   1240  1.1  tron 
   1241  1.1  tron void XMLCALL
   1242  1.1  tron XML_SetProcessingInstructionHandler(XML_Parser parser,
   1243  1.1  tron                                     XML_ProcessingInstructionHandler handler)
   1244  1.1  tron {
   1245  1.1  tron   processingInstructionHandler = handler;
   1246  1.1  tron }
   1247  1.1  tron 
   1248  1.1  tron void XMLCALL
   1249  1.1  tron XML_SetCommentHandler(XML_Parser parser,
   1250  1.1  tron                       XML_CommentHandler handler)
   1251  1.1  tron {
   1252  1.1  tron   commentHandler = handler;
   1253  1.1  tron }
   1254  1.1  tron 
   1255  1.1  tron void XMLCALL
   1256  1.1  tron XML_SetCdataSectionHandler(XML_Parser parser,
   1257  1.1  tron                            XML_StartCdataSectionHandler start,
   1258  1.1  tron                            XML_EndCdataSectionHandler end)
   1259  1.1  tron {
   1260  1.1  tron   startCdataSectionHandler = start;
   1261  1.1  tron   endCdataSectionHandler = end;
   1262  1.1  tron }
   1263  1.1  tron 
   1264  1.1  tron void XMLCALL
   1265  1.1  tron XML_SetStartCdataSectionHandler(XML_Parser parser,
   1266  1.1  tron                                 XML_StartCdataSectionHandler start) {
   1267  1.1  tron   startCdataSectionHandler = start;
   1268  1.1  tron }
   1269  1.1  tron 
   1270  1.1  tron void XMLCALL
   1271  1.1  tron XML_SetEndCdataSectionHandler(XML_Parser parser,
   1272  1.1  tron                               XML_EndCdataSectionHandler end) {
   1273  1.1  tron   endCdataSectionHandler = end;
   1274  1.1  tron }
   1275  1.1  tron 
   1276  1.1  tron void XMLCALL
   1277  1.1  tron XML_SetDefaultHandler(XML_Parser parser,
   1278  1.1  tron                       XML_DefaultHandler handler)
   1279  1.1  tron {
   1280  1.1  tron   defaultHandler = handler;
   1281  1.1  tron   defaultExpandInternalEntities = XML_FALSE;
   1282  1.1  tron }
   1283  1.1  tron 
   1284  1.1  tron void XMLCALL
   1285  1.1  tron XML_SetDefaultHandlerExpand(XML_Parser parser,
   1286  1.1  tron                             XML_DefaultHandler handler)
   1287  1.1  tron {
   1288  1.1  tron   defaultHandler = handler;
   1289  1.1  tron   defaultExpandInternalEntities = XML_TRUE;
   1290  1.1  tron }
   1291  1.1  tron 
   1292  1.1  tron void XMLCALL
   1293  1.1  tron XML_SetDoctypeDeclHandler(XML_Parser parser,
   1294  1.1  tron                           XML_StartDoctypeDeclHandler start,
   1295  1.1  tron                           XML_EndDoctypeDeclHandler end)
   1296  1.1  tron {
   1297  1.1  tron   startDoctypeDeclHandler = start;
   1298  1.1  tron   endDoctypeDeclHandler = end;
   1299  1.1  tron }
   1300  1.1  tron 
   1301  1.1  tron void XMLCALL
   1302  1.1  tron XML_SetStartDoctypeDeclHandler(XML_Parser parser,
   1303  1.1  tron                                XML_StartDoctypeDeclHandler start) {
   1304  1.1  tron   startDoctypeDeclHandler = start;
   1305  1.1  tron }
   1306  1.1  tron 
   1307  1.1  tron void XMLCALL
   1308  1.1  tron XML_SetEndDoctypeDeclHandler(XML_Parser parser,
   1309  1.1  tron                              XML_EndDoctypeDeclHandler end) {
   1310  1.1  tron   endDoctypeDeclHandler = end;
   1311  1.1  tron }
   1312  1.1  tron 
   1313  1.1  tron void XMLCALL
   1314  1.1  tron XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
   1315  1.1  tron                                  XML_UnparsedEntityDeclHandler handler)
   1316  1.1  tron {
   1317  1.1  tron   unparsedEntityDeclHandler = handler;
   1318  1.1  tron }
   1319  1.1  tron 
   1320  1.1  tron void XMLCALL
   1321  1.1  tron XML_SetNotationDeclHandler(XML_Parser parser,
   1322  1.1  tron                            XML_NotationDeclHandler handler)
   1323  1.1  tron {
   1324  1.1  tron   notationDeclHandler = handler;
   1325  1.1  tron }
   1326  1.1  tron 
   1327  1.1  tron void XMLCALL
   1328  1.1  tron XML_SetNamespaceDeclHandler(XML_Parser parser,
   1329  1.1  tron                             XML_StartNamespaceDeclHandler start,
   1330  1.1  tron                             XML_EndNamespaceDeclHandler end)
   1331  1.1  tron {
   1332  1.1  tron   startNamespaceDeclHandler = start;
   1333  1.1  tron   endNamespaceDeclHandler = end;
   1334  1.1  tron }
   1335  1.1  tron 
   1336  1.1  tron void XMLCALL
   1337  1.1  tron XML_SetStartNamespaceDeclHandler(XML_Parser parser,
   1338  1.1  tron                                  XML_StartNamespaceDeclHandler start) {
   1339  1.1  tron   startNamespaceDeclHandler = start;
   1340  1.1  tron }
   1341  1.1  tron 
   1342  1.1  tron void XMLCALL
   1343  1.1  tron XML_SetEndNamespaceDeclHandler(XML_Parser parser,
   1344  1.1  tron                                XML_EndNamespaceDeclHandler end) {
   1345  1.1  tron   endNamespaceDeclHandler = end;
   1346  1.1  tron }
   1347  1.1  tron 
   1348  1.1  tron void XMLCALL
   1349  1.1  tron XML_SetNotStandaloneHandler(XML_Parser parser,
   1350  1.1  tron                             XML_NotStandaloneHandler handler)
   1351  1.1  tron {
   1352  1.1  tron   notStandaloneHandler = handler;
   1353  1.1  tron }
   1354  1.1  tron 
   1355  1.1  tron void XMLCALL
   1356  1.1  tron XML_SetExternalEntityRefHandler(XML_Parser parser,
   1357  1.1  tron                                 XML_ExternalEntityRefHandler handler)
   1358  1.1  tron {
   1359  1.1  tron   externalEntityRefHandler = handler;
   1360  1.1  tron }
   1361  1.1  tron 
   1362  1.1  tron void XMLCALL
   1363  1.1  tron XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
   1364  1.1  tron {
   1365  1.1  tron   if (arg)
   1366  1.1  tron     externalEntityRefHandlerArg = (XML_Parser)arg;
   1367  1.1  tron   else
   1368  1.1  tron     externalEntityRefHandlerArg = parser;
   1369  1.1  tron }
   1370  1.1  tron 
   1371  1.1  tron void XMLCALL
   1372  1.1  tron XML_SetSkippedEntityHandler(XML_Parser parser,
   1373  1.1  tron                             XML_SkippedEntityHandler handler)
   1374  1.1  tron {
   1375  1.1  tron   skippedEntityHandler = handler;
   1376  1.1  tron }
   1377  1.1  tron 
   1378  1.1  tron void XMLCALL
   1379  1.1  tron XML_SetUnknownEncodingHandler(XML_Parser parser,
   1380  1.1  tron                               XML_UnknownEncodingHandler handler,
   1381  1.1  tron                               void *data)
   1382  1.1  tron {
   1383  1.1  tron   unknownEncodingHandler = handler;
   1384  1.1  tron   unknownEncodingHandlerData = data;
   1385  1.1  tron }
   1386  1.1  tron 
   1387  1.1  tron void XMLCALL
   1388  1.1  tron XML_SetElementDeclHandler(XML_Parser parser,
   1389  1.1  tron                           XML_ElementDeclHandler eldecl)
   1390  1.1  tron {
   1391  1.1  tron   elementDeclHandler = eldecl;
   1392  1.1  tron }
   1393  1.1  tron 
   1394  1.1  tron void XMLCALL
   1395  1.1  tron XML_SetAttlistDeclHandler(XML_Parser parser,
   1396  1.1  tron                           XML_AttlistDeclHandler attdecl)
   1397  1.1  tron {
   1398  1.1  tron   attlistDeclHandler = attdecl;
   1399  1.1  tron }
   1400  1.1  tron 
   1401  1.1  tron void XMLCALL
   1402  1.1  tron XML_SetEntityDeclHandler(XML_Parser parser,
   1403  1.1  tron                          XML_EntityDeclHandler handler)
   1404  1.1  tron {
   1405  1.1  tron   entityDeclHandler = handler;
   1406  1.1  tron }
   1407  1.1  tron 
   1408  1.1  tron void XMLCALL
   1409  1.1  tron XML_SetXmlDeclHandler(XML_Parser parser,
   1410  1.1  tron                       XML_XmlDeclHandler handler) {
   1411  1.1  tron   xmlDeclHandler = handler;
   1412  1.1  tron }
   1413  1.1  tron 
   1414  1.1  tron int XMLCALL
   1415  1.1  tron XML_SetParamEntityParsing(XML_Parser parser,
   1416  1.1  tron                           enum XML_ParamEntityParsing peParsing)
   1417  1.1  tron {
   1418  1.1  tron   /* block after XML_Parse()/XML_ParseBuffer() has been called */
   1419  1.1  tron   if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
   1420  1.1  tron     return 0;
   1421  1.1  tron #ifdef XML_DTD
   1422  1.1  tron   paramEntityParsing = peParsing;
   1423  1.1  tron   return 1;
   1424  1.1  tron #else
   1425  1.1  tron   return peParsing == XML_PARAM_ENTITY_PARSING_NEVER;
   1426  1.1  tron #endif
   1427  1.1  tron }
   1428  1.1  tron 
   1429  1.1  tron enum XML_Status XMLCALL
   1430  1.1  tron XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
   1431  1.1  tron {
   1432  1.1  tron   switch (ps_parsing) {
   1433  1.1  tron   case XML_SUSPENDED:
   1434  1.1  tron     errorCode = XML_ERROR_SUSPENDED;
   1435  1.1  tron     return XML_STATUS_ERROR;
   1436  1.1  tron   case XML_FINISHED:
   1437  1.1  tron     errorCode = XML_ERROR_FINISHED;
   1438  1.1  tron     return XML_STATUS_ERROR;
   1439  1.1  tron   default:
   1440  1.1  tron     ps_parsing = XML_PARSING;
   1441  1.1  tron   }
   1442  1.1  tron 
   1443  1.1  tron   if (len == 0) {
   1444  1.1  tron     ps_finalBuffer = (XML_Bool)isFinal;
   1445  1.1  tron     if (!isFinal)
   1446  1.1  tron       return XML_STATUS_OK;
   1447  1.1  tron     positionPtr = bufferPtr;
   1448  1.1  tron     parseEndPtr = bufferEnd;
   1449  1.1  tron 
   1450  1.1  tron     /* If data are left over from last buffer, and we now know that these
   1451  1.1  tron        data are the final chunk of input, then we have to check them again
   1452  1.1  tron        to detect errors based on that fact.
   1453  1.1  tron     */
   1454  1.1  tron     errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
   1455  1.1  tron 
   1456  1.1  tron     if (errorCode == XML_ERROR_NONE) {
   1457  1.1  tron       switch (ps_parsing) {
   1458  1.1  tron       case XML_SUSPENDED:
   1459  1.1  tron         XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
   1460  1.1  tron         positionPtr = bufferPtr;
   1461  1.1  tron         return XML_STATUS_SUSPENDED;
   1462  1.1  tron       case XML_INITIALIZED:
   1463  1.1  tron       case XML_PARSING:
   1464  1.1  tron         ps_parsing = XML_FINISHED;
   1465  1.1  tron         /* fall through */
   1466  1.1  tron       default:
   1467  1.1  tron         return XML_STATUS_OK;
   1468  1.1  tron       }
   1469  1.1  tron     }
   1470  1.1  tron     eventEndPtr = eventPtr;
   1471  1.1  tron     processor = errorProcessor;
   1472  1.1  tron     return XML_STATUS_ERROR;
   1473  1.1  tron   }
   1474  1.1  tron #ifndef XML_CONTEXT_BYTES
   1475  1.1  tron   else if (bufferPtr == bufferEnd) {
   1476  1.1  tron     const char *end;
   1477  1.1  tron     int nLeftOver;
   1478  1.1  tron     enum XML_Error result;
   1479  1.1  tron     parseEndByteIndex += len;
   1480  1.1  tron     positionPtr = s;
   1481  1.1  tron     ps_finalBuffer = (XML_Bool)isFinal;
   1482  1.1  tron 
   1483  1.1  tron     errorCode = processor(parser, s, parseEndPtr = s + len, &end);
   1484  1.1  tron 
   1485  1.1  tron     if (errorCode != XML_ERROR_NONE) {
   1486  1.1  tron       eventEndPtr = eventPtr;
   1487  1.1  tron       processor = errorProcessor;
   1488  1.1  tron       return XML_STATUS_ERROR;
   1489  1.1  tron     }
   1490  1.1  tron     else {
   1491  1.1  tron       switch (ps_parsing) {
   1492  1.1  tron       case XML_SUSPENDED:
   1493  1.1  tron         result = XML_STATUS_SUSPENDED;
   1494  1.1  tron         break;
   1495  1.1  tron       case XML_INITIALIZED:
   1496  1.1  tron       case XML_PARSING:
   1497  1.1  tron         result = XML_STATUS_OK;
   1498  1.1  tron         if (isFinal) {
   1499  1.1  tron           ps_parsing = XML_FINISHED;
   1500  1.1  tron           return result;
   1501  1.1  tron         }
   1502  1.1  tron       }
   1503  1.1  tron     }
   1504  1.1  tron 
   1505  1.1  tron     XmlUpdatePosition(encoding, positionPtr, end, &position);
   1506  1.1  tron     nLeftOver = s + len - end;
   1507  1.1  tron     if (nLeftOver) {
   1508  1.1  tron       if (buffer == NULL || nLeftOver > bufferLim - buffer) {
   1509  1.1  tron         /* FIXME avoid integer overflow */
   1510  1.1  tron         char *temp;
   1511  1.1  tron         temp = (buffer == NULL
   1512  1.1  tron                 ? (char *)MALLOC(len * 2)
   1513  1.1  tron                 : (char *)REALLOC(buffer, len * 2));
   1514  1.1  tron         if (temp == NULL) {
   1515  1.1  tron           errorCode = XML_ERROR_NO_MEMORY;
   1516  1.1  tron           return XML_STATUS_ERROR;
   1517  1.1  tron         }
   1518  1.1  tron         buffer = temp;
   1519  1.1  tron         if (!buffer) {
   1520  1.1  tron           errorCode = XML_ERROR_NO_MEMORY;
   1521  1.1  tron           eventPtr = eventEndPtr = NULL;
   1522  1.1  tron           processor = errorProcessor;
   1523  1.1  tron           return XML_STATUS_ERROR;
   1524  1.1  tron         }
   1525  1.1  tron         bufferLim = buffer + len * 2;
   1526  1.1  tron       }
   1527  1.1  tron       memcpy(buffer, end, nLeftOver);
   1528  1.1  tron     }
   1529  1.1  tron     bufferPtr = buffer;
   1530  1.1  tron     bufferEnd = buffer + nLeftOver;
   1531  1.1  tron     positionPtr = bufferPtr;
   1532  1.1  tron     parseEndPtr = bufferEnd;
   1533  1.1  tron     eventPtr = bufferPtr;
   1534  1.1  tron     eventEndPtr = bufferPtr;
   1535  1.1  tron     return result;
   1536  1.1  tron   }
   1537  1.1  tron #endif  /* not defined XML_CONTEXT_BYTES */
   1538  1.1  tron   else {
   1539  1.1  tron     void *buff = XML_GetBuffer(parser, len);
   1540  1.1  tron     if (buff == NULL)
   1541  1.1  tron       return XML_STATUS_ERROR;
   1542  1.1  tron     else {
   1543  1.1  tron       memcpy(buff, s, len);
   1544  1.1  tron       return XML_ParseBuffer(parser, len, isFinal);
   1545  1.1  tron     }
   1546  1.1  tron   }
   1547  1.1  tron }
   1548  1.1  tron 
   1549  1.1  tron enum XML_Status XMLCALL
   1550  1.1  tron XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
   1551  1.1  tron {
   1552  1.1  tron   const char *start;
   1553  1.1  tron   enum XML_Status result = XML_STATUS_OK;
   1554  1.1  tron 
   1555  1.1  tron   switch (ps_parsing) {
   1556  1.1  tron   case XML_SUSPENDED:
   1557  1.1  tron     errorCode = XML_ERROR_SUSPENDED;
   1558  1.1  tron     return XML_STATUS_ERROR;
   1559  1.1  tron   case XML_FINISHED:
   1560  1.1  tron     errorCode = XML_ERROR_FINISHED;
   1561  1.1  tron     return XML_STATUS_ERROR;
   1562  1.1  tron   default:
   1563  1.1  tron     ps_parsing = XML_PARSING;
   1564  1.1  tron   }
   1565  1.1  tron 
   1566  1.1  tron   start = bufferPtr;
   1567  1.1  tron   positionPtr = start;
   1568  1.1  tron   bufferEnd += len;
   1569  1.1  tron   parseEndPtr = bufferEnd;
   1570  1.1  tron   parseEndByteIndex += len;
   1571  1.1  tron   ps_finalBuffer = (XML_Bool)isFinal;
   1572  1.1  tron 
   1573  1.1  tron   errorCode = processor(parser, start, parseEndPtr, &bufferPtr);
   1574  1.1  tron 
   1575  1.1  tron   if (errorCode != XML_ERROR_NONE) {
   1576  1.1  tron     eventEndPtr = eventPtr;
   1577  1.1  tron     processor = errorProcessor;
   1578  1.1  tron     return XML_STATUS_ERROR;
   1579  1.1  tron   }
   1580  1.1  tron   else {
   1581  1.1  tron     switch (ps_parsing) {
   1582  1.1  tron     case XML_SUSPENDED:
   1583  1.1  tron       result = XML_STATUS_SUSPENDED;
   1584  1.1  tron       break;
   1585  1.1  tron     case XML_INITIALIZED:
   1586  1.1  tron     case XML_PARSING:
   1587  1.1  tron       if (isFinal) {
   1588  1.1  tron         ps_parsing = XML_FINISHED;
   1589  1.1  tron         return result;
   1590  1.1  tron       }
   1591  1.1  tron     default: ;  /* should not happen */
   1592  1.1  tron     }
   1593  1.1  tron   }
   1594  1.1  tron 
   1595  1.1  tron   XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
   1596  1.1  tron   positionPtr = bufferPtr;
   1597  1.1  tron   return result;
   1598  1.1  tron }
   1599  1.1  tron 
   1600  1.1  tron void * XMLCALL
   1601  1.1  tron XML_GetBuffer(XML_Parser parser, int len)
   1602  1.1  tron {
   1603  1.1  tron   switch (ps_parsing) {
   1604  1.1  tron   case XML_SUSPENDED:
   1605  1.1  tron     errorCode = XML_ERROR_SUSPENDED;
   1606  1.1  tron     return NULL;
   1607  1.1  tron   case XML_FINISHED:
   1608  1.1  tron     errorCode = XML_ERROR_FINISHED;
   1609  1.1  tron     return NULL;
   1610  1.1  tron   default: ;
   1611  1.1  tron   }
   1612  1.1  tron 
   1613  1.1  tron   if (len > bufferLim - bufferEnd) {
   1614  1.1  tron     /* FIXME avoid integer overflow */
   1615  1.1  tron     int neededSize = len + (int)(bufferEnd - bufferPtr);
   1616  1.1  tron #ifdef XML_CONTEXT_BYTES
   1617  1.1  tron     int keep = (int)(bufferPtr - buffer);
   1618  1.1  tron 
   1619  1.1  tron     if (keep > XML_CONTEXT_BYTES)
   1620  1.1  tron       keep = XML_CONTEXT_BYTES;
   1621  1.1  tron     neededSize += keep;
   1622  1.1  tron #endif  /* defined XML_CONTEXT_BYTES */
   1623  1.1  tron     if (neededSize  <= bufferLim - buffer) {
   1624  1.1  tron #ifdef XML_CONTEXT_BYTES
   1625  1.1  tron       if (keep < bufferPtr - buffer) {
   1626  1.1  tron         int offset = (int)(bufferPtr - buffer) - keep;
   1627  1.1  tron         memmove(buffer, &buffer[offset], bufferEnd - bufferPtr + keep);
   1628  1.1  tron         bufferEnd -= offset;
   1629  1.1  tron         bufferPtr -= offset;
   1630  1.1  tron       }
   1631  1.1  tron #else
   1632  1.1  tron       memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
   1633  1.1  tron       bufferEnd = buffer + (bufferEnd - bufferPtr);
   1634  1.1  tron       bufferPtr = buffer;
   1635  1.1  tron #endif  /* not defined XML_CONTEXT_BYTES */
   1636  1.1  tron     }
   1637  1.1  tron     else {
   1638  1.1  tron       char *newBuf;
   1639  1.1  tron       int bufferSize = (int)(bufferLim - bufferPtr);
   1640  1.1  tron       if (bufferSize == 0)
   1641  1.1  tron         bufferSize = INIT_BUFFER_SIZE;
   1642  1.1  tron       do {
   1643  1.1  tron         bufferSize *= 2;
   1644  1.1  tron       } while (bufferSize < neededSize);
   1645  1.1  tron       newBuf = (char *)MALLOC(bufferSize);
   1646  1.1  tron       if (newBuf == 0) {
   1647  1.1  tron         errorCode = XML_ERROR_NO_MEMORY;
   1648  1.1  tron         return NULL;
   1649  1.1  tron       }
   1650  1.1  tron       bufferLim = newBuf + bufferSize;
   1651  1.1  tron #ifdef XML_CONTEXT_BYTES
   1652  1.1  tron       if (bufferPtr) {
   1653  1.1  tron         int keep = (int)(bufferPtr - buffer);
   1654  1.1  tron         if (keep > XML_CONTEXT_BYTES)
   1655  1.1  tron           keep = XML_CONTEXT_BYTES;
   1656  1.1  tron         memcpy(newBuf, &bufferPtr[-keep], bufferEnd - bufferPtr + keep);
   1657  1.1  tron         FREE(buffer);
   1658  1.1  tron         buffer = newBuf;
   1659  1.1  tron         bufferEnd = buffer + (bufferEnd - bufferPtr) + keep;
   1660  1.1  tron         bufferPtr = buffer + keep;
   1661  1.1  tron       }
   1662  1.1  tron       else {
   1663  1.1  tron         bufferEnd = newBuf + (bufferEnd - bufferPtr);
   1664  1.1  tron         bufferPtr = buffer = newBuf;
   1665  1.1  tron       }
   1666  1.1  tron #else
   1667  1.1  tron       if (bufferPtr) {
   1668  1.1  tron         memcpy(newBuf, bufferPtr, bufferEnd - bufferPtr);
   1669  1.1  tron         FREE(buffer);
   1670  1.1  tron       }
   1671  1.1  tron       bufferEnd = newBuf + (bufferEnd - bufferPtr);
   1672  1.1  tron       bufferPtr = buffer = newBuf;
   1673  1.1  tron #endif  /* not defined XML_CONTEXT_BYTES */
   1674  1.1  tron     }
   1675  1.1  tron   }
   1676  1.1  tron   return bufferEnd;
   1677  1.1  tron }
   1678  1.1  tron 
   1679  1.1  tron enum XML_Status XMLCALL
   1680  1.1  tron XML_StopParser(XML_Parser parser, XML_Bool resumable)
   1681  1.1  tron {
   1682  1.1  tron   switch (ps_parsing) {
   1683  1.1  tron   case XML_SUSPENDED:
   1684  1.1  tron     if (resumable) {
   1685  1.1  tron       errorCode = XML_ERROR_SUSPENDED;
   1686  1.1  tron       return XML_STATUS_ERROR;
   1687  1.1  tron     }
   1688  1.1  tron     ps_parsing = XML_FINISHED;
   1689  1.1  tron     break;
   1690  1.1  tron   case XML_FINISHED:
   1691  1.1  tron     errorCode = XML_ERROR_FINISHED;
   1692  1.1  tron     return XML_STATUS_ERROR;
   1693  1.1  tron   default:
   1694  1.1  tron     if (resumable) {
   1695  1.1  tron #ifdef XML_DTD
   1696  1.1  tron       if (isParamEntity) {
   1697  1.1  tron         errorCode = XML_ERROR_SUSPEND_PE;
   1698  1.1  tron         return XML_STATUS_ERROR;
   1699  1.1  tron       }
   1700  1.1  tron #endif
   1701  1.1  tron       ps_parsing = XML_SUSPENDED;
   1702  1.1  tron     }
   1703  1.1  tron     else
   1704  1.1  tron       ps_parsing = XML_FINISHED;
   1705  1.1  tron   }
   1706  1.1  tron   return XML_STATUS_OK;
   1707  1.1  tron }
   1708  1.1  tron 
   1709  1.1  tron enum XML_Status XMLCALL
   1710  1.1  tron XML_ResumeParser(XML_Parser parser)
   1711  1.1  tron {
   1712  1.1  tron   enum XML_Status result = XML_STATUS_OK;
   1713  1.1  tron 
   1714  1.1  tron   if (ps_parsing != XML_SUSPENDED) {
   1715  1.1  tron     errorCode = XML_ERROR_NOT_SUSPENDED;
   1716  1.1  tron     return XML_STATUS_ERROR;
   1717  1.1  tron   }
   1718  1.1  tron   ps_parsing = XML_PARSING;
   1719  1.1  tron 
   1720  1.1  tron   errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
   1721  1.1  tron 
   1722  1.1  tron   if (errorCode != XML_ERROR_NONE) {
   1723  1.1  tron     eventEndPtr = eventPtr;
   1724  1.1  tron     processor = errorProcessor;
   1725  1.1  tron     return XML_STATUS_ERROR;
   1726  1.1  tron   }
   1727  1.1  tron   else {
   1728  1.1  tron     switch (ps_parsing) {
   1729  1.1  tron     case XML_SUSPENDED:
   1730  1.1  tron       result = XML_STATUS_SUSPENDED;
   1731  1.1  tron       break;
   1732  1.1  tron     case XML_INITIALIZED:
   1733  1.1  tron     case XML_PARSING:
   1734  1.1  tron       if (ps_finalBuffer) {
   1735  1.1  tron         ps_parsing = XML_FINISHED;
   1736  1.1  tron         return result;
   1737  1.1  tron       }
   1738  1.1  tron     default: ;
   1739  1.1  tron     }
   1740  1.1  tron   }
   1741  1.1  tron 
   1742  1.1  tron   XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
   1743  1.1  tron   positionPtr = bufferPtr;
   1744  1.1  tron   return result;
   1745  1.1  tron }
   1746  1.1  tron 
   1747  1.1  tron void XMLCALL
   1748  1.1  tron XML_GetParsingStatus(XML_Parser parser, XML_ParsingStatus *status)
   1749  1.1  tron {
   1750  1.1  tron   assert(status != NULL);
   1751  1.1  tron   *status = parser->m_parsingStatus;
   1752  1.1  tron }
   1753  1.1  tron 
   1754  1.1  tron enum XML_Error XMLCALL
   1755  1.1  tron XML_GetErrorCode(XML_Parser parser)
   1756  1.1  tron {
   1757  1.1  tron   return errorCode;
   1758  1.1  tron }
   1759  1.1  tron 
   1760  1.1  tron XML_Index XMLCALL
   1761  1.1  tron XML_GetCurrentByteIndex(XML_Parser parser)
   1762  1.1  tron {
   1763  1.1  tron   if (eventPtr)
   1764  1.1  tron     return parseEndByteIndex - (parseEndPtr - eventPtr);
   1765  1.1  tron   return -1;
   1766  1.1  tron }
   1767  1.1  tron 
   1768  1.1  tron int XMLCALL
   1769  1.1  tron XML_GetCurrentByteCount(XML_Parser parser)
   1770  1.1  tron {
   1771  1.1  tron   if (eventEndPtr && eventPtr)
   1772  1.1  tron     return (int)(eventEndPtr - eventPtr);
   1773  1.1  tron   return 0;
   1774  1.1  tron }
   1775  1.1  tron 
   1776  1.1  tron const char * XMLCALL
   1777  1.1  tron XML_GetInputContext(XML_Parser parser, int *offset, int *size)
   1778  1.1  tron {
   1779  1.1  tron #ifdef XML_CONTEXT_BYTES
   1780  1.1  tron   if (eventPtr && buffer) {
   1781  1.1  tron     *offset = (int)(eventPtr - buffer);
   1782  1.1  tron     *size   = (int)(bufferEnd - buffer);
   1783  1.1  tron     return buffer;
   1784  1.1  tron   }
   1785  1.1  tron #endif /* defined XML_CONTEXT_BYTES */
   1786  1.1  tron   return (char *) 0;
   1787  1.1  tron }
   1788  1.1  tron 
   1789  1.1  tron XML_Size XMLCALL
   1790  1.1  tron XML_GetCurrentLineNumber(XML_Parser parser)
   1791  1.1  tron {
   1792  1.1  tron   if (eventPtr && eventPtr >= positionPtr) {
   1793  1.1  tron     XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
   1794  1.1  tron     positionPtr = eventPtr;
   1795  1.1  tron   }
   1796  1.1  tron   return position.lineNumber + 1;
   1797  1.1  tron }
   1798  1.1  tron 
   1799  1.1  tron XML_Size XMLCALL
   1800  1.1  tron XML_GetCurrentColumnNumber(XML_Parser parser)
   1801  1.1  tron {
   1802  1.1  tron   if (eventPtr && eventPtr >= positionPtr) {
   1803  1.1  tron     XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
   1804  1.1  tron     positionPtr = eventPtr;
   1805  1.1  tron   }
   1806  1.1  tron   return position.columnNumber;
   1807  1.1  tron }
   1808  1.1  tron 
   1809  1.1  tron void XMLCALL
   1810  1.1  tron XML_FreeContentModel(XML_Parser parser, XML_Content *model)
   1811  1.1  tron {
   1812  1.1  tron   FREE(model);
   1813  1.1  tron }
   1814  1.1  tron 
   1815  1.1  tron void * XMLCALL
   1816  1.1  tron XML_MemMalloc(XML_Parser parser, size_t size)
   1817  1.1  tron {
   1818  1.1  tron   return MALLOC(size);
   1819  1.1  tron }
   1820  1.1  tron 
   1821  1.1  tron void * XMLCALL
   1822  1.1  tron XML_MemRealloc(XML_Parser parser, void *ptr, size_t size)
   1823  1.1  tron {
   1824  1.1  tron   return REALLOC(ptr, size);
   1825  1.1  tron }
   1826  1.1  tron 
   1827  1.1  tron void XMLCALL
   1828  1.1  tron XML_MemFree(XML_Parser parser, void *ptr)
   1829  1.1  tron {
   1830  1.1  tron   FREE(ptr);
   1831  1.1  tron }
   1832  1.1  tron 
   1833  1.1  tron void XMLCALL
   1834  1.1  tron XML_DefaultCurrent(XML_Parser parser)
   1835  1.1  tron {
   1836  1.1  tron   if (defaultHandler) {
   1837  1.1  tron     if (openInternalEntities)
   1838  1.1  tron       reportDefault(parser,
   1839  1.1  tron                     internalEncoding,
   1840  1.1  tron                     openInternalEntities->internalEventPtr,
   1841  1.1  tron                     openInternalEntities->internalEventEndPtr);
   1842  1.1  tron     else
   1843  1.1  tron       reportDefault(parser, encoding, eventPtr, eventEndPtr);
   1844  1.1  tron   }
   1845  1.1  tron }
   1846  1.1  tron 
   1847  1.1  tron const XML_LChar * XMLCALL
   1848  1.1  tron XML_ErrorString(enum XML_Error code)
   1849  1.1  tron {
   1850  1.1  tron   static const XML_LChar* const message[] = {
   1851  1.1  tron     0,
   1852  1.1  tron     XML_L("out of memory"),
   1853  1.1  tron     XML_L("syntax error"),
   1854  1.1  tron     XML_L("no element found"),
   1855  1.1  tron     XML_L("not well-formed (invalid token)"),
   1856  1.1  tron     XML_L("unclosed token"),
   1857  1.1  tron     XML_L("partial character"),
   1858  1.1  tron     XML_L("mismatched tag"),
   1859  1.1  tron     XML_L("duplicate attribute"),
   1860  1.1  tron     XML_L("junk after document element"),
   1861  1.1  tron     XML_L("illegal parameter entity reference"),
   1862  1.1  tron     XML_L("undefined entity"),
   1863  1.1  tron     XML_L("recursive entity reference"),
   1864  1.1  tron     XML_L("asynchronous entity"),
   1865  1.1  tron     XML_L("reference to invalid character number"),
   1866  1.1  tron     XML_L("reference to binary entity"),
   1867  1.1  tron     XML_L("reference to external entity in attribute"),
   1868  1.1  tron     XML_L("XML or text declaration not at start of entity"),
   1869  1.1  tron     XML_L("unknown encoding"),
   1870  1.1  tron     XML_L("encoding specified in XML declaration is incorrect"),
   1871  1.1  tron     XML_L("unclosed CDATA section"),
   1872  1.1  tron     XML_L("error in processing external entity reference"),
   1873  1.1  tron     XML_L("document is not standalone"),
   1874  1.1  tron     XML_L("unexpected parser state - please send a bug report"),
   1875  1.1  tron     XML_L("entity declared in parameter entity"),
   1876  1.1  tron     XML_L("requested feature requires XML_DTD support in Expat"),
   1877  1.1  tron     XML_L("cannot change setting once parsing has begun"),
   1878  1.1  tron     XML_L("unbound prefix"),
   1879  1.1  tron     XML_L("must not undeclare prefix"),
   1880  1.1  tron     XML_L("incomplete markup in parameter entity"),
   1881  1.1  tron     XML_L("XML declaration not well-formed"),
   1882  1.1  tron     XML_L("text declaration not well-formed"),
   1883  1.1  tron     XML_L("illegal character(s) in public id"),
   1884  1.1  tron     XML_L("parser suspended"),
   1885  1.1  tron     XML_L("parser not suspended"),
   1886  1.1  tron     XML_L("parsing aborted"),
   1887  1.1  tron     XML_L("parsing finished"),
   1888  1.1  tron     XML_L("cannot suspend in external parameter entity"),
   1889  1.1  tron     XML_L("reserved prefix (xml) must not be undeclared or bound to another namespace name"),
   1890  1.1  tron     XML_L("reserved prefix (xmlns) must not be declared or undeclared"),
   1891  1.1  tron     XML_L("prefix must not be bound to one of the reserved namespace names")
   1892  1.1  tron   };
   1893  1.1  tron   if (code > 0 && code < sizeof(message)/sizeof(message[0]))
   1894  1.1  tron     return message[code];
   1895  1.1  tron   return NULL;
   1896  1.1  tron }
   1897  1.1  tron 
   1898  1.1  tron const XML_LChar * XMLCALL
   1899  1.1  tron XML_ExpatVersion(void) {
   1900  1.1  tron 
   1901  1.1  tron   /* V1 is used to string-ize the version number. However, it would
   1902  1.1  tron      string-ize the actual version macro *names* unless we get them
   1903  1.1  tron      substituted before being passed to V1. CPP is defined to expand
   1904  1.1  tron      a macro, then rescan for more expansions. Thus, we use V2 to expand
   1905  1.1  tron      the version macros, then CPP will expand the resulting V1() macro
   1906  1.1  tron      with the correct numerals. */
   1907  1.1  tron   /* ### I'm assuming cpp is portable in this respect... */
   1908  1.1  tron 
   1909  1.1  tron #define V1(a,b,c) XML_L(#a)XML_L(".")XML_L(#b)XML_L(".")XML_L(#c)
   1910  1.1  tron #define V2(a,b,c) XML_L("expat_")V1(a,b,c)
   1911  1.1  tron 
   1912  1.1  tron   return V2(XML_MAJOR_VERSION, XML_MINOR_VERSION, XML_MICRO_VERSION);
   1913  1.1  tron 
   1914  1.1  tron #undef V1
   1915  1.1  tron #undef V2
   1916  1.1  tron }
   1917  1.1  tron 
   1918  1.1  tron XML_Expat_Version XMLCALL
   1919  1.1  tron XML_ExpatVersionInfo(void)
   1920  1.1  tron {
   1921  1.1  tron   XML_Expat_Version version;
   1922  1.1  tron 
   1923  1.1  tron   version.major = XML_MAJOR_VERSION;
   1924  1.1  tron   version.minor = XML_MINOR_VERSION;
   1925  1.1  tron   version.micro = XML_MICRO_VERSION;
   1926  1.1  tron 
   1927  1.1  tron   return version;
   1928  1.1  tron }
   1929  1.1  tron 
   1930  1.1  tron const XML_Feature * XMLCALL
   1931  1.1  tron XML_GetFeatureList(void)
   1932  1.1  tron {
   1933  1.1  tron   static const XML_Feature features[] = {
   1934  1.1  tron     {XML_FEATURE_SIZEOF_XML_CHAR,  XML_L("sizeof(XML_Char)"),
   1935  1.1  tron      sizeof(XML_Char)},
   1936  1.1  tron     {XML_FEATURE_SIZEOF_XML_LCHAR, XML_L("sizeof(XML_LChar)"),
   1937  1.1  tron      sizeof(XML_LChar)},
   1938  1.1  tron #ifdef XML_UNICODE
   1939  1.1  tron     {XML_FEATURE_UNICODE,          XML_L("XML_UNICODE"), 0},
   1940  1.1  tron #endif
   1941  1.1  tron #ifdef XML_UNICODE_WCHAR_T
   1942  1.1  tron     {XML_FEATURE_UNICODE_WCHAR_T,  XML_L("XML_UNICODE_WCHAR_T"), 0},
   1943  1.1  tron #endif
   1944  1.1  tron #ifdef XML_DTD
   1945  1.1  tron     {XML_FEATURE_DTD,              XML_L("XML_DTD"), 0},
   1946  1.1  tron #endif
   1947  1.1  tron #ifdef XML_CONTEXT_BYTES
   1948  1.1  tron     {XML_FEATURE_CONTEXT_BYTES,    XML_L("XML_CONTEXT_BYTES"),
   1949  1.1  tron      XML_CONTEXT_BYTES},
   1950  1.1  tron #endif
   1951  1.1  tron #ifdef XML_MIN_SIZE
   1952  1.1  tron     {XML_FEATURE_MIN_SIZE,         XML_L("XML_MIN_SIZE"), 0},
   1953  1.1  tron #endif
   1954  1.1  tron #ifdef XML_NS
   1955  1.1  tron     {XML_FEATURE_NS,               XML_L("XML_NS"), 0},
   1956  1.1  tron #endif
   1957  1.1  tron #ifdef XML_LARGE_SIZE
   1958  1.1  tron     {XML_FEATURE_LARGE_SIZE,       XML_L("XML_LARGE_SIZE"), 0},
   1959  1.1  tron #endif
   1960  1.1  tron     {XML_FEATURE_END,              NULL, 0}
   1961  1.1  tron   };
   1962  1.1  tron 
   1963  1.1  tron   return features;
   1964  1.1  tron }
   1965  1.1  tron 
   1966  1.1  tron /* Initially tag->rawName always points into the parse buffer;
   1967  1.1  tron    for those TAG instances opened while the current parse buffer was
   1968  1.1  tron    processed, and not yet closed, we need to store tag->rawName in a more
   1969  1.1  tron    permanent location, since the parse buffer is about to be discarded.
   1970  1.1  tron */
   1971  1.1  tron static XML_Bool
   1972  1.1  tron storeRawNames(XML_Parser parser)
   1973  1.1  tron {
   1974  1.1  tron   TAG *tag = tagStack;
   1975  1.1  tron   while (tag) {
   1976  1.1  tron     int bufSize;
   1977  1.1  tron     int nameLen = sizeof(XML_Char) * (tag->name.strLen + 1);
   1978  1.1  tron     char *rawNameBuf = tag->buf + nameLen;
   1979  1.1  tron     /* Stop if already stored.  Since tagStack is a stack, we can stop
   1980  1.1  tron        at the first entry that has already been copied; everything
   1981  1.1  tron        below it in the stack is already been accounted for in a
   1982  1.1  tron        previous call to this function.
   1983  1.1  tron     */
   1984  1.1  tron     if (tag->rawName == rawNameBuf)
   1985  1.1  tron       break;
   1986  1.1  tron     /* For re-use purposes we need to ensure that the
   1987  1.1  tron        size of tag->buf is a multiple of sizeof(XML_Char).
   1988  1.1  tron     */
   1989  1.1  tron     bufSize = nameLen + ROUND_UP(tag->rawNameLength, sizeof(XML_Char));
   1990  1.1  tron     if (bufSize > tag->bufEnd - tag->buf) {
   1991  1.1  tron       char *temp = (char *)REALLOC(tag->buf, bufSize);
   1992  1.1  tron       if (temp == NULL)
   1993  1.1  tron         return XML_FALSE;
   1994  1.1  tron       /* if tag->name.str points to tag->buf (only when namespace
   1995  1.1  tron          processing is off) then we have to update it
   1996  1.1  tron       */
   1997  1.1  tron       if (tag->name.str == (XML_Char *)tag->buf)
   1998  1.1  tron         tag->name.str = (XML_Char *)temp;
   1999  1.1  tron       /* if tag->name.localPart is set (when namespace processing is on)
   2000  1.1  tron          then update it as well, since it will always point into tag->buf
   2001  1.1  tron       */
   2002  1.1  tron       if (tag->name.localPart)
   2003  1.1  tron         tag->name.localPart = (XML_Char *)temp + (tag->name.localPart -
   2004  1.1  tron                                                   (XML_Char *)tag->buf);
   2005  1.1  tron       tag->buf = temp;
   2006  1.1  tron       tag->bufEnd = temp + bufSize;
   2007  1.1  tron       rawNameBuf = temp + nameLen;
   2008  1.1  tron     }
   2009  1.1  tron     memcpy(rawNameBuf, tag->rawName, tag->rawNameLength);
   2010  1.1  tron     tag->rawName = rawNameBuf;
   2011  1.1  tron     tag = tag->parent;
   2012  1.1  tron   }
   2013  1.1  tron   return XML_TRUE;
   2014  1.1  tron }
   2015  1.1  tron 
   2016  1.1  tron static enum XML_Error PTRCALL
   2017  1.1  tron contentProcessor(XML_Parser parser,
   2018  1.1  tron                  const char *start,
   2019  1.1  tron                  const char *end,
   2020  1.1  tron                  const char **endPtr)
   2021  1.1  tron {
   2022  1.1  tron   enum XML_Error result = doContent(parser, 0, encoding, start, end,
   2023  1.1  tron                                     endPtr, (XML_Bool)!ps_finalBuffer);
   2024  1.1  tron   if (result == XML_ERROR_NONE) {
   2025  1.1  tron     if (!storeRawNames(parser))
   2026  1.1  tron       return XML_ERROR_NO_MEMORY;
   2027  1.1  tron   }
   2028  1.1  tron   return result;
   2029  1.1  tron }
   2030  1.1  tron 
   2031  1.1  tron static enum XML_Error PTRCALL
   2032  1.1  tron externalEntityInitProcessor(XML_Parser parser,
   2033  1.1  tron                             const char *start,
   2034  1.1  tron                             const char *end,
   2035  1.1  tron                             const char **endPtr)
   2036  1.1  tron {
   2037  1.1  tron   enum XML_Error result = initializeEncoding(parser);
   2038  1.1  tron   if (result != XML_ERROR_NONE)
   2039  1.1  tron     return result;
   2040  1.1  tron   processor = externalEntityInitProcessor2;
   2041  1.1  tron   return externalEntityInitProcessor2(parser, start, end, endPtr);
   2042  1.1  tron }
   2043  1.1  tron 
   2044  1.1  tron static enum XML_Error PTRCALL
   2045  1.1  tron externalEntityInitProcessor2(XML_Parser parser,
   2046  1.1  tron                              const char *start,
   2047  1.1  tron                              const char *end,
   2048  1.1  tron                              const char **endPtr)
   2049  1.1  tron {
   2050  1.1  tron   const char *next = start; /* XmlContentTok doesn't always set the last arg */
   2051  1.1  tron   int tok = XmlContentTok(encoding, start, end, &next);
   2052  1.1  tron   switch (tok) {
   2053  1.1  tron   case XML_TOK_BOM:
   2054  1.1  tron     /* If we are at the end of the buffer, this would cause the next stage,
   2055  1.1  tron        i.e. externalEntityInitProcessor3, to pass control directly to
   2056  1.1  tron        doContent (by detecting XML_TOK_NONE) without processing any xml text
   2057  1.1  tron        declaration - causing the error XML_ERROR_MISPLACED_XML_PI in doContent.
   2058  1.1  tron     */
   2059  1.1  tron     if (next == end && !ps_finalBuffer) {
   2060  1.1  tron       *endPtr = next;
   2061  1.1  tron       return XML_ERROR_NONE;
   2062  1.1  tron     }
   2063  1.1  tron     start = next;
   2064  1.1  tron     break;
   2065  1.1  tron   case XML_TOK_PARTIAL:
   2066  1.1  tron     if (!ps_finalBuffer) {
   2067  1.1  tron       *endPtr = start;
   2068  1.1  tron       return XML_ERROR_NONE;
   2069  1.1  tron     }
   2070  1.1  tron     eventPtr = start;
   2071  1.1  tron     return XML_ERROR_UNCLOSED_TOKEN;
   2072  1.1  tron   case XML_TOK_PARTIAL_CHAR:
   2073  1.1  tron     if (!ps_finalBuffer) {
   2074  1.1  tron       *endPtr = start;
   2075  1.1  tron       return XML_ERROR_NONE;
   2076  1.1  tron     }
   2077  1.1  tron     eventPtr = start;
   2078  1.1  tron     return XML_ERROR_PARTIAL_CHAR;
   2079  1.1  tron   }
   2080  1.1  tron   processor = externalEntityInitProcessor3;
   2081  1.1  tron   return externalEntityInitProcessor3(parser, start, end, endPtr);
   2082  1.1  tron }
   2083  1.1  tron 
   2084  1.1  tron static enum XML_Error PTRCALL
   2085  1.1  tron externalEntityInitProcessor3(XML_Parser parser,
   2086  1.1  tron                              const char *start,
   2087  1.1  tron                              const char *end,
   2088  1.1  tron                              const char **endPtr)
   2089  1.1  tron {
   2090  1.1  tron   int tok;
   2091  1.1  tron   const char *next = start; /* XmlContentTok doesn't always set the last arg */
   2092  1.1  tron   eventPtr = start;
   2093  1.1  tron   tok = XmlContentTok(encoding, start, end, &next);
   2094  1.1  tron   eventEndPtr = next;
   2095  1.1  tron 
   2096  1.1  tron   switch (tok) {
   2097  1.1  tron   case XML_TOK_XML_DECL:
   2098  1.1  tron     {
   2099  1.1  tron       enum XML_Error result;
   2100  1.1  tron       result = processXmlDecl(parser, 1, start, next);
   2101  1.1  tron       if (result != XML_ERROR_NONE)
   2102  1.1  tron         return result;
   2103  1.1  tron       switch (ps_parsing) {
   2104  1.1  tron       case XML_SUSPENDED:
   2105  1.1  tron         *endPtr = next;
   2106  1.1  tron         return XML_ERROR_NONE;
   2107  1.1  tron       case XML_FINISHED:
   2108  1.1  tron         return XML_ERROR_ABORTED;
   2109  1.1  tron       default:
   2110  1.1  tron         start = next;
   2111  1.1  tron       }
   2112  1.1  tron     }
   2113  1.1  tron     break;
   2114  1.1  tron   case XML_TOK_PARTIAL:
   2115  1.1  tron     if (!ps_finalBuffer) {
   2116  1.1  tron       *endPtr = start;
   2117  1.1  tron       return XML_ERROR_NONE;
   2118  1.1  tron     }
   2119  1.1  tron     return XML_ERROR_UNCLOSED_TOKEN;
   2120  1.1  tron   case XML_TOK_PARTIAL_CHAR:
   2121  1.1  tron     if (!ps_finalBuffer) {
   2122  1.1  tron       *endPtr = start;
   2123  1.1  tron       return XML_ERROR_NONE;
   2124  1.1  tron     }
   2125  1.1  tron     return XML_ERROR_PARTIAL_CHAR;
   2126  1.1  tron   }
   2127  1.1  tron   processor = externalEntityContentProcessor;
   2128  1.1  tron   tagLevel = 1;
   2129  1.1  tron   return externalEntityContentProcessor(parser, start, end, endPtr);
   2130  1.1  tron }
   2131  1.1  tron 
   2132  1.1  tron static enum XML_Error PTRCALL
   2133  1.1  tron externalEntityContentProcessor(XML_Parser parser,
   2134  1.1  tron                                const char *start,
   2135  1.1  tron                                const char *end,
   2136  1.1  tron                                const char **endPtr)
   2137  1.1  tron {
   2138  1.1  tron   enum XML_Error result = doContent(parser, 1, encoding, start, end,
   2139  1.1  tron                                     endPtr, (XML_Bool)!ps_finalBuffer);
   2140  1.1  tron   if (result == XML_ERROR_NONE) {
   2141  1.1  tron     if (!storeRawNames(parser))
   2142  1.1  tron       return XML_ERROR_NO_MEMORY;
   2143  1.1  tron   }
   2144  1.1  tron   return result;
   2145  1.1  tron }
   2146  1.1  tron 
   2147  1.1  tron static enum XML_Error
   2148  1.1  tron doContent(XML_Parser parser,
   2149  1.1  tron           int startTagLevel,
   2150  1.1  tron           const ENCODING *enc,
   2151  1.1  tron           const char *s,
   2152  1.1  tron           const char *end,
   2153  1.1  tron           const char **nextPtr,
   2154  1.1  tron           XML_Bool haveMore)
   2155  1.1  tron {
   2156  1.1  tron   /* save one level of indirection */
   2157  1.1  tron   DTD * const dtd = _dtd;
   2158  1.1  tron 
   2159  1.1  tron   const char **eventPP;
   2160  1.1  tron   const char **eventEndPP;
   2161  1.1  tron   if (enc == encoding) {
   2162  1.1  tron     eventPP = &eventPtr;
   2163  1.1  tron     eventEndPP = &eventEndPtr;
   2164  1.1  tron   }
   2165  1.1  tron   else {
   2166  1.1  tron     eventPP = &(openInternalEntities->internalEventPtr);
   2167  1.1  tron     eventEndPP = &(openInternalEntities->internalEventEndPtr);
   2168  1.1  tron   }
   2169  1.1  tron   *eventPP = s;
   2170  1.1  tron 
   2171  1.1  tron   for (;;) {
   2172  1.1  tron     const char *next = s; /* XmlContentTok doesn't always set the last arg */
   2173  1.1  tron     int tok = XmlContentTok(enc, s, end, &next);
   2174  1.1  tron     *eventEndPP = next;
   2175  1.1  tron     switch (tok) {
   2176  1.1  tron     case XML_TOK_TRAILING_CR:
   2177  1.1  tron       if (haveMore) {
   2178  1.1  tron         *nextPtr = s;
   2179  1.1  tron         return XML_ERROR_NONE;
   2180  1.1  tron       }
   2181  1.1  tron       *eventEndPP = end;
   2182  1.1  tron       if (characterDataHandler) {
   2183  1.1  tron         XML_Char c = 0xA;
   2184  1.1  tron         characterDataHandler(handlerArg, &c, 1);
   2185  1.1  tron       }
   2186  1.1  tron       else if (defaultHandler)
   2187  1.1  tron         reportDefault(parser, enc, s, end);
   2188  1.1  tron       /* We are at the end of the final buffer, should we check for
   2189  1.1  tron          XML_SUSPENDED, XML_FINISHED?
   2190  1.1  tron       */
   2191  1.1  tron       if (startTagLevel == 0)
   2192  1.1  tron         return XML_ERROR_NO_ELEMENTS;
   2193  1.1  tron       if (tagLevel != startTagLevel)
   2194  1.1  tron         return XML_ERROR_ASYNC_ENTITY;
   2195  1.1  tron       *nextPtr = end;
   2196  1.1  tron       return XML_ERROR_NONE;
   2197  1.1  tron     case XML_TOK_NONE:
   2198  1.1  tron       if (haveMore) {
   2199  1.1  tron         *nextPtr = s;
   2200  1.1  tron         return XML_ERROR_NONE;
   2201  1.1  tron       }
   2202  1.1  tron       if (startTagLevel > 0) {
   2203  1.1  tron         if (tagLevel != startTagLevel)
   2204  1.1  tron           return XML_ERROR_ASYNC_ENTITY;
   2205  1.1  tron         *nextPtr = s;
   2206  1.1  tron         return XML_ERROR_NONE;
   2207  1.1  tron       }
   2208  1.1  tron       return XML_ERROR_NO_ELEMENTS;
   2209  1.1  tron     case XML_TOK_INVALID:
   2210  1.1  tron       *eventPP = next;
   2211  1.1  tron       return XML_ERROR_INVALID_TOKEN;
   2212  1.1  tron     case XML_TOK_PARTIAL:
   2213  1.1  tron       if (haveMore) {
   2214  1.1  tron         *nextPtr = s;
   2215  1.1  tron         return XML_ERROR_NONE;
   2216  1.1  tron       }
   2217  1.1  tron       return XML_ERROR_UNCLOSED_TOKEN;
   2218  1.1  tron     case XML_TOK_PARTIAL_CHAR:
   2219  1.1  tron       if (haveMore) {
   2220  1.1  tron         *nextPtr = s;
   2221  1.1  tron         return XML_ERROR_NONE;
   2222  1.1  tron       }
   2223  1.1  tron       return XML_ERROR_PARTIAL_CHAR;
   2224  1.1  tron     case XML_TOK_ENTITY_REF:
   2225  1.1  tron       {
   2226  1.1  tron         const XML_Char *name;
   2227  1.1  tron         ENTITY *entity;
   2228  1.1  tron         XML_Char ch = (XML_Char) XmlPredefinedEntityName(enc,
   2229  1.1  tron                                               s + enc->minBytesPerChar,
   2230  1.1  tron                                               next - enc->minBytesPerChar);
   2231  1.1  tron         if (ch) {
   2232  1.1  tron           if (characterDataHandler)
   2233  1.1  tron             characterDataHandler(handlerArg, &ch, 1);
   2234  1.1  tron           else if (defaultHandler)
   2235  1.1  tron             reportDefault(parser, enc, s, next);
   2236  1.1  tron           break;
   2237  1.1  tron         }
   2238  1.1  tron         name = poolStoreString(&dtd->pool, enc,
   2239  1.1  tron                                 s + enc->minBytesPerChar,
   2240  1.1  tron                                 next - enc->minBytesPerChar);
   2241  1.1  tron         if (!name)
   2242  1.1  tron           return XML_ERROR_NO_MEMORY;
   2243  1.1  tron         entity = (ENTITY *)lookup(&dtd->generalEntities, name, 0);
   2244  1.1  tron         poolDiscard(&dtd->pool);
   2245  1.1  tron         /* First, determine if a check for an existing declaration is needed;
   2246  1.1  tron            if yes, check that the entity exists, and that it is internal,
   2247  1.1  tron            otherwise call the skipped entity or default handler.
   2248  1.1  tron         */
   2249  1.1  tron         if (!dtd->hasParamEntityRefs || dtd->standalone) {
   2250  1.1  tron           if (!entity)
   2251  1.1  tron             return XML_ERROR_UNDEFINED_ENTITY;
   2252  1.1  tron           else if (!entity->is_internal)
   2253  1.1  tron             return XML_ERROR_ENTITY_DECLARED_IN_PE;
   2254  1.1  tron         }
   2255  1.1  tron         else if (!entity) {
   2256  1.1  tron           if (skippedEntityHandler)
   2257  1.1  tron             skippedEntityHandler(handlerArg, name, 0);
   2258  1.1  tron           else if (defaultHandler)
   2259  1.1  tron             reportDefault(parser, enc, s, next);
   2260  1.1  tron           break;
   2261  1.1  tron         }
   2262  1.1  tron         if (entity->open)
   2263  1.1  tron           return XML_ERROR_RECURSIVE_ENTITY_REF;
   2264  1.1  tron         if (entity->notation)
   2265  1.1  tron           return XML_ERROR_BINARY_ENTITY_REF;
   2266  1.1  tron         if (entity->textPtr) {
   2267  1.1  tron           enum XML_Error result;
   2268  1.1  tron           if (!defaultExpandInternalEntities) {
   2269  1.1  tron             if (skippedEntityHandler)
   2270  1.1  tron               skippedEntityHandler(handlerArg, entity->name, 0);
   2271  1.1  tron             else if (defaultHandler)
   2272  1.1  tron               reportDefault(parser, enc, s, next);
   2273  1.1  tron             break;
   2274  1.1  tron           }
   2275  1.1  tron           result = processInternalEntity(parser, entity, XML_FALSE);
   2276  1.1  tron           if (result != XML_ERROR_NONE)
   2277  1.1  tron             return result;
   2278  1.1  tron         }
   2279  1.1  tron         else if (externalEntityRefHandler) {
   2280  1.1  tron           const XML_Char *context;
   2281  1.1  tron           entity->open = XML_TRUE;
   2282  1.1  tron           context = getContext(parser);
   2283  1.1  tron           entity->open = XML_FALSE;
   2284  1.1  tron           if (!context)
   2285  1.1  tron             return XML_ERROR_NO_MEMORY;
   2286  1.1  tron           if (!externalEntityRefHandler(externalEntityRefHandlerArg,
   2287  1.1  tron                                         context,
   2288  1.1  tron                                         entity->base,
   2289  1.1  tron                                         entity->systemId,
   2290  1.1  tron                                         entity->publicId))
   2291  1.1  tron             return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
   2292  1.1  tron           poolDiscard(&tempPool);
   2293  1.1  tron         }
   2294  1.1  tron         else if (defaultHandler)
   2295  1.1  tron           reportDefault(parser, enc, s, next);
   2296  1.1  tron         break;
   2297  1.1  tron       }
   2298  1.1  tron     case XML_TOK_START_TAG_NO_ATTS:
   2299  1.1  tron       /* fall through */
   2300  1.1  tron     case XML_TOK_START_TAG_WITH_ATTS:
   2301  1.1  tron       {
   2302  1.1  tron         TAG *tag;
   2303  1.1  tron         enum XML_Error result;
   2304  1.1  tron         XML_Char *toPtr;
   2305  1.1  tron         if (freeTagList) {
   2306  1.1  tron           tag = freeTagList;
   2307  1.1  tron           freeTagList = freeTagList->parent;
   2308  1.1  tron         }
   2309  1.1  tron         else {
   2310  1.1  tron           tag = (TAG *)MALLOC(sizeof(TAG));
   2311  1.1  tron           if (!tag)
   2312  1.1  tron             return XML_ERROR_NO_MEMORY;
   2313  1.1  tron           tag->buf = (char *)MALLOC(INIT_TAG_BUF_SIZE);
   2314  1.1  tron           if (!tag->buf) {
   2315  1.1  tron             FREE(tag);
   2316  1.1  tron             return XML_ERROR_NO_MEMORY;
   2317  1.1  tron           }
   2318  1.1  tron           tag->bufEnd = tag->buf + INIT_TAG_BUF_SIZE;
   2319  1.1  tron         }
   2320  1.1  tron         tag->bindings = NULL;
   2321  1.1  tron         tag->parent = tagStack;
   2322  1.1  tron         tagStack = tag;
   2323  1.1  tron         tag->name.localPart = NULL;
   2324  1.1  tron         tag->name.prefix = NULL;
   2325  1.1  tron         tag->rawName = s + enc->minBytesPerChar;
   2326  1.1  tron         tag->rawNameLength = XmlNameLength(enc, tag->rawName);
   2327  1.1  tron         ++tagLevel;
   2328  1.1  tron         {
   2329  1.1  tron           const char *rawNameEnd = tag->rawName + tag->rawNameLength;
   2330  1.1  tron           const char *fromPtr = tag->rawName;
   2331  1.1  tron           toPtr = (XML_Char *)tag->buf;
   2332  1.1  tron           for (;;) {
   2333  1.1  tron             int bufSize;
   2334  1.1  tron             int convLen;
   2335  1.1  tron             XmlConvert(enc,
   2336  1.1  tron                        &fromPtr, rawNameEnd,
   2337  1.1  tron                        (ICHAR **)&toPtr, (ICHAR *)tag->bufEnd - 1);
   2338  1.1  tron             convLen = (int)(toPtr - (XML_Char *)tag->buf);
   2339  1.1  tron             if (fromPtr == rawNameEnd) {
   2340  1.1  tron               tag->name.strLen = convLen;
   2341  1.1  tron               break;
   2342  1.1  tron             }
   2343  1.1  tron             bufSize = (int)(tag->bufEnd - tag->buf) << 1;
   2344  1.1  tron             {
   2345  1.1  tron               char *temp = (char *)REALLOC(tag->buf, bufSize);
   2346  1.1  tron               if (temp == NULL)
   2347  1.1  tron                 return XML_ERROR_NO_MEMORY;
   2348  1.1  tron               tag->buf = temp;
   2349  1.1  tron               tag->bufEnd = temp + bufSize;
   2350  1.1  tron               toPtr = (XML_Char *)temp + convLen;
   2351  1.1  tron             }
   2352  1.1  tron           }
   2353  1.1  tron         }
   2354  1.1  tron         tag->name.str = (XML_Char *)tag->buf;
   2355  1.1  tron         *toPtr = XML_T('\0');
   2356  1.1  tron         result = storeAtts(parser, enc, s, &(tag->name), &(tag->bindings));
   2357  1.1  tron         if (result)
   2358  1.1  tron           return result;
   2359  1.1  tron         if (startElementHandler)
   2360  1.1  tron           startElementHandler(handlerArg, tag->name.str,
   2361  1.1  tron                               (const XML_Char **)atts);
   2362  1.1  tron         else if (defaultHandler)
   2363  1.1  tron           reportDefault(parser, enc, s, next);
   2364  1.1  tron         poolClear(&tempPool);
   2365  1.1  tron         break;
   2366  1.1  tron       }
   2367  1.1  tron     case XML_TOK_EMPTY_ELEMENT_NO_ATTS:
   2368  1.1  tron       /* fall through */
   2369  1.1  tron     case XML_TOK_EMPTY_ELEMENT_WITH_ATTS:
   2370  1.1  tron       {
   2371  1.1  tron         const char *rawName = s + enc->minBytesPerChar;
   2372  1.1  tron         enum XML_Error result;
   2373  1.1  tron         BINDING *bindings = NULL;
   2374  1.1  tron         XML_Bool noElmHandlers = XML_TRUE;
   2375  1.1  tron         TAG_NAME name;
   2376  1.1  tron         name.str = poolStoreString(&tempPool, enc, rawName,
   2377  1.1  tron                                    rawName + XmlNameLength(enc, rawName));
   2378  1.1  tron         if (!name.str)
   2379  1.1  tron           return XML_ERROR_NO_MEMORY;
   2380  1.1  tron         poolFinish(&tempPool);
   2381  1.1  tron         result = storeAtts(parser, enc, s, &name, &bindings);
   2382  1.1  tron         if (result)
   2383  1.1  tron           return result;
   2384  1.1  tron         poolFinish(&tempPool);
   2385  1.1  tron         if (startElementHandler) {
   2386  1.1  tron           startElementHandler(handlerArg, name.str, (const XML_Char **)atts);
   2387  1.1  tron           noElmHandlers = XML_FALSE;
   2388  1.1  tron         }
   2389  1.1  tron         if (endElementHandler) {
   2390  1.1  tron           if (startElementHandler)
   2391  1.1  tron             *eventPP = *eventEndPP;
   2392  1.1  tron           endElementHandler(handlerArg, name.str);
   2393  1.1  tron           noElmHandlers = XML_FALSE;
   2394  1.1  tron         }
   2395  1.1  tron         if (noElmHandlers && defaultHandler)
   2396  1.1  tron           reportDefault(parser, enc, s, next);
   2397  1.1  tron         poolClear(&tempPool);
   2398  1.1  tron         while (bindings) {
   2399  1.1  tron           BINDING *b = bindings;
   2400  1.1  tron           if (endNamespaceDeclHandler)
   2401  1.1  tron             endNamespaceDeclHandler(handlerArg, b->prefix->name);
   2402  1.1  tron           bindings = bindings->nextTagBinding;
   2403  1.1  tron           b->nextTagBinding = freeBindingList;
   2404  1.1  tron           freeBindingList = b;
   2405  1.1  tron           b->prefix->binding = b->prevPrefixBinding;
   2406  1.1  tron         }
   2407  1.1  tron       }
   2408  1.1  tron       if (tagLevel == 0)
   2409  1.1  tron         return epilogProcessor(parser, next, end, nextPtr);
   2410  1.1  tron       break;
   2411  1.1  tron     case XML_TOK_END_TAG:
   2412  1.1  tron       if (tagLevel == startTagLevel)
   2413  1.1  tron         return XML_ERROR_ASYNC_ENTITY;
   2414  1.1  tron       else {
   2415  1.1  tron         int len;
   2416  1.1  tron         const char *rawName;
   2417  1.1  tron         TAG *tag = tagStack;
   2418  1.1  tron         tagStack = tag->parent;
   2419  1.1  tron         tag->parent = freeTagList;
   2420  1.1  tron         freeTagList = tag;
   2421  1.1  tron         rawName = s + enc->minBytesPerChar*2;
   2422  1.1  tron         len = XmlNameLength(enc, rawName);
   2423  1.1  tron         if (len != tag->rawNameLength
   2424  1.1  tron             || memcmp(tag->rawName, rawName, len) != 0) {
   2425  1.1  tron           *eventPP = rawName;
   2426  1.1  tron           return XML_ERROR_TAG_MISMATCH;
   2427  1.1  tron         }
   2428  1.1  tron         --tagLevel;
   2429  1.1  tron         if (endElementHandler) {
   2430  1.1  tron           const XML_Char *localPart;
   2431  1.1  tron           const XML_Char *prefix;
   2432  1.1  tron           XML_Char *uri;
   2433  1.1  tron           localPart = tag->name.localPart;
   2434  1.1  tron           if (ns && localPart) {
   2435  1.1  tron             /* localPart and prefix may have been overwritten in
   2436  1.1  tron                tag->name.str, since this points to the binding->uri
   2437  1.1  tron                buffer which gets re-used; so we have to add them again
   2438  1.1  tron             */
   2439  1.1  tron             uri = (XML_Char *)tag->name.str + tag->name.uriLen;
   2440  1.1  tron             /* don't need to check for space - already done in storeAtts() */
   2441  1.1  tron             while (*localPart) *uri++ = *localPart++;
   2442  1.1  tron             prefix = (XML_Char *)tag->name.prefix;
   2443  1.1  tron             if (ns_triplets && prefix) {
   2444  1.1  tron               *uri++ = namespaceSeparator;
   2445  1.1  tron               while (*prefix) *uri++ = *prefix++;
   2446  1.1  tron              }
   2447  1.1  tron             *uri = XML_T('\0');
   2448  1.1  tron           }
   2449  1.1  tron           endElementHandler(handlerArg, tag->name.str);
   2450  1.1  tron         }
   2451  1.1  tron         else if (defaultHandler)
   2452  1.1  tron           reportDefault(parser, enc, s, next);
   2453  1.1  tron         while (tag->bindings) {
   2454  1.1  tron           BINDING *b = tag->bindings;
   2455  1.1  tron           if (endNamespaceDeclHandler)
   2456  1.1  tron             endNamespaceDeclHandler(handlerArg, b->prefix->name);
   2457  1.1  tron           tag->bindings = tag->bindings->nextTagBinding;
   2458  1.1  tron           b->nextTagBinding = freeBindingList;
   2459  1.1  tron           freeBindingList = b;
   2460  1.1  tron           b->prefix->binding = b->prevPrefixBinding;
   2461  1.1  tron         }
   2462  1.1  tron         if (tagLevel == 0)
   2463  1.1  tron           return epilogProcessor(parser, next, end, nextPtr);
   2464  1.1  tron       }
   2465  1.1  tron       break;
   2466  1.1  tron     case XML_TOK_CHAR_REF:
   2467  1.1  tron       {
   2468  1.1  tron         int n = XmlCharRefNumber(enc, s);
   2469  1.1  tron         if (n < 0)
   2470  1.1  tron           return XML_ERROR_BAD_CHAR_REF;
   2471  1.1  tron         if (characterDataHandler) {
   2472  1.1  tron           XML_Char buf[XML_ENCODE_MAX];
   2473  1.1  tron           characterDataHandler(handlerArg, buf, XmlEncode(n, (ICHAR *)buf));
   2474  1.1  tron         }
   2475  1.1  tron         else if (defaultHandler)
   2476  1.1  tron           reportDefault(parser, enc, s, next);
   2477  1.1  tron       }
   2478  1.1  tron       break;
   2479  1.1  tron     case XML_TOK_XML_DECL:
   2480  1.1  tron       return XML_ERROR_MISPLACED_XML_PI;
   2481  1.1  tron     case XML_TOK_DATA_NEWLINE:
   2482  1.1  tron       if (characterDataHandler) {
   2483  1.1  tron         XML_Char c = 0xA;
   2484  1.1  tron         characterDataHandler(handlerArg, &c, 1);
   2485  1.1  tron       }
   2486  1.1  tron       else if (defaultHandler)
   2487  1.1  tron         reportDefault(parser, enc, s, next);
   2488  1.1  tron       break;
   2489  1.1  tron     case XML_TOK_CDATA_SECT_OPEN:
   2490  1.1  tron       {
   2491  1.1  tron         enum XML_Error result;
   2492  1.1  tron         if (startCdataSectionHandler)
   2493  1.1  tron           startCdataSectionHandler(handlerArg);
   2494  1.1  tron #if 0
   2495  1.1  tron         /* Suppose you doing a transformation on a document that involves
   2496  1.1  tron            changing only the character data.  You set up a defaultHandler
   2497  1.1  tron            and a characterDataHandler.  The defaultHandler simply copies
   2498  1.1  tron            characters through.  The characterDataHandler does the
   2499  1.1  tron            transformation and writes the characters out escaping them as
   2500  1.1  tron            necessary.  This case will fail to work if we leave out the
   2501  1.1  tron            following two lines (because & and < inside CDATA sections will
   2502  1.1  tron            be incorrectly escaped).
   2503  1.1  tron 
   2504  1.1  tron            However, now we have a start/endCdataSectionHandler, so it seems
   2505  1.1  tron            easier to let the user deal with this.
   2506  1.1  tron         */
   2507  1.1  tron         else if (characterDataHandler)
   2508  1.1  tron           characterDataHandler(handlerArg, dataBuf, 0);
   2509  1.1  tron #endif
   2510  1.1  tron         else if (defaultHandler)
   2511  1.1  tron           reportDefault(parser, enc, s, next);
   2512  1.1  tron         result = doCdataSection(parser, enc, &next, end, nextPtr, haveMore);
   2513  1.1  tron         if (result != XML_ERROR_NONE)
   2514  1.1  tron           return result;
   2515  1.1  tron         else if (!next) {
   2516  1.1  tron           processor = cdataSectionProcessor;
   2517  1.1  tron           return result;
   2518  1.1  tron         }
   2519  1.1  tron       }
   2520  1.1  tron       break;
   2521  1.1  tron     case XML_TOK_TRAILING_RSQB:
   2522  1.1  tron       if (haveMore) {
   2523  1.1  tron         *nextPtr = s;
   2524  1.1  tron         return XML_ERROR_NONE;
   2525  1.1  tron       }
   2526  1.1  tron       if (characterDataHandler) {
   2527  1.1  tron         if (MUST_CONVERT(enc, s)) {
   2528  1.1  tron           ICHAR *dataPtr = (ICHAR *)dataBuf;
   2529  1.1  tron           XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
   2530  1.1  tron           characterDataHandler(handlerArg, dataBuf,
   2531  1.1  tron                                (int)(dataPtr - (ICHAR *)dataBuf));
   2532  1.1  tron         }
   2533  1.1  tron         else
   2534  1.1  tron           characterDataHandler(handlerArg,
   2535  1.1  tron                                (XML_Char *)s,
   2536  1.1  tron                                (int)((XML_Char *)end - (XML_Char *)s));
   2537  1.1  tron       }
   2538  1.1  tron       else if (defaultHandler)
   2539  1.1  tron         reportDefault(parser, enc, s, end);
   2540  1.1  tron       /* We are at the end of the final buffer, should we check for
   2541  1.1  tron          XML_SUSPENDED, XML_FINISHED?
   2542  1.1  tron       */
   2543  1.1  tron       if (startTagLevel == 0) {
   2544  1.1  tron         *eventPP = end;
   2545  1.1  tron         return XML_ERROR_NO_ELEMENTS;
   2546  1.1  tron       }
   2547  1.1  tron       if (tagLevel != startTagLevel) {
   2548  1.1  tron         *eventPP = end;
   2549  1.1  tron         return XML_ERROR_ASYNC_ENTITY;
   2550  1.1  tron       }
   2551  1.1  tron       *nextPtr = end;
   2552  1.1  tron       return XML_ERROR_NONE;
   2553  1.1  tron     case XML_TOK_DATA_CHARS:
   2554  1.1  tron       {
   2555  1.1  tron         XML_CharacterDataHandler charDataHandler = characterDataHandler;
   2556  1.1  tron         if (charDataHandler) {
   2557  1.1  tron           if (MUST_CONVERT(enc, s)) {
   2558  1.1  tron             for (;;) {
   2559  1.1  tron               ICHAR *dataPtr = (ICHAR *)dataBuf;
   2560  1.1  tron               XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
   2561  1.1  tron               *eventEndPP = s;
   2562  1.1  tron               charDataHandler(handlerArg, dataBuf,
   2563  1.1  tron                               (int)(dataPtr - (ICHAR *)dataBuf));
   2564  1.1  tron               if (s == next)
   2565  1.1  tron                 break;
   2566  1.1  tron               *eventPP = s;
   2567  1.1  tron             }
   2568  1.1  tron           }
   2569  1.1  tron           else
   2570  1.1  tron             charDataHandler(handlerArg,
   2571  1.1  tron                             (XML_Char *)s,
   2572  1.1  tron                             (int)((XML_Char *)next - (XML_Char *)s));
   2573  1.1  tron         }
   2574  1.1  tron         else if (defaultHandler)
   2575  1.1  tron           reportDefault(parser, enc, s, next);
   2576  1.1  tron       }
   2577  1.1  tron       break;
   2578  1.1  tron     case XML_TOK_PI:
   2579  1.1  tron       if (!reportProcessingInstruction(parser, enc, s, next))
   2580  1.1  tron         return XML_ERROR_NO_MEMORY;
   2581  1.1  tron       break;
   2582  1.1  tron     case XML_TOK_COMMENT:
   2583  1.1  tron       if (!reportComment(parser, enc, s, next))
   2584  1.1  tron         return XML_ERROR_NO_MEMORY;
   2585  1.1  tron       break;
   2586  1.1  tron     default:
   2587  1.1  tron       if (defaultHandler)
   2588  1.1  tron         reportDefault(parser, enc, s, next);
   2589  1.1  tron       break;
   2590  1.1  tron     }
   2591  1.1  tron     *eventPP = s = next;
   2592  1.1  tron     switch (ps_parsing) {
   2593  1.1  tron     case XML_SUSPENDED:
   2594  1.1  tron       *nextPtr = next;
   2595  1.1  tron       return XML_ERROR_NONE;
   2596  1.1  tron     case XML_FINISHED:
   2597  1.1  tron       return XML_ERROR_ABORTED;
   2598  1.1  tron     default: ;
   2599  1.1  tron     }
   2600  1.1  tron   }
   2601  1.1  tron   /* not reached */
   2602  1.1  tron }
   2603  1.1  tron 
   2604  1.1  tron /* Precondition: all arguments must be non-NULL;
   2605  1.1  tron    Purpose:
   2606  1.1  tron    - normalize attributes
   2607  1.1  tron    - check attributes for well-formedness
   2608  1.1  tron    - generate namespace aware attribute names (URI, prefix)
   2609  1.1  tron    - build list of attributes for startElementHandler
   2610  1.1  tron    - default attributes
   2611  1.1  tron    - process namespace declarations (check and report them)
   2612  1.1  tron    - generate namespace aware element name (URI, prefix)
   2613  1.1  tron */
   2614  1.1  tron static enum XML_Error
   2615  1.1  tron storeAtts(XML_Parser parser, const ENCODING *enc,
   2616  1.1  tron           const char *attStr, TAG_NAME *tagNamePtr,
   2617  1.1  tron           BINDING **bindingsPtr)
   2618  1.1  tron {
   2619  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   2620  1.1  tron   ELEMENT_TYPE *elementType;
   2621  1.1  tron   int nDefaultAtts;
   2622  1.1  tron   const XML_Char **appAtts;   /* the attribute list for the application */
   2623  1.1  tron   int attIndex = 0;
   2624  1.1  tron   int prefixLen;
   2625  1.1  tron   int i;
   2626  1.1  tron   int n;
   2627  1.1  tron   XML_Char *uri;
   2628  1.1  tron   int nPrefixes = 0;
   2629  1.1  tron   BINDING *binding;
   2630  1.1  tron   const XML_Char *localPart;
   2631  1.1  tron 
   2632  1.1  tron   /* lookup the element type name */
   2633  1.1  tron   elementType = (ELEMENT_TYPE *)lookup(&dtd->elementTypes, tagNamePtr->str,0);
   2634  1.1  tron   if (!elementType) {
   2635  1.1  tron     const XML_Char *name = poolCopyString(&dtd->pool, tagNamePtr->str);
   2636  1.1  tron     if (!name)
   2637  1.1  tron       return XML_ERROR_NO_MEMORY;
   2638  1.1  tron     elementType = (ELEMENT_TYPE *)lookup(&dtd->elementTypes, name,
   2639  1.1  tron                                          sizeof(ELEMENT_TYPE));
   2640  1.1  tron     if (!elementType)
   2641  1.1  tron       return XML_ERROR_NO_MEMORY;
   2642  1.1  tron     if (ns && !setElementTypePrefix(parser, elementType))
   2643  1.1  tron       return XML_ERROR_NO_MEMORY;
   2644  1.1  tron   }
   2645  1.1  tron   nDefaultAtts = elementType->nDefaultAtts;
   2646  1.1  tron 
   2647  1.1  tron   /* get the attributes from the tokenizer */
   2648  1.1  tron   n = XmlGetAttributes(enc, attStr, attsSize, atts);
   2649  1.1  tron   if (n + nDefaultAtts > attsSize) {
   2650  1.1  tron     int oldAttsSize = attsSize;
   2651  1.1  tron     ATTRIBUTE *temp;
   2652  1.1  tron     attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
   2653  1.1  tron     temp = (ATTRIBUTE *)REALLOC((void *)atts, attsSize * sizeof(ATTRIBUTE));
   2654  1.1  tron     if (temp == NULL)
   2655  1.1  tron       return XML_ERROR_NO_MEMORY;
   2656  1.1  tron     atts = temp;
   2657  1.1  tron     if (n > oldAttsSize)
   2658  1.1  tron       XmlGetAttributes(enc, attStr, n, atts);
   2659  1.1  tron   }
   2660  1.1  tron 
   2661  1.1  tron   appAtts = (const XML_Char **)atts;
   2662  1.1  tron   for (i = 0; i < n; i++) {
   2663  1.1  tron     /* add the name and value to the attribute list */
   2664  1.1  tron     ATTRIBUTE_ID *attId = getAttributeId(parser, enc, atts[i].name,
   2665  1.1  tron                                          atts[i].name
   2666  1.1  tron                                          + XmlNameLength(enc, atts[i].name));
   2667  1.1  tron     if (!attId)
   2668  1.1  tron       return XML_ERROR_NO_MEMORY;
   2669  1.1  tron     /* Detect duplicate attributes by their QNames. This does not work when
   2670  1.1  tron        namespace processing is turned on and different prefixes for the same
   2671  1.1  tron        namespace are used. For this case we have a check further down.
   2672  1.1  tron     */
   2673  1.1  tron     if ((attId->name)[-1]) {
   2674  1.1  tron       if (enc == encoding)
   2675  1.1  tron         eventPtr = atts[i].name;
   2676  1.1  tron       return XML_ERROR_DUPLICATE_ATTRIBUTE;
   2677  1.1  tron     }
   2678  1.1  tron     (attId->name)[-1] = 1;
   2679  1.1  tron     appAtts[attIndex++] = attId->name;
   2680  1.1  tron     if (!atts[i].normalized) {
   2681  1.1  tron       enum XML_Error result;
   2682  1.1  tron       XML_Bool isCdata = XML_TRUE;
   2683  1.1  tron 
   2684  1.1  tron       /* figure out whether declared as other than CDATA */
   2685  1.1  tron       if (attId->maybeTokenized) {
   2686  1.1  tron         int j;
   2687  1.1  tron         for (j = 0; j < nDefaultAtts; j++) {
   2688  1.1  tron           if (attId == elementType->defaultAtts[j].id) {
   2689  1.1  tron             isCdata = elementType->defaultAtts[j].isCdata;
   2690  1.1  tron             break;
   2691  1.1  tron           }
   2692  1.1  tron         }
   2693  1.1  tron       }
   2694  1.1  tron 
   2695  1.1  tron       /* normalize the attribute value */
   2696  1.1  tron       result = storeAttributeValue(parser, enc, isCdata,
   2697  1.1  tron                                    atts[i].valuePtr, atts[i].valueEnd,
   2698  1.1  tron                                    &tempPool);
   2699  1.1  tron       if (result)
   2700  1.1  tron         return result;
   2701  1.1  tron       appAtts[attIndex] = poolStart(&tempPool);
   2702  1.1  tron       poolFinish(&tempPool);
   2703  1.1  tron     }
   2704  1.1  tron     else {
   2705  1.1  tron       /* the value did not need normalizing */
   2706  1.1  tron       appAtts[attIndex] = poolStoreString(&tempPool, enc, atts[i].valuePtr,
   2707  1.1  tron                                           atts[i].valueEnd);
   2708  1.1  tron       if (appAtts[attIndex] == 0)
   2709  1.1  tron         return XML_ERROR_NO_MEMORY;
   2710  1.1  tron       poolFinish(&tempPool);
   2711  1.1  tron     }
   2712  1.1  tron     /* handle prefixed attribute names */
   2713  1.1  tron     if (attId->prefix) {
   2714  1.1  tron       if (attId->xmlns) {
   2715  1.1  tron         /* deal with namespace declarations here */
   2716  1.1  tron         enum XML_Error result = addBinding(parser, attId->prefix, attId,
   2717  1.1  tron                                            appAtts[attIndex], bindingsPtr);
   2718  1.1  tron         if (result)
   2719  1.1  tron           return result;
   2720  1.1  tron         --attIndex;
   2721  1.1  tron       }
   2722  1.1  tron       else {
   2723  1.1  tron         /* deal with other prefixed names later */
   2724  1.1  tron         attIndex++;
   2725  1.1  tron         nPrefixes++;
   2726  1.1  tron         (attId->name)[-1] = 2;
   2727  1.1  tron       }
   2728  1.1  tron     }
   2729  1.1  tron     else
   2730  1.1  tron       attIndex++;
   2731  1.1  tron   }
   2732  1.1  tron 
   2733  1.1  tron   /* set-up for XML_GetSpecifiedAttributeCount and XML_GetIdAttributeIndex */
   2734  1.1  tron   nSpecifiedAtts = attIndex;
   2735  1.1  tron   if (elementType->idAtt && (elementType->idAtt->name)[-1]) {
   2736  1.1  tron     for (i = 0; i < attIndex; i += 2)
   2737  1.1  tron       if (appAtts[i] == elementType->idAtt->name) {
   2738  1.1  tron         idAttIndex = i;
   2739  1.1  tron         break;
   2740  1.1  tron       }
   2741  1.1  tron   }
   2742  1.1  tron   else
   2743  1.1  tron     idAttIndex = -1;
   2744  1.1  tron 
   2745  1.1  tron   /* do attribute defaulting */
   2746  1.1  tron   for (i = 0; i < nDefaultAtts; i++) {
   2747  1.1  tron     const DEFAULT_ATTRIBUTE *da = elementType->defaultAtts + i;
   2748  1.1  tron     if (!(da->id->name)[-1] && da->value) {
   2749  1.1  tron       if (da->id->prefix) {
   2750  1.1  tron         if (da->id->xmlns) {
   2751  1.1  tron           enum XML_Error result = addBinding(parser, da->id->prefix, da->id,
   2752  1.1  tron                                              da->value, bindingsPtr);
   2753  1.1  tron           if (result)
   2754  1.1  tron             return result;
   2755  1.1  tron         }
   2756  1.1  tron         else {
   2757  1.1  tron           (da->id->name)[-1] = 2;
   2758  1.1  tron           nPrefixes++;
   2759  1.1  tron           appAtts[attIndex++] = da->id->name;
   2760  1.1  tron           appAtts[attIndex++] = da->value;
   2761  1.1  tron         }
   2762  1.1  tron       }
   2763  1.1  tron       else {
   2764  1.1  tron         (da->id->name)[-1] = 1;
   2765  1.1  tron         appAtts[attIndex++] = da->id->name;
   2766  1.1  tron         appAtts[attIndex++] = da->value;
   2767  1.1  tron       }
   2768  1.1  tron     }
   2769  1.1  tron   }
   2770  1.1  tron   appAtts[attIndex] = 0;
   2771  1.1  tron 
   2772  1.1  tron   /* expand prefixed attribute names, check for duplicates,
   2773  1.1  tron      and clear flags that say whether attributes were specified */
   2774  1.1  tron   i = 0;
   2775  1.1  tron   if (nPrefixes) {
   2776  1.1  tron     int j;  /* hash table index */
   2777  1.1  tron     unsigned long version = nsAttsVersion;
   2778  1.1  tron     int nsAttsSize = (int)1 << nsAttsPower;
   2779  1.1  tron     /* size of hash table must be at least 2 * (# of prefixed attributes) */
   2780  1.1  tron     if ((nPrefixes << 1) >> nsAttsPower) {  /* true for nsAttsPower = 0 */
   2781  1.1  tron       NS_ATT *temp;
   2782  1.1  tron       /* hash table size must also be a power of 2 and >= 8 */
   2783  1.1  tron       while (nPrefixes >> nsAttsPower++);
   2784  1.1  tron       if (nsAttsPower < 3)
   2785  1.1  tron         nsAttsPower = 3;
   2786  1.1  tron       nsAttsSize = (int)1 << nsAttsPower;
   2787  1.1  tron       temp = (NS_ATT *)REALLOC(nsAtts, nsAttsSize * sizeof(NS_ATT));
   2788  1.1  tron       if (!temp)
   2789  1.1  tron         return XML_ERROR_NO_MEMORY;
   2790  1.1  tron       nsAtts = temp;
   2791  1.1  tron       version = 0;  /* force re-initialization of nsAtts hash table */
   2792  1.1  tron     }
   2793  1.1  tron     /* using a version flag saves us from initializing nsAtts every time */
   2794  1.1  tron     if (!version) {  /* initialize version flags when version wraps around */
   2795  1.1  tron       version = INIT_ATTS_VERSION;
   2796  1.1  tron       for (j = nsAttsSize; j != 0; )
   2797  1.1  tron         nsAtts[--j].version = version;
   2798  1.1  tron     }
   2799  1.1  tron     nsAttsVersion = --version;
   2800  1.1  tron 
   2801  1.1  tron     /* expand prefixed names and check for duplicates */
   2802  1.1  tron     for (; i < attIndex; i += 2) {
   2803  1.1  tron       const XML_Char *s = appAtts[i];
   2804  1.1  tron       if (s[-1] == 2) {  /* prefixed */
   2805  1.1  tron         ATTRIBUTE_ID *id;
   2806  1.1  tron         const BINDING *b;
   2807  1.1  tron         unsigned long uriHash = 0;
   2808  1.1  tron         ((XML_Char *)s)[-1] = 0;  /* clear flag */
   2809  1.1  tron         id = (ATTRIBUTE_ID *)lookup(&dtd->attributeIds, s, 0);
   2810  1.1  tron         b = id->prefix->binding;
   2811  1.1  tron         if (!b)
   2812  1.1  tron           return XML_ERROR_UNBOUND_PREFIX;
   2813  1.1  tron 
   2814  1.1  tron         /* as we expand the name we also calculate its hash value */
   2815  1.1  tron         for (j = 0; j < b->uriLen; j++) {
   2816  1.1  tron           const XML_Char c = b->uri[j];
   2817  1.1  tron           if (!poolAppendChar(&tempPool, c))
   2818  1.1  tron             return XML_ERROR_NO_MEMORY;
   2819  1.1  tron           uriHash = CHAR_HASH(uriHash, c);
   2820  1.1  tron         }
   2821  1.1  tron         while (*s++ != XML_T(ASCII_COLON))
   2822  1.1  tron           ;
   2823  1.1  tron         do {  /* copies null terminator */
   2824  1.1  tron           const XML_Char c = *s;
   2825  1.1  tron           if (!poolAppendChar(&tempPool, *s))
   2826  1.1  tron             return XML_ERROR_NO_MEMORY;
   2827  1.1  tron           uriHash = CHAR_HASH(uriHash, c);
   2828  1.1  tron         } while (*s++);
   2829  1.1  tron 
   2830  1.1  tron         { /* Check hash table for duplicate of expanded name (uriName).
   2831  1.1  tron              Derived from code in lookup(HASH_TABLE *table, ...).
   2832  1.1  tron           */
   2833  1.1  tron           unsigned char step = 0;
   2834  1.1  tron           unsigned long mask = nsAttsSize - 1;
   2835  1.1  tron           j = uriHash & mask;  /* index into hash table */
   2836  1.1  tron           while (nsAtts[j].version == version) {
   2837  1.1  tron             /* for speed we compare stored hash values first */
   2838  1.1  tron             if (uriHash == nsAtts[j].hash) {
   2839  1.1  tron               const XML_Char *s1 = poolStart(&tempPool);
   2840  1.1  tron               const XML_Char *s2 = nsAtts[j].uriName;
   2841  1.1  tron               /* s1 is null terminated, but not s2 */
   2842  1.1  tron               for (; *s1 == *s2 && *s1 != 0; s1++, s2++);
   2843  1.1  tron               if (*s1 == 0)
   2844  1.1  tron                 return XML_ERROR_DUPLICATE_ATTRIBUTE;
   2845  1.1  tron             }
   2846  1.1  tron             if (!step)
   2847  1.1  tron               step = PROBE_STEP(uriHash, mask, nsAttsPower);
   2848  1.1  tron             j < step ? (j += nsAttsSize - step) : (j -= step);
   2849  1.1  tron           }
   2850  1.1  tron         }
   2851  1.1  tron 
   2852  1.1  tron         if (ns_triplets) {  /* append namespace separator and prefix */
   2853  1.1  tron           tempPool.ptr[-1] = namespaceSeparator;
   2854  1.1  tron           s = b->prefix->name;
   2855  1.1  tron           do {
   2856  1.1  tron             if (!poolAppendChar(&tempPool, *s))
   2857  1.1  tron               return XML_ERROR_NO_MEMORY;
   2858  1.1  tron           } while (*s++);
   2859  1.1  tron         }
   2860  1.1  tron 
   2861  1.1  tron         /* store expanded name in attribute list */
   2862  1.1  tron         s = poolStart(&tempPool);
   2863  1.1  tron         poolFinish(&tempPool);
   2864  1.1  tron         appAtts[i] = s;
   2865  1.1  tron 
   2866  1.1  tron         /* fill empty slot with new version, uriName and hash value */
   2867  1.1  tron         nsAtts[j].version = version;
   2868  1.1  tron         nsAtts[j].hash = uriHash;
   2869  1.1  tron         nsAtts[j].uriName = s;
   2870  1.1  tron 
   2871  1.1  tron         if (!--nPrefixes) {
   2872  1.1  tron           i += 2;
   2873  1.1  tron           break;
   2874  1.1  tron         }
   2875  1.1  tron       }
   2876  1.1  tron       else  /* not prefixed */
   2877  1.1  tron         ((XML_Char *)s)[-1] = 0;  /* clear flag */
   2878  1.1  tron     }
   2879  1.1  tron   }
   2880  1.1  tron   /* clear flags for the remaining attributes */
   2881  1.1  tron   for (; i < attIndex; i += 2)
   2882  1.1  tron     ((XML_Char *)(appAtts[i]))[-1] = 0;
   2883  1.1  tron   for (binding = *bindingsPtr; binding; binding = binding->nextTagBinding)
   2884  1.1  tron     binding->attId->name[-1] = 0;
   2885  1.1  tron 
   2886  1.1  tron   if (!ns)
   2887  1.1  tron     return XML_ERROR_NONE;
   2888  1.1  tron 
   2889  1.1  tron   /* expand the element type name */
   2890  1.1  tron   if (elementType->prefix) {
   2891  1.1  tron     binding = elementType->prefix->binding;
   2892  1.1  tron     if (!binding)
   2893  1.1  tron       return XML_ERROR_UNBOUND_PREFIX;
   2894  1.1  tron     localPart = tagNamePtr->str;
   2895  1.1  tron     while (*localPart++ != XML_T(ASCII_COLON))
   2896  1.1  tron       ;
   2897  1.1  tron   }
   2898  1.1  tron   else if (dtd->defaultPrefix.binding) {
   2899  1.1  tron     binding = dtd->defaultPrefix.binding;
   2900  1.1  tron     localPart = tagNamePtr->str;
   2901  1.1  tron   }
   2902  1.1  tron   else
   2903  1.1  tron     return XML_ERROR_NONE;
   2904  1.1  tron   prefixLen = 0;
   2905  1.1  tron   if (ns_triplets && binding->prefix->name) {
   2906  1.1  tron     for (; binding->prefix->name[prefixLen++];)
   2907  1.1  tron       ;  /* prefixLen includes null terminator */
   2908  1.1  tron   }
   2909  1.1  tron   tagNamePtr->localPart = localPart;
   2910  1.1  tron   tagNamePtr->uriLen = binding->uriLen;
   2911  1.1  tron   tagNamePtr->prefix = binding->prefix->name;
   2912  1.1  tron   tagNamePtr->prefixLen = prefixLen;
   2913  1.1  tron   for (i = 0; localPart[i++];)
   2914  1.1  tron     ;  /* i includes null terminator */
   2915  1.1  tron   n = i + binding->uriLen + prefixLen;
   2916  1.1  tron   if (n > binding->uriAlloc) {
   2917  1.1  tron     TAG *p;
   2918  1.1  tron     uri = (XML_Char *)MALLOC((n + EXPAND_SPARE) * sizeof(XML_Char));
   2919  1.1  tron     if (!uri)
   2920  1.1  tron       return XML_ERROR_NO_MEMORY;
   2921  1.1  tron     binding->uriAlloc = n + EXPAND_SPARE;
   2922  1.1  tron     memcpy(uri, binding->uri, binding->uriLen * sizeof(XML_Char));
   2923  1.1  tron     for (p = tagStack; p; p = p->parent)
   2924  1.1  tron       if (p->name.str == binding->uri)
   2925  1.1  tron         p->name.str = uri;
   2926  1.1  tron     FREE(binding->uri);
   2927  1.1  tron     binding->uri = uri;
   2928  1.1  tron   }
   2929  1.1  tron   /* if namespaceSeparator != '\0' then uri includes it already */
   2930  1.1  tron   uri = binding->uri + binding->uriLen;
   2931  1.1  tron   memcpy(uri, localPart, i * sizeof(XML_Char));
   2932  1.1  tron   /* we always have a namespace separator between localPart and prefix */
   2933  1.1  tron   if (prefixLen) {
   2934  1.1  tron     uri += i - 1;
   2935  1.1  tron     *uri = namespaceSeparator;  /* replace null terminator */
   2936  1.1  tron     memcpy(uri + 1, binding->prefix->name, prefixLen * sizeof(XML_Char));
   2937  1.1  tron   }
   2938  1.1  tron   tagNamePtr->str = binding->uri;
   2939  1.1  tron   return XML_ERROR_NONE;
   2940  1.1  tron }
   2941  1.1  tron 
   2942  1.1  tron /* addBinding() overwrites the value of prefix->binding without checking.
   2943  1.1  tron    Therefore one must keep track of the old value outside of addBinding().
   2944  1.1  tron */
   2945  1.1  tron static enum XML_Error
   2946  1.1  tron addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
   2947  1.1  tron            const XML_Char *uri, BINDING **bindingsPtr)
   2948  1.1  tron {
   2949  1.1  tron   static const XML_Char xmlNamespace[] = {
   2950  1.1  tron     ASCII_h, ASCII_t, ASCII_t, ASCII_p, ASCII_COLON, ASCII_SLASH, ASCII_SLASH,
   2951  1.1  tron     ASCII_w, ASCII_w, ASCII_w, ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD,
   2952  1.1  tron     ASCII_o, ASCII_r, ASCII_g, ASCII_SLASH, ASCII_X, ASCII_M, ASCII_L,
   2953  1.1  tron     ASCII_SLASH, ASCII_1, ASCII_9, ASCII_9, ASCII_8, ASCII_SLASH,
   2954  1.1  tron     ASCII_n, ASCII_a, ASCII_m, ASCII_e, ASCII_s, ASCII_p, ASCII_a, ASCII_c,
   2955  1.1  tron     ASCII_e, '\0'
   2956  1.1  tron   };
   2957  1.1  tron   static const int xmlLen =
   2958  1.1  tron     (int)sizeof(xmlNamespace)/sizeof(XML_Char) - 1;
   2959  1.1  tron   static const XML_Char xmlnsNamespace[] = {
   2960  1.1  tron     ASCII_h, ASCII_t, ASCII_t, ASCII_p, ASCII_COLON, ASCII_SLASH, ASCII_SLASH,
   2961  1.1  tron     ASCII_w, ASCII_w, ASCII_w, ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD,
   2962  1.1  tron     ASCII_o, ASCII_r, ASCII_g, ASCII_SLASH, ASCII_2, ASCII_0, ASCII_0,
   2963  1.1  tron     ASCII_0, ASCII_SLASH, ASCII_x, ASCII_m, ASCII_l, ASCII_n, ASCII_s,
   2964  1.1  tron     ASCII_SLASH, '\0'
   2965  1.1  tron   };
   2966  1.1  tron   static const int xmlnsLen =
   2967  1.1  tron     (int)sizeof(xmlnsNamespace)/sizeof(XML_Char) - 1;
   2968  1.1  tron 
   2969  1.1  tron   XML_Bool mustBeXML = XML_FALSE;
   2970  1.1  tron   XML_Bool isXML = XML_TRUE;
   2971  1.1  tron   XML_Bool isXMLNS = XML_TRUE;
   2972  1.1  tron 
   2973  1.1  tron   BINDING *b;
   2974  1.1  tron   int len;
   2975  1.1  tron 
   2976  1.1  tron   /* empty URI is only valid for default namespace per XML NS 1.0 (not 1.1) */
   2977  1.1  tron   if (*uri == XML_T('\0') && prefix->name)
   2978  1.1  tron     return XML_ERROR_UNDECLARING_PREFIX;
   2979  1.1  tron 
   2980  1.1  tron   if (prefix->name
   2981  1.1  tron       && prefix->name[0] == XML_T(ASCII_x)
   2982  1.1  tron       && prefix->name[1] == XML_T(ASCII_m)
   2983  1.1  tron       && prefix->name[2] == XML_T(ASCII_l)) {
   2984  1.1  tron 
   2985  1.1  tron     /* Not allowed to bind xmlns */
   2986  1.1  tron     if (prefix->name[3] == XML_T(ASCII_n)
   2987  1.1  tron         && prefix->name[4] == XML_T(ASCII_s)
   2988  1.1  tron         && prefix->name[5] == XML_T('\0'))
   2989  1.1  tron       return XML_ERROR_RESERVED_PREFIX_XMLNS;
   2990  1.1  tron 
   2991  1.1  tron     if (prefix->name[3] == XML_T('\0'))
   2992  1.1  tron       mustBeXML = XML_TRUE;
   2993  1.1  tron   }
   2994  1.1  tron 
   2995  1.1  tron   for (len = 0; uri[len]; len++) {
   2996  1.1  tron     if (isXML && (len > xmlLen || uri[len] != xmlNamespace[len]))
   2997  1.1  tron       isXML = XML_FALSE;
   2998  1.1  tron 
   2999  1.1  tron     if (!mustBeXML && isXMLNS
   3000  1.1  tron         && (len > xmlnsLen || uri[len] != xmlnsNamespace[len]))
   3001  1.1  tron       isXMLNS = XML_FALSE;
   3002  1.1  tron   }
   3003  1.1  tron   isXML = isXML && len == xmlLen;
   3004  1.1  tron   isXMLNS = isXMLNS && len == xmlnsLen;
   3005  1.1  tron 
   3006  1.1  tron   if (mustBeXML != isXML)
   3007  1.1  tron     return mustBeXML ? XML_ERROR_RESERVED_PREFIX_XML
   3008  1.1  tron                      : XML_ERROR_RESERVED_NAMESPACE_URI;
   3009  1.1  tron 
   3010  1.1  tron   if (isXMLNS)
   3011  1.1  tron     return XML_ERROR_RESERVED_NAMESPACE_URI;
   3012  1.1  tron 
   3013  1.1  tron   if (namespaceSeparator)
   3014  1.1  tron     len++;
   3015  1.1  tron   if (freeBindingList) {
   3016  1.1  tron     b = freeBindingList;
   3017  1.1  tron     if (len > b->uriAlloc) {
   3018  1.1  tron       XML_Char *temp = (XML_Char *)REALLOC(b->uri,
   3019  1.1  tron                           sizeof(XML_Char) * (len + EXPAND_SPARE));
   3020  1.1  tron       if (temp == NULL)
   3021  1.1  tron         return XML_ERROR_NO_MEMORY;
   3022  1.1  tron       b->uri = temp;
   3023  1.1  tron       b->uriAlloc = len + EXPAND_SPARE;
   3024  1.1  tron     }
   3025  1.1  tron     freeBindingList = b->nextTagBinding;
   3026  1.1  tron   }
   3027  1.1  tron   else {
   3028  1.1  tron     b = (BINDING *)MALLOC(sizeof(BINDING));
   3029  1.1  tron     if (!b)
   3030  1.1  tron       return XML_ERROR_NO_MEMORY;
   3031  1.1  tron     b->uri = (XML_Char *)MALLOC(sizeof(XML_Char) * (len + EXPAND_SPARE));
   3032  1.1  tron     if (!b->uri) {
   3033  1.1  tron       FREE(b);
   3034  1.1  tron       return XML_ERROR_NO_MEMORY;
   3035  1.1  tron     }
   3036  1.1  tron     b->uriAlloc = len + EXPAND_SPARE;
   3037  1.1  tron   }
   3038  1.1  tron   b->uriLen = len;
   3039  1.1  tron   memcpy(b->uri, uri, len * sizeof(XML_Char));
   3040  1.1  tron   if (namespaceSeparator)
   3041  1.1  tron     b->uri[len - 1] = namespaceSeparator;
   3042  1.1  tron   b->prefix = prefix;
   3043  1.1  tron   b->attId = attId;
   3044  1.1  tron   b->prevPrefixBinding = prefix->binding;
   3045  1.1  tron   /* NULL binding when default namespace undeclared */
   3046  1.1  tron   if (*uri == XML_T('\0') && prefix == &_dtd->defaultPrefix)
   3047  1.1  tron     prefix->binding = NULL;
   3048  1.1  tron   else
   3049  1.1  tron     prefix->binding = b;
   3050  1.1  tron   b->nextTagBinding = *bindingsPtr;
   3051  1.1  tron   *bindingsPtr = b;
   3052  1.1  tron   /* if attId == NULL then we are not starting a namespace scope */
   3053  1.1  tron   if (attId && startNamespaceDeclHandler)
   3054  1.1  tron     startNamespaceDeclHandler(handlerArg, prefix->name,
   3055  1.1  tron                               prefix->binding ? uri : 0);
   3056  1.1  tron   return XML_ERROR_NONE;
   3057  1.1  tron }
   3058  1.1  tron 
   3059  1.1  tron /* The idea here is to avoid using stack for each CDATA section when
   3060  1.1  tron    the whole file is parsed with one call.
   3061  1.1  tron */
   3062  1.1  tron static enum XML_Error PTRCALL
   3063  1.1  tron cdataSectionProcessor(XML_Parser parser,
   3064  1.1  tron                       const char *start,
   3065  1.1  tron                       const char *end,
   3066  1.1  tron                       const char **endPtr)
   3067  1.1  tron {
   3068  1.1  tron   enum XML_Error result = doCdataSection(parser, encoding, &start, end,
   3069  1.1  tron                                          endPtr, (XML_Bool)!ps_finalBuffer);
   3070  1.1  tron   if (result != XML_ERROR_NONE)
   3071  1.1  tron     return result;
   3072  1.1  tron   if (start) {
   3073  1.1  tron     if (parentParser) {  /* we are parsing an external entity */
   3074  1.1  tron       processor = externalEntityContentProcessor;
   3075  1.1  tron       return externalEntityContentProcessor(parser, start, end, endPtr);
   3076  1.1  tron     }
   3077  1.1  tron     else {
   3078  1.1  tron       processor = contentProcessor;
   3079  1.1  tron       return contentProcessor(parser, start, end, endPtr);
   3080  1.1  tron     }
   3081  1.1  tron   }
   3082  1.1  tron   return result;
   3083  1.1  tron }
   3084  1.1  tron 
   3085  1.1  tron /* startPtr gets set to non-null if the section is closed, and to null if
   3086  1.1  tron    the section is not yet closed.
   3087  1.1  tron */
   3088  1.1  tron static enum XML_Error
   3089  1.1  tron doCdataSection(XML_Parser parser,
   3090  1.1  tron                const ENCODING *enc,
   3091  1.1  tron                const char **startPtr,
   3092  1.1  tron                const char *end,
   3093  1.1  tron                const char **nextPtr,
   3094  1.1  tron                XML_Bool haveMore)
   3095  1.1  tron {
   3096  1.1  tron   const char *s = *startPtr;
   3097  1.1  tron   const char **eventPP;
   3098  1.1  tron   const char **eventEndPP;
   3099  1.1  tron   if (enc == encoding) {
   3100  1.1  tron     eventPP = &eventPtr;
   3101  1.1  tron     *eventPP = s;
   3102  1.1  tron     eventEndPP = &eventEndPtr;
   3103  1.1  tron   }
   3104  1.1  tron   else {
   3105  1.1  tron     eventPP = &(openInternalEntities->internalEventPtr);
   3106  1.1  tron     eventEndPP = &(openInternalEntities->internalEventEndPtr);
   3107  1.1  tron   }
   3108  1.1  tron   *eventPP = s;
   3109  1.1  tron   *startPtr = NULL;
   3110  1.1  tron 
   3111  1.1  tron   for (;;) {
   3112  1.1  tron     const char *next;
   3113  1.1  tron     int tok = XmlCdataSectionTok(enc, s, end, &next);
   3114  1.1  tron     *eventEndPP = next;
   3115  1.1  tron     switch (tok) {
   3116  1.1  tron     case XML_TOK_CDATA_SECT_CLOSE:
   3117  1.1  tron       if (endCdataSectionHandler)
   3118  1.1  tron         endCdataSectionHandler(handlerArg);
   3119  1.1  tron #if 0
   3120  1.1  tron       /* see comment under XML_TOK_CDATA_SECT_OPEN */
   3121  1.1  tron       else if (characterDataHandler)
   3122  1.1  tron         characterDataHandler(handlerArg, dataBuf, 0);
   3123  1.1  tron #endif
   3124  1.1  tron       else if (defaultHandler)
   3125  1.1  tron         reportDefault(parser, enc, s, next);
   3126  1.1  tron       *startPtr = next;
   3127  1.1  tron       *nextPtr = next;
   3128  1.1  tron       if (ps_parsing == XML_FINISHED)
   3129  1.1  tron         return XML_ERROR_ABORTED;
   3130  1.1  tron       else
   3131  1.1  tron         return XML_ERROR_NONE;
   3132  1.1  tron     case XML_TOK_DATA_NEWLINE:
   3133  1.1  tron       if (characterDataHandler) {
   3134  1.1  tron         XML_Char c = 0xA;
   3135  1.1  tron         characterDataHandler(handlerArg, &c, 1);
   3136  1.1  tron       }
   3137  1.1  tron       else if (defaultHandler)
   3138  1.1  tron         reportDefault(parser, enc, s, next);
   3139  1.1  tron       break;
   3140  1.1  tron     case XML_TOK_DATA_CHARS:
   3141  1.1  tron       {
   3142  1.1  tron         XML_CharacterDataHandler charDataHandler = characterDataHandler;
   3143  1.1  tron         if (charDataHandler) {
   3144  1.1  tron           if (MUST_CONVERT(enc, s)) {
   3145  1.1  tron             for (;;) {
   3146  1.1  tron               ICHAR *dataPtr = (ICHAR *)dataBuf;
   3147  1.1  tron               XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
   3148  1.1  tron               *eventEndPP = next;
   3149  1.1  tron               charDataHandler(handlerArg, dataBuf,
   3150  1.1  tron                               (int)(dataPtr - (ICHAR *)dataBuf));
   3151  1.1  tron               if (s == next)
   3152  1.1  tron                 break;
   3153  1.1  tron               *eventPP = s;
   3154  1.1  tron             }
   3155  1.1  tron           }
   3156  1.1  tron           else
   3157  1.1  tron             charDataHandler(handlerArg,
   3158  1.1  tron                             (XML_Char *)s,
   3159  1.1  tron                             (int)((XML_Char *)next - (XML_Char *)s));
   3160  1.1  tron         }
   3161  1.1  tron         else if (defaultHandler)
   3162  1.1  tron           reportDefault(parser, enc, s, next);
   3163  1.1  tron       }
   3164  1.1  tron       break;
   3165  1.1  tron     case XML_TOK_INVALID:
   3166  1.1  tron       *eventPP = next;
   3167  1.1  tron       return XML_ERROR_INVALID_TOKEN;
   3168  1.1  tron     case XML_TOK_PARTIAL_CHAR:
   3169  1.1  tron       if (haveMore) {
   3170  1.1  tron         *nextPtr = s;
   3171  1.1  tron         return XML_ERROR_NONE;
   3172  1.1  tron       }
   3173  1.1  tron       return XML_ERROR_PARTIAL_CHAR;
   3174  1.1  tron     case XML_TOK_PARTIAL:
   3175  1.1  tron     case XML_TOK_NONE:
   3176  1.1  tron       if (haveMore) {
   3177  1.1  tron         *nextPtr = s;
   3178  1.1  tron         return XML_ERROR_NONE;
   3179  1.1  tron       }
   3180  1.1  tron       return XML_ERROR_UNCLOSED_CDATA_SECTION;
   3181  1.1  tron     default:
   3182  1.1  tron       *eventPP = next;
   3183  1.1  tron       return XML_ERROR_UNEXPECTED_STATE;
   3184  1.1  tron     }
   3185  1.1  tron 
   3186  1.1  tron     *eventPP = s = next;
   3187  1.1  tron     switch (ps_parsing) {
   3188  1.1  tron     case XML_SUSPENDED:
   3189  1.1  tron       *nextPtr = next;
   3190  1.1  tron       return XML_ERROR_NONE;
   3191  1.1  tron     case XML_FINISHED:
   3192  1.1  tron       return XML_ERROR_ABORTED;
   3193  1.1  tron     default: ;
   3194  1.1  tron     }
   3195  1.1  tron   }
   3196  1.1  tron   /* not reached */
   3197  1.1  tron }
   3198  1.1  tron 
   3199  1.1  tron #ifdef XML_DTD
   3200  1.1  tron 
   3201  1.1  tron /* The idea here is to avoid using stack for each IGNORE section when
   3202  1.1  tron    the whole file is parsed with one call.
   3203  1.1  tron */
   3204  1.1  tron static enum XML_Error PTRCALL
   3205  1.1  tron ignoreSectionProcessor(XML_Parser parser,
   3206  1.1  tron                        const char *start,
   3207  1.1  tron                        const char *end,
   3208  1.1  tron                        const char **endPtr)
   3209  1.1  tron {
   3210  1.1  tron   enum XML_Error result = doIgnoreSection(parser, encoding, &start, end,
   3211  1.1  tron                                           endPtr, (XML_Bool)!ps_finalBuffer);
   3212  1.1  tron   if (result != XML_ERROR_NONE)
   3213  1.1  tron     return result;
   3214  1.1  tron   if (start) {
   3215  1.1  tron     processor = prologProcessor;
   3216  1.1  tron     return prologProcessor(parser, start, end, endPtr);
   3217  1.1  tron   }
   3218  1.1  tron   return result;
   3219  1.1  tron }
   3220  1.1  tron 
   3221  1.1  tron /* startPtr gets set to non-null is the section is closed, and to null
   3222  1.1  tron    if the section is not yet closed.
   3223  1.1  tron */
   3224  1.1  tron static enum XML_Error
   3225  1.1  tron doIgnoreSection(XML_Parser parser,
   3226  1.1  tron                 const ENCODING *enc,
   3227  1.1  tron                 const char **startPtr,
   3228  1.1  tron                 const char *end,
   3229  1.1  tron                 const char **nextPtr,
   3230  1.1  tron                 XML_Bool haveMore)
   3231  1.1  tron {
   3232  1.1  tron   const char *next;
   3233  1.1  tron   int tok;
   3234  1.1  tron   const char *s = *startPtr;
   3235  1.1  tron   const char **eventPP;
   3236  1.1  tron   const char **eventEndPP;
   3237  1.1  tron   if (enc == encoding) {
   3238  1.1  tron     eventPP = &eventPtr;
   3239  1.1  tron     *eventPP = s;
   3240  1.1  tron     eventEndPP = &eventEndPtr;
   3241  1.1  tron   }
   3242  1.1  tron   else {
   3243  1.1  tron     eventPP = &(openInternalEntities->internalEventPtr);
   3244  1.1  tron     eventEndPP = &(openInternalEntities->internalEventEndPtr);
   3245  1.1  tron   }
   3246  1.1  tron   *eventPP = s;
   3247  1.1  tron   *startPtr = NULL;
   3248  1.1  tron   tok = XmlIgnoreSectionTok(enc, s, end, &next);
   3249  1.1  tron   *eventEndPP = next;
   3250  1.1  tron   switch (tok) {
   3251  1.1  tron   case XML_TOK_IGNORE_SECT:
   3252  1.1  tron     if (defaultHandler)
   3253  1.1  tron       reportDefault(parser, enc, s, next);
   3254  1.1  tron     *startPtr = next;
   3255  1.1  tron     *nextPtr = next;
   3256  1.1  tron     if (ps_parsing == XML_FINISHED)
   3257  1.1  tron       return XML_ERROR_ABORTED;
   3258  1.1  tron     else
   3259  1.1  tron       return XML_ERROR_NONE;
   3260  1.1  tron   case XML_TOK_INVALID:
   3261  1.1  tron     *eventPP = next;
   3262  1.1  tron     return XML_ERROR_INVALID_TOKEN;
   3263  1.1  tron   case XML_TOK_PARTIAL_CHAR:
   3264  1.1  tron     if (haveMore) {
   3265  1.1  tron       *nextPtr = s;
   3266  1.1  tron       return XML_ERROR_NONE;
   3267  1.1  tron     }
   3268  1.1  tron     return XML_ERROR_PARTIAL_CHAR;
   3269  1.1  tron   case XML_TOK_PARTIAL:
   3270  1.1  tron   case XML_TOK_NONE:
   3271  1.1  tron     if (haveMore) {
   3272  1.1  tron       *nextPtr = s;
   3273  1.1  tron       return XML_ERROR_NONE;
   3274  1.1  tron     }
   3275  1.1  tron     return XML_ERROR_SYNTAX; /* XML_ERROR_UNCLOSED_IGNORE_SECTION */
   3276  1.1  tron   default:
   3277  1.1  tron     *eventPP = next;
   3278  1.1  tron     return XML_ERROR_UNEXPECTED_STATE;
   3279  1.1  tron   }
   3280  1.1  tron   /* not reached */
   3281  1.1  tron }
   3282  1.1  tron 
   3283  1.1  tron #endif /* XML_DTD */
   3284  1.1  tron 
   3285  1.1  tron static enum XML_Error
   3286  1.1  tron initializeEncoding(XML_Parser parser)
   3287  1.1  tron {
   3288  1.1  tron   const char *s;
   3289  1.1  tron #ifdef XML_UNICODE
   3290  1.1  tron   char encodingBuf[128];
   3291  1.1  tron   if (!protocolEncodingName)
   3292  1.1  tron     s = NULL;
   3293  1.1  tron   else {
   3294  1.1  tron     int i;
   3295  1.1  tron     for (i = 0; protocolEncodingName[i]; i++) {
   3296  1.1  tron       if (i == sizeof(encodingBuf) - 1
   3297  1.1  tron           || (protocolEncodingName[i] & ~0x7f) != 0) {
   3298  1.1  tron         encodingBuf[0] = '\0';
   3299  1.1  tron         break;
   3300  1.1  tron       }
   3301  1.1  tron       encodingBuf[i] = (char)protocolEncodingName[i];
   3302  1.1  tron     }
   3303  1.1  tron     encodingBuf[i] = '\0';
   3304  1.1  tron     s = encodingBuf;
   3305  1.1  tron   }
   3306  1.1  tron #else
   3307  1.1  tron   s = protocolEncodingName;
   3308  1.1  tron #endif
   3309  1.1  tron   if ((ns ? XmlInitEncodingNS : XmlInitEncoding)(&initEncoding, &encoding, s))
   3310  1.1  tron     return XML_ERROR_NONE;
   3311  1.1  tron   return handleUnknownEncoding(parser, protocolEncodingName);
   3312  1.1  tron }
   3313  1.1  tron 
   3314  1.1  tron static enum XML_Error
   3315  1.1  tron processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
   3316  1.1  tron                const char *s, const char *next)
   3317  1.1  tron {
   3318  1.1  tron   const char *encodingName = NULL;
   3319  1.1  tron   const XML_Char *storedEncName = NULL;
   3320  1.1  tron   const ENCODING *newEncoding = NULL;
   3321  1.1  tron   const char *version = NULL;
   3322  1.1  tron   const char *versionend;
   3323  1.1  tron   const XML_Char *storedversion = NULL;
   3324  1.1  tron   int standalone = -1;
   3325  1.1  tron   if (!(ns
   3326  1.1  tron         ? XmlParseXmlDeclNS
   3327  1.1  tron         : XmlParseXmlDecl)(isGeneralTextEntity,
   3328  1.1  tron                            encoding,
   3329  1.1  tron                            s,
   3330  1.1  tron                            next,
   3331  1.1  tron                            &eventPtr,
   3332  1.1  tron                            &version,
   3333  1.1  tron                            &versionend,
   3334  1.1  tron                            &encodingName,
   3335  1.1  tron                            &newEncoding,
   3336  1.1  tron                            &standalone)) {
   3337  1.1  tron     if (isGeneralTextEntity)
   3338  1.1  tron       return XML_ERROR_TEXT_DECL;
   3339  1.1  tron     else
   3340  1.1  tron       return XML_ERROR_XML_DECL;
   3341  1.1  tron   }
   3342  1.1  tron   if (!isGeneralTextEntity && standalone == 1) {
   3343  1.1  tron     _dtd->standalone = XML_TRUE;
   3344  1.1  tron #ifdef XML_DTD
   3345  1.1  tron     if (paramEntityParsing == XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
   3346  1.1  tron       paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
   3347  1.1  tron #endif /* XML_DTD */
   3348  1.1  tron   }
   3349  1.1  tron   if (xmlDeclHandler) {
   3350  1.1  tron     if (encodingName != NULL) {
   3351  1.1  tron       storedEncName = poolStoreString(&temp2Pool,
   3352  1.1  tron                                       encoding,
   3353  1.1  tron                                       encodingName,
   3354  1.1  tron                                       encodingName
   3355  1.1  tron                                       + XmlNameLength(encoding, encodingName));
   3356  1.1  tron       if (!storedEncName)
   3357  1.1  tron               return XML_ERROR_NO_MEMORY;
   3358  1.1  tron       poolFinish(&temp2Pool);
   3359  1.1  tron     }
   3360  1.1  tron     if (version) {
   3361  1.1  tron       storedversion = poolStoreString(&temp2Pool,
   3362  1.1  tron                                       encoding,
   3363  1.1  tron                                       version,
   3364  1.1  tron                                       versionend - encoding->minBytesPerChar);
   3365  1.1  tron       if (!storedversion)
   3366  1.1  tron         return XML_ERROR_NO_MEMORY;
   3367  1.1  tron     }
   3368  1.1  tron     xmlDeclHandler(handlerArg, storedversion, storedEncName, standalone);
   3369  1.1  tron   }
   3370  1.1  tron   else if (defaultHandler)
   3371  1.1  tron     reportDefault(parser, encoding, s, next);
   3372  1.1  tron   if (protocolEncodingName == NULL) {
   3373  1.1  tron     if (newEncoding) {
   3374  1.1  tron       if (newEncoding->minBytesPerChar != encoding->minBytesPerChar) {
   3375  1.1  tron         eventPtr = encodingName;
   3376  1.1  tron         return XML_ERROR_INCORRECT_ENCODING;
   3377  1.1  tron       }
   3378  1.1  tron       encoding = newEncoding;
   3379  1.1  tron     }
   3380  1.1  tron     else if (encodingName) {
   3381  1.1  tron       enum XML_Error result;
   3382  1.1  tron       if (!storedEncName) {
   3383  1.1  tron         storedEncName = poolStoreString(
   3384  1.1  tron           &temp2Pool, encoding, encodingName,
   3385  1.1  tron           encodingName + XmlNameLength(encoding, encodingName));
   3386  1.1  tron         if (!storedEncName)
   3387  1.1  tron           return XML_ERROR_NO_MEMORY;
   3388  1.1  tron       }
   3389  1.1  tron       result = handleUnknownEncoding(parser, storedEncName);
   3390  1.1  tron       poolClear(&temp2Pool);
   3391  1.1  tron       if (result == XML_ERROR_UNKNOWN_ENCODING)
   3392  1.1  tron         eventPtr = encodingName;
   3393  1.1  tron       return result;
   3394  1.1  tron     }
   3395  1.1  tron   }
   3396  1.1  tron 
   3397  1.1  tron   if (storedEncName || storedversion)
   3398  1.1  tron     poolClear(&temp2Pool);
   3399  1.1  tron 
   3400  1.1  tron   return XML_ERROR_NONE;
   3401  1.1  tron }
   3402  1.1  tron 
   3403  1.1  tron static enum XML_Error
   3404  1.1  tron handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName)
   3405  1.1  tron {
   3406  1.1  tron   if (unknownEncodingHandler) {
   3407  1.1  tron     XML_Encoding info;
   3408  1.1  tron     int i;
   3409  1.1  tron     for (i = 0; i < 256; i++)
   3410  1.1  tron       info.map[i] = -1;
   3411  1.1  tron     info.convert = NULL;
   3412  1.1  tron     info.data = NULL;
   3413  1.1  tron     info.release = NULL;
   3414  1.1  tron     if (unknownEncodingHandler(unknownEncodingHandlerData, encodingName,
   3415  1.1  tron                                &info)) {
   3416  1.1  tron       ENCODING *enc;
   3417  1.1  tron       unknownEncodingMem = MALLOC(XmlSizeOfUnknownEncoding());
   3418  1.1  tron       if (!unknownEncodingMem) {
   3419  1.1  tron         if (info.release)
   3420  1.1  tron           info.release(info.data);
   3421  1.1  tron         return XML_ERROR_NO_MEMORY;
   3422  1.1  tron       }
   3423  1.1  tron       enc = (ns
   3424  1.1  tron              ? XmlInitUnknownEncodingNS
   3425  1.1  tron              : XmlInitUnknownEncoding)(unknownEncodingMem,
   3426  1.1  tron                                        info.map,
   3427  1.1  tron                                        info.convert,
   3428  1.1  tron                                        info.data);
   3429  1.1  tron       if (enc) {
   3430  1.1  tron         unknownEncodingData = info.data;
   3431  1.1  tron         unknownEncodingRelease = info.release;
   3432  1.1  tron         encoding = enc;
   3433  1.1  tron         return XML_ERROR_NONE;
   3434  1.1  tron       }
   3435  1.1  tron     }
   3436  1.1  tron     if (info.release != NULL)
   3437  1.1  tron       info.release(info.data);
   3438  1.1  tron   }
   3439  1.1  tron   return XML_ERROR_UNKNOWN_ENCODING;
   3440  1.1  tron }
   3441  1.1  tron 
   3442  1.1  tron static enum XML_Error PTRCALL
   3443  1.1  tron prologInitProcessor(XML_Parser parser,
   3444  1.1  tron                     const char *s,
   3445  1.1  tron                     const char *end,
   3446  1.1  tron                     const char **nextPtr)
   3447  1.1  tron {
   3448  1.1  tron   enum XML_Error result = initializeEncoding(parser);
   3449  1.1  tron   if (result != XML_ERROR_NONE)
   3450  1.1  tron     return result;
   3451  1.1  tron   processor = prologProcessor;
   3452  1.1  tron   return prologProcessor(parser, s, end, nextPtr);
   3453  1.1  tron }
   3454  1.1  tron 
   3455  1.1  tron #ifdef XML_DTD
   3456  1.1  tron 
   3457  1.1  tron static enum XML_Error PTRCALL
   3458  1.1  tron externalParEntInitProcessor(XML_Parser parser,
   3459  1.1  tron                             const char *s,
   3460  1.1  tron                             const char *end,
   3461  1.1  tron                             const char **nextPtr)
   3462  1.1  tron {
   3463  1.1  tron   enum XML_Error result = initializeEncoding(parser);
   3464  1.1  tron   if (result != XML_ERROR_NONE)
   3465  1.1  tron     return result;
   3466  1.1  tron 
   3467  1.1  tron   /* we know now that XML_Parse(Buffer) has been called,
   3468  1.1  tron      so we consider the external parameter entity read */
   3469  1.1  tron   _dtd->paramEntityRead = XML_TRUE;
   3470  1.1  tron 
   3471  1.1  tron   if (prologState.inEntityValue) {
   3472  1.1  tron     processor = entityValueInitProcessor;
   3473  1.1  tron     return entityValueInitProcessor(parser, s, end, nextPtr);
   3474  1.1  tron   }
   3475  1.1  tron   else {
   3476  1.1  tron     processor = externalParEntProcessor;
   3477  1.1  tron     return externalParEntProcessor(parser, s, end, nextPtr);
   3478  1.1  tron   }
   3479  1.1  tron }
   3480  1.1  tron 
   3481  1.1  tron static enum XML_Error PTRCALL
   3482  1.1  tron entityValueInitProcessor(XML_Parser parser,
   3483  1.1  tron                          const char *s,
   3484  1.1  tron                          const char *end,
   3485  1.1  tron                          const char **nextPtr)
   3486  1.1  tron {
   3487  1.1  tron   int tok;
   3488  1.1  tron   const char *start = s;
   3489  1.1  tron   const char *next = start;
   3490  1.1  tron   eventPtr = start;
   3491  1.1  tron 
   3492  1.1  tron   for (;;) {
   3493  1.1  tron     tok = XmlPrologTok(encoding, start, end, &next);
   3494  1.1  tron     eventEndPtr = next;
   3495  1.1  tron     if (tok <= 0) {
   3496  1.1  tron       if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
   3497  1.1  tron         *nextPtr = s;
   3498  1.1  tron         return XML_ERROR_NONE;
   3499  1.1  tron       }
   3500  1.1  tron       switch (tok) {
   3501  1.1  tron       case XML_TOK_INVALID:
   3502  1.1  tron         return XML_ERROR_INVALID_TOKEN;
   3503  1.1  tron       case XML_TOK_PARTIAL:
   3504  1.1  tron         return XML_ERROR_UNCLOSED_TOKEN;
   3505  1.1  tron       case XML_TOK_PARTIAL_CHAR:
   3506  1.1  tron         return XML_ERROR_PARTIAL_CHAR;
   3507  1.1  tron       case XML_TOK_NONE:   /* start == end */
   3508  1.1  tron       default:
   3509  1.1  tron         break;
   3510  1.1  tron       }
   3511  1.1  tron       /* found end of entity value - can store it now */
   3512  1.1  tron       return storeEntityValue(parser, encoding, s, end);
   3513  1.1  tron     }
   3514  1.1  tron     else if (tok == XML_TOK_XML_DECL) {
   3515  1.1  tron       enum XML_Error result;
   3516  1.1  tron       result = processXmlDecl(parser, 0, start, next);
   3517  1.1  tron       if (result != XML_ERROR_NONE)
   3518  1.1  tron         return result;
   3519  1.1  tron       switch (ps_parsing) {
   3520  1.1  tron       case XML_SUSPENDED:
   3521  1.1  tron         *nextPtr = next;
   3522  1.1  tron         return XML_ERROR_NONE;
   3523  1.1  tron       case XML_FINISHED:
   3524  1.1  tron         return XML_ERROR_ABORTED;
   3525  1.1  tron       default:
   3526  1.1  tron         *nextPtr = next;
   3527  1.1  tron       }
   3528  1.1  tron       /* stop scanning for text declaration - we found one */
   3529  1.1  tron       processor = entityValueProcessor;
   3530  1.1  tron       return entityValueProcessor(parser, next, end, nextPtr);
   3531  1.1  tron     }
   3532  1.1  tron     /* If we are at the end of the buffer, this would cause XmlPrologTok to
   3533  1.1  tron        return XML_TOK_NONE on the next call, which would then cause the
   3534  1.1  tron        function to exit with *nextPtr set to s - that is what we want for other
   3535  1.1  tron        tokens, but not for the BOM - we would rather like to skip it;
   3536  1.1  tron        then, when this routine is entered the next time, XmlPrologTok will
   3537  1.1  tron        return XML_TOK_INVALID, since the BOM is still in the buffer
   3538  1.1  tron     */
   3539  1.1  tron     else if (tok == XML_TOK_BOM && next == end && !ps_finalBuffer) {
   3540  1.1  tron       *nextPtr = next;
   3541  1.1  tron       return XML_ERROR_NONE;
   3542  1.1  tron     }
   3543  1.1  tron     start = next;
   3544  1.1  tron     eventPtr = start;
   3545  1.1  tron   }
   3546  1.1  tron }
   3547  1.1  tron 
   3548  1.1  tron static enum XML_Error PTRCALL
   3549  1.1  tron externalParEntProcessor(XML_Parser parser,
   3550  1.1  tron                         const char *s,
   3551  1.1  tron                         const char *end,
   3552  1.1  tron                         const char **nextPtr)
   3553  1.1  tron {
   3554  1.1  tron   const char *next = s;
   3555  1.1  tron   int tok;
   3556  1.1  tron 
   3557  1.1  tron   tok = XmlPrologTok(encoding, s, end, &next);
   3558  1.1  tron   if (tok <= 0) {
   3559  1.1  tron     if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
   3560  1.1  tron       *nextPtr = s;
   3561  1.1  tron       return XML_ERROR_NONE;
   3562  1.1  tron     }
   3563  1.1  tron     switch (tok) {
   3564  1.1  tron     case XML_TOK_INVALID:
   3565  1.1  tron       return XML_ERROR_INVALID_TOKEN;
   3566  1.1  tron     case XML_TOK_PARTIAL:
   3567  1.1  tron       return XML_ERROR_UNCLOSED_TOKEN;
   3568  1.1  tron     case XML_TOK_PARTIAL_CHAR:
   3569  1.1  tron       return XML_ERROR_PARTIAL_CHAR;
   3570  1.1  tron     case XML_TOK_NONE:   /* start == end */
   3571  1.1  tron     default:
   3572  1.1  tron       break;
   3573  1.1  tron     }
   3574  1.1  tron   }
   3575  1.1  tron   /* This would cause the next stage, i.e. doProlog to be passed XML_TOK_BOM.
   3576  1.1  tron      However, when parsing an external subset, doProlog will not accept a BOM
   3577  1.1  tron      as valid, and report a syntax error, so we have to skip the BOM
   3578  1.1  tron   */
   3579  1.1  tron   else if (tok == XML_TOK_BOM) {
   3580  1.1  tron     s = next;
   3581  1.1  tron     tok = XmlPrologTok(encoding, s, end, &next);
   3582  1.1  tron   }
   3583  1.1  tron 
   3584  1.1  tron   processor = prologProcessor;
   3585  1.1  tron   return doProlog(parser, encoding, s, end, tok, next,
   3586  1.1  tron                   nextPtr, (XML_Bool)!ps_finalBuffer);
   3587  1.1  tron }
   3588  1.1  tron 
   3589  1.1  tron static enum XML_Error PTRCALL
   3590  1.1  tron entityValueProcessor(XML_Parser parser,
   3591  1.1  tron                      const char *s,
   3592  1.1  tron                      const char *end,
   3593  1.1  tron                      const char **nextPtr)
   3594  1.1  tron {
   3595  1.1  tron   const char *start = s;
   3596  1.1  tron   const char *next = s;
   3597  1.1  tron   const ENCODING *enc = encoding;
   3598  1.1  tron   int tok;
   3599  1.1  tron 
   3600  1.1  tron   for (;;) {
   3601  1.1  tron     tok = XmlPrologTok(enc, start, end, &next);
   3602  1.1  tron     if (tok <= 0) {
   3603  1.1  tron       if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
   3604  1.1  tron         *nextPtr = s;
   3605  1.1  tron         return XML_ERROR_NONE;
   3606  1.1  tron       }
   3607  1.1  tron       switch (tok) {
   3608  1.1  tron       case XML_TOK_INVALID:
   3609  1.1  tron         return XML_ERROR_INVALID_TOKEN;
   3610  1.1  tron       case XML_TOK_PARTIAL:
   3611  1.1  tron         return XML_ERROR_UNCLOSED_TOKEN;
   3612  1.1  tron       case XML_TOK_PARTIAL_CHAR:
   3613  1.1  tron         return XML_ERROR_PARTIAL_CHAR;
   3614  1.1  tron       case XML_TOK_NONE:   /* start == end */
   3615  1.1  tron       default:
   3616  1.1  tron         break;
   3617  1.1  tron       }
   3618  1.1  tron       /* found end of entity value - can store it now */
   3619  1.1  tron       return storeEntityValue(parser, enc, s, end);
   3620  1.1  tron     }
   3621  1.1  tron     start = next;
   3622  1.1  tron   }
   3623  1.1  tron }
   3624  1.1  tron 
   3625  1.1  tron #endif /* XML_DTD */
   3626  1.1  tron 
   3627  1.1  tron static enum XML_Error PTRCALL
   3628  1.1  tron prologProcessor(XML_Parser parser,
   3629  1.1  tron                 const char *s,
   3630  1.1  tron                 const char *end,
   3631  1.1  tron                 const char **nextPtr)
   3632  1.1  tron {
   3633  1.1  tron   const char *next = s;
   3634  1.1  tron   int tok = XmlPrologTok(encoding, s, end, &next);
   3635  1.1  tron   return doProlog(parser, encoding, s, end, tok, next,
   3636  1.1  tron                   nextPtr, (XML_Bool)!ps_finalBuffer);
   3637  1.1  tron }
   3638  1.1  tron 
   3639  1.1  tron static enum XML_Error
   3640  1.1  tron doProlog(XML_Parser parser,
   3641  1.1  tron          const ENCODING *enc,
   3642  1.1  tron          const char *s,
   3643  1.1  tron          const char *end,
   3644  1.1  tron          int tok,
   3645  1.1  tron          const char *next,
   3646  1.1  tron          const char **nextPtr,
   3647  1.1  tron          XML_Bool haveMore)
   3648  1.1  tron {
   3649  1.1  tron #ifdef XML_DTD
   3650  1.1  tron   static const XML_Char externalSubsetName[] = { ASCII_HASH , '\0' };
   3651  1.1  tron #endif /* XML_DTD */
   3652  1.1  tron   static const XML_Char atypeCDATA[] =
   3653  1.1  tron       { ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
   3654  1.1  tron   static const XML_Char atypeID[] = { ASCII_I, ASCII_D, '\0' };
   3655  1.1  tron   static const XML_Char atypeIDREF[] =
   3656  1.1  tron       { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, '\0' };
   3657  1.1  tron   static const XML_Char atypeIDREFS[] =
   3658  1.1  tron       { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, ASCII_S, '\0' };
   3659  1.1  tron   static const XML_Char atypeENTITY[] =
   3660  1.1  tron       { ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_Y, '\0' };
   3661  1.1  tron   static const XML_Char atypeENTITIES[] = { ASCII_E, ASCII_N,
   3662  1.1  tron       ASCII_T, ASCII_I, ASCII_T, ASCII_I, ASCII_E, ASCII_S, '\0' };
   3663  1.1  tron   static const XML_Char atypeNMTOKEN[] = {
   3664  1.1  tron       ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, '\0' };
   3665  1.1  tron   static const XML_Char atypeNMTOKENS[] = { ASCII_N, ASCII_M, ASCII_T,
   3666  1.1  tron       ASCII_O, ASCII_K, ASCII_E, ASCII_N, ASCII_S, '\0' };
   3667  1.1  tron   static const XML_Char notationPrefix[] = { ASCII_N, ASCII_O, ASCII_T,
   3668  1.1  tron       ASCII_A, ASCII_T, ASCII_I, ASCII_O, ASCII_N, ASCII_LPAREN, '\0' };
   3669  1.1  tron   static const XML_Char enumValueSep[] = { ASCII_PIPE, '\0' };
   3670  1.1  tron   static const XML_Char enumValueStart[] = { ASCII_LPAREN, '\0' };
   3671  1.1  tron 
   3672  1.1  tron   /* save one level of indirection */
   3673  1.1  tron   DTD * const dtd = _dtd;
   3674  1.1  tron 
   3675  1.1  tron   const char **eventPP;
   3676  1.1  tron   const char **eventEndPP;
   3677  1.1  tron   enum XML_Content_Quant quant;
   3678  1.1  tron 
   3679  1.1  tron   if (enc == encoding) {
   3680  1.1  tron     eventPP = &eventPtr;
   3681  1.1  tron     eventEndPP = &eventEndPtr;
   3682  1.1  tron   }
   3683  1.1  tron   else {
   3684  1.1  tron     eventPP = &(openInternalEntities->internalEventPtr);
   3685  1.1  tron     eventEndPP = &(openInternalEntities->internalEventEndPtr);
   3686  1.1  tron   }
   3687  1.1  tron 
   3688  1.1  tron   for (;;) {
   3689  1.1  tron     int role;
   3690  1.1  tron     XML_Bool handleDefault = XML_TRUE;
   3691  1.1  tron     *eventPP = s;
   3692  1.1  tron     *eventEndPP = next;
   3693  1.1  tron     if (tok <= 0) {
   3694  1.1  tron       if (haveMore && tok != XML_TOK_INVALID) {
   3695  1.1  tron         *nextPtr = s;
   3696  1.1  tron         return XML_ERROR_NONE;
   3697  1.1  tron       }
   3698  1.1  tron       switch (tok) {
   3699  1.1  tron       case XML_TOK_INVALID:
   3700  1.1  tron         *eventPP = next;
   3701  1.1  tron         return XML_ERROR_INVALID_TOKEN;
   3702  1.1  tron       case XML_TOK_PARTIAL:
   3703  1.1  tron         return XML_ERROR_UNCLOSED_TOKEN;
   3704  1.1  tron       case XML_TOK_PARTIAL_CHAR:
   3705  1.1  tron         return XML_ERROR_PARTIAL_CHAR;
   3706  1.2  tron       case -XML_TOK_PROLOG_S:
   3707  1.2  tron 	tok = -tok;
   3708  1.2  tron 	break;
   3709  1.1  tron       case XML_TOK_NONE:
   3710  1.1  tron #ifdef XML_DTD
   3711  1.1  tron         /* for internal PE NOT referenced between declarations */
   3712  1.1  tron         if (enc != encoding && !openInternalEntities->betweenDecl) {
   3713  1.1  tron           *nextPtr = s;
   3714  1.1  tron           return XML_ERROR_NONE;
   3715  1.1  tron         }
   3716  1.1  tron         /* WFC: PE Between Declarations - must check that PE contains
   3717  1.1  tron            complete markup, not only for external PEs, but also for
   3718  1.1  tron            internal PEs if the reference occurs between declarations.
   3719  1.1  tron         */
   3720  1.1  tron         if (isParamEntity || enc != encoding) {
   3721  1.1  tron           if (XmlTokenRole(&prologState, XML_TOK_NONE, end, end, enc)
   3722  1.1  tron               == XML_ROLE_ERROR)
   3723  1.1  tron             return XML_ERROR_INCOMPLETE_PE;
   3724  1.1  tron           *nextPtr = s;
   3725  1.1  tron           return XML_ERROR_NONE;
   3726  1.1  tron         }
   3727  1.1  tron #endif /* XML_DTD */
   3728  1.1  tron         return XML_ERROR_NO_ELEMENTS;
   3729  1.1  tron       default:
   3730  1.1  tron         tok = -tok;
   3731  1.1  tron         next = end;
   3732  1.1  tron         break;
   3733  1.1  tron       }
   3734  1.1  tron     }
   3735  1.1  tron     role = XmlTokenRole(&prologState, tok, s, next, enc);
   3736  1.1  tron     switch (role) {
   3737  1.1  tron     case XML_ROLE_XML_DECL:
   3738  1.1  tron       {
   3739  1.1  tron         enum XML_Error result = processXmlDecl(parser, 0, s, next);
   3740  1.1  tron         if (result != XML_ERROR_NONE)
   3741  1.1  tron           return result;
   3742  1.1  tron         enc = encoding;
   3743  1.1  tron         handleDefault = XML_FALSE;
   3744  1.1  tron       }
   3745  1.1  tron       break;
   3746  1.1  tron     case XML_ROLE_DOCTYPE_NAME:
   3747  1.1  tron       if (startDoctypeDeclHandler) {
   3748  1.1  tron         doctypeName = poolStoreString(&tempPool, enc, s, next);
   3749  1.1  tron         if (!doctypeName)
   3750  1.1  tron           return XML_ERROR_NO_MEMORY;
   3751  1.1  tron         poolFinish(&tempPool);
   3752  1.1  tron         doctypePubid = NULL;
   3753  1.1  tron         handleDefault = XML_FALSE;
   3754  1.1  tron       }
   3755  1.1  tron       doctypeSysid = NULL; /* always initialize to NULL */
   3756  1.1  tron       break;
   3757  1.1  tron     case XML_ROLE_DOCTYPE_INTERNAL_SUBSET:
   3758  1.1  tron       if (startDoctypeDeclHandler) {
   3759  1.1  tron         startDoctypeDeclHandler(handlerArg, doctypeName, doctypeSysid,
   3760  1.1  tron                                 doctypePubid, 1);
   3761  1.1  tron         doctypeName = NULL;
   3762  1.1  tron         poolClear(&tempPool);
   3763  1.1  tron         handleDefault = XML_FALSE;
   3764  1.1  tron       }
   3765  1.1  tron       break;
   3766  1.1  tron #ifdef XML_DTD
   3767  1.1  tron     case XML_ROLE_TEXT_DECL:
   3768  1.1  tron       {
   3769  1.1  tron         enum XML_Error result = processXmlDecl(parser, 1, s, next);
   3770  1.1  tron         if (result != XML_ERROR_NONE)
   3771  1.1  tron           return result;
   3772  1.1  tron         enc = encoding;
   3773  1.1  tron         handleDefault = XML_FALSE;
   3774  1.1  tron       }
   3775  1.1  tron       break;
   3776  1.1  tron #endif /* XML_DTD */
   3777  1.1  tron     case XML_ROLE_DOCTYPE_PUBLIC_ID:
   3778  1.1  tron #ifdef XML_DTD
   3779  1.1  tron       useForeignDTD = XML_FALSE;
   3780  1.1  tron       declEntity = (ENTITY *)lookup(&dtd->paramEntities,
   3781  1.1  tron                                     externalSubsetName,
   3782  1.1  tron                                     sizeof(ENTITY));
   3783  1.1  tron       if (!declEntity)
   3784  1.1  tron         return XML_ERROR_NO_MEMORY;
   3785  1.1  tron #endif /* XML_DTD */
   3786  1.1  tron       dtd->hasParamEntityRefs = XML_TRUE;
   3787  1.1  tron       if (startDoctypeDeclHandler) {
   3788  1.1  tron         if (!XmlIsPublicId(enc, s, next, eventPP))
   3789  1.1  tron           return XML_ERROR_PUBLICID;
   3790  1.1  tron         doctypePubid = poolStoreString(&tempPool, enc,
   3791  1.1  tron                                        s + enc->minBytesPerChar,
   3792  1.1  tron                                        next - enc->minBytesPerChar);
   3793  1.1  tron         if (!doctypePubid)
   3794  1.1  tron           return XML_ERROR_NO_MEMORY;
   3795  1.1  tron         normalizePublicId((XML_Char *)doctypePubid);
   3796  1.1  tron         poolFinish(&tempPool);
   3797  1.1  tron         handleDefault = XML_FALSE;
   3798  1.1  tron         goto alreadyChecked;
   3799  1.1  tron       }
   3800  1.1  tron       /* fall through */
   3801  1.1  tron     case XML_ROLE_ENTITY_PUBLIC_ID:
   3802  1.1  tron       if (!XmlIsPublicId(enc, s, next, eventPP))
   3803  1.1  tron         return XML_ERROR_PUBLICID;
   3804  1.1  tron     alreadyChecked:
   3805  1.1  tron       if (dtd->keepProcessing && declEntity) {
   3806  1.1  tron         XML_Char *tem = poolStoreString(&dtd->pool,
   3807  1.1  tron                                         enc,
   3808  1.1  tron                                         s + enc->minBytesPerChar,
   3809  1.1  tron                                         next - enc->minBytesPerChar);
   3810  1.1  tron         if (!tem)
   3811  1.1  tron           return XML_ERROR_NO_MEMORY;
   3812  1.1  tron         normalizePublicId(tem);
   3813  1.1  tron         declEntity->publicId = tem;
   3814  1.1  tron         poolFinish(&dtd->pool);
   3815  1.1  tron         if (entityDeclHandler)
   3816  1.1  tron           handleDefault = XML_FALSE;
   3817  1.1  tron       }
   3818  1.1  tron       break;
   3819  1.1  tron     case XML_ROLE_DOCTYPE_CLOSE:
   3820  1.1  tron       if (doctypeName) {
   3821  1.1  tron         startDoctypeDeclHandler(handlerArg, doctypeName,
   3822  1.1  tron                                 doctypeSysid, doctypePubid, 0);
   3823  1.1  tron         poolClear(&tempPool);
   3824  1.1  tron         handleDefault = XML_FALSE;
   3825  1.1  tron       }
   3826  1.1  tron       /* doctypeSysid will be non-NULL in the case of a previous
   3827  1.1  tron          XML_ROLE_DOCTYPE_SYSTEM_ID, even if startDoctypeDeclHandler
   3828  1.1  tron          was not set, indicating an external subset
   3829  1.1  tron       */
   3830  1.1  tron #ifdef XML_DTD
   3831  1.1  tron       if (doctypeSysid || useForeignDTD) {
   3832  1.1  tron         XML_Bool hadParamEntityRefs = dtd->hasParamEntityRefs;
   3833  1.1  tron         dtd->hasParamEntityRefs = XML_TRUE;
   3834  1.1  tron         if (paramEntityParsing && externalEntityRefHandler) {
   3835  1.1  tron           ENTITY *entity = (ENTITY *)lookup(&dtd->paramEntities,
   3836  1.1  tron                                             externalSubsetName,
   3837  1.1  tron                                             sizeof(ENTITY));
   3838  1.1  tron           if (!entity)
   3839  1.1  tron             return XML_ERROR_NO_MEMORY;
   3840  1.1  tron           if (useForeignDTD)
   3841  1.1  tron             entity->base = curBase;
   3842  1.1  tron           dtd->paramEntityRead = XML_FALSE;
   3843  1.1  tron           if (!externalEntityRefHandler(externalEntityRefHandlerArg,
   3844  1.1  tron                                         0,
   3845  1.1  tron                                         entity->base,
   3846  1.1  tron                                         entity->systemId,
   3847  1.1  tron                                         entity->publicId))
   3848  1.1  tron             return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
   3849  1.1  tron           if (dtd->paramEntityRead) {
   3850  1.1  tron             if (!dtd->standalone &&
   3851  1.1  tron                 notStandaloneHandler &&
   3852  1.1  tron                 !notStandaloneHandler(handlerArg))
   3853  1.1  tron               return XML_ERROR_NOT_STANDALONE;
   3854  1.1  tron           }
   3855  1.1  tron           /* if we didn't read the foreign DTD then this means that there
   3856  1.1  tron              is no external subset and we must reset dtd->hasParamEntityRefs
   3857  1.1  tron           */
   3858  1.1  tron           else if (!doctypeSysid)
   3859  1.1  tron             dtd->hasParamEntityRefs = hadParamEntityRefs;
   3860  1.1  tron           /* end of DTD - no need to update dtd->keepProcessing */
   3861  1.1  tron         }
   3862  1.1  tron         useForeignDTD = XML_FALSE;
   3863  1.1  tron       }
   3864  1.1  tron #endif /* XML_DTD */
   3865  1.1  tron       if (endDoctypeDeclHandler) {
   3866  1.1  tron         endDoctypeDeclHandler(handlerArg);
   3867  1.1  tron         handleDefault = XML_FALSE;
   3868  1.1  tron       }
   3869  1.1  tron       break;
   3870  1.1  tron     case XML_ROLE_INSTANCE_START:
   3871  1.1  tron #ifdef XML_DTD
   3872  1.1  tron       /* if there is no DOCTYPE declaration then now is the
   3873  1.1  tron          last chance to read the foreign DTD
   3874  1.1  tron       */
   3875  1.1  tron       if (useForeignDTD) {
   3876  1.1  tron         XML_Bool hadParamEntityRefs = dtd->hasParamEntityRefs;
   3877  1.1  tron         dtd->hasParamEntityRefs = XML_TRUE;
   3878  1.1  tron         if (paramEntityParsing && externalEntityRefHandler) {
   3879  1.1  tron           ENTITY *entity = (ENTITY *)lookup(&dtd->paramEntities,
   3880  1.1  tron                                             externalSubsetName,
   3881  1.1  tron                                             sizeof(ENTITY));
   3882  1.1  tron           if (!entity)
   3883  1.1  tron             return XML_ERROR_NO_MEMORY;
   3884  1.1  tron           entity->base = curBase;
   3885  1.1  tron           dtd->paramEntityRead = XML_FALSE;
   3886  1.1  tron           if (!externalEntityRefHandler(externalEntityRefHandlerArg,
   3887  1.1  tron                                         0,
   3888  1.1  tron                                         entity->base,
   3889  1.1  tron                                         entity->systemId,
   3890  1.1  tron                                         entity->publicId))
   3891  1.1  tron             return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
   3892  1.1  tron           if (dtd->paramEntityRead) {
   3893  1.1  tron             if (!dtd->standalone &&
   3894  1.1  tron                 notStandaloneHandler &&
   3895  1.1  tron                 !notStandaloneHandler(handlerArg))
   3896  1.1  tron               return XML_ERROR_NOT_STANDALONE;
   3897  1.1  tron           }
   3898  1.1  tron           /* if we didn't read the foreign DTD then this means that there
   3899  1.1  tron              is no external subset and we must reset dtd->hasParamEntityRefs
   3900  1.1  tron           */
   3901  1.1  tron           else
   3902  1.1  tron             dtd->hasParamEntityRefs = hadParamEntityRefs;
   3903  1.1  tron           /* end of DTD - no need to update dtd->keepProcessing */
   3904  1.1  tron         }
   3905  1.1  tron       }
   3906  1.1  tron #endif /* XML_DTD */
   3907  1.1  tron       processor = contentProcessor;
   3908  1.1  tron       return contentProcessor(parser, s, end, nextPtr);
   3909  1.1  tron     case XML_ROLE_ATTLIST_ELEMENT_NAME:
   3910  1.1  tron       declElementType = getElementType(parser, enc, s, next);
   3911  1.1  tron       if (!declElementType)
   3912  1.1  tron         return XML_ERROR_NO_MEMORY;
   3913  1.1  tron       goto checkAttListDeclHandler;
   3914  1.1  tron     case XML_ROLE_ATTRIBUTE_NAME:
   3915  1.1  tron       declAttributeId = getAttributeId(parser, enc, s, next);
   3916  1.1  tron       if (!declAttributeId)
   3917  1.1  tron         return XML_ERROR_NO_MEMORY;
   3918  1.1  tron       declAttributeIsCdata = XML_FALSE;
   3919  1.1  tron       declAttributeType = NULL;
   3920  1.1  tron       declAttributeIsId = XML_FALSE;
   3921  1.1  tron       goto checkAttListDeclHandler;
   3922  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_CDATA:
   3923  1.1  tron       declAttributeIsCdata = XML_TRUE;
   3924  1.1  tron       declAttributeType = atypeCDATA;
   3925  1.1  tron       goto checkAttListDeclHandler;
   3926  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_ID:
   3927  1.1  tron       declAttributeIsId = XML_TRUE;
   3928  1.1  tron       declAttributeType = atypeID;
   3929  1.1  tron       goto checkAttListDeclHandler;
   3930  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_IDREF:
   3931  1.1  tron       declAttributeType = atypeIDREF;
   3932  1.1  tron       goto checkAttListDeclHandler;
   3933  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_IDREFS:
   3934  1.1  tron       declAttributeType = atypeIDREFS;
   3935  1.1  tron       goto checkAttListDeclHandler;
   3936  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_ENTITY:
   3937  1.1  tron       declAttributeType = atypeENTITY;
   3938  1.1  tron       goto checkAttListDeclHandler;
   3939  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_ENTITIES:
   3940  1.1  tron       declAttributeType = atypeENTITIES;
   3941  1.1  tron       goto checkAttListDeclHandler;
   3942  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_NMTOKEN:
   3943  1.1  tron       declAttributeType = atypeNMTOKEN;
   3944  1.1  tron       goto checkAttListDeclHandler;
   3945  1.1  tron     case XML_ROLE_ATTRIBUTE_TYPE_NMTOKENS:
   3946  1.1  tron       declAttributeType = atypeNMTOKENS;
   3947  1.1  tron     checkAttListDeclHandler:
   3948  1.1  tron       if (dtd->keepProcessing && attlistDeclHandler)
   3949  1.1  tron         handleDefault = XML_FALSE;
   3950  1.1  tron       break;
   3951  1.1  tron     case XML_ROLE_ATTRIBUTE_ENUM_VALUE:
   3952  1.1  tron     case XML_ROLE_ATTRIBUTE_NOTATION_VALUE:
   3953  1.1  tron       if (dtd->keepProcessing && attlistDeclHandler) {
   3954  1.1  tron         const XML_Char *prefix;
   3955  1.1  tron         if (declAttributeType) {
   3956  1.1  tron           prefix = enumValueSep;
   3957  1.1  tron         }
   3958  1.1  tron         else {
   3959  1.1  tron           prefix = (role == XML_ROLE_ATTRIBUTE_NOTATION_VALUE
   3960  1.1  tron                     ? notationPrefix
   3961  1.1  tron                     : enumValueStart);
   3962  1.1  tron         }
   3963  1.1  tron         if (!poolAppendString(&tempPool, prefix))
   3964  1.1  tron           return XML_ERROR_NO_MEMORY;
   3965  1.1  tron         if (!poolAppend(&tempPool, enc, s, next))
   3966  1.1  tron           return XML_ERROR_NO_MEMORY;
   3967  1.1  tron         declAttributeType = tempPool.start;
   3968  1.1  tron         handleDefault = XML_FALSE;
   3969  1.1  tron       }
   3970  1.1  tron       break;
   3971  1.1  tron     case XML_ROLE_IMPLIED_ATTRIBUTE_VALUE:
   3972  1.1  tron     case XML_ROLE_REQUIRED_ATTRIBUTE_VALUE:
   3973  1.1  tron       if (dtd->keepProcessing) {
   3974  1.1  tron         if (!defineAttribute(declElementType, declAttributeId,
   3975  1.1  tron                              declAttributeIsCdata, declAttributeIsId,
   3976  1.1  tron                              0, parser))
   3977  1.1  tron           return XML_ERROR_NO_MEMORY;
   3978  1.1  tron         if (attlistDeclHandler && declAttributeType) {
   3979  1.1  tron           if (*declAttributeType == XML_T(ASCII_LPAREN)
   3980  1.1  tron               || (*declAttributeType == XML_T(ASCII_N)
   3981  1.1  tron                   && declAttributeType[1] == XML_T(ASCII_O))) {
   3982  1.1  tron             /* Enumerated or Notation type */
   3983  1.1  tron             if (!poolAppendChar(&tempPool, XML_T(ASCII_RPAREN))
   3984  1.1  tron                 || !poolAppendChar(&tempPool, XML_T('\0')))
   3985  1.1  tron               return XML_ERROR_NO_MEMORY;
   3986  1.1  tron             declAttributeType = tempPool.start;
   3987  1.1  tron             poolFinish(&tempPool);
   3988  1.1  tron           }
   3989  1.1  tron           *eventEndPP = s;
   3990  1.1  tron           attlistDeclHandler(handlerArg, declElementType->name,
   3991  1.1  tron                              declAttributeId->name, declAttributeType,
   3992  1.1  tron                              0, role == XML_ROLE_REQUIRED_ATTRIBUTE_VALUE);
   3993  1.1  tron           poolClear(&tempPool);
   3994  1.1  tron           handleDefault = XML_FALSE;
   3995  1.1  tron         }
   3996  1.1  tron       }
   3997  1.1  tron       break;
   3998  1.1  tron     case XML_ROLE_DEFAULT_ATTRIBUTE_VALUE:
   3999  1.1  tron     case XML_ROLE_FIXED_ATTRIBUTE_VALUE:
   4000  1.1  tron       if (dtd->keepProcessing) {
   4001  1.1  tron         const XML_Char *attVal;
   4002  1.1  tron         enum XML_Error result =
   4003  1.1  tron           storeAttributeValue(parser, enc, declAttributeIsCdata,
   4004  1.1  tron                               s + enc->minBytesPerChar,
   4005  1.1  tron                               next - enc->minBytesPerChar,
   4006  1.1  tron                               &dtd->pool);
   4007  1.1  tron         if (result)
   4008  1.1  tron           return result;
   4009  1.1  tron         attVal = poolStart(&dtd->pool);
   4010  1.1  tron         poolFinish(&dtd->pool);
   4011  1.1  tron         /* ID attributes aren't allowed to have a default */
   4012  1.1  tron         if (!defineAttribute(declElementType, declAttributeId,
   4013  1.1  tron                              declAttributeIsCdata, XML_FALSE, attVal, parser))
   4014  1.1  tron           return XML_ERROR_NO_MEMORY;
   4015  1.1  tron         if (attlistDeclHandler && declAttributeType) {
   4016  1.1  tron           if (*declAttributeType == XML_T(ASCII_LPAREN)
   4017  1.1  tron               || (*declAttributeType == XML_T(ASCII_N)
   4018  1.1  tron                   && declAttributeType[1] == XML_T(ASCII_O))) {
   4019  1.1  tron             /* Enumerated or Notation type */
   4020  1.1  tron             if (!poolAppendChar(&tempPool, XML_T(ASCII_RPAREN))
   4021  1.1  tron                 || !poolAppendChar(&tempPool, XML_T('\0')))
   4022  1.1  tron               return XML_ERROR_NO_MEMORY;
   4023  1.1  tron             declAttributeType = tempPool.start;
   4024  1.1  tron             poolFinish(&tempPool);
   4025  1.1  tron           }
   4026  1.1  tron           *eventEndPP = s;
   4027  1.1  tron           attlistDeclHandler(handlerArg, declElementType->name,
   4028  1.1  tron                              declAttributeId->name, declAttributeType,
   4029  1.1  tron                              attVal,
   4030  1.1  tron                              role == XML_ROLE_FIXED_ATTRIBUTE_VALUE);
   4031  1.1  tron           poolClear(&tempPool);
   4032  1.1  tron           handleDefault = XML_FALSE;
   4033  1.1  tron         }
   4034  1.1  tron       }
   4035  1.1  tron       break;
   4036  1.1  tron     case XML_ROLE_ENTITY_VALUE:
   4037  1.1  tron       if (dtd->keepProcessing) {
   4038  1.1  tron         enum XML_Error result = storeEntityValue(parser, enc,
   4039  1.1  tron                                             s + enc->minBytesPerChar,
   4040  1.1  tron                                             next - enc->minBytesPerChar);
   4041  1.1  tron         if (declEntity) {
   4042  1.1  tron           declEntity->textPtr = poolStart(&dtd->entityValuePool);
   4043  1.1  tron           declEntity->textLen = (int)(poolLength(&dtd->entityValuePool));
   4044  1.1  tron           poolFinish(&dtd->entityValuePool);
   4045  1.1  tron           if (entityDeclHandler) {
   4046  1.1  tron             *eventEndPP = s;
   4047  1.1  tron             entityDeclHandler(handlerArg,
   4048  1.1  tron                               declEntity->name,
   4049  1.1  tron                               declEntity->is_param,
   4050  1.1  tron                               declEntity->textPtr,
   4051  1.1  tron                               declEntity->textLen,
   4052  1.1  tron                               curBase, 0, 0, 0);
   4053  1.1  tron             handleDefault = XML_FALSE;
   4054  1.1  tron           }
   4055  1.1  tron         }
   4056  1.1  tron         else
   4057  1.1  tron           poolDiscard(&dtd->entityValuePool);
   4058  1.1  tron         if (result != XML_ERROR_NONE)
   4059  1.1  tron           return result;
   4060  1.1  tron       }
   4061  1.1  tron       break;
   4062  1.1  tron     case XML_ROLE_DOCTYPE_SYSTEM_ID:
   4063  1.1  tron #ifdef XML_DTD
   4064  1.1  tron       useForeignDTD = XML_FALSE;
   4065  1.1  tron #endif /* XML_DTD */
   4066  1.1  tron       dtd->hasParamEntityRefs = XML_TRUE;
   4067  1.1  tron       if (startDoctypeDeclHandler) {
   4068  1.1  tron         doctypeSysid = poolStoreString(&tempPool, enc,
   4069  1.1  tron                                        s + enc->minBytesPerChar,
   4070  1.1  tron                                        next - enc->minBytesPerChar);
   4071  1.1  tron         if (doctypeSysid == NULL)
   4072  1.1  tron           return XML_ERROR_NO_MEMORY;
   4073  1.1  tron         poolFinish(&tempPool);
   4074  1.1  tron         handleDefault = XML_FALSE;
   4075  1.1  tron       }
   4076  1.1  tron #ifdef XML_DTD
   4077  1.1  tron       else
   4078  1.1  tron         /* use externalSubsetName to make doctypeSysid non-NULL
   4079  1.1  tron            for the case where no startDoctypeDeclHandler is set */
   4080  1.1  tron         doctypeSysid = externalSubsetName;
   4081  1.1  tron #endif /* XML_DTD */
   4082  1.1  tron       if (!dtd->standalone
   4083  1.1  tron #ifdef XML_DTD
   4084  1.1  tron           && !paramEntityParsing
   4085  1.1  tron #endif /* XML_DTD */
   4086  1.1  tron           && notStandaloneHandler
   4087  1.1  tron           && !notStandaloneHandler(handlerArg))
   4088  1.1  tron         return XML_ERROR_NOT_STANDALONE;
   4089  1.1  tron #ifndef XML_DTD
   4090  1.1  tron       break;
   4091  1.1  tron #else /* XML_DTD */
   4092  1.1  tron       if (!declEntity) {
   4093  1.1  tron         declEntity = (ENTITY *)lookup(&dtd->paramEntities,
   4094  1.1  tron                                       externalSubsetName,
   4095  1.1  tron                                       sizeof(ENTITY));
   4096  1.1  tron         if (!declEntity)
   4097  1.1  tron           return XML_ERROR_NO_MEMORY;
   4098  1.1  tron         declEntity->publicId = NULL;
   4099  1.1  tron       }
   4100  1.1  tron       /* fall through */
   4101  1.1  tron #endif /* XML_DTD */
   4102  1.1  tron     case XML_ROLE_ENTITY_SYSTEM_ID:
   4103  1.1  tron       if (dtd->keepProcessing && declEntity) {
   4104  1.1  tron         declEntity->systemId = poolStoreString(&dtd->pool, enc,
   4105  1.1  tron                                                s + enc->minBytesPerChar,
   4106  1.1  tron                                                next - enc->minBytesPerChar);
   4107  1.1  tron         if (!declEntity->systemId)
   4108  1.1  tron           return XML_ERROR_NO_MEMORY;
   4109  1.1  tron         declEntity->base = curBase;
   4110  1.1  tron         poolFinish(&dtd->pool);
   4111  1.1  tron         if (entityDeclHandler)
   4112  1.1  tron           handleDefault = XML_FALSE;
   4113  1.1  tron       }
   4114  1.1  tron       break;
   4115  1.1  tron     case XML_ROLE_ENTITY_COMPLETE:
   4116  1.1  tron       if (dtd->keepProcessing && declEntity && entityDeclHandler) {
   4117  1.1  tron         *eventEndPP = s;
   4118  1.1  tron         entityDeclHandler(handlerArg,
   4119  1.1  tron                           declEntity->name,
   4120  1.1  tron                           declEntity->is_param,
   4121  1.1  tron                           0,0,
   4122  1.1  tron                           declEntity->base,
   4123  1.1  tron                           declEntity->systemId,
   4124  1.1  tron                           declEntity->publicId,
   4125  1.1  tron                           0);
   4126  1.1  tron         handleDefault = XML_FALSE;
   4127  1.1  tron       }
   4128  1.1  tron       break;
   4129  1.1  tron     case XML_ROLE_ENTITY_NOTATION_NAME:
   4130  1.1  tron       if (dtd->keepProcessing && declEntity) {
   4131  1.1  tron         declEntity->notation = poolStoreString(&dtd->pool, enc, s, next);
   4132  1.1  tron         if (!declEntity->notation)
   4133  1.1  tron           return XML_ERROR_NO_MEMORY;
   4134  1.1  tron         poolFinish(&dtd->pool);
   4135  1.1  tron         if (unparsedEntityDeclHandler) {
   4136  1.1  tron           *eventEndPP = s;
   4137  1.1  tron           unparsedEntityDeclHandler(handlerArg,
   4138  1.1  tron                                     declEntity->name,
   4139  1.1  tron                                     declEntity->base,
   4140  1.1  tron                                     declEntity->systemId,
   4141  1.1  tron                                     declEntity->publicId,
   4142  1.1  tron                                     declEntity->notation);
   4143  1.1  tron           handleDefault = XML_FALSE;
   4144  1.1  tron         }
   4145  1.1  tron         else if (entityDeclHandler) {
   4146  1.1  tron           *eventEndPP = s;
   4147  1.1  tron           entityDeclHandler(handlerArg,
   4148  1.1  tron                             declEntity->name,
   4149  1.1  tron                             0,0,0,
   4150  1.1  tron                             declEntity->base,
   4151  1.1  tron                             declEntity->systemId,
   4152  1.1  tron                             declEntity->publicId,
   4153  1.1  tron                             declEntity->notation);
   4154  1.1  tron           handleDefault = XML_FALSE;
   4155  1.1  tron         }
   4156  1.1  tron       }
   4157  1.1  tron       break;
   4158  1.1  tron     case XML_ROLE_GENERAL_ENTITY_NAME:
   4159  1.1  tron       {
   4160  1.1  tron         if (XmlPredefinedEntityName(enc, s, next)) {
   4161  1.1  tron           declEntity = NULL;
   4162  1.1  tron           break;
   4163  1.1  tron         }
   4164  1.1  tron         if (dtd->keepProcessing) {
   4165  1.1  tron           const XML_Char *name = poolStoreString(&dtd->pool, enc, s, next);
   4166  1.1  tron           if (!name)
   4167  1.1  tron             return XML_ERROR_NO_MEMORY;
   4168  1.1  tron           declEntity = (ENTITY *)lookup(&dtd->generalEntities, name,
   4169  1.1  tron                                         sizeof(ENTITY));
   4170  1.1  tron           if (!declEntity)
   4171  1.1  tron             return XML_ERROR_NO_MEMORY;
   4172  1.1  tron           if (declEntity->name != name) {
   4173  1.1  tron             poolDiscard(&dtd->pool);
   4174  1.1  tron             declEntity = NULL;
   4175  1.1  tron           }
   4176  1.1  tron           else {
   4177  1.1  tron             poolFinish(&dtd->pool);
   4178  1.1  tron             declEntity->publicId = NULL;
   4179  1.1  tron             declEntity->is_param = XML_FALSE;
   4180  1.1  tron             /* if we have a parent parser or are reading an internal parameter
   4181  1.1  tron                entity, then the entity declaration is not considered "internal"
   4182  1.1  tron             */
   4183  1.1  tron             declEntity->is_internal = !(parentParser || openInternalEntities);
   4184  1.1  tron             if (entityDeclHandler)
   4185  1.1  tron               handleDefault = XML_FALSE;
   4186  1.1  tron           }
   4187  1.1  tron         }
   4188  1.1  tron         else {
   4189  1.1  tron           poolDiscard(&dtd->pool);
   4190  1.1  tron           declEntity = NULL;
   4191  1.1  tron         }
   4192  1.1  tron       }
   4193  1.1  tron       break;
   4194  1.1  tron     case XML_ROLE_PARAM_ENTITY_NAME:
   4195  1.1  tron #ifdef XML_DTD
   4196  1.1  tron       if (dtd->keepProcessing) {
   4197  1.1  tron         const XML_Char *name = poolStoreString(&dtd->pool, enc, s, next);
   4198  1.1  tron         if (!name)
   4199  1.1  tron           return XML_ERROR_NO_MEMORY;
   4200  1.1  tron         declEntity = (ENTITY *)lookup(&dtd->paramEntities,
   4201  1.1  tron                                            name, sizeof(ENTITY));
   4202  1.1  tron         if (!declEntity)
   4203  1.1  tron           return XML_ERROR_NO_MEMORY;
   4204  1.1  tron         if (declEntity->name != name) {
   4205  1.1  tron           poolDiscard(&dtd->pool);
   4206  1.1  tron           declEntity = NULL;
   4207  1.1  tron         }
   4208  1.1  tron         else {
   4209  1.1  tron           poolFinish(&dtd->pool);
   4210  1.1  tron           declEntity->publicId = NULL;
   4211  1.1  tron           declEntity->is_param = XML_TRUE;
   4212  1.1  tron           /* if we have a parent parser or are reading an internal parameter
   4213  1.1  tron              entity, then the entity declaration is not considered "internal"
   4214  1.1  tron           */
   4215  1.1  tron           declEntity->is_internal = !(parentParser || openInternalEntities);
   4216  1.1  tron           if (entityDeclHandler)
   4217  1.1  tron             handleDefault = XML_FALSE;
   4218  1.1  tron         }
   4219  1.1  tron       }
   4220  1.1  tron       else {
   4221  1.1  tron         poolDiscard(&dtd->pool);
   4222  1.1  tron         declEntity = NULL;
   4223  1.1  tron       }
   4224  1.1  tron #else /* not XML_DTD */
   4225  1.1  tron       declEntity = NULL;
   4226  1.1  tron #endif /* XML_DTD */
   4227  1.1  tron       break;
   4228  1.1  tron     case XML_ROLE_NOTATION_NAME:
   4229  1.1  tron       declNotationPublicId = NULL;
   4230  1.1  tron       declNotationName = NULL;
   4231  1.1  tron       if (notationDeclHandler) {
   4232  1.1  tron         declNotationName = poolStoreString(&tempPool, enc, s, next);
   4233  1.1  tron         if (!declNotationName)
   4234  1.1  tron           return XML_ERROR_NO_MEMORY;
   4235  1.1  tron         poolFinish(&tempPool);
   4236  1.1  tron         handleDefault = XML_FALSE;
   4237  1.1  tron       }
   4238  1.1  tron       break;
   4239  1.1  tron     case XML_ROLE_NOTATION_PUBLIC_ID:
   4240  1.1  tron       if (!XmlIsPublicId(enc, s, next, eventPP))
   4241  1.1  tron         return XML_ERROR_PUBLICID;
   4242  1.1  tron       if (declNotationName) {  /* means notationDeclHandler != NULL */
   4243  1.1  tron         XML_Char *tem = poolStoreString(&tempPool,
   4244  1.1  tron                                         enc,
   4245  1.1  tron                                         s + enc->minBytesPerChar,
   4246  1.1  tron                                         next - enc->minBytesPerChar);
   4247  1.1  tron         if (!tem)
   4248  1.1  tron           return XML_ERROR_NO_MEMORY;
   4249  1.1  tron         normalizePublicId(tem);
   4250  1.1  tron         declNotationPublicId = tem;
   4251  1.1  tron         poolFinish(&tempPool);
   4252  1.1  tron         handleDefault = XML_FALSE;
   4253  1.1  tron       }
   4254  1.1  tron       break;
   4255  1.1  tron     case XML_ROLE_NOTATION_SYSTEM_ID:
   4256  1.1  tron       if (declNotationName && notationDeclHandler) {
   4257  1.1  tron         const XML_Char *systemId
   4258  1.1  tron           = poolStoreString(&tempPool, enc,
   4259  1.1  tron                             s + enc->minBytesPerChar,
   4260  1.1  tron                             next - enc->minBytesPerChar);
   4261  1.1  tron         if (!systemId)
   4262  1.1  tron           return XML_ERROR_NO_MEMORY;
   4263  1.1  tron         *eventEndPP = s;
   4264  1.1  tron         notationDeclHandler(handlerArg,
   4265  1.1  tron                             declNotationName,
   4266  1.1  tron                             curBase,
   4267  1.1  tron                             systemId,
   4268  1.1  tron                             declNotationPublicId);
   4269  1.1  tron         handleDefault = XML_FALSE;
   4270  1.1  tron       }
   4271  1.1  tron       poolClear(&tempPool);
   4272  1.1  tron       break;
   4273  1.1  tron     case XML_ROLE_NOTATION_NO_SYSTEM_ID:
   4274  1.1  tron       if (declNotationPublicId && notationDeclHandler) {
   4275  1.1  tron         *eventEndPP = s;
   4276  1.1  tron         notationDeclHandler(handlerArg,
   4277  1.1  tron                             declNotationName,
   4278  1.1  tron                             curBase,
   4279  1.1  tron                             0,
   4280  1.1  tron                             declNotationPublicId);
   4281  1.1  tron         handleDefault = XML_FALSE;
   4282  1.1  tron       }
   4283  1.1  tron       poolClear(&tempPool);
   4284  1.1  tron       break;
   4285  1.1  tron     case XML_ROLE_ERROR:
   4286  1.1  tron       switch (tok) {
   4287  1.1  tron       case XML_TOK_PARAM_ENTITY_REF:
   4288  1.1  tron         /* PE references in internal subset are
   4289  1.1  tron            not allowed within declarations. */
   4290  1.1  tron         return XML_ERROR_PARAM_ENTITY_REF;
   4291  1.1  tron       case XML_TOK_XML_DECL:
   4292  1.1  tron         return XML_ERROR_MISPLACED_XML_PI;
   4293  1.1  tron       default:
   4294  1.1  tron         return XML_ERROR_SYNTAX;
   4295  1.1  tron       }
   4296  1.1  tron #ifdef XML_DTD
   4297  1.1  tron     case XML_ROLE_IGNORE_SECT:
   4298  1.1  tron       {
   4299  1.1  tron         enum XML_Error result;
   4300  1.1  tron         if (defaultHandler)
   4301  1.1  tron           reportDefault(parser, enc, s, next);
   4302  1.1  tron         handleDefault = XML_FALSE;
   4303  1.1  tron         result = doIgnoreSection(parser, enc, &next, end, nextPtr, haveMore);
   4304  1.1  tron         if (result != XML_ERROR_NONE)
   4305  1.1  tron           return result;
   4306  1.1  tron         else if (!next) {
   4307  1.1  tron           processor = ignoreSectionProcessor;
   4308  1.1  tron           return result;
   4309  1.1  tron         }
   4310  1.1  tron       }
   4311  1.1  tron       break;
   4312  1.1  tron #endif /* XML_DTD */
   4313  1.1  tron     case XML_ROLE_GROUP_OPEN:
   4314  1.1  tron       if (prologState.level >= groupSize) {
   4315  1.1  tron         if (groupSize) {
   4316  1.1  tron           char *temp = (char *)REALLOC(groupConnector, groupSize *= 2);
   4317  1.1  tron           if (temp == NULL)
   4318  1.1  tron             return XML_ERROR_NO_MEMORY;
   4319  1.1  tron           groupConnector = temp;
   4320  1.1  tron           if (dtd->scaffIndex) {
   4321  1.1  tron             int *temp = (int *)REALLOC(dtd->scaffIndex,
   4322  1.1  tron                           groupSize * sizeof(int));
   4323  1.1  tron             if (temp == NULL)
   4324  1.1  tron               return XML_ERROR_NO_MEMORY;
   4325  1.1  tron             dtd->scaffIndex = temp;
   4326  1.1  tron           }
   4327  1.1  tron         }
   4328  1.1  tron         else {
   4329  1.1  tron           groupConnector = (char *)MALLOC(groupSize = 32);
   4330  1.1  tron           if (!groupConnector)
   4331  1.1  tron             return XML_ERROR_NO_MEMORY;
   4332  1.1  tron         }
   4333  1.1  tron       }
   4334  1.1  tron       groupConnector[prologState.level] = 0;
   4335  1.1  tron       if (dtd->in_eldecl) {
   4336  1.1  tron         int myindex = nextScaffoldPart(parser);
   4337  1.1  tron         if (myindex < 0)
   4338  1.1  tron           return XML_ERROR_NO_MEMORY;
   4339  1.1  tron         dtd->scaffIndex[dtd->scaffLevel] = myindex;
   4340  1.1  tron         dtd->scaffLevel++;
   4341  1.1  tron         dtd->scaffold[myindex].type = XML_CTYPE_SEQ;
   4342  1.1  tron         if (elementDeclHandler)
   4343  1.1  tron           handleDefault = XML_FALSE;
   4344  1.1  tron       }
   4345  1.1  tron       break;
   4346  1.1  tron     case XML_ROLE_GROUP_SEQUENCE:
   4347  1.1  tron       if (groupConnector[prologState.level] == ASCII_PIPE)
   4348  1.1  tron         return XML_ERROR_SYNTAX;
   4349  1.1  tron       groupConnector[prologState.level] = ASCII_COMMA;
   4350  1.1  tron       if (dtd->in_eldecl && elementDeclHandler)
   4351  1.1  tron         handleDefault = XML_FALSE;
   4352  1.1  tron       break;
   4353  1.1  tron     case XML_ROLE_GROUP_CHOICE:
   4354  1.1  tron       if (groupConnector[prologState.level] == ASCII_COMMA)
   4355  1.1  tron         return XML_ERROR_SYNTAX;
   4356  1.1  tron       if (dtd->in_eldecl
   4357  1.1  tron           && !groupConnector[prologState.level]
   4358  1.1  tron           && (dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
   4359  1.1  tron               != XML_CTYPE_MIXED)
   4360  1.1  tron           ) {
   4361  1.1  tron         dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
   4362  1.1  tron             = XML_CTYPE_CHOICE;
   4363  1.1  tron         if (elementDeclHandler)
   4364  1.1  tron           handleDefault = XML_FALSE;
   4365  1.1  tron       }
   4366  1.1  tron       groupConnector[prologState.level] = ASCII_PIPE;
   4367  1.1  tron       break;
   4368  1.1  tron     case XML_ROLE_PARAM_ENTITY_REF:
   4369  1.1  tron #ifdef XML_DTD
   4370  1.1  tron     case XML_ROLE_INNER_PARAM_ENTITY_REF:
   4371  1.1  tron       dtd->hasParamEntityRefs = XML_TRUE;
   4372  1.1  tron       if (!paramEntityParsing)
   4373  1.1  tron         dtd->keepProcessing = dtd->standalone;
   4374  1.1  tron       else {
   4375  1.1  tron         const XML_Char *name;
   4376  1.1  tron         ENTITY *entity;
   4377  1.1  tron         name = poolStoreString(&dtd->pool, enc,
   4378  1.1  tron                                 s + enc->minBytesPerChar,
   4379  1.1  tron                                 next - enc->minBytesPerChar);
   4380  1.1  tron         if (!name)
   4381  1.1  tron           return XML_ERROR_NO_MEMORY;
   4382  1.1  tron         entity = (ENTITY *)lookup(&dtd->paramEntities, name, 0);
   4383  1.1  tron         poolDiscard(&dtd->pool);
   4384  1.1  tron         /* first, determine if a check for an existing declaration is needed;
   4385  1.1  tron            if yes, check that the entity exists, and that it is internal,
   4386  1.1  tron            otherwise call the skipped entity handler
   4387  1.1  tron         */
   4388  1.1  tron         if (prologState.documentEntity &&
   4389  1.1  tron             (dtd->standalone
   4390  1.1  tron              ? !openInternalEntities
   4391  1.1  tron              : !dtd->hasParamEntityRefs)) {
   4392  1.1  tron           if (!entity)
   4393  1.1  tron             return XML_ERROR_UNDEFINED_ENTITY;
   4394  1.1  tron           else if (!entity->is_internal)
   4395  1.1  tron             return XML_ERROR_ENTITY_DECLARED_IN_PE;
   4396  1.1  tron         }
   4397  1.1  tron         else if (!entity) {
   4398  1.1  tron           dtd->keepProcessing = dtd->standalone;
   4399  1.1  tron           /* cannot report skipped entities in declarations */
   4400  1.1  tron           if ((role == XML_ROLE_PARAM_ENTITY_REF) && skippedEntityHandler) {
   4401  1.1  tron             skippedEntityHandler(handlerArg, name, 1);
   4402  1.1  tron             handleDefault = XML_FALSE;
   4403  1.1  tron           }
   4404  1.1  tron           break;
   4405  1.1  tron         }
   4406  1.1  tron         if (entity->open)
   4407  1.1  tron           return XML_ERROR_RECURSIVE_ENTITY_REF;
   4408  1.1  tron         if (entity->textPtr) {
   4409  1.1  tron           enum XML_Error result;
   4410  1.1  tron           XML_Bool betweenDecl =
   4411  1.1  tron             (role == XML_ROLE_PARAM_ENTITY_REF ? XML_TRUE : XML_FALSE);
   4412  1.1  tron           result = processInternalEntity(parser, entity, betweenDecl);
   4413  1.1  tron           if (result != XML_ERROR_NONE)
   4414  1.1  tron             return result;
   4415  1.1  tron           handleDefault = XML_FALSE;
   4416  1.1  tron           break;
   4417  1.1  tron         }
   4418  1.1  tron         if (externalEntityRefHandler) {
   4419  1.1  tron           dtd->paramEntityRead = XML_FALSE;
   4420  1.1  tron           entity->open = XML_TRUE;
   4421  1.1  tron           if (!externalEntityRefHandler(externalEntityRefHandlerArg,
   4422  1.1  tron                                         0,
   4423  1.1  tron                                         entity->base,
   4424  1.1  tron                                         entity->systemId,
   4425  1.1  tron                                         entity->publicId)) {
   4426  1.1  tron             entity->open = XML_FALSE;
   4427  1.1  tron             return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
   4428  1.1  tron           }
   4429  1.1  tron           entity->open = XML_FALSE;
   4430  1.1  tron           handleDefault = XML_FALSE;
   4431  1.1  tron           if (!dtd->paramEntityRead) {
   4432  1.1  tron             dtd->keepProcessing = dtd->standalone;
   4433  1.1  tron             break;
   4434  1.1  tron           }
   4435  1.1  tron         }
   4436  1.1  tron         else {
   4437  1.1  tron           dtd->keepProcessing = dtd->standalone;
   4438  1.1  tron           break;
   4439  1.1  tron         }
   4440  1.1  tron       }
   4441  1.1  tron #endif /* XML_DTD */
   4442  1.1  tron       if (!dtd->standalone &&
   4443  1.1  tron           notStandaloneHandler &&
   4444  1.1  tron           !notStandaloneHandler(handlerArg))
   4445  1.1  tron         return XML_ERROR_NOT_STANDALONE;
   4446  1.1  tron       break;
   4447  1.1  tron 
   4448  1.1  tron     /* Element declaration stuff */
   4449  1.1  tron 
   4450  1.1  tron     case XML_ROLE_ELEMENT_NAME:
   4451  1.1  tron       if (elementDeclHandler) {
   4452  1.1  tron         declElementType = getElementType(parser, enc, s, next);
   4453  1.1  tron         if (!declElementType)
   4454  1.1  tron           return XML_ERROR_NO_MEMORY;
   4455  1.1  tron         dtd->scaffLevel = 0;
   4456  1.1  tron         dtd->scaffCount = 0;
   4457  1.1  tron         dtd->in_eldecl = XML_TRUE;
   4458  1.1  tron         handleDefault = XML_FALSE;
   4459  1.1  tron       }
   4460  1.1  tron       break;
   4461  1.1  tron 
   4462  1.1  tron     case XML_ROLE_CONTENT_ANY:
   4463  1.1  tron     case XML_ROLE_CONTENT_EMPTY:
   4464  1.1  tron       if (dtd->in_eldecl) {
   4465  1.1  tron         if (elementDeclHandler) {
   4466  1.1  tron           XML_Content * content = (XML_Content *) MALLOC(sizeof(XML_Content));
   4467  1.1  tron           if (!content)
   4468  1.1  tron             return XML_ERROR_NO_MEMORY;
   4469  1.1  tron           content->quant = XML_CQUANT_NONE;
   4470  1.1  tron           content->name = NULL;
   4471  1.1  tron           content->numchildren = 0;
   4472  1.1  tron           content->children = NULL;
   4473  1.1  tron           content->type = ((role == XML_ROLE_CONTENT_ANY) ?
   4474  1.1  tron                            XML_CTYPE_ANY :
   4475  1.1  tron                            XML_CTYPE_EMPTY);
   4476  1.1  tron           *eventEndPP = s;
   4477  1.1  tron           elementDeclHandler(handlerArg, declElementType->name, content);
   4478  1.1  tron           handleDefault = XML_FALSE;
   4479  1.1  tron         }
   4480  1.1  tron         dtd->in_eldecl = XML_FALSE;
   4481  1.1  tron       }
   4482  1.1  tron       break;
   4483  1.1  tron 
   4484  1.1  tron     case XML_ROLE_CONTENT_PCDATA:
   4485  1.1  tron       if (dtd->in_eldecl) {
   4486  1.1  tron         dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
   4487  1.1  tron             = XML_CTYPE_MIXED;
   4488  1.1  tron         if (elementDeclHandler)
   4489  1.1  tron           handleDefault = XML_FALSE;
   4490  1.1  tron       }
   4491  1.1  tron       break;
   4492  1.1  tron 
   4493  1.1  tron     case XML_ROLE_CONTENT_ELEMENT:
   4494  1.1  tron       quant = XML_CQUANT_NONE;
   4495  1.1  tron       goto elementContent;
   4496  1.1  tron     case XML_ROLE_CONTENT_ELEMENT_OPT:
   4497  1.1  tron       quant = XML_CQUANT_OPT;
   4498  1.1  tron       goto elementContent;
   4499  1.1  tron     case XML_ROLE_CONTENT_ELEMENT_REP:
   4500  1.1  tron       quant = XML_CQUANT_REP;
   4501  1.1  tron       goto elementContent;
   4502  1.1  tron     case XML_ROLE_CONTENT_ELEMENT_PLUS:
   4503  1.1  tron       quant = XML_CQUANT_PLUS;
   4504  1.1  tron     elementContent:
   4505  1.1  tron       if (dtd->in_eldecl) {
   4506  1.1  tron         ELEMENT_TYPE *el;
   4507  1.1  tron         const XML_Char *name;
   4508  1.1  tron         int nameLen;
   4509  1.1  tron         const char *nxt = (quant == XML_CQUANT_NONE
   4510  1.1  tron                            ? next
   4511  1.1  tron                            : next - enc->minBytesPerChar);
   4512  1.1  tron         int myindex = nextScaffoldPart(parser);
   4513  1.1  tron         if (myindex < 0)
   4514  1.1  tron           return XML_ERROR_NO_MEMORY;
   4515  1.1  tron         dtd->scaffold[myindex].type = XML_CTYPE_NAME;
   4516  1.1  tron         dtd->scaffold[myindex].quant = quant;
   4517  1.1  tron         el = getElementType(parser, enc, s, nxt);
   4518  1.1  tron         if (!el)
   4519  1.1  tron           return XML_ERROR_NO_MEMORY;
   4520  1.1  tron         name = el->name;
   4521  1.1  tron         dtd->scaffold[myindex].name = name;
   4522  1.1  tron         nameLen = 0;
   4523  1.1  tron         for (; name[nameLen++]; );
   4524  1.1  tron         dtd->contentStringLen +=  nameLen;
   4525  1.1  tron         if (elementDeclHandler)
   4526  1.1  tron           handleDefault = XML_FALSE;
   4527  1.1  tron       }
   4528  1.1  tron       break;
   4529  1.1  tron 
   4530  1.1  tron     case XML_ROLE_GROUP_CLOSE:
   4531  1.1  tron       quant = XML_CQUANT_NONE;
   4532  1.1  tron       goto closeGroup;
   4533  1.1  tron     case XML_ROLE_GROUP_CLOSE_OPT:
   4534  1.1  tron       quant = XML_CQUANT_OPT;
   4535  1.1  tron       goto closeGroup;
   4536  1.1  tron     case XML_ROLE_GROUP_CLOSE_REP:
   4537  1.1  tron       quant = XML_CQUANT_REP;
   4538  1.1  tron       goto closeGroup;
   4539  1.1  tron     case XML_ROLE_GROUP_CLOSE_PLUS:
   4540  1.1  tron       quant = XML_CQUANT_PLUS;
   4541  1.1  tron     closeGroup:
   4542  1.1  tron       if (dtd->in_eldecl) {
   4543  1.1  tron         if (elementDeclHandler)
   4544  1.1  tron           handleDefault = XML_FALSE;
   4545  1.1  tron         dtd->scaffLevel--;
   4546  1.1  tron         dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel]].quant = quant;
   4547  1.1  tron         if (dtd->scaffLevel == 0) {
   4548  1.1  tron           if (!handleDefault) {
   4549  1.1  tron             XML_Content *model = build_model(parser);
   4550  1.1  tron             if (!model)
   4551  1.1  tron               return XML_ERROR_NO_MEMORY;
   4552  1.1  tron             *eventEndPP = s;
   4553  1.1  tron             elementDeclHandler(handlerArg, declElementType->name, model);
   4554  1.1  tron           }
   4555  1.1  tron           dtd->in_eldecl = XML_FALSE;
   4556  1.1  tron           dtd->contentStringLen = 0;
   4557  1.1  tron         }
   4558  1.1  tron       }
   4559  1.1  tron       break;
   4560  1.1  tron       /* End element declaration stuff */
   4561  1.1  tron 
   4562  1.1  tron     case XML_ROLE_PI:
   4563  1.1  tron       if (!reportProcessingInstruction(parser, enc, s, next))
   4564  1.1  tron         return XML_ERROR_NO_MEMORY;
   4565  1.1  tron       handleDefault = XML_FALSE;
   4566  1.1  tron       break;
   4567  1.1  tron     case XML_ROLE_COMMENT:
   4568  1.1  tron       if (!reportComment(parser, enc, s, next))
   4569  1.1  tron         return XML_ERROR_NO_MEMORY;
   4570  1.1  tron       handleDefault = XML_FALSE;
   4571  1.1  tron       break;
   4572  1.1  tron     case XML_ROLE_NONE:
   4573  1.1  tron       switch (tok) {
   4574  1.1  tron       case XML_TOK_BOM:
   4575  1.1  tron         handleDefault = XML_FALSE;
   4576  1.1  tron         break;
   4577  1.1  tron       }
   4578  1.1  tron       break;
   4579  1.1  tron     case XML_ROLE_DOCTYPE_NONE:
   4580  1.1  tron       if (startDoctypeDeclHandler)
   4581  1.1  tron         handleDefault = XML_FALSE;
   4582  1.1  tron       break;
   4583  1.1  tron     case XML_ROLE_ENTITY_NONE:
   4584  1.1  tron       if (dtd->keepProcessing && entityDeclHandler)
   4585  1.1  tron         handleDefault = XML_FALSE;
   4586  1.1  tron       break;
   4587  1.1  tron     case XML_ROLE_NOTATION_NONE:
   4588  1.1  tron       if (notationDeclHandler)
   4589  1.1  tron         handleDefault = XML_FALSE;
   4590  1.1  tron       break;
   4591  1.1  tron     case XML_ROLE_ATTLIST_NONE:
   4592  1.1  tron       if (dtd->keepProcessing && attlistDeclHandler)
   4593  1.1  tron         handleDefault = XML_FALSE;
   4594  1.1  tron       break;
   4595  1.1  tron     case XML_ROLE_ELEMENT_NONE:
   4596  1.1  tron       if (elementDeclHandler)
   4597  1.1  tron         handleDefault = XML_FALSE;
   4598  1.1  tron       break;
   4599  1.1  tron     } /* end of big switch */
   4600  1.1  tron 
   4601  1.1  tron     if (handleDefault && defaultHandler)
   4602  1.1  tron       reportDefault(parser, enc, s, next);
   4603  1.1  tron 
   4604  1.1  tron     switch (ps_parsing) {
   4605  1.1  tron     case XML_SUSPENDED:
   4606  1.1  tron       *nextPtr = next;
   4607  1.1  tron       return XML_ERROR_NONE;
   4608  1.1  tron     case XML_FINISHED:
   4609  1.1  tron       return XML_ERROR_ABORTED;
   4610  1.1  tron     default:
   4611  1.1  tron       s = next;
   4612  1.1  tron       tok = XmlPrologTok(enc, s, end, &next);
   4613  1.1  tron     }
   4614  1.1  tron   }
   4615  1.1  tron   /* not reached */
   4616  1.1  tron }
   4617  1.1  tron 
   4618  1.1  tron static enum XML_Error PTRCALL
   4619  1.1  tron epilogProcessor(XML_Parser parser,
   4620  1.1  tron                 const char *s,
   4621  1.1  tron                 const char *end,
   4622  1.1  tron                 const char **nextPtr)
   4623  1.1  tron {
   4624  1.1  tron   processor = epilogProcessor;
   4625  1.1  tron   eventPtr = s;
   4626  1.1  tron   for (;;) {
   4627  1.1  tron     const char *next = NULL;
   4628  1.1  tron     int tok = XmlPrologTok(encoding, s, end, &next);
   4629  1.1  tron     eventEndPtr = next;
   4630  1.1  tron     switch (tok) {
   4631  1.1  tron     /* report partial linebreak - it might be the last token */
   4632  1.1  tron     case -XML_TOK_PROLOG_S:
   4633  1.1  tron       if (defaultHandler) {
   4634  1.1  tron         reportDefault(parser, encoding, s, next);
   4635  1.1  tron         if (ps_parsing == XML_FINISHED)
   4636  1.1  tron           return XML_ERROR_ABORTED;
   4637  1.1  tron       }
   4638  1.1  tron       *nextPtr = next;
   4639  1.1  tron       return XML_ERROR_NONE;
   4640  1.1  tron     case XML_TOK_NONE:
   4641  1.1  tron       *nextPtr = s;
   4642  1.1  tron       return XML_ERROR_NONE;
   4643  1.1  tron     case XML_TOK_PROLOG_S:
   4644  1.1  tron       if (defaultHandler)
   4645  1.1  tron         reportDefault(parser, encoding, s, next);
   4646  1.1  tron       break;
   4647  1.1  tron     case XML_TOK_PI:
   4648  1.1  tron       if (!reportProcessingInstruction(parser, encoding, s, next))
   4649  1.1  tron         return XML_ERROR_NO_MEMORY;
   4650  1.1  tron       break;
   4651  1.1  tron     case XML_TOK_COMMENT:
   4652  1.1  tron       if (!reportComment(parser, encoding, s, next))
   4653  1.1  tron         return XML_ERROR_NO_MEMORY;
   4654  1.1  tron       break;
   4655  1.1  tron     case XML_TOK_INVALID:
   4656  1.1  tron       eventPtr = next;
   4657  1.1  tron       return XML_ERROR_INVALID_TOKEN;
   4658  1.1  tron     case XML_TOK_PARTIAL:
   4659  1.1  tron       if (!ps_finalBuffer) {
   4660  1.1  tron         *nextPtr = s;
   4661  1.1  tron         return XML_ERROR_NONE;
   4662  1.1  tron       }
   4663  1.1  tron       return XML_ERROR_UNCLOSED_TOKEN;
   4664  1.1  tron     case XML_TOK_PARTIAL_CHAR:
   4665  1.1  tron       if (!ps_finalBuffer) {
   4666  1.1  tron         *nextPtr = s;
   4667  1.1  tron         return XML_ERROR_NONE;
   4668  1.1  tron       }
   4669  1.1  tron       return XML_ERROR_PARTIAL_CHAR;
   4670  1.1  tron     default:
   4671  1.1  tron       return XML_ERROR_JUNK_AFTER_DOC_ELEMENT;
   4672  1.1  tron     }
   4673  1.1  tron     eventPtr = s = next;
   4674  1.1  tron     switch (ps_parsing) {
   4675  1.1  tron     case XML_SUSPENDED:
   4676  1.1  tron       *nextPtr = next;
   4677  1.1  tron       return XML_ERROR_NONE;
   4678  1.1  tron     case XML_FINISHED:
   4679  1.1  tron       return XML_ERROR_ABORTED;
   4680  1.1  tron     default: ;
   4681  1.1  tron     }
   4682  1.1  tron   }
   4683  1.1  tron }
   4684  1.1  tron 
   4685  1.1  tron static enum XML_Error
   4686  1.1  tron processInternalEntity(XML_Parser parser, ENTITY *entity,
   4687  1.1  tron                       XML_Bool betweenDecl)
   4688  1.1  tron {
   4689  1.1  tron   const char *textStart, *textEnd;
   4690  1.1  tron   const char *next;
   4691  1.1  tron   enum XML_Error result;
   4692  1.1  tron   OPEN_INTERNAL_ENTITY *openEntity;
   4693  1.1  tron 
   4694  1.1  tron   if (freeInternalEntities) {
   4695  1.1  tron     openEntity = freeInternalEntities;
   4696  1.1  tron     freeInternalEntities = openEntity->next;
   4697  1.1  tron   }
   4698  1.1  tron   else {
   4699  1.1  tron     openEntity = (OPEN_INTERNAL_ENTITY *)MALLOC(sizeof(OPEN_INTERNAL_ENTITY));
   4700  1.1  tron     if (!openEntity)
   4701  1.1  tron       return XML_ERROR_NO_MEMORY;
   4702  1.1  tron   }
   4703  1.1  tron   entity->open = XML_TRUE;
   4704  1.1  tron   entity->processed = 0;
   4705  1.1  tron   openEntity->next = openInternalEntities;
   4706  1.1  tron   openInternalEntities = openEntity;
   4707  1.1  tron   openEntity->entity = entity;
   4708  1.1  tron   openEntity->startTagLevel = tagLevel;
   4709  1.1  tron   openEntity->betweenDecl = betweenDecl;
   4710  1.1  tron   openEntity->internalEventPtr = NULL;
   4711  1.1  tron   openEntity->internalEventEndPtr = NULL;
   4712  1.1  tron   textStart = (char *)entity->textPtr;
   4713  1.1  tron   textEnd = (char *)(entity->textPtr + entity->textLen);
   4714  1.1  tron 
   4715  1.1  tron #ifdef XML_DTD
   4716  1.1  tron   if (entity->is_param) {
   4717  1.1  tron     int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next);
   4718  1.1  tron     result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
   4719  1.1  tron                       next, &next, XML_FALSE);
   4720  1.1  tron   }
   4721  1.1  tron   else
   4722  1.1  tron #endif /* XML_DTD */
   4723  1.1  tron     result = doContent(parser, tagLevel, internalEncoding, textStart,
   4724  1.1  tron                        textEnd, &next, XML_FALSE);
   4725  1.1  tron 
   4726  1.1  tron   if (result == XML_ERROR_NONE) {
   4727  1.1  tron     if (textEnd != next && ps_parsing == XML_SUSPENDED) {
   4728  1.1  tron       entity->processed = (int)(next - textStart);
   4729  1.1  tron       processor = internalEntityProcessor;
   4730  1.1  tron     }
   4731  1.1  tron     else {
   4732  1.1  tron       entity->open = XML_FALSE;
   4733  1.1  tron       openInternalEntities = openEntity->next;
   4734  1.1  tron       /* put openEntity back in list of free instances */
   4735  1.1  tron       openEntity->next = freeInternalEntities;
   4736  1.1  tron       freeInternalEntities = openEntity;
   4737  1.1  tron     }
   4738  1.1  tron   }
   4739  1.1  tron   return result;
   4740  1.1  tron }
   4741  1.1  tron 
   4742  1.1  tron static enum XML_Error PTRCALL
   4743  1.1  tron internalEntityProcessor(XML_Parser parser,
   4744  1.1  tron                         const char *s,
   4745  1.1  tron                         const char *end,
   4746  1.1  tron                         const char **nextPtr)
   4747  1.1  tron {
   4748  1.1  tron   ENTITY *entity;
   4749  1.1  tron   const char *textStart, *textEnd;
   4750  1.1  tron   const char *next;
   4751  1.1  tron   enum XML_Error result;
   4752  1.1  tron   OPEN_INTERNAL_ENTITY *openEntity = openInternalEntities;
   4753  1.1  tron   if (!openEntity)
   4754  1.1  tron     return XML_ERROR_UNEXPECTED_STATE;
   4755  1.1  tron 
   4756  1.1  tron   entity = openEntity->entity;
   4757  1.1  tron   textStart = ((char *)entity->textPtr) + entity->processed;
   4758  1.1  tron   textEnd = (char *)(entity->textPtr + entity->textLen);
   4759  1.1  tron 
   4760  1.1  tron #ifdef XML_DTD
   4761  1.1  tron   if (entity->is_param) {
   4762  1.1  tron     int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next);
   4763  1.1  tron     result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
   4764  1.1  tron                       next, &next, XML_FALSE);
   4765  1.1  tron   }
   4766  1.1  tron   else
   4767  1.1  tron #endif /* XML_DTD */
   4768  1.1  tron     result = doContent(parser, openEntity->startTagLevel, internalEncoding,
   4769  1.1  tron                        textStart, textEnd, &next, XML_FALSE);
   4770  1.1  tron 
   4771  1.1  tron   if (result != XML_ERROR_NONE)
   4772  1.1  tron     return result;
   4773  1.1  tron   else if (textEnd != next && ps_parsing == XML_SUSPENDED) {
   4774  1.1  tron     entity->processed = (int)(next - (char *)entity->textPtr);
   4775  1.1  tron     return result;
   4776  1.1  tron   }
   4777  1.1  tron   else {
   4778  1.1  tron     entity->open = XML_FALSE;
   4779  1.1  tron     openInternalEntities = openEntity->next;
   4780  1.1  tron     /* put openEntity back in list of free instances */
   4781  1.1  tron     openEntity->next = freeInternalEntities;
   4782  1.1  tron     freeInternalEntities = openEntity;
   4783  1.1  tron   }
   4784  1.1  tron 
   4785  1.1  tron #ifdef XML_DTD
   4786  1.1  tron   if (entity->is_param) {
   4787  1.1  tron     int tok;
   4788  1.1  tron     processor = prologProcessor;
   4789  1.1  tron     tok = XmlPrologTok(encoding, s, end, &next);
   4790  1.1  tron     return doProlog(parser, encoding, s, end, tok, next, nextPtr,
   4791  1.1  tron                     (XML_Bool)!ps_finalBuffer);
   4792  1.1  tron   }
   4793  1.1  tron   else
   4794  1.1  tron #endif /* XML_DTD */
   4795  1.1  tron   {
   4796  1.1  tron     processor = contentProcessor;
   4797  1.1  tron     /* see externalEntityContentProcessor vs contentProcessor */
   4798  1.1  tron     return doContent(parser, parentParser ? 1 : 0, encoding, s, end,
   4799  1.1  tron                      nextPtr, (XML_Bool)!ps_finalBuffer);
   4800  1.1  tron   }
   4801  1.1  tron }
   4802  1.1  tron 
   4803  1.1  tron static enum XML_Error PTRCALL
   4804  1.1  tron errorProcessor(XML_Parser parser,
   4805  1.1  tron                const char *s,
   4806  1.1  tron                const char *end,
   4807  1.1  tron                const char **nextPtr)
   4808  1.1  tron {
   4809  1.1  tron   return errorCode;
   4810  1.1  tron }
   4811  1.1  tron 
   4812  1.1  tron static enum XML_Error
   4813  1.1  tron storeAttributeValue(XML_Parser parser, const ENCODING *enc, XML_Bool isCdata,
   4814  1.1  tron                     const char *ptr, const char *end,
   4815  1.1  tron                     STRING_POOL *pool)
   4816  1.1  tron {
   4817  1.1  tron   enum XML_Error result = appendAttributeValue(parser, enc, isCdata, ptr,
   4818  1.1  tron                                                end, pool);
   4819  1.1  tron   if (result)
   4820  1.1  tron     return result;
   4821  1.1  tron   if (!isCdata && poolLength(pool) && poolLastChar(pool) == 0x20)
   4822  1.1  tron     poolChop(pool);
   4823  1.1  tron   if (!poolAppendChar(pool, XML_T('\0')))
   4824  1.1  tron     return XML_ERROR_NO_MEMORY;
   4825  1.1  tron   return XML_ERROR_NONE;
   4826  1.1  tron }
   4827  1.1  tron 
   4828  1.1  tron static enum XML_Error
   4829  1.1  tron appendAttributeValue(XML_Parser parser, const ENCODING *enc, XML_Bool isCdata,
   4830  1.1  tron                      const char *ptr, const char *end,
   4831  1.1  tron                      STRING_POOL *pool)
   4832  1.1  tron {
   4833  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   4834  1.1  tron   for (;;) {
   4835  1.1  tron     const char *next;
   4836  1.1  tron     int tok = XmlAttributeValueTok(enc, ptr, end, &next);
   4837  1.1  tron     switch (tok) {
   4838  1.1  tron     case XML_TOK_NONE:
   4839  1.1  tron       return XML_ERROR_NONE;
   4840  1.1  tron     case XML_TOK_INVALID:
   4841  1.1  tron       if (enc == encoding)
   4842  1.1  tron         eventPtr = next;
   4843  1.1  tron       return XML_ERROR_INVALID_TOKEN;
   4844  1.1  tron     case XML_TOK_PARTIAL:
   4845  1.1  tron       if (enc == encoding)
   4846  1.1  tron         eventPtr = ptr;
   4847  1.1  tron       return XML_ERROR_INVALID_TOKEN;
   4848  1.1  tron     case XML_TOK_CHAR_REF:
   4849  1.1  tron       {
   4850  1.1  tron         XML_Char buf[XML_ENCODE_MAX];
   4851  1.1  tron         int i;
   4852  1.1  tron         int n = XmlCharRefNumber(enc, ptr);
   4853  1.1  tron         if (n < 0) {
   4854  1.1  tron           if (enc == encoding)
   4855  1.1  tron             eventPtr = ptr;
   4856  1.1  tron           return XML_ERROR_BAD_CHAR_REF;
   4857  1.1  tron         }
   4858  1.1  tron         if (!isCdata
   4859  1.1  tron             && n == 0x20 /* space */
   4860  1.1  tron             && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
   4861  1.1  tron           break;
   4862  1.1  tron         n = XmlEncode(n, (ICHAR *)buf);
   4863  1.1  tron         if (!n) {
   4864  1.1  tron           if (enc == encoding)
   4865  1.1  tron             eventPtr = ptr;
   4866  1.1  tron           return XML_ERROR_BAD_CHAR_REF;
   4867  1.1  tron         }
   4868  1.1  tron         for (i = 0; i < n; i++) {
   4869  1.1  tron           if (!poolAppendChar(pool, buf[i]))
   4870  1.1  tron             return XML_ERROR_NO_MEMORY;
   4871  1.1  tron         }
   4872  1.1  tron       }
   4873  1.1  tron       break;
   4874  1.1  tron     case XML_TOK_DATA_CHARS:
   4875  1.1  tron       if (!poolAppend(pool, enc, ptr, next))
   4876  1.1  tron         return XML_ERROR_NO_MEMORY;
   4877  1.1  tron       break;
   4878  1.1  tron     case XML_TOK_TRAILING_CR:
   4879  1.1  tron       next = ptr + enc->minBytesPerChar;
   4880  1.1  tron       /* fall through */
   4881  1.1  tron     case XML_TOK_ATTRIBUTE_VALUE_S:
   4882  1.1  tron     case XML_TOK_DATA_NEWLINE:
   4883  1.1  tron       if (!isCdata && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
   4884  1.1  tron         break;
   4885  1.1  tron       if (!poolAppendChar(pool, 0x20))
   4886  1.1  tron         return XML_ERROR_NO_MEMORY;
   4887  1.1  tron       break;
   4888  1.1  tron     case XML_TOK_ENTITY_REF:
   4889  1.1  tron       {
   4890  1.1  tron         const XML_Char *name;
   4891  1.1  tron         ENTITY *entity;
   4892  1.1  tron         char checkEntityDecl;
   4893  1.1  tron         XML_Char ch = (XML_Char) XmlPredefinedEntityName(enc,
   4894  1.1  tron                                               ptr + enc->minBytesPerChar,
   4895  1.1  tron                                               next - enc->minBytesPerChar);
   4896  1.1  tron         if (ch) {
   4897  1.1  tron           if (!poolAppendChar(pool, ch))
   4898  1.1  tron                 return XML_ERROR_NO_MEMORY;
   4899  1.1  tron           break;
   4900  1.1  tron         }
   4901  1.1  tron         name = poolStoreString(&temp2Pool, enc,
   4902  1.1  tron                                ptr + enc->minBytesPerChar,
   4903  1.1  tron                                next - enc->minBytesPerChar);
   4904  1.1  tron         if (!name)
   4905  1.1  tron           return XML_ERROR_NO_MEMORY;
   4906  1.1  tron         entity = (ENTITY *)lookup(&dtd->generalEntities, name, 0);
   4907  1.1  tron         poolDiscard(&temp2Pool);
   4908  1.1  tron         /* First, determine if a check for an existing declaration is needed;
   4909  1.1  tron            if yes, check that the entity exists, and that it is internal.
   4910  1.1  tron         */
   4911  1.1  tron         if (pool == &dtd->pool)  /* are we called from prolog? */
   4912  1.1  tron           checkEntityDecl =
   4913  1.1  tron #ifdef XML_DTD
   4914  1.1  tron               prologState.documentEntity &&
   4915  1.1  tron #endif /* XML_DTD */
   4916  1.1  tron               (dtd->standalone
   4917  1.1  tron                ? !openInternalEntities
   4918  1.1  tron                : !dtd->hasParamEntityRefs);
   4919  1.1  tron         else /* if (pool == &tempPool): we are called from content */
   4920  1.1  tron           checkEntityDecl = !dtd->hasParamEntityRefs || dtd->standalone;
   4921  1.1  tron         if (checkEntityDecl) {
   4922  1.1  tron           if (!entity)
   4923  1.1  tron             return XML_ERROR_UNDEFINED_ENTITY;
   4924  1.1  tron           else if (!entity->is_internal)
   4925  1.1  tron             return XML_ERROR_ENTITY_DECLARED_IN_PE;
   4926  1.1  tron         }
   4927  1.1  tron         else if (!entity) {
   4928  1.1  tron           /* Cannot report skipped entity here - see comments on
   4929  1.1  tron              skippedEntityHandler.
   4930  1.1  tron           if (skippedEntityHandler)
   4931  1.1  tron             skippedEntityHandler(handlerArg, name, 0);
   4932  1.1  tron           */
   4933  1.1  tron           /* Cannot call the default handler because this would be
   4934  1.1  tron              out of sync with the call to the startElementHandler.
   4935  1.1  tron           if ((pool == &tempPool) && defaultHandler)
   4936  1.1  tron             reportDefault(parser, enc, ptr, next);
   4937  1.1  tron           */
   4938  1.1  tron           break;
   4939  1.1  tron         }
   4940  1.1  tron         if (entity->open) {
   4941  1.1  tron           if (enc == encoding)
   4942  1.1  tron             eventPtr = ptr;
   4943  1.1  tron           return XML_ERROR_RECURSIVE_ENTITY_REF;
   4944  1.1  tron         }
   4945  1.1  tron         if (entity->notation) {
   4946  1.1  tron           if (enc == encoding)
   4947  1.1  tron             eventPtr = ptr;
   4948  1.1  tron           return XML_ERROR_BINARY_ENTITY_REF;
   4949  1.1  tron         }
   4950  1.1  tron         if (!entity->textPtr) {
   4951  1.1  tron           if (enc == encoding)
   4952  1.1  tron             eventPtr = ptr;
   4953  1.1  tron               return XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF;
   4954  1.1  tron         }
   4955  1.1  tron         else {
   4956  1.1  tron           enum XML_Error result;
   4957  1.1  tron           const XML_Char *textEnd = entity->textPtr + entity->textLen;
   4958  1.1  tron           entity->open = XML_TRUE;
   4959  1.1  tron           result = appendAttributeValue(parser, internalEncoding, isCdata,
   4960  1.1  tron                                         (char *)entity->textPtr,
   4961  1.1  tron                                         (char *)textEnd, pool);
   4962  1.1  tron           entity->open = XML_FALSE;
   4963  1.1  tron           if (result)
   4964  1.1  tron             return result;
   4965  1.1  tron         }
   4966  1.1  tron       }
   4967  1.1  tron       break;
   4968  1.1  tron     default:
   4969  1.1  tron       if (enc == encoding)
   4970  1.1  tron         eventPtr = ptr;
   4971  1.1  tron       return XML_ERROR_UNEXPECTED_STATE;
   4972  1.1  tron     }
   4973  1.1  tron     ptr = next;
   4974  1.1  tron   }
   4975  1.1  tron   /* not reached */
   4976  1.1  tron }
   4977  1.1  tron 
   4978  1.1  tron static enum XML_Error
   4979  1.1  tron storeEntityValue(XML_Parser parser,
   4980  1.1  tron                  const ENCODING *enc,
   4981  1.1  tron                  const char *entityTextPtr,
   4982  1.1  tron                  const char *entityTextEnd)
   4983  1.1  tron {
   4984  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   4985  1.1  tron   STRING_POOL *pool = &(dtd->entityValuePool);
   4986  1.1  tron   enum XML_Error result = XML_ERROR_NONE;
   4987  1.1  tron #ifdef XML_DTD
   4988  1.1  tron   int oldInEntityValue = prologState.inEntityValue;
   4989  1.1  tron   prologState.inEntityValue = 1;
   4990  1.1  tron #endif /* XML_DTD */
   4991  1.1  tron   /* never return Null for the value argument in EntityDeclHandler,
   4992  1.1  tron      since this would indicate an external entity; therefore we
   4993  1.1  tron      have to make sure that entityValuePool.start is not null */
   4994  1.1  tron   if (!pool->blocks) {
   4995  1.1  tron     if (!poolGrow(pool))
   4996  1.1  tron       return XML_ERROR_NO_MEMORY;
   4997  1.1  tron   }
   4998  1.1  tron 
   4999  1.1  tron   for (;;) {
   5000  1.1  tron     const char *next;
   5001  1.1  tron     int tok = XmlEntityValueTok(enc, entityTextPtr, entityTextEnd, &next);
   5002  1.1  tron     switch (tok) {
   5003  1.1  tron     case XML_TOK_PARAM_ENTITY_REF:
   5004  1.1  tron #ifdef XML_DTD
   5005  1.1  tron       if (isParamEntity || enc != encoding) {
   5006  1.1  tron         const XML_Char *name;
   5007  1.1  tron         ENTITY *entity;
   5008  1.1  tron         name = poolStoreString(&tempPool, enc,
   5009  1.1  tron                                entityTextPtr + enc->minBytesPerChar,
   5010  1.1  tron                                next - enc->minBytesPerChar);
   5011  1.1  tron         if (!name) {
   5012  1.1  tron           result = XML_ERROR_NO_MEMORY;
   5013  1.1  tron           goto endEntityValue;
   5014  1.1  tron         }
   5015  1.1  tron         entity = (ENTITY *)lookup(&dtd->paramEntities, name, 0);
   5016  1.1  tron         poolDiscard(&tempPool);
   5017  1.1  tron         if (!entity) {
   5018  1.1  tron           /* not a well-formedness error - see XML 1.0: WFC Entity Declared */
   5019  1.1  tron           /* cannot report skipped entity here - see comments on
   5020  1.1  tron              skippedEntityHandler
   5021  1.1  tron           if (skippedEntityHandler)
   5022  1.1  tron             skippedEntityHandler(handlerArg, name, 0);
   5023  1.1  tron           */
   5024  1.1  tron           dtd->keepProcessing = dtd->standalone;
   5025  1.1  tron           goto endEntityValue;
   5026  1.1  tron         }
   5027  1.1  tron         if (entity->open) {
   5028  1.1  tron           if (enc == encoding)
   5029  1.1  tron             eventPtr = entityTextPtr;
   5030  1.1  tron           result = XML_ERROR_RECURSIVE_ENTITY_REF;
   5031  1.1  tron           goto endEntityValue;
   5032  1.1  tron         }
   5033  1.1  tron         if (entity->systemId) {
   5034  1.1  tron           if (externalEntityRefHandler) {
   5035  1.1  tron             dtd->paramEntityRead = XML_FALSE;
   5036  1.1  tron             entity->open = XML_TRUE;
   5037  1.1  tron             if (!externalEntityRefHandler(externalEntityRefHandlerArg,
   5038  1.1  tron                                           0,
   5039  1.1  tron                                           entity->base,
   5040  1.1  tron                                           entity->systemId,
   5041  1.1  tron                                           entity->publicId)) {
   5042  1.1  tron               entity->open = XML_FALSE;
   5043  1.1  tron               result = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
   5044  1.1  tron               goto endEntityValue;
   5045  1.1  tron             }
   5046  1.1  tron             entity->open = XML_FALSE;
   5047  1.1  tron             if (!dtd->paramEntityRead)
   5048  1.1  tron               dtd->keepProcessing = dtd->standalone;
   5049  1.1  tron           }
   5050  1.1  tron           else
   5051  1.1  tron             dtd->keepProcessing = dtd->standalone;
   5052  1.1  tron         }
   5053  1.1  tron         else {
   5054  1.1  tron           entity->open = XML_TRUE;
   5055  1.1  tron           result = storeEntityValue(parser,
   5056  1.1  tron                                     internalEncoding,
   5057  1.1  tron                                     (char *)entity->textPtr,
   5058  1.1  tron                                     (char *)(entity->textPtr
   5059  1.1  tron                                              + entity->textLen));
   5060  1.1  tron           entity->open = XML_FALSE;
   5061  1.1  tron           if (result)
   5062  1.1  tron             goto endEntityValue;
   5063  1.1  tron         }
   5064  1.1  tron         break;
   5065  1.1  tron       }
   5066  1.1  tron #endif /* XML_DTD */
   5067  1.1  tron       /* In the internal subset, PE references are not legal
   5068  1.1  tron          within markup declarations, e.g entity values in this case. */
   5069  1.1  tron       eventPtr = entityTextPtr;
   5070  1.1  tron       result = XML_ERROR_PARAM_ENTITY_REF;
   5071  1.1  tron       goto endEntityValue;
   5072  1.1  tron     case XML_TOK_NONE:
   5073  1.1  tron       result = XML_ERROR_NONE;
   5074  1.1  tron       goto endEntityValue;
   5075  1.1  tron     case XML_TOK_ENTITY_REF:
   5076  1.1  tron     case XML_TOK_DATA_CHARS:
   5077  1.1  tron       if (!poolAppend(pool, enc, entityTextPtr, next)) {
   5078  1.1  tron         result = XML_ERROR_NO_MEMORY;
   5079  1.1  tron         goto endEntityValue;
   5080  1.1  tron       }
   5081  1.1  tron       break;
   5082  1.1  tron     case XML_TOK_TRAILING_CR:
   5083  1.1  tron       next = entityTextPtr + enc->minBytesPerChar;
   5084  1.1  tron       /* fall through */
   5085  1.1  tron     case XML_TOK_DATA_NEWLINE:
   5086  1.1  tron       if (pool->end == pool->ptr && !poolGrow(pool)) {
   5087  1.1  tron               result = XML_ERROR_NO_MEMORY;
   5088  1.1  tron         goto endEntityValue;
   5089  1.1  tron       }
   5090  1.1  tron       *(pool->ptr)++ = 0xA;
   5091  1.1  tron       break;
   5092  1.1  tron     case XML_TOK_CHAR_REF:
   5093  1.1  tron       {
   5094  1.1  tron         XML_Char buf[XML_ENCODE_MAX];
   5095  1.1  tron         int i;
   5096  1.1  tron         int n = XmlCharRefNumber(enc, entityTextPtr);
   5097  1.1  tron         if (n < 0) {
   5098  1.1  tron           if (enc == encoding)
   5099  1.1  tron             eventPtr = entityTextPtr;
   5100  1.1  tron           result = XML_ERROR_BAD_CHAR_REF;
   5101  1.1  tron           goto endEntityValue;
   5102  1.1  tron         }
   5103  1.1  tron         n = XmlEncode(n, (ICHAR *)buf);
   5104  1.1  tron         if (!n) {
   5105  1.1  tron           if (enc == encoding)
   5106  1.1  tron             eventPtr = entityTextPtr;
   5107  1.1  tron           result = XML_ERROR_BAD_CHAR_REF;
   5108  1.1  tron           goto endEntityValue;
   5109  1.1  tron         }
   5110  1.1  tron         for (i = 0; i < n; i++) {
   5111  1.1  tron           if (pool->end == pool->ptr && !poolGrow(pool)) {
   5112  1.1  tron             result = XML_ERROR_NO_MEMORY;
   5113  1.1  tron             goto endEntityValue;
   5114  1.1  tron           }
   5115  1.1  tron           *(pool->ptr)++ = buf[i];
   5116  1.1  tron         }
   5117  1.1  tron       }
   5118  1.1  tron       break;
   5119  1.1  tron     case XML_TOK_PARTIAL:
   5120  1.1  tron       if (enc == encoding)
   5121  1.1  tron         eventPtr = entityTextPtr;
   5122  1.1  tron       result = XML_ERROR_INVALID_TOKEN;
   5123  1.1  tron       goto endEntityValue;
   5124  1.1  tron     case XML_TOK_INVALID:
   5125  1.1  tron       if (enc == encoding)
   5126  1.1  tron         eventPtr = next;
   5127  1.1  tron       result = XML_ERROR_INVALID_TOKEN;
   5128  1.1  tron       goto endEntityValue;
   5129  1.1  tron     default:
   5130  1.1  tron       if (enc == encoding)
   5131  1.1  tron         eventPtr = entityTextPtr;
   5132  1.1  tron       result = XML_ERROR_UNEXPECTED_STATE;
   5133  1.1  tron       goto endEntityValue;
   5134  1.1  tron     }
   5135  1.1  tron     entityTextPtr = next;
   5136  1.1  tron   }
   5137  1.1  tron endEntityValue:
   5138  1.1  tron #ifdef XML_DTD
   5139  1.1  tron   prologState.inEntityValue = oldInEntityValue;
   5140  1.1  tron #endif /* XML_DTD */
   5141  1.1  tron   return result;
   5142  1.1  tron }
   5143  1.1  tron 
   5144  1.1  tron static void FASTCALL
   5145  1.1  tron normalizeLines(XML_Char *s)
   5146  1.1  tron {
   5147  1.1  tron   XML_Char *p;
   5148  1.1  tron   for (;; s++) {
   5149  1.1  tron     if (*s == XML_T('\0'))
   5150  1.1  tron       return;
   5151  1.1  tron     if (*s == 0xD)
   5152  1.1  tron       break;
   5153  1.1  tron   }
   5154  1.1  tron   p = s;
   5155  1.1  tron   do {
   5156  1.1  tron     if (*s == 0xD) {
   5157  1.1  tron       *p++ = 0xA;
   5158  1.1  tron       if (*++s == 0xA)
   5159  1.1  tron         s++;
   5160  1.1  tron     }
   5161  1.1  tron     else
   5162  1.1  tron       *p++ = *s++;
   5163  1.1  tron   } while (*s);
   5164  1.1  tron   *p = XML_T('\0');
   5165  1.1  tron }
   5166  1.1  tron 
   5167  1.1  tron static int
   5168  1.1  tron reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
   5169  1.1  tron                             const char *start, const char *end)
   5170  1.1  tron {
   5171  1.1  tron   const XML_Char *target;
   5172  1.1  tron   XML_Char *data;
   5173  1.1  tron   const char *tem;
   5174  1.1  tron   if (!processingInstructionHandler) {
   5175  1.1  tron     if (defaultHandler)
   5176  1.1  tron       reportDefault(parser, enc, start, end);
   5177  1.1  tron     return 1;
   5178  1.1  tron   }
   5179  1.1  tron   start += enc->minBytesPerChar * 2;
   5180  1.1  tron   tem = start + XmlNameLength(enc, start);
   5181  1.1  tron   target = poolStoreString(&tempPool, enc, start, tem);
   5182  1.1  tron   if (!target)
   5183  1.1  tron     return 0;
   5184  1.1  tron   poolFinish(&tempPool);
   5185  1.1  tron   data = poolStoreString(&tempPool, enc,
   5186  1.1  tron                         XmlSkipS(enc, tem),
   5187  1.1  tron                         end - enc->minBytesPerChar*2);
   5188  1.1  tron   if (!data)
   5189  1.1  tron     return 0;
   5190  1.1  tron   normalizeLines(data);
   5191  1.1  tron   processingInstructionHandler(handlerArg, target, data);
   5192  1.1  tron   poolClear(&tempPool);
   5193  1.1  tron   return 1;
   5194  1.1  tron }
   5195  1.1  tron 
   5196  1.1  tron static int
   5197  1.1  tron reportComment(XML_Parser parser, const ENCODING *enc,
   5198  1.1  tron               const char *start, const char *end)
   5199  1.1  tron {
   5200  1.1  tron   XML_Char *data;
   5201  1.1  tron   if (!commentHandler) {
   5202  1.1  tron     if (defaultHandler)
   5203  1.1  tron       reportDefault(parser, enc, start, end);
   5204  1.1  tron     return 1;
   5205  1.1  tron   }
   5206  1.1  tron   data = poolStoreString(&tempPool,
   5207  1.1  tron                          enc,
   5208  1.1  tron                          start + enc->minBytesPerChar * 4,
   5209  1.1  tron                          end - enc->minBytesPerChar * 3);
   5210  1.1  tron   if (!data)
   5211  1.1  tron     return 0;
   5212  1.1  tron   normalizeLines(data);
   5213  1.1  tron   commentHandler(handlerArg, data);
   5214  1.1  tron   poolClear(&tempPool);
   5215  1.1  tron   return 1;
   5216  1.1  tron }
   5217  1.1  tron 
   5218  1.1  tron static void
   5219  1.1  tron reportDefault(XML_Parser parser, const ENCODING *enc,
   5220  1.1  tron               const char *s, const char *end)
   5221  1.1  tron {
   5222  1.1  tron   if (MUST_CONVERT(enc, s)) {
   5223  1.1  tron     const char **eventPP;
   5224  1.1  tron     const char **eventEndPP;
   5225  1.1  tron     if (enc == encoding) {
   5226  1.1  tron       eventPP = &eventPtr;
   5227  1.1  tron       eventEndPP = &eventEndPtr;
   5228  1.1  tron     }
   5229  1.1  tron     else {
   5230  1.1  tron       eventPP = &(openInternalEntities->internalEventPtr);
   5231  1.1  tron       eventEndPP = &(openInternalEntities->internalEventEndPtr);
   5232  1.1  tron     }
   5233  1.1  tron     do {
   5234  1.1  tron       ICHAR *dataPtr = (ICHAR *)dataBuf;
   5235  1.1  tron       XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
   5236  1.1  tron       *eventEndPP = s;
   5237  1.1  tron       defaultHandler(handlerArg, dataBuf, (int)(dataPtr - (ICHAR *)dataBuf));
   5238  1.1  tron       *eventPP = s;
   5239  1.1  tron     } while (s != end);
   5240  1.1  tron   }
   5241  1.1  tron   else
   5242  1.1  tron     defaultHandler(handlerArg, (XML_Char *)s, (int)((XML_Char *)end - (XML_Char *)s));
   5243  1.1  tron }
   5244  1.1  tron 
   5245  1.1  tron 
   5246  1.1  tron static int
   5247  1.1  tron defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, XML_Bool isCdata,
   5248  1.1  tron                 XML_Bool isId, const XML_Char *value, XML_Parser parser)
   5249  1.1  tron {
   5250  1.1  tron   DEFAULT_ATTRIBUTE *att;
   5251  1.1  tron   if (value || isId) {
   5252  1.1  tron     /* The handling of default attributes gets messed up if we have
   5253  1.1  tron        a default which duplicates a non-default. */
   5254  1.1  tron     int i;
   5255  1.1  tron     for (i = 0; i < type->nDefaultAtts; i++)
   5256  1.1  tron       if (attId == type->defaultAtts[i].id)
   5257  1.1  tron         return 1;
   5258  1.1  tron     if (isId && !type->idAtt && !attId->xmlns)
   5259  1.1  tron       type->idAtt = attId;
   5260  1.1  tron   }
   5261  1.1  tron   if (type->nDefaultAtts == type->allocDefaultAtts) {
   5262  1.1  tron     if (type->allocDefaultAtts == 0) {
   5263  1.1  tron       type->allocDefaultAtts = 8;
   5264  1.1  tron       type->defaultAtts = (DEFAULT_ATTRIBUTE *)MALLOC(type->allocDefaultAtts
   5265  1.1  tron                             * sizeof(DEFAULT_ATTRIBUTE));
   5266  1.1  tron       if (!type->defaultAtts)
   5267  1.1  tron         return 0;
   5268  1.1  tron     }
   5269  1.1  tron     else {
   5270  1.1  tron       DEFAULT_ATTRIBUTE *temp;
   5271  1.1  tron       int count = type->allocDefaultAtts * 2;
   5272  1.1  tron       temp = (DEFAULT_ATTRIBUTE *)
   5273  1.1  tron         REALLOC(type->defaultAtts, (count * sizeof(DEFAULT_ATTRIBUTE)));
   5274  1.1  tron       if (temp == NULL)
   5275  1.1  tron         return 0;
   5276  1.1  tron       type->allocDefaultAtts = count;
   5277  1.1  tron       type->defaultAtts = temp;
   5278  1.1  tron     }
   5279  1.1  tron   }
   5280  1.1  tron   att = type->defaultAtts + type->nDefaultAtts;
   5281  1.1  tron   att->id = attId;
   5282  1.1  tron   att->value = value;
   5283  1.1  tron   att->isCdata = isCdata;
   5284  1.1  tron   if (!isCdata)
   5285  1.1  tron     attId->maybeTokenized = XML_TRUE;
   5286  1.1  tron   type->nDefaultAtts += 1;
   5287  1.1  tron   return 1;
   5288  1.1  tron }
   5289  1.1  tron 
   5290  1.1  tron static int
   5291  1.1  tron setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *elementType)
   5292  1.1  tron {
   5293  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   5294  1.1  tron   const XML_Char *name;
   5295  1.1  tron   for (name = elementType->name; *name; name++) {
   5296  1.1  tron     if (*name == XML_T(ASCII_COLON)) {
   5297  1.1  tron       PREFIX *prefix;
   5298  1.1  tron       const XML_Char *s;
   5299  1.1  tron       for (s = elementType->name; s != name; s++) {
   5300  1.1  tron         if (!poolAppendChar(&dtd->pool, *s))
   5301  1.1  tron           return 0;
   5302  1.1  tron       }
   5303  1.1  tron       if (!poolAppendChar(&dtd->pool, XML_T('\0')))
   5304  1.1  tron         return 0;
   5305  1.1  tron       prefix = (PREFIX *)lookup(&dtd->prefixes, poolStart(&dtd->pool),
   5306  1.1  tron                                 sizeof(PREFIX));
   5307  1.1  tron       if (!prefix)
   5308  1.1  tron         return 0;
   5309  1.1  tron       if (prefix->name == poolStart(&dtd->pool))
   5310  1.1  tron         poolFinish(&dtd->pool);
   5311  1.1  tron       else
   5312  1.1  tron         poolDiscard(&dtd->pool);
   5313  1.1  tron       elementType->prefix = prefix;
   5314  1.1  tron 
   5315  1.1  tron     }
   5316  1.1  tron   }
   5317  1.1  tron   return 1;
   5318  1.1  tron }
   5319  1.1  tron 
   5320  1.1  tron static ATTRIBUTE_ID *
   5321  1.1  tron getAttributeId(XML_Parser parser, const ENCODING *enc,
   5322  1.1  tron                const char *start, const char *end)
   5323  1.1  tron {
   5324  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   5325  1.1  tron   ATTRIBUTE_ID *id;
   5326  1.1  tron   const XML_Char *name;
   5327  1.1  tron   if (!poolAppendChar(&dtd->pool, XML_T('\0')))
   5328  1.1  tron     return NULL;
   5329  1.1  tron   name = poolStoreString(&dtd->pool, enc, start, end);
   5330  1.1  tron   if (!name)
   5331  1.1  tron     return NULL;
   5332  1.1  tron   /* skip quotation mark - its storage will be re-used (like in name[-1]) */
   5333  1.1  tron   ++name;
   5334  1.1  tron   id = (ATTRIBUTE_ID *)lookup(&dtd->attributeIds, name, sizeof(ATTRIBUTE_ID));
   5335  1.1  tron   if (!id)
   5336  1.1  tron     return NULL;
   5337  1.1  tron   if (id->name != name)
   5338  1.1  tron     poolDiscard(&dtd->pool);
   5339  1.1  tron   else {
   5340  1.1  tron     poolFinish(&dtd->pool);
   5341  1.1  tron     if (!ns)
   5342  1.1  tron       ;
   5343  1.1  tron     else if (name[0] == XML_T(ASCII_x)
   5344  1.1  tron         && name[1] == XML_T(ASCII_m)
   5345  1.1  tron         && name[2] == XML_T(ASCII_l)
   5346  1.1  tron         && name[3] == XML_T(ASCII_n)
   5347  1.1  tron         && name[4] == XML_T(ASCII_s)
   5348  1.1  tron         && (name[5] == XML_T('\0') || name[5] == XML_T(ASCII_COLON))) {
   5349  1.1  tron       if (name[5] == XML_T('\0'))
   5350  1.1  tron         id->prefix = &dtd->defaultPrefix;
   5351  1.1  tron       else
   5352  1.1  tron         id->prefix = (PREFIX *)lookup(&dtd->prefixes, name + 6, sizeof(PREFIX));
   5353  1.1  tron       id->xmlns = XML_TRUE;
   5354  1.1  tron     }
   5355  1.1  tron     else {
   5356  1.1  tron       int i;
   5357  1.1  tron       for (i = 0; name[i]; i++) {
   5358  1.1  tron         /* attributes without prefix are *not* in the default namespace */
   5359  1.1  tron         if (name[i] == XML_T(ASCII_COLON)) {
   5360  1.1  tron           int j;
   5361  1.1  tron           for (j = 0; j < i; j++) {
   5362  1.1  tron             if (!poolAppendChar(&dtd->pool, name[j]))
   5363  1.1  tron               return NULL;
   5364  1.1  tron           }
   5365  1.1  tron           if (!poolAppendChar(&dtd->pool, XML_T('\0')))
   5366  1.1  tron             return NULL;
   5367  1.1  tron           id->prefix = (PREFIX *)lookup(&dtd->prefixes, poolStart(&dtd->pool),
   5368  1.1  tron                                         sizeof(PREFIX));
   5369  1.1  tron           if (id->prefix->name == poolStart(&dtd->pool))
   5370  1.1  tron             poolFinish(&dtd->pool);
   5371  1.1  tron           else
   5372  1.1  tron             poolDiscard(&dtd->pool);
   5373  1.1  tron           break;
   5374  1.1  tron         }
   5375  1.1  tron       }
   5376  1.1  tron     }
   5377  1.1  tron   }
   5378  1.1  tron   return id;
   5379  1.1  tron }
   5380  1.1  tron 
   5381  1.1  tron #define CONTEXT_SEP XML_T(ASCII_FF)
   5382  1.1  tron 
   5383  1.1  tron static const XML_Char *
   5384  1.1  tron getContext(XML_Parser parser)
   5385  1.1  tron {
   5386  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   5387  1.1  tron   HASH_TABLE_ITER iter;
   5388  1.1  tron   XML_Bool needSep = XML_FALSE;
   5389  1.1  tron 
   5390  1.1  tron   if (dtd->defaultPrefix.binding) {
   5391  1.1  tron     int i;
   5392  1.1  tron     int len;
   5393  1.1  tron     if (!poolAppendChar(&tempPool, XML_T(ASCII_EQUALS)))
   5394  1.1  tron       return NULL;
   5395  1.1  tron     len = dtd->defaultPrefix.binding->uriLen;
   5396  1.1  tron     if (namespaceSeparator)
   5397  1.1  tron       len--;
   5398  1.1  tron     for (i = 0; i < len; i++)
   5399  1.1  tron       if (!poolAppendChar(&tempPool, dtd->defaultPrefix.binding->uri[i]))
   5400  1.1  tron         return NULL;
   5401  1.1  tron     needSep = XML_TRUE;
   5402  1.1  tron   }
   5403  1.1  tron 
   5404  1.1  tron   hashTableIterInit(&iter, &(dtd->prefixes));
   5405  1.1  tron   for (;;) {
   5406  1.1  tron     int i;
   5407  1.1  tron     int len;
   5408  1.1  tron     const XML_Char *s;
   5409  1.1  tron     PREFIX *prefix = (PREFIX *)hashTableIterNext(&iter);
   5410  1.1  tron     if (!prefix)
   5411  1.1  tron       break;
   5412  1.1  tron     if (!prefix->binding)
   5413  1.1  tron       continue;
   5414  1.1  tron     if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
   5415  1.1  tron       return NULL;
   5416  1.1  tron     for (s = prefix->name; *s; s++)
   5417  1.1  tron       if (!poolAppendChar(&tempPool, *s))
   5418  1.1  tron         return NULL;
   5419  1.1  tron     if (!poolAppendChar(&tempPool, XML_T(ASCII_EQUALS)))
   5420  1.1  tron       return NULL;
   5421  1.1  tron     len = prefix->binding->uriLen;
   5422  1.1  tron     if (namespaceSeparator)
   5423  1.1  tron       len--;
   5424  1.1  tron     for (i = 0; i < len; i++)
   5425  1.1  tron       if (!poolAppendChar(&tempPool, prefix->binding->uri[i]))
   5426  1.1  tron         return NULL;
   5427  1.1  tron     needSep = XML_TRUE;
   5428  1.1  tron   }
   5429  1.1  tron 
   5430  1.1  tron 
   5431  1.1  tron   hashTableIterInit(&iter, &(dtd->generalEntities));
   5432  1.1  tron   for (;;) {
   5433  1.1  tron     const XML_Char *s;
   5434  1.1  tron     ENTITY *e = (ENTITY *)hashTableIterNext(&iter);
   5435  1.1  tron     if (!e)
   5436  1.1  tron       break;
   5437  1.1  tron     if (!e->open)
   5438  1.1  tron       continue;
   5439  1.1  tron     if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
   5440  1.1  tron       return NULL;
   5441  1.1  tron     for (s = e->name; *s; s++)
   5442  1.1  tron       if (!poolAppendChar(&tempPool, *s))
   5443  1.1  tron         return 0;
   5444  1.1  tron     needSep = XML_TRUE;
   5445  1.1  tron   }
   5446  1.1  tron 
   5447  1.1  tron   if (!poolAppendChar(&tempPool, XML_T('\0')))
   5448  1.1  tron     return NULL;
   5449  1.1  tron   return tempPool.start;
   5450  1.1  tron }
   5451  1.1  tron 
   5452  1.1  tron static XML_Bool
   5453  1.1  tron setContext(XML_Parser parser, const XML_Char *context)
   5454  1.1  tron {
   5455  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   5456  1.1  tron   const XML_Char *s = context;
   5457  1.1  tron 
   5458  1.1  tron   while (*context != XML_T('\0')) {
   5459  1.1  tron     if (*s == CONTEXT_SEP || *s == XML_T('\0')) {
   5460  1.1  tron       ENTITY *e;
   5461  1.1  tron       if (!poolAppendChar(&tempPool, XML_T('\0')))
   5462  1.1  tron         return XML_FALSE;
   5463  1.1  tron       e = (ENTITY *)lookup(&dtd->generalEntities, poolStart(&tempPool), 0);
   5464  1.1  tron       if (e)
   5465  1.1  tron         e->open = XML_TRUE;
   5466  1.1  tron       if (*s != XML_T('\0'))
   5467  1.1  tron         s++;
   5468  1.1  tron       context = s;
   5469  1.1  tron       poolDiscard(&tempPool);
   5470  1.1  tron     }
   5471  1.1  tron     else if (*s == XML_T(ASCII_EQUALS)) {
   5472  1.1  tron       PREFIX *prefix;
   5473  1.1  tron       if (poolLength(&tempPool) == 0)
   5474  1.1  tron         prefix = &dtd->defaultPrefix;
   5475  1.1  tron       else {
   5476  1.1  tron         if (!poolAppendChar(&tempPool, XML_T('\0')))
   5477  1.1  tron           return XML_FALSE;
   5478  1.1  tron         prefix = (PREFIX *)lookup(&dtd->prefixes, poolStart(&tempPool),
   5479  1.1  tron                                   sizeof(PREFIX));
   5480  1.1  tron         if (!prefix)
   5481  1.1  tron           return XML_FALSE;
   5482  1.1  tron         if (prefix->name == poolStart(&tempPool)) {
   5483  1.1  tron           prefix->name = poolCopyString(&dtd->pool, prefix->name);
   5484  1.1  tron           if (!prefix->name)
   5485  1.1  tron             return XML_FALSE;
   5486  1.1  tron         }
   5487  1.1  tron         poolDiscard(&tempPool);
   5488  1.1  tron       }
   5489  1.1  tron       for (context = s + 1;
   5490  1.1  tron            *context != CONTEXT_SEP && *context != XML_T('\0');
   5491  1.1  tron            context++)
   5492  1.1  tron         if (!poolAppendChar(&tempPool, *context))
   5493  1.1  tron           return XML_FALSE;
   5494  1.1  tron       if (!poolAppendChar(&tempPool, XML_T('\0')))
   5495  1.1  tron         return XML_FALSE;
   5496  1.1  tron       if (addBinding(parser, prefix, NULL, poolStart(&tempPool),
   5497  1.1  tron                      &inheritedBindings) != XML_ERROR_NONE)
   5498  1.1  tron         return XML_FALSE;
   5499  1.1  tron       poolDiscard(&tempPool);
   5500  1.1  tron       if (*context != XML_T('\0'))
   5501  1.1  tron         ++context;
   5502  1.1  tron       s = context;
   5503  1.1  tron     }
   5504  1.1  tron     else {
   5505  1.1  tron       if (!poolAppendChar(&tempPool, *s))
   5506  1.1  tron         return XML_FALSE;
   5507  1.1  tron       s++;
   5508  1.1  tron     }
   5509  1.1  tron   }
   5510  1.1  tron   return XML_TRUE;
   5511  1.1  tron }
   5512  1.1  tron 
   5513  1.1  tron static void FASTCALL
   5514  1.1  tron normalizePublicId(XML_Char *publicId)
   5515  1.1  tron {
   5516  1.1  tron   XML_Char *p = publicId;
   5517  1.1  tron   XML_Char *s;
   5518  1.1  tron   for (s = publicId; *s; s++) {
   5519  1.1  tron     switch (*s) {
   5520  1.1  tron     case 0x20:
   5521  1.1  tron     case 0xD:
   5522  1.1  tron     case 0xA:
   5523  1.1  tron       if (p != publicId && p[-1] != 0x20)
   5524  1.1  tron         *p++ = 0x20;
   5525  1.1  tron       break;
   5526  1.1  tron     default:
   5527  1.1  tron       *p++ = *s;
   5528  1.1  tron     }
   5529  1.1  tron   }
   5530  1.1  tron   if (p != publicId && p[-1] == 0x20)
   5531  1.1  tron     --p;
   5532  1.1  tron   *p = XML_T('\0');
   5533  1.1  tron }
   5534  1.1  tron 
   5535  1.1  tron static DTD *
   5536  1.1  tron dtdCreate(const XML_Memory_Handling_Suite *ms)
   5537  1.1  tron {
   5538  1.1  tron   DTD *p = (DTD *)ms->malloc_fcn(sizeof(DTD));
   5539  1.1  tron   if (p == NULL)
   5540  1.1  tron     return p;
   5541  1.1  tron   poolInit(&(p->pool), ms);
   5542  1.1  tron   poolInit(&(p->entityValuePool), ms);
   5543  1.1  tron   hashTableInit(&(p->generalEntities), ms);
   5544  1.1  tron   hashTableInit(&(p->elementTypes), ms);
   5545  1.1  tron   hashTableInit(&(p->attributeIds), ms);
   5546  1.1  tron   hashTableInit(&(p->prefixes), ms);
   5547  1.1  tron #ifdef XML_DTD
   5548  1.1  tron   p->paramEntityRead = XML_FALSE;
   5549  1.1  tron   hashTableInit(&(p->paramEntities), ms);
   5550  1.1  tron #endif /* XML_DTD */
   5551  1.1  tron   p->defaultPrefix.name = NULL;
   5552  1.1  tron   p->defaultPrefix.binding = NULL;
   5553  1.1  tron 
   5554  1.1  tron   p->in_eldecl = XML_FALSE;
   5555  1.1  tron   p->scaffIndex = NULL;
   5556  1.1  tron   p->scaffold = NULL;
   5557  1.1  tron   p->scaffLevel = 0;
   5558  1.1  tron   p->scaffSize = 0;
   5559  1.1  tron   p->scaffCount = 0;
   5560  1.1  tron   p->contentStringLen = 0;
   5561  1.1  tron 
   5562  1.1  tron   p->keepProcessing = XML_TRUE;
   5563  1.1  tron   p->hasParamEntityRefs = XML_FALSE;
   5564  1.1  tron   p->standalone = XML_FALSE;
   5565  1.1  tron   return p;
   5566  1.1  tron }
   5567  1.1  tron 
   5568  1.1  tron static void
   5569  1.1  tron dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms)
   5570  1.1  tron {
   5571  1.1  tron   HASH_TABLE_ITER iter;
   5572  1.1  tron   hashTableIterInit(&iter, &(p->elementTypes));
   5573  1.1  tron   for (;;) {
   5574  1.1  tron     ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
   5575  1.1  tron     if (!e)
   5576  1.1  tron       break;
   5577  1.1  tron     if (e->allocDefaultAtts != 0)
   5578  1.1  tron       ms->free_fcn(e->defaultAtts);
   5579  1.1  tron   }
   5580  1.1  tron   hashTableClear(&(p->generalEntities));
   5581  1.1  tron #ifdef XML_DTD
   5582  1.1  tron   p->paramEntityRead = XML_FALSE;
   5583  1.1  tron   hashTableClear(&(p->paramEntities));
   5584  1.1  tron #endif /* XML_DTD */
   5585  1.1  tron   hashTableClear(&(p->elementTypes));
   5586  1.1  tron   hashTableClear(&(p->attributeIds));
   5587  1.1  tron   hashTableClear(&(p->prefixes));
   5588  1.1  tron   poolClear(&(p->pool));
   5589  1.1  tron   poolClear(&(p->entityValuePool));
   5590  1.1  tron   p->defaultPrefix.name = NULL;
   5591  1.1  tron   p->defaultPrefix.binding = NULL;
   5592  1.1  tron 
   5593  1.1  tron   p->in_eldecl = XML_FALSE;
   5594  1.1  tron 
   5595  1.1  tron   ms->free_fcn(p->scaffIndex);
   5596  1.1  tron   p->scaffIndex = NULL;
   5597  1.1  tron   ms->free_fcn(p->scaffold);
   5598  1.1  tron   p->scaffold = NULL;
   5599  1.1  tron 
   5600  1.1  tron   p->scaffLevel = 0;
   5601  1.1  tron   p->scaffSize = 0;
   5602  1.1  tron   p->scaffCount = 0;
   5603  1.1  tron   p->contentStringLen = 0;
   5604  1.1  tron 
   5605  1.1  tron   p->keepProcessing = XML_TRUE;
   5606  1.1  tron   p->hasParamEntityRefs = XML_FALSE;
   5607  1.1  tron   p->standalone = XML_FALSE;
   5608  1.1  tron }
   5609  1.1  tron 
   5610  1.1  tron static void
   5611  1.1  tron dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms)
   5612  1.1  tron {
   5613  1.1  tron   HASH_TABLE_ITER iter;
   5614  1.1  tron   hashTableIterInit(&iter, &(p->elementTypes));
   5615  1.1  tron   for (;;) {
   5616  1.1  tron     ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
   5617  1.1  tron     if (!e)
   5618  1.1  tron       break;
   5619  1.1  tron     if (e->allocDefaultAtts != 0)
   5620  1.1  tron       ms->free_fcn(e->defaultAtts);
   5621  1.1  tron   }
   5622  1.1  tron   hashTableDestroy(&(p->generalEntities));
   5623  1.1  tron #ifdef XML_DTD
   5624  1.1  tron   hashTableDestroy(&(p->paramEntities));
   5625  1.1  tron #endif /* XML_DTD */
   5626  1.1  tron   hashTableDestroy(&(p->elementTypes));
   5627  1.1  tron   hashTableDestroy(&(p->attributeIds));
   5628  1.1  tron   hashTableDestroy(&(p->prefixes));
   5629  1.1  tron   poolDestroy(&(p->pool));
   5630  1.1  tron   poolDestroy(&(p->entityValuePool));
   5631  1.1  tron   if (isDocEntity) {
   5632  1.1  tron     ms->free_fcn(p->scaffIndex);
   5633  1.1  tron     ms->free_fcn(p->scaffold);
   5634  1.1  tron   }
   5635  1.1  tron   ms->free_fcn(p);
   5636  1.1  tron }
   5637  1.1  tron 
   5638  1.1  tron /* Do a deep copy of the DTD. Return 0 for out of memory, non-zero otherwise.
   5639  1.1  tron    The new DTD has already been initialized.
   5640  1.1  tron */
   5641  1.1  tron static int
   5642  1.1  tron dtdCopy(DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms)
   5643  1.1  tron {
   5644  1.1  tron   HASH_TABLE_ITER iter;
   5645  1.1  tron 
   5646  1.1  tron   /* Copy the prefix table. */
   5647  1.1  tron 
   5648  1.1  tron   hashTableIterInit(&iter, &(oldDtd->prefixes));
   5649  1.1  tron   for (;;) {
   5650  1.1  tron     const XML_Char *name;
   5651  1.1  tron     const PREFIX *oldP = (PREFIX *)hashTableIterNext(&iter);
   5652  1.1  tron     if (!oldP)
   5653  1.1  tron       break;
   5654  1.1  tron     name = poolCopyString(&(newDtd->pool), oldP->name);
   5655  1.1  tron     if (!name)
   5656  1.1  tron       return 0;
   5657  1.1  tron     if (!lookup(&(newDtd->prefixes), name, sizeof(PREFIX)))
   5658  1.1  tron       return 0;
   5659  1.1  tron   }
   5660  1.1  tron 
   5661  1.1  tron   hashTableIterInit(&iter, &(oldDtd->attributeIds));
   5662  1.1  tron 
   5663  1.1  tron   /* Copy the attribute id table. */
   5664  1.1  tron 
   5665  1.1  tron   for (;;) {
   5666  1.1  tron     ATTRIBUTE_ID *newA;
   5667  1.1  tron     const XML_Char *name;
   5668  1.1  tron     const ATTRIBUTE_ID *oldA = (ATTRIBUTE_ID *)hashTableIterNext(&iter);
   5669  1.1  tron 
   5670  1.1  tron     if (!oldA)
   5671  1.1  tron       break;
   5672  1.1  tron     /* Remember to allocate the scratch byte before the name. */
   5673  1.1  tron     if (!poolAppendChar(&(newDtd->pool), XML_T('\0')))
   5674  1.1  tron       return 0;
   5675  1.1  tron     name = poolCopyString(&(newDtd->pool), oldA->name);
   5676  1.1  tron     if (!name)
   5677  1.1  tron       return 0;
   5678  1.1  tron     ++name;
   5679  1.1  tron     newA = (ATTRIBUTE_ID *)lookup(&(newDtd->attributeIds), name,
   5680  1.1  tron                                   sizeof(ATTRIBUTE_ID));
   5681  1.1  tron     if (!newA)
   5682  1.1  tron       return 0;
   5683  1.1  tron     newA->maybeTokenized = oldA->maybeTokenized;
   5684  1.1  tron     if (oldA->prefix) {
   5685  1.1  tron       newA->xmlns = oldA->xmlns;
   5686  1.1  tron       if (oldA->prefix == &oldDtd->defaultPrefix)
   5687  1.1  tron         newA->prefix = &newDtd->defaultPrefix;
   5688  1.1  tron       else
   5689  1.1  tron         newA->prefix = (PREFIX *)lookup(&(newDtd->prefixes),
   5690  1.1  tron                                         oldA->prefix->name, 0);
   5691  1.1  tron     }
   5692  1.1  tron   }
   5693  1.1  tron 
   5694  1.1  tron   /* Copy the element type table. */
   5695  1.1  tron 
   5696  1.1  tron   hashTableIterInit(&iter, &(oldDtd->elementTypes));
   5697  1.1  tron 
   5698  1.1  tron   for (;;) {
   5699  1.1  tron     int i;
   5700  1.1  tron     ELEMENT_TYPE *newE;
   5701  1.1  tron     const XML_Char *name;
   5702  1.1  tron     const ELEMENT_TYPE *oldE = (ELEMENT_TYPE *)hashTableIterNext(&iter);
   5703  1.1  tron     if (!oldE)
   5704  1.1  tron       break;
   5705  1.1  tron     name = poolCopyString(&(newDtd->pool), oldE->name);
   5706  1.1  tron     if (!name)
   5707  1.1  tron       return 0;
   5708  1.1  tron     newE = (ELEMENT_TYPE *)lookup(&(newDtd->elementTypes), name,
   5709  1.1  tron                                   sizeof(ELEMENT_TYPE));
   5710  1.1  tron     if (!newE)
   5711  1.1  tron       return 0;
   5712  1.1  tron     if (oldE->nDefaultAtts) {
   5713  1.1  tron       newE->defaultAtts = (DEFAULT_ATTRIBUTE *)
   5714  1.1  tron           ms->malloc_fcn(oldE->nDefaultAtts * sizeof(DEFAULT_ATTRIBUTE));
   5715  1.1  tron       if (!newE->defaultAtts) {
   5716  1.1  tron         ms->free_fcn(newE);
   5717  1.1  tron         return 0;
   5718  1.1  tron       }
   5719  1.1  tron     }
   5720  1.1  tron     if (oldE->idAtt)
   5721  1.1  tron       newE->idAtt = (ATTRIBUTE_ID *)
   5722  1.1  tron           lookup(&(newDtd->attributeIds), oldE->idAtt->name, 0);
   5723  1.1  tron     newE->allocDefaultAtts = newE->nDefaultAtts = oldE->nDefaultAtts;
   5724  1.1  tron     if (oldE->prefix)
   5725  1.1  tron       newE->prefix = (PREFIX *)lookup(&(newDtd->prefixes),
   5726  1.1  tron                                       oldE->prefix->name, 0);
   5727  1.1  tron     for (i = 0; i < newE->nDefaultAtts; i++) {
   5728  1.1  tron       newE->defaultAtts[i].id = (ATTRIBUTE_ID *)
   5729  1.1  tron           lookup(&(newDtd->attributeIds), oldE->defaultAtts[i].id->name, 0);
   5730  1.1  tron       newE->defaultAtts[i].isCdata = oldE->defaultAtts[i].isCdata;
   5731  1.1  tron       if (oldE->defaultAtts[i].value) {
   5732  1.1  tron         newE->defaultAtts[i].value
   5733  1.1  tron             = poolCopyString(&(newDtd->pool), oldE->defaultAtts[i].value);
   5734  1.1  tron         if (!newE->defaultAtts[i].value)
   5735  1.1  tron           return 0;
   5736  1.1  tron       }
   5737  1.1  tron       else
   5738  1.1  tron         newE->defaultAtts[i].value = NULL;
   5739  1.1  tron     }
   5740  1.1  tron   }
   5741  1.1  tron 
   5742  1.1  tron   /* Copy the entity tables. */
   5743  1.1  tron   if (!copyEntityTable(&(newDtd->generalEntities),
   5744  1.1  tron                        &(newDtd->pool),
   5745  1.1  tron                        &(oldDtd->generalEntities)))
   5746  1.1  tron       return 0;
   5747  1.1  tron 
   5748  1.1  tron #ifdef XML_DTD
   5749  1.1  tron   if (!copyEntityTable(&(newDtd->paramEntities),
   5750  1.1  tron                        &(newDtd->pool),
   5751  1.1  tron                        &(oldDtd->paramEntities)))
   5752  1.1  tron       return 0;
   5753  1.1  tron   newDtd->paramEntityRead = oldDtd->paramEntityRead;
   5754  1.1  tron #endif /* XML_DTD */
   5755  1.1  tron 
   5756  1.1  tron   newDtd->keepProcessing = oldDtd->keepProcessing;
   5757  1.1  tron   newDtd->hasParamEntityRefs = oldDtd->hasParamEntityRefs;
   5758  1.1  tron   newDtd->standalone = oldDtd->standalone;
   5759  1.1  tron 
   5760  1.1  tron   /* Don't want deep copying for scaffolding */
   5761  1.1  tron   newDtd->in_eldecl = oldDtd->in_eldecl;
   5762  1.1  tron   newDtd->scaffold = oldDtd->scaffold;
   5763  1.1  tron   newDtd->contentStringLen = oldDtd->contentStringLen;
   5764  1.1  tron   newDtd->scaffSize = oldDtd->scaffSize;
   5765  1.1  tron   newDtd->scaffLevel = oldDtd->scaffLevel;
   5766  1.1  tron   newDtd->scaffIndex = oldDtd->scaffIndex;
   5767  1.1  tron 
   5768  1.1  tron   return 1;
   5769  1.1  tron }  /* End dtdCopy */
   5770  1.1  tron 
   5771  1.1  tron static int
   5772  1.1  tron copyEntityTable(HASH_TABLE *newTable,
   5773  1.1  tron                 STRING_POOL *newPool,
   5774  1.1  tron                 const HASH_TABLE *oldTable)
   5775  1.1  tron {
   5776  1.1  tron   HASH_TABLE_ITER iter;
   5777  1.1  tron   const XML_Char *cachedOldBase = NULL;
   5778  1.1  tron   const XML_Char *cachedNewBase = NULL;
   5779  1.1  tron 
   5780  1.1  tron   hashTableIterInit(&iter, oldTable);
   5781  1.1  tron 
   5782  1.1  tron   for (;;) {
   5783  1.1  tron     ENTITY *newE;
   5784  1.1  tron     const XML_Char *name;
   5785  1.1  tron     const ENTITY *oldE = (ENTITY *)hashTableIterNext(&iter);
   5786  1.1  tron     if (!oldE)
   5787  1.1  tron       break;
   5788  1.1  tron     name = poolCopyString(newPool, oldE->name);
   5789  1.1  tron     if (!name)
   5790  1.1  tron       return 0;
   5791  1.1  tron     newE = (ENTITY *)lookup(newTable, name, sizeof(ENTITY));
   5792  1.1  tron     if (!newE)
   5793  1.1  tron       return 0;
   5794  1.1  tron     if (oldE->systemId) {
   5795  1.1  tron       const XML_Char *tem = poolCopyString(newPool, oldE->systemId);
   5796  1.1  tron       if (!tem)
   5797  1.1  tron         return 0;
   5798  1.1  tron       newE->systemId = tem;
   5799  1.1  tron       if (oldE->base) {
   5800  1.1  tron         if (oldE->base == cachedOldBase)
   5801  1.1  tron           newE->base = cachedNewBase;
   5802  1.1  tron         else {
   5803  1.1  tron           cachedOldBase = oldE->base;
   5804  1.1  tron           tem = poolCopyString(newPool, cachedOldBase);
   5805  1.1  tron           if (!tem)
   5806  1.1  tron             return 0;
   5807  1.1  tron           cachedNewBase = newE->base = tem;
   5808  1.1  tron         }
   5809  1.1  tron       }
   5810  1.1  tron       if (oldE->publicId) {
   5811  1.1  tron         tem = poolCopyString(newPool, oldE->publicId);
   5812  1.1  tron         if (!tem)
   5813  1.1  tron           return 0;
   5814  1.1  tron         newE->publicId = tem;
   5815  1.1  tron       }
   5816  1.1  tron     }
   5817  1.1  tron     else {
   5818  1.1  tron       const XML_Char *tem = poolCopyStringN(newPool, oldE->textPtr,
   5819  1.1  tron                                             oldE->textLen);
   5820  1.1  tron       if (!tem)
   5821  1.1  tron         return 0;
   5822  1.1  tron       newE->textPtr = tem;
   5823  1.1  tron       newE->textLen = oldE->textLen;
   5824  1.1  tron     }
   5825  1.1  tron     if (oldE->notation) {
   5826  1.1  tron       const XML_Char *tem = poolCopyString(newPool, oldE->notation);
   5827  1.1  tron       if (!tem)
   5828  1.1  tron         return 0;
   5829  1.1  tron       newE->notation = tem;
   5830  1.1  tron     }
   5831  1.1  tron     newE->is_param = oldE->is_param;
   5832  1.1  tron     newE->is_internal = oldE->is_internal;
   5833  1.1  tron   }
   5834  1.1  tron   return 1;
   5835  1.1  tron }
   5836  1.1  tron 
   5837  1.1  tron #define INIT_POWER 6
   5838  1.1  tron 
   5839  1.1  tron static XML_Bool FASTCALL
   5840  1.1  tron keyeq(KEY s1, KEY s2)
   5841  1.1  tron {
   5842  1.1  tron   for (; *s1 == *s2; s1++, s2++)
   5843  1.1  tron     if (*s1 == 0)
   5844  1.1  tron       return XML_TRUE;
   5845  1.1  tron   return XML_FALSE;
   5846  1.1  tron }
   5847  1.1  tron 
   5848  1.1  tron static unsigned long FASTCALL
   5849  1.1  tron hash(KEY s)
   5850  1.1  tron {
   5851  1.1  tron   unsigned long h = 0;
   5852  1.1  tron   while (*s)
   5853  1.1  tron     h = CHAR_HASH(h, *s++);
   5854  1.1  tron   return h;
   5855  1.1  tron }
   5856  1.1  tron 
   5857  1.1  tron static NAMED *
   5858  1.1  tron lookup(HASH_TABLE *table, KEY name, size_t createSize)
   5859  1.1  tron {
   5860  1.1  tron   size_t i;
   5861  1.1  tron   if (table->size == 0) {
   5862  1.1  tron     size_t tsize;
   5863  1.1  tron     if (!createSize)
   5864  1.1  tron       return NULL;
   5865  1.1  tron     table->power = INIT_POWER;
   5866  1.1  tron     /* table->size is a power of 2 */
   5867  1.1  tron     table->size = (size_t)1 << INIT_POWER;
   5868  1.1  tron     tsize = table->size * sizeof(NAMED *);
   5869  1.1  tron     table->v = (NAMED **)table->mem->malloc_fcn(tsize);
   5870  1.1  tron     if (!table->v) {
   5871  1.1  tron       table->size = 0;
   5872  1.1  tron       return NULL;
   5873  1.1  tron     }
   5874  1.1  tron     memset(table->v, 0, tsize);
   5875  1.1  tron     i = hash(name) & ((unsigned long)table->size - 1);
   5876  1.1  tron   }
   5877  1.1  tron   else {
   5878  1.1  tron     unsigned long h = hash(name);
   5879  1.1  tron     unsigned long mask = (unsigned long)table->size - 1;
   5880  1.1  tron     unsigned char step = 0;
   5881  1.1  tron     i = h & mask;
   5882  1.1  tron     while (table->v[i]) {
   5883  1.1  tron       if (keyeq(name, table->v[i]->name))
   5884  1.1  tron         return table->v[i];
   5885  1.1  tron       if (!step)
   5886  1.1  tron         step = PROBE_STEP(h, mask, table->power);
   5887  1.1  tron       i < step ? (i += table->size - step) : (i -= step);
   5888  1.1  tron     }
   5889  1.1  tron     if (!createSize)
   5890  1.1  tron       return NULL;
   5891  1.1  tron 
   5892  1.1  tron     /* check for overflow (table is half full) */
   5893  1.1  tron     if (table->used >> (table->power - 1)) {
   5894  1.1  tron       unsigned char newPower = table->power + 1;
   5895  1.1  tron       size_t newSize = (size_t)1 << newPower;
   5896  1.1  tron       unsigned long newMask = (unsigned long)newSize - 1;
   5897  1.1  tron       size_t tsize = newSize * sizeof(NAMED *);
   5898  1.1  tron       NAMED **newV = (NAMED **)table->mem->malloc_fcn(tsize);
   5899  1.1  tron       if (!newV)
   5900  1.1  tron         return NULL;
   5901  1.1  tron       memset(newV, 0, tsize);
   5902  1.1  tron       for (i = 0; i < table->size; i++)
   5903  1.1  tron         if (table->v[i]) {
   5904  1.1  tron           unsigned long newHash = hash(table->v[i]->name);
   5905  1.1  tron           size_t j = newHash & newMask;
   5906  1.1  tron           step = 0;
   5907  1.1  tron           while (newV[j]) {
   5908  1.1  tron             if (!step)
   5909  1.1  tron               step = PROBE_STEP(newHash, newMask, newPower);
   5910  1.1  tron             j < step ? (j += newSize - step) : (j -= step);
   5911  1.1  tron           }
   5912  1.1  tron           newV[j] = table->v[i];
   5913  1.1  tron         }
   5914  1.1  tron       table->mem->free_fcn(table->v);
   5915  1.1  tron       table->v = newV;
   5916  1.1  tron       table->power = newPower;
   5917  1.1  tron       table->size = newSize;
   5918  1.1  tron       i = h & newMask;
   5919  1.1  tron       step = 0;
   5920  1.1  tron       while (table->v[i]) {
   5921  1.1  tron         if (!step)
   5922  1.1  tron           step = PROBE_STEP(h, newMask, newPower);
   5923  1.1  tron         i < step ? (i += newSize - step) : (i -= step);
   5924  1.1  tron       }
   5925  1.1  tron     }
   5926  1.1  tron   }
   5927  1.1  tron   table->v[i] = (NAMED *)table->mem->malloc_fcn(createSize);
   5928  1.1  tron   if (!table->v[i])
   5929  1.1  tron     return NULL;
   5930  1.1  tron   memset(table->v[i], 0, createSize);
   5931  1.1  tron   table->v[i]->name = name;
   5932  1.1  tron   (table->used)++;
   5933  1.1  tron   return table->v[i];
   5934  1.1  tron }
   5935  1.1  tron 
   5936  1.1  tron static void FASTCALL
   5937  1.1  tron hashTableClear(HASH_TABLE *table)
   5938  1.1  tron {
   5939  1.1  tron   size_t i;
   5940  1.1  tron   for (i = 0; i < table->size; i++) {
   5941  1.1  tron     table->mem->free_fcn(table->v[i]);
   5942  1.1  tron     table->v[i] = NULL;
   5943  1.1  tron   }
   5944  1.1  tron   table->used = 0;
   5945  1.1  tron }
   5946  1.1  tron 
   5947  1.1  tron static void FASTCALL
   5948  1.1  tron hashTableDestroy(HASH_TABLE *table)
   5949  1.1  tron {
   5950  1.1  tron   size_t i;
   5951  1.1  tron   for (i = 0; i < table->size; i++)
   5952  1.1  tron     table->mem->free_fcn(table->v[i]);
   5953  1.1  tron   table->mem->free_fcn(table->v);
   5954  1.1  tron }
   5955  1.1  tron 
   5956  1.1  tron static void FASTCALL
   5957  1.1  tron hashTableInit(HASH_TABLE *p, const XML_Memory_Handling_Suite *ms)
   5958  1.1  tron {
   5959  1.1  tron   p->power = 0;
   5960  1.1  tron   p->size = 0;
   5961  1.1  tron   p->used = 0;
   5962  1.1  tron   p->v = NULL;
   5963  1.1  tron   p->mem = ms;
   5964  1.1  tron }
   5965  1.1  tron 
   5966  1.1  tron static void FASTCALL
   5967  1.1  tron hashTableIterInit(HASH_TABLE_ITER *iter, const HASH_TABLE *table)
   5968  1.1  tron {
   5969  1.1  tron   iter->p = table->v;
   5970  1.1  tron   iter->end = iter->p + table->size;
   5971  1.1  tron }
   5972  1.1  tron 
   5973  1.1  tron static NAMED * FASTCALL
   5974  1.1  tron hashTableIterNext(HASH_TABLE_ITER *iter)
   5975  1.1  tron {
   5976  1.1  tron   while (iter->p != iter->end) {
   5977  1.1  tron     NAMED *tem = *(iter->p)++;
   5978  1.1  tron     if (tem)
   5979  1.1  tron       return tem;
   5980  1.1  tron   }
   5981  1.1  tron   return NULL;
   5982  1.1  tron }
   5983  1.1  tron 
   5984  1.1  tron static void FASTCALL
   5985  1.1  tron poolInit(STRING_POOL *pool, const XML_Memory_Handling_Suite *ms)
   5986  1.1  tron {
   5987  1.1  tron   pool->blocks = NULL;
   5988  1.1  tron   pool->freeBlocks = NULL;
   5989  1.1  tron   pool->start = NULL;
   5990  1.1  tron   pool->ptr = NULL;
   5991  1.1  tron   pool->end = NULL;
   5992  1.1  tron   pool->mem = ms;
   5993  1.1  tron }
   5994  1.1  tron 
   5995  1.1  tron static void FASTCALL
   5996  1.1  tron poolClear(STRING_POOL *pool)
   5997  1.1  tron {
   5998  1.1  tron   if (!pool->freeBlocks)
   5999  1.1  tron     pool->freeBlocks = pool->blocks;
   6000  1.1  tron   else {
   6001  1.1  tron     BLOCK *p = pool->blocks;
   6002  1.1  tron     while (p) {
   6003  1.1  tron       BLOCK *tem = p->next;
   6004  1.1  tron       p->next = pool->freeBlocks;
   6005  1.1  tron       pool->freeBlocks = p;
   6006  1.1  tron       p = tem;
   6007  1.1  tron     }
   6008  1.1  tron   }
   6009  1.1  tron   pool->blocks = NULL;
   6010  1.1  tron   pool->start = NULL;
   6011  1.1  tron   pool->ptr = NULL;
   6012  1.1  tron   pool->end = NULL;
   6013  1.1  tron }
   6014  1.1  tron 
   6015  1.1  tron static void FASTCALL
   6016  1.1  tron poolDestroy(STRING_POOL *pool)
   6017  1.1  tron {
   6018  1.1  tron   BLOCK *p = pool->blocks;
   6019  1.1  tron   while (p) {
   6020  1.1  tron     BLOCK *tem = p->next;
   6021  1.1  tron     pool->mem->free_fcn(p);
   6022  1.1  tron     p = tem;
   6023  1.1  tron   }
   6024  1.1  tron   p = pool->freeBlocks;
   6025  1.1  tron   while (p) {
   6026  1.1  tron     BLOCK *tem = p->next;
   6027  1.1  tron     pool->mem->free_fcn(p);
   6028  1.1  tron     p = tem;
   6029  1.1  tron   }
   6030  1.1  tron }
   6031  1.1  tron 
   6032  1.1  tron static XML_Char *
   6033  1.1  tron poolAppend(STRING_POOL *pool, const ENCODING *enc,
   6034  1.1  tron            const char *ptr, const char *end)
   6035  1.1  tron {
   6036  1.1  tron   if (!pool->ptr && !poolGrow(pool))
   6037  1.1  tron     return NULL;
   6038  1.1  tron   for (;;) {
   6039  1.1  tron     XmlConvert(enc, &ptr, end, (ICHAR **)&(pool->ptr), (ICHAR *)pool->end);
   6040  1.1  tron     if (ptr == end)
   6041  1.1  tron       break;
   6042  1.1  tron     if (!poolGrow(pool))
   6043  1.1  tron       return NULL;
   6044  1.1  tron   }
   6045  1.1  tron   return pool->start;
   6046  1.1  tron }
   6047  1.1  tron 
   6048  1.1  tron static const XML_Char * FASTCALL
   6049  1.1  tron poolCopyString(STRING_POOL *pool, const XML_Char *s)
   6050  1.1  tron {
   6051  1.1  tron   do {
   6052  1.1  tron     if (!poolAppendChar(pool, *s))
   6053  1.1  tron       return NULL;
   6054  1.1  tron   } while (*s++);
   6055  1.1  tron   s = pool->start;
   6056  1.1  tron   poolFinish(pool);
   6057  1.1  tron   return s;
   6058  1.1  tron }
   6059  1.1  tron 
   6060  1.1  tron static const XML_Char *
   6061  1.1  tron poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n)
   6062  1.1  tron {
   6063  1.1  tron   if (!pool->ptr && !poolGrow(pool))
   6064  1.1  tron     return NULL;
   6065  1.1  tron   for (; n > 0; --n, s++) {
   6066  1.1  tron     if (!poolAppendChar(pool, *s))
   6067  1.1  tron       return NULL;
   6068  1.1  tron   }
   6069  1.1  tron   s = pool->start;
   6070  1.1  tron   poolFinish(pool);
   6071  1.1  tron   return s;
   6072  1.1  tron }
   6073  1.1  tron 
   6074  1.1  tron static const XML_Char * FASTCALL
   6075  1.1  tron poolAppendString(STRING_POOL *pool, const XML_Char *s)
   6076  1.1  tron {
   6077  1.1  tron   while (*s) {
   6078  1.1  tron     if (!poolAppendChar(pool, *s))
   6079  1.1  tron       return NULL;
   6080  1.1  tron     s++;
   6081  1.1  tron   }
   6082  1.1  tron   return pool->start;
   6083  1.1  tron }
   6084  1.1  tron 
   6085  1.1  tron static XML_Char *
   6086  1.1  tron poolStoreString(STRING_POOL *pool, const ENCODING *enc,
   6087  1.1  tron                 const char *ptr, const char *end)
   6088  1.1  tron {
   6089  1.1  tron   if (!poolAppend(pool, enc, ptr, end))
   6090  1.1  tron     return NULL;
   6091  1.1  tron   if (pool->ptr == pool->end && !poolGrow(pool))
   6092  1.1  tron     return NULL;
   6093  1.1  tron   *(pool->ptr)++ = 0;
   6094  1.1  tron   return pool->start;
   6095  1.1  tron }
   6096  1.1  tron 
   6097  1.1  tron static XML_Bool FASTCALL
   6098  1.1  tron poolGrow(STRING_POOL *pool)
   6099  1.1  tron {
   6100  1.1  tron   if (pool->freeBlocks) {
   6101  1.1  tron     if (pool->start == 0) {
   6102  1.1  tron       pool->blocks = pool->freeBlocks;
   6103  1.1  tron       pool->freeBlocks = pool->freeBlocks->next;
   6104  1.1  tron       pool->blocks->next = NULL;
   6105  1.1  tron       pool->start = pool->blocks->s;
   6106  1.1  tron       pool->end = pool->start + pool->blocks->size;
   6107  1.1  tron       pool->ptr = pool->start;
   6108  1.1  tron       return XML_TRUE;
   6109  1.1  tron     }
   6110  1.1  tron     if (pool->end - pool->start < pool->freeBlocks->size) {
   6111  1.1  tron       BLOCK *tem = pool->freeBlocks->next;
   6112  1.1  tron       pool->freeBlocks->next = pool->blocks;
   6113  1.1  tron       pool->blocks = pool->freeBlocks;
   6114  1.1  tron       pool->freeBlocks = tem;
   6115  1.1  tron       memcpy(pool->blocks->s, pool->start,
   6116  1.1  tron              (pool->end - pool->start) * sizeof(XML_Char));
   6117  1.1  tron       pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
   6118  1.1  tron       pool->start = pool->blocks->s;
   6119  1.1  tron       pool->end = pool->start + pool->blocks->size;
   6120  1.1  tron       return XML_TRUE;
   6121  1.1  tron     }
   6122  1.1  tron   }
   6123  1.1  tron   if (pool->blocks && pool->start == pool->blocks->s) {
   6124  1.1  tron     int blockSize = (int)(pool->end - pool->start)*2;
   6125  1.1  tron     pool->blocks = (BLOCK *)
   6126  1.1  tron       pool->mem->realloc_fcn(pool->blocks,
   6127  1.1  tron                              (offsetof(BLOCK, s)
   6128  1.1  tron                               + blockSize * sizeof(XML_Char)));
   6129  1.1  tron     if (pool->blocks == NULL)
   6130  1.1  tron       return XML_FALSE;
   6131  1.1  tron     pool->blocks->size = blockSize;
   6132  1.1  tron     pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
   6133  1.1  tron     pool->start = pool->blocks->s;
   6134  1.1  tron     pool->end = pool->start + blockSize;
   6135  1.1  tron   }
   6136  1.1  tron   else {
   6137  1.1  tron     BLOCK *tem;
   6138  1.1  tron     int blockSize = (int)(pool->end - pool->start);
   6139  1.1  tron     if (blockSize < INIT_BLOCK_SIZE)
   6140  1.1  tron       blockSize = INIT_BLOCK_SIZE;
   6141  1.1  tron     else
   6142  1.1  tron       blockSize *= 2;
   6143  1.1  tron     tem = (BLOCK *)pool->mem->malloc_fcn(offsetof(BLOCK, s)
   6144  1.1  tron                                         + blockSize * sizeof(XML_Char));
   6145  1.1  tron     if (!tem)
   6146  1.1  tron       return XML_FALSE;
   6147  1.1  tron     tem->size = blockSize;
   6148  1.1  tron     tem->next = pool->blocks;
   6149  1.1  tron     pool->blocks = tem;
   6150  1.1  tron     if (pool->ptr != pool->start)
   6151  1.1  tron       memcpy(tem->s, pool->start,
   6152  1.1  tron              (pool->ptr - pool->start) * sizeof(XML_Char));
   6153  1.1  tron     pool->ptr = tem->s + (pool->ptr - pool->start);
   6154  1.1  tron     pool->start = tem->s;
   6155  1.1  tron     pool->end = tem->s + blockSize;
   6156  1.1  tron   }
   6157  1.1  tron   return XML_TRUE;
   6158  1.1  tron }
   6159  1.1  tron 
   6160  1.1  tron static int FASTCALL
   6161  1.1  tron nextScaffoldPart(XML_Parser parser)
   6162  1.1  tron {
   6163  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   6164  1.1  tron   CONTENT_SCAFFOLD * me;
   6165  1.1  tron   int next;
   6166  1.1  tron 
   6167  1.1  tron   if (!dtd->scaffIndex) {
   6168  1.1  tron     dtd->scaffIndex = (int *)MALLOC(groupSize * sizeof(int));
   6169  1.1  tron     if (!dtd->scaffIndex)
   6170  1.1  tron       return -1;
   6171  1.1  tron     dtd->scaffIndex[0] = 0;
   6172  1.1  tron   }
   6173  1.1  tron 
   6174  1.1  tron   if (dtd->scaffCount >= dtd->scaffSize) {
   6175  1.1  tron     CONTENT_SCAFFOLD *temp;
   6176  1.1  tron     if (dtd->scaffold) {
   6177  1.1  tron       temp = (CONTENT_SCAFFOLD *)
   6178  1.1  tron         REALLOC(dtd->scaffold, dtd->scaffSize * 2 * sizeof(CONTENT_SCAFFOLD));
   6179  1.1  tron       if (temp == NULL)
   6180  1.1  tron         return -1;
   6181  1.1  tron       dtd->scaffSize *= 2;
   6182  1.1  tron     }
   6183  1.1  tron     else {
   6184  1.1  tron       temp = (CONTENT_SCAFFOLD *)MALLOC(INIT_SCAFFOLD_ELEMENTS
   6185  1.1  tron                                         * sizeof(CONTENT_SCAFFOLD));
   6186  1.1  tron       if (temp == NULL)
   6187  1.1  tron         return -1;
   6188  1.1  tron       dtd->scaffSize = INIT_SCAFFOLD_ELEMENTS;
   6189  1.1  tron     }
   6190  1.1  tron     dtd->scaffold = temp;
   6191  1.1  tron   }
   6192  1.1  tron   next = dtd->scaffCount++;
   6193  1.1  tron   me = &dtd->scaffold[next];
   6194  1.1  tron   if (dtd->scaffLevel) {
   6195  1.1  tron     CONTENT_SCAFFOLD *parent = &dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel-1]];
   6196  1.1  tron     if (parent->lastchild) {
   6197  1.1  tron       dtd->scaffold[parent->lastchild].nextsib = next;
   6198  1.1  tron     }
   6199  1.1  tron     if (!parent->childcnt)
   6200  1.1  tron       parent->firstchild = next;
   6201  1.1  tron     parent->lastchild = next;
   6202  1.1  tron     parent->childcnt++;
   6203  1.1  tron   }
   6204  1.1  tron   me->firstchild = me->lastchild = me->childcnt = me->nextsib = 0;
   6205  1.1  tron   return next;
   6206  1.1  tron }
   6207  1.1  tron 
   6208  1.1  tron static void
   6209  1.1  tron build_node(XML_Parser parser,
   6210  1.1  tron            int src_node,
   6211  1.1  tron            XML_Content *dest,
   6212  1.1  tron            XML_Content **contpos,
   6213  1.1  tron            XML_Char **strpos)
   6214  1.1  tron {
   6215  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   6216  1.1  tron   dest->type = dtd->scaffold[src_node].type;
   6217  1.1  tron   dest->quant = dtd->scaffold[src_node].quant;
   6218  1.1  tron   if (dest->type == XML_CTYPE_NAME) {
   6219  1.1  tron     const XML_Char *src;
   6220  1.1  tron     dest->name = *strpos;
   6221  1.1  tron     src = dtd->scaffold[src_node].name;
   6222  1.1  tron     for (;;) {
   6223  1.1  tron       *(*strpos)++ = *src;
   6224  1.1  tron       if (!*src)
   6225  1.1  tron         break;
   6226  1.1  tron       src++;
   6227  1.1  tron     }
   6228  1.1  tron     dest->numchildren = 0;
   6229  1.1  tron     dest->children = NULL;
   6230  1.1  tron   }
   6231  1.1  tron   else {
   6232  1.1  tron     unsigned int i;
   6233  1.1  tron     int cn;
   6234  1.1  tron     dest->numchildren = dtd->scaffold[src_node].childcnt;
   6235  1.1  tron     dest->children = *contpos;
   6236  1.1  tron     *contpos += dest->numchildren;
   6237  1.1  tron     for (i = 0, cn = dtd->scaffold[src_node].firstchild;
   6238  1.1  tron          i < dest->numchildren;
   6239  1.1  tron          i++, cn = dtd->scaffold[cn].nextsib) {
   6240  1.1  tron       build_node(parser, cn, &(dest->children[i]), contpos, strpos);
   6241  1.1  tron     }
   6242  1.1  tron     dest->name = NULL;
   6243  1.1  tron   }
   6244  1.1  tron }
   6245  1.1  tron 
   6246  1.1  tron static XML_Content *
   6247  1.1  tron build_model (XML_Parser parser)
   6248  1.1  tron {
   6249  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   6250  1.1  tron   XML_Content *ret;
   6251  1.1  tron   XML_Content *cpos;
   6252  1.1  tron   XML_Char * str;
   6253  1.1  tron   int allocsize = (dtd->scaffCount * sizeof(XML_Content)
   6254  1.1  tron                    + (dtd->contentStringLen * sizeof(XML_Char)));
   6255  1.1  tron 
   6256  1.1  tron   ret = (XML_Content *)MALLOC(allocsize);
   6257  1.1  tron   if (!ret)
   6258  1.1  tron     return NULL;
   6259  1.1  tron 
   6260  1.1  tron   str =  (XML_Char *) (&ret[dtd->scaffCount]);
   6261  1.1  tron   cpos = &ret[1];
   6262  1.1  tron 
   6263  1.1  tron   build_node(parser, 0, ret, &cpos, &str);
   6264  1.1  tron   return ret;
   6265  1.1  tron }
   6266  1.1  tron 
   6267  1.1  tron static ELEMENT_TYPE *
   6268  1.1  tron getElementType(XML_Parser parser,
   6269  1.1  tron                const ENCODING *enc,
   6270  1.1  tron                const char *ptr,
   6271  1.1  tron                const char *end)
   6272  1.1  tron {
   6273  1.1  tron   DTD * const dtd = _dtd;  /* save one level of indirection */
   6274  1.1  tron   const XML_Char *name = poolStoreString(&dtd->pool, enc, ptr, end);
   6275  1.1  tron   ELEMENT_TYPE *ret;
   6276  1.1  tron 
   6277  1.1  tron   if (!name)
   6278  1.1  tron     return NULL;
   6279  1.1  tron   ret = (ELEMENT_TYPE *) lookup(&dtd->elementTypes, name, sizeof(ELEMENT_TYPE));
   6280  1.1  tron   if (!ret)
   6281  1.1  tron     return NULL;
   6282  1.1  tron   if (ret->name != name)
   6283  1.1  tron     poolDiscard(&dtd->pool);
   6284  1.1  tron   else {
   6285  1.1  tron     poolFinish(&dtd->pool);
   6286  1.1  tron     if (!setElementTypePrefix(parser, ret))
   6287  1.1  tron       return NULL;
   6288  1.1  tron   }
   6289  1.1  tron   return ret;
   6290  1.1  tron }
   6291