Coverage Report

Created: 2025-06-16 06:38

/src/libxml2/parser.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * parser.c : an XML 1.0 parser, namespaces and validity support are mostly
3
 *            implemented on top of the SAX interfaces
4
 *
5
 * References:
6
 *   The XML specification:
7
 *     http://www.w3.org/TR/REC-xml
8
 *   Original 1.0 version:
9
 *     http://www.w3.org/TR/1998/REC-xml-19980210
10
 *   XML second edition working draft
11
 *     http://www.w3.org/TR/2000/WD-xml-2e-20000814
12
 *
13
 * Okay this is a big file, the parser core is around 7000 lines, then it
14
 * is followed by the progressive parser top routines, then the various
15
 * high level APIs to call the parser and a few miscellaneous functions.
16
 * A number of helper functions and deprecated ones have been moved to
17
 * parserInternals.c to reduce this file size.
18
 * As much as possible the functions are associated with their relative
19
 * production in the XML specification. A few productions defining the
20
 * different ranges of character are actually implanted either in
21
 * parserInternals.h or parserInternals.c
22
 * The DOM tree build is realized from the default SAX callbacks in
23
 * the module SAX2.c.
24
 * The routines doing the validation checks are in valid.c and called either
25
 * from the SAX callbacks or as standalone functions using a preparsed
26
 * document.
27
 *
28
 * See Copyright for the status of this software.
29
 *
30
 * Author: Daniel Veillard
31
 */
32
33
/* To avoid EBCDIC trouble when parsing on zOS */
34
#if defined(__MVS__)
35
#pragma convert("ISO8859-1")
36
#endif
37
38
#define IN_LIBXML
39
#include "libxml.h"
40
41
#if defined(_WIN32)
42
#define XML_DIR_SEP '\\'
43
#else
44
#define XML_DIR_SEP '/'
45
#endif
46
47
#include <stdlib.h>
48
#include <limits.h>
49
#include <string.h>
50
#include <stdarg.h>
51
#include <stddef.h>
52
#include <ctype.h>
53
#include <stdlib.h>
54
#include <libxml/parser.h>
55
#include <libxml/xmlmemory.h>
56
#include <libxml/tree.h>
57
#include <libxml/parserInternals.h>
58
#include <libxml/valid.h>
59
#include <libxml/entities.h>
60
#include <libxml/xmlerror.h>
61
#include <libxml/encoding.h>
62
#include <libxml/xmlIO.h>
63
#include <libxml/uri.h>
64
#include <libxml/SAX2.h>
65
#include <libxml/HTMLparser.h>
66
#ifdef LIBXML_CATALOG_ENABLED
67
#include <libxml/catalog.h>
68
#endif
69
70
#include "private/buf.h"
71
#include "private/dict.h"
72
#include "private/entities.h"
73
#include "private/error.h"
74
#include "private/html.h"
75
#include "private/io.h"
76
#include "private/memory.h"
77
#include "private/parser.h"
78
#include "private/tree.h"
79
80
1.39M
#define NS_INDEX_EMPTY  INT_MAX
81
87.4k
#define NS_INDEX_XML    (INT_MAX - 1)
82
566k
#define URI_HASH_EMPTY  0xD943A04E
83
30.5k
#define URI_HASH_XML    0xF0451F02
84
85
#ifndef STDIN_FILENO
86
0
  #define STDIN_FILENO 0
87
#endif
88
89
#ifndef SIZE_MAX
90
  #define SIZE_MAX ((size_t) -1)
91
#endif
92
93
288k
#define XML_MAX_ATTRS 100000000 /* 100 million */
94
95
651k
#define XML_SPECIAL_EXTERNAL    (1 << 20)
96
651k
#define XML_SPECIAL_TYPE_MASK   (XML_SPECIAL_EXTERNAL - 1)
97
98
727k
#define XML_ATTVAL_ALLOC        (1 << 0)
99
53.2k
#define XML_ATTVAL_NORM_CHANGE  (1 << 1)
100
101
struct _xmlStartTag {
102
    const xmlChar *prefix;
103
    const xmlChar *URI;
104
    int line;
105
    int nsNr;
106
};
107
108
typedef struct {
109
    void *saxData;
110
    unsigned prefixHashValue;
111
    unsigned uriHashValue;
112
    unsigned elementId;
113
    int oldIndex;
114
} xmlParserNsExtra;
115
116
typedef struct {
117
    unsigned hashValue;
118
    int index;
119
} xmlParserNsBucket;
120
121
struct _xmlParserNsData {
122
    xmlParserNsExtra *extra;
123
124
    unsigned hashSize;
125
    unsigned hashElems;
126
    xmlParserNsBucket *hash;
127
128
    unsigned elementId;
129
    int defaultNsIndex;
130
    int minNsIndex;
131
};
132
133
static int
134
xmlParseElementStart(xmlParserCtxtPtr ctxt);
135
136
static void
137
xmlParseElementEnd(xmlParserCtxtPtr ctxt);
138
139
static xmlEntityPtr
140
xmlLookupGeneralEntity(xmlParserCtxtPtr ctxt, const xmlChar *name, int inAttr);
141
142
static const xmlChar *
143
xmlParseEntityRefInternal(xmlParserCtxtPtr ctxt);
144
145
/************************************************************************
146
 *                  *
147
 *  Arbitrary limits set in the parser. See XML_PARSE_HUGE    *
148
 *                  *
149
 ************************************************************************/
150
151
#define XML_PARSER_BIG_ENTITY 1000
152
#define XML_PARSER_LOT_ENTITY 5000
153
154
/*
155
 * Constants for protection against abusive entity expansion
156
 * ("billion laughs").
157
 */
158
159
/*
160
 * A certain amount of entity expansion which is always allowed.
161
 */
162
1.15M
#define XML_PARSER_ALLOWED_EXPANSION 1000000
163
164
/*
165
 * Fixed cost for each entity reference. This crudely models processing time
166
 * as well to protect, for example, against exponential expansion of empty
167
 * or very short entities.
168
 */
169
1.15M
#define XML_ENT_FIXED_COST 20
170
171
27.7M
#define XML_PARSER_BIG_BUFFER_SIZE 300
172
177k
#define XML_PARSER_BUFFER_SIZE 100
173
33.0k
#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
174
175
/**
176
 * XML_PARSER_CHUNK_SIZE
177
 *
178
 * When calling GROW that's the minimal amount of data
179
 * the parser expected to have received. It is not a hard
180
 * limit but an optimization when reading strings like Names
181
 * It is not strictly needed as long as inputs available characters
182
 * are followed by 0, which should be provided by the I/O level
183
 */
184
#define XML_PARSER_CHUNK_SIZE 100
185
186
/**
187
 * Constant string describing the internal version of the library
188
 */
189
const char *const
190
xmlParserVersion = LIBXML_VERSION_STRING LIBXML_VERSION_EXTRA;
191
192
/*
193
 * List of XML prefixed PI allowed by W3C specs
194
 */
195
196
static const char* const xmlW3CPIs[] = {
197
    "xml-stylesheet",
198
    "xml-model",
199
    NULL
200
};
201
202
203
/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
204
static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
205
                                              const xmlChar **str);
206
207
static void
208
xmlCtxtParseEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr ent);
209
210
static int
211
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity);
212
213
static void
214
xmlParsePERefInternal(xmlParserCtxt *ctxt, int markupDecl);
215
216
/************************************************************************
217
 *                  *
218
 *    Some factorized error routines        *
219
 *                  *
220
 ************************************************************************/
221
222
static void
223
0
xmlErrMemory(xmlParserCtxtPtr ctxt) {
224
0
    xmlCtxtErrMemory(ctxt);
225
0
}
226
227
/**
228
 * Handle a redefinition of attribute error
229
 *
230
 * @param ctxt  an XML parser context
231
 * @param prefix  the attribute prefix
232
 * @param localname  the attribute localname
233
 */
234
static void
235
xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
236
                   const xmlChar * localname)
237
369k
{
238
369k
    if (prefix == NULL)
239
359k
        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
240
359k
                   XML_ERR_FATAL, localname, NULL, NULL, 0,
241
359k
                   "Attribute %s redefined\n", localname);
242
9.73k
    else
243
9.73k
        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
244
9.73k
                   XML_ERR_FATAL, prefix, localname, NULL, 0,
245
9.73k
                   "Attribute %s:%s redefined\n", prefix, localname);
246
369k
}
247
248
/**
249
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
250
 *
251
 * @param ctxt  an XML parser context
252
 * @param error  the error number
253
 * @param msg  the error message
254
 */
255
static void LIBXML_ATTR_FORMAT(3,0)
256
xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
257
               const char *msg)
258
9.60M
{
259
9.60M
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
260
9.60M
               NULL, NULL, NULL, 0, "%s", msg);
261
9.60M
}
262
263
/**
264
 * Handle a warning.
265
 *
266
 * @param ctxt  an XML parser context
267
 * @param error  the error number
268
 * @param msg  the error message
269
 * @param str1  extra data
270
 * @param str2  extra data
271
 */
272
void LIBXML_ATTR_FORMAT(3,0)
273
xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
274
              const char *msg, const xmlChar *str1, const xmlChar *str2)
275
4.05k
{
276
4.05k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
277
4.05k
               str1, str2, NULL, 0, msg, str1, str2);
278
4.05k
}
279
280
#ifdef LIBXML_VALID_ENABLED
281
/**
282
 * Handle a validity error.
283
 *
284
 * @param ctxt  an XML parser context
285
 * @param error  the error number
286
 * @param msg  the error message
287
 * @param str1  extra data
288
 * @param str2  extra data
289
 */
290
static void LIBXML_ATTR_FORMAT(3,0)
291
xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
292
              const char *msg, const xmlChar *str1, const xmlChar *str2)
293
0
{
294
0
    ctxt->valid = 0;
295
296
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR,
297
0
               str1, str2, NULL, 0, msg, str1, str2);
298
0
}
299
#endif
300
301
/**
302
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
303
 *
304
 * @param ctxt  an XML parser context
305
 * @param error  the error number
306
 * @param msg  the error message
307
 * @param val  an integer value
308
 */
309
static void LIBXML_ATTR_FORMAT(3,0)
310
xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
311
                  const char *msg, int val)
312
2.99M
{
313
2.99M
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
314
2.99M
               NULL, NULL, NULL, val, msg, val);
315
2.99M
}
316
317
/**
318
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
319
 *
320
 * @param ctxt  an XML parser context
321
 * @param error  the error number
322
 * @param msg  the error message
323
 * @param str1  an string info
324
 * @param val  an integer value
325
 * @param str2  an string info
326
 */
327
static void LIBXML_ATTR_FORMAT(3,0)
328
xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
329
                  const char *msg, const xmlChar *str1, int val,
330
      const xmlChar *str2)
331
283k
{
332
283k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
333
283k
               str1, str2, NULL, val, msg, str1, val, str2);
334
283k
}
335
336
/**
337
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
338
 *
339
 * @param ctxt  an XML parser context
340
 * @param error  the error number
341
 * @param msg  the error message
342
 * @param val  a string value
343
 */
344
static void LIBXML_ATTR_FORMAT(3,0)
345
xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
346
                  const char *msg, const xmlChar * val)
347
460k
{
348
460k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
349
460k
               val, NULL, NULL, 0, msg, val);
350
460k
}
351
352
/**
353
 * Handle a non fatal parser error
354
 *
355
 * @param ctxt  an XML parser context
356
 * @param error  the error number
357
 * @param msg  the error message
358
 * @param val  a string value
359
 */
360
static void LIBXML_ATTR_FORMAT(3,0)
361
xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
362
                  const char *msg, const xmlChar * val)
363
39.3k
{
364
39.3k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_ERROR,
365
39.3k
               val, NULL, NULL, 0, msg, val);
366
39.3k
}
367
368
/**
369
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
370
 *
371
 * @param ctxt  an XML parser context
372
 * @param error  the error number
373
 * @param msg  the message
374
 * @param info1  extra information string
375
 * @param info2  extra information string
376
 * @param info3  extra information string
377
 */
378
static void LIBXML_ATTR_FORMAT(3,0)
379
xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
380
         const char *msg,
381
         const xmlChar * info1, const xmlChar * info2,
382
         const xmlChar * info3)
383
377k
{
384
377k
    ctxt->nsWellFormed = 0;
385
386
377k
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
387
377k
               info1, info2, info3, 0, msg, info1, info2, info3);
388
377k
}
389
390
/**
391
 * Handle a namespace warning error
392
 *
393
 * @param ctxt  an XML parser context
394
 * @param error  the error number
395
 * @param msg  the message
396
 * @param info1  extra information string
397
 * @param info2  extra information string
398
 * @param info3  extra information string
399
 */
400
static void LIBXML_ATTR_FORMAT(3,0)
401
xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error,
402
         const char *msg,
403
         const xmlChar * info1, const xmlChar * info2,
404
         const xmlChar * info3)
405
19.3k
{
406
19.3k
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
407
19.3k
               info1, info2, info3, 0, msg, info1, info2, info3);
408
19.3k
}
409
410
static void
411
3.46M
xmlSaturatedAdd(unsigned long *dst, unsigned long val) {
412
3.46M
    if (val > ULONG_MAX - *dst)
413
0
        *dst = ULONG_MAX;
414
3.46M
    else
415
3.46M
        *dst += val;
416
3.46M
}
417
418
static void
419
1.16M
xmlSaturatedAddSizeT(unsigned long *dst, unsigned long val) {
420
1.16M
    if (val > ULONG_MAX - *dst)
421
0
        *dst = ULONG_MAX;
422
1.16M
    else
423
1.16M
        *dst += val;
424
1.16M
}
425
426
/**
427
 * Check for non-linear entity expansion behaviour.
428
 *
429
 * In some cases like xmlExpandEntityInAttValue, this function is called
430
 * for each, possibly nested entity and its unexpanded content length.
431
 *
432
 * In other cases like #xmlParseReference, it's only called for each
433
 * top-level entity with its unexpanded content length plus the sum of
434
 * the unexpanded content lengths (plus fixed cost) of all nested
435
 * entities.
436
 *
437
 * Summing the unexpanded lengths also adds the length of the reference.
438
 * This is by design. Taking the length of the entity name into account
439
 * discourages attacks that try to waste CPU time with abusively long
440
 * entity names. See test/recurse/lol6.xml for example. Each call also
441
 * adds some fixed cost XML_ENT_FIXED_COST to discourage attacks with
442
 * short entities.
443
 *
444
 * @param ctxt  parser context
445
 * @param extra  sum of unexpanded entity sizes
446
 * @returns 1 on error, 0 on success.
447
 */
448
static int
449
xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long extra)
450
1.15M
{
451
1.15M
    unsigned long consumed;
452
1.15M
    unsigned long *expandedSize;
453
1.15M
    xmlParserInputPtr input = ctxt->input;
454
1.15M
    xmlEntityPtr entity = input->entity;
455
456
1.15M
    if ((entity) && (entity->flags & XML_ENT_CHECKED))
457
452
        return(0);
458
459
    /*
460
     * Compute total consumed bytes so far, including input streams of
461
     * external entities.
462
     */
463
1.15M
    consumed = input->consumed;
464
1.15M
    xmlSaturatedAddSizeT(&consumed, input->cur - input->base);
465
1.15M
    xmlSaturatedAdd(&consumed, ctxt->sizeentities);
466
467
1.15M
    if (entity)
468
3.33k
        expandedSize = &entity->expandedSize;
469
1.15M
    else
470
1.15M
        expandedSize = &ctxt->sizeentcopy;
471
472
    /*
473
     * Add extra cost and some fixed cost.
474
     */
475
1.15M
    xmlSaturatedAdd(expandedSize, extra);
476
1.15M
    xmlSaturatedAdd(expandedSize, XML_ENT_FIXED_COST);
477
478
    /*
479
     * It's important to always use saturation arithmetic when tracking
480
     * entity sizes to make the size checks reliable. If "sizeentcopy"
481
     * overflows, we have to abort.
482
     */
483
1.15M
    if ((*expandedSize > XML_PARSER_ALLOWED_EXPANSION) &&
484
1.15M
        ((*expandedSize >= ULONG_MAX) ||
485
76.0k
         (*expandedSize / ctxt->maxAmpl > consumed))) {
486
364
        xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
487
364
                       "Maximum entity amplification factor exceeded, see "
488
364
                       "xmlCtxtSetMaxAmplification.\n");
489
364
        xmlHaltParser(ctxt);
490
364
        return(1);
491
364
    }
492
493
1.15M
    return(0);
494
1.15M
}
495
496
/************************************************************************
497
 *                  *
498
 *    Library wide options          *
499
 *                  *
500
 ************************************************************************/
501
502
/**
503
 * Examines if the library has been compiled with a given feature.
504
 *
505
 * @param feature  the feature to be examined
506
 * @returns zero (0) if the feature does not exist or an unknown
507
 * feature is requested, non-zero otherwise.
508
 */
509
int
510
xmlHasFeature(xmlFeature feature)
511
0
{
512
0
    switch (feature) {
513
0
  case XML_WITH_THREAD:
514
0
#ifdef LIBXML_THREAD_ENABLED
515
0
      return(1);
516
#else
517
      return(0);
518
#endif
519
0
        case XML_WITH_TREE:
520
0
            return(1);
521
0
        case XML_WITH_OUTPUT:
522
0
#ifdef LIBXML_OUTPUT_ENABLED
523
0
            return(1);
524
#else
525
            return(0);
526
#endif
527
0
        case XML_WITH_PUSH:
528
0
#ifdef LIBXML_PUSH_ENABLED
529
0
            return(1);
530
#else
531
            return(0);
532
#endif
533
0
        case XML_WITH_READER:
534
0
#ifdef LIBXML_READER_ENABLED
535
0
            return(1);
536
#else
537
            return(0);
538
#endif
539
0
        case XML_WITH_PATTERN:
540
0
#ifdef LIBXML_PATTERN_ENABLED
541
0
            return(1);
542
#else
543
            return(0);
544
#endif
545
0
        case XML_WITH_WRITER:
546
0
#ifdef LIBXML_WRITER_ENABLED
547
0
            return(1);
548
#else
549
            return(0);
550
#endif
551
0
        case XML_WITH_SAX1:
552
0
#ifdef LIBXML_SAX1_ENABLED
553
0
            return(1);
554
#else
555
            return(0);
556
#endif
557
0
        case XML_WITH_HTTP:
558
0
            return(0);
559
0
        case XML_WITH_VALID:
560
0
#ifdef LIBXML_VALID_ENABLED
561
0
            return(1);
562
#else
563
            return(0);
564
#endif
565
0
        case XML_WITH_HTML:
566
0
#ifdef LIBXML_HTML_ENABLED
567
0
            return(1);
568
#else
569
            return(0);
570
#endif
571
0
        case XML_WITH_LEGACY:
572
0
            return(0);
573
0
        case XML_WITH_C14N:
574
0
#ifdef LIBXML_C14N_ENABLED
575
0
            return(1);
576
#else
577
            return(0);
578
#endif
579
0
        case XML_WITH_CATALOG:
580
0
#ifdef LIBXML_CATALOG_ENABLED
581
0
            return(1);
582
#else
583
            return(0);
584
#endif
585
0
        case XML_WITH_XPATH:
586
0
#ifdef LIBXML_XPATH_ENABLED
587
0
            return(1);
588
#else
589
            return(0);
590
#endif
591
0
        case XML_WITH_XPTR:
592
0
#ifdef LIBXML_XPTR_ENABLED
593
0
            return(1);
594
#else
595
            return(0);
596
#endif
597
0
        case XML_WITH_XINCLUDE:
598
0
#ifdef LIBXML_XINCLUDE_ENABLED
599
0
            return(1);
600
#else
601
            return(0);
602
#endif
603
0
        case XML_WITH_ICONV:
604
0
#ifdef LIBXML_ICONV_ENABLED
605
0
            return(1);
606
#else
607
            return(0);
608
#endif
609
0
        case XML_WITH_ISO8859X:
610
0
#ifdef LIBXML_ISO8859X_ENABLED
611
0
            return(1);
612
#else
613
            return(0);
614
#endif
615
0
        case XML_WITH_UNICODE:
616
0
            return(0);
617
0
        case XML_WITH_REGEXP:
618
0
#ifdef LIBXML_REGEXP_ENABLED
619
0
            return(1);
620
#else
621
            return(0);
622
#endif
623
0
        case XML_WITH_AUTOMATA:
624
0
#ifdef LIBXML_REGEXP_ENABLED
625
0
            return(1);
626
#else
627
            return(0);
628
#endif
629
0
        case XML_WITH_EXPR:
630
0
            return(0);
631
0
        case XML_WITH_RELAXNG:
632
0
#ifdef LIBXML_RELAXNG_ENABLED
633
0
            return(1);
634
#else
635
            return(0);
636
#endif
637
0
        case XML_WITH_SCHEMAS:
638
0
#ifdef LIBXML_SCHEMAS_ENABLED
639
0
            return(1);
640
#else
641
            return(0);
642
#endif
643
0
        case XML_WITH_SCHEMATRON:
644
#ifdef LIBXML_SCHEMATRON_ENABLED
645
            return(1);
646
#else
647
0
            return(0);
648
0
#endif
649
0
        case XML_WITH_MODULES:
650
0
#ifdef LIBXML_MODULES_ENABLED
651
0
            return(1);
652
#else
653
            return(0);
654
#endif
655
0
        case XML_WITH_DEBUG:
656
0
#ifdef LIBXML_DEBUG_ENABLED
657
0
            return(1);
658
#else
659
            return(0);
660
#endif
661
0
        case XML_WITH_DEBUG_MEM:
662
0
            return(0);
663
0
        case XML_WITH_ZLIB:
664
#ifdef LIBXML_ZLIB_ENABLED
665
            return(1);
666
#else
667
0
            return(0);
668
0
#endif
669
0
        case XML_WITH_LZMA:
670
#ifdef LIBXML_LZMA_ENABLED
671
            return(1);
672
#else
673
0
            return(0);
674
0
#endif
675
0
        case XML_WITH_ICU:
676
#ifdef LIBXML_ICU_ENABLED
677
            return(1);
678
#else
679
0
            return(0);
680
0
#endif
681
0
        default:
682
0
      break;
683
0
     }
684
0
     return(0);
685
0
}
686
687
/************************************************************************
688
 *                  *
689
 *      Simple string buffer        *
690
 *                  *
691
 ************************************************************************/
692
693
typedef struct {
694
    xmlChar *mem;
695
    unsigned size;
696
    unsigned cap; /* size < cap */
697
    unsigned max; /* size <= max */
698
    xmlParserErrors code;
699
} xmlSBuf;
700
701
static void
702
672k
xmlSBufInit(xmlSBuf *buf, unsigned max) {
703
672k
    buf->mem = NULL;
704
672k
    buf->size = 0;
705
672k
    buf->cap = 0;
706
672k
    buf->max = max;
707
672k
    buf->code = XML_ERR_OK;
708
672k
}
709
710
static int
711
169k
xmlSBufGrow(xmlSBuf *buf, unsigned len) {
712
169k
    xmlChar *mem;
713
169k
    unsigned cap;
714
715
169k
    if (len >= UINT_MAX / 2 - buf->size) {
716
0
        if (buf->code == XML_ERR_OK)
717
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
718
0
        return(-1);
719
0
    }
720
721
169k
    cap = (buf->size + len) * 2;
722
169k
    if (cap < 240)
723
125k
        cap = 240;
724
725
169k
    mem = xmlRealloc(buf->mem, cap);
726
169k
    if (mem == NULL) {
727
0
        buf->code = XML_ERR_NO_MEMORY;
728
0
        return(-1);
729
0
    }
730
731
169k
    buf->mem = mem;
732
169k
    buf->cap = cap;
733
734
169k
    return(0);
735
169k
}
736
737
static void
738
86.9M
xmlSBufAddString(xmlSBuf *buf, const xmlChar *str, unsigned len) {
739
86.9M
    if (buf->max - buf->size < len) {
740
0
        if (buf->code == XML_ERR_OK)
741
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
742
0
        return;
743
0
    }
744
745
86.9M
    if (buf->cap - buf->size <= len) {
746
159k
        if (xmlSBufGrow(buf, len) < 0)
747
0
            return;
748
159k
    }
749
750
86.9M
    if (len > 0)
751
86.9M
        memcpy(buf->mem + buf->size, str, len);
752
86.9M
    buf->size += len;
753
86.9M
}
754
755
static void
756
85.1M
xmlSBufAddCString(xmlSBuf *buf, const char *str, unsigned len) {
757
85.1M
    xmlSBufAddString(buf, (const xmlChar *) str, len);
758
85.1M
}
759
760
static void
761
142k
xmlSBufAddChar(xmlSBuf *buf, int c) {
762
142k
    xmlChar *end;
763
764
142k
    if (buf->max - buf->size < 4) {
765
0
        if (buf->code == XML_ERR_OK)
766
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
767
0
        return;
768
0
    }
769
770
142k
    if (buf->cap - buf->size <= 4) {
771
10.7k
        if (xmlSBufGrow(buf, 4) < 0)
772
0
            return;
773
10.7k
    }
774
775
142k
    end = buf->mem + buf->size;
776
777
142k
    if (c < 0x80) {
778
59.6k
        *end = (xmlChar) c;
779
59.6k
        buf->size += 1;
780
82.4k
    } else {
781
82.4k
        buf->size += xmlCopyCharMultiByte(end, c);
782
82.4k
    }
783
142k
}
784
785
static void
786
64.8M
xmlSBufAddReplChar(xmlSBuf *buf) {
787
64.8M
    xmlSBufAddCString(buf, "\xEF\xBF\xBD", 3);
788
64.8M
}
789
790
static void
791
0
xmlSBufReportError(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
792
0
    if (buf->code == XML_ERR_NO_MEMORY)
793
0
        xmlCtxtErrMemory(ctxt);
794
0
    else
795
0
        xmlFatalErr(ctxt, buf->code, errMsg);
796
0
}
797
798
static xmlChar *
799
xmlSBufFinish(xmlSBuf *buf, int *sizeOut, xmlParserCtxtPtr ctxt,
800
138k
              const char *errMsg) {
801
138k
    if (buf->mem == NULL) {
802
15.0k
        buf->mem = xmlMalloc(1);
803
15.0k
        if (buf->mem == NULL) {
804
0
            buf->code = XML_ERR_NO_MEMORY;
805
15.0k
        } else {
806
15.0k
            buf->mem[0] = 0;
807
15.0k
        }
808
123k
    } else {
809
123k
        buf->mem[buf->size] = 0;
810
123k
    }
811
812
138k
    if (buf->code == XML_ERR_OK) {
813
138k
        if (sizeOut != NULL)
814
101k
            *sizeOut = buf->size;
815
138k
        return(buf->mem);
816
138k
    }
817
818
0
    xmlSBufReportError(buf, ctxt, errMsg);
819
820
0
    xmlFree(buf->mem);
821
822
0
    if (sizeOut != NULL)
823
0
        *sizeOut = 0;
824
0
    return(NULL);
825
138k
}
826
827
static void
828
517k
xmlSBufCleanup(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
829
517k
    if (buf->code != XML_ERR_OK)
830
0
        xmlSBufReportError(buf, ctxt, errMsg);
831
832
517k
    xmlFree(buf->mem);
833
517k
}
834
835
static int
836
xmlUTF8MultibyteLen(xmlParserCtxtPtr ctxt, const xmlChar *str,
837
91.4M
                    const char *errMsg) {
838
91.4M
    int c = str[0];
839
91.4M
    int c1 = str[1];
840
841
91.4M
    if ((c1 & 0xC0) != 0x80)
842
3.90M
        goto encoding_error;
843
844
87.5M
    if (c < 0xE0) {
845
        /* 2-byte sequence */
846
85.8M
        if (c < 0xC2)
847
53.3M
            goto encoding_error;
848
849
32.4M
        return(2);
850
85.8M
    } else {
851
1.66M
        int c2 = str[2];
852
853
1.66M
        if ((c2 & 0xC0) != 0x80)
854
6.23k
            goto encoding_error;
855
856
1.65M
        if (c < 0xF0) {
857
            /* 3-byte sequence */
858
1.64M
            if (c == 0xE0) {
859
                /* overlong */
860
1.10M
                if (c1 < 0xA0)
861
899
                    goto encoding_error;
862
1.10M
            } else if (c == 0xED) {
863
                /* surrogate */
864
768
                if (c1 >= 0xA0)
865
396
                    goto encoding_error;
866
541k
            } else if (c == 0xEF) {
867
                /* U+FFFE and U+FFFF are invalid Chars */
868
38.7k
                if ((c1 == 0xBF) && (c2 >= 0xBE))
869
1.58k
                    xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, errMsg);
870
38.7k
            }
871
872
1.64M
            return(3);
873
1.64M
        } else {
874
            /* 4-byte sequence */
875
10.4k
            if ((str[3] & 0xC0) != 0x80)
876
1.42k
                goto encoding_error;
877
9.00k
            if (c == 0xF0) {
878
                /* overlong */
879
1.43k
                if (c1 < 0x90)
880
288
                    goto encoding_error;
881
7.57k
            } else if (c >= 0xF4) {
882
                /* greater than 0x10FFFF */
883
3.60k
                if ((c > 0xF4) || (c1 >= 0x90))
884
2.91k
                    goto encoding_error;
885
3.60k
            }
886
887
5.80k
            return(4);
888
9.00k
        }
889
1.65M
    }
890
891
57.3M
encoding_error:
892
    /* Only report the first error */
893
57.3M
    if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
894
1.69k
        xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
895
1.69k
        ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
896
1.69k
    }
897
898
57.3M
    return(0);
899
87.5M
}
900
901
/************************************************************************
902
 *                  *
903
 *    SAX2 defaulted attributes handling      *
904
 *                  *
905
 ************************************************************************/
906
907
/**
908
 * Final initialization of the parser context before starting to parse.
909
 *
910
 * This accounts for users modifying struct members of parser context
911
 * directly.
912
 *
913
 * @param ctxt  an XML parser context
914
 */
915
static void
916
14.5k
xmlCtxtInitializeLate(xmlParserCtxtPtr ctxt) {
917
14.5k
    xmlSAXHandlerPtr sax;
918
919
    /* Avoid unused variable warning if features are disabled. */
920
14.5k
    (void) sax;
921
922
    /*
923
     * Changing the SAX struct directly is still widespread practice
924
     * in internal and external code.
925
     */
926
14.5k
    if (ctxt == NULL) return;
927
14.5k
    sax = ctxt->sax;
928
14.5k
#ifdef LIBXML_SAX1_ENABLED
929
    /*
930
     * Only enable SAX2 if there SAX2 element handlers, except when there
931
     * are no element handlers at all.
932
     */
933
14.5k
    if (((ctxt->options & XML_PARSE_SAX1) == 0) &&
934
14.5k
        (sax) &&
935
14.5k
        (sax->initialized == XML_SAX2_MAGIC) &&
936
14.5k
        ((sax->startElementNs != NULL) ||
937
14.5k
         (sax->endElementNs != NULL) ||
938
14.5k
         ((sax->startElement == NULL) && (sax->endElement == NULL))))
939
14.5k
        ctxt->sax2 = 1;
940
#else
941
    ctxt->sax2 = 1;
942
#endif /* LIBXML_SAX1_ENABLED */
943
944
    /*
945
     * Some users replace the dictionary directly in the context struct.
946
     * We really need an API function to do that cleanly.
947
     */
948
14.5k
    ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
949
14.5k
    ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
950
14.5k
    ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
951
14.5k
    if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
952
14.5k
    (ctxt->str_xml_ns == NULL)) {
953
0
        xmlErrMemory(ctxt);
954
0
    }
955
956
14.5k
    xmlDictSetLimit(ctxt->dict,
957
14.5k
                    (ctxt->options & XML_PARSE_HUGE) ?
958
14.5k
                        0 :
959
14.5k
                        XML_MAX_DICTIONARY_LIMIT);
960
961
14.5k
#ifdef LIBXML_VALID_ENABLED
962
14.5k
    if (ctxt->validate)
963
0
        ctxt->vctxt.flags |= XML_VCTXT_VALIDATE;
964
14.5k
    else
965
14.5k
        ctxt->vctxt.flags &= ~XML_VCTXT_VALIDATE;
966
14.5k
#endif /* LIBXML_VALID_ENABLED */
967
14.5k
}
968
969
typedef struct {
970
    xmlHashedString prefix;
971
    xmlHashedString name;
972
    xmlHashedString value;
973
    const xmlChar *valueEnd;
974
    int external;
975
    int expandedSize;
976
} xmlDefAttr;
977
978
typedef struct _xmlDefAttrs xmlDefAttrs;
979
typedef xmlDefAttrs *xmlDefAttrsPtr;
980
struct _xmlDefAttrs {
981
    int nbAttrs;  /* number of defaulted attributes on that element */
982
    int maxAttrs;       /* the size of the array */
983
#if __STDC_VERSION__ >= 199901L
984
    /* Using a C99 flexible array member avoids UBSan errors. */
985
    xmlDefAttr attrs[] ATTRIBUTE_COUNTED_BY(maxAttrs);
986
#else
987
    xmlDefAttr attrs[1];
988
#endif
989
};
990
991
/**
992
 * Normalize the space in non CDATA attribute values:
993
 * If the attribute type is not CDATA, then the XML processor MUST further
994
 * process the normalized attribute value by discarding any leading and
995
 * trailing space (\#x20) characters, and by replacing sequences of space
996
 * (\#x20) characters by a single space (\#x20) character.
997
 * Note that the size of dst need to be at least src, and if one doesn't need
998
 * to preserve dst (and it doesn't come from a dictionary or read-only) then
999
 * passing src as dst is just fine.
1000
 *
1001
 * @param src  the source string
1002
 * @param dst  the target string
1003
 * @returns a pointer to the normalized value (dst) or NULL if no conversion
1004
 *         is needed.
1005
 */
1006
static xmlChar *
1007
xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst)
1008
14.8k
{
1009
14.8k
    if ((src == NULL) || (dst == NULL))
1010
0
        return(NULL);
1011
1012
16.8k
    while (*src == 0x20) src++;
1013
14.4M
    while (*src != 0) {
1014
14.3M
  if (*src == 0x20) {
1015
146k
      while (*src == 0x20) src++;
1016
10.9k
      if (*src != 0)
1017
10.3k
    *dst++ = 0x20;
1018
14.3M
  } else {
1019
14.3M
      *dst++ = *src++;
1020
14.3M
  }
1021
14.3M
    }
1022
14.8k
    *dst = 0;
1023
14.8k
    if (dst == src)
1024
13.4k
       return(NULL);
1025
1.34k
    return(dst);
1026
14.8k
}
1027
1028
/**
1029
 * Add a defaulted attribute for an element
1030
 *
1031
 * @param ctxt  an XML parser context
1032
 * @param fullname  the element fullname
1033
 * @param fullattr  the attribute fullname
1034
 * @param value  the attribute value
1035
 */
1036
static void
1037
xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
1038
               const xmlChar *fullname,
1039
               const xmlChar *fullattr,
1040
15.8k
               const xmlChar *value) {
1041
15.8k
    xmlDefAttrsPtr defaults;
1042
15.8k
    xmlDefAttr *attr;
1043
15.8k
    int len, expandedSize;
1044
15.8k
    xmlHashedString name;
1045
15.8k
    xmlHashedString prefix;
1046
15.8k
    xmlHashedString hvalue;
1047
15.8k
    const xmlChar *localname;
1048
1049
    /*
1050
     * Allows to detect attribute redefinitions
1051
     */
1052
15.8k
    if (ctxt->attsSpecial != NULL) {
1053
14.3k
        if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL)
1054
5.06k
      return;
1055
14.3k
    }
1056
1057
10.8k
    if (ctxt->attsDefault == NULL) {
1058
1.53k
        ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict);
1059
1.53k
  if (ctxt->attsDefault == NULL)
1060
0
      goto mem_error;
1061
1.53k
    }
1062
1063
    /*
1064
     * split the element name into prefix:localname , the string found
1065
     * are within the DTD and then not associated to namespace names.
1066
     */
1067
10.8k
    localname = xmlSplitQName3(fullname, &len);
1068
10.8k
    if (localname == NULL) {
1069
7.34k
        name = xmlDictLookupHashed(ctxt->dict, fullname, -1);
1070
7.34k
  prefix.name = NULL;
1071
7.34k
    } else {
1072
3.45k
        name = xmlDictLookupHashed(ctxt->dict, localname, -1);
1073
3.45k
  prefix = xmlDictLookupHashed(ctxt->dict, fullname, len);
1074
3.45k
        if (prefix.name == NULL)
1075
0
            goto mem_error;
1076
3.45k
    }
1077
10.8k
    if (name.name == NULL)
1078
0
        goto mem_error;
1079
1080
    /*
1081
     * make sure there is some storage
1082
     */
1083
10.8k
    defaults = xmlHashLookup2(ctxt->attsDefault, name.name, prefix.name);
1084
10.8k
    if ((defaults == NULL) ||
1085
10.8k
        (defaults->nbAttrs >= defaults->maxAttrs)) {
1086
3.71k
        xmlDefAttrsPtr temp;
1087
3.71k
        int newSize;
1088
1089
3.71k
        if (defaults == NULL) {
1090
2.40k
            newSize = 4;
1091
2.40k
        } else {
1092
1.30k
            if ((defaults->maxAttrs >= XML_MAX_ATTRS) ||
1093
1.30k
                ((size_t) defaults->maxAttrs >
1094
1.30k
                     SIZE_MAX / 2 / sizeof(temp[0]) - sizeof(*defaults)))
1095
0
                goto mem_error;
1096
1097
1.30k
            if (defaults->maxAttrs > XML_MAX_ATTRS / 2)
1098
0
                newSize = XML_MAX_ATTRS;
1099
1.30k
            else
1100
1.30k
                newSize = defaults->maxAttrs * 2;
1101
1.30k
        }
1102
3.71k
        temp = xmlRealloc(defaults,
1103
3.71k
                          sizeof(*defaults) + newSize * sizeof(xmlDefAttr));
1104
3.71k
  if (temp == NULL)
1105
0
      goto mem_error;
1106
3.71k
        if (defaults == NULL)
1107
2.40k
            temp->nbAttrs = 0;
1108
3.71k
  temp->maxAttrs = newSize;
1109
3.71k
        defaults = temp;
1110
3.71k
  if (xmlHashUpdateEntry2(ctxt->attsDefault, name.name, prefix.name,
1111
3.71k
                          defaults, NULL) < 0) {
1112
0
      xmlFree(defaults);
1113
0
      goto mem_error;
1114
0
  }
1115
3.71k
    }
1116
1117
    /*
1118
     * Split the attribute name into prefix:localname , the string found
1119
     * are within the DTD and hen not associated to namespace names.
1120
     */
1121
10.8k
    localname = xmlSplitQName3(fullattr, &len);
1122
10.8k
    if (localname == NULL) {
1123
5.98k
        name = xmlDictLookupHashed(ctxt->dict, fullattr, -1);
1124
5.98k
  prefix.name = NULL;
1125
5.98k
    } else {
1126
4.81k
        name = xmlDictLookupHashed(ctxt->dict, localname, -1);
1127
4.81k
  prefix = xmlDictLookupHashed(ctxt->dict, fullattr, len);
1128
4.81k
        if (prefix.name == NULL)
1129
0
            goto mem_error;
1130
4.81k
    }
1131
10.8k
    if (name.name == NULL)
1132
0
        goto mem_error;
1133
1134
    /* intern the string and precompute the end */
1135
10.8k
    len = strlen((const char *) value);
1136
10.8k
    hvalue = xmlDictLookupHashed(ctxt->dict, value, len);
1137
10.8k
    if (hvalue.name == NULL)
1138
0
        goto mem_error;
1139
1140
10.8k
    expandedSize = strlen((const char *) name.name);
1141
10.8k
    if (prefix.name != NULL)
1142
4.81k
        expandedSize += strlen((const char *) prefix.name);
1143
10.8k
    expandedSize += len;
1144
1145
10.8k
    attr = &defaults->attrs[defaults->nbAttrs++];
1146
10.8k
    attr->name = name;
1147
10.8k
    attr->prefix = prefix;
1148
10.8k
    attr->value = hvalue;
1149
10.8k
    attr->valueEnd = hvalue.name + len;
1150
10.8k
    attr->external = PARSER_EXTERNAL(ctxt);
1151
10.8k
    attr->expandedSize = expandedSize;
1152
1153
10.8k
    return;
1154
1155
0
mem_error:
1156
0
    xmlErrMemory(ctxt);
1157
0
}
1158
1159
/**
1160
 * Register this attribute type
1161
 *
1162
 * @param ctxt  an XML parser context
1163
 * @param fullname  the element fullname
1164
 * @param fullattr  the attribute fullname
1165
 * @param type  the attribute type
1166
 */
1167
static void
1168
xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
1169
      const xmlChar *fullname,
1170
      const xmlChar *fullattr,
1171
      int type)
1172
16.7k
{
1173
16.7k
    if (ctxt->attsSpecial == NULL) {
1174
1.74k
        ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict);
1175
1.74k
  if (ctxt->attsSpecial == NULL)
1176
0
      goto mem_error;
1177
1.74k
    }
1178
1179
16.7k
    if (PARSER_EXTERNAL(ctxt))
1180
0
        type |= XML_SPECIAL_EXTERNAL;
1181
1182
16.7k
    if (xmlHashAdd2(ctxt->attsSpecial, fullname, fullattr,
1183
16.7k
                    XML_INT_TO_PTR(type)) < 0)
1184
0
        goto mem_error;
1185
16.7k
    return;
1186
1187
16.7k
mem_error:
1188
0
    xmlErrMemory(ctxt);
1189
0
}
1190
1191
/**
1192
 * Removes CDATA attributes from the special attribute table
1193
 */
1194
static void
1195
xmlCleanSpecialAttrCallback(void *payload, void *data,
1196
                            const xmlChar *fullname, const xmlChar *fullattr,
1197
11.1k
                            const xmlChar *unused ATTRIBUTE_UNUSED) {
1198
11.1k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data;
1199
1200
11.1k
    if (XML_PTR_TO_INT(payload) == XML_ATTRIBUTE_CDATA) {
1201
827
        xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL);
1202
827
    }
1203
11.1k
}
1204
1205
/**
1206
 * Trim the list of attributes defined to remove all those of type
1207
 * CDATA as they are not special. This call should be done when finishing
1208
 * to parse the DTD and before starting to parse the document root.
1209
 *
1210
 * @param ctxt  an XML parser context
1211
 */
1212
static void
1213
xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt)
1214
5.92k
{
1215
5.92k
    if (ctxt->attsSpecial == NULL)
1216
4.17k
        return;
1217
1218
1.74k
    xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt);
1219
1220
1.74k
    if (xmlHashSize(ctxt->attsSpecial) == 0) {
1221
59
        xmlHashFree(ctxt->attsSpecial, NULL);
1222
59
        ctxt->attsSpecial = NULL;
1223
59
    }
1224
1.74k
}
1225
1226
/**
1227
 * Checks that the value conforms to the LanguageID production:
1228
 *
1229
 * @deprecated Internal function, do not use.
1230
 *
1231
 * NOTE: this is somewhat deprecated, those productions were removed from
1232
 * the XML Second edition.
1233
 *
1234
 *     [33] LanguageID ::= Langcode ('-' Subcode)*
1235
 *     [34] Langcode ::= ISO639Code |  IanaCode |  UserCode
1236
 *     [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
1237
 *     [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
1238
 *     [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
1239
 *     [38] Subcode ::= ([a-z] | [A-Z])+
1240
 *
1241
 * The current REC reference the successors of RFC 1766, currently 5646
1242
 *
1243
 * http://www.rfc-editor.org/rfc/rfc5646.txt
1244
 *
1245
 *     langtag       = language
1246
 *                     ["-" script]
1247
 *                     ["-" region]
1248
 *                     *("-" variant)
1249
 *                     *("-" extension)
1250
 *                     ["-" privateuse]
1251
 *     language      = 2*3ALPHA            ; shortest ISO 639 code
1252
 *                     ["-" extlang]       ; sometimes followed by
1253
 *                                         ; extended language subtags
1254
 *                   / 4ALPHA              ; or reserved for future use
1255
 *                   / 5*8ALPHA            ; or registered language subtag
1256
 *
1257
 *     extlang       = 3ALPHA              ; selected ISO 639 codes
1258
 *                     *2("-" 3ALPHA)      ; permanently reserved
1259
 *
1260
 *     script        = 4ALPHA              ; ISO 15924 code
1261
 *
1262
 *     region        = 2ALPHA              ; ISO 3166-1 code
1263
 *                   / 3DIGIT              ; UN M.49 code
1264
 *
1265
 *     variant       = 5*8alphanum         ; registered variants
1266
 *                   / (DIGIT 3alphanum)
1267
 *
1268
 *     extension     = singleton 1*("-" (2*8alphanum))
1269
 *
1270
 *                                         ; Single alphanumerics
1271
 *                                         ; "x" reserved for private use
1272
 *     singleton     = DIGIT               ; 0 - 9
1273
 *                   / %x41-57             ; A - W
1274
 *                   / %x59-5A             ; Y - Z
1275
 *                   / %x61-77             ; a - w
1276
 *                   / %x79-7A             ; y - z
1277
 *
1278
 * it sounds right to still allow Irregular i-xxx IANA and user codes too
1279
 * The parser below doesn't try to cope with extension or privateuse
1280
 * that could be added but that's not interoperable anyway
1281
 *
1282
 * @param lang  pointer to the string value
1283
 * @returns 1 if correct 0 otherwise
1284
 **/
1285
int
1286
xmlCheckLanguageID(const xmlChar * lang)
1287
0
{
1288
0
    const xmlChar *cur = lang, *nxt;
1289
1290
0
    if (cur == NULL)
1291
0
        return (0);
1292
0
    if (((cur[0] == 'i') && (cur[1] == '-')) ||
1293
0
        ((cur[0] == 'I') && (cur[1] == '-')) ||
1294
0
        ((cur[0] == 'x') && (cur[1] == '-')) ||
1295
0
        ((cur[0] == 'X') && (cur[1] == '-'))) {
1296
        /*
1297
         * Still allow IANA code and user code which were coming
1298
         * from the previous version of the XML-1.0 specification
1299
         * it's deprecated but we should not fail
1300
         */
1301
0
        cur += 2;
1302
0
        while (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
1303
0
               ((cur[0] >= 'a') && (cur[0] <= 'z')))
1304
0
            cur++;
1305
0
        return(cur[0] == 0);
1306
0
    }
1307
0
    nxt = cur;
1308
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1309
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1310
0
           nxt++;
1311
0
    if (nxt - cur >= 4) {
1312
        /*
1313
         * Reserved
1314
         */
1315
0
        if ((nxt - cur > 8) || (nxt[0] != 0))
1316
0
            return(0);
1317
0
        return(1);
1318
0
    }
1319
0
    if (nxt - cur < 2)
1320
0
        return(0);
1321
    /* we got an ISO 639 code */
1322
0
    if (nxt[0] == 0)
1323
0
        return(1);
1324
0
    if (nxt[0] != '-')
1325
0
        return(0);
1326
1327
0
    nxt++;
1328
0
    cur = nxt;
1329
    /* now we can have extlang or script or region or variant */
1330
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1331
0
        goto region_m49;
1332
1333
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1334
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1335
0
           nxt++;
1336
0
    if (nxt - cur == 4)
1337
0
        goto script;
1338
0
    if (nxt - cur == 2)
1339
0
        goto region;
1340
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1341
0
        goto variant;
1342
0
    if (nxt - cur != 3)
1343
0
        return(0);
1344
    /* we parsed an extlang */
1345
0
    if (nxt[0] == 0)
1346
0
        return(1);
1347
0
    if (nxt[0] != '-')
1348
0
        return(0);
1349
1350
0
    nxt++;
1351
0
    cur = nxt;
1352
    /* now we can have script or region or variant */
1353
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1354
0
        goto region_m49;
1355
1356
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1357
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1358
0
           nxt++;
1359
0
    if (nxt - cur == 2)
1360
0
        goto region;
1361
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1362
0
        goto variant;
1363
0
    if (nxt - cur != 4)
1364
0
        return(0);
1365
    /* we parsed a script */
1366
0
script:
1367
0
    if (nxt[0] == 0)
1368
0
        return(1);
1369
0
    if (nxt[0] != '-')
1370
0
        return(0);
1371
1372
0
    nxt++;
1373
0
    cur = nxt;
1374
    /* now we can have region or variant */
1375
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1376
0
        goto region_m49;
1377
1378
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1379
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1380
0
           nxt++;
1381
1382
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1383
0
        goto variant;
1384
0
    if (nxt - cur != 2)
1385
0
        return(0);
1386
    /* we parsed a region */
1387
0
region:
1388
0
    if (nxt[0] == 0)
1389
0
        return(1);
1390
0
    if (nxt[0] != '-')
1391
0
        return(0);
1392
1393
0
    nxt++;
1394
0
    cur = nxt;
1395
    /* now we can just have a variant */
1396
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1397
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1398
0
           nxt++;
1399
1400
0
    if ((nxt - cur < 5) || (nxt - cur > 8))
1401
0
        return(0);
1402
1403
    /* we parsed a variant */
1404
0
variant:
1405
0
    if (nxt[0] == 0)
1406
0
        return(1);
1407
0
    if (nxt[0] != '-')
1408
0
        return(0);
1409
    /* extensions and private use subtags not checked */
1410
0
    return (1);
1411
1412
0
region_m49:
1413
0
    if (((nxt[1] >= '0') && (nxt[1] <= '9')) &&
1414
0
        ((nxt[2] >= '0') && (nxt[2] <= '9'))) {
1415
0
        nxt += 3;
1416
0
        goto region;
1417
0
    }
1418
0
    return(0);
1419
0
}
1420
1421
/************************************************************************
1422
 *                  *
1423
 *    Parser stacks related functions and macros    *
1424
 *                  *
1425
 ************************************************************************/
1426
1427
static xmlChar *
1428
xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar **str);
1429
1430
/**
1431
 * Create a new namespace database.
1432
 *
1433
 * @returns the new obejct.
1434
 */
1435
xmlParserNsData *
1436
14.5k
xmlParserNsCreate(void) {
1437
14.5k
    xmlParserNsData *nsdb = xmlMalloc(sizeof(*nsdb));
1438
1439
14.5k
    if (nsdb == NULL)
1440
0
        return(NULL);
1441
14.5k
    memset(nsdb, 0, sizeof(*nsdb));
1442
14.5k
    nsdb->defaultNsIndex = INT_MAX;
1443
1444
14.5k
    return(nsdb);
1445
14.5k
}
1446
1447
/**
1448
 * Free a namespace database.
1449
 *
1450
 * @param nsdb  namespace database
1451
 */
1452
void
1453
14.5k
xmlParserNsFree(xmlParserNsData *nsdb) {
1454
14.5k
    if (nsdb == NULL)
1455
0
        return;
1456
1457
14.5k
    xmlFree(nsdb->extra);
1458
14.5k
    xmlFree(nsdb->hash);
1459
14.5k
    xmlFree(nsdb);
1460
14.5k
}
1461
1462
/**
1463
 * Reset a namespace database.
1464
 *
1465
 * @param nsdb  namespace database
1466
 */
1467
static void
1468
0
xmlParserNsReset(xmlParserNsData *nsdb) {
1469
0
    if (nsdb == NULL)
1470
0
        return;
1471
1472
0
    nsdb->hashElems = 0;
1473
0
    nsdb->elementId = 0;
1474
0
    nsdb->defaultNsIndex = INT_MAX;
1475
1476
0
    if (nsdb->hash)
1477
0
        memset(nsdb->hash, 0, nsdb->hashSize * sizeof(nsdb->hash[0]));
1478
0
}
1479
1480
/**
1481
 * Signal that a new element has started.
1482
 *
1483
 * @param nsdb  namespace database
1484
 * @returns 0 on success, -1 if the element counter overflowed.
1485
 */
1486
static int
1487
2.21M
xmlParserNsStartElement(xmlParserNsData *nsdb) {
1488
2.21M
    if (nsdb->elementId == UINT_MAX)
1489
0
        return(-1);
1490
2.21M
    nsdb->elementId++;
1491
1492
2.21M
    return(0);
1493
2.21M
}
1494
1495
/**
1496
 * Lookup namespace with given prefix. If `bucketPtr` is non-NULL, it will
1497
 * be set to the matching bucket, or the first empty bucket if no match
1498
 * was found.
1499
 *
1500
 * @param ctxt  parser context
1501
 * @param prefix  namespace prefix
1502
 * @param bucketPtr  optional bucket (return value)
1503
 * @returns the namespace index on success, INT_MAX if no namespace was
1504
 * found.
1505
 */
1506
static int
1507
xmlParserNsLookup(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
1508
2.08M
                  xmlParserNsBucket **bucketPtr) {
1509
2.08M
    xmlParserNsBucket *bucket, *tombstone;
1510
2.08M
    unsigned index, hashValue;
1511
1512
2.08M
    if (prefix->name == NULL)
1513
586k
        return(ctxt->nsdb->defaultNsIndex);
1514
1515
1.49M
    if (ctxt->nsdb->hashSize == 0)
1516
32.6k
        return(INT_MAX);
1517
1518
1.46M
    hashValue = prefix->hashValue;
1519
1.46M
    index = hashValue & (ctxt->nsdb->hashSize - 1);
1520
1.46M
    bucket = &ctxt->nsdb->hash[index];
1521
1.46M
    tombstone = NULL;
1522
1523
2.20M
    while (bucket->hashValue) {
1524
1.97M
        if (bucket->index == INT_MAX) {
1525
66.5k
            if (tombstone == NULL)
1526
60.2k
                tombstone = bucket;
1527
1.90M
        } else if (bucket->hashValue == hashValue) {
1528
1.23M
            if (ctxt->nsTab[bucket->index * 2] == prefix->name) {
1529
1.23M
                if (bucketPtr != NULL)
1530
1.07M
                    *bucketPtr = bucket;
1531
1.23M
                return(bucket->index);
1532
1.23M
            }
1533
1.23M
        }
1534
1535
737k
        index++;
1536
737k
        bucket++;
1537
737k
        if (index == ctxt->nsdb->hashSize) {
1538
203k
            index = 0;
1539
203k
            bucket = ctxt->nsdb->hash;
1540
203k
        }
1541
737k
    }
1542
1543
228k
    if (bucketPtr != NULL)
1544
54.7k
        *bucketPtr = tombstone ? tombstone : bucket;
1545
228k
    return(INT_MAX);
1546
1.46M
}
1547
1548
/**
1549
 * Lookup namespace URI with given prefix.
1550
 *
1551
 * @param ctxt  parser context
1552
 * @param prefix  namespace prefix
1553
 * @returns the namespace URI on success, NULL if no namespace was found.
1554
 */
1555
static const xmlChar *
1556
605k
xmlParserNsLookupUri(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix) {
1557
605k
    const xmlChar *ret;
1558
605k
    int nsIndex;
1559
1560
605k
    if (prefix->name == ctxt->str_xml)
1561
531
        return(ctxt->str_xml_ns);
1562
1563
    /*
1564
     * minNsIndex is used when building an entity tree. We must
1565
     * ignore namespaces declared outside the entity.
1566
     */
1567
605k
    nsIndex = xmlParserNsLookup(ctxt, prefix, NULL);
1568
605k
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1569
204k
        return(NULL);
1570
1571
400k
    ret = ctxt->nsTab[nsIndex * 2 + 1];
1572
400k
    if (ret[0] == 0)
1573
28.8k
        ret = NULL;
1574
400k
    return(ret);
1575
605k
}
1576
1577
/**
1578
 * Lookup extra data for the given prefix. This returns data stored
1579
 * with xmlParserNsUdpateSax.
1580
 *
1581
 * @param ctxt  parser context
1582
 * @param prefix  namespace prefix
1583
 * @returns the data on success, NULL if no namespace was found.
1584
 */
1585
void *
1586
28.2k
xmlParserNsLookupSax(xmlParserCtxt *ctxt, const xmlChar *prefix) {
1587
28.2k
    xmlHashedString hprefix;
1588
28.2k
    int nsIndex;
1589
1590
28.2k
    if (prefix == ctxt->str_xml)
1591
6.73k
        return(NULL);
1592
1593
21.5k
    hprefix.name = prefix;
1594
21.5k
    if (prefix != NULL)
1595
13.6k
        hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
1596
7.92k
    else
1597
7.92k
        hprefix.hashValue = 0;
1598
21.5k
    nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
1599
21.5k
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1600
0
        return(NULL);
1601
1602
21.5k
    return(ctxt->nsdb->extra[nsIndex].saxData);
1603
21.5k
}
1604
1605
/**
1606
 * Sets or updates extra data for the given prefix. This value will be
1607
 * returned by xmlParserNsLookupSax as long as the namespace with the
1608
 * given prefix is in scope.
1609
 *
1610
 * @param ctxt  parser context
1611
 * @param prefix  namespace prefix
1612
 * @param saxData  extra data for SAX handler
1613
 * @returns the data on success, NULL if no namespace was found.
1614
 */
1615
int
1616
xmlParserNsUpdateSax(xmlParserCtxt *ctxt, const xmlChar *prefix,
1617
78.5k
                     void *saxData) {
1618
78.5k
    xmlHashedString hprefix;
1619
78.5k
    int nsIndex;
1620
1621
78.5k
    if (prefix == ctxt->str_xml)
1622
0
        return(-1);
1623
1624
78.5k
    hprefix.name = prefix;
1625
78.5k
    if (prefix != NULL)
1626
77.0k
        hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
1627
1.49k
    else
1628
1.49k
        hprefix.hashValue = 0;
1629
78.5k
    nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
1630
78.5k
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1631
0
        return(-1);
1632
1633
78.5k
    ctxt->nsdb->extra[nsIndex].saxData = saxData;
1634
78.5k
    return(0);
1635
78.5k
}
1636
1637
/**
1638
 * Grows the namespace tables.
1639
 *
1640
 * @param ctxt  parser context
1641
 * @returns 0 on success, -1 if a memory allocation failed.
1642
 */
1643
static int
1644
13.2k
xmlParserNsGrow(xmlParserCtxtPtr ctxt) {
1645
13.2k
    const xmlChar **table;
1646
13.2k
    xmlParserNsExtra *extra;
1647
13.2k
    int newSize;
1648
1649
13.2k
    newSize = xmlGrowCapacity(ctxt->nsMax,
1650
13.2k
                              sizeof(table[0]) + sizeof(extra[0]),
1651
13.2k
                              16, XML_MAX_ITEMS);
1652
13.2k
    if (newSize < 0)
1653
0
        goto error;
1654
1655
13.2k
    table = xmlRealloc(ctxt->nsTab, 2 * newSize * sizeof(table[0]));
1656
13.2k
    if (table == NULL)
1657
0
        goto error;
1658
13.2k
    ctxt->nsTab = table;
1659
1660
13.2k
    extra = xmlRealloc(ctxt->nsdb->extra, newSize * sizeof(extra[0]));
1661
13.2k
    if (extra == NULL)
1662
0
        goto error;
1663
13.2k
    ctxt->nsdb->extra = extra;
1664
1665
13.2k
    ctxt->nsMax = newSize;
1666
13.2k
    return(0);
1667
1668
0
error:
1669
0
    xmlErrMemory(ctxt);
1670
0
    return(-1);
1671
13.2k
}
1672
1673
/**
1674
 * Push a new namespace on the table.
1675
 *
1676
 * @param ctxt  parser context
1677
 * @param prefix  prefix with hash value
1678
 * @param uri  uri with hash value
1679
 * @param saxData  extra data for SAX handler
1680
 * @param defAttr  whether the namespace comes from a default attribute
1681
 * @returns 1 if the namespace was pushed, 0 if the namespace was ignored,
1682
 * -1 if a memory allocation failed.
1683
 */
1684
static int
1685
xmlParserNsPush(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
1686
702k
                const xmlHashedString *uri, void *saxData, int defAttr) {
1687
702k
    xmlParserNsBucket *bucket = NULL;
1688
702k
    xmlParserNsExtra *extra;
1689
702k
    const xmlChar **ns;
1690
702k
    unsigned hashValue, nsIndex, oldIndex;
1691
1692
702k
    if ((prefix != NULL) && (prefix->name == ctxt->str_xml))
1693
148
        return(0);
1694
1695
701k
    if ((ctxt->nsNr >= ctxt->nsMax) && (xmlParserNsGrow(ctxt) < 0)) {
1696
0
        xmlErrMemory(ctxt);
1697
0
        return(-1);
1698
0
    }
1699
1700
    /*
1701
     * Default namespace and 'xml' namespace
1702
     */
1703
701k
    if ((prefix == NULL) || (prefix->name == NULL)) {
1704
136k
        oldIndex = ctxt->nsdb->defaultNsIndex;
1705
1706
136k
        if (oldIndex != INT_MAX) {
1707
121k
            extra = &ctxt->nsdb->extra[oldIndex];
1708
1709
121k
            if (extra->elementId == ctxt->nsdb->elementId) {
1710
19.1k
                if (defAttr == 0)
1711
18.5k
                    xmlErrAttributeDup(ctxt, NULL, BAD_CAST "xmlns");
1712
19.1k
                return(0);
1713
19.1k
            }
1714
1715
102k
            if ((ctxt->options & XML_PARSE_NSCLEAN) &&
1716
102k
                (uri->name == ctxt->nsTab[oldIndex * 2 + 1]))
1717
0
                return(0);
1718
102k
        }
1719
1720
117k
        ctxt->nsdb->defaultNsIndex = ctxt->nsNr;
1721
117k
        goto populate_entry;
1722
136k
    }
1723
1724
    /*
1725
     * Hash table lookup
1726
     */
1727
565k
    oldIndex = xmlParserNsLookup(ctxt, prefix, &bucket);
1728
565k
    if (oldIndex != INT_MAX) {
1729
509k
        extra = &ctxt->nsdb->extra[oldIndex];
1730
1731
        /*
1732
         * Check for duplicate definitions on the same element.
1733
         */
1734
509k
        if (extra->elementId == ctxt->nsdb->elementId) {
1735
852
            if (defAttr == 0)
1736
672
                xmlErrAttributeDup(ctxt, BAD_CAST "xmlns", prefix->name);
1737
852
            return(0);
1738
852
        }
1739
1740
508k
        if ((ctxt->options & XML_PARSE_NSCLEAN) &&
1741
508k
            (uri->name == ctxt->nsTab[bucket->index * 2 + 1]))
1742
0
            return(0);
1743
1744
508k
        bucket->index = ctxt->nsNr;
1745
508k
        goto populate_entry;
1746
508k
    }
1747
1748
    /*
1749
     * Insert new bucket
1750
     */
1751
1752
55.9k
    hashValue = prefix->hashValue;
1753
1754
    /*
1755
     * Grow hash table, 50% fill factor
1756
     */
1757
55.9k
    if (ctxt->nsdb->hashElems + 1 > ctxt->nsdb->hashSize / 2) {
1758
1.78k
        xmlParserNsBucket *newHash;
1759
1.78k
        unsigned newSize, i, index;
1760
1761
1.78k
        if (ctxt->nsdb->hashSize > UINT_MAX / 2) {
1762
0
            xmlErrMemory(ctxt);
1763
0
            return(-1);
1764
0
        }
1765
1.78k
        newSize = ctxt->nsdb->hashSize ? ctxt->nsdb->hashSize * 2 : 16;
1766
1.78k
        newHash = xmlMalloc(newSize * sizeof(newHash[0]));
1767
1.78k
        if (newHash == NULL) {
1768
0
            xmlErrMemory(ctxt);
1769
0
            return(-1);
1770
0
        }
1771
1.78k
        memset(newHash, 0, newSize * sizeof(newHash[0]));
1772
1773
170k
        for (i = 0; i < ctxt->nsdb->hashSize; i++) {
1774
168k
            unsigned hv = ctxt->nsdb->hash[i].hashValue;
1775
168k
            unsigned newIndex;
1776
1777
168k
            if ((hv == 0) || (ctxt->nsdb->hash[i].index == INT_MAX))
1778
166k
                continue;
1779
2.24k
            newIndex = hv & (newSize - 1);
1780
1781
3.09k
            while (newHash[newIndex].hashValue != 0) {
1782
843
                newIndex++;
1783
843
                if (newIndex == newSize)
1784
199
                    newIndex = 0;
1785
843
            }
1786
1787
2.24k
            newHash[newIndex] = ctxt->nsdb->hash[i];
1788
2.24k
        }
1789
1790
1.78k
        xmlFree(ctxt->nsdb->hash);
1791
1.78k
        ctxt->nsdb->hash = newHash;
1792
1.78k
        ctxt->nsdb->hashSize = newSize;
1793
1794
        /*
1795
         * Relookup
1796
         */
1797
1.78k
        index = hashValue & (newSize - 1);
1798
1799
2.00k
        while (newHash[index].hashValue != 0) {
1800
225
            index++;
1801
225
            if (index == newSize)
1802
26
                index = 0;
1803
225
        }
1804
1805
1.78k
        bucket = &newHash[index];
1806
1.78k
    }
1807
1808
55.9k
    bucket->hashValue = hashValue;
1809
55.9k
    bucket->index = ctxt->nsNr;
1810
55.9k
    ctxt->nsdb->hashElems++;
1811
55.9k
    oldIndex = INT_MAX;
1812
1813
681k
populate_entry:
1814
681k
    nsIndex = ctxt->nsNr;
1815
1816
681k
    ns = &ctxt->nsTab[nsIndex * 2];
1817
681k
    ns[0] = prefix ? prefix->name : NULL;
1818
681k
    ns[1] = uri->name;
1819
1820
681k
    extra = &ctxt->nsdb->extra[nsIndex];
1821
681k
    extra->saxData = saxData;
1822
681k
    extra->prefixHashValue = prefix ? prefix->hashValue : 0;
1823
681k
    extra->uriHashValue = uri->hashValue;
1824
681k
    extra->elementId = ctxt->nsdb->elementId;
1825
681k
    extra->oldIndex = oldIndex;
1826
1827
681k
    ctxt->nsNr++;
1828
1829
681k
    return(1);
1830
55.9k
}
1831
1832
/**
1833
 * Pops the top `nr` namespaces and restores the hash table.
1834
 *
1835
 * @param ctxt  an XML parser context
1836
 * @param nr  the number to pop
1837
 * @returns the number of namespaces popped.
1838
 */
1839
static int
1840
xmlParserNsPop(xmlParserCtxtPtr ctxt, int nr)
1841
228k
{
1842
228k
    int i;
1843
1844
    /* assert(nr <= ctxt->nsNr); */
1845
1846
908k
    for (i = ctxt->nsNr - 1; i >= ctxt->nsNr - nr; i--) {
1847
679k
        const xmlChar *prefix = ctxt->nsTab[i * 2];
1848
679k
        xmlParserNsExtra *extra = &ctxt->nsdb->extra[i];
1849
1850
679k
        if (prefix == NULL) {
1851
116k
            ctxt->nsdb->defaultNsIndex = extra->oldIndex;
1852
563k
        } else {
1853
563k
            xmlHashedString hprefix;
1854
563k
            xmlParserNsBucket *bucket = NULL;
1855
1856
563k
            hprefix.name = prefix;
1857
563k
            hprefix.hashValue = extra->prefixHashValue;
1858
563k
            xmlParserNsLookup(ctxt, &hprefix, &bucket);
1859
            /* assert(bucket && bucket->hashValue); */
1860
563k
            bucket->index = extra->oldIndex;
1861
563k
        }
1862
679k
    }
1863
1864
228k
    ctxt->nsNr -= nr;
1865
228k
    return(nr);
1866
228k
}
1867
1868
static int
1869
5.64k
xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt) {
1870
5.64k
    const xmlChar **atts;
1871
5.64k
    unsigned *attallocs;
1872
5.64k
    int newSize;
1873
1874
5.64k
    newSize = xmlGrowCapacity(ctxt->maxatts / 5,
1875
5.64k
                              sizeof(atts[0]) * 5 + sizeof(attallocs[0]),
1876
5.64k
                              10, XML_MAX_ATTRS);
1877
5.64k
    if (newSize < 0) {
1878
0
        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
1879
0
                    "Maximum number of attributes exceeded");
1880
0
        return(-1);
1881
0
    }
1882
1883
5.64k
    atts = xmlRealloc(ctxt->atts, newSize * sizeof(atts[0]) * 5);
1884
5.64k
    if (atts == NULL)
1885
0
        goto mem_error;
1886
5.64k
    ctxt->atts = atts;
1887
1888
5.64k
    attallocs = xmlRealloc(ctxt->attallocs,
1889
5.64k
                           newSize * sizeof(attallocs[0]));
1890
5.64k
    if (attallocs == NULL)
1891
0
        goto mem_error;
1892
5.64k
    ctxt->attallocs = attallocs;
1893
1894
5.64k
    ctxt->maxatts = newSize * 5;
1895
1896
5.64k
    return(0);
1897
1898
0
mem_error:
1899
0
    xmlErrMemory(ctxt);
1900
0
    return(-1);
1901
5.64k
}
1902
1903
/**
1904
 * Pushes a new parser input on top of the input stack
1905
 *
1906
 * @param ctxt  an XML parser context
1907
 * @param value  the parser input
1908
 * @returns -1 in case of error, the index in the stack otherwise
1909
 */
1910
int
1911
xmlCtxtPushInput(xmlParserCtxt *ctxt, xmlParserInput *value)
1912
52.9k
{
1913
52.9k
    char *directory = NULL;
1914
52.9k
    int maxDepth;
1915
1916
52.9k
    if ((ctxt == NULL) || (value == NULL))
1917
0
        return(-1);
1918
1919
52.9k
    maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
1920
1921
52.9k
    if (ctxt->inputNr >= ctxt->inputMax) {
1922
1.45k
        xmlParserInputPtr *tmp;
1923
1.45k
        int newSize;
1924
1925
1.45k
        newSize = xmlGrowCapacity(ctxt->inputMax, sizeof(tmp[0]),
1926
1.45k
                                  5, maxDepth);
1927
1.45k
        if (newSize < 0) {
1928
0
            xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
1929
0
                           "Maximum entity nesting depth exceeded");
1930
0
            xmlHaltParser(ctxt);
1931
0
            return(-1);
1932
0
        }
1933
1.45k
        tmp = xmlRealloc(ctxt->inputTab, newSize * sizeof(tmp[0]));
1934
1.45k
        if (tmp == NULL) {
1935
0
            xmlErrMemory(ctxt);
1936
0
            return(-1);
1937
0
        }
1938
1.45k
        ctxt->inputTab = tmp;
1939
1.45k
        ctxt->inputMax = newSize;
1940
1.45k
    }
1941
1942
52.9k
    if ((ctxt->inputNr == 0) && (value->filename != NULL)) {
1943
0
        directory = xmlParserGetDirectory(value->filename);
1944
0
        if (directory == NULL) {
1945
0
            xmlErrMemory(ctxt);
1946
0
            return(-1);
1947
0
        }
1948
0
    }
1949
1950
52.9k
    if (ctxt->input_id >= INT_MAX) {
1951
0
        xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT, "Input ID overflow\n");
1952
0
        return(-1);
1953
0
    }
1954
1955
52.9k
    ctxt->inputTab[ctxt->inputNr] = value;
1956
52.9k
    ctxt->input = value;
1957
1958
52.9k
    if (ctxt->inputNr == 0) {
1959
14.5k
        xmlFree(ctxt->directory);
1960
14.5k
        ctxt->directory = directory;
1961
14.5k
    }
1962
1963
    /*
1964
     * The input ID is unused internally, but there are entity
1965
     * loaders in downstream code that detect the main document
1966
     * by checking for "input_id == 1".
1967
     */
1968
52.9k
    value->id = ctxt->input_id++;
1969
1970
52.9k
    return(ctxt->inputNr++);
1971
52.9k
}
1972
1973
/**
1974
 * Pops the top parser input from the input stack
1975
 *
1976
 * @param ctxt  an XML parser context
1977
 * @returns the input just removed
1978
 */
1979
xmlParserInput *
1980
xmlCtxtPopInput(xmlParserCtxt *ctxt)
1981
82.0k
{
1982
82.0k
    xmlParserInputPtr ret;
1983
1984
82.0k
    if (ctxt == NULL)
1985
0
        return(NULL);
1986
82.0k
    if (ctxt->inputNr <= 0)
1987
29.0k
        return (NULL);
1988
52.9k
    ctxt->inputNr--;
1989
52.9k
    if (ctxt->inputNr > 0)
1990
38.4k
        ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
1991
14.5k
    else
1992
14.5k
        ctxt->input = NULL;
1993
52.9k
    ret = ctxt->inputTab[ctxt->inputNr];
1994
52.9k
    ctxt->inputTab[ctxt->inputNr] = NULL;
1995
52.9k
    return (ret);
1996
82.0k
}
1997
1998
/**
1999
 * Pushes a new element node on top of the node stack
2000
 *
2001
 * @deprecated Internal function, do not use.
2002
 *
2003
 * @param ctxt  an XML parser context
2004
 * @param value  the element node
2005
 * @returns -1 in case of error, the index in the stack otherwise
2006
 */
2007
int
2008
nodePush(xmlParserCtxt *ctxt, xmlNode *value)
2009
63.1k
{
2010
63.1k
    if (ctxt == NULL)
2011
0
        return(0);
2012
2013
63.1k
    if (ctxt->nodeNr >= ctxt->nodeMax) {
2014
5.10k
        int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
2015
5.10k
        xmlNodePtr *tmp;
2016
5.10k
        int newSize;
2017
2018
5.10k
        newSize = xmlGrowCapacity(ctxt->nodeMax, sizeof(tmp[0]),
2019
5.10k
                                  10, maxDepth);
2020
5.10k
        if (newSize < 0) {
2021
6
            xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
2022
6
                    "Excessive depth in document: %d,"
2023
6
                    " use XML_PARSE_HUGE option\n",
2024
6
                    ctxt->nodeNr);
2025
6
            xmlHaltParser(ctxt);
2026
6
            return(-1);
2027
6
        }
2028
2029
5.09k
  tmp = xmlRealloc(ctxt->nodeTab, newSize * sizeof(tmp[0]));
2030
5.09k
        if (tmp == NULL) {
2031
0
            xmlErrMemory(ctxt);
2032
0
            return (-1);
2033
0
        }
2034
5.09k
        ctxt->nodeTab = tmp;
2035
5.09k
  ctxt->nodeMax = newSize;
2036
5.09k
    }
2037
2038
63.1k
    ctxt->nodeTab[ctxt->nodeNr] = value;
2039
63.1k
    ctxt->node = value;
2040
63.1k
    return (ctxt->nodeNr++);
2041
63.1k
}
2042
2043
/**
2044
 * Pops the top element node from the node stack
2045
 *
2046
 * @deprecated Internal function, do not use.
2047
 *
2048
 * @param ctxt  an XML parser context
2049
 * @returns the node just removed
2050
 */
2051
xmlNode *
2052
nodePop(xmlParserCtxt *ctxt)
2053
319k
{
2054
319k
    xmlNodePtr ret;
2055
2056
319k
    if (ctxt == NULL) return(NULL);
2057
319k
    if (ctxt->nodeNr <= 0)
2058
259k
        return (NULL);
2059
60.4k
    ctxt->nodeNr--;
2060
60.4k
    if (ctxt->nodeNr > 0)
2061
58.8k
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
2062
1.60k
    else
2063
1.60k
        ctxt->node = NULL;
2064
60.4k
    ret = ctxt->nodeTab[ctxt->nodeNr];
2065
60.4k
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
2066
60.4k
    return (ret);
2067
319k
}
2068
2069
/**
2070
 * Pushes a new element name/prefix/URL on top of the name stack
2071
 *
2072
 * @param ctxt  an XML parser context
2073
 * @param value  the element name
2074
 * @param prefix  the element prefix
2075
 * @param URI  the element namespace name
2076
 * @param line  the current line number for error messages
2077
 * @param nsNr  the number of namespaces pushed on the namespace table
2078
 * @returns -1 in case of error, the index in the stack otherwise
2079
 */
2080
static int
2081
nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
2082
           const xmlChar *prefix, const xmlChar *URI, int line, int nsNr)
2083
612k
{
2084
612k
    xmlStartTag *tag;
2085
2086
612k
    if (ctxt->nameNr >= ctxt->nameMax) {
2087
18.3k
        const xmlChar **tmp;
2088
18.3k
        xmlStartTag *tmp2;
2089
18.3k
        int newSize;
2090
2091
18.3k
        newSize = xmlGrowCapacity(ctxt->nameMax,
2092
18.3k
                                  sizeof(tmp[0]) + sizeof(tmp2[0]),
2093
18.3k
                                  10, XML_MAX_ITEMS);
2094
18.3k
        if (newSize < 0)
2095
0
            goto mem_error;
2096
2097
18.3k
        tmp = xmlRealloc(ctxt->nameTab, newSize * sizeof(tmp[0]));
2098
18.3k
        if (tmp == NULL)
2099
0
      goto mem_error;
2100
18.3k
  ctxt->nameTab = tmp;
2101
2102
18.3k
        tmp2 = xmlRealloc(ctxt->pushTab, newSize * sizeof(tmp2[0]));
2103
18.3k
        if (tmp2 == NULL)
2104
0
      goto mem_error;
2105
18.3k
  ctxt->pushTab = tmp2;
2106
2107
18.3k
        ctxt->nameMax = newSize;
2108
594k
    } else if (ctxt->pushTab == NULL) {
2109
9.07k
        ctxt->pushTab = xmlMalloc(ctxt->nameMax * sizeof(ctxt->pushTab[0]));
2110
9.07k
        if (ctxt->pushTab == NULL)
2111
0
            goto mem_error;
2112
9.07k
    }
2113
612k
    ctxt->nameTab[ctxt->nameNr] = value;
2114
612k
    ctxt->name = value;
2115
612k
    tag = &ctxt->pushTab[ctxt->nameNr];
2116
612k
    tag->prefix = prefix;
2117
612k
    tag->URI = URI;
2118
612k
    tag->line = line;
2119
612k
    tag->nsNr = nsNr;
2120
612k
    return (ctxt->nameNr++);
2121
0
mem_error:
2122
0
    xmlErrMemory(ctxt);
2123
0
    return (-1);
2124
612k
}
2125
#ifdef LIBXML_PUSH_ENABLED
2126
/**
2127
 * Pops the top element/prefix/URI name from the name stack
2128
 *
2129
 * @param ctxt  an XML parser context
2130
 * @returns the name just removed
2131
 */
2132
static const xmlChar *
2133
nameNsPop(xmlParserCtxtPtr ctxt)
2134
0
{
2135
0
    const xmlChar *ret;
2136
2137
0
    if (ctxt->nameNr <= 0)
2138
0
        return (NULL);
2139
0
    ctxt->nameNr--;
2140
0
    if (ctxt->nameNr > 0)
2141
0
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
2142
0
    else
2143
0
        ctxt->name = NULL;
2144
0
    ret = ctxt->nameTab[ctxt->nameNr];
2145
0
    ctxt->nameTab[ctxt->nameNr] = NULL;
2146
0
    return (ret);
2147
0
}
2148
#endif /* LIBXML_PUSH_ENABLED */
2149
2150
/**
2151
 * Pops the top element name from the name stack
2152
 *
2153
 * @deprecated Internal function, do not use.
2154
 *
2155
 * @param ctxt  an XML parser context
2156
 * @returns the name just removed
2157
 */
2158
static const xmlChar *
2159
namePop(xmlParserCtxtPtr ctxt)
2160
607k
{
2161
607k
    const xmlChar *ret;
2162
2163
607k
    if ((ctxt == NULL) || (ctxt->nameNr <= 0))
2164
0
        return (NULL);
2165
607k
    ctxt->nameNr--;
2166
607k
    if (ctxt->nameNr > 0)
2167
603k
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
2168
3.61k
    else
2169
3.61k
        ctxt->name = NULL;
2170
607k
    ret = ctxt->nameTab[ctxt->nameNr];
2171
607k
    ctxt->nameTab[ctxt->nameNr] = NULL;
2172
607k
    return (ret);
2173
607k
}
2174
2175
2.22M
static int spacePush(xmlParserCtxtPtr ctxt, int val) {
2176
2.22M
    if (ctxt->spaceNr >= ctxt->spaceMax) {
2177
26.2k
        int *tmp;
2178
26.2k
        int newSize;
2179
2180
26.2k
        newSize = xmlGrowCapacity(ctxt->spaceMax, sizeof(tmp[0]),
2181
26.2k
                                  10, XML_MAX_ITEMS);
2182
26.2k
        if (newSize < 0) {
2183
0
      xmlErrMemory(ctxt);
2184
0
      return(-1);
2185
0
        }
2186
2187
26.2k
        tmp = xmlRealloc(ctxt->spaceTab, newSize * sizeof(tmp[0]));
2188
26.2k
        if (tmp == NULL) {
2189
0
      xmlErrMemory(ctxt);
2190
0
      return(-1);
2191
0
  }
2192
26.2k
  ctxt->spaceTab = tmp;
2193
2194
26.2k
        ctxt->spaceMax = newSize;
2195
26.2k
    }
2196
2.22M
    ctxt->spaceTab[ctxt->spaceNr] = val;
2197
2.22M
    ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
2198
2.22M
    return(ctxt->spaceNr++);
2199
2.22M
}
2200
2201
2.21M
static int spacePop(xmlParserCtxtPtr ctxt) {
2202
2.21M
    int ret;
2203
2.21M
    if (ctxt->spaceNr <= 0) return(0);
2204
2.21M
    ctxt->spaceNr--;
2205
2.21M
    if (ctxt->spaceNr > 0)
2206
2.21M
  ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
2207
0
    else
2208
0
        ctxt->space = &ctxt->spaceTab[0];
2209
2.21M
    ret = ctxt->spaceTab[ctxt->spaceNr];
2210
2.21M
    ctxt->spaceTab[ctxt->spaceNr] = -1;
2211
2.21M
    return(ret);
2212
2.21M
}
2213
2214
/*
2215
 * Macros for accessing the content. Those should be used only by the parser,
2216
 * and not exported.
2217
 *
2218
 * Dirty macros, i.e. one often need to make assumption on the context to
2219
 * use them
2220
 *
2221
 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
2222
 *           To be used with extreme caution since operations consuming
2223
 *           characters may move the input buffer to a different location !
2224
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
2225
 *           This should be used internally by the parser
2226
 *           only to compare to ASCII values otherwise it would break when
2227
 *           running with UTF-8 encoding.
2228
 *   RAW     same as CUR but in the input buffer, bypass any token
2229
 *           extraction that may have been done
2230
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
2231
 *           to compare on ASCII based substring.
2232
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
2233
 *           strings without newlines within the parser.
2234
 *   NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
2235
 *           defined char within the parser.
2236
 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
2237
 *
2238
 *   NEXT    Skip to the next character, this does the proper decoding
2239
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
2240
 *   NEXTL(l) Skip the current unicode character of l xmlChars long.
2241
 *   COPY_BUF  copy the current unicode char to the target buffer, increment
2242
 *            the index
2243
 *   GROW, SHRINK  handling of input buffers
2244
 */
2245
2246
11.9M
#define RAW (*ctxt->input->cur)
2247
171M
#define CUR (*ctxt->input->cur)
2248
6.28M
#define NXT(val) ctxt->input->cur[(val)]
2249
363M
#define CUR_PTR ctxt->input->cur
2250
5.70M
#define BASE_PTR ctxt->input->base
2251
2252
#define CMP4( s, c1, c2, c3, c4 ) \
2253
12.6M
  ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
2254
6.31M
    ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
2255
#define CMP5( s, c1, c2, c3, c4, c5 ) \
2256
12.5M
  ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
2257
#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
2258
12.2M
  ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
2259
#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
2260
12.0M
  ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
2261
#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
2262
11.9M
  ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
2263
#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
2264
5.96M
  ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
2265
5.96M
    ((unsigned char *) s)[ 8 ] == c9 )
2266
#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
2267
2.51k
  ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
2268
2.51k
    ((unsigned char *) s)[ 9 ] == c10 )
2269
2270
605k
#define SKIP(val) do {             \
2271
605k
    ctxt->input->cur += (val),ctxt->input->col+=(val);      \
2272
605k
    if (*ctxt->input->cur == 0)           \
2273
605k
        xmlParserGrow(ctxt);           \
2274
605k
  } while (0)
2275
2276
#define SKIPL(val) do {             \
2277
    int skipl;                \
2278
    for(skipl=0; skipl<val; skipl++) {          \
2279
  if (*(ctxt->input->cur) == '\n') {        \
2280
  ctxt->input->line++; ctxt->input->col = 1;      \
2281
  } else ctxt->input->col++;          \
2282
  ctxt->input->cur++;           \
2283
    }                 \
2284
    if (*ctxt->input->cur == 0)           \
2285
        xmlParserGrow(ctxt);            \
2286
  } while (0)
2287
2288
#define SHRINK \
2289
9.73M
    if (!PARSER_PROGRESSIVE(ctxt)) \
2290
9.73M
  xmlParserShrink(ctxt);
2291
2292
#define GROW \
2293
21.5M
    if ((!PARSER_PROGRESSIVE(ctxt)) && \
2294
21.5M
        (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
2295
1.81M
  xmlParserGrow(ctxt);
2296
2297
3.33M
#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
2298
2299
271k
#define SKIP_BLANKS_PE xmlSkipBlankCharsPE(ctxt)
2300
2301
12.3M
#define NEXT xmlNextChar(ctxt)
2302
2303
2.54M
#define NEXT1 {               \
2304
2.54M
  ctxt->input->col++;           \
2305
2.54M
  ctxt->input->cur++;           \
2306
2.54M
  if (*ctxt->input->cur == 0)         \
2307
2.54M
      xmlParserGrow(ctxt);           \
2308
2.54M
    }
2309
2310
218M
#define NEXTL(l) do {             \
2311
218M
    if (*(ctxt->input->cur) == '\n') {         \
2312
14.5M
  ctxt->input->line++; ctxt->input->col = 1;      \
2313
204M
    } else ctxt->input->col++;           \
2314
218M
    ctxt->input->cur += l;        \
2315
218M
  } while (0)
2316
2317
#define COPY_BUF(b, i, v)           \
2318
48.1M
    if (v < 0x80) b[i++] = v;           \
2319
48.1M
    else i += xmlCopyCharMultiByte(&b[i],v)
2320
2321
static int
2322
44.4M
xmlCurrentCharRecover(xmlParserCtxtPtr ctxt, int *len) {
2323
44.4M
    int c = xmlCurrentChar(ctxt, len);
2324
2325
44.4M
    if (c == XML_INVALID_CHAR)
2326
9.49M
        c = 0xFFFD; /* replacement character */
2327
2328
44.4M
    return(c);
2329
44.4M
}
2330
2331
/**
2332
 * Skip whitespace in the input stream.
2333
 *
2334
 * @deprecated Internal function, do not use.
2335
 *
2336
 * @param ctxt  the XML parser context
2337
 * @returns the number of space chars skipped
2338
 */
2339
int
2340
3.45M
xmlSkipBlankChars(xmlParserCtxt *ctxt) {
2341
3.45M
    const xmlChar *cur;
2342
3.45M
    int res = 0;
2343
2344
3.45M
    cur = ctxt->input->cur;
2345
3.45M
    while (IS_BLANK_CH(*cur)) {
2346
2.32M
        if (*cur == '\n') {
2347
1.41M
            ctxt->input->line++; ctxt->input->col = 1;
2348
1.41M
        } else {
2349
907k
            ctxt->input->col++;
2350
907k
        }
2351
2.32M
        cur++;
2352
2.32M
        if (res < INT_MAX)
2353
2.32M
            res++;
2354
2.32M
        if (*cur == 0) {
2355
8.57k
            ctxt->input->cur = cur;
2356
8.57k
            xmlParserGrow(ctxt);
2357
8.57k
            cur = ctxt->input->cur;
2358
8.57k
        }
2359
2.32M
    }
2360
3.45M
    ctxt->input->cur = cur;
2361
2362
3.45M
    if (res > 4)
2363
8.97k
        GROW;
2364
2365
3.45M
    return(res);
2366
3.45M
}
2367
2368
static void
2369
31.3k
xmlPopPE(xmlParserCtxtPtr ctxt) {
2370
31.3k
    unsigned long consumed;
2371
31.3k
    xmlEntityPtr ent;
2372
2373
31.3k
    ent = ctxt->input->entity;
2374
2375
31.3k
    ent->flags &= ~XML_ENT_EXPANDING;
2376
2377
31.3k
    if ((ent->flags & XML_ENT_CHECKED) == 0) {
2378
948
        int result;
2379
2380
        /*
2381
         * Read the rest of the stream in case of errors. We want
2382
         * to account for the whole entity size.
2383
         */
2384
948
        do {
2385
948
            ctxt->input->cur = ctxt->input->end;
2386
948
            xmlParserShrink(ctxt);
2387
948
            result = xmlParserGrow(ctxt);
2388
948
        } while (result > 0);
2389
2390
948
        consumed = ctxt->input->consumed;
2391
948
        xmlSaturatedAddSizeT(&consumed,
2392
948
                             ctxt->input->end - ctxt->input->base);
2393
2394
948
        xmlSaturatedAdd(&ent->expandedSize, consumed);
2395
2396
        /*
2397
         * Add to sizeentities when parsing an external entity
2398
         * for the first time.
2399
         */
2400
948
        if (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
2401
0
            xmlSaturatedAdd(&ctxt->sizeentities, consumed);
2402
0
        }
2403
2404
948
        ent->flags |= XML_ENT_CHECKED;
2405
948
    }
2406
2407
31.3k
    xmlFreeInputStream(xmlCtxtPopInput(ctxt));
2408
2409
31.3k
    xmlParserEntityCheck(ctxt, ent->expandedSize);
2410
2411
31.3k
    GROW;
2412
31.3k
}
2413
2414
/**
2415
 * Skip whitespace in the input stream, also handling parameter
2416
 * entities.
2417
 *
2418
 * @param ctxt  the XML parser context
2419
 * @returns the number of space chars skipped
2420
 */
2421
static int
2422
271k
xmlSkipBlankCharsPE(xmlParserCtxtPtr ctxt) {
2423
271k
    int res = 0;
2424
271k
    int inParam;
2425
271k
    int expandParam;
2426
2427
271k
    inParam = PARSER_IN_PE(ctxt);
2428
271k
    expandParam = PARSER_EXTERNAL(ctxt);
2429
2430
271k
    if (!inParam && !expandParam)
2431
120k
        return(xmlSkipBlankChars(ctxt));
2432
2433
    /*
2434
     * It's Okay to use CUR/NEXT here since all the blanks are on
2435
     * the ASCII range.
2436
     */
2437
7.37M
    while (PARSER_STOPPED(ctxt) == 0) {
2438
7.37M
        if (IS_BLANK_CH(CUR)) { /* CHECKED tstblanks.xml */
2439
7.22M
            NEXT;
2440
7.22M
        } else if (CUR == '%') {
2441
458
            if ((expandParam == 0) ||
2442
458
                (IS_BLANK_CH(NXT(1))) || (NXT(1) == 0))
2443
458
                break;
2444
2445
            /*
2446
             * Expand parameter entity. We continue to consume
2447
             * whitespace at the start of the entity and possible
2448
             * even consume the whole entity and pop it. We might
2449
             * even pop multiple PEs in this loop.
2450
             */
2451
0
            xmlParsePERefInternal(ctxt, 0);
2452
2453
0
            inParam = PARSER_IN_PE(ctxt);
2454
0
            expandParam = PARSER_EXTERNAL(ctxt);
2455
150k
        } else if (CUR == 0) {
2456
9.48k
            if (inParam == 0)
2457
0
                break;
2458
2459
            /*
2460
             * Don't pop parameter entities that start a markup
2461
             * declaration to detect Well-formedness constraint:
2462
             * PE Between Declarations.
2463
             */
2464
9.48k
            if (ctxt->input->flags & XML_INPUT_MARKUP_DECL)
2465
9.48k
                break;
2466
2467
0
            xmlPopPE(ctxt);
2468
2469
0
            inParam = PARSER_IN_PE(ctxt);
2470
0
            expandParam = PARSER_EXTERNAL(ctxt);
2471
141k
        } else {
2472
141k
            break;
2473
141k
        }
2474
2475
        /*
2476
         * Also increase the counter when entering or exiting a PERef.
2477
         * The spec says: "When a parameter-entity reference is recognized
2478
         * in the DTD and included, its replacement text MUST be enlarged
2479
         * by the attachment of one leading and one following space (#x20)
2480
         * character."
2481
         */
2482
7.22M
        if (res < INT_MAX)
2483
7.22M
            res++;
2484
7.22M
    }
2485
2486
151k
    return(res);
2487
271k
}
2488
2489
/************************************************************************
2490
 *                  *
2491
 *    Commodity functions to handle entities      *
2492
 *                  *
2493
 ************************************************************************/
2494
2495
/**
2496
 * @deprecated Internal function, don't use.
2497
 *
2498
 * @param ctxt  an XML parser context
2499
 * @returns the current xmlChar in the parser context
2500
 */
2501
xmlChar
2502
0
xmlPopInput(xmlParserCtxt *ctxt) {
2503
0
    xmlParserInputPtr input;
2504
2505
0
    if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0);
2506
0
    input = xmlCtxtPopInput(ctxt);
2507
0
    xmlFreeInputStream(input);
2508
0
    if (*ctxt->input->cur == 0)
2509
0
        xmlParserGrow(ctxt);
2510
0
    return(CUR);
2511
0
}
2512
2513
/**
2514
 * Push an input stream onto the stack.
2515
 *
2516
 * @deprecated Internal function, don't use.
2517
 *
2518
 * @param ctxt  an XML parser context
2519
 * @param input  an XML parser input fragment (entity, XML fragment ...).
2520
 * @returns -1 in case of error or the index in the input stack
2521
 */
2522
int
2523
0
xmlPushInput(xmlParserCtxt *ctxt, xmlParserInput *input) {
2524
0
    int ret;
2525
2526
0
    if ((ctxt == NULL) || (input == NULL))
2527
0
        return(-1);
2528
2529
0
    ret = xmlCtxtPushInput(ctxt, input);
2530
0
    if (ret >= 0)
2531
0
        GROW;
2532
0
    return(ret);
2533
0
}
2534
2535
/**
2536
 * Parse a numeric character reference. Always consumes '&'.
2537
 *
2538
 * @deprecated Internal function, don't use.
2539
 *
2540
 *     [66] CharRef ::= '&#' [0-9]+ ';' |
2541
 *                      '&#x' [0-9a-fA-F]+ ';'
2542
 *
2543
 * [ WFC: Legal Character ]
2544
 * Characters referred to using character references must match the
2545
 * production for Char.
2546
 *
2547
 * @param ctxt  an XML parser context
2548
 * @returns the value parsed (as an int), 0 in case of error
2549
 */
2550
int
2551
100k
xmlParseCharRef(xmlParserCtxt *ctxt) {
2552
100k
    int val = 0;
2553
100k
    int count = 0;
2554
2555
    /*
2556
     * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
2557
     */
2558
100k
    if ((RAW == '&') && (NXT(1) == '#') &&
2559
100k
        (NXT(2) == 'x')) {
2560
44.1k
  SKIP(3);
2561
44.1k
  GROW;
2562
170k
  while ((RAW != ';') && (PARSER_STOPPED(ctxt) == 0)) {
2563
131k
      if (count++ > 20) {
2564
461
    count = 0;
2565
461
    GROW;
2566
461
      }
2567
131k
      if ((RAW >= '0') && (RAW <= '9'))
2568
8.80k
          val = val * 16 + (CUR - '0');
2569
122k
      else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
2570
18.7k
          val = val * 16 + (CUR - 'a') + 10;
2571
103k
      else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
2572
98.5k
          val = val * 16 + (CUR - 'A') + 10;
2573
4.86k
      else {
2574
4.86k
    xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
2575
4.86k
    val = 0;
2576
4.86k
    break;
2577
4.86k
      }
2578
126k
      if (val > 0x110000)
2579
9.28k
          val = 0x110000;
2580
2581
126k
      NEXT;
2582
126k
      count++;
2583
126k
  }
2584
44.1k
  if (RAW == ';') {
2585
      /* on purpose to avoid reentrancy problems with NEXT and SKIP */
2586
39.2k
      ctxt->input->col++;
2587
39.2k
      ctxt->input->cur++;
2588
39.2k
  }
2589
56.2k
    } else if  ((RAW == '&') && (NXT(1) == '#')) {
2590
56.2k
  SKIP(2);
2591
56.2k
  GROW;
2592
168k
  while (RAW != ';') { /* loop blocked by count */
2593
126k
      if (count++ > 20) {
2594
1.54k
    count = 0;
2595
1.54k
    GROW;
2596
1.54k
      }
2597
126k
      if ((RAW >= '0') && (RAW <= '9'))
2598
111k
          val = val * 10 + (CUR - '0');
2599
14.4k
      else {
2600
14.4k
    xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
2601
14.4k
    val = 0;
2602
14.4k
    break;
2603
14.4k
      }
2604
111k
      if (val > 0x110000)
2605
13.1k
          val = 0x110000;
2606
2607
111k
      NEXT;
2608
111k
      count++;
2609
111k
  }
2610
56.2k
  if (RAW == ';') {
2611
      /* on purpose to avoid reentrancy problems with NEXT and SKIP */
2612
41.8k
      ctxt->input->col++;
2613
41.8k
      ctxt->input->cur++;
2614
41.8k
  }
2615
56.2k
    } else {
2616
0
        if (RAW == '&')
2617
0
            SKIP(1);
2618
0
        xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
2619
0
    }
2620
2621
    /*
2622
     * [ WFC: Legal Character ]
2623
     * Characters referred to using character references must match the
2624
     * production for Char.
2625
     */
2626
100k
    if (val >= 0x110000) {
2627
351
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2628
351
                "xmlParseCharRef: character reference out of bounds\n",
2629
351
          val);
2630
100k
    } else if (IS_CHAR(val)) {
2631
77.0k
        return(val);
2632
77.0k
    } else {
2633
22.9k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2634
22.9k
                          "xmlParseCharRef: invalid xmlChar value %d\n",
2635
22.9k
                    val);
2636
22.9k
    }
2637
23.3k
    return(0);
2638
100k
}
2639
2640
/**
2641
 * parse Reference declarations, variant parsing from a string rather
2642
 * than an an input flow.
2643
 *
2644
 *     [66] CharRef ::= '&#' [0-9]+ ';' |
2645
 *                      '&#x' [0-9a-fA-F]+ ';'
2646
 *
2647
 * [ WFC: Legal Character ]
2648
 * Characters referred to using character references must match the
2649
 * production for Char.
2650
 *
2651
 * @param ctxt  an XML parser context
2652
 * @param str  a pointer to an index in the string
2653
 * @returns the value parsed (as an int), 0 in case of error, str will be
2654
 *         updated to the current value of the index
2655
 */
2656
static int
2657
178k
xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
2658
178k
    const xmlChar *ptr;
2659
178k
    xmlChar cur;
2660
178k
    int val = 0;
2661
2662
178k
    if ((str == NULL) || (*str == NULL)) return(0);
2663
178k
    ptr = *str;
2664
178k
    cur = *ptr;
2665
178k
    if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
2666
44.5k
  ptr += 3;
2667
44.5k
  cur = *ptr;
2668
856k
  while (cur != ';') { /* Non input consuming loop */
2669
813k
      if ((cur >= '0') && (cur <= '9'))
2670
738k
          val = val * 16 + (cur - '0');
2671
75.1k
      else if ((cur >= 'a') && (cur <= 'f'))
2672
1.27k
          val = val * 16 + (cur - 'a') + 10;
2673
73.9k
      else if ((cur >= 'A') && (cur <= 'F'))
2674
72.7k
          val = val * 16 + (cur - 'A') + 10;
2675
1.17k
      else {
2676
1.17k
    xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
2677
1.17k
    val = 0;
2678
1.17k
    break;
2679
1.17k
      }
2680
812k
      if (val > 0x110000)
2681
5.82k
          val = 0x110000;
2682
2683
812k
      ptr++;
2684
812k
      cur = *ptr;
2685
812k
  }
2686
44.5k
  if (cur == ';')
2687
43.3k
      ptr++;
2688
133k
    } else if  ((cur == '&') && (ptr[1] == '#')){
2689
133k
  ptr += 2;
2690
133k
  cur = *ptr;
2691
445k
  while (cur != ';') { /* Non input consuming loops */
2692
313k
      if ((cur >= '0') && (cur <= '9'))
2693
312k
          val = val * 10 + (cur - '0');
2694
1.86k
      else {
2695
1.86k
    xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
2696
1.86k
    val = 0;
2697
1.86k
    break;
2698
1.86k
      }
2699
312k
      if (val > 0x110000)
2700
1.69k
          val = 0x110000;
2701
2702
312k
      ptr++;
2703
312k
      cur = *ptr;
2704
312k
  }
2705
133k
  if (cur == ';')
2706
131k
      ptr++;
2707
133k
    } else {
2708
0
  xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
2709
0
  return(0);
2710
0
    }
2711
178k
    *str = ptr;
2712
2713
    /*
2714
     * [ WFC: Legal Character ]
2715
     * Characters referred to using character references must match the
2716
     * production for Char.
2717
     */
2718
178k
    if (val >= 0x110000) {
2719
248
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2720
248
                "xmlParseStringCharRef: character reference out of bounds\n",
2721
248
                val);
2722
178k
    } else if (IS_CHAR(val)) {
2723
174k
        return(val);
2724
174k
    } else {
2725
3.96k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2726
3.96k
        "xmlParseStringCharRef: invalid xmlChar value %d\n",
2727
3.96k
        val);
2728
3.96k
    }
2729
4.21k
    return(0);
2730
178k
}
2731
2732
/**
2733
 *     [69] PEReference ::= '%' Name ';'
2734
 *
2735
 * @deprecated Internal function, do not use.
2736
 *
2737
 * [ WFC: No Recursion ]
2738
 * A parsed entity must not contain a recursive
2739
 * reference to itself, either directly or indirectly.
2740
 *
2741
 * [ WFC: Entity Declared ]
2742
 * In a document without any DTD, a document with only an internal DTD
2743
 * subset which contains no parameter entity references, or a document
2744
 * with "standalone='yes'", ...  ... The declaration of a parameter
2745
 * entity must precede any reference to it...
2746
 *
2747
 * [ VC: Entity Declared ]
2748
 * In a document with an external subset or external parameter entities
2749
 * with "standalone='no'", ...  ... The declaration of a parameter entity
2750
 * must precede any reference to it...
2751
 *
2752
 * [ WFC: In DTD ]
2753
 * Parameter-entity references may only appear in the DTD.
2754
 * NOTE: misleading but this is handled.
2755
 *
2756
 * A PEReference may have been detected in the current input stream
2757
 * the handling is done accordingly to
2758
 *      http://www.w3.org/TR/REC-xml#entproc
2759
 * i.e.
2760
 *   - Included in literal in entity values
2761
 *   - Included as Parameter Entity reference within DTDs
2762
 * @param ctxt  the parser context
2763
 */
2764
void
2765
0
xmlParserHandlePEReference(xmlParserCtxt *ctxt) {
2766
0
    xmlParsePERefInternal(ctxt, 0);
2767
0
}
2768
2769
/**
2770
 * @deprecated Internal function, don't use.
2771
 *
2772
 * @param ctxt  the parser context
2773
 * @param str  the input string
2774
 * @param len  the string length
2775
 * @param what  combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2776
 * @param end  an end marker xmlChar, 0 if none
2777
 * @param end2  an end marker xmlChar, 0 if none
2778
 * @param end3  an end marker xmlChar, 0 if none
2779
 * @returns A newly allocated string with the substitution done. The caller
2780
 *      must deallocate it !
2781
 */
2782
xmlChar *
2783
xmlStringLenDecodeEntities(xmlParserCtxt *ctxt, const xmlChar *str, int len,
2784
                           int what ATTRIBUTE_UNUSED,
2785
0
                           xmlChar end, xmlChar end2, xmlChar end3) {
2786
0
    if ((ctxt == NULL) || (str == NULL) || (len < 0))
2787
0
        return(NULL);
2788
2789
0
    if ((str[len] != 0) ||
2790
0
        (end != 0) || (end2 != 0) || (end3 != 0))
2791
0
        return(NULL);
2792
2793
0
    return(xmlExpandEntitiesInAttValue(ctxt, str, 0));
2794
0
}
2795
2796
/**
2797
 * @deprecated Internal function, don't use.
2798
 *
2799
 * @param ctxt  the parser context
2800
 * @param str  the input string
2801
 * @param what  combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2802
 * @param end  an end marker xmlChar, 0 if none
2803
 * @param end2  an end marker xmlChar, 0 if none
2804
 * @param end3  an end marker xmlChar, 0 if none
2805
 * @returns A newly allocated string with the substitution done. The caller
2806
 *      must deallocate it !
2807
 */
2808
xmlChar *
2809
xmlStringDecodeEntities(xmlParserCtxt *ctxt, const xmlChar *str,
2810
                        int what ATTRIBUTE_UNUSED,
2811
0
            xmlChar end, xmlChar  end2, xmlChar end3) {
2812
0
    if ((ctxt == NULL) || (str == NULL))
2813
0
        return(NULL);
2814
2815
0
    if ((end != 0) || (end2 != 0) || (end3 != 0))
2816
0
        return(NULL);
2817
2818
0
    return(xmlExpandEntitiesInAttValue(ctxt, str, 0));
2819
0
}
2820
2821
/************************************************************************
2822
 *                  *
2823
 *    Commodity functions, cleanup needed ?     *
2824
 *                  *
2825
 ************************************************************************/
2826
2827
/**
2828
 * Is this a sequence of blank chars that one can ignore ?
2829
 *
2830
 * @param ctxt  an XML parser context
2831
 * @param str  a xmlChar *
2832
 * @param len  the size of `str`
2833
 * @param blank_chars  we know the chars are blanks
2834
 * @returns 1 if ignorable 0 otherwise.
2835
 */
2836
2837
static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2838
0
                     int blank_chars) {
2839
0
    int i;
2840
0
    xmlNodePtr lastChild;
2841
2842
    /*
2843
     * Check for xml:space value.
2844
     */
2845
0
    if ((ctxt->space == NULL) || (*(ctxt->space) == 1) ||
2846
0
        (*(ctxt->space) == -2))
2847
0
  return(0);
2848
2849
    /*
2850
     * Check that the string is made of blanks
2851
     */
2852
0
    if (blank_chars == 0) {
2853
0
  for (i = 0;i < len;i++)
2854
0
      if (!(IS_BLANK_CH(str[i]))) return(0);
2855
0
    }
2856
2857
    /*
2858
     * Look if the element is mixed content in the DTD if available
2859
     */
2860
0
    if (ctxt->node == NULL) return(0);
2861
0
    if (ctxt->myDoc != NULL) {
2862
0
        xmlElementPtr elemDecl = NULL;
2863
0
        xmlDocPtr doc = ctxt->myDoc;
2864
0
        const xmlChar *prefix = NULL;
2865
2866
0
        if (ctxt->node->ns)
2867
0
            prefix = ctxt->node->ns->prefix;
2868
0
        if (doc->intSubset != NULL)
2869
0
            elemDecl = xmlHashLookup2(doc->intSubset->elements, ctxt->node->name,
2870
0
                                      prefix);
2871
0
        if ((elemDecl == NULL) && (doc->extSubset != NULL))
2872
0
            elemDecl = xmlHashLookup2(doc->extSubset->elements, ctxt->node->name,
2873
0
                                      prefix);
2874
0
        if (elemDecl != NULL) {
2875
0
            if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)
2876
0
                return(1);
2877
0
            if ((elemDecl->etype == XML_ELEMENT_TYPE_ANY) ||
2878
0
                (elemDecl->etype == XML_ELEMENT_TYPE_MIXED))
2879
0
                return(0);
2880
0
        }
2881
0
    }
2882
2883
    /*
2884
     * Otherwise, heuristic :-\
2885
     *
2886
     * When push parsing, we could be at the end of a chunk.
2887
     * This makes the look-ahead and consequently the NOBLANKS
2888
     * option unreliable.
2889
     */
2890
0
    if ((RAW != '<') && (RAW != 0xD)) return(0);
2891
0
    if ((ctxt->node->children == NULL) &&
2892
0
  (RAW == '<') && (NXT(1) == '/')) return(0);
2893
2894
0
    lastChild = xmlGetLastChild(ctxt->node);
2895
0
    if (lastChild == NULL) {
2896
0
        if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2897
0
            (ctxt->node->content != NULL)) return(0);
2898
0
    } else if (xmlNodeIsText(lastChild))
2899
0
        return(0);
2900
0
    else if ((ctxt->node->children != NULL) &&
2901
0
             (xmlNodeIsText(ctxt->node->children)))
2902
0
        return(0);
2903
0
    return(1);
2904
0
}
2905
2906
/************************************************************************
2907
 *                  *
2908
 *    Extra stuff for namespace support     *
2909
 *  Relates to http://www.w3.org/TR/WD-xml-names      *
2910
 *                  *
2911
 ************************************************************************/
2912
2913
/**
2914
 * parse an UTF8 encoded XML qualified name string
2915
 *
2916
 * @deprecated Don't use.
2917
 *
2918
 * @param ctxt  an XML parser context
2919
 * @param name  an XML parser context
2920
 * @param prefixOut  a xmlChar **
2921
 * @returns the local part, and prefix is updated
2922
 *   to get the Prefix if any.
2923
 */
2924
2925
xmlChar *
2926
0
xmlSplitQName(xmlParserCtxt *ctxt, const xmlChar *name, xmlChar **prefixOut) {
2927
0
    xmlChar *ret;
2928
0
    const xmlChar *localname;
2929
2930
0
    localname = xmlSplitQName4(name, prefixOut);
2931
0
    if (localname == NULL) {
2932
0
        xmlCtxtErrMemory(ctxt);
2933
0
        return(NULL);
2934
0
    }
2935
2936
0
    ret = xmlStrdup(localname);
2937
0
    if (ret == NULL) {
2938
0
        xmlCtxtErrMemory(ctxt);
2939
0
        xmlFree(*prefixOut);
2940
0
    }
2941
2942
0
    return(ret);
2943
0
}
2944
2945
/************************************************************************
2946
 *                  *
2947
 *      The parser itself       *
2948
 *  Relates to http://www.w3.org/TR/REC-xml       *
2949
 *                  *
2950
 ************************************************************************/
2951
2952
/************************************************************************
2953
 *                  *
2954
 *  Routines to parse Name, NCName and NmToken      *
2955
 *                  *
2956
 ************************************************************************/
2957
2958
/*
2959
 * The two following functions are related to the change of accepted
2960
 * characters for Name and NmToken in the Revision 5 of XML-1.0
2961
 * They correspond to the modified production [4] and the new production [4a]
2962
 * changes in that revision. Also note that the macros used for the
2963
 * productions Letter, Digit, CombiningChar and Extender are not needed
2964
 * anymore.
2965
 * We still keep compatibility to pre-revision5 parsing semantic if the
2966
 * new XML_PARSE_OLD10 option is given to the parser.
2967
 */
2968
2969
static int
2970
2.28M
xmlIsNameStartCharNew(int c) {
2971
    /*
2972
     * Use the new checks of production [4] [4a] amd [5] of the
2973
     * Update 5 of XML-1.0
2974
     */
2975
2.28M
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
2976
2.28M
        (((c >= 'a') && (c <= 'z')) ||
2977
2.28M
         ((c >= 'A') && (c <= 'Z')) ||
2978
2.28M
         (c == '_') || (c == ':') ||
2979
2.28M
         ((c >= 0xC0) && (c <= 0xD6)) ||
2980
2.28M
         ((c >= 0xD8) && (c <= 0xF6)) ||
2981
2.28M
         ((c >= 0xF8) && (c <= 0x2FF)) ||
2982
2.28M
         ((c >= 0x370) && (c <= 0x37D)) ||
2983
2.28M
         ((c >= 0x37F) && (c <= 0x1FFF)) ||
2984
2.28M
         ((c >= 0x200C) && (c <= 0x200D)) ||
2985
2.28M
         ((c >= 0x2070) && (c <= 0x218F)) ||
2986
2.28M
         ((c >= 0x2C00) && (c <= 0x2FEF)) ||
2987
2.28M
         ((c >= 0x3001) && (c <= 0xD7FF)) ||
2988
2.28M
         ((c >= 0xF900) && (c <= 0xFDCF)) ||
2989
2.28M
         ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
2990
2.28M
         ((c >= 0x10000) && (c <= 0xEFFFF))))
2991
475k
        return(1);
2992
1.80M
    return(0);
2993
2.28M
}
2994
2995
static int
2996
17.0M
xmlIsNameCharNew(int c) {
2997
    /*
2998
     * Use the new checks of production [4] [4a] amd [5] of the
2999
     * Update 5 of XML-1.0
3000
     */
3001
17.0M
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3002
17.0M
        (((c >= 'a') && (c <= 'z')) ||
3003
17.0M
         ((c >= 'A') && (c <= 'Z')) ||
3004
17.0M
         ((c >= '0') && (c <= '9')) || /* !start */
3005
17.0M
         (c == '_') || (c == ':') ||
3006
17.0M
         (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3007
17.0M
         ((c >= 0xC0) && (c <= 0xD6)) ||
3008
17.0M
         ((c >= 0xD8) && (c <= 0xF6)) ||
3009
17.0M
         ((c >= 0xF8) && (c <= 0x2FF)) ||
3010
17.0M
         ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3011
17.0M
         ((c >= 0x370) && (c <= 0x37D)) ||
3012
17.0M
         ((c >= 0x37F) && (c <= 0x1FFF)) ||
3013
17.0M
         ((c >= 0x200C) && (c <= 0x200D)) ||
3014
17.0M
         ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3015
17.0M
         ((c >= 0x2070) && (c <= 0x218F)) ||
3016
17.0M
         ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3017
17.0M
         ((c >= 0x3001) && (c <= 0xD7FF)) ||
3018
17.0M
         ((c >= 0xF900) && (c <= 0xFDCF)) ||
3019
17.0M
         ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3020
17.0M
         ((c >= 0x10000) && (c <= 0xEFFFF))))
3021
16.5M
         return(1);
3022
501k
    return(0);
3023
17.0M
}
3024
3025
static int
3026
0
xmlIsNameStartCharOld(int c) {
3027
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3028
0
        ((IS_LETTER(c) || (c == '_') || (c == ':'))))
3029
0
        return(1);
3030
0
    return(0);
3031
0
}
3032
3033
static int
3034
0
xmlIsNameCharOld(int c) {
3035
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3036
0
        ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3037
0
         (c == '.') || (c == '-') ||
3038
0
         (c == '_') || (c == ':') ||
3039
0
         (IS_COMBINING(c)) ||
3040
0
         (IS_EXTENDER(c))))
3041
0
        return(1);
3042
0
    return(0);
3043
0
}
3044
3045
static int
3046
2.28M
xmlIsNameStartChar(int c, int old10) {
3047
2.28M
    if (!old10)
3048
2.28M
        return(xmlIsNameStartCharNew(c));
3049
0
    else
3050
0
        return(xmlIsNameStartCharOld(c));
3051
2.28M
}
3052
3053
static int
3054
17.0M
xmlIsNameChar(int c, int old10) {
3055
17.0M
    if (!old10)
3056
17.0M
        return(xmlIsNameCharNew(c));
3057
0
    else
3058
0
        return(xmlIsNameCharOld(c));
3059
17.0M
}
3060
3061
/*
3062
 * Scan an XML Name, NCName or Nmtoken.
3063
 *
3064
 * Returns a pointer to the end of the name on success. If the
3065
 * name is invalid, returns `ptr`. If the name is longer than
3066
 * `maxSize` bytes, returns NULL.
3067
 *
3068
 * @param ptr  pointer to the start of the name
3069
 * @param maxSize  maximum size in bytes
3070
 * @param flags  XML_SCAN_* flags
3071
 * @returns a pointer to the end of the name or NULL
3072
 */
3073
const xmlChar *
3074
378k
xmlScanName(const xmlChar *ptr, size_t maxSize, int flags) {
3075
378k
    int stop = flags & XML_SCAN_NC ? ':' : 0;
3076
378k
    int old10 = flags & XML_SCAN_OLD10 ? 1 : 0;
3077
3078
3.41M
    while (1) {
3079
3.41M
        int c, len;
3080
3081
3.41M
        c = *ptr;
3082
3.41M
        if (c < 0x80) {
3083
2.06M
            if (c == stop)
3084
207
                break;
3085
2.06M
            len = 1;
3086
2.06M
        } else {
3087
1.34M
            len = 4;
3088
1.34M
            c = xmlGetUTF8Char(ptr, &len);
3089
1.34M
            if (c < 0)
3090
513
                break;
3091
1.34M
        }
3092
3093
3.41M
        if (flags & XML_SCAN_NMTOKEN ?
3094
3.03M
                !xmlIsNameChar(c, old10) :
3095
3.41M
                !xmlIsNameStartChar(c, old10))
3096
378k
            break;
3097
3098
3.03M
        if ((size_t) len > maxSize)
3099
0
            return(NULL);
3100
3.03M
        ptr += len;
3101
3.03M
        maxSize -= len;
3102
3.03M
        flags |= XML_SCAN_NMTOKEN;
3103
3.03M
    }
3104
3105
378k
    return(ptr);
3106
378k
}
3107
3108
static const xmlChar *
3109
132k
xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
3110
132k
    const xmlChar *ret;
3111
132k
    int len = 0, l;
3112
132k
    int c;
3113
132k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3114
132k
                    XML_MAX_TEXT_LENGTH :
3115
132k
                    XML_MAX_NAME_LENGTH;
3116
132k
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3117
3118
    /*
3119
     * Handler for more complex cases
3120
     */
3121
132k
    c = xmlCurrentChar(ctxt, &l);
3122
132k
    if (!xmlIsNameStartChar(c, old10))
3123
99.9k
        return(NULL);
3124
32.3k
    len += l;
3125
32.3k
    NEXTL(l);
3126
32.3k
    c = xmlCurrentChar(ctxt, &l);
3127
3.94M
    while (xmlIsNameChar(c, old10)) {
3128
3.91M
        if (len <= INT_MAX - l)
3129
3.91M
            len += l;
3130
3.91M
        NEXTL(l);
3131
3.91M
        c = xmlCurrentChar(ctxt, &l);
3132
3.91M
    }
3133
32.3k
    if (len > maxLength) {
3134
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
3135
0
        return(NULL);
3136
0
    }
3137
32.3k
    if (ctxt->input->cur - ctxt->input->base < len) {
3138
        /*
3139
         * There were a couple of bugs where PERefs lead to to a change
3140
         * of the buffer. Check the buffer size to avoid passing an invalid
3141
         * pointer to xmlDictLookup.
3142
         */
3143
0
        xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
3144
0
                    "unexpected change of input buffer");
3145
0
        return (NULL);
3146
0
    }
3147
32.3k
    if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r'))
3148
587
        ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len);
3149
31.8k
    else
3150
31.8k
        ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len);
3151
32.3k
    if (ret == NULL)
3152
0
        xmlErrMemory(ctxt);
3153
32.3k
    return(ret);
3154
32.3k
}
3155
3156
/**
3157
 * parse an XML name.
3158
 *
3159
 * @deprecated Internal function, don't use.
3160
 *
3161
 *     [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
3162
 *                      CombiningChar | Extender
3163
 *
3164
 *     [5] Name ::= (Letter | '_' | ':') (NameChar)*
3165
 *
3166
 *     [6] Names ::= Name (#x20 Name)*
3167
 *
3168
 * @param ctxt  an XML parser context
3169
 * @returns the Name parsed or NULL
3170
 */
3171
3172
const xmlChar *
3173
680k
xmlParseName(xmlParserCtxt *ctxt) {
3174
680k
    const xmlChar *in;
3175
680k
    const xmlChar *ret;
3176
680k
    size_t count = 0;
3177
680k
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3178
680k
                       XML_MAX_TEXT_LENGTH :
3179
680k
                       XML_MAX_NAME_LENGTH;
3180
3181
680k
    GROW;
3182
3183
    /*
3184
     * Accelerator for simple ASCII names
3185
     */
3186
680k
    in = ctxt->input->cur;
3187
680k
    if (((*in >= 0x61) && (*in <= 0x7A)) ||
3188
680k
  ((*in >= 0x41) && (*in <= 0x5A)) ||
3189
680k
  (*in == '_') || (*in == ':')) {
3190
568k
  in++;
3191
11.9M
  while (((*in >= 0x61) && (*in <= 0x7A)) ||
3192
11.9M
         ((*in >= 0x41) && (*in <= 0x5A)) ||
3193
11.9M
         ((*in >= 0x30) && (*in <= 0x39)) ||
3194
11.9M
         (*in == '_') || (*in == '-') ||
3195
11.9M
         (*in == ':') || (*in == '.'))
3196
11.3M
      in++;
3197
568k
  if ((*in > 0) && (*in < 0x80)) {
3198
548k
      count = in - ctxt->input->cur;
3199
548k
            if (count > maxLength) {
3200
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
3201
0
                return(NULL);
3202
0
            }
3203
548k
      ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
3204
548k
      ctxt->input->cur = in;
3205
548k
      ctxt->input->col += count;
3206
548k
      if (ret == NULL)
3207
0
          xmlErrMemory(ctxt);
3208
548k
      return(ret);
3209
548k
  }
3210
568k
    }
3211
    /* accelerator for special cases */
3212
132k
    return(xmlParseNameComplex(ctxt));
3213
680k
}
3214
3215
static xmlHashedString
3216
1.79M
xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
3217
1.79M
    xmlHashedString ret;
3218
1.79M
    int len = 0, l;
3219
1.79M
    int c;
3220
1.79M
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3221
1.79M
                    XML_MAX_TEXT_LENGTH :
3222
1.79M
                    XML_MAX_NAME_LENGTH;
3223
1.79M
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3224
1.79M
    size_t startPosition = 0;
3225
3226
1.79M
    ret.name = NULL;
3227
1.79M
    ret.hashValue = 0;
3228
3229
    /*
3230
     * Handler for more complex cases
3231
     */
3232
1.79M
    startPosition = CUR_PTR - BASE_PTR;
3233
1.79M
    c = xmlCurrentChar(ctxt, &l);
3234
1.79M
    if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
3235
1.79M
  (!xmlIsNameStartChar(c, old10) || (c == ':'))) {
3236
1.74M
  return(ret);
3237
1.74M
    }
3238
3239
2.96M
    while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
3240
2.96M
     (xmlIsNameChar(c, old10) && (c != ':'))) {
3241
2.90M
        if (len <= INT_MAX - l)
3242
2.90M
      len += l;
3243
2.90M
  NEXTL(l);
3244
2.90M
  c = xmlCurrentChar(ctxt, &l);
3245
2.90M
    }
3246
53.0k
    if (len > maxLength) {
3247
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3248
0
        return(ret);
3249
0
    }
3250
53.0k
    ret = xmlDictLookupHashed(ctxt->dict, (BASE_PTR + startPosition), len);
3251
53.0k
    if (ret.name == NULL)
3252
0
        xmlErrMemory(ctxt);
3253
53.0k
    return(ret);
3254
53.0k
}
3255
3256
/**
3257
 * parse an XML name.
3258
 *
3259
 *     [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
3260
 *                          CombiningChar | Extender
3261
 *
3262
 *     [5NS] NCName ::= (Letter | '_') (NCNameChar)*
3263
 *
3264
 * @param ctxt  an XML parser context
3265
 * @returns the Name parsed or NULL
3266
 */
3267
3268
static xmlHashedString
3269
3.15M
xmlParseNCName(xmlParserCtxtPtr ctxt) {
3270
3.15M
    const xmlChar *in, *e;
3271
3.15M
    xmlHashedString ret;
3272
3.15M
    size_t count = 0;
3273
3.15M
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3274
3.15M
                       XML_MAX_TEXT_LENGTH :
3275
3.15M
                       XML_MAX_NAME_LENGTH;
3276
3277
3.15M
    ret.name = NULL;
3278
3279
    /*
3280
     * Accelerator for simple ASCII names
3281
     */
3282
3.15M
    in = ctxt->input->cur;
3283
3.15M
    e = ctxt->input->end;
3284
3.15M
    if ((((*in >= 0x61) && (*in <= 0x7A)) ||
3285
3.15M
   ((*in >= 0x41) && (*in <= 0x5A)) ||
3286
3.15M
   (*in == '_')) && (in < e)) {
3287
1.39M
  in++;
3288
5.46M
  while ((((*in >= 0x61) && (*in <= 0x7A)) ||
3289
5.46M
          ((*in >= 0x41) && (*in <= 0x5A)) ||
3290
5.46M
          ((*in >= 0x30) && (*in <= 0x39)) ||
3291
5.46M
          (*in == '_') || (*in == '-') ||
3292
5.46M
          (*in == '.')) && (in < e))
3293
4.06M
      in++;
3294
1.39M
  if (in >= e)
3295
1.00k
      goto complex;
3296
1.39M
  if ((*in > 0) && (*in < 0x80)) {
3297
1.35M
      count = in - ctxt->input->cur;
3298
1.35M
            if (count > maxLength) {
3299
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3300
0
                return(ret);
3301
0
            }
3302
1.35M
      ret = xmlDictLookupHashed(ctxt->dict, ctxt->input->cur, count);
3303
1.35M
      ctxt->input->cur = in;
3304
1.35M
      ctxt->input->col += count;
3305
1.35M
      if (ret.name == NULL) {
3306
0
          xmlErrMemory(ctxt);
3307
0
      }
3308
1.35M
      return(ret);
3309
1.35M
  }
3310
1.39M
    }
3311
1.79M
complex:
3312
1.79M
    return(xmlParseNCNameComplex(ctxt));
3313
3.15M
}
3314
3315
/**
3316
 * parse an XML name and compares for match
3317
 * (specialized for endtag parsing)
3318
 *
3319
 * @param ctxt  an XML parser context
3320
 * @param other  the name to compare with
3321
 * @returns NULL for an illegal name, (xmlChar*) 1 for success
3322
 * and the name for mismatch
3323
 */
3324
3325
static const xmlChar *
3326
22.1k
xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
3327
22.1k
    register const xmlChar *cmp = other;
3328
22.1k
    register const xmlChar *in;
3329
22.1k
    const xmlChar *ret;
3330
3331
22.1k
    GROW;
3332
3333
22.1k
    in = ctxt->input->cur;
3334
32.4k
    while (*in != 0 && *in == *cmp) {
3335
10.3k
  ++in;
3336
10.3k
  ++cmp;
3337
10.3k
    }
3338
22.1k
    if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
3339
  /* success */
3340
8.55k
  ctxt->input->col += in - ctxt->input->cur;
3341
8.55k
  ctxt->input->cur = in;
3342
8.55k
  return (const xmlChar*) 1;
3343
8.55k
    }
3344
    /* failure (or end of input buffer), check with full function */
3345
13.5k
    ret = xmlParseName (ctxt);
3346
    /* strings coming from the dictionary direct compare possible */
3347
13.5k
    if (ret == other) {
3348
1.18k
  return (const xmlChar*) 1;
3349
1.18k
    }
3350
12.3k
    return ret;
3351
13.5k
}
3352
3353
/**
3354
 * Parse an XML name.
3355
 *
3356
 * @param ctxt  an XML parser context
3357
 * @param str  a pointer to the string pointer (IN/OUT)
3358
 * @returns the Name parsed or NULL. The `str` pointer
3359
 * is updated to the current location in the string.
3360
 */
3361
3362
static xmlChar *
3363
373k
xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
3364
373k
    xmlChar *ret;
3365
373k
    const xmlChar *cur = *str;
3366
373k
    int flags = 0;
3367
373k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3368
373k
                    XML_MAX_TEXT_LENGTH :
3369
373k
                    XML_MAX_NAME_LENGTH;
3370
3371
373k
    if (ctxt->options & XML_PARSE_OLD10)
3372
0
        flags |= XML_SCAN_OLD10;
3373
3374
373k
    cur = xmlScanName(*str, maxLength, flags);
3375
373k
    if (cur == NULL) {
3376
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3377
0
        return(NULL);
3378
0
    }
3379
373k
    if (cur == *str)
3380
997
        return(NULL);
3381
3382
372k
    ret = xmlStrndup(*str, cur - *str);
3383
372k
    if (ret == NULL)
3384
0
        xmlErrMemory(ctxt);
3385
372k
    *str = cur;
3386
372k
    return(ret);
3387
373k
}
3388
3389
/**
3390
 * parse an XML Nmtoken.
3391
 *
3392
 * @deprecated Internal function, don't use.
3393
 *
3394
 *     [7] Nmtoken ::= (NameChar)+
3395
 *
3396
 *     [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
3397
 *
3398
 * @param ctxt  an XML parser context
3399
 * @returns the Nmtoken parsed or NULL
3400
 */
3401
3402
xmlChar *
3403
48.0k
xmlParseNmtoken(xmlParserCtxt *ctxt) {
3404
48.0k
    xmlChar buf[XML_MAX_NAMELEN + 5];
3405
48.0k
    xmlChar *ret;
3406
48.0k
    int len = 0, l;
3407
48.0k
    int c;
3408
48.0k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3409
48.0k
                    XML_MAX_TEXT_LENGTH :
3410
48.0k
                    XML_MAX_NAME_LENGTH;
3411
48.0k
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3412
3413
48.0k
    c = xmlCurrentChar(ctxt, &l);
3414
3415
213k
    while (xmlIsNameChar(c, old10)) {
3416
166k
  COPY_BUF(buf, len, c);
3417
166k
  NEXTL(l);
3418
166k
  c = xmlCurrentChar(ctxt, &l);
3419
166k
  if (len >= XML_MAX_NAMELEN) {
3420
      /*
3421
       * Okay someone managed to make a huge token, so he's ready to pay
3422
       * for the processing speed.
3423
       */
3424
1.07k
      xmlChar *buffer;
3425
1.07k
      int max = len * 2;
3426
3427
1.07k
      buffer = xmlMalloc(max);
3428
1.07k
      if (buffer == NULL) {
3429
0
          xmlErrMemory(ctxt);
3430
0
    return(NULL);
3431
0
      }
3432
1.07k
      memcpy(buffer, buf, len);
3433
6.88M
      while (xmlIsNameChar(c, old10)) {
3434
6.88M
    if (len + 10 > max) {
3435
2.89k
        xmlChar *tmp;
3436
2.89k
                    int newSize;
3437
3438
2.89k
                    newSize = xmlGrowCapacity(max, 1, 1, maxLength);
3439
2.89k
                    if (newSize < 0) {
3440
0
                        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
3441
0
                        xmlFree(buffer);
3442
0
                        return(NULL);
3443
0
                    }
3444
2.89k
        tmp = xmlRealloc(buffer, newSize);
3445
2.89k
        if (tmp == NULL) {
3446
0
      xmlErrMemory(ctxt);
3447
0
      xmlFree(buffer);
3448
0
      return(NULL);
3449
0
        }
3450
2.89k
        buffer = tmp;
3451
2.89k
                    max = newSize;
3452
2.89k
    }
3453
6.88M
    COPY_BUF(buffer, len, c);
3454
6.88M
    NEXTL(l);
3455
6.88M
    c = xmlCurrentChar(ctxt, &l);
3456
6.88M
      }
3457
1.07k
      buffer[len] = 0;
3458
1.07k
      return(buffer);
3459
1.07k
  }
3460
166k
    }
3461
46.9k
    if (len == 0)
3462
17.4k
        return(NULL);
3463
29.5k
    if (len > maxLength) {
3464
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
3465
0
        return(NULL);
3466
0
    }
3467
29.5k
    ret = xmlStrndup(buf, len);
3468
29.5k
    if (ret == NULL)
3469
0
        xmlErrMemory(ctxt);
3470
29.5k
    return(ret);
3471
29.5k
}
3472
3473
/**
3474
 * Validate an entity value and expand parameter entities.
3475
 *
3476
 * @param ctxt  parser context
3477
 * @param buf  string buffer
3478
 * @param str  entity value
3479
 * @param length  size of entity value
3480
 * @param depth  nesting depth
3481
 */
3482
static void
3483
xmlExpandPEsInEntityValue(xmlParserCtxtPtr ctxt, xmlSBuf *buf,
3484
20.8k
                          const xmlChar *str, int length, int depth) {
3485
20.8k
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3486
20.8k
    const xmlChar *end, *chunk;
3487
20.8k
    int c, l;
3488
3489
20.8k
    if (str == NULL)
3490
0
        return;
3491
3492
20.8k
    depth += 1;
3493
20.8k
    if (depth > maxDepth) {
3494
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3495
0
                       "Maximum entity nesting depth exceeded");
3496
0
  return;
3497
0
    }
3498
3499
20.8k
    end = str + length;
3500
20.8k
    chunk = str;
3501
3502
69.0M
    while ((str < end) && (!PARSER_STOPPED(ctxt))) {
3503
69.0M
        c = *str;
3504
3505
69.0M
        if (c >= 0x80) {
3506
54.9M
            l = xmlUTF8MultibyteLen(ctxt, str,
3507
54.9M
                    "invalid character in entity value\n");
3508
54.9M
            if (l == 0) {
3509
24.7M
                if (chunk < str)
3510
55.2k
                    xmlSBufAddString(buf, chunk, str - chunk);
3511
24.7M
                xmlSBufAddReplChar(buf);
3512
24.7M
                str += 1;
3513
24.7M
                chunk = str;
3514
30.1M
            } else {
3515
30.1M
                str += l;
3516
30.1M
            }
3517
54.9M
        } else if (c == '&') {
3518
29.0k
            if (str[1] == '#') {
3519
16.9k
                if (chunk < str)
3520
7.13k
                    xmlSBufAddString(buf, chunk, str - chunk);
3521
3522
16.9k
                c = xmlParseStringCharRef(ctxt, &str);
3523
16.9k
                if (c == 0)
3524
4.20k
                    return;
3525
3526
12.7k
                xmlSBufAddChar(buf, c);
3527
3528
12.7k
                chunk = str;
3529
12.7k
            } else {
3530
12.0k
                xmlChar *name;
3531
3532
                /*
3533
                 * General entity references are checked for
3534
                 * syntactic validity.
3535
                 */
3536
12.0k
                str++;
3537
12.0k
                name = xmlParseStringName(ctxt, &str);
3538
3539
12.0k
                if ((name == NULL) || (*str++ != ';')) {
3540
1.09k
                    xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
3541
1.09k
                            "EntityValue: '&' forbidden except for entities "
3542
1.09k
                            "references\n");
3543
1.09k
                    xmlFree(name);
3544
1.09k
                    return;
3545
1.09k
                }
3546
3547
10.9k
                xmlFree(name);
3548
10.9k
            }
3549
14.0M
        } else if (c == '%') {
3550
1.38k
            xmlEntityPtr ent;
3551
3552
1.38k
            if (chunk < str)
3553
1.01k
                xmlSBufAddString(buf, chunk, str - chunk);
3554
3555
1.38k
            ent = xmlParseStringPEReference(ctxt, &str);
3556
1.38k
            if (ent == NULL)
3557
1.01k
                return;
3558
3559
376
            if (!PARSER_EXTERNAL(ctxt)) {
3560
376
                xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
3561
376
                return;
3562
376
            }
3563
3564
0
            if (ent->content == NULL) {
3565
                /*
3566
                 * Note: external parsed entities will not be loaded,
3567
                 * it is not required for a non-validating parser to
3568
                 * complete external PEReferences coming from the
3569
                 * internal subset
3570
                 */
3571
0
                if (((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
3572
0
                    ((ctxt->replaceEntities) ||
3573
0
                     (ctxt->validate))) {
3574
0
                    xmlLoadEntityContent(ctxt, ent);
3575
0
                } else {
3576
0
                    xmlWarningMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
3577
0
                                  "not validating will not read content for "
3578
0
                                  "PE entity %s\n", ent->name, NULL);
3579
0
                }
3580
0
            }
3581
3582
            /*
3583
             * TODO: Skip if ent->content is still NULL.
3584
             */
3585
3586
0
            if (xmlParserEntityCheck(ctxt, ent->length))
3587
0
                return;
3588
3589
0
            if (ent->flags & XML_ENT_EXPANDING) {
3590
0
                xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3591
0
                xmlHaltParser(ctxt);
3592
0
                return;
3593
0
            }
3594
3595
0
            ent->flags |= XML_ENT_EXPANDING;
3596
0
            xmlExpandPEsInEntityValue(ctxt, buf, ent->content, ent->length,
3597
0
                                      depth);
3598
0
            ent->flags &= ~XML_ENT_EXPANDING;
3599
3600
0
            chunk = str;
3601
14.0M
        } else {
3602
            /* Normal ASCII char */
3603
14.0M
            if (!IS_BYTE_CHAR(c)) {
3604
2.94M
                xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
3605
2.94M
                        "invalid character in entity value\n");
3606
2.94M
                if (chunk < str)
3607
15.3k
                    xmlSBufAddString(buf, chunk, str - chunk);
3608
2.94M
                xmlSBufAddReplChar(buf);
3609
2.94M
                str += 1;
3610
2.94M
                chunk = str;
3611
11.1M
            } else {
3612
11.1M
                str += 1;
3613
11.1M
            }
3614
14.0M
        }
3615
69.0M
    }
3616
3617
14.1k
    if (chunk < str)
3618
12.4k
        xmlSBufAddString(buf, chunk, str - chunk);
3619
14.1k
}
3620
3621
/**
3622
 * parse a value for ENTITY declarations
3623
 *
3624
 * @deprecated Internal function, don't use.
3625
 *
3626
 *     [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
3627
 *                         "'" ([^%&'] | PEReference | Reference)* "'"
3628
 *
3629
 * @param ctxt  an XML parser context
3630
 * @param orig  if non-NULL store a copy of the original entity value
3631
 * @returns the EntityValue parsed with reference substituted or NULL
3632
 */
3633
xmlChar *
3634
20.8k
xmlParseEntityValue(xmlParserCtxt *ctxt, xmlChar **orig) {
3635
20.8k
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3636
20.8k
                         XML_MAX_HUGE_LENGTH :
3637
20.8k
                         XML_MAX_TEXT_LENGTH;
3638
20.8k
    xmlSBuf buf;
3639
20.8k
    const xmlChar *start;
3640
20.8k
    int quote, length;
3641
3642
20.8k
    xmlSBufInit(&buf, maxLength);
3643
3644
20.8k
    GROW;
3645
3646
20.8k
    quote = CUR;
3647
20.8k
    if ((quote != '"') && (quote != '\'')) {
3648
0
  xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
3649
0
  return(NULL);
3650
0
    }
3651
20.8k
    CUR_PTR++;
3652
3653
20.8k
    length = 0;
3654
3655
    /*
3656
     * Copy raw content of the entity into a buffer
3657
     */
3658
101M
    while (1) {
3659
101M
        int c;
3660
3661
101M
        if (PARSER_STOPPED(ctxt))
3662
0
            goto error;
3663
3664
101M
        if (CUR_PTR >= ctxt->input->end) {
3665
34
            xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
3666
34
            goto error;
3667
34
        }
3668
3669
101M
        c = CUR;
3670
3671
101M
        if (c == 0) {
3672
11
            xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
3673
11
                    "invalid character in entity value\n");
3674
11
            goto error;
3675
11
        }
3676
101M
        if (c == quote)
3677
20.8k
            break;
3678
101M
        NEXTL(1);
3679
101M
        length += 1;
3680
3681
        /*
3682
         * TODO: Check growth threshold
3683
         */
3684
101M
        if (ctxt->input->end - CUR_PTR < 10)
3685
35.0k
            GROW;
3686
101M
    }
3687
3688
20.8k
    start = CUR_PTR - length;
3689
3690
20.8k
    if (orig != NULL) {
3691
20.8k
        *orig = xmlStrndup(start, length);
3692
20.8k
        if (*orig == NULL)
3693
0
            xmlErrMemory(ctxt);
3694
20.8k
    }
3695
3696
20.8k
    xmlExpandPEsInEntityValue(ctxt, &buf, start, length, ctxt->inputNr);
3697
3698
20.8k
    NEXTL(1);
3699
3700
20.8k
    return(xmlSBufFinish(&buf, NULL, ctxt, "entity length too long"));
3701
3702
45
error:
3703
45
    xmlSBufCleanup(&buf, ctxt, "entity length too long");
3704
45
    return(NULL);
3705
20.8k
}
3706
3707
/**
3708
 * Check an entity reference in an attribute value for validity
3709
 * without expanding it.
3710
 *
3711
 * @param ctxt  parser context
3712
 * @param pent  entity
3713
 * @param depth  nesting depth
3714
 */
3715
static void
3716
0
xmlCheckEntityInAttValue(xmlParserCtxtPtr ctxt, xmlEntityPtr pent, int depth) {
3717
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3718
0
    const xmlChar *str;
3719
0
    unsigned long expandedSize = pent->length;
3720
0
    int c, flags;
3721
3722
0
    depth += 1;
3723
0
    if (depth > maxDepth) {
3724
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3725
0
                       "Maximum entity nesting depth exceeded");
3726
0
  return;
3727
0
    }
3728
3729
0
    if (pent->flags & XML_ENT_EXPANDING) {
3730
0
        xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3731
0
        xmlHaltParser(ctxt);
3732
0
        return;
3733
0
    }
3734
3735
    /*
3736
     * If we're parsing a default attribute value in DTD content,
3737
     * the entity might reference other entities which weren't
3738
     * defined yet, so the check isn't reliable.
3739
     */
3740
0
    if (ctxt->inSubset == 0)
3741
0
        flags = XML_ENT_CHECKED | XML_ENT_VALIDATED;
3742
0
    else
3743
0
        flags = XML_ENT_VALIDATED;
3744
3745
0
    str = pent->content;
3746
0
    if (str == NULL)
3747
0
        goto done;
3748
3749
    /*
3750
     * Note that entity values are already validated. We only check
3751
     * for illegal less-than signs and compute the expanded size
3752
     * of the entity. No special handling for multi-byte characters
3753
     * is needed.
3754
     */
3755
0
    while (!PARSER_STOPPED(ctxt)) {
3756
0
        c = *str;
3757
3758
0
  if (c != '&') {
3759
0
            if (c == 0)
3760
0
                break;
3761
3762
0
            if (c == '<')
3763
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
3764
0
                        "'<' in entity '%s' is not allowed in attributes "
3765
0
                        "values\n", pent->name);
3766
3767
0
            str += 1;
3768
0
        } else if (str[1] == '#') {
3769
0
            int val;
3770
3771
0
      val = xmlParseStringCharRef(ctxt, &str);
3772
0
      if (val == 0) {
3773
0
                pent->content[0] = 0;
3774
0
                break;
3775
0
            }
3776
0
  } else {
3777
0
            xmlChar *name;
3778
0
            xmlEntityPtr ent;
3779
3780
0
      name = xmlParseStringEntityRef(ctxt, &str);
3781
0
      if (name == NULL) {
3782
0
                pent->content[0] = 0;
3783
0
                break;
3784
0
            }
3785
3786
0
            ent = xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 1);
3787
0
            xmlFree(name);
3788
3789
0
            if ((ent != NULL) &&
3790
0
                (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
3791
0
                if ((ent->flags & flags) != flags) {
3792
0
                    pent->flags |= XML_ENT_EXPANDING;
3793
0
                    xmlCheckEntityInAttValue(ctxt, ent, depth);
3794
0
                    pent->flags &= ~XML_ENT_EXPANDING;
3795
0
                }
3796
3797
0
                xmlSaturatedAdd(&expandedSize, ent->expandedSize);
3798
0
                xmlSaturatedAdd(&expandedSize, XML_ENT_FIXED_COST);
3799
0
            }
3800
0
        }
3801
0
    }
3802
3803
0
done:
3804
0
    if (ctxt->inSubset == 0)
3805
0
        pent->expandedSize = expandedSize;
3806
3807
0
    pent->flags |= flags;
3808
0
}
3809
3810
/**
3811
 * Expand general entity references in an entity or attribute value.
3812
 * Perform attribute value normalization.
3813
 *
3814
 * @param ctxt  parser context
3815
 * @param buf  string buffer
3816
 * @param str  entity or attribute value
3817
 * @param pent  entity for entity value, NULL for attribute values
3818
 * @param normalize  whether to collapse whitespace
3819
 * @param inSpace  whitespace state
3820
 * @param depth  nesting depth
3821
 * @param check  whether to check for amplification
3822
 * @returns  whether there was a normalization change
3823
 */
3824
static int
3825
xmlExpandEntityInAttValue(xmlParserCtxtPtr ctxt, xmlSBuf *buf,
3826
                          const xmlChar *str, xmlEntityPtr pent, int normalize,
3827
285k
                          int *inSpace, int depth, int check) {
3828
285k
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3829
285k
    int c, chunkSize;
3830
285k
    int normChange = 0;
3831
3832
285k
    if (str == NULL)
3833
0
        return(0);
3834
3835
285k
    depth += 1;
3836
285k
    if (depth > maxDepth) {
3837
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3838
0
                       "Maximum entity nesting depth exceeded");
3839
0
  return(0);
3840
0
    }
3841
3842
285k
    if (pent != NULL) {
3843
285k
        if (pent->flags & XML_ENT_EXPANDING) {
3844
11
            xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3845
11
            xmlHaltParser(ctxt);
3846
11
            return(0);
3847
11
        }
3848
3849
285k
        if (check) {
3850
285k
            if (xmlParserEntityCheck(ctxt, pent->length))
3851
59
                return(0);
3852
285k
        }
3853
285k
    }
3854
3855
285k
    chunkSize = 0;
3856
3857
    /*
3858
     * Note that entity values are already validated. No special
3859
     * handling for multi-byte characters is needed.
3860
     */
3861
436M
    while (!PARSER_STOPPED(ctxt)) {
3862
436M
        c = *str;
3863
3864
436M
  if (c != '&') {
3865
436M
            if (c == 0)
3866
242k
                break;
3867
3868
            /*
3869
             * If this function is called without an entity, it is used to
3870
             * expand entities in an attribute content where less-than was
3871
             * already unscaped and is allowed.
3872
             */
3873
435M
            if ((pent != NULL) && (c == '<')) {
3874
43.1k
                xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
3875
43.1k
                        "'<' in entity '%s' is not allowed in attributes "
3876
43.1k
                        "values\n", pent->name);
3877
43.1k
                break;
3878
43.1k
            }
3879
3880
435M
            if (c <= 0x20) {
3881
16.2M
                if ((normalize) && (*inSpace)) {
3882
                    /* Skip char */
3883
124k
                    if (chunkSize > 0) {
3884
17.6k
                        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3885
17.6k
                        chunkSize = 0;
3886
17.6k
                    }
3887
124k
                    normChange = 1;
3888
16.0M
                } else if (c < 0x20) {
3889
15.7M
                    if (chunkSize > 0) {
3890
487k
                        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3891
487k
                        chunkSize = 0;
3892
487k
                    }
3893
3894
15.7M
                    xmlSBufAddCString(buf, " ", 1);
3895
15.7M
                } else {
3896
342k
                    chunkSize += 1;
3897
342k
                }
3898
3899
16.2M
                *inSpace = 1;
3900
419M
            } else {
3901
419M
                chunkSize += 1;
3902
419M
                *inSpace = 0;
3903
419M
            }
3904
3905
435M
            str += 1;
3906
435M
        } else if (str[1] == '#') {
3907
161k
            int val;
3908
3909
161k
            if (chunkSize > 0) {
3910
160k
                xmlSBufAddString(buf, str - chunkSize, chunkSize);
3911
160k
                chunkSize = 0;
3912
160k
            }
3913
3914
161k
      val = xmlParseStringCharRef(ctxt, &str);
3915
161k
      if (val == 0) {
3916
7
                if (pent != NULL)
3917
7
                    pent->content[0] = 0;
3918
7
                break;
3919
7
            }
3920
3921
161k
            if (val == ' ') {
3922
70.6k
                if ((normalize) && (*inSpace))
3923
298
                    normChange = 1;
3924
70.4k
                else
3925
70.4k
                    xmlSBufAddCString(buf, " ", 1);
3926
70.6k
                *inSpace = 1;
3927
90.6k
            } else {
3928
90.6k
                xmlSBufAddChar(buf, val);
3929
90.6k
                *inSpace = 0;
3930
90.6k
            }
3931
359k
  } else {
3932
359k
            xmlChar *name;
3933
359k
            xmlEntityPtr ent;
3934
3935
359k
            if (chunkSize > 0) {
3936
276k
                xmlSBufAddString(buf, str - chunkSize, chunkSize);
3937
276k
                chunkSize = 0;
3938
276k
            }
3939
3940
359k
      name = xmlParseStringEntityRef(ctxt, &str);
3941
359k
            if (name == NULL) {
3942
6
                if (pent != NULL)
3943
6
                    pent->content[0] = 0;
3944
6
                break;
3945
6
            }
3946
3947
359k
            ent = xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 1);
3948
359k
            xmlFree(name);
3949
3950
359k
      if ((ent != NULL) &&
3951
359k
    (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
3952
107k
    if (ent->content == NULL) {
3953
0
        xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
3954
0
          "predefined entity has no content\n");
3955
0
                    break;
3956
0
                }
3957
3958
107k
                xmlSBufAddString(buf, ent->content, ent->length);
3959
3960
107k
                *inSpace = 0;
3961
252k
      } else if ((ent != NULL) && (ent->content != NULL)) {
3962
191k
                if (pent != NULL)
3963
191k
                    pent->flags |= XML_ENT_EXPANDING;
3964
191k
    normChange |= xmlExpandEntityInAttValue(ctxt, buf,
3965
191k
                        ent->content, ent, normalize, inSpace, depth, check);
3966
191k
                if (pent != NULL)
3967
191k
                    pent->flags &= ~XML_ENT_EXPANDING;
3968
191k
      }
3969
359k
        }
3970
436M
    }
3971
3972
285k
    if (chunkSize > 0)
3973
236k
        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3974
3975
285k
    return(normChange);
3976
285k
}
3977
3978
/**
3979
 * Expand general entity references in an entity or attribute value.
3980
 * Perform attribute value normalization.
3981
 *
3982
 * @param ctxt  parser context
3983
 * @param str  entity or attribute value
3984
 * @param normalize  whether to collapse whitespace
3985
 * @returns the expanded attribtue value.
3986
 */
3987
xmlChar *
3988
xmlExpandEntitiesInAttValue(xmlParserCtxt *ctxt, const xmlChar *str,
3989
0
                            int normalize) {
3990
0
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3991
0
                         XML_MAX_HUGE_LENGTH :
3992
0
                         XML_MAX_TEXT_LENGTH;
3993
0
    xmlSBuf buf;
3994
0
    int inSpace = 1;
3995
3996
0
    xmlSBufInit(&buf, maxLength);
3997
3998
0
    xmlExpandEntityInAttValue(ctxt, &buf, str, NULL, normalize, &inSpace,
3999
0
                              ctxt->inputNr, /* check */ 0);
4000
4001
0
    if ((normalize) && (inSpace) && (buf.size > 0))
4002
0
        buf.size--;
4003
4004
0
    return(xmlSBufFinish(&buf, NULL, ctxt, "AttValue length too long"));
4005
0
}
4006
4007
/**
4008
 * parse a value for an attribute.
4009
 *
4010
 * NOTE: if no normalization is needed, the routine will return pointers
4011
 * directly from the data buffer.
4012
 *
4013
 * 3.3.3 Attribute-Value Normalization:
4014
 *
4015
 * Before the value of an attribute is passed to the application or
4016
 * checked for validity, the XML processor must normalize it as follows:
4017
 *
4018
 * - a character reference is processed by appending the referenced
4019
 *   character to the attribute value
4020
 * - an entity reference is processed by recursively processing the
4021
 *   replacement text of the entity
4022
 * - a whitespace character (\#x20, \#xD, \#xA, \#x9) is processed by
4023
 *   appending \#x20 to the normalized value, except that only a single
4024
 *   \#x20 is appended for a "#xD#xA" sequence that is part of an external
4025
 *   parsed entity or the literal entity value of an internal parsed entity
4026
 * - other characters are processed by appending them to the normalized value
4027
 *
4028
 * If the declared value is not CDATA, then the XML processor must further
4029
 * process the normalized attribute value by discarding any leading and
4030
 * trailing space (\#x20) characters, and by replacing sequences of space
4031
 * (\#x20) characters by a single space (\#x20) character.
4032
 * All attributes for which no declaration has been read should be treated
4033
 * by a non-validating parser as if declared CDATA.
4034
 *
4035
 * @param ctxt  an XML parser context
4036
 * @param attlen  attribute len result
4037
 * @param outFlags  resulting XML_ATTVAL_* flags
4038
 * @param special  value from attsSpecial
4039
 * @param isNamespace  whether this is a namespace declaration
4040
 * @returns the AttValue parsed or NULL. The value has to be freed by the
4041
 *     caller if it was copied, this can be detected by val[*len] == 0.
4042
 */
4043
static xmlChar *
4044
xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *attlen, int *outFlags,
4045
651k
                         int special, int isNamespace) {
4046
651k
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4047
651k
                         XML_MAX_HUGE_LENGTH :
4048
651k
                         XML_MAX_TEXT_LENGTH;
4049
651k
    xmlSBuf buf;
4050
651k
    xmlChar *ret;
4051
651k
    int c, l, quote, entFlags, chunkSize;
4052
651k
    int inSpace = 1;
4053
651k
    int replaceEntities;
4054
651k
    int normalize = (special & XML_SPECIAL_TYPE_MASK) != 0;
4055
651k
    int attvalFlags = 0;
4056
4057
    /* Always expand namespace URIs */
4058
651k
    replaceEntities = (ctxt->replaceEntities) || (isNamespace);
4059
4060
651k
    xmlSBufInit(&buf, maxLength);
4061
4062
651k
    GROW;
4063
4064
651k
    quote = CUR;
4065
651k
    if ((quote != '"') && (quote != '\'')) {
4066
17.0k
  xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
4067
17.0k
  return(NULL);
4068
17.0k
    }
4069
634k
    NEXTL(1);
4070
4071
634k
    if (ctxt->inSubset == 0)
4072
618k
        entFlags = XML_ENT_CHECKED | XML_ENT_VALIDATED;
4073
16.5k
    else
4074
16.5k
        entFlags = XML_ENT_VALIDATED;
4075
4076
634k
    inSpace = 1;
4077
634k
    chunkSize = 0;
4078
4079
58.5M
    while (1) {
4080
58.5M
        if (PARSER_STOPPED(ctxt))
4081
70
            goto error;
4082
4083
58.5M
        if (CUR_PTR >= ctxt->input->end) {
4084
1.58k
            xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
4085
1.58k
                           "AttValue: ' expected\n");
4086
1.58k
            goto error;
4087
1.58k
        }
4088
4089
        /*
4090
         * TODO: Check growth threshold
4091
         */
4092
58.5M
        if (ctxt->input->end - CUR_PTR < 10)
4093
26.0k
            GROW;
4094
4095
58.5M
        c = CUR;
4096
4097
58.5M
        if (c >= 0x80) {
4098
36.4M
            l = xmlUTF8MultibyteLen(ctxt, CUR_PTR,
4099
36.4M
                    "invalid character in attribute value\n");
4100
36.4M
            if (l == 0) {
4101
32.5M
                if (chunkSize > 0) {
4102
112k
                    xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4103
112k
                    chunkSize = 0;
4104
112k
                }
4105
32.5M
                xmlSBufAddReplChar(&buf);
4106
32.5M
                NEXTL(1);
4107
32.5M
            } else {
4108
3.94M
                chunkSize += l;
4109
3.94M
                NEXTL(l);
4110
3.94M
            }
4111
4112
36.4M
            inSpace = 0;
4113
36.4M
        } else if (c != '&') {
4114
21.8M
            if (c > 0x20) {
4115
12.2M
                if (c == quote)
4116
625k
                    break;
4117
4118
11.6M
                if (c == '<')
4119
323k
                    xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
4120
4121
11.6M
                chunkSize += 1;
4122
11.6M
                inSpace = 0;
4123
11.6M
            } else if (!IS_BYTE_CHAR(c)) {
4124
4.62M
                xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
4125
4.62M
                        "invalid character in attribute value\n");
4126
4.62M
                if (chunkSize > 0) {
4127
42.1k
                    xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4128
42.1k
                    chunkSize = 0;
4129
42.1k
                }
4130
4.62M
                xmlSBufAddReplChar(&buf);
4131
4.62M
                inSpace = 0;
4132
4.96M
            } else {
4133
                /* Whitespace */
4134
4.96M
                if ((normalize) && (inSpace)) {
4135
                    /* Skip char */
4136
41.7k
                    if (chunkSize > 0) {
4137
12.4k
                        xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4138
12.4k
                        chunkSize = 0;
4139
12.4k
                    }
4140
41.7k
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4141
4.92M
                } else if (c < 0x20) {
4142
                    /* Convert to space */
4143
4.47M
                    if (chunkSize > 0) {
4144
61.3k
                        xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4145
61.3k
                        chunkSize = 0;
4146
61.3k
                    }
4147
4148
4.47M
                    xmlSBufAddCString(&buf, " ", 1);
4149
4.47M
                } else {
4150
447k
                    chunkSize += 1;
4151
447k
                }
4152
4153
4.96M
                inSpace = 1;
4154
4155
4.96M
                if ((c == 0xD) && (NXT(1) == 0xA))
4156
2.42k
                    CUR_PTR++;
4157
4.96M
            }
4158
4159
21.2M
            NEXTL(1);
4160
21.2M
        } else if (NXT(1) == '#') {
4161
50.1k
            int val;
4162
4163
50.1k
            if (chunkSize > 0) {
4164
23.3k
                xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4165
23.3k
                chunkSize = 0;
4166
23.3k
            }
4167
4168
50.1k
            val = xmlParseCharRef(ctxt);
4169
50.1k
            if (val == 0)
4170
7.16k
                goto error;
4171
4172
42.9k
            if ((val == '&') && (!replaceEntities)) {
4173
                /*
4174
                 * The reparsing will be done in xmlNodeParseContent()
4175
                 * called from SAX2.c
4176
                 */
4177
0
                xmlSBufAddCString(&buf, "&#38;", 5);
4178
0
                inSpace = 0;
4179
42.9k
            } else if (val == ' ') {
4180
4.33k
                if ((normalize) && (inSpace))
4181
315
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4182
4.02k
                else
4183
4.02k
                    xmlSBufAddCString(&buf, " ", 1);
4184
4.33k
                inSpace = 1;
4185
38.6k
            } else {
4186
38.6k
                xmlSBufAddChar(&buf, val);
4187
38.6k
                inSpace = 0;
4188
38.6k
            }
4189
170k
        } else {
4190
170k
            const xmlChar *name;
4191
170k
            xmlEntityPtr ent;
4192
4193
170k
            if (chunkSize > 0) {
4194
75.9k
                xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4195
75.9k
                chunkSize = 0;
4196
75.9k
            }
4197
4198
170k
            name = xmlParseEntityRefInternal(ctxt);
4199
170k
            if (name == NULL) {
4200
                /*
4201
                 * Probably a literal '&' which wasn't escaped.
4202
                 * TODO: Handle gracefully in recovery mode.
4203
                 */
4204
38.4k
                continue;
4205
38.4k
            }
4206
4207
131k
            ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 1);
4208
131k
            if (ent == NULL)
4209
32.9k
                continue;
4210
4211
98.7k
            if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
4212
4.18k
                if ((ent->content[0] == '&') && (!replaceEntities))
4213
0
                    xmlSBufAddCString(&buf, "&#38;", 5);
4214
4.18k
                else
4215
4.18k
                    xmlSBufAddString(&buf, ent->content, ent->length);
4216
4.18k
                inSpace = 0;
4217
94.5k
            } else if (replaceEntities) {
4218
94.5k
                if (xmlExpandEntityInAttValue(ctxt, &buf,
4219
94.5k
                        ent->content, ent, normalize, &inSpace, ctxt->inputNr,
4220
94.5k
                        /* check */ 1) > 0)
4221
7.00k
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4222
94.5k
            } else {
4223
0
                if ((ent->flags & entFlags) != entFlags)
4224
0
                    xmlCheckEntityInAttValue(ctxt, ent, ctxt->inputNr);
4225
4226
0
                if (xmlParserEntityCheck(ctxt, ent->expandedSize)) {
4227
0
                    ent->content[0] = 0;
4228
0
                    goto error;
4229
0
                }
4230
4231
                /*
4232
                 * Just output the reference
4233
                 */
4234
0
                xmlSBufAddCString(&buf, "&", 1);
4235
0
                xmlSBufAddString(&buf, ent->name, xmlStrlen(ent->name));
4236
0
                xmlSBufAddCString(&buf, ";", 1);
4237
4238
0
                inSpace = 0;
4239
0
            }
4240
98.7k
  }
4241
58.5M
    }
4242
4243
625k
    if ((buf.mem == NULL) && (outFlags != NULL)) {
4244
508k
        ret = (xmlChar *) CUR_PTR - chunkSize;
4245
4246
508k
        if (attlen != NULL)
4247
508k
            *attlen = chunkSize;
4248
508k
        if ((normalize) && (inSpace) && (chunkSize > 0)) {
4249
333
            attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4250
333
            *attlen -= 1;
4251
333
        }
4252
4253
        /* Report potential error */
4254
508k
        xmlSBufCleanup(&buf, ctxt, "AttValue length too long");
4255
508k
    } else {
4256
117k
        if (chunkSize > 0)
4257
77.1k
            xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4258
4259
117k
        if ((normalize) && (inSpace) && (buf.size > 0)) {
4260
3.82k
            attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4261
3.82k
            buf.size--;
4262
3.82k
        }
4263
4264
117k
        ret = xmlSBufFinish(&buf, attlen, ctxt, "AttValue length too long");
4265
117k
        attvalFlags |= XML_ATTVAL_ALLOC;
4266
4267
117k
        if (ret != NULL) {
4268
117k
            if (attlen != NULL)
4269
101k
                *attlen = buf.size;
4270
117k
        }
4271
117k
    }
4272
4273
625k
    if (outFlags != NULL)
4274
609k
        *outFlags = attvalFlags;
4275
4276
625k
    NEXTL(1);
4277
4278
625k
    return(ret);
4279
4280
8.81k
error:
4281
8.81k
    xmlSBufCleanup(&buf, ctxt, "AttValue length too long");
4282
8.81k
    return(NULL);
4283
634k
}
4284
4285
/**
4286
 * parse a value for an attribute
4287
 * Note: the parser won't do substitution of entities here, this
4288
 * will be handled later in #xmlStringGetNodeList
4289
 *
4290
 * @deprecated Internal function, don't use.
4291
 *
4292
 *     [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
4293
 *                       "'" ([^<&'] | Reference)* "'"
4294
 *
4295
 * 3.3.3 Attribute-Value Normalization:
4296
 *
4297
 * Before the value of an attribute is passed to the application or
4298
 * checked for validity, the XML processor must normalize it as follows:
4299
 *
4300
 * - a character reference is processed by appending the referenced
4301
 *   character to the attribute value
4302
 * - an entity reference is processed by recursively processing the
4303
 *   replacement text of the entity
4304
 * - a whitespace character (\#x20, \#xD, \#xA, \#x9) is processed by
4305
 *   appending \#x20 to the normalized value, except that only a single
4306
 *   \#x20 is appended for a "#xD#xA" sequence that is part of an external
4307
 *   parsed entity or the literal entity value of an internal parsed entity
4308
 * - other characters are processed by appending them to the normalized value
4309
 *
4310
 * If the declared value is not CDATA, then the XML processor must further
4311
 * process the normalized attribute value by discarding any leading and
4312
 * trailing space (\#x20) characters, and by replacing sequences of space
4313
 * (\#x20) characters by a single space (\#x20) character.
4314
 * All attributes for which no declaration has been read should be treated
4315
 * by a non-validating parser as if declared CDATA.
4316
 *
4317
 * @param ctxt  an XML parser context
4318
 * @returns the AttValue parsed or NULL. The value has to be freed by the
4319
 * caller.
4320
 */
4321
xmlChar *
4322
18.5k
xmlParseAttValue(xmlParserCtxt *ctxt) {
4323
18.5k
    if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
4324
18.5k
    return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0, 0));
4325
18.5k
}
4326
4327
/**
4328
 * parse an XML Literal
4329
 *
4330
 * @deprecated Internal function, don't use.
4331
 *
4332
 *     [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
4333
 *
4334
 * @param ctxt  an XML parser context
4335
 * @returns the SystemLiteral parsed or NULL
4336
 */
4337
4338
xmlChar *
4339
3.88k
xmlParseSystemLiteral(xmlParserCtxt *ctxt) {
4340
3.88k
    xmlChar *buf = NULL;
4341
3.88k
    int len = 0;
4342
3.88k
    int size = XML_PARSER_BUFFER_SIZE;
4343
3.88k
    int cur, l;
4344
3.88k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4345
3.88k
                    XML_MAX_TEXT_LENGTH :
4346
3.88k
                    XML_MAX_NAME_LENGTH;
4347
3.88k
    xmlChar stop;
4348
4349
3.88k
    if (RAW == '"') {
4350
1.07k
        NEXT;
4351
1.07k
  stop = '"';
4352
2.81k
    } else if (RAW == '\'') {
4353
1.94k
        NEXT;
4354
1.94k
  stop = '\'';
4355
1.94k
    } else {
4356
877
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
4357
877
  return(NULL);
4358
877
    }
4359
4360
3.01k
    buf = xmlMalloc(size);
4361
3.01k
    if (buf == NULL) {
4362
0
        xmlErrMemory(ctxt);
4363
0
  return(NULL);
4364
0
    }
4365
3.01k
    cur = xmlCurrentCharRecover(ctxt, &l);
4366
2.53M
    while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
4367
2.53M
  if (len + 5 >= size) {
4368
1.70k
      xmlChar *tmp;
4369
1.70k
            int newSize;
4370
4371
1.70k
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4372
1.70k
            if (newSize < 0) {
4373
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral");
4374
0
                xmlFree(buf);
4375
0
                return(NULL);
4376
0
            }
4377
1.70k
      tmp = xmlRealloc(buf, newSize);
4378
1.70k
      if (tmp == NULL) {
4379
0
          xmlFree(buf);
4380
0
    xmlErrMemory(ctxt);
4381
0
    return(NULL);
4382
0
      }
4383
1.70k
      buf = tmp;
4384
1.70k
            size = newSize;
4385
1.70k
  }
4386
2.53M
  COPY_BUF(buf, len, cur);
4387
2.53M
  NEXTL(l);
4388
2.53M
  cur = xmlCurrentCharRecover(ctxt, &l);
4389
2.53M
    }
4390
3.01k
    buf[len] = 0;
4391
3.01k
    if (!IS_CHAR(cur)) {
4392
361
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
4393
2.65k
    } else {
4394
2.65k
  NEXT;
4395
2.65k
    }
4396
3.01k
    return(buf);
4397
3.01k
}
4398
4399
/**
4400
 * parse an XML public literal
4401
 *
4402
 * @deprecated Internal function, don't use.
4403
 *
4404
 *     [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
4405
 *
4406
 * @param ctxt  an XML parser context
4407
 * @returns the PubidLiteral parsed or NULL.
4408
 */
4409
4410
xmlChar *
4411
5.93k
xmlParsePubidLiteral(xmlParserCtxt *ctxt) {
4412
5.93k
    xmlChar *buf = NULL;
4413
5.93k
    int len = 0;
4414
5.93k
    int size = XML_PARSER_BUFFER_SIZE;
4415
5.93k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4416
5.93k
                    XML_MAX_TEXT_LENGTH :
4417
5.93k
                    XML_MAX_NAME_LENGTH;
4418
5.93k
    xmlChar cur;
4419
5.93k
    xmlChar stop;
4420
4421
5.93k
    if (RAW == '"') {
4422
1.98k
        NEXT;
4423
1.98k
  stop = '"';
4424
3.95k
    } else if (RAW == '\'') {
4425
815
        NEXT;
4426
815
  stop = '\'';
4427
3.13k
    } else {
4428
3.13k
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
4429
3.13k
  return(NULL);
4430
3.13k
    }
4431
2.79k
    buf = xmlMalloc(size);
4432
2.79k
    if (buf == NULL) {
4433
0
  xmlErrMemory(ctxt);
4434
0
  return(NULL);
4435
0
    }
4436
2.79k
    cur = CUR;
4437
544k
    while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop) &&
4438
544k
           (PARSER_STOPPED(ctxt) == 0)) { /* checked */
4439
541k
  if (len + 1 >= size) {
4440
356
      xmlChar *tmp;
4441
356
            int newSize;
4442
4443
356
      newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4444
356
            if (newSize < 0) {
4445
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID");
4446
0
                xmlFree(buf);
4447
0
                return(NULL);
4448
0
            }
4449
356
      tmp = xmlRealloc(buf, newSize);
4450
356
      if (tmp == NULL) {
4451
0
    xmlErrMemory(ctxt);
4452
0
    xmlFree(buf);
4453
0
    return(NULL);
4454
0
      }
4455
356
      buf = tmp;
4456
356
            size = newSize;
4457
356
  }
4458
541k
  buf[len++] = cur;
4459
541k
  NEXT;
4460
541k
  cur = CUR;
4461
541k
    }
4462
2.79k
    buf[len] = 0;
4463
2.79k
    if (cur != stop) {
4464
947
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
4465
1.85k
    } else {
4466
1.85k
  NEXTL(1);
4467
1.85k
    }
4468
2.79k
    return(buf);
4469
2.79k
}
4470
4471
static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial);
4472
4473
/*
4474
 * used for the test in the inner loop of the char data testing
4475
 */
4476
static const unsigned char test_char_data[256] = {
4477
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4478
    0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */
4479
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4480
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4481
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */
4482
    0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
4483
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
4484
    0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */
4485
    0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
4486
    0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
4487
    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
4488
    0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */
4489
    0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
4490
    0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
4491
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
4492
    0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
4493
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */
4494
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4495
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4496
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4497
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4498
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4499
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4500
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4501
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4502
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4503
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4504
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4505
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4506
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4507
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4508
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4509
};
4510
4511
static void
4512
xmlCharacters(xmlParserCtxtPtr ctxt, const xmlChar *buf, int size,
4513
1.03M
              int isBlank) {
4514
1.03M
    int checkBlanks;
4515
4516
1.03M
    if ((ctxt->sax == NULL) || (ctxt->disableSAX))
4517
938k
        return;
4518
4519
95.5k
    checkBlanks = (!ctxt->keepBlanks) ||
4520
95.5k
                  (ctxt->sax->ignorableWhitespace != ctxt->sax->characters);
4521
4522
    /*
4523
     * Calling areBlanks with only parts of a text node
4524
     * is fundamentally broken, making the NOBLANKS option
4525
     * essentially unusable.
4526
     */
4527
95.5k
    if ((checkBlanks) &&
4528
95.5k
        (areBlanks(ctxt, buf, size, isBlank))) {
4529
0
        if ((ctxt->sax->ignorableWhitespace != NULL) &&
4530
0
            (ctxt->keepBlanks))
4531
0
            ctxt->sax->ignorableWhitespace(ctxt->userData, buf, size);
4532
95.5k
    } else {
4533
95.5k
        if (ctxt->sax->characters != NULL)
4534
95.5k
            ctxt->sax->characters(ctxt->userData, buf, size);
4535
4536
        /*
4537
         * The old code used to update this value for "complex" data
4538
         * even if checkBlanks was false. This was probably a bug.
4539
         */
4540
95.5k
        if ((checkBlanks) && (*ctxt->space == -1))
4541
0
            *ctxt->space = -2;
4542
95.5k
    }
4543
95.5k
}
4544
4545
/**
4546
 * Parse character data. Always makes progress if the first char isn't
4547
 * '<' or '&'.
4548
 *
4549
 * The right angle bracket (>) may be represented using the string "&gt;",
4550
 * and must, for compatibility, be escaped using "&gt;" or a character
4551
 * reference when it appears in the string "]]>" in content, when that
4552
 * string is not marking the end of a CDATA section.
4553
 *
4554
 *     [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
4555
 * @param ctxt  an XML parser context
4556
 * @param partial  buffer may contain partial UTF-8 sequences
4557
 */
4558
static void
4559
3.44M
xmlParseCharDataInternal(xmlParserCtxtPtr ctxt, int partial) {
4560
3.44M
    const xmlChar *in;
4561
3.44M
    int line = ctxt->input->line;
4562
3.44M
    int col = ctxt->input->col;
4563
3.44M
    int ccol;
4564
4565
3.44M
    GROW;
4566
    /*
4567
     * Accelerated common case where input don't need to be
4568
     * modified before passing it to the handler.
4569
     */
4570
3.44M
    in = ctxt->input->cur;
4571
3.44M
    do {
4572
3.46M
get_more_space:
4573
3.47M
        while (*in == 0x20) { in++; ctxt->input->col++; }
4574
3.46M
        if (*in == 0xA) {
4575
927k
            do {
4576
927k
                ctxt->input->line++; ctxt->input->col = 1;
4577
927k
                in++;
4578
927k
            } while (*in == 0xA);
4579
13.3k
            goto get_more_space;
4580
13.3k
        }
4581
3.44M
        if (*in == '<') {
4582
7.06k
            while (in > ctxt->input->cur) {
4583
3.53k
                const xmlChar *tmp = ctxt->input->cur;
4584
3.53k
                size_t nbchar = in - tmp;
4585
4586
3.53k
                if (nbchar > XML_MAX_ITEMS)
4587
0
                    nbchar = XML_MAX_ITEMS;
4588
3.53k
                ctxt->input->cur += nbchar;
4589
4590
3.53k
                xmlCharacters(ctxt, tmp, nbchar, 1);
4591
3.53k
            }
4592
3.53k
            return;
4593
3.53k
        }
4594
4595
3.49M
get_more:
4596
3.49M
        ccol = ctxt->input->col;
4597
11.5M
        while (test_char_data[*in]) {
4598
8.00M
            in++;
4599
8.00M
            ccol++;
4600
8.00M
        }
4601
3.49M
        ctxt->input->col = ccol;
4602
3.49M
        if (*in == 0xA) {
4603
1.32M
            do {
4604
1.32M
                ctxt->input->line++; ctxt->input->col = 1;
4605
1.32M
                in++;
4606
1.32M
            } while (*in == 0xA);
4607
18.3k
            goto get_more;
4608
18.3k
        }
4609
3.47M
        if (*in == ']') {
4610
33.1k
            if ((in[1] == ']') && (in[2] == '>')) {
4611
2.82k
                xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
4612
2.82k
                ctxt->input->cur = in + 1;
4613
2.82k
                return;
4614
2.82k
            }
4615
30.3k
            if ((!partial) || (ctxt->input->end - in >= 2)) {
4616
30.3k
                in++;
4617
30.3k
                ctxt->input->col++;
4618
30.3k
                goto get_more;
4619
30.3k
            }
4620
30.3k
        }
4621
3.93M
        while (in > ctxt->input->cur) {
4622
497k
            const xmlChar *tmp = ctxt->input->cur;
4623
497k
            size_t nbchar = in - tmp;
4624
4625
497k
            if (nbchar > XML_MAX_ITEMS)
4626
0
                nbchar = XML_MAX_ITEMS;
4627
497k
            ctxt->input->cur += nbchar;
4628
4629
497k
            xmlCharacters(ctxt, tmp, nbchar, 0);
4630
4631
497k
            line = ctxt->input->line;
4632
497k
            col = ctxt->input->col;
4633
497k
        }
4634
3.44M
        ctxt->input->cur = in;
4635
3.44M
        if (*in == 0xD) {
4636
29.3k
            in++;
4637
29.3k
            if (*in == 0xA) {
4638
3.71k
                ctxt->input->cur = in;
4639
3.71k
                in++;
4640
3.71k
                ctxt->input->line++; ctxt->input->col = 1;
4641
3.71k
                continue; /* while */
4642
3.71k
            }
4643
25.5k
            in--;
4644
25.5k
        }
4645
3.43M
        if (*in == '<') {
4646
219k
            return;
4647
219k
        }
4648
3.21M
        if (*in == '&') {
4649
52.6k
            return;
4650
52.6k
        }
4651
3.16M
        if ((partial) && (*in == ']') && (ctxt->input->end - in < 2)) {
4652
0
            return;
4653
0
        }
4654
3.16M
        SHRINK;
4655
3.16M
        GROW;
4656
3.16M
        in = ctxt->input->cur;
4657
3.16M
    } while (((*in >= 0x20) && (*in <= 0x7F)) ||
4658
3.16M
             (*in == 0x09) || (*in == 0x0a));
4659
3.16M
    ctxt->input->line = line;
4660
3.16M
    ctxt->input->col = col;
4661
3.16M
    xmlParseCharDataComplex(ctxt, partial);
4662
3.16M
}
4663
4664
/**
4665
 * Always makes progress if the first char isn't '<' or '&'.
4666
 *
4667
 * parse a CharData section.this is the fallback function
4668
 * of #xmlParseCharData when the parsing requires handling
4669
 * of non-ASCII characters.
4670
 *
4671
 * @param ctxt  an XML parser context
4672
 * @param partial  whether the input can end with truncated UTF-8
4673
 */
4674
static void
4675
3.16M
xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial) {
4676
3.16M
    xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
4677
3.16M
    int nbchar = 0;
4678
3.16M
    int cur, l;
4679
4680
3.16M
    cur = xmlCurrentCharRecover(ctxt, &l);
4681
30.9M
    while ((cur != '<') && /* checked */
4682
30.9M
           (cur != '&') &&
4683
30.9M
           ((!partial) || (cur != ']') ||
4684
30.7M
            (ctxt->input->end - ctxt->input->cur >= 2)) &&
4685
30.9M
     (IS_CHAR(cur))) {
4686
27.7M
  if ((cur == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
4687
819
      xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
4688
819
  }
4689
27.7M
  COPY_BUF(buf, nbchar, cur);
4690
  /* move current position before possible calling of ctxt->sax->characters */
4691
27.7M
  NEXTL(l);
4692
27.7M
  if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
4693
164k
      buf[nbchar] = 0;
4694
4695
164k
            xmlCharacters(ctxt, buf, nbchar, 0);
4696
164k
      nbchar = 0;
4697
164k
            SHRINK;
4698
164k
  }
4699
27.7M
  cur = xmlCurrentCharRecover(ctxt, &l);
4700
27.7M
    }
4701
3.16M
    if (nbchar != 0) {
4702
368k
        buf[nbchar] = 0;
4703
4704
368k
        xmlCharacters(ctxt, buf, nbchar, 0);
4705
368k
    }
4706
    /*
4707
     * cur == 0 can mean
4708
     *
4709
     * - End of buffer.
4710
     * - An actual 0 character.
4711
     * - An incomplete UTF-8 sequence. This is allowed if partial is set.
4712
     */
4713
3.16M
    if (ctxt->input->cur < ctxt->input->end) {
4714
3.16M
        if ((cur == 0) && (CUR != 0)) {
4715
407
            if (partial == 0) {
4716
407
                xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4717
407
                        "Incomplete UTF-8 sequence starting with %02X\n", CUR);
4718
407
                NEXTL(1);
4719
407
            }
4720
3.16M
        } else if ((cur != '<') && (cur != '&') && (cur != ']')) {
4721
            /* Generate the error and skip the offending character */
4722
2.93M
            xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4723
2.93M
                              "PCDATA invalid Char value %d\n", cur);
4724
2.93M
            NEXTL(l);
4725
2.93M
        }
4726
3.16M
    }
4727
3.16M
}
4728
4729
/**
4730
 * @deprecated Internal function, don't use.
4731
 * @param ctxt  an XML parser context
4732
 * @param cdata  unused
4733
 */
4734
void
4735
0
xmlParseCharData(xmlParserCtxt *ctxt, ATTRIBUTE_UNUSED int cdata) {
4736
0
    xmlParseCharDataInternal(ctxt, 0);
4737
0
}
4738
4739
/**
4740
 * Parse an External ID or a Public ID
4741
 *
4742
 * @deprecated Internal function, don't use.
4743
 *
4744
 * NOTE: Productions [75] and [83] interact badly since [75] can generate
4745
 * `'PUBLIC' S PubidLiteral S SystemLiteral`
4746
 *
4747
 *     [75] ExternalID ::= 'SYSTEM' S SystemLiteral
4748
 *                       | 'PUBLIC' S PubidLiteral S SystemLiteral
4749
 *
4750
 *     [83] PublicID ::= 'PUBLIC' S PubidLiteral
4751
 *
4752
 * @param ctxt  an XML parser context
4753
 * @param publicId  a xmlChar** receiving PubidLiteral
4754
 * @param strict  indicate whether we should restrict parsing to only
4755
 *          production [75], see NOTE below
4756
 * @returns the function returns SystemLiteral and in the second
4757
 *                case publicID receives PubidLiteral, is strict is off
4758
 *                it is possible to return NULL and have publicID set.
4759
 */
4760
4761
xmlChar *
4762
16.1k
xmlParseExternalID(xmlParserCtxt *ctxt, xmlChar **publicId, int strict) {
4763
16.1k
    xmlChar *URI = NULL;
4764
4765
16.1k
    *publicId = NULL;
4766
16.1k
    if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
4767
2.91k
        SKIP(6);
4768
2.91k
  if (SKIP_BLANKS == 0) {
4769
1.09k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4770
1.09k
                     "Space required after 'SYSTEM'\n");
4771
1.09k
  }
4772
2.91k
  URI = xmlParseSystemLiteral(ctxt);
4773
2.91k
  if (URI == NULL) {
4774
597
      xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
4775
597
        }
4776
13.2k
    } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
4777
5.93k
        SKIP(6);
4778
5.93k
  if (SKIP_BLANKS == 0) {
4779
4.19k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4780
4.19k
        "Space required after 'PUBLIC'\n");
4781
4.19k
  }
4782
5.93k
  *publicId = xmlParsePubidLiteral(ctxt);
4783
5.93k
  if (*publicId == NULL) {
4784
3.13k
      xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
4785
3.13k
  }
4786
5.93k
  if (strict) {
4787
      /*
4788
       * We don't handle [83] so "S SystemLiteral" is required.
4789
       */
4790
712
      if (SKIP_BLANKS == 0) {
4791
273
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4792
273
      "Space required after the Public Identifier\n");
4793
273
      }
4794
5.22k
  } else {
4795
      /*
4796
       * We handle [83] so we return immediately, if
4797
       * "S SystemLiteral" is not detected. We skip blanks if no
4798
             * system literal was found, but this is harmless since we must
4799
             * be at the end of a NotationDecl.
4800
       */
4801
5.22k
      if (SKIP_BLANKS == 0) return(NULL);
4802
1.37k
      if ((CUR != '\'') && (CUR != '"')) return(NULL);
4803
1.37k
  }
4804
971
  URI = xmlParseSystemLiteral(ctxt);
4805
971
  if (URI == NULL) {
4806
280
      xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
4807
280
        }
4808
971
    }
4809
11.1k
    return(URI);
4810
16.1k
}
4811
4812
/**
4813
 * Skip an XML (SGML) comment <!-- .... -->
4814
 *  The spec says that "For compatibility, the string "--" (double-hyphen)
4815
 *  must not occur within comments. "
4816
 * This is the slow routine in case the accelerator for ascii didn't work
4817
 *
4818
 *     [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
4819
 * @param ctxt  an XML parser context
4820
 * @param buf  the already parsed part of the buffer
4821
 * @param len  number of bytes in the buffer
4822
 * @param size  allocated size of the buffer
4823
 */
4824
static void
4825
xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
4826
36.8k
                       size_t len, size_t size) {
4827
36.8k
    int q, ql;
4828
36.8k
    int r, rl;
4829
36.8k
    int cur, l;
4830
36.8k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4831
36.8k
                    XML_MAX_HUGE_LENGTH :
4832
36.8k
                    XML_MAX_TEXT_LENGTH;
4833
4834
36.8k
    if (buf == NULL) {
4835
12.9k
        len = 0;
4836
12.9k
  size = XML_PARSER_BUFFER_SIZE;
4837
12.9k
  buf = xmlMalloc(size);
4838
12.9k
  if (buf == NULL) {
4839
0
      xmlErrMemory(ctxt);
4840
0
      return;
4841
0
  }
4842
12.9k
    }
4843
36.8k
    q = xmlCurrentCharRecover(ctxt, &ql);
4844
36.8k
    if (q == 0)
4845
1.51k
        goto not_terminated;
4846
35.3k
    if (!IS_CHAR(q)) {
4847
10.7k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4848
10.7k
                          "xmlParseComment: invalid xmlChar value %d\n",
4849
10.7k
                    q);
4850
10.7k
  xmlFree (buf);
4851
10.7k
  return;
4852
10.7k
    }
4853
24.6k
    NEXTL(ql);
4854
24.6k
    r = xmlCurrentCharRecover(ctxt, &rl);
4855
24.6k
    if (r == 0)
4856
1.54k
        goto not_terminated;
4857
23.0k
    if (!IS_CHAR(r)) {
4858
2.13k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4859
2.13k
                          "xmlParseComment: invalid xmlChar value %d\n",
4860
2.13k
                    r);
4861
2.13k
  xmlFree (buf);
4862
2.13k
  return;
4863
2.13k
    }
4864
20.9k
    NEXTL(rl);
4865
20.9k
    cur = xmlCurrentCharRecover(ctxt, &l);
4866
20.9k
    if (cur == 0)
4867
892
        goto not_terminated;
4868
1.84M
    while (IS_CHAR(cur) && /* checked */
4869
1.84M
           ((cur != '>') ||
4870
1.82M
      (r != '-') || (q != '-'))) {
4871
1.82M
  if ((r == '-') && (q == '-')) {
4872
244k
      xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
4873
244k
  }
4874
1.82M
  if (len + 5 >= size) {
4875
4.19k
      xmlChar *tmp;
4876
4.19k
            int newSize;
4877
4878
4.19k
      newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4879
4.19k
            if (newSize < 0) {
4880
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4881
0
                             "Comment too big found", NULL);
4882
0
                xmlFree (buf);
4883
0
                return;
4884
0
            }
4885
4.19k
      tmp = xmlRealloc(buf, newSize);
4886
4.19k
      if (tmp == NULL) {
4887
0
    xmlErrMemory(ctxt);
4888
0
    xmlFree(buf);
4889
0
    return;
4890
0
      }
4891
4.19k
      buf = tmp;
4892
4.19k
            size = newSize;
4893
4.19k
  }
4894
1.82M
  COPY_BUF(buf, len, q);
4895
4896
1.82M
  q = r;
4897
1.82M
  ql = rl;
4898
1.82M
  r = cur;
4899
1.82M
  rl = l;
4900
4901
1.82M
  NEXTL(l);
4902
1.82M
  cur = xmlCurrentCharRecover(ctxt, &l);
4903
4904
1.82M
    }
4905
20.0k
    buf[len] = 0;
4906
20.0k
    if (cur == 0) {
4907
4.90k
  xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4908
4.90k
                       "Comment not terminated \n<!--%.50s\n", buf);
4909
15.1k
    } else if (!IS_CHAR(cur)) {
4910
12.0k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4911
12.0k
                          "xmlParseComment: invalid xmlChar value %d\n",
4912
12.0k
                    cur);
4913
12.0k
    } else {
4914
3.06k
        NEXT;
4915
3.06k
  if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
4916
3.06k
      (!ctxt->disableSAX))
4917
1.35k
      ctxt->sax->comment(ctxt->userData, buf);
4918
3.06k
    }
4919
20.0k
    xmlFree(buf);
4920
20.0k
    return;
4921
3.94k
not_terminated:
4922
3.94k
    xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4923
3.94k
       "Comment not terminated\n", NULL);
4924
3.94k
    xmlFree(buf);
4925
3.94k
}
4926
4927
/**
4928
 * Parse an XML (SGML) comment. Always consumes '<!'.
4929
 *
4930
 * @deprecated Internal function, don't use.
4931
 *
4932
 *  The spec says that "For compatibility, the string "--" (double-hyphen)
4933
 *  must not occur within comments. "
4934
 *
4935
 *     [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
4936
 * @param ctxt  an XML parser context
4937
 */
4938
void
4939
46.1k
xmlParseComment(xmlParserCtxt *ctxt) {
4940
46.1k
    xmlChar *buf = NULL;
4941
46.1k
    size_t size = XML_PARSER_BUFFER_SIZE;
4942
46.1k
    size_t len = 0;
4943
46.1k
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4944
46.1k
                       XML_MAX_HUGE_LENGTH :
4945
46.1k
                       XML_MAX_TEXT_LENGTH;
4946
46.1k
    const xmlChar *in;
4947
46.1k
    size_t nbchar = 0;
4948
46.1k
    int ccol;
4949
4950
    /*
4951
     * Check that there is a comment right here.
4952
     */
4953
46.1k
    if ((RAW != '<') || (NXT(1) != '!'))
4954
0
        return;
4955
46.1k
    SKIP(2);
4956
46.1k
    if ((RAW != '-') || (NXT(1) != '-'))
4957
3
        return;
4958
46.1k
    SKIP(2);
4959
46.1k
    GROW;
4960
4961
    /*
4962
     * Accelerated common case where input don't need to be
4963
     * modified before passing it to the handler.
4964
     */
4965
46.1k
    in = ctxt->input->cur;
4966
46.5k
    do {
4967
46.5k
  if (*in == 0xA) {
4968
10.4k
      do {
4969
10.4k
    ctxt->input->line++; ctxt->input->col = 1;
4970
10.4k
    in++;
4971
10.4k
      } while (*in == 0xA);
4972
1.19k
  }
4973
311k
get_more:
4974
311k
        ccol = ctxt->input->col;
4975
2.88M
  while (((*in > '-') && (*in <= 0x7F)) ||
4976
2.88M
         ((*in >= 0x20) && (*in < '-')) ||
4977
2.88M
         (*in == 0x09)) {
4978
2.57M
        in++;
4979
2.57M
        ccol++;
4980
2.57M
  }
4981
311k
  ctxt->input->col = ccol;
4982
311k
  if (*in == 0xA) {
4983
78.3k
      do {
4984
78.3k
    ctxt->input->line++; ctxt->input->col = 1;
4985
78.3k
    in++;
4986
78.3k
      } while (*in == 0xA);
4987
11.8k
      goto get_more;
4988
11.8k
  }
4989
299k
  nbchar = in - ctxt->input->cur;
4990
  /*
4991
   * save current set of data
4992
   */
4993
299k
  if (nbchar > 0) {
4994
268k
            if (nbchar > maxLength - len) {
4995
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4996
0
                                  "Comment too big found", NULL);
4997
0
                xmlFree(buf);
4998
0
                return;
4999
0
            }
5000
268k
            if (buf == NULL) {
5001
27.9k
                if ((*in == '-') && (in[1] == '-'))
5002
6.05k
                    size = nbchar + 1;
5003
21.9k
                else
5004
21.9k
                    size = XML_PARSER_BUFFER_SIZE + nbchar;
5005
27.9k
                buf = xmlMalloc(size);
5006
27.9k
                if (buf == NULL) {
5007
0
                    xmlErrMemory(ctxt);
5008
0
                    return;
5009
0
                }
5010
27.9k
                len = 0;
5011
240k
            } else if (len + nbchar + 1 >= size) {
5012
3.78k
                xmlChar *new_buf;
5013
3.78k
                size += len + nbchar + XML_PARSER_BUFFER_SIZE;
5014
3.78k
                new_buf = xmlRealloc(buf, size);
5015
3.78k
                if (new_buf == NULL) {
5016
0
                    xmlErrMemory(ctxt);
5017
0
                    xmlFree(buf);
5018
0
                    return;
5019
0
                }
5020
3.78k
                buf = new_buf;
5021
3.78k
            }
5022
268k
            memcpy(&buf[len], ctxt->input->cur, nbchar);
5023
268k
            len += nbchar;
5024
268k
            buf[len] = 0;
5025
268k
  }
5026
299k
  ctxt->input->cur = in;
5027
299k
  if (*in == 0xA) {
5028
0
      in++;
5029
0
      ctxt->input->line++; ctxt->input->col = 1;
5030
0
  }
5031
299k
  if (*in == 0xD) {
5032
10.7k
      in++;
5033
10.7k
      if (*in == 0xA) {
5034
5.36k
    ctxt->input->cur = in;
5035
5.36k
    in++;
5036
5.36k
    ctxt->input->line++; ctxt->input->col = 1;
5037
5.36k
    goto get_more;
5038
5.36k
      }
5039
5.35k
      in--;
5040
5.35k
  }
5041
293k
  SHRINK;
5042
293k
  GROW;
5043
293k
  in = ctxt->input->cur;
5044
293k
  if (*in == '-') {
5045
256k
      if (in[1] == '-') {
5046
238k
          if (in[2] == '>') {
5047
9.32k
        SKIP(3);
5048
9.32k
        if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
5049
9.32k
            (!ctxt->disableSAX)) {
5050
865
      if (buf != NULL)
5051
161
          ctxt->sax->comment(ctxt->userData, buf);
5052
704
      else
5053
704
          ctxt->sax->comment(ctxt->userData, BAD_CAST "");
5054
865
        }
5055
9.32k
        if (buf != NULL)
5056
4.10k
            xmlFree(buf);
5057
9.32k
        return;
5058
9.32k
    }
5059
229k
    if (buf != NULL) {
5060
227k
        xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
5061
227k
                          "Double hyphen within comment: "
5062
227k
                                      "<!--%.50s\n",
5063
227k
              buf);
5064
227k
    } else
5065
1.69k
        xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
5066
1.69k
                          "Double hyphen within comment\n", NULL);
5067
229k
    in++;
5068
229k
    ctxt->input->col++;
5069
229k
      }
5070
247k
      in++;
5071
247k
      ctxt->input->col++;
5072
247k
      goto get_more;
5073
256k
  }
5074
293k
    } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09) || (*in == 0x0a));
5075
36.8k
    xmlParseCommentComplex(ctxt, buf, len, size);
5076
36.8k
}
5077
5078
5079
/**
5080
 * parse the name of a PI
5081
 *
5082
 * @deprecated Internal function, don't use.
5083
 *
5084
 *     [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
5085
 *
5086
 * @param ctxt  an XML parser context
5087
 * @returns the PITarget name or NULL
5088
 */
5089
5090
const xmlChar *
5091
52.5k
xmlParsePITarget(xmlParserCtxt *ctxt) {
5092
52.5k
    const xmlChar *name;
5093
5094
52.5k
    name = xmlParseName(ctxt);
5095
52.5k
    if ((name != NULL) &&
5096
52.5k
        ((name[0] == 'x') || (name[0] == 'X')) &&
5097
52.5k
        ((name[1] == 'm') || (name[1] == 'M')) &&
5098
52.5k
        ((name[2] == 'l') || (name[2] == 'L'))) {
5099
8.91k
  int i;
5100
8.91k
  if ((name[0] == 'x') && (name[1] == 'm') &&
5101
8.91k
      (name[2] == 'l') && (name[3] == 0)) {
5102
4.75k
      xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
5103
4.75k
     "XML declaration allowed only at the start of the document\n");
5104
4.75k
      return(name);
5105
4.75k
  } else if (name[3] == 0) {
5106
490
      xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
5107
490
      return(name);
5108
490
  }
5109
10.2k
  for (i = 0;;i++) {
5110
10.2k
      if (xmlW3CPIs[i] == NULL) break;
5111
7.11k
      if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
5112
512
          return(name);
5113
7.11k
  }
5114
3.15k
  xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
5115
3.15k
          "xmlParsePITarget: invalid name prefix 'xml'\n",
5116
3.15k
          NULL, NULL);
5117
3.15k
    }
5118
46.8k
    if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) {
5119
1.97k
  xmlNsErr(ctxt, XML_NS_ERR_COLON,
5120
1.97k
     "colons are forbidden from PI names '%s'\n", name, NULL, NULL);
5121
1.97k
    }
5122
46.8k
    return(name);
5123
52.5k
}
5124
5125
#ifdef LIBXML_CATALOG_ENABLED
5126
/**
5127
 * parse an XML Catalog Processing Instruction.
5128
 *
5129
 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
5130
 *
5131
 * Occurs only if allowed by the user and if happening in the Misc
5132
 * part of the document before any doctype information
5133
 * This will add the given catalog to the parsing context in order
5134
 * to be used if there is a resolution need further down in the document
5135
 *
5136
 * @param ctxt  an XML parser context
5137
 * @param catalog  the PI value string
5138
 */
5139
5140
static void
5141
0
xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
5142
0
    xmlChar *URL = NULL;
5143
0
    const xmlChar *tmp, *base;
5144
0
    xmlChar marker;
5145
5146
0
    tmp = catalog;
5147
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5148
0
    if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
5149
0
  goto error;
5150
0
    tmp += 7;
5151
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5152
0
    if (*tmp != '=') {
5153
0
  return;
5154
0
    }
5155
0
    tmp++;
5156
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5157
0
    marker = *tmp;
5158
0
    if ((marker != '\'') && (marker != '"'))
5159
0
  goto error;
5160
0
    tmp++;
5161
0
    base = tmp;
5162
0
    while ((*tmp != 0) && (*tmp != marker)) tmp++;
5163
0
    if (*tmp == 0)
5164
0
  goto error;
5165
0
    URL = xmlStrndup(base, tmp - base);
5166
0
    tmp++;
5167
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5168
0
    if (*tmp != 0)
5169
0
  goto error;
5170
5171
0
    if (URL != NULL) {
5172
        /*
5173
         * Unfortunately, the catalog API doesn't report OOM errors.
5174
         * xmlGetLastError isn't very helpful since we don't know
5175
         * where the last error came from. We'd have to reset it
5176
         * before this call and restore it afterwards.
5177
         */
5178
0
  ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
5179
0
  xmlFree(URL);
5180
0
    }
5181
0
    return;
5182
5183
0
error:
5184
0
    xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
5185
0
            "Catalog PI syntax error: %s\n",
5186
0
      catalog, NULL);
5187
0
    if (URL != NULL)
5188
0
  xmlFree(URL);
5189
0
}
5190
#endif
5191
5192
/**
5193
 * parse an XML Processing Instruction.
5194
 *
5195
 * @deprecated Internal function, don't use.
5196
 *
5197
 *     [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
5198
 *
5199
 * The processing is transferred to SAX once parsed.
5200
 *
5201
 * @param ctxt  an XML parser context
5202
 */
5203
5204
void
5205
52.5k
xmlParsePI(xmlParserCtxt *ctxt) {
5206
52.5k
    xmlChar *buf = NULL;
5207
52.5k
    size_t len = 0;
5208
52.5k
    size_t size = XML_PARSER_BUFFER_SIZE;
5209
52.5k
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
5210
52.5k
                       XML_MAX_HUGE_LENGTH :
5211
52.5k
                       XML_MAX_TEXT_LENGTH;
5212
52.5k
    int cur, l;
5213
52.5k
    const xmlChar *target;
5214
5215
52.5k
    if ((RAW == '<') && (NXT(1) == '?')) {
5216
  /*
5217
   * this is a Processing Instruction.
5218
   */
5219
52.5k
  SKIP(2);
5220
5221
  /*
5222
   * Parse the target name and check for special support like
5223
   * namespace.
5224
   */
5225
52.5k
        target = xmlParsePITarget(ctxt);
5226
52.5k
  if (target != NULL) {
5227
43.5k
      if ((RAW == '?') && (NXT(1) == '>')) {
5228
9.04k
    SKIP(2);
5229
5230
    /*
5231
     * SAX: PI detected.
5232
     */
5233
9.04k
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
5234
9.04k
        (ctxt->sax->processingInstruction != NULL))
5235
2.61k
        ctxt->sax->processingInstruction(ctxt->userData,
5236
2.61k
                                         target, NULL);
5237
9.04k
    return;
5238
9.04k
      }
5239
34.4k
      buf = xmlMalloc(size);
5240
34.4k
      if (buf == NULL) {
5241
0
    xmlErrMemory(ctxt);
5242
0
    return;
5243
0
      }
5244
34.4k
      if (SKIP_BLANKS == 0) {
5245
23.8k
    xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
5246
23.8k
        "ParsePI: PI %s space expected\n", target);
5247
23.8k
      }
5248
34.4k
      cur = xmlCurrentCharRecover(ctxt, &l);
5249
4.86M
      while (IS_CHAR(cur) && /* checked */
5250
4.86M
       ((cur != '?') || (NXT(1) != '>'))) {
5251
4.83M
    if (len + 5 >= size) {
5252
5.88k
        xmlChar *tmp;
5253
5.88k
                    int newSize;
5254
5255
5.88k
                    newSize = xmlGrowCapacity(size, 1, 1, maxLength);
5256
5.88k
                    if (newSize < 0) {
5257
0
                        xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
5258
0
                                          "PI %s too big found", target);
5259
0
                        xmlFree(buf);
5260
0
                        return;
5261
0
                    }
5262
5.88k
        tmp = xmlRealloc(buf, newSize);
5263
5.88k
        if (tmp == NULL) {
5264
0
      xmlErrMemory(ctxt);
5265
0
      xmlFree(buf);
5266
0
      return;
5267
0
        }
5268
5.88k
        buf = tmp;
5269
5.88k
                    size = newSize;
5270
5.88k
    }
5271
4.83M
    COPY_BUF(buf, len, cur);
5272
4.83M
    NEXTL(l);
5273
4.83M
    cur = xmlCurrentCharRecover(ctxt, &l);
5274
4.83M
      }
5275
34.4k
      buf[len] = 0;
5276
34.4k
      if (cur != '?') {
5277
16.2k
    xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
5278
16.2k
          "ParsePI: PI %s never end ...\n", target);
5279
18.2k
      } else {
5280
18.2k
    SKIP(2);
5281
5282
18.2k
#ifdef LIBXML_CATALOG_ENABLED
5283
18.2k
    if ((ctxt->inSubset == 0) &&
5284
18.2k
        (xmlStrEqual(target, XML_CATALOG_PI))) {
5285
2.78k
        xmlCatalogAllow allow = xmlCatalogGetDefaults();
5286
5287
2.78k
        if ((ctxt->options & XML_PARSE_CATALOG_PI) &&
5288
2.78k
                        ((allow == XML_CATA_ALLOW_DOCUMENT) ||
5289
0
       (allow == XML_CATA_ALLOW_ALL)))
5290
0
      xmlParseCatalogPI(ctxt, buf);
5291
2.78k
    }
5292
18.2k
#endif
5293
5294
    /*
5295
     * SAX: PI detected.
5296
     */
5297
18.2k
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
5298
18.2k
        (ctxt->sax->processingInstruction != NULL))
5299
443
        ctxt->sax->processingInstruction(ctxt->userData,
5300
443
                                         target, buf);
5301
18.2k
      }
5302
34.4k
      xmlFree(buf);
5303
34.4k
  } else {
5304
9.05k
      xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
5305
9.05k
  }
5306
52.5k
    }
5307
52.5k
}
5308
5309
/**
5310
 * Parse a notation declaration. Always consumes '<!'.
5311
 *
5312
 * @deprecated Internal function, don't use.
5313
 *
5314
 *     [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID |  PublicID)
5315
 *                           S? '>'
5316
 *
5317
 * Hence there is actually 3 choices:
5318
 *
5319
 *     'PUBLIC' S PubidLiteral
5320
 *     'PUBLIC' S PubidLiteral S SystemLiteral
5321
 *     'SYSTEM' S SystemLiteral
5322
 *
5323
 * See the NOTE on #xmlParseExternalID.
5324
 *
5325
 * @param ctxt  an XML parser context
5326
 */
5327
5328
void
5329
8.84k
xmlParseNotationDecl(xmlParserCtxt *ctxt) {
5330
8.84k
    const xmlChar *name;
5331
8.84k
    xmlChar *Pubid;
5332
8.84k
    xmlChar *Systemid;
5333
5334
8.84k
    if ((CUR != '<') || (NXT(1) != '!'))
5335
0
        return;
5336
8.84k
    SKIP(2);
5337
5338
8.84k
    if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
5339
8.79k
#ifdef LIBXML_VALID_ENABLED
5340
8.79k
  int oldInputNr = ctxt->inputNr;
5341
8.79k
#endif
5342
5343
8.79k
  SKIP(8);
5344
8.79k
  if (SKIP_BLANKS_PE == 0) {
5345
645
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5346
645
         "Space required after '<!NOTATION'\n");
5347
645
      return;
5348
645
  }
5349
5350
8.14k
        name = xmlParseName(ctxt);
5351
8.14k
  if (name == NULL) {
5352
248
      xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
5353
248
      return;
5354
248
  }
5355
7.90k
  if (xmlStrchr(name, ':') != NULL) {
5356
2.82k
      xmlNsErr(ctxt, XML_NS_ERR_COLON,
5357
2.82k
         "colons are forbidden from notation names '%s'\n",
5358
2.82k
         name, NULL, NULL);
5359
2.82k
  }
5360
7.90k
  if (SKIP_BLANKS_PE == 0) {
5361
1.13k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5362
1.13k
         "Space required after the NOTATION name'\n");
5363
1.13k
      return;
5364
1.13k
  }
5365
5366
  /*
5367
   * Parse the IDs.
5368
   */
5369
6.76k
  Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
5370
6.76k
  SKIP_BLANKS_PE;
5371
5372
6.76k
  if (RAW == '>') {
5373
807
#ifdef LIBXML_VALID_ENABLED
5374
807
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
5375
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5376
0
                           "Notation declaration doesn't start and stop"
5377
0
                                 " in the same entity\n",
5378
0
                                 NULL, NULL);
5379
0
      }
5380
807
#endif
5381
807
      NEXT;
5382
807
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5383
807
    (ctxt->sax->notationDecl != NULL))
5384
303
    ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
5385
5.95k
  } else {
5386
5.95k
      xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
5387
5.95k
  }
5388
6.76k
  if (Systemid != NULL) xmlFree(Systemid);
5389
6.76k
  if (Pubid != NULL) xmlFree(Pubid);
5390
6.76k
    }
5391
8.84k
}
5392
5393
/**
5394
 * Parse an entity declaration. Always consumes '<!'.
5395
 *
5396
 * @deprecated Internal function, don't use.
5397
 *
5398
 *     [70] EntityDecl ::= GEDecl | PEDecl
5399
 *
5400
 *     [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
5401
 *
5402
 *     [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
5403
 *
5404
 *     [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
5405
 *
5406
 *     [74] PEDef ::= EntityValue | ExternalID
5407
 *
5408
 *     [76] NDataDecl ::= S 'NDATA' S Name
5409
 *
5410
 * [ VC: Notation Declared ]
5411
 * The Name must match the declared name of a notation.
5412
 *
5413
 * @param ctxt  an XML parser context
5414
 */
5415
5416
void
5417
24.8k
xmlParseEntityDecl(xmlParserCtxt *ctxt) {
5418
24.8k
    const xmlChar *name = NULL;
5419
24.8k
    xmlChar *value = NULL;
5420
24.8k
    xmlChar *URI = NULL, *literal = NULL;
5421
24.8k
    const xmlChar *ndata = NULL;
5422
24.8k
    int isParameter = 0;
5423
24.8k
    xmlChar *orig = NULL;
5424
5425
24.8k
    if ((CUR != '<') || (NXT(1) != '!'))
5426
0
        return;
5427
24.8k
    SKIP(2);
5428
5429
    /* GROW; done in the caller */
5430
24.8k
    if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
5431
24.7k
#ifdef LIBXML_VALID_ENABLED
5432
24.7k
  int oldInputNr = ctxt->inputNr;
5433
24.7k
#endif
5434
5435
24.7k
  SKIP(6);
5436
24.7k
  if (SKIP_BLANKS_PE == 0) {
5437
13.4k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5438
13.4k
         "Space required after '<!ENTITY'\n");
5439
13.4k
  }
5440
5441
24.7k
  if (RAW == '%') {
5442
4.27k
      NEXT;
5443
4.27k
      if (SKIP_BLANKS_PE == 0) {
5444
297
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5445
297
             "Space required after '%%'\n");
5446
297
      }
5447
4.27k
      isParameter = 1;
5448
4.27k
  }
5449
5450
24.7k
        name = xmlParseName(ctxt);
5451
24.7k
  if (name == NULL) {
5452
427
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5453
427
                     "xmlParseEntityDecl: no name\n");
5454
427
            return;
5455
427
  }
5456
24.3k
  if (xmlStrchr(name, ':') != NULL) {
5457
850
      xmlNsErr(ctxt, XML_NS_ERR_COLON,
5458
850
         "colons are forbidden from entities names '%s'\n",
5459
850
         name, NULL, NULL);
5460
850
  }
5461
24.3k
  if (SKIP_BLANKS_PE == 0) {
5462
10.2k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5463
10.2k
         "Space required after the entity name\n");
5464
10.2k
  }
5465
5466
  /*
5467
   * handle the various case of definitions...
5468
   */
5469
24.3k
  if (isParameter) {
5470
4.22k
      if ((RAW == '"') || (RAW == '\'')) {
5471
3.04k
          value = xmlParseEntityValue(ctxt, &orig);
5472
3.04k
    if (value) {
5473
3.03k
        if ((ctxt->sax != NULL) &&
5474
3.03k
      (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5475
1.46k
      ctxt->sax->entityDecl(ctxt->userData, name,
5476
1.46k
                        XML_INTERNAL_PARAMETER_ENTITY,
5477
1.46k
            NULL, NULL, value);
5478
3.03k
    }
5479
3.04k
      } else {
5480
1.18k
          URI = xmlParseExternalID(ctxt, &literal, 1);
5481
1.18k
    if ((URI == NULL) && (literal == NULL)) {
5482
209
        xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
5483
209
    }
5484
1.18k
    if (URI) {
5485
899
                    if (xmlStrchr(URI, '#')) {
5486
87
                        xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
5487
812
                    } else {
5488
812
                        if ((ctxt->sax != NULL) &&
5489
812
                            (!ctxt->disableSAX) &&
5490
812
                            (ctxt->sax->entityDecl != NULL))
5491
438
                            ctxt->sax->entityDecl(ctxt->userData, name,
5492
438
                                        XML_EXTERNAL_PARAMETER_ENTITY,
5493
438
                                        literal, URI, NULL);
5494
812
                    }
5495
899
    }
5496
1.18k
      }
5497
20.1k
  } else {
5498
20.1k
      if ((RAW == '"') || (RAW == '\'')) {
5499
17.8k
          value = xmlParseEntityValue(ctxt, &orig);
5500
17.8k
    if ((ctxt->sax != NULL) &&
5501
17.8k
        (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5502
939
        ctxt->sax->entityDecl(ctxt->userData, name,
5503
939
        XML_INTERNAL_GENERAL_ENTITY,
5504
939
        NULL, NULL, value);
5505
    /*
5506
     * For expat compatibility in SAX mode.
5507
     */
5508
17.8k
    if ((ctxt->myDoc == NULL) ||
5509
17.8k
        (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
5510
9.63k
        if (ctxt->myDoc == NULL) {
5511
1.09k
      ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
5512
1.09k
      if (ctxt->myDoc == NULL) {
5513
0
          xmlErrMemory(ctxt);
5514
0
          goto done;
5515
0
      }
5516
1.09k
      ctxt->myDoc->properties = XML_DOC_INTERNAL;
5517
1.09k
        }
5518
9.63k
        if (ctxt->myDoc->intSubset == NULL) {
5519
1.09k
      ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
5520
1.09k
              BAD_CAST "fake", NULL, NULL);
5521
1.09k
                        if (ctxt->myDoc->intSubset == NULL) {
5522
0
                            xmlErrMemory(ctxt);
5523
0
                            goto done;
5524
0
                        }
5525
1.09k
                    }
5526
5527
9.63k
        xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
5528
9.63k
                    NULL, NULL, value);
5529
9.63k
    }
5530
17.8k
      } else {
5531
2.26k
          URI = xmlParseExternalID(ctxt, &literal, 1);
5532
2.26k
    if ((URI == NULL) && (literal == NULL)) {
5533
991
        xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
5534
991
    }
5535
2.26k
    if (URI) {
5536
1.10k
                    if (xmlStrchr(URI, '#')) {
5537
409
                        xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
5538
409
                    }
5539
1.10k
    }
5540
2.26k
    if ((RAW != '>') && (SKIP_BLANKS_PE == 0)) {
5541
433
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5542
433
           "Space required before 'NDATA'\n");
5543
433
    }
5544
2.26k
    if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
5545
355
        SKIP(5);
5546
355
        if (SKIP_BLANKS_PE == 0) {
5547
199
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5548
199
               "Space required after 'NDATA'\n");
5549
199
        }
5550
355
        ndata = xmlParseName(ctxt);
5551
355
        if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5552
355
            (ctxt->sax->unparsedEntityDecl != NULL))
5553
23
      ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
5554
23
            literal, URI, ndata);
5555
1.90k
    } else {
5556
1.90k
        if ((ctxt->sax != NULL) &&
5557
1.90k
            (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5558
78
      ctxt->sax->entityDecl(ctxt->userData, name,
5559
78
            XML_EXTERNAL_GENERAL_PARSED_ENTITY,
5560
78
            literal, URI, NULL);
5561
        /*
5562
         * For expat compatibility in SAX mode.
5563
         * assuming the entity replacement was asked for
5564
         */
5565
1.90k
        if ((ctxt->replaceEntities != 0) &&
5566
1.90k
      ((ctxt->myDoc == NULL) ||
5567
1.90k
      (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
5568
757
      if (ctxt->myDoc == NULL) {
5569
73
          ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
5570
73
          if (ctxt->myDoc == NULL) {
5571
0
              xmlErrMemory(ctxt);
5572
0
        goto done;
5573
0
          }
5574
73
          ctxt->myDoc->properties = XML_DOC_INTERNAL;
5575
73
      }
5576
5577
757
      if (ctxt->myDoc->intSubset == NULL) {
5578
73
          ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
5579
73
            BAD_CAST "fake", NULL, NULL);
5580
73
                            if (ctxt->myDoc->intSubset == NULL) {
5581
0
                                xmlErrMemory(ctxt);
5582
0
                                goto done;
5583
0
                            }
5584
73
                        }
5585
757
      xmlSAX2EntityDecl(ctxt, name,
5586
757
                  XML_EXTERNAL_GENERAL_PARSED_ENTITY,
5587
757
                  literal, URI, NULL);
5588
757
        }
5589
1.90k
    }
5590
2.26k
      }
5591
20.1k
  }
5592
24.3k
  SKIP_BLANKS_PE;
5593
24.3k
  if (RAW != '>') {
5594
680
      xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
5595
680
              "xmlParseEntityDecl: entity %s not terminated\n", name);
5596
680
      xmlHaltParser(ctxt);
5597
23.6k
  } else {
5598
23.6k
#ifdef LIBXML_VALID_ENABLED
5599
23.6k
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
5600
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5601
0
                           "Entity declaration doesn't start and stop in"
5602
0
                                 " the same entity\n",
5603
0
                                 NULL, NULL);
5604
0
      }
5605
23.6k
#endif
5606
23.6k
      NEXT;
5607
23.6k
  }
5608
24.3k
  if (orig != NULL) {
5609
      /*
5610
       * Ugly mechanism to save the raw entity value.
5611
       */
5612
20.8k
      xmlEntityPtr cur = NULL;
5613
5614
20.8k
      if (isParameter) {
5615
3.03k
          if ((ctxt->sax != NULL) &&
5616
3.03k
        (ctxt->sax->getParameterEntity != NULL))
5617
3.03k
        cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
5618
17.8k
      } else {
5619
17.8k
          if ((ctxt->sax != NULL) &&
5620
17.8k
        (ctxt->sax->getEntity != NULL))
5621
17.8k
        cur = ctxt->sax->getEntity(ctxt->userData, name);
5622
17.8k
    if ((cur == NULL) && (ctxt->userData==ctxt)) {
5623
5.03k
        cur = xmlSAX2GetEntity(ctxt, name);
5624
5.03k
    }
5625
17.8k
      }
5626
20.8k
            if ((cur != NULL) && (cur->orig == NULL)) {
5627
3.44k
    cur->orig = orig;
5628
3.44k
                orig = NULL;
5629
3.44k
      }
5630
20.8k
  }
5631
5632
24.3k
done:
5633
24.3k
  if (value != NULL) xmlFree(value);
5634
24.3k
  if (URI != NULL) xmlFree(URI);
5635
24.3k
  if (literal != NULL) xmlFree(literal);
5636
24.3k
        if (orig != NULL) xmlFree(orig);
5637
24.3k
    }
5638
24.8k
}
5639
5640
/**
5641
 * Parse an attribute default declaration
5642
 *
5643
 * @deprecated Internal function, don't use.
5644
 *
5645
 *     [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
5646
 *
5647
 * [ VC: Required Attribute ]
5648
 * if the default declaration is the keyword \#REQUIRED, then the
5649
 * attribute must be specified for all elements of the type in the
5650
 * attribute-list declaration.
5651
 *
5652
 * [ VC: Attribute Default Legal ]
5653
 * The declared default value must meet the lexical constraints of
5654
 * the declared attribute type c.f. #xmlValidateAttributeDecl
5655
 *
5656
 * [ VC: Fixed Attribute Default ]
5657
 * if an attribute has a default value declared with the \#FIXED
5658
 * keyword, instances of that attribute must match the default value.
5659
 *
5660
 * [ WFC: No < in Attribute Values ]
5661
 * handled in #xmlParseAttValue
5662
 *
5663
 * @param ctxt  an XML parser context
5664
 * @param value  Receive a possible fixed default value for the attribute
5665
 * @returns XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
5666
 *          or XML_ATTRIBUTE_FIXED.
5667
 */
5668
5669
int
5670
19.1k
xmlParseDefaultDecl(xmlParserCtxt *ctxt, xmlChar **value) {
5671
19.1k
    int val;
5672
19.1k
    xmlChar *ret;
5673
5674
19.1k
    *value = NULL;
5675
19.1k
    if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
5676
109
  SKIP(9);
5677
109
  return(XML_ATTRIBUTE_REQUIRED);
5678
109
    }
5679
19.0k
    if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
5680
504
  SKIP(8);
5681
504
  return(XML_ATTRIBUTE_IMPLIED);
5682
504
    }
5683
18.5k
    val = XML_ATTRIBUTE_NONE;
5684
18.5k
    if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
5685
967
  SKIP(6);
5686
967
  val = XML_ATTRIBUTE_FIXED;
5687
967
  if (SKIP_BLANKS_PE == 0) {
5688
770
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5689
770
         "Space required after '#FIXED'\n");
5690
770
  }
5691
967
    }
5692
18.5k
    ret = xmlParseAttValue(ctxt);
5693
18.5k
    if (ret == NULL) {
5694
2.54k
  xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
5695
2.54k
           "Attribute default value declaration error\n");
5696
2.54k
    } else
5697
15.9k
        *value = ret;
5698
18.5k
    return(val);
5699
19.0k
}
5700
5701
/**
5702
 * parse an Notation attribute type.
5703
 *
5704
 * @deprecated Internal function, don't use.
5705
 *
5706
 * Note: the leading 'NOTATION' S part has already being parsed...
5707
 *
5708
 *     [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
5709
 *
5710
 * [ VC: Notation Attributes ]
5711
 * Values of this type must match one of the notation names included
5712
 * in the declaration; all notation names in the declaration must be declared.
5713
 *
5714
 * @param ctxt  an XML parser context
5715
 * @returns the notation attribute tree built while parsing
5716
 */
5717
5718
xmlEnumeration *
5719
765
xmlParseNotationType(xmlParserCtxt *ctxt) {
5720
765
    const xmlChar *name;
5721
765
    xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
5722
5723
765
    if (RAW != '(') {
5724
334
  xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
5725
334
  return(NULL);
5726
334
    }
5727
571
    do {
5728
571
        NEXT;
5729
571
  SKIP_BLANKS_PE;
5730
571
        name = xmlParseName(ctxt);
5731
571
  if (name == NULL) {
5732
129
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5733
129
         "Name expected in NOTATION declaration\n");
5734
129
            xmlFreeEnumeration(ret);
5735
129
      return(NULL);
5736
129
  }
5737
442
        tmp = NULL;
5738
442
#ifdef LIBXML_VALID_ENABLED
5739
442
        if (ctxt->validate) {
5740
0
            tmp = ret;
5741
0
            while (tmp != NULL) {
5742
0
                if (xmlStrEqual(name, tmp->name)) {
5743
0
                    xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
5744
0
              "standalone: attribute notation value token %s duplicated\n",
5745
0
                                     name, NULL);
5746
0
                    if (!xmlDictOwns(ctxt->dict, name))
5747
0
                        xmlFree((xmlChar *) name);
5748
0
                    break;
5749
0
                }
5750
0
                tmp = tmp->next;
5751
0
            }
5752
0
        }
5753
442
#endif /* LIBXML_VALID_ENABLED */
5754
442
  if (tmp == NULL) {
5755
442
      cur = xmlCreateEnumeration(name);
5756
442
      if (cur == NULL) {
5757
0
                xmlErrMemory(ctxt);
5758
0
                xmlFreeEnumeration(ret);
5759
0
                return(NULL);
5760
0
            }
5761
442
      if (last == NULL) ret = last = cur;
5762
136
      else {
5763
136
    last->next = cur;
5764
136
    last = cur;
5765
136
      }
5766
442
  }
5767
442
  SKIP_BLANKS_PE;
5768
442
    } while (RAW == '|');
5769
302
    if (RAW != ')') {
5770
262
  xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
5771
262
        xmlFreeEnumeration(ret);
5772
262
  return(NULL);
5773
262
    }
5774
40
    NEXT;
5775
40
    return(ret);
5776
302
}
5777
5778
/**
5779
 * parse an Enumeration attribute type.
5780
 *
5781
 * @deprecated Internal function, don't use.
5782
 *
5783
 *     [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
5784
 *
5785
 * [ VC: Enumeration ]
5786
 * Values of this type must match one of the Nmtoken tokens in
5787
 * the declaration
5788
 *
5789
 * @param ctxt  an XML parser context
5790
 * @returns the enumeration attribute tree built while parsing
5791
 */
5792
5793
xmlEnumeration *
5794
4.77k
xmlParseEnumerationType(xmlParserCtxt *ctxt) {
5795
4.77k
    xmlChar *name;
5796
4.77k
    xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
5797
5798
4.77k
    if (RAW != '(') {
5799
891
  xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
5800
891
  return(NULL);
5801
891
    }
5802
9.13k
    do {
5803
9.13k
        NEXT;
5804
9.13k
  SKIP_BLANKS_PE;
5805
9.13k
        name = xmlParseNmtoken(ctxt);
5806
9.13k
  if (name == NULL) {
5807
800
      xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
5808
800
      return(ret);
5809
800
  }
5810
8.33k
        tmp = NULL;
5811
8.33k
#ifdef LIBXML_VALID_ENABLED
5812
8.33k
        if (ctxt->validate) {
5813
0
            tmp = ret;
5814
0
            while (tmp != NULL) {
5815
0
                if (xmlStrEqual(name, tmp->name)) {
5816
0
                    xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
5817
0
              "standalone: attribute enumeration value token %s duplicated\n",
5818
0
                                     name, NULL);
5819
0
                    if (!xmlDictOwns(ctxt->dict, name))
5820
0
                        xmlFree(name);
5821
0
                    break;
5822
0
                }
5823
0
                tmp = tmp->next;
5824
0
            }
5825
0
        }
5826
8.33k
#endif /* LIBXML_VALID_ENABLED */
5827
8.33k
  if (tmp == NULL) {
5828
8.33k
      cur = xmlCreateEnumeration(name);
5829
8.33k
      if (!xmlDictOwns(ctxt->dict, name))
5830
8.33k
    xmlFree(name);
5831
8.33k
      if (cur == NULL) {
5832
0
                xmlErrMemory(ctxt);
5833
0
                xmlFreeEnumeration(ret);
5834
0
                return(NULL);
5835
0
            }
5836
8.33k
      if (last == NULL) ret = last = cur;
5837
4.50k
      else {
5838
4.50k
    last->next = cur;
5839
4.50k
    last = cur;
5840
4.50k
      }
5841
8.33k
  }
5842
8.33k
  SKIP_BLANKS_PE;
5843
8.33k
    } while (RAW == '|');
5844
3.08k
    if (RAW != ')') {
5845
581
  xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
5846
581
  return(ret);
5847
581
    }
5848
2.50k
    NEXT;
5849
2.50k
    return(ret);
5850
3.08k
}
5851
5852
/**
5853
 * parse an Enumerated attribute type.
5854
 *
5855
 * @deprecated Internal function, don't use.
5856
 *
5857
 *     [57] EnumeratedType ::= NotationType | Enumeration
5858
 *
5859
 *     [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
5860
 *
5861
 * @param ctxt  an XML parser context
5862
 * @param tree  the enumeration tree built while parsing
5863
 * @returns XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
5864
 */
5865
5866
int
5867
5.74k
xmlParseEnumeratedType(xmlParserCtxt *ctxt, xmlEnumeration **tree) {
5868
5.74k
    if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
5869
969
  SKIP(8);
5870
969
  if (SKIP_BLANKS_PE == 0) {
5871
204
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5872
204
         "Space required after 'NOTATION'\n");
5873
204
      return(0);
5874
204
  }
5875
765
  *tree = xmlParseNotationType(ctxt);
5876
765
  if (*tree == NULL) return(0);
5877
40
  return(XML_ATTRIBUTE_NOTATION);
5878
765
    }
5879
4.77k
    *tree = xmlParseEnumerationType(ctxt);
5880
4.77k
    if (*tree == NULL) return(0);
5881
3.83k
    return(XML_ATTRIBUTE_ENUMERATION);
5882
4.77k
}
5883
5884
/**
5885
 * parse the Attribute list def for an element
5886
 *
5887
 * @deprecated Internal function, don't use.
5888
 *
5889
 *     [54] AttType ::= StringType | TokenizedType | EnumeratedType
5890
 *
5891
 *     [55] StringType ::= 'CDATA'
5892
 *
5893
 *     [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
5894
 *                            'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
5895
 *
5896
 * Validity constraints for attribute values syntax are checked in
5897
 * #xmlValidateAttributeValue
5898
 *
5899
 * [ VC: ID ]
5900
 * Values of type ID must match the Name production. A name must not
5901
 * appear more than once in an XML document as a value of this type;
5902
 * i.e., ID values must uniquely identify the elements which bear them.
5903
 *
5904
 * [ VC: One ID per Element Type ]
5905
 * No element type may have more than one ID attribute specified.
5906
 *
5907
 * [ VC: ID Attribute Default ]
5908
 * An ID attribute must have a declared default of \#IMPLIED or \#REQUIRED.
5909
 *
5910
 * [ VC: IDREF ]
5911
 * Values of type IDREF must match the Name production, and values
5912
 * of type IDREFS must match Names; each IDREF Name must match the value
5913
 * of an ID attribute on some element in the XML document; i.e. IDREF
5914
 * values must match the value of some ID attribute.
5915
 *
5916
 * [ VC: Entity Name ]
5917
 * Values of type ENTITY must match the Name production, values
5918
 * of type ENTITIES must match Names; each Entity Name must match the
5919
 * name of an unparsed entity declared in the DTD.
5920
 *
5921
 * [ VC: Name Token ]
5922
 * Values of type NMTOKEN must match the Nmtoken production; values
5923
 * of type NMTOKENS must match Nmtokens.
5924
 *
5925
 * @param ctxt  an XML parser context
5926
 * @param tree  the enumeration tree built while parsing
5927
 * @returns the attribute type
5928
 */
5929
int
5930
24.5k
xmlParseAttributeType(xmlParserCtxt *ctxt, xmlEnumeration **tree) {
5931
24.5k
    if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
5932
1.37k
  SKIP(5);
5933
1.37k
  return(XML_ATTRIBUTE_CDATA);
5934
23.1k
     } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
5935
484
  SKIP(6);
5936
484
  return(XML_ATTRIBUTE_IDREFS);
5937
22.7k
     } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
5938
539
  SKIP(5);
5939
539
  return(XML_ATTRIBUTE_IDREF);
5940
22.1k
     } else if ((RAW == 'I') && (NXT(1) == 'D')) {
5941
13.0k
        SKIP(2);
5942
13.0k
  return(XML_ATTRIBUTE_ID);
5943
13.0k
     } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
5944
1.27k
  SKIP(6);
5945
1.27k
  return(XML_ATTRIBUTE_ENTITY);
5946
7.86k
     } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
5947
1.40k
  SKIP(8);
5948
1.40k
  return(XML_ATTRIBUTE_ENTITIES);
5949
6.45k
     } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
5950
502
  SKIP(8);
5951
502
  return(XML_ATTRIBUTE_NMTOKENS);
5952
5.95k
     } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
5953
213
  SKIP(7);
5954
213
  return(XML_ATTRIBUTE_NMTOKEN);
5955
213
     }
5956
5.74k
     return(xmlParseEnumeratedType(ctxt, tree));
5957
24.5k
}
5958
5959
/**
5960
 * Parse an attribute list declaration for an element. Always consumes '<!'.
5961
 *
5962
 * @deprecated Internal function, don't use.
5963
 *
5964
 *     [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
5965
 *
5966
 *     [53] AttDef ::= S Name S AttType S DefaultDecl
5967
 * @param ctxt  an XML parser context
5968
 */
5969
void
5970
24.8k
xmlParseAttributeListDecl(xmlParserCtxt *ctxt) {
5971
24.8k
    const xmlChar *elemName;
5972
24.8k
    const xmlChar *attrName;
5973
24.8k
    xmlEnumerationPtr tree;
5974
5975
24.8k
    if ((CUR != '<') || (NXT(1) != '!'))
5976
0
        return;
5977
24.8k
    SKIP(2);
5978
5979
24.8k
    if (CMP7(CUR_PTR, 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
5980
24.8k
#ifdef LIBXML_VALID_ENABLED
5981
24.8k
  int oldInputNr = ctxt->inputNr;
5982
24.8k
#endif
5983
5984
24.8k
  SKIP(7);
5985
24.8k
  if (SKIP_BLANKS_PE == 0) {
5986
14.9k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5987
14.9k
                     "Space required after '<!ATTLIST'\n");
5988
14.9k
  }
5989
24.8k
        elemName = xmlParseName(ctxt);
5990
24.8k
  if (elemName == NULL) {
5991
2.28k
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5992
2.28k
         "ATTLIST: no name for Element\n");
5993
2.28k
      return;
5994
2.28k
  }
5995
22.5k
  SKIP_BLANKS_PE;
5996
22.5k
  GROW;
5997
39.2k
  while ((RAW != '>') && (PARSER_STOPPED(ctxt) == 0)) {
5998
35.1k
      int type;
5999
35.1k
      int def;
6000
35.1k
      xmlChar *defaultValue = NULL;
6001
6002
35.1k
      GROW;
6003
35.1k
            tree = NULL;
6004
35.1k
      attrName = xmlParseName(ctxt);
6005
35.1k
      if (attrName == NULL) {
6006
9.99k
    xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6007
9.99k
             "ATTLIST: no name for Attribute\n");
6008
9.99k
    break;
6009
9.99k
      }
6010
25.1k
      GROW;
6011
25.1k
      if (SKIP_BLANKS_PE == 0) {
6012
613
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6013
613
            "Space required after the attribute name\n");
6014
613
    break;
6015
613
      }
6016
6017
24.5k
      type = xmlParseAttributeType(ctxt, &tree);
6018
24.5k
      if (type <= 0) {
6019
1.86k
          break;
6020
1.86k
      }
6021
6022
22.7k
      GROW;
6023
22.7k
      if (SKIP_BLANKS_PE == 0) {
6024
3.56k
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6025
3.56k
             "Space required after the attribute type\n");
6026
3.56k
          if (tree != NULL)
6027
1.37k
        xmlFreeEnumeration(tree);
6028
3.56k
    break;
6029
3.56k
      }
6030
6031
19.1k
      def = xmlParseDefaultDecl(ctxt, &defaultValue);
6032
19.1k
      if (def <= 0) {
6033
0
                if (defaultValue != NULL)
6034
0
        xmlFree(defaultValue);
6035
0
          if (tree != NULL)
6036
0
        xmlFreeEnumeration(tree);
6037
0
          break;
6038
0
      }
6039
19.1k
      if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL))
6040
14.8k
          xmlAttrNormalizeSpace(defaultValue, defaultValue);
6041
6042
19.1k
      GROW;
6043
19.1k
            if (RAW != '>') {
6044
16.5k
    if (SKIP_BLANKS_PE == 0) {
6045
2.41k
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6046
2.41k
      "Space required after the attribute default value\n");
6047
2.41k
        if (defaultValue != NULL)
6048
113
      xmlFree(defaultValue);
6049
2.41k
        if (tree != NULL)
6050
1.01k
      xmlFreeEnumeration(tree);
6051
2.41k
        break;
6052
2.41k
    }
6053
16.5k
      }
6054
16.7k
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6055
16.7k
    (ctxt->sax->attributeDecl != NULL))
6056
9.93k
    ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
6057
9.93k
                          type, def, defaultValue, tree);
6058
6.80k
      else if (tree != NULL)
6059
717
    xmlFreeEnumeration(tree);
6060
6061
16.7k
      if ((ctxt->sax2) && (defaultValue != NULL) &&
6062
16.7k
          (def != XML_ATTRIBUTE_IMPLIED) &&
6063
16.7k
    (def != XML_ATTRIBUTE_REQUIRED)) {
6064
15.8k
    xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
6065
15.8k
      }
6066
16.7k
      if (ctxt->sax2) {
6067
16.7k
    xmlAddSpecialAttr(ctxt, elemName, attrName, type);
6068
16.7k
      }
6069
16.7k
      if (defaultValue != NULL)
6070
15.8k
          xmlFree(defaultValue);
6071
16.7k
      GROW;
6072
16.7k
  }
6073
22.5k
  if (RAW == '>') {
6074
4.14k
#ifdef LIBXML_VALID_ENABLED
6075
4.14k
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
6076
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6077
0
                                 "Attribute list declaration doesn't start and"
6078
0
                                 " stop in the same entity\n",
6079
0
                                 NULL, NULL);
6080
0
      }
6081
4.14k
#endif
6082
4.14k
      NEXT;
6083
4.14k
  }
6084
22.5k
    }
6085
24.8k
}
6086
6087
/**
6088
 * Handle PEs and check that we don't pop the entity that started
6089
 * a balanced group.
6090
 *
6091
 * @param ctxt  parser context
6092
 * @param openInputNr  input nr of the entity with opening '('
6093
 */
6094
static void
6095
488k
xmlSkipBlankCharsPEBalanced(xmlParserCtxt *ctxt, int openInputNr) {
6096
488k
    SKIP_BLANKS;
6097
488k
    GROW;
6098
6099
488k
    (void) openInputNr;
6100
6101
488k
    if (!PARSER_EXTERNAL(ctxt) && !PARSER_IN_PE(ctxt))
6102
44.8k
        return;
6103
6104
463k
    while (!PARSER_STOPPED(ctxt)) {
6105
463k
        if (ctxt->input->cur >= ctxt->input->end) {
6106
8.59k
#ifdef LIBXML_VALID_ENABLED
6107
8.59k
            if ((ctxt->validate) && (ctxt->inputNr <= openInputNr)) {
6108
0
                xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6109
0
                                 "Element content declaration doesn't start "
6110
0
                                 "and stop in the same entity\n",
6111
0
                                 NULL, NULL);
6112
0
            }
6113
8.59k
#endif
6114
8.59k
            if (PARSER_IN_PE(ctxt))
6115
8.54k
                xmlPopPE(ctxt);
6116
51
            else
6117
51
                break;
6118
455k
        } else if (RAW == '%') {
6119
11.3k
            xmlParsePERefInternal(ctxt, 0);
6120
443k
        } else {
6121
443k
            break;
6122
443k
        }
6123
6124
19.8k
        SKIP_BLANKS;
6125
19.8k
        GROW;
6126
19.8k
    }
6127
444k
}
6128
6129
/**
6130
 * parse the declaration for a Mixed Element content
6131
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6132
 *
6133
 * @deprecated Internal function, don't use.
6134
 *
6135
 *     [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
6136
 *                    '(' S? '#PCDATA' S? ')'
6137
 *
6138
 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
6139
 *
6140
 * [ VC: No Duplicate Types ]
6141
 * The same name must not appear more than once in a single
6142
 * mixed-content declaration.
6143
 *
6144
 * @param ctxt  an XML parser context
6145
 * @param openInputNr  the input used for the current entity, needed for
6146
 * boundary checks
6147
 * @returns the list of the xmlElementContent describing the element choices
6148
 */
6149
xmlElementContent *
6150
1.17k
xmlParseElementMixedContentDecl(xmlParserCtxt *ctxt, int openInputNr) {
6151
1.17k
    xmlElementContentPtr ret = NULL, cur = NULL, n;
6152
1.17k
    const xmlChar *elem = NULL;
6153
6154
1.17k
    GROW;
6155
1.17k
    if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
6156
1.17k
  SKIP(7);
6157
1.17k
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6158
1.17k
  if (RAW == ')') {
6159
296
#ifdef LIBXML_VALID_ENABLED
6160
296
      if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6161
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6162
0
                                 "Element content declaration doesn't start "
6163
0
                                 "and stop in the same entity\n",
6164
0
                                 NULL, NULL);
6165
0
      }
6166
296
#endif
6167
296
      NEXT;
6168
296
      ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
6169
296
      if (ret == NULL)
6170
0
                goto mem_error;
6171
296
      if (RAW == '*') {
6172
70
    ret->ocur = XML_ELEMENT_CONTENT_MULT;
6173
70
    NEXT;
6174
70
      }
6175
296
      return(ret);
6176
296
  }
6177
882
  if ((RAW == '(') || (RAW == '|')) {
6178
525
      ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
6179
525
      if (ret == NULL)
6180
0
                goto mem_error;
6181
525
  }
6182
2.33k
  while ((RAW == '|') && (PARSER_STOPPED(ctxt) == 0)) {
6183
1.71k
      NEXT;
6184
1.71k
            n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
6185
1.71k
            if (n == NULL)
6186
0
                goto mem_error;
6187
1.71k
      if (elem == NULL) {
6188
524
    n->c1 = cur;
6189
524
    if (cur != NULL)
6190
524
        cur->parent = n;
6191
524
    ret = cur = n;
6192
1.19k
      } else {
6193
1.19k
          cur->c2 = n;
6194
1.19k
    n->parent = cur;
6195
1.19k
    n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6196
1.19k
                if (n->c1 == NULL)
6197
0
                    goto mem_error;
6198
1.19k
    n->c1->parent = n;
6199
1.19k
    cur = n;
6200
1.19k
      }
6201
1.71k
            xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6202
1.71k
      elem = xmlParseName(ctxt);
6203
1.71k
      if (elem == NULL) {
6204
258
    xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6205
258
      "xmlParseElementMixedContentDecl : Name expected\n");
6206
258
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6207
258
    return(NULL);
6208
258
      }
6209
1.45k
            xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6210
1.45k
  }
6211
624
  if ((RAW == ')') && (NXT(1) == '*')) {
6212
208
      if (elem != NULL) {
6213
208
    cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem,
6214
208
                                   XML_ELEMENT_CONTENT_ELEMENT);
6215
208
    if (cur->c2 == NULL)
6216
0
                    goto mem_error;
6217
208
    cur->c2->parent = cur;
6218
208
            }
6219
208
            if (ret != NULL)
6220
208
                ret->ocur = XML_ELEMENT_CONTENT_MULT;
6221
208
#ifdef LIBXML_VALID_ENABLED
6222
208
      if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6223
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6224
0
                                 "Element content declaration doesn't start "
6225
0
                                 "and stop in the same entity\n",
6226
0
                                 NULL, NULL);
6227
0
      }
6228
208
#endif
6229
208
      SKIP(2);
6230
416
  } else {
6231
416
      xmlFreeDocElementContent(ctxt->myDoc, ret);
6232
416
      xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
6233
416
      return(NULL);
6234
416
  }
6235
6236
624
    } else {
6237
0
  xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
6238
0
    }
6239
208
    return(ret);
6240
6241
0
mem_error:
6242
0
    xmlErrMemory(ctxt);
6243
0
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6244
0
    return(NULL);
6245
1.17k
}
6246
6247
/**
6248
 * parse the declaration for a Mixed Element content
6249
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6250
 *
6251
 *     [47] children ::= (choice | seq) ('?' | '*' | '+')?
6252
 *
6253
 *     [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
6254
 *
6255
 *     [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
6256
 *
6257
 *     [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
6258
 *
6259
 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
6260
 * TODO Parameter-entity replacement text must be properly nested
6261
 *  with parenthesized groups. That is to say, if either of the
6262
 *  opening or closing parentheses in a choice, seq, or Mixed
6263
 *  construct is contained in the replacement text for a parameter
6264
 *  entity, both must be contained in the same replacement text. For
6265
 *  interoperability, if a parameter-entity reference appears in a
6266
 *  choice, seq, or Mixed construct, its replacement text should not
6267
 *  be empty, and neither the first nor last non-blank character of
6268
 *  the replacement text should be a connector (| or ,).
6269
 *
6270
 * @param ctxt  an XML parser context
6271
 * @param openInputNr  the input used for the current entity, needed for
6272
 * boundary checks
6273
 * @param depth  the level of recursion
6274
 * @returns the tree of xmlElementContent describing the element
6275
 *          hierarchy.
6276
 */
6277
static xmlElementContentPtr
6278
xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int openInputNr,
6279
104k
                                       int depth) {
6280
104k
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
6281
104k
    xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
6282
104k
    const xmlChar *elem;
6283
104k
    xmlChar type = 0;
6284
6285
104k
    if (depth > maxDepth) {
6286
1
        xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
6287
1
                "xmlParseElementChildrenContentDecl : depth %d too deep, "
6288
1
                "use XML_PARSE_HUGE\n", depth);
6289
1
  return(NULL);
6290
1
    }
6291
104k
    xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6292
104k
    if (RAW == '(') {
6293
30.5k
        int newInputNr = ctxt->inputNr;
6294
6295
        /* Recurse on first child */
6296
30.5k
  NEXT;
6297
30.5k
        cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, newInputNr,
6298
30.5k
                                                           depth + 1);
6299
30.5k
        if (cur == NULL)
6300
30.0k
            return(NULL);
6301
74.3k
    } else {
6302
74.3k
  elem = xmlParseName(ctxt);
6303
74.3k
  if (elem == NULL) {
6304
1.09k
      xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
6305
1.09k
      return(NULL);
6306
1.09k
  }
6307
73.2k
        cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6308
73.2k
  if (cur == NULL) {
6309
0
      xmlErrMemory(ctxt);
6310
0
      return(NULL);
6311
0
  }
6312
73.2k
  GROW;
6313
73.2k
  if (RAW == '?') {
6314
59.9k
      cur->ocur = XML_ELEMENT_CONTENT_OPT;
6315
59.9k
      NEXT;
6316
59.9k
  } else if (RAW == '*') {
6317
913
      cur->ocur = XML_ELEMENT_CONTENT_MULT;
6318
913
      NEXT;
6319
12.4k
  } else if (RAW == '+') {
6320
707
      cur->ocur = XML_ELEMENT_CONTENT_PLUS;
6321
707
      NEXT;
6322
11.7k
  } else {
6323
11.7k
      cur->ocur = XML_ELEMENT_CONTENT_ONCE;
6324
11.7k
  }
6325
73.2k
  GROW;
6326
73.2k
    }
6327
209k
    while (!PARSER_STOPPED(ctxt)) {
6328
209k
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6329
209k
        if (RAW == ')')
6330
43.7k
            break;
6331
        /*
6332
   * Each loop we parse one separator and one element.
6333
   */
6334
166k
        if (RAW == ',') {
6335
584
      if (type == 0) type = CUR;
6336
6337
      /*
6338
       * Detect "Name | Name , Name" error
6339
       */
6340
200
      else if (type != CUR) {
6341
1
    xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
6342
1
        "xmlParseElementChildrenContentDecl : '%c' expected\n",
6343
1
                      type);
6344
1
    if ((last != NULL) && (last != ret))
6345
1
        xmlFreeDocElementContent(ctxt->myDoc, last);
6346
1
    if (ret != NULL)
6347
1
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6348
1
    return(NULL);
6349
1
      }
6350
583
      NEXT;
6351
6352
583
      op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ);
6353
583
      if (op == NULL) {
6354
0
                xmlErrMemory(ctxt);
6355
0
    if ((last != NULL) && (last != ret))
6356
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6357
0
          xmlFreeDocElementContent(ctxt->myDoc, ret);
6358
0
    return(NULL);
6359
0
      }
6360
583
      if (last == NULL) {
6361
384
    op->c1 = ret;
6362
384
    if (ret != NULL)
6363
384
        ret->parent = op;
6364
384
    ret = cur = op;
6365
384
      } else {
6366
199
          cur->c2 = op;
6367
199
    if (op != NULL)
6368
199
        op->parent = cur;
6369
199
    op->c1 = last;
6370
199
    if (last != NULL)
6371
199
        last->parent = op;
6372
199
    cur =op;
6373
199
    last = NULL;
6374
199
      }
6375
165k
  } else if (RAW == '|') {
6376
157k
      if (type == 0) type = CUR;
6377
6378
      /*
6379
       * Detect "Name , Name | Name" error
6380
       */
6381
87.7k
      else if (type != CUR) {
6382
1
    xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
6383
1
        "xmlParseElementChildrenContentDecl : '%c' expected\n",
6384
1
          type);
6385
1
    if ((last != NULL) && (last != ret))
6386
1
        xmlFreeDocElementContent(ctxt->myDoc, last);
6387
1
    if (ret != NULL)
6388
1
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6389
1
    return(NULL);
6390
1
      }
6391
157k
      NEXT;
6392
6393
157k
      op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
6394
157k
      if (op == NULL) {
6395
0
                xmlErrMemory(ctxt);
6396
0
    if ((last != NULL) && (last != ret))
6397
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6398
0
    if (ret != NULL)
6399
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6400
0
    return(NULL);
6401
0
      }
6402
157k
      if (last == NULL) {
6403
69.3k
    op->c1 = ret;
6404
69.3k
    if (ret != NULL)
6405
69.3k
        ret->parent = op;
6406
69.3k
    ret = cur = op;
6407
87.7k
      } else {
6408
87.7k
          cur->c2 = op;
6409
87.7k
    if (op != NULL)
6410
87.7k
        op->parent = cur;
6411
87.7k
    op->c1 = last;
6412
87.7k
    if (last != NULL)
6413
87.7k
        last->parent = op;
6414
87.7k
    cur =op;
6415
87.7k
    last = NULL;
6416
87.7k
      }
6417
157k
  } else {
6418
8.50k
      xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
6419
8.50k
      if ((last != NULL) && (last != ret))
6420
7.55k
          xmlFreeDocElementContent(ctxt->myDoc, last);
6421
8.50k
      if (ret != NULL)
6422
8.50k
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6423
8.50k
      return(NULL);
6424
8.50k
  }
6425
157k
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6426
157k
        if (RAW == '(') {
6427
63.6k
            int newInputNr = ctxt->inputNr;
6428
6429
      /* Recurse on second child */
6430
63.6k
      NEXT;
6431
63.6k
      last = xmlParseElementChildrenContentDeclPriv(ctxt, newInputNr,
6432
63.6k
                                                          depth + 1);
6433
63.6k
            if (last == NULL) {
6434
20.9k
    if (ret != NULL)
6435
20.9k
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6436
20.9k
    return(NULL);
6437
20.9k
            }
6438
94.0k
  } else {
6439
94.0k
      elem = xmlParseName(ctxt);
6440
94.0k
      if (elem == NULL) {
6441
569
    xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
6442
569
    if (ret != NULL)
6443
569
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6444
569
    return(NULL);
6445
569
      }
6446
93.5k
      last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6447
93.5k
      if (last == NULL) {
6448
0
                xmlErrMemory(ctxt);
6449
0
    if (ret != NULL)
6450
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6451
0
    return(NULL);
6452
0
      }
6453
93.5k
      if (RAW == '?') {
6454
26.8k
    last->ocur = XML_ELEMENT_CONTENT_OPT;
6455
26.8k
    NEXT;
6456
66.6k
      } else if (RAW == '*') {
6457
19.1k
    last->ocur = XML_ELEMENT_CONTENT_MULT;
6458
19.1k
    NEXT;
6459
47.4k
      } else if (RAW == '+') {
6460
79
    last->ocur = XML_ELEMENT_CONTENT_PLUS;
6461
79
    NEXT;
6462
47.3k
      } else {
6463
47.3k
    last->ocur = XML_ELEMENT_CONTENT_ONCE;
6464
47.3k
      }
6465
93.5k
  }
6466
157k
    }
6467
43.7k
    if ((cur != NULL) && (last != NULL)) {
6468
40.6k
        cur->c2 = last;
6469
40.6k
  if (last != NULL)
6470
40.6k
      last->parent = cur;
6471
40.6k
    }
6472
43.7k
#ifdef LIBXML_VALID_ENABLED
6473
43.7k
    if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6474
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6475
0
                         "Element content declaration doesn't start "
6476
0
                         "and stop in the same entity\n",
6477
0
                         NULL, NULL);
6478
0
    }
6479
43.7k
#endif
6480
43.7k
    NEXT;
6481
43.7k
    if (RAW == '?') {
6482
1.34k
  if (ret != NULL) {
6483
1.34k
      if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
6484
1.34k
          (ret->ocur == XML_ELEMENT_CONTENT_MULT))
6485
1.07k
          ret->ocur = XML_ELEMENT_CONTENT_MULT;
6486
271
      else
6487
271
          ret->ocur = XML_ELEMENT_CONTENT_OPT;
6488
1.34k
  }
6489
1.34k
  NEXT;
6490
42.3k
    } else if (RAW == '*') {
6491
38.4k
  if (ret != NULL) {
6492
38.4k
      ret->ocur = XML_ELEMENT_CONTENT_MULT;
6493
38.4k
      cur = ret;
6494
      /*
6495
       * Some normalization:
6496
       * (a | b* | c?)* == (a | b | c)*
6497
       */
6498
115k
      while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
6499
76.7k
    if ((cur->c1 != NULL) &&
6500
76.7k
              ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
6501
76.7k
         (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
6502
51.2k
        cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
6503
76.7k
    if ((cur->c2 != NULL) &&
6504
76.7k
              ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
6505
76.7k
         (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
6506
38.1k
        cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
6507
76.7k
    cur = cur->c2;
6508
76.7k
      }
6509
38.4k
  }
6510
38.4k
  NEXT;
6511
38.4k
    } else if (RAW == '+') {
6512
1.74k
  if (ret != NULL) {
6513
1.74k
      int found = 0;
6514
6515
1.74k
      if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
6516
1.74k
          (ret->ocur == XML_ELEMENT_CONTENT_MULT))
6517
719
          ret->ocur = XML_ELEMENT_CONTENT_MULT;
6518
1.03k
      else
6519
1.03k
          ret->ocur = XML_ELEMENT_CONTENT_PLUS;
6520
      /*
6521
       * Some normalization:
6522
       * (a | b*)+ == (a | b)*
6523
       * (a | b?)+ == (a | b)*
6524
       */
6525
4.57k
      while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
6526
2.83k
    if ((cur->c1 != NULL) &&
6527
2.83k
              ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
6528
2.83k
         (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
6529
756
        cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
6530
756
        found = 1;
6531
756
    }
6532
2.83k
    if ((cur->c2 != NULL) &&
6533
2.83k
              ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
6534
2.83k
         (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
6535
877
        cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
6536
877
        found = 1;
6537
877
    }
6538
2.83k
    cur = cur->c2;
6539
2.83k
      }
6540
1.74k
      if (found)
6541
905
    ret->ocur = XML_ELEMENT_CONTENT_MULT;
6542
1.74k
  }
6543
1.74k
  NEXT;
6544
1.74k
    }
6545
43.7k
    return(ret);
6546
73.7k
}
6547
6548
/**
6549
 * parse the declaration for a Mixed Element content
6550
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6551
 *
6552
 * @deprecated Internal function, don't use.
6553
 *
6554
 *     [47] children ::= (choice | seq) ('?' | '*' | '+')?
6555
 *
6556
 *     [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
6557
 *
6558
 *     [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
6559
 *
6560
 *     [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
6561
 *
6562
 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
6563
 * TODO Parameter-entity replacement text must be properly nested
6564
 *  with parenthesized groups. That is to say, if either of the
6565
 *  opening or closing parentheses in a choice, seq, or Mixed
6566
 *  construct is contained in the replacement text for a parameter
6567
 *  entity, both must be contained in the same replacement text. For
6568
 *  interoperability, if a parameter-entity reference appears in a
6569
 *  choice, seq, or Mixed construct, its replacement text should not
6570
 *  be empty, and neither the first nor last non-blank character of
6571
 *  the replacement text should be a connector (| or ,).
6572
 *
6573
 * @param ctxt  an XML parser context
6574
 * @param inputchk  the input used for the current entity, needed for boundary checks
6575
 * @returns the tree of xmlElementContent describing the element
6576
 *          hierarchy.
6577
 */
6578
xmlElementContent *
6579
0
xmlParseElementChildrenContentDecl(xmlParserCtxt *ctxt, int inputchk) {
6580
    /* stub left for API/ABI compat */
6581
0
    return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1));
6582
0
}
6583
6584
/**
6585
 * parse the declaration for an Element content either Mixed or Children,
6586
 * the cases EMPTY and ANY are handled directly in #xmlParseElementDecl
6587
 *
6588
 * @deprecated Internal function, don't use.
6589
 *
6590
 *     [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
6591
 *
6592
 * @param ctxt  an XML parser context
6593
 * @param name  the name of the element being defined.
6594
 * @param result  the Element Content pointer will be stored here if any
6595
 * @returns an xmlElementTypeVal value or -1 on error
6596
 */
6597
6598
int
6599
xmlParseElementContentDecl(xmlParserCtxt *ctxt, const xmlChar *name,
6600
11.9k
                           xmlElementContent **result) {
6601
6602
11.9k
    xmlElementContentPtr tree = NULL;
6603
11.9k
    int openInputNr = ctxt->inputNr;
6604
11.9k
    int res;
6605
6606
11.9k
    *result = NULL;
6607
6608
11.9k
    if (RAW != '(') {
6609
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
6610
0
    "xmlParseElementContentDecl : %s '(' expected\n", name);
6611
0
  return(-1);
6612
0
    }
6613
11.9k
    NEXT;
6614
11.9k
    xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6615
11.9k
    if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
6616
1.17k
        tree = xmlParseElementMixedContentDecl(ctxt, openInputNr);
6617
1.17k
  res = XML_ELEMENT_TYPE_MIXED;
6618
10.7k
    } else {
6619
10.7k
        tree = xmlParseElementChildrenContentDeclPriv(ctxt, openInputNr, 1);
6620
10.7k
  res = XML_ELEMENT_TYPE_ELEMENT;
6621
10.7k
    }
6622
11.9k
    if (tree == NULL)
6623
10.8k
        return(-1);
6624
1.07k
    SKIP_BLANKS_PE;
6625
1.07k
    *result = tree;
6626
1.07k
    return(res);
6627
11.9k
}
6628
6629
/**
6630
 * Parse an element declaration. Always consumes '<!'.
6631
 *
6632
 * @deprecated Internal function, don't use.
6633
 *
6634
 *     [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
6635
 *
6636
 * [ VC: Unique Element Type Declaration ]
6637
 * No element type may be declared more than once
6638
 *
6639
 * @param ctxt  an XML parser context
6640
 * @returns the type of the element, or -1 in case of error
6641
 */
6642
int
6643
20.5k
xmlParseElementDecl(xmlParserCtxt *ctxt) {
6644
20.5k
    const xmlChar *name;
6645
20.5k
    int ret = -1;
6646
20.5k
    xmlElementContentPtr content  = NULL;
6647
6648
20.5k
    if ((CUR != '<') || (NXT(1) != '!'))
6649
0
        return(ret);
6650
20.5k
    SKIP(2);
6651
6652
    /* GROW; done in the caller */
6653
20.5k
    if (CMP7(CUR_PTR, 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
6654
20.5k
#ifdef LIBXML_VALID_ENABLED
6655
20.5k
  int oldInputNr = ctxt->inputNr;
6656
20.5k
#endif
6657
6658
20.5k
  SKIP(7);
6659
20.5k
  if (SKIP_BLANKS_PE == 0) {
6660
6.94k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6661
6.94k
               "Space required after 'ELEMENT'\n");
6662
6.94k
      return(-1);
6663
6.94k
  }
6664
13.6k
        name = xmlParseName(ctxt);
6665
13.6k
  if (name == NULL) {
6666
264
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6667
264
         "xmlParseElementDecl: no name for Element\n");
6668
264
      return(-1);
6669
264
  }
6670
13.3k
  if (SKIP_BLANKS_PE == 0) {
6671
11.6k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6672
11.6k
         "Space required after the element name\n");
6673
11.6k
  }
6674
13.3k
  if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
6675
331
      SKIP(5);
6676
      /*
6677
       * Element must always be empty.
6678
       */
6679
331
      ret = XML_ELEMENT_TYPE_EMPTY;
6680
13.0k
  } else if ((RAW == 'A') && (NXT(1) == 'N') &&
6681
13.0k
             (NXT(2) == 'Y')) {
6682
601
      SKIP(3);
6683
      /*
6684
       * Element is a generic container.
6685
       */
6686
601
      ret = XML_ELEMENT_TYPE_ANY;
6687
12.4k
  } else if (RAW == '(') {
6688
11.9k
      ret = xmlParseElementContentDecl(ctxt, name, &content);
6689
11.9k
            if (ret <= 0)
6690
10.8k
                return(-1);
6691
11.9k
  } else {
6692
      /*
6693
       * [ WFC: PEs in Internal Subset ] error handling.
6694
       */
6695
517
            xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
6696
517
                  "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
6697
517
      return(-1);
6698
517
  }
6699
6700
2.00k
  SKIP_BLANKS_PE;
6701
6702
2.00k
  if (RAW != '>') {
6703
1.28k
      xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
6704
1.28k
      if (content != NULL) {
6705
722
    xmlFreeDocElementContent(ctxt->myDoc, content);
6706
722
      }
6707
1.28k
  } else {
6708
718
#ifdef LIBXML_VALID_ENABLED
6709
718
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
6710
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6711
0
                                 "Element declaration doesn't start and stop in"
6712
0
                                 " the same entity\n",
6713
0
                                 NULL, NULL);
6714
0
      }
6715
718
#endif
6716
6717
718
      NEXT;
6718
718
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6719
718
    (ctxt->sax->elementDecl != NULL)) {
6720
479
    if (content != NULL)
6721
300
        content->parent = NULL;
6722
479
          ctxt->sax->elementDecl(ctxt->userData, name, ret,
6723
479
                           content);
6724
479
    if ((content != NULL) && (content->parent == NULL)) {
6725
        /*
6726
         * this is a trick: if xmlAddElementDecl is called,
6727
         * instead of copying the full tree it is plugged directly
6728
         * if called from the parser. Avoid duplicating the
6729
         * interfaces or change the API/ABI
6730
         */
6731
154
        xmlFreeDocElementContent(ctxt->myDoc, content);
6732
154
    }
6733
479
      } else if (content != NULL) {
6734
48
    xmlFreeDocElementContent(ctxt->myDoc, content);
6735
48
      }
6736
718
  }
6737
2.00k
    }
6738
2.03k
    return(ret);
6739
20.5k
}
6740
6741
/**
6742
 * Parse a conditional section. Always consumes '<!['.
6743
 *
6744
 *     [61] conditionalSect ::= includeSect | ignoreSect
6745
 *     [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
6746
 *     [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
6747
 *     [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>'
6748
 *                                 Ignore)*
6749
 *     [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
6750
 * @param ctxt  an XML parser context
6751
 */
6752
6753
static void
6754
0
xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
6755
0
    size_t depth = 0;
6756
0
    int isFreshPE = 0;
6757
0
    int oldInputNr = ctxt->inputNr;
6758
0
    int declInputNr = ctxt->inputNr;
6759
6760
0
    while (!PARSER_STOPPED(ctxt)) {
6761
0
        if (ctxt->input->cur >= ctxt->input->end) {
6762
0
            if (ctxt->inputNr <= oldInputNr) {
6763
0
                xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
6764
0
                return;
6765
0
            }
6766
6767
0
            xmlPopPE(ctxt);
6768
0
            declInputNr = ctxt->inputNr;
6769
0
        } else if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
6770
0
            SKIP(3);
6771
0
            SKIP_BLANKS_PE;
6772
6773
0
            isFreshPE = 0;
6774
6775
0
            if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
6776
0
                SKIP(7);
6777
0
                SKIP_BLANKS_PE;
6778
0
                if (RAW != '[') {
6779
0
                    xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
6780
0
                    return;
6781
0
                }
6782
0
#ifdef LIBXML_VALID_ENABLED
6783
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6784
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6785
0
                                     "All markup of the conditional section is"
6786
0
                                     " not in the same entity\n",
6787
0
                                     NULL, NULL);
6788
0
                }
6789
0
#endif
6790
0
                NEXT;
6791
6792
0
                depth++;
6793
0
            } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
6794
0
                size_t ignoreDepth = 0;
6795
6796
0
                SKIP(6);
6797
0
                SKIP_BLANKS_PE;
6798
0
                if (RAW != '[') {
6799
0
                    xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
6800
0
                    return;
6801
0
                }
6802
0
#ifdef LIBXML_VALID_ENABLED
6803
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6804
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6805
0
                                     "All markup of the conditional section is"
6806
0
                                     " not in the same entity\n",
6807
0
                                     NULL, NULL);
6808
0
                }
6809
0
#endif
6810
0
                NEXT;
6811
6812
0
                while (PARSER_STOPPED(ctxt) == 0) {
6813
0
                    if (RAW == 0) {
6814
0
                        xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
6815
0
                        return;
6816
0
                    }
6817
0
                    if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
6818
0
                        SKIP(3);
6819
0
                        ignoreDepth++;
6820
                        /* Check for integer overflow */
6821
0
                        if (ignoreDepth == 0) {
6822
0
                            xmlErrMemory(ctxt);
6823
0
                            return;
6824
0
                        }
6825
0
                    } else if ((RAW == ']') && (NXT(1) == ']') &&
6826
0
                               (NXT(2) == '>')) {
6827
0
                        SKIP(3);
6828
0
                        if (ignoreDepth == 0)
6829
0
                            break;
6830
0
                        ignoreDepth--;
6831
0
                    } else {
6832
0
                        NEXT;
6833
0
                    }
6834
0
                }
6835
6836
0
#ifdef LIBXML_VALID_ENABLED
6837
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6838
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6839
0
                                     "All markup of the conditional section is"
6840
0
                                     " not in the same entity\n",
6841
0
                                     NULL, NULL);
6842
0
                }
6843
0
#endif
6844
0
            } else {
6845
0
                xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
6846
0
                return;
6847
0
            }
6848
0
        } else if ((depth > 0) &&
6849
0
                   (RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
6850
0
            if (isFreshPE) {
6851
0
                xmlFatalErrMsg(ctxt, XML_ERR_CONDSEC_INVALID,
6852
0
                               "Parameter entity must match "
6853
0
                               "extSubsetDecl\n");
6854
0
                return;
6855
0
            }
6856
6857
0
            depth--;
6858
0
#ifdef LIBXML_VALID_ENABLED
6859
0
            if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6860
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6861
0
                                 "All markup of the conditional section is not"
6862
0
                                 " in the same entity\n",
6863
0
                                 NULL, NULL);
6864
0
            }
6865
0
#endif
6866
0
            SKIP(3);
6867
0
        } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
6868
0
            isFreshPE = 0;
6869
0
            xmlParseMarkupDecl(ctxt);
6870
0
        } else if (RAW == '%') {
6871
0
            xmlParsePERefInternal(ctxt, 1);
6872
0
            if (ctxt->inputNr > declInputNr) {
6873
0
                isFreshPE = 1;
6874
0
                declInputNr = ctxt->inputNr;
6875
0
            }
6876
0
        } else {
6877
0
            xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
6878
0
            return;
6879
0
        }
6880
6881
0
        if (depth == 0)
6882
0
            break;
6883
6884
0
        SKIP_BLANKS;
6885
0
        SHRINK;
6886
0
        GROW;
6887
0
    }
6888
0
}
6889
6890
/**
6891
 * Parse markup declarations. Always consumes '<!' or '<?'.
6892
 *
6893
 * @deprecated Internal function, don't use.
6894
 *
6895
 *     [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
6896
 *                         NotationDecl | PI | Comment
6897
 *
6898
 * [ VC: Proper Declaration/PE Nesting ]
6899
 * Parameter-entity replacement text must be properly nested with
6900
 * markup declarations. That is to say, if either the first character
6901
 * or the last character of a markup declaration (markupdecl above) is
6902
 * contained in the replacement text for a parameter-entity reference,
6903
 * both must be contained in the same replacement text.
6904
 *
6905
 * [ WFC: PEs in Internal Subset ]
6906
 * In the internal DTD subset, parameter-entity references can occur
6907
 * only where markup declarations can occur, not within markup declarations.
6908
 * (This does not apply to references that occur in external parameter
6909
 * entities or to the external subset.)
6910
 *
6911
 * @param ctxt  an XML parser context
6912
 */
6913
void
6914
106k
xmlParseMarkupDecl(xmlParserCtxt *ctxt) {
6915
106k
    GROW;
6916
106k
    if (CUR == '<') {
6917
106k
        if (NXT(1) == '!') {
6918
84.2k
      switch (NXT(2)) {
6919
45.4k
          case 'E':
6920
45.4k
        if (NXT(3) == 'L')
6921
20.5k
      xmlParseElementDecl(ctxt);
6922
24.8k
        else if (NXT(3) == 'N')
6923
24.8k
      xmlParseEntityDecl(ctxt);
6924
16
                    else
6925
16
                        SKIP(2);
6926
45.4k
        break;
6927
24.8k
          case 'A':
6928
24.8k
        xmlParseAttributeListDecl(ctxt);
6929
24.8k
        break;
6930
8.84k
          case 'N':
6931
8.84k
        xmlParseNotationDecl(ctxt);
6932
8.84k
        break;
6933
2.88k
          case '-':
6934
2.88k
        xmlParseComment(ctxt);
6935
2.88k
        break;
6936
2.27k
    default:
6937
2.27k
                    xmlFatalErr(ctxt,
6938
2.27k
                                ctxt->inSubset == 2 ?
6939
0
                                    XML_ERR_EXT_SUBSET_NOT_FINISHED :
6940
2.27k
                                    XML_ERR_INT_SUBSET_NOT_FINISHED,
6941
2.27k
                                NULL);
6942
2.27k
                    SKIP(2);
6943
2.27k
        break;
6944
84.2k
      }
6945
84.2k
  } else if (NXT(1) == '?') {
6946
22.5k
      xmlParsePI(ctxt);
6947
22.5k
  }
6948
106k
    }
6949
106k
}
6950
6951
/**
6952
 * parse an XML declaration header for external entities
6953
 *
6954
 * @deprecated Internal function, don't use.
6955
 *
6956
 *     [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
6957
 * @param ctxt  an XML parser context
6958
 */
6959
6960
void
6961
0
xmlParseTextDecl(xmlParserCtxt *ctxt) {
6962
0
    xmlChar *version;
6963
6964
    /*
6965
     * We know that '<?xml' is here.
6966
     */
6967
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
6968
0
  SKIP(5);
6969
0
    } else {
6970
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
6971
0
  return;
6972
0
    }
6973
6974
0
    if (SKIP_BLANKS == 0) {
6975
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6976
0
           "Space needed after '<?xml'\n");
6977
0
    }
6978
6979
    /*
6980
     * We may have the VersionInfo here.
6981
     */
6982
0
    version = xmlParseVersionInfo(ctxt);
6983
0
    if (version == NULL) {
6984
0
  version = xmlCharStrdup(XML_DEFAULT_VERSION);
6985
0
        if (version == NULL) {
6986
0
            xmlErrMemory(ctxt);
6987
0
            return;
6988
0
        }
6989
0
    } else {
6990
0
  if (SKIP_BLANKS == 0) {
6991
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6992
0
               "Space needed here\n");
6993
0
  }
6994
0
    }
6995
0
    ctxt->input->version = version;
6996
6997
    /*
6998
     * We must have the encoding declaration
6999
     */
7000
0
    xmlParseEncodingDecl(ctxt);
7001
7002
0
    SKIP_BLANKS;
7003
0
    if ((RAW == '?') && (NXT(1) == '>')) {
7004
0
        SKIP(2);
7005
0
    } else if (RAW == '>') {
7006
        /* Deprecated old WD ... */
7007
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
7008
0
  NEXT;
7009
0
    } else {
7010
0
        int c;
7011
7012
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
7013
0
        while ((PARSER_STOPPED(ctxt) == 0) && ((c = CUR) != 0)) {
7014
0
            NEXT;
7015
0
            if (c == '>')
7016
0
                break;
7017
0
        }
7018
0
    }
7019
0
}
7020
7021
/**
7022
 * parse Markup declarations from an external subset
7023
 *
7024
 * @deprecated Internal function, don't use.
7025
 *
7026
 *     [30] extSubset ::= textDecl? extSubsetDecl
7027
 *
7028
 *     [31] extSubsetDecl ::= (markupdecl | conditionalSect |
7029
 *                             PEReference | S) *
7030
 * @param ctxt  an XML parser context
7031
 * @param publicId  the public identifier
7032
 * @param systemId  the system identifier (URL)
7033
 */
7034
void
7035
xmlParseExternalSubset(xmlParserCtxt *ctxt, const xmlChar *publicId,
7036
0
                       const xmlChar *systemId) {
7037
0
    int oldInputNr;
7038
7039
0
    xmlCtxtInitializeLate(ctxt);
7040
7041
0
    xmlDetectEncoding(ctxt);
7042
7043
0
    if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
7044
0
  xmlParseTextDecl(ctxt);
7045
0
    }
7046
0
    if (ctxt->myDoc == NULL) {
7047
0
        ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
7048
0
  if (ctxt->myDoc == NULL) {
7049
0
      xmlErrMemory(ctxt);
7050
0
      return;
7051
0
  }
7052
0
  ctxt->myDoc->properties = XML_DOC_INTERNAL;
7053
0
    }
7054
0
    if ((ctxt->myDoc->intSubset == NULL) &&
7055
0
        (xmlCreateIntSubset(ctxt->myDoc, NULL, publicId, systemId) == NULL)) {
7056
0
        xmlErrMemory(ctxt);
7057
0
    }
7058
7059
0
    ctxt->inSubset = 2;
7060
0
    oldInputNr = ctxt->inputNr;
7061
7062
0
    SKIP_BLANKS;
7063
0
    while (!PARSER_STOPPED(ctxt)) {
7064
0
        if (ctxt->input->cur >= ctxt->input->end) {
7065
0
            if (ctxt->inputNr <= oldInputNr) {
7066
0
                xmlParserCheckEOF(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED);
7067
0
                break;
7068
0
            }
7069
7070
0
            xmlPopPE(ctxt);
7071
0
        } else if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
7072
0
            xmlParseConditionalSections(ctxt);
7073
0
        } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
7074
0
            xmlParseMarkupDecl(ctxt);
7075
0
        } else if (RAW == '%') {
7076
0
            xmlParsePERefInternal(ctxt, 1);
7077
0
        } else {
7078
0
            xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
7079
7080
0
            while (ctxt->inputNr > oldInputNr)
7081
0
                xmlPopPE(ctxt);
7082
0
            break;
7083
0
        }
7084
0
        SKIP_BLANKS;
7085
0
        SHRINK;
7086
0
        GROW;
7087
0
    }
7088
0
}
7089
7090
/**
7091
 * parse and handle entity references in content, depending on the SAX
7092
 * interface, this may end-up in a call to character() if this is a
7093
 * CharRef, a predefined entity, if there is no reference() callback.
7094
 * or if the parser was asked to switch to that mode.
7095
 *
7096
 * @deprecated Internal function, don't use.
7097
 *
7098
 * Always consumes '&'.
7099
 *
7100
 *     [67] Reference ::= EntityRef | CharRef
7101
 * @param ctxt  an XML parser context
7102
 */
7103
void
7104
166k
xmlParseReference(xmlParserCtxt *ctxt) {
7105
166k
    xmlEntityPtr ent = NULL;
7106
166k
    const xmlChar *name;
7107
166k
    xmlChar *val;
7108
7109
166k
    if (RAW != '&')
7110
0
        return;
7111
7112
    /*
7113
     * Simple case of a CharRef
7114
     */
7115
166k
    if (NXT(1) == '#') {
7116
50.2k
  int i = 0;
7117
50.2k
  xmlChar out[16];
7118
50.2k
  int value = xmlParseCharRef(ctxt);
7119
7120
50.2k
  if (value == 0)
7121
16.1k
      return;
7122
7123
        /*
7124
         * Just encode the value in UTF-8
7125
         */
7126
34.1k
        COPY_BUF(out, i, value);
7127
34.1k
        out[i] = 0;
7128
34.1k
        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
7129
34.1k
            (!ctxt->disableSAX))
7130
386
            ctxt->sax->characters(ctxt->userData, out, i);
7131
34.1k
  return;
7132
50.2k
    }
7133
7134
    /*
7135
     * We are seeing an entity reference
7136
     */
7137
116k
    name = xmlParseEntityRefInternal(ctxt);
7138
116k
    if (name == NULL)
7139
66.8k
        return;
7140
49.3k
    ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 0);
7141
49.3k
    if (ent == NULL) {
7142
        /*
7143
         * Create a reference for undeclared entities.
7144
         */
7145
23.7k
        if ((ctxt->replaceEntities == 0) &&
7146
23.7k
            (ctxt->sax != NULL) &&
7147
23.7k
            (ctxt->disableSAX == 0) &&
7148
23.7k
            (ctxt->sax->reference != NULL)) {
7149
0
            ctxt->sax->reference(ctxt->userData, name);
7150
0
        }
7151
23.7k
        return;
7152
23.7k
    }
7153
25.6k
    if (!ctxt->wellFormed)
7154
13.0k
  return;
7155
7156
    /* special case of predefined entities */
7157
12.5k
    if ((ent->name == NULL) ||
7158
12.5k
        (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
7159
1.72k
  val = ent->content;
7160
1.72k
  if (val == NULL) return;
7161
  /*
7162
   * inline the entity.
7163
   */
7164
1.72k
  if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
7165
1.72k
      (!ctxt->disableSAX))
7166
1.72k
      ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
7167
1.72k
  return;
7168
1.72k
    }
7169
7170
    /*
7171
     * Some users try to parse entities on their own and used to set
7172
     * the renamed "checked" member. Fix the flags to cover this
7173
     * case.
7174
     */
7175
10.8k
    if (((ent->flags & XML_ENT_PARSED) == 0) && (ent->children != NULL))
7176
0
        ent->flags |= XML_ENT_PARSED;
7177
7178
    /*
7179
     * The first reference to the entity trigger a parsing phase
7180
     * where the ent->children is filled with the result from
7181
     * the parsing.
7182
     * Note: external parsed entities will not be loaded, it is not
7183
     * required for a non-validating parser, unless the parsing option
7184
     * of validating, or substituting entities were given. Doing so is
7185
     * far more secure as the parser will only process data coming from
7186
     * the document entity by default.
7187
     *
7188
     * FIXME: This doesn't work correctly since entities can be
7189
     * expanded with different namespace declarations in scope.
7190
     * For example:
7191
     *
7192
     * <!DOCTYPE doc [
7193
     *   <!ENTITY ent "<ns:elem/>">
7194
     * ]>
7195
     * <doc>
7196
     *   <decl1 xmlns:ns="urn:ns1">
7197
     *     &ent;
7198
     *   </decl1>
7199
     *   <decl2 xmlns:ns="urn:ns2">
7200
     *     &ent;
7201
     *   </decl2>
7202
     * </doc>
7203
     *
7204
     * Proposed fix:
7205
     *
7206
     * - Ignore current namespace declarations when parsing the
7207
     *   entity. If a prefix can't be resolved, don't report an error
7208
     *   but mark it as unresolved.
7209
     * - Try to resolve these prefixes when expanding the entity.
7210
     *   This will require a specialized version of xmlStaticCopyNode
7211
     *   which can also make use of the namespace hash table to avoid
7212
     *   quadratic behavior.
7213
     *
7214
     * Alternatively, we could simply reparse the entity on each
7215
     * expansion like we already do with custom SAX callbacks.
7216
     * External entity content should be cached in this case.
7217
     */
7218
10.8k
    if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
7219
10.8k
        (((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
7220
203
         ((ctxt->replaceEntities) ||
7221
10.6k
          (ctxt->validate)))) {
7222
10.6k
        if ((ent->flags & XML_ENT_PARSED) == 0) {
7223
583
            xmlCtxtParseEntity(ctxt, ent);
7224
10.0k
        } else if (ent->children == NULL) {
7225
            /*
7226
             * Probably running in SAX mode and the callbacks don't
7227
             * build the entity content. Parse the entity again.
7228
             *
7229
             * This will also be triggered in normal tree builder mode
7230
             * if an entity happens to be empty, causing unnecessary
7231
             * reloads. It's hard to come up with a reliable check in
7232
             * which mode we're running.
7233
             */
7234
6.60k
            xmlCtxtParseEntity(ctxt, ent);
7235
6.60k
        }
7236
10.6k
    }
7237
7238
    /*
7239
     * We also check for amplification if entities aren't substituted.
7240
     * They might be expanded later.
7241
     */
7242
10.8k
    if (xmlParserEntityCheck(ctxt, ent->expandedSize))
7243
39
        return;
7244
7245
10.8k
    if ((ctxt->sax == NULL) || (ctxt->disableSAX))
7246
141
        return;
7247
7248
10.6k
    if (ctxt->replaceEntities == 0) {
7249
  /*
7250
   * Create a reference
7251
   */
7252
0
        if (ctxt->sax->reference != NULL)
7253
0
      ctxt->sax->reference(ctxt->userData, ent->name);
7254
10.6k
    } else if ((ent->children != NULL) && (ctxt->node != NULL)) {
7255
3.83k
        xmlNodePtr copy, cur;
7256
7257
        /*
7258
         * Seems we are generating the DOM content, copy the tree
7259
   */
7260
3.83k
        cur = ent->children;
7261
7262
        /*
7263
         * Handle first text node with SAX to coalesce text efficiently
7264
         */
7265
3.83k
        if ((cur->type == XML_TEXT_NODE) ||
7266
3.83k
            (cur->type == XML_CDATA_SECTION_NODE)) {
7267
3.07k
            int len = xmlStrlen(cur->content);
7268
7269
3.07k
            if ((cur->type == XML_TEXT_NODE) ||
7270
3.07k
                (ctxt->options & XML_PARSE_NOCDATA)) {
7271
2.80k
                if (ctxt->sax->characters != NULL)
7272
2.80k
                    ctxt->sax->characters(ctxt, cur->content, len);
7273
2.80k
            } else {
7274
269
                if (ctxt->sax->cdataBlock != NULL)
7275
269
                    ctxt->sax->cdataBlock(ctxt, cur->content, len);
7276
269
            }
7277
7278
3.07k
            cur = cur->next;
7279
3.07k
        }
7280
7281
10.1k
        while (cur != NULL) {
7282
8.53k
            xmlNodePtr last;
7283
7284
            /*
7285
             * Handle last text node with SAX to coalesce text efficiently
7286
             */
7287
8.53k
            if ((cur->next == NULL) &&
7288
8.53k
                ((cur->type == XML_TEXT_NODE) ||
7289
2.62k
                 (cur->type == XML_CDATA_SECTION_NODE))) {
7290
2.19k
                int len = xmlStrlen(cur->content);
7291
7292
2.19k
                if ((cur->type == XML_TEXT_NODE) ||
7293
2.19k
                    (ctxt->options & XML_PARSE_NOCDATA)) {
7294
2.03k
                    if (ctxt->sax->characters != NULL)
7295
2.03k
                        ctxt->sax->characters(ctxt, cur->content, len);
7296
2.03k
                } else {
7297
162
                    if (ctxt->sax->cdataBlock != NULL)
7298
162
                        ctxt->sax->cdataBlock(ctxt, cur->content, len);
7299
162
                }
7300
7301
2.19k
                break;
7302
2.19k
            }
7303
7304
            /*
7305
             * Reset coalesce buffer stats only for non-text nodes.
7306
             */
7307
6.33k
            ctxt->nodemem = 0;
7308
6.33k
            ctxt->nodelen = 0;
7309
7310
6.33k
            copy = xmlDocCopyNode(cur, ctxt->myDoc, 1);
7311
7312
6.33k
            if (copy == NULL) {
7313
0
                xmlErrMemory(ctxt);
7314
0
                break;
7315
0
            }
7316
7317
6.33k
            if (ctxt->parseMode == XML_PARSE_READER) {
7318
                /* Needed for reader */
7319
0
                copy->extra = cur->extra;
7320
                /* Maybe needed for reader */
7321
0
                copy->_private = cur->_private;
7322
0
            }
7323
7324
6.33k
            copy->parent = ctxt->node;
7325
6.33k
            last = ctxt->node->last;
7326
6.33k
            if (last == NULL) {
7327
248
                ctxt->node->children = copy;
7328
6.09k
            } else {
7329
6.09k
                last->next = copy;
7330
6.09k
                copy->prev = last;
7331
6.09k
            }
7332
6.33k
            ctxt->node->last = copy;
7333
7334
6.33k
            cur = cur->next;
7335
6.33k
        }
7336
3.83k
    }
7337
10.6k
}
7338
7339
static void
7340
123k
xmlHandleUndeclaredEntity(xmlParserCtxtPtr ctxt, const xmlChar *name) {
7341
    /*
7342
     * [ WFC: Entity Declared ]
7343
     * In a document without any DTD, a document with only an
7344
     * internal DTD subset which contains no parameter entity
7345
     * references, or a document with "standalone='yes'", the
7346
     * Name given in the entity reference must match that in an
7347
     * entity declaration, except that well-formed documents
7348
     * need not declare any of the following entities: amp, lt,
7349
     * gt, apos, quot.
7350
     * The declaration of a parameter entity must precede any
7351
     * reference to it.
7352
     * Similarly, the declaration of a general entity must
7353
     * precede any reference to it which appears in a default
7354
     * value in an attribute-list declaration. Note that if
7355
     * entities are declared in the external subset or in
7356
     * external parameter entities, a non-validating processor
7357
     * is not obligated to read and process their declarations;
7358
     * for such documents, the rule that an entity must be
7359
     * declared is a well-formedness constraint only if
7360
     * standalone='yes'.
7361
     */
7362
123k
    if ((ctxt->standalone == 1) ||
7363
123k
        ((ctxt->hasExternalSubset == 0) &&
7364
123k
         (ctxt->hasPErefs == 0))) {
7365
84.5k
        xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
7366
84.5k
                          "Entity '%s' not defined\n", name);
7367
84.5k
#ifdef LIBXML_VALID_ENABLED
7368
84.5k
    } else if (ctxt->validate) {
7369
        /*
7370
         * [ VC: Entity Declared ]
7371
         * In a document with an external subset or external
7372
         * parameter entities with "standalone='no'", ...
7373
         * ... The declaration of a parameter entity must
7374
         * precede any reference to it...
7375
         */
7376
0
        xmlValidityError(ctxt, XML_ERR_UNDECLARED_ENTITY,
7377
0
                         "Entity '%s' not defined\n", name, NULL);
7378
0
#endif
7379
39.3k
    } else if ((ctxt->loadsubset & ~XML_SKIP_IDS) ||
7380
39.3k
               ((ctxt->replaceEntities) &&
7381
39.3k
                ((ctxt->options & XML_PARSE_NO_XXE) == 0))) {
7382
        /*
7383
         * Also raise a non-fatal error
7384
         *
7385
         * - if the external subset is loaded and all entity declarations
7386
         *   should be available, or
7387
         * - entity substition was requested without restricting
7388
         *   external entity access.
7389
         */
7390
39.3k
        xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
7391
39.3k
                     "Entity '%s' not defined\n", name);
7392
39.3k
    } else {
7393
0
        xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7394
0
                      "Entity '%s' not defined\n", name, NULL);
7395
0
    }
7396
7397
123k
    ctxt->valid = 0;
7398
123k
}
7399
7400
static xmlEntityPtr
7401
540k
xmlLookupGeneralEntity(xmlParserCtxtPtr ctxt, const xmlChar *name, int inAttr) {
7402
540k
    xmlEntityPtr ent = NULL;
7403
7404
    /*
7405
     * Predefined entities override any extra definition
7406
     */
7407
540k
    if ((ctxt->options & XML_PARSE_OLDSAX) == 0) {
7408
540k
        ent = xmlGetPredefinedEntity(name);
7409
540k
        if (ent != NULL)
7410
116k
            return(ent);
7411
540k
    }
7412
7413
    /*
7414
     * Ask first SAX for entity resolution, otherwise try the
7415
     * entities which may have stored in the parser context.
7416
     */
7417
423k
    if (ctxt->sax != NULL) {
7418
423k
  if (ctxt->sax->getEntity != NULL)
7419
423k
      ent = ctxt->sax->getEntity(ctxt->userData, name);
7420
423k
  if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
7421
423k
      (ctxt->options & XML_PARSE_OLDSAX))
7422
0
      ent = xmlGetPredefinedEntity(name);
7423
423k
  if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
7424
423k
      (ctxt->userData==ctxt)) {
7425
23.8k
      ent = xmlSAX2GetEntity(ctxt, name);
7426
23.8k
  }
7427
423k
    }
7428
7429
423k
    if (ent == NULL) {
7430
117k
        xmlHandleUndeclaredEntity(ctxt, name);
7431
117k
    }
7432
7433
    /*
7434
     * [ WFC: Parsed Entity ]
7435
     * An entity reference must not contain the name of an
7436
     * unparsed entity
7437
     */
7438
306k
    else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
7439
198
  xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
7440
198
     "Entity reference to unparsed entity %s\n", name);
7441
198
        ent = NULL;
7442
198
    }
7443
7444
    /*
7445
     * [ WFC: No External Entity References ]
7446
     * Attribute values cannot contain direct or indirect
7447
     * entity references to external entities.
7448
     */
7449
306k
    else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
7450
747
        if (inAttr) {
7451
237
            xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
7452
237
                 "Attribute references external entity '%s'\n", name);
7453
237
            ent = NULL;
7454
237
        }
7455
747
    }
7456
7457
423k
    return(ent);
7458
540k
}
7459
7460
/**
7461
 * Parse an entity reference. Always consumes '&'.
7462
 *
7463
 *     [68] EntityRef ::= '&' Name ';'
7464
 *
7465
 * @param ctxt  an XML parser context
7466
 * @returns the name, or NULL in case of error.
7467
 */
7468
static const xmlChar *
7469
286k
xmlParseEntityRefInternal(xmlParserCtxtPtr ctxt) {
7470
286k
    const xmlChar *name;
7471
7472
286k
    GROW;
7473
7474
286k
    if (RAW != '&')
7475
0
        return(NULL);
7476
286k
    NEXT;
7477
286k
    name = xmlParseName(ctxt);
7478
286k
    if (name == NULL) {
7479
64.2k
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7480
64.2k
           "xmlParseEntityRef: no name\n");
7481
64.2k
        return(NULL);
7482
64.2k
    }
7483
222k
    if (RAW != ';') {
7484
41.0k
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7485
41.0k
  return(NULL);
7486
41.0k
    }
7487
181k
    NEXT;
7488
7489
181k
    return(name);
7490
222k
}
7491
7492
/**
7493
 * @deprecated Internal function, don't use.
7494
 *
7495
 * @param ctxt  an XML parser context
7496
 * @returns the xmlEntity if found, or NULL otherwise.
7497
 */
7498
xmlEntity *
7499
0
xmlParseEntityRef(xmlParserCtxt *ctxt) {
7500
0
    const xmlChar *name;
7501
7502
0
    if (ctxt == NULL)
7503
0
        return(NULL);
7504
7505
0
    name = xmlParseEntityRefInternal(ctxt);
7506
0
    if (name == NULL)
7507
0
        return(NULL);
7508
7509
0
    return(xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 0));
7510
0
}
7511
7512
/**
7513
 * parse ENTITY references declarations, but this version parses it from
7514
 * a string value.
7515
 *
7516
 *     [68] EntityRef ::= '&' Name ';'
7517
 *
7518
 * [ WFC: Entity Declared ]
7519
 * In a document without any DTD, a document with only an internal DTD
7520
 * subset which contains no parameter entity references, or a document
7521
 * with "standalone='yes'", the Name given in the entity reference
7522
 * must match that in an entity declaration, except that well-formed
7523
 * documents need not declare any of the following entities: amp, lt,
7524
 * gt, apos, quot.  The declaration of a parameter entity must precede
7525
 * any reference to it.  Similarly, the declaration of a general entity
7526
 * must precede any reference to it which appears in a default value in an
7527
 * attribute-list declaration. Note that if entities are declared in the
7528
 * external subset or in external parameter entities, a non-validating
7529
 * processor is not obligated to read and process their declarations;
7530
 * for such documents, the rule that an entity must be declared is a
7531
 * well-formedness constraint only if standalone='yes'.
7532
 *
7533
 * [ WFC: Parsed Entity ]
7534
 * An entity reference must not contain the name of an unparsed entity
7535
 *
7536
 * @param ctxt  an XML parser context
7537
 * @param str  a pointer to an index in the string
7538
 * @returns the xmlEntity if found, or NULL otherwise. The str pointer
7539
 * is updated to the current location in the string.
7540
 */
7541
static xmlChar *
7542
359k
xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
7543
359k
    xmlChar *name;
7544
359k
    const xmlChar *ptr;
7545
359k
    xmlChar cur;
7546
7547
359k
    if ((str == NULL) || (*str == NULL))
7548
0
        return(NULL);
7549
359k
    ptr = *str;
7550
359k
    cur = *ptr;
7551
359k
    if (cur != '&')
7552
0
  return(NULL);
7553
7554
359k
    ptr++;
7555
359k
    name = xmlParseStringName(ctxt, &ptr);
7556
359k
    if (name == NULL) {
7557
4
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7558
4
           "xmlParseStringEntityRef: no name\n");
7559
4
  *str = ptr;
7560
4
  return(NULL);
7561
4
    }
7562
359k
    if (*ptr != ';') {
7563
2
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7564
2
        xmlFree(name);
7565
2
  *str = ptr;
7566
2
  return(NULL);
7567
2
    }
7568
359k
    ptr++;
7569
7570
359k
    *str = ptr;
7571
359k
    return(name);
7572
359k
}
7573
7574
/**
7575
 * Parse a parameter entity reference. Always consumes '%'.
7576
 *
7577
 * The entity content is handled directly by pushing it's content as
7578
 * a new input stream.
7579
 *
7580
 *     [69] PEReference ::= '%' Name ';'
7581
 *
7582
 * [ WFC: No Recursion ]
7583
 * A parsed entity must not contain a recursive
7584
 * reference to itself, either directly or indirectly.
7585
 *
7586
 * [ WFC: Entity Declared ]
7587
 * In a document without any DTD, a document with only an internal DTD
7588
 * subset which contains no parameter entity references, or a document
7589
 * with "standalone='yes'", ...  ... The declaration of a parameter
7590
 * entity must precede any reference to it...
7591
 *
7592
 * [ VC: Entity Declared ]
7593
 * In a document with an external subset or external parameter entities
7594
 * with "standalone='no'", ...  ... The declaration of a parameter entity
7595
 * must precede any reference to it...
7596
 *
7597
 * [ WFC: In DTD ]
7598
 * Parameter-entity references may only appear in the DTD.
7599
 * NOTE: misleading but this is handled.
7600
 *
7601
 * @param ctxt  an XML parser context
7602
 * @param markupDecl  whether the PERef starts a markup declaration
7603
 */
7604
static void
7605
44.7k
xmlParsePERefInternal(xmlParserCtxt *ctxt, int markupDecl) {
7606
44.7k
    const xmlChar *name;
7607
44.7k
    xmlEntityPtr entity = NULL;
7608
44.7k
    xmlParserInputPtr input;
7609
7610
44.7k
    if (RAW != '%')
7611
0
        return;
7612
44.7k
    NEXT;
7613
44.7k
    name = xmlParseName(ctxt);
7614
44.7k
    if (name == NULL) {
7615
2.41k
  xmlFatalErrMsg(ctxt, XML_ERR_PEREF_NO_NAME, "PEReference: no name\n");
7616
2.41k
  return;
7617
2.41k
    }
7618
42.3k
    if (RAW != ';') {
7619
3.96k
  xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
7620
3.96k
        return;
7621
3.96k
    }
7622
7623
38.3k
    NEXT;
7624
7625
    /* Must be set before xmlHandleUndeclaredEntity */
7626
38.3k
    ctxt->hasPErefs = 1;
7627
7628
    /*
7629
     * Request the entity from SAX
7630
     */
7631
38.3k
    if ((ctxt->sax != NULL) &&
7632
38.3k
  (ctxt->sax->getParameterEntity != NULL))
7633
38.3k
  entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
7634
7635
38.3k
    if (entity == NULL) {
7636
6.18k
        xmlHandleUndeclaredEntity(ctxt, name);
7637
32.1k
    } else {
7638
  /*
7639
   * Internal checking in case the entity quest barfed
7640
   */
7641
32.1k
  if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
7642
32.1k
      (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
7643
0
      xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7644
0
      "Internal: %%%s; is not a parameter entity\n",
7645
0
        name, NULL);
7646
32.1k
  } else {
7647
32.1k
      if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
7648
32.1k
                ((ctxt->options & XML_PARSE_NO_XXE) ||
7649
850
     ((ctxt->loadsubset == 0) &&
7650
0
      (ctxt->replaceEntities == 0) &&
7651
0
      (ctxt->validate == 0))))
7652
850
    return;
7653
7654
31.3k
            if (entity->flags & XML_ENT_EXPANDING) {
7655
1
                xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
7656
1
                xmlHaltParser(ctxt);
7657
1
                return;
7658
1
            }
7659
7660
31.3k
      input = xmlNewEntityInputStream(ctxt, entity);
7661
31.3k
      if (xmlCtxtPushInput(ctxt, input) < 0) {
7662
0
                xmlFreeInputStream(input);
7663
0
    return;
7664
0
            }
7665
7666
31.3k
            entity->flags |= XML_ENT_EXPANDING;
7667
7668
31.3k
            if (markupDecl)
7669
23.1k
                input->flags |= XML_INPUT_MARKUP_DECL;
7670
7671
31.3k
            GROW;
7672
7673
31.3k
      if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
7674
0
                xmlDetectEncoding(ctxt);
7675
7676
0
                if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
7677
0
                    (IS_BLANK_CH(NXT(5)))) {
7678
0
                    xmlParseTextDecl(ctxt);
7679
0
                }
7680
0
            }
7681
31.3k
  }
7682
32.1k
    }
7683
38.3k
}
7684
7685
/**
7686
 * Parse a parameter entity reference.
7687
 *
7688
 * @deprecated Internal function, don't use.
7689
 *
7690
 * @param ctxt  an XML parser context
7691
 */
7692
void
7693
0
xmlParsePEReference(xmlParserCtxt *ctxt) {
7694
0
    xmlParsePERefInternal(ctxt, 0);
7695
0
}
7696
7697
/**
7698
 * Load the content of an entity.
7699
 *
7700
 * @param ctxt  an XML parser context
7701
 * @param entity  an unloaded system entity
7702
 * @returns 0 in case of success and -1 in case of failure
7703
 */
7704
static int
7705
0
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
7706
0
    xmlParserInputPtr oldinput, input = NULL;
7707
0
    xmlParserInputPtr *oldinputTab;
7708
0
    const xmlChar *oldencoding;
7709
0
    xmlChar *content = NULL;
7710
0
    xmlResourceType rtype;
7711
0
    size_t length, i;
7712
0
    int oldinputNr, oldinputMax;
7713
0
    int ret = -1;
7714
0
    int res;
7715
7716
0
    if ((ctxt == NULL) || (entity == NULL) ||
7717
0
        ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) &&
7718
0
   (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) ||
7719
0
  (entity->content != NULL)) {
7720
0
  xmlFatalErr(ctxt, XML_ERR_ARGUMENT,
7721
0
              "xmlLoadEntityContent parameter error");
7722
0
        return(-1);
7723
0
    }
7724
7725
0
    if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)
7726
0
        rtype = XML_RESOURCE_PARAMETER_ENTITY;
7727
0
    else
7728
0
        rtype = XML_RESOURCE_GENERAL_ENTITY;
7729
7730
0
    input = xmlLoadResource(ctxt, (char *) entity->URI,
7731
0
                            (char *) entity->ExternalID, rtype);
7732
0
    if (input == NULL)
7733
0
        return(-1);
7734
7735
0
    oldinput = ctxt->input;
7736
0
    oldinputNr = ctxt->inputNr;
7737
0
    oldinputMax = ctxt->inputMax;
7738
0
    oldinputTab = ctxt->inputTab;
7739
0
    oldencoding = ctxt->encoding;
7740
7741
0
    ctxt->input = NULL;
7742
0
    ctxt->inputNr = 0;
7743
0
    ctxt->inputMax = 1;
7744
0
    ctxt->encoding = NULL;
7745
0
    ctxt->inputTab = xmlMalloc(sizeof(xmlParserInputPtr));
7746
0
    if (ctxt->inputTab == NULL) {
7747
0
        xmlErrMemory(ctxt);
7748
0
        xmlFreeInputStream(input);
7749
0
        goto error;
7750
0
    }
7751
7752
0
    xmlBufResetInput(input->buf->buffer, input);
7753
7754
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
7755
0
        xmlFreeInputStream(input);
7756
0
        goto error;
7757
0
    }
7758
7759
0
    xmlDetectEncoding(ctxt);
7760
7761
    /*
7762
     * Parse a possible text declaration first
7763
     */
7764
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
7765
0
  xmlParseTextDecl(ctxt);
7766
        /*
7767
         * An XML-1.0 document can't reference an entity not XML-1.0
7768
         */
7769
0
        if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
7770
0
            (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
7771
0
            xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
7772
0
                           "Version mismatch between document and entity\n");
7773
0
        }
7774
0
    }
7775
7776
0
    length = input->cur - input->base;
7777
0
    xmlBufShrink(input->buf->buffer, length);
7778
0
    xmlSaturatedAdd(&ctxt->sizeentities, length);
7779
7780
0
    while ((res = xmlParserInputBufferGrow(input->buf, 4096)) > 0)
7781
0
        ;
7782
7783
0
    xmlBufResetInput(input->buf->buffer, input);
7784
7785
0
    if (res < 0) {
7786
0
        xmlCtxtErrIO(ctxt, input->buf->error, NULL);
7787
0
        goto error;
7788
0
    }
7789
7790
0
    length = xmlBufUse(input->buf->buffer);
7791
0
    if (length > INT_MAX) {
7792
0
        xmlErrMemory(ctxt);
7793
0
        goto error;
7794
0
    }
7795
7796
0
    content = xmlStrndup(xmlBufContent(input->buf->buffer), length);
7797
0
    if (content == NULL) {
7798
0
        xmlErrMemory(ctxt);
7799
0
        goto error;
7800
0
    }
7801
7802
0
    for (i = 0; i < length; ) {
7803
0
        int clen = length - i;
7804
0
        int c = xmlGetUTF8Char(content + i, &clen);
7805
7806
0
        if ((c < 0) || (!IS_CHAR(c))) {
7807
0
            xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
7808
0
                              "xmlLoadEntityContent: invalid char value %d\n",
7809
0
                              content[i]);
7810
0
            goto error;
7811
0
        }
7812
0
        i += clen;
7813
0
    }
7814
7815
0
    xmlSaturatedAdd(&ctxt->sizeentities, length);
7816
0
    entity->content = content;
7817
0
    entity->length = length;
7818
0
    content = NULL;
7819
0
    ret = 0;
7820
7821
0
error:
7822
0
    while (ctxt->inputNr > 0)
7823
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
7824
0
    xmlFree(ctxt->inputTab);
7825
0
    xmlFree((xmlChar *) ctxt->encoding);
7826
7827
0
    ctxt->input = oldinput;
7828
0
    ctxt->inputNr = oldinputNr;
7829
0
    ctxt->inputMax = oldinputMax;
7830
0
    ctxt->inputTab = oldinputTab;
7831
0
    ctxt->encoding = oldencoding;
7832
7833
0
    xmlFree(content);
7834
7835
0
    return(ret);
7836
0
}
7837
7838
/**
7839
 * parse PEReference declarations
7840
 *
7841
 *     [69] PEReference ::= '%' Name ';'
7842
 *
7843
 * [ WFC: No Recursion ]
7844
 * A parsed entity must not contain a recursive
7845
 * reference to itself, either directly or indirectly.
7846
 *
7847
 * [ WFC: Entity Declared ]
7848
 * In a document without any DTD, a document with only an internal DTD
7849
 * subset which contains no parameter entity references, or a document
7850
 * with "standalone='yes'", ...  ... The declaration of a parameter
7851
 * entity must precede any reference to it...
7852
 *
7853
 * [ VC: Entity Declared ]
7854
 * In a document with an external subset or external parameter entities
7855
 * with "standalone='no'", ...  ... The declaration of a parameter entity
7856
 * must precede any reference to it...
7857
 *
7858
 * [ WFC: In DTD ]
7859
 * Parameter-entity references may only appear in the DTD.
7860
 * NOTE: misleading but this is handled.
7861
 *
7862
 * @param ctxt  an XML parser context
7863
 * @param str  a pointer to an index in the string
7864
 * @returns the string of the entity content.
7865
 *         str is updated to the current value of the index
7866
 */
7867
static xmlEntityPtr
7868
1.38k
xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
7869
1.38k
    const xmlChar *ptr;
7870
1.38k
    xmlChar cur;
7871
1.38k
    xmlChar *name;
7872
1.38k
    xmlEntityPtr entity = NULL;
7873
7874
1.38k
    if ((str == NULL) || (*str == NULL)) return(NULL);
7875
1.38k
    ptr = *str;
7876
1.38k
    cur = *ptr;
7877
1.38k
    if (cur != '%')
7878
0
        return(NULL);
7879
1.38k
    ptr++;
7880
1.38k
    name = xmlParseStringName(ctxt, &ptr);
7881
1.38k
    if (name == NULL) {
7882
173
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7883
173
           "xmlParseStringPEReference: no name\n");
7884
173
  *str = ptr;
7885
173
  return(NULL);
7886
173
    }
7887
1.21k
    cur = *ptr;
7888
1.21k
    if (cur != ';') {
7889
411
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7890
411
  xmlFree(name);
7891
411
  *str = ptr;
7892
411
  return(NULL);
7893
411
    }
7894
804
    ptr++;
7895
7896
    /* Must be set before xmlHandleUndeclaredEntity */
7897
804
    ctxt->hasPErefs = 1;
7898
7899
    /*
7900
     * Request the entity from SAX
7901
     */
7902
804
    if ((ctxt->sax != NULL) &&
7903
804
  (ctxt->sax->getParameterEntity != NULL))
7904
804
  entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
7905
7906
804
    if (entity == NULL) {
7907
428
        xmlHandleUndeclaredEntity(ctxt, name);
7908
428
    } else {
7909
  /*
7910
   * Internal checking in case the entity quest barfed
7911
   */
7912
376
  if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
7913
376
      (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
7914
0
      xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7915
0
        "%%%s; is not a parameter entity\n",
7916
0
        name, NULL);
7917
0
  }
7918
376
    }
7919
7920
804
    xmlFree(name);
7921
804
    *str = ptr;
7922
804
    return(entity);
7923
1.21k
}
7924
7925
/**
7926
 * parse a DOCTYPE declaration
7927
 *
7928
 * @deprecated Internal function, don't use.
7929
 *
7930
 *     [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
7931
 *                          ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
7932
 *
7933
 * [ VC: Root Element Type ]
7934
 * The Name in the document type declaration must match the element
7935
 * type of the root element.
7936
 *
7937
 * @param ctxt  an XML parser context
7938
 */
7939
7940
void
7941
5.92k
xmlParseDocTypeDecl(xmlParserCtxt *ctxt) {
7942
5.92k
    const xmlChar *name = NULL;
7943
5.92k
    xmlChar *publicId = NULL;
7944
5.92k
    xmlChar *URI = NULL;
7945
7946
    /*
7947
     * We know that '<!DOCTYPE' has been detected.
7948
     */
7949
5.92k
    SKIP(9);
7950
7951
5.92k
    if (SKIP_BLANKS == 0) {
7952
3.09k
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7953
3.09k
                       "Space required after 'DOCTYPE'\n");
7954
3.09k
    }
7955
7956
    /*
7957
     * Parse the DOCTYPE name.
7958
     */
7959
5.92k
    name = xmlParseName(ctxt);
7960
5.92k
    if (name == NULL) {
7961
1.41k
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7962
1.41k
           "xmlParseDocTypeDecl : no DOCTYPE name !\n");
7963
1.41k
    }
7964
5.92k
    ctxt->intSubName = name;
7965
7966
5.92k
    SKIP_BLANKS;
7967
7968
    /*
7969
     * Check for public and system identifier (URI)
7970
     */
7971
5.92k
    URI = xmlParseExternalID(ctxt, &publicId, 1);
7972
7973
5.92k
    if ((URI != NULL) || (publicId != NULL)) {
7974
298
        ctxt->hasExternalSubset = 1;
7975
298
    }
7976
5.92k
    ctxt->extSubURI = URI;
7977
5.92k
    ctxt->extSubSystem = publicId;
7978
7979
5.92k
    SKIP_BLANKS;
7980
7981
    /*
7982
     * Create and update the internal subset.
7983
     */
7984
5.92k
    if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
7985
5.92k
  (!ctxt->disableSAX))
7986
2.68k
  ctxt->sax->internalSubset(ctxt->userData, name, publicId, URI);
7987
7988
5.92k
    if ((RAW != '[') && (RAW != '>')) {
7989
175
  xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
7990
175
    }
7991
5.92k
}
7992
7993
/**
7994
 * parse the internal subset declaration
7995
 *
7996
 *     [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
7997
 * @param ctxt  an XML parser context
7998
 */
7999
8000
static void
8001
5.62k
xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
8002
    /*
8003
     * Is there any DTD definition ?
8004
     */
8005
5.62k
    if (RAW == '[') {
8006
5.62k
        int oldInputNr = ctxt->inputNr;
8007
8008
5.62k
        NEXT;
8009
  /*
8010
   * Parse the succession of Markup declarations and
8011
   * PEReferences.
8012
   * Subsequence (markupdecl | PEReference | S)*
8013
   */
8014
5.62k
  SKIP_BLANKS;
8015
168k
        while (1) {
8016
168k
            if (PARSER_STOPPED(ctxt)) {
8017
694
                return;
8018
167k
            } else if (ctxt->input->cur >= ctxt->input->end) {
8019
23.7k
                if (ctxt->inputNr <= oldInputNr) {
8020
1.03k
                xmlFatalErr(ctxt, XML_ERR_INT_SUBSET_NOT_FINISHED, NULL);
8021
1.03k
                    return;
8022
1.03k
                }
8023
22.6k
                xmlPopPE(ctxt);
8024
144k
            } else if ((RAW == ']') && (ctxt->inputNr <= oldInputNr)) {
8025
1.95k
                NEXT;
8026
1.95k
                SKIP_BLANKS;
8027
1.95k
                break;
8028
142k
            } else if ((PARSER_EXTERNAL(ctxt)) &&
8029
142k
                       (RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
8030
                /*
8031
                 * Conditional sections are allowed in external entities
8032
                 * included by PE References in the internal subset.
8033
                 */
8034
0
                xmlParseConditionalSections(ctxt);
8035
142k
            } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
8036
106k
                xmlParseMarkupDecl(ctxt);
8037
106k
            } else if (RAW == '%') {
8038
33.3k
                xmlParsePERefInternal(ctxt, 1);
8039
33.3k
            } else {
8040
1.93k
                xmlFatalErr(ctxt, XML_ERR_INT_SUBSET_NOT_FINISHED, NULL);
8041
8042
2.00k
                while (ctxt->inputNr > oldInputNr)
8043
63
                    xmlPopPE(ctxt);
8044
1.93k
                return;
8045
1.93k
            }
8046
162k
            SKIP_BLANKS;
8047
162k
            SHRINK;
8048
162k
            GROW;
8049
162k
        }
8050
5.62k
    }
8051
8052
    /*
8053
     * We should be at the end of the DOCTYPE declaration.
8054
     */
8055
1.95k
    if (RAW != '>') {
8056
158
        xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
8057
158
        return;
8058
158
    }
8059
1.79k
    NEXT;
8060
1.79k
}
8061
8062
#ifdef LIBXML_SAX1_ENABLED
8063
/**
8064
 * parse an attribute
8065
 *
8066
 * @deprecated Internal function, don't use.
8067
 *
8068
 *     [41] Attribute ::= Name Eq AttValue
8069
 *
8070
 * [ WFC: No External Entity References ]
8071
 * Attribute values cannot contain direct or indirect entity references
8072
 * to external entities.
8073
 *
8074
 * [ WFC: No < in Attribute Values ]
8075
 * The replacement text of any entity referred to directly or indirectly in
8076
 * an attribute value (other than "&lt;") must not contain a <.
8077
 *
8078
 * [ VC: Attribute Value Type ]
8079
 * The attribute must have been declared; the value must be of the type
8080
 * declared for it.
8081
 *
8082
 *     [25] Eq ::= S? '=' S?
8083
 *
8084
 * With namespace:
8085
 *
8086
 *     [NS 11] Attribute ::= QName Eq AttValue
8087
 *
8088
 * Also the case QName == xmlns:??? is handled independently as a namespace
8089
 * definition.
8090
 *
8091
 * @param ctxt  an XML parser context
8092
 * @param value  a xmlChar ** used to store the value of the attribute
8093
 * @returns the attribute name, and the value in *value.
8094
 */
8095
8096
const xmlChar *
8097
0
xmlParseAttribute(xmlParserCtxt *ctxt, xmlChar **value) {
8098
0
    const xmlChar *name;
8099
0
    xmlChar *val;
8100
8101
0
    *value = NULL;
8102
0
    GROW;
8103
0
    name = xmlParseName(ctxt);
8104
0
    if (name == NULL) {
8105
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8106
0
                 "error parsing attribute name\n");
8107
0
        return(NULL);
8108
0
    }
8109
8110
    /*
8111
     * read the value
8112
     */
8113
0
    SKIP_BLANKS;
8114
0
    if (RAW == '=') {
8115
0
        NEXT;
8116
0
  SKIP_BLANKS;
8117
0
  val = xmlParseAttValue(ctxt);
8118
0
    } else {
8119
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
8120
0
         "Specification mandates value for attribute %s\n", name);
8121
0
  return(name);
8122
0
    }
8123
8124
    /*
8125
     * Check that xml:lang conforms to the specification
8126
     * No more registered as an error, just generate a warning now
8127
     * since this was deprecated in XML second edition
8128
     */
8129
0
    if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
8130
0
  if (!xmlCheckLanguageID(val)) {
8131
0
      xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
8132
0
              "Malformed value for xml:lang : %s\n",
8133
0
        val, NULL);
8134
0
  }
8135
0
    }
8136
8137
    /*
8138
     * Check that xml:space conforms to the specification
8139
     */
8140
0
    if (xmlStrEqual(name, BAD_CAST "xml:space")) {
8141
0
  if (xmlStrEqual(val, BAD_CAST "default"))
8142
0
      *(ctxt->space) = 0;
8143
0
  else if (xmlStrEqual(val, BAD_CAST "preserve"))
8144
0
      *(ctxt->space) = 1;
8145
0
  else {
8146
0
    xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
8147
0
"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
8148
0
                                 val, NULL);
8149
0
  }
8150
0
    }
8151
8152
0
    *value = val;
8153
0
    return(name);
8154
0
}
8155
8156
/**
8157
 * Parse a start tag. Always consumes '<'.
8158
 *
8159
 * @deprecated Internal function, don't use.
8160
 *
8161
 *     [40] STag ::= '<' Name (S Attribute)* S? '>'
8162
 *
8163
 * [ WFC: Unique Att Spec ]
8164
 * No attribute name may appear more than once in the same start-tag or
8165
 * empty-element tag.
8166
 *
8167
 *     [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
8168
 *
8169
 * [ WFC: Unique Att Spec ]
8170
 * No attribute name may appear more than once in the same start-tag or
8171
 * empty-element tag.
8172
 *
8173
 * With namespace:
8174
 *
8175
 *     [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
8176
 *
8177
 *     [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
8178
 *
8179
 * @param ctxt  an XML parser context
8180
 * @returns the element name parsed
8181
 */
8182
8183
const xmlChar *
8184
0
xmlParseStartTag(xmlParserCtxt *ctxt) {
8185
0
    const xmlChar *name;
8186
0
    const xmlChar *attname;
8187
0
    xmlChar *attvalue;
8188
0
    const xmlChar **atts = ctxt->atts;
8189
0
    int nbatts = 0;
8190
0
    int maxatts = ctxt->maxatts;
8191
0
    int i;
8192
8193
0
    if (RAW != '<') return(NULL);
8194
0
    NEXT1;
8195
8196
0
    name = xmlParseName(ctxt);
8197
0
    if (name == NULL) {
8198
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8199
0
       "xmlParseStartTag: invalid element name\n");
8200
0
        return(NULL);
8201
0
    }
8202
8203
    /*
8204
     * Now parse the attributes, it ends up with the ending
8205
     *
8206
     * (S Attribute)* S?
8207
     */
8208
0
    SKIP_BLANKS;
8209
0
    GROW;
8210
8211
0
    while (((RAW != '>') &&
8212
0
     ((RAW != '/') || (NXT(1) != '>')) &&
8213
0
     (IS_BYTE_CHAR(RAW))) && (PARSER_STOPPED(ctxt) == 0)) {
8214
0
  attname = xmlParseAttribute(ctxt, &attvalue);
8215
0
        if (attname == NULL)
8216
0
      break;
8217
0
        if (attvalue != NULL) {
8218
      /*
8219
       * [ WFC: Unique Att Spec ]
8220
       * No attribute name may appear more than once in the same
8221
       * start-tag or empty-element tag.
8222
       */
8223
0
      for (i = 0; i < nbatts;i += 2) {
8224
0
          if (xmlStrEqual(atts[i], attname)) {
8225
0
        xmlErrAttributeDup(ctxt, NULL, attname);
8226
0
        goto failed;
8227
0
    }
8228
0
      }
8229
      /*
8230
       * Add the pair to atts
8231
       */
8232
0
      if (nbatts + 4 > maxatts) {
8233
0
          const xmlChar **n;
8234
0
                int newSize;
8235
8236
0
                newSize = xmlGrowCapacity(maxatts, sizeof(n[0]) * 2,
8237
0
                                          11, XML_MAX_ATTRS);
8238
0
                if (newSize < 0) {
8239
0
        xmlErrMemory(ctxt);
8240
0
        goto failed;
8241
0
    }
8242
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
8243
0
                if (newSize < 2)
8244
0
                    newSize = 2;
8245
0
#endif
8246
0
          n = xmlRealloc(atts, newSize * sizeof(n[0]) * 2);
8247
0
    if (n == NULL) {
8248
0
        xmlErrMemory(ctxt);
8249
0
        goto failed;
8250
0
    }
8251
0
    atts = n;
8252
0
                maxatts = newSize * 2;
8253
0
    ctxt->atts = atts;
8254
0
    ctxt->maxatts = maxatts;
8255
0
      }
8256
8257
0
      atts[nbatts++] = attname;
8258
0
      atts[nbatts++] = attvalue;
8259
0
      atts[nbatts] = NULL;
8260
0
      atts[nbatts + 1] = NULL;
8261
8262
0
            attvalue = NULL;
8263
0
  }
8264
8265
0
failed:
8266
8267
0
        if (attvalue != NULL)
8268
0
            xmlFree(attvalue);
8269
8270
0
  GROW
8271
0
  if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
8272
0
      break;
8273
0
  if (SKIP_BLANKS == 0) {
8274
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8275
0
         "attributes construct error\n");
8276
0
  }
8277
0
  SHRINK;
8278
0
        GROW;
8279
0
    }
8280
8281
    /*
8282
     * SAX: Start of Element !
8283
     */
8284
0
    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
8285
0
  (!ctxt->disableSAX)) {
8286
0
  if (nbatts > 0)
8287
0
      ctxt->sax->startElement(ctxt->userData, name, atts);
8288
0
  else
8289
0
      ctxt->sax->startElement(ctxt->userData, name, NULL);
8290
0
    }
8291
8292
0
    if (atts != NULL) {
8293
        /* Free only the content strings */
8294
0
        for (i = 1;i < nbatts;i+=2)
8295
0
      if (atts[i] != NULL)
8296
0
         xmlFree((xmlChar *) atts[i]);
8297
0
    }
8298
0
    return(name);
8299
0
}
8300
8301
/**
8302
 * Parse an end tag. Always consumes '</'.
8303
 *
8304
 *     [42] ETag ::= '</' Name S? '>'
8305
 *
8306
 * With namespace
8307
 *
8308
 *     [NS 9] ETag ::= '</' QName S? '>'
8309
 * @param ctxt  an XML parser context
8310
 * @param line  line of the start tag
8311
 */
8312
8313
static void
8314
0
xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
8315
0
    const xmlChar *name;
8316
8317
0
    GROW;
8318
0
    if ((RAW != '<') || (NXT(1) != '/')) {
8319
0
  xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
8320
0
           "xmlParseEndTag: '</' not found\n");
8321
0
  return;
8322
0
    }
8323
0
    SKIP(2);
8324
8325
0
    name = xmlParseNameAndCompare(ctxt,ctxt->name);
8326
8327
    /*
8328
     * We should definitely be at the ending "S? '>'" part
8329
     */
8330
0
    GROW;
8331
0
    SKIP_BLANKS;
8332
0
    if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
8333
0
  xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
8334
0
    } else
8335
0
  NEXT1;
8336
8337
    /*
8338
     * [ WFC: Element Type Match ]
8339
     * The Name in an element's end-tag must match the element type in the
8340
     * start-tag.
8341
     *
8342
     */
8343
0
    if (name != (xmlChar*)1) {
8344
0
        if (name == NULL) name = BAD_CAST "unparsable";
8345
0
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
8346
0
         "Opening and ending tag mismatch: %s line %d and %s\n",
8347
0
                    ctxt->name, line, name);
8348
0
    }
8349
8350
    /*
8351
     * SAX: End of Tag
8352
     */
8353
0
    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
8354
0
  (!ctxt->disableSAX))
8355
0
        ctxt->sax->endElement(ctxt->userData, ctxt->name);
8356
8357
0
    namePop(ctxt);
8358
0
    spacePop(ctxt);
8359
0
}
8360
8361
/**
8362
 * parse an end of tag
8363
 *
8364
 * @deprecated Internal function, don't use.
8365
 *
8366
 *     [42] ETag ::= '</' Name S? '>'
8367
 *
8368
 * With namespace
8369
 *
8370
 *     [NS 9] ETag ::= '</' QName S? '>'
8371
 * @param ctxt  an XML parser context
8372
 */
8373
8374
void
8375
0
xmlParseEndTag(xmlParserCtxt *ctxt) {
8376
0
    xmlParseEndTag1(ctxt, 0);
8377
0
}
8378
#endif /* LIBXML_SAX1_ENABLED */
8379
8380
/************************************************************************
8381
 *                  *
8382
 *          SAX 2 specific operations       *
8383
 *                  *
8384
 ************************************************************************/
8385
8386
/**
8387
 * parse an XML Namespace QName
8388
 *
8389
 *     [6]  QName  ::= (Prefix ':')? LocalPart
8390
 *     [7]  Prefix  ::= NCName
8391
 *     [8]  LocalPart  ::= NCName
8392
 *
8393
 * @param ctxt  an XML parser context
8394
 * @param prefix  pointer to store the prefix part
8395
 * @returns the Name parsed or NULL
8396
 */
8397
8398
static xmlHashedString
8399
3.00M
xmlParseQNameHashed(xmlParserCtxtPtr ctxt, xmlHashedString *prefix) {
8400
3.00M
    xmlHashedString l, p;
8401
3.00M
    int start, isNCName = 0;
8402
8403
3.00M
    l.name = NULL;
8404
3.00M
    p.name = NULL;
8405
8406
3.00M
    GROW;
8407
3.00M
    start = CUR_PTR - BASE_PTR;
8408
8409
3.00M
    l = xmlParseNCName(ctxt);
8410
3.00M
    if (l.name != NULL) {
8411
1.27M
        isNCName = 1;
8412
1.27M
        if (CUR == ':') {
8413
151k
            NEXT;
8414
151k
            p = l;
8415
151k
            l = xmlParseNCName(ctxt);
8416
151k
        }
8417
1.27M
    }
8418
3.00M
    if ((l.name == NULL) || (CUR == ':')) {
8419
1.74M
        xmlChar *tmp;
8420
8421
1.74M
        l.name = NULL;
8422
1.74M
        p.name = NULL;
8423
1.74M
        if ((isNCName == 0) && (CUR != ':'))
8424
1.71M
            return(l);
8425
38.8k
        tmp = xmlParseNmtoken(ctxt);
8426
38.8k
        if (tmp != NULL)
8427
22.2k
            xmlFree(tmp);
8428
38.8k
        l = xmlDictLookupHashed(ctxt->dict, BASE_PTR + start,
8429
38.8k
                                CUR_PTR - (BASE_PTR + start));
8430
38.8k
        if (l.name == NULL) {
8431
0
            xmlErrMemory(ctxt);
8432
0
            return(l);
8433
0
        }
8434
38.8k
        xmlNsErr(ctxt, XML_NS_ERR_QNAME,
8435
38.8k
                 "Failed to parse QName '%s'\n", l.name, NULL, NULL);
8436
38.8k
    }
8437
8438
1.28M
    *prefix = p;
8439
1.28M
    return(l);
8440
3.00M
}
8441
8442
/**
8443
 * parse an XML Namespace QName
8444
 *
8445
 *     [6]  QName  ::= (Prefix ':')? LocalPart
8446
 *     [7]  Prefix  ::= NCName
8447
 *     [8]  LocalPart  ::= NCName
8448
 *
8449
 * @param ctxt  an XML parser context
8450
 * @param prefix  pointer to store the prefix part
8451
 * @returns the Name parsed or NULL
8452
 */
8453
8454
static const xmlChar *
8455
10.3k
xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
8456
10.3k
    xmlHashedString n, p;
8457
8458
10.3k
    n = xmlParseQNameHashed(ctxt, &p);
8459
10.3k
    if (n.name == NULL)
8460
4.23k
        return(NULL);
8461
6.15k
    *prefix = p.name;
8462
6.15k
    return(n.name);
8463
10.3k
}
8464
8465
/**
8466
 * parse an XML name and compares for match
8467
 * (specialized for endtag parsing)
8468
 *
8469
 * @param ctxt  an XML parser context
8470
 * @param name  the localname
8471
 * @param prefix  the prefix, if any.
8472
 * @returns NULL for an illegal name, (xmlChar*) 1 for success
8473
 * and the name for mismatch
8474
 */
8475
8476
static const xmlChar *
8477
xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
8478
13.6k
                        xmlChar const *prefix) {
8479
13.6k
    const xmlChar *cmp;
8480
13.6k
    const xmlChar *in;
8481
13.6k
    const xmlChar *ret;
8482
13.6k
    const xmlChar *prefix2;
8483
8484
13.6k
    if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
8485
8486
13.6k
    GROW;
8487
13.6k
    in = ctxt->input->cur;
8488
8489
13.6k
    cmp = prefix;
8490
22.9k
    while (*in != 0 && *in == *cmp) {
8491
9.28k
  ++in;
8492
9.28k
  ++cmp;
8493
9.28k
    }
8494
13.6k
    if ((*cmp == 0) && (*in == ':')) {
8495
7.09k
        in++;
8496
7.09k
  cmp = name;
8497
12.9k
  while (*in != 0 && *in == *cmp) {
8498
5.87k
      ++in;
8499
5.87k
      ++cmp;
8500
5.87k
  }
8501
7.09k
  if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
8502
      /* success */
8503
3.26k
            ctxt->input->col += in - ctxt->input->cur;
8504
3.26k
      ctxt->input->cur = in;
8505
3.26k
      return((const xmlChar*) 1);
8506
3.26k
  }
8507
7.09k
    }
8508
    /*
8509
     * all strings coms from the dictionary, equality can be done directly
8510
     */
8511
10.3k
    ret = xmlParseQName (ctxt, &prefix2);
8512
10.3k
    if (ret == NULL)
8513
4.23k
        return(NULL);
8514
6.15k
    if ((ret == name) && (prefix == prefix2))
8515
2.05k
  return((const xmlChar*) 1);
8516
4.09k
    return ret;
8517
6.15k
}
8518
8519
/**
8520
 * parse an attribute in the new SAX2 framework.
8521
 *
8522
 * @param ctxt  an XML parser context
8523
 * @param pref  the element prefix
8524
 * @param elem  the element name
8525
 * @param hprefix  resulting attribute prefix
8526
 * @param value  resulting value of the attribute
8527
 * @param len  resulting length of the attribute
8528
 * @param alloc  resulting indicator if the attribute was allocated
8529
 * @returns the attribute name, and the value in *value, .
8530
 */
8531
8532
static xmlHashedString
8533
xmlParseAttribute2(xmlParserCtxtPtr ctxt,
8534
                   const xmlChar * pref, const xmlChar * elem,
8535
                   xmlHashedString * hprefix, xmlChar ** value,
8536
                   int *len, int *alloc)
8537
774k
{
8538
774k
    xmlHashedString hname;
8539
774k
    const xmlChar *prefix, *name;
8540
774k
    xmlChar *val = NULL, *internal_val = NULL;
8541
774k
    int special = 0;
8542
774k
    int isNamespace;
8543
774k
    int flags;
8544
8545
774k
    *value = NULL;
8546
774k
    GROW;
8547
774k
    hname = xmlParseQNameHashed(ctxt, hprefix);
8548
774k
    if (hname.name == NULL) {
8549
97.0k
        xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8550
97.0k
                       "error parsing attribute name\n");
8551
97.0k
        return(hname);
8552
97.0k
    }
8553
677k
    name = hname.name;
8554
677k
    prefix = hprefix->name;
8555
8556
    /*
8557
     * get the type if needed
8558
     */
8559
677k
    if (ctxt->attsSpecial != NULL) {
8560
126k
        special = XML_PTR_TO_INT(xmlHashQLookup2(ctxt->attsSpecial, pref, elem,
8561
126k
                                              prefix, name));
8562
126k
    }
8563
8564
    /*
8565
     * read the value
8566
     */
8567
677k
    SKIP_BLANKS;
8568
677k
    if (RAW != '=') {
8569
44.1k
        xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
8570
44.1k
                          "Specification mandates value for attribute %s\n",
8571
44.1k
                          name);
8572
44.1k
        goto error;
8573
44.1k
    }
8574
8575
8576
633k
    NEXT;
8577
633k
    SKIP_BLANKS;
8578
633k
    flags = 0;
8579
633k
    isNamespace = (((prefix == NULL) && (name == ctxt->str_xmlns)) ||
8580
633k
                   (prefix == ctxt->str_xmlns));
8581
633k
    val = xmlParseAttValueInternal(ctxt, len, &flags, special,
8582
633k
                                   isNamespace);
8583
633k
    if (val == NULL)
8584
23.2k
        goto error;
8585
8586
609k
    *alloc = (flags & XML_ATTVAL_ALLOC) != 0;
8587
8588
609k
#ifdef LIBXML_VALID_ENABLED
8589
609k
    if ((ctxt->validate) &&
8590
609k
        (ctxt->standalone) &&
8591
609k
        (special & XML_SPECIAL_EXTERNAL) &&
8592
609k
        (flags & XML_ATTVAL_NORM_CHANGE)) {
8593
0
        xmlValidityError(ctxt, XML_DTD_NOT_STANDALONE,
8594
0
                         "standalone: normalization of attribute %s on %s "
8595
0
                         "by external subset declaration\n",
8596
0
                         name, elem);
8597
0
    }
8598
609k
#endif
8599
8600
609k
    if (prefix == ctxt->str_xml) {
8601
        /*
8602
         * Check that xml:lang conforms to the specification
8603
         * No more registered as an error, just generate a warning now
8604
         * since this was deprecated in XML second edition
8605
         */
8606
16.8k
        if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) {
8607
0
            internal_val = xmlStrndup(val, *len);
8608
0
            if (internal_val == NULL)
8609
0
                goto mem_error;
8610
0
            if (!xmlCheckLanguageID(internal_val)) {
8611
0
                xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
8612
0
                              "Malformed value for xml:lang : %s\n",
8613
0
                              internal_val, NULL);
8614
0
            }
8615
0
        }
8616
8617
        /*
8618
         * Check that xml:space conforms to the specification
8619
         */
8620
16.8k
        if (xmlStrEqual(name, BAD_CAST "space")) {
8621
1.62k
            internal_val = xmlStrndup(val, *len);
8622
1.62k
            if (internal_val == NULL)
8623
0
                goto mem_error;
8624
1.62k
            if (xmlStrEqual(internal_val, BAD_CAST "default"))
8625
618
                *(ctxt->space) = 0;
8626
1.00k
            else if (xmlStrEqual(internal_val, BAD_CAST "preserve"))
8627
377
                *(ctxt->space) = 1;
8628
631
            else {
8629
631
                xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
8630
631
                              "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
8631
631
                              internal_val, NULL);
8632
631
            }
8633
1.62k
        }
8634
16.8k
        if (internal_val) {
8635
1.62k
            xmlFree(internal_val);
8636
1.62k
        }
8637
16.8k
    }
8638
8639
609k
    *value = val;
8640
609k
    return (hname);
8641
8642
0
mem_error:
8643
0
    xmlErrMemory(ctxt);
8644
67.4k
error:
8645
67.4k
    if ((val != NULL) && (*alloc != 0))
8646
0
        xmlFree(val);
8647
67.4k
    return(hname);
8648
0
}
8649
8650
/**
8651
 * Inserts a new attribute into the hash table.
8652
 *
8653
 * @param ctxt  parser context
8654
 * @param size  size of the hash table
8655
 * @param name  attribute name
8656
 * @param uri  namespace uri
8657
 * @param hashValue  combined hash value of name and uri
8658
 * @param aindex  attribute index (this is a multiple of 5)
8659
 * @returns INT_MAX if no existing attribute was found, the attribute
8660
 * index if an attribute was found, -1 if a memory allocation failed.
8661
 */
8662
static int
8663
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, unsigned size, const xmlChar *name,
8664
622k
                  const xmlChar *uri, unsigned hashValue, int aindex) {
8665
622k
    xmlAttrHashBucket *table = ctxt->attrHash;
8666
622k
    xmlAttrHashBucket *bucket;
8667
622k
    unsigned hindex;
8668
8669
622k
    hindex = hashValue & (size - 1);
8670
622k
    bucket = &table[hindex];
8671
8672
692k
    while (bucket->index >= 0) {
8673
435k
        const xmlChar **atts = &ctxt->atts[bucket->index];
8674
8675
435k
        if (name == atts[0]) {
8676
386k
            int nsIndex = XML_PTR_TO_INT(atts[2]);
8677
8678
386k
            if ((nsIndex == NS_INDEX_EMPTY) ? (uri == NULL) :
8679
386k
                (nsIndex == NS_INDEX_XML) ? (uri == ctxt->str_xml_ns) :
8680
24.9k
                (uri == ctxt->nsTab[nsIndex * 2 + 1]))
8681
365k
                return(bucket->index);
8682
386k
        }
8683
8684
70.8k
        hindex++;
8685
70.8k
        bucket++;
8686
70.8k
        if (hindex >= size) {
8687
14.5k
            hindex = 0;
8688
14.5k
            bucket = table;
8689
14.5k
        }
8690
70.8k
    }
8691
8692
256k
    bucket->index = aindex;
8693
8694
256k
    return(INT_MAX);
8695
622k
}
8696
8697
static int
8698
xmlAttrHashInsertQName(xmlParserCtxtPtr ctxt, unsigned size,
8699
                       const xmlChar *name, const xmlChar *prefix,
8700
9.53k
                       unsigned hashValue, int aindex) {
8701
9.53k
    xmlAttrHashBucket *table = ctxt->attrHash;
8702
9.53k
    xmlAttrHashBucket *bucket;
8703
9.53k
    unsigned hindex;
8704
8705
9.53k
    hindex = hashValue & (size - 1);
8706
9.53k
    bucket = &table[hindex];
8707
8708
12.8k
    while (bucket->index >= 0) {
8709
8.63k
        const xmlChar **atts = &ctxt->atts[bucket->index];
8710
8711
8.63k
        if ((name == atts[0]) && (prefix == atts[1]))
8712
5.29k
            return(bucket->index);
8713
8714
3.33k
        hindex++;
8715
3.33k
        bucket++;
8716
3.33k
        if (hindex >= size) {
8717
258
            hindex = 0;
8718
258
            bucket = table;
8719
258
        }
8720
3.33k
    }
8721
8722
4.23k
    bucket->index = aindex;
8723
8724
4.23k
    return(INT_MAX);
8725
9.53k
}
8726
/**
8727
 * Parse a start tag. Always consumes '<'.
8728
 *
8729
 * This routine is called when running SAX2 parsing
8730
 *
8731
 *     [40] STag ::= '<' Name (S Attribute)* S? '>'
8732
 *
8733
 * [ WFC: Unique Att Spec ]
8734
 * No attribute name may appear more than once in the same start-tag or
8735
 * empty-element tag.
8736
 *
8737
 *     [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
8738
 *
8739
 * [ WFC: Unique Att Spec ]
8740
 * No attribute name may appear more than once in the same start-tag or
8741
 * empty-element tag.
8742
 *
8743
 * With namespace:
8744
 *
8745
 *     [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
8746
 *
8747
 *     [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
8748
 *
8749
 * @param ctxt  an XML parser context
8750
 * @param pref  resulting namespace prefix
8751
 * @param URI  resulting namespace URI
8752
 * @param nbNsPtr  resulting number of namespace declarations
8753
 * @returns the element name parsed
8754
 */
8755
8756
static const xmlChar *
8757
xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
8758
2.21M
                  const xmlChar **URI, int *nbNsPtr) {
8759
2.21M
    xmlHashedString hlocalname;
8760
2.21M
    xmlHashedString hprefix;
8761
2.21M
    xmlHashedString hattname;
8762
2.21M
    xmlHashedString haprefix;
8763
2.21M
    const xmlChar *localname;
8764
2.21M
    const xmlChar *prefix;
8765
2.21M
    const xmlChar *attname;
8766
2.21M
    const xmlChar *aprefix;
8767
2.21M
    const xmlChar *uri;
8768
2.21M
    xmlChar *attvalue = NULL;
8769
2.21M
    const xmlChar **atts = ctxt->atts;
8770
2.21M
    unsigned attrHashSize = 0;
8771
2.21M
    int maxatts = ctxt->maxatts;
8772
2.21M
    int nratts, nbatts, nbdef;
8773
2.21M
    int i, j, nbNs, nbTotalDef, attval, nsIndex, maxAtts;
8774
2.21M
    int alloc = 0;
8775
2.21M
    int numNsErr = 0;
8776
2.21M
    int numDupErr = 0;
8777
8778
2.21M
    if (RAW != '<') return(NULL);
8779
2.21M
    NEXT1;
8780
8781
2.21M
    nbatts = 0;
8782
2.21M
    nratts = 0;
8783
2.21M
    nbdef = 0;
8784
2.21M
    nbNs = 0;
8785
2.21M
    nbTotalDef = 0;
8786
2.21M
    attval = 0;
8787
8788
2.21M
    if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
8789
0
        xmlErrMemory(ctxt);
8790
0
        return(NULL);
8791
0
    }
8792
8793
2.21M
    hlocalname = xmlParseQNameHashed(ctxt, &hprefix);
8794
2.21M
    if (hlocalname.name == NULL) {
8795
1.60M
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8796
1.60M
           "StartTag: invalid element name\n");
8797
1.60M
        return(NULL);
8798
1.60M
    }
8799
605k
    localname = hlocalname.name;
8800
605k
    prefix = hprefix.name;
8801
8802
    /*
8803
     * Now parse the attributes, it ends up with the ending
8804
     *
8805
     * (S Attribute)* S?
8806
     */
8807
605k
    SKIP_BLANKS;
8808
605k
    GROW;
8809
8810
    /*
8811
     * The ctxt->atts array will be ultimately passed to the SAX callback
8812
     * containing five xmlChar pointers for each attribute:
8813
     *
8814
     * [0] attribute name
8815
     * [1] attribute prefix
8816
     * [2] namespace URI
8817
     * [3] attribute value
8818
     * [4] end of attribute value
8819
     *
8820
     * To save memory, we reuse this array temporarily and store integers
8821
     * in these pointer variables.
8822
     *
8823
     * [0] attribute name
8824
     * [1] attribute prefix
8825
     * [2] hash value of attribute prefix, and later namespace index
8826
     * [3] for non-allocated values: ptrdiff_t offset into input buffer
8827
     * [4] for non-allocated values: ptrdiff_t offset into input buffer
8828
     *
8829
     * The ctxt->attallocs array contains an additional unsigned int for
8830
     * each attribute, containing the hash value of the attribute name
8831
     * and the alloc flag in bit 31.
8832
     */
8833
8834
1.03M
    while (((RAW != '>') &&
8835
1.03M
     ((RAW != '/') || (NXT(1) != '>')) &&
8836
1.03M
     (IS_BYTE_CHAR(RAW))) && (PARSER_STOPPED(ctxt) == 0)) {
8837
774k
  int len = -1;
8838
8839
774k
  hattname = xmlParseAttribute2(ctxt, prefix, localname,
8840
774k
                                          &haprefix, &attvalue, &len,
8841
774k
                                          &alloc);
8842
774k
        if (hattname.name == NULL)
8843
97.0k
      break;
8844
677k
        if (attvalue == NULL)
8845
67.4k
            goto next_attr;
8846
609k
        attname = hattname.name;
8847
609k
        aprefix = haprefix.name;
8848
609k
  if (len < 0) len = xmlStrlen(attvalue);
8849
8850
609k
        if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
8851
130k
            xmlHashedString huri;
8852
130k
            xmlURIPtr parsedUri;
8853
8854
130k
            huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
8855
130k
            uri = huri.name;
8856
130k
            if (uri == NULL) {
8857
0
                xmlErrMemory(ctxt);
8858
0
                goto next_attr;
8859
0
            }
8860
130k
            if (*uri != 0) {
8861
125k
                if (xmlParseURISafe((const char *) uri, &parsedUri) < 0) {
8862
0
                    xmlErrMemory(ctxt);
8863
0
                    goto next_attr;
8864
0
                }
8865
125k
                if (parsedUri == NULL) {
8866
84.5k
                    xmlNsErr(ctxt, XML_WAR_NS_URI,
8867
84.5k
                             "xmlns: '%s' is not a valid URI\n",
8868
84.5k
                                       uri, NULL, NULL);
8869
84.5k
                } else {
8870
40.8k
                    if (parsedUri->scheme == NULL) {
8871
19.3k
                        xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
8872
19.3k
                                  "xmlns: URI %s is not absolute\n",
8873
19.3k
                                  uri, NULL, NULL);
8874
19.3k
                    }
8875
40.8k
                    xmlFreeURI(parsedUri);
8876
40.8k
                }
8877
125k
                if (uri == ctxt->str_xml_ns) {
8878
726
                    if (attname != ctxt->str_xml) {
8879
726
                        xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8880
726
                     "xml namespace URI cannot be the default namespace\n",
8881
726
                                 NULL, NULL, NULL);
8882
726
                    }
8883
726
                    goto next_attr;
8884
726
                }
8885
124k
                if ((len == 29) &&
8886
124k
                    (xmlStrEqual(uri,
8887
1.81k
                             BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
8888
276
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8889
276
                         "reuse of the xmlns namespace name is forbidden\n",
8890
276
                             NULL, NULL, NULL);
8891
276
                    goto next_attr;
8892
276
                }
8893
124k
            }
8894
8895
129k
            if (xmlParserNsPush(ctxt, NULL, &huri, NULL, 0) > 0)
8896
110k
                nbNs++;
8897
479k
        } else if (aprefix == ctxt->str_xmlns) {
8898
35.5k
            xmlHashedString huri;
8899
35.5k
            xmlURIPtr parsedUri;
8900
8901
35.5k
            huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
8902
35.5k
            uri = huri.name;
8903
35.5k
            if (uri == NULL) {
8904
0
                xmlErrMemory(ctxt);
8905
0
                goto next_attr;
8906
0
            }
8907
8908
35.5k
            if (attname == ctxt->str_xml) {
8909
1.64k
                if (uri != ctxt->str_xml_ns) {
8910
896
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8911
896
                             "xml namespace prefix mapped to wrong URI\n",
8912
896
                             NULL, NULL, NULL);
8913
896
                }
8914
                /*
8915
                 * Do not keep a namespace definition node
8916
                 */
8917
1.64k
                goto next_attr;
8918
1.64k
            }
8919
33.9k
            if (uri == ctxt->str_xml_ns) {
8920
4.47k
                if (attname != ctxt->str_xml) {
8921
4.47k
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8922
4.47k
                             "xml namespace URI mapped to wrong prefix\n",
8923
4.47k
                             NULL, NULL, NULL);
8924
4.47k
                }
8925
4.47k
                goto next_attr;
8926
4.47k
            }
8927
29.4k
            if (attname == ctxt->str_xmlns) {
8928
7.75k
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8929
7.75k
                         "redefinition of the xmlns prefix is forbidden\n",
8930
7.75k
                         NULL, NULL, NULL);
8931
7.75k
                goto next_attr;
8932
7.75k
            }
8933
21.6k
            if ((len == 29) &&
8934
21.6k
                (xmlStrEqual(uri,
8935
919
                             BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
8936
343
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8937
343
                         "reuse of the xmlns namespace name is forbidden\n",
8938
343
                         NULL, NULL, NULL);
8939
343
                goto next_attr;
8940
343
            }
8941
21.3k
            if ((uri == NULL) || (uri[0] == 0)) {
8942
721
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8943
721
                         "xmlns:%s: Empty XML namespace is not allowed\n",
8944
721
                              attname, NULL, NULL);
8945
721
                goto next_attr;
8946
20.6k
            } else {
8947
20.6k
                if (xmlParseURISafe((const char *) uri, &parsedUri) < 0) {
8948
0
                    xmlErrMemory(ctxt);
8949
0
                    goto next_attr;
8950
0
                }
8951
20.6k
                if (parsedUri == NULL) {
8952
9.56k
                    xmlNsErr(ctxt, XML_WAR_NS_URI,
8953
9.56k
                         "xmlns:%s: '%s' is not a valid URI\n",
8954
9.56k
                                       attname, uri, NULL);
8955
11.0k
                } else {
8956
11.0k
                    if ((ctxt->pedantic) && (parsedUri->scheme == NULL)) {
8957
0
                        xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
8958
0
                                  "xmlns:%s: URI %s is not absolute\n",
8959
0
                                  attname, uri, NULL);
8960
0
                    }
8961
11.0k
                    xmlFreeURI(parsedUri);
8962
11.0k
                }
8963
20.6k
            }
8964
8965
20.6k
            if (xmlParserNsPush(ctxt, &hattname, &huri, NULL, 0) > 0)
8966
19.9k
                nbNs++;
8967
443k
        } else {
8968
            /*
8969
             * Populate attributes array, see above for repurposing
8970
             * of xmlChar pointers.
8971
             */
8972
443k
            if ((atts == NULL) || (nbatts + 5 > maxatts)) {
8973
4.10k
                int res = xmlCtxtGrowAttrs(ctxt);
8974
8975
4.10k
                maxatts = ctxt->maxatts;
8976
4.10k
                atts = ctxt->atts;
8977
8978
4.10k
                if (res < 0)
8979
0
                    goto next_attr;
8980
4.10k
            }
8981
443k
            ctxt->attallocs[nratts++] = (hattname.hashValue & 0x7FFFFFFF) |
8982
443k
                                        ((unsigned) alloc << 31);
8983
443k
            atts[nbatts++] = attname;
8984
443k
            atts[nbatts++] = aprefix;
8985
443k
            atts[nbatts++] = XML_INT_TO_PTR(haprefix.hashValue);
8986
443k
            if (alloc) {
8987
55.1k
                atts[nbatts++] = attvalue;
8988
55.1k
                attvalue += len;
8989
55.1k
                atts[nbatts++] = attvalue;
8990
388k
            } else {
8991
                /*
8992
                 * attvalue points into the input buffer which can be
8993
                 * reallocated. Store differences to input->base instead.
8994
                 * The pointers will be reconstructed later.
8995
                 */
8996
388k
                atts[nbatts++] = XML_INT_TO_PTR(attvalue - BASE_PTR);
8997
388k
                attvalue += len;
8998
388k
                atts[nbatts++] = XML_INT_TO_PTR(attvalue - BASE_PTR);
8999
388k
            }
9000
            /*
9001
             * tag if some deallocation is needed
9002
             */
9003
443k
            if (alloc != 0) attval = 1;
9004
443k
            attvalue = NULL; /* moved into atts */
9005
443k
        }
9006
9007
677k
next_attr:
9008
677k
        if ((attvalue != NULL) && (alloc != 0)) {
9009
46.5k
            xmlFree(attvalue);
9010
46.5k
            attvalue = NULL;
9011
46.5k
        }
9012
9013
677k
  GROW
9014
677k
  if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
9015
92.3k
      break;
9016
584k
  if (SKIP_BLANKS == 0) {
9017
153k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
9018
153k
         "attributes construct error\n");
9019
153k
      break;
9020
153k
  }
9021
431k
        GROW;
9022
431k
    }
9023
9024
    /*
9025
     * Namespaces from default attributes
9026
     */
9027
605k
    if (ctxt->attsDefault != NULL) {
9028
232k
        xmlDefAttrsPtr defaults;
9029
9030
232k
  defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
9031
232k
  if (defaults != NULL) {
9032
956k
      for (i = 0; i < defaults->nbAttrs; i++) {
9033
831k
                xmlDefAttr *attr = &defaults->attrs[i];
9034
9035
831k
          attname = attr->name.name;
9036
831k
    aprefix = attr->prefix.name;
9037
9038
831k
    if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
9039
6.94k
                    xmlParserEntityCheck(ctxt, attr->expandedSize);
9040
9041
6.94k
                    if (xmlParserNsPush(ctxt, NULL, &attr->value, NULL, 1) > 0)
9042
6.33k
                        nbNs++;
9043
824k
    } else if (aprefix == ctxt->str_xmlns) {
9044
544k
                    xmlParserEntityCheck(ctxt, attr->expandedSize);
9045
9046
544k
                    if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
9047
544k
                                      NULL, 1) > 0)
9048
544k
                        nbNs++;
9049
544k
    } else {
9050
280k
                    if (nratts + nbTotalDef >= XML_MAX_ATTRS) {
9051
0
                        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
9052
0
                                    "Maximum number of attributes exceeded");
9053
0
                        break;
9054
0
                    }
9055
280k
                    nbTotalDef += 1;
9056
280k
                }
9057
831k
      }
9058
124k
  }
9059
232k
    }
9060
9061
    /*
9062
     * Resolve attribute namespaces
9063
     */
9064
1.04M
    for (i = 0; i < nbatts; i += 5) {
9065
443k
        attname = atts[i];
9066
443k
        aprefix = atts[i+1];
9067
9068
        /*
9069
  * The default namespace does not apply to attribute names.
9070
  */
9071
443k
  if (aprefix == NULL) {
9072
392k
            nsIndex = NS_INDEX_EMPTY;
9073
392k
        } else if (aprefix == ctxt->str_xml) {
9074
16.8k
            nsIndex = NS_INDEX_XML;
9075
34.1k
        } else {
9076
34.1k
            haprefix.name = aprefix;
9077
34.1k
            haprefix.hashValue = (size_t) atts[i+2];
9078
34.1k
            nsIndex = xmlParserNsLookup(ctxt, &haprefix, NULL);
9079
9080
34.1k
      if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex)) {
9081
18.6k
                xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9082
18.6k
        "Namespace prefix %s for %s on %s is not defined\n",
9083
18.6k
        aprefix, attname, localname);
9084
18.6k
                nsIndex = NS_INDEX_EMPTY;
9085
18.6k
            }
9086
34.1k
        }
9087
9088
443k
        atts[i+2] = XML_INT_TO_PTR(nsIndex);
9089
443k
    }
9090
9091
    /*
9092
     * Maximum number of attributes including default attributes.
9093
     */
9094
605k
    maxAtts = nratts + nbTotalDef;
9095
9096
    /*
9097
     * Verify that attribute names are unique.
9098
     */
9099
605k
    if (maxAtts > 1) {
9100
85.2k
        attrHashSize = 4;
9101
159k
        while (attrHashSize / 2 < (unsigned) maxAtts)
9102
73.9k
            attrHashSize *= 2;
9103
9104
85.2k
        if (attrHashSize > ctxt->attrHashMax) {
9105
1.28k
            xmlAttrHashBucket *tmp;
9106
9107
1.28k
            tmp = xmlRealloc(ctxt->attrHash, attrHashSize * sizeof(tmp[0]));
9108
1.28k
            if (tmp == NULL) {
9109
0
                xmlErrMemory(ctxt);
9110
0
                goto done;
9111
0
            }
9112
9113
1.28k
            ctxt->attrHash = tmp;
9114
1.28k
            ctxt->attrHashMax = attrHashSize;
9115
1.28k
        }
9116
9117
85.2k
        memset(ctxt->attrHash, -1, attrHashSize * sizeof(ctxt->attrHash[0]));
9118
9119
487k
        for (i = 0, j = 0; j < nratts; i += 5, j++) {
9120
402k
            const xmlChar *nsuri;
9121
402k
            unsigned hashValue, nameHashValue, uriHashValue;
9122
402k
            int res;
9123
9124
402k
            attname = atts[i];
9125
402k
            aprefix = atts[i+1];
9126
402k
            nsIndex = XML_PTR_TO_INT(atts[i+2]);
9127
            /* Hash values always have bit 31 set, see dict.c */
9128
402k
            nameHashValue = ctxt->attallocs[j] | 0x80000000;
9129
9130
402k
            if (nsIndex == NS_INDEX_EMPTY) {
9131
                /*
9132
                 * Prefix with empty namespace means an undeclared
9133
                 * prefix which was already reported above.
9134
                 */
9135
382k
                if (aprefix != NULL)
9136
15.0k
                    continue;
9137
367k
                nsuri = NULL;
9138
367k
                uriHashValue = URI_HASH_EMPTY;
9139
367k
            } else if (nsIndex == NS_INDEX_XML) {
9140
4.16k
                nsuri = ctxt->str_xml_ns;
9141
4.16k
                uriHashValue = URI_HASH_XML;
9142
15.1k
            } else {
9143
15.1k
                nsuri = ctxt->nsTab[nsIndex * 2 + 1];
9144
15.1k
                uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
9145
15.1k
            }
9146
9147
387k
            hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
9148
387k
            res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
9149
387k
                                    hashValue, i);
9150
387k
            if (res < 0)
9151
0
                continue;
9152
9153
            /*
9154
             * [ WFC: Unique Att Spec ]
9155
             * No attribute name may appear more than once in the same
9156
             * start-tag or empty-element tag.
9157
             * As extended by the Namespace in XML REC.
9158
             */
9159
387k
            if (res < INT_MAX) {
9160
351k
                if (aprefix == atts[res+1]) {
9161
344k
                    xmlErrAttributeDup(ctxt, aprefix, attname);
9162
344k
                    numDupErr += 1;
9163
344k
                } else {
9164
6.69k
                    xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
9165
6.69k
                             "Namespaced Attribute %s in '%s' redefined\n",
9166
6.69k
                             attname, nsuri, NULL);
9167
6.69k
                    numNsErr += 1;
9168
6.69k
                }
9169
351k
            }
9170
387k
        }
9171
85.2k
    }
9172
9173
    /*
9174
     * Default attributes
9175
     */
9176
605k
    if (ctxt->attsDefault != NULL) {
9177
232k
        xmlDefAttrsPtr defaults;
9178
9179
232k
  defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
9180
232k
  if (defaults != NULL) {
9181
956k
      for (i = 0; i < defaults->nbAttrs; i++) {
9182
831k
                xmlDefAttr *attr = &defaults->attrs[i];
9183
831k
                const xmlChar *nsuri = NULL;
9184
831k
                unsigned hashValue, uriHashValue = 0;
9185
831k
                int res;
9186
9187
831k
          attname = attr->name.name;
9188
831k
    aprefix = attr->prefix.name;
9189
9190
831k
    if ((attname == ctxt->str_xmlns) && (aprefix == NULL))
9191
6.94k
                    continue;
9192
824k
    if (aprefix == ctxt->str_xmlns)
9193
544k
                    continue;
9194
9195
280k
                if (aprefix == NULL) {
9196
35.7k
                    nsIndex = NS_INDEX_EMPTY;
9197
35.7k
                    nsuri = NULL;
9198
35.7k
                    uriHashValue = URI_HASH_EMPTY;
9199
244k
                } else if (aprefix == ctxt->str_xml) {
9200
26.3k
                    nsIndex = NS_INDEX_XML;
9201
26.3k
                    nsuri = ctxt->str_xml_ns;
9202
26.3k
                    uriHashValue = URI_HASH_XML;
9203
217k
                } else {
9204
217k
                    nsIndex = xmlParserNsLookup(ctxt, &attr->prefix, NULL);
9205
217k
                    if ((nsIndex == INT_MAX) ||
9206
217k
                        (nsIndex < ctxt->nsdb->minNsIndex)) {
9207
163k
                        xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9208
163k
                                 "Namespace prefix %s for %s on %s is not "
9209
163k
                                 "defined\n",
9210
163k
                                 aprefix, attname, localname);
9211
163k
                        nsIndex = NS_INDEX_EMPTY;
9212
163k
                        nsuri = NULL;
9213
163k
                        uriHashValue = URI_HASH_EMPTY;
9214
163k
                    } else {
9215
54.7k
                        nsuri = ctxt->nsTab[nsIndex * 2 + 1];
9216
54.7k
                        uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
9217
54.7k
                    }
9218
217k
                }
9219
9220
                /*
9221
                 * Check whether the attribute exists
9222
                 */
9223
280k
                if (maxAtts > 1) {
9224
234k
                    hashValue = xmlDictCombineHash(attr->name.hashValue,
9225
234k
                                                   uriHashValue);
9226
234k
                    res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
9227
234k
                                            hashValue, nbatts);
9228
234k
                    if (res < 0)
9229
0
                        continue;
9230
234k
                    if (res < INT_MAX) {
9231
13.6k
                        if (aprefix == atts[res+1])
9232
3.27k
                            continue;
9233
10.4k
                        xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
9234
10.4k
                                 "Namespaced Attribute %s in '%s' redefined\n",
9235
10.4k
                                 attname, nsuri, NULL);
9236
10.4k
                    }
9237
234k
                }
9238
9239
276k
                xmlParserEntityCheck(ctxt, attr->expandedSize);
9240
9241
276k
                if ((atts == NULL) || (nbatts + 5 > maxatts)) {
9242
1.54k
                    res = xmlCtxtGrowAttrs(ctxt);
9243
9244
1.54k
                    maxatts = ctxt->maxatts;
9245
1.54k
                    atts = ctxt->atts;
9246
9247
1.54k
                    if (res < 0) {
9248
0
                        localname = NULL;
9249
0
                        goto done;
9250
0
                    }
9251
1.54k
                }
9252
9253
276k
                atts[nbatts++] = attname;
9254
276k
                atts[nbatts++] = aprefix;
9255
276k
                atts[nbatts++] = XML_INT_TO_PTR(nsIndex);
9256
276k
                atts[nbatts++] = attr->value.name;
9257
276k
                atts[nbatts++] = attr->valueEnd;
9258
9259
276k
#ifdef LIBXML_VALID_ENABLED
9260
                /*
9261
                 * This should be moved to valid.c, but we don't keep track
9262
                 * whether an attribute was defaulted.
9263
                 */
9264
276k
                if ((ctxt->validate) &&
9265
276k
                    (ctxt->standalone == 1) &&
9266
276k
                    (attr->external != 0)) {
9267
0
                    xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED,
9268
0
                            "standalone: attribute %s on %s defaulted "
9269
0
                            "from external subset\n",
9270
0
                            attname, localname);
9271
0
                }
9272
276k
#endif
9273
276k
                nbdef++;
9274
276k
      }
9275
124k
  }
9276
232k
    }
9277
9278
    /*
9279
     * Using a single hash table for nsUri/localName pairs cannot
9280
     * detect duplicate QNames reliably. The following example will
9281
     * only result in two namespace errors.
9282
     *
9283
     * <doc xmlns:a="a" xmlns:b="a">
9284
     *   <elem a:a="" b:a="" b:a=""/>
9285
     * </doc>
9286
     *
9287
     * If we saw more than one namespace error but no duplicate QNames
9288
     * were found, we have to scan for duplicate QNames.
9289
     */
9290
605k
    if ((numDupErr == 0) && (numNsErr > 1)) {
9291
1.22k
        memset(ctxt->attrHash, -1,
9292
1.22k
               attrHashSize * sizeof(ctxt->attrHash[0]));
9293
9294
11.8k
        for (i = 0, j = 0; j < nratts; i += 5, j++) {
9295
10.5k
            unsigned hashValue, nameHashValue, prefixHashValue;
9296
10.5k
            int res;
9297
9298
10.5k
            aprefix = atts[i+1];
9299
10.5k
            if (aprefix == NULL)
9300
1.06k
                continue;
9301
9302
9.53k
            attname = atts[i];
9303
            /* Hash values always have bit 31 set, see dict.c */
9304
9.53k
            nameHashValue = ctxt->attallocs[j] | 0x80000000;
9305
9.53k
            prefixHashValue = xmlDictComputeHash(ctxt->dict, aprefix);
9306
9307
9.53k
            hashValue = xmlDictCombineHash(nameHashValue, prefixHashValue);
9308
9.53k
            res = xmlAttrHashInsertQName(ctxt, attrHashSize, attname,
9309
9.53k
                                         aprefix, hashValue, i);
9310
9.53k
            if (res < INT_MAX)
9311
5.29k
                xmlErrAttributeDup(ctxt, aprefix, attname);
9312
9.53k
        }
9313
1.22k
    }
9314
9315
    /*
9316
     * Reconstruct attribute pointers
9317
     */
9318
1.32M
    for (i = 0, j = 0; i < nbatts; i += 5, j++) {
9319
        /* namespace URI */
9320
720k
        nsIndex = XML_PTR_TO_INT(atts[i+2]);
9321
720k
        if (nsIndex == INT_MAX)
9322
608k
            atts[i+2] = NULL;
9323
112k
        else if (nsIndex == INT_MAX - 1)
9324
42.6k
            atts[i+2] = ctxt->str_xml_ns;
9325
69.5k
        else
9326
69.5k
            atts[i+2] = ctxt->nsTab[nsIndex * 2 + 1];
9327
9328
720k
        if ((j < nratts) && (ctxt->attallocs[j] & 0x80000000) == 0) {
9329
388k
            atts[i+3] = BASE_PTR + XML_PTR_TO_INT(atts[i+3]);  /* value */
9330
388k
            atts[i+4] = BASE_PTR + XML_PTR_TO_INT(atts[i+4]);  /* valuend */
9331
388k
        }
9332
720k
    }
9333
9334
605k
    uri = xmlParserNsLookupUri(ctxt, &hprefix);
9335
605k
    if ((prefix != NULL) && (uri == NULL)) {
9336
24.2k
  xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9337
24.2k
           "Namespace prefix %s on %s is not defined\n",
9338
24.2k
     prefix, localname, NULL);
9339
24.2k
    }
9340
605k
    *pref = prefix;
9341
605k
    *URI = uri;
9342
9343
    /*
9344
     * SAX callback
9345
     */
9346
605k
    if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
9347
605k
  (!ctxt->disableSAX)) {
9348
56.0k
  if (nbNs > 0)
9349
24.5k
      ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
9350
24.5k
                          nbNs, ctxt->nsTab + 2 * (ctxt->nsNr - nbNs),
9351
24.5k
        nbatts / 5, nbdef, atts);
9352
31.5k
  else
9353
31.5k
      ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
9354
31.5k
                          0, NULL, nbatts / 5, nbdef, atts);
9355
56.0k
    }
9356
9357
605k
done:
9358
    /*
9359
     * Free allocated attribute values
9360
     */
9361
605k
    if (attval != 0) {
9362
405k
  for (i = 0, j = 0; j < nratts; i += 5, j++)
9363
370k
      if (ctxt->attallocs[j] & 0x80000000)
9364
55.1k
          xmlFree((xmlChar *) atts[i+3]);
9365
34.6k
    }
9366
9367
605k
    *nbNsPtr = nbNs;
9368
605k
    return(localname);
9369
605k
}
9370
9371
/**
9372
 * Parse an end tag. Always consumes '</'.
9373
 *
9374
 *     [42] ETag ::= '</' Name S? '>'
9375
 *
9376
 * With namespace
9377
 *
9378
 *     [NS 9] ETag ::= '</' QName S? '>'
9379
 * @param ctxt  an XML parser context
9380
 * @param tag  the corresponding start tag
9381
 */
9382
9383
static void
9384
35.9k
xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlStartTag *tag) {
9385
35.9k
    const xmlChar *name;
9386
9387
35.9k
    GROW;
9388
35.9k
    if ((RAW != '<') || (NXT(1) != '/')) {
9389
148
  xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
9390
148
  return;
9391
148
    }
9392
35.7k
    SKIP(2);
9393
9394
35.7k
    if (tag->prefix == NULL)
9395
22.1k
        name = xmlParseNameAndCompare(ctxt, ctxt->name);
9396
13.6k
    else
9397
13.6k
        name = xmlParseQNameAndCompare(ctxt, ctxt->name, tag->prefix);
9398
9399
    /*
9400
     * We should definitely be at the ending "S? '>'" part
9401
     */
9402
35.7k
    GROW;
9403
35.7k
    SKIP_BLANKS;
9404
35.7k
    if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
9405
27.3k
  xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
9406
27.3k
    } else
9407
8.43k
  NEXT1;
9408
9409
    /*
9410
     * [ WFC: Element Type Match ]
9411
     * The Name in an element's end-tag must match the element type in the
9412
     * start-tag.
9413
     *
9414
     */
9415
35.7k
    if (name != (xmlChar*)1) {
9416
20.7k
        if (name == NULL) name = BAD_CAST "unparsable";
9417
20.7k
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
9418
20.7k
         "Opening and ending tag mismatch: %s line %d and %s\n",
9419
20.7k
                    ctxt->name, tag->line, name);
9420
20.7k
    }
9421
9422
    /*
9423
     * SAX: End of Tag
9424
     */
9425
35.7k
    if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
9426
35.7k
  (!ctxt->disableSAX))
9427
868
  ctxt->sax->endElementNs(ctxt->userData, ctxt->name, tag->prefix,
9428
868
                                tag->URI);
9429
9430
35.7k
    spacePop(ctxt);
9431
35.7k
    if (tag->nsNr != 0)
9432
4.90k
  xmlParserNsPop(ctxt, tag->nsNr);
9433
35.7k
}
9434
9435
/**
9436
 * Parse escaped pure raw content. Always consumes '<!['.
9437
 *
9438
 * @deprecated Internal function, don't use.
9439
 *
9440
 *     [18] CDSect ::= CDStart CData CDEnd
9441
 *
9442
 *     [19] CDStart ::= '<![CDATA['
9443
 *
9444
 *     [20] Data ::= (Char* - (Char* ']]>' Char*))
9445
 *
9446
 *     [21] CDEnd ::= ']]>'
9447
 * @param ctxt  an XML parser context
9448
 */
9449
void
9450
29.8k
xmlParseCDSect(xmlParserCtxt *ctxt) {
9451
29.8k
    xmlChar *buf = NULL;
9452
29.8k
    int len = 0;
9453
29.8k
    int size = XML_PARSER_BUFFER_SIZE;
9454
29.8k
    int r, rl;
9455
29.8k
    int s, sl;
9456
29.8k
    int cur, l;
9457
29.8k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9458
29.8k
                    XML_MAX_HUGE_LENGTH :
9459
29.8k
                    XML_MAX_TEXT_LENGTH;
9460
9461
29.8k
    if ((CUR != '<') || (NXT(1) != '!') || (NXT(2) != '['))
9462
0
        return;
9463
29.8k
    SKIP(3);
9464
9465
29.8k
    if (!CMP6(CUR_PTR, 'C', 'D', 'A', 'T', 'A', '['))
9466
0
        return;
9467
29.8k
    SKIP(6);
9468
9469
29.8k
    r = xmlCurrentCharRecover(ctxt, &rl);
9470
29.8k
    if (!IS_CHAR(r)) {
9471
5.52k
  xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
9472
5.52k
        goto out;
9473
5.52k
    }
9474
24.2k
    NEXTL(rl);
9475
24.2k
    s = xmlCurrentCharRecover(ctxt, &sl);
9476
24.2k
    if (!IS_CHAR(s)) {
9477
11.4k
  xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
9478
11.4k
        goto out;
9479
11.4k
    }
9480
12.8k
    NEXTL(sl);
9481
12.8k
    cur = xmlCurrentCharRecover(ctxt, &l);
9482
12.8k
    buf = xmlMalloc(size);
9483
12.8k
    if (buf == NULL) {
9484
0
  xmlErrMemory(ctxt);
9485
0
        goto out;
9486
0
    }
9487
4.11M
    while (IS_CHAR(cur) &&
9488
4.11M
           ((r != ']') || (s != ']') || (cur != '>'))) {
9489
4.10M
  if (len + 5 >= size) {
9490
7.31k
      xmlChar *tmp;
9491
7.31k
            int newSize;
9492
9493
7.31k
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
9494
7.31k
            if (newSize < 0) {
9495
0
                xmlFatalErrMsg(ctxt, XML_ERR_CDATA_NOT_FINISHED,
9496
0
                               "CData section too big found\n");
9497
0
                goto out;
9498
0
            }
9499
7.31k
      tmp = xmlRealloc(buf, newSize);
9500
7.31k
      if (tmp == NULL) {
9501
0
    xmlErrMemory(ctxt);
9502
0
                goto out;
9503
0
      }
9504
7.31k
      buf = tmp;
9505
7.31k
      size = newSize;
9506
7.31k
  }
9507
4.10M
  COPY_BUF(buf, len, r);
9508
4.10M
  r = s;
9509
4.10M
  rl = sl;
9510
4.10M
  s = cur;
9511
4.10M
  sl = l;
9512
4.10M
  NEXTL(l);
9513
4.10M
  cur = xmlCurrentCharRecover(ctxt, &l);
9514
4.10M
    }
9515
12.8k
    buf[len] = 0;
9516
12.8k
    if (cur != '>') {
9517
8.51k
  xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
9518
8.51k
                       "CData section not finished\n%.50s\n", buf);
9519
8.51k
        goto out;
9520
8.51k
    }
9521
4.28k
    NEXTL(l);
9522
9523
    /*
9524
     * OK the buffer is to be consumed as cdata.
9525
     */
9526
4.28k
    if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9527
3.40k
        if ((ctxt->sax->cdataBlock != NULL) &&
9528
3.40k
            ((ctxt->options & XML_PARSE_NOCDATA) == 0)) {
9529
3.40k
            ctxt->sax->cdataBlock(ctxt->userData, buf, len);
9530
3.40k
        } else if (ctxt->sax->characters != NULL) {
9531
0
            ctxt->sax->characters(ctxt->userData, buf, len);
9532
0
        }
9533
3.40k
    }
9534
9535
29.8k
out:
9536
29.8k
    xmlFree(buf);
9537
29.8k
}
9538
9539
/**
9540
 * Parse a content sequence. Stops at EOF or '</'. Leaves checking of
9541
 * unexpected EOF to the caller.
9542
 *
9543
 * @param ctxt  an XML parser context
9544
 */
9545
9546
static void
9547
12.9k
xmlParseContentInternal(xmlParserCtxtPtr ctxt) {
9548
12.9k
    int oldNameNr = ctxt->nameNr;
9549
12.9k
    int oldSpaceNr = ctxt->spaceNr;
9550
12.9k
    int oldNodeNr = ctxt->nodeNr;
9551
9552
12.9k
    GROW;
9553
5.96M
    while ((ctxt->input->cur < ctxt->input->end) &&
9554
5.96M
     (PARSER_STOPPED(ctxt) == 0)) {
9555
5.95M
  const xmlChar *cur = ctxt->input->cur;
9556
9557
  /*
9558
   * First case : a Processing Instruction.
9559
   */
9560
5.95M
  if ((*cur == '<') && (cur[1] == '?')) {
9561
27.8k
      xmlParsePI(ctxt);
9562
27.8k
  }
9563
9564
  /*
9565
   * Second case : a CDSection
9566
   */
9567
  /* 2.6.0 test was *cur not RAW */
9568
5.92M
  else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
9569
29.8k
      xmlParseCDSect(ctxt);
9570
29.8k
  }
9571
9572
  /*
9573
   * Third case :  a comment
9574
   */
9575
5.89M
  else if ((*cur == '<') && (NXT(1) == '!') &&
9576
5.89M
     (NXT(2) == '-') && (NXT(3) == '-')) {
9577
42.4k
      xmlParseComment(ctxt);
9578
42.4k
  }
9579
9580
  /*
9581
   * Fourth case :  a sub-element.
9582
   */
9583
5.85M
  else if (*cur == '<') {
9584
2.24M
            if (NXT(1) == '/') {
9585
35.7k
                if (ctxt->nameNr <= oldNameNr)
9586
206
                    break;
9587
35.5k
          xmlParseElementEnd(ctxt);
9588
2.20M
            } else {
9589
2.20M
          xmlParseElementStart(ctxt);
9590
2.20M
            }
9591
2.24M
  }
9592
9593
  /*
9594
   * Fifth case : a reference. If if has not been resolved,
9595
   *    parsing returns it's Name, create the node
9596
   */
9597
9598
3.61M
  else if (*cur == '&') {
9599
166k
      xmlParseReference(ctxt);
9600
166k
  }
9601
9602
  /*
9603
   * Last case, text. Note that References are handled directly.
9604
   */
9605
3.44M
  else {
9606
3.44M
      xmlParseCharDataInternal(ctxt, 0);
9607
3.44M
  }
9608
9609
5.95M
  SHRINK;
9610
5.95M
  GROW;
9611
5.95M
    }
9612
9613
12.9k
    if ((ctxt->nameNr > oldNameNr) &&
9614
12.9k
        (ctxt->input->cur >= ctxt->input->end) &&
9615
12.9k
        (ctxt->wellFormed)) {
9616
186
        const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
9617
186
        int line = ctxt->pushTab[ctxt->nameNr - 1].line;
9618
186
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
9619
186
                "Premature end of data in tag %s line %d\n",
9620
186
                name, line, NULL);
9621
186
    }
9622
9623
    /*
9624
     * Clean up in error case
9625
     */
9626
9627
58.6k
    while (ctxt->nodeNr > oldNodeNr)
9628
45.6k
        nodePop(ctxt);
9629
9630
291k
    while (ctxt->nameNr > oldNameNr) {
9631
278k
        xmlStartTag *tag = &ctxt->pushTab[ctxt->nameNr - 1];
9632
9633
278k
        if (tag->nsNr != 0)
9634
130k
            xmlParserNsPop(ctxt, tag->nsNr);
9635
9636
278k
        namePop(ctxt);
9637
278k
    }
9638
9639
291k
    while (ctxt->spaceNr > oldSpaceNr)
9640
278k
        spacePop(ctxt);
9641
12.9k
}
9642
9643
/**
9644
 * Parse XML element content. This is useful if you're only interested
9645
 * in custom SAX callbacks. If you want a node list, use
9646
 * #xmlCtxtParseContent.
9647
 *
9648
 * @param ctxt  an XML parser context
9649
 */
9650
void
9651
0
xmlParseContent(xmlParserCtxt *ctxt) {
9652
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
9653
0
        return;
9654
9655
0
    xmlCtxtInitializeLate(ctxt);
9656
9657
0
    xmlParseContentInternal(ctxt);
9658
9659
0
    xmlParserCheckEOF(ctxt, XML_ERR_NOT_WELL_BALANCED);
9660
0
}
9661
9662
/**
9663
 * parse an XML element
9664
 *
9665
 * @deprecated Internal function, don't use.
9666
 *
9667
 *     [39] element ::= EmptyElemTag | STag content ETag
9668
 *
9669
 * [ WFC: Element Type Match ]
9670
 * The Name in an element's end-tag must match the element type in the
9671
 * start-tag.
9672
 *
9673
 * @param ctxt  an XML parser context
9674
 */
9675
9676
void
9677
9.43k
xmlParseElement(xmlParserCtxt *ctxt) {
9678
9.43k
    if (xmlParseElementStart(ctxt) != 0)
9679
3.62k
        return;
9680
9681
5.81k
    xmlParseContentInternal(ctxt);
9682
9683
5.81k
    if (ctxt->input->cur >= ctxt->input->end) {
9684
5.46k
        if (ctxt->wellFormed) {
9685
450
            const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
9686
450
            int line = ctxt->pushTab[ctxt->nameNr - 1].line;
9687
450
            xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
9688
450
                    "Premature end of data in tag %s line %d\n",
9689
450
                    name, line, NULL);
9690
450
        }
9691
5.46k
        return;
9692
5.46k
    }
9693
9694
353
    xmlParseElementEnd(ctxt);
9695
353
}
9696
9697
/**
9698
 * Parse the start of an XML element. Returns -1 in case of error, 0 if an
9699
 * opening tag was parsed, 1 if an empty element was parsed.
9700
 *
9701
 * Always consumes '<'.
9702
 *
9703
 * @param ctxt  an XML parser context
9704
 */
9705
static int
9706
2.21M
xmlParseElementStart(xmlParserCtxtPtr ctxt) {
9707
2.21M
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
9708
2.21M
    const xmlChar *name;
9709
2.21M
    const xmlChar *prefix = NULL;
9710
2.21M
    const xmlChar *URI = NULL;
9711
2.21M
    xmlParserNodeInfo node_info;
9712
2.21M
    int line;
9713
2.21M
    xmlNodePtr cur;
9714
2.21M
    int nbNs = 0;
9715
9716
2.21M
    if (ctxt->nameNr > maxDepth) {
9717
11
        xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
9718
11
                "Excessive depth in document: %d use XML_PARSE_HUGE option\n",
9719
11
                ctxt->nameNr);
9720
11
  xmlHaltParser(ctxt);
9721
11
  return(-1);
9722
11
    }
9723
9724
    /* Capture start position */
9725
2.21M
    if (ctxt->record_info) {
9726
0
        node_info.begin_pos = ctxt->input->consumed +
9727
0
                          (CUR_PTR - ctxt->input->base);
9728
0
  node_info.begin_line = ctxt->input->line;
9729
0
    }
9730
9731
2.21M
    if (ctxt->spaceNr == 0)
9732
0
  spacePush(ctxt, -1);
9733
2.21M
    else if (*ctxt->space == -2)
9734
0
  spacePush(ctxt, -1);
9735
2.21M
    else
9736
2.21M
  spacePush(ctxt, *ctxt->space);
9737
9738
2.21M
    line = ctxt->input->line;
9739
2.21M
#ifdef LIBXML_SAX1_ENABLED
9740
2.21M
    if (ctxt->sax2)
9741
2.21M
#endif /* LIBXML_SAX1_ENABLED */
9742
2.21M
        name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
9743
0
#ifdef LIBXML_SAX1_ENABLED
9744
0
    else
9745
0
  name = xmlParseStartTag(ctxt);
9746
2.21M
#endif /* LIBXML_SAX1_ENABLED */
9747
2.21M
    if (name == NULL) {
9748
1.60M
  spacePop(ctxt);
9749
1.60M
        return(-1);
9750
1.60M
    }
9751
605k
    nameNsPush(ctxt, name, prefix, URI, line, nbNs);
9752
605k
    cur = ctxt->node;
9753
9754
605k
#ifdef LIBXML_VALID_ENABLED
9755
    /*
9756
     * [ VC: Root Element Type ]
9757
     * The Name in the document type declaration must match the element
9758
     * type of the root element.
9759
     */
9760
605k
    if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9761
605k
        ctxt->node && (ctxt->node == ctxt->myDoc->children))
9762
0
        ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
9763
605k
#endif /* LIBXML_VALID_ENABLED */
9764
9765
    /*
9766
     * Check for an Empty Element.
9767
     */
9768
605k
    if ((RAW == '/') && (NXT(1) == '>')) {
9769
23.4k
        SKIP(2);
9770
23.4k
  if (ctxt->sax2) {
9771
23.4k
      if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
9772
23.4k
    (!ctxt->disableSAX))
9773
3.31k
    ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
9774
23.4k
#ifdef LIBXML_SAX1_ENABLED
9775
23.4k
  } else {
9776
0
      if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
9777
0
    (!ctxt->disableSAX))
9778
0
    ctxt->sax->endElement(ctxt->userData, name);
9779
0
#endif /* LIBXML_SAX1_ENABLED */
9780
0
  }
9781
23.4k
  namePop(ctxt);
9782
23.4k
  spacePop(ctxt);
9783
23.4k
  if (nbNs > 0)
9784
3.20k
      xmlParserNsPop(ctxt, nbNs);
9785
23.4k
  if (cur != NULL && ctxt->record_info) {
9786
0
            node_info.node = cur;
9787
0
            node_info.end_pos = ctxt->input->consumed +
9788
0
                                (CUR_PTR - ctxt->input->base);
9789
0
            node_info.end_line = ctxt->input->line;
9790
0
            xmlParserAddNodeInfo(ctxt, &node_info);
9791
0
  }
9792
23.4k
  return(1);
9793
23.4k
    }
9794
582k
    if (RAW == '>') {
9795
319k
        NEXT1;
9796
319k
        if (cur != NULL && ctxt->record_info) {
9797
0
            node_info.node = cur;
9798
0
            node_info.end_pos = 0;
9799
0
            node_info.end_line = 0;
9800
0
            xmlParserAddNodeInfo(ctxt, &node_info);
9801
0
        }
9802
319k
    } else {
9803
262k
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
9804
262k
         "Couldn't find end of Start Tag %s line %d\n",
9805
262k
                    name, line, NULL);
9806
9807
  /*
9808
   * end of parsing of this node.
9809
   */
9810
262k
  nodePop(ctxt);
9811
262k
  namePop(ctxt);
9812
262k
  spacePop(ctxt);
9813
262k
  if (nbNs > 0)
9814
89.7k
      xmlParserNsPop(ctxt, nbNs);
9815
262k
  return(-1);
9816
262k
    }
9817
9818
319k
    return(0);
9819
582k
}
9820
9821
/**
9822
 * Parse the end of an XML element. Always consumes '</'.
9823
 *
9824
 * @param ctxt  an XML parser context
9825
 */
9826
static void
9827
35.9k
xmlParseElementEnd(xmlParserCtxtPtr ctxt) {
9828
35.9k
    xmlNodePtr cur = ctxt->node;
9829
9830
35.9k
    if (ctxt->nameNr <= 0) {
9831
0
        if ((RAW == '<') && (NXT(1) == '/'))
9832
0
            SKIP(2);
9833
0
        return;
9834
0
    }
9835
9836
    /*
9837
     * parse the end of tag: '</' should be here.
9838
     */
9839
35.9k
    if (ctxt->sax2) {
9840
35.9k
  xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
9841
35.9k
  namePop(ctxt);
9842
35.9k
    }
9843
0
#ifdef LIBXML_SAX1_ENABLED
9844
0
    else
9845
0
  xmlParseEndTag1(ctxt, 0);
9846
35.9k
#endif /* LIBXML_SAX1_ENABLED */
9847
9848
    /*
9849
     * Capture end position
9850
     */
9851
35.9k
    if (cur != NULL && ctxt->record_info) {
9852
0
        xmlParserNodeInfoPtr node_info;
9853
9854
0
        node_info = (xmlParserNodeInfoPtr) xmlParserFindNodeInfo(ctxt, cur);
9855
0
        if (node_info != NULL) {
9856
0
            node_info->end_pos = ctxt->input->consumed +
9857
0
                                 (CUR_PTR - ctxt->input->base);
9858
0
            node_info->end_line = ctxt->input->line;
9859
0
        }
9860
0
    }
9861
35.9k
}
9862
9863
/**
9864
 * parse the XML version value.
9865
 *
9866
 * @deprecated Internal function, don't use.
9867
 *
9868
 *     [26] VersionNum ::= '1.' [0-9]+
9869
 *
9870
 * In practice allow [0-9].[0-9]+ at that level
9871
 *
9872
 * @param ctxt  an XML parser context
9873
 * @returns the string giving the XML version number, or NULL
9874
 */
9875
xmlChar *
9876
638
xmlParseVersionNum(xmlParserCtxt *ctxt) {
9877
638
    xmlChar *buf = NULL;
9878
638
    int len = 0;
9879
638
    int size = 10;
9880
638
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9881
638
                    XML_MAX_TEXT_LENGTH :
9882
638
                    XML_MAX_NAME_LENGTH;
9883
638
    xmlChar cur;
9884
9885
638
    buf = xmlMalloc(size);
9886
638
    if (buf == NULL) {
9887
0
  xmlErrMemory(ctxt);
9888
0
  return(NULL);
9889
0
    }
9890
638
    cur = CUR;
9891
638
    if (!((cur >= '0') && (cur <= '9'))) {
9892
35
  xmlFree(buf);
9893
35
  return(NULL);
9894
35
    }
9895
603
    buf[len++] = cur;
9896
603
    NEXT;
9897
603
    cur=CUR;
9898
603
    if (cur != '.') {
9899
14
  xmlFree(buf);
9900
14
  return(NULL);
9901
14
    }
9902
589
    buf[len++] = cur;
9903
589
    NEXT;
9904
589
    cur=CUR;
9905
46.8k
    while ((cur >= '0') && (cur <= '9')) {
9906
46.2k
  if (len + 1 >= size) {
9907
129
      xmlChar *tmp;
9908
129
            int newSize;
9909
9910
129
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
9911
129
            if (newSize < 0) {
9912
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "VersionNum");
9913
0
                xmlFree(buf);
9914
0
                return(NULL);
9915
0
            }
9916
129
      tmp = xmlRealloc(buf, newSize);
9917
129
      if (tmp == NULL) {
9918
0
    xmlErrMemory(ctxt);
9919
0
          xmlFree(buf);
9920
0
    return(NULL);
9921
0
      }
9922
129
      buf = tmp;
9923
129
            size = newSize;
9924
129
  }
9925
46.2k
  buf[len++] = cur;
9926
46.2k
  NEXT;
9927
46.2k
  cur=CUR;
9928
46.2k
    }
9929
589
    buf[len] = 0;
9930
589
    return(buf);
9931
589
}
9932
9933
/**
9934
 * parse the XML version.
9935
 *
9936
 * @deprecated Internal function, don't use.
9937
 *
9938
 *     [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
9939
 *
9940
 *     [25] Eq ::= S? '=' S?
9941
 *
9942
 * @param ctxt  an XML parser context
9943
 * @returns the version string, e.g. "1.0"
9944
 */
9945
9946
xmlChar *
9947
2.58k
xmlParseVersionInfo(xmlParserCtxt *ctxt) {
9948
2.58k
    xmlChar *version = NULL;
9949
9950
2.58k
    if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
9951
719
  SKIP(7);
9952
719
  SKIP_BLANKS;
9953
719
  if (RAW != '=') {
9954
16
      xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
9955
16
      return(NULL);
9956
16
        }
9957
703
  NEXT;
9958
703
  SKIP_BLANKS;
9959
703
  if (RAW == '"') {
9960
146
      NEXT;
9961
146
      version = xmlParseVersionNum(ctxt);
9962
146
      if (RAW != '"') {
9963
18
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
9964
18
      } else
9965
128
          NEXT;
9966
557
  } else if (RAW == '\''){
9967
492
      NEXT;
9968
492
      version = xmlParseVersionNum(ctxt);
9969
492
      if (RAW != '\'') {
9970
54
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
9971
54
      } else
9972
438
          NEXT;
9973
492
  } else {
9974
65
      xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
9975
65
  }
9976
703
    }
9977
2.56k
    return(version);
9978
2.58k
}
9979
9980
/**
9981
 * parse the XML encoding name
9982
 *
9983
 * @deprecated Internal function, don't use.
9984
 *
9985
 *     [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
9986
 *
9987
 * @param ctxt  an XML parser context
9988
 * @returns the encoding name value or NULL
9989
 */
9990
xmlChar *
9991
1.12k
xmlParseEncName(xmlParserCtxt *ctxt) {
9992
1.12k
    xmlChar *buf = NULL;
9993
1.12k
    int len = 0;
9994
1.12k
    int size = 10;
9995
1.12k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9996
1.12k
                    XML_MAX_TEXT_LENGTH :
9997
1.12k
                    XML_MAX_NAME_LENGTH;
9998
1.12k
    xmlChar cur;
9999
10000
1.12k
    cur = CUR;
10001
1.12k
    if (((cur >= 'a') && (cur <= 'z')) ||
10002
1.12k
        ((cur >= 'A') && (cur <= 'Z'))) {
10003
1.11k
  buf = xmlMalloc(size);
10004
1.11k
  if (buf == NULL) {
10005
0
      xmlErrMemory(ctxt);
10006
0
      return(NULL);
10007
0
  }
10008
10009
1.11k
  buf[len++] = cur;
10010
1.11k
  NEXT;
10011
1.11k
  cur = CUR;
10012
1.05M
  while (((cur >= 'a') && (cur <= 'z')) ||
10013
1.05M
         ((cur >= 'A') && (cur <= 'Z')) ||
10014
1.05M
         ((cur >= '0') && (cur <= '9')) ||
10015
1.05M
         (cur == '.') || (cur == '_') ||
10016
1.05M
         (cur == '-')) {
10017
1.05M
      if (len + 1 >= size) {
10018
823
          xmlChar *tmp;
10019
823
                int newSize;
10020
10021
823
                newSize = xmlGrowCapacity(size, 1, 1, maxLength);
10022
823
                if (newSize < 0) {
10023
0
                    xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "EncName");
10024
0
                    xmlFree(buf);
10025
0
                    return(NULL);
10026
0
                }
10027
823
    tmp = xmlRealloc(buf, newSize);
10028
823
    if (tmp == NULL) {
10029
0
        xmlErrMemory(ctxt);
10030
0
        xmlFree(buf);
10031
0
        return(NULL);
10032
0
    }
10033
823
    buf = tmp;
10034
823
                size = newSize;
10035
823
      }
10036
1.05M
      buf[len++] = cur;
10037
1.05M
      NEXT;
10038
1.05M
      cur = CUR;
10039
1.05M
        }
10040
1.11k
  buf[len] = 0;
10041
1.11k
    } else {
10042
14
  xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
10043
14
    }
10044
1.12k
    return(buf);
10045
1.12k
}
10046
10047
/**
10048
 * parse the XML encoding declaration
10049
 *
10050
 * @deprecated Internal function, don't use.
10051
 *
10052
 *     [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | 
10053
 *                           "'" EncName "'")
10054
 *
10055
 * this setups the conversion filters.
10056
 *
10057
 * @param ctxt  an XML parser context
10058
 * @returns the encoding value or NULL
10059
 */
10060
10061
const xmlChar *
10062
2.56k
xmlParseEncodingDecl(xmlParserCtxt *ctxt) {
10063
2.56k
    xmlChar *encoding = NULL;
10064
10065
2.56k
    SKIP_BLANKS;
10066
2.56k
    if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g') == 0)
10067
1.42k
        return(NULL);
10068
10069
1.13k
    SKIP(8);
10070
1.13k
    SKIP_BLANKS;
10071
1.13k
    if (RAW != '=') {
10072
8
        xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
10073
8
        return(NULL);
10074
8
    }
10075
1.12k
    NEXT;
10076
1.12k
    SKIP_BLANKS;
10077
1.12k
    if (RAW == '"') {
10078
671
        NEXT;
10079
671
        encoding = xmlParseEncName(ctxt);
10080
671
        if (RAW != '"') {
10081
93
            xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10082
93
            xmlFree((xmlChar *) encoding);
10083
93
            return(NULL);
10084
93
        } else
10085
578
            NEXT;
10086
671
    } else if (RAW == '\''){
10087
453
        NEXT;
10088
453
        encoding = xmlParseEncName(ctxt);
10089
453
        if (RAW != '\'') {
10090
31
            xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10091
31
            xmlFree((xmlChar *) encoding);
10092
31
            return(NULL);
10093
31
        } else
10094
422
            NEXT;
10095
453
    } else {
10096
4
        xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10097
4
    }
10098
10099
1.00k
    if (encoding == NULL)
10100
5
        return(NULL);
10101
10102
999
    xmlSetDeclaredEncoding(ctxt, encoding);
10103
10104
999
    return(ctxt->encoding);
10105
1.00k
}
10106
10107
/**
10108
 * parse the XML standalone declaration
10109
 *
10110
 * @deprecated Internal function, don't use.
10111
 *
10112
 *     [32] SDDecl ::= S 'standalone' Eq
10113
 *                     (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
10114
 *
10115
 * [ VC: Standalone Document Declaration ]
10116
 * TODO The standalone document declaration must have the value "no"
10117
 * if any external markup declarations contain declarations of:
10118
 *  - attributes with default values, if elements to which these
10119
 *    attributes apply appear in the document without specifications
10120
 *    of values for these attributes, or
10121
 *  - entities (other than amp, lt, gt, apos, quot), if references
10122
 *    to those entities appear in the document, or
10123
 *  - attributes with values subject to normalization, where the
10124
 *    attribute appears in the document with a value which will change
10125
 *    as a result of normalization, or
10126
 *  - element types with element content, if white space occurs directly
10127
 *    within any instance of those types.
10128
 *
10129
 * @param ctxt  an XML parser context
10130
 * @returns
10131
 *   1 if standalone="yes"
10132
 *   0 if standalone="no"
10133
 *  -2 if standalone attribute is missing or invalid
10134
 *    (A standalone value of -2 means that the XML declaration was found,
10135
 *     but no value was specified for the standalone attribute).
10136
 */
10137
10138
int
10139
2.51k
xmlParseSDDecl(xmlParserCtxt *ctxt) {
10140
2.51k
    int standalone = -2;
10141
10142
2.51k
    SKIP_BLANKS;
10143
2.51k
    if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
10144
349
  SKIP(10);
10145
349
        SKIP_BLANKS;
10146
349
  if (RAW != '=') {
10147
8
      xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
10148
8
      return(standalone);
10149
8
        }
10150
341
  NEXT;
10151
341
  SKIP_BLANKS;
10152
341
        if (RAW == '\''){
10153
296
      NEXT;
10154
296
      if ((RAW == 'n') && (NXT(1) == 'o')) {
10155
284
          standalone = 0;
10156
284
                SKIP(2);
10157
284
      } else if ((RAW == 'y') && (NXT(1) == 'e') &&
10158
12
                 (NXT(2) == 's')) {
10159
3
          standalone = 1;
10160
3
    SKIP(3);
10161
9
            } else {
10162
9
    xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
10163
9
      }
10164
296
      if (RAW != '\'') {
10165
15
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10166
15
      } else
10167
281
          NEXT;
10168
296
  } else if (RAW == '"'){
10169
40
      NEXT;
10170
40
      if ((RAW == 'n') && (NXT(1) == 'o')) {
10171
2
          standalone = 0;
10172
2
    SKIP(2);
10173
38
      } else if ((RAW == 'y') && (NXT(1) == 'e') &&
10174
38
                 (NXT(2) == 's')) {
10175
19
          standalone = 1;
10176
19
                SKIP(3);
10177
19
            } else {
10178
19
    xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
10179
19
      }
10180
40
      if (RAW != '"') {
10181
23
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10182
23
      } else
10183
17
          NEXT;
10184
40
  } else {
10185
5
      xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10186
5
        }
10187
341
    }
10188
2.51k
    return(standalone);
10189
2.51k
}
10190
10191
/**
10192
 * parse an XML declaration header
10193
 *
10194
 * @deprecated Internal function, don't use.
10195
 *
10196
 *     [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
10197
 * @param ctxt  an XML parser context
10198
 */
10199
10200
void
10201
2.58k
xmlParseXMLDecl(xmlParserCtxt *ctxt) {
10202
2.58k
    xmlChar *version;
10203
10204
    /*
10205
     * This value for standalone indicates that the document has an
10206
     * XML declaration but it does not have a standalone attribute.
10207
     * It will be overwritten later if a standalone attribute is found.
10208
     */
10209
10210
2.58k
    ctxt->standalone = -2;
10211
10212
    /*
10213
     * We know that '<?xml' is here.
10214
     */
10215
2.58k
    SKIP(5);
10216
10217
2.58k
    if (!IS_BLANK_CH(RAW)) {
10218
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
10219
0
                 "Blank needed after '<?xml'\n");
10220
0
    }
10221
2.58k
    SKIP_BLANKS;
10222
10223
    /*
10224
     * We must have the VersionInfo here.
10225
     */
10226
2.58k
    version = xmlParseVersionInfo(ctxt);
10227
2.58k
    if (version == NULL) {
10228
1.99k
  xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
10229
1.99k
    } else {
10230
589
  if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
10231
      /*
10232
       * Changed here for XML-1.0 5th edition
10233
       */
10234
272
      if (ctxt->options & XML_PARSE_OLD10) {
10235
0
    xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
10236
0
                "Unsupported version '%s'\n",
10237
0
                version);
10238
272
      } else {
10239
272
          if ((version[0] == '1') && ((version[1] == '.'))) {
10240
260
        xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
10241
260
                      "Unsupported version '%s'\n",
10242
260
          version, NULL);
10243
260
    } else {
10244
12
        xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
10245
12
              "Unsupported version '%s'\n",
10246
12
              version);
10247
12
    }
10248
272
      }
10249
272
  }
10250
589
  if (ctxt->version != NULL)
10251
0
      xmlFree((void *) ctxt->version);
10252
589
  ctxt->version = version;
10253
589
    }
10254
10255
    /*
10256
     * We may have the encoding declaration
10257
     */
10258
2.58k
    if (!IS_BLANK_CH(RAW)) {
10259
2.01k
        if ((RAW == '?') && (NXT(1) == '>')) {
10260
22
      SKIP(2);
10261
22
      return;
10262
22
  }
10263
1.99k
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
10264
1.99k
    }
10265
2.56k
    xmlParseEncodingDecl(ctxt);
10266
10267
    /*
10268
     * We may have the standalone status.
10269
     */
10270
2.56k
    if ((ctxt->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
10271
564
        if ((RAW == '?') && (NXT(1) == '>')) {
10272
42
      SKIP(2);
10273
42
      return;
10274
42
  }
10275
522
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
10276
522
    }
10277
10278
    /*
10279
     * We can grow the input buffer freely at that point
10280
     */
10281
2.51k
    GROW;
10282
10283
2.51k
    SKIP_BLANKS;
10284
2.51k
    ctxt->standalone = xmlParseSDDecl(ctxt);
10285
10286
2.51k
    SKIP_BLANKS;
10287
2.51k
    if ((RAW == '?') && (NXT(1) == '>')) {
10288
347
        SKIP(2);
10289
2.17k
    } else if (RAW == '>') {
10290
        /* Deprecated old WD ... */
10291
433
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
10292
433
  NEXT;
10293
1.73k
    } else {
10294
1.73k
        int c;
10295
10296
1.73k
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
10297
1.38M
        while ((PARSER_STOPPED(ctxt) == 0) &&
10298
1.38M
               ((c = CUR) != 0)) {
10299
1.38M
            NEXT;
10300
1.38M
            if (c == '>')
10301
1.25k
                break;
10302
1.38M
        }
10303
1.73k
    }
10304
2.51k
}
10305
10306
/**
10307
 * @since 2.14.0
10308
 *
10309
 * @param ctxt  parser context
10310
 * @returns the version from the XML declaration.
10311
 */
10312
const xmlChar *
10313
0
xmlCtxtGetVersion(xmlParserCtxt *ctxt) {
10314
0
    if (ctxt == NULL)
10315
0
        return(NULL);
10316
10317
0
    return(ctxt->version);
10318
0
}
10319
10320
/**
10321
 * @since 2.14.0
10322
 *
10323
 * @param ctxt  parser context
10324
 * @returns the value from the standalone document declaration.
10325
 */
10326
int
10327
0
xmlCtxtGetStandalone(xmlParserCtxt *ctxt) {
10328
0
    if (ctxt == NULL)
10329
0
        return(0);
10330
10331
0
    return(ctxt->standalone);
10332
0
}
10333
10334
/**
10335
 * parse an XML Misc* optional field.
10336
 *
10337
 * @deprecated Internal function, don't use.
10338
 *
10339
 *     [27] Misc ::= Comment | PI |  S
10340
 * @param ctxt  an XML parser context
10341
 */
10342
10343
void
10344
29.8k
xmlParseMisc(xmlParserCtxt *ctxt) {
10345
32.8k
    while (PARSER_STOPPED(ctxt) == 0) {
10346
31.9k
        SKIP_BLANKS;
10347
31.9k
        GROW;
10348
31.9k
        if ((RAW == '<') && (NXT(1) == '?')) {
10349
2.23k
      xmlParsePI(ctxt);
10350
29.7k
        } else if (CMP4(CUR_PTR, '<', '!', '-', '-')) {
10351
820
      xmlParseComment(ctxt);
10352
28.9k
        } else {
10353
28.9k
            break;
10354
28.9k
        }
10355
31.9k
    }
10356
29.8k
}
10357
10358
static void
10359
14.4k
xmlFinishDocument(xmlParserCtxtPtr ctxt) {
10360
14.4k
    xmlDocPtr doc;
10361
10362
    /*
10363
     * SAX: end of the document processing.
10364
     */
10365
14.4k
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
10366
14.4k
        ctxt->sax->endDocument(ctxt->userData);
10367
10368
    /*
10369
     * Remove locally kept entity definitions if the tree was not built
10370
     */
10371
14.4k
    doc = ctxt->myDoc;
10372
14.4k
    if ((doc != NULL) &&
10373
14.4k
        (xmlStrEqual(doc->version, SAX_COMPAT_MODE))) {
10374
1.16k
        xmlFreeDoc(doc);
10375
1.16k
        ctxt->myDoc = NULL;
10376
1.16k
    }
10377
14.4k
}
10378
10379
/**
10380
 * Parse an XML document and invoke the SAX handlers. This is useful
10381
 * if you're only interested in custom SAX callbacks. If you want a
10382
 * document tree, use #xmlCtxtParseDocument.
10383
 *
10384
 * @param ctxt  an XML parser context
10385
 * @returns 0, -1 in case of error.
10386
 */
10387
10388
int
10389
14.5k
xmlParseDocument(xmlParserCtxt *ctxt) {
10390
14.5k
    if ((ctxt == NULL) || (ctxt->input == NULL))
10391
0
        return(-1);
10392
10393
14.5k
    GROW;
10394
10395
    /*
10396
     * SAX: detecting the level.
10397
     */
10398
14.5k
    xmlCtxtInitializeLate(ctxt);
10399
10400
14.5k
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10401
14.5k
        ctxt->sax->setDocumentLocator(ctxt->userData,
10402
14.5k
                (xmlSAXLocator *) &xmlDefaultSAXLocator);
10403
14.5k
    }
10404
10405
14.5k
    xmlDetectEncoding(ctxt);
10406
10407
14.5k
    if (CUR == 0) {
10408
36
  xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
10409
36
  return(-1);
10410
36
    }
10411
10412
14.4k
    GROW;
10413
14.4k
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
10414
10415
  /*
10416
   * Note that we will switch encoding on the fly.
10417
   */
10418
2.58k
  xmlParseXMLDecl(ctxt);
10419
2.58k
  SKIP_BLANKS;
10420
11.8k
    } else {
10421
11.8k
  ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10422
11.8k
        if (ctxt->version == NULL) {
10423
0
            xmlErrMemory(ctxt);
10424
0
            return(-1);
10425
0
        }
10426
11.8k
    }
10427
14.4k
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
10428
12.1k
        ctxt->sax->startDocument(ctxt->userData);
10429
14.4k
    if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) &&
10430
14.4k
        (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) {
10431
0
  ctxt->myDoc->compression = ctxt->input->buf->compressed;
10432
0
    }
10433
10434
    /*
10435
     * The Misc part of the Prolog
10436
     */
10437
14.4k
    xmlParseMisc(ctxt);
10438
10439
    /*
10440
     * Then possibly doc type declaration(s) and more Misc
10441
     * (doctypedecl Misc*)?
10442
     */
10443
14.4k
    GROW;
10444
14.4k
    if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
10445
10446
5.92k
  ctxt->inSubset = 1;
10447
5.92k
  xmlParseDocTypeDecl(ctxt);
10448
5.92k
  if (RAW == '[') {
10449
5.62k
      xmlParseInternalSubset(ctxt);
10450
5.62k
  } else if (RAW == '>') {
10451
124
            NEXT;
10452
124
        }
10453
10454
  /*
10455
   * Create and update the external subset.
10456
   */
10457
5.92k
  ctxt->inSubset = 2;
10458
5.92k
  if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
10459
5.92k
      (!ctxt->disableSAX))
10460
849
      ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
10461
849
                                ctxt->extSubSystem, ctxt->extSubURI);
10462
5.92k
  ctxt->inSubset = 0;
10463
10464
5.92k
        xmlCleanSpecialAttr(ctxt);
10465
10466
5.92k
  xmlParseMisc(ctxt);
10467
5.92k
    }
10468
10469
    /*
10470
     * Time to start parsing the tree itself
10471
     */
10472
14.4k
    GROW;
10473
14.4k
    if (RAW != '<') {
10474
5.03k
        if (ctxt->wellFormed)
10475
280
            xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
10476
280
                           "Start tag expected, '<' not found\n");
10477
9.43k
    } else {
10478
9.43k
  xmlParseElement(ctxt);
10479
10480
  /*
10481
   * The Misc part at the end
10482
   */
10483
9.43k
  xmlParseMisc(ctxt);
10484
10485
9.43k
        xmlParserCheckEOF(ctxt, XML_ERR_DOCUMENT_END);
10486
9.43k
    }
10487
10488
14.4k
    ctxt->instate = XML_PARSER_EOF;
10489
14.4k
    xmlFinishDocument(ctxt);
10490
10491
14.4k
    if (! ctxt->wellFormed) {
10492
14.4k
  ctxt->valid = 0;
10493
14.4k
  return(-1);
10494
14.4k
    }
10495
10496
30
    return(0);
10497
14.4k
}
10498
10499
/**
10500
 * parse a general parsed entity
10501
 * An external general parsed entity is well-formed if it matches the
10502
 * production labeled extParsedEnt.
10503
 *
10504
 * @deprecated Internal function, don't use.
10505
 *
10506
 *     [78] extParsedEnt ::= TextDecl? content
10507
 *
10508
 * @param ctxt  an XML parser context
10509
 * @returns 0, -1 in case of error. the parser context is augmented
10510
 *                as a result of the parsing.
10511
 */
10512
10513
int
10514
0
xmlParseExtParsedEnt(xmlParserCtxt *ctxt) {
10515
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
10516
0
        return(-1);
10517
10518
0
    xmlCtxtInitializeLate(ctxt);
10519
10520
0
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10521
0
        ctxt->sax->setDocumentLocator(ctxt->userData,
10522
0
                (xmlSAXLocator *) &xmlDefaultSAXLocator);
10523
0
    }
10524
10525
0
    xmlDetectEncoding(ctxt);
10526
10527
0
    if (CUR == 0) {
10528
0
  xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
10529
0
    }
10530
10531
    /*
10532
     * Check for the XMLDecl in the Prolog.
10533
     */
10534
0
    GROW;
10535
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
10536
10537
  /*
10538
   * Note that we will switch encoding on the fly.
10539
   */
10540
0
  xmlParseXMLDecl(ctxt);
10541
0
  SKIP_BLANKS;
10542
0
    } else {
10543
0
  ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10544
0
    }
10545
0
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
10546
0
        ctxt->sax->startDocument(ctxt->userData);
10547
10548
    /*
10549
     * Doing validity checking on chunk doesn't make sense
10550
     */
10551
0
    ctxt->options &= ~XML_PARSE_DTDVALID;
10552
0
    ctxt->validate = 0;
10553
0
    ctxt->depth = 0;
10554
10555
0
    xmlParseContentInternal(ctxt);
10556
10557
0
    if (ctxt->input->cur < ctxt->input->end)
10558
0
  xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
10559
10560
    /*
10561
     * SAX: end of the document processing.
10562
     */
10563
0
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
10564
0
        ctxt->sax->endDocument(ctxt->userData);
10565
10566
0
    if (! ctxt->wellFormed) return(-1);
10567
0
    return(0);
10568
0
}
10569
10570
#ifdef LIBXML_PUSH_ENABLED
10571
/************************************************************************
10572
 *                  *
10573
 *    Progressive parsing interfaces        *
10574
 *                  *
10575
 ************************************************************************/
10576
10577
/**
10578
 * Check whether the input buffer contains a character.
10579
 *
10580
 * @param ctxt  an XML parser context
10581
 * @param c  character
10582
 */
10583
static int
10584
0
xmlParseLookupChar(xmlParserCtxtPtr ctxt, int c) {
10585
0
    const xmlChar *cur;
10586
10587
0
    if (ctxt->checkIndex == 0) {
10588
0
        cur = ctxt->input->cur + 1;
10589
0
    } else {
10590
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10591
0
    }
10592
10593
0
    if (memchr(cur, c, ctxt->input->end - cur) == NULL) {
10594
0
        size_t index = ctxt->input->end - ctxt->input->cur;
10595
10596
0
        if (index > LONG_MAX) {
10597
0
            ctxt->checkIndex = 0;
10598
0
            return(1);
10599
0
        }
10600
0
        ctxt->checkIndex = index;
10601
0
        return(0);
10602
0
    } else {
10603
0
        ctxt->checkIndex = 0;
10604
0
        return(1);
10605
0
    }
10606
0
}
10607
10608
/**
10609
 * Check whether the input buffer contains a string.
10610
 *
10611
 * @param ctxt  an XML parser context
10612
 * @param startDelta  delta to apply at the start
10613
 * @param str  string
10614
 * @param strLen  length of string
10615
 */
10616
static const xmlChar *
10617
xmlParseLookupString(xmlParserCtxtPtr ctxt, size_t startDelta,
10618
0
                     const char *str, size_t strLen) {
10619
0
    const xmlChar *cur, *term;
10620
10621
0
    if (ctxt->checkIndex == 0) {
10622
0
        cur = ctxt->input->cur + startDelta;
10623
0
    } else {
10624
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10625
0
    }
10626
10627
0
    term = BAD_CAST strstr((const char *) cur, str);
10628
0
    if (term == NULL) {
10629
0
        const xmlChar *end = ctxt->input->end;
10630
0
        size_t index;
10631
10632
        /* Rescan (strLen - 1) characters. */
10633
0
        if ((size_t) (end - cur) < strLen)
10634
0
            end = cur;
10635
0
        else
10636
0
            end -= strLen - 1;
10637
0
        index = end - ctxt->input->cur;
10638
0
        if (index > LONG_MAX) {
10639
0
            ctxt->checkIndex = 0;
10640
0
            return(ctxt->input->end - strLen);
10641
0
        }
10642
0
        ctxt->checkIndex = index;
10643
0
    } else {
10644
0
        ctxt->checkIndex = 0;
10645
0
    }
10646
10647
0
    return(term);
10648
0
}
10649
10650
/**
10651
 * Check whether the input buffer contains terminated char data.
10652
 *
10653
 * @param ctxt  an XML parser context
10654
 */
10655
static int
10656
0
xmlParseLookupCharData(xmlParserCtxtPtr ctxt) {
10657
0
    const xmlChar *cur = ctxt->input->cur + ctxt->checkIndex;
10658
0
    const xmlChar *end = ctxt->input->end;
10659
0
    size_t index;
10660
10661
0
    while (cur < end) {
10662
0
        if ((*cur == '<') || (*cur == '&')) {
10663
0
            ctxt->checkIndex = 0;
10664
0
            return(1);
10665
0
        }
10666
0
        cur++;
10667
0
    }
10668
10669
0
    index = cur - ctxt->input->cur;
10670
0
    if (index > LONG_MAX) {
10671
0
        ctxt->checkIndex = 0;
10672
0
        return(1);
10673
0
    }
10674
0
    ctxt->checkIndex = index;
10675
0
    return(0);
10676
0
}
10677
10678
/**
10679
 * Check whether there's enough data in the input buffer to finish parsing
10680
 * a start tag. This has to take quotes into account.
10681
 *
10682
 * @param ctxt  an XML parser context
10683
 */
10684
static int
10685
0
xmlParseLookupGt(xmlParserCtxtPtr ctxt) {
10686
0
    const xmlChar *cur;
10687
0
    const xmlChar *end = ctxt->input->end;
10688
0
    int state = ctxt->endCheckState;
10689
0
    size_t index;
10690
10691
0
    if (ctxt->checkIndex == 0)
10692
0
        cur = ctxt->input->cur + 1;
10693
0
    else
10694
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10695
10696
0
    while (cur < end) {
10697
0
        if (state) {
10698
0
            if (*cur == state)
10699
0
                state = 0;
10700
0
        } else if (*cur == '\'' || *cur == '"') {
10701
0
            state = *cur;
10702
0
        } else if (*cur == '>') {
10703
0
            ctxt->checkIndex = 0;
10704
0
            ctxt->endCheckState = 0;
10705
0
            return(1);
10706
0
        }
10707
0
        cur++;
10708
0
    }
10709
10710
0
    index = cur - ctxt->input->cur;
10711
0
    if (index > LONG_MAX) {
10712
0
        ctxt->checkIndex = 0;
10713
0
        ctxt->endCheckState = 0;
10714
0
        return(1);
10715
0
    }
10716
0
    ctxt->checkIndex = index;
10717
0
    ctxt->endCheckState = state;
10718
0
    return(0);
10719
0
}
10720
10721
/**
10722
 * Check whether there's enough data in the input buffer to finish parsing
10723
 * the internal subset.
10724
 *
10725
 * @param ctxt  an XML parser context
10726
 */
10727
static int
10728
0
xmlParseLookupInternalSubset(xmlParserCtxtPtr ctxt) {
10729
    /*
10730
     * Sorry, but progressive parsing of the internal subset is not
10731
     * supported. We first check that the full content of the internal
10732
     * subset is available and parsing is launched only at that point.
10733
     * Internal subset ends with "']' S? '>'" in an unescaped section and
10734
     * not in a ']]>' sequence which are conditional sections.
10735
     */
10736
0
    const xmlChar *cur, *start;
10737
0
    const xmlChar *end = ctxt->input->end;
10738
0
    int state = ctxt->endCheckState;
10739
0
    size_t index;
10740
10741
0
    if (ctxt->checkIndex == 0) {
10742
0
        cur = ctxt->input->cur + 1;
10743
0
    } else {
10744
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10745
0
    }
10746
0
    start = cur;
10747
10748
0
    while (cur < end) {
10749
0
        if (state == '-') {
10750
0
            if ((*cur == '-') &&
10751
0
                (cur[1] == '-') &&
10752
0
                (cur[2] == '>')) {
10753
0
                state = 0;
10754
0
                cur += 3;
10755
0
                start = cur;
10756
0
                continue;
10757
0
            }
10758
0
        }
10759
0
        else if (state == ']') {
10760
0
            if (*cur == '>') {
10761
0
                ctxt->checkIndex = 0;
10762
0
                ctxt->endCheckState = 0;
10763
0
                return(1);
10764
0
            }
10765
0
            if (IS_BLANK_CH(*cur)) {
10766
0
                state = ' ';
10767
0
            } else if (*cur != ']') {
10768
0
                state = 0;
10769
0
                start = cur;
10770
0
                continue;
10771
0
            }
10772
0
        }
10773
0
        else if (state == ' ') {
10774
0
            if (*cur == '>') {
10775
0
                ctxt->checkIndex = 0;
10776
0
                ctxt->endCheckState = 0;
10777
0
                return(1);
10778
0
            }
10779
0
            if (!IS_BLANK_CH(*cur)) {
10780
0
                state = 0;
10781
0
                start = cur;
10782
0
                continue;
10783
0
            }
10784
0
        }
10785
0
        else if (state != 0) {
10786
0
            if (*cur == state) {
10787
0
                state = 0;
10788
0
                start = cur + 1;
10789
0
            }
10790
0
        }
10791
0
        else if (*cur == '<') {
10792
0
            if ((cur[1] == '!') &&
10793
0
                (cur[2] == '-') &&
10794
0
                (cur[3] == '-')) {
10795
0
                state = '-';
10796
0
                cur += 4;
10797
                /* Don't treat <!--> as comment */
10798
0
                start = cur;
10799
0
                continue;
10800
0
            }
10801
0
        }
10802
0
        else if ((*cur == '"') || (*cur == '\'') || (*cur == ']')) {
10803
0
            state = *cur;
10804
0
        }
10805
10806
0
        cur++;
10807
0
    }
10808
10809
    /*
10810
     * Rescan the three last characters to detect "<!--" and "-->"
10811
     * split across chunks.
10812
     */
10813
0
    if ((state == 0) || (state == '-')) {
10814
0
        if (cur - start < 3)
10815
0
            cur = start;
10816
0
        else
10817
0
            cur -= 3;
10818
0
    }
10819
0
    index = cur - ctxt->input->cur;
10820
0
    if (index > LONG_MAX) {
10821
0
        ctxt->checkIndex = 0;
10822
0
        ctxt->endCheckState = 0;
10823
0
        return(1);
10824
0
    }
10825
0
    ctxt->checkIndex = index;
10826
0
    ctxt->endCheckState = state;
10827
0
    return(0);
10828
0
}
10829
10830
/**
10831
 * Try to progress on parsing
10832
 *
10833
 * @param ctxt  an XML parser context
10834
 * @param terminate  last chunk indicator
10835
 * @returns zero if no parsing was possible
10836
 */
10837
static int
10838
0
xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
10839
0
    int ret = 0;
10840
0
    size_t avail;
10841
0
    xmlChar cur, next;
10842
10843
0
    if (ctxt->input == NULL)
10844
0
        return(0);
10845
10846
0
    if ((ctxt->input != NULL) &&
10847
0
        (ctxt->input->cur - ctxt->input->base > 4096)) {
10848
0
        xmlParserShrink(ctxt);
10849
0
    }
10850
10851
0
    while (ctxt->disableSAX == 0) {
10852
0
        avail = ctxt->input->end - ctxt->input->cur;
10853
0
        if (avail < 1)
10854
0
      goto done;
10855
0
        switch (ctxt->instate) {
10856
0
            case XML_PARSER_EOF:
10857
          /*
10858
     * Document parsing is done !
10859
     */
10860
0
          goto done;
10861
0
            case XML_PARSER_START:
10862
                /*
10863
                 * Very first chars read from the document flow.
10864
                 */
10865
0
                if ((!terminate) && (avail < 4))
10866
0
                    goto done;
10867
10868
                /*
10869
                 * We need more bytes to detect EBCDIC code pages.
10870
                 * See xmlDetectEBCDIC.
10871
                 */
10872
0
                if ((CMP4(CUR_PTR, 0x4C, 0x6F, 0xA7, 0x94)) &&
10873
0
                    (!terminate) && (avail < 200))
10874
0
                    goto done;
10875
10876
0
                xmlDetectEncoding(ctxt);
10877
0
                ctxt->instate = XML_PARSER_XML_DECL;
10878
0
    break;
10879
10880
0
            case XML_PARSER_XML_DECL:
10881
0
    if ((!terminate) && (avail < 2))
10882
0
        goto done;
10883
0
    cur = ctxt->input->cur[0];
10884
0
    next = ctxt->input->cur[1];
10885
0
          if ((cur == '<') && (next == '?')) {
10886
        /* PI or XML decl */
10887
0
        if ((!terminate) &&
10888
0
                        (!xmlParseLookupString(ctxt, 2, "?>", 2)))
10889
0
      goto done;
10890
0
        if ((ctxt->input->cur[2] == 'x') &&
10891
0
      (ctxt->input->cur[3] == 'm') &&
10892
0
      (ctxt->input->cur[4] == 'l') &&
10893
0
      (IS_BLANK_CH(ctxt->input->cur[5]))) {
10894
0
      ret += 5;
10895
0
      xmlParseXMLDecl(ctxt);
10896
0
        } else {
10897
0
      ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10898
0
                        if (ctxt->version == NULL) {
10899
0
                            xmlErrMemory(ctxt);
10900
0
                            break;
10901
0
                        }
10902
0
        }
10903
0
    } else {
10904
0
        ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10905
0
        if (ctxt->version == NULL) {
10906
0
            xmlErrMemory(ctxt);
10907
0
      break;
10908
0
        }
10909
0
    }
10910
0
                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10911
0
                    ctxt->sax->setDocumentLocator(ctxt->userData,
10912
0
                            (xmlSAXLocator *) &xmlDefaultSAXLocator);
10913
0
                }
10914
0
                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
10915
0
                    (!ctxt->disableSAX))
10916
0
                    ctxt->sax->startDocument(ctxt->userData);
10917
0
                ctxt->instate = XML_PARSER_MISC;
10918
0
    break;
10919
0
            case XML_PARSER_START_TAG: {
10920
0
          const xmlChar *name;
10921
0
    const xmlChar *prefix = NULL;
10922
0
    const xmlChar *URI = NULL;
10923
0
                int line = ctxt->input->line;
10924
0
    int nbNs = 0;
10925
10926
0
    if ((!terminate) && (avail < 2))
10927
0
        goto done;
10928
0
    cur = ctxt->input->cur[0];
10929
0
          if (cur != '<') {
10930
0
        xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
10931
0
                                   "Start tag expected, '<' not found");
10932
0
                    ctxt->instate = XML_PARSER_EOF;
10933
0
                    xmlFinishDocument(ctxt);
10934
0
        goto done;
10935
0
    }
10936
0
    if ((!terminate) && (!xmlParseLookupGt(ctxt)))
10937
0
                    goto done;
10938
0
    if (ctxt->spaceNr == 0)
10939
0
        spacePush(ctxt, -1);
10940
0
    else if (*ctxt->space == -2)
10941
0
        spacePush(ctxt, -1);
10942
0
    else
10943
0
        spacePush(ctxt, *ctxt->space);
10944
0
#ifdef LIBXML_SAX1_ENABLED
10945
0
    if (ctxt->sax2)
10946
0
#endif /* LIBXML_SAX1_ENABLED */
10947
0
        name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
10948
0
#ifdef LIBXML_SAX1_ENABLED
10949
0
    else
10950
0
        name = xmlParseStartTag(ctxt);
10951
0
#endif /* LIBXML_SAX1_ENABLED */
10952
0
    if (name == NULL) {
10953
0
        spacePop(ctxt);
10954
0
                    ctxt->instate = XML_PARSER_EOF;
10955
0
                    xmlFinishDocument(ctxt);
10956
0
        goto done;
10957
0
    }
10958
0
#ifdef LIBXML_VALID_ENABLED
10959
    /*
10960
     * [ VC: Root Element Type ]
10961
     * The Name in the document type declaration must match
10962
     * the element type of the root element.
10963
     */
10964
0
    if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
10965
0
        ctxt->node && (ctxt->node == ctxt->myDoc->children))
10966
0
        ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
10967
0
#endif /* LIBXML_VALID_ENABLED */
10968
10969
    /*
10970
     * Check for an Empty Element.
10971
     */
10972
0
    if ((RAW == '/') && (NXT(1) == '>')) {
10973
0
        SKIP(2);
10974
10975
0
        if (ctxt->sax2) {
10976
0
      if ((ctxt->sax != NULL) &&
10977
0
          (ctxt->sax->endElementNs != NULL) &&
10978
0
          (!ctxt->disableSAX))
10979
0
          ctxt->sax->endElementNs(ctxt->userData, name,
10980
0
                                  prefix, URI);
10981
0
      if (nbNs > 0)
10982
0
          xmlParserNsPop(ctxt, nbNs);
10983
0
#ifdef LIBXML_SAX1_ENABLED
10984
0
        } else {
10985
0
      if ((ctxt->sax != NULL) &&
10986
0
          (ctxt->sax->endElement != NULL) &&
10987
0
          (!ctxt->disableSAX))
10988
0
          ctxt->sax->endElement(ctxt->userData, name);
10989
0
#endif /* LIBXML_SAX1_ENABLED */
10990
0
        }
10991
0
        spacePop(ctxt);
10992
0
    } else if (RAW == '>') {
10993
0
        NEXT;
10994
0
                    nameNsPush(ctxt, name, prefix, URI, line, nbNs);
10995
0
    } else {
10996
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
10997
0
           "Couldn't find end of Start Tag %s\n",
10998
0
           name);
10999
0
        nodePop(ctxt);
11000
0
        spacePop(ctxt);
11001
0
                    if (nbNs > 0)
11002
0
                        xmlParserNsPop(ctxt, nbNs);
11003
0
    }
11004
11005
0
                if (ctxt->nameNr == 0)
11006
0
                    ctxt->instate = XML_PARSER_EPILOG;
11007
0
                else
11008
0
                    ctxt->instate = XML_PARSER_CONTENT;
11009
0
                break;
11010
0
      }
11011
0
            case XML_PARSER_CONTENT: {
11012
0
    cur = ctxt->input->cur[0];
11013
11014
0
    if (cur == '<') {
11015
0
                    if ((!terminate) && (avail < 2))
11016
0
                        goto done;
11017
0
        next = ctxt->input->cur[1];
11018
11019
0
                    if (next == '/') {
11020
0
                        ctxt->instate = XML_PARSER_END_TAG;
11021
0
                        break;
11022
0
                    } else if (next == '?') {
11023
0
                        if ((!terminate) &&
11024
0
                            (!xmlParseLookupString(ctxt, 2, "?>", 2)))
11025
0
                            goto done;
11026
0
                        xmlParsePI(ctxt);
11027
0
                        ctxt->instate = XML_PARSER_CONTENT;
11028
0
                        break;
11029
0
                    } else if (next == '!') {
11030
0
                        if ((!terminate) && (avail < 3))
11031
0
                            goto done;
11032
0
                        next = ctxt->input->cur[2];
11033
11034
0
                        if (next == '-') {
11035
0
                            if ((!terminate) && (avail < 4))
11036
0
                                goto done;
11037
0
                            if (ctxt->input->cur[3] == '-') {
11038
0
                                if ((!terminate) &&
11039
0
                                    (!xmlParseLookupString(ctxt, 4, "-->", 3)))
11040
0
                                    goto done;
11041
0
                                xmlParseComment(ctxt);
11042
0
                                ctxt->instate = XML_PARSER_CONTENT;
11043
0
                                break;
11044
0
                            }
11045
0
                        } else if (next == '[') {
11046
0
                            if ((!terminate) && (avail < 9))
11047
0
                                goto done;
11048
0
                            if ((ctxt->input->cur[2] == '[') &&
11049
0
                                (ctxt->input->cur[3] == 'C') &&
11050
0
                                (ctxt->input->cur[4] == 'D') &&
11051
0
                                (ctxt->input->cur[5] == 'A') &&
11052
0
                                (ctxt->input->cur[6] == 'T') &&
11053
0
                                (ctxt->input->cur[7] == 'A') &&
11054
0
                                (ctxt->input->cur[8] == '[')) {
11055
0
                                if ((!terminate) &&
11056
0
                                    (!xmlParseLookupString(ctxt, 9, "]]>", 3)))
11057
0
                                    goto done;
11058
0
                                ctxt->instate = XML_PARSER_CDATA_SECTION;
11059
0
                                xmlParseCDSect(ctxt);
11060
0
                                ctxt->instate = XML_PARSER_CONTENT;
11061
0
                                break;
11062
0
                            }
11063
0
                        }
11064
0
                    }
11065
0
    } else if (cur == '&') {
11066
0
        if ((!terminate) && (!xmlParseLookupChar(ctxt, ';')))
11067
0
      goto done;
11068
0
        xmlParseReference(ctxt);
11069
0
                    break;
11070
0
    } else {
11071
        /* TODO Avoid the extra copy, handle directly !!! */
11072
        /*
11073
         * Goal of the following test is:
11074
         *  - minimize calls to the SAX 'character' callback
11075
         *    when they are mergeable
11076
         *  - handle an problem for isBlank when we only parse
11077
         *    a sequence of blank chars and the next one is
11078
         *    not available to check against '<' presence.
11079
         *  - tries to homogenize the differences in SAX
11080
         *    callbacks between the push and pull versions
11081
         *    of the parser.
11082
         */
11083
0
        if (avail < XML_PARSER_BIG_BUFFER_SIZE) {
11084
0
      if ((!terminate) && (!xmlParseLookupCharData(ctxt)))
11085
0
          goto done;
11086
0
                    }
11087
0
                    ctxt->checkIndex = 0;
11088
0
        xmlParseCharDataInternal(ctxt, !terminate);
11089
0
                    break;
11090
0
    }
11091
11092
0
                ctxt->instate = XML_PARSER_START_TAG;
11093
0
    break;
11094
0
      }
11095
0
            case XML_PARSER_END_TAG:
11096
0
    if ((!terminate) && (!xmlParseLookupChar(ctxt, '>')))
11097
0
        goto done;
11098
0
    if (ctxt->sax2) {
11099
0
              xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
11100
0
        nameNsPop(ctxt);
11101
0
    }
11102
0
#ifdef LIBXML_SAX1_ENABLED
11103
0
      else
11104
0
        xmlParseEndTag1(ctxt, 0);
11105
0
#endif /* LIBXML_SAX1_ENABLED */
11106
0
    if (ctxt->nameNr == 0) {
11107
0
        ctxt->instate = XML_PARSER_EPILOG;
11108
0
    } else {
11109
0
        ctxt->instate = XML_PARSER_CONTENT;
11110
0
    }
11111
0
    break;
11112
0
            case XML_PARSER_MISC:
11113
0
            case XML_PARSER_PROLOG:
11114
0
            case XML_PARSER_EPILOG:
11115
0
    SKIP_BLANKS;
11116
0
                avail = ctxt->input->end - ctxt->input->cur;
11117
0
    if (avail < 1)
11118
0
        goto done;
11119
0
    if (ctxt->input->cur[0] == '<') {
11120
0
                    if ((!terminate) && (avail < 2))
11121
0
                        goto done;
11122
0
                    next = ctxt->input->cur[1];
11123
0
                    if (next == '?') {
11124
0
                        if ((!terminate) &&
11125
0
                            (!xmlParseLookupString(ctxt, 2, "?>", 2)))
11126
0
                            goto done;
11127
0
                        xmlParsePI(ctxt);
11128
0
                        break;
11129
0
                    } else if (next == '!') {
11130
0
                        if ((!terminate) && (avail < 3))
11131
0
                            goto done;
11132
11133
0
                        if (ctxt->input->cur[2] == '-') {
11134
0
                            if ((!terminate) && (avail < 4))
11135
0
                                goto done;
11136
0
                            if (ctxt->input->cur[3] == '-') {
11137
0
                                if ((!terminate) &&
11138
0
                                    (!xmlParseLookupString(ctxt, 4, "-->", 3)))
11139
0
                                    goto done;
11140
0
                                xmlParseComment(ctxt);
11141
0
                                break;
11142
0
                            }
11143
0
                        } else if (ctxt->instate == XML_PARSER_MISC) {
11144
0
                            if ((!terminate) && (avail < 9))
11145
0
                                goto done;
11146
0
                            if ((ctxt->input->cur[2] == 'D') &&
11147
0
                                (ctxt->input->cur[3] == 'O') &&
11148
0
                                (ctxt->input->cur[4] == 'C') &&
11149
0
                                (ctxt->input->cur[5] == 'T') &&
11150
0
                                (ctxt->input->cur[6] == 'Y') &&
11151
0
                                (ctxt->input->cur[7] == 'P') &&
11152
0
                                (ctxt->input->cur[8] == 'E')) {
11153
0
                                if ((!terminate) && (!xmlParseLookupGt(ctxt)))
11154
0
                                    goto done;
11155
0
                                ctxt->inSubset = 1;
11156
0
                                xmlParseDocTypeDecl(ctxt);
11157
0
                                if (RAW == '[') {
11158
0
                                    ctxt->instate = XML_PARSER_DTD;
11159
0
                                } else {
11160
0
                                    if (RAW == '>')
11161
0
                                        NEXT;
11162
                                    /*
11163
                                     * Create and update the external subset.
11164
                                     */
11165
0
                                    ctxt->inSubset = 2;
11166
0
                                    if ((ctxt->sax != NULL) &&
11167
0
                                        (!ctxt->disableSAX) &&
11168
0
                                        (ctxt->sax->externalSubset != NULL))
11169
0
                                        ctxt->sax->externalSubset(
11170
0
                                                ctxt->userData,
11171
0
                                                ctxt->intSubName,
11172
0
                                                ctxt->extSubSystem,
11173
0
                                                ctxt->extSubURI);
11174
0
                                    ctxt->inSubset = 0;
11175
0
                                    xmlCleanSpecialAttr(ctxt);
11176
0
                                    ctxt->instate = XML_PARSER_PROLOG;
11177
0
                                }
11178
0
                                break;
11179
0
                            }
11180
0
                        }
11181
0
                    }
11182
0
                }
11183
11184
0
                if (ctxt->instate == XML_PARSER_EPILOG) {
11185
0
                    if (ctxt->errNo == XML_ERR_OK)
11186
0
                        xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
11187
0
        ctxt->instate = XML_PARSER_EOF;
11188
0
                    xmlFinishDocument(ctxt);
11189
0
                } else {
11190
0
        ctxt->instate = XML_PARSER_START_TAG;
11191
0
    }
11192
0
    break;
11193
0
            case XML_PARSER_DTD: {
11194
0
                if ((!terminate) && (!xmlParseLookupInternalSubset(ctxt)))
11195
0
                    goto done;
11196
0
    xmlParseInternalSubset(ctxt);
11197
0
    ctxt->inSubset = 2;
11198
0
    if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
11199
0
        (ctxt->sax->externalSubset != NULL))
11200
0
        ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
11201
0
          ctxt->extSubSystem, ctxt->extSubURI);
11202
0
    ctxt->inSubset = 0;
11203
0
    xmlCleanSpecialAttr(ctxt);
11204
0
    ctxt->instate = XML_PARSER_PROLOG;
11205
0
                break;
11206
0
      }
11207
0
            default:
11208
0
                xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
11209
0
      "PP: internal error\n");
11210
0
    ctxt->instate = XML_PARSER_EOF;
11211
0
    break;
11212
0
  }
11213
0
    }
11214
0
done:
11215
0
    return(ret);
11216
0
}
11217
11218
/**
11219
 * Parse a chunk of memory in push parser mode.
11220
 *
11221
 * Assumes that the parser context was initialized with
11222
 * #xmlCreatePushParserCtxt.
11223
 *
11224
 * The last chunk, which will often be empty, must be marked with
11225
 * the `terminate` flag. With the default SAX callbacks, the resulting
11226
 * document will be available in ctxt->myDoc. This pointer will not
11227
 * be freed when calling #xmlFreeParserCtxt and must be freed by the
11228
 * caller. If the document isn't well-formed, it will still be returned
11229
 * in ctxt->myDoc.
11230
 *
11231
 * As an exception, #xmlCtxtResetPush will free the document in
11232
 * ctxt->myDoc. So ctxt->myDoc should be set to NULL after extracting
11233
 * the document.
11234
 *
11235
 * Since 2.14.0, #xmlCtxtGetDocument can be used to retrieve the
11236
 * result document.
11237
 *
11238
 * @param ctxt  an XML parser context
11239
 * @param chunk  chunk of memory
11240
 * @param size  size of chunk in bytes
11241
 * @param terminate  last chunk indicator
11242
 * @returns an xmlParserErrors code (0 on success).
11243
 */
11244
int
11245
xmlParseChunk(xmlParserCtxt *ctxt, const char *chunk, int size,
11246
0
              int terminate) {
11247
0
    size_t curBase;
11248
0
    size_t maxLength;
11249
0
    size_t pos;
11250
0
    int end_in_lf = 0;
11251
0
    int res;
11252
11253
0
    if ((ctxt == NULL) || (size < 0))
11254
0
        return(XML_ERR_ARGUMENT);
11255
0
    if ((chunk == NULL) && (size > 0))
11256
0
        return(XML_ERR_ARGUMENT);
11257
0
    if ((ctxt->input == NULL) || (ctxt->input->buf == NULL))
11258
0
        return(XML_ERR_ARGUMENT);
11259
0
    if (ctxt->disableSAX != 0)
11260
0
        return(ctxt->errNo);
11261
11262
0
    ctxt->input->flags |= XML_INPUT_PROGRESSIVE;
11263
0
    if (ctxt->instate == XML_PARSER_START)
11264
0
        xmlCtxtInitializeLate(ctxt);
11265
0
    if ((size > 0) && (chunk != NULL) && (!terminate) &&
11266
0
        (chunk[size - 1] == '\r')) {
11267
0
  end_in_lf = 1;
11268
0
  size--;
11269
0
    }
11270
11271
    /*
11272
     * Also push an empty chunk to make sure that the raw buffer
11273
     * will be flushed if there is an encoder.
11274
     */
11275
0
    pos = ctxt->input->cur - ctxt->input->base;
11276
0
    res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
11277
0
    xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
11278
0
    if (res < 0) {
11279
0
        xmlCtxtErrIO(ctxt, ctxt->input->buf->error, NULL);
11280
0
        xmlHaltParser(ctxt);
11281
0
        return(ctxt->errNo);
11282
0
    }
11283
11284
0
    xmlParseTryOrFinish(ctxt, terminate);
11285
11286
0
    curBase = ctxt->input->cur - ctxt->input->base;
11287
0
    maxLength = (ctxt->options & XML_PARSE_HUGE) ?
11288
0
                XML_MAX_HUGE_LENGTH :
11289
0
                XML_MAX_LOOKUP_LIMIT;
11290
0
    if (curBase > maxLength) {
11291
0
        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
11292
0
                    "Buffer size limit exceeded, try XML_PARSE_HUGE\n");
11293
0
        xmlHaltParser(ctxt);
11294
0
    }
11295
11296
0
    if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX != 0))
11297
0
        return(ctxt->errNo);
11298
11299
0
    if (end_in_lf == 1) {
11300
0
  pos = ctxt->input->cur - ctxt->input->base;
11301
0
  res = xmlParserInputBufferPush(ctxt->input->buf, 1, "\r");
11302
0
  xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
11303
0
        if (res < 0) {
11304
0
            xmlCtxtErrIO(ctxt, ctxt->input->buf->error, NULL);
11305
0
            xmlHaltParser(ctxt);
11306
0
            return(ctxt->errNo);
11307
0
        }
11308
0
    }
11309
0
    if (terminate) {
11310
  /*
11311
   * Check for termination
11312
   */
11313
0
        if ((ctxt->instate != XML_PARSER_EOF) &&
11314
0
            (ctxt->instate != XML_PARSER_EPILOG)) {
11315
0
            if (ctxt->nameNr > 0) {
11316
0
                const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
11317
0
                int line = ctxt->pushTab[ctxt->nameNr - 1].line;
11318
0
                xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
11319
0
                        "Premature end of data in tag %s line %d\n",
11320
0
                        name, line, NULL);
11321
0
            } else if (ctxt->instate == XML_PARSER_START) {
11322
0
                xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
11323
0
            } else {
11324
0
                xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
11325
0
                               "Start tag expected, '<' not found\n");
11326
0
            }
11327
0
        } else {
11328
0
            xmlParserCheckEOF(ctxt, XML_ERR_DOCUMENT_END);
11329
0
        }
11330
0
  if (ctxt->instate != XML_PARSER_EOF) {
11331
0
            ctxt->instate = XML_PARSER_EOF;
11332
0
            xmlFinishDocument(ctxt);
11333
0
  }
11334
0
    }
11335
0
    if (ctxt->wellFormed == 0)
11336
0
  return((xmlParserErrors) ctxt->errNo);
11337
0
    else
11338
0
        return(0);
11339
0
}
11340
11341
/************************************************************************
11342
 *                  *
11343
 *    I/O front end functions to the parser     *
11344
 *                  *
11345
 ************************************************************************/
11346
11347
/**
11348
 * Create a parser context for using the XML parser in push mode.
11349
 * See #xmlParseChunk.
11350
 *
11351
 * Passing an initial chunk is useless and deprecated.
11352
 *
11353
 * The push parser doesn't support recovery mode or the
11354
 * XML_PARSE_NOBLANKS option.
11355
 *
11356
 * `filename` is used as base URI to fetch external entities and for
11357
 * error reports.
11358
 *
11359
 * @param sax  a SAX handler (optional)
11360
 * @param user_data  user data for SAX callbacks (optional)
11361
 * @param chunk  initial chunk (optional, deprecated)
11362
 * @param size  size of initial chunk in bytes
11363
 * @param filename  file name or URI (optional)
11364
 * @returns the new parser context or NULL if a memory allocation
11365
 * failed.
11366
 */
11367
11368
xmlParserCtxt *
11369
xmlCreatePushParserCtxt(xmlSAXHandler *sax, void *user_data,
11370
0
                        const char *chunk, int size, const char *filename) {
11371
0
    xmlParserCtxtPtr ctxt;
11372
0
    xmlParserInputPtr input;
11373
11374
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11375
0
    if (ctxt == NULL)
11376
0
  return(NULL);
11377
11378
0
    ctxt->options &= ~XML_PARSE_NODICT;
11379
0
    ctxt->dictNames = 1;
11380
11381
0
    input = xmlNewPushInput(filename, chunk, size);
11382
0
    if (input == NULL) {
11383
0
  xmlFreeParserCtxt(ctxt);
11384
0
  return(NULL);
11385
0
    }
11386
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11387
0
        xmlFreeInputStream(input);
11388
0
        xmlFreeParserCtxt(ctxt);
11389
0
        return(NULL);
11390
0
    }
11391
11392
0
    return(ctxt);
11393
0
}
11394
#endif /* LIBXML_PUSH_ENABLED */
11395
11396
/**
11397
 * Blocks further parser processing
11398
 *
11399
 * @param ctxt  an XML parser context
11400
 */
11401
void
11402
0
xmlStopParser(xmlParserCtxt *ctxt) {
11403
0
    if (ctxt == NULL)
11404
0
        return;
11405
0
    xmlHaltParser(ctxt);
11406
0
    if (ctxt->errNo != XML_ERR_NO_MEMORY)
11407
0
        ctxt->errNo = XML_ERR_USER_STOP;
11408
0
}
11409
11410
/**
11411
 * Create a parser context for using the XML parser with an existing
11412
 * I/O stream
11413
 *
11414
 * @param sax  a SAX handler (optional)
11415
 * @param user_data  user data for SAX callbacks (optional)
11416
 * @param ioread  an I/O read function
11417
 * @param ioclose  an I/O close function (optional)
11418
 * @param ioctx  an I/O handler
11419
 * @param enc  the charset encoding if known (deprecated)
11420
 * @returns the new parser context or NULL
11421
 */
11422
xmlParserCtxt *
11423
xmlCreateIOParserCtxt(xmlSAXHandler *sax, void *user_data,
11424
                      xmlInputReadCallback ioread,
11425
                      xmlInputCloseCallback ioclose,
11426
0
                      void *ioctx, xmlCharEncoding enc) {
11427
0
    xmlParserCtxtPtr ctxt;
11428
0
    xmlParserInputPtr input;
11429
0
    const char *encoding;
11430
11431
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11432
0
    if (ctxt == NULL)
11433
0
  return(NULL);
11434
11435
0
    encoding = xmlGetCharEncodingName(enc);
11436
0
    input = xmlCtxtNewInputFromIO(ctxt, NULL, ioread, ioclose, ioctx,
11437
0
                                  encoding, 0);
11438
0
    if (input == NULL) {
11439
0
  xmlFreeParserCtxt(ctxt);
11440
0
        return (NULL);
11441
0
    }
11442
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11443
0
        xmlFreeInputStream(input);
11444
0
        xmlFreeParserCtxt(ctxt);
11445
0
        return(NULL);
11446
0
    }
11447
11448
0
    return(ctxt);
11449
0
}
11450
11451
#ifdef LIBXML_VALID_ENABLED
11452
/************************************************************************
11453
 *                  *
11454
 *    Front ends when parsing a DTD       *
11455
 *                  *
11456
 ************************************************************************/
11457
11458
/**
11459
 * Parse a DTD.
11460
 *
11461
 * Option XML_PARSE_DTDLOAD should be enabled in the parser context
11462
 * to make external entities work.
11463
 *
11464
 * @since 2.14.0
11465
 *
11466
 * @param ctxt  a parser context
11467
 * @param input  a parser input
11468
 * @param publicId  public ID of the DTD (optional)
11469
 * @param systemId  system ID of the DTD (optional)
11470
 * @returns the resulting xmlDtd or NULL in case of error.
11471
 * `input` will be freed by the function in any case.
11472
 */
11473
xmlDtd *
11474
xmlCtxtParseDtd(xmlParserCtxt *ctxt, xmlParserInput *input,
11475
0
                const xmlChar *publicId, const xmlChar *systemId) {
11476
0
    xmlDtdPtr ret = NULL;
11477
11478
0
    if ((ctxt == NULL) || (input == NULL)) {
11479
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
11480
0
        xmlFreeInputStream(input);
11481
0
        return(NULL);
11482
0
    }
11483
11484
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11485
0
        xmlFreeInputStream(input);
11486
0
        return(NULL);
11487
0
    }
11488
11489
0
    if (publicId == NULL)
11490
0
        publicId = BAD_CAST "none";
11491
0
    if (systemId == NULL)
11492
0
        systemId = BAD_CAST "none";
11493
11494
0
    ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
11495
0
    if (ctxt->myDoc == NULL) {
11496
0
        xmlErrMemory(ctxt);
11497
0
        goto error;
11498
0
    }
11499
0
    ctxt->myDoc->properties = XML_DOC_INTERNAL;
11500
0
    ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
11501
0
                                       publicId, systemId);
11502
0
    if (ctxt->myDoc->extSubset == NULL) {
11503
0
        xmlErrMemory(ctxt);
11504
0
        xmlFreeDoc(ctxt->myDoc);
11505
0
        goto error;
11506
0
    }
11507
11508
0
    xmlParseExternalSubset(ctxt, publicId, systemId);
11509
11510
0
    if (ctxt->wellFormed) {
11511
0
        ret = ctxt->myDoc->extSubset;
11512
0
        ctxt->myDoc->extSubset = NULL;
11513
0
        if (ret != NULL) {
11514
0
            xmlNodePtr tmp;
11515
11516
0
            ret->doc = NULL;
11517
0
            tmp = ret->children;
11518
0
            while (tmp != NULL) {
11519
0
                tmp->doc = NULL;
11520
0
                tmp = tmp->next;
11521
0
            }
11522
0
        }
11523
0
    } else {
11524
0
        ret = NULL;
11525
0
    }
11526
0
    xmlFreeDoc(ctxt->myDoc);
11527
0
    ctxt->myDoc = NULL;
11528
11529
0
error:
11530
0
    xmlFreeInputStream(xmlCtxtPopInput(ctxt));
11531
11532
0
    return(ret);
11533
0
}
11534
11535
/**
11536
 * Load and parse a DTD
11537
 *
11538
 * @deprecated Use #xmlCtxtParseDtd.
11539
 *
11540
 * @param sax  the SAX handler block or NULL
11541
 * @param input  an Input Buffer
11542
 * @param enc  the charset encoding if known
11543
 * @returns the resulting xmlDtd or NULL in case of error.
11544
 * `input` will be freed by the function in any case.
11545
 */
11546
11547
xmlDtd *
11548
xmlIOParseDTD(xmlSAXHandler *sax, xmlParserInputBuffer *input,
11549
0
        xmlCharEncoding enc) {
11550
0
    xmlDtdPtr ret = NULL;
11551
0
    xmlParserCtxtPtr ctxt;
11552
0
    xmlParserInputPtr pinput = NULL;
11553
11554
0
    if (input == NULL)
11555
0
  return(NULL);
11556
11557
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
11558
0
    if (ctxt == NULL) {
11559
0
        xmlFreeParserInputBuffer(input);
11560
0
  return(NULL);
11561
0
    }
11562
0
    xmlCtxtSetOptions(ctxt, XML_PARSE_DTDLOAD);
11563
11564
    /*
11565
     * generate a parser input from the I/O handler
11566
     */
11567
11568
0
    pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
11569
0
    if (pinput == NULL) {
11570
0
        xmlFreeParserInputBuffer(input);
11571
0
  xmlFreeParserCtxt(ctxt);
11572
0
  return(NULL);
11573
0
    }
11574
11575
0
    if (enc != XML_CHAR_ENCODING_NONE) {
11576
0
        xmlSwitchEncoding(ctxt, enc);
11577
0
    }
11578
11579
0
    ret = xmlCtxtParseDtd(ctxt, pinput, NULL, NULL);
11580
11581
0
    xmlFreeParserCtxt(ctxt);
11582
0
    return(ret);
11583
0
}
11584
11585
/**
11586
 * Load and parse an external subset.
11587
 *
11588
 * @deprecated Use #xmlCtxtParseDtd.
11589
 *
11590
 * @param sax  the SAX handler block
11591
 * @param publicId  public identifier of the DTD (optional)
11592
 * @param systemId  system identifier (URL) of the DTD
11593
 * @returns the resulting xmlDtd or NULL in case of error.
11594
 */
11595
11596
xmlDtd *
11597
xmlSAXParseDTD(xmlSAXHandler *sax, const xmlChar *publicId,
11598
0
               const xmlChar *systemId) {
11599
0
    xmlDtdPtr ret = NULL;
11600
0
    xmlParserCtxtPtr ctxt;
11601
0
    xmlParserInputPtr input = NULL;
11602
0
    xmlChar* systemIdCanonic;
11603
11604
0
    if ((publicId == NULL) && (systemId == NULL)) return(NULL);
11605
11606
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
11607
0
    if (ctxt == NULL) {
11608
0
  return(NULL);
11609
0
    }
11610
0
    xmlCtxtSetOptions(ctxt, XML_PARSE_DTDLOAD);
11611
11612
    /*
11613
     * Canonicalise the system ID
11614
     */
11615
0
    systemIdCanonic = xmlCanonicPath(systemId);
11616
0
    if ((systemId != NULL) && (systemIdCanonic == NULL)) {
11617
0
  xmlFreeParserCtxt(ctxt);
11618
0
  return(NULL);
11619
0
    }
11620
11621
    /*
11622
     * Ask the Entity resolver to load the damn thing
11623
     */
11624
11625
0
    if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
11626
0
  input = ctxt->sax->resolveEntity(ctxt->userData, publicId,
11627
0
                                   systemIdCanonic);
11628
0
    if (input == NULL) {
11629
0
  xmlFreeParserCtxt(ctxt);
11630
0
  if (systemIdCanonic != NULL)
11631
0
      xmlFree(systemIdCanonic);
11632
0
  return(NULL);
11633
0
    }
11634
11635
0
    if (input->filename == NULL)
11636
0
  input->filename = (char *) systemIdCanonic;
11637
0
    else
11638
0
  xmlFree(systemIdCanonic);
11639
11640
0
    ret = xmlCtxtParseDtd(ctxt, input, publicId, systemId);
11641
11642
0
    xmlFreeParserCtxt(ctxt);
11643
0
    return(ret);
11644
0
}
11645
11646
11647
/**
11648
 * Load and parse an external subset.
11649
 *
11650
 * @param publicId  public identifier of the DTD (optional)
11651
 * @param systemId  system identifier (URL) of the DTD
11652
 * @returns the resulting xmlDtd or NULL in case of error.
11653
 */
11654
11655
xmlDtd *
11656
0
xmlParseDTD(const xmlChar *publicId, const xmlChar *systemId) {
11657
0
    return(xmlSAXParseDTD(NULL, publicId, systemId));
11658
0
}
11659
#endif /* LIBXML_VALID_ENABLED */
11660
11661
/************************************************************************
11662
 *                  *
11663
 *    Front ends when parsing an Entity     *
11664
 *                  *
11665
 ************************************************************************/
11666
11667
static xmlNodePtr
11668
xmlCtxtParseContentInternal(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
11669
7.17k
                            int hasTextDecl, int buildTree) {
11670
7.17k
    xmlNodePtr root = NULL;
11671
7.17k
    xmlNodePtr list = NULL;
11672
7.17k
    xmlChar *rootName = BAD_CAST "#root";
11673
7.17k
    int result;
11674
11675
7.17k
    if (buildTree) {
11676
7.17k
        root = xmlNewDocNode(ctxt->myDoc, NULL, rootName, NULL);
11677
7.17k
        if (root == NULL) {
11678
0
            xmlErrMemory(ctxt);
11679
0
            goto error;
11680
0
        }
11681
7.17k
    }
11682
11683
7.17k
    if (xmlCtxtPushInput(ctxt, input) < 0)
11684
0
        goto error;
11685
11686
7.17k
    nameNsPush(ctxt, rootName, NULL, NULL, 0, 0);
11687
7.17k
    spacePush(ctxt, -1);
11688
11689
7.17k
    if (buildTree)
11690
7.17k
        nodePush(ctxt, root);
11691
11692
7.17k
    if (hasTextDecl) {
11693
0
        xmlDetectEncoding(ctxt);
11694
11695
        /*
11696
         * Parse a possible text declaration first
11697
         */
11698
0
        if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
11699
0
            (IS_BLANK_CH(NXT(5)))) {
11700
0
            xmlParseTextDecl(ctxt);
11701
            /*
11702
             * An XML-1.0 document can't reference an entity not XML-1.0
11703
             */
11704
0
            if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
11705
0
                (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
11706
0
                xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
11707
0
                               "Version mismatch between document and "
11708
0
                               "entity\n");
11709
0
            }
11710
0
        }
11711
0
    }
11712
11713
7.17k
    xmlParseContentInternal(ctxt);
11714
11715
7.17k
    if (ctxt->input->cur < ctxt->input->end)
11716
35
  xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11717
11718
7.17k
    if ((ctxt->wellFormed) ||
11719
7.17k
        ((ctxt->recovery) && (!xmlCtxtIsCatastrophicError(ctxt)))) {
11720
7.04k
        if (root != NULL) {
11721
7.04k
            xmlNodePtr cur;
11722
11723
            /*
11724
             * Unlink newly created node list.
11725
             */
11726
7.04k
            list = root->children;
11727
7.04k
            root->children = NULL;
11728
7.04k
            root->last = NULL;
11729
8.06k
            for (cur = list; cur != NULL; cur = cur->next)
11730
1.02k
                cur->parent = NULL;
11731
7.04k
        }
11732
7.04k
    }
11733
11734
    /*
11735
     * Read the rest of the stream in case of errors. We want
11736
     * to account for the whole entity size.
11737
     */
11738
7.17k
    do {
11739
7.17k
        ctxt->input->cur = ctxt->input->end;
11740
7.17k
        xmlParserShrink(ctxt);
11741
7.17k
        result = xmlParserGrow(ctxt);
11742
7.17k
    } while (result > 0);
11743
11744
7.17k
    if (buildTree)
11745
7.17k
        nodePop(ctxt);
11746
11747
7.17k
    namePop(ctxt);
11748
7.17k
    spacePop(ctxt);
11749
11750
7.17k
    xmlCtxtPopInput(ctxt);
11751
11752
7.17k
error:
11753
7.17k
    xmlFreeNode(root);
11754
11755
7.17k
    return(list);
11756
7.17k
}
11757
11758
static void
11759
7.18k
xmlCtxtParseEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr ent) {
11760
7.18k
    xmlParserInputPtr input;
11761
7.18k
    xmlNodePtr list;
11762
7.18k
    unsigned long consumed;
11763
7.18k
    int isExternal;
11764
7.18k
    int buildTree;
11765
7.18k
    int oldMinNsIndex;
11766
7.18k
    int oldNodelen, oldNodemem;
11767
11768
7.18k
    isExternal = (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY);
11769
7.18k
    buildTree = (ctxt->node != NULL);
11770
11771
    /*
11772
     * Recursion check
11773
     */
11774
7.18k
    if (ent->flags & XML_ENT_EXPANDING) {
11775
10
        xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
11776
10
        xmlHaltParser(ctxt);
11777
10
        goto error;
11778
10
    }
11779
11780
    /*
11781
     * Load entity
11782
     */
11783
7.17k
    input = xmlNewEntityInputStream(ctxt, ent);
11784
7.17k
    if (input == NULL)
11785
0
        goto error;
11786
11787
    /*
11788
     * When building a tree, we need to limit the scope of namespace
11789
     * declarations, so that entities don't reference xmlNs structs
11790
     * from the parent of a reference.
11791
     */
11792
7.17k
    oldMinNsIndex = ctxt->nsdb->minNsIndex;
11793
7.17k
    if (buildTree)
11794
7.17k
        ctxt->nsdb->minNsIndex = ctxt->nsNr;
11795
11796
7.17k
    oldNodelen = ctxt->nodelen;
11797
7.17k
    oldNodemem = ctxt->nodemem;
11798
7.17k
    ctxt->nodelen = 0;
11799
7.17k
    ctxt->nodemem = 0;
11800
11801
    /*
11802
     * Parse content
11803
     *
11804
     * This initiates a recursive call chain:
11805
     *
11806
     * - xmlCtxtParseContentInternal
11807
     * - xmlParseContentInternal
11808
     * - xmlParseReference
11809
     * - xmlCtxtParseEntity
11810
     *
11811
     * The nesting depth is limited by the maximum number of inputs,
11812
     * see xmlCtxtPushInput.
11813
     *
11814
     * It's possible to make this non-recursive (minNsIndex must be
11815
     * stored in the input struct) at the expense of code readability.
11816
     */
11817
11818
7.17k
    ent->flags |= XML_ENT_EXPANDING;
11819
11820
7.17k
    list = xmlCtxtParseContentInternal(ctxt, input, isExternal, buildTree);
11821
11822
7.17k
    ent->flags &= ~XML_ENT_EXPANDING;
11823
11824
7.17k
    ctxt->nsdb->minNsIndex = oldMinNsIndex;
11825
7.17k
    ctxt->nodelen = oldNodelen;
11826
7.17k
    ctxt->nodemem = oldNodemem;
11827
11828
    /*
11829
     * Entity size accounting
11830
     */
11831
7.17k
    consumed = input->consumed;
11832
7.17k
    xmlSaturatedAddSizeT(&consumed, input->end - input->base);
11833
11834
7.17k
    if ((ent->flags & XML_ENT_CHECKED) == 0)
11835
563
        xmlSaturatedAdd(&ent->expandedSize, consumed);
11836
11837
7.17k
    if ((ent->flags & XML_ENT_PARSED) == 0) {
11838
563
        if (isExternal)
11839
0
            xmlSaturatedAdd(&ctxt->sizeentities, consumed);
11840
11841
563
        ent->children = list;
11842
11843
1.58k
        while (list != NULL) {
11844
1.02k
            list->parent = (xmlNodePtr) ent;
11845
11846
            /*
11847
             * Downstream code like the nginx xslt module can set
11848
             * ctxt->myDoc->extSubset to a separate DTD, so the entity
11849
             * might have a different or a NULL document.
11850
             */
11851
1.02k
            if (list->doc != ent->doc)
11852
0
                xmlSetTreeDoc(list, ent->doc);
11853
11854
1.02k
            if (list->next == NULL)
11855
397
                ent->last = list;
11856
1.02k
            list = list->next;
11857
1.02k
        }
11858
6.61k
    } else {
11859
6.61k
        xmlFreeNodeList(list);
11860
6.61k
    }
11861
11862
7.17k
    xmlFreeInputStream(input);
11863
11864
7.18k
error:
11865
7.18k
    ent->flags |= XML_ENT_PARSED | XML_ENT_CHECKED;
11866
7.18k
}
11867
11868
/**
11869
 * Parse an external general entity within an existing parsing context
11870
 * An external general parsed entity is well-formed if it matches the
11871
 * production labeled extParsedEnt.
11872
 *
11873
 *     [78] extParsedEnt ::= TextDecl? content
11874
 *
11875
 * @param ctxt  the existing parsing context
11876
 * @param URL  the URL for the entity to load
11877
 * @param ID  the System ID for the entity to load
11878
 * @param listOut  the return value for the set of parsed nodes
11879
 * @returns 0 if the entity is well formed, -1 in case of args problem and
11880
 *    the parser error code otherwise
11881
 */
11882
11883
int
11884
xmlParseCtxtExternalEntity(xmlParserCtxt *ctxt, const xmlChar *URL,
11885
0
                           const xmlChar *ID, xmlNode **listOut) {
11886
0
    xmlParserInputPtr input;
11887
0
    xmlNodePtr list;
11888
11889
0
    if (listOut != NULL)
11890
0
        *listOut = NULL;
11891
11892
0
    if (ctxt == NULL)
11893
0
        return(XML_ERR_ARGUMENT);
11894
11895
0
    input = xmlLoadResource(ctxt, (char *) URL, (char *) ID,
11896
0
                            XML_RESOURCE_GENERAL_ENTITY);
11897
0
    if (input == NULL)
11898
0
        return(ctxt->errNo);
11899
11900
0
    xmlCtxtInitializeLate(ctxt);
11901
11902
0
    list = xmlCtxtParseContentInternal(ctxt, input, /* hasTextDecl */ 1, 1);
11903
0
    if (listOut != NULL)
11904
0
        *listOut = list;
11905
0
    else
11906
0
        xmlFreeNodeList(list);
11907
11908
0
    xmlFreeInputStream(input);
11909
0
    return(ctxt->errNo);
11910
0
}
11911
11912
#ifdef LIBXML_SAX1_ENABLED
11913
/**
11914
 * Parse an external general entity
11915
 * An external general parsed entity is well-formed if it matches the
11916
 * production labeled extParsedEnt.
11917
 *
11918
 * @deprecated Use #xmlParseCtxtExternalEntity.
11919
 *
11920
 *     [78] extParsedEnt ::= TextDecl? content
11921
 *
11922
 * @param doc  the document the chunk pertains to
11923
 * @param sax  the SAX handler block (possibly NULL)
11924
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
11925
 * @param depth  Used for loop detection, use 0
11926
 * @param URL  the URL for the entity to load
11927
 * @param ID  the System ID for the entity to load
11928
 * @param list  the return value for the set of parsed nodes
11929
 * @returns 0 if the entity is well formed, -1 in case of args problem and
11930
 *    the parser error code otherwise
11931
 */
11932
11933
int
11934
xmlParseExternalEntity(xmlDoc *doc, xmlSAXHandler *sax, void *user_data,
11935
0
    int depth, const xmlChar *URL, const xmlChar *ID, xmlNode **list) {
11936
0
    xmlParserCtxtPtr ctxt;
11937
0
    int ret;
11938
11939
0
    if (list != NULL)
11940
0
        *list = NULL;
11941
11942
0
    if (doc == NULL)
11943
0
        return(XML_ERR_ARGUMENT);
11944
11945
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11946
0
    if (ctxt == NULL)
11947
0
        return(XML_ERR_NO_MEMORY);
11948
11949
0
    ctxt->depth = depth;
11950
0
    ctxt->myDoc = doc;
11951
0
    ret = xmlParseCtxtExternalEntity(ctxt, URL, ID, list);
11952
11953
0
    xmlFreeParserCtxt(ctxt);
11954
0
    return(ret);
11955
0
}
11956
11957
/**
11958
 * Parse a well-balanced chunk of an XML document
11959
 * called by the parser
11960
 * The allowed sequence for the Well Balanced Chunk is the one defined by
11961
 * the content production in the XML grammar:
11962
 *
11963
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
11964
 *                       Comment)*
11965
 *
11966
 * @param doc  the document the chunk pertains to (must not be NULL)
11967
 * @param sax  the SAX handler block (possibly NULL)
11968
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
11969
 * @param depth  Used for loop detection, use 0
11970
 * @param string  the input string in UTF8 or ISO-Latin (zero terminated)
11971
 * @param lst  the return value for the set of parsed nodes
11972
 * @returns 0 if the chunk is well balanced, -1 in case of args problem and
11973
 *    the parser error code otherwise
11974
 */
11975
11976
int
11977
xmlParseBalancedChunkMemory(xmlDoc *doc, xmlSAXHandler *sax,
11978
0
     void *user_data, int depth, const xmlChar *string, xmlNode **lst) {
11979
0
    return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
11980
0
                                                depth, string, lst, 0 );
11981
0
}
11982
#endif /* LIBXML_SAX1_ENABLED */
11983
11984
/**
11985
 * Parse a well-balanced chunk of XML matching the 'content' production.
11986
 *
11987
 * Namespaces in scope of `node` and entities of `node`'s document are
11988
 * recognized. When validating, the DTD of `node`'s document is used.
11989
 *
11990
 * Always consumes `input` even in error case.
11991
 *
11992
 * @since 2.14.0
11993
 *
11994
 * @param ctxt  parser context
11995
 * @param input  parser input
11996
 * @param node  target node or document
11997
 * @param hasTextDecl  whether to parse text declaration
11998
 * @returns a node list or NULL in case of error.
11999
 */
12000
xmlNode *
12001
xmlCtxtParseContent(xmlParserCtxt *ctxt, xmlParserInput *input,
12002
0
                    xmlNode *node, int hasTextDecl) {
12003
0
    xmlDocPtr doc;
12004
0
    xmlNodePtr cur, list = NULL;
12005
0
    int nsnr = 0;
12006
0
    xmlDictPtr oldDict;
12007
0
    int oldOptions, oldDictNames, oldLoadSubset;
12008
12009
0
    if ((ctxt == NULL) || (input == NULL) || (node == NULL)) {
12010
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12011
0
        goto exit;
12012
0
    }
12013
12014
0
    doc = node->doc;
12015
0
    if (doc == NULL) {
12016
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12017
0
        goto exit;
12018
0
    }
12019
12020
0
    switch (node->type) {
12021
0
        case XML_ELEMENT_NODE:
12022
0
        case XML_DOCUMENT_NODE:
12023
0
        case XML_HTML_DOCUMENT_NODE:
12024
0
            break;
12025
12026
0
        case XML_ATTRIBUTE_NODE:
12027
0
        case XML_TEXT_NODE:
12028
0
        case XML_CDATA_SECTION_NODE:
12029
0
        case XML_ENTITY_REF_NODE:
12030
0
        case XML_PI_NODE:
12031
0
        case XML_COMMENT_NODE:
12032
0
            for (cur = node->parent; cur != NULL; cur = node->parent) {
12033
0
                if ((cur->type == XML_ELEMENT_NODE) ||
12034
0
                    (cur->type == XML_DOCUMENT_NODE) ||
12035
0
                    (cur->type == XML_HTML_DOCUMENT_NODE)) {
12036
0
                    node = cur;
12037
0
                    break;
12038
0
                }
12039
0
            }
12040
0
            break;
12041
12042
0
        default:
12043
0
            xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12044
0
            goto exit;
12045
0
    }
12046
12047
0
    xmlCtxtReset(ctxt);
12048
12049
0
    oldDict = ctxt->dict;
12050
0
    oldOptions = ctxt->options;
12051
0
    oldDictNames = ctxt->dictNames;
12052
0
    oldLoadSubset = ctxt->loadsubset;
12053
12054
    /*
12055
     * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
12056
     */
12057
0
    if (doc->dict != NULL) {
12058
0
        ctxt->dict = doc->dict;
12059
0
    } else {
12060
0
        ctxt->options |= XML_PARSE_NODICT;
12061
0
        ctxt->dictNames = 0;
12062
0
    }
12063
12064
    /*
12065
     * Disable IDs
12066
     */
12067
0
    ctxt->loadsubset |= XML_SKIP_IDS;
12068
12069
0
    ctxt->myDoc = doc;
12070
12071
0
#ifdef LIBXML_HTML_ENABLED
12072
0
    if (ctxt->html) {
12073
        /*
12074
         * When parsing in context, it makes no sense to add implied
12075
         * elements like html/body/etc...
12076
         */
12077
0
        ctxt->options |= HTML_PARSE_NOIMPLIED;
12078
12079
0
        list = htmlCtxtParseContentInternal(ctxt, input);
12080
0
    } else
12081
0
#endif
12082
0
    {
12083
0
        xmlCtxtInitializeLate(ctxt);
12084
12085
        /*
12086
         * initialize the SAX2 namespaces stack
12087
         */
12088
0
        cur = node;
12089
0
        while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
12090
0
            xmlNsPtr ns = cur->nsDef;
12091
0
            xmlHashedString hprefix, huri;
12092
12093
0
            while (ns != NULL) {
12094
0
                hprefix = xmlDictLookupHashed(ctxt->dict, ns->prefix, -1);
12095
0
                huri = xmlDictLookupHashed(ctxt->dict, ns->href, -1);
12096
0
                if (xmlParserNsPush(ctxt, &hprefix, &huri, ns, 1) > 0)
12097
0
                    nsnr++;
12098
0
                ns = ns->next;
12099
0
            }
12100
0
            cur = cur->parent;
12101
0
        }
12102
12103
0
        list = xmlCtxtParseContentInternal(ctxt, input, hasTextDecl, 1);
12104
12105
0
        if (nsnr > 0)
12106
0
            xmlParserNsPop(ctxt, nsnr);
12107
0
    }
12108
12109
0
    ctxt->dict = oldDict;
12110
0
    ctxt->options = oldOptions;
12111
0
    ctxt->dictNames = oldDictNames;
12112
0
    ctxt->loadsubset = oldLoadSubset;
12113
0
    ctxt->myDoc = NULL;
12114
0
    ctxt->node = NULL;
12115
12116
0
exit:
12117
0
    xmlFreeInputStream(input);
12118
0
    return(list);
12119
0
}
12120
12121
/**
12122
 * Parse a well-balanced chunk of an XML document
12123
 * within the context (DTD, namespaces, etc ...) of the given node.
12124
 *
12125
 * The allowed sequence for the data is a Well Balanced Chunk defined by
12126
 * the content production in the XML grammar:
12127
 *
12128
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12129
 *                       Comment)*
12130
 *
12131
 * This function assumes the encoding of `node`'s document which is
12132
 * typically not what you want. A better alternative is
12133
 * #xmlCtxtParseContent.
12134
 *
12135
 * @param node  the context node
12136
 * @param data  the input string
12137
 * @param datalen  the input string length in bytes
12138
 * @param options  a combination of xmlParserOption
12139
 * @param listOut  the return value for the set of parsed nodes
12140
 * @returns XML_ERR_OK if the chunk is well balanced, and the parser
12141
 * error code otherwise
12142
 */
12143
xmlParserErrors
12144
xmlParseInNodeContext(xmlNode *node, const char *data, int datalen,
12145
0
                      int options, xmlNode **listOut) {
12146
0
    xmlParserCtxtPtr ctxt;
12147
0
    xmlParserInputPtr input;
12148
0
    xmlDocPtr doc;
12149
0
    xmlNodePtr list;
12150
0
    xmlParserErrors ret;
12151
12152
0
    if (listOut == NULL)
12153
0
        return(XML_ERR_INTERNAL_ERROR);
12154
0
    *listOut = NULL;
12155
12156
0
    if ((node == NULL) || (data == NULL) || (datalen < 0))
12157
0
        return(XML_ERR_INTERNAL_ERROR);
12158
12159
0
    doc = node->doc;
12160
0
    if (doc == NULL)
12161
0
        return(XML_ERR_INTERNAL_ERROR);
12162
12163
0
#ifdef LIBXML_HTML_ENABLED
12164
0
    if (doc->type == XML_HTML_DOCUMENT_NODE) {
12165
0
        ctxt = htmlNewParserCtxt();
12166
0
    }
12167
0
    else
12168
0
#endif
12169
0
        ctxt = xmlNewParserCtxt();
12170
12171
0
    if (ctxt == NULL)
12172
0
        return(XML_ERR_NO_MEMORY);
12173
12174
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, data, datalen,
12175
0
                                      (const char *) doc->encoding,
12176
0
                                      XML_INPUT_BUF_STATIC);
12177
0
    if (input == NULL) {
12178
0
        xmlFreeParserCtxt(ctxt);
12179
0
        return(XML_ERR_NO_MEMORY);
12180
0
    }
12181
12182
0
    xmlCtxtUseOptions(ctxt, options);
12183
12184
0
    list = xmlCtxtParseContent(ctxt, input, node, /* hasTextDecl */ 0);
12185
12186
0
    if (list == NULL) {
12187
0
        ret = ctxt->errNo;
12188
0
        if (ret == XML_ERR_ARGUMENT)
12189
0
            ret = XML_ERR_INTERNAL_ERROR;
12190
0
    } else {
12191
0
        ret = XML_ERR_OK;
12192
0
        *listOut = list;
12193
0
    }
12194
12195
0
    xmlFreeParserCtxt(ctxt);
12196
12197
0
    return(ret);
12198
0
}
12199
12200
#ifdef LIBXML_SAX1_ENABLED
12201
/**
12202
 * Parse a well-balanced chunk of an XML document
12203
 *
12204
 * The allowed sequence for the Well Balanced Chunk is the one defined by
12205
 * the content production in the XML grammar:
12206
 *
12207
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12208
 *                       Comment)*
12209
 *
12210
 * In case recover is set to 1, the nodelist will not be empty even if
12211
 * the parsed chunk is not well balanced, assuming the parsing succeeded to
12212
 * some extent.
12213
 *
12214
 * @param doc  the document the chunk pertains to (must not be NULL)
12215
 * @param sax  the SAX handler block (possibly NULL)
12216
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
12217
 * @param depth  Used for loop detection, use 0
12218
 * @param string  the input string in UTF8 or ISO-Latin (zero terminated)
12219
 * @param listOut  the return value for the set of parsed nodes
12220
 * @param recover  return nodes even if the data is broken (use 0)
12221
 * @returns 0 if the chunk is well balanced, or thehe parser error code
12222
 * otherwise.
12223
 */
12224
int
12225
xmlParseBalancedChunkMemoryRecover(xmlDoc *doc, xmlSAXHandler *sax,
12226
     void *user_data, int depth, const xmlChar *string, xmlNode **listOut,
12227
0
     int recover) {
12228
0
    xmlParserCtxtPtr ctxt;
12229
0
    xmlParserInputPtr input;
12230
0
    xmlNodePtr list;
12231
0
    int ret;
12232
12233
0
    if (listOut != NULL)
12234
0
        *listOut = NULL;
12235
12236
0
    if (string == NULL)
12237
0
        return(XML_ERR_ARGUMENT);
12238
12239
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
12240
0
    if (ctxt == NULL)
12241
0
        return(XML_ERR_NO_MEMORY);
12242
12243
0
    xmlCtxtInitializeLate(ctxt);
12244
12245
0
    ctxt->depth = depth;
12246
0
    ctxt->myDoc = doc;
12247
0
    if (recover) {
12248
0
        ctxt->options |= XML_PARSE_RECOVER;
12249
0
        ctxt->recovery = 1;
12250
0
    }
12251
12252
0
    input = xmlNewStringInputStream(ctxt, string);
12253
0
    if (input == NULL) {
12254
0
        ret = ctxt->errNo;
12255
0
        goto error;
12256
0
    }
12257
12258
0
    list = xmlCtxtParseContentInternal(ctxt, input, /* hasTextDecl */ 0, 1);
12259
0
    if (listOut != NULL)
12260
0
        *listOut = list;
12261
0
    else
12262
0
        xmlFreeNodeList(list);
12263
12264
0
    if (!ctxt->wellFormed)
12265
0
        ret = ctxt->errNo;
12266
0
    else
12267
0
        ret = XML_ERR_OK;
12268
12269
0
error:
12270
0
    xmlFreeInputStream(input);
12271
0
    xmlFreeParserCtxt(ctxt);
12272
0
    return(ret);
12273
0
}
12274
12275
/**
12276
 * parse an XML external entity out of context and build a tree.
12277
 * It use the given SAX function block to handle the parsing callback.
12278
 * If sax is NULL, fallback to the default DOM tree building routines.
12279
 *
12280
 * @deprecated Don't use.
12281
 *
12282
 *     [78] extParsedEnt ::= TextDecl? content
12283
 *
12284
 * This correspond to a "Well Balanced" chunk
12285
 *
12286
 * @param sax  the SAX handler block
12287
 * @param filename  the filename
12288
 * @returns the resulting document tree
12289
 */
12290
12291
xmlDoc *
12292
0
xmlSAXParseEntity(xmlSAXHandler *sax, const char *filename) {
12293
0
    xmlDocPtr ret;
12294
0
    xmlParserCtxtPtr ctxt;
12295
12296
0
    ctxt = xmlCreateFileParserCtxt(filename);
12297
0
    if (ctxt == NULL) {
12298
0
  return(NULL);
12299
0
    }
12300
0
    if (sax != NULL) {
12301
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12302
0
            *ctxt->sax = *sax;
12303
0
        } else {
12304
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12305
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12306
0
        }
12307
0
        ctxt->userData = NULL;
12308
0
    }
12309
12310
0
    xmlParseExtParsedEnt(ctxt);
12311
12312
0
    if (ctxt->wellFormed) {
12313
0
  ret = ctxt->myDoc;
12314
0
    } else {
12315
0
        ret = NULL;
12316
0
        xmlFreeDoc(ctxt->myDoc);
12317
0
    }
12318
12319
0
    xmlFreeParserCtxt(ctxt);
12320
12321
0
    return(ret);
12322
0
}
12323
12324
/**
12325
 * parse an XML external entity out of context and build a tree.
12326
 *
12327
 *     [78] extParsedEnt ::= TextDecl? content
12328
 *
12329
 * This correspond to a "Well Balanced" chunk
12330
 *
12331
 * @param filename  the filename
12332
 * @returns the resulting document tree
12333
 */
12334
12335
xmlDoc *
12336
0
xmlParseEntity(const char *filename) {
12337
0
    return(xmlSAXParseEntity(NULL, filename));
12338
0
}
12339
#endif /* LIBXML_SAX1_ENABLED */
12340
12341
/**
12342
 * Create a parser context for an external entity
12343
 * Automatic support for ZLIB/Compress compressed document is provided
12344
 * by default if found at compile-time.
12345
 *
12346
 * @deprecated Don't use.
12347
 *
12348
 * @param URL  the entity URL
12349
 * @param ID  the entity PUBLIC ID
12350
 * @param base  a possible base for the target URI
12351
 * @returns the new parser context or NULL
12352
 */
12353
xmlParserCtxt *
12354
xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
12355
0
                    const xmlChar *base) {
12356
0
    xmlParserCtxtPtr ctxt;
12357
0
    xmlParserInputPtr input;
12358
0
    xmlChar *uri = NULL;
12359
12360
0
    ctxt = xmlNewParserCtxt();
12361
0
    if (ctxt == NULL)
12362
0
  return(NULL);
12363
12364
0
    if (base != NULL) {
12365
0
        if (xmlBuildURISafe(URL, base, &uri) < 0)
12366
0
            goto error;
12367
0
        if (uri != NULL)
12368
0
            URL = uri;
12369
0
    }
12370
12371
0
    input = xmlLoadResource(ctxt, (char *) URL, (char *) ID,
12372
0
                            XML_RESOURCE_UNKNOWN);
12373
0
    if (input == NULL)
12374
0
        goto error;
12375
12376
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12377
0
        xmlFreeInputStream(input);
12378
0
        goto error;
12379
0
    }
12380
12381
0
    xmlFree(uri);
12382
0
    return(ctxt);
12383
12384
0
error:
12385
0
    xmlFree(uri);
12386
0
    xmlFreeParserCtxt(ctxt);
12387
0
    return(NULL);
12388
0
}
12389
12390
/************************************************************************
12391
 *                  *
12392
 *    Front ends when parsing from a file     *
12393
 *                  *
12394
 ************************************************************************/
12395
12396
/**
12397
 * Create a parser context for a file or URL content.
12398
 * Automatic support for ZLIB/Compress compressed document is provided
12399
 * by default if found at compile-time and for file accesses
12400
 *
12401
 * @deprecated Use #xmlNewParserCtxt and #xmlCtxtReadFile.
12402
 *
12403
 * @param filename  the filename or URL
12404
 * @param options  a combination of xmlParserOption
12405
 * @returns the new parser context or NULL
12406
 */
12407
xmlParserCtxt *
12408
xmlCreateURLParserCtxt(const char *filename, int options)
12409
0
{
12410
0
    xmlParserCtxtPtr ctxt;
12411
0
    xmlParserInputPtr input;
12412
12413
0
    ctxt = xmlNewParserCtxt();
12414
0
    if (ctxt == NULL)
12415
0
  return(NULL);
12416
12417
0
    options |= XML_PARSE_UNZIP;
12418
12419
0
    xmlCtxtUseOptions(ctxt, options);
12420
12421
0
    input = xmlLoadResource(ctxt, filename, NULL, XML_RESOURCE_MAIN_DOCUMENT);
12422
0
    if (input == NULL) {
12423
0
  xmlFreeParserCtxt(ctxt);
12424
0
  return(NULL);
12425
0
    }
12426
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12427
0
        xmlFreeInputStream(input);
12428
0
        xmlFreeParserCtxt(ctxt);
12429
0
        return(NULL);
12430
0
    }
12431
12432
0
    return(ctxt);
12433
0
}
12434
12435
/**
12436
 * Create a parser context for a file content.
12437
 * Automatic support for ZLIB/Compress compressed document is provided
12438
 * by default if found at compile-time.
12439
 *
12440
 * @deprecated Use #xmlNewParserCtxt and #xmlCtxtReadFile.
12441
 *
12442
 * @param filename  the filename
12443
 * @returns the new parser context or NULL
12444
 */
12445
xmlParserCtxt *
12446
xmlCreateFileParserCtxt(const char *filename)
12447
0
{
12448
0
    return(xmlCreateURLParserCtxt(filename, 0));
12449
0
}
12450
12451
#ifdef LIBXML_SAX1_ENABLED
12452
/**
12453
 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
12454
 * compressed document is provided by default if found at compile-time.
12455
 * It use the given SAX function block to handle the parsing callback.
12456
 * If sax is NULL, fallback to the default DOM tree building routines.
12457
 *
12458
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12459
 *
12460
 * User data (void *) is stored within the parser context in the
12461
 * context's _private member, so it is available nearly everywhere in libxml
12462
 *
12463
 * @param sax  the SAX handler block
12464
 * @param filename  the filename
12465
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12466
 *             documents
12467
 * @param data  the userdata
12468
 * @returns the resulting document tree
12469
 */
12470
12471
xmlDoc *
12472
xmlSAXParseFileWithData(xmlSAXHandler *sax, const char *filename,
12473
0
                        int recovery, void *data) {
12474
0
    xmlDocPtr ret = NULL;
12475
0
    xmlParserCtxtPtr ctxt;
12476
0
    xmlParserInputPtr input;
12477
12478
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
12479
0
    if (ctxt == NULL)
12480
0
  return(NULL);
12481
12482
0
    if (data != NULL)
12483
0
  ctxt->_private = data;
12484
12485
0
    if (recovery) {
12486
0
        ctxt->options |= XML_PARSE_RECOVER;
12487
0
        ctxt->recovery = 1;
12488
0
    }
12489
12490
0
    if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0))
12491
0
        input = xmlCtxtNewInputFromFd(ctxt, filename, STDIN_FILENO, NULL, 0);
12492
0
    else
12493
0
        input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, NULL, 0);
12494
12495
0
    if (input != NULL)
12496
0
        ret = xmlCtxtParseDocument(ctxt, input);
12497
12498
0
    xmlFreeParserCtxt(ctxt);
12499
0
    return(ret);
12500
0
}
12501
12502
/**
12503
 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
12504
 * compressed document is provided by default if found at compile-time.
12505
 * It use the given SAX function block to handle the parsing callback.
12506
 * If sax is NULL, fallback to the default DOM tree building routines.
12507
 *
12508
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12509
 *
12510
 * @param sax  the SAX handler block
12511
 * @param filename  the filename
12512
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12513
 *             documents
12514
 * @returns the resulting document tree
12515
 */
12516
12517
xmlDoc *
12518
xmlSAXParseFile(xmlSAXHandler *sax, const char *filename,
12519
0
                          int recovery) {
12520
0
    return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
12521
0
}
12522
12523
/**
12524
 * parse an XML in-memory document and build a tree.
12525
 * In the case the document is not Well Formed, a attempt to build a
12526
 * tree is tried anyway
12527
 *
12528
 * @deprecated Use #xmlReadDoc with XML_PARSE_RECOVER.
12529
 *
12530
 * @param cur  a pointer to an array of xmlChar
12531
 * @returns the resulting document tree or NULL in case of failure
12532
 */
12533
12534
xmlDoc *
12535
0
xmlRecoverDoc(const xmlChar *cur) {
12536
0
    return(xmlSAXParseDoc(NULL, cur, 1));
12537
0
}
12538
12539
/**
12540
 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
12541
 * compressed document is provided by default if found at compile-time.
12542
 *
12543
 * @deprecated Use #xmlReadFile.
12544
 *
12545
 * @param filename  the filename
12546
 * @returns the resulting document tree if the file was wellformed,
12547
 * NULL otherwise.
12548
 */
12549
12550
xmlDoc *
12551
0
xmlParseFile(const char *filename) {
12552
0
    return(xmlSAXParseFile(NULL, filename, 0));
12553
0
}
12554
12555
/**
12556
 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
12557
 * compressed document is provided by default if found at compile-time.
12558
 * In the case the document is not Well Formed, it attempts to build
12559
 * a tree anyway
12560
 *
12561
 * @deprecated Use #xmlReadFile with XML_PARSE_RECOVER.
12562
 *
12563
 * @param filename  the filename
12564
 * @returns the resulting document tree or NULL in case of failure
12565
 */
12566
12567
xmlDoc *
12568
0
xmlRecoverFile(const char *filename) {
12569
0
    return(xmlSAXParseFile(NULL, filename, 1));
12570
0
}
12571
12572
12573
/**
12574
 * Setup the parser context to parse a new buffer; Clears any prior
12575
 * contents from the parser context. The buffer parameter must not be
12576
 * NULL, but the filename parameter can be
12577
 *
12578
 * @deprecated Don't use.
12579
 *
12580
 * @param ctxt  an XML parser context
12581
 * @param buffer  a xmlChar * buffer
12582
 * @param filename  a file name
12583
 */
12584
void
12585
xmlSetupParserForBuffer(xmlParserCtxt *ctxt, const xmlChar* buffer,
12586
                             const char* filename)
12587
0
{
12588
0
    xmlParserInputPtr input;
12589
12590
0
    if ((ctxt == NULL) || (buffer == NULL))
12591
0
        return;
12592
12593
0
    xmlCtxtReset(ctxt);
12594
12595
0
    input = xmlCtxtNewInputFromString(ctxt, filename, (const char *) buffer,
12596
0
                                      NULL, 0);
12597
0
    if (input == NULL)
12598
0
        return;
12599
0
    if (xmlCtxtPushInput(ctxt, input) < 0)
12600
0
        xmlFreeInputStream(input);
12601
0
}
12602
12603
/**
12604
 * parse an XML file and call the given SAX handler routines.
12605
 * Automatic support for ZLIB/Compress compressed document is provided
12606
 *
12607
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12608
 *
12609
 * @param sax  a SAX handler
12610
 * @param user_data  The user data returned on SAX callbacks
12611
 * @param filename  a file name
12612
 * @returns 0 in case of success or a error number otherwise
12613
 */
12614
int
12615
xmlSAXUserParseFile(xmlSAXHandler *sax, void *user_data,
12616
0
                    const char *filename) {
12617
0
    int ret = 0;
12618
0
    xmlParserCtxtPtr ctxt;
12619
12620
0
    ctxt = xmlCreateFileParserCtxt(filename);
12621
0
    if (ctxt == NULL) return -1;
12622
0
    if (sax != NULL) {
12623
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12624
0
            *ctxt->sax = *sax;
12625
0
        } else {
12626
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12627
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12628
0
        }
12629
0
  ctxt->userData = user_data;
12630
0
    }
12631
12632
0
    xmlParseDocument(ctxt);
12633
12634
0
    if (ctxt->wellFormed)
12635
0
  ret = 0;
12636
0
    else {
12637
0
        if (ctxt->errNo != 0)
12638
0
      ret = ctxt->errNo;
12639
0
  else
12640
0
      ret = -1;
12641
0
    }
12642
0
    if (ctxt->myDoc != NULL) {
12643
0
        xmlFreeDoc(ctxt->myDoc);
12644
0
  ctxt->myDoc = NULL;
12645
0
    }
12646
0
    xmlFreeParserCtxt(ctxt);
12647
12648
0
    return ret;
12649
0
}
12650
#endif /* LIBXML_SAX1_ENABLED */
12651
12652
/************************************************************************
12653
 *                  *
12654
 *    Front ends when parsing from memory     *
12655
 *                  *
12656
 ************************************************************************/
12657
12658
/**
12659
 * Create a parser context for an XML in-memory document. The input buffer
12660
 * must not contain a terminating null byte.
12661
 *
12662
 * @param buffer  a pointer to a char array
12663
 * @param size  the size of the array
12664
 * @returns the new parser context or NULL
12665
 */
12666
xmlParserCtxt *
12667
14.5k
xmlCreateMemoryParserCtxt(const char *buffer, int size) {
12668
14.5k
    xmlParserCtxtPtr ctxt;
12669
14.5k
    xmlParserInputPtr input;
12670
12671
14.5k
    if (size < 0)
12672
0
  return(NULL);
12673
12674
14.5k
    ctxt = xmlNewParserCtxt();
12675
14.5k
    if (ctxt == NULL)
12676
0
  return(NULL);
12677
12678
14.5k
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, buffer, size, NULL, 0);
12679
14.5k
    if (input == NULL) {
12680
0
  xmlFreeParserCtxt(ctxt);
12681
0
  return(NULL);
12682
0
    }
12683
14.5k
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12684
0
        xmlFreeInputStream(input);
12685
0
        xmlFreeParserCtxt(ctxt);
12686
0
        return(NULL);
12687
0
    }
12688
12689
14.5k
    return(ctxt);
12690
14.5k
}
12691
12692
#ifdef LIBXML_SAX1_ENABLED
12693
/**
12694
 * parse an XML in-memory block and use the given SAX function block
12695
 * to handle the parsing callback. If sax is NULL, fallback to the default
12696
 * DOM tree building routines.
12697
 *
12698
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12699
 *
12700
 * User data (void *) is stored within the parser context in the
12701
 * context's _private member, so it is available nearly everywhere in libxml
12702
 *
12703
 * @param sax  the SAX handler block
12704
 * @param buffer  an pointer to a char array
12705
 * @param size  the size of the array
12706
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12707
 *             documents
12708
 * @param data  the userdata
12709
 * @returns the resulting document tree
12710
 */
12711
12712
xmlDoc *
12713
xmlSAXParseMemoryWithData(xmlSAXHandler *sax, const char *buffer,
12714
0
                          int size, int recovery, void *data) {
12715
0
    xmlDocPtr ret = NULL;
12716
0
    xmlParserCtxtPtr ctxt;
12717
0
    xmlParserInputPtr input;
12718
12719
0
    if (size < 0)
12720
0
        return(NULL);
12721
12722
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
12723
0
    if (ctxt == NULL)
12724
0
        return(NULL);
12725
12726
0
    if (data != NULL)
12727
0
  ctxt->_private=data;
12728
12729
0
    if (recovery) {
12730
0
        ctxt->options |= XML_PARSE_RECOVER;
12731
0
        ctxt->recovery = 1;
12732
0
    }
12733
12734
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, buffer, size, NULL,
12735
0
                                      XML_INPUT_BUF_STATIC);
12736
12737
0
    if (input != NULL)
12738
0
        ret = xmlCtxtParseDocument(ctxt, input);
12739
12740
0
    xmlFreeParserCtxt(ctxt);
12741
0
    return(ret);
12742
0
}
12743
12744
/**
12745
 * parse an XML in-memory block and use the given SAX function block
12746
 * to handle the parsing callback. If sax is NULL, fallback to the default
12747
 * DOM tree building routines.
12748
 *
12749
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12750
 *
12751
 * @param sax  the SAX handler block
12752
 * @param buffer  an pointer to a char array
12753
 * @param size  the size of the array
12754
 * @param recovery  work in recovery mode, i.e. tries to read not Well Formed
12755
 *             documents
12756
 * @returns the resulting document tree
12757
 */
12758
xmlDoc *
12759
xmlSAXParseMemory(xmlSAXHandler *sax, const char *buffer,
12760
0
            int size, int recovery) {
12761
0
    return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
12762
0
}
12763
12764
/**
12765
 * parse an XML in-memory block and build a tree.
12766
 *
12767
 * @deprecated Use #xmlReadMemory.
12768
 *
12769
 * @param buffer  an pointer to a char array
12770
 * @param size  the size of the array
12771
 * @returns the resulting document tree
12772
 */
12773
12774
0
xmlDoc *xmlParseMemory(const char *buffer, int size) {
12775
0
   return(xmlSAXParseMemory(NULL, buffer, size, 0));
12776
0
}
12777
12778
/**
12779
 * parse an XML in-memory block and build a tree.
12780
 * In the case the document is not Well Formed, an attempt to
12781
 * build a tree is tried anyway
12782
 *
12783
 * @deprecated Use #xmlReadMemory with XML_PARSE_RECOVER.
12784
 *
12785
 * @param buffer  an pointer to a char array
12786
 * @param size  the size of the array
12787
 * @returns the resulting document tree or NULL in case of error
12788
 */
12789
12790
0
xmlDoc *xmlRecoverMemory(const char *buffer, int size) {
12791
0
   return(xmlSAXParseMemory(NULL, buffer, size, 1));
12792
0
}
12793
12794
/**
12795
 * parse an XML in-memory buffer and call the given SAX handler routines.
12796
 *
12797
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12798
 *
12799
 * @param sax  a SAX handler
12800
 * @param user_data  The user data returned on SAX callbacks
12801
 * @param buffer  an in-memory XML document input
12802
 * @param size  the length of the XML document in bytes
12803
 * @returns 0 in case of success or a error number otherwise
12804
 */
12805
int xmlSAXUserParseMemory(xmlSAXHandler *sax, void *user_data,
12806
0
        const char *buffer, int size) {
12807
0
    int ret = 0;
12808
0
    xmlParserCtxtPtr ctxt;
12809
12810
0
    ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12811
0
    if (ctxt == NULL) return -1;
12812
0
    if (sax != NULL) {
12813
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12814
0
            *ctxt->sax = *sax;
12815
0
        } else {
12816
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12817
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12818
0
        }
12819
0
  ctxt->userData = user_data;
12820
0
    }
12821
12822
0
    xmlParseDocument(ctxt);
12823
12824
0
    if (ctxt->wellFormed)
12825
0
  ret = 0;
12826
0
    else {
12827
0
        if (ctxt->errNo != 0)
12828
0
      ret = ctxt->errNo;
12829
0
  else
12830
0
      ret = -1;
12831
0
    }
12832
0
    if (ctxt->myDoc != NULL) {
12833
0
        xmlFreeDoc(ctxt->myDoc);
12834
0
  ctxt->myDoc = NULL;
12835
0
    }
12836
0
    xmlFreeParserCtxt(ctxt);
12837
12838
0
    return ret;
12839
0
}
12840
#endif /* LIBXML_SAX1_ENABLED */
12841
12842
/**
12843
 * Creates a parser context for an XML in-memory document.
12844
 *
12845
 * @param str  a pointer to an array of xmlChar
12846
 * @returns the new parser context or NULL
12847
 */
12848
xmlParserCtxt *
12849
0
xmlCreateDocParserCtxt(const xmlChar *str) {
12850
0
    xmlParserCtxtPtr ctxt;
12851
0
    xmlParserInputPtr input;
12852
12853
0
    ctxt = xmlNewParserCtxt();
12854
0
    if (ctxt == NULL)
12855
0
  return(NULL);
12856
12857
0
    input = xmlCtxtNewInputFromString(ctxt, NULL, (const char *) str, NULL, 0);
12858
0
    if (input == NULL) {
12859
0
  xmlFreeParserCtxt(ctxt);
12860
0
  return(NULL);
12861
0
    }
12862
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12863
0
        xmlFreeInputStream(input);
12864
0
        xmlFreeParserCtxt(ctxt);
12865
0
        return(NULL);
12866
0
    }
12867
12868
0
    return(ctxt);
12869
0
}
12870
12871
#ifdef LIBXML_SAX1_ENABLED
12872
/**
12873
 * parse an XML in-memory document and build a tree.
12874
 * It use the given SAX function block to handle the parsing callback.
12875
 * If sax is NULL, fallback to the default DOM tree building routines.
12876
 *
12877
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadDoc.
12878
 *
12879
 * @param sax  the SAX handler block
12880
 * @param cur  a pointer to an array of xmlChar
12881
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12882
 *             documents
12883
 * @returns the resulting document tree
12884
 */
12885
12886
xmlDoc *
12887
0
xmlSAXParseDoc(xmlSAXHandler *sax, const xmlChar *cur, int recovery) {
12888
0
    xmlDocPtr ret;
12889
0
    xmlParserCtxtPtr ctxt;
12890
0
    xmlSAXHandlerPtr oldsax = NULL;
12891
12892
0
    if (cur == NULL) return(NULL);
12893
12894
12895
0
    ctxt = xmlCreateDocParserCtxt(cur);
12896
0
    if (ctxt == NULL) return(NULL);
12897
0
    if (sax != NULL) {
12898
0
        oldsax = ctxt->sax;
12899
0
        ctxt->sax = sax;
12900
0
        ctxt->userData = NULL;
12901
0
    }
12902
12903
0
    xmlParseDocument(ctxt);
12904
0
    if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
12905
0
    else {
12906
0
       ret = NULL;
12907
0
       xmlFreeDoc(ctxt->myDoc);
12908
0
       ctxt->myDoc = NULL;
12909
0
    }
12910
0
    if (sax != NULL)
12911
0
  ctxt->sax = oldsax;
12912
0
    xmlFreeParserCtxt(ctxt);
12913
12914
0
    return(ret);
12915
0
}
12916
12917
/**
12918
 * parse an XML in-memory document and build a tree.
12919
 *
12920
 * @deprecated Use #xmlReadDoc.
12921
 *
12922
 * @param cur  a pointer to an array of xmlChar
12923
 * @returns the resulting document tree
12924
 */
12925
12926
xmlDoc *
12927
0
xmlParseDoc(const xmlChar *cur) {
12928
0
    return(xmlSAXParseDoc(NULL, cur, 0));
12929
0
}
12930
#endif /* LIBXML_SAX1_ENABLED */
12931
12932
/************************************************************************
12933
 *                  *
12934
 *  New set (2.6.0) of simpler and more flexible APIs   *
12935
 *                  *
12936
 ************************************************************************/
12937
12938
/**
12939
 * Free a string if it is not owned by the "dict" dictionary in the
12940
 * current scope
12941
 *
12942
 * @param str  a string
12943
 */
12944
#define DICT_FREE(str)            \
12945
0
  if ((str) && ((!dict) ||       \
12946
0
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
12947
0
      xmlFree((char *)(str));
12948
12949
/**
12950
 * Reset a parser context
12951
 *
12952
 * @param ctxt  an XML parser context
12953
 */
12954
void
12955
xmlCtxtReset(xmlParserCtxt *ctxt)
12956
0
{
12957
0
    xmlParserInputPtr input;
12958
0
    xmlDictPtr dict;
12959
12960
0
    if (ctxt == NULL)
12961
0
        return;
12962
12963
0
    dict = ctxt->dict;
12964
12965
0
    while ((input = xmlCtxtPopInput(ctxt)) != NULL) { /* Non consuming */
12966
0
        xmlFreeInputStream(input);
12967
0
    }
12968
0
    ctxt->inputNr = 0;
12969
0
    ctxt->input = NULL;
12970
12971
0
    ctxt->spaceNr = 0;
12972
0
    if (ctxt->spaceTab != NULL) {
12973
0
  ctxt->spaceTab[0] = -1;
12974
0
  ctxt->space = &ctxt->spaceTab[0];
12975
0
    } else {
12976
0
        ctxt->space = NULL;
12977
0
    }
12978
12979
12980
0
    ctxt->nodeNr = 0;
12981
0
    ctxt->node = NULL;
12982
12983
0
    ctxt->nameNr = 0;
12984
0
    ctxt->name = NULL;
12985
12986
0
    ctxt->nsNr = 0;
12987
0
    xmlParserNsReset(ctxt->nsdb);
12988
12989
0
    DICT_FREE(ctxt->version);
12990
0
    ctxt->version = NULL;
12991
0
    DICT_FREE(ctxt->encoding);
12992
0
    ctxt->encoding = NULL;
12993
0
    DICT_FREE(ctxt->extSubURI);
12994
0
    ctxt->extSubURI = NULL;
12995
0
    DICT_FREE(ctxt->extSubSystem);
12996
0
    ctxt->extSubSystem = NULL;
12997
12998
0
    if (ctxt->directory != NULL) {
12999
0
        xmlFree(ctxt->directory);
13000
0
        ctxt->directory = NULL;
13001
0
    }
13002
13003
0
    if (ctxt->myDoc != NULL)
13004
0
        xmlFreeDoc(ctxt->myDoc);
13005
0
    ctxt->myDoc = NULL;
13006
13007
0
    ctxt->standalone = -1;
13008
0
    ctxt->hasExternalSubset = 0;
13009
0
    ctxt->hasPErefs = 0;
13010
0
    ctxt->html = ctxt->html ? 1 : 0;
13011
0
    ctxt->instate = XML_PARSER_START;
13012
13013
0
    ctxt->wellFormed = 1;
13014
0
    ctxt->nsWellFormed = 1;
13015
0
    ctxt->disableSAX = 0;
13016
0
    ctxt->valid = 1;
13017
0
    ctxt->record_info = 0;
13018
0
    ctxt->checkIndex = 0;
13019
0
    ctxt->endCheckState = 0;
13020
0
    ctxt->inSubset = 0;
13021
0
    ctxt->errNo = XML_ERR_OK;
13022
0
    ctxt->depth = 0;
13023
0
    ctxt->catalogs = NULL;
13024
0
    ctxt->sizeentities = 0;
13025
0
    ctxt->sizeentcopy = 0;
13026
0
    xmlInitNodeInfoSeq(&ctxt->node_seq);
13027
13028
0
    if (ctxt->attsDefault != NULL) {
13029
0
        xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
13030
0
        ctxt->attsDefault = NULL;
13031
0
    }
13032
0
    if (ctxt->attsSpecial != NULL) {
13033
0
        xmlHashFree(ctxt->attsSpecial, NULL);
13034
0
        ctxt->attsSpecial = NULL;
13035
0
    }
13036
13037
0
#ifdef LIBXML_CATALOG_ENABLED
13038
0
    if (ctxt->catalogs != NULL)
13039
0
  xmlCatalogFreeLocal(ctxt->catalogs);
13040
0
#endif
13041
0
    ctxt->nbErrors = 0;
13042
0
    ctxt->nbWarnings = 0;
13043
0
    if (ctxt->lastError.code != XML_ERR_OK)
13044
0
        xmlResetError(&ctxt->lastError);
13045
0
}
13046
13047
/**
13048
 * Reset a push parser context
13049
 *
13050
 * @param ctxt  an XML parser context
13051
 * @param chunk  a pointer to an array of chars
13052
 * @param size  number of chars in the array
13053
 * @param filename  an optional file name or URI
13054
 * @param encoding  the document encoding, or NULL
13055
 * @returns 0 in case of success and 1 in case of error
13056
 */
13057
int
13058
xmlCtxtResetPush(xmlParserCtxt *ctxt, const char *chunk,
13059
                 int size, const char *filename, const char *encoding)
13060
0
{
13061
0
    xmlParserInputPtr input;
13062
13063
0
    if (ctxt == NULL)
13064
0
        return(1);
13065
13066
0
    xmlCtxtReset(ctxt);
13067
13068
0
    input = xmlNewPushInput(filename, chunk, size);
13069
0
    if (input == NULL)
13070
0
        return(1);
13071
13072
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
13073
0
        xmlFreeInputStream(input);
13074
0
        return(1);
13075
0
    }
13076
13077
0
    if (encoding != NULL)
13078
0
        xmlSwitchEncodingName(ctxt, encoding);
13079
13080
0
    return(0);
13081
0
}
13082
13083
static int
13084
xmlCtxtSetOptionsInternal(xmlParserCtxtPtr ctxt, int options, int keepMask)
13085
29.0k
{
13086
29.0k
    int allMask;
13087
13088
29.0k
    if (ctxt == NULL)
13089
0
        return(-1);
13090
13091
    /*
13092
     * XInclude options aren't handled by the parser.
13093
     *
13094
     * XML_PARSE_XINCLUDE
13095
     * XML_PARSE_NOXINCNODE
13096
     * XML_PARSE_NOBASEFIX
13097
     */
13098
29.0k
    allMask = XML_PARSE_RECOVER |
13099
29.0k
              XML_PARSE_NOENT |
13100
29.0k
              XML_PARSE_DTDLOAD |
13101
29.0k
              XML_PARSE_DTDATTR |
13102
29.0k
              XML_PARSE_DTDVALID |
13103
29.0k
              XML_PARSE_NOERROR |
13104
29.0k
              XML_PARSE_NOWARNING |
13105
29.0k
              XML_PARSE_PEDANTIC |
13106
29.0k
              XML_PARSE_NOBLANKS |
13107
29.0k
#ifdef LIBXML_SAX1_ENABLED
13108
29.0k
              XML_PARSE_SAX1 |
13109
29.0k
#endif
13110
29.0k
              XML_PARSE_NONET |
13111
29.0k
              XML_PARSE_NODICT |
13112
29.0k
              XML_PARSE_NSCLEAN |
13113
29.0k
              XML_PARSE_NOCDATA |
13114
29.0k
              XML_PARSE_COMPACT |
13115
29.0k
              XML_PARSE_OLD10 |
13116
29.0k
              XML_PARSE_HUGE |
13117
29.0k
              XML_PARSE_OLDSAX |
13118
29.0k
              XML_PARSE_IGNORE_ENC |
13119
29.0k
              XML_PARSE_BIG_LINES |
13120
29.0k
              XML_PARSE_NO_XXE |
13121
29.0k
              XML_PARSE_UNZIP |
13122
29.0k
              XML_PARSE_NO_SYS_CATALOG |
13123
29.0k
              XML_PARSE_CATALOG_PI;
13124
13125
29.0k
    ctxt->options = (ctxt->options & keepMask) | (options & allMask);
13126
13127
    /*
13128
     * For some options, struct members are historically the source
13129
     * of truth. The values are initalized from global variables and
13130
     * old code could also modify them directly. Several older API
13131
     * functions that don't take an options argument rely on these
13132
     * deprecated mechanisms.
13133
     *
13134
     * Once public access to struct members and the globals are
13135
     * disabled, we can use the options bitmask as source of
13136
     * truth, making all these struct members obsolete.
13137
     *
13138
     * The XML_DETECT_IDS flags is misnamed. It simply enables
13139
     * loading of the external subset.
13140
     */
13141
29.0k
    ctxt->recovery = (options & XML_PARSE_RECOVER) ? 1 : 0;
13142
29.0k
    ctxt->replaceEntities = (options & XML_PARSE_NOENT) ? 1 : 0;
13143
29.0k
    ctxt->loadsubset = (options & XML_PARSE_DTDLOAD) ? XML_DETECT_IDS : 0;
13144
29.0k
    ctxt->loadsubset |= (options & XML_PARSE_DTDATTR) ? XML_COMPLETE_ATTRS : 0;
13145
29.0k
    ctxt->validate = (options & XML_PARSE_DTDVALID) ? 1 : 0;
13146
29.0k
    ctxt->pedantic = (options & XML_PARSE_PEDANTIC) ? 1 : 0;
13147
29.0k
    ctxt->keepBlanks = (options & XML_PARSE_NOBLANKS) ? 0 : 1;
13148
29.0k
    ctxt->dictNames = (options & XML_PARSE_NODICT) ? 0 : 1;
13149
13150
29.0k
    if (options & XML_PARSE_HUGE) {
13151
29.0k
        if (ctxt->dict != NULL)
13152
29.0k
            xmlDictSetLimit(ctxt->dict, 0);
13153
29.0k
    }
13154
13155
29.0k
    return(options & ~allMask);
13156
29.0k
}
13157
13158
/**
13159
 * Applies the options to the parser context. Unset options are
13160
 * cleared.
13161
 *
13162
 * @since 2.13.0
13163
 *
13164
 * With older versions, you can use #xmlCtxtUseOptions.
13165
 *
13166
 * @param ctxt  an XML parser context
13167
 * @param options  a bitmask of xmlParserOption values
13168
 * @returns 0 in case of success, the set of unknown or unimplemented options
13169
 *         in case of error.
13170
 */
13171
int
13172
xmlCtxtSetOptions(xmlParserCtxt *ctxt, int options)
13173
14.5k
{
13174
14.5k
#ifdef LIBXML_HTML_ENABLED
13175
14.5k
    if ((ctxt != NULL) && (ctxt->html))
13176
0
        return(htmlCtxtSetOptions(ctxt, options));
13177
14.5k
#endif
13178
13179
14.5k
    return(xmlCtxtSetOptionsInternal(ctxt, options, 0));
13180
14.5k
}
13181
13182
/**
13183
 * Get the current options of the parser context.
13184
 *
13185
 * @since 2.14.0
13186
 *
13187
 * @param ctxt  an XML parser context
13188
 * @returns the current options set in the parser context, or -1 if ctxt is NULL.
13189
 */
13190
int
13191
xmlCtxtGetOptions(xmlParserCtxt *ctxt)
13192
0
{
13193
0
    if (ctxt == NULL)
13194
0
        return(-1);
13195
13196
0
    return(ctxt->options);
13197
0
}
13198
13199
/**
13200
 * Applies the options to the parser context. The following options
13201
 * are never cleared and can only be enabled:
13202
 *
13203
 * @deprecated Use #xmlCtxtSetOptions.
13204
 *
13205
 * - XML_PARSE_NOERROR
13206
 * - XML_PARSE_NOWARNING
13207
 * - XML_PARSE_NONET
13208
 * - XML_PARSE_NSCLEAN
13209
 * - XML_PARSE_NOCDATA
13210
 * - XML_PARSE_COMPACT
13211
 * - XML_PARSE_OLD10
13212
 * - XML_PARSE_HUGE
13213
 * - XML_PARSE_OLDSA- X
13214
 * - XML_PARSE_IGNORE_ENC
13215
 * - XML_PARSE_BIG_LINES
13216
 *
13217
 * @param ctxt  an XML parser context
13218
 * @param options  a combination of xmlParserOption
13219
 * @returns 0 in case of success, the set of unknown or unimplemented options
13220
 *         in case of error.
13221
 */
13222
int
13223
xmlCtxtUseOptions(xmlParserCtxt *ctxt, int options)
13224
14.5k
{
13225
14.5k
    int keepMask;
13226
13227
14.5k
#ifdef LIBXML_HTML_ENABLED
13228
14.5k
    if ((ctxt != NULL) && (ctxt->html))
13229
0
        return(htmlCtxtUseOptions(ctxt, options));
13230
14.5k
#endif
13231
13232
    /*
13233
     * For historic reasons, some options can only be enabled.
13234
     */
13235
14.5k
    keepMask = XML_PARSE_NOERROR |
13236
14.5k
               XML_PARSE_NOWARNING |
13237
14.5k
               XML_PARSE_NONET |
13238
14.5k
               XML_PARSE_NSCLEAN |
13239
14.5k
               XML_PARSE_NOCDATA |
13240
14.5k
               XML_PARSE_COMPACT |
13241
14.5k
               XML_PARSE_OLD10 |
13242
14.5k
               XML_PARSE_HUGE |
13243
14.5k
               XML_PARSE_OLDSAX |
13244
14.5k
               XML_PARSE_IGNORE_ENC |
13245
14.5k
               XML_PARSE_BIG_LINES;
13246
13247
14.5k
    return(xmlCtxtSetOptionsInternal(ctxt, options, keepMask));
13248
14.5k
}
13249
13250
/**
13251
 * To protect against exponential entity expansion ("billion laughs"), the
13252
 * size of serialized output is (roughly) limited to the input size
13253
 * multiplied by this factor. The default value is 5.
13254
 *
13255
 * When working with documents making heavy use of entity expansion, it can
13256
 * be necessary to increase the value. For security reasons, this should only
13257
 * be considered when processing trusted input.
13258
 *
13259
 * @param ctxt  an XML parser context
13260
 * @param maxAmpl  maximum amplification factor
13261
 */
13262
void
13263
xmlCtxtSetMaxAmplification(xmlParserCtxt *ctxt, unsigned maxAmpl)
13264
0
{
13265
0
    if (ctxt == NULL)
13266
0
        return;
13267
0
    ctxt->maxAmpl = maxAmpl;
13268
0
}
13269
13270
/**
13271
 * Parse an XML document and return the resulting document tree.
13272
 * Takes ownership of the input object.
13273
 *
13274
 * @since 2.13.0
13275
 *
13276
 * @param ctxt  an XML parser context
13277
 * @param input  parser input
13278
 * @returns the resulting document tree or NULL
13279
 */
13280
xmlDoc *
13281
xmlCtxtParseDocument(xmlParserCtxt *ctxt, xmlParserInput *input)
13282
0
{
13283
0
    xmlDocPtr ret = NULL;
13284
13285
0
    if ((ctxt == NULL) || (input == NULL)) {
13286
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
13287
0
        xmlFreeInputStream(input);
13288
0
        return(NULL);
13289
0
    }
13290
13291
    /* assert(ctxt->inputNr == 0); */
13292
0
    while (ctxt->inputNr > 0)
13293
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
13294
13295
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
13296
0
        xmlFreeInputStream(input);
13297
0
        return(NULL);
13298
0
    }
13299
13300
0
    xmlParseDocument(ctxt);
13301
13302
0
    ret = xmlCtxtGetDocument(ctxt);
13303
13304
    /* assert(ctxt->inputNr == 1); */
13305
0
    while (ctxt->inputNr > 0)
13306
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
13307
13308
0
    return(ret);
13309
0
}
13310
13311
/**
13312
 * Convenience function to parse an XML document from a
13313
 * zero-terminated string.
13314
 *
13315
 * See #xmlCtxtReadDoc for details.
13316
 *
13317
 * @param cur  a pointer to a zero terminated string
13318
 * @param URL  base URL (optional)
13319
 * @param encoding  the document encoding (optional)
13320
 * @param options  a combination of xmlParserOption
13321
 * @returns the resulting document tree
13322
 */
13323
xmlDoc *
13324
xmlReadDoc(const xmlChar *cur, const char *URL, const char *encoding,
13325
           int options)
13326
0
{
13327
0
    xmlParserCtxtPtr ctxt;
13328
0
    xmlParserInputPtr input;
13329
0
    xmlDocPtr doc = NULL;
13330
13331
0
    ctxt = xmlNewParserCtxt();
13332
0
    if (ctxt == NULL)
13333
0
        return(NULL);
13334
13335
0
    xmlCtxtUseOptions(ctxt, options);
13336
13337
0
    input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) cur, encoding,
13338
0
                                      XML_INPUT_BUF_STATIC);
13339
13340
0
    if (input != NULL)
13341
0
        doc = xmlCtxtParseDocument(ctxt, input);
13342
13343
0
    xmlFreeParserCtxt(ctxt);
13344
0
    return(doc);
13345
0
}
13346
13347
/**
13348
 * Convenience function to parse an XML file from the filesystem,
13349
 * the network or a global user-define resource loader.
13350
 *
13351
 * This function always enables the XML_PARSE_UNZIP option for
13352
 * backward compatibility. If a "-" filename is passed, it will
13353
 * read from stdin. Both of these features are potentially
13354
 * insecure and might be removed from later versions.
13355
 *
13356
 * See #xmlCtxtReadFile for details.
13357
 *
13358
 * @param filename  a file or URL
13359
 * @param encoding  the document encoding (optional)
13360
 * @param options  a combination of xmlParserOption
13361
 * @returns the resulting document tree
13362
 */
13363
xmlDoc *
13364
xmlReadFile(const char *filename, const char *encoding, int options)
13365
0
{
13366
0
    xmlParserCtxtPtr ctxt;
13367
0
    xmlParserInputPtr input;
13368
0
    xmlDocPtr doc = NULL;
13369
13370
0
    ctxt = xmlNewParserCtxt();
13371
0
    if (ctxt == NULL)
13372
0
        return(NULL);
13373
13374
0
    options |= XML_PARSE_UNZIP;
13375
13376
0
    xmlCtxtUseOptions(ctxt, options);
13377
13378
    /*
13379
     * Backward compatibility for users of command line utilities like
13380
     * xmlstarlet expecting "-" to mean stdin. This is dangerous and
13381
     * should be removed at some point.
13382
     */
13383
0
    if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0))
13384
0
        input = xmlCtxtNewInputFromFd(ctxt, filename, STDIN_FILENO,
13385
0
                                      encoding, 0);
13386
0
    else
13387
0
        input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0);
13388
13389
0
    if (input != NULL)
13390
0
        doc = xmlCtxtParseDocument(ctxt, input);
13391
13392
0
    xmlFreeParserCtxt(ctxt);
13393
0
    return(doc);
13394
0
}
13395
13396
/**
13397
 * Parse an XML in-memory document and build a tree. The input buffer must
13398
 * not contain a terminating null byte.
13399
 *
13400
 * See #xmlCtxtReadMemory for details.
13401
 *
13402
 * @param buffer  a pointer to a char array
13403
 * @param size  the size of the array
13404
 * @param url  base URL (optional)
13405
 * @param encoding  the document encoding (optional)
13406
 * @param options  a combination of xmlParserOption
13407
 * @returns the resulting document tree
13408
 */
13409
xmlDoc *
13410
xmlReadMemory(const char *buffer, int size, const char *url,
13411
              const char *encoding, int options)
13412
0
{
13413
0
    xmlParserCtxtPtr ctxt;
13414
0
    xmlParserInputPtr input;
13415
0
    xmlDocPtr doc = NULL;
13416
13417
0
    if (size < 0)
13418
0
  return(NULL);
13419
13420
0
    ctxt = xmlNewParserCtxt();
13421
0
    if (ctxt == NULL)
13422
0
        return(NULL);
13423
13424
0
    xmlCtxtUseOptions(ctxt, options);
13425
13426
0
    input = xmlCtxtNewInputFromMemory(ctxt, url, buffer, size, encoding,
13427
0
                                      XML_INPUT_BUF_STATIC);
13428
13429
0
    if (input != NULL)
13430
0
        doc = xmlCtxtParseDocument(ctxt, input);
13431
13432
0
    xmlFreeParserCtxt(ctxt);
13433
0
    return(doc);
13434
0
}
13435
13436
/**
13437
 * Parse an XML from a file descriptor and build a tree.
13438
 *
13439
 * See #xmlCtxtReadFd for details.
13440
 *
13441
 * NOTE that the file descriptor will not be closed when the
13442
 * context is freed or reset.
13443
 *
13444
 * @param fd  an open file descriptor
13445
 * @param URL  base URL (optional)
13446
 * @param encoding  the document encoding (optional)
13447
 * @param options  a combination of xmlParserOption
13448
 * @returns the resulting document tree
13449
 */
13450
xmlDoc *
13451
xmlReadFd(int fd, const char *URL, const char *encoding, int options)
13452
0
{
13453
0
    xmlParserCtxtPtr ctxt;
13454
0
    xmlParserInputPtr input;
13455
0
    xmlDocPtr doc = NULL;
13456
13457
0
    ctxt = xmlNewParserCtxt();
13458
0
    if (ctxt == NULL)
13459
0
        return(NULL);
13460
13461
0
    xmlCtxtUseOptions(ctxt, options);
13462
13463
0
    input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0);
13464
13465
0
    if (input != NULL)
13466
0
        doc = xmlCtxtParseDocument(ctxt, input);
13467
13468
0
    xmlFreeParserCtxt(ctxt);
13469
0
    return(doc);
13470
0
}
13471
13472
/**
13473
 * Parse an XML document from I/O functions and context and build a tree.
13474
 *
13475
 * See #xmlCtxtReadIO for details.
13476
 *
13477
 * @param ioread  an I/O read function
13478
 * @param ioclose  an I/O close function (optional)
13479
 * @param ioctx  an I/O handler
13480
 * @param URL  base URL (optional)
13481
 * @param encoding  the document encoding (optional)
13482
 * @param options  a combination of xmlParserOption
13483
 * @returns the resulting document tree
13484
 */
13485
xmlDoc *
13486
xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
13487
          void *ioctx, const char *URL, const char *encoding, int options)
13488
0
{
13489
0
    xmlParserCtxtPtr ctxt;
13490
0
    xmlParserInputPtr input;
13491
0
    xmlDocPtr doc = NULL;
13492
13493
0
    ctxt = xmlNewParserCtxt();
13494
0
    if (ctxt == NULL)
13495
0
        return(NULL);
13496
13497
0
    xmlCtxtUseOptions(ctxt, options);
13498
13499
0
    input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx,
13500
0
                                  encoding, 0);
13501
13502
0
    if (input != NULL)
13503
0
        doc = xmlCtxtParseDocument(ctxt, input);
13504
13505
0
    xmlFreeParserCtxt(ctxt);
13506
0
    return(doc);
13507
0
}
13508
13509
/**
13510
 * Parse an XML in-memory document and build a tree.
13511
 *
13512
 * `URL` is used as base to resolve external entities and for error
13513
 * reporting.
13514
 *
13515
 * See #xmlCtxtUseOptions for details.
13516
 *
13517
 * @param ctxt  an XML parser context
13518
 * @param str  a pointer to a zero terminated string
13519
 * @param URL  base URL (optional)
13520
 * @param encoding  the document encoding (optional)
13521
 * @param options  a combination of xmlParserOption
13522
 * @returns the resulting document tree
13523
 */
13524
xmlDoc *
13525
xmlCtxtReadDoc(xmlParserCtxt *ctxt, const xmlChar *str,
13526
               const char *URL, const char *encoding, int options)
13527
0
{
13528
0
    xmlParserInputPtr input;
13529
13530
0
    if (ctxt == NULL)
13531
0
        return(NULL);
13532
13533
0
    xmlCtxtReset(ctxt);
13534
0
    xmlCtxtUseOptions(ctxt, options);
13535
13536
0
    input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) str, encoding,
13537
0
                                      XML_INPUT_BUF_STATIC);
13538
0
    if (input == NULL)
13539
0
        return(NULL);
13540
13541
0
    return(xmlCtxtParseDocument(ctxt, input));
13542
0
}
13543
13544
/**
13545
 * Parse an XML file from the filesystem, the network or a user-defined
13546
 * resource loader.
13547
 *
13548
 * This function always enables the XML_PARSE_UNZIP option for
13549
 * backward compatibility. This feature is potentially insecure
13550
 * and might be removed from later versions.
13551
 *
13552
 * @param ctxt  an XML parser context
13553
 * @param filename  a file or URL
13554
 * @param encoding  the document encoding (optional)
13555
 * @param options  a combination of xmlParserOption
13556
 * @returns the resulting document tree
13557
 */
13558
xmlDoc *
13559
xmlCtxtReadFile(xmlParserCtxt *ctxt, const char *filename,
13560
                const char *encoding, int options)
13561
0
{
13562
0
    xmlParserInputPtr input;
13563
13564
0
    if (ctxt == NULL)
13565
0
        return(NULL);
13566
13567
0
    options |= XML_PARSE_UNZIP;
13568
13569
0
    xmlCtxtReset(ctxt);
13570
0
    xmlCtxtUseOptions(ctxt, options);
13571
13572
0
    input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0);
13573
0
    if (input == NULL)
13574
0
        return(NULL);
13575
13576
0
    return(xmlCtxtParseDocument(ctxt, input));
13577
0
}
13578
13579
/**
13580
 * Parse an XML in-memory document and build a tree. The input buffer must
13581
 * not contain a terminating null byte.
13582
 *
13583
 * `URL` is used as base to resolve external entities and for error
13584
 * reporting.
13585
 *
13586
 * See #xmlCtxtUseOptions for details.
13587
 *
13588
 * @param ctxt  an XML parser context
13589
 * @param buffer  a pointer to a char array
13590
 * @param size  the size of the array
13591
 * @param URL  base URL (optional)
13592
 * @param encoding  the document encoding (optional)
13593
 * @param options  a combination of xmlParserOption
13594
 * @returns the resulting document tree
13595
 */
13596
xmlDoc *
13597
xmlCtxtReadMemory(xmlParserCtxt *ctxt, const char *buffer, int size,
13598
                  const char *URL, const char *encoding, int options)
13599
0
{
13600
0
    xmlParserInputPtr input;
13601
13602
0
    if ((ctxt == NULL) || (size < 0))
13603
0
        return(NULL);
13604
13605
0
    xmlCtxtReset(ctxt);
13606
0
    xmlCtxtUseOptions(ctxt, options);
13607
13608
0
    input = xmlCtxtNewInputFromMemory(ctxt, URL, buffer, size, encoding,
13609
0
                                      XML_INPUT_BUF_STATIC);
13610
0
    if (input == NULL)
13611
0
        return(NULL);
13612
13613
0
    return(xmlCtxtParseDocument(ctxt, input));
13614
0
}
13615
13616
/**
13617
 * Parse an XML document from a file descriptor and build a tree.
13618
 *
13619
 * NOTE that the file descriptor will not be closed when the
13620
 * context is freed or reset.
13621
 *
13622
 * `URL` is used as base to resolve external entities and for error
13623
 * reporting.
13624
 *
13625
 * See #xmlCtxtUseOptions for details.
13626
 *
13627
 * @param ctxt  an XML parser context
13628
 * @param fd  an open file descriptor
13629
 * @param URL  base URL (optional)
13630
 * @param encoding  the document encoding (optional)
13631
 * @param options  a combination of xmlParserOption
13632
 * @returns the resulting document tree
13633
 */
13634
xmlDoc *
13635
xmlCtxtReadFd(xmlParserCtxt *ctxt, int fd,
13636
              const char *URL, const char *encoding, int options)
13637
0
{
13638
0
    xmlParserInputPtr input;
13639
13640
0
    if (ctxt == NULL)
13641
0
        return(NULL);
13642
13643
0
    xmlCtxtReset(ctxt);
13644
0
    xmlCtxtUseOptions(ctxt, options);
13645
13646
0
    input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0);
13647
0
    if (input == NULL)
13648
0
        return(NULL);
13649
13650
0
    return(xmlCtxtParseDocument(ctxt, input));
13651
0
}
13652
13653
/**
13654
 * parse an XML document from I/O functions and source and build a tree.
13655
 * This reuses the existing `ctxt` parser context
13656
 *
13657
 * `URL` is used as base to resolve external entities and for error
13658
 * reporting.
13659
 *
13660
 * See #xmlCtxtUseOptions for details.
13661
 *
13662
 * @param ctxt  an XML parser context
13663
 * @param ioread  an I/O read function
13664
 * @param ioclose  an I/O close function
13665
 * @param ioctx  an I/O handler
13666
 * @param URL  the base URL to use for the document
13667
 * @param encoding  the document encoding, or NULL
13668
 * @param options  a combination of xmlParserOption
13669
 * @returns the resulting document tree
13670
 */
13671
xmlDoc *
13672
xmlCtxtReadIO(xmlParserCtxt *ctxt, xmlInputReadCallback ioread,
13673
              xmlInputCloseCallback ioclose, void *ioctx,
13674
        const char *URL,
13675
              const char *encoding, int options)
13676
0
{
13677
0
    xmlParserInputPtr input;
13678
13679
0
    if (ctxt == NULL)
13680
0
        return(NULL);
13681
13682
0
    xmlCtxtReset(ctxt);
13683
0
    xmlCtxtUseOptions(ctxt, options);
13684
13685
0
    input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx,
13686
0
                                  encoding, 0);
13687
0
    if (input == NULL)
13688
0
        return(NULL);
13689
13690
0
    return(xmlCtxtParseDocument(ctxt, input));
13691
0
}
13692