GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libexpat/lib/xmlparse.c Lines: 3243 3373 96.1 %
Date: 2017-11-07 Branches: 1998 2329 85.8 %

Line Branch Exec Source
1
/* 8c6b2be7c6281da65ce05218fc15c339f02a811706340824ab596aa86e1fd51a (2.2.4+)
2
                            __  __            _
3
                         ___\ \/ /_ __   __ _| |_
4
                        / _ \\  /| '_ \ / _` | __|
5
                       |  __//  \| |_) | (_| | |_
6
                        \___/_/\_\ .__/ \__,_|\__|
7
                                 |_| XML parser
8
9
   Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10
   Copyright (c) 2000-2017 Expat development team
11
   Licensed under the MIT license:
12
13
   Permission is  hereby granted,  free of charge,  to any  person obtaining
14
   a  copy  of  this  software   and  associated  documentation  files  (the
15
   "Software"),  to  deal in  the  Software  without restriction,  including
16
   without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17
   distribute, sublicense, and/or sell copies of the Software, and to permit
18
   persons  to whom  the Software  is  furnished to  do so,  subject to  the
19
   following conditions:
20
21
   The above copyright  notice and this permission notice  shall be included
22
   in all copies or substantial portions of the Software.
23
24
   THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25
   EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27
   NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28
   DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29
   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30
   USE OR OTHER DEALINGS IN THE SOFTWARE.
31
*/
32
33
#if !defined(_GNU_SOURCE)
34
# define _GNU_SOURCE 1                  /* syscall prototype */
35
#endif
36
37
#include <stddef.h>
38
#include <string.h>                     /* memset(), memcpy() */
39
#include <assert.h>
40
#include <limits.h>                     /* UINT_MAX */
41
#include <stdio.h>                      /* fprintf */
42
#include <stdlib.h>                     /* getenv */
43
44
#ifdef _WIN32
45
#define getpid GetCurrentProcessId
46
#else
47
#include <sys/time.h>                   /* gettimeofday() */
48
#include <sys/types.h>                  /* getpid() */
49
#include <unistd.h>                     /* getpid() */
50
#include <fcntl.h>                      /* O_RDONLY */
51
#include <errno.h>
52
#endif
53
54
#define XML_BUILDING_EXPAT 1
55
56
#ifdef _WIN32
57
#include "winconfig.h"
58
#elif defined(HAVE_EXPAT_CONFIG_H)
59
#include <expat_config.h>
60
#endif /* ndef _WIN32 */
61
62
#include "ascii.h"
63
#include "expat.h"
64
#include "siphash.h"
65
66
#ifdef XML_UNICODE
67
#define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
68
#define XmlConvert XmlUtf16Convert
69
#define XmlGetInternalEncoding XmlGetUtf16InternalEncoding
70
#define XmlGetInternalEncodingNS XmlGetUtf16InternalEncodingNS
71
#define XmlEncode XmlUtf16Encode
72
/* Using pointer subtraction to convert to integer type. */
73
#define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((char *)(s) - (char *)NULL) & 1))
74
typedef unsigned short ICHAR;
75
#else
76
#define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
77
#define XmlConvert XmlUtf8Convert
78
#define XmlGetInternalEncoding XmlGetUtf8InternalEncoding
79
#define XmlGetInternalEncodingNS XmlGetUtf8InternalEncodingNS
80
#define XmlEncode XmlUtf8Encode
81
#define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
82
typedef char ICHAR;
83
#endif
84
85
86
#ifndef XML_NS
87
88
#define XmlInitEncodingNS XmlInitEncoding
89
#define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
90
#undef XmlGetInternalEncodingNS
91
#define XmlGetInternalEncodingNS XmlGetInternalEncoding
92
#define XmlParseXmlDeclNS XmlParseXmlDecl
93
94
#endif
95
96
#ifdef XML_UNICODE
97
98
#ifdef XML_UNICODE_WCHAR_T
99
#define XML_T(x) (const wchar_t)x
100
#define XML_L(x) L ## x
101
#else
102
#define XML_T(x) (const unsigned short)x
103
#define XML_L(x) x
104
#endif
105
106
#else
107
108
#define XML_T(x) x
109
#define XML_L(x) x
110
111
#endif
112
113
/* Round up n to be a multiple of sz, where sz is a power of 2. */
114
#define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
115
116
/* Handle the case where memmove() doesn't exist. */
117
#ifndef HAVE_MEMMOVE
118
#ifdef HAVE_BCOPY
119
#define memmove(d,s,l) bcopy((s),(d),(l))
120
#else
121
#error memmove does not exist on this platform, nor is a substitute available
122
#endif /* HAVE_BCOPY */
123
#endif /* HAVE_MEMMOVE */
124
125
#include "internal.h"
126
#include "xmltok.h"
127
#include "xmlrole.h"
128
129
typedef const XML_Char *KEY;
130
131
typedef struct {
132
  KEY name;
133
} NAMED;
134
135
typedef struct {
136
  NAMED **v;
137
  unsigned char power;
138
  size_t size;
139
  size_t used;
140
  const XML_Memory_Handling_Suite *mem;
141
} HASH_TABLE;
142
143
static size_t
144
keylen(KEY s);
145
146
static void
147
copy_salt_to_sipkey(XML_Parser parser, struct sipkey * key);
148
149
/* For probing (after a collision) we need a step size relative prime
150
   to the hash table size, which is a power of 2. We use double-hashing,
151
   since we can calculate a second hash value cheaply by taking those bits
152
   of the first hash value that were discarded (masked out) when the table
153
   index was calculated: index = hash & mask, where mask = table->size - 1.
154
   We limit the maximum step size to table->size / 4 (mask >> 2) and make
155
   it odd, since odd numbers are always relative prime to a power of 2.
156
*/
157
#define SECOND_HASH(hash, mask, power) \
158
  ((((hash) & ~(mask)) >> ((power) - 1)) & ((mask) >> 2))
159
#define PROBE_STEP(hash, mask, power) \
160
  ((unsigned char)((SECOND_HASH(hash, mask, power)) | 1))
161
162
typedef struct {
163
  NAMED **p;
164
  NAMED **end;
165
} HASH_TABLE_ITER;
166
167
#define INIT_TAG_BUF_SIZE 32  /* must be a multiple of sizeof(XML_Char) */
168
#define INIT_DATA_BUF_SIZE 1024
169
#define INIT_ATTS_SIZE 16
170
#define INIT_ATTS_VERSION 0xFFFFFFFF
171
#define INIT_BLOCK_SIZE 1024
172
#define INIT_BUFFER_SIZE 1024
173
174
#define EXPAND_SPARE 24
175
176
typedef struct binding {
177
  struct prefix *prefix;
178
  struct binding *nextTagBinding;
179
  struct binding *prevPrefixBinding;
180
  const struct attribute_id *attId;
181
  XML_Char *uri;
182
  int uriLen;
183
  int uriAlloc;
184
} BINDING;
185
186
typedef struct prefix {
187
  const XML_Char *name;
188
  BINDING *binding;
189
} PREFIX;
190
191
typedef struct {
192
  const XML_Char *str;
193
  const XML_Char *localPart;
194
  const XML_Char *prefix;
195
  int strLen;
196
  int uriLen;
197
  int prefixLen;
198
} TAG_NAME;
199
200
/* TAG represents an open element.
201
   The name of the element is stored in both the document and API
202
   encodings.  The memory buffer 'buf' is a separately-allocated
203
   memory area which stores the name.  During the XML_Parse()/
204
   XMLParseBuffer() when the element is open, the memory for the 'raw'
205
   version of the name (in the document encoding) is shared with the
206
   document buffer.  If the element is open across calls to
207
   XML_Parse()/XML_ParseBuffer(), the buffer is re-allocated to
208
   contain the 'raw' name as well.
209
210
   A parser re-uses these structures, maintaining a list of allocated
211
   TAG objects in a free list.
212
*/
213
typedef struct tag {
214
  struct tag *parent;           /* parent of this element */
215
  const char *rawName;          /* tagName in the original encoding */
216
  int rawNameLength;
217
  TAG_NAME name;                /* tagName in the API encoding */
218
  char *buf;                    /* buffer for name components */
219
  char *bufEnd;                 /* end of the buffer */
220
  BINDING *bindings;
221
} TAG;
222
223
typedef struct {
224
  const XML_Char *name;
225
  const XML_Char *textPtr;
226
  int textLen;                  /* length in XML_Chars */
227
  int processed;                /* # of processed bytes - when suspended */
228
  const XML_Char *systemId;
229
  const XML_Char *base;
230
  const XML_Char *publicId;
231
  const XML_Char *notation;
232
  XML_Bool open;
233
  XML_Bool is_param;
234
  XML_Bool is_internal; /* true if declared in internal subset outside PE */
235
} ENTITY;
236
237
typedef struct {
238
  enum XML_Content_Type         type;
239
  enum XML_Content_Quant        quant;
240
  const XML_Char *              name;
241
  int                           firstchild;
242
  int                           lastchild;
243
  int                           childcnt;
244
  int                           nextsib;
245
} CONTENT_SCAFFOLD;
246
247
#define INIT_SCAFFOLD_ELEMENTS 32
248
249
typedef struct block {
250
  struct block *next;
251
  int size;
252
  XML_Char s[1];
253
} BLOCK;
254
255
typedef struct {
256
  BLOCK *blocks;
257
  BLOCK *freeBlocks;
258
  const XML_Char *end;
259
  XML_Char *ptr;
260
  XML_Char *start;
261
  const XML_Memory_Handling_Suite *mem;
262
} STRING_POOL;
263
264
/* The XML_Char before the name is used to determine whether
265
   an attribute has been specified. */
266
typedef struct attribute_id {
267
  XML_Char *name;
268
  PREFIX *prefix;
269
  XML_Bool maybeTokenized;
270
  XML_Bool xmlns;
271
} ATTRIBUTE_ID;
272
273
typedef struct {
274
  const ATTRIBUTE_ID *id;
275
  XML_Bool isCdata;
276
  const XML_Char *value;
277
} DEFAULT_ATTRIBUTE;
278
279
typedef struct {
280
  unsigned long version;
281
  unsigned long hash;
282
  const XML_Char *uriName;
283
} NS_ATT;
284
285
typedef struct {
286
  const XML_Char *name;
287
  PREFIX *prefix;
288
  const ATTRIBUTE_ID *idAtt;
289
  int nDefaultAtts;
290
  int allocDefaultAtts;
291
  DEFAULT_ATTRIBUTE *defaultAtts;
292
} ELEMENT_TYPE;
293
294
typedef struct {
295
  HASH_TABLE generalEntities;
296
  HASH_TABLE elementTypes;
297
  HASH_TABLE attributeIds;
298
  HASH_TABLE prefixes;
299
  STRING_POOL pool;
300
  STRING_POOL entityValuePool;
301
  /* false once a parameter entity reference has been skipped */
302
  XML_Bool keepProcessing;
303
  /* true once an internal or external PE reference has been encountered;
304
     this includes the reference to an external subset */
305
  XML_Bool hasParamEntityRefs;
306
  XML_Bool standalone;
307
#ifdef XML_DTD
308
  /* indicates if external PE has been read */
309
  XML_Bool paramEntityRead;
310
  HASH_TABLE paramEntities;
311
#endif /* XML_DTD */
312
  PREFIX defaultPrefix;
313
  /* === scaffolding for building content model === */
314
  XML_Bool in_eldecl;
315
  CONTENT_SCAFFOLD *scaffold;
316
  unsigned contentStringLen;
317
  unsigned scaffSize;
318
  unsigned scaffCount;
319
  int scaffLevel;
320
  int *scaffIndex;
321
} DTD;
322
323
typedef struct open_internal_entity {
324
  const char *internalEventPtr;
325
  const char *internalEventEndPtr;
326
  struct open_internal_entity *next;
327
  ENTITY *entity;
328
  int startTagLevel;
329
  XML_Bool betweenDecl; /* WFC: PE Between Declarations */
330
} OPEN_INTERNAL_ENTITY;
331
332
typedef enum XML_Error PTRCALL Processor(XML_Parser parser,
333
                                         const char *start,
334
                                         const char *end,
335
                                         const char **endPtr);
336
337
static Processor prologProcessor;
338
static Processor prologInitProcessor;
339
static Processor contentProcessor;
340
static Processor cdataSectionProcessor;
341
#ifdef XML_DTD
342
static Processor ignoreSectionProcessor;
343
static Processor externalParEntProcessor;
344
static Processor externalParEntInitProcessor;
345
static Processor entityValueProcessor;
346
static Processor entityValueInitProcessor;
347
#endif /* XML_DTD */
348
static Processor epilogProcessor;
349
static Processor errorProcessor;
350
static Processor externalEntityInitProcessor;
351
static Processor externalEntityInitProcessor2;
352
static Processor externalEntityInitProcessor3;
353
static Processor externalEntityContentProcessor;
354
static Processor internalEntityProcessor;
355
356
static enum XML_Error
357
handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName);
358
static enum XML_Error
359
processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
360
               const char *s, const char *next);
361
static enum XML_Error
362
initializeEncoding(XML_Parser parser);
363
static enum XML_Error
364
doProlog(XML_Parser parser, const ENCODING *enc, const char *s,
365
         const char *end, int tok, const char *next, const char **nextPtr,
366
         XML_Bool haveMore);
367
static enum XML_Error
368
processInternalEntity(XML_Parser parser, ENTITY *entity,
369
                      XML_Bool betweenDecl);
370
static enum XML_Error
371
doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc,
372
          const char *start, const char *end, const char **endPtr,
373
          XML_Bool haveMore);
374
static enum XML_Error
375
doCdataSection(XML_Parser parser, const ENCODING *, const char **startPtr,
376
               const char *end, const char **nextPtr, XML_Bool haveMore);
377
#ifdef XML_DTD
378
static enum XML_Error
379
doIgnoreSection(XML_Parser parser, const ENCODING *, const char **startPtr,
380
                const char *end, const char **nextPtr, XML_Bool haveMore);
381
#endif /* XML_DTD */
382
383
static void
384
freeBindings(XML_Parser parser, BINDING *bindings);
385
static enum XML_Error
386
storeAtts(XML_Parser parser, const ENCODING *, const char *s,
387
          TAG_NAME *tagNamePtr, BINDING **bindingsPtr);
388
static enum XML_Error
389
addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
390
           const XML_Char *uri, BINDING **bindingsPtr);
391
static int
392
defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *, XML_Bool isCdata,
393
                XML_Bool isId, const XML_Char *dfltValue, XML_Parser parser);
394
static enum XML_Error
395
storeAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
396
                    const char *, const char *, STRING_POOL *);
397
static enum XML_Error
398
appendAttributeValue(XML_Parser parser, const ENCODING *, XML_Bool isCdata,
399
                     const char *, const char *, STRING_POOL *);
400
static ATTRIBUTE_ID *
401
getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start,
402
               const char *end);
403
static int
404
setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *);
405
static enum XML_Error
406
storeEntityValue(XML_Parser parser, const ENCODING *enc, const char *start,
407
                 const char *end);
408
static int
409
reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
410
                            const char *start, const char *end);
411
static int
412
reportComment(XML_Parser parser, const ENCODING *enc, const char *start,
413
              const char *end);
414
static void
415
reportDefault(XML_Parser parser, const ENCODING *enc, const char *start,
416
              const char *end);
417
418
static const XML_Char * getContext(XML_Parser parser);
419
static XML_Bool
420
setContext(XML_Parser parser, const XML_Char *context);
421
422
static void FASTCALL normalizePublicId(XML_Char *s);
423
424
static DTD * dtdCreate(const XML_Memory_Handling_Suite *ms);
425
/* do not call if parentParser != NULL */
426
static void dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms);
427
static void
428
dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms);
429
static int
430
dtdCopy(XML_Parser oldParser,
431
        DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms);
432
static int
433
copyEntityTable(XML_Parser oldParser,
434
                HASH_TABLE *, STRING_POOL *, const HASH_TABLE *);
435
static NAMED *
436
lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize);
437
static void FASTCALL
438
hashTableInit(HASH_TABLE *, const XML_Memory_Handling_Suite *ms);
439
static void FASTCALL hashTableClear(HASH_TABLE *);
440
static void FASTCALL hashTableDestroy(HASH_TABLE *);
441
static void FASTCALL
442
hashTableIterInit(HASH_TABLE_ITER *, const HASH_TABLE *);
443
static NAMED * FASTCALL hashTableIterNext(HASH_TABLE_ITER *);
444
445
static void FASTCALL
446
poolInit(STRING_POOL *, const XML_Memory_Handling_Suite *ms);
447
static void FASTCALL poolClear(STRING_POOL *);
448
static void FASTCALL poolDestroy(STRING_POOL *);
449
static XML_Char *
450
poolAppend(STRING_POOL *pool, const ENCODING *enc,
451
           const char *ptr, const char *end);
452
static XML_Char *
453
poolStoreString(STRING_POOL *pool, const ENCODING *enc,
454
                const char *ptr, const char *end);
455
static XML_Bool FASTCALL poolGrow(STRING_POOL *pool);
456
static const XML_Char * FASTCALL
457
poolCopyString(STRING_POOL *pool, const XML_Char *s);
458
static const XML_Char *
459
poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n);
460
static const XML_Char * FASTCALL
461
poolAppendString(STRING_POOL *pool, const XML_Char *s);
462
463
static int FASTCALL nextScaffoldPart(XML_Parser parser);
464
static XML_Content * build_model(XML_Parser parser);
465
static ELEMENT_TYPE *
466
getElementType(XML_Parser parser, const ENCODING *enc,
467
               const char *ptr, const char *end);
468
469
static XML_Char *copyString(const XML_Char *s,
470
                            const XML_Memory_Handling_Suite *memsuite);
471
472
static unsigned long generate_hash_secret_salt(XML_Parser parser);
473
static XML_Bool startParsing(XML_Parser parser);
474
475
static XML_Parser
476
parserCreate(const XML_Char *encodingName,
477
             const XML_Memory_Handling_Suite *memsuite,
478
             const XML_Char *nameSep,
479
             DTD *dtd);
480
481
static void
482
parserInit(XML_Parser parser, const XML_Char *encodingName);
483
484
#define poolStart(pool) ((pool)->start)
485
#define poolEnd(pool) ((pool)->ptr)
486
#define poolLength(pool) ((pool)->ptr - (pool)->start)
487
#define poolChop(pool) ((void)--(pool->ptr))
488
#define poolLastChar(pool) (((pool)->ptr)[-1])
489
#define poolDiscard(pool) ((pool)->ptr = (pool)->start)
490
#define poolFinish(pool) ((pool)->start = (pool)->ptr)
491
#define poolAppendChar(pool, c) \
492
  (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
493
   ? 0 \
494
   : ((*((pool)->ptr)++ = c), 1))
495
496
struct XML_ParserStruct {
497
  /* The first member must be userData so that the XML_GetUserData
498
     macro works. */
499
  void *m_userData;
500
  void *m_handlerArg;
501
  char *m_buffer;
502
  const XML_Memory_Handling_Suite m_mem;
503
  /* first character to be parsed */
504
  const char *m_bufferPtr;
505
  /* past last character to be parsed */
506
  char *m_bufferEnd;
507
  /* allocated end of buffer */
508
  const char *m_bufferLim;
509
  XML_Index m_parseEndByteIndex;
510
  const char *m_parseEndPtr;
511
  XML_Char *m_dataBuf;
512
  XML_Char *m_dataBufEnd;
513
  XML_StartElementHandler m_startElementHandler;
514
  XML_EndElementHandler m_endElementHandler;
515
  XML_CharacterDataHandler m_characterDataHandler;
516
  XML_ProcessingInstructionHandler m_processingInstructionHandler;
517
  XML_CommentHandler m_commentHandler;
518
  XML_StartCdataSectionHandler m_startCdataSectionHandler;
519
  XML_EndCdataSectionHandler m_endCdataSectionHandler;
520
  XML_DefaultHandler m_defaultHandler;
521
  XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
522
  XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
523
  XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
524
  XML_NotationDeclHandler m_notationDeclHandler;
525
  XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
526
  XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
527
  XML_NotStandaloneHandler m_notStandaloneHandler;
528
  XML_ExternalEntityRefHandler m_externalEntityRefHandler;
529
  XML_Parser m_externalEntityRefHandlerArg;
530
  XML_SkippedEntityHandler m_skippedEntityHandler;
531
  XML_UnknownEncodingHandler m_unknownEncodingHandler;
532
  XML_ElementDeclHandler m_elementDeclHandler;
533
  XML_AttlistDeclHandler m_attlistDeclHandler;
534
  XML_EntityDeclHandler m_entityDeclHandler;
535
  XML_XmlDeclHandler m_xmlDeclHandler;
536
  const ENCODING *m_encoding;
537
  INIT_ENCODING m_initEncoding;
538
  const ENCODING *m_internalEncoding;
539
  const XML_Char *m_protocolEncodingName;
540
  XML_Bool m_ns;
541
  XML_Bool m_ns_triplets;
542
  void *m_unknownEncodingMem;
543
  void *m_unknownEncodingData;
544
  void *m_unknownEncodingHandlerData;
545
  void (XMLCALL *m_unknownEncodingRelease)(void *);
546
  PROLOG_STATE m_prologState;
547
  Processor *m_processor;
548
  enum XML_Error m_errorCode;
549
  const char *m_eventPtr;
550
  const char *m_eventEndPtr;
551
  const char *m_positionPtr;
552
  OPEN_INTERNAL_ENTITY *m_openInternalEntities;
553
  OPEN_INTERNAL_ENTITY *m_freeInternalEntities;
554
  XML_Bool m_defaultExpandInternalEntities;
555
  int m_tagLevel;
556
  ENTITY *m_declEntity;
557
  const XML_Char *m_doctypeName;
558
  const XML_Char *m_doctypeSysid;
559
  const XML_Char *m_doctypePubid;
560
  const XML_Char *m_declAttributeType;
561
  const XML_Char *m_declNotationName;
562
  const XML_Char *m_declNotationPublicId;
563
  ELEMENT_TYPE *m_declElementType;
564
  ATTRIBUTE_ID *m_declAttributeId;
565
  XML_Bool m_declAttributeIsCdata;
566
  XML_Bool m_declAttributeIsId;
567
  DTD *m_dtd;
568
  const XML_Char *m_curBase;
569
  TAG *m_tagStack;
570
  TAG *m_freeTagList;
571
  BINDING *m_inheritedBindings;
572
  BINDING *m_freeBindingList;
573
  int m_attsSize;
574
  int m_nSpecifiedAtts;
575
  int m_idAttIndex;
576
  ATTRIBUTE *m_atts;
577
  NS_ATT *m_nsAtts;
578
  unsigned long m_nsAttsVersion;
579
  unsigned char m_nsAttsPower;
580
#ifdef XML_ATTR_INFO
581
  XML_AttrInfo *m_attInfo;
582
#endif
583
  POSITION m_position;
584
  STRING_POOL m_tempPool;
585
  STRING_POOL m_temp2Pool;
586
  char *m_groupConnector;
587
  unsigned int m_groupSize;
588
  XML_Char m_namespaceSeparator;
589
  XML_Parser m_parentParser;
590
  XML_ParsingStatus m_parsingStatus;
591
#ifdef XML_DTD
592
  XML_Bool m_isParamEntity;
593
  XML_Bool m_useForeignDTD;
594
  enum XML_ParamEntityParsing m_paramEntityParsing;
595
#endif
596
  unsigned long m_hash_secret_salt;
597
};
598
599
#define MALLOC(s) (parser->m_mem.malloc_fcn((s)))
600
#define REALLOC(p,s) (parser->m_mem.realloc_fcn((p),(s)))
601
#define FREE(p) (parser->m_mem.free_fcn((p)))
602
603
#define userData (parser->m_userData)
604
#define handlerArg (parser->m_handlerArg)
605
#define startElementHandler (parser->m_startElementHandler)
606
#define endElementHandler (parser->m_endElementHandler)
607
#define characterDataHandler (parser->m_characterDataHandler)
608
#define processingInstructionHandler \
609
        (parser->m_processingInstructionHandler)
610
#define commentHandler (parser->m_commentHandler)
611
#define startCdataSectionHandler \
612
        (parser->m_startCdataSectionHandler)
613
#define endCdataSectionHandler (parser->m_endCdataSectionHandler)
614
#define defaultHandler (parser->m_defaultHandler)
615
#define startDoctypeDeclHandler (parser->m_startDoctypeDeclHandler)
616
#define endDoctypeDeclHandler (parser->m_endDoctypeDeclHandler)
617
#define unparsedEntityDeclHandler \
618
        (parser->m_unparsedEntityDeclHandler)
619
#define notationDeclHandler (parser->m_notationDeclHandler)
620
#define startNamespaceDeclHandler \
621
        (parser->m_startNamespaceDeclHandler)
622
#define endNamespaceDeclHandler (parser->m_endNamespaceDeclHandler)
623
#define notStandaloneHandler (parser->m_notStandaloneHandler)
624
#define externalEntityRefHandler \
625
        (parser->m_externalEntityRefHandler)
626
#define externalEntityRefHandlerArg \
627
        (parser->m_externalEntityRefHandlerArg)
628
#define internalEntityRefHandler \
629
        (parser->m_internalEntityRefHandler)
630
#define skippedEntityHandler (parser->m_skippedEntityHandler)
631
#define unknownEncodingHandler (parser->m_unknownEncodingHandler)
632
#define elementDeclHandler (parser->m_elementDeclHandler)
633
#define attlistDeclHandler (parser->m_attlistDeclHandler)
634
#define entityDeclHandler (parser->m_entityDeclHandler)
635
#define xmlDeclHandler (parser->m_xmlDeclHandler)
636
#define encoding (parser->m_encoding)
637
#define initEncoding (parser->m_initEncoding)
638
#define internalEncoding (parser->m_internalEncoding)
639
#define unknownEncodingMem (parser->m_unknownEncodingMem)
640
#define unknownEncodingData (parser->m_unknownEncodingData)
641
#define unknownEncodingHandlerData \
642
  (parser->m_unknownEncodingHandlerData)
643
#define unknownEncodingRelease (parser->m_unknownEncodingRelease)
644
#define protocolEncodingName (parser->m_protocolEncodingName)
645
#define ns (parser->m_ns)
646
#define ns_triplets (parser->m_ns_triplets)
647
#define prologState (parser->m_prologState)
648
#define processor (parser->m_processor)
649
#define errorCode (parser->m_errorCode)
650
#define eventPtr (parser->m_eventPtr)
651
#define eventEndPtr (parser->m_eventEndPtr)
652
#define positionPtr (parser->m_positionPtr)
653
#define position (parser->m_position)
654
#define openInternalEntities (parser->m_openInternalEntities)
655
#define freeInternalEntities (parser->m_freeInternalEntities)
656
#define defaultExpandInternalEntities \
657
        (parser->m_defaultExpandInternalEntities)
658
#define tagLevel (parser->m_tagLevel)
659
#define buffer (parser->m_buffer)
660
#define bufferPtr (parser->m_bufferPtr)
661
#define bufferEnd (parser->m_bufferEnd)
662
#define parseEndByteIndex (parser->m_parseEndByteIndex)
663
#define parseEndPtr (parser->m_parseEndPtr)
664
#define bufferLim (parser->m_bufferLim)
665
#define dataBuf (parser->m_dataBuf)
666
#define dataBufEnd (parser->m_dataBufEnd)
667
#define _dtd (parser->m_dtd)
668
#define curBase (parser->m_curBase)
669
#define declEntity (parser->m_declEntity)
670
#define doctypeName (parser->m_doctypeName)
671
#define doctypeSysid (parser->m_doctypeSysid)
672
#define doctypePubid (parser->m_doctypePubid)
673
#define declAttributeType (parser->m_declAttributeType)
674
#define declNotationName (parser->m_declNotationName)
675
#define declNotationPublicId (parser->m_declNotationPublicId)
676
#define declElementType (parser->m_declElementType)
677
#define declAttributeId (parser->m_declAttributeId)
678
#define declAttributeIsCdata (parser->m_declAttributeIsCdata)
679
#define declAttributeIsId (parser->m_declAttributeIsId)
680
#define freeTagList (parser->m_freeTagList)
681
#define freeBindingList (parser->m_freeBindingList)
682
#define inheritedBindings (parser->m_inheritedBindings)
683
#define tagStack (parser->m_tagStack)
684
#define atts (parser->m_atts)
685
#define attsSize (parser->m_attsSize)
686
#define nSpecifiedAtts (parser->m_nSpecifiedAtts)
687
#define idAttIndex (parser->m_idAttIndex)
688
#define nsAtts (parser->m_nsAtts)
689
#define nsAttsVersion (parser->m_nsAttsVersion)
690
#define nsAttsPower (parser->m_nsAttsPower)
691
#define attInfo (parser->m_attInfo)
692
#define tempPool (parser->m_tempPool)
693
#define temp2Pool (parser->m_temp2Pool)
694
#define groupConnector (parser->m_groupConnector)
695
#define groupSize (parser->m_groupSize)
696
#define namespaceSeparator (parser->m_namespaceSeparator)
697
#define parentParser (parser->m_parentParser)
698
#define ps_parsing (parser->m_parsingStatus.parsing)
699
#define ps_finalBuffer (parser->m_parsingStatus.finalBuffer)
700
#ifdef XML_DTD
701
#define isParamEntity (parser->m_isParamEntity)
702
#define useForeignDTD (parser->m_useForeignDTD)
703
#define paramEntityParsing (parser->m_paramEntityParsing)
704
#endif /* XML_DTD */
705
#define hash_secret_salt (parser->m_hash_secret_salt)
706
707
XML_Parser XMLCALL
708
XML_ParserCreate(const XML_Char *encodingName)
709
{
710
4210
  return XML_ParserCreate_MM(encodingName, NULL, NULL);
711
}
712
713
XML_Parser XMLCALL
714
XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
715
{
716
640
  XML_Char tmp[2];
717
320
  *tmp = nsSep;
718
640
  return XML_ParserCreate_MM(encodingName, NULL, tmp);
719
320
}
720
721
static const XML_Char implicitContext[] = {
722
  ASCII_x, ASCII_m, ASCII_l, ASCII_EQUALS, ASCII_h, ASCII_t, ASCII_t, ASCII_p,
723
  ASCII_COLON, ASCII_SLASH, ASCII_SLASH, ASCII_w, ASCII_w, ASCII_w,
724
  ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD, ASCII_o, ASCII_r, ASCII_g,
725
  ASCII_SLASH, ASCII_X, ASCII_M, ASCII_L, ASCII_SLASH, ASCII_1, ASCII_9,
726
  ASCII_9, ASCII_8, ASCII_SLASH, ASCII_n, ASCII_a, ASCII_m, ASCII_e,
727
  ASCII_s, ASCII_p, ASCII_a, ASCII_c, ASCII_e, '\0'
728
};
729
730
static unsigned long
731
generate_hash_secret_salt(XML_Parser parser)
732
{
733
128568
  unsigned long entropy;
734
  (void)parser;
735
64284
  arc4random_buf(&entropy, sizeof(entropy));
736
128568
  return entropy;
737
64284
}
738
739
static unsigned long
740
get_hash_secret_salt(XML_Parser parser) {
741
46344688
  if (parser->m_parentParser != NULL)
742
4380
    return get_hash_secret_salt(parser->m_parentParser);
743
23167964
  return parser->m_hash_secret_salt;
744
23172344
}
745
746
static XML_Bool  /* only valid for root parser */
747
startParsing(XML_Parser parser)
748
{
749
    /* hash functions must be initialized before setContext() is called */
750
128648
    if (hash_secret_salt == 0)
751
64284
      hash_secret_salt = generate_hash_secret_salt(parser);
752
64324
    if (ns) {
753
      /* implicit context only set for root parser, since child
754
         parsers (i.e. external entity parsers) will inherit it
755
      */
756
4740
      return setContext(parser, implicitContext);
757
    }
758
59584
    return XML_TRUE;
759
64324
}
760
761
XML_Parser XMLCALL
762
XML_ParserCreate_MM(const XML_Char *encodingName,
763
                    const XML_Memory_Handling_Suite *memsuite,
764
                    const XML_Char *nameSep)
765
{
766
24638
  return parserCreate(encodingName, memsuite, nameSep, NULL);
767
}
768
769
static XML_Parser
770
parserCreate(const XML_Char *encodingName,
771
             const XML_Memory_Handling_Suite *memsuite,
772
             const XML_Char *nameSep,
773
             DTD *dtd)
774
{
775
  XML_Parser parser;
776
777
35786
  if (memsuite) {
778
    XML_Memory_Handling_Suite *mtemp;
779
15468
    parser = (XML_Parser)
780
15468
      memsuite->malloc_fcn(sizeof(struct XML_ParserStruct));
781
15468
    if (parser != NULL) {
782
15248
      mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
783
15248
      mtemp->malloc_fcn = memsuite->malloc_fcn;
784
15248
      mtemp->realloc_fcn = memsuite->realloc_fcn;
785
15248
      mtemp->free_fcn = memsuite->free_fcn;
786
15248
    }
787
15468
  }
788
  else {
789
    XML_Memory_Handling_Suite *mtemp;
790
2425
    parser = (XML_Parser)malloc(sizeof(struct XML_ParserStruct));
791
2425
    if (parser != NULL) {
792
2425
      mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem);
793
2425
      mtemp->malloc_fcn = malloc;
794
2425
      mtemp->realloc_fcn = realloc;
795
2425
      mtemp->free_fcn = free;
796
2425
    }
797
  }
798
799
17893
  if (!parser)
800
220
    return parser;
801
802
17673
  buffer = NULL;
803
17673
  bufferLim = NULL;
804
805
17673
  attsSize = INIT_ATTS_SIZE;
806
17673
  atts = (ATTRIBUTE *)MALLOC(attsSize * sizeof(ATTRIBUTE));
807
17673
  if (atts == NULL) {
808
220
    FREE(parser);
809
220
    return NULL;
810
  }
811
#ifdef XML_ATTR_INFO
812
  attInfo = (XML_AttrInfo*)MALLOC(attsSize * sizeof(XML_AttrInfo));
813
  if (attInfo == NULL) {
814
    FREE(atts);
815
    FREE(parser);
816
    return NULL;
817
  }
818
#endif
819
17453
  dataBuf = (XML_Char *)MALLOC(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
820
17453
  if (dataBuf == NULL) {
821
220
    FREE(atts);
822
#ifdef XML_ATTR_INFO
823
    FREE(attInfo);
824
#endif
825
220
    FREE(parser);
826
220
    return NULL;
827
  }
828
17233
  dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
829
830
17233
  if (dtd)
831
2890
    _dtd = dtd;
832
  else {
833
14343
    _dtd = dtdCreate(&parser->m_mem);
834
14343
    if (_dtd == NULL) {
835
130
      FREE(dataBuf);
836
130
      FREE(atts);
837
#ifdef XML_ATTR_INFO
838
      FREE(attInfo);
839
#endif
840
130
      FREE(parser);
841
130
      return NULL;
842
    }
843
  }
844
845
17103
  freeBindingList = NULL;
846
17103
  freeTagList = NULL;
847
17103
  freeInternalEntities = NULL;
848
849
17103
  groupSize = 0;
850
17103
  groupConnector = NULL;
851
852
17103
  unknownEncodingHandler = NULL;
853
17103
  unknownEncodingHandlerData = NULL;
854
855
17103
  namespaceSeparator = ASCII_EXCL;
856
17103
  ns = XML_FALSE;
857
17103
  ns_triplets = XML_FALSE;
858
859
17103
  nsAtts = NULL;
860
17103
  nsAttsVersion = 0;
861
17103
  nsAttsPower = 0;
862
863
17103
  protocolEncodingName = NULL;
864
865
17103
  poolInit(&tempPool, &(parser->m_mem));
866
17103
  poolInit(&temp2Pool, &(parser->m_mem));
867
17103
  parserInit(parser, encodingName);
868
869

17143
  if (encodingName && !protocolEncodingName) {
870
10
    XML_ParserFree(parser);
871
10
    return NULL;
872
  }
873
874
17093
  if (nameSep) {
875
6710
    ns = XML_TRUE;
876
6710
    internalEncoding = XmlGetInternalEncodingNS();
877
6710
    namespaceSeparator = *nameSep;
878
6710
  }
879
  else {
880
10383
    internalEncoding = XmlGetInternalEncoding();
881
  }
882
883
17093
  return parser;
884
17893
}
885
886
static void
887
parserInit(XML_Parser parser, const XML_Char *encodingName)
888
{
889
138806
  processor = prologInitProcessor;
890
69403
  XmlPrologStateInit(&prologState);
891
69403
  if (encodingName != NULL) {
892
40
    protocolEncodingName = copyString(encodingName, &(parser->m_mem));
893
40
  }
894
69403
  curBase = NULL;
895
69403
  XmlInitEncoding(&initEncoding, &encoding, 0);
896
69403
  userData = NULL;
897
69403
  handlerArg = NULL;
898
69403
  startElementHandler = NULL;
899
69403
  endElementHandler = NULL;
900
69403
  characterDataHandler = NULL;
901
69403
  processingInstructionHandler = NULL;
902
69403
  commentHandler = NULL;
903
69403
  startCdataSectionHandler = NULL;
904
69403
  endCdataSectionHandler = NULL;
905
69403
  defaultHandler = NULL;
906
69403
  startDoctypeDeclHandler = NULL;
907
69403
  endDoctypeDeclHandler = NULL;
908
69403
  unparsedEntityDeclHandler = NULL;
909
69403
  notationDeclHandler = NULL;
910
69403
  startNamespaceDeclHandler = NULL;
911
69403
  endNamespaceDeclHandler = NULL;
912
69403
  notStandaloneHandler = NULL;
913
69403
  externalEntityRefHandler = NULL;
914
69403
  externalEntityRefHandlerArg = parser;
915
69403
  skippedEntityHandler = NULL;
916
69403
  elementDeclHandler = NULL;
917
69403
  attlistDeclHandler = NULL;
918
69403
  entityDeclHandler = NULL;
919
69403
  xmlDeclHandler = NULL;
920
69403
  bufferPtr = buffer;
921
69403
  bufferEnd = buffer;
922
69403
  parseEndByteIndex = 0;
923
69403
  parseEndPtr = NULL;
924
69403
  declElementType = NULL;
925
69403
  declAttributeId = NULL;
926
69403
  declEntity = NULL;
927
69403
  doctypeName = NULL;
928
69403
  doctypeSysid = NULL;
929
69403
  doctypePubid = NULL;
930
69403
  declAttributeType = NULL;
931
69403
  declNotationName = NULL;
932
69403
  declNotationPublicId = NULL;
933
69403
  declAttributeIsCdata = XML_FALSE;
934
69403
  declAttributeIsId = XML_FALSE;
935
69403
  memset(&position, 0, sizeof(POSITION));
936
69403
  errorCode = XML_ERROR_NONE;
937
69403
  eventPtr = NULL;
938
69403
  eventEndPtr = NULL;
939
69403
  positionPtr = NULL;
940
69403
  openInternalEntities = NULL;
941
69403
  defaultExpandInternalEntities = XML_TRUE;
942
69403
  tagLevel = 0;
943
69403
  tagStack = NULL;
944
69403
  inheritedBindings = NULL;
945
69403
  nSpecifiedAtts = 0;
946
69403
  unknownEncodingMem = NULL;
947
69403
  unknownEncodingRelease = NULL;
948
69403
  unknownEncodingData = NULL;
949
69403
  parentParser = NULL;
950
69403
  ps_parsing = XML_INITIALIZED;
951
#ifdef XML_DTD
952
69403
  isParamEntity = XML_FALSE;
953
69403
  useForeignDTD = XML_FALSE;
954
69403
  paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
955
#endif
956
69403
  hash_secret_salt = 0;
957
69403
}
958
959
/* moves list of bindings to freeBindingList */
960
static void FASTCALL
961
moveToFreeBindingList(XML_Parser parser, BINDING *bindings)
962
{
963
162690
  while (bindings) {
964
    BINDING *b = bindings;
965
60
    bindings = bindings->nextTagBinding;
966
60
    b->nextTagBinding = freeBindingList;
967
60
    freeBindingList = b;
968
  }
969
54190
}
970
971
XML_Bool XMLCALL
972
XML_ParserReset(XML_Parser parser, const XML_Char *encodingName)
973
{
974
  TAG *tStk;
975
  OPEN_INTERNAL_ENTITY *openEntityList;
976
977
104620
  if (parser == NULL)
978
      return XML_FALSE;
979
980
52310
  if (parentParser)
981
10
    return XML_FALSE;
982
  /* move tagStack to freeTagList */
983
52300
  tStk = tagStack;
984
108380
  while (tStk) {
985
    TAG *tag = tStk;
986
1890
    tStk = tStk->parent;
987
1890
    tag->parent = freeTagList;
988
1890
    moveToFreeBindingList(parser, tag->bindings);
989
1890
    tag->bindings = NULL;
990
1890
    freeTagList = tag;
991
  }
992
  /* move openInternalEntities to freeInternalEntities */
993
52300
  openEntityList = openInternalEntities;
994
104620
  while (openEntityList) {
995
    OPEN_INTERNAL_ENTITY *openEntity = openEntityList;
996
10
    openEntityList = openEntity->next;
997
10
    openEntity->next = freeInternalEntities;
998
10
    freeInternalEntities = openEntity;
999
  }
1000
52300
  moveToFreeBindingList(parser, inheritedBindings);
1001
52300
  FREE(unknownEncodingMem);
1002
52300
  if (unknownEncodingRelease)
1003
    unknownEncodingRelease(unknownEncodingData);
1004
52300
  poolClear(&tempPool);
1005
52300
  poolClear(&temp2Pool);
1006
52300
  FREE((void *)protocolEncodingName);
1007
52300
  protocolEncodingName = NULL;
1008
52300
  parserInit(parser, encodingName);
1009
52300
  dtdReset(_dtd, &parser->m_mem);
1010
52300
  return XML_TRUE;
1011
52310
}
1012
1013
enum XML_Status XMLCALL
1014
XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
1015
{
1016
420
  if (parser == NULL)
1017
      return XML_STATUS_ERROR;
1018
  /* Block after XML_Parse()/XML_ParseBuffer() has been called.
1019
     XXX There's no way for the caller to determine which of the
1020
     XXX possible error cases caused the XML_STATUS_ERROR return.
1021
  */
1022

410
  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1023
10
    return XML_STATUS_ERROR;
1024
1025
  /* Get rid of any previous encoding name */
1026
200
  FREE((void *)protocolEncodingName);
1027
1028
200
  if (encodingName == NULL)
1029
    /* No new encoding name */
1030
20
    protocolEncodingName = NULL;
1031
  else {
1032
    /* Copy the new encoding name into allocated memory */
1033
180
    protocolEncodingName = copyString(encodingName, &(parser->m_mem));
1034
180
    if (!protocolEncodingName)
1035
20
      return XML_STATUS_ERROR;
1036
  }
1037
180
  return XML_STATUS_OK;
1038
210
}
1039
1040
XML_Parser XMLCALL
1041
XML_ExternalEntityParserCreate(XML_Parser oldParser,
1042
                               const XML_Char *context,
1043
                               const XML_Char *encodingName)
1044
{
1045
  XML_Parser parser = oldParser;
1046
  DTD *newDtd = NULL;
1047
  DTD *oldDtd;
1048
  XML_StartElementHandler oldStartElementHandler;
1049
  XML_EndElementHandler oldEndElementHandler;
1050
  XML_CharacterDataHandler oldCharacterDataHandler;
1051
  XML_ProcessingInstructionHandler oldProcessingInstructionHandler;
1052
  XML_CommentHandler oldCommentHandler;
1053
  XML_StartCdataSectionHandler oldStartCdataSectionHandler;
1054
  XML_EndCdataSectionHandler oldEndCdataSectionHandler;
1055
  XML_DefaultHandler oldDefaultHandler;
1056
  XML_UnparsedEntityDeclHandler oldUnparsedEntityDeclHandler;
1057
  XML_NotationDeclHandler oldNotationDeclHandler;
1058
  XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler;
1059
  XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler;
1060
  XML_NotStandaloneHandler oldNotStandaloneHandler;
1061
  XML_ExternalEntityRefHandler oldExternalEntityRefHandler;
1062
  XML_SkippedEntityHandler oldSkippedEntityHandler;
1063
  XML_UnknownEncodingHandler oldUnknownEncodingHandler;
1064
  XML_ElementDeclHandler oldElementDeclHandler;
1065
  XML_AttlistDeclHandler oldAttlistDeclHandler;
1066
  XML_EntityDeclHandler oldEntityDeclHandler;
1067
  XML_XmlDeclHandler oldXmlDeclHandler;
1068
  ELEMENT_TYPE * oldDeclElementType;
1069
1070
  void *oldUserData;
1071
  void *oldHandlerArg;
1072
  XML_Bool oldDefaultExpandInternalEntities;
1073
  XML_Parser oldExternalEntityRefHandlerArg;
1074
#ifdef XML_DTD
1075
  enum XML_ParamEntityParsing oldParamEntityParsing;
1076
  int oldInEntityValue;
1077
#endif
1078
  XML_Bool oldns_triplets;
1079
  /* Note that the new parser shares the same hash secret as the old
1080
     parser, so that dtdCopy and copyEntityTable can lookup values
1081
     from hash tables associated with either parser without us having
1082
     to worry which hash secrets each table has.
1083
  */
1084
  unsigned long oldhash_secret_salt;
1085
1086
  /* Validate the oldParser parameter before we pull everything out of it */
1087
11148
  if (oldParser == NULL)
1088
    return NULL;
1089
1090
  /* Stash the original parser contents on the stack */
1091
5574
  oldDtd = _dtd;
1092
5574
  oldStartElementHandler = startElementHandler;
1093
5574
  oldEndElementHandler = endElementHandler;
1094
5574
  oldCharacterDataHandler = characterDataHandler;
1095
5574
  oldProcessingInstructionHandler = processingInstructionHandler;
1096
5574
  oldCommentHandler = commentHandler;
1097
5574
  oldStartCdataSectionHandler = startCdataSectionHandler;
1098
5574
  oldEndCdataSectionHandler = endCdataSectionHandler;
1099
5574
  oldDefaultHandler = defaultHandler;
1100
5574
  oldUnparsedEntityDeclHandler = unparsedEntityDeclHandler;
1101
5574
  oldNotationDeclHandler = notationDeclHandler;
1102
5574
  oldStartNamespaceDeclHandler = startNamespaceDeclHandler;
1103
5574
  oldEndNamespaceDeclHandler = endNamespaceDeclHandler;
1104
5574
  oldNotStandaloneHandler = notStandaloneHandler;
1105
5574
  oldExternalEntityRefHandler = externalEntityRefHandler;
1106
5574
  oldSkippedEntityHandler = skippedEntityHandler;
1107
5574
  oldUnknownEncodingHandler = unknownEncodingHandler;
1108
5574
  oldElementDeclHandler = elementDeclHandler;
1109
5574
  oldAttlistDeclHandler = attlistDeclHandler;
1110
5574
  oldEntityDeclHandler = entityDeclHandler;
1111
5574
  oldXmlDeclHandler = xmlDeclHandler;
1112
5574
  oldDeclElementType = declElementType;
1113
1114
5574
  oldUserData = userData;
1115
5574
  oldHandlerArg = handlerArg;
1116
5574
  oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
1117
5574
  oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
1118
#ifdef XML_DTD
1119
5574
  oldParamEntityParsing = paramEntityParsing;
1120
5574
  oldInEntityValue = prologState.inEntityValue;
1121
#endif
1122
5574
  oldns_triplets = ns_triplets;
1123
  /* Note that the new parser shares the same hash secret as the old
1124
     parser, so that dtdCopy and copyEntityTable can lookup values
1125
     from hash tables associated with either parser without us having
1126
     to worry which hash secrets each table has.
1127
  */
1128
5574
  oldhash_secret_salt = hash_secret_salt;
1129
1130
#ifdef XML_DTD
1131
5574
  if (!context)
1132
3190
    newDtd = oldDtd;
1133
#endif /* XML_DTD */
1134
1135
  /* Note that the magical uses of the pre-processor to make field
1136
     access look more like C++ require that `parser' be overwritten
1137
     here.  This makes this function more painful to follow than it
1138
     would be otherwise.
1139
  */
1140
5574
  if (ns) {
1141
2280
    XML_Char tmp[2];
1142
2280
    *tmp = namespaceSeparator;
1143
2280
    parser = parserCreate(encodingName, &parser->m_mem, tmp, newDtd);
1144
2280
  }
1145
  else {
1146
3294
    parser = parserCreate(encodingName, &parser->m_mem, NULL, newDtd);
1147
  }
1148
1149
5574
  if (!parser)
1150
710
    return NULL;
1151
1152
4864
  startElementHandler = oldStartElementHandler;
1153
4864
  endElementHandler = oldEndElementHandler;
1154
4864
  characterDataHandler = oldCharacterDataHandler;
1155
4864
  processingInstructionHandler = oldProcessingInstructionHandler;
1156
4864
  commentHandler = oldCommentHandler;
1157
4864
  startCdataSectionHandler = oldStartCdataSectionHandler;
1158
4864
  endCdataSectionHandler = oldEndCdataSectionHandler;
1159
4864
  defaultHandler = oldDefaultHandler;
1160
4864
  unparsedEntityDeclHandler = oldUnparsedEntityDeclHandler;
1161
4864
  notationDeclHandler = oldNotationDeclHandler;
1162
4864
  startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
1163
4864
  endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
1164
4864
  notStandaloneHandler = oldNotStandaloneHandler;
1165
4864
  externalEntityRefHandler = oldExternalEntityRefHandler;
1166
4864
  skippedEntityHandler = oldSkippedEntityHandler;
1167
4864
  unknownEncodingHandler = oldUnknownEncodingHandler;
1168
4864
  elementDeclHandler = oldElementDeclHandler;
1169
4864
  attlistDeclHandler = oldAttlistDeclHandler;
1170
4864
  entityDeclHandler = oldEntityDeclHandler;
1171
4864
  xmlDeclHandler = oldXmlDeclHandler;
1172
4864
  declElementType = oldDeclElementType;
1173
4864
  userData = oldUserData;
1174
4864
  if (oldUserData == oldHandlerArg)
1175
4854
    handlerArg = userData;
1176
  else
1177
10
    handlerArg = parser;
1178
4864
  if (oldExternalEntityRefHandlerArg != oldParser)
1179
10
    externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
1180
4864
  defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
1181
4864
  ns_triplets = oldns_triplets;
1182
4864
  hash_secret_salt = oldhash_secret_salt;
1183
4864
  parentParser = oldParser;
1184
#ifdef XML_DTD
1185
4864
  paramEntityParsing = oldParamEntityParsing;
1186
4864
  prologState.inEntityValue = oldInEntityValue;
1187
4864
  if (context) {
1188
#endif /* XML_DTD */
1189
2904
    if (!dtdCopy(oldParser, _dtd, oldDtd, &parser->m_mem)
1190
2904
      || !setContext(parser, context)) {
1191
1305
      XML_ParserFree(parser);
1192
1305
      return NULL;
1193
    }
1194
    processor = externalEntityInitProcessor;
1195
#ifdef XML_DTD
1196
669
  }
1197
  else {
1198
    /* The DTD instance referenced by _dtd is shared between the document's
1199
       root parser and external PE parsers, therefore one does not need to
1200
       call setContext. In addition, one also *must* not call setContext,
1201
       because this would overwrite existing prefix->binding pointers in
1202
       _dtd with ones that get destroyed with the external PE parser.
1203
       This would leave those prefixes with dangling pointers.
1204
    */
1205
2890
    isParamEntity = XML_TRUE;
1206
2890
    XmlPrologStateInitExternalEntity(&prologState);
1207
    processor = externalParEntInitProcessor;
1208
  }
1209
#endif /* XML_DTD */
1210
3559
  return parser;
1211
5574
}
1212
1213
static void FASTCALL
1214
destroyBindings(BINDING *bindings, XML_Parser parser)
1215
{
1216
81470
  for (;;) {
1217
    BINDING *b = bindings;
1218
46876
    if (!b)
1219
40735
      break;
1220
6141
    bindings = b->nextTagBinding;
1221
6141
    FREE(b->uri);
1222
6141
    FREE(b);
1223
6141
  }
1224
40735
}
1225
1226
void XMLCALL
1227
XML_ParserFree(XML_Parser parser)
1228
{
1229
  TAG *tagList;
1230
  OPEN_INTERNAL_ENTITY *entityList;
1231
34226
  if (parser == NULL)
1232
10
    return;
1233
  /* free tagStack and freeTagList */
1234
17103
  tagList = tagStack;
1235
17103
  for (;;) {
1236
    TAG *p;
1237
23632
    if (tagList == NULL) {
1238
18888
      if (freeTagList == NULL)
1239
17103
        break;
1240
      tagList = freeTagList;
1241
1785
      freeTagList = NULL;
1242
1785
    }
1243
    p = tagList;
1244
6529
    tagList = tagList->parent;
1245
6529
    FREE(p->buf);
1246
6529
    destroyBindings(p->bindings, parser);
1247
6529
    FREE(p);
1248
6529
  }
1249
  /* free openInternalEntities and freeInternalEntities */
1250
17103
  entityList = openInternalEntities;
1251
17103
  for (;;) {
1252
    OPEN_INTERNAL_ENTITY *openEntity;
1253
17293
    if (entityList == NULL) {
1254
17253
      if (freeInternalEntities == NULL)
1255
17103
        break;
1256
      entityList = freeInternalEntities;
1257
150
      freeInternalEntities = NULL;
1258
150
    }
1259
    openEntity = entityList;
1260
190
    entityList = entityList->next;
1261
190
    FREE(openEntity);
1262
190
  }
1263
1264
17103
  destroyBindings(freeBindingList, parser);
1265
17103
  destroyBindings(inheritedBindings, parser);
1266
17103
  poolDestroy(&tempPool);
1267
17103
  poolDestroy(&temp2Pool);
1268
17103
  FREE((void *)protocolEncodingName);
1269
#ifdef XML_DTD
1270
  /* external parameter entity parsers share the DTD structure
1271
     parser->m_dtd with the root parser, so we must not destroy it
1272
  */
1273

31316
  if (!isParamEntity && _dtd)
1274
#else
1275
  if (_dtd)
1276
#endif /* XML_DTD */
1277
14213
    dtdDestroy(_dtd, (XML_Bool)!parentParser, &parser->m_mem);
1278
17103
  FREE((void *)atts);
1279
#ifdef XML_ATTR_INFO
1280
  FREE((void *)attInfo);
1281
#endif
1282
17103
  FREE(groupConnector);
1283
17103
  FREE(buffer);
1284
17103
  FREE(dataBuf);
1285
17103
  FREE(nsAtts);
1286
17103
  FREE(unknownEncodingMem);
1287
17103
  if (unknownEncodingRelease)
1288
100
    unknownEncodingRelease(unknownEncodingData);
1289
17103
  FREE(parser);
1290
34216
}
1291
1292
void XMLCALL
1293
XML_UseParserAsHandlerArg(XML_Parser parser)
1294
{
1295
40
  if (parser != NULL)
1296
20
    handlerArg = parser;
1297
20
}
1298
1299
enum XML_Error XMLCALL
1300
XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD)
1301
{
1302
440
  if (parser == NULL)
1303
    return XML_ERROR_INVALID_ARGUMENT;
1304
#ifdef XML_DTD
1305
  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1306

420
  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1307
20
    return XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING;
1308
200
  useForeignDTD = useDTD;
1309
200
  return XML_ERROR_NONE;
1310
#else
1311
  return XML_ERROR_FEATURE_REQUIRES_XML_DTD;
1312
#endif
1313
220
}
1314
1315
void XMLCALL
1316
XML_SetReturnNSTriplet(XML_Parser parser, int do_nst)
1317
{
1318
1140
  if (parser == NULL)
1319
    return;
1320
  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1321

1120
  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1322
    return;
1323
550
  ns_triplets = do_nst ? XML_TRUE : XML_FALSE;
1324
1120
}
1325
1326
void XMLCALL
1327
XML_SetUserData(XML_Parser parser, void *p)
1328
{
1329
14388
  if (parser == NULL)
1330
    return;
1331
7194
  if (handlerArg == userData)
1332
7184
    handlerArg = userData = p;
1333
  else
1334
    userData = p;
1335
7194
}
1336
1337
enum XML_Status XMLCALL
1338
XML_SetBase(XML_Parser parser, const XML_Char *p)
1339
{
1340
540
  if (parser == NULL)
1341
    return XML_STATUS_ERROR;
1342
270
  if (p) {
1343
250
    p = poolCopyString(&_dtd->pool, p);
1344
250
    if (!p)
1345
20
      return XML_STATUS_ERROR;
1346
    curBase = p;
1347
230
  }
1348
  else
1349
    curBase = NULL;
1350
250
  return XML_STATUS_OK;
1351
270
}
1352
1353
const XML_Char * XMLCALL
1354
XML_GetBase(XML_Parser parser)
1355
{
1356
60
  if (parser == NULL)
1357
    return NULL;
1358
30
  return curBase;
1359
30
}
1360
1361
int XMLCALL
1362
XML_GetSpecifiedAttributeCount(XML_Parser parser)
1363
{
1364
40
  if (parser == NULL)
1365
    return -1;
1366
20
  return nSpecifiedAtts;
1367
20
}
1368
1369
int XMLCALL
1370
XML_GetIdAttributeIndex(XML_Parser parser)
1371
{
1372
40
  if (parser == NULL)
1373
    return -1;
1374
20
  return idAttIndex;
1375
20
}
1376
1377
#ifdef XML_ATTR_INFO
1378
const XML_AttrInfo * XMLCALL
1379
XML_GetAttributeInfo(XML_Parser parser)
1380
{
1381
  if (parser == NULL)
1382
    return NULL;
1383
  return attInfo;
1384
}
1385
#endif
1386
1387
void XMLCALL
1388
XML_SetElementHandler(XML_Parser parser,
1389
                      XML_StartElementHandler start,
1390
                      XML_EndElementHandler end)
1391
{
1392
1120
  if (parser == NULL)
1393
    return;
1394
560
  startElementHandler = start;
1395
560
  endElementHandler = end;
1396
1120
}
1397
1398
void XMLCALL
1399
XML_SetStartElementHandler(XML_Parser parser,
1400
                           XML_StartElementHandler start) {
1401
860
  if (parser != NULL)
1402
430
    startElementHandler = start;
1403
430
}
1404
1405
void XMLCALL
1406
XML_SetEndElementHandler(XML_Parser parser,
1407
                         XML_EndElementHandler end) {
1408
120
  if (parser != NULL)
1409
60
    endElementHandler = end;
1410
60
}
1411
1412
void XMLCALL
1413
XML_SetCharacterDataHandler(XML_Parser parser,
1414
                            XML_CharacterDataHandler handler)
1415
{
1416
2000
  if (parser != NULL)
1417
1000
    characterDataHandler = handler;
1418
1000
}
1419
1420
void XMLCALL
1421
XML_SetProcessingInstructionHandler(XML_Parser parser,
1422
                                    XML_ProcessingInstructionHandler handler)
1423
{
1424
1100
  if (parser != NULL)
1425
550
    processingInstructionHandler = handler;
1426
550
}
1427
1428
void XMLCALL
1429
XML_SetCommentHandler(XML_Parser parser,
1430
                      XML_CommentHandler handler)
1431
{
1432
920
  if (parser != NULL)
1433
460
    commentHandler = handler;
1434
460
}
1435
1436
void XMLCALL
1437
XML_SetCdataSectionHandler(XML_Parser parser,
1438
                           XML_StartCdataSectionHandler start,
1439
                           XML_EndCdataSectionHandler end)
1440
{
1441
360
  if (parser == NULL)
1442
    return;
1443
180
  startCdataSectionHandler = start;
1444
180
  endCdataSectionHandler = end;
1445
360
}
1446
1447
void XMLCALL
1448
XML_SetStartCdataSectionHandler(XML_Parser parser,
1449
                                XML_StartCdataSectionHandler start) {
1450
40
  if (parser != NULL)
1451
20
    startCdataSectionHandler = start;
1452
20
}
1453
1454
void XMLCALL
1455
XML_SetEndCdataSectionHandler(XML_Parser parser,
1456
                              XML_EndCdataSectionHandler end) {
1457
40
  if (parser != NULL)
1458
20
    endCdataSectionHandler = end;
1459
20
}
1460
1461
void XMLCALL
1462
XML_SetDefaultHandler(XML_Parser parser,
1463
                      XML_DefaultHandler handler)
1464
{
1465
1260
  if (parser == NULL)
1466
    return;
1467
630
  defaultHandler = handler;
1468
630
  defaultExpandInternalEntities = XML_FALSE;
1469
1260
}
1470
1471
void XMLCALL
1472
XML_SetDefaultHandlerExpand(XML_Parser parser,
1473
                            XML_DefaultHandler handler)
1474
{
1475
40
  if (parser == NULL)
1476
    return;
1477
20
  defaultHandler = handler;
1478
20
  defaultExpandInternalEntities = XML_TRUE;
1479
40
}
1480
1481
void XMLCALL
1482
XML_SetDoctypeDeclHandler(XML_Parser parser,
1483
                          XML_StartDoctypeDeclHandler start,
1484
                          XML_EndDoctypeDeclHandler end)
1485
{
1486
840
  if (parser == NULL)
1487
    return;
1488
420
  startDoctypeDeclHandler = start;
1489
420
  endDoctypeDeclHandler = end;
1490
840
}
1491
1492
void XMLCALL
1493
XML_SetStartDoctypeDeclHandler(XML_Parser parser,
1494
                               XML_StartDoctypeDeclHandler start) {
1495
80
  if (parser != NULL)
1496
40
    startDoctypeDeclHandler = start;
1497
40
}
1498
1499
void XMLCALL
1500
XML_SetEndDoctypeDeclHandler(XML_Parser parser,
1501
                             XML_EndDoctypeDeclHandler end) {
1502
80
  if (parser != NULL)
1503
40
    endDoctypeDeclHandler = end;
1504
40
}
1505
1506
void XMLCALL
1507
XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
1508
                                 XML_UnparsedEntityDeclHandler handler)
1509
{
1510
360
  if (parser != NULL)
1511
180
    unparsedEntityDeclHandler = handler;
1512
180
}
1513
1514
void XMLCALL
1515
XML_SetNotationDeclHandler(XML_Parser parser,
1516
                           XML_NotationDeclHandler handler)
1517
{
1518
960
  if (parser != NULL)
1519
480
    notationDeclHandler = handler;
1520
480
}
1521
1522
void XMLCALL
1523
XML_SetNamespaceDeclHandler(XML_Parser parser,
1524
                            XML_StartNamespaceDeclHandler start,
1525
                            XML_EndNamespaceDeclHandler end)
1526
{
1527
40
  if (parser == NULL)
1528
    return;
1529
20
  startNamespaceDeclHandler = start;
1530
20
  endNamespaceDeclHandler = end;
1531
40
}
1532
1533
void XMLCALL
1534
XML_SetStartNamespaceDeclHandler(XML_Parser parser,
1535
                                 XML_StartNamespaceDeclHandler start) {
1536
40
  if (parser != NULL)
1537
20
    startNamespaceDeclHandler = start;
1538
20
}
1539
1540
void XMLCALL
1541
XML_SetEndNamespaceDeclHandler(XML_Parser parser,
1542
                               XML_EndNamespaceDeclHandler end) {
1543
40
  if (parser != NULL)
1544
20
    endNamespaceDeclHandler = end;
1545
20
}
1546
1547
void XMLCALL
1548
XML_SetNotStandaloneHandler(XML_Parser parser,
1549
                            XML_NotStandaloneHandler handler)
1550
{
1551
140
  if (parser != NULL)
1552
70
    notStandaloneHandler = handler;
1553
70
}
1554
1555
void XMLCALL
1556
XML_SetExternalEntityRefHandler(XML_Parser parser,
1557
                                XML_ExternalEntityRefHandler handler)
1558
{
1559
10868
  if (parser != NULL)
1560
5434
    externalEntityRefHandler = handler;
1561
5434
}
1562
1563
void XMLCALL
1564
XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
1565
{
1566
40
  if (parser == NULL)
1567
    return;
1568
40
  if (arg)
1569
20
    externalEntityRefHandlerArg = (XML_Parser)arg;
1570
  else
1571
    externalEntityRefHandlerArg = parser;
1572
40
}
1573
1574
void XMLCALL
1575
XML_SetSkippedEntityHandler(XML_Parser parser,
1576
                            XML_SkippedEntityHandler handler)
1577
{
1578
60
  if (parser != NULL)
1579
30
    skippedEntityHandler = handler;
1580
30
}
1581
1582
void XMLCALL
1583
XML_SetUnknownEncodingHandler(XML_Parser parser,
1584
                              XML_UnknownEncodingHandler handler,
1585
                              void *data)
1586
{
1587
880
  if (parser == NULL)
1588
    return;
1589
440
  unknownEncodingHandler = handler;
1590
440
  unknownEncodingHandlerData = data;
1591
880
}
1592
1593
void XMLCALL
1594
XML_SetElementDeclHandler(XML_Parser parser,
1595
                          XML_ElementDeclHandler eldecl)
1596
{
1597
1880
  if (parser != NULL)
1598
940
    elementDeclHandler = eldecl;
1599
940
}
1600
1601
void XMLCALL
1602
XML_SetAttlistDeclHandler(XML_Parser parser,
1603
                          XML_AttlistDeclHandler attdecl)
1604
{
1605
1000
  if (parser != NULL)
1606
500
    attlistDeclHandler = attdecl;
1607
500
}
1608
1609
void XMLCALL
1610
XML_SetEntityDeclHandler(XML_Parser parser,
1611
                         XML_EntityDeclHandler handler)
1612
{
1613
1220
  if (parser != NULL)
1614
610
    entityDeclHandler = handler;
1615
610
}
1616
1617
void XMLCALL
1618
XML_SetXmlDeclHandler(XML_Parser parser,
1619
                      XML_XmlDeclHandler handler) {
1620
680
  if (parser != NULL)
1621
340
    xmlDeclHandler = handler;
1622
340
}
1623
1624
int XMLCALL
1625
XML_SetParamEntityParsing(XML_Parser parser,
1626
                          enum XML_ParamEntityParsing peParsing)
1627
{
1628
10028
  if (parser == NULL)
1629
    return 0;
1630
  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1631

10018
  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1632
10
    return 0;
1633
#ifdef XML_DTD
1634
5004
  paramEntityParsing = peParsing;
1635
5004
  return 1;
1636
#else
1637
  return peParsing == XML_PARAM_ENTITY_PARSING_NEVER;
1638
#endif
1639
5014
}
1640
1641
int XMLCALL
1642
XML_SetHashSalt(XML_Parser parser,
1643
                unsigned long hash_salt)
1644
{
1645
100
  if (parser == NULL)
1646
    return 0;
1647
50
  if (parser->m_parentParser)
1648
    return XML_SetHashSalt(parser->m_parentParser, hash_salt);
1649
  /* block after XML_Parse()/XML_ParseBuffer() has been called */
1650

80
  if (ps_parsing == XML_PARSING || ps_parsing == XML_SUSPENDED)
1651
20
    return 0;
1652
30
  hash_secret_salt = hash_salt;
1653
30
  return 1;
1654
50
}
1655
1656
enum XML_Status XMLCALL
1657
XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
1658
{
1659

22132893
  if ((parser == NULL) || (len < 0) || ((s == NULL) && (len != 0))) {
1660
    if (parser != NULL)
1661
      parser->m_errorCode = XML_ERROR_INVALID_ARGUMENT;
1662
    return XML_STATUS_ERROR;
1663
  }
1664

7444514
  switch (ps_parsing) {
1665
  case XML_SUSPENDED:
1666
20
    errorCode = XML_ERROR_SUSPENDED;
1667
20
    return XML_STATUS_ERROR;
1668
  case XML_FINISHED:
1669
10
    errorCode = XML_ERROR_FINISHED;
1670
10
    return XML_STATUS_ERROR;
1671
  case XML_INITIALIZED:
1672

131777
    if (parentParser == NULL && !startParsing(parser)) {
1673
720
      errorCode = XML_ERROR_NO_MEMORY;
1674
720
      return XML_STATUS_ERROR;
1675
    }
1676
  default:
1677
7376881
    ps_parsing = XML_PARSING;
1678
  }
1679
1680
7376881
  if (len == 0) {
1681
50
    ps_finalBuffer = (XML_Bool)isFinal;
1682
50
    if (!isFinal)
1683
10
      return XML_STATUS_OK;
1684
40
    positionPtr = bufferPtr;
1685
40
    parseEndPtr = bufferEnd;
1686
1687
    /* If data are left over from last buffer, and we now know that these
1688
       data are the final chunk of input, then we have to check them again
1689
       to detect errors based on that fact.
1690
    */
1691
40
    errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
1692
1693
40
    if (errorCode == XML_ERROR_NONE) {
1694

20
      switch (ps_parsing) {
1695
      case XML_SUSPENDED:
1696
        /* It is hard to be certain, but it seems that this case
1697
         * cannot occur.  This code is cleaning up a previous parse
1698
         * with no new data (since len == 0).  Changing the parsing
1699
         * state requires getting to execute a handler function, and
1700
         * there doesn't seem to be an opportunity for that while in
1701
         * this circumstance.
1702
         *
1703
         * Given the uncertainty, we retain the code but exclude it
1704
         * from coverage tests.
1705
         *
1706
         * LCOV_EXCL_START
1707
         */
1708
        XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
1709
        positionPtr = bufferPtr;
1710
        return XML_STATUS_SUSPENDED;
1711
        /* LCOV_EXCL_STOP */
1712
      case XML_INITIALIZED:
1713
      case XML_PARSING:
1714
10
        ps_parsing = XML_FINISHED;
1715
        /* fall through */
1716
      default:
1717
10
        return XML_STATUS_OK;
1718
      }
1719
    }
1720
30
    eventEndPtr = eventPtr;
1721
30
    processor = errorProcessor;
1722
30
    return XML_STATUS_ERROR;
1723
  }
1724
#ifndef XML_CONTEXT_BYTES
1725
  else if (bufferPtr == bufferEnd) {
1726
    const char *end;
1727
    int nLeftOver;
1728
    enum XML_Status result;
1729
    /* Detect overflow (a+b > MAX <==> b > MAX-a) */
1730
    if (len > ((XML_Size)-1) / 2 - parseEndByteIndex) {
1731
       errorCode = XML_ERROR_NO_MEMORY;
1732
       eventPtr = eventEndPtr = NULL;
1733
       processor = errorProcessor;
1734
       return XML_STATUS_ERROR;
1735
    }
1736
    parseEndByteIndex += len;
1737
    positionPtr = s;
1738
    ps_finalBuffer = (XML_Bool)isFinal;
1739
1740
    errorCode = processor(parser, s, parseEndPtr = s + len, &end);
1741
1742
    if (errorCode != XML_ERROR_NONE) {
1743
      eventEndPtr = eventPtr;
1744
      processor = errorProcessor;
1745
      return XML_STATUS_ERROR;
1746
    }
1747
    else {
1748
      switch (ps_parsing) {
1749
      case XML_SUSPENDED:
1750
        result = XML_STATUS_SUSPENDED;
1751
        break;
1752
      case XML_INITIALIZED:
1753
      case XML_PARSING:
1754
        if (isFinal) {
1755
          ps_parsing = XML_FINISHED;
1756
          return XML_STATUS_OK;
1757
        }
1758
      /* fall through */
1759
      default:
1760
        result = XML_STATUS_OK;
1761
      }
1762
    }
1763
1764
    XmlUpdatePosition(encoding, positionPtr, end, &position);
1765
    nLeftOver = s + len - end;
1766
    if (nLeftOver) {
1767
      if (buffer == NULL || nLeftOver > bufferLim - buffer) {
1768
        /* avoid _signed_ integer overflow */
1769
        char *temp = NULL;
1770
        const int bytesToAllocate = (int)((unsigned)len * 2U);
1771
        if (bytesToAllocate > 0) {
1772
          temp = (buffer == NULL
1773
                ? (char *)MALLOC(bytesToAllocate)
1774
                : (char *)REALLOC(buffer, bytesToAllocate));
1775
        }
1776
        if (temp == NULL) {
1777
          errorCode = XML_ERROR_NO_MEMORY;
1778
          eventPtr = eventEndPtr = NULL;
1779
          processor = errorProcessor;
1780
          return XML_STATUS_ERROR;
1781
        }
1782
        buffer = temp;
1783
        bufferLim = buffer + bytesToAllocate;
1784
      }
1785
      memcpy(buffer, end, nLeftOver);
1786
    }
1787
    bufferPtr = buffer;
1788
    bufferEnd = buffer + nLeftOver;
1789
    positionPtr = bufferPtr;
1790
    parseEndPtr = bufferEnd;
1791
    eventPtr = bufferPtr;
1792
    eventEndPtr = bufferPtr;
1793
    return result;
1794
  }
1795
#endif  /* not defined XML_CONTEXT_BYTES */
1796
  else {
1797
7376831
    void *buff = XML_GetBuffer(parser, len);
1798
7376831
    if (buff == NULL)
1799
709
      return XML_STATUS_ERROR;
1800
    else {
1801
7376122
      memcpy(buff, s, len);
1802
7376122
      return XML_ParseBuffer(parser, len, isFinal);
1803
    }
1804
  }
1805
7377631
}
1806
1807
enum XML_Status XMLCALL
1808
XML_ParseBuffer(XML_Parser parser, int len, int isFinal)
1809
{
1810
  const char *start;
1811
  enum XML_Status result = XML_STATUS_OK;
1812
1813
14752884
  if (parser == NULL)
1814
    return XML_STATUS_ERROR;
1815

7376692
  switch (ps_parsing) {
1816
  case XML_SUSPENDED:
1817
10
    errorCode = XML_ERROR_SUSPENDED;
1818
10
    return XML_STATUS_ERROR;
1819
  case XML_FINISHED:
1820
10
    errorCode = XML_ERROR_FINISHED;
1821
10
    return XML_STATUS_ERROR;
1822
  case XML_INITIALIZED:
1823

410
    if (parentParser == NULL && !startParsing(parser)) {
1824
10
      errorCode = XML_ERROR_NO_MEMORY;
1825
10
      return XML_STATUS_ERROR;
1826
    }
1827
  default:
1828
7376412
    ps_parsing = XML_PARSING;
1829
  }
1830
1831
7376412
  start = bufferPtr;
1832
7376412
  positionPtr = start;
1833
7376412
  bufferEnd += len;
1834
7376412
  parseEndPtr = bufferEnd;
1835
7376412
  parseEndByteIndex += len;
1836
7376412
  ps_finalBuffer = (XML_Bool)isFinal;
1837
1838
7376412
  errorCode = processor(parser, start, parseEndPtr, &bufferPtr);
1839
1840
7376412
  if (errorCode != XML_ERROR_NONE) {
1841
11554
    eventEndPtr = eventPtr;
1842
11554
    processor = errorProcessor;
1843
11554
    return XML_STATUS_ERROR;
1844
  }
1845
  else {
1846

14675076
    switch (ps_parsing) {
1847
    case XML_SUSPENDED:
1848
      result = XML_STATUS_SUSPENDED;
1849
150
      break;
1850
    case XML_INITIALIZED:
1851
    case XML_PARSING:
1852
7364708
      if (isFinal) {
1853
54640
        ps_parsing = XML_FINISHED;
1854
54640
        return result;
1855
      }
1856
    default: ;  /* should not happen */
1857
    }
1858
  }
1859
1860
7310218
  XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
1861
7310218
  positionPtr = bufferPtr;
1862
7310218
  return result;
1863
7376442
}
1864
1865
void * XMLCALL
1866
XML_GetBuffer(XML_Parser parser, int len)
1867
{
1868
14754302
  if (parser == NULL)
1869
    return NULL;
1870
7377151
  if (len < 0) {
1871
10
    errorCode = XML_ERROR_NO_MEMORY;
1872
10
    return NULL;
1873
  }
1874
7377141
  switch (ps_parsing) {
1875
  case XML_SUSPENDED:
1876
10
    errorCode = XML_ERROR_SUSPENDED;
1877
10
    return NULL;
1878
  case XML_FINISHED:
1879
10
    errorCode = XML_ERROR_FINISHED;
1880
10
    return NULL;
1881
  default: ;
1882
  }
1883
1884
7377121
  if (len > bufferLim - bufferEnd) {
1885
#ifdef XML_CONTEXT_BYTES
1886
    int keep;
1887
#endif  /* defined XML_CONTEXT_BYTES */
1888
    /* Do not invoke signed arithmetic overflow: */
1889
17168
    int neededSize = (int) ((unsigned)len + (unsigned)(bufferEnd - bufferPtr));
1890
17168
    if (neededSize < 0) {
1891
10
      errorCode = XML_ERROR_NO_MEMORY;
1892
10
      return NULL;
1893
    }
1894
#ifdef XML_CONTEXT_BYTES
1895
17158
    keep = (int)(bufferPtr - buffer);
1896
17158
    if (keep > XML_CONTEXT_BYTES)
1897
      keep = XML_CONTEXT_BYTES;
1898
17158
    neededSize += keep;
1899
#endif  /* defined XML_CONTEXT_BYTES */
1900
17158
    if (neededSize  <= bufferLim - buffer) {
1901
#ifdef XML_CONTEXT_BYTES
1902
660
      if (keep < bufferPtr - buffer) {
1903
660
        int offset = (int)(bufferPtr - buffer) - keep;
1904
660
        memmove(buffer, &buffer[offset], bufferEnd - bufferPtr + keep);
1905
660
        bufferEnd -= offset;
1906
660
        bufferPtr -= offset;
1907
660
      }
1908
#else
1909
      memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
1910
      bufferEnd = buffer + (bufferEnd - bufferPtr);
1911
      bufferPtr = buffer;
1912
#endif  /* not defined XML_CONTEXT_BYTES */
1913
    }
1914
    else {
1915
      char *newBuf;
1916
16498
      int bufferSize = (int)(bufferLim - bufferPtr);
1917
16498
      if (bufferSize == 0)
1918
        bufferSize = INIT_BUFFER_SIZE;
1919
16498
      do {
1920
        /* Do not invoke signed arithmetic overflow: */
1921
17318
        bufferSize = (int) (2U * (unsigned) bufferSize);
1922
17318
      } while (bufferSize < neededSize && bufferSize > 0);
1923
16498
      if (bufferSize <= 0) {
1924
10
        errorCode = XML_ERROR_NO_MEMORY;
1925
10
        return NULL;
1926
      }
1927
16488
      newBuf = (char *)MALLOC(bufferSize);
1928
16488
      if (newBuf == 0) {
1929
709
        errorCode = XML_ERROR_NO_MEMORY;
1930
709
        return NULL;
1931
      }
1932
15779
      bufferLim = newBuf + bufferSize;
1933
#ifdef XML_CONTEXT_BYTES
1934
15779
      if (bufferPtr) {
1935
1460
        int keep = (int)(bufferPtr - buffer);
1936
1460
        if (keep > XML_CONTEXT_BYTES)
1937
          keep = XML_CONTEXT_BYTES;
1938
1460
        memcpy(newBuf, &bufferPtr[-keep], bufferEnd - bufferPtr + keep);
1939
1460
        FREE(buffer);
1940
1460
        buffer = newBuf;
1941
1460
        bufferEnd = buffer + (bufferEnd - bufferPtr) + keep;
1942
1460
        bufferPtr = buffer + keep;
1943
1460
      }
1944
      else {
1945
14319
        bufferEnd = newBuf + (bufferEnd - bufferPtr);
1946
14319
        bufferPtr = buffer = newBuf;
1947
      }
1948
#else
1949
      if (bufferPtr) {
1950
        memcpy(newBuf, bufferPtr, bufferEnd - bufferPtr);
1951
        FREE(buffer);
1952
      }
1953
      bufferEnd = newBuf + (bufferEnd - bufferPtr);
1954
      bufferPtr = buffer = newBuf;
1955
#endif  /* not defined XML_CONTEXT_BYTES */
1956
15779
    }
1957
16439
    eventPtr = eventEndPtr = NULL;
1958
16439
    positionPtr = NULL;
1959
16439
  }
1960
7376392
  return bufferEnd;
1961
7377151
}
1962
1963
enum XML_Status XMLCALL
1964
XML_StopParser(XML_Parser parser, XML_Bool resumable)
1965
{
1966
600
  if (parser == NULL)
1967
    return XML_STATUS_ERROR;
1968
300
  switch (ps_parsing) {
1969
  case XML_SUSPENDED:
1970
20
    if (resumable) {
1971
10
      errorCode = XML_ERROR_SUSPENDED;
1972
10
      return XML_STATUS_ERROR;
1973
    }
1974
    ps_parsing = XML_FINISHED;
1975
    break;
1976
  case XML_FINISHED:
1977
10
    errorCode = XML_ERROR_FINISHED;
1978
10
    return XML_STATUS_ERROR;
1979
  default:
1980
270
    if (resumable) {
1981
#ifdef XML_DTD
1982
190
      if (isParamEntity) {
1983
10
        errorCode = XML_ERROR_SUSPEND_PE;
1984
10
        return XML_STATUS_ERROR;
1985
      }
1986
#endif
1987
      ps_parsing = XML_SUSPENDED;
1988
    }
1989
    else
1990
      ps_parsing = XML_FINISHED;
1991
  }
1992
270
  return XML_STATUS_OK;
1993
300
}
1994
1995
enum XML_Status XMLCALL
1996
XML_ResumeParser(XML_Parser parser)
1997
{
1998
  enum XML_Status result = XML_STATUS_OK;
1999
2000
220
  if (parser == NULL)
2001
    return XML_STATUS_ERROR;
2002
110
  if (ps_parsing != XML_SUSPENDED) {
2003
10
    errorCode = XML_ERROR_NOT_SUSPENDED;
2004
10
    return XML_STATUS_ERROR;
2005
  }
2006
100
  ps_parsing = XML_PARSING;
2007
2008
100
  errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
2009
2010
100
  if (errorCode != XML_ERROR_NONE) {
2011
20
    eventEndPtr = eventPtr;
2012
20
    processor = errorProcessor;
2013
20
    return XML_STATUS_ERROR;
2014
  }
2015
  else {
2016

120
    switch (ps_parsing) {
2017
    case XML_SUSPENDED:
2018
      result = XML_STATUS_SUSPENDED;
2019
20
      break;
2020
    case XML_INITIALIZED:
2021
    case XML_PARSING:
2022
60
      if (ps_finalBuffer) {
2023
40
        ps_parsing = XML_FINISHED;
2024
40
        return result;
2025
      }
2026
    default: ;
2027
    }
2028
  }
2029
2030
40
  XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
2031
40
  positionPtr = bufferPtr;
2032
40
  return result;
2033
110
}
2034
2035
void XMLCALL
2036
XML_GetParsingStatus(XML_Parser parser, XML_ParsingStatus *status)
2037
{
2038
200
  if (parser == NULL)
2039
    return;
2040
100
  assert(status != NULL);
2041
100
  *status = parser->m_parsingStatus;
2042
200
}
2043
2044
enum XML_Error XMLCALL
2045
XML_GetErrorCode(XML_Parser parser)
2046
{
2047
6600
  if (parser == NULL)
2048
    return XML_ERROR_INVALID_ARGUMENT;
2049
3300
  return errorCode;
2050
3300
}
2051
2052
XML_Index XMLCALL
2053
XML_GetCurrentByteIndex(XML_Parser parser)
2054
{
2055
60
  if (parser == NULL)
2056
    return -1;
2057
30
  if (eventPtr)
2058
20
    return (XML_Index)(parseEndByteIndex - (parseEndPtr - eventPtr));
2059
10
  return -1;
2060
30
}
2061
2062
int XMLCALL
2063
XML_GetCurrentByteCount(XML_Parser parser)
2064
{
2065
60
  if (parser == NULL)
2066
    return 0;
2067

40
  if (eventEndPtr && eventPtr)
2068
10
    return (int)(eventEndPtr - eventPtr);
2069
20
  return 0;
2070
30
}
2071
2072
const char * XMLCALL
2073
XML_GetInputContext(XML_Parser parser, int *offset, int *size)
2074
{
2075
#ifdef XML_CONTEXT_BYTES
2076
20
  if (parser == NULL)
2077
    return NULL;
2078

10
  if (eventPtr && buffer) {
2079
    if (offset != NULL)
2080
      *offset = (int)(eventPtr - buffer);
2081
    if (size != NULL)
2082
      *size   = (int)(bufferEnd - buffer);
2083
    return buffer;
2084
  }
2085
#else
2086
  (void)parser;
2087
  (void)offset;
2088
  (void)size;
2089
#endif /* defined XML_CONTEXT_BYTES */
2090
10
  return (char *) 0;
2091
10
}
2092
2093
XML_Size XMLCALL
2094
XML_GetCurrentLineNumber(XML_Parser parser)
2095
{
2096
240
  if (parser == NULL)
2097
    return 0;
2098

240
  if (eventPtr && eventPtr >= positionPtr) {
2099
120
    XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
2100
120
    positionPtr = eventPtr;
2101
120
  }
2102
120
  return position.lineNumber + 1;
2103
120
}
2104
2105
XML_Size XMLCALL
2106
XML_GetCurrentColumnNumber(XML_Parser parser)
2107
{
2108
240
  if (parser == NULL)
2109
    return 0;
2110

240
  if (eventPtr && eventPtr >= positionPtr) {
2111
120
    XmlUpdatePosition(encoding, positionPtr, eventPtr, &position);
2112
120
    positionPtr = eventPtr;
2113
120
  }
2114
120
  return position.columnNumber;
2115
120
}
2116
2117
void XMLCALL
2118
XML_FreeContentModel(XML_Parser parser, XML_Content *model)
2119
{
2120
660
  if (parser != NULL)
2121
330
    FREE(model);
2122
330
}
2123
2124
void * XMLCALL
2125
XML_MemMalloc(XML_Parser parser, size_t size)
2126
{
2127
20
  if (parser == NULL)
2128
    return NULL;
2129
10
  return MALLOC(size);
2130
10
}
2131
2132
void * XMLCALL
2133
XML_MemRealloc(XML_Parser parser, void *ptr, size_t size)
2134
{
2135
20
  if (parser == NULL)
2136
    return NULL;
2137
10
  return REALLOC(ptr, size);
2138
10
}
2139
2140
void XMLCALL
2141
XML_MemFree(XML_Parser parser, void *ptr)
2142
{
2143
20
  if (parser != NULL)
2144
10
    FREE(ptr);
2145
10
}
2146
2147
void XMLCALL
2148
XML_DefaultCurrent(XML_Parser parser)
2149
{
2150
120
  if (parser == NULL)
2151
    return;
2152
60
  if (defaultHandler) {
2153
60
    if (openInternalEntities)
2154
10
      reportDefault(parser,
2155
10
                    internalEncoding,
2156
10
                    openInternalEntities->internalEventPtr,
2157
10
                    openInternalEntities->internalEventEndPtr);
2158
    else
2159
50
      reportDefault(parser, encoding, eventPtr, eventEndPtr);
2160
  }
2161
60
}
2162
2163
const XML_LChar * XMLCALL
2164
XML_ErrorString(enum XML_Error code)
2165
{
2166
  static const XML_LChar* const message[] = {
2167
    0,
2168
    XML_L("out of memory"),
2169
    XML_L("syntax error"),
2170
    XML_L("no element found"),
2171
    XML_L("not well-formed (invalid token)"),
2172
    XML_L("unclosed token"),
2173
    XML_L("partial character"),
2174
    XML_L("mismatched tag"),
2175
    XML_L("duplicate attribute"),
2176
    XML_L("junk after document element"),
2177
    XML_L("illegal parameter entity reference"),
2178
    XML_L("undefined entity"),
2179
    XML_L("recursive entity reference"),
2180
    XML_L("asynchronous entity"),
2181
    XML_L("reference to invalid character number"),
2182
    XML_L("reference to binary entity"),
2183
    XML_L("reference to external entity in attribute"),
2184
    XML_L("XML or text declaration not at start of entity"),
2185
    XML_L("unknown encoding"),
2186
    XML_L("encoding specified in XML declaration is incorrect"),
2187
    XML_L("unclosed CDATA section"),
2188
    XML_L("error in processing external entity reference"),
2189
    XML_L("document is not standalone"),
2190
    XML_L("unexpected parser state - please send a bug report"),
2191
    XML_L("entity declared in parameter entity"),
2192
    XML_L("requested feature requires XML_DTD support in Expat"),
2193
    XML_L("cannot change setting once parsing has begun"),
2194
    XML_L("unbound prefix"),
2195
    XML_L("must not undeclare prefix"),
2196
    XML_L("incomplete markup in parameter entity"),
2197
    XML_L("XML declaration not well-formed"),
2198
    XML_L("text declaration not well-formed"),
2199
    XML_L("illegal character(s) in public id"),
2200
    XML_L("parser suspended"),
2201
    XML_L("parser not suspended"),
2202
    XML_L("parsing aborted"),
2203
    XML_L("parsing finished"),
2204
    XML_L("cannot suspend in external parameter entity"),
2205
    XML_L("reserved prefix (xml) must not be undeclared or bound to another namespace name"),
2206
    XML_L("reserved prefix (xmlns) must not be declared or undeclared"),
2207
    XML_L("prefix must not be bound to one of the reserved namespace names")
2208
  };
2209

60
  if (code > 0 && code < sizeof(message)/sizeof(message[0]))
2210
    return message[code];
2211
20
  return NULL;
2212
20
}
2213
2214
const XML_LChar * XMLCALL
2215
XML_ExpatVersion(void) {
2216
2217
  /* V1 is used to string-ize the version number. However, it would
2218
     string-ize the actual version macro *names* unless we get them
2219
     substituted before being passed to V1. CPP is defined to expand
2220
     a macro, then rescan for more expansions. Thus, we use V2 to expand
2221
     the version macros, then CPP will expand the resulting V1() macro
2222
     with the correct numerals. */
2223
  /* ### I'm assuming cpp is portable in this respect... */
2224
2225
#define V1(a,b,c) XML_L(#a)XML_L(".")XML_L(#b)XML_L(".")XML_L(#c)
2226
#define V2(a,b,c) XML_L("expat_")V1(a,b,c)
2227
2228
40
  return V2(XML_MAJOR_VERSION, XML_MINOR_VERSION, XML_MICRO_VERSION);
2229
2230
#undef V1
2231
#undef V2
2232
}
2233
2234
XML_Expat_Version XMLCALL
2235
XML_ExpatVersionInfo(void)
2236
{
2237
  XML_Expat_Version version;
2238
2239
  version.major = XML_MAJOR_VERSION;
2240
  version.minor = XML_MINOR_VERSION;
2241
  version.micro = XML_MICRO_VERSION;
2242
2243
  return version;
2244
20
}
2245
2246
const XML_Feature * XMLCALL
2247
XML_GetFeatureList(void)
2248
{
2249
  static const XML_Feature features[] = {
2250
    {XML_FEATURE_SIZEOF_XML_CHAR,  XML_L("sizeof(XML_Char)"),
2251
     sizeof(XML_Char)},
2252
    {XML_FEATURE_SIZEOF_XML_LCHAR, XML_L("sizeof(XML_LChar)"),
2253
     sizeof(XML_LChar)},
2254
#ifdef XML_UNICODE
2255
    {XML_FEATURE_UNICODE,          XML_L("XML_UNICODE"), 0},
2256
#endif
2257
#ifdef XML_UNICODE_WCHAR_T
2258
    {XML_FEATURE_UNICODE_WCHAR_T,  XML_L("XML_UNICODE_WCHAR_T"), 0},
2259
#endif
2260
#ifdef XML_DTD
2261
    {XML_FEATURE_DTD,              XML_L("XML_DTD"), 0},
2262
#endif
2263
#ifdef XML_CONTEXT_BYTES
2264
    {XML_FEATURE_CONTEXT_BYTES,    XML_L("XML_CONTEXT_BYTES"),
2265
     XML_CONTEXT_BYTES},
2266
#endif
2267
#ifdef XML_MIN_SIZE
2268
    {XML_FEATURE_MIN_SIZE,         XML_L("XML_MIN_SIZE"), 0},
2269
#endif
2270
#ifdef XML_NS
2271
    {XML_FEATURE_NS,               XML_L("XML_NS"), 0},
2272
#endif
2273
#ifdef XML_LARGE_SIZE
2274
    {XML_FEATURE_LARGE_SIZE,       XML_L("XML_LARGE_SIZE"), 0},
2275
#endif
2276
#ifdef XML_ATTR_INFO
2277
    {XML_FEATURE_ATTR_INFO,        XML_L("XML_ATTR_INFO"), 0},
2278
#endif
2279
    {XML_FEATURE_END,              NULL, 0}
2280
  };
2281
2282
40
  return features;
2283
}
2284
2285
/* Initially tag->rawName always points into the parse buffer;
2286
   for those TAG instances opened while the current parse buffer was
2287
   processed, and not yet closed, we need to store tag->rawName in a more
2288
   permanent location, since the parse buffer is about to be discarded.
2289
*/
2290
static XML_Bool
2291
storeRawNames(XML_Parser parser)
2292
{
2293
7613056
  TAG *tag = tagStack;
2294
7619580
  while (tag) {
2295
    int bufSize;
2296
367196
    int nameLen = sizeof(XML_Char) * (tag->name.strLen + 1);
2297
367196
    char *rawNameBuf = tag->buf + nameLen;
2298
    /* Stop if already stored.  Since tagStack is a stack, we can stop
2299
       at the first entry that has already been copied; everything
2300
       below it in the stack is already been accounted for in a
2301
       previous call to this function.
2302
    */
2303
367196
    if (tag->rawName == rawNameBuf)
2304
360622
      break;
2305
    /* For re-use purposes we need to ensure that the
2306
       size of tag->buf is a multiple of sizeof(XML_Char).
2307
    */
2308
6574
    bufSize = nameLen + ROUND_UP(tag->rawNameLength, sizeof(XML_Char));
2309
6574
    if (bufSize > tag->bufEnd - tag->buf) {
2310
350
      char *temp = (char *)REALLOC(tag->buf, bufSize);
2311
350
      if (temp == NULL)
2312
50
        return XML_FALSE;
2313
      /* if tag->name.str points to tag->buf (only when namespace
2314
         processing is off) then we have to update it
2315
      */
2316
300
      if (tag->name.str == (XML_Char *)tag->buf)
2317
60
        tag->name.str = (XML_Char *)temp;
2318
      /* if tag->name.localPart is set (when namespace processing is on)
2319
         then update it as well, since it will always point into tag->buf
2320
      */
2321
300
      if (tag->name.localPart)
2322
240
        tag->name.localPart = (XML_Char *)temp + (tag->name.localPart -
2323
240
                                                  (XML_Char *)tag->buf);
2324
300
      tag->buf = temp;
2325
300
      tag->bufEnd = temp + bufSize;
2326
300
      rawNameBuf = temp + nameLen;
2327
300
    }
2328
6524
    memcpy(rawNameBuf, tag->rawName, tag->rawNameLength);
2329
6524
    tag->rawName = rawNameBuf;
2330
6524
    tag = tag->parent;
2331
6524
  }
2332
3806478
  return XML_TRUE;
2333
3806528
}
2334
2335
static enum XML_Error PTRCALL
2336
contentProcessor(XML_Parser parser,
2337
                 const char *start,
2338
                 const char *end,
2339
                 const char **endPtr)
2340
{
2341
11437416
  enum XML_Error result = doContent(parser, 0, encoding, start, end,
2342
3812472
                                    endPtr, (XML_Bool)!ps_finalBuffer);
2343
3812472
  if (result == XML_ERROR_NONE) {
2344
3805118
    if (!storeRawNames(parser))
2345
40
      return XML_ERROR_NO_MEMORY;
2346
  }
2347
3812432
  return result;
2348
3812472
}
2349
2350
static enum XML_Error PTRCALL
2351
externalEntityInitProcessor(XML_Parser parser,
2352
                            const char *start,
2353
                            const char *end,
2354
                            const char **endPtr)
2355
{
2356
1120
  enum XML_Error result = initializeEncoding(parser);
2357
560
  if (result != XML_ERROR_NONE)
2358
10
    return result;
2359
550
  processor = externalEntityInitProcessor2;
2360
550
  return externalEntityInitProcessor2(parser, start, end, endPtr);
2361
560
}
2362
2363
static enum XML_Error PTRCALL
2364
externalEntityInitProcessor2(XML_Parser parser,
2365
                             const char *start,
2366
                             const char *end,
2367
                             const char **endPtr)
2368
{
2369
8960
  const char *next = start; /* XmlContentTok doesn't always set the last arg */
2370
4480
  int tok = XmlContentTok(encoding, start, end, &next);
2371

4480
  switch (tok) {
2372
  case XML_TOK_BOM:
2373
    /* If we are at the end of the buffer, this would cause the next stage,
2374
       i.e. externalEntityInitProcessor3, to pass control directly to
2375
       doContent (by detecting XML_TOK_NONE) without processing any xml text
2376
       declaration - causing the error XML_ERROR_MISPLACED_XML_PI in doContent.
2377
    */
2378

20
    if (next == end && !ps_finalBuffer) {
2379
10
      *endPtr = next;
2380
10
      return XML_ERROR_NONE;
2381
    }
2382
    start = next;
2383
    break;
2384
  case XML_TOK_PARTIAL:
2385
3920
    if (!ps_finalBuffer) {
2386
3910
      *endPtr = start;
2387
3910
      return XML_ERROR_NONE;
2388
    }
2389
10
    eventPtr = start;
2390
10
    return XML_ERROR_UNCLOSED_TOKEN;
2391
  case XML_TOK_PARTIAL_CHAR:
2392
20
    if (!ps_finalBuffer) {
2393
10
      *endPtr = start;
2394
10
      return XML_ERROR_NONE;
2395
    }
2396
10
    eventPtr = start;
2397
10
    return XML_ERROR_PARTIAL_CHAR;
2398
  }
2399
530
  processor = externalEntityInitProcessor3;
2400
530
  return externalEntityInitProcessor3(parser, start, end, endPtr);
2401
4480
}
2402
2403
static enum XML_Error PTRCALL
2404
externalEntityInitProcessor3(XML_Parser parser,
2405
                             const char *start,
2406
                             const char *end,
2407
                             const char **endPtr)
2408
{
2409
  int tok;
2410
1140
  const char *next = start; /* XmlContentTok doesn't always set the last arg */
2411
570
  eventPtr = start;
2412
570
  tok = XmlContentTok(encoding, start, end, &next);
2413
570
  eventEndPtr = next;
2414
2415

570
  switch (tok) {
2416
  case XML_TOK_XML_DECL:
2417
    {
2418
      enum XML_Error result;
2419
110
      result = processXmlDecl(parser, 1, start, next);
2420
110
      if (result != XML_ERROR_NONE)
2421
        return result;
2422
110
      switch (ps_parsing) {
2423
      case XML_SUSPENDED:
2424
30
        *endPtr = next;
2425
30
        return XML_ERROR_NONE;
2426
      case XML_FINISHED:
2427
10
        return XML_ERROR_ABORTED;
2428
      default:
2429
70
        start = next;
2430
      }
2431
70
    }
2432
    break;
2433
  case XML_TOK_PARTIAL:
2434
20
    if (!ps_finalBuffer) {
2435
10
      *endPtr = start;
2436
10
      return XML_ERROR_NONE;
2437
    }
2438
10
    return XML_ERROR_UNCLOSED_TOKEN;
2439
  case XML_TOK_PARTIAL_CHAR:
2440
20
    if (!ps_finalBuffer) {
2441
10
      *endPtr = start;
2442
10
      return XML_ERROR_NONE;
2443
    }
2444
10
    return XML_ERROR_PARTIAL_CHAR;
2445
  }
2446
490
  processor = externalEntityContentProcessor;
2447
490
  tagLevel = 1;
2448
490
  return externalEntityContentProcessor(parser, start, end, endPtr);
2449
570
}
2450
2451
static enum XML_Error PTRCALL
2452
externalEntityContentProcessor(XML_Parser parser,
2453
                               const char *start,
2454
                               const char *end,
2455
                               const char **endPtr)
2456
{
2457
4680
  enum XML_Error result = doContent(parser, 1, encoding, start, end,
2458
1560
                                    endPtr, (XML_Bool)!ps_finalBuffer);
2459
1560
  if (result == XML_ERROR_NONE) {
2460
1410
    if (!storeRawNames(parser))
2461
10
      return XML_ERROR_NO_MEMORY;
2462
  }
2463
1550
  return result;
2464
1560
}
2465
2466
static enum XML_Error
2467
doContent(XML_Parser parser,
2468
          int startTagLevel,
2469
          const ENCODING *enc,
2470
          const char *s,
2471
          const char *end,
2472
          const char **nextPtr,
2473
          XML_Bool haveMore)
2474
{
2475
  /* save one level of indirection */
2476
3814182
  DTD * const dtd = _dtd;
2477
2478
  const char **eventPP;
2479
  const char **eventEndPP;
2480
3814182
  if (enc == encoding) {
2481
3814042
    eventPP = &eventPtr;
2482
3814042
    eventEndPP = &eventEndPtr;
2483
3814042
  }
2484
  else {
2485
140
    eventPP = &(openInternalEntities->internalEventPtr);
2486
140
    eventEndPP = &(openInternalEntities->internalEventEndPtr);
2487
  }
2488
3814182
  *eventPP = s;
2489
2490
3814182
  for (;;) {
2491
127691516
    const char *next = s; /* XmlContentTok doesn't always set the last arg */
2492
127691516
    int tok = XmlContentTok(enc, s, end, &next);
2493
127691516
    *eventEndPP = next;
2494





127691516
    switch (tok) {
2495
    case XML_TOK_TRAILING_CR:
2496
60
      if (haveMore) {
2497
20
        *nextPtr = s;
2498
20
        return XML_ERROR_NONE;
2499
      }
2500
40
      *eventEndPP = end;
2501
40
      if (characterDataHandler) {
2502
30
        XML_Char c = 0xA;
2503
30
        characterDataHandler(handlerArg, &c, 1);
2504
30
      }
2505
10
      else if (defaultHandler)
2506
10
        reportDefault(parser, enc, s, end);
2507
      /* We are at the end of the final buffer, should we check for
2508
         XML_SUSPENDED, XML_FINISHED?
2509
      */
2510
40
      if (startTagLevel == 0)
2511
20
        return XML_ERROR_NO_ELEMENTS;
2512
20
      if (tagLevel != startTagLevel)
2513
10
        return XML_ERROR_ASYNC_ENTITY;
2514
10
      *nextPtr = end;
2515
10
      return XML_ERROR_NONE;
2516
    case XML_TOK_NONE:
2517
27134
      if (haveMore) {
2518
26744
        *nextPtr = s;
2519
26744
        return XML_ERROR_NONE;
2520
      }
2521
390
      if (startTagLevel > 0) {
2522
380
        if (tagLevel != startTagLevel)
2523
          return XML_ERROR_ASYNC_ENTITY;
2524
380
        *nextPtr = s;
2525
380
        return XML_ERROR_NONE;
2526
      }
2527
10
      return XML_ERROR_NO_ELEMENTS;
2528
    case XML_TOK_INVALID:
2529
1470
      *eventPP = next;
2530
1470
      return XML_ERROR_INVALID_TOKEN;
2531
    case XML_TOK_PARTIAL:
2532
3725944
      if (haveMore) {
2533
3725664
        *nextPtr = s;
2534
3725664
        return XML_ERROR_NONE;
2535
      }
2536
280
      return XML_ERROR_UNCLOSED_TOKEN;
2537
    case XML_TOK_PARTIAL_CHAR:
2538
1060
      if (haveMore) {
2539
1050
        *nextPtr = s;
2540
1050
        return XML_ERROR_NONE;
2541
      }
2542
10
      return XML_ERROR_PARTIAL_CHAR;
2543
    case XML_TOK_ENTITY_REF:
2544
      {
2545
        const XML_Char *name;
2546
        ENTITY *entity;
2547
5002924
        XML_Char ch = (XML_Char) XmlPredefinedEntityName(enc,
2548
                                              s + enc->minBytesPerChar,
2549
                                              next - enc->minBytesPerChar);
2550
5002924
        if (ch) {
2551
5000110
          if (characterDataHandler)
2552
60
            characterDataHandler(handlerArg, &ch, 1);
2553
5000050
          else if (defaultHandler)
2554
50
            reportDefault(parser, enc, s, next);
2555
5000110
          break;
2556
        }
2557
5628
        name = poolStoreString(&dtd->pool, enc,
2558
2814
                                s + enc->minBytesPerChar,
2559
2814
                                next - enc->minBytesPerChar);
2560
2814
        if (!name)
2561
10
          return XML_ERROR_NO_MEMORY;
2562
2804
        entity = (ENTITY *)lookup(parser, &dtd->generalEntities, name, 0);
2563
2804
        poolDiscard(&dtd->pool);
2564
        /* First, determine if a check for an existing declaration is needed;
2565
           if yes, check that the entity exists, and that it is internal,
2566
           otherwise call the skipped entity or default handler.
2567
        */
2568

4104
        if (!dtd->hasParamEntityRefs || dtd->standalone) {
2569
1534
          if (!entity)
2570
100
            return XML_ERROR_UNDEFINED_ENTITY;
2571
1434
          else if (!entity->is_internal)
2572
            return XML_ERROR_ENTITY_DECLARED_IN_PE;
2573
        }
2574
1270
        else if (!entity) {
2575
150
          if (skippedEntityHandler)
2576
10
            skippedEntityHandler(handlerArg, name, 0);
2577
140
          else if (defaultHandler)
2578
40
            reportDefault(parser, enc, s, next);
2579
150
          break;
2580
        }
2581
2554
        if (entity->open)
2582
10
          return XML_ERROR_RECURSIVE_ENTITY_REF;
2583
2544
        if (entity->notation)
2584
          return XML_ERROR_BINARY_ENTITY_REF;
2585
2544
        if (entity->textPtr) {
2586
          enum XML_Error result;
2587
140
          if (!defaultExpandInternalEntities) {
2588
30
            if (skippedEntityHandler)
2589
10
              skippedEntityHandler(handlerArg, entity->name, 0);
2590
20
            else if (defaultHandler)
2591
20
              reportDefault(parser, enc, s, next);
2592
30
            break;
2593
          }
2594
110
          result = processInternalEntity(parser, entity, XML_FALSE);
2595
110
          if (result != XML_ERROR_NONE)
2596
20
            return result;
2597
90
        }
2598
2404
        else if (externalEntityRefHandler) {
2599
          const XML_Char *context;
2600
2394
          entity->open = XML_TRUE;
2601
2394
          context = getContext(parser);
2602
2394
          entity->open = XML_FALSE;
2603
2394
          if (!context)
2604
130
            return XML_ERROR_NO_MEMORY;
2605
4528
          if (!externalEntityRefHandler(externalEntityRefHandlerArg,
2606
                                        context,
2607
2264
                                        entity->base,
2608
2264
                                        entity->systemId,
2609
2264
                                        entity->publicId))
2610
1894
            return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
2611
370
          poolDiscard(&tempPool);
2612
370
        }
2613
10
        else if (defaultHandler)
2614
10
          reportDefault(parser, enc, s, next);
2615
470
        break;
2616
5002924
      }
2617
    case XML_TOK_START_TAG_NO_ATTS:
2618
      /* fall through */
2619
    case XML_TOK_START_TAG_WITH_ATTS:
2620
      {
2621
        TAG *tag;
2622
        enum XML_Error result;
2623
16259424
        XML_Char *toPtr;
2624
16259424
        if (freeTagList) {
2625
          tag = freeTagList;
2626
16252155
          freeTagList = freeTagList->parent;
2627
16252155
        }
2628
        else {
2629
7269
          tag = (TAG *)MALLOC(sizeof(TAG));
2630
7269
          if (!tag)
2631
370
            return XML_ERROR_NO_MEMORY;
2632
6899
          tag->buf = (char *)MALLOC(INIT_TAG_BUF_SIZE);
2633
6899
          if (!tag->buf) {
2634
370
            FREE(tag);
2635
370
            return XML_ERROR_NO_MEMORY;
2636
          }
2637
6529
          tag->bufEnd = tag->buf + INIT_TAG_BUF_SIZE;
2638
        }
2639
16258684
        tag->bindings = NULL;
2640
16258684
        tag->parent = tagStack;
2641
16258684
        tagStack = tag;
2642
16258684
        tag->name.localPart = NULL;
2643
16258684
        tag->name.prefix = NULL;
2644
16258684
        tag->rawName = s + enc->minBytesPerChar;
2645
16258684
        tag->rawNameLength = XmlNameLength(enc, tag->rawName);
2646
16258684
        ++tagLevel;
2647
        {
2648
16258684
          const char *rawNameEnd = tag->rawName + tag->rawNameLength;
2649
16258684
          const char *fromPtr = tag->rawName;
2650
16258684
          toPtr = (XML_Char *)tag->buf;
2651
16258684
          for (;;) {
2652
            int bufSize;
2653
            int convLen;
2654
16263544
            const enum XML_Convert_Result convert_res = XmlConvert(enc,
2655
                       &fromPtr, rawNameEnd,
2656
                       (ICHAR **)&toPtr, (ICHAR *)tag->bufEnd - 1);
2657
16263544
            convLen = (int)(toPtr - (XML_Char *)tag->buf);
2658
16263544
            if ((fromPtr >= rawNameEnd) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE)) {
2659
16258384
              tag->name.strLen = convLen;
2660
16258384
              break;
2661
            }
2662
5160
            bufSize = (int)(tag->bufEnd - tag->buf) << 1;
2663
            {
2664
5160
              char *temp = (char *)REALLOC(tag->buf, bufSize);
2665
5160
              if (temp == NULL)
2666
300
                return XML_ERROR_NO_MEMORY;
2667
4860
              tag->buf = temp;
2668
4860
              tag->bufEnd = temp + bufSize;
2669
4860
              toPtr = (XML_Char *)temp + convLen;
2670
4860
            }
2671
4860
          }
2672
32517068
        }
2673
16258384
        tag->name.str = (XML_Char *)tag->buf;
2674
16258384
        *toPtr = XML_T('\0');
2675
16258384
        result = storeAtts(parser, enc, s, &(tag->name), &(tag->bindings));
2676
16258384
        if (result)
2677
1740
          return result;
2678
16256644
        if (startElementHandler)
2679
760
          startElementHandler(handlerArg, tag->name.str,
2680
380
                              (const XML_Char **)atts);
2681
16256264
        else if (defaultHandler)
2682
230
          reportDefault(parser, enc, s, next);
2683
16256644
        poolClear(&tempPool);
2684
16256644
        break;
2685
16259424
      }
2686
    case XML_TOK_EMPTY_ELEMENT_NO_ATTS:
2687
      /* fall through */
2688
    case XML_TOK_EMPTY_ELEMENT_WITH_ATTS:
2689
      {
2690
1760
        const char *rawName = s + enc->minBytesPerChar;
2691
        enum XML_Error result;
2692
1760
        BINDING *bindings = NULL;
2693
        XML_Bool noElmHandlers = XML_TRUE;
2694
1760
        TAG_NAME name;
2695
3520
        name.str = poolStoreString(&tempPool, enc, rawName,
2696
1760
                                   rawName + XmlNameLength(enc, rawName));
2697
1760
        if (!name.str)
2698
70
          return XML_ERROR_NO_MEMORY;
2699
1690
        poolFinish(&tempPool);
2700
1690
        result = storeAtts(parser, enc, s, &name, &bindings);
2701
1690
        if (result != XML_ERROR_NONE) {
2702
560
          freeBindings(parser, bindings);
2703
560
          return result;
2704
        }
2705
1130
        poolFinish(&tempPool);
2706
1130
        if (startElementHandler) {
2707
170
          startElementHandler(handlerArg, name.str, (const XML_Char **)atts);
2708
          noElmHandlers = XML_FALSE;
2709
170
        }
2710
1130
        if (endElementHandler) {
2711
90
          if (startElementHandler)
2712
60
            *eventPP = *eventEndPP;
2713
90
          endElementHandler(handlerArg, name.str);
2714
          noElmHandlers = XML_FALSE;
2715
90
        }
2716

2060
        if (noElmHandlers && defaultHandler)
2717
40
          reportDefault(parser, enc, s, next);
2718
1130
        poolClear(&tempPool);
2719
1130
        freeBindings(parser, bindings);
2720
2890
      }
2721
1130
      if (tagLevel == 0)
2722
470
        return epilogProcessor(parser, next, end, nextPtr);
2723
      break;
2724
    case XML_TOK_END_TAG:
2725
16252200
      if (tagLevel == startTagLevel)
2726
        return XML_ERROR_ASYNC_ENTITY;
2727
      else {
2728
        int len;
2729
        const char *rawName;
2730
16252200
        TAG *tag = tagStack;
2731
16252200
        tagStack = tag->parent;
2732
16252200
        tag->parent = freeTagList;
2733
16252200
        freeTagList = tag;
2734
16252200
        rawName = s + enc->minBytesPerChar*2;
2735
16252200
        len = XmlNameLength(enc, rawName);
2736
32504380
        if (len != tag->rawNameLength
2737
32504380
            || memcmp(tag->rawName, rawName, len) != 0) {
2738
40
          *eventPP = rawName;
2739
40
          return XML_ERROR_TAG_MISMATCH;
2740
        }
2741
16252160
        --tagLevel;
2742
16252160
        if (endElementHandler) {
2743
          const XML_Char *localPart;
2744
          const XML_Char *prefix;
2745
          XML_Char *uri;
2746
200
          localPart = tag->name.localPart;
2747
200
          if (ns && localPart) {
2748
            /* localPart and prefix may have been overwritten in
2749
               tag->name.str, since this points to the binding->uri
2750
               buffer which gets re-used; so we have to add them again
2751
            */
2752
70
            uri = (XML_Char *)tag->name.str + tag->name.uriLen;
2753
            /* don't need to check for space - already done in storeAtts() */
2754
2240
            while (*localPart) *uri++ = *localPart++;
2755
70
            prefix = (XML_Char *)tag->name.prefix;
2756
70
            if (ns_triplets && prefix) {
2757
60
              *uri++ = namespaceSeparator;
2758
440
              while (*prefix) *uri++ = *prefix++;
2759
             }
2760
70
            *uri = XML_T('\0');
2761
70
          }
2762
200
          endElementHandler(handlerArg, tag->name.str);
2763
200
        }
2764
16251960
        else if (defaultHandler)
2765
190
          reportDefault(parser, enc, s, next);
2766
16253060
        while (tag->bindings) {
2767
          BINDING *b = tag->bindings;
2768
450
          if (endNamespaceDeclHandler)
2769
60
            endNamespaceDeclHandler(handlerArg, b->prefix->name);
2770
450
          tag->bindings = tag->bindings->nextTagBinding;
2771
450
          b->nextTagBinding = freeBindingList;
2772
450
          freeBindingList = b;
2773
450
          b->prefix->binding = b->prevPrefixBinding;
2774
        }
2775
16252160
        if (tagLevel == 0)
2776
51920
          return epilogProcessor(parser, next, end, nextPtr);
2777
16200240
      }
2778
      break;
2779
    case XML_TOK_CHAR_REF:
2780
      {
2781
370
        int n = XmlCharRefNumber(enc, s);
2782
370
        if (n < 0)
2783
10
          return XML_ERROR_BAD_CHAR_REF;
2784
360
        if (characterDataHandler) {
2785
240
          XML_Char buf[XML_ENCODE_MAX];
2786
240
          characterDataHandler(handlerArg, buf, XmlEncode(n, (ICHAR *)buf));
2787
240
        }
2788
120
        else if (defaultHandler)
2789
60
          reportDefault(parser, enc, s, next);
2790
360
      }
2791
      break;
2792
    case XML_TOK_XML_DECL:
2793
      return XML_ERROR_MISPLACED_XML_PI;
2794
    case XML_TOK_DATA_NEWLINE:
2795
35651810
      if (characterDataHandler) {
2796
30
        XML_Char c = 0xA;
2797
30
        characterDataHandler(handlerArg, &c, 1);
2798
30
      }
2799
35651780
      else if (defaultHandler)
2800
50
        reportDefault(parser, enc, s, next);
2801
      break;
2802
    case XML_TOK_CDATA_SECT_OPEN:
2803
      {
2804
        enum XML_Error result;
2805
270
        if (startCdataSectionHandler)
2806
20
          startCdataSectionHandler(handlerArg);
2807
#if 0
2808
        /* Suppose you doing a transformation on a document that involves
2809
           changing only the character data.  You set up a defaultHandler
2810
           and a characterDataHandler.  The defaultHandler simply copies
2811
           characters through.  The characterDataHandler does the
2812
           transformation and writes the characters out escaping them as
2813
           necessary.  This case will fail to work if we leave out the
2814
           following two lines (because & and < inside CDATA sections will
2815
           be incorrectly escaped).
2816
2817
           However, now we have a start/endCdataSectionHandler, so it seems
2818
           easier to let the user deal with this.
2819
        */
2820
        else if (characterDataHandler)
2821
          characterDataHandler(handlerArg, dataBuf, 0);
2822
#endif
2823
250
        else if (defaultHandler)
2824
20
          reportDefault(parser, enc, s, next);
2825
270
        result = doCdataSection(parser, enc, &next, end, nextPtr, haveMore);
2826
270
        if (result != XML_ERROR_NONE)
2827
20
          return result;
2828
250
        else if (!next) {
2829
240
          processor = cdataSectionProcessor;
2830
240
          return result;
2831
        }
2832
10
      }
2833
      break;
2834
    case XML_TOK_TRAILING_RSQB:
2835
60
      if (haveMore) {
2836
20
        *nextPtr = s;
2837
20
        return XML_ERROR_NONE;
2838
      }
2839
40
      if (characterDataHandler) {
2840
30
        if (MUST_CONVERT(enc, s)) {
2841
10
          ICHAR *dataPtr = (ICHAR *)dataBuf;
2842
10
          XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
2843
20
          characterDataHandler(handlerArg, dataBuf,
2844
10
                               (int)(dataPtr - (ICHAR *)dataBuf));
2845
10
        }
2846
        else
2847
40
          characterDataHandler(handlerArg,
2848
20
                               (XML_Char *)s,
2849
20
                               (int)((XML_Char *)end - (XML_Char *)s));
2850
      }
2851
10
      else if (defaultHandler)
2852
10
        reportDefault(parser, enc, s, end);
2853
      /* We are at the end of the final buffer, should we check for
2854
         XML_SUSPENDED, XML_FINISHED?
2855
      */
2856
40
      if (startTagLevel == 0) {
2857
30
        *eventPP = end;
2858
30
        return XML_ERROR_NO_ELEMENTS;
2859
      }
2860
10
      if (tagLevel != startTagLevel) {
2861
10
        *eventPP = end;
2862
10
        return XML_ERROR_ASYNC_ENTITY;
2863
      }
2864
      *nextPtr = end;
2865
      return XML_ERROR_NONE;
2866
    case XML_TOK_DATA_CHARS:
2867
      {
2868
50716980
        XML_CharacterDataHandler charDataHandler = characterDataHandler;
2869
50716980
        if (charDataHandler) {
2870
1640
          if (MUST_CONVERT(enc, s)) {
2871
            for (;;) {
2872
1090
              ICHAR *dataPtr = (ICHAR *)dataBuf;
2873
1090
              const enum XML_Convert_Result convert_res = XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
2874
1090
              *eventEndPP = s;
2875
2180
              charDataHandler(handlerArg, dataBuf,
2876
1090
                              (int)(dataPtr - (ICHAR *)dataBuf));
2877
1090
              if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE))
2878
1080
                break;
2879
10
              *eventPP = s;
2880
1100
            }
2881
          }
2882
          else
2883
1120
            charDataHandler(handlerArg,
2884
560
                            (XML_Char *)s,
2885
560
                            (int)((XML_Char *)next - (XML_Char *)s));
2886
        }
2887
50715340
        else if (defaultHandler)
2888
210
          reportDefault(parser, enc, s, next);
2889
      }
2890
50716980
      break;
2891
    case XML_TOK_PI:
2892
20
      if (!reportProcessingInstruction(parser, enc, s, next))
2893
10
        return XML_ERROR_NO_MEMORY;
2894
      break;
2895
    case XML_TOK_COMMENT:
2896
50030
      if (!reportComment(parser, enc, s, next))
2897
10
        return XML_ERROR_NO_MEMORY;
2898
      break;
2899
    default:
2900
      /* All of the tokens produced by XmlContentTok() have their own
2901
       * explicit cases, so this default is not strictly necessary.
2902
       * However it is a useful safety net, so we retain the code and
2903
       * simply exclude it from the coverage tests.
2904
       *
2905
       * LCOV_EXCL_START
2906
       */
2907
      if (defaultHandler)
2908
        reportDefault(parser, enc, s, next);
2909
      break;
2910
      /* LCOV_EXCL_STOP */
2911
    }
2912
123877494
    *eventPP = s = next;
2913
123877494
    switch (ps_parsing) {
2914
    case XML_SUSPENDED:
2915
130
      *nextPtr = next;
2916
130
      return XML_ERROR_NONE;
2917
    case XML_FINISHED:
2918
30
      return XML_ERROR_ABORTED;
2919
    default: ;
2920
    }
2921
251568850
  }
2922
  /* not reached */
2923
3814182
}
2924
2925
/* This function does not call free() on the allocated memory, merely
2926
 * moving it to the parser's freeBindingList where it can be freed or
2927
 * reused as appropriate.
2928
 */
2929
static void
2930
freeBindings(XML_Parser parser, BINDING *bindings)
2931
{
2932
5350
  while (bindings) {
2933
    BINDING *b = bindings;
2934
2935
    /* startNamespaceDeclHandler will have been called for this
2936
     * binding in addBindings(), so call the end handler now.
2937
     */
2938
140
    if (endNamespaceDeclHandler)
2939
10
        endNamespaceDeclHandler(handlerArg, b->prefix->name);
2940
2941
140
    bindings = bindings->nextTagBinding;
2942
140
    b->nextTagBinding = freeBindingList;
2943
140
    freeBindingList = b;
2944
140
    b->prefix->binding = b->prevPrefixBinding;
2945
  }
2946
1690
}
2947
2948
/* Precondition: all arguments must be non-NULL;
2949
   Purpose:
2950
   - normalize attributes
2951
   - check attributes for well-formedness
2952
   - generate namespace aware attribute names (URI, prefix)
2953
   - build list of attributes for startElementHandler
2954
   - default attributes
2955
   - process namespace declarations (check and report them)
2956
   - generate namespace aware element name (URI, prefix)
2957
*/
2958
static enum XML_Error
2959
storeAtts(XML_Parser parser, const ENCODING *enc,
2960
          const char *attStr, TAG_NAME *tagNamePtr,
2961
          BINDING **bindingsPtr)
2962
{
2963
32520148
  DTD * const dtd = _dtd;  /* save one level of indirection */
2964
  ELEMENT_TYPE *elementType;
2965
  int nDefaultAtts;
2966
  const XML_Char **appAtts;   /* the attribute list for the application */
2967
  int attIndex = 0;
2968
  int prefixLen;
2969
  int i;
2970
  int n;
2971
  XML_Char *uri;
2972
  int nPrefixes = 0;
2973
  BINDING *binding;
2974
  const XML_Char *localPart;
2975
2976
  /* lookup the element type name */
2977
16260074
  elementType = (ELEMENT_TYPE *)lookup(parser, &dtd->elementTypes, tagNamePtr->str,0);
2978
16260074
  if (!elementType) {
2979
2058724
    const XML_Char *name = poolCopyString(&dtd->pool, tagNamePtr->str);
2980
2058724
    if (!name)
2981
220
      return XML_ERROR_NO_MEMORY;
2982
2058504
    elementType = (ELEMENT_TYPE *)lookup(parser, &dtd->elementTypes, name,
2983
                                         sizeof(ELEMENT_TYPE));
2984
2058504
    if (!elementType)
2985
790
      return XML_ERROR_NO_MEMORY;
2986

2060094
    if (ns && !setElementTypePrefix(parser, elementType))
2987
140
      return XML_ERROR_NO_MEMORY;
2988
2057574
  }
2989
16258924
  nDefaultAtts = elementType->nDefaultAtts;
2990
2991
  /* get the attributes from the tokenizer */
2992
16258924
  n = XmlGetAttributes(enc, attStr, attsSize, atts);
2993
16258924
  if (n + nDefaultAtts > attsSize) {
2994
    int oldAttsSize = attsSize;
2995
    ATTRIBUTE *temp;
2996
#ifdef XML_ATTR_INFO
2997
    XML_AttrInfo *temp2;
2998
#endif
2999
20
    attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
3000
20
    temp = (ATTRIBUTE *)REALLOC((void *)atts, attsSize * sizeof(ATTRIBUTE));
3001
20
    if (temp == NULL) {
3002
10
      attsSize = oldAttsSize;
3003
10
      return XML_ERROR_NO_MEMORY;
3004
    }
3005
10
    atts = temp;
3006
#ifdef XML_ATTR_INFO
3007
    temp2 = (XML_AttrInfo *)REALLOC((void *)attInfo, attsSize * sizeof(XML_AttrInfo));
3008
    if (temp2 == NULL) {
3009
      attsSize = oldAttsSize;
3010
      return XML_ERROR_NO_MEMORY;
3011
    }
3012
    attInfo = temp2;
3013
#endif
3014
10
    if (n > oldAttsSize)
3015
10
      XmlGetAttributes(enc, attStr, n, atts);
3016
10
  }
3017
3018
16258914
  appAtts = (const XML_Char **)atts;
3019
42024768
  for (i = 0; i < n; i++) {
3020
4754500
    ATTRIBUTE *currAtt = &atts[i];
3021
#ifdef XML_ATTR_INFO
3022
    XML_AttrInfo *currAttInfo = &attInfo[i];
3023
#endif
3024
    /* add the name and value to the attribute list */
3025
9509000
    ATTRIBUTE_ID *attId = getAttributeId(parser, enc, currAtt->name,
3026
                                         currAtt->name
3027
4754500
                                         + XmlNameLength(enc, currAtt->name));
3028
4754500
    if (!attId)
3029
560
      return XML_ERROR_NO_MEMORY;
3030
#ifdef XML_ATTR_INFO
3031
    currAttInfo->nameStart = parseEndByteIndex - (parseEndPtr - currAtt->name);
3032
    currAttInfo->nameEnd = currAttInfo->nameStart +
3033
                           XmlNameLength(enc, currAtt->name);
3034
    currAttInfo->valueStart = parseEndByteIndex -
3035
                            (parseEndPtr - currAtt->valuePtr);
3036
    currAttInfo->valueEnd = parseEndByteIndex - (parseEndPtr - currAtt->valueEnd);
3037
#endif
3038
    /* Detect duplicate attributes by their QNames. This does not work when
3039
       namespace processing is turned on and different prefixes for the same
3040
       namespace are used. For this case we have a check further down.
3041
    */
3042
4753940
    if ((attId->name)[-1]) {
3043
      if (enc == encoding)
3044
        eventPtr = atts[i].name;
3045
      return XML_ERROR_DUPLICATE_ATTRIBUTE;
3046
    }
3047
4753940
    (attId->name)[-1] = 1;
3048
4753940
    appAtts[attIndex++] = attId->name;
3049
4753940
    if (!atts[i].normalized) {
3050
      enum XML_Error result;
3051
      XML_Bool isCdata = XML_TRUE;
3052
3053
      /* figure out whether declared as other than CDATA */
3054
210
      if (attId->maybeTokenized) {
3055
        int j;
3056
120
        for (j = 0; j < nDefaultAtts; j++) {
3057
60
          if (attId == elementType->defaultAtts[j].id) {
3058
30
            isCdata = elementType->defaultAtts[j].isCdata;
3059
30
            break;
3060
          }
3061
        }
3062
30
      }
3063
3064
      /* normalize the attribute value */
3065
210
      result = storeAttributeValue(parser, enc, isCdata,
3066
210
                                   atts[i].valuePtr, atts[i].valueEnd,
3067
210
                                   &tempPool);
3068
210
      if (result)
3069
50
        return result;
3070
160
      appAtts[attIndex] = poolStart(&tempPool);
3071
160
      poolFinish(&tempPool);
3072
160
    }
3073
    else {
3074
      /* the value did not need normalizing */
3075
9507460
      appAtts[attIndex] = poolStoreString(&tempPool, enc, atts[i].valuePtr,
3076
4753730
                                          atts[i].valueEnd);
3077
4753730
      if (appAtts[attIndex] == 0)
3078
50
        return XML_ERROR_NO_MEMORY;
3079
4753680
      poolFinish(&tempPool);
3080
    }
3081
    /* handle prefixed attribute names */
3082
4753840
    if (attId->prefix) {
3083
2590
      if (attId->xmlns) {
3084
        /* deal with namespace declarations here */
3085
2140
        enum XML_Error result = addBinding(parser, attId->prefix, attId,
3086
2140
                                           appAtts[attIndex], bindingsPtr);
3087
2140
        if (result)
3088
370
          return result;
3089
        --attIndex;
3090
1770
      }
3091
      else {
3092
        /* deal with other prefixed names later */
3093
450
        attIndex++;
3094
450
        nPrefixes++;
3095
450
        (attId->name)[-1] = 2;
3096
      }
3097
    }
3098
    else
3099
4751250
      attIndex++;
3100
4753470
  }
3101
3102
  /* set-up for XML_GetSpecifiedAttributeCount and XML_GetIdAttributeIndex */
3103
16257884
  nSpecifiedAtts = attIndex;
3104

16258414
  if (elementType->idAtt && (elementType->idAtt->name)[-1]) {
3105
1060
    for (i = 0; i < attIndex; i += 2)
3106
530
      if (appAtts[i] == elementType->idAtt->name) {
3107
        idAttIndex = i;
3108
520
        break;
3109
      }
3110
  }
3111
  else
3112
    idAttIndex = -1;
3113
3114
  /* do attribute defaulting */
3115
48775932
  for (i = 0; i < nDefaultAtts; i++) {
3116
1150
    const DEFAULT_ATTRIBUTE *da = elementType->defaultAtts + i;
3117

1530
    if (!(da->id->name)[-1] && da->value) {
3118
250
      if (da->id->prefix) {
3119
30
        if (da->id->xmlns) {
3120
20
          enum XML_Error result = addBinding(parser, da->id->prefix, da->id,
3121
                                             da->value, bindingsPtr);
3122
20
          if (result)
3123
10
            return result;
3124
10
        }
3125
        else {
3126
10
          (da->id->name)[-1] = 2;
3127
10
          nPrefixes++;
3128
10
          appAtts[attIndex++] = da->id->name;
3129
10
          appAtts[attIndex++] = da->value;
3130
        }
3131
      }
3132
      else {
3133
220
        (da->id->name)[-1] = 1;
3134
220
        appAtts[attIndex++] = da->id->name;
3135
220
        appAtts[attIndex++] = da->value;
3136
      }
3137
230
    }
3138
1140
  }
3139
16257874
  appAtts[attIndex] = 0;
3140
3141
  /* expand prefixed attribute names, check for duplicates,
3142
     and clear flags that say whether attributes were specified */
3143
  i = 0;
3144
16257874
  if (nPrefixes) {
3145
    int j;  /* hash table index */
3146
290
    unsigned long version = nsAttsVersion;
3147
290
    int nsAttsSize = (int)1 << nsAttsPower;
3148
    unsigned char oldNsAttsPower = nsAttsPower;
3149
    /* size of hash table must be at least 2 * (# of prefixed attributes) */
3150
290
    if ((nPrefixes << 1) >> nsAttsPower) {  /* true for nsAttsPower = 0 */
3151
      NS_ATT *temp;
3152
      /* hash table size must also be a power of 2 and >= 8 */
3153
840
      while (nPrefixes >> nsAttsPower++);
3154
270
      if (nsAttsPower < 3)
3155
240
        nsAttsPower = 3;
3156
270
      nsAttsSize = (int)1 << nsAttsPower;
3157
270
      temp = (NS_ATT *)REALLOC(nsAtts, nsAttsSize * sizeof(NS_ATT));
3158
270
      if (!temp) {
3159
        /* Restore actual size of memory in nsAtts */
3160
10
        nsAttsPower = oldNsAttsPower;
3161
10
        return XML_ERROR_NO_MEMORY;
3162
      }
3163
260
      nsAtts = temp;
3164
      version = 0;  /* force re-initialization of nsAtts hash table */
3165
260
    }
3166
    /* using a version flag saves us from initializing nsAtts every time */
3167
280
    if (!version) {  /* initialize version flags when version wraps around */
3168
      version = INIT_ATTS_VERSION;
3169
4680
      for (j = nsAttsSize; j != 0; )
3170
2080
        nsAtts[--j].version = version;
3171
    }
3172
280
    nsAttsVersion = --version;
3173
3174
    /* expand prefixed names and check for duplicates */
3175
620
    for (; i < attIndex; i += 2) {
3176
310
      const XML_Char *s = appAtts[i];
3177
310
      if (s[-1] == 2) {  /* prefixed */
3178
        ATTRIBUTE_ID *id;
3179
        const BINDING *b;
3180
        unsigned long uriHash;
3181
300
        struct siphash sip_state;
3182
300
        struct sipkey sip_key;
3183
3184
300
        copy_salt_to_sipkey(parser, &sip_key);
3185
300
        sip24_init(&sip_state, &sip_key);
3186
3187
300
        ((XML_Char *)s)[-1] = 0;  /* clear flag */
3188
300
        id = (ATTRIBUTE_ID *)lookup(parser, &dtd->attributeIds, s, 0);
3189

600
        if (!id || !id->prefix) {
3190
          /* This code is walking through the appAtts array, dealing
3191
           * with (in this case) a prefixed attribute name.  To be in
3192
           * the array, the attribute must have already been bound, so
3193
           * has to have passed through the hash table lookup once
3194
           * already.  That implies that an entry for it already
3195
           * exists, so the lookup above will return a pointer to
3196
           * already allocated memory.  There is no opportunaity for
3197
           * the allocator to fail, so the condition above cannot be
3198
           * fulfilled.
3199
           *
3200
           * Since it is difficult to be certain that the above
3201
           * analysis is complete, we retain the test and merely
3202
           * remove the code from coverage tests.
3203
           */
3204
          return XML_ERROR_NO_MEMORY; /* LCOV_EXCL_LINE */
3205
        }
3206
300
        b = id->prefix->binding;
3207
300
        if (!b)
3208
30
          return XML_ERROR_UNBOUND_PREFIX;
3209
3210
50940
        for (j = 0; j < b->uriLen; j++) {
3211
25210
          const XML_Char c = b->uri[j];
3212

50440
          if (!poolAppendChar(&tempPool, c))
3213
10
            return XML_ERROR_NO_MEMORY;
3214
25200
        }
3215
3216
260
        sip24_update(&sip_state, b->uri, b->uriLen * sizeof(XML_Char));
3217
3218
57260
        while (*s++ != XML_T(ASCII_COLON))
3219
          ;
3220
3221
260
        sip24_update(&sip_state, s, keylen(s) * sizeof(XML_Char));
3222
3223
260
        do {  /* copies null terminator */
3224

41220
          if (!poolAppendChar(&tempPool, *s))
3225
10
            return XML_ERROR_NO_MEMORY;
3226
20590
        } while (*s++);
3227
3228
250
        uriHash = (unsigned long)sip24_final(&sip_state);
3229
3230
        { /* Check hash table for duplicate of expanded name (uriName).
3231
             Derived from code in lookup(parser, HASH_TABLE *table, ...).
3232
          */
3233
          unsigned char step = 0;
3234
250
          unsigned long mask = nsAttsSize - 1;
3235
250
          j = uriHash & mask;  /* index into hash table */
3236
501
          while (nsAtts[j].version == version) {
3237
            /* for speed we compare stored hash values first */
3238
11
            if (uriHash == nsAtts[j].hash) {
3239
10
              const XML_Char *s1 = poolStart(&tempPool);
3240
10
              const XML_Char *s2 = nsAtts[j].uriName;
3241
              /* s1 is null terminated, but not s2 */
3242

690
              for (; *s1 == *s2 && *s1 != 0; s1++, s2++);
3243
10
              if (*s1 == 0)
3244
10
                return XML_ERROR_DUPLICATE_ATTRIBUTE;
3245
            }
3246
1
            if (!step)
3247
1
              step = PROBE_STEP(uriHash, mask, nsAttsPower);
3248
2
            j < step ? (j += nsAttsSize - step) : (j -= step);
3249
          }
3250
240
        }
3251
3252
240
        if (ns_triplets) {  /* append namespace separator and prefix */
3253
90
          tempPool.ptr[-1] = namespaceSeparator;
3254
90
          s = b->prefix->name;
3255
90
          do {
3256

40200
            if (!poolAppendChar(&tempPool, *s))
3257
10
              return XML_ERROR_NO_MEMORY;
3258
20080
          } while (*s++);
3259
        }
3260
3261
        /* store expanded name in attribute list */
3262
230
        s = poolStart(&tempPool);
3263
230
        poolFinish(&tempPool);
3264
230
        appAtts[i] = s;
3265
3266
        /* fill empty slot with new version, uriName and hash value */
3267
230
        nsAtts[j].version = version;
3268
230
        nsAtts[j].hash = uriHash;
3269
230
        nsAtts[j].uriName = s;
3270
3271
230
        if (!--nPrefixes) {
3272
210
          i += 2;
3273
210
          break;
3274
        }
3275
320
      }
3276
      else  /* not prefixed */
3277
10
        ((XML_Char *)s)[-1] = 0;  /* clear flag */
3278
30
    }
3279
210
  }
3280
  /* clear flags for the remaining attributes */
3281
25760714
  for (; i < attIndex; i += 2)
3282
4751460
    ((XML_Char *)(appAtts[i]))[-1] = 0;
3283
32518368
  for (binding = *bindingsPtr; binding; binding = binding->nextTagBinding)
3284
1390
    binding->attId->name[-1] = 0;
3285
3286
16257794
  if (!ns)
3287
16255864
    return XML_ERROR_NONE;
3288
3289
  /* expand the element type name */
3290
1930
  if (elementType->prefix) {
3291
770
    binding = elementType->prefix->binding;
3292
770
    if (!binding)
3293
10
      return XML_ERROR_UNBOUND_PREFIX;
3294
760
    localPart = tagNamePtr->str;
3295
238160
    while (*localPart++ != XML_T(ASCII_COLON))
3296
      ;
3297
  }
3298
1160
  else if (dtd->defaultPrefix.binding) {
3299
    binding = dtd->defaultPrefix.binding;
3300
630
    localPart = tagNamePtr->str;
3301
  }
3302
  else
3303
530
    return XML_ERROR_NONE;
3304
  prefixLen = 0;
3305

1490
  if (ns_triplets && binding->prefix->name) {
3306
370
    for (; binding->prefix->name[prefixLen++];)
3307
      ;  /* prefixLen includes null terminator */
3308
  }
3309
1390
  tagNamePtr->localPart = localPart;
3310
1390
  tagNamePtr->uriLen = binding->uriLen;
3311
1390
  tagNamePtr->prefix = binding->prefix->name;
3312
1390
  tagNamePtr->prefixLen = prefixLen;
3313
9000
  for (i = 0; localPart[i++];)
3314
    ;  /* i includes null terminator */
3315
1390
  n = i + binding->uriLen + prefixLen;
3316
1390
  if (n > binding->uriAlloc) {
3317
    TAG *p;
3318
40
    uri = (XML_Char *)MALLOC((n + EXPAND_SPARE) * sizeof(XML_Char));
3319
40
    if (!uri)
3320
10
      return XML_ERROR_NO_MEMORY;
3321
30
    binding->uriAlloc = n + EXPAND_SPARE;
3322
30
    memcpy(uri, binding->uri, binding->uriLen * sizeof(XML_Char));
3323
120
    for (p = tagStack; p; p = p->parent)
3324
30
      if (p->name.str == binding->uri)
3325
10
        p->name.str = uri;
3326
30
    FREE(binding->uri);
3327
30
    binding->uri = uri;
3328
30
  }
3329
  /* if namespaceSeparator != '\0' then uri includes it already */
3330
1380
  uri = binding->uri + binding->uriLen;
3331
1380
  memcpy(uri, localPart, i * sizeof(XML_Char));
3332
  /* we always have a namespace separator between localPart and prefix */
3333
1380
  if (prefixLen) {
3334
90
    uri += i - 1;
3335
90
    *uri = namespaceSeparator;  /* replace null terminator */
3336
90
    memcpy(uri + 1, binding->prefix->name, prefixLen * sizeof(XML_Char));
3337
90
  }
3338
1380
  tagNamePtr->str = binding->uri;
3339
1380
  return XML_ERROR_NONE;
3340
16260074
}
3341
3342
/* addBinding() overwrites the value of prefix->binding without checking.
3343
   Therefore one must keep track of the old value outside of addBinding().
3344
*/
3345
static enum XML_Error
3346
addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
3347
           const XML_Char *uri, BINDING **bindingsPtr)
3348
{
3349
  static const XML_Char xmlNamespace[] = {
3350
    ASCII_h, ASCII_t, ASCII_t, ASCII_p, ASCII_COLON, ASCII_SLASH, ASCII_SLASH,
3351
    ASCII_w, ASCII_w, ASCII_w, ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD,
3352
    ASCII_o, ASCII_r, ASCII_g, ASCII_SLASH, ASCII_X, ASCII_M, ASCII_L,
3353
    ASCII_SLASH, ASCII_1, ASCII_9, ASCII_9, ASCII_8, ASCII_SLASH,
3354
    ASCII_n, ASCII_a, ASCII_m, ASCII_e, ASCII_s, ASCII_p, ASCII_a, ASCII_c,
3355
    ASCII_e, '\0'
3356
  };
3357
  static const int xmlLen =
3358
    (int)sizeof(xmlNamespace)/sizeof(XML_Char) - 1;
3359
  static const XML_Char xmlnsNamespace[] = {
3360
    ASCII_h, ASCII_t, ASCII_t, ASCII_p, ASCII_COLON, ASCII_SLASH, ASCII_SLASH,
3361
    ASCII_w, ASCII_w, ASCII_w, ASCII_PERIOD, ASCII_w, ASCII_3, ASCII_PERIOD,
3362
    ASCII_o, ASCII_r, ASCII_g, ASCII_SLASH, ASCII_2, ASCII_0, ASCII_0,
3363
    ASCII_0, ASCII_SLASH, ASCII_x, ASCII_m, ASCII_l, ASCII_n, ASCII_s,
3364
    ASCII_SLASH, '\0'
3365
  };
3366
  static const int xmlnsLen =
3367
    (int)sizeof(xmlnsNamespace)/sizeof(XML_Char) - 1;
3368
3369
  XML_Bool mustBeXML = XML_FALSE;
3370
  XML_Bool isXML = XML_TRUE;
3371
  XML_Bool isXMLNS = XML_TRUE;
3372
3373
  BINDING *b;
3374
  int len;
3375
3376
  /* empty URI is only valid for default namespace per XML NS 1.0 (not 1.1) */
3377

14012
  if (*uri == XML_T('\0') && prefix->name)
3378
30
    return XML_ERROR_UNDECLARING_PREFIX;
3379
3380
11515
  if (prefix->name
3381
13042
      && prefix->name[0] == XML_T(ASCII_x)
3382
10695
      && prefix->name[1] == XML_T(ASCII_m)
3383
9168
      && prefix->name[2] == XML_T(ASCII_l)) {
3384
3385
    /* Not allowed to bind xmlns */
3386
4594
    if (prefix->name[3] == XML_T(ASCII_n)
3387
4594
        && prefix->name[4] == XML_T(ASCII_s)
3388
20
        && prefix->name[5] == XML_T('\0'))
3389
10
      return XML_ERROR_RESERVED_PREFIX_XMLNS;
3390
3391
4574
    if (prefix->name[3] == XML_T('\0'))
3392
4574
      mustBeXML = XML_TRUE;
3393
  }
3394
3395
1834076
  for (len = 0; uri[len]; len++) {
3396

1274437
    if (isXML && (len > xmlLen || uri[len] != xmlNamespace[len]))
3397
2287
      isXML = XML_FALSE;
3398
3399

1673446
    if (!mustBeXML && isXMLNS
3400

781035
        && (len > xmlnsLen || uri[len] != xmlnsNamespace[len]))
3401
2277
      isXMLNS = XML_FALSE;
3402
  }
3403
6921
  isXML = isXML && len == xmlLen;
3404
6921
  isXMLNS = isXMLNS && len == xmlnsLen;
3405
3406
6921
  if (mustBeXML != isXML)
3407
20
    return mustBeXML ? XML_ERROR_RESERVED_PREFIX_XML
3408
                     : XML_ERROR_RESERVED_NAMESPACE_URI;
3409
3410
6901
  if (isXMLNS)
3411
10
    return XML_ERROR_RESERVED_NAMESPACE_URI;
3412
3413
6891
  if (namespaceSeparator)
3414
6891
    len++;
3415
6891
  if (freeBindingList) {
3416
    b = freeBindingList;
3417
90
    if (len > b->uriAlloc) {
3418
20
      XML_Char *temp = (XML_Char *)REALLOC(b->uri,
3419
                          sizeof(XML_Char) * (len + EXPAND_SPARE));
3420
20
      if (temp == NULL)
3421
10
        return XML_ERROR_NO_MEMORY;
3422
10
      b->uri = temp;
3423
10
      b->uriAlloc = len + EXPAND_SPARE;
3424
10
    }
3425
80
    freeBindingList = b->nextTagBinding;
3426
80
  }
3427
  else {
3428
6801
    b = (BINDING *)MALLOC(sizeof(BINDING));
3429
6801
    if (!b)
3430
330
      return XML_ERROR_NO_MEMORY;
3431
6471
    b->uri = (XML_Char *)MALLOC(sizeof(XML_Char) * (len + EXPAND_SPARE));
3432
6471
    if (!b->uri) {
3433
330
      FREE(b);
3434
330
      return XML_ERROR_NO_MEMORY;
3435
    }
3436
6141
    b->uriAlloc = len + EXPAND_SPARE;
3437
  }
3438
6221
  b->uriLen = len;
3439
6221
  memcpy(b->uri, uri, len * sizeof(XML_Char));
3440
6221
  if (namespaceSeparator)
3441
6221
    b->uri[len - 1] = namespaceSeparator;
3442
6221
  b->prefix = prefix;
3443
6221
  b->attId = attId;
3444
6221
  b->prevPrefixBinding = prefix->binding;
3445
  /* NULL binding when default namespace undeclared */
3446

6261
  if (*uri == XML_T('\0') && prefix == &_dtd->defaultPrefix)
3447
40
    prefix->binding = NULL;
3448
  else
3449
    prefix->binding = b;
3450
6221
  b->nextTagBinding = *bindingsPtr;
3451
6221
  *bindingsPtr = b;
3452
  /* if attId == NULL then we are not starting a namespace scope */
3453

8001
  if (attId && startNamespaceDeclHandler)
3454
140
    startNamespaceDeclHandler(handlerArg, prefix->name,
3455
70
                              prefix->binding ? uri : 0);
3456
6221
  return XML_ERROR_NONE;
3457
6961
}
3458
3459
/* The idea here is to avoid using stack for each CDATA section when
3460
   the whole file is parsed with one call.
3461
*/
3462
static enum XML_Error PTRCALL
3463
cdataSectionProcessor(XML_Parser parser,
3464
                      const char *start,
3465
                      const char *end,
3466
                      const char **endPtr)
3467
{
3468
5040
  enum XML_Error result = doCdataSection(parser, encoding, &start, end,
3469
2520
                                         endPtr, (XML_Bool)!ps_finalBuffer);
3470
2520
  if (result != XML_ERROR_NONE)
3471
130
    return result;
3472
2390
  if (start) {
3473
100
    if (parentParser) {  /* we are parsing an external entity */
3474
10
      processor = externalEntityContentProcessor;
3475
10
      return externalEntityContentProcessor(parser, start, end, endPtr);
3476
    }
3477
    else {
3478
90
      processor = contentProcessor;
3479
90
      return contentProcessor(parser, start, end, endPtr);
3480
    }
3481
  }
3482
2290
  return result;
3483
2520
}
3484
3485
/* startPtr gets set to non-null if the section is closed, and to null if
3486
   the section is not yet closed.
3487
*/
3488
static enum XML_Error
3489
doCdataSection(XML_Parser parser,
3490
               const ENCODING *enc,
3491
               const char **startPtr,
3492
               const char *end,
3493
               const char **nextPtr,
3494
               XML_Bool haveMore)
3495
{
3496
5580
  const char *s = *startPtr;
3497
  const char **eventPP;
3498
  const char **eventEndPP;
3499
2790
  if (enc == encoding) {
3500
2790
    eventPP = &eventPtr;
3501
2790
    *eventPP = s;
3502
2790
    eventEndPP = &eventEndPtr;
3503
2790
  }
3504
  else {
3505
    eventPP = &(openInternalEntities->internalEventPtr);
3506
    eventEndPP = &(openInternalEntities->internalEventEndPtr);
3507
  }
3508
2790
  *eventPP = s;
3509
2790
  *startPtr = NULL;
3510
3511
2790
  for (;;) {
3512
4500
    const char *next;
3513
4500
    int tok = XmlCdataSectionTok(enc, s, end, &next);
3514
4500
    *eventEndPP = next;
3515


4500
    switch (tok) {
3516
    case XML_TOK_CDATA_SECT_CLOSE:
3517
110
      if (endCdataSectionHandler)
3518
20
        endCdataSectionHandler(handlerArg);
3519
#if 0
3520
      /* see comment under XML_TOK_CDATA_SECT_OPEN */
3521
      else if (characterDataHandler)
3522
        characterDataHandler(handlerArg, dataBuf, 0);
3523
#endif
3524
90
      else if (defaultHandler)
3525
20
        reportDefault(parser, enc, s, next);
3526
110
      *startPtr = next;
3527
110
      *nextPtr = next;
3528
110
      if (ps_parsing == XML_FINISHED)
3529
        return XML_ERROR_ABORTED;
3530
      else
3531
110
        return XML_ERROR_NONE;
3532
    case XML_TOK_DATA_NEWLINE:
3533
10
      if (characterDataHandler) {
3534
        XML_Char c = 0xA;
3535
        characterDataHandler(handlerArg, &c, 1);
3536
      }
3537
10
      else if (defaultHandler)
3538
10
        reportDefault(parser, enc, s, next);
3539
      break;
3540
    case XML_TOK_DATA_CHARS:
3541
      {
3542
1720
        XML_CharacterDataHandler charDataHandler = characterDataHandler;
3543
1720
        if (charDataHandler) {
3544
1430
          if (MUST_CONVERT(enc, s)) {
3545
            for (;;) {
3546
140
              ICHAR *dataPtr = (ICHAR *)dataBuf;
3547
140
              const enum XML_Convert_Result convert_res = XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
3548
140
              *eventEndPP = next;
3549
280
              charDataHandler(handlerArg, dataBuf,
3550
140
                              (int)(dataPtr - (ICHAR *)dataBuf));
3551
140
              if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE))
3552
130
                break;
3553
10
              *eventPP = s;
3554
150
            }
3555
          }
3556
          else
3557
2600
            charDataHandler(handlerArg,
3558
1300
                            (XML_Char *)s,
3559
1300
                            (int)((XML_Char *)next - (XML_Char *)s));
3560
        }
3561
290
        else if (defaultHandler)
3562
100
          reportDefault(parser, enc, s, next);
3563
      }
3564
1720
      break;
3565
    case XML_TOK_INVALID:
3566
10
      *eventPP = next;
3567
10
      return XML_ERROR_INVALID_TOKEN;
3568
    case XML_TOK_PARTIAL_CHAR:
3569
120
      if (haveMore) {
3570
100
        *nextPtr = s;
3571
100
        return XML_ERROR_NONE;
3572
      }
3573
20
      return XML_ERROR_PARTIAL_CHAR;
3574
    case XML_TOK_PARTIAL:
3575
    case XML_TOK_NONE:
3576
2530
      if (haveMore) {
3577
2420
        *nextPtr = s;
3578
2420
        return XML_ERROR_NONE;
3579
      }
3580
110
      return XML_ERROR_UNCLOSED_CDATA_SECTION;
3581
    default:
3582
      /* Every token returned by XmlCdataSectionTok() has its own
3583
       * explicit case, so this default case will never be executed.
3584
       * We retain it as a safety net and exclude it from the coverage
3585
       * statistics.
3586
       *
3587
       * LCOV_EXCL_START
3588
      */
3589
      *eventPP = next;
3590
      return XML_ERROR_UNEXPECTED_STATE;
3591
      /* LCOV_EXCL_STOP */
3592
    }
3593
3594
1730
    *eventPP = s = next;
3595
1730
    switch (ps_parsing) {
3596
    case XML_SUSPENDED:
3597
10
      *nextPtr = next;
3598
10
      return XML_ERROR_NONE;
3599
    case XML_FINISHED:
3600
10
      return XML_ERROR_ABORTED;
3601
    default: ;
3602
    }
3603
6210
  }
3604
  /* not reached */
3605
2790
}
3606
3607
#ifdef XML_DTD
3608
3609
/* The idea here is to avoid using stack for each IGNORE section when
3610
   the whole file is parsed with one call.
3611
*/
3612
static enum XML_Error PTRCALL
3613
ignoreSectionProcessor(XML_Parser parser,
3614
                       const char *start,
3615
                       const char *end,
3616
                       const char **endPtr)
3617
{
3618
3020
  enum XML_Error result = doIgnoreSection(parser, encoding, &start, end,
3619
1510
                                          endPtr, (XML_Bool)!ps_finalBuffer);
3620
1510
  if (result != XML_ERROR_NONE)
3621
40
    return result;
3622
1470
  if (start) {
3623
30
    processor = prologProcessor;
3624
30
    return prologProcessor(parser, start, end, endPtr);
3625
  }
3626
1440
  return result;
3627
1510
}
3628
3629
/* startPtr gets set to non-null is the section is closed, and to null
3630
   if the section is not yet closed.
3631
*/
3632
static enum XML_Error
3633
doIgnoreSection(XML_Parser parser,
3634
                const ENCODING *enc,
3635
                const char **startPtr,
3636
                const char *end,
3637
                const char **nextPtr,
3638
                XML_Bool haveMore)
3639
{
3640
3160
  const char *next;
3641
  int tok;
3642
1580
  const char *s = *startPtr;
3643
  const char **eventPP;
3644
  const char **eventEndPP;
3645
1580
  if (enc == encoding) {
3646
1580
    eventPP = &eventPtr;
3647
1580
    *eventPP = s;
3648
1580
    eventEndPP = &eventEndPtr;
3649
1580
  }
3650
  else {
3651
    /* It's not entirely clear, but it seems the following two lines
3652
     * of code cannot be executed.  The only occasions on which 'enc'
3653
     * is not 'parser->m_encoding' are when this function is called
3654
     * from the internal entity processing, and IGNORE sections are an
3655
     * error in internal entities.
3656
     *
3657
     * Since it really isn't clear that this is true, we keep the code
3658
     * and just remove it from our coverage tests.
3659
     *
3660
     * LCOV_EXCL_START
3661
     */
3662
    eventPP = &(openInternalEntities->internalEventPtr);
3663
    eventEndPP = &(openInternalEntities->internalEventEndPtr);
3664
    /* LCOV_EXCL_STOP */
3665
  }
3666
1580
  *eventPP = s;
3667
1580
  *startPtr = NULL;
3668
1580
  tok = XmlIgnoreSectionTok(enc, s, end, &next);
3669
1580
  *eventEndPP = next;
3670

1580
  switch (tok) {
3671
  case XML_TOK_IGNORE_SECT:
3672
30
    if (defaultHandler)
3673
30
      reportDefault(parser, enc, s, next);
3674
30
    *startPtr = next;
3675
30
    *nextPtr = next;
3676
30
    if (ps_parsing == XML_FINISHED)
3677
      return XML_ERROR_ABORTED;
3678
    else
3679
30
      return XML_ERROR_NONE;
3680
  case XML_TOK_INVALID:
3681
20
    *eventPP = next;
3682
20
    return XML_ERROR_INVALID_TOKEN;
3683
  case XML_TOK_PARTIAL_CHAR:
3684
30
    if (haveMore) {
3685
20
      *nextPtr = s;
3686
20
      return XML_ERROR_NONE;
3687
    }
3688
10
    return XML_ERROR_PARTIAL_CHAR;
3689
  case XML_TOK_PARTIAL:
3690
  case XML_TOK_NONE:
3691
1500
    if (haveMore) {
3692
1490
      *nextPtr = s;
3693
1490
      return XML_ERROR_NONE;
3694
    }
3695
10
    return XML_ERROR_SYNTAX; /* XML_ERROR_UNCLOSED_IGNORE_SECTION */
3696
  default:
3697
    /* All of the tokens that XmlIgnoreSectionTok() returns have
3698
     * explicit cases to handle them, so this default case is never
3699
     * executed.  We keep it as a safety net anyway, and remove it
3700
     * from our test coverage statistics.
3701
     *
3702
     * LCOV_EXCL_START
3703
     */
3704
    *eventPP = next;
3705
    return XML_ERROR_UNEXPECTED_STATE;
3706
    /* LCOV_EXCL_STOP */
3707
  }
3708
  /* not reached */
3709
1580
}
3710
3711
#endif /* XML_DTD */
3712
3713
static enum XML_Error
3714
initializeEncoding(XML_Parser parser)
3715
{
3716
  const char *s;
3717
#ifdef XML_UNICODE
3718
  char encodingBuf[128];
3719
  /* See comments abount `protoclEncodingName` in parserInit() */
3720
  if (!protocolEncodingName)
3721
    s = NULL;
3722
  else {
3723
    int i;
3724
    for (i = 0; protocolEncodingName[i]; i++) {
3725
      if (i == sizeof(encodingBuf) - 1
3726
          || (protocolEncodingName[i] & ~0x7f) != 0) {
3727
        encodingBuf[0] = '\0';
3728
        break;
3729
      }
3730
      encodingBuf[i] = (char)protocolEncodingName[i];
3731
    }
3732
    encodingBuf[i] = '\0';
3733
    s = encodingBuf;
3734
  }
3735
#else
3736
132988
  s = protocolEncodingName;
3737
#endif
3738
66494
  if ((ns ? XmlInitEncodingNS : XmlInitEncoding)(&initEncoding, &encoding, s))
3739
66454
    return XML_ERROR_NONE;
3740
40
  return handleUnknownEncoding(parser, protocolEncodingName);
3741
66494
}
3742
3743
static enum XML_Error
3744
processXmlDecl(XML_Parser parser, int isGeneralTextEntity,
3745
               const char *s, const char *next)
3746
{
3747
105620
  const char *encodingName = NULL;
3748
  const XML_Char *storedEncName = NULL;
3749
52810
  const ENCODING *newEncoding = NULL;
3750
52810
  const char *version = NULL;
3751
52810
  const char *versionend;
3752
  const XML_Char *storedversion = NULL;
3753
52810
  int standalone = -1;
3754
105620
  if (!(ns
3755
        ? XmlParseXmlDeclNS
3756
        : XmlParseXmlDecl)(isGeneralTextEntity,
3757
52810
                           encoding,
3758
                           s,
3759
                           next,
3760
52810
                           &eventPtr,
3761
                           &version,
3762
                           &versionend,
3763
                           &encodingName,
3764
                           &newEncoding,
3765
                           &standalone)) {
3766
50
    if (isGeneralTextEntity)
3767
10
      return XML_ERROR_TEXT_DECL;
3768
    else
3769
40
      return XML_ERROR_XML_DECL;
3770
  }
3771
52760
  if (!isGeneralTextEntity && standalone == 1) {
3772
50
    _dtd->standalone = XML_TRUE;
3773
#ifdef XML_DTD
3774
50
    if (paramEntityParsing == XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
3775
10
      paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
3776
#endif /* XML_DTD */
3777
  }
3778
52760
  if (xmlDeclHandler) {
3779
250
    if (encodingName != NULL) {
3780
500
      storedEncName = poolStoreString(&temp2Pool,
3781
250
                                      encoding,
3782
                                      encodingName,
3783
                                      encodingName
3784
250
                                      + XmlNameLength(encoding, encodingName));
3785
250
      if (!storedEncName)
3786
20
              return XML_ERROR_NO_MEMORY;
3787
230
      poolFinish(&temp2Pool);
3788
230
    }
3789
230
    if (version) {
3790
460
      storedversion = poolStoreString(&temp2Pool,
3791
230
                                      encoding,
3792
                                      version,
3793
230
                                      versionend - encoding->minBytesPerChar);
3794
230
      if (!storedversion)
3795
10
        return XML_ERROR_NO_MEMORY;
3796
    }
3797
220
    xmlDeclHandler(handlerArg, storedversion, storedEncName, standalone);
3798
220
  }
3799
52510
  else if (defaultHandler)
3800
40
    reportDefault(parser, encoding, s, next);
3801
52730
  if (protocolEncodingName == NULL) {
3802
52690
    if (newEncoding) {
3803
      /* Check that the specified encoding does not conflict with what
3804
       * the parser has already deduced.  Do we have the same number
3805
       * of bytes in the smallest representation of a character?  If
3806
       * this is UTF-16, is it the same endianness?
3807
       */
3808
51950
      if (newEncoding->minBytesPerChar != encoding->minBytesPerChar
3809

103290
          || (newEncoding->minBytesPerChar == 2 &&
3810
300
              newEncoding != encoding)) {
3811
10
        eventPtr = encodingName;
3812
10
        return XML_ERROR_INCORRECT_ENCODING;
3813
      }
3814
51640
      encoding = newEncoding;
3815
51640
    }
3816
1040
    else if (encodingName) {
3817
      enum XML_Error result;
3818
390
      if (!storedEncName) {
3819
320
        storedEncName = poolStoreString(
3820
320
          &temp2Pool, encoding, encodingName,
3821
320
          encodingName + XmlNameLength(encoding, encodingName));
3822
320
        if (!storedEncName)
3823
10
          return XML_ERROR_NO_MEMORY;
3824
      }
3825
380
      result = handleUnknownEncoding(parser, storedEncName);
3826
380
      poolClear(&temp2Pool);
3827
380
      if (result == XML_ERROR_UNKNOWN_ENCODING)
3828
60
        eventPtr = encodingName;
3829
380
      return result;
3830
    }
3831
  }
3832
3833
52330
  if (storedEncName || storedversion)
3834
140
    poolClear(&temp2Pool);
3835
3836
52330
  return XML_ERROR_NONE;
3837
52810
}
3838
3839
static enum XML_Error
3840
handleUnknownEncoding(XML_Parser parser, const XML_Char *encodingName)
3841
{
3842
840
  if (unknownEncodingHandler) {
3843
390
    XML_Encoding info;
3844
    int i;
3845
200460
    for (i = 0; i < 256; i++)
3846
99840
      info.map[i] = -1;
3847
390
    info.convert = NULL;
3848
390
    info.data = NULL;
3849
390
    info.release = NULL;
3850
390
    if (unknownEncodingHandler(unknownEncodingHandlerData, encodingName,
3851
                               &info)) {
3852
      ENCODING *enc;
3853
380
      unknownEncodingMem = MALLOC(XmlSizeOfUnknownEncoding());
3854
380
      if (!unknownEncodingMem) {
3855
20
        if (info.release)
3856
10
          info.release(info.data);
3857
20
        return XML_ERROR_NO_MEMORY;
3858
      }
3859
720
      enc = (ns
3860
             ? XmlInitUnknownEncodingNS
3861
             : XmlInitUnknownEncoding)(unknownEncodingMem,
3862
360
                                       info.map,
3863
360
                                       info.convert,
3864
360
                                       info.data);
3865
360
      if (enc) {
3866
310
        unknownEncodingData = info.data;
3867
310
        unknownEncodingRelease = info.release;
3868
310
        encoding = enc;
3869
310
        return XML_ERROR_NONE;
3870
      }
3871
50
    }
3872
60
    if (info.release != NULL)
3873
10
      info.release(info.data);
3874
450
  }
3875
90
  return XML_ERROR_UNKNOWN_ENCODING;
3876
420
}
3877
3878
static enum XML_Error PTRCALL
3879
prologInitProcessor(XML_Parser parser,
3880
                    const char *s,
3881
                    const char *end,
3882
                    const char **nextPtr)
3883
{
3884
126288
  enum XML_Error result = initializeEncoding(parser);
3885
63144
  if (result != XML_ERROR_NONE)
3886
10
    return result;
3887
63134
  processor = prologProcessor;
3888
63134
  return prologProcessor(parser, s, end, nextPtr);
3889
63144
}
3890
3891
#ifdef XML_DTD
3892
3893
static enum XML_Error PTRCALL
3894
externalParEntInitProcessor(XML_Parser parser,
3895
                            const char *s,
3896
                            const char *end,
3897
                            const char **nextPtr)
3898
{
3899
5580
  enum XML_Error result = initializeEncoding(parser);
3900
2790
  if (result != XML_ERROR_NONE)
3901
10
    return result;
3902
3903
  /* we know now that XML_Parse(Buffer) has been called,
3904
     so we consider the external parameter entity read */
3905
2780
  _dtd->paramEntityRead = XML_TRUE;
3906
3907
2780
  if (prologState.inEntityValue) {
3908
250
    processor = entityValueInitProcessor;
3909
250
    return entityValueInitProcessor(parser, s, end, nextPtr);
3910
  }
3911
  else {
3912
2530
    processor = externalParEntProcessor;
3913
2530
    return externalParEntProcessor(parser, s, end, nextPtr);
3914
  }
3915
2790
}
3916
3917
static enum XML_Error PTRCALL
3918
entityValueInitProcessor(XML_Parser parser,
3919
                         const char *s,
3920
                         const char *end,
3921
                         const char **nextPtr)
3922
{
3923
  int tok;
3924
  const char *start = s;
3925
13360
  const char *next = start;
3926
  eventPtr = start;
3927
3928
25020
  for (;;) {
3929
25020
    tok = XmlPrologTok(encoding, start, end, &next);
3930
25020
    eventEndPtr = next;
3931
25020
    if (tok <= 0) {
3932
6600
      if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
3933
6420
        *nextPtr = s;
3934
6420
        return XML_ERROR_NONE;
3935
      }
3936

180
      switch (tok) {
3937
      case XML_TOK_INVALID:
3938
10
        return XML_ERROR_INVALID_TOKEN;
3939
      case XML_TOK_PARTIAL:
3940
10
        return XML_ERROR_UNCLOSED_TOKEN;
3941
      case XML_TOK_PARTIAL_CHAR:
3942
10
        return XML_ERROR_PARTIAL_CHAR;
3943
      case XML_TOK_NONE:   /* start == end */
3944
      default:
3945
        break;
3946
      }
3947
      /* found end of entity value - can store it now */
3948
150
      return storeEntityValue(parser, encoding, s, end);
3949
    }
3950
18420
    else if (tok == XML_TOK_XML_DECL) {
3951
      enum XML_Error result;
3952
60
      result = processXmlDecl(parser, 0, start, next);
3953
60
      if (result != XML_ERROR_NONE)
3954
10
        return result;
3955
      /* At this point, ps_parsing cannot be XML_SUSPENDED.  For that
3956
       * to happen, a parameter entity parsing handler must have
3957
       * attempted to suspend the parser, which fails and raises an
3958
       * error.  The parser can be aborted, but can't be suspended.
3959
       */
3960
50
      if (ps_parsing == XML_FINISHED)
3961
10
        return XML_ERROR_ABORTED;
3962
40
      *nextPtr = next;
3963
      /* stop scanning for text declaration - we found one */
3964
40
      processor = entityValueProcessor;
3965
40
      return entityValueProcessor(parser, next, end, nextPtr);
3966
    }
3967
    /* If we are at the end of the buffer, this would cause XmlPrologTok to
3968
       return XML_TOK_NONE on the next call, which would then cause the
3969
       function to exit with *nextPtr set to s - that is what we want for other
3970
       tokens, but not for the BOM - we would rather like to skip it;
3971
       then, when this routine is entered the next time, XmlPrologTok will
3972
       return XML_TOK_INVALID, since the BOM is still in the buffer
3973
    */
3974

18380
    else if (tok == XML_TOK_BOM && next == end && !ps_finalBuffer) {
3975
10
      *nextPtr = next;
3976
10
      return XML_ERROR_NONE;
3977
    }
3978
    /* If we get this token, we have the start of what might be a
3979
       normal tag, but not a declaration (i.e. it doesn't begin with
3980
       "<!").  In a DTD context, that isn't legal.
3981
    */
3982
18350
    else if (tok == XML_TOK_INSTANCE_START) {
3983
10
      *nextPtr = next;
3984
10
      return XML_ERROR_SYNTAX;
3985
    }
3986
    start = next;
3987
    eventPtr = start;
3988
  }
3989
6680
}
3990
3991
static enum XML_Error PTRCALL
3992
externalParEntProcessor(XML_Parser parser,
3993
                        const char *s,
3994
                        const char *end,
3995
                        const char **nextPtr)
3996
{
3997
52180
  const char *next = s;
3998
  int tok;
3999
4000
26090
  tok = XmlPrologTok(encoding, s, end, &next);
4001
26090
  if (tok <= 0) {
4002
23590
    if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
4003
23560
      *nextPtr = s;
4004
23560
      return XML_ERROR_NONE;
4005
    }
4006

530
    switch (tok) {
4007
    case XML_TOK_INVALID:
4008
10
      return XML_ERROR_INVALID_TOKEN;
4009
    case XML_TOK_PARTIAL:
4010
10
      return XML_ERROR_UNCLOSED_TOKEN;
4011
    case XML_TOK_PARTIAL_CHAR:
4012
10
      return XML_ERROR_PARTIAL_CHAR;
4013
    case XML_TOK_NONE:   /* start == end */
4014
    default:
4015
      break;
4016
    }
4017
  }
4018
  /* This would cause the next stage, i.e. doProlog to be passed XML_TOK_BOM.
4019
     However, when parsing an external subset, doProlog will not accept a BOM
4020
     as valid, and report a syntax error, so we have to skip the BOM
4021
  */
4022
2500
  else if (tok == XML_TOK_BOM) {
4023
10
    s = next;
4024
10
    tok = XmlPrologTok(encoding, s, end, &next);
4025
10
  }
4026
4027
2500
  processor = prologProcessor;
4028
5000
  return doProlog(parser, encoding, s, end, tok, next,
4029
2500
                  nextPtr, (XML_Bool)!ps_finalBuffer);
4030
26090
}
4031
4032
static enum XML_Error PTRCALL
4033
entityValueProcessor(XML_Parser parser,
4034
                     const char *s,
4035
                     const char *end,
4036
                     const char **nextPtr)
4037
{
4038
  const char *start = s;
4039
360
  const char *next = s;
4040
180
  const ENCODING *enc = encoding;
4041
  int tok;
4042
4043
320
  for (;;) {
4044
320
    tok = XmlPrologTok(enc, start, end, &next);
4045
320
    if (tok <= 0) {
4046
180
      if (!ps_finalBuffer && tok != XML_TOK_INVALID) {
4047
140
        *nextPtr = s;
4048
140
        return XML_ERROR_NONE;
4049
      }
4050

40
      switch (tok) {
4051
      case XML_TOK_INVALID:
4052
10
        return XML_ERROR_INVALID_TOKEN;
4053
      case XML_TOK_PARTIAL:
4054
10
        return XML_ERROR_UNCLOSED_TOKEN;
4055
      case XML_TOK_PARTIAL_CHAR:
4056
10
        return XML_ERROR_PARTIAL_CHAR;
4057
      case XML_TOK_NONE:   /* start == end */
4058
      default:
4059
        break;
4060
      }
4061
      /* found end of entity value - can store it now */
4062
10
      return storeEntityValue(parser, enc, s, end);
4063
    }
4064
140
    start = next;
4065
  }
4066
180
}
4067
4068
#endif /* XML_DTD */
4069
4070
static enum XML_Error PTRCALL
4071
prologProcessor(XML_Parser parser,
4072
                const char *s,
4073
                const char *end,
4074
                const char **nextPtr)
4075
{
4076
7160748
  const char *next = s;
4077
3580374
  int tok = XmlPrologTok(encoding, s, end, &next);
4078
10741122
  return doProlog(parser, encoding, s, end, tok, next,
4079
3580374
                  nextPtr, (XML_Bool)!ps_finalBuffer);
4080
3580374
}
4081
4082
static enum XML_Error
4083
doProlog(XML_Parser parser,
4084
         const ENCODING *enc,
4085
         const char *s,
4086
         const char *end,
4087
         int tok,
4088
         const char *next,
4089
         const char **nextPtr,
4090
         XML_Bool haveMore)
4091
{
4092
#ifdef XML_DTD
4093
  static const XML_Char externalSubsetName[] = { ASCII_HASH , '\0' };
4094
#endif /* XML_DTD */
4095
  static const XML_Char atypeCDATA[] =
4096
      { ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0' };
4097
  static const XML_Char atypeID[] = { ASCII_I, ASCII_D, '\0' };
4098
  static const XML_Char atypeIDREF[] =
4099
      { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, '\0' };
4100
  static const XML_Char atypeIDREFS[] =
4101
      { ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, ASCII_S, '\0' };
4102
  static const XML_Char atypeENTITY[] =
4103
      { ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_Y, '\0' };
4104
  static const XML_Char atypeENTITIES[] = { ASCII_E, ASCII_N,
4105
      ASCII_T, ASCII_I, ASCII_T, ASCII_I, ASCII_E, ASCII_S, '\0' };
4106
  static const XML_Char atypeNMTOKEN[] = {
4107
      ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, '\0' };
4108
  static const XML_Char atypeNMTOKENS[] = { ASCII_N, ASCII_M, ASCII_T,
4109
      ASCII_O, ASCII_K, ASCII_E, ASCII_N, ASCII_S, '\0' };
4110
  static const XML_Char notationPrefix[] = { ASCII_N, ASCII_O, ASCII_T,
4111
      ASCII_A, ASCII_T, ASCII_I, ASCII_O, ASCII_N, ASCII_LPAREN, '\0' };
4112
  static const XML_Char enumValueSep[] = { ASCII_PIPE, '\0' };
4113
  static const XML_Char enumValueStart[] = { ASCII_LPAREN, '\0' };
4114
4115
  /* save one level of indirection */
4116
3582984
  DTD * const dtd = _dtd;
4117
4118
  const char **eventPP;
4119
  const char **eventEndPP;
4120
  enum XML_Content_Quant quant;
4121
4122
3582984
  if (enc == encoding) {
4123
3582884
    eventPP = &eventPtr;
4124
3582884
    eventEndPP = &eventEndPtr;
4125
3582884
  }
4126
  else {
4127
100
    eventPP = &(openInternalEntities->internalEventPtr);
4128
100
    eventEndPP = &(openInternalEntities->internalEventEndPtr);
4129
  }
4130
4131
  for (;;) {
4132
    int role;
4133
    XML_Bool handleDefault = XML_TRUE;
4134
4747910
    *eventPP = s;
4135
4747910
    *eventEndPP = next;
4136
4747910
    if (tok <= 0) {
4137
3519360
      if (haveMore && tok != XML_TOK_INVALID) {
4138
3517230
        *nextPtr = s;
4139
3517230
        return XML_ERROR_NONE;
4140
      }
4141

2130
      switch (tok) {
4142
      case XML_TOK_INVALID:
4143
90
        *eventPP = next;
4144
90
        return XML_ERROR_INVALID_TOKEN;
4145
      case XML_TOK_PARTIAL:
4146
10
        return XML_ERROR_UNCLOSED_TOKEN;
4147
      case XML_TOK_PARTIAL_CHAR:
4148
10
        return XML_ERROR_PARTIAL_CHAR;
4149
      case -XML_TOK_PROLOG_S:
4150
        tok = -tok;
4151
        break;
4152
      case XML_TOK_NONE:
4153
#ifdef XML_DTD
4154
        /* for internal PE NOT referenced between declarations */
4155

2100
        if (enc != encoding && !openInternalEntities->betweenDecl) {
4156
          *nextPtr = s;
4157
          return XML_ERROR_NONE;
4158
        }
4159
        /* WFC: PE Between Declarations - must check that PE contains
4160
           complete markup, not only for external PEs, but also for
4161
           internal PEs if the reference occurs between declarations.
4162
        */
4163

2060
        if (isParamEntity || enc != encoding) {
4164
4020
          if (XmlTokenRole(&prologState, XML_TOK_NONE, end, end, enc)
4165
2010
              == XML_ROLE_ERROR)
4166
            return XML_ERROR_INCOMPLETE_PE;
4167
2010
          *nextPtr = s;
4168
2010
          return XML_ERROR_NONE;
4169
        }
4170
#endif /* XML_DTD */
4171
10
        return XML_ERROR_NO_ELEMENTS;
4172
      default:
4173
        tok = -tok;
4174
        next = end;
4175
        break;
4176
      }
4177
    }
4178
2494396
    role = XmlTokenRole(&prologState, tok, s, next, enc);
4179















2494396
    switch (role) {
4180
    case XML_ROLE_XML_DECL:
4181
      {
4182
52580
        enum XML_Error result = processXmlDecl(parser, 0, s, next);
4183
52580
        if (result != XML_ERROR_NONE)
4184
160
          return result;
4185
52420
        enc = encoding;
4186
        handleDefault = XML_FALSE;
4187
52420
      }
4188
      break;
4189
    case XML_ROLE_DOCTYPE_NAME:
4190
57074
      if (startDoctypeDeclHandler) {
4191
430
        doctypeName = poolStoreString(&tempPool, enc, s, next);
4192
430
        if (!doctypeName)
4193
30
          return XML_ERROR_NO_MEMORY;
4194
400
        poolFinish(&tempPool);
4195
400
        doctypePubid = NULL;
4196
        handleDefault = XML_FALSE;
4197
400
      }
4198
57044
      doctypeSysid = NULL; /* always initialize to NULL */
4199
57044
      break;
4200
    case XML_ROLE_DOCTYPE_INTERNAL_SUBSET:
4201
5624
      if (startDoctypeDeclHandler) {
4202
340
        startDoctypeDeclHandler(handlerArg, doctypeName, doctypeSysid,
4203
170
                                doctypePubid, 1);
4204
170
        doctypeName = NULL;
4205
170
        poolClear(&tempPool);
4206
        handleDefault = XML_FALSE;
4207
170
      }
4208
      break;
4209
#ifdef XML_DTD
4210
    case XML_ROLE_TEXT_DECL:
4211
      {
4212
60
        enum XML_Error result = processXmlDecl(parser, 1, s, next);
4213
60
        if (result != XML_ERROR_NONE)
4214
10
          return result;
4215
50
        enc = encoding;
4216
        handleDefault = XML_FALSE;
4217
50
      }
4218
      break;
4219
#endif /* XML_DTD */
4220
    case XML_ROLE_DOCTYPE_PUBLIC_ID:
4221
#ifdef XML_DTD
4222
50210
      useForeignDTD = XML_FALSE;
4223
50210
      declEntity = (ENTITY *)lookup(parser,
4224
50210
                                    &dtd->paramEntities,
4225
                                    externalSubsetName,
4226
                                    sizeof(ENTITY));
4227
50210
      if (!declEntity)
4228
40
        return XML_ERROR_NO_MEMORY;
4229
#endif /* XML_DTD */
4230
50170
      dtd->hasParamEntityRefs = XML_TRUE;
4231
50170
      if (startDoctypeDeclHandler) {
4232
        XML_Char *pubId;
4233
160
        if (!XmlIsPublicId(enc, s, next, eventPP))
4234
10
          return XML_ERROR_PUBLICID;
4235
300
        pubId = poolStoreString(&tempPool, enc,
4236
150
                                s + enc->minBytesPerChar,
4237
150
                                next - enc->minBytesPerChar);
4238
150
        if (!pubId)
4239
10
          return XML_ERROR_NO_MEMORY;
4240
140
        normalizePublicId(pubId);
4241
140
        poolFinish(&tempPool);
4242
140
        doctypePubid = pubId;
4243
        handleDefault = XML_FALSE;
4244
140
        goto alreadyChecked;
4245
      }
4246
      /* fall through */
4247
    case XML_ROLE_ENTITY_PUBLIC_ID:
4248
50440
      if (!XmlIsPublicId(enc, s, next, eventPP))
4249
        return XML_ERROR_PUBLICID;
4250
    alreadyChecked:
4251

101160
      if (dtd->keepProcessing && declEntity) {
4252
101160
        XML_Char *tem = poolStoreString(&dtd->pool,
4253
                                        enc,
4254
50580
                                        s + enc->minBytesPerChar,
4255
50580
                                        next - enc->minBytesPerChar);
4256
50580
        if (!tem)
4257
30
          return XML_ERROR_NO_MEMORY;
4258
50550
        normalizePublicId(tem);
4259
50550
        declEntity->publicId = tem;
4260
50550
        poolFinish(&dtd->pool);
4261
50550
        if (entityDeclHandler)
4262
200
          handleDefault = XML_FALSE;
4263
50550
      }
4264
      break;
4265
    case XML_ROLE_DOCTYPE_CLOSE:
4266
55084
      if (doctypeName) {
4267
260
        startDoctypeDeclHandler(handlerArg, doctypeName,
4268
130
                                doctypeSysid, doctypePubid, 0);
4269
130
        poolClear(&tempPool);
4270
        handleDefault = XML_FALSE;
4271
130
      }
4272
      /* doctypeSysid will be non-NULL in the case of a previous
4273
         XML_ROLE_DOCTYPE_SYSTEM_ID, even if startDoctypeDeclHandler
4274
         was not set, indicating an external subset
4275
      */
4276
#ifdef XML_DTD
4277

57538
      if (doctypeSysid || useForeignDTD) {
4278
52650
        XML_Bool hadParamEntityRefs = dtd->hasParamEntityRefs;
4279
52650
        dtd->hasParamEntityRefs = XML_TRUE;
4280

55160
        if (paramEntityParsing && externalEntityRefHandler) {
4281
2510
          ENTITY *entity = (ENTITY *)lookup(parser,
4282
2510
                                            &dtd->paramEntities,
4283
                                            externalSubsetName,
4284
                                            sizeof(ENTITY));
4285
2510
          if (!entity) {
4286
            /* The external subset name "#" will have already been
4287
             * inserted into the hash table at the start of the
4288
             * external entity parsing, so no allocation will happen
4289
             * and lookup() cannot fail.
4290
             */
4291
            return XML_ERROR_NO_MEMORY; /* LCOV_EXCL_LINE */
4292
          }
4293
2510
          if (useForeignDTD)
4294
20
            entity->base = curBase;
4295
2510
          dtd->paramEntityRead = XML_FALSE;
4296
5020
          if (!externalEntityRefHandler(externalEntityRefHandlerArg,
4297
                                        0,
4298
2510
                                        entity->base,
4299
2510
                                        entity->systemId,
4300
2510
                                        entity->publicId))
4301
700
            return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
4302
1810
          if (dtd->paramEntityRead) {
4303

1750
            if (!dtd->standalone &&
4304
1710
                notStandaloneHandler &&
4305
20
                !notStandaloneHandler(handlerArg))
4306
10
              return XML_ERROR_NOT_STANDALONE;
4307
          }
4308
          /* if we didn't read the foreign DTD then this means that there
4309
             is no external subset and we must reset dtd->hasParamEntityRefs
4310
          */
4311
80
          else if (!doctypeSysid)
4312
10
            dtd->hasParamEntityRefs = hadParamEntityRefs;
4313
          /* end of DTD - no need to update dtd->keepProcessing */
4314
1800
        }
4315
51940
        useForeignDTD = XML_FALSE;
4316
51940
      }
4317
#endif /* XML_DTD */
4318
54374
      if (endDoctypeDeclHandler) {
4319
170
        endDoctypeDeclHandler(handlerArg);
4320
        handleDefault = XML_FALSE;
4321
170
      }
4322
      break;
4323
    case XML_ROLE_INSTANCE_START:
4324
#ifdef XML_DTD
4325
      /* if there is no DOCTYPE declaration then now is the
4326
         last chance to read the foreign DTD
4327
      */
4328
60134
      if (useForeignDTD) {
4329
170
        XML_Bool hadParamEntityRefs = dtd->hasParamEntityRefs;
4330
170
        dtd->hasParamEntityRefs = XML_TRUE;
4331

340
        if (paramEntityParsing && externalEntityRefHandler) {
4332
170
          ENTITY *entity = (ENTITY *)lookup(parser, &dtd->paramEntities,
4333
                                            externalSubsetName,
4334
                                            sizeof(ENTITY));
4335
170
          if (!entity)
4336
20
            return XML_ERROR_NO_MEMORY;
4337
150
          entity->base = curBase;
4338
150
          dtd->paramEntityRead = XML_FALSE;
4339
300
          if (!externalEntityRefHandler(externalEntityRefHandlerArg,
4340
                                        0,
4341
150
                                        entity->base,
4342
150
                                        entity->systemId,
4343
150
                                        entity->publicId))
4344
60
            return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
4345
90
          if (dtd->paramEntityRead) {
4346

90
            if (!dtd->standalone &&
4347
80
                notStandaloneHandler &&
4348
10
                !notStandaloneHandler(handlerArg))
4349
10
              return XML_ERROR_NOT_STANDALONE;
4350
          }
4351
          /* if we didn't read the foreign DTD then this means that there
4352
             is no external subset and we must reset dtd->hasParamEntityRefs
4353
          */
4354
          else
4355
10
            dtd->hasParamEntityRefs = hadParamEntityRefs;
4356
          /* end of DTD - no need to update dtd->keepProcessing */
4357
80
        }
4358
80
      }
4359
#endif /* XML_DTD */
4360
60044
      processor = contentProcessor;
4361
60044
      return contentProcessor(parser, s, end, nextPtr);
4362
    case XML_ROLE_ATTLIST_ELEMENT_NAME:
4363
2170
      declElementType = getElementType(parser, enc, s, next);
4364
2170
      if (!declElementType)
4365
160
        return XML_ERROR_NO_MEMORY;
4366
      goto checkAttListDeclHandler;
4367
    case XML_ROLE_ATTRIBUTE_NAME:
4368
2200
      declAttributeId = getAttributeId(parser, enc, s, next);
4369
2200
      if (!declAttributeId)
4370
160
        return XML_ERROR_NO_MEMORY;
4371
2040
      declAttributeIsCdata = XML_FALSE;
4372
2040
      declAttributeType = NULL;
4373
2040
      declAttributeIsId = XML_FALSE;
4374
2040
      goto checkAttListDeclHandler;
4375
    case XML_ROLE_ATTRIBUTE_TYPE_CDATA:
4376
890
      declAttributeIsCdata = XML_TRUE;
4377
890
      declAttributeType = atypeCDATA;
4378
890
      goto checkAttListDeclHandler;
4379
    case XML_ROLE_ATTRIBUTE_TYPE_ID:
4380
790
      declAttributeIsId = XML_TRUE;
4381
790
      declAttributeType = atypeID;
4382
790
      goto checkAttListDeclHandler;
4383
    case XML_ROLE_ATTRIBUTE_TYPE_IDREF:
4384
20
      declAttributeType = atypeIDREF;
4385
20
      goto checkAttListDeclHandler;
4386
    case XML_ROLE_ATTRIBUTE_TYPE_IDREFS:
4387
30
      declAttributeType = atypeIDREFS;
4388
30
      goto checkAttListDeclHandler;
4389
    case XML_ROLE_ATTRIBUTE_TYPE_ENTITY:
4390
20
      declAttributeType = atypeENTITY;
4391
20
      goto checkAttListDeclHandler;
4392
    case XML_ROLE_ATTRIBUTE_TYPE_ENTITIES:
4393
30
      declAttributeType = atypeENTITIES;
4394
30
      goto checkAttListDeclHandler;
4395
    case XML_ROLE_ATTRIBUTE_TYPE_NMTOKEN:
4396
20
      declAttributeType = atypeNMTOKEN;
4397
20
      goto checkAttListDeclHandler;
4398
    case XML_ROLE_ATTRIBUTE_TYPE_NMTOKENS:
4399
30
      declAttributeType = atypeNMTOKENS;
4400
    checkAttListDeclHandler:
4401

11760
      if (dtd->keepProcessing && attlistDeclHandler)
4402
560
        handleDefault = XML_FALSE;
4403
      break;
4404
    case XML_ROLE_ATTRIBUTE_ENUM_VALUE:
4405
    case XML_ROLE_ATTRIBUTE_NOTATION_VALUE:
4406

2560
      if (dtd->keepProcessing && attlistDeclHandler) {
4407
        const XML_Char *prefix;
4408
1180
        if (declAttributeType) {
4409
          prefix = enumValueSep;
4410
1010
        }
4411
        else {
4412
170
          prefix = (role == XML_ROLE_ATTRIBUTE_NOTATION_VALUE
4413
                    ? notationPrefix
4414
                    : enumValueStart);
4415
        }
4416
1180
        if (!poolAppendString(&tempPool, prefix))
4417
10
          return XML_ERROR_NO_MEMORY;
4418
1170
        if (!poolAppend(&tempPool, enc, s, next))
4419
10
          return XML_ERROR_NO_MEMORY;
4420
1160
        declAttributeType = tempPool.start;
4421
        handleDefault = XML_FALSE;
4422
1160
      }
4423
      break;
4424
    case XML_ROLE_IMPLIED_ATTRIBUTE_VALUE:
4425
    case XML_ROLE_REQUIRED_ATTRIBUTE_VALUE:
4426
1300
      if (dtd->keepProcessing) {
4427
2600
        if (!defineAttribute(declElementType, declAttributeId,
4428
1300
                             declAttributeIsCdata, declAttributeIsId,
4429
                             0, parser))
4430
50
          return XML_ERROR_NO_MEMORY;
4431

1330
        if (attlistDeclHandler && declAttributeType) {
4432
90
          if (*declAttributeType == XML_T(ASCII_LPAREN)
4433
130
              || (*declAttributeType == XML_T(ASCII_N)
4434
60
                  && declAttributeType[1] == XML_T(ASCII_O))) {
4435
            /* Enumerated or Notation type */
4436

100
            if (!poolAppendChar(&tempPool, XML_T(ASCII_RPAREN))
4437

90
                || !poolAppendChar(&tempPool, XML_T('\0')))
4438
10
              return XML_ERROR_NO_MEMORY;
4439
30
            declAttributeType = tempPool.start;
4440
30
            poolFinish(&tempPool);
4441
30
          }
4442
70
          *eventEndPP = s;
4443
140
          attlistDeclHandler(handlerArg, declElementType->name,
4444
70
                             declAttributeId->name, declAttributeType,
4445
70
                             0, role == XML_ROLE_REQUIRED_ATTRIBUTE_VALUE);
4446
70
          poolClear(&tempPool);
4447
          handleDefault = XML_FALSE;
4448
70
        }
4449
      }
4450
      break;
4451
    case XML_ROLE_DEFAULT_ATTRIBUTE_VALUE:
4452
    case XML_ROLE_FIXED_ATTRIBUTE_VALUE:
4453
700
      if (dtd->keepProcessing) {
4454
        const XML_Char *attVal;
4455
        enum XML_Error result =
4456
1400
          storeAttributeValue(parser, enc, declAttributeIsCdata,
4457
700
                              s + enc->minBytesPerChar,
4458
700
                              next - enc->minBytesPerChar,
4459
700
                              &dtd->pool);
4460
700
        if (result)
4461
40
          return result;
4462
660
        attVal = poolStart(&dtd->pool);
4463
660
        poolFinish(&dtd->pool);
4464
        /* ID attributes aren't allowed to have a default */
4465
1320
        if (!defineAttribute(declElementType, declAttributeId,
4466
660
                             declAttributeIsCdata, XML_FALSE, attVal, parser))
4467
40
          return XML_ERROR_NO_MEMORY;
4468

730
        if (attlistDeclHandler && declAttributeType) {
4469
120
          if (*declAttributeType == XML_T(ASCII_LPAREN)
4470
130
              || (*declAttributeType == XML_T(ASCII_N)
4471
30
                  && declAttributeType[1] == XML_T(ASCII_O))) {
4472
            /* Enumerated or Notation type */
4473

220
            if (!poolAppendChar(&tempPool, XML_T(ASCII_RPAREN))
4474

210
                || !poolAppendChar(&tempPool, XML_T('\0')))
4475
10
              return XML_ERROR_NO_MEMORY;
4476
90
            declAttributeType = tempPool.start;
4477
90
            poolFinish(&tempPool);
4478
90
          }
4479
100
          *eventEndPP = s;
4480
200
          attlistDeclHandler(handlerArg, declElementType->name,
4481
100
                             declAttributeId->name, declAttributeType,
4482
                             attVal,
4483
100
                             role == XML_ROLE_FIXED_ATTRIBUTE_VALUE);
4484
100
          poolClear(&tempPool);
4485
          handleDefault = XML_FALSE;
4486
100
        }
4487
610
      }
4488
      break;
4489
    case XML_ROLE_ENTITY_VALUE:
4490
1084
      if (dtd->keepProcessing) {
4491
1074
        enum XML_Error result = storeEntityValue(parser, enc,
4492
1074
                                            s + enc->minBytesPerChar,
4493
1074
                                            next - enc->minBytesPerChar);
4494
1074
        if (declEntity) {
4495
1064
          declEntity->textPtr = poolStart(&dtd->entityValuePool);
4496
1064
          declEntity->textLen = (int)(poolLength(&dtd->entityValuePool));
4497
1064
          poolFinish(&dtd->entityValuePool);
4498
1064
          if (entityDeclHandler) {
4499
200
            *eventEndPP = s;
4500
400
            entityDeclHandler(handlerArg,
4501
200
                              declEntity->name,
4502
200
                              declEntity->is_param,
4503
200
                              declEntity->textPtr,
4504
200
                              declEntity->textLen,
4505
200
                              curBase, 0, 0, 0);
4506
            handleDefault = XML_FALSE;
4507
200
          }
4508
        }
4509
        else
4510
10
          poolDiscard(&dtd->entityValuePool);
4511
1074
        if (result != XML_ERROR_NONE)
4512
160
          return result;
4513
914
      }
4514
      break;
4515
    case XML_ROLE_DOCTYPE_SYSTEM_ID:
4516
#ifdef XML_DTD
4517
53060
      useForeignDTD = XML_FALSE;
4518
#endif /* XML_DTD */
4519
53060
      dtd->hasParamEntityRefs = XML_TRUE;
4520
53060
      if (startDoctypeDeclHandler) {
4521
300
        doctypeSysid = poolStoreString(&tempPool, enc,
4522
150
                                       s + enc->minBytesPerChar,
4523
150
                                       next - enc->minBytesPerChar);
4524
150
        if (doctypeSysid == NULL)
4525
10
          return XML_ERROR_NO_MEMORY;
4526
140
        poolFinish(&tempPool);
4527
        handleDefault = XML_FALSE;
4528
140
      }
4529
#ifdef XML_DTD
4530
      else
4531
        /* use externalSubsetName to make doctypeSysid non-NULL
4532
           for the case where no startDoctypeDeclHandler is set */
4533
52910
        doctypeSysid = externalSubsetName;
4534
#endif /* XML_DTD */
4535
53070
      if (!dtd->standalone
4536
#ifdef XML_DTD
4537
106060
          && !paramEntityParsing
4538
#endif /* XML_DTD */
4539
103160
          && notStandaloneHandler
4540
50170
          && !notStandaloneHandler(handlerArg))
4541
10
        return XML_ERROR_NOT_STANDALONE;
4542
#ifndef XML_DTD
4543
      break;
4544
#else /* XML_DTD */
4545
53040
      if (!declEntity) {
4546
2920
        declEntity = (ENTITY *)lookup(parser,
4547
2920
                                      &dtd->paramEntities,
4548
                                      externalSubsetName,
4549
                                      sizeof(ENTITY));
4550
2920
        if (!declEntity)
4551
140
          return XML_ERROR_NO_MEMORY;
4552
2780
        declEntity->publicId = NULL;
4553
2780
      }
4554
      /* fall through */
4555
#endif /* XML_DTD */
4556
    case XML_ROLE_ENTITY_SYSTEM_ID:
4557

114148
      if (dtd->keepProcessing && declEntity) {
4558
114148
        declEntity->systemId = poolStoreString(&dtd->pool, enc,
4559
57074
                                               s + enc->minBytesPerChar,
4560
57074
                                               next - enc->minBytesPerChar);
4561
57074
        if (!declEntity->systemId)
4562
60
          return XML_ERROR_NO_MEMORY;
4563
57014
        declEntity->base = curBase;
4564
57014
        poolFinish(&dtd->pool);
4565
57014
        if (entityDeclHandler)
4566
770
          handleDefault = XML_FALSE;
4567
      }
4568
      break;
4569
    case XML_ROLE_ENTITY_COMPLETE:
4570

11142
      if (dtd->keepProcessing && declEntity && entityDeclHandler) {
4571
340
        *eventEndPP = s;
4572
680
        entityDeclHandler(handlerArg,
4573
340
                          declEntity->name,
4574
340
                          declEntity->is_param,
4575
                          0,0,
4576
340
                          declEntity->base,
4577
340
                          declEntity->systemId,
4578
340
                          declEntity->publicId,
4579
                          0);
4580
        handleDefault = XML_FALSE;
4581
340
      }
4582
      break;
4583
    case XML_ROLE_ENTITY_NOTATION_NAME:
4584

860
      if (dtd->keepProcessing && declEntity) {
4585
430
        declEntity->notation = poolStoreString(&dtd->pool, enc, s, next);
4586
430
        if (!declEntity->notation)
4587
20
          return XML_ERROR_NO_MEMORY;
4588
410
        poolFinish(&dtd->pool);
4589
410
        if (unparsedEntityDeclHandler) {
4590
120
          *eventEndPP = s;
4591
240
          unparsedEntityDeclHandler(handlerArg,
4592
120
                                    declEntity->name,
4593
120
                                    declEntity->base,
4594
120
                                    declEntity->systemId,
4595
120
                                    declEntity->publicId,
4596
120
                                    declEntity->notation);
4597
          handleDefault = XML_FALSE;
4598
120
        }
4599
290
        else if (entityDeclHandler) {
4600
30
          *eventEndPP = s;
4601
60
          entityDeclHandler(handlerArg,
4602
30
                            declEntity->name,
4603
                            0,0,0,
4604
30
                            declEntity->base,
4605
30
                            declEntity->systemId,
4606
30
                            declEntity->publicId,
4607
30
                            declEntity->notation);
4608
          handleDefault = XML_FALSE;
4609
30
        }
4610
      }
4611
      break;
4612
    case XML_ROLE_GENERAL_ENTITY_NAME:
4613
      {
4614
4848
        if (XmlPredefinedEntityName(enc, s, next)) {
4615
10
          declEntity = NULL;
4616
10
          break;
4617
        }
4618
4838
        if (dtd->keepProcessing) {
4619
4828
          const XML_Char *name = poolStoreString(&dtd->pool, enc, s, next);
4620
4828
          if (!name)
4621
110
            return XML_ERROR_NO_MEMORY;
4622
4718
          declEntity = (ENTITY *)lookup(parser, &dtd->generalEntities, name,
4623
                                        sizeof(ENTITY));
4624
4718
          if (!declEntity)
4625
350
            return XML_ERROR_NO_MEMORY;
4626
4368
          if (declEntity->name != name) {
4627
            poolDiscard(&dtd->pool);
4628
            declEntity = NULL;
4629
          }
4630
          else {
4631
4368
            poolFinish(&dtd->pool);
4632
4368
            declEntity->publicId = NULL;
4633
4368
            declEntity->is_param = XML_FALSE;
4634
            /* if we have a parent parser or are reading an internal parameter
4635
               entity, then the entity declaration is not considered "internal"
4636
            */
4637
13084
            declEntity->is_internal = !(parentParser || openInternalEntities);
4638
4368
            if (entityDeclHandler)
4639
310
              handleDefault = XML_FALSE;
4640
          }
4641
4368
        }
4642
        else {
4643
10
          poolDiscard(&dtd->pool);
4644
10
          declEntity = NULL;
4645
        }
4646
      }
4647
      break;
4648
    case XML_ROLE_PARAM_ENTITY_NAME:
4649
#ifdef XML_DTD
4650
960
      if (dtd->keepProcessing) {
4651
960
        const XML_Char *name = poolStoreString(&dtd->pool, enc, s, next);
4652
960
        if (!name)
4653
20
          return XML_ERROR_NO_MEMORY;
4654
940
        declEntity = (ENTITY *)lookup(parser, &dtd->paramEntities,
4655
                                           name, sizeof(ENTITY));
4656
940
        if (!declEntity)
4657
20
          return XML_ERROR_NO_MEMORY;
4658
920
        if (declEntity->name != name) {
4659
          poolDiscard(&dtd->pool);
4660
          declEntity = NULL;
4661
        }
4662
        else {
4663
920
          poolFinish(&dtd->pool);
4664
920
          declEntity->publicId = NULL;
4665
920
          declEntity->is_param = XML_TRUE;
4666
          /* if we have a parent parser or are reading an internal parameter
4667
             entity, then the entity declaration is not considered "internal"
4668
          */
4669
1930
          declEntity->is_internal = !(parentParser || openInternalEntities);
4670
920
          if (entityDeclHandler)
4671
400
            handleDefault = XML_FALSE;
4672
        }
4673
920
      }
4674
      else {
4675
        poolDiscard(&dtd->pool);
4676
        declEntity = NULL;
4677
      }
4678
#else /* not XML_DTD */
4679
      declEntity = NULL;
4680
#endif /* XML_DTD */
4681
      break;
4682
    case XML_ROLE_NOTATION_NAME:
4683
670
      declNotationPublicId = NULL;
4684
670
      declNotationName = NULL;
4685
670
      if (notationDeclHandler) {
4686
400
        declNotationName = poolStoreString(&tempPool, enc, s, next);
4687
400
        if (!declNotationName)
4688
30
          return XML_ERROR_NO_MEMORY;
4689
370
        poolFinish(&tempPool);
4690
        handleDefault = XML_FALSE;
4691
370
      }
4692
      break;
4693
    case XML_ROLE_NOTATION_PUBLIC_ID:
4694
80
      if (!XmlIsPublicId(enc, s, next, eventPP))
4695
        return XML_ERROR_PUBLICID;
4696
80
      if (declNotationName) {  /* means notationDeclHandler != NULL */
4697
160
        XML_Char *tem = poolStoreString(&tempPool,
4698
                                        enc,
4699
80
                                        s + enc->minBytesPerChar,
4700
80
                                        next - enc->minBytesPerChar);
4701
80
        if (!tem)
4702
10
          return XML_ERROR_NO_MEMORY;
4703
70
        normalizePublicId(tem);
4704
70
        declNotationPublicId = tem;
4705
70
        poolFinish(&tempPool);
4706
        handleDefault = XML_FALSE;
4707
70
      }
4708
      break;
4709
    case XML_ROLE_NOTATION_SYSTEM_ID:
4710

960
      if (declNotationName && notationDeclHandler) {
4711
        const XML_Char *systemId
4712
700
          = poolStoreString(&tempPool, enc,
4713
350
                            s + enc->minBytesPerChar,
4714
350
                            next - enc->minBytesPerChar);
4715
350
        if (!systemId)
4716
10
          return XML_ERROR_NO_MEMORY;
4717
340
        *eventEndPP = s;
4718
680
        notationDeclHandler(handlerArg,
4719
340
                            declNotationName,
4720
340
                            curBase,
4721
                            systemId,
4722
340
                            declNotationPublicId);
4723
        handleDefault = XML_FALSE;
4724
340
      }
4725
600
      poolClear(&tempPool);
4726
600
      break;
4727
    case XML_ROLE_NOTATION_NO_SYSTEM_ID:
4728

20
      if (declNotationPublicId && notationDeclHandler) {
4729
10
        *eventEndPP = s;
4730
20
        notationDeclHandler(handlerArg,
4731
10
                            declNotationName,
4732
10
                            curBase,
4733
                            0,
4734
10
                            declNotationPublicId);
4735
        handleDefault = XML_FALSE;
4736
10
      }
4737
10
      poolClear(&tempPool);
4738
10
      break;
4739
    case XML_ROLE_ERROR:
4740
130
      switch (tok) {
4741
      case XML_TOK_PARAM_ENTITY_REF:
4742
        /* PE references in internal subset are
4743
           not allowed within declarations. */
4744
        return XML_ERROR_PARAM_ENTITY_REF;
4745
      case XML_TOK_XML_DECL:
4746
10
        return XML_ERROR_MISPLACED_XML_PI;
4747
      default:
4748
120
        return XML_ERROR_SYNTAX;
4749
      }
4750
#ifdef XML_DTD
4751
    case XML_ROLE_IGNORE_SECT:
4752
      {
4753
        enum XML_Error result;
4754
70
        if (defaultHandler)
4755
30
          reportDefault(parser, enc, s, next);
4756
        handleDefault = XML_FALSE;
4757
70
        result = doIgnoreSection(parser, enc, &next, end, nextPtr, haveMore);
4758
70
        if (result != XML_ERROR_NONE)
4759
          return result;
4760
70
        else if (!next) {
4761
70
          processor = ignoreSectionProcessor;
4762
70
          return result;
4763
        }
4764
      }
4765
      break;
4766
#endif /* XML_DTD */
4767
    case XML_ROLE_GROUP_OPEN:
4768
4680
      if (prologState.level >= groupSize) {
4769
1500
        if (groupSize) {
4770
100
          char *temp = (char *)REALLOC(groupConnector, groupSize *= 2);
4771
100
          if (temp == NULL) {
4772
10
            groupSize /= 2;
4773
10
            return XML_ERROR_NO_MEMORY;
4774
          }
4775
90
          groupConnector = temp;
4776
90
          if (dtd->scaffIndex) {
4777
90
            int *temp = (int *)REALLOC(dtd->scaffIndex,
4778
                          groupSize * sizeof(int));
4779
90
            if (temp == NULL)
4780
10
              return XML_ERROR_NO_MEMORY;
4781
80
            dtd->scaffIndex = temp;
4782
80
          }
4783
80
        }
4784
        else {
4785
1400
          groupConnector = (char *)MALLOC(groupSize = 32);
4786
1400
          if (!groupConnector) {
4787
60
            groupSize = 0;
4788
60
            return XML_ERROR_NO_MEMORY;
4789
          }
4790
        }
4791
      }
4792
4600
      groupConnector[prologState.level] = 0;
4793
4600
      if (dtd->in_eldecl) {
4794
3960
        int myindex = nextScaffoldPart(parser);
4795
3960
        if (myindex < 0)
4796
70
          return XML_ERROR_NO_MEMORY;
4797
3890
        dtd->scaffIndex[dtd->scaffLevel] = myindex;
4798
3890
        dtd->scaffLevel++;
4799
3890
        dtd->scaffold[myindex].type = XML_CTYPE_SEQ;
4800
3890
        if (elementDeclHandler)
4801
3890
          handleDefault = XML_FALSE;
4802
3890
      }
4803
      break;
4804
    case XML_ROLE_GROUP_SEQUENCE:
4805
3260
      if (groupConnector[prologState.level] == ASCII_PIPE)
4806
        return XML_ERROR_SYNTAX;
4807
3260
      groupConnector[prologState.level] = ASCII_COMMA;
4808

6520
      if (dtd->in_eldecl && elementDeclHandler)
4809
3260
        handleDefault = XML_FALSE;
4810
      break;
4811
    case XML_ROLE_GROUP_CHOICE:
4812
7840
      if (groupConnector[prologState.level] == ASCII_COMMA)
4813
        return XML_ERROR_SYNTAX;
4814
8250
      if (dtd->in_eldecl
4815
15680
          && !groupConnector[prologState.level]
4816
8250
          && (dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
4817
410
              != XML_CTYPE_MIXED)
4818
          ) {
4819
        dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
4820
410
            = XML_CTYPE_CHOICE;
4821
410
        if (elementDeclHandler)
4822
410
          handleDefault = XML_FALSE;
4823
      }
4824
7840
      groupConnector[prologState.level] = ASCII_PIPE;
4825
7840
      break;
4826
    case XML_ROLE_PARAM_ENTITY_REF:
4827
#ifdef XML_DTD
4828
    case XML_ROLE_INNER_PARAM_ENTITY_REF:
4829
450
      dtd->hasParamEntityRefs = XML_TRUE;
4830
450
      if (!paramEntityParsing)
4831
20
        dtd->keepProcessing = dtd->standalone;
4832
      else {
4833
        const XML_Char *name;
4834
        ENTITY *entity;
4835
860
        name = poolStoreString(&dtd->pool, enc,
4836
430
                                s + enc->minBytesPerChar,
4837
430
                                next - enc->minBytesPerChar);
4838
430
        if (!name)
4839
10
          return XML_ERROR_NO_MEMORY;
4840
420
        entity = (ENTITY *)lookup(parser, &dtd->paramEntities, name, 0);
4841
420
        poolDiscard(&dtd->pool);
4842
        /* first, determine if a check for an existing declaration is needed;
4843
           if yes, check that the entity exists, and that it is internal,
4844
           otherwise call the skipped entity handler
4845
        */
4846

500
        if (prologState.documentEntity &&
4847
70
            (dtd->standalone
4848
20
             ? !openInternalEntities
4849
30
             : !dtd->hasParamEntityRefs)) {
4850
20
          if (!entity)
4851
            return XML_ERROR_UNDEFINED_ENTITY;
4852
20
          else if (!entity->is_internal) {
4853
            /* It's hard to exhaustively search the code to be sure,
4854
             * but there doesn't seem to be a way of executing the
4855
             * following line.  There are two cases:
4856
             *
4857
             * If 'standalone' is false, the DTD must have no
4858
             * parameter entities or we wouldn't have passed the outer
4859
             * 'if' statement.  That measn the only entity in the hash
4860
             * table is the external subset name "#" which cannot be
4861
             * given as a parameter entity name in XML syntax, so the
4862
             * lookup must have returned NULL and we don't even reach
4863
             * the test for an internal entity.
4864
             *
4865
             * If 'standalone' is true, it does not seem to be
4866
             * possible to create entities taking this code path that
4867
             * are not internal entities, so fail the test above.
4868
             *
4869
             * Because this analysis is very uncertain, the code is
4870
             * being left in place and merely removed from the
4871
             * coverage test statistics.
4872
             */
4873
            return XML_ERROR_ENTITY_DECLARED_IN_PE; /* LCOV_EXCL_LINE */
4874
          }
4875
        }
4876
400
        else if (!entity) {
4877
10
          dtd->keepProcessing = dtd->standalone;
4878
          /* cannot report skipped entities in declarations */
4879

20
          if ((role == XML_ROLE_PARAM_ENTITY_REF) && skippedEntityHandler) {
4880
10
            skippedEntityHandler(handlerArg, name, 1);
4881
            handleDefault = XML_FALSE;
4882
10
          }
4883
10
          break;
4884
        }
4885
410
        if (entity->open)
4886
20
          return XML_ERROR_RECURSIVE_ENTITY_REF;
4887
390
        if (entity->textPtr) {
4888
          enum XML_Error result;
4889
          XML_Bool betweenDecl =
4890
90
            (role == XML_ROLE_PARAM_ENTITY_REF ? XML_TRUE : XML_FALSE);
4891
90
          result = processInternalEntity(parser, entity, betweenDecl);
4892
90
          if (result != XML_ERROR_NONE)
4893
10
            return result;
4894
          handleDefault = XML_FALSE;
4895
80
          break;
4896
        }
4897
300
        if (externalEntityRefHandler) {
4898
290
          dtd->paramEntityRead = XML_FALSE;
4899
290
          entity->open = XML_TRUE;
4900
580
          if (!externalEntityRefHandler(externalEntityRefHandlerArg,
4901
                                        0,
4902
290
                                        entity->base,
4903
290
                                        entity->systemId,
4904
290
                                        entity->publicId)) {
4905
            entity->open = XML_FALSE;
4906
90
            return XML_ERROR_EXTERNAL_ENTITY_HANDLING;
4907
          }
4908
          entity->open = XML_FALSE;
4909
          handleDefault = XML_FALSE;
4910
200
          if (!dtd->paramEntityRead) {
4911
10
            dtd->keepProcessing = dtd->standalone;
4912
10
            break;
4913
          }
4914
        }
4915
        else {
4916
10
          dtd->keepProcessing = dtd->standalone;
4917
10
          break;
4918
        }
4919
190
      }
4920
#endif /* XML_DTD */
4921

220
      if (!dtd->standalone &&
4922
210
          notStandaloneHandler &&
4923
10
          !notStandaloneHandler(handlerArg))
4924
10
        return XML_ERROR_NOT_STANDALONE;
4925
      break;
4926
4927
    /* Element declaration stuff */
4928
4929
    case XML_ROLE_ELEMENT_NAME:
4930
3630
      if (elementDeclHandler) {
4931
930
        declElementType = getElementType(parser, enc, s, next);
4932
930
        if (!declElementType)
4933
80
          return XML_ERROR_NO_MEMORY;
4934
850
        dtd->scaffLevel = 0;
4935
850
        dtd->scaffCount = 0;
4936
850
        dtd->in_eldecl = XML_TRUE;
4937
        handleDefault = XML_FALSE;
4938
850
      }
4939
      break;
4940
4941
    case XML_ROLE_CONTENT_ANY:
4942
    case XML_ROLE_CONTENT_EMPTY:
4943
2130
      if (dtd->in_eldecl) {
4944
100
        if (elementDeclHandler) {
4945
100
          XML_Content * content = (XML_Content *) MALLOC(sizeof(XML_Content));
4946
100
          if (!content)
4947
10
            return XML_ERROR_NO_MEMORY;
4948
90
          content->quant = XML_CQUANT_NONE;
4949
90
          content->name = NULL;
4950
90
          content->numchildren = 0;
4951
90
          content->children = NULL;
4952
90
          content->type = ((role == XML_ROLE_CONTENT_ANY) ?
4953
                           XML_CTYPE_ANY :
4954
                           XML_CTYPE_EMPTY);
4955
90
          *eventEndPP = s;
4956
90
          elementDeclHandler(handlerArg, declElementType->name, content);
4957
          handleDefault = XML_FALSE;
4958
90
        }
4959
90
        dtd->in_eldecl = XML_FALSE;
4960
90
      }
4961
      break;
4962
4963
    case XML_ROLE_CONTENT_PCDATA:
4964
460
      if (dtd->in_eldecl) {
4965
110
        dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel - 1]].type
4966
110
            = XML_CTYPE_MIXED;
4967
110
        if (elementDeclHandler)
4968
110
          handleDefault = XML_FALSE;
4969
      }
4970
      break;
4971
4972
    case XML_ROLE_CONTENT_ELEMENT:
4973
      quant = XML_CQUANT_NONE;
4974
8540
      goto elementContent;
4975
    case XML_ROLE_CONTENT_ELEMENT_OPT:
4976
      quant = XML_CQUANT_OPT;
4977
3230
      goto elementContent;
4978
    case XML_ROLE_CONTENT_ELEMENT_REP:
4979
      quant = XML_CQUANT_REP;
4980
      goto elementContent;
4981
    case XML_ROLE_CONTENT_ELEMENT_PLUS:
4982
280
      quant = XML_CQUANT_PLUS;
4983
    elementContent:
4984
12050
      if (dtd->in_eldecl) {
4985
        ELEMENT_TYPE *el;
4986
        const XML_Char *name;
4987
        int nameLen;
4988
26470
        const char *nxt = (quant == XML_CQUANT_NONE
4989
                           ? next
4990
3230
                           : next - enc->minBytesPerChar);
4991
11620
        int myindex = nextScaffoldPart(parser);
4992
11620
        if (myindex < 0)
4993
10
          return XML_ERROR_NO_MEMORY;
4994
11610
        dtd->scaffold[myindex].type = XML_CTYPE_NAME;
4995
11610
        dtd->scaffold[myindex].quant = quant;
4996
11610
        el = getElementType(parser, enc, s, nxt);
4997
11610
        if (!el)
4998
350
          return XML_ERROR_NO_MEMORY;
4999
11260
        name = el->name;
5000
11260
        dtd->scaffold[myindex].name = name;
5001
        nameLen = 0;
5002
41720
        for (; name[nameLen++]; );
5003
11260
        dtd->contentStringLen +=  nameLen;
5004
11260
        if (elementDeclHandler)
5005
11260
          handleDefault = XML_FALSE;
5006
11260
      }
5007
      break;
5008
5009
    case XML_ROLE_GROUP_CLOSE:
5010
      quant = XML_CQUANT_NONE;
5011
2970
      goto closeGroup;
5012
    case XML_ROLE_GROUP_CLOSE_OPT:
5013
      quant = XML_CQUANT_OPT;
5014
      goto closeGroup;
5015
    case XML_ROLE_GROUP_CLOSE_REP:
5016
      quant = XML_CQUANT_REP;
5017
350
      goto closeGroup;
5018
    case XML_ROLE_GROUP_CLOSE_PLUS:
5019
70
      quant = XML_CQUANT_PLUS;
5020
    closeGroup:
5021
3390
      if (dtd->in_eldecl) {
5022
2750
        if (elementDeclHandler)
5023
2750
          handleDefault = XML_FALSE;
5024
2750
        dtd->scaffLevel--;
5025
2750
        dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel]].quant = quant;
5026
2750
        if (dtd->scaffLevel == 0) {
5027
270
          if (!handleDefault) {
5028
270
            XML_Content *model = build_model(parser);
5029
270
            if (!model)
5030
30
              return XML_ERROR_NO_MEMORY;
5031
240
            *eventEndPP = s;
5032
240
            elementDeclHandler(handlerArg, declElementType->name, model);
5033
240
          }
5034
240
          dtd->in_eldecl = XML_FALSE;
5035
240
          dtd->contentStringLen = 0;
5036
240
        }
5037
      }
5038
      break;
5039
      /* End element declaration stuff */
5040
5041
    case XML_ROLE_PI:
5042
50230
      if (!reportProcessingInstruction(parser, enc, s, next))
5043
30
        return XML_ERROR_NO_MEMORY;
5044
      handleDefault = XML_FALSE;
5045
50200
      break;
5046
    case XML_ROLE_COMMENT:
5047
100160
      if (!reportComment(parser, enc, s, next))
5048
10
        return XML_ERROR_NO_MEMORY;
5049
      handleDefault = XML_FALSE;
5050
100150
      break;
5051
    case XML_ROLE_NONE:
5052
282212
      switch (tok) {
5053
      case XML_TOK_BOM:
5054
        handleDefault = XML_FALSE;
5055
60
        break;
5056
      }
5057
      break;
5058
    case XML_ROLE_DOCTYPE_NONE:
5059
333456
      if (startDoctypeDeclHandler)
5060
1880
        handleDefault = XML_FALSE;
5061
      break;
5062
    case XML_ROLE_ENTITY_NONE:
5063

60622
      if (dtd->keepProcessing && entityDeclHandler)
5064
5210
        handleDefault = XML_FALSE;
5065
      break;
5066
    case XML_ROLE_NOTATION_NONE:
5067
3910
      if (notationDeclHandler)
5068
2310
        handleDefault = XML_FALSE;
5069
      break;
5070
    case XML_ROLE_ATTLIST_NONE:
5071

31720
      if (dtd->keepProcessing && attlistDeclHandler)
5072
2890
        handleDefault = XML_FALSE;
5073
      break;
5074
    case XML_ROLE_ELEMENT_NONE:
5075
13970
      if (elementDeclHandler)
5076
3030
        handleDefault = XML_FALSE;
5077
      break;
5078
    } /* end of big switch */
5079
5080

2082882
    if (handleDefault && defaultHandler)
5081
2080
      reportDefault(parser, enc, s, next);
5082
5083
1164966
    switch (ps_parsing) {
5084
    case XML_SUSPENDED:
5085
30
      *nextPtr = next;
5086
30
      return XML_ERROR_NONE;
5087
    case XML_FINISHED:
5088
10
      return XML_ERROR_ABORTED;
5089
    default:
5090
1164926
      s = next;
5091
1164926
      tok = XmlPrologTok(enc, s, end, &next);
5092
    }
5093
1164926
  }
5094
  /* not reached */
5095
3582984
}
5096
5097
static enum XML_Error PTRCALL
5098
epilogProcessor(XML_Parser parser,
5099
                const char *s,
5100
                const char *end,
5101
                const char **nextPtr)
5102
{
5103
107320
  processor = epilogProcessor;
5104
53660
  eventPtr = s;
5105
53660
  for (;;) {
5106
303980
    const char *next = NULL;
5107
303980
    int tok = XmlPrologTok(encoding, s, end, &next);
5108
303980
    eventEndPtr = next;
5109


303980
    switch (tok) {
5110
    /* report partial linebreak - it might be the last token */
5111
    case -XML_TOK_PROLOG_S:
5112
30
      if (defaultHandler) {
5113
10
        reportDefault(parser, encoding, s, next);
5114
10
        if (ps_parsing == XML_FINISHED)
5115
10
          return XML_ERROR_ABORTED;
5116
      }
5117
20
      *nextPtr = next;
5118
20
      return XML_ERROR_NONE;
5119
    case XML_TOK_NONE:
5120
52710
      *nextPtr = s;
5121
52710
      return XML_ERROR_NONE;
5122
    case XML_TOK_PROLOG_S:
5123
150320
      if (defaultHandler)
5124
30
        reportDefault(parser, encoding, s, next);
5125
      break;
5126
    case XML_TOK_PI:
5127
20
      if (!reportProcessingInstruction(parser, encoding, s, next))
5128
10
        return XML_ERROR_NO_MEMORY;
5129
      break;
5130
    case XML_TOK_COMMENT:
5131
100020
      if (!reportComment(parser, encoding, s, next))
5132
10
        return XML_ERROR_NO_MEMORY;
5133
      break;
5134
    case XML_TOK_INVALID:
5135
      eventPtr = next;
5136
      return XML_ERROR_INVALID_TOKEN;
5137
    case XML_TOK_PARTIAL:
5138
850
      if (!ps_finalBuffer) {
5139
840
        *nextPtr = s;
5140
840
        return XML_ERROR_NONE;
5141
      }
5142
10
      return XML_ERROR_UNCLOSED_TOKEN;
5143
    case XML_TOK_PARTIAL_CHAR:
5144
30
      if (!ps_finalBuffer) {
5145
20
        *nextPtr = s;
5146
20
        return XML_ERROR_NONE;
5147
      }
5148
10
      return XML_ERROR_PARTIAL_CHAR;
5149
    default:
5150
      return XML_ERROR_JUNK_AFTER_DOC_ELEMENT;
5151
    }
5152
250340
    eventPtr = s = next;
5153
250340
    switch (ps_parsing) {
5154
    case XML_SUSPENDED:
5155
10
      *nextPtr = next;
5156
10
      return XML_ERROR_NONE;
5157
    case XML_FINISHED:
5158
10
      return XML_ERROR_ABORTED;
5159
    default: ;
5160
    }
5161
554300
  }
5162
53660
}
5163
5164
static enum XML_Error
5165
processInternalEntity(XML_Parser parser, ENTITY *entity,
5166
                      XML_Bool betweenDecl)
5167
{
5168
  const char *textStart, *textEnd;
5169
400
  const char *next;
5170
  enum XML_Error result;
5171
  OPEN_INTERNAL_ENTITY *openEntity;
5172
5173
200
  if (freeInternalEntities) {
5174
    openEntity = freeInternalEntities;
5175
10
    freeInternalEntities = openEntity->next;
5176
10
  }
5177
  else {
5178
190
    openEntity = (OPEN_INTERNAL_ENTITY *)MALLOC(sizeof(OPEN_INTERNAL_ENTITY));
5179
190
    if (!openEntity)
5180
      return XML_ERROR_NO_MEMORY;
5181
  }
5182
200
  entity->open = XML_TRUE;
5183
200
  entity->processed = 0;
5184
200
  openEntity->next = openInternalEntities;
5185
200
  openInternalEntities = openEntity;
5186
200
  openEntity->entity = entity;
5187
200
  openEntity->startTagLevel = tagLevel;
5188
200
  openEntity->betweenDecl = betweenDecl;
5189
200
  openEntity->internalEventPtr = NULL;
5190
200
  openEntity->internalEventEndPtr = NULL;
5191
200
  textStart = (char *)entity->textPtr;
5192
200
  textEnd = (char *)(entity->textPtr + entity->textLen);
5193
  /* Set a safe default value in case 'next' does not get set */
5194
200
  next = textStart;
5195
5196
#ifdef XML_DTD
5197
200
  if (entity->is_param) {
5198
90
    int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next);
5199
180
    result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
5200
90
                      next, &next, XML_FALSE);
5201
90
  }
5202
  else
5203
#endif /* XML_DTD */
5204
110
    result = doContent(parser, tagLevel, internalEncoding, textStart,
5205
                       textEnd, &next, XML_FALSE);
5206
5207
200
  if (result == XML_ERROR_NONE) {
5208

210
    if (textEnd != next && ps_parsing == XML_SUSPENDED) {
5209
40
      entity->processed = (int)(next - textStart);
5210
40
      processor = internalEntityProcessor;
5211
40
    }
5212
    else {
5213
130
      entity->open = XML_FALSE;
5214
130
      openInternalEntities = openEntity->next;
5215
      /* put openEntity back in list of free instances */
5216
130
      openEntity->next = freeInternalEntities;
5217
130
      freeInternalEntities = openEntity;
5218
    }
5219
  }
5220
200
  return result;
5221
200
}
5222
5223
static enum XML_Error PTRCALL
5224
internalEntityProcessor(XML_Parser parser,
5225
                        const char *s,
5226
                        const char *end,
5227
                        const char **nextPtr)
5228
{
5229
  ENTITY *entity;
5230
  const char *textStart, *textEnd;
5231
80
  const char *next;
5232
  enum XML_Error result;
5233
40
  OPEN_INTERNAL_ENTITY *openEntity = openInternalEntities;
5234
40
  if (!openEntity)
5235
    return XML_ERROR_UNEXPECTED_STATE;
5236
5237
40
  entity = openEntity->entity;
5238
40
  textStart = ((char *)entity->textPtr) + entity->processed;
5239
40
  textEnd = (char *)(entity->textPtr + entity->textLen);
5240
  /* Set a safe default value in case 'next' does not get set */
5241
40
  next = textStart;
5242
5243
#ifdef XML_DTD
5244
40
  if (entity->is_param) {
5245
10
    int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next);
5246
20
    result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
5247
10
                      next, &next, XML_FALSE);
5248
10
  }
5249
  else
5250
#endif /* XML_DTD */
5251
30
    result = doContent(parser, openEntity->startTagLevel, internalEncoding,
5252
                       textStart, textEnd, &next, XML_FALSE);
5253
5254
40
  if (result != XML_ERROR_NONE)
5255
10
    return result;
5256

40
  else if (textEnd != next && ps_parsing == XML_SUSPENDED) {
5257
10
    entity->processed = (int)(next - (char *)entity->textPtr);
5258
10
    return result;
5259
  }
5260
  else {
5261
20
    entity->open = XML_FALSE;
5262
20
    openInternalEntities = openEntity->next;
5263
    /* put openEntity back in list of free instances */
5264
20
    openEntity->next = freeInternalEntities;
5265
20
    freeInternalEntities = openEntity;
5266
  }
5267
5268
#ifdef XML_DTD
5269
20
  if (entity->is_param) {
5270
    int tok;
5271
10
    processor = prologProcessor;
5272
10
    tok = XmlPrologTok(encoding, s, end, &next);
5273
20
    return doProlog(parser, encoding, s, end, tok, next, nextPtr,
5274
10
                    (XML_Bool)!ps_finalBuffer);
5275
  }
5276
  else
5277
#endif /* XML_DTD */
5278
  {
5279
10
    processor = contentProcessor;
5280
    /* see externalEntityContentProcessor vs contentProcessor */
5281
20
    return doContent(parser, parentParser ? 1 : 0, encoding, s, end,
5282
10
                     nextPtr, (XML_Bool)!ps_finalBuffer);
5283
  }
5284
40
}
5285
5286
static enum XML_Error PTRCALL
5287
errorProcessor(XML_Parser parser,
5288
               const char *UNUSED_P(s),
5289
               const char *UNUSED_P(end),
5290
               const char **UNUSED_P(nextPtr))
5291
{
5292
20
  return errorCode;
5293
}
5294
5295
static enum XML_Error
5296
storeAttributeValue(XML_Parser parser, const ENCODING *enc, XML_Bool isCdata,
5297
                    const char *ptr, const char *end,
5298
                    STRING_POOL *pool)
5299
{
5300
1820
  enum XML_Error result = appendAttributeValue(parser, enc, isCdata, ptr,
5301
                                               end, pool);
5302
910
  if (result)
5303
80
    return result;
5304

1150
  if (!isCdata && poolLength(pool) && poolLastChar(pool) == 0x20)
5305
30
    poolChop(pool);
5306

1940
  if (!poolAppendChar(pool, XML_T('\0')))
5307
10
    return XML_ERROR_NO_MEMORY;
5308
820
  return XML_ERROR_NONE;
5309
910
}
5310
5311
static enum XML_Error
5312
appendAttributeValue(XML_Parser parser, const ENCODING *enc, XML_Bool isCdata,
5313
                     const char *ptr, const char *end,
5314
                     STRING_POOL *pool)
5315
{
5316
1960
  DTD * const dtd = _dtd;  /* save one level of indirection */
5317
980
  for (;;) {
5318
6170
    const char *next;
5319
6170
    int tok = XmlAttributeValueTok(enc, ptr, end, &next);
5320


6170
    switch (tok) {
5321
    case XML_TOK_NONE:
5322
890
      return XML_ERROR_NONE;
5323
    case XML_TOK_INVALID:
5324
10
      if (enc == encoding)
5325
10
        eventPtr = next;
5326
10
      return XML_ERROR_INVALID_TOKEN;
5327
    case XML_TOK_PARTIAL:
5328
10
      if (enc == encoding)
5329
10
        eventPtr = ptr;
5330
10
      return XML_ERROR_INVALID_TOKEN;
5331
    case XML_TOK_CHAR_REF:
5332
      {
5333
320
        XML_Char buf[XML_ENCODE_MAX];
5334
        int i;
5335
320
        int n = XmlCharRefNumber(enc, ptr);
5336
320
        if (n < 0) {
5337
          if (enc == encoding)
5338
            eventPtr = ptr;
5339
          return XML_ERROR_BAD_CHAR_REF;
5340
        }
5341
320
        if (!isCdata
5342
320
            && n == 0x20 /* space */
5343
            && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
5344
          break;
5345
320
        n = XmlEncode(n, (ICHAR *)buf);
5346
        /* The XmlEncode() functions can never return 0 here.  That
5347
         * error return happens if the code point passed in is either
5348
         * negative or greater than or equal to 0x110000.  The
5349
         * XmlCharRefNumber() functions will all return a number
5350
         * strictly less than 0x110000 or a negative value if an error
5351
         * occurred.  The negative value is intercepted above, so
5352
         * XmlEncode() is never passed a value it might return an
5353
         * error for.
5354
         */
5355
1820
        for (i = 0; i < n; i++) {
5356

1250
          if (!poolAppendChar(pool, buf[i]))
5357
10
            return XML_ERROR_NO_MEMORY;
5358
        }
5359
940
      }
5360
      break;
5361
    case XML_TOK_DATA_CHARS:
5362
2390
      if (!poolAppend(pool, enc, ptr, next))
5363
20
        return XML_ERROR_NO_MEMORY;
5364
      break;
5365
    case XML_TOK_TRAILING_CR:
5366
10
      next = ptr + enc->minBytesPerChar;
5367
      /* fall through */
5368
    case XML_TOK_ATTRIBUTE_VALUE_S:
5369
    case XML_TOK_DATA_NEWLINE:
5370

3180
      if (!isCdata && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
5371
        break;
5372

4270
      if (!poolAppendChar(pool, 0x20))
5373
10
        return XML_ERROR_NO_MEMORY;
5374
2120
      break;
5375
    case XML_TOK_ENTITY_REF:
5376
      {
5377
        const XML_Char *name;
5378
        ENTITY *entity;
5379
        char checkEntityDecl;
5380
100
        XML_Char ch = (XML_Char) XmlPredefinedEntityName(enc,
5381
                                              ptr + enc->minBytesPerChar,
5382
                                              next - enc->minBytesPerChar);
5383
100
        if (ch) {
5384

50
          if (!poolAppendChar(pool, ch))
5385
10
                return XML_ERROR_NO_MEMORY;
5386
10
          break;
5387
        }
5388
160
        name = poolStoreString(&temp2Pool, enc,
5389
80
                               ptr + enc->minBytesPerChar,
5390
80
                               next - enc->minBytesPerChar);
5391
80
        if (!name)
5392
10
          return XML_ERROR_NO_MEMORY;
5393
70
        entity = (ENTITY *)lookup(parser, &dtd->generalEntities, name, 0);
5394
70
        poolDiscard(&temp2Pool);
5395
        /* First, determine if a check for an existing declaration is needed;
5396
           if yes, check that the entity exists, and that it is internal.
5397
        */
5398
70
        if (pool == &dtd->pool)  /* are we called from prolog? */
5399
10
          checkEntityDecl =
5400
#ifdef XML_DTD
5401
20
              prologState.documentEntity &&
5402
#endif /* XML_DTD */
5403
20
              (dtd->standalone
5404
10
               ? !openInternalEntities
5405
               : !dtd->hasParamEntityRefs);
5406
        else /* if (pool == &tempPool): we are called from content */
5407
120
          checkEntityDecl = !dtd->hasParamEntityRefs || dtd->standalone;
5408
70
        if (checkEntityDecl) {
5409
60
          if (!entity)
5410
            return XML_ERROR_UNDEFINED_ENTITY;
5411
60
          else if (!entity->is_internal)
5412
            return XML_ERROR_ENTITY_DECLARED_IN_PE;
5413
        }
5414
10
        else if (!entity) {
5415
          /* Cannot report skipped entity here - see comments on
5416
             skippedEntityHandler.
5417
          if (skippedEntityHandler)
5418
            skippedEntityHandler(handlerArg, name, 0);
5419
          */
5420
          /* Cannot call the default handler because this would be
5421
             out of sync with the call to the startElementHandler.
5422
          if ((pool == &tempPool) && defaultHandler)
5423
            reportDefault(parser, enc, ptr, next);
5424
          */
5425
          break;
5426
        }
5427
70
        if (entity->open) {
5428
          if (enc == encoding) {
5429
            /* It does not appear that this line can be executed.
5430
             *
5431
             * The "if (entity->open)" check catches recursive entity
5432
             * definitions.  In order to be called with an open
5433
             * entity, it must have gone through this code before and
5434
             * been through the recursive call to
5435
             * appendAttributeValue() some lines below.  That call
5436
             * sets the local encoding ("enc") to the parser's
5437
             * internal encoding (internal_utf8 or internal_utf16),
5438
             * which can never be the same as the principle encoding.
5439
             * It doesn't appear there is another code path that gets
5440
             * here with entity->open being TRUE.
5441
             *
5442
             * Since it is not certain that this logic is watertight,
5443
             * we keep the line and merely exclude it from coverage
5444
             * tests.
5445
             */
5446
            eventPtr = ptr; /* LCOV_EXCL_LINE */
5447
          }
5448
          return XML_ERROR_RECURSIVE_ENTITY_REF;
5449
        }
5450
70
        if (entity->notation) {
5451
          if (enc == encoding)
5452
            eventPtr = ptr;
5453
          return XML_ERROR_BINARY_ENTITY_REF;
5454
        }
5455
70
        if (!entity->textPtr) {
5456
          if (enc == encoding)
5457
            eventPtr = ptr;
5458
          return XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF;
5459
        }
5460
        else {
5461
          enum XML_Error result;
5462
70
          const XML_Char *textEnd = entity->textPtr + entity->textLen;
5463
70
          entity->open = XML_TRUE;
5464
140
          result = appendAttributeValue(parser, internalEncoding, isCdata,
5465
70
                                        (char *)entity->textPtr,
5466
                                        (char *)textEnd, pool);
5467
70
          entity->open = XML_FALSE;
5468
70
          if (result)
5469
10
            return result;
5470
60
        }
5471
60
      }
5472
      break;
5473
    default:
5474
      /* The only token returned by XmlAttributeValueTok() that does
5475
       * not have an explicit case here is XML_TOK_PARTIAL_CHAR.
5476
       * Getting that would require an entity name to contain an
5477
       * incomplete XML character (e.g. \xE2\x82); however previous
5478
       * tokenisers will have already recognised and rejected such
5479
       * names before XmlAttributeValueTok() gets a look-in.  This
5480
       * default case should be retained as a safety net, but the code
5481
       * excluded from coverage tests.
5482
       *
5483
       * LCOV_EXCL_START
5484
       */
5485
      if (enc == encoding)
5486
        eventPtr = ptr;
5487
      return XML_ERROR_UNEXPECTED_STATE;
5488
      /* LCOV_EXCL_STOP */
5489
    }
5490
5180
    ptr = next;
5491
11340
  }
5492
  /* not reached */
5493
980
}
5494
5495
static enum XML_Error
5496
storeEntityValue(XML_Parser parser,
5497
                 const ENCODING *enc,
5498
                 const char *entityTextPtr,
5499
                 const char *entityTextEnd)
5500
{
5501
2488
  DTD * const dtd = _dtd;  /* save one level of indirection */
5502
1244
  STRING_POOL *pool = &(dtd->entityValuePool);
5503
  enum XML_Error result = XML_ERROR_NONE;
5504
#ifdef XML_DTD
5505
1244
  int oldInEntityValue = prologState.inEntityValue;
5506
1244
  prologState.inEntityValue = 1;
5507
#endif /* XML_DTD */
5508
  /* never return Null for the value argument in EntityDeclHandler,
5509
     since this would indicate an external entity; therefore we
5510
     have to make sure that entityValuePool.start is not null */
5511
1244
  if (!pool->blocks) {
5512
1034
    if (!poolGrow(pool))
5513
40
      return XML_ERROR_NO_MEMORY;
5514
  }
5515
5516
  for (;;) {
5517
2478
    const char *next;
5518
2478
    int tok = XmlEntityValueTok(enc, entityTextPtr, entityTextEnd, &next);
5519


2478
    switch (tok) {
5520
    case XML_TOK_PARAM_ENTITY_REF:
5521
#ifdef XML_DTD
5522

350
      if (isParamEntity || enc != encoding) {
5523
        const XML_Char *name;
5524
        ENTITY *entity;
5525
700
        name = poolStoreString(&tempPool, enc,
5526
350
                               entityTextPtr + enc->minBytesPerChar,
5527
350
                               next - enc->minBytesPerChar);
5528
350
        if (!name) {
5529
          result = XML_ERROR_NO_MEMORY;
5530
10
          goto endEntityValue;
5531
        }
5532
340
        entity = (ENTITY *)lookup(parser, &dtd->paramEntities, name, 0);
5533
340
        poolDiscard(&tempPool);
5534
340
        if (!entity) {
5535
          /* not a well-formedness error - see XML 1.0: WFC Entity Declared */
5536
          /* cannot report skipped entity here - see comments on
5537
             skippedEntityHandler
5538
          if (skippedEntityHandler)
5539
            skippedEntityHandler(handlerArg, name, 0);
5540
          */
5541
10
          dtd->keepProcessing = dtd->standalone;
5542
10
          goto endEntityValue;
5543
        }
5544
330
        if (entity->open) {
5545
10
          if (enc == encoding)
5546
10
            eventPtr = entityTextPtr;
5547
          result = XML_ERROR_RECURSIVE_ENTITY_REF;
5548
10
          goto endEntityValue;
5549
        }
5550
320
        if (entity->systemId) {
5551
310
          if (externalEntityRefHandler) {
5552
300
            dtd->paramEntityRead = XML_FALSE;
5553
300
            entity->open = XML_TRUE;
5554
600
            if (!externalEntityRefHandler(externalEntityRefHandlerArg,
5555
                                          0,
5556
300
                                          entity->base,
5557
300
                                          entity->systemId,
5558
300
                                          entity->publicId)) {
5559
              entity->open = XML_FALSE;
5560
              result = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
5561
50
              goto endEntityValue;
5562
            }
5563
            entity->open = XML_FALSE;
5564
250
            if (!dtd->paramEntityRead)
5565
10
              dtd->keepProcessing = dtd->standalone;
5566
          }
5567
          else
5568
10
            dtd->keepProcessing = dtd->standalone;
5569
20
        }
5570
        else {
5571
10
          entity->open = XML_TRUE;
5572
10
          result = storeEntityValue(parser,
5573
10
                                    internalEncoding,
5574
10
                                    (char *)entity->textPtr,
5575
                                    (char *)(entity->textPtr
5576
10
                                             + entity->textLen));
5577
10
          entity->open = XML_FALSE;
5578
10
          if (result)
5579
10
            goto endEntityValue;
5580
        }
5581
260
        break;
5582
      }
5583
#endif /* XML_DTD */
5584
      /* In the internal subset, PE references are not legal
5585
         within markup declarations, e.g entity values in this case. */
5586
      eventPtr = entityTextPtr;
5587
      result = XML_ERROR_PARAM_ENTITY_REF;
5588
      goto endEntityValue;
5589
    case XML_TOK_NONE:
5590
      result = XML_ERROR_NONE;
5591
1054
      goto endEntityValue;
5592
    case XML_TOK_ENTITY_REF:
5593
    case XML_TOK_DATA_CHARS:
5594
824
      if (!poolAppend(pool, enc, entityTextPtr, next)) {
5595
        result = XML_ERROR_NO_MEMORY;
5596
10
        goto endEntityValue;
5597
      }
5598
      break;
5599
    case XML_TOK_TRAILING_CR:
5600
10
      next = entityTextPtr + enc->minBytesPerChar;
5601
      /* fall through */
5602
    case XML_TOK_DATA_NEWLINE:
5603

150
      if (pool->end == pool->ptr && !poolGrow(pool)) {
5604
              result = XML_ERROR_NO_MEMORY;
5605
10
        goto endEntityValue;
5606
      }
5607
120
      *(pool->ptr)++ = 0xA;
5608
120
      break;
5609
    case XML_TOK_CHAR_REF:
5610
      {
5611
100
        XML_Char buf[XML_ENCODE_MAX];
5612
        int i;
5613
100
        int n = XmlCharRefNumber(enc, entityTextPtr);
5614
100
        if (n < 0) {
5615
20
          if (enc == encoding)
5616
20
            eventPtr = entityTextPtr;
5617
          result = XML_ERROR_BAD_CHAR_REF;
5618
20
          goto endEntityValue;
5619
        }
5620
80
        n = XmlEncode(n, (ICHAR *)buf);
5621
        /* The XmlEncode() functions can never return 0 here.  That
5622
         * error return happens if the code point passed in is either
5623
         * negative or greater than or equal to 0x110000.  The
5624
         * XmlCharRefNumber() functions will all return a number
5625
         * strictly less than 0x110000 or a negative value if an error
5626
         * occurred.  The negative value is intercepted above, so
5627
         * XmlEncode() is never passed a value it might return an
5628
         * error for.
5629
         */
5630
300
        for (i = 0; i < n; i++) {
5631

100
          if (pool->end == pool->ptr && !poolGrow(pool)) {
5632
            result = XML_ERROR_NO_MEMORY;
5633
10
            goto endEntityValue;
5634
          }
5635
70
          *(pool->ptr)++ = buf[i];
5636
        }
5637
170
      }
5638
      break;
5639
    case XML_TOK_PARTIAL:
5640
      if (enc == encoding)
5641
        eventPtr = entityTextPtr;
5642
      result = XML_ERROR_INVALID_TOKEN;
5643
      goto endEntityValue;
5644
    case XML_TOK_INVALID:
5645
10
      if (enc == encoding)
5646
10
        eventPtr = next;
5647
      result = XML_ERROR_INVALID_TOKEN;
5648
10
      goto endEntityValue;
5649
    default:
5650
      /* This default case should be unnecessary -- all the tokens
5651
       * that XmlEntityValueTok() can return have their own explicit
5652
       * cases -- but should be retained for safety.  We do however
5653
       * exclude it from the coverage statistics.
5654
       *
5655
       * LCOV_EXCL_START
5656
       */
5657
      if (enc == encoding)
5658
        eventPtr = entityTextPtr;
5659
      result = XML_ERROR_UNEXPECTED_STATE;
5660
      goto endEntityValue;
5661
      /* LCOV_EXCL_STOP */
5662
    }
5663
1264
    entityTextPtr = next;
5664
5990
  }
5665
endEntityValue:
5666
#ifdef XML_DTD
5667
1204
  prologState.inEntityValue = oldInEntityValue;
5668
#endif /* XML_DTD */
5669
1204
  return result;
5670
1244
}
5671
5672
static void FASTCALL
5673
normalizeLines(XML_Char *s)
5674
{
5675
  XML_Char *p;
5676
46360
  for (;; s++) {
5677
45980
    if (*s == XML_T('\0'))
5678
380
      return;
5679
45600
    if (*s == 0xD)
5680
      break;
5681
  }
5682
  p = s;
5683
  do {
5684
    if (*s == 0xD) {
5685
      *p++ = 0xA;
5686
      if (*++s == 0xA)
5687
        s++;
5688
    }
5689
    else
5690
      *p++ = *s++;
5691
  } while (*s);
5692
  *p = XML_T('\0');
5693
380
}
5694
5695
static int
5696
reportProcessingInstruction(XML_Parser parser, const ENCODING *enc,
5697
                            const char *start, const char *end)
5698
{
5699
  const XML_Char *target;
5700
  XML_Char *data;
5701
  const char *tem;
5702
100540
  if (!processingInstructionHandler) {
5703
50010
    if (defaultHandler)
5704
10
      reportDefault(parser, enc, start, end);
5705
50010
    return 1;
5706
  }
5707
260
  start += enc->minBytesPerChar * 2;
5708
260
  tem = start + XmlNameLength(enc, start);
5709
260
  target = poolStoreString(&tempPool, enc, start, tem);
5710
260
  if (!target)
5711
40
    return 0;
5712
220
  poolFinish(&tempPool);
5713
220
  data = poolStoreString(&tempPool, enc,
5714
220
                        XmlSkipS(enc, tem),
5715
220
                        end - enc->minBytesPerChar*2);
5716
220
  if (!data)
5717
10
    return 0;
5718
210
  normalizeLines(data);
5719
210
  processingInstructionHandler(handlerArg, target, data);
5720
210
  poolClear(&tempPool);
5721
210
  return 1;
5722
50270
}
5723
5724
static int
5725
reportComment(XML_Parser parser, const ENCODING *enc,
5726
              const char *start, const char *end)
5727
{
5728
  XML_Char *data;
5729
500420
  if (!commentHandler) {
5730
250010
    if (defaultHandler)
5731
10
      reportDefault(parser, enc, start, end);
5732
250010
    return 1;
5733
  }
5734
400
  data = poolStoreString(&tempPool,
5735
                         enc,
5736
200
                         start + enc->minBytesPerChar * 4,
5737
200
                         end - enc->minBytesPerChar * 3);
5738
200
  if (!data)
5739
30
    return 0;
5740
170
  normalizeLines(data);
5741
170
  commentHandler(handlerArg, data);
5742
170
  poolClear(&tempPool);
5743
170
  return 1;
5744
250210
}
5745
5746
static void
5747
reportDefault(XML_Parser parser, const ENCODING *enc,
5748
              const char *s, const char *end)
5749
{
5750
3370
  if (MUST_CONVERT(enc, s)) {
5751
    enum XML_Convert_Result convert_res;
5752
    const char **eventPP;
5753
    const char **eventEndPP;
5754
380
    if (enc == encoding) {
5755
380
      eventPP = &eventPtr;
5756
380
      eventEndPP = &eventEndPtr;
5757
380
    }
5758
    else {
5759
      /* To get here, two things must be true; the parser must be
5760
       * using a character encoding that is not the same as the
5761
       * encoding passed in, and the encoding passed in must need
5762
       * conversion to the internal format (UTF-8 unless XML_UNICODE
5763
       * is defined).  The only occasions on which the encoding passed
5764
       * in is not the same as the parser's encoding are when it is
5765
       * the internal encoding (e.g. a previously defined parameter
5766
       * entity, already converted to internal format).  This by
5767
       * definition doesn't need conversion, so the whole branch never
5768
       * gets executed.
5769
       *
5770
       * For safety's sake we don't delete these lines and merely
5771
       * exclude them from coverage statistics.
5772
       *
5773
       * LCOV_EXCL_START
5774
       */
5775
      eventPP = &(openInternalEntities->internalEventPtr);
5776
      eventEndPP = &(openInternalEntities->internalEventEndPtr);
5777
      /* LCOV_EXCL_STOP */
5778
    }
5779
    do {
5780
380
      ICHAR *dataPtr = (ICHAR *)dataBuf;
5781
380
      convert_res = XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
5782
380
      *eventEndPP = s;
5783
380
      defaultHandler(handlerArg, dataBuf, (int)(dataPtr - (ICHAR *)dataBuf));
5784
380
      *eventPP = s;
5785
380
    } while ((convert_res != XML_CONVERT_COMPLETED) && (convert_res != XML_CONVERT_INPUT_INCOMPLETE));
5786
380
  }
5787
  else
5788
2990
    defaultHandler(handlerArg, (XML_Char *)s, (int)((XML_Char *)end - (XML_Char *)s));
5789
3370
}
5790
5791
5792
static int
5793
defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, XML_Bool isCdata,
5794
                XML_Bool isId, const XML_Char *value, XML_Parser parser)
5795
{
5796
  DEFAULT_ATTRIBUTE *att;
5797

5220
  if (value || isId) {
5798
    /* The handling of default attributes gets messed up if we have
5799
       a default which duplicates a non-default. */
5800
    int i;
5801
3180
    for (i = 0; i < type->nDefaultAtts; i++)
5802
140
      if (attId == type->defaultAtts[i].id)
5803
        return 1;
5804

3030
    if (isId && !type->idAtt && !attId->xmlns)
5805
790
      type->idAtt = attId;
5806
1450
  }
5807
1960
  if (type->nDefaultAtts == type->allocDefaultAtts) {
5808
1750
    if (type->allocDefaultAtts == 0) {
5809
1730
      type->allocDefaultAtts = 8;
5810
1730
      type->defaultAtts = (DEFAULT_ATTRIBUTE *)MALLOC(type->allocDefaultAtts
5811
                            * sizeof(DEFAULT_ATTRIBUTE));
5812
1730
      if (!type->defaultAtts)
5813
80
        return 0;
5814
    }
5815
    else {
5816
      DEFAULT_ATTRIBUTE *temp;
5817
20
      int count = type->allocDefaultAtts * 2;
5818
20
      temp = (DEFAULT_ATTRIBUTE *)
5819
20
        REALLOC(type->defaultAtts, (count * sizeof(DEFAULT_ATTRIBUTE)));
5820
20
      if (temp == NULL)
5821
10
        return 0;
5822
10
      type->allocDefaultAtts = count;
5823
10
      type->defaultAtts = temp;
5824
10
    }
5825
  }
5826
1870
  att = type->defaultAtts + type->nDefaultAtts;
5827
1870
  att->id = attId;
5828
1870
  att->value = value;
5829
1870
  att->isCdata = isCdata;
5830
1870
  if (!isCdata)
5831
1090
    attId->maybeTokenized = XML_TRUE;
5832
1870
  type->nDefaultAtts += 1;
5833
1870
  return 1;
5834
1960
}
5835
5836
static int
5837
setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *elementType)
5838
{
5839
26120
  DTD * const dtd = _dtd;  /* save one level of indirection */
5840
  const XML_Char *name;
5841
1151720
  for (name = elementType->name; *name; name++) {
5842
562950
    if (*name == XML_T(ASCII_COLON)) {
5843
      PREFIX *prefix;
5844
      const XML_Char *s;
5845
1053740
      for (s = elementType->name; s != name; s++) {
5846

1050790
        if (!poolAppendChar(&dtd->pool, *s))
5847
50
          return 0;
5848
      }
5849

3830
      if (!poolAppendChar(&dtd->pool, XML_T('\0')))
5850
20
        return 0;
5851
1730
      prefix = (PREFIX *)lookup(parser, &dtd->prefixes, poolStart(&dtd->pool),
5852
                                sizeof(PREFIX));
5853
1730
      if (!prefix)
5854
80
        return 0;
5855
1650
      if (prefix->name == poolStart(&dtd->pool))
5856
1530
        poolFinish(&dtd->pool);
5857
      else
5858
120
        poolDiscard(&dtd->pool);
5859
1650
      elementType->prefix = prefix;
5860
5861
1650
    }
5862
  }
5863
12910
  return 1;
5864
13060
}
5865
5866
static ATTRIBUTE_ID *
5867
getAttributeId(XML_Parser parser, const ENCODING *enc,
5868
               const char *start, const char *end)
5869
{
5870
9513400
  DTD * const dtd = _dtd;  /* save one level of indirection */
5871
  ATTRIBUTE_ID *id;
5872
  const XML_Char *name;
5873

9513440
  if (!poolAppendChar(&dtd->pool, XML_T('\0')))
5874
10
    return NULL;
5875
4756690
  name = poolStoreString(&dtd->pool, enc, start, end);
5876
4756690
  if (!name)
5877
100
    return NULL;
5878
  /* skip quotation mark - its storage will be re-used (like in name[-1]) */
5879
4756590
  ++name;
5880
4756590
  id = (ATTRIBUTE_ID *)lookup(parser, &dtd->attributeIds, name, sizeof(ATTRIBUTE_ID));
5881
4756590
  if (!id)
5882
540
    return NULL;
5883
4756050
  if (id->name != name)
5884
4300880
    poolDiscard(&dtd->pool);
5885
  else {
5886
455170
    poolFinish(&dtd->pool);
5887
455170
    if (!ns)
5888
      ;
5889
5200
    else if (name[0] == XML_T(ASCII_x)
5890
5800
        && name[1] == XML_T(ASCII_m)
5891
4220
        && name[2] == XML_T(ASCII_l)
5892
4220
        && name[3] == XML_T(ASCII_n)
5893
4220
        && name[4] == XML_T(ASCII_s)
5894

5730
        && (name[5] == XML_T('\0') || name[5] == XML_T(ASCII_COLON))) {
5895
2110
      if (name[5] == XML_T('\0'))
5896
600
        id->prefix = &dtd->defaultPrefix;
5897
      else
5898
1510
        id->prefix = (PREFIX *)lookup(parser, &dtd->prefixes, name + 6, sizeof(PREFIX));
5899
2110
      id->xmlns = XML_TRUE;
5900
2110
    }
5901
    else {
5902
      int i;
5903
277000
      for (i = 0; name[i]; i++) {
5904
        /* attributes without prefix are *not* in the default namespace */
5905
137450
        if (name[i] == XML_T(ASCII_COLON)) {
5906
          int j;
5907
268980
          for (j = 0; j < i; j++) {
5908

268050
            if (!poolAppendChar(&dtd->pool, name[j]))
5909
20
              return NULL;
5910
          }
5911

1110
          if (!poolAppendChar(&dtd->pool, XML_T('\0')))
5912
10
            return NULL;
5913
500
          id->prefix = (PREFIX *)lookup(parser, &dtd->prefixes, poolStart(&dtd->pool),
5914
                                        sizeof(PREFIX));
5915
500
          if (!id->prefix)
5916
40
            return NULL;
5917
460
          if (id->prefix->name == poolStart(&dtd->pool))
5918
320
            poolFinish(&dtd->pool);
5919
          else
5920
140
            poolDiscard(&dtd->pool);
5921
460
          break;
5922
        }
5923
      }
5924
1510
    }
5925
  }
5926
4755980
  return id;
5927
4756700
}
5928
5929
#define CONTEXT_SEP XML_T(ASCII_FF)
5930
5931
static const XML_Char *
5932
getContext(XML_Parser parser)
5933
{
5934
4788
  DTD * const dtd = _dtd;  /* save one level of indirection */
5935
2394
  HASH_TABLE_ITER iter;
5936
  XML_Bool needSep = XML_FALSE;
5937
5938
2394
  if (dtd->defaultPrefix.binding) {
5939
    int i;
5940
    int len;
5941

1320
    if (!poolAppendChar(&tempPool, XML_T(ASCII_EQUALS)))
5942
      return NULL;
5943
440
    len = dtd->defaultPrefix.binding->uriLen;
5944
440
    if (namespaceSeparator)
5945
440
      len--;
5946
796940
    for (i = 0; i < len; i++) {
5947

796060
      if (!poolAppendChar(&tempPool, dtd->defaultPrefix.binding->uri[i])) {
5948
        /* Because of memory caching, I don't believe this line can be
5949
         * executed.
5950
         *
5951
         * This is part of a loop copying the default prefix binding
5952
         * URI into the parser's temporary string pool.  Previously,
5953
         * that URI was copied into the same string pool, with a
5954
         * terminating NUL character, as part of setContext().  When
5955
         * the pool was cleared, that leaves a block definitely big
5956
         * enough to hold the URI on the free block list of the pool.
5957
         * The URI copy in getContext() therefore cannot run out of
5958
         * memory.
5959
         *
5960
         * If the pool is used between the setContext() and
5961
         * getContext() calls, the worst it can do is leave a bigger
5962
         * block on the front of the free list.  Given that this is
5963
         * all somewhat inobvious and program logic can be changed, we
5964
         * don't delete the line but we do exclude it from the test
5965
         * coverage statistics.
5966
         */
5967
        return NULL; /* LCOV_EXCL_LINE */
5968
      }
5969
    }
5970
    needSep = XML_TRUE;
5971
440
  }
5972
5973
2394
  hashTableIterInit(&iter, &(dtd->prefixes));
5974
2394
  for (;;) {
5975
    int i;
5976
    int len;
5977
    const XML_Char *s;
5978
3827
    PREFIX *prefix = (PREFIX *)hashTableIterNext(&iter);
5979
3827
    if (!prefix)
5980
2344
      break;
5981
1483
    if (!prefix->binding) {
5982
      /* This test appears to be (justifiable) paranoia.  There does
5983
       * not seem to be a way of injecting a prefix without a binding
5984
       * that doesn't get errored long before this function is called.
5985
       * The test should remain for safety's sake, so we instead
5986
       * exclude the following line from the coverage statistics.
5987
       */
5988
      continue; /* LCOV_EXCL_LINE */
5989
    }
5990

3249
    if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
5991
10
      return NULL;
5992
133998
    for (s = prefix->name; *s; s++)
5993

131835
      if (!poolAppendChar(&tempPool, *s))
5994
13
        return NULL;
5995

2963
    if (!poolAppendChar(&tempPool, XML_T(ASCII_EQUALS)))
5996
17
      return NULL;
5997
1443
    len = prefix->binding->uriLen;
5998
1443
    if (namespaceSeparator)
5999
1443
      len--;
6000
94782
    for (i = 0; i < len; i++)
6001

91926
      if (!poolAppendChar(&tempPool, prefix->binding->uri[i]))
6002
10
        return NULL;
6003
    needSep = XML_TRUE;
6004

1433
  }
6005
6006
6007
2344
  hashTableIterInit(&iter, &(dtd->generalEntities));
6008
2344
  for (;;) {
6009
    const XML_Char *s;
6010
4913
    ENTITY *e = (ENTITY *)hashTableIterNext(&iter);
6011
4913
    if (!e)
6012
2274
      break;
6013
2639
    if (!e->open)
6014
295
      continue;
6015

4594
    if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
6016
10
      return NULL;
6017
93784
    for (s = e->name; *s; s++)
6018

90440
      if (!poolAppendChar(&tempPool, *s))
6019
60
        return 0;
6020
    needSep = XML_TRUE;
6021

2274
  }
6022
6023

4558
  if (!poolAppendChar(&tempPool, XML_T('\0')))
6024
10
    return NULL;
6025
2264
  return tempPool.start;
6026
2394
}
6027
6028
static XML_Bool
6029
setContext(XML_Parser parser, const XML_Char *context)
6030
{
6031
11340
  DTD * const dtd = _dtd;  /* save one level of indirection */
6032
  const XML_Char *s = context;
6033
6034
73721
  while (*context != XML_T('\0')) {
6035

126744
    if (*s == CONTEXT_SEP || *s == XML_T('\0')) {
6036
      ENTITY *e;
6037

1368
      if (!poolAppendChar(&tempPool, XML_T('\0')))
6038
10
        return XML_FALSE;
6039
669
      e = (ENTITY *)lookup(parser, &dtd->generalEntities, poolStart(&tempPool), 0);
6040
669
      if (e)
6041
669
        e->open = XML_TRUE;
6042
669
      if (*s != XML_T('\0'))
6043
        s++;
6044
      context = s;
6045
669
      poolDiscard(&tempPool);
6046
669
    }
6047
62693
    else if (*s == XML_T(ASCII_EQUALS)) {
6048
      PREFIX *prefix;
6049
5191
      if (poolLength(&tempPool) == 0)
6050
190
        prefix = &dtd->defaultPrefix;
6051
      else {
6052

10012
        if (!poolAppendChar(&tempPool, XML_T('\0')))
6053
10
          return XML_FALSE;
6054
4991
        prefix = (PREFIX *)lookup(parser, &dtd->prefixes, poolStart(&tempPool),
6055
                                  sizeof(PREFIX));
6056
4991
        if (!prefix)
6057
240
          return XML_FALSE;
6058
4751
        if (prefix->name == poolStart(&tempPool)) {
6059
4370
          prefix->name = poolCopyString(&dtd->pool, prefix->name);
6060
4370
          if (!prefix->name)
6061
120
            return XML_FALSE;
6062
        }
6063
4631
        poolDiscard(&tempPool);
6064
      }
6065
646816
      for (context = s + 1;
6066
646255
           *context != CONTEXT_SEP && *context != XML_T('\0');
6067
318587
           context++)
6068

637374
        if (!poolAppendChar(&tempPool, *context))
6069
10
          return XML_FALSE;
6070

9632
      if (!poolAppendChar(&tempPool, XML_T('\0')))
6071
10
        return XML_FALSE;
6072
14403
      if (addBinding(parser, prefix, NULL, poolStart(&tempPool),
6073
9602
                     &inheritedBindings) != XML_ERROR_NONE)
6074
360
        return XML_FALSE;
6075
4441
      poolDiscard(&tempPool);
6076
4441
      if (*context != XML_T('\0'))
6077
431
        ++context;
6078
      s = context;
6079
4441
    }
6080
    else {
6081

120253
      if (!poolAppendChar(&tempPool, *s))
6082
231
        return XML_FALSE;
6083
57271
      s++;
6084
    }
6085
  }
6086
4679
  return XML_TRUE;
6087
5670
}
6088
6089
static void FASTCALL
6090
normalizePublicId(XML_Char *publicId)
6091
{
6092
  XML_Char *p = publicId;
6093
  XML_Char *s;
6094
4277240
  for (s = publicId; *s; s++) {
6095

2062480
    switch (*s) {
6096
    case 0x20:
6097
    case 0xD:
6098
    case 0xA:
6099

300000
      if (p != publicId && p[-1] != 0x20)
6100
150000
        *p++ = 0x20;
6101
      break;
6102
    default:
6103
1912480
      *p++ = *s;
6104
1912480
    }
6105
  }
6106

101520
  if (p != publicId && p[-1] == 0x20)
6107
    --p;
6108
50760
  *p = XML_T('\0');
6109
50760
}
6110
6111
static DTD *
6112
dtdCreate(const XML_Memory_Handling_Suite *ms)
6113
{
6114
28686
  DTD *p = (DTD *)ms->malloc_fcn(sizeof(DTD));
6115
14343
  if (p == NULL)
6116
130
    return p;
6117
14213
  poolInit(&(p->pool), ms);
6118
14213
  poolInit(&(p->entityValuePool), ms);
6119
14213
  hashTableInit(&(p->generalEntities), ms);
6120
14213
  hashTableInit(&(p->elementTypes), ms);
6121
14213
  hashTableInit(&(p->attributeIds), ms);
6122
14213
  hashTableInit(&(p->prefixes), ms);
6123
#ifdef XML_DTD
6124
14213
  p->paramEntityRead = XML_FALSE;
6125
14213
  hashTableInit(&(p->paramEntities), ms);
6126
#endif /* XML_DTD */
6127
14213
  p->defaultPrefix.name = NULL;
6128
14213
  p->defaultPrefix.binding = NULL;
6129
6130
14213
  p->in_eldecl = XML_FALSE;
6131
14213
  p->scaffIndex = NULL;
6132
14213
  p->scaffold = NULL;
6133
14213
  p->scaffLevel = 0;
6134
14213
  p->scaffSize = 0;
6135
14213
  p->scaffCount = 0;
6136
14213
  p->contentStringLen = 0;
6137
6138
14213
  p->keepProcessing = XML_TRUE;
6139
14213
  p->hasParamEntityRefs = XML_FALSE;
6140
14213
  p->standalone = XML_FALSE;
6141
14213
  return p;
6142
14343
}
6143
6144
static void
6145
dtdReset(DTD *p, const XML_Memory_Handling_Suite *ms)
6146
{
6147
104600
  HASH_TABLE_ITER iter;
6148
52300
  hashTableIterInit(&iter, &(p->elementTypes));
6149
52300
  for (;;) {
6150
2104550
    ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
6151
2104550
    if (!e)
6152
52300
      break;
6153
2052250
    if (e->allocDefaultAtts != 0)
6154
60
      ms->free_fcn(e->defaultAtts);
6155
2052250
  }
6156
52300
  hashTableClear(&(p->generalEntities));
6157
#ifdef XML_DTD
6158
52300
  p->paramEntityRead = XML_FALSE;
6159
52300
  hashTableClear(&(p->paramEntities));
6160
#endif /* XML_DTD */
6161
52300
  hashTableClear(&(p->elementTypes));
6162
52300
  hashTableClear(&(p->attributeIds));
6163
52300
  hashTableClear(&(p->prefixes));
6164
52300
  poolClear(&(p->pool));
6165
52300
  poolClear(&(p->entityValuePool));
6166
52300
  p->defaultPrefix.name = NULL;
6167
52300
  p->defaultPrefix.binding = NULL;
6168
6169
52300
  p->in_eldecl = XML_FALSE;
6170
6171
52300
  ms->free_fcn(p->scaffIndex);
6172
52300
  p->scaffIndex = NULL;
6173
52300
  ms->free_fcn(p->scaffold);
6174
52300
  p->scaffold = NULL;
6175
6176
52300
  p->scaffLevel = 0;
6177
52300
  p->scaffSize = 0;
6178
52300
  p->scaffCount = 0;
6179
52300
  p->contentStringLen = 0;
6180
6181
52300
  p->keepProcessing = XML_TRUE;
6182
52300
  p->hasParamEntityRefs = XML_FALSE;
6183
52300
  p->standalone = XML_FALSE;
6184
52300
}
6185
6186
static void
6187
dtdDestroy(DTD *p, XML_Bool isDocEntity, const XML_Memory_Handling_Suite *ms)
6188
{
6189
28426
  HASH_TABLE_ITER iter;
6190
14213
  hashTableIterInit(&iter, &(p->elementTypes));
6191
14213
  for (;;) {
6192
31842
    ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
6193
31842
    if (!e)
6194
14213
      break;
6195
17629
    if (e->allocDefaultAtts != 0)
6196
2146
      ms->free_fcn(e->defaultAtts);
6197
17629
  }
6198
14213
  hashTableDestroy(&(p->generalEntities));
6199
#ifdef XML_DTD
6200
14213
  hashTableDestroy(&(p->paramEntities));
6201
#endif /* XML_DTD */
6202
14213
  hashTableDestroy(&(p->elementTypes));
6203
14213
  hashTableDestroy(&(p->attributeIds));
6204
14213
  hashTableDestroy(&(p->prefixes));
6205
14213
  poolDestroy(&(p->pool));
6206
14213
  poolDestroy(&(p->entityValuePool));
6207
14213
  if (isDocEntity) {
6208
12239
    ms->free_fcn(p->scaffIndex);
6209
12239
    ms->free_fcn(p->scaffold);
6210
12239
  }
6211
14213
  ms->free_fcn(p);
6212
14213
}
6213
6214
/* Do a deep copy of the DTD. Return 0 for out of memory, non-zero otherwise.
6215
   The new DTD has already been initialized.
6216
*/
6217
static int
6218
dtdCopy(XML_Parser oldParser, DTD *newDtd, const DTD *oldDtd, const XML_Memory_Handling_Suite *ms)
6219
{
6220
3948
  HASH_TABLE_ITER iter;
6221
6222
  /* Copy the prefix table. */
6223
6224
1974
  hashTableIterInit(&iter, &(oldDtd->prefixes));
6225
1974
  for (;;) {
6226
    const XML_Char *name;
6227
2997
    const PREFIX *oldP = (PREFIX *)hashTableIterNext(&iter);
6228
2997
    if (!oldP)
6229
1834
      break;
6230
1163
    name = poolCopyString(&(newDtd->pool), oldP->name);
6231
1163
    if (!name)
6232
50
      return 0;
6233
1113
    if (!lookup(oldParser, &(newDtd->prefixes), name, sizeof(PREFIX)))
6234
90
      return 0;
6235
1023
  }
6236
6237
1834
  hashTableIterInit(&iter, &(oldDtd->attributeIds));
6238
6239
  /* Copy the attribute id table. */
6240
6241
1834
  for (;;) {
6242
    ATTRIBUTE_ID *newA;
6243
    const XML_Char *name;
6244
2924
    const ATTRIBUTE_ID *oldA = (ATTRIBUTE_ID *)hashTableIterNext(&iter);
6245
6246
2924
    if (!oldA)
6247
1684
      break;
6248
    /* Remember to allocate the scratch byte before the name. */
6249

2720
    if (!poolAppendChar(&(newDtd->pool), XML_T('\0')))
6250
20
      return 0;
6251
1220
    name = poolCopyString(&(newDtd->pool), oldA->name);
6252
1220
    if (!name)
6253
10
      return 0;
6254
1210
    ++name;
6255
1210
    newA = (ATTRIBUTE_ID *)lookup(oldParser, &(newDtd->attributeIds), name,
6256
                                  sizeof(ATTRIBUTE_ID));
6257
1210
    if (!newA)
6258
120
      return 0;
6259
1090
    newA->maybeTokenized = oldA->maybeTokenized;
6260
1090
    if (oldA->prefix) {
6261
460
      newA->xmlns = oldA->xmlns;
6262
460
      if (oldA->prefix == &oldDtd->defaultPrefix)
6263
273
        newA->prefix = &newDtd->defaultPrefix;
6264
      else
6265
374
        newA->prefix = (PREFIX *)lookup(oldParser, &(newDtd->prefixes),
6266
187
                                        oldA->prefix->name, 0);
6267
460
    }
6268
1090
  }
6269
6270
  /* Copy the element type table. */
6271
6272
1684
  hashTableIterInit(&iter, &(oldDtd->elementTypes));
6273
6274
1684
  for (;;) {
6275
    int i;
6276
    ELEMENT_TYPE *newE;
6277
    const XML_Char *name;
6278
3105
    const ELEMENT_TYPE *oldE = (ELEMENT_TYPE *)hashTableIterNext(&iter);
6279
3105
    if (!oldE)
6280
1334
      break;
6281
1771
    name = poolCopyString(&(newDtd->pool), oldE->name);
6282
1771
    if (!name)
6283
60
      return 0;
6284
1711
    newE = (ELEMENT_TYPE *)lookup(oldParser, &(newDtd->elementTypes), name,
6285
                                  sizeof(ELEMENT_TYPE));
6286
1711
    if (!newE)
6287
226
      return 0;
6288
1485
    if (oldE->nDefaultAtts) {
6289
527
      newE->defaultAtts = (DEFAULT_ATTRIBUTE *)
6290
527
          ms->malloc_fcn(oldE->nDefaultAtts * sizeof(DEFAULT_ATTRIBUTE));
6291
527
      if (!newE->defaultAtts) {
6292
51
        return 0;
6293
      }
6294
    }
6295
1434
    if (oldE->idAtt)
6296
240
      newE->idAtt = (ATTRIBUTE_ID *)
6297
240
          lookup(oldParser, &(newDtd->attributeIds), oldE->idAtt->name, 0);
6298
1434
    newE->allocDefaultAtts = newE->nDefaultAtts = oldE->nDefaultAtts;
6299
1434
    if (oldE->prefix)
6300
280
      newE->prefix = (PREFIX *)lookup(oldParser, &(newDtd->prefixes),
6301
140
                                      oldE->prefix->name, 0);
6302
3794
    for (i = 0; i < newE->nDefaultAtts; i++) {
6303
476
      newE->defaultAtts[i].id = (ATTRIBUTE_ID *)
6304
476
          lookup(oldParser, &(newDtd->attributeIds), oldE->defaultAtts[i].id->name, 0);
6305
476
      newE->defaultAtts[i].isCdata = oldE->defaultAtts[i].isCdata;
6306
476
      if (oldE->defaultAtts[i].value) {
6307
86
        newE->defaultAtts[i].value
6308
172
            = poolCopyString(&(newDtd->pool), oldE->defaultAtts[i].value);
6309
86
        if (!newE->defaultAtts[i].value)
6310
13
          return 0;
6311
      }
6312
      else
6313
390
        newE->defaultAtts[i].value = NULL;
6314
    }
6315
1421
  }
6316
6317
  /* Copy the entity tables. */
6318
1334
  if (!copyEntityTable(oldParser,
6319
1334
                       &(newDtd->generalEntities),
6320
1334
                       &(newDtd->pool),
6321
1334
                       &(oldDtd->generalEntities)))
6322
304
      return 0;
6323
6324
#ifdef XML_DTD
6325
1030
  if (!copyEntityTable(oldParser,
6326
1030
                       &(newDtd->paramEntities),
6327
                       &(newDtd->pool),
6328
1030
                       &(oldDtd->paramEntities)))
6329
100
      return 0;
6330
930
  newDtd->paramEntityRead = oldDtd->paramEntityRead;
6331
#endif /* XML_DTD */
6332
6333
930
  newDtd->keepProcessing = oldDtd->keepProcessing;
6334
930
  newDtd->hasParamEntityRefs = oldDtd->hasParamEntityRefs;
6335
930
  newDtd->standalone = oldDtd->standalone;
6336
6337
  /* Don't want deep copying for scaffolding */
6338
930
  newDtd->in_eldecl = oldDtd->in_eldecl;
6339
930
  newDtd->scaffold = oldDtd->scaffold;
6340
930
  newDtd->contentStringLen = oldDtd->contentStringLen;
6341
930
  newDtd->scaffSize = oldDtd->scaffSize;
6342
930
  newDtd->scaffLevel = oldDtd->scaffLevel;
6343
930
  newDtd->scaffIndex = oldDtd->scaffIndex;
6344
6345
930
  return 1;
6346
1974
}  /* End dtdCopy */
6347
6348
static int
6349
copyEntityTable(XML_Parser oldParser,
6350
                HASH_TABLE *newTable,
6351
                STRING_POOL *newPool,
6352
                const HASH_TABLE *oldTable)
6353
{
6354
4728
  HASH_TABLE_ITER iter;
6355
  const XML_Char *cachedOldBase = NULL;
6356
  const XML_Char *cachedNewBase = NULL;
6357
6358
2364
  hashTableIterInit(&iter, oldTable);
6359
6360
2364
  for (;;) {
6361
    ENTITY *newE;
6362
    const XML_Char *name;
6363
3878
    const ENTITY *oldE = (ENTITY *)hashTableIterNext(&iter);
6364
3878
    if (!oldE)
6365
1960
      break;
6366
1918
    name = poolCopyString(newPool, oldE->name);
6367
1918
    if (!name)
6368
12
      return 0;
6369
1906
    newE = (ENTITY *)lookup(oldParser, newTable, name, sizeof(ENTITY));
6370
1906
    if (!newE)
6371
336
      return 0;
6372
1570
    if (oldE->systemId) {
6373
1520
      const XML_Char *tem = poolCopyString(newPool, oldE->systemId);
6374
1520
      if (!tem)
6375
10
        return 0;
6376
1510
      newE->systemId = tem;
6377
1510
      if (oldE->base) {
6378
40
        if (oldE->base == cachedOldBase)
6379
          newE->base = cachedNewBase;
6380
        else {
6381
          cachedOldBase = oldE->base;
6382
40
          tem = poolCopyString(newPool, cachedOldBase);
6383
40
          if (!tem)
6384
10
            return 0;
6385
30
          cachedNewBase = newE->base = tem;
6386
        }
6387
      }
6388
1500
      if (oldE->publicId) {
6389
40
        tem = poolCopyString(newPool, oldE->publicId);
6390
40
        if (!tem)
6391
10
          return 0;
6392
30
        newE->publicId = tem;
6393
30
      }
6394
1490
    }
6395
    else {
6396
100
      const XML_Char *tem = poolCopyStringN(newPool, oldE->textPtr,
6397
50
                                            oldE->textLen);
6398
50
      if (!tem)
6399
14
        return 0;
6400
36
      newE->textPtr = tem;
6401
36
      newE->textLen = oldE->textLen;
6402
36
    }
6403
1526
    if (oldE->notation) {
6404
44
      const XML_Char *tem = poolCopyString(newPool, oldE->notation);
6405
44
      if (!tem)
6406
12
        return 0;
6407
32
      newE->notation = tem;
6408
32
    }
6409
1514
    newE->is_param = oldE->is_param;
6410
1514
    newE->is_internal = oldE->is_internal;
6411
1514
  }
6412
1960
  return 1;
6413
2364
}
6414
6415
#define INIT_POWER 6
6416
6417
static XML_Bool FASTCALL
6418
keyeq(KEY s1, KEY s2)
6419
{
6420
290154693
  for (; *s1 == *s2; s1++, s2++)
6421
130360300
    if (*s1 == 0)
6422
18515617
      return XML_TRUE;
6423
3639492
  return XML_FALSE;
6424
22155109
}
6425
6426
static size_t
6427
keylen(KEY s)
6428
{
6429
  size_t len = 0;
6430
366068346
  for (; *s; s++, len++);
6431
23167924
  return len;
6432
}
6433
6434
static void
6435
copy_salt_to_sipkey(XML_Parser parser, struct sipkey * key)
6436
{
6437
46335928
  key->k[0] = 0;
6438
23167964
  key->k[1] = get_hash_secret_salt(parser);
6439
23167964
}
6440
6441
static unsigned long FASTCALL
6442
hash(XML_Parser parser, KEY s)
6443
{
6444
46335328
  struct siphash state;
6445
23167664
  struct sipkey key;
6446
  (void)sip_tobin;
6447
  (void)sip24_valid;
6448
23167664
  copy_salt_to_sipkey(parser, &key);
6449
23167664
  sip24_init(&state, &key);
6450
23167664
  sip24_update(&state, s, keylen(s) * sizeof(XML_Char));
6451
46335328
  return (unsigned long)sip24_final(&state);
6452
23167664
}
6453
6454
static NAMED *
6455
lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize)
6456
{
6457
  size_t i;
6458
46343266
  if (table->size == 0) {
6459
    size_t tsize;
6460
34423
    if (!createSize)
6461
5609
      return NULL;
6462
28814
    table->power = INIT_POWER;
6463
    /* table->size is a power of 2 */
6464
28814
    table->size = (size_t)1 << INIT_POWER;
6465
    tsize = table->size * sizeof(NAMED *);
6466
28814
    table->v = (NAMED **)table->mem->malloc_fcn(tsize);
6467
28814
    if (!table->v) {
6468
1400
      table->size = 0;
6469
1400
      return NULL;
6470
    }
6471
27414
    memset(table->v, 0, tsize);
6472
27414
    i = hash(parser, name) & ((unsigned long)table->size - 1);
6473
27414
  }
6474
  else {
6475
23137210
    unsigned long h = hash(parser, name);
6476
23137210
    unsigned long mask = (unsigned long)table->size - 1;
6477
    unsigned char step = 0;
6478
23137210
    i = h & mask;
6479
49913912
    while (table->v[i]) {
6480
22155109
      if (keyeq(name, table->v[i]->name))
6481
18515617
        return table->v[i];
6482
3639492
      if (!step)
6483
2968599
        step = PROBE_STEP(h, mask, table->power);
6484
7278984
      i < step ? (i += table->size - step) : (i -= step);
6485
    }
6486
4621593
    if (!createSize)
6487
2053385
      return NULL;
6488
6489
    /* check for overflow (table is half full) */
6490
2568208
    if (table->used >> (table->power - 1)) {
6491
105
      unsigned char newPower = table->power + 1;
6492
105
      size_t newSize = (size_t)1 << newPower;
6493
105
      unsigned long newMask = (unsigned long)newSize - 1;
6494
105
      size_t tsize = newSize * sizeof(NAMED *);
6495
105
      NAMED **newV = (NAMED **)table->mem->malloc_fcn(tsize);
6496
105
      if (!newV)
6497
10
        return NULL;
6498
95
      memset(newV, 0, tsize);
6499
12350
      for (i = 0; i < table->size; i++)
6500
6080
        if (table->v[i]) {
6501
3040
          unsigned long newHash = hash(parser, table->v[i]->name);
6502
3040
          size_t j = newHash & newMask;
6503
          step = 0;
6504
6520
          while (newV[j]) {
6505
440
            if (!step)
6506
344
              step = PROBE_STEP(newHash, newMask, newPower);
6507
880
            j < step ? (j += newSize - step) : (j -= step);
6508
          }
6509
3040
          newV[j] = table->v[i];
6510
3040
        }
6511
95
      table->mem->free_fcn(table->v);
6512
95
      table->v = newV;
6513
95
      table->power = newPower;
6514
95
      table->size = newSize;
6515
95
      i = h & newMask;
6516
      step = 0;
6517
211
      while (table->v[i]) {
6518
21
        if (!step)
6519
17
          step = PROBE_STEP(h, newMask, newPower);
6520
42
        i < step ? (i += newSize - step) : (i -= step);
6521
      }
6522
95
    }
6523
2568198
  }
6524
2595612
  table->v[i] = (NAMED *)table->mem->malloc_fcn(createSize);
6525
2595612
  if (!table->v[i])
6526
2172
    return NULL;
6527
2593440
  memset(table->v[i], 0, createSize);
6528
2593440
  table->v[i]->name = name;
6529
2593440
  (table->used)++;
6530
2593440
  return table->v[i];
6531
23171633
}
6532
6533
static void FASTCALL
6534
hashTableClear(HASH_TABLE *table)
6535
{
6536
  size_t i;
6537
26745460
  for (i = 0; i < table->size; i++) {
6538
12980480
    table->mem->free_fcn(table->v[i]);
6539
12980480
    table->v[i] = NULL;
6540
  }
6541
261500
  table->used = 0;
6542
261500
}
6543
6544
static void FASTCALL
6545
hashTableDestroy(HASH_TABLE *table)
6546
{
6547
  size_t i;
6548
3734347
  for (i = 0; i < table->size; i++)
6549
1760576
    table->mem->free_fcn(table->v[i]);
6550
71065
  table->mem->free_fcn(table->v);
6551
71065
}
6552
6553
static void FASTCALL
6554
hashTableInit(HASH_TABLE *p, const XML_Memory_Handling_Suite *ms)
6555
{
6556
142130
  p->power = 0;
6557
71065
  p->size = 0;
6558
71065
  p->used = 0;
6559
71065
  p->v = NULL;
6560
71065
  p->mem = ms;
6561
71065
}
6562
6563
static void FASTCALL
6564
hashTableIterInit(HASH_TABLE_ITER *iter, const HASH_TABLE *table)
6565
{
6566
158214
  iter->p = table->v;
6567
79107
  iter->end = iter->p + table->size;
6568
79107
}
6569
6570
static NAMED * FASTCALL
6571
hashTableIterNext(HASH_TABLE_ITER *iter)
6572
{
6573
12053004
  while (iter->p != iter->end) {
6574
7658989
    NAMED *tem = *(iter->p)++;
6575
7658989
    if (tem)
6576
2080093
      return tem;
6577
5578896
  }
6578
77943
  return NULL;
6579
2158036
}
6580
6581
static void FASTCALL
6582
poolInit(STRING_POOL *pool, const XML_Memory_Handling_Suite *ms)
6583
{
6584
125264
  pool->blocks = NULL;
6585
62632
  pool->freeBlocks = NULL;
6586
62632
  pool->start = NULL;
6587
62632
  pool->ptr = NULL;
6588
62632
  pool->end = NULL;
6589
62632
  pool->mem = ms;
6590
62632
}
6591
6592
static void FASTCALL
6593
poolClear(STRING_POOL *pool)
6594
{
6595
32937908
  if (!pool->freeBlocks)
6596
3718494
    pool->freeBlocks = pool->blocks;
6597
  else {
6598
12750460
    BLOCK *p = pool->blocks;
6599
25500980
    while (p) {
6600
30
      BLOCK *tem = p->next;
6601
30
      p->next = pool->freeBlocks;
6602
30
      pool->freeBlocks = p;
6603
      p = tem;
6604
    }
6605
  }
6606
16468954
  pool->blocks = NULL;
6607
16468954
  pool->start = NULL;
6608
16468954
  pool->ptr = NULL;
6609
16468954
  pool->end = NULL;
6610
16468954
}
6611
6612
static void FASTCALL
6613
poolDestroy(STRING_POOL *pool)
6614
{
6615
125264
  BLOCK *p = pool->blocks;
6616
171722
  while (p) {
6617
23229
    BLOCK *tem = p->next;
6618
23229
    pool->mem->free_fcn(p);
6619
    p = tem;
6620
  }
6621
62632
  p = pool->freeBlocks;
6622
131584
  while (p) {
6623
3160
    BLOCK *tem = p->next;
6624
3160
    pool->mem->free_fcn(p);
6625
    p = tem;
6626
  }
6627
62632
}
6628
6629
static XML_Char *
6630
poolAppend(STRING_POOL *pool, const ENCODING *enc,
6631
           const char *ptr, const char *end)
6632
{
6633

13260514
  if (!pool->ptr && !poolGrow(pool))
6634
450
    return NULL;
6635
  for (;;) {
6636
9654920
    const enum XML_Convert_Result convert_res = XmlConvert(enc, &ptr, end, (ICHAR **)&(pool->ptr), (ICHAR *)pool->end);
6637
9654920
    if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE))
6638
9651120
      break;
6639
3800
    if (!poolGrow(pool))
6640
290
      return NULL;
6641

3510
  }
6642
9651120
  return pool->start;
6643
9651860
}
6644
6645
static const XML_Char * FASTCALL
6646
poolCopyString(STRING_POOL *pool, const XML_Char *s)
6647
{
6648
4142292
  do {
6649

39480491
    if (!poolAppendChar(pool, *s))
6650
547
      return NULL;
6651
19733955
  } while (*s++);
6652
2070599
  s = pool->start;
6653
2070599
  poolFinish(pool);
6654
2070599
  return s;
6655
2071146
}
6656
6657
static const XML_Char *
6658
poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n)
6659
{
6660

100
  if (!pool->ptr && !poolGrow(pool)) {
6661
    /* The following line is unreachable given the current usage of
6662
     * poolCopyStringN().  Currently it is called from exactly one
6663
     * place to copy the text of a simple general entity.  By that
6664
     * point, the name of the entity is already stored in the pool, so
6665
     * pool->ptr cannot be NULL.
6666
     *
6667
     * If poolCopyStringN() is used elsewhere as it well might be,
6668
     * this line may well become executable again.  Regardless, this
6669
     * sort of check shouldn't be removed lightly, so we just exclude
6670
     * it from the coverage statistics.
6671
     */
6672
    return NULL; /* LCOV_EXCL_LINE */
6673
  }
6674
102142
  for (; n > 0; --n, s++) {
6675

102156
    if (!poolAppendChar(pool, *s))
6676
14
      return NULL;
6677
  }
6678
36
  s = pool->start;
6679
36
  poolFinish(pool);
6680
36
  return s;
6681
50
}
6682
6683
static const XML_Char * FASTCALL
6684
poolAppendString(STRING_POOL *pool, const XML_Char *s)
6685
{
6686
6200
  while (*s) {
6687

2840
    if (!poolAppendChar(pool, *s))
6688
10
      return NULL;
6689
1330
    s++;
6690
  }
6691
1170
  return pool->start;
6692
1180
}
6693
6694
static XML_Char *
6695
poolStoreString(STRING_POOL *pool, const ENCODING *enc,
6696
                const char *ptr, const char *end)
6697
{
6698
19294952
  if (!poolAppend(pool, enc, ptr, end))
6699
700
    return NULL;
6700

9648266
  if (pool->ptr == pool->end && !poolGrow(pool))
6701
50
    return NULL;
6702
9646726
  *(pool->ptr)++ = 0;
6703
9646726
  return pool->start;
6704
9647476
}
6705
6706
static size_t
6707
poolBytesToAllocateFor(int blockSize)
6708
{
6709
  /* Unprotected math would be:
6710
  ** return offsetof(BLOCK, s) + blockSize * sizeof(XML_Char);
6711
  **
6712
  ** Detect overflow, avoiding _signed_ overflow undefined behavior
6713
  ** For a + b * c we check b * c in isolation first, so that addition of a
6714
  ** on top has no chance of making us accept a small non-negative number
6715
  */
6716
  const size_t stretch = sizeof(XML_Char);  /* can be 4 bytes */
6717
6718
65972
  if (blockSize <= 0)
6719
    return 0;
6720
6721
32986
  if (blockSize > (int)(INT_MAX / stretch))
6722
    return 0;
6723
6724
  {
6725
    const int stretchedBlockSize = blockSize * (int)stretch;
6726
32986
    const int bytesToAllocate = (int)(
6727
32986
        offsetof(BLOCK, s) + (unsigned)stretchedBlockSize);
6728
32986
    if (bytesToAllocate < 0)
6729
      return 0;
6730
6731
32986
    return (size_t)bytesToAllocate;
6732
  }
6733
32986
}
6734
6735
static XML_Bool FASTCALL
6736
poolGrow(STRING_POOL *pool)
6737
{
6738
7275712
  if (pool->freeBlocks) {
6739
3605120
    if (pool->start == 0) {
6740
3604870
      pool->blocks = pool->freeBlocks;
6741
3604870
      pool->freeBlocks = pool->freeBlocks->next;
6742
3604870
      pool->blocks->next = NULL;
6743
3604870
      pool->start = pool->blocks->s;
6744
3604870
      pool->end = pool->start + pool->blocks->size;
6745
3604870
      pool->ptr = pool->start;
6746
3604870
      return XML_TRUE;
6747
    }
6748
250
    if (pool->end - pool->start < pool->freeBlocks->size) {
6749
      BLOCK *tem = pool->freeBlocks->next;
6750
      pool->freeBlocks->next = pool->blocks;
6751
      pool->blocks = pool->freeBlocks;
6752
      pool->freeBlocks = tem;
6753
      memcpy(pool->blocks->s, pool->start,
6754
             (pool->end - pool->start) * sizeof(XML_Char));
6755
      pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
6756
      pool->start = pool->blocks->s;
6757
      pool->end = pool->start + pool->blocks->size;
6758
      return XML_TRUE;
6759
    }
6760
  }
6761

42806
  if (pool->blocks && pool->start == pool->blocks->s) {
6762
    BLOCK *temp;
6763
4925
    int blockSize = (int)((unsigned)(pool->end - pool->start)*2U);
6764
    size_t bytesToAllocate;
6765
6766
    // NOTE: Needs to be calculated prior to calling `realloc`
6767
    //       to avoid dangling pointers:
6768
4925
    const ptrdiff_t offsetInsideBlock = pool->ptr - pool->start;
6769
6770
4925
    if (blockSize < 0) {
6771
      /* This condition traps a situation where either more than
6772
       * INT_MAX/2 bytes have already been allocated.  This isn't
6773
       * readily testable, since it is unlikely that an average
6774
       * machine will have that much memory, so we exclude it from the
6775
       * coverage statistics.
6776
       */
6777
      return XML_FALSE; /* LCOV_EXCL_LINE */
6778
    }
6779
6780
4925
    bytesToAllocate = poolBytesToAllocateFor(blockSize);
6781
4925
    if (bytesToAllocate == 0)
6782
      return XML_FALSE;
6783
6784
4925
    temp = (BLOCK *)
6785
4925
      pool->mem->realloc_fcn(pool->blocks, (unsigned)bytesToAllocate);
6786
4925
    if (temp == NULL)
6787
370
      return XML_FALSE;
6788
4555
    pool->blocks = temp;
6789
4555
    pool->blocks->size = blockSize;
6790
4555
    pool->ptr = pool->blocks->s + offsetInsideBlock;
6791
4555
    pool->start = pool->blocks->s;
6792
4555
    pool->end = pool->start + blockSize;
6793
4555
  }
6794
  else {
6795
    BLOCK *tem;
6796
28061
    int blockSize = (int)(pool->end - pool->start);
6797
    size_t bytesToAllocate;
6798
6799
28061
    if (blockSize < 0) {
6800
      /* This condition traps a situation where either more than
6801
       * INT_MAX bytes have already been allocated (which is prevented
6802
       * by various pieces of program logic, not least this one, never
6803
       * mind the unlikelihood of actually having that much memory) or
6804
       * the pool control fields have been corrupted (which could
6805
       * conceivably happen in an extremely buggy user handler
6806
       * function).  Either way it isn't readily testable, so we
6807
       * exclude it from the coverage statistics.
6808
       */
6809
      return XML_FALSE;  /* LCOV_EXCL_LINE */
6810
    }
6811
6812
28061
    if (blockSize < INIT_BLOCK_SIZE)
6813
28041
      blockSize = INIT_BLOCK_SIZE;
6814
    else {
6815
      /* Detect overflow, avoiding _signed_ overflow undefined behavior */
6816
20
      if ((int)((unsigned)blockSize * 2U) < 0) {
6817
        return XML_FALSE;
6818
      }
6819
      blockSize *= 2;
6820
    }
6821
6822
28061
    bytesToAllocate = poolBytesToAllocateFor(blockSize);
6823
28061
    if (bytesToAllocate == 0)
6824
      return XML_FALSE;
6825
6826
28061
    tem = (BLOCK *)pool->mem->malloc_fcn(bytesToAllocate);
6827
28061
    if (!tem)
6828
1672
      return XML_FALSE;
6829
26389
    tem->size = blockSize;
6830
26389
    tem->next = pool->blocks;
6831
26389
    pool->blocks = tem;
6832
26389
    if (pool->ptr != pool->start)
6833
8196
      memcpy(tem->s, pool->start,
6834
4098
             (pool->ptr - pool->start) * sizeof(XML_Char));
6835
26389
    pool->ptr = tem->s + (pool->ptr - pool->start);
6836
26389
    pool->start = tem->s;
6837
26389
    pool->end = tem->s + blockSize;
6838
26389
  }
6839
30944
  return XML_TRUE;
6840
3637856
}
6841
6842
static int FASTCALL
6843
nextScaffoldPart(XML_Parser parser)
6844
{
6845
31160
  DTD * const dtd = _dtd;  /* save one level of indirection */
6846
  CONTENT_SCAFFOLD * me;
6847
  int next;
6848
6849
15580
  if (!dtd->scaffIndex) {
6850
700
    dtd->scaffIndex = (int *)MALLOC(groupSize * sizeof(int));
6851
700
    if (!dtd->scaffIndex)
6852
30
      return -1;
6853
670
    dtd->scaffIndex[0] = 0;
6854
670
  }
6855
6856
15550
  if (dtd->scaffCount >= dtd->scaffSize) {
6857
    CONTENT_SCAFFOLD *temp;
6858
880
    if (dtd->scaffold) {
6859
210
      temp = (CONTENT_SCAFFOLD *)
6860
210
        REALLOC(dtd->scaffold, dtd->scaffSize * 2 * sizeof(CONTENT_SCAFFOLD));
6861
210
      if (temp == NULL)
6862
20
        return -1;
6863
190
      dtd->scaffSize *= 2;
6864
190
    }
6865
    else {
6866
670
      temp = (CONTENT_SCAFFOLD *)MALLOC(INIT_SCAFFOLD_ELEMENTS
6867
                                        * sizeof(CONTENT_SCAFFOLD));
6868
670
      if (temp == NULL)
6869
30
        return -1;
6870
640
      dtd->scaffSize = INIT_SCAFFOLD_ELEMENTS;
6871
    }
6872
830
    dtd->scaffold = temp;
6873
830
  }
6874
15500
  next = dtd->scaffCount++;
6875
15500
  me = &dtd->scaffold[next];
6876
15500
  if (dtd->scaffLevel) {
6877
14840
    CONTENT_SCAFFOLD *parent = &dtd->scaffold[dtd->scaffIndex[dtd->scaffLevel-1]];
6878
14840
    if (parent->lastchild) {
6879
11060
      dtd->scaffold[parent->lastchild].nextsib = next;
6880
11060
    }
6881
14840
    if (!parent->childcnt)
6882
3780
      parent->firstchild = next;
6883
14840
    parent->lastchild = next;
6884
14840
    parent->childcnt++;
6885
14840
  }
6886
15500
  me->firstchild = me->lastchild = me->childcnt = me->nextsib = 0;
6887
15500
  return next;
6888
15580
}
6889
6890
static void
6891
build_node(XML_Parser parser,
6892
           int src_node,
6893
           XML_Content *dest,
6894
           XML_Content **contpos,
6895
           XML_Char **strpos)
6896
{
6897
12680
  DTD * const dtd = _dtd;  /* save one level of indirection */
6898
6340
  dest->type = dtd->scaffold[src_node].type;
6899
6340
  dest->quant = dtd->scaffold[src_node].quant;
6900
6340
  if (dest->type == XML_CTYPE_NAME) {
6901
    const XML_Char *src;
6902
3930
    dest->name = *strpos;
6903
3930
    src = dtd->scaffold[src_node].name;
6904
9570
    for (;;) {
6905
9570
      *(*strpos)++ = *src;
6906
9570
      if (!*src)
6907
        break;
6908
5640
      src++;
6909
    }
6910
3930
    dest->numchildren = 0;
6911
3930
    dest->children = NULL;
6912
3930
  }
6913
  else {
6914
    unsigned int i;
6915
    int cn;
6916
2410
    dest->numchildren = dtd->scaffold[src_node].childcnt;
6917
2410
    dest->children = *contpos;
6918
2410
    *contpos += dest->numchildren;
6919
17020
    for (i = 0, cn = dtd->scaffold[src_node].firstchild;
6920
8510
         i < dest->numchildren;
6921
6100
         i++, cn = dtd->scaffold[cn].nextsib) {
6922
6100
      build_node(parser, cn, &(dest->children[i]), contpos, strpos);
6923
    }
6924
2410
    dest->name = NULL;
6925
  }
6926
6340
}
6927
6928
static XML_Content *
6929
build_model (XML_Parser parser)
6930
{
6931
540
  DTD * const dtd = _dtd;  /* save one level of indirection */
6932
  XML_Content *ret;
6933
270
  XML_Content *cpos;
6934
270
  XML_Char * str;
6935
540
  int allocsize = (dtd->scaffCount * sizeof(XML_Content)
6936
270
                   + (dtd->contentStringLen * sizeof(XML_Char)));
6937
6938
270
  ret = (XML_Content *)MALLOC(allocsize);
6939
270
  if (!ret)
6940
30
    return NULL;
6941
6942
240
  str =  (XML_Char *) (&ret[dtd->scaffCount]);
6943
240
  cpos = &ret[1];
6944
6945
240
  build_node(parser, 0, ret, &cpos, &str);
6946
240
  return ret;
6947
270
}
6948
6949
static ELEMENT_TYPE *
6950
getElementType(XML_Parser parser,
6951
               const ENCODING *enc,
6952
               const char *ptr,
6953
               const char *end)
6954
{
6955
29420
  DTD * const dtd = _dtd;  /* save one level of indirection */
6956
14710
  const XML_Char *name = poolStoreString(&dtd->pool, enc, ptr, end);
6957
  ELEMENT_TYPE *ret;
6958
6959
14710
  if (!name)
6960
30
    return NULL;
6961
14680
  ret = (ELEMENT_TYPE *) lookup(parser, &dtd->elementTypes, name, sizeof(ELEMENT_TYPE));
6962
14680
  if (!ret)
6963
550
    return NULL;
6964
14130
  if (ret->name != name)
6965
3450
    poolDiscard(&dtd->pool);
6966
  else {
6967
10680
    poolFinish(&dtd->pool);
6968
10680
    if (!setElementTypePrefix(parser, ret))
6969
10
      return NULL;
6970
  }
6971
14120
  return ret;
6972
14710
}
6973
6974
static XML_Char *
6975
copyString(const XML_Char *s,
6976
           const XML_Memory_Handling_Suite *memsuite)
6977
{
6978
    int charsRequired = 0;
6979
    XML_Char *result;
6980
6981
    /* First determine how long the string is */
6982
2470
    while (s[charsRequired] != 0) {
6983
      charsRequired++;
6984
    }
6985
    /* Include the terminator */
6986
    charsRequired++;
6987
6988
    /* Now allocate space for the copy */
6989
220
    result = memsuite->malloc_fcn(charsRequired * sizeof(XML_Char));
6990
220
    if (result == NULL)
6991
30
        return NULL;
6992
    /* Copy the original into place */
6993
190
    memcpy(result, s, charsRequired * sizeof(XML_Char));
6994
190
    return result;
6995
220
}