Coverage Report

Created: 2026-09-01 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/parser.c
Line
Count
Source
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
4.67M
#define NS_INDEX_EMPTY  INT_MAX
81
493k
#define NS_INDEX_XML    (INT_MAX - 1)
82
1.90M
#define URI_HASH_EMPTY  0xD943A04E
83
209k
#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
382k
#define XML_MAX_ATTRS 100000000 /* 100 million */
94
95
2.84M
#define XML_SPECIAL_EXTERNAL    (1 << 20)
96
2.84M
#define XML_SPECIAL_TYPE_MASK   (XML_SPECIAL_EXTERNAL - 1)
97
98
2.93M
#define XML_ATTVAL_ALLOC        (1 << 0)
99
630k
#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
2.90M
#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
2.90M
#define XML_ENT_FIXED_COST 20
170
171
52.2M
#define XML_PARSER_BIG_BUFFER_SIZE 300
172
340k
#define XML_PARSER_BUFFER_SIZE 100
173
103k
#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 version of the library used at
188
 * run-time.
189
 */
190
const char *const
191
xmlParserVersion = LIBXML_VERSION_STRING LIBXML_VERSION_EXTRA;
192
193
/*
194
 * List of XML prefixed PI allowed by W3C specs
195
 */
196
197
static const char* const xmlW3CPIs[] = {
198
    "xml-stylesheet",
199
    "xml-model",
200
    NULL
201
};
202
203
204
/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
205
static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
206
                                              const xmlChar **str);
207
208
static void
209
xmlCtxtParseEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr ent);
210
211
static int
212
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity);
213
214
static void
215
xmlParsePERefInternal(xmlParserCtxt *ctxt, int markupDecl);
216
217
/************************************************************************
218
 *                  *
219
 *    Some factorized error routines        *
220
 *                  *
221
 ************************************************************************/
222
223
static void
224
0
xmlErrMemory(xmlParserCtxtPtr ctxt) {
225
0
    xmlCtxtErrMemory(ctxt);
226
0
}
227
228
/**
229
 * Handle a redefinition of attribute error
230
 *
231
 * @param ctxt  an XML parser context
232
 * @param prefix  the attribute prefix
233
 * @param localname  the attribute localname
234
 */
235
static void
236
xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
237
                   const xmlChar * localname)
238
315k
{
239
315k
    if (prefix == NULL)
240
301k
        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
241
301k
                   XML_ERR_FATAL, localname, NULL, NULL, 0,
242
301k
                   "Attribute %s redefined\n", localname);
243
14.3k
    else
244
14.3k
        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
245
14.3k
                   XML_ERR_FATAL, prefix, localname, NULL, 0,
246
14.3k
                   "Attribute %s:%s redefined\n", prefix, localname);
247
315k
}
248
249
/**
250
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
251
 *
252
 * @param ctxt  an XML parser context
253
 * @param error  the error number
254
 * @param msg  the error message
255
 */
256
static void LIBXML_ATTR_FORMAT(3,0)
257
xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
258
               const char *msg)
259
11.1M
{
260
11.1M
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
261
11.1M
               NULL, NULL, NULL, 0, "%s", msg);
262
11.1M
}
263
264
/**
265
 * Handle a warning.
266
 *
267
 * @param ctxt  an XML parser context
268
 * @param error  the error number
269
 * @param msg  the error message
270
 * @param str1  extra data
271
 * @param str2  extra data
272
 */
273
void LIBXML_ATTR_FORMAT(3,0)
274
xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
275
              const char *msg, const xmlChar *str1, const xmlChar *str2)
276
5.97k
{
277
5.97k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
278
5.97k
               str1, str2, NULL, 0, msg, str1, str2);
279
5.97k
}
280
281
#ifdef LIBXML_VALID_ENABLED
282
/**
283
 * Handle a validity error.
284
 *
285
 * @param ctxt  an XML parser context
286
 * @param error  the error number
287
 * @param msg  the error message
288
 * @param str1  extra data
289
 * @param str2  extra data
290
 */
291
static void LIBXML_ATTR_FORMAT(3,0)
292
xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
293
              const char *msg, const xmlChar *str1, const xmlChar *str2)
294
0
{
295
0
    ctxt->valid = 0;
296
297
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR,
298
0
               str1, str2, NULL, 0, msg, str1, str2);
299
0
}
300
#endif
301
302
/**
303
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
304
 *
305
 * @param ctxt  an XML parser context
306
 * @param error  the error number
307
 * @param msg  the error message
308
 * @param val  an integer value
309
 */
310
static void LIBXML_ATTR_FORMAT(3,0)
311
xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
312
                  const char *msg, int val)
313
2.64M
{
314
2.64M
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
315
2.64M
               NULL, NULL, NULL, val, msg, val);
316
2.64M
}
317
318
/**
319
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
320
 *
321
 * @param ctxt  an XML parser context
322
 * @param error  the error number
323
 * @param msg  the error message
324
 * @param str1  an string info
325
 * @param val  an integer value
326
 * @param str2  an string info
327
 */
328
static void LIBXML_ATTR_FORMAT(3,0)
329
xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
330
                  const char *msg, const xmlChar *str1, int val,
331
      const xmlChar *str2)
332
241k
{
333
241k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
334
241k
               str1, str2, NULL, val, msg, str1, val, str2);
335
241k
}
336
337
/**
338
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
339
 *
340
 * @param ctxt  an XML parser context
341
 * @param error  the error number
342
 * @param msg  the error message
343
 * @param val  a string value
344
 */
345
static void LIBXML_ATTR_FORMAT(3,0)
346
xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
347
                  const char *msg, const xmlChar * val)
348
445k
{
349
445k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
350
445k
               val, NULL, NULL, 0, msg, val);
351
445k
}
352
353
/**
354
 * Handle a non fatal parser error
355
 *
356
 * @param ctxt  an XML parser context
357
 * @param error  the error number
358
 * @param msg  the error message
359
 * @param val  a string value
360
 */
361
static void LIBXML_ATTR_FORMAT(3,0)
362
xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
363
                  const char *msg, const xmlChar * val)
364
16.7k
{
365
16.7k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_ERROR,
366
16.7k
               val, NULL, NULL, 0, msg, val);
367
16.7k
}
368
369
/**
370
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
371
 *
372
 * @param ctxt  an XML parser context
373
 * @param error  the error number
374
 * @param msg  the message
375
 * @param info1  extra information string
376
 * @param info2  extra information string
377
 * @param info3  extra information string
378
 */
379
static void LIBXML_ATTR_FORMAT(3,0)
380
xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
381
         const char *msg,
382
         const xmlChar * info1, const xmlChar * info2,
383
         const xmlChar * info3)
384
846k
{
385
846k
    ctxt->nsWellFormed = 0;
386
387
846k
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
388
846k
               info1, info2, info3, 0, msg, info1, info2, info3);
389
846k
}
390
391
/**
392
 * Handle a namespace warning error
393
 *
394
 * @param ctxt  an XML parser context
395
 * @param error  the error number
396
 * @param msg  the message
397
 * @param info1  extra information string
398
 * @param info2  extra information string
399
 * @param info3  extra information string
400
 */
401
static void LIBXML_ATTR_FORMAT(3,0)
402
xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error,
403
         const char *msg,
404
         const xmlChar * info1, const xmlChar * info2,
405
         const xmlChar * info3)
406
22.2k
{
407
22.2k
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
408
22.2k
               info1, info2, info3, 0, msg, info1, info2, info3);
409
22.2k
}
410
411
/**
412
 * Check for non-linear entity expansion behaviour.
413
 *
414
 * In some cases like xmlExpandEntityInAttValue, this function is called
415
 * for each, possibly nested entity and its unexpanded content length.
416
 *
417
 * In other cases like #xmlParseReference, it's only called for each
418
 * top-level entity with its unexpanded content length plus the sum of
419
 * the unexpanded content lengths (plus fixed cost) of all nested
420
 * entities.
421
 *
422
 * Summing the unexpanded lengths also adds the length of the reference.
423
 * This is by design. Taking the length of the entity name into account
424
 * discourages attacks that try to waste CPU time with abusively long
425
 * entity names. See test/recurse/lol6.xml for example. Each call also
426
 * adds some fixed cost XML_ENT_FIXED_COST to discourage attacks with
427
 * short entities.
428
 *
429
 * @param ctxt  parser context
430
 * @param extra  sum of unexpanded entity sizes
431
 * @returns 1 on error, 0 on success.
432
 */
433
static int
434
xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long extra)
435
2.92M
{
436
2.92M
    unsigned long consumed;
437
2.92M
    unsigned long *expandedSize;
438
2.92M
    xmlParserInputPtr input = ctxt->input;
439
2.92M
    xmlEntityPtr entity = input->entity;
440
441
2.92M
    if ((entity) && (entity->flags & XML_ENT_CHECKED))
442
19.3k
        return(0);
443
444
    /*
445
     * Compute total consumed bytes so far, including input streams of
446
     * external entities.
447
     */
448
2.90M
    consumed = input->consumed;
449
2.90M
    xmlSaturatedAddSizeT(&consumed, input->cur - input->base);
450
2.90M
    xmlSaturatedAdd(&consumed, ctxt->sizeentities);
451
452
2.90M
    if (entity)
453
17.1k
        expandedSize = &entity->expandedSize;
454
2.88M
    else
455
2.88M
        expandedSize = &ctxt->sizeentcopy;
456
457
    /*
458
     * Add extra cost and some fixed cost.
459
     */
460
2.90M
    xmlSaturatedAdd(expandedSize, extra);
461
2.90M
    xmlSaturatedAdd(expandedSize, XML_ENT_FIXED_COST);
462
463
    /*
464
     * It's important to always use saturation arithmetic when tracking
465
     * entity sizes to make the size checks reliable. If "sizeentcopy"
466
     * overflows, we have to abort.
467
     */
468
2.90M
    if ((ctxt->maxAmpl > 0) &&
469
2.90M
        (*expandedSize > XML_PARSER_ALLOWED_EXPANSION) &&
470
434k
        ((*expandedSize >= ULONG_MAX) ||
471
434k
         (*expandedSize / ctxt->maxAmpl > consumed))) {
472
785
        xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
473
785
                       "Maximum entity amplification factor exceeded, see "
474
785
                       "xmlCtxtSetMaxAmplification.\n");
475
785
        return(1);
476
785
    }
477
478
2.90M
    return(0);
479
2.90M
}
480
481
/************************************************************************
482
 *                  *
483
 *    Library wide options          *
484
 *                  *
485
 ************************************************************************/
486
487
/**
488
 * Examines if the library has been compiled with a given feature.
489
 *
490
 * @param feature  the feature to be examined
491
 * @returns zero (0) if the feature does not exist or an unknown
492
 * feature is requested, non-zero otherwise.
493
 */
494
int
495
xmlHasFeature(xmlFeature feature)
496
0
{
497
0
    switch (feature) {
498
0
  case XML_WITH_THREAD:
499
0
#ifdef LIBXML_THREAD_ENABLED
500
0
      return(1);
501
#else
502
      return(0);
503
#endif
504
0
        case XML_WITH_TREE:
505
0
            return(1);
506
0
        case XML_WITH_OUTPUT:
507
0
#ifdef LIBXML_OUTPUT_ENABLED
508
0
            return(1);
509
#else
510
            return(0);
511
#endif
512
0
        case XML_WITH_PUSH:
513
0
#ifdef LIBXML_PUSH_ENABLED
514
0
            return(1);
515
#else
516
            return(0);
517
#endif
518
0
        case XML_WITH_READER:
519
0
#ifdef LIBXML_READER_ENABLED
520
0
            return(1);
521
#else
522
            return(0);
523
#endif
524
0
        case XML_WITH_PATTERN:
525
0
#ifdef LIBXML_PATTERN_ENABLED
526
0
            return(1);
527
#else
528
            return(0);
529
#endif
530
0
        case XML_WITH_WRITER:
531
0
#ifdef LIBXML_WRITER_ENABLED
532
0
            return(1);
533
#else
534
            return(0);
535
#endif
536
0
        case XML_WITH_SAX1:
537
0
#ifdef LIBXML_SAX1_ENABLED
538
0
            return(1);
539
#else
540
            return(0);
541
#endif
542
0
        case XML_WITH_HTTP:
543
0
            return(0);
544
0
        case XML_WITH_VALID:
545
0
#ifdef LIBXML_VALID_ENABLED
546
0
            return(1);
547
#else
548
            return(0);
549
#endif
550
0
        case XML_WITH_HTML:
551
0
#ifdef LIBXML_HTML_ENABLED
552
0
            return(1);
553
#else
554
            return(0);
555
#endif
556
0
        case XML_WITH_LEGACY:
557
0
            return(0);
558
0
        case XML_WITH_C14N:
559
0
#ifdef LIBXML_C14N_ENABLED
560
0
            return(1);
561
#else
562
            return(0);
563
#endif
564
0
        case XML_WITH_CATALOG:
565
0
#ifdef LIBXML_CATALOG_ENABLED
566
0
            return(1);
567
#else
568
            return(0);
569
#endif
570
0
        case XML_WITH_XPATH:
571
0
#ifdef LIBXML_XPATH_ENABLED
572
0
            return(1);
573
#else
574
            return(0);
575
#endif
576
0
        case XML_WITH_XPTR:
577
0
#ifdef LIBXML_XPTR_ENABLED
578
0
            return(1);
579
#else
580
            return(0);
581
#endif
582
0
        case XML_WITH_XINCLUDE:
583
0
#ifdef LIBXML_XINCLUDE_ENABLED
584
0
            return(1);
585
#else
586
            return(0);
587
#endif
588
0
        case XML_WITH_ICONV:
589
0
#ifdef LIBXML_ICONV_ENABLED
590
0
            return(1);
591
#else
592
            return(0);
593
#endif
594
0
        case XML_WITH_ISO8859X:
595
0
#ifdef LIBXML_ISO8859X_ENABLED
596
0
            return(1);
597
#else
598
            return(0);
599
#endif
600
0
        case XML_WITH_UNICODE:
601
0
            return(0);
602
0
        case XML_WITH_REGEXP:
603
0
#ifdef LIBXML_REGEXP_ENABLED
604
0
            return(1);
605
#else
606
            return(0);
607
#endif
608
0
        case XML_WITH_AUTOMATA:
609
0
#ifdef LIBXML_REGEXP_ENABLED
610
0
            return(1);
611
#else
612
            return(0);
613
#endif
614
0
        case XML_WITH_EXPR:
615
0
            return(0);
616
0
        case XML_WITH_RELAXNG:
617
0
#ifdef LIBXML_RELAXNG_ENABLED
618
0
            return(1);
619
#else
620
            return(0);
621
#endif
622
0
        case XML_WITH_SCHEMAS:
623
0
#ifdef LIBXML_SCHEMAS_ENABLED
624
0
            return(1);
625
#else
626
            return(0);
627
#endif
628
0
        case XML_WITH_SCHEMATRON:
629
#ifdef LIBXML_SCHEMATRON_ENABLED
630
            return(1);
631
#else
632
0
            return(0);
633
0
#endif
634
0
        case XML_WITH_MODULES:
635
0
#ifdef LIBXML_MODULES_ENABLED
636
0
            return(1);
637
#else
638
            return(0);
639
#endif
640
0
        case XML_WITH_DEBUG:
641
0
#ifdef LIBXML_DEBUG_ENABLED
642
0
            return(1);
643
#else
644
            return(0);
645
#endif
646
0
        case XML_WITH_DEBUG_MEM:
647
0
            return(0);
648
0
        case XML_WITH_ZLIB:
649
#ifdef LIBXML_ZLIB_ENABLED
650
            return(1);
651
#else
652
0
            return(0);
653
0
#endif
654
0
        case XML_WITH_LZMA:
655
0
            return(0);
656
0
        case XML_WITH_ICU:
657
#ifdef LIBXML_ICU_ENABLED
658
            return(1);
659
#else
660
0
            return(0);
661
0
#endif
662
0
        default:
663
0
      break;
664
0
     }
665
0
     return(0);
666
0
}
667
668
/************************************************************************
669
 *                  *
670
 *      Simple string buffer        *
671
 *                  *
672
 ************************************************************************/
673
674
typedef struct {
675
    xmlChar *mem;
676
    unsigned size;
677
    unsigned cap; /* size < cap */
678
    unsigned max; /* size <= max */
679
    xmlParserErrors code;
680
} xmlSBuf;
681
682
static void
683
2.88M
xmlSBufInit(xmlSBuf *buf, unsigned max) {
684
2.88M
    buf->mem = NULL;
685
2.88M
    buf->size = 0;
686
2.88M
    buf->cap = 0;
687
2.88M
    buf->max = max;
688
2.88M
    buf->code = XML_ERR_OK;
689
2.88M
}
690
691
static int
692
214k
xmlSBufGrow(xmlSBuf *buf, unsigned len) {
693
214k
    xmlChar *mem;
694
214k
    unsigned cap;
695
696
214k
    if (len >= UINT_MAX / 2 - buf->size) {
697
0
        if (buf->code == XML_ERR_OK)
698
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
699
0
        return(-1);
700
0
    }
701
702
214k
    cap = (buf->size + len) * 2;
703
214k
    if (cap < 240)
704
153k
        cap = 240;
705
706
214k
    mem = xmlRealloc(buf->mem, cap);
707
214k
    if (mem == NULL) {
708
0
        buf->code = XML_ERR_NO_MEMORY;
709
0
        return(-1);
710
0
    }
711
712
214k
    buf->mem = mem;
713
214k
    buf->cap = cap;
714
715
214k
    return(0);
716
214k
}
717
718
static void
719
76.8M
xmlSBufAddString(xmlSBuf *buf, const xmlChar *str, unsigned len) {
720
76.8M
    if (buf->max - buf->size < len) {
721
108
        if (buf->code == XML_ERR_OK)
722
30
            buf->code = XML_ERR_RESOURCE_LIMIT;
723
108
        return;
724
108
    }
725
726
76.8M
    if (buf->cap - buf->size <= len) {
727
206k
        if (xmlSBufGrow(buf, len) < 0)
728
0
            return;
729
206k
    }
730
731
76.8M
    if (len > 0)
732
76.8M
        memcpy(buf->mem + buf->size, str, len);
733
76.8M
    buf->size += len;
734
76.8M
}
735
736
static void
737
72.7M
xmlSBufAddCString(xmlSBuf *buf, const char *str, unsigned len) {
738
72.7M
    xmlSBufAddString(buf, (const xmlChar *) str, len);
739
72.7M
}
740
741
static void
742
233k
xmlSBufAddChar(xmlSBuf *buf, int c) {
743
233k
    xmlChar *end;
744
745
233k
    if (buf->max - buf->size < 4) {
746
0
        if (buf->code == XML_ERR_OK)
747
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
748
0
        return;
749
0
    }
750
751
233k
    if (buf->cap - buf->size <= 4) {
752
7.82k
        if (xmlSBufGrow(buf, 4) < 0)
753
0
            return;
754
7.82k
    }
755
756
233k
    end = buf->mem + buf->size;
757
758
233k
    if (c < 0x80) {
759
125k
        *end = (xmlChar) c;
760
125k
        buf->size += 1;
761
125k
    } else {
762
107k
        buf->size += xmlCopyCharMultiByte(end, c);
763
107k
    }
764
233k
}
765
766
static void
767
55.8M
xmlSBufAddReplChar(xmlSBuf *buf) {
768
55.8M
    xmlSBufAddCString(buf, "\xEF\xBF\xBD", 3);
769
55.8M
}
770
771
static void
772
30
xmlSBufReportError(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
773
30
    if (buf->code == XML_ERR_NO_MEMORY)
774
0
        xmlCtxtErrMemory(ctxt);
775
30
    else
776
30
        xmlFatalErr(ctxt, buf->code, errMsg);
777
30
}
778
779
static xmlChar *
780
xmlSBufFinish(xmlSBuf *buf, int *sizeOut, xmlParserCtxtPtr ctxt,
781
177k
              const char *errMsg) {
782
177k
    if (buf->mem == NULL) {
783
26.1k
        buf->mem = xmlMalloc(1);
784
26.1k
        if (buf->mem == NULL) {
785
0
            buf->code = XML_ERR_NO_MEMORY;
786
26.1k
        } else {
787
26.1k
            buf->mem[0] = 0;
788
26.1k
        }
789
151k
    } else {
790
151k
        buf->mem[buf->size] = 0;
791
151k
    }
792
793
177k
    if (buf->code == XML_ERR_OK) {
794
177k
        if (sizeOut != NULL)
795
104k
            *sizeOut = buf->size;
796
177k
        return(buf->mem);
797
177k
    }
798
799
1
    xmlSBufReportError(buf, ctxt, errMsg);
800
801
1
    xmlFree(buf->mem);
802
803
1
    if (sizeOut != NULL)
804
1
        *sizeOut = 0;
805
1
    return(NULL);
806
177k
}
807
808
static void
809
2.70M
xmlSBufCleanup(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
810
2.70M
    if (buf->code != XML_ERR_OK)
811
29
        xmlSBufReportError(buf, ctxt, errMsg);
812
813
2.70M
    xmlFree(buf->mem);
814
2.70M
}
815
816
static int
817
xmlUTF8MultibyteLen(xmlParserCtxtPtr ctxt, const xmlChar *str,
818
251M
                    const char *errMsg) {
819
251M
    int c = str[0];
820
251M
    int c1 = str[1];
821
822
251M
    if ((c1 & 0xC0) != 0x80)
823
28.9M
        goto encoding_error;
824
825
222M
    if (c < 0xE0) {
826
        /* 2-byte sequence */
827
197M
        if (c < 0xC2)
828
18.7M
            goto encoding_error;
829
830
178M
        return(2);
831
197M
    } else {
832
25.2M
        int c2 = str[2];
833
834
25.2M
        if ((c2 & 0xC0) != 0x80)
835
13.3k
            goto encoding_error;
836
837
25.2M
        if (c < 0xF0) {
838
            /* 3-byte sequence */
839
25.1M
            if (c == 0xE0) {
840
                /* overlong */
841
14.5M
                if (c1 < 0xA0)
842
5.36k
                    goto encoding_error;
843
14.5M
            } else if (c == 0xED) {
844
                /* surrogate */
845
1.20k
                if (c1 >= 0xA0)
846
532
                    goto encoding_error;
847
10.5M
            } else if (c == 0xEF) {
848
                /* U+FFFE and U+FFFF are invalid Chars */
849
7.71M
                if ((c1 == 0xBF) && (c2 >= 0xBE))
850
521
                    xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, errMsg);
851
7.71M
            }
852
853
25.1M
            return(3);
854
25.1M
        } else {
855
            /* 4-byte sequence */
856
105k
            if ((str[3] & 0xC0) != 0x80)
857
1.35k
                goto encoding_error;
858
103k
            if (c == 0xF0) {
859
                /* overlong */
860
2.16k
                if (c1 < 0x90)
861
1.26k
                    goto encoding_error;
862
101k
            } else if (c >= 0xF4) {
863
                /* greater than 0x10FFFF */
864
89.5k
                if ((c > 0xF4) || (c1 >= 0x90))
865
15.5k
                    goto encoding_error;
866
89.5k
            }
867
868
87.0k
            return(4);
869
103k
        }
870
25.2M
    }
871
872
47.7M
encoding_error:
873
    /* Only report the first error */
874
47.7M
    if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
875
3.96k
        xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
876
3.96k
        ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
877
3.96k
    }
878
879
47.7M
    return(0);
880
222M
}
881
882
/************************************************************************
883
 *                  *
884
 *    SAX2 defaulted attributes handling      *
885
 *                  *
886
 ************************************************************************/
887
888
/**
889
 * Final initialization of the parser context before starting to parse.
890
 *
891
 * This accounts for users modifying struct members of parser context
892
 * directly.
893
 *
894
 * @param ctxt  an XML parser context
895
 */
896
static void
897
58.3k
xmlCtxtInitializeLate(xmlParserCtxtPtr ctxt) {
898
58.3k
    xmlSAXHandlerPtr sax;
899
900
    /* Avoid unused variable warning if features are disabled. */
901
58.3k
    (void) sax;
902
903
    /*
904
     * Changing the SAX struct directly is still widespread practice
905
     * in internal and external code.
906
     */
907
58.3k
    if (ctxt == NULL) return;
908
58.3k
    sax = ctxt->sax;
909
58.3k
#ifdef LIBXML_SAX1_ENABLED
910
    /*
911
     * Only enable SAX2 if there SAX2 element handlers, except when there
912
     * are no element handlers at all.
913
     */
914
58.3k
    if (((ctxt->options & XML_PARSE_SAX1) == 0) &&
915
58.3k
        (sax) &&
916
58.3k
        (sax->initialized == XML_SAX2_MAGIC) &&
917
58.3k
        ((sax->startElementNs != NULL) ||
918
0
         (sax->endElementNs != NULL) ||
919
0
         ((sax->startElement == NULL) && (sax->endElement == NULL))))
920
58.3k
        ctxt->sax2 = 1;
921
#else
922
    ctxt->sax2 = 1;
923
#endif /* LIBXML_SAX1_ENABLED */
924
925
    /*
926
     * Some users replace the dictionary directly in the context struct.
927
     * We really need an API function to do that cleanly.
928
     */
929
58.3k
    ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
930
58.3k
    ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
931
58.3k
    ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
932
58.3k
    if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
933
58.3k
    (ctxt->str_xml_ns == NULL)) {
934
0
        xmlErrMemory(ctxt);
935
0
    }
936
937
58.3k
    xmlDictSetLimit(ctxt->dict,
938
58.3k
                    (ctxt->options & XML_PARSE_HUGE) ?
939
34.6k
                        0 :
940
58.3k
                        XML_MAX_DICTIONARY_LIMIT);
941
942
58.3k
#ifdef LIBXML_VALID_ENABLED
943
58.3k
    if (ctxt->validate)
944
0
        ctxt->vctxt.flags |= XML_VCTXT_VALIDATE;
945
58.3k
    else
946
58.3k
        ctxt->vctxt.flags &= ~XML_VCTXT_VALIDATE;
947
58.3k
#endif /* LIBXML_VALID_ENABLED */
948
58.3k
}
949
950
typedef struct {
951
    xmlHashedString prefix;
952
    xmlHashedString name;
953
    xmlHashedString value;
954
    const xmlChar *valueEnd;
955
    int external;
956
    int expandedSize;
957
} xmlDefAttr;
958
959
typedef struct _xmlDefAttrs xmlDefAttrs;
960
typedef xmlDefAttrs *xmlDefAttrsPtr;
961
struct _xmlDefAttrs {
962
    int nbAttrs;  /* number of defaulted attributes on that element */
963
    int maxAttrs;       /* the size of the array */
964
#if __STDC_VERSION__ >= 199901L
965
    /* Using a C99 flexible array member avoids UBSan errors. */
966
    xmlDefAttr attrs[] ATTRIBUTE_COUNTED_BY(maxAttrs);
967
#else
968
    xmlDefAttr attrs[1];
969
#endif
970
};
971
972
/**
973
 * Normalize the space in non CDATA attribute values:
974
 * If the attribute type is not CDATA, then the XML processor MUST further
975
 * process the normalized attribute value by discarding any leading and
976
 * trailing space (\#x20) characters, and by replacing sequences of space
977
 * (\#x20) characters by a single space (\#x20) character.
978
 * Note that the size of dst need to be at least src, and if one doesn't need
979
 * to preserve dst (and it doesn't come from a dictionary or read-only) then
980
 * passing src as dst is just fine.
981
 *
982
 * @param src  the source string
983
 * @param dst  the target string
984
 * @returns a pointer to the normalized value (dst) or NULL if no conversion
985
 *         is needed.
986
 */
987
xmlChar *
988
xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst)
989
29.3k
{
990
29.3k
    if ((src == NULL) || (dst == NULL))
991
0
        return(NULL);
992
993
39.7k
    while (*src == 0x20) src++;
994
9.91M
    while (*src != 0) {
995
9.88M
  if (*src == 0x20) {
996
299k
      while (*src == 0x20) src++;
997
81.5k
      if (*src != 0)
998
79.3k
    *dst++ = 0x20;
999
9.80M
  } else {
1000
9.80M
      *dst++ = *src++;
1001
9.80M
  }
1002
9.88M
    }
1003
29.3k
    *dst = 0;
1004
29.3k
    if (dst == src)
1005
25.7k
       return(NULL);
1006
3.62k
    return(dst);
1007
29.3k
}
1008
1009
/**
1010
 * TODO: This function should also remove leading and trailing
1011
 *       whitespaces, and also group whitespaces together, but right
1012
 *       now the parse doesn't do that with XML_PARSE_NOENT, so this
1013
 *       replacement is consistent with the parser normalization
1014
 *
1015
 *       Maybe merge with xmlAttrNormalizeSpace, or do something
1016
 *       similar
1017
 *
1018
 * Normalize attritube entity values
1019
 * Replaces any space character with 0x20
1020
 * https://www.w3.org/TR/REC-xml/#AVNormalize
1021
 */
1022
xmlChar *
1023
xmlAttrNormalize(xmlChar *src)
1024
0
{
1025
0
    xmlChar *out = NULL;
1026
0
    xmlChar *dst = NULL;
1027
1028
0
    if (src == NULL)
1029
0
        return(NULL);
1030
1031
0
    out = src;
1032
0
    dst = out;
1033
0
    while (*src != 0) {
1034
0
        if (*src < 0x20) {
1035
0
            src++;
1036
0
            *dst++ = 0x20;
1037
0
        } else {
1038
0
            *dst++ = *src++;
1039
0
        }
1040
0
    }
1041
0
    *dst = 0;
1042
0
    return(out);
1043
0
}
1044
1045
/**
1046
 * Add a defaulted attribute for an element
1047
 *
1048
 * @param ctxt  an XML parser context
1049
 * @param fullname  the element fullname
1050
 * @param fullattr  the attribute fullname
1051
 * @param value  the attribute value
1052
 */
1053
static void
1054
xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
1055
               const xmlChar *fullname,
1056
               const xmlChar *fullattr,
1057
33.4k
               const xmlChar *value) {
1058
33.4k
    xmlDefAttrsPtr defaults;
1059
33.4k
    xmlDefAttr *attr;
1060
33.4k
    int len, expandedSize;
1061
33.4k
    xmlHashedString name;
1062
33.4k
    xmlHashedString prefix;
1063
33.4k
    xmlHashedString hvalue;
1064
33.4k
    const xmlChar *localname;
1065
1066
    /*
1067
     * Allows to detect attribute redefinitions
1068
     */
1069
33.4k
    if (ctxt->attsSpecial != NULL) {
1070
30.3k
        if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL)
1071
12.4k
      return;
1072
30.3k
    }
1073
1074
21.0k
    if (ctxt->attsDefault == NULL) {
1075
3.05k
        ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict);
1076
3.05k
  if (ctxt->attsDefault == NULL)
1077
0
      goto mem_error;
1078
3.05k
    }
1079
1080
    /*
1081
     * split the element name into prefix:localname , the string found
1082
     * are within the DTD and then not associated to namespace names.
1083
     */
1084
21.0k
    localname = xmlSplitQName3(fullname, &len);
1085
21.0k
    if (localname == NULL) {
1086
18.2k
        name = xmlDictLookupHashed(ctxt->dict, fullname, -1);
1087
18.2k
  prefix.name = NULL;
1088
18.2k
    } else {
1089
2.80k
        name = xmlDictLookupHashed(ctxt->dict, localname, -1);
1090
2.80k
  prefix = xmlDictLookupHashed(ctxt->dict, fullname, len);
1091
2.80k
        if (prefix.name == NULL)
1092
0
            goto mem_error;
1093
2.80k
    }
1094
21.0k
    if (name.name == NULL)
1095
0
        goto mem_error;
1096
1097
    /*
1098
     * make sure there is some storage
1099
     */
1100
21.0k
    defaults = xmlHashLookup2(ctxt->attsDefault, name.name, prefix.name);
1101
21.0k
    if ((defaults == NULL) ||
1102
16.8k
        (defaults->nbAttrs >= defaults->maxAttrs)) {
1103
6.70k
        xmlDefAttrsPtr temp;
1104
6.70k
        int newSize;
1105
1106
6.70k
        if (defaults == NULL) {
1107
4.23k
            newSize = 4;
1108
4.23k
        } else {
1109
2.46k
            if ((defaults->maxAttrs >= XML_MAX_ATTRS) ||
1110
2.46k
                ((size_t) defaults->maxAttrs >
1111
2.46k
                     SIZE_MAX / 2 / sizeof(temp[0]) - sizeof(*defaults)))
1112
0
                goto mem_error;
1113
1114
2.46k
            if (defaults->maxAttrs > XML_MAX_ATTRS / 2)
1115
0
                newSize = XML_MAX_ATTRS;
1116
2.46k
            else
1117
2.46k
                newSize = defaults->maxAttrs * 2;
1118
2.46k
        }
1119
6.70k
        temp = xmlRealloc(defaults,
1120
6.70k
                          sizeof(*defaults) + newSize * sizeof(xmlDefAttr));
1121
6.70k
  if (temp == NULL)
1122
0
      goto mem_error;
1123
6.70k
        if (defaults == NULL)
1124
4.23k
            temp->nbAttrs = 0;
1125
6.70k
  temp->maxAttrs = newSize;
1126
6.70k
        defaults = temp;
1127
6.70k
  if (xmlHashUpdateEntry2(ctxt->attsDefault, name.name, prefix.name,
1128
6.70k
                          defaults, NULL) < 0) {
1129
0
      xmlFree(defaults);
1130
0
      goto mem_error;
1131
0
  }
1132
6.70k
    }
1133
1134
    /*
1135
     * Split the attribute name into prefix:localname , the string found
1136
     * are within the DTD and hen not associated to namespace names.
1137
     */
1138
21.0k
    localname = xmlSplitQName3(fullattr, &len);
1139
21.0k
    if (localname == NULL) {
1140
13.0k
        name = xmlDictLookupHashed(ctxt->dict, fullattr, -1);
1141
13.0k
  prefix.name = NULL;
1142
13.0k
    } else {
1143
7.93k
        name = xmlDictLookupHashed(ctxt->dict, localname, -1);
1144
7.93k
  prefix = xmlDictLookupHashed(ctxt->dict, fullattr, len);
1145
7.93k
        if (prefix.name == NULL)
1146
0
            goto mem_error;
1147
7.93k
    }
1148
21.0k
    if (name.name == NULL)
1149
0
        goto mem_error;
1150
1151
    /* intern the string and precompute the end */
1152
21.0k
    len = strlen((const char *) value);
1153
21.0k
    hvalue = xmlDictLookupHashed(ctxt->dict, value, len);
1154
21.0k
    if (hvalue.name == NULL)
1155
0
        goto mem_error;
1156
1157
21.0k
    expandedSize = strlen((const char *) name.name);
1158
21.0k
    if (prefix.name != NULL)
1159
7.93k
        expandedSize += strlen((const char *) prefix.name);
1160
21.0k
    expandedSize += len;
1161
1162
21.0k
    attr = &defaults->attrs[defaults->nbAttrs++];
1163
21.0k
    attr->name = name;
1164
21.0k
    attr->prefix = prefix;
1165
21.0k
    attr->value = hvalue;
1166
21.0k
    attr->valueEnd = hvalue.name + len;
1167
21.0k
    attr->external = PARSER_EXTERNAL(ctxt);
1168
21.0k
    attr->expandedSize = expandedSize;
1169
1170
21.0k
    return;
1171
1172
0
mem_error:
1173
0
    xmlErrMemory(ctxt);
1174
0
}
1175
1176
/**
1177
 * Register this attribute type
1178
 *
1179
 * @param ctxt  an XML parser context
1180
 * @param fullname  the element fullname
1181
 * @param fullattr  the attribute fullname
1182
 * @param type  the attribute type
1183
 */
1184
static void
1185
xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
1186
      const xmlChar *fullname,
1187
      const xmlChar *fullattr,
1188
      int type)
1189
40.1k
{
1190
40.1k
    if (ctxt->attsSpecial == NULL) {
1191
5.79k
        ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict);
1192
5.79k
  if (ctxt->attsSpecial == NULL)
1193
0
      goto mem_error;
1194
5.79k
    }
1195
1196
40.1k
    if (PARSER_EXTERNAL(ctxt))
1197
0
        type |= XML_SPECIAL_EXTERNAL;
1198
1199
40.1k
    if (xmlHashAdd2(ctxt->attsSpecial, fullname, fullattr,
1200
40.1k
                    XML_INT_TO_PTR(type)) < 0)
1201
0
        goto mem_error;
1202
40.1k
    return;
1203
1204
40.1k
mem_error:
1205
0
    xmlErrMemory(ctxt);
1206
0
}
1207
1208
/**
1209
 * Removes CDATA attributes from the special attribute table
1210
 */
1211
static void
1212
xmlCleanSpecialAttrCallback(void *payload, void *data,
1213
                            const xmlChar *fullname, const xmlChar *fullattr,
1214
24.7k
                            const xmlChar *unused ATTRIBUTE_UNUSED) {
1215
24.7k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data;
1216
1217
24.7k
    if (XML_PTR_TO_INT(payload) == XML_ATTRIBUTE_CDATA) {
1218
3.70k
        xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL);
1219
3.70k
    }
1220
24.7k
}
1221
1222
/**
1223
 * Trim the list of attributes defined to remove all those of type
1224
 * CDATA as they are not special. This call should be done when finishing
1225
 * to parse the DTD and before starting to parse the document root.
1226
 *
1227
 * @param ctxt  an XML parser context
1228
 */
1229
static void
1230
xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt)
1231
17.1k
{
1232
17.1k
    if (ctxt->attsSpecial == NULL)
1233
11.3k
        return;
1234
1235
5.79k
    xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt);
1236
1237
5.79k
    if (xmlHashSize(ctxt->attsSpecial) == 0) {
1238
273
        xmlHashFree(ctxt->attsSpecial, NULL);
1239
273
        ctxt->attsSpecial = NULL;
1240
273
    }
1241
5.79k
}
1242
1243
/**
1244
 * Checks that the value conforms to the LanguageID production:
1245
 *
1246
 * @deprecated Internal function, do not use.
1247
 *
1248
 * NOTE: this is somewhat deprecated, those productions were removed from
1249
 * the XML Second edition.
1250
 *
1251
 *     [33] LanguageID ::= Langcode ('-' Subcode)*
1252
 *     [34] Langcode ::= ISO639Code |  IanaCode |  UserCode
1253
 *     [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
1254
 *     [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
1255
 *     [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
1256
 *     [38] Subcode ::= ([a-z] | [A-Z])+
1257
 *
1258
 * The current REC reference the successors of RFC 1766, currently 5646
1259
 *
1260
 * http://www.rfc-editor.org/rfc/rfc5646.txt
1261
 *
1262
 *     langtag       = language
1263
 *                     ["-" script]
1264
 *                     ["-" region]
1265
 *                     *("-" variant)
1266
 *                     *("-" extension)
1267
 *                     ["-" privateuse]
1268
 *     language      = 2*3ALPHA            ; shortest ISO 639 code
1269
 *                     ["-" extlang]       ; sometimes followed by
1270
 *                                         ; extended language subtags
1271
 *                   / 4ALPHA              ; or reserved for future use
1272
 *                   / 5*8ALPHA            ; or registered language subtag
1273
 *
1274
 *     extlang       = 3ALPHA              ; selected ISO 639 codes
1275
 *                     *2("-" 3ALPHA)      ; permanently reserved
1276
 *
1277
 *     script        = 4ALPHA              ; ISO 15924 code
1278
 *
1279
 *     region        = 2ALPHA              ; ISO 3166-1 code
1280
 *                   / 3DIGIT              ; UN M.49 code
1281
 *
1282
 *     variant       = 5*8alphanum         ; registered variants
1283
 *                   / (DIGIT 3alphanum)
1284
 *
1285
 *     extension     = singleton 1*("-" (2*8alphanum))
1286
 *
1287
 *                                         ; Single alphanumerics
1288
 *                                         ; "x" reserved for private use
1289
 *     singleton     = DIGIT               ; 0 - 9
1290
 *                   / %x41-57             ; A - W
1291
 *                   / %x59-5A             ; Y - Z
1292
 *                   / %x61-77             ; a - w
1293
 *                   / %x79-7A             ; y - z
1294
 *
1295
 * it sounds right to still allow Irregular i-xxx IANA and user codes too
1296
 * The parser below doesn't try to cope with extension or privateuse
1297
 * that could be added but that's not interoperable anyway
1298
 *
1299
 * @param lang  pointer to the string value
1300
 * @returns 1 if correct 0 otherwise
1301
 **/
1302
int
1303
xmlCheckLanguageID(const xmlChar * lang)
1304
0
{
1305
0
    const xmlChar *cur = lang, *nxt;
1306
1307
0
    if (cur == NULL)
1308
0
        return (0);
1309
0
    if (((cur[0] == 'i') && (cur[1] == '-')) ||
1310
0
        ((cur[0] == 'I') && (cur[1] == '-')) ||
1311
0
        ((cur[0] == 'x') && (cur[1] == '-')) ||
1312
0
        ((cur[0] == 'X') && (cur[1] == '-'))) {
1313
        /*
1314
         * Still allow IANA code and user code which were coming
1315
         * from the previous version of the XML-1.0 specification
1316
         * it's deprecated but we should not fail
1317
         */
1318
0
        cur += 2;
1319
0
        while (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
1320
0
               ((cur[0] >= 'a') && (cur[0] <= 'z')))
1321
0
            cur++;
1322
0
        return(cur[0] == 0);
1323
0
    }
1324
0
    nxt = cur;
1325
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1326
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1327
0
           nxt++;
1328
0
    if (nxt - cur >= 4) {
1329
        /*
1330
         * Reserved
1331
         */
1332
0
        if ((nxt - cur > 8) || (nxt[0] != 0))
1333
0
            return(0);
1334
0
        return(1);
1335
0
    }
1336
0
    if (nxt - cur < 2)
1337
0
        return(0);
1338
    /* we got an ISO 639 code */
1339
0
    if (nxt[0] == 0)
1340
0
        return(1);
1341
0
    if (nxt[0] != '-')
1342
0
        return(0);
1343
1344
0
    nxt++;
1345
0
    cur = nxt;
1346
    /* now we can have extlang or script or region or variant */
1347
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1348
0
        goto region_m49;
1349
1350
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1351
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1352
0
           nxt++;
1353
0
    if (nxt - cur == 4)
1354
0
        goto script;
1355
0
    if (nxt - cur == 2)
1356
0
        goto region;
1357
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1358
0
        goto variant;
1359
0
    if (nxt - cur != 3)
1360
0
        return(0);
1361
    /* we parsed an extlang */
1362
0
    if (nxt[0] == 0)
1363
0
        return(1);
1364
0
    if (nxt[0] != '-')
1365
0
        return(0);
1366
1367
0
    nxt++;
1368
0
    cur = nxt;
1369
    /* now we can have script or region or variant */
1370
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1371
0
        goto region_m49;
1372
1373
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1374
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1375
0
           nxt++;
1376
0
    if (nxt - cur == 2)
1377
0
        goto region;
1378
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1379
0
        goto variant;
1380
0
    if (nxt - cur != 4)
1381
0
        return(0);
1382
    /* we parsed a script */
1383
0
script:
1384
0
    if (nxt[0] == 0)
1385
0
        return(1);
1386
0
    if (nxt[0] != '-')
1387
0
        return(0);
1388
1389
0
    nxt++;
1390
0
    cur = nxt;
1391
    /* now we can have region or variant */
1392
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1393
0
        goto region_m49;
1394
1395
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1396
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1397
0
           nxt++;
1398
1399
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1400
0
        goto variant;
1401
0
    if (nxt - cur != 2)
1402
0
        return(0);
1403
    /* we parsed a region */
1404
0
region:
1405
0
    if (nxt[0] == 0)
1406
0
        return(1);
1407
0
    if (nxt[0] != '-')
1408
0
        return(0);
1409
1410
0
    nxt++;
1411
0
    cur = nxt;
1412
    /* now we can just have a variant */
1413
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1414
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1415
0
           nxt++;
1416
1417
0
    if ((nxt - cur < 5) || (nxt - cur > 8))
1418
0
        return(0);
1419
1420
    /* we parsed a variant */
1421
0
variant:
1422
0
    if (nxt[0] == 0)
1423
0
        return(1);
1424
0
    if (nxt[0] != '-')
1425
0
        return(0);
1426
    /* extensions and private use subtags not checked */
1427
0
    return (1);
1428
1429
0
region_m49:
1430
0
    if (((nxt[1] >= '0') && (nxt[1] <= '9')) &&
1431
0
        ((nxt[2] >= '0') && (nxt[2] <= '9'))) {
1432
0
        nxt += 3;
1433
0
        goto region;
1434
0
    }
1435
0
    return(0);
1436
0
}
1437
1438
/************************************************************************
1439
 *                  *
1440
 *    Parser stacks related functions and macros    *
1441
 *                  *
1442
 ************************************************************************/
1443
1444
static xmlChar *
1445
xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar **str);
1446
1447
/**
1448
 * Create a new namespace database.
1449
 *
1450
 * @returns the new obejct.
1451
 */
1452
xmlParserNsData *
1453
58.3k
xmlParserNsCreate(void) {
1454
58.3k
    xmlParserNsData *nsdb = xmlMalloc(sizeof(*nsdb));
1455
1456
58.3k
    if (nsdb == NULL)
1457
0
        return(NULL);
1458
58.3k
    memset(nsdb, 0, sizeof(*nsdb));
1459
58.3k
    nsdb->defaultNsIndex = INT_MAX;
1460
1461
58.3k
    return(nsdb);
1462
58.3k
}
1463
1464
/**
1465
 * Free a namespace database.
1466
 *
1467
 * @param nsdb  namespace database
1468
 */
1469
void
1470
58.3k
xmlParserNsFree(xmlParserNsData *nsdb) {
1471
58.3k
    if (nsdb == NULL)
1472
0
        return;
1473
1474
58.3k
    xmlFree(nsdb->extra);
1475
58.3k
    xmlFree(nsdb->hash);
1476
58.3k
    xmlFree(nsdb);
1477
58.3k
}
1478
1479
/**
1480
 * Reset a namespace database.
1481
 *
1482
 * @param nsdb  namespace database
1483
 */
1484
static void
1485
0
xmlParserNsReset(xmlParserNsData *nsdb) {
1486
0
    if (nsdb == NULL)
1487
0
        return;
1488
1489
0
    nsdb->hashElems = 0;
1490
0
    nsdb->elementId = 0;
1491
0
    nsdb->defaultNsIndex = INT_MAX;
1492
1493
0
    if (nsdb->hash)
1494
0
        memset(nsdb->hash, 0, nsdb->hashSize * sizeof(nsdb->hash[0]));
1495
0
}
1496
1497
/**
1498
 * Signal that a new element has started.
1499
 *
1500
 * @param nsdb  namespace database
1501
 * @returns 0 on success, -1 if the element counter overflowed.
1502
 */
1503
static int
1504
4.55M
xmlParserNsStartElement(xmlParserNsData *nsdb) {
1505
4.55M
    if (nsdb->elementId == UINT_MAX)
1506
0
        return(-1);
1507
4.55M
    nsdb->elementId++;
1508
1509
4.55M
    return(0);
1510
4.55M
}
1511
1512
/**
1513
 * Lookup namespace with given prefix. If `bucketPtr` is non-NULL, it will
1514
 * be set to the matching bucket, or the first empty bucket if no match
1515
 * was found.
1516
 *
1517
 * @param ctxt  parser context
1518
 * @param prefix  namespace prefix
1519
 * @param bucketPtr  optional bucket (return value)
1520
 * @returns the namespace index on success, INT_MAX if no namespace was
1521
 * found.
1522
 */
1523
static int
1524
xmlParserNsLookup(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
1525
7.74M
                  xmlParserNsBucket **bucketPtr) {
1526
7.74M
    xmlParserNsBucket *bucket, *tombstone;
1527
7.74M
    unsigned index, hashValue;
1528
1529
7.74M
    if (prefix->name == NULL)
1530
3.24M
        return(ctxt->nsdb->defaultNsIndex);
1531
1532
4.50M
    if (ctxt->nsdb->hashSize == 0)
1533
71.9k
        return(INT_MAX);
1534
1535
4.43M
    hashValue = prefix->hashValue;
1536
4.43M
    index = hashValue & (ctxt->nsdb->hashSize - 1);
1537
4.43M
    bucket = &ctxt->nsdb->hash[index];
1538
4.43M
    tombstone = NULL;
1539
1540
5.79M
    while (bucket->hashValue) {
1541
4.96M
        if (bucket->index == INT_MAX) {
1542
586k
            if (tombstone == NULL)
1543
564k
                tombstone = bucket;
1544
4.38M
        } else if (bucket->hashValue == hashValue) {
1545
3.60M
            if (ctxt->nsTab[bucket->index * 2] == prefix->name) {
1546
3.60M
                if (bucketPtr != NULL)
1547
3.15M
                    *bucketPtr = bucket;
1548
3.60M
                return(bucket->index);
1549
3.60M
            }
1550
3.60M
        }
1551
1552
1.36M
        index++;
1553
1.36M
        bucket++;
1554
1.36M
        if (index == ctxt->nsdb->hashSize) {
1555
66.5k
            index = 0;
1556
66.5k
            bucket = ctxt->nsdb->hash;
1557
66.5k
        }
1558
1.36M
    }
1559
1560
828k
    if (bucketPtr != NULL)
1561
572k
        *bucketPtr = tombstone ? tombstone : bucket;
1562
828k
    return(INT_MAX);
1563
4.43M
}
1564
1565
/**
1566
 * Lookup namespace URI with given prefix.
1567
 *
1568
 * @param ctxt  parser context
1569
 * @param prefix  namespace prefix
1570
 * @returns the namespace URI on success, NULL if no namespace was found.
1571
 */
1572
static const xmlChar *
1573
2.01M
xmlParserNsLookupUri(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix) {
1574
2.01M
    const xmlChar *ret;
1575
2.01M
    int nsIndex;
1576
1577
2.01M
    if (prefix->name == ctxt->str_xml)
1578
2.20k
        return(ctxt->str_xml_ns);
1579
1580
    /*
1581
     * minNsIndex is used when building an entity tree. We must
1582
     * ignore namespaces declared outside the entity.
1583
     */
1584
2.00M
    nsIndex = xmlParserNsLookup(ctxt, prefix, NULL);
1585
2.00M
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1586
417k
        return(NULL);
1587
1588
1.59M
    ret = ctxt->nsTab[nsIndex * 2 + 1];
1589
1.59M
    if (ret[0] == 0)
1590
52.7k
        ret = NULL;
1591
1.59M
    return(ret);
1592
2.00M
}
1593
1594
/**
1595
 * Lookup extra data for the given prefix. This returns data stored
1596
 * with xmlParserNsUdpateSax.
1597
 *
1598
 * @param ctxt  parser context
1599
 * @param prefix  namespace prefix
1600
 * @returns the data on success, NULL if no namespace was found.
1601
 */
1602
void *
1603
1.53M
xmlParserNsLookupSax(xmlParserCtxt *ctxt, const xmlChar *prefix) {
1604
1.53M
    xmlHashedString hprefix;
1605
1.53M
    int nsIndex;
1606
1607
1.53M
    if (prefix == ctxt->str_xml)
1608
236k
        return(NULL);
1609
1610
1.29M
    hprefix.name = prefix;
1611
1.29M
    if (prefix != NULL)
1612
10.6k
        hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
1613
1.28M
    else
1614
1.28M
        hprefix.hashValue = 0;
1615
1.29M
    nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
1616
1.29M
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1617
0
        return(NULL);
1618
1619
1.29M
    return(ctxt->nsdb->extra[nsIndex].saxData);
1620
1.29M
}
1621
1622
/**
1623
 * Sets or updates extra data for the given prefix. This value will be
1624
 * returned by xmlParserNsLookupSax as long as the namespace with the
1625
 * given prefix is in scope.
1626
 *
1627
 * @param ctxt  parser context
1628
 * @param prefix  namespace prefix
1629
 * @param saxData  extra data for SAX handler
1630
 * @returns the data on success, NULL if no namespace was found.
1631
 */
1632
int
1633
xmlParserNsUpdateSax(xmlParserCtxt *ctxt, const xmlChar *prefix,
1634
437k
                     void *saxData) {
1635
437k
    xmlHashedString hprefix;
1636
437k
    int nsIndex;
1637
1638
437k
    if (prefix == ctxt->str_xml)
1639
0
        return(-1);
1640
1641
437k
    hprefix.name = prefix;
1642
437k
    if (prefix != NULL)
1643
359k
        hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
1644
78.8k
    else
1645
78.8k
        hprefix.hashValue = 0;
1646
437k
    nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
1647
437k
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1648
0
        return(-1);
1649
1650
437k
    ctxt->nsdb->extra[nsIndex].saxData = saxData;
1651
437k
    return(0);
1652
437k
}
1653
1654
/**
1655
 * Grows the namespace tables.
1656
 *
1657
 * @param ctxt  parser context
1658
 * @returns 0 on success, -1 if a memory allocation failed.
1659
 */
1660
static int
1661
60.5k
xmlParserNsGrow(xmlParserCtxtPtr ctxt) {
1662
60.5k
    const xmlChar **table;
1663
60.5k
    xmlParserNsExtra *extra;
1664
60.5k
    int newSize;
1665
1666
60.5k
    newSize = xmlGrowCapacity(ctxt->nsMax,
1667
60.5k
                              sizeof(table[0]) + sizeof(extra[0]),
1668
60.5k
                              16, XML_MAX_ITEMS);
1669
60.5k
    if (newSize < 0)
1670
0
        goto error;
1671
1672
60.5k
    table = xmlRealloc(ctxt->nsTab, 2 * newSize * sizeof(table[0]));
1673
60.5k
    if (table == NULL)
1674
0
        goto error;
1675
60.5k
    ctxt->nsTab = table;
1676
1677
60.5k
    extra = xmlRealloc(ctxt->nsdb->extra, newSize * sizeof(extra[0]));
1678
60.5k
    if (extra == NULL)
1679
0
        goto error;
1680
60.5k
    ctxt->nsdb->extra = extra;
1681
1682
60.5k
    ctxt->nsMax = newSize;
1683
60.5k
    return(0);
1684
1685
0
error:
1686
0
    xmlErrMemory(ctxt);
1687
0
    return(-1);
1688
60.5k
}
1689
1690
/**
1691
 * Push a new namespace on the table.
1692
 *
1693
 * @param ctxt  parser context
1694
 * @param prefix  prefix with hash value
1695
 * @param uri  uri with hash value
1696
 * @param saxData  extra data for SAX handler
1697
 * @param defAttr  whether the namespace comes from a default attribute
1698
 * @returns 1 if the namespace was pushed, 0 if the namespace was ignored,
1699
 * -1 if a memory allocation failed.
1700
 */
1701
static int
1702
xmlParserNsPush(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
1703
2.05M
                const xmlHashedString *uri, void *saxData, int defAttr) {
1704
2.05M
    xmlParserNsBucket *bucket = NULL;
1705
2.05M
    xmlParserNsExtra *extra;
1706
2.05M
    const xmlChar **ns;
1707
2.05M
    unsigned hashValue, nsIndex, oldIndex;
1708
1709
2.05M
    if ((prefix != NULL) && (prefix->name == ctxt->str_xml))
1710
388
        return(0);
1711
1712
2.05M
    if ((ctxt->nsNr >= ctxt->nsMax) && (xmlParserNsGrow(ctxt) < 0)) {
1713
0
        xmlErrMemory(ctxt);
1714
0
        return(-1);
1715
0
    }
1716
1717
    /*
1718
     * Default namespace and 'xml' namespace
1719
     */
1720
2.05M
    if ((prefix == NULL) || (prefix->name == NULL)) {
1721
183k
        oldIndex = ctxt->nsdb->defaultNsIndex;
1722
1723
183k
        if (oldIndex != INT_MAX) {
1724
146k
            extra = &ctxt->nsdb->extra[oldIndex];
1725
1726
146k
            if (extra->elementId == ctxt->nsdb->elementId) {
1727
41.7k
                if (defAttr == 0)
1728
41.3k
                    xmlErrAttributeDup(ctxt, NULL, BAD_CAST "xmlns");
1729
41.7k
                return(0);
1730
41.7k
            }
1731
1732
104k
            if ((ctxt->options & XML_PARSE_NSCLEAN) &&
1733
0
                (uri->name == ctxt->nsTab[oldIndex * 2 + 1]))
1734
0
                return(0);
1735
104k
        }
1736
1737
141k
        ctxt->nsdb->defaultNsIndex = ctxt->nsNr;
1738
141k
        goto populate_entry;
1739
183k
    }
1740
1741
    /*
1742
     * Hash table lookup
1743
     */
1744
1.87M
    oldIndex = xmlParserNsLookup(ctxt, prefix, &bucket);
1745
1.87M
    if (oldIndex != INT_MAX) {
1746
1.29M
        extra = &ctxt->nsdb->extra[oldIndex];
1747
1748
        /*
1749
         * Check for duplicate definitions on the same element.
1750
         */
1751
1.29M
        if (extra->elementId == ctxt->nsdb->elementId) {
1752
2.16k
            if (defAttr == 0)
1753
1.77k
                xmlErrAttributeDup(ctxt, BAD_CAST "xmlns", prefix->name);
1754
2.16k
            return(0);
1755
2.16k
        }
1756
1757
1.28M
        if ((ctxt->options & XML_PARSE_NSCLEAN) &&
1758
0
            (uri->name == ctxt->nsTab[bucket->index * 2 + 1]))
1759
0
            return(0);
1760
1761
1.28M
        bucket->index = ctxt->nsNr;
1762
1.28M
        goto populate_entry;
1763
1.28M
    }
1764
1765
    /*
1766
     * Insert new bucket
1767
     */
1768
1769
578k
    hashValue = prefix->hashValue;
1770
1771
    /*
1772
     * Grow hash table, 50% fill factor
1773
     */
1774
578k
    if (ctxt->nsdb->hashElems + 1 > ctxt->nsdb->hashSize / 2) {
1775
11.6k
        xmlParserNsBucket *newHash;
1776
11.6k
        unsigned newSize, i, index;
1777
1778
11.6k
        if (ctxt->nsdb->hashSize > UINT_MAX / 2) {
1779
0
            xmlErrMemory(ctxt);
1780
0
            return(-1);
1781
0
        }
1782
11.6k
        newSize = ctxt->nsdb->hashSize ? ctxt->nsdb->hashSize * 2 : 16;
1783
11.6k
        newHash = xmlMalloc(newSize * sizeof(newHash[0]));
1784
11.6k
        if (newHash == NULL) {
1785
0
            xmlErrMemory(ctxt);
1786
0
            return(-1);
1787
0
        }
1788
11.6k
        memset(newHash, 0, newSize * sizeof(newHash[0]));
1789
1790
2.05M
        for (i = 0; i < ctxt->nsdb->hashSize; i++) {
1791
2.04M
            unsigned hv = ctxt->nsdb->hash[i].hashValue;
1792
2.04M
            unsigned newIndex;
1793
1794
2.04M
            if ((hv == 0) || (ctxt->nsdb->hash[i].index == INT_MAX))
1795
2.03M
                continue;
1796
9.57k
            newIndex = hv & (newSize - 1);
1797
1798
12.0k
            while (newHash[newIndex].hashValue != 0) {
1799
2.47k
                newIndex++;
1800
2.47k
                if (newIndex == newSize)
1801
407
                    newIndex = 0;
1802
2.47k
            }
1803
1804
9.57k
            newHash[newIndex] = ctxt->nsdb->hash[i];
1805
9.57k
        }
1806
1807
11.6k
        xmlFree(ctxt->nsdb->hash);
1808
11.6k
        ctxt->nsdb->hash = newHash;
1809
11.6k
        ctxt->nsdb->hashSize = newSize;
1810
1811
        /*
1812
         * Relookup
1813
         */
1814
11.6k
        index = hashValue & (newSize - 1);
1815
1816
13.0k
        while (newHash[index].hashValue != 0) {
1817
1.35k
            index++;
1818
1.35k
            if (index == newSize)
1819
421
                index = 0;
1820
1.35k
        }
1821
1822
11.6k
        bucket = &newHash[index];
1823
11.6k
    }
1824
1825
578k
    bucket->hashValue = hashValue;
1826
578k
    bucket->index = ctxt->nsNr;
1827
578k
    ctxt->nsdb->hashElems++;
1828
578k
    oldIndex = INT_MAX;
1829
1830
2.00M
populate_entry:
1831
2.00M
    nsIndex = ctxt->nsNr;
1832
1833
2.00M
    ns = &ctxt->nsTab[nsIndex * 2];
1834
2.00M
    ns[0] = prefix ? prefix->name : NULL;
1835
2.00M
    ns[1] = uri->name;
1836
1837
2.00M
    extra = &ctxt->nsdb->extra[nsIndex];
1838
2.00M
    extra->saxData = saxData;
1839
2.00M
    extra->prefixHashValue = prefix ? prefix->hashValue : 0;
1840
2.00M
    extra->uriHashValue = uri->hashValue;
1841
2.00M
    extra->elementId = ctxt->nsdb->elementId;
1842
2.00M
    extra->oldIndex = oldIndex;
1843
1844
2.00M
    ctxt->nsNr++;
1845
1846
2.00M
    return(1);
1847
578k
}
1848
1849
/**
1850
 * Pops the top `nr` namespaces and restores the hash table.
1851
 *
1852
 * @param ctxt  an XML parser context
1853
 * @param nr  the number to pop
1854
 * @returns the number of namespaces popped.
1855
 */
1856
static int
1857
xmlParserNsPop(xmlParserCtxtPtr ctxt, int nr)
1858
330k
{
1859
330k
    int i;
1860
1861
    /* assert(nr <= ctxt->nsNr); */
1862
1863
2.33M
    for (i = ctxt->nsNr - 1; i >= ctxt->nsNr - nr; i--) {
1864
2.00M
        const xmlChar *prefix = ctxt->nsTab[i * 2];
1865
2.00M
        xmlParserNsExtra *extra = &ctxt->nsdb->extra[i];
1866
1867
2.00M
        if (prefix == NULL) {
1868
140k
            ctxt->nsdb->defaultNsIndex = extra->oldIndex;
1869
1.86M
        } else {
1870
1.86M
            xmlHashedString hprefix;
1871
1.86M
            xmlParserNsBucket *bucket = NULL;
1872
1873
1.86M
            hprefix.name = prefix;
1874
1.86M
            hprefix.hashValue = extra->prefixHashValue;
1875
1.86M
            xmlParserNsLookup(ctxt, &hprefix, &bucket);
1876
            /* assert(bucket && bucket->hashValue); */
1877
1.86M
            bucket->index = extra->oldIndex;
1878
1.86M
        }
1879
2.00M
    }
1880
1881
330k
    ctxt->nsNr -= nr;
1882
330k
    return(nr);
1883
330k
}
1884
1885
static int
1886
54.7k
xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt) {
1887
54.7k
    const xmlChar **atts;
1888
54.7k
    unsigned *attallocs;
1889
54.7k
    int newSize;
1890
1891
54.7k
    newSize = xmlGrowCapacity(ctxt->maxatts / 5,
1892
54.7k
                              sizeof(atts[0]) * 5 + sizeof(attallocs[0]),
1893
54.7k
                              10, XML_MAX_ATTRS);
1894
54.7k
    if (newSize < 0) {
1895
0
        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
1896
0
                    "Maximum number of attributes exceeded");
1897
0
        return(-1);
1898
0
    }
1899
1900
54.7k
    atts = xmlRealloc(ctxt->atts, newSize * sizeof(atts[0]) * 5);
1901
54.7k
    if (atts == NULL)
1902
0
        goto mem_error;
1903
54.7k
    ctxt->atts = atts;
1904
1905
54.7k
    attallocs = xmlRealloc(ctxt->attallocs,
1906
54.7k
                           newSize * sizeof(attallocs[0]));
1907
54.7k
    if (attallocs == NULL)
1908
0
        goto mem_error;
1909
54.7k
    ctxt->attallocs = attallocs;
1910
1911
54.7k
    ctxt->maxatts = newSize * 5;
1912
1913
54.7k
    return(0);
1914
1915
0
mem_error:
1916
0
    xmlErrMemory(ctxt);
1917
0
    return(-1);
1918
54.7k
}
1919
1920
/**
1921
 * Pushes a new parser input on top of the input stack
1922
 *
1923
 * @param ctxt  an XML parser context
1924
 * @param value  the parser input
1925
 * @returns -1 in case of error, the index in the stack otherwise
1926
 */
1927
int
1928
xmlCtxtPushInput(xmlParserCtxt *ctxt, xmlParserInput *value)
1929
109k
{
1930
109k
    char *directory = NULL;
1931
109k
    int maxDepth;
1932
1933
109k
    if ((ctxt == NULL) || (value == NULL))
1934
0
        return(-1);
1935
1936
109k
    maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
1937
1938
109k
    if (ctxt->inputNr >= ctxt->inputMax) {
1939
3.26k
        xmlParserInputPtr *tmp;
1940
3.26k
        int newSize;
1941
1942
3.26k
        newSize = xmlGrowCapacity(ctxt->inputMax, sizeof(tmp[0]),
1943
3.26k
                                  5, maxDepth);
1944
3.26k
        if (newSize < 0) {
1945
0
            xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
1946
0
                           "Maximum entity nesting depth exceeded");
1947
0
            return(-1);
1948
0
        }
1949
3.26k
        tmp = xmlRealloc(ctxt->inputTab, newSize * sizeof(tmp[0]));
1950
3.26k
        if (tmp == NULL) {
1951
0
            xmlErrMemory(ctxt);
1952
0
            return(-1);
1953
0
        }
1954
3.26k
        ctxt->inputTab = tmp;
1955
3.26k
        ctxt->inputMax = newSize;
1956
3.26k
    }
1957
1958
109k
    if ((ctxt->inputNr == 0) && (value->filename != NULL)) {
1959
23.6k
        directory = xmlParserGetDirectory(value->filename);
1960
23.6k
        if (directory == NULL) {
1961
0
            xmlErrMemory(ctxt);
1962
0
            return(-1);
1963
0
        }
1964
23.6k
    }
1965
1966
109k
    if (ctxt->input_id >= INT_MAX) {
1967
0
        xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT, "Input ID overflow\n");
1968
0
        return(-1);
1969
0
    }
1970
1971
109k
    ctxt->inputTab[ctxt->inputNr] = value;
1972
109k
    ctxt->input = value;
1973
1974
109k
    if (ctxt->inputNr == 0) {
1975
58.3k
        xmlFree(ctxt->directory);
1976
58.3k
        ctxt->directory = directory;
1977
58.3k
    }
1978
1979
    /*
1980
     * The input ID is unused internally, but there are entity
1981
     * loaders in downstream code that detect the main document
1982
     * by checking for "input_id == 1".
1983
     */
1984
109k
    value->id = ctxt->input_id++;
1985
1986
109k
    return(ctxt->inputNr++);
1987
109k
}
1988
1989
/**
1990
 * Pops the top parser input from the input stack
1991
 *
1992
 * @param ctxt  an XML parser context
1993
 * @returns the input just removed
1994
 */
1995
xmlParserInput *
1996
xmlCtxtPopInput(xmlParserCtxt *ctxt)
1997
226k
{
1998
226k
    xmlParserInputPtr ret;
1999
2000
226k
    if (ctxt == NULL)
2001
0
        return(NULL);
2002
226k
    if (ctxt->inputNr <= 0)
2003
116k
        return (NULL);
2004
109k
    ctxt->inputNr--;
2005
109k
    if (ctxt->inputNr > 0)
2006
51.3k
        ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
2007
58.3k
    else
2008
58.3k
        ctxt->input = NULL;
2009
109k
    ret = ctxt->inputTab[ctxt->inputNr];
2010
109k
    ctxt->inputTab[ctxt->inputNr] = NULL;
2011
109k
    return (ret);
2012
226k
}
2013
2014
/**
2015
 * Pushes a new element node on top of the node stack
2016
 *
2017
 * @deprecated Internal function, do not use.
2018
 *
2019
 * @param ctxt  an XML parser context
2020
 * @param value  the element node
2021
 * @returns -1 in case of error, the index in the stack otherwise
2022
 */
2023
int
2024
nodePush(xmlParserCtxt *ctxt, xmlNode *value)
2025
1.57M
{
2026
1.57M
    if (ctxt == NULL)
2027
0
        return(0);
2028
2029
1.57M
    if (ctxt->nodeNr >= ctxt->nodeMax) {
2030
50.6k
        int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
2031
50.6k
        xmlNodePtr *tmp;
2032
50.6k
        int newSize;
2033
2034
50.6k
        newSize = xmlGrowCapacity(ctxt->nodeMax, sizeof(tmp[0]),
2035
50.6k
                                  10, maxDepth);
2036
50.6k
        if (newSize < 0) {
2037
21
            xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
2038
21
                    "Excessive depth in document: %d,"
2039
21
                    " use XML_PARSE_HUGE option\n",
2040
21
                    ctxt->nodeNr);
2041
21
            return(-1);
2042
21
        }
2043
2044
50.5k
  tmp = xmlRealloc(ctxt->nodeTab, newSize * sizeof(tmp[0]));
2045
50.5k
        if (tmp == NULL) {
2046
0
            xmlErrMemory(ctxt);
2047
0
            return (-1);
2048
0
        }
2049
50.5k
        ctxt->nodeTab = tmp;
2050
50.5k
  ctxt->nodeMax = newSize;
2051
50.5k
    }
2052
2053
1.57M
    ctxt->nodeTab[ctxt->nodeNr] = value;
2054
1.57M
    ctxt->node = value;
2055
1.57M
    return (ctxt->nodeNr++);
2056
1.57M
}
2057
2058
/**
2059
 * Pops the top element node from the node stack
2060
 *
2061
 * @deprecated Internal function, do not use.
2062
 *
2063
 * @param ctxt  an XML parser context
2064
 * @returns the node just removed
2065
 */
2066
xmlNode *
2067
nodePop(xmlParserCtxt *ctxt)
2068
1.78M
{
2069
1.78M
    xmlNodePtr ret;
2070
2071
1.78M
    if (ctxt == NULL) return(NULL);
2072
1.78M
    if (ctxt->nodeNr <= 0)
2073
216k
        return (NULL);
2074
1.56M
    ctxt->nodeNr--;
2075
1.56M
    if (ctxt->nodeNr > 0)
2076
1.54M
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
2077
24.3k
    else
2078
24.3k
        ctxt->node = NULL;
2079
1.56M
    ret = ctxt->nodeTab[ctxt->nodeNr];
2080
1.56M
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
2081
1.56M
    return (ret);
2082
1.78M
}
2083
2084
/**
2085
 * Pushes a new element name/prefix/URL on top of the name stack
2086
 *
2087
 * @param ctxt  an XML parser context
2088
 * @param value  the element name
2089
 * @param prefix  the element prefix
2090
 * @param URI  the element namespace name
2091
 * @param line  the current line number for error messages
2092
 * @param nsNr  the number of namespaces pushed on the namespace table
2093
 * @returns -1 in case of error, the index in the stack otherwise
2094
 */
2095
static int
2096
nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
2097
           const xmlChar *prefix, const xmlChar *URI, int line, int nsNr)
2098
2.02M
{
2099
2.02M
    xmlStartTag *tag;
2100
2101
2.02M
    if (ctxt->nameNr >= ctxt->nameMax) {
2102
69.0k
        const xmlChar **tmp;
2103
69.0k
        xmlStartTag *tmp2;
2104
69.0k
        int newSize;
2105
2106
69.0k
        newSize = xmlGrowCapacity(ctxt->nameMax,
2107
69.0k
                                  sizeof(tmp[0]) + sizeof(tmp2[0]),
2108
69.0k
                                  10, XML_MAX_ITEMS);
2109
69.0k
        if (newSize < 0)
2110
0
            goto mem_error;
2111
2112
69.0k
        tmp = xmlRealloc(ctxt->nameTab, newSize * sizeof(tmp[0]));
2113
69.0k
        if (tmp == NULL)
2114
0
      goto mem_error;
2115
69.0k
  ctxt->nameTab = tmp;
2116
2117
69.0k
        tmp2 = xmlRealloc(ctxt->pushTab, newSize * sizeof(tmp2[0]));
2118
69.0k
        if (tmp2 == NULL)
2119
0
      goto mem_error;
2120
69.0k
  ctxt->pushTab = tmp2;
2121
2122
69.0k
        ctxt->nameMax = newSize;
2123
1.95M
    } else if (ctxt->pushTab == NULL) {
2124
44.2k
        ctxt->pushTab = xmlMalloc(ctxt->nameMax * sizeof(ctxt->pushTab[0]));
2125
44.2k
        if (ctxt->pushTab == NULL)
2126
0
            goto mem_error;
2127
44.2k
    }
2128
2.02M
    ctxt->nameTab[ctxt->nameNr] = value;
2129
2.02M
    ctxt->name = value;
2130
2.02M
    tag = &ctxt->pushTab[ctxt->nameNr];
2131
2.02M
    tag->prefix = prefix;
2132
2.02M
    tag->URI = URI;
2133
2.02M
    tag->line = line;
2134
2.02M
    tag->nsNr = nsNr;
2135
2.02M
    return (ctxt->nameNr++);
2136
0
mem_error:
2137
0
    xmlErrMemory(ctxt);
2138
0
    return (-1);
2139
2.02M
}
2140
#ifdef LIBXML_PUSH_ENABLED
2141
/**
2142
 * Pops the top element/prefix/URI name from the name stack
2143
 *
2144
 * @param ctxt  an XML parser context
2145
 * @returns the name just removed
2146
 */
2147
static const xmlChar *
2148
nameNsPop(xmlParserCtxtPtr ctxt)
2149
0
{
2150
0
    const xmlChar *ret;
2151
2152
0
    if (ctxt->nameNr <= 0)
2153
0
        return (NULL);
2154
0
    ctxt->nameNr--;
2155
0
    if (ctxt->nameNr > 0)
2156
0
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
2157
0
    else
2158
0
        ctxt->name = NULL;
2159
0
    ret = ctxt->nameTab[ctxt->nameNr];
2160
0
    ctxt->nameTab[ctxt->nameNr] = NULL;
2161
0
    return (ret);
2162
0
}
2163
#endif /* LIBXML_PUSH_ENABLED */
2164
2165
/**
2166
 * Pops the top element name from the name stack
2167
 *
2168
 * @deprecated Internal function, do not use.
2169
 *
2170
 * @param ctxt  an XML parser context
2171
 * @returns the name just removed
2172
 */
2173
static const xmlChar *
2174
namePop(xmlParserCtxtPtr ctxt)
2175
2.01M
{
2176
2.01M
    const xmlChar *ret;
2177
2178
2.01M
    if ((ctxt == NULL) || (ctxt->nameNr <= 0))
2179
0
        return (NULL);
2180
2.01M
    ctxt->nameNr--;
2181
2.01M
    if (ctxt->nameNr > 0)
2182
1.98M
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
2183
32.1k
    else
2184
32.1k
        ctxt->name = NULL;
2185
2.01M
    ret = ctxt->nameTab[ctxt->nameNr];
2186
2.01M
    ctxt->nameTab[ctxt->nameNr] = NULL;
2187
2.01M
    return (ret);
2188
2.01M
}
2189
2190
4.56M
static int spacePush(xmlParserCtxtPtr ctxt, int val) {
2191
4.56M
    if (ctxt->spaceNr >= ctxt->spaceMax) {
2192
108k
        int *tmp;
2193
108k
        int newSize;
2194
2195
108k
        newSize = xmlGrowCapacity(ctxt->spaceMax, sizeof(tmp[0]),
2196
108k
                                  10, XML_MAX_ITEMS);
2197
108k
        if (newSize < 0) {
2198
0
      xmlErrMemory(ctxt);
2199
0
      return(-1);
2200
0
        }
2201
2202
108k
        tmp = xmlRealloc(ctxt->spaceTab, newSize * sizeof(tmp[0]));
2203
108k
        if (tmp == NULL) {
2204
0
      xmlErrMemory(ctxt);
2205
0
      return(-1);
2206
0
  }
2207
108k
  ctxt->spaceTab = tmp;
2208
2209
108k
        ctxt->spaceMax = newSize;
2210
108k
    }
2211
4.56M
    ctxt->spaceTab[ctxt->spaceNr] = val;
2212
4.56M
    ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
2213
4.56M
    return(ctxt->spaceNr++);
2214
4.56M
}
2215
2216
4.55M
static int spacePop(xmlParserCtxtPtr ctxt) {
2217
4.55M
    int ret;
2218
4.55M
    if (ctxt->spaceNr <= 0) return(0);
2219
4.55M
    ctxt->spaceNr--;
2220
4.55M
    if (ctxt->spaceNr > 0)
2221
4.55M
  ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
2222
0
    else
2223
0
        ctxt->space = &ctxt->spaceTab[0];
2224
4.55M
    ret = ctxt->spaceTab[ctxt->spaceNr];
2225
4.55M
    ctxt->spaceTab[ctxt->spaceNr] = -1;
2226
4.55M
    return(ret);
2227
4.55M
}
2228
2229
/*
2230
 * Macros for accessing the content. Those should be used only by the parser,
2231
 * and not exported.
2232
 *
2233
 * Dirty macros, i.e. one often need to make assumption on the context to
2234
 * use them
2235
 *
2236
 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
2237
 *           To be used with extreme caution since operations consuming
2238
 *           characters may move the input buffer to a different location !
2239
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
2240
 *           This should be used internally by the parser
2241
 *           only to compare to ASCII values otherwise it would break when
2242
 *           running with UTF-8 encoding.
2243
 *   RAW     same as CUR but in the input buffer, bypass any token
2244
 *           extraction that may have been done
2245
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
2246
 *           to compare on ASCII based substring.
2247
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
2248
 *           strings without newlines within the parser.
2249
 *   NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
2250
 *           defined char within the parser.
2251
 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
2252
 *
2253
 *   NEXT    Skip to the next character, this does the proper decoding
2254
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
2255
 *   NEXTL(l) Skip the current unicode character of l xmlChars long.
2256
 *   COPY_BUF  copy the current unicode char to the target buffer, increment
2257
 *            the index
2258
 *   GROW, SHRINK  handling of input buffers
2259
 */
2260
2261
98.4M
#define RAW (*ctxt->input->cur)
2262
659M
#define CUR (*ctxt->input->cur)
2263
38.7M
#define NXT(val) ctxt->input->cur[(val)]
2264
1.33G
#define CUR_PTR ctxt->input->cur
2265
16.4M
#define BASE_PTR ctxt->input->base
2266
2267
#define CMP4( s, c1, c2, c3, c4 ) \
2268
67.6M
  ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
2269
33.8M
    ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
2270
#define CMP5( s, c1, c2, c3, c4, c5 ) \
2271
67.2M
  ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
2272
#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
2273
66.7M
  ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
2274
#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
2275
66.4M
  ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
2276
#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
2277
66.2M
  ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
2278
#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
2279
33.0M
  ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
2280
33.0M
    ((unsigned char *) s)[ 8 ] == c9 )
2281
#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
2282
6.98k
  ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
2283
6.98k
    ((unsigned char *) s)[ 9 ] == c10 )
2284
2285
2.60M
#define SKIP(val) do {             \
2286
2.60M
    ctxt->input->cur += (val),ctxt->input->col+=(val);      \
2287
2.60M
    if (*ctxt->input->cur == 0)           \
2288
2.60M
        xmlParserGrow(ctxt);           \
2289
2.60M
  } while (0)
2290
2291
#define SKIPL(val) do {             \
2292
    int skipl;                \
2293
    for(skipl=0; skipl<val; skipl++) {          \
2294
  if (*(ctxt->input->cur) == '\n') {        \
2295
  ctxt->input->line++; ctxt->input->col = 1;      \
2296
  } else ctxt->input->col++;          \
2297
  ctxt->input->cur++;           \
2298
    }                 \
2299
    if (*ctxt->input->cur == 0)           \
2300
        xmlParserGrow(ctxt);            \
2301
  } while (0)
2302
2303
#define SHRINK \
2304
36.5M
    if (!PARSER_PROGRESSIVE(ctxt)) \
2305
36.5M
  xmlParserShrink(ctxt);
2306
2307
#define GROW \
2308
113M
    if ((!PARSER_PROGRESSIVE(ctxt)) && \
2309
113M
        (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
2310
3.05M
  xmlParserGrow(ctxt);
2311
2312
12.5M
#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
2313
2314
522k
#define SKIP_BLANKS_PE xmlSkipBlankCharsPE(ctxt)
2315
2316
69.3M
#define NEXT xmlNextChar(ctxt)
2317
2318
7.61M
#define NEXT1 {               \
2319
7.61M
  ctxt->input->col++;           \
2320
7.61M
  ctxt->input->cur++;           \
2321
7.61M
  if (*ctxt->input->cur == 0)         \
2322
7.61M
      xmlParserGrow(ctxt);           \
2323
7.61M
    }
2324
2325
743M
#define NEXTL(l) do {             \
2326
743M
    if (*(ctxt->input->cur) == '\n') {         \
2327
10.8M
  ctxt->input->line++; ctxt->input->col = 1;      \
2328
732M
    } else ctxt->input->col++;           \
2329
743M
    ctxt->input->cur += l;        \
2330
743M
  } while (0)
2331
2332
#define COPY_BUF(b, i, v)           \
2333
99.6M
    if (v < 0x80) b[i++] = v;           \
2334
99.6M
    else i += xmlCopyCharMultiByte(&b[i],v)
2335
2336
static int
2337
94.9M
xmlCurrentCharRecover(xmlParserCtxtPtr ctxt, int *len) {
2338
94.9M
    int c = xmlCurrentChar(ctxt, len);
2339
2340
94.9M
    if (c == XML_INVALID_CHAR)
2341
19.7M
        c = 0xFFFD; /* replacement character */
2342
2343
94.9M
    return(c);
2344
94.9M
}
2345
2346
/**
2347
 * Skip whitespace in the input stream.
2348
 *
2349
 * @deprecated Internal function, do not use.
2350
 *
2351
 * @param ctxt  the XML parser context
2352
 * @returns the number of space chars skipped
2353
 */
2354
int
2355
12.9M
xmlSkipBlankChars(xmlParserCtxt *ctxt) {
2356
12.9M
    const xmlChar *cur;
2357
12.9M
    int res = 0;
2358
2359
12.9M
    cur = ctxt->input->cur;
2360
12.9M
    while (IS_BLANK_CH(*cur)) {
2361
11.1M
        if (*cur == '\n') {
2362
2.69M
            ctxt->input->line++; ctxt->input->col = 1;
2363
8.44M
        } else {
2364
8.44M
            ctxt->input->col++;
2365
8.44M
        }
2366
11.1M
        cur++;
2367
11.1M
        if (res < INT_MAX)
2368
11.1M
            res++;
2369
11.1M
        if (*cur == 0) {
2370
7.55k
            ctxt->input->cur = cur;
2371
7.55k
            xmlParserGrow(ctxt);
2372
7.55k
            cur = ctxt->input->cur;
2373
7.55k
        }
2374
11.1M
    }
2375
12.9M
    ctxt->input->cur = cur;
2376
2377
12.9M
    if (res > 4)
2378
38.4k
        GROW;
2379
2380
12.9M
    return(res);
2381
12.9M
}
2382
2383
static void
2384
38.8k
xmlPopPE(xmlParserCtxtPtr ctxt) {
2385
38.8k
    unsigned long consumed;
2386
38.8k
    xmlEntityPtr ent;
2387
2388
38.8k
    ent = ctxt->input->entity;
2389
2390
38.8k
    ent->flags &= ~XML_ENT_EXPANDING;
2391
2392
38.8k
    if ((ent->flags & XML_ENT_CHECKED) == 0) {
2393
2.22k
        int result;
2394
2395
        /*
2396
         * Read the rest of the stream in case of errors. We want
2397
         * to account for the whole entity size.
2398
         */
2399
2.22k
        do {
2400
2.22k
            ctxt->input->cur = ctxt->input->end;
2401
2.22k
            xmlParserShrink(ctxt);
2402
2.22k
            result = xmlParserGrow(ctxt);
2403
2.22k
        } while (result > 0);
2404
2405
2.22k
        consumed = ctxt->input->consumed;
2406
2.22k
        xmlSaturatedAddSizeT(&consumed,
2407
2.22k
                             ctxt->input->end - ctxt->input->base);
2408
2409
2.22k
        xmlSaturatedAdd(&ent->expandedSize, consumed);
2410
2411
        /*
2412
         * Add to sizeentities when parsing an external entity
2413
         * for the first time.
2414
         */
2415
2.22k
        if (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
2416
0
            xmlSaturatedAdd(&ctxt->sizeentities, consumed);
2417
0
        }
2418
2419
2.22k
        ent->flags |= XML_ENT_CHECKED;
2420
2.22k
    }
2421
2422
38.8k
    xmlFreeInputStream(xmlCtxtPopInput(ctxt));
2423
2424
38.8k
    xmlParserEntityCheck(ctxt, ent->expandedSize);
2425
2426
38.8k
    GROW;
2427
38.8k
}
2428
2429
/**
2430
 * Skip whitespace in the input stream, also handling parameter
2431
 * entities.
2432
 *
2433
 * @param ctxt  the XML parser context
2434
 * @returns the number of space chars skipped
2435
 */
2436
static int
2437
522k
xmlSkipBlankCharsPE(xmlParserCtxtPtr ctxt) {
2438
522k
    int res = 0;
2439
522k
    int inParam;
2440
522k
    int expandParam;
2441
2442
522k
    inParam = PARSER_IN_PE(ctxt);
2443
522k
    expandParam = PARSER_EXTERNAL(ctxt);
2444
2445
522k
    if (!inParam && !expandParam)
2446
370k
        return(xmlSkipBlankChars(ctxt));
2447
2448
    /*
2449
     * It's Okay to use CUR/NEXT here since all the blanks are on
2450
     * the ASCII range.
2451
     */
2452
629k
    while (PARSER_STOPPED(ctxt) == 0) {
2453
629k
        if (IS_BLANK_CH(CUR)) { /* CHECKED tstblanks.xml */
2454
476k
            NEXT;
2455
476k
        } else if (CUR == '%') {
2456
2.04k
            if ((expandParam == 0) ||
2457
0
                (IS_BLANK_CH(NXT(1))) || (NXT(1) == 0))
2458
2.04k
                break;
2459
2460
            /*
2461
             * Expand parameter entity. We continue to consume
2462
             * whitespace at the start of the entity and possible
2463
             * even consume the whole entity and pop it. We might
2464
             * even pop multiple PEs in this loop.
2465
             */
2466
0
            xmlParsePERefInternal(ctxt, 0);
2467
2468
0
            inParam = PARSER_IN_PE(ctxt);
2469
0
            expandParam = PARSER_EXTERNAL(ctxt);
2470
151k
        } else if (CUR == 0) {
2471
28.9k
            if (inParam == 0)
2472
6
                break;
2473
2474
            /*
2475
             * Don't pop parameter entities that start a markup
2476
             * declaration to detect Well-formedness constraint:
2477
             * PE Between Declarations.
2478
             */
2479
28.9k
            if (ctxt->input->flags & XML_INPUT_MARKUP_DECL)
2480
28.0k
                break;
2481
2482
953
            xmlPopPE(ctxt);
2483
2484
953
            inParam = PARSER_IN_PE(ctxt);
2485
953
            expandParam = PARSER_EXTERNAL(ctxt);
2486
122k
        } else {
2487
122k
            break;
2488
122k
        }
2489
2490
        /*
2491
         * Also increase the counter when entering or exiting a PERef.
2492
         * The spec says: "When a parameter-entity reference is recognized
2493
         * in the DTD and included, its replacement text MUST be enlarged
2494
         * by the attachment of one leading and one following space (#x20)
2495
         * character."
2496
         */
2497
476k
        if (res < INT_MAX)
2498
476k
            res++;
2499
476k
    }
2500
2501
152k
    return(res);
2502
522k
}
2503
2504
/************************************************************************
2505
 *                  *
2506
 *    Commodity functions to handle entities      *
2507
 *                  *
2508
 ************************************************************************/
2509
2510
/**
2511
 * @deprecated Internal function, don't use.
2512
 *
2513
 * @param ctxt  an XML parser context
2514
 * @returns the current xmlChar in the parser context
2515
 */
2516
xmlChar
2517
0
xmlPopInput(xmlParserCtxt *ctxt) {
2518
0
    xmlParserInputPtr input;
2519
2520
0
    if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0);
2521
0
    input = xmlCtxtPopInput(ctxt);
2522
0
    xmlFreeInputStream(input);
2523
0
    if (*ctxt->input->cur == 0)
2524
0
        xmlParserGrow(ctxt);
2525
0
    return(CUR);
2526
0
}
2527
2528
/**
2529
 * Push an input stream onto the stack.
2530
 *
2531
 * @deprecated Internal function, don't use.
2532
 *
2533
 * @param ctxt  an XML parser context
2534
 * @param input  an XML parser input fragment (entity, XML fragment ...).
2535
 * @returns -1 in case of error or the index in the input stack
2536
 */
2537
int
2538
0
xmlPushInput(xmlParserCtxt *ctxt, xmlParserInput *input) {
2539
0
    int ret;
2540
2541
0
    if ((ctxt == NULL) || (input == NULL))
2542
0
        return(-1);
2543
2544
0
    ret = xmlCtxtPushInput(ctxt, input);
2545
0
    if (ret >= 0)
2546
0
        GROW;
2547
0
    return(ret);
2548
0
}
2549
2550
/**
2551
 * Parse a numeric character reference. Always consumes '&'.
2552
 *
2553
 * @deprecated Internal function, don't use.
2554
 *
2555
 *     [66] CharRef ::= '&#' [0-9]+ ';' |
2556
 *                      '&#x' [0-9a-fA-F]+ ';'
2557
 *
2558
 * [ WFC: Legal Character ]
2559
 * Characters referred to using character references must match the
2560
 * production for Char.
2561
 *
2562
 * @param ctxt  an XML parser context
2563
 * @returns the value parsed (as an int), 0 in case of error
2564
 */
2565
int
2566
126k
xmlParseCharRef(xmlParserCtxt *ctxt) {
2567
126k
    int val = 0;
2568
126k
    int count = 0;
2569
2570
    /*
2571
     * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
2572
     */
2573
126k
    if ((RAW == '&') && (NXT(1) == '#') &&
2574
126k
        (NXT(2) == 'x')) {
2575
82.8k
  SKIP(3);
2576
82.8k
  GROW;
2577
196k
  while ((RAW != ';') && (PARSER_STOPPED(ctxt) == 0)) {
2578
125k
      if (count++ > 20) {
2579
898
    count = 0;
2580
898
    GROW;
2581
898
      }
2582
125k
      if ((RAW >= '0') && (RAW <= '9'))
2583
27.4k
          val = val * 16 + (CUR - '0');
2584
98.3k
      else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
2585
27.7k
          val = val * 16 + (CUR - 'a') + 10;
2586
70.5k
      else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
2587
58.4k
          val = val * 16 + (CUR - 'A') + 10;
2588
12.1k
      else {
2589
12.1k
    xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
2590
12.1k
    val = 0;
2591
12.1k
    break;
2592
12.1k
      }
2593
113k
      if (val > 0x110000)
2594
13.8k
          val = 0x110000;
2595
2596
113k
      NEXT;
2597
113k
      count++;
2598
113k
  }
2599
82.8k
  if (RAW == ';') {
2600
      /* on purpose to avoid reentrancy problems with NEXT and SKIP */
2601
70.7k
      ctxt->input->col++;
2602
70.7k
      ctxt->input->cur++;
2603
70.7k
  }
2604
82.8k
    } else if  ((RAW == '&') && (NXT(1) == '#')) {
2605
43.2k
  SKIP(2);
2606
43.2k
  GROW;
2607
104k
  while (RAW != ';') { /* loop blocked by count */
2608
71.2k
      if (count++ > 20) {
2609
864
    count = 0;
2610
864
    GROW;
2611
864
      }
2612
71.2k
      if ((RAW >= '0') && (RAW <= '9'))
2613
61.6k
          val = val * 10 + (CUR - '0');
2614
9.62k
      else {
2615
9.62k
    xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
2616
9.62k
    val = 0;
2617
9.62k
    break;
2618
9.62k
      }
2619
61.6k
      if (val > 0x110000)
2620
6.69k
          val = 0x110000;
2621
2622
61.6k
      NEXT;
2623
61.6k
      count++;
2624
61.6k
  }
2625
43.2k
  if (RAW == ';') {
2626
      /* on purpose to avoid reentrancy problems with NEXT and SKIP */
2627
33.6k
      ctxt->input->col++;
2628
33.6k
      ctxt->input->cur++;
2629
33.6k
  }
2630
43.2k
    } else {
2631
0
        if (RAW == '&')
2632
0
            SKIP(1);
2633
0
        xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
2634
0
    }
2635
2636
    /*
2637
     * [ WFC: Legal Character ]
2638
     * Characters referred to using character references must match the
2639
     * production for Char.
2640
     */
2641
126k
    if (val >= 0x110000) {
2642
550
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2643
550
                "xmlParseCharRef: character reference out of bounds\n",
2644
550
          val);
2645
550
        val = 0xFFFD;
2646
125k
    } else if (!IS_CHAR(val)) {
2647
26.1k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2648
26.1k
                          "xmlParseCharRef: invalid xmlChar value %d\n",
2649
26.1k
                    val);
2650
26.1k
    }
2651
126k
    return(val);
2652
126k
}
2653
2654
/**
2655
 * Parse Reference declarations, variant parsing from a string rather
2656
 * than an an input flow.
2657
 *
2658
 *     [66] CharRef ::= '&#' [0-9]+ ';' |
2659
 *                      '&#x' [0-9a-fA-F]+ ';'
2660
 *
2661
 * [ WFC: Legal Character ]
2662
 * Characters referred to using character references must match the
2663
 * production for Char.
2664
 *
2665
 * @param ctxt  an XML parser context
2666
 * @param str  a pointer to an index in the string
2667
 * @returns the value parsed (as an int), 0 in case of error, str will be
2668
 *         updated to the current value of the index
2669
 */
2670
static int
2671
250k
xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
2672
250k
    const xmlChar *ptr;
2673
250k
    xmlChar cur;
2674
250k
    int val = 0;
2675
2676
250k
    if ((str == NULL) || (*str == NULL)) return(0);
2677
250k
    ptr = *str;
2678
250k
    cur = *ptr;
2679
250k
    if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
2680
35.1k
  ptr += 3;
2681
35.1k
  cur = *ptr;
2682
96.5k
  while (cur != ';') { /* Non input consuming loop */
2683
63.1k
      if ((cur >= '0') && (cur <= '9'))
2684
50.8k
          val = val * 16 + (cur - '0');
2685
12.2k
      else if ((cur >= 'a') && (cur <= 'f'))
2686
6.56k
          val = val * 16 + (cur - 'a') + 10;
2687
5.69k
      else if ((cur >= 'A') && (cur <= 'F'))
2688
3.96k
          val = val * 16 + (cur - 'A') + 10;
2689
1.72k
      else {
2690
1.72k
    xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
2691
1.72k
    val = 0;
2692
1.72k
    break;
2693
1.72k
      }
2694
61.3k
      if (val > 0x110000)
2695
1.85k
          val = 0x110000;
2696
2697
61.3k
      ptr++;
2698
61.3k
      cur = *ptr;
2699
61.3k
  }
2700
35.1k
  if (cur == ';')
2701
33.3k
      ptr++;
2702
215k
    } else if  ((cur == '&') && (ptr[1] == '#')){
2703
215k
  ptr += 2;
2704
215k
  cur = *ptr;
2705
793k
  while (cur != ';') { /* Non input consuming loops */
2706
580k
      if ((cur >= '0') && (cur <= '9'))
2707
578k
          val = val * 10 + (cur - '0');
2708
1.80k
      else {
2709
1.80k
    xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
2710
1.80k
    val = 0;
2711
1.80k
    break;
2712
1.80k
      }
2713
578k
      if (val > 0x110000)
2714
5.12k
          val = 0x110000;
2715
2716
578k
      ptr++;
2717
578k
      cur = *ptr;
2718
578k
  }
2719
215k
  if (cur == ';')
2720
213k
      ptr++;
2721
215k
    } else {
2722
0
  xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
2723
0
  return(0);
2724
0
    }
2725
250k
    *str = ptr;
2726
2727
    /*
2728
     * [ WFC: Legal Character ]
2729
     * Characters referred to using character references must match the
2730
     * production for Char.
2731
     */
2732
250k
    if (val >= 0x110000) {
2733
294
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2734
294
                "xmlParseStringCharRef: character reference out of bounds\n",
2735
294
                val);
2736
249k
    } else if (IS_CHAR(val)) {
2737
243k
        return(val);
2738
243k
    } else {
2739
6.74k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2740
6.74k
        "xmlParseStringCharRef: invalid xmlChar value %d\n",
2741
6.74k
        val);
2742
6.74k
    }
2743
7.04k
    return(0);
2744
250k
}
2745
2746
/**
2747
 *     [69] PEReference ::= '%' Name ';'
2748
 *
2749
 * @deprecated Internal function, do not use.
2750
 *
2751
 * [ WFC: No Recursion ]
2752
 * A parsed entity must not contain a recursive
2753
 * reference to itself, either directly or indirectly.
2754
 *
2755
 * [ WFC: Entity Declared ]
2756
 * In a document without any DTD, a document with only an internal DTD
2757
 * subset which contains no parameter entity references, or a document
2758
 * with "standalone='yes'", ...  ... The declaration of a parameter
2759
 * entity must precede any reference to it...
2760
 *
2761
 * [ VC: Entity Declared ]
2762
 * In a document with an external subset or external parameter entities
2763
 * with "standalone='no'", ...  ... The declaration of a parameter entity
2764
 * must precede any reference to it...
2765
 *
2766
 * [ WFC: In DTD ]
2767
 * Parameter-entity references may only appear in the DTD.
2768
 * NOTE: misleading but this is handled.
2769
 *
2770
 * A PEReference may have been detected in the current input stream
2771
 * the handling is done accordingly to
2772
 *      http://www.w3.org/TR/REC-xml#entproc
2773
 * i.e.
2774
 *   - Included in literal in entity values
2775
 *   - Included as Parameter Entity reference within DTDs
2776
 * @param ctxt  the parser context
2777
 */
2778
void
2779
0
xmlParserHandlePEReference(xmlParserCtxt *ctxt) {
2780
0
    xmlParsePERefInternal(ctxt, 0);
2781
0
}
2782
2783
/**
2784
 * @deprecated Internal function, don't use.
2785
 *
2786
 * @param ctxt  the parser context
2787
 * @param str  the input string
2788
 * @param len  the string length
2789
 * @param what  combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2790
 * @param end  an end marker xmlChar, 0 if none
2791
 * @param end2  an end marker xmlChar, 0 if none
2792
 * @param end3  an end marker xmlChar, 0 if none
2793
 * @returns A newly allocated string with the substitution done. The caller
2794
 *      must deallocate it !
2795
 */
2796
xmlChar *
2797
xmlStringLenDecodeEntities(xmlParserCtxt *ctxt, const xmlChar *str, int len,
2798
                           int what ATTRIBUTE_UNUSED,
2799
0
                           xmlChar end, xmlChar end2, xmlChar end3) {
2800
0
    if ((ctxt == NULL) || (str == NULL) || (len < 0))
2801
0
        return(NULL);
2802
2803
0
    if ((str[len] != 0) ||
2804
0
        (end != 0) || (end2 != 0) || (end3 != 0))
2805
0
        return(NULL);
2806
2807
0
    return(xmlExpandEntitiesInAttValue(ctxt, str, 0));
2808
0
}
2809
2810
/**
2811
 * @deprecated Internal function, don't use.
2812
 *
2813
 * @param ctxt  the parser context
2814
 * @param str  the input string
2815
 * @param what  combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2816
 * @param end  an end marker xmlChar, 0 if none
2817
 * @param end2  an end marker xmlChar, 0 if none
2818
 * @param end3  an end marker xmlChar, 0 if none
2819
 * @returns A newly allocated string with the substitution done. The caller
2820
 *      must deallocate it !
2821
 */
2822
xmlChar *
2823
xmlStringDecodeEntities(xmlParserCtxt *ctxt, const xmlChar *str,
2824
                        int what ATTRIBUTE_UNUSED,
2825
0
            xmlChar end, xmlChar  end2, xmlChar end3) {
2826
0
    if ((ctxt == NULL) || (str == NULL))
2827
0
        return(NULL);
2828
2829
0
    if ((end != 0) || (end2 != 0) || (end3 != 0))
2830
0
        return(NULL);
2831
2832
0
    return(xmlExpandEntitiesInAttValue(ctxt, str, 0));
2833
0
}
2834
2835
/************************************************************************
2836
 *                  *
2837
 *    Commodity functions, cleanup needed ?     *
2838
 *                  *
2839
 ************************************************************************/
2840
2841
/**
2842
 * Is this a sequence of blank chars that one can ignore ?
2843
 *
2844
 * @param ctxt  an XML parser context
2845
 * @param str  a xmlChar *
2846
 * @param len  the size of `str`
2847
 * @param blank_chars  we know the chars are blanks
2848
 * @returns 1 if ignorable 0 otherwise.
2849
 */
2850
2851
static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2852
0
                     int blank_chars) {
2853
0
    int i;
2854
0
    xmlNodePtr lastChild;
2855
2856
    /*
2857
     * Check for xml:space value.
2858
     */
2859
0
    if ((ctxt->space == NULL) || (*(ctxt->space) == 1) ||
2860
0
        (*(ctxt->space) == -2))
2861
0
  return(0);
2862
2863
    /*
2864
     * Check that the string is made of blanks
2865
     */
2866
0
    if (blank_chars == 0) {
2867
0
  for (i = 0;i < len;i++)
2868
0
      if (!(IS_BLANK_CH(str[i]))) return(0);
2869
0
    }
2870
2871
    /*
2872
     * Look if the element is mixed content in the DTD if available
2873
     */
2874
0
    if (ctxt->node == NULL) return(0);
2875
0
    if (ctxt->myDoc != NULL) {
2876
0
        xmlElementPtr elemDecl = NULL;
2877
0
        xmlDocPtr doc = ctxt->myDoc;
2878
0
        const xmlChar *prefix = NULL;
2879
2880
0
        if (ctxt->node->ns)
2881
0
            prefix = ctxt->node->ns->prefix;
2882
0
        if (doc->intSubset != NULL)
2883
0
            elemDecl = xmlHashLookup2(doc->intSubset->elements, ctxt->node->name,
2884
0
                                      prefix);
2885
0
        if ((elemDecl == NULL) && (doc->extSubset != NULL))
2886
0
            elemDecl = xmlHashLookup2(doc->extSubset->elements, ctxt->node->name,
2887
0
                                      prefix);
2888
0
        if (elemDecl != NULL) {
2889
0
            if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)
2890
0
                return(1);
2891
0
            if ((elemDecl->etype == XML_ELEMENT_TYPE_ANY) ||
2892
0
                (elemDecl->etype == XML_ELEMENT_TYPE_MIXED))
2893
0
                return(0);
2894
0
        }
2895
0
    }
2896
2897
    /*
2898
     * Otherwise, heuristic :-\
2899
     *
2900
     * When push parsing, we could be at the end of a chunk.
2901
     * This makes the look-ahead and consequently the NOBLANKS
2902
     * option unreliable.
2903
     */
2904
0
    if ((RAW != '<') && (RAW != 0xD)) return(0);
2905
0
    if ((ctxt->node->children == NULL) &&
2906
0
  (RAW == '<') && (NXT(1) == '/')) return(0);
2907
2908
0
    lastChild = xmlGetLastChild(ctxt->node);
2909
0
    if (lastChild == NULL) {
2910
0
        if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2911
0
            (ctxt->node->content != NULL)) return(0);
2912
0
    } else if (xmlNodeIsText(lastChild))
2913
0
        return(0);
2914
0
    else if ((ctxt->node->children != NULL) &&
2915
0
             (xmlNodeIsText(ctxt->node->children)))
2916
0
        return(0);
2917
0
    return(1);
2918
0
}
2919
2920
/************************************************************************
2921
 *                  *
2922
 *    Extra stuff for namespace support     *
2923
 *  Relates to http://www.w3.org/TR/WD-xml-names      *
2924
 *                  *
2925
 ************************************************************************/
2926
2927
/**
2928
 * Parse an UTF8 encoded XML qualified name string
2929
 *
2930
 * @deprecated Don't use.
2931
 *
2932
 * @param ctxt  an XML parser context
2933
 * @param name  an XML parser context
2934
 * @param prefixOut  a xmlChar **
2935
 * @returns the local part, and prefix is updated
2936
 *   to get the Prefix if any.
2937
 */
2938
2939
xmlChar *
2940
0
xmlSplitQName(xmlParserCtxt *ctxt, const xmlChar *name, xmlChar **prefixOut) {
2941
0
    xmlChar *ret;
2942
0
    const xmlChar *localname;
2943
2944
0
    localname = xmlSplitQName4(name, prefixOut);
2945
0
    if (localname == NULL) {
2946
0
        xmlCtxtErrMemory(ctxt);
2947
0
        return(NULL);
2948
0
    }
2949
2950
0
    ret = xmlStrdup(localname);
2951
0
    if (ret == NULL) {
2952
0
        xmlCtxtErrMemory(ctxt);
2953
0
        xmlFree(*prefixOut);
2954
0
    }
2955
2956
0
    return(ret);
2957
0
}
2958
2959
/************************************************************************
2960
 *                  *
2961
 *      The parser itself       *
2962
 *  Relates to http://www.w3.org/TR/REC-xml       *
2963
 *                  *
2964
 ************************************************************************/
2965
2966
/************************************************************************
2967
 *                  *
2968
 *  Routines to parse Name, NCName and NmToken      *
2969
 *                  *
2970
 ************************************************************************/
2971
2972
/*
2973
 * The two following functions are related to the change of accepted
2974
 * characters for Name and NmToken in the Revision 5 of XML-1.0
2975
 * They correspond to the modified production [4] and the new production [4a]
2976
 * changes in that revision. Also note that the macros used for the
2977
 * productions Letter, Digit, CombiningChar and Extender are not needed
2978
 * anymore.
2979
 * We still keep compatibility to pre-revision5 parsing semantic if the
2980
 * new XML_PARSE_OLD10 option is given to the parser.
2981
 */
2982
2983
static int
2984
4.75M
xmlIsNameStartCharNew(int c) {
2985
    /*
2986
     * Use the new checks of production [4] [4a] amd [5] of the
2987
     * Update 5 of XML-1.0
2988
     */
2989
4.75M
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
2990
4.74M
        (((c >= 'a') && (c <= 'z')) ||
2991
3.99M
         ((c >= 'A') && (c <= 'Z')) ||
2992
3.89M
         (c == '_') || (c == ':') ||
2993
3.00M
         ((c >= 0xC0) && (c <= 0xD6)) ||
2994
3.00M
         ((c >= 0xD8) && (c <= 0xF6)) ||
2995
2.99M
         ((c >= 0xF8) && (c <= 0x2FF)) ||
2996
2.98M
         ((c >= 0x370) && (c <= 0x37D)) ||
2997
2.98M
         ((c >= 0x37F) && (c <= 0x1FFF)) ||
2998
2.98M
         ((c >= 0x200C) && (c <= 0x200D)) ||
2999
2.98M
         ((c >= 0x2070) && (c <= 0x218F)) ||
3000
2.98M
         ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3001
2.98M
         ((c >= 0x3001) && (c <= 0xD7FF)) ||
3002
2.97M
         ((c >= 0xF900) && (c <= 0xFDCF)) ||
3003
2.97M
         ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3004
2.97M
         ((c >= 0x10000) && (c <= 0xEFFFF))))
3005
1.77M
        return(1);
3006
2.97M
    return(0);
3007
4.75M
}
3008
3009
static int
3010
36.2M
xmlIsNameCharNew(int c) {
3011
    /*
3012
     * Use the new checks of production [4] [4a] amd [5] of the
3013
     * Update 5 of XML-1.0
3014
     */
3015
36.2M
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3016
36.0M
        (((c >= 'a') && (c <= 'z')) ||
3017
25.1M
         ((c >= 'A') && (c <= 'Z')) ||
3018
23.0M
         ((c >= '0') && (c <= '9')) || /* !start */
3019
21.8M
         (c == '_') || (c == ':') ||
3020
18.9M
         (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3021
18.3M
         ((c >= 0xC0) && (c <= 0xD6)) ||
3022
18.3M
         ((c >= 0xD8) && (c <= 0xF6)) ||
3023
18.0M
         ((c >= 0xF8) && (c <= 0x2FF)) ||
3024
16.6M
         ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3025
16.6M
         ((c >= 0x370) && (c <= 0x37D)) ||
3026
16.6M
         ((c >= 0x37F) && (c <= 0x1FFF)) ||
3027
13.7M
         ((c >= 0x200C) && (c <= 0x200D)) ||
3028
13.7M
         ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3029
13.7M
         ((c >= 0x2070) && (c <= 0x218F)) ||
3030
13.7M
         ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3031
13.6M
         ((c >= 0x3001) && (c <= 0xD7FF)) ||
3032
10.4M
         ((c >= 0xF900) && (c <= 0xFDCF)) ||
3033
10.3M
         ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3034
1.65M
         ((c >= 0x10000) && (c <= 0xEFFFF))))
3035
34.4M
         return(1);
3036
1.79M
    return(0);
3037
36.2M
}
3038
3039
static int
3040
0
xmlIsNameStartCharOld(int c) {
3041
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3042
0
        ((IS_LETTER(c) || (c == '_') || (c == ':'))))
3043
0
        return(1);
3044
0
    return(0);
3045
0
}
3046
3047
static int
3048
0
xmlIsNameCharOld(int c) {
3049
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3050
0
        ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3051
0
         (c == '.') || (c == '-') ||
3052
0
         (c == '_') || (c == ':') ||
3053
0
         (IS_COMBINING(c)) ||
3054
0
         (IS_EXTENDER(c))))
3055
0
        return(1);
3056
0
    return(0);
3057
0
}
3058
3059
static int
3060
4.75M
xmlIsNameStartChar(int c, int old10) {
3061
4.75M
    if (!old10)
3062
4.75M
        return(xmlIsNameStartCharNew(c));
3063
0
    else
3064
0
        return(xmlIsNameStartCharOld(c));
3065
4.75M
}
3066
3067
static int
3068
36.2M
xmlIsNameChar(int c, int old10) {
3069
36.2M
    if (!old10)
3070
36.2M
        return(xmlIsNameCharNew(c));
3071
0
    else
3072
0
        return(xmlIsNameCharOld(c));
3073
36.2M
}
3074
3075
/*
3076
 * Scan an XML Name, NCName or Nmtoken.
3077
 *
3078
 * Returns a pointer to the end of the name on success. If the
3079
 * name is invalid, returns `ptr`. If the name is longer than
3080
 * `maxSize` bytes, returns NULL.
3081
 *
3082
 * @param ptr  pointer to the start of the name
3083
 * @param maxSize  maximum size in bytes
3084
 * @param flags  XML_SCAN_* flags
3085
 * @returns a pointer to the end of the name or NULL
3086
 */
3087
const xmlChar *
3088
1.48M
xmlScanName(const xmlChar *ptr, size_t maxSize, int flags) {
3089
1.48M
    int stop = flags & XML_SCAN_NC ? ':' : 0;
3090
1.48M
    int old10 = flags & XML_SCAN_OLD10 ? 1 : 0;
3091
3092
11.4M
    while (1) {
3093
11.4M
        int c, len;
3094
3095
11.4M
        c = *ptr;
3096
11.4M
        if (c < 0x80) {
3097
10.1M
            if (c == stop)
3098
18.4k
                break;
3099
10.1M
            len = 1;
3100
10.1M
        } else {
3101
1.28M
            len = 4;
3102
1.28M
            c = xmlGetUTF8Char(ptr, &len);
3103
1.28M
            if (c < 0)
3104
1.67k
                break;
3105
1.28M
        }
3106
3107
11.4M
        if (flags & XML_SCAN_NMTOKEN ?
3108
9.97M
                !xmlIsNameChar(c, old10) :
3109
11.4M
                !xmlIsNameStartChar(c, old10))
3110
1.46M
            break;
3111
3112
9.99M
        if ((size_t) len > maxSize)
3113
22
            return(NULL);
3114
9.99M
        ptr += len;
3115
9.99M
        maxSize -= len;
3116
9.99M
        flags |= XML_SCAN_NMTOKEN;
3117
9.99M
    }
3118
3119
1.48M
    return(ptr);
3120
1.48M
}
3121
3122
static const xmlChar *
3123
138k
xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
3124
138k
    const xmlChar *ret;
3125
138k
    int len = 0, l;
3126
138k
    int c;
3127
138k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3128
74.3k
                    XML_MAX_TEXT_LENGTH :
3129
138k
                    XML_MAX_NAME_LENGTH;
3130
138k
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3131
3132
    /*
3133
     * Handler for more complex cases
3134
     */
3135
138k
    c = xmlCurrentChar(ctxt, &l);
3136
138k
    if (!xmlIsNameStartChar(c, old10))
3137
106k
        return(NULL);
3138
32.4k
    len += l;
3139
32.4k
    NEXTL(l);
3140
32.4k
    c = xmlCurrentChar(ctxt, &l);
3141
6.03M
    while (xmlIsNameChar(c, old10)) {
3142
6.00M
        if (len <= INT_MAX - l)
3143
6.00M
            len += l;
3144
6.00M
        NEXTL(l);
3145
6.00M
        c = xmlCurrentChar(ctxt, &l);
3146
6.00M
    }
3147
32.4k
    if (len > maxLength) {
3148
49
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
3149
49
        return(NULL);
3150
49
    }
3151
32.4k
    if (ctxt->input->cur - ctxt->input->base < len) {
3152
        /*
3153
         * There were a couple of bugs where PERefs lead to to a change
3154
         * of the buffer. Check the buffer size to avoid passing an invalid
3155
         * pointer to xmlDictLookup.
3156
         */
3157
0
        xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
3158
0
                    "unexpected change of input buffer");
3159
0
        return (NULL);
3160
0
    }
3161
32.4k
    if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r'))
3162
390
        ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len);
3163
32.0k
    else
3164
32.0k
        ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len);
3165
32.4k
    if (ret == NULL)
3166
0
        xmlErrMemory(ctxt);
3167
32.4k
    return(ret);
3168
32.4k
}
3169
3170
/**
3171
 * Parse an XML name.
3172
 *
3173
 * @deprecated Internal function, don't use.
3174
 *
3175
 *     [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
3176
 *                      CombiningChar | Extender
3177
 *
3178
 *     [5] Name ::= (Letter | '_' | ':') (NameChar)*
3179
 *
3180
 *     [6] Names ::= Name (#x20 Name)*
3181
 *
3182
 * @param ctxt  an XML parser context
3183
 * @returns the Name parsed or NULL
3184
 */
3185
3186
const xmlChar *
3187
23.3M
xmlParseName(xmlParserCtxt *ctxt) {
3188
23.3M
    const xmlChar *in;
3189
23.3M
    const xmlChar *ret;
3190
23.3M
    size_t count = 0;
3191
23.3M
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3192
22.8M
                       XML_MAX_TEXT_LENGTH :
3193
23.3M
                       XML_MAX_NAME_LENGTH;
3194
3195
23.3M
    GROW;
3196
3197
    /*
3198
     * Accelerator for simple ASCII names
3199
     */
3200
23.3M
    in = ctxt->input->cur;
3201
23.3M
    if (((*in >= 0x61) && (*in <= 0x7A)) ||
3202
888k
  ((*in >= 0x41) && (*in <= 0x5A)) ||
3203
23.2M
  (*in == '_') || (*in == ':')) {
3204
23.2M
  in++;
3205
48.4M
  while (((*in >= 0x61) && (*in <= 0x7A)) ||
3206
24.6M
         ((*in >= 0x41) && (*in <= 0x5A)) ||
3207
23.7M
         ((*in >= 0x30) && (*in <= 0x39)) ||
3208
23.3M
         (*in == '_') || (*in == '-') ||
3209
23.2M
         (*in == ':') || (*in == '.'))
3210
25.2M
      in++;
3211
23.2M
  if ((*in > 0) && (*in < 0x80)) {
3212
23.2M
      count = in - ctxt->input->cur;
3213
23.2M
            if (count > maxLength) {
3214
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
3215
0
                return(NULL);
3216
0
            }
3217
23.2M
      ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
3218
23.2M
      ctxt->input->cur = in;
3219
23.2M
      ctxt->input->col += count;
3220
23.2M
      if (ret == NULL)
3221
0
          xmlErrMemory(ctxt);
3222
23.2M
      return(ret);
3223
23.2M
  }
3224
23.2M
    }
3225
    /* accelerator for special cases */
3226
138k
    return(xmlParseNameComplex(ctxt));
3227
23.3M
}
3228
3229
static xmlHashedString
3230
3.14M
xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
3231
3.14M
    xmlHashedString ret;
3232
3.14M
    int len = 0, l;
3233
3.14M
    int c;
3234
3.14M
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3235
3.04M
                    XML_MAX_TEXT_LENGTH :
3236
3.14M
                    XML_MAX_NAME_LENGTH;
3237
3.14M
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3238
3.14M
    size_t startPosition = 0;
3239
3240
3.14M
    ret.name = NULL;
3241
3.14M
    ret.hashValue = 0;
3242
3243
    /*
3244
     * Handler for more complex cases
3245
     */
3246
3.14M
    startPosition = CUR_PTR - BASE_PTR;
3247
3.14M
    c = xmlCurrentChar(ctxt, &l);
3248
3.14M
    if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
3249
3.13M
  (!xmlIsNameStartChar(c, old10) || (c == ':'))) {
3250
3.09M
  return(ret);
3251
3.09M
    }
3252
3253
12.0M
    while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
3254
12.0M
     (xmlIsNameChar(c, old10) && (c != ':'))) {
3255
11.9M
        if (len <= INT_MAX - l)
3256
11.9M
      len += l;
3257
11.9M
  NEXTL(l);
3258
11.9M
  c = xmlCurrentChar(ctxt, &l);
3259
11.9M
    }
3260
53.3k
    if (len > maxLength) {
3261
49
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3262
49
        return(ret);
3263
49
    }
3264
53.3k
    ret = xmlDictLookupHashed(ctxt->dict, (BASE_PTR + startPosition), len);
3265
53.3k
    if (ret.name == NULL)
3266
0
        xmlErrMemory(ctxt);
3267
53.3k
    return(ret);
3268
53.3k
}
3269
3270
/**
3271
 * Parse an XML name.
3272
 *
3273
 *     [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
3274
 *                          CombiningChar | Extender
3275
 *
3276
 *     [5NS] NCName ::= (Letter | '_') (NCNameChar)*
3277
 *
3278
 * @param ctxt  an XML parser context
3279
 * @returns the Name parsed or NULL
3280
 */
3281
3282
static xmlHashedString
3283
8.18M
xmlParseNCName(xmlParserCtxtPtr ctxt) {
3284
8.18M
    const xmlChar *in, *e;
3285
8.18M
    xmlHashedString ret;
3286
8.18M
    size_t count = 0;
3287
8.18M
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3288
7.36M
                       XML_MAX_TEXT_LENGTH :
3289
8.18M
                       XML_MAX_NAME_LENGTH;
3290
3291
8.18M
    ret.name = NULL;
3292
3293
    /*
3294
     * Accelerator for simple ASCII names
3295
     */
3296
8.18M
    in = ctxt->input->cur;
3297
8.18M
    e = ctxt->input->end;
3298
8.18M
    if ((((*in >= 0x61) && (*in <= 0x7A)) ||
3299
6.40M
   ((*in >= 0x41) && (*in <= 0x5A)) ||
3300
5.06M
   (*in == '_')) && (in < e)) {
3301
5.06M
  in++;
3302
38.7M
  while ((((*in >= 0x61) && (*in <= 0x7A)) ||
3303
9.68M
          ((*in >= 0x41) && (*in <= 0x5A)) ||
3304
5.31M
          ((*in >= 0x30) && (*in <= 0x39)) ||
3305
5.19M
          (*in == '_') || (*in == '-') ||
3306
33.6M
          (*in == '.')) && (in < e))
3307
33.6M
      in++;
3308
5.06M
  if (in >= e)
3309
2.71k
      goto complex;
3310
5.06M
  if ((*in > 0) && (*in < 0x80)) {
3311
5.03M
      count = in - ctxt->input->cur;
3312
5.03M
            if (count > maxLength) {
3313
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3314
0
                return(ret);
3315
0
            }
3316
5.03M
      ret = xmlDictLookupHashed(ctxt->dict, ctxt->input->cur, count);
3317
5.03M
      ctxt->input->cur = in;
3318
5.03M
      ctxt->input->col += count;
3319
5.03M
      if (ret.name == NULL) {
3320
0
          xmlErrMemory(ctxt);
3321
0
      }
3322
5.03M
      return(ret);
3323
5.03M
  }
3324
5.06M
    }
3325
3.14M
complex:
3326
3.14M
    return(xmlParseNCNameComplex(ctxt));
3327
8.18M
}
3328
3329
/**
3330
 * Parse an XML name and compares for match
3331
 * (specialized for endtag parsing)
3332
 *
3333
 * @param ctxt  an XML parser context
3334
 * @param other  the name to compare with
3335
 * @returns NULL for an illegal name, (xmlChar*) 1 for success
3336
 * and the name for mismatch
3337
 */
3338
3339
static const xmlChar *
3340
1.29M
xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
3341
1.29M
    register const xmlChar *cmp = other;
3342
1.29M
    register const xmlChar *in;
3343
1.29M
    const xmlChar *ret;
3344
3345
1.29M
    GROW;
3346
3347
1.29M
    in = ctxt->input->cur;
3348
19.2M
    while (*in != 0 && *in == *cmp) {
3349
17.9M
  ++in;
3350
17.9M
  ++cmp;
3351
17.9M
    }
3352
1.29M
    if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
3353
  /* success */
3354
1.28M
  ctxt->input->col += in - ctxt->input->cur;
3355
1.28M
  ctxt->input->cur = in;
3356
1.28M
  return (const xmlChar*) 1;
3357
1.28M
    }
3358
    /* failure (or end of input buffer), check with full function */
3359
12.8k
    ret = xmlParseName (ctxt);
3360
    /* strings coming from the dictionary direct compare possible */
3361
12.8k
    if (ret == other) {
3362
1.77k
  return (const xmlChar*) 1;
3363
1.77k
    }
3364
11.1k
    return ret;
3365
12.8k
}
3366
3367
/**
3368
 * Parse an XML name.
3369
 *
3370
 * @param ctxt  an XML parser context
3371
 * @param str  a pointer to the string pointer (IN/OUT)
3372
 * @returns the Name parsed or NULL. The `str` pointer
3373
 * is updated to the current location in the string.
3374
 */
3375
3376
static xmlChar *
3377
898k
xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
3378
898k
    xmlChar *ret;
3379
898k
    const xmlChar *cur = *str;
3380
898k
    int flags = 0;
3381
898k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3382
775k
                    XML_MAX_TEXT_LENGTH :
3383
898k
                    XML_MAX_NAME_LENGTH;
3384
3385
898k
    if (ctxt->options & XML_PARSE_OLD10)
3386
0
        flags |= XML_SCAN_OLD10;
3387
3388
898k
    cur = xmlScanName(*str, maxLength, flags);
3389
898k
    if (cur == NULL) {
3390
20
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3391
20
        return(NULL);
3392
20
    }
3393
898k
    if (cur == *str)
3394
3.23k
        return(NULL);
3395
3396
895k
    ret = xmlStrndup(*str, cur - *str);
3397
895k
    if (ret == NULL)
3398
0
        xmlErrMemory(ctxt);
3399
895k
    *str = cur;
3400
895k
    return(ret);
3401
898k
}
3402
3403
/**
3404
 * Parse an XML Nmtoken.
3405
 *
3406
 * @deprecated Internal function, don't use.
3407
 *
3408
 *     [7] Nmtoken ::= (NameChar)+
3409
 *
3410
 *     [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
3411
 *
3412
 * @param ctxt  an XML parser context
3413
 * @returns the Nmtoken parsed or NULL
3414
 */
3415
3416
xmlChar *
3417
431k
xmlParseNmtoken(xmlParserCtxt *ctxt) {
3418
431k
    xmlChar buf[XML_MAX_NAMELEN + 5];
3419
431k
    xmlChar *ret;
3420
431k
    int len = 0, l;
3421
431k
    int c;
3422
431k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3423
411k
                    XML_MAX_TEXT_LENGTH :
3424
431k
                    XML_MAX_NAME_LENGTH;
3425
431k
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3426
3427
431k
    c = xmlCurrentChar(ctxt, &l);
3428
3429
2.58M
    while (xmlIsNameChar(c, old10)) {
3430
2.15M
  COPY_BUF(buf, len, c);
3431
2.15M
  NEXTL(l);
3432
2.15M
  c = xmlCurrentChar(ctxt, &l);
3433
2.15M
  if (len >= XML_MAX_NAMELEN) {
3434
      /*
3435
       * Okay someone managed to make a huge token, so he's ready to pay
3436
       * for the processing speed.
3437
       */
3438
9.40k
      xmlChar *buffer;
3439
9.40k
      int max = len * 2;
3440
3441
9.40k
      buffer = xmlMalloc(max);
3442
9.40k
      if (buffer == NULL) {
3443
0
          xmlErrMemory(ctxt);
3444
0
    return(NULL);
3445
0
      }
3446
9.40k
      memcpy(buffer, buf, len);
3447
5.59M
      while (xmlIsNameChar(c, old10)) {
3448
5.58M
    if (len + 10 > max) {
3449
18.2k
        xmlChar *tmp;
3450
18.2k
                    int newSize;
3451
3452
18.2k
                    newSize = xmlGrowCapacity(max, 1, 1, maxLength);
3453
18.2k
                    if (newSize < 0) {
3454
47
                        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
3455
47
                        xmlFree(buffer);
3456
47
                        return(NULL);
3457
47
                    }
3458
18.2k
        tmp = xmlRealloc(buffer, newSize);
3459
18.2k
        if (tmp == NULL) {
3460
0
      xmlErrMemory(ctxt);
3461
0
      xmlFree(buffer);
3462
0
      return(NULL);
3463
0
        }
3464
18.2k
        buffer = tmp;
3465
18.2k
                    max = newSize;
3466
18.2k
    }
3467
5.58M
    COPY_BUF(buffer, len, c);
3468
5.58M
    NEXTL(l);
3469
5.58M
    c = xmlCurrentChar(ctxt, &l);
3470
5.58M
      }
3471
9.36k
      buffer[len] = 0;
3472
9.36k
      return(buffer);
3473
9.40k
  }
3474
2.15M
    }
3475
421k
    if (len == 0)
3476
20.4k
        return(NULL);
3477
401k
    if (len > maxLength) {
3478
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
3479
0
        return(NULL);
3480
0
    }
3481
401k
    ret = xmlStrndup(buf, len);
3482
401k
    if (ret == NULL)
3483
0
        xmlErrMemory(ctxt);
3484
401k
    return(ret);
3485
401k
}
3486
3487
/**
3488
 * Validate an entity value and expand parameter entities.
3489
 *
3490
 * @param ctxt  parser context
3491
 * @param buf  string buffer
3492
 * @param str  entity value
3493
 * @param length  size of entity value
3494
 * @param depth  nesting depth
3495
 */
3496
static void
3497
xmlExpandPEsInEntityValue(xmlParserCtxtPtr ctxt, xmlSBuf *buf,
3498
38.7k
                          const xmlChar *str, int length, int depth) {
3499
38.7k
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3500
38.7k
    const xmlChar *end, *chunk;
3501
38.7k
    int c, l;
3502
3503
38.7k
    if (str == NULL)
3504
0
        return;
3505
3506
38.7k
    depth += 1;
3507
38.7k
    if (depth > maxDepth) {
3508
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3509
0
                       "Maximum entity nesting depth exceeded");
3510
0
  return;
3511
0
    }
3512
3513
38.7k
    end = str + length;
3514
38.7k
    chunk = str;
3515
3516
202M
    while ((str < end) && (!PARSER_STOPPED(ctxt))) {
3517
202M
        c = *str;
3518
3519
202M
        if (c >= 0x80) {
3520
176M
            l = xmlUTF8MultibyteLen(ctxt, str,
3521
176M
                    "invalid character in entity value\n");
3522
176M
            if (l == 0) {
3523
25.6M
                if (chunk < str)
3524
339k
                    xmlSBufAddString(buf, chunk, str - chunk);
3525
25.6M
                xmlSBufAddReplChar(buf);
3526
25.6M
                str += 1;
3527
25.6M
                chunk = str;
3528
151M
            } else {
3529
151M
                str += l;
3530
151M
            }
3531
176M
        } else if (c == '&') {
3532
87.0k
            if (str[1] == '#') {
3533
29.3k
                if (chunk < str)
3534
20.0k
                    xmlSBufAddString(buf, chunk, str - chunk);
3535
3536
29.3k
                c = xmlParseStringCharRef(ctxt, &str);
3537
29.3k
                if (c == 0)
3538
7.02k
                    return;
3539
3540
22.2k
                xmlSBufAddChar(buf, c);
3541
3542
22.2k
                chunk = str;
3543
57.7k
            } else {
3544
57.7k
                xmlChar *name;
3545
3546
                /*
3547
                 * General entity references are checked for
3548
                 * syntactic validity.
3549
                 */
3550
57.7k
                str++;
3551
57.7k
                name = xmlParseStringName(ctxt, &str);
3552
3553
57.7k
                if ((name == NULL) || (*str++ != ';')) {
3554
2.18k
                    xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
3555
2.18k
                            "EntityValue: '&' forbidden except for entities "
3556
2.18k
                            "references\n");
3557
2.18k
                    xmlFree(name);
3558
2.18k
                    return;
3559
2.18k
                }
3560
3561
55.5k
                xmlFree(name);
3562
55.5k
            }
3563
25.2M
        } else if (c == '%') {
3564
5.88k
            xmlEntityPtr ent;
3565
3566
5.88k
            if (chunk < str)
3567
3.79k
                xmlSBufAddString(buf, chunk, str - chunk);
3568
3569
5.88k
            ent = xmlParseStringPEReference(ctxt, &str);
3570
5.88k
            if (ent == NULL)
3571
5.26k
                return;
3572
3573
628
            if (!PARSER_EXTERNAL(ctxt)) {
3574
628
                xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
3575
628
                return;
3576
628
            }
3577
3578
0
            if (ent->content == NULL) {
3579
                /*
3580
                 * Note: external parsed entities will not be loaded,
3581
                 * it is not required for a non-validating parser to
3582
                 * complete external PEReferences coming from the
3583
                 * internal subset
3584
                 */
3585
0
                if (((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
3586
0
                    ((ctxt->replaceEntities) ||
3587
0
                     (ctxt->validate))) {
3588
0
                    xmlLoadEntityContent(ctxt, ent);
3589
0
                } else {
3590
0
                    xmlWarningMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
3591
0
                                  "not validating will not read content for "
3592
0
                                  "PE entity %s\n", ent->name, NULL);
3593
0
                }
3594
0
            }
3595
3596
            /*
3597
             * TODO: Skip if ent->content is still NULL.
3598
             */
3599
3600
0
            if (xmlParserEntityCheck(ctxt, ent->length))
3601
0
                return;
3602
3603
0
            if (ent->flags & XML_ENT_EXPANDING) {
3604
0
                xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3605
0
                return;
3606
0
            }
3607
3608
0
            ent->flags |= XML_ENT_EXPANDING;
3609
0
            xmlExpandPEsInEntityValue(ctxt, buf, ent->content, ent->length,
3610
0
                                      depth);
3611
0
            ent->flags &= ~XML_ENT_EXPANDING;
3612
3613
0
            chunk = str;
3614
25.2M
        } else {
3615
            /* Normal ASCII char */
3616
25.2M
            if (!IS_BYTE_CHAR(c)) {
3617
3.26M
                xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
3618
3.26M
                        "invalid character in entity value\n");
3619
3.26M
                if (chunk < str)
3620
111k
                    xmlSBufAddString(buf, chunk, str - chunk);
3621
3.26M
                xmlSBufAddReplChar(buf);
3622
3.26M
                str += 1;
3623
3.26M
                chunk = str;
3624
21.9M
            } else {
3625
21.9M
                str += 1;
3626
21.9M
            }
3627
25.2M
        }
3628
202M
    }
3629
3630
23.6k
    if (chunk < str)
3631
18.2k
        xmlSBufAddString(buf, chunk, str - chunk);
3632
23.6k
}
3633
3634
/**
3635
 * Parse a value for ENTITY declarations
3636
 *
3637
 * @deprecated Internal function, don't use.
3638
 *
3639
 *     [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
3640
 *                         "'" ([^%&'] | PEReference | Reference)* "'"
3641
 *
3642
 * @param ctxt  an XML parser context
3643
 * @param orig  if non-NULL store a copy of the original entity value
3644
 * @returns the EntityValue parsed with reference substituted or NULL
3645
 */
3646
xmlChar *
3647
39.9k
xmlParseEntityValue(xmlParserCtxt *ctxt, xmlChar **orig) {
3648
39.9k
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3649
21.2k
                         XML_MAX_HUGE_LENGTH :
3650
39.9k
                         XML_MAX_TEXT_LENGTH;
3651
39.9k
    xmlSBuf buf;
3652
39.9k
    const xmlChar *start;
3653
39.9k
    int quote, length;
3654
3655
39.9k
    xmlSBufInit(&buf, maxLength);
3656
3657
39.9k
    GROW;
3658
3659
39.9k
    quote = CUR;
3660
39.9k
    if ((quote != '"') && (quote != '\'')) {
3661
0
  xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
3662
0
  return(NULL);
3663
0
    }
3664
39.9k
    CUR_PTR++;
3665
3666
39.9k
    length = 0;
3667
3668
    /*
3669
     * Copy raw content of the entity into a buffer
3670
     */
3671
391M
    while (1) {
3672
391M
        int c;
3673
3674
391M
        if (PARSER_STOPPED(ctxt))
3675
1
            goto error;
3676
3677
391M
        if (CUR_PTR >= ctxt->input->end) {
3678
1.17k
            xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
3679
1.17k
            goto error;
3680
1.17k
        }
3681
3682
391M
        c = CUR;
3683
3684
391M
        if (c == 0) {
3685
21
            xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
3686
21
                    "invalid character in entity value\n");
3687
21
            goto error;
3688
21
        }
3689
391M
        if (c == quote)
3690
38.7k
            break;
3691
391M
        NEXTL(1);
3692
391M
        length += 1;
3693
3694
        /*
3695
         * TODO: Check growth threshold
3696
         */
3697
391M
        if (ctxt->input->end - CUR_PTR < 10)
3698
103k
            GROW;
3699
391M
    }
3700
3701
38.7k
    start = CUR_PTR - length;
3702
3703
38.7k
    if (orig != NULL) {
3704
38.7k
        *orig = xmlStrndup(start, length);
3705
38.7k
        if (*orig == NULL)
3706
0
            xmlErrMemory(ctxt);
3707
38.7k
    }
3708
3709
38.7k
    xmlExpandPEsInEntityValue(ctxt, &buf, start, length, ctxt->inputNr);
3710
3711
38.7k
    NEXTL(1);
3712
3713
38.7k
    return(xmlSBufFinish(&buf, NULL, ctxt, "entity length too long"));
3714
3715
1.19k
error:
3716
1.19k
    xmlSBufCleanup(&buf, ctxt, "entity length too long");
3717
1.19k
    return(NULL);
3718
39.9k
}
3719
3720
/**
3721
 * Check an entity reference in an attribute value for validity
3722
 * without expanding it.
3723
 *
3724
 * @param ctxt  parser context
3725
 * @param pent  entity
3726
 * @param depth  nesting depth
3727
 */
3728
static void
3729
0
xmlCheckEntityInAttValue(xmlParserCtxtPtr ctxt, xmlEntityPtr pent, int depth) {
3730
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3731
0
    const xmlChar *str;
3732
0
    unsigned long expandedSize = pent->length;
3733
0
    int c, flags;
3734
3735
0
    depth += 1;
3736
0
    if (depth > maxDepth) {
3737
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3738
0
                       "Maximum entity nesting depth exceeded");
3739
0
  return;
3740
0
    }
3741
3742
0
    if (pent->flags & XML_ENT_EXPANDING) {
3743
0
        xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3744
0
        return;
3745
0
    }
3746
3747
    /*
3748
     * If we're parsing a default attribute value in DTD content,
3749
     * the entity might reference other entities which weren't
3750
     * defined yet, so the check isn't reliable.
3751
     */
3752
0
    if (ctxt->inSubset == 0)
3753
0
        flags = XML_ENT_CHECKED | XML_ENT_VALIDATED;
3754
0
    else
3755
0
        flags = XML_ENT_VALIDATED;
3756
3757
0
    str = pent->content;
3758
0
    if (str == NULL)
3759
0
        goto done;
3760
3761
    /*
3762
     * Note that entity values are already validated. We only check
3763
     * for illegal less-than signs and compute the expanded size
3764
     * of the entity. No special handling for multi-byte characters
3765
     * is needed.
3766
     */
3767
0
    while (!PARSER_STOPPED(ctxt)) {
3768
0
        c = *str;
3769
3770
0
  if (c != '&') {
3771
0
            if (c == 0)
3772
0
                break;
3773
3774
0
            if (c == '<')
3775
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
3776
0
                        "'<' in entity '%s' is not allowed in attributes "
3777
0
                        "values\n", pent->name);
3778
3779
0
            str += 1;
3780
0
        } else if (str[1] == '#') {
3781
0
            int val;
3782
3783
0
      val = xmlParseStringCharRef(ctxt, &str);
3784
0
      if (val == 0) {
3785
0
                pent->content[0] = 0;
3786
0
                break;
3787
0
            }
3788
0
  } else {
3789
0
            xmlChar *name;
3790
0
            xmlEntityPtr ent;
3791
3792
0
      name = xmlParseStringEntityRef(ctxt, &str);
3793
0
      if (name == NULL) {
3794
0
                pent->content[0] = 0;
3795
0
                break;
3796
0
            }
3797
3798
0
            ent = xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 1);
3799
0
            xmlFree(name);
3800
3801
0
            if ((ent != NULL) &&
3802
0
                (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
3803
0
                if ((ent->flags & flags) != flags) {
3804
0
                    pent->flags |= XML_ENT_EXPANDING;
3805
0
                    xmlCheckEntityInAttValue(ctxt, ent, depth);
3806
0
                    pent->flags &= ~XML_ENT_EXPANDING;
3807
0
                }
3808
3809
0
                xmlSaturatedAdd(&expandedSize, ent->expandedSize);
3810
0
                xmlSaturatedAdd(&expandedSize, XML_ENT_FIXED_COST);
3811
0
            }
3812
0
        }
3813
0
    }
3814
3815
0
done:
3816
0
    if (ctxt->inSubset == 0)
3817
0
        pent->expandedSize = expandedSize;
3818
3819
0
    pent->flags |= flags;
3820
0
}
3821
3822
/**
3823
 * Expand general entity references in an entity or attribute value.
3824
 * Perform attribute value normalization.
3825
 *
3826
 * @param ctxt  parser context
3827
 * @param buf  string buffer
3828
 * @param str  entity or attribute value
3829
 * @param pent  entity for entity value, NULL for attribute values
3830
 * @param normalize  whether to collapse whitespace
3831
 * @param inSpace  whitespace state
3832
 * @param depth  nesting depth
3833
 * @param check  whether to check for amplification
3834
 * @returns  whether there was a normalization change
3835
 */
3836
static int
3837
xmlExpandEntityInAttValue(xmlParserCtxtPtr ctxt, xmlSBuf *buf,
3838
                          const xmlChar *str, xmlEntityPtr pent, int normalize,
3839
801k
                          int *inSpace, int depth, int check) {
3840
801k
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3841
801k
    int c, chunkSize;
3842
801k
    int normChange = 0;
3843
3844
801k
    if (str == NULL)
3845
0
        return(0);
3846
3847
801k
    depth += 1;
3848
801k
    if (depth > maxDepth) {
3849
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3850
0
                       "Maximum entity nesting depth exceeded");
3851
0
  return(0);
3852
0
    }
3853
3854
801k
    if (pent != NULL) {
3855
801k
        if (pent->flags & XML_ENT_EXPANDING) {
3856
28
            xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3857
28
            return(0);
3858
28
        }
3859
3860
801k
        if (check) {
3861
801k
            if (xmlParserEntityCheck(ctxt, pent->length))
3862
305
                return(0);
3863
801k
        }
3864
801k
    }
3865
3866
801k
    chunkSize = 0;
3867
3868
    /*
3869
     * Note that entity values are already validated. No special
3870
     * handling for multi-byte characters is needed.
3871
     */
3872
1.97G
    while (!PARSER_STOPPED(ctxt)) {
3873
1.97G
        c = *str;
3874
3875
1.97G
  if (c != '&') {
3876
1.97G
            if (c == 0)
3877
728k
                break;
3878
3879
            /*
3880
             * If this function is called without an entity, it is used to
3881
             * expand entities in an attribute content where less-than was
3882
             * already unscaped and is allowed.
3883
             */
3884
1.97G
            if ((pent != NULL) && (c == '<')) {
3885
72.5k
                xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
3886
72.5k
                        "'<' in entity '%s' is not allowed in attributes "
3887
72.5k
                        "values\n", pent->name);
3888
72.5k
                break;
3889
72.5k
            }
3890
3891
1.97G
            if (c <= 0x20) {
3892
16.5M
                if ((normalize) && (*inSpace)) {
3893
                    /* Skip char */
3894
1.11M
                    if (chunkSize > 0) {
3895
133k
                        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3896
133k
                        chunkSize = 0;
3897
133k
                    }
3898
1.11M
                    normChange = 1;
3899
15.4M
                } else if (c < 0x20) {
3900
6.84M
                    if (chunkSize > 0) {
3901
621k
                        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3902
621k
                        chunkSize = 0;
3903
621k
                    }
3904
3905
6.84M
                    xmlSBufAddCString(buf, " ", 1);
3906
8.58M
                } else {
3907
8.58M
                    chunkSize += 1;
3908
8.58M
                }
3909
3910
16.5M
                *inSpace = 1;
3911
1.95G
            } else {
3912
1.95G
                chunkSize += 1;
3913
1.95G
                *inSpace = 0;
3914
1.95G
            }
3915
3916
1.97G
            str += 1;
3917
1.97G
        } else if (str[1] == '#') {
3918
220k
            int val;
3919
3920
220k
            if (chunkSize > 0) {
3921
217k
                xmlSBufAddString(buf, str - chunkSize, chunkSize);
3922
217k
                chunkSize = 0;
3923
217k
            }
3924
3925
220k
      val = xmlParseStringCharRef(ctxt, &str);
3926
220k
      if (val == 0) {
3927
19
                if (pent != NULL)
3928
19
                    pent->content[0] = 0;
3929
19
                break;
3930
19
            }
3931
3932
220k
            if (val == ' ') {
3933
76.3k
                if ((normalize) && (*inSpace))
3934
196
                    normChange = 1;
3935
76.1k
                else
3936
76.1k
                    xmlSBufAddCString(buf, " ", 1);
3937
76.3k
                *inSpace = 1;
3938
144k
            } else {
3939
144k
                xmlSBufAddChar(buf, val);
3940
144k
                *inSpace = 0;
3941
144k
            }
3942
834k
  } else {
3943
834k
            xmlChar *name;
3944
834k
            xmlEntityPtr ent;
3945
3946
834k
            if (chunkSize > 0) {
3947
334k
                xmlSBufAddString(buf, str - chunkSize, chunkSize);
3948
334k
                chunkSize = 0;
3949
334k
            }
3950
3951
834k
      name = xmlParseStringEntityRef(ctxt, &str);
3952
834k
            if (name == NULL) {
3953
20
                if (pent != NULL)
3954
20
                    pent->content[0] = 0;
3955
20
                break;
3956
20
            }
3957
3958
834k
            ent = xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 1);
3959
834k
            xmlFree(name);
3960
3961
834k
      if ((ent != NULL) &&
3962
788k
    (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
3963
115k
    if (ent->content == NULL) {
3964
0
        xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
3965
0
          "predefined entity has no content\n");
3966
0
                    break;
3967
0
                }
3968
3969
115k
                xmlSBufAddString(buf, ent->content, ent->length);
3970
3971
115k
                *inSpace = 0;
3972
719k
      } else if ((ent != NULL) && (ent->content != NULL)) {
3973
673k
                if (pent != NULL)
3974
673k
                    pent->flags |= XML_ENT_EXPANDING;
3975
673k
    normChange |= xmlExpandEntityInAttValue(ctxt, buf,
3976
673k
                        ent->content, ent, normalize, inSpace, depth, check);
3977
673k
                if (pent != NULL)
3978
673k
                    pent->flags &= ~XML_ENT_EXPANDING;
3979
673k
      }
3980
834k
        }
3981
1.97G
    }
3982
3983
801k
    if (chunkSize > 0)
3984
710k
        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3985
3986
801k
    return(normChange);
3987
801k
}
3988
3989
/**
3990
 * Expand general entity references in an entity or attribute value.
3991
 * Perform attribute value normalization.
3992
 *
3993
 * @param ctxt  parser context
3994
 * @param str  entity or attribute value
3995
 * @param normalize  whether to collapse whitespace
3996
 * @returns the expanded attribtue value.
3997
 */
3998
xmlChar *
3999
xmlExpandEntitiesInAttValue(xmlParserCtxt *ctxt, const xmlChar *str,
4000
0
                            int normalize) {
4001
0
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4002
0
                         XML_MAX_HUGE_LENGTH :
4003
0
                         XML_MAX_TEXT_LENGTH;
4004
0
    xmlSBuf buf;
4005
0
    int inSpace = 1;
4006
4007
0
    xmlSBufInit(&buf, maxLength);
4008
4009
0
    xmlExpandEntityInAttValue(ctxt, &buf, str, NULL, normalize, &inSpace,
4010
0
                              ctxt->inputNr, /* check */ 0);
4011
4012
0
    if ((normalize) && (inSpace) && (buf.size > 0))
4013
0
        buf.size--;
4014
4015
0
    return(xmlSBufFinish(&buf, NULL, ctxt, "AttValue length too long"));
4016
0
}
4017
4018
/**
4019
 * Parse a value for an attribute.
4020
 *
4021
 * NOTE: if no normalization is needed, the routine will return pointers
4022
 * directly from the data buffer.
4023
 *
4024
 * 3.3.3 Attribute-Value Normalization:
4025
 *
4026
 * Before the value of an attribute is passed to the application or
4027
 * checked for validity, the XML processor must normalize it as follows:
4028
 *
4029
 * - a character reference is processed by appending the referenced
4030
 *   character to the attribute value
4031
 * - an entity reference is processed by recursively processing the
4032
 *   replacement text of the entity
4033
 * - a whitespace character (\#x20, \#xD, \#xA, \#x9) is processed by
4034
 *   appending \#x20 to the normalized value, except that only a single
4035
 *   \#x20 is appended for a "#xD#xA" sequence that is part of an external
4036
 *   parsed entity or the literal entity value of an internal parsed entity
4037
 * - other characters are processed by appending them to the normalized value
4038
 *
4039
 * If the declared value is not CDATA, then the XML processor must further
4040
 * process the normalized attribute value by discarding any leading and
4041
 * trailing space (\#x20) characters, and by replacing sequences of space
4042
 * (\#x20) characters by a single space (\#x20) character.
4043
 * All attributes for which no declaration has been read should be treated
4044
 * by a non-validating parser as if declared CDATA.
4045
 *
4046
 * @param ctxt  an XML parser context
4047
 * @param attlen  attribute len result
4048
 * @param outFlags  resulting XML_ATTVAL_* flags
4049
 * @param special  value from attsSpecial
4050
 * @param isNamespace  whether this is a namespace declaration
4051
 * @returns the AttValue parsed or NULL. The value has to be freed by the
4052
 *     caller if it was copied, this can be detected by val[*len] == 0.
4053
 */
4054
static xmlChar *
4055
xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *attlen, int *outFlags,
4056
2.84M
                         int special, int isNamespace) {
4057
2.84M
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4058
2.54M
                         XML_MAX_HUGE_LENGTH :
4059
2.84M
                         XML_MAX_TEXT_LENGTH;
4060
2.84M
    xmlSBuf buf;
4061
2.84M
    xmlChar *ret;
4062
2.84M
    int c, l, quote, entFlags, chunkSize;
4063
2.84M
    int inSpace = 1;
4064
2.84M
    int replaceEntities;
4065
2.84M
    int normalize = (special & XML_SPECIAL_TYPE_MASK) > XML_ATTRIBUTE_CDATA;
4066
2.84M
    int attvalFlags = 0;
4067
4068
    /* Always expand namespace URIs */
4069
2.84M
    replaceEntities = (ctxt->replaceEntities) || (isNamespace);
4070
4071
2.84M
    xmlSBufInit(&buf, maxLength);
4072
4073
2.84M
    GROW;
4074
4075
2.84M
    quote = CUR;
4076
2.84M
    if ((quote != '"') && (quote != '\'')) {
4077
8.94k
  xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
4078
8.94k
  return(NULL);
4079
8.94k
    }
4080
2.83M
    NEXTL(1);
4081
4082
2.83M
    if (ctxt->inSubset == 0)
4083
2.79M
        entFlags = XML_ENT_CHECKED | XML_ENT_VALIDATED;
4084
39.4k
    else
4085
39.4k
        entFlags = XML_ENT_VALIDATED;
4086
4087
2.83M
    inSpace = 1;
4088
2.83M
    chunkSize = 0;
4089
4090
228M
    while (1) {
4091
228M
        if (PARSER_STOPPED(ctxt))
4092
333
            goto error;
4093
4094
228M
        if (CUR_PTR >= ctxt->input->end) {
4095
7.35k
            xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
4096
7.35k
                           "AttValue: ' expected\n");
4097
7.35k
            goto error;
4098
7.35k
        }
4099
4100
        /*
4101
         * TODO: Check growth threshold
4102
         */
4103
228M
        if (ctxt->input->end - CUR_PTR < 10)
4104
111k
            GROW;
4105
4106
228M
        c = CUR;
4107
4108
228M
        if (c >= 0x80) {
4109
74.9M
            l = xmlUTF8MultibyteLen(ctxt, CUR_PTR,
4110
74.9M
                    "invalid character in attribute value\n");
4111
74.9M
            if (l == 0) {
4112
22.1M
                if (chunkSize > 0) {
4113
577k
                    xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4114
577k
                    chunkSize = 0;
4115
577k
                }
4116
22.1M
                xmlSBufAddReplChar(&buf);
4117
22.1M
                NEXTL(1);
4118
52.7M
            } else {
4119
52.7M
                chunkSize += l;
4120
52.7M
                NEXTL(l);
4121
52.7M
            }
4122
4123
74.9M
            inSpace = 0;
4124
153M
        } else if (c != '&') {
4125
153M
            if (c > 0x20) {
4126
127M
                if (c == quote)
4127
2.82M
                    break;
4128
4129
124M
                if (c == '<')
4130
150k
                    xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
4131
4132
124M
                chunkSize += 1;
4133
124M
                inSpace = 0;
4134
124M
            } else if (!IS_BYTE_CHAR(c)) {
4135
4.84M
                xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
4136
4.84M
                        "invalid character in attribute value\n");
4137
4.84M
                if (chunkSize > 0) {
4138
220k
                    xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4139
220k
                    chunkSize = 0;
4140
220k
                }
4141
4.84M
                xmlSBufAddReplChar(&buf);
4142
4.84M
                inSpace = 0;
4143
21.1M
            } else {
4144
                /* Whitespace */
4145
21.1M
                if ((normalize) && (inSpace)) {
4146
                    /* Skip char */
4147
618k
                    if (chunkSize > 0) {
4148
32.0k
                        xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4149
32.0k
                        chunkSize = 0;
4150
32.0k
                    }
4151
618k
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4152
20.4M
                } else if (c < 0x20) {
4153
                    /* Convert to space */
4154
9.97M
                    if (chunkSize > 0) {
4155
313k
                        xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4156
313k
                        chunkSize = 0;
4157
313k
                    }
4158
4159
9.97M
                    xmlSBufAddCString(&buf, " ", 1);
4160
10.5M
                } else {
4161
10.5M
                    chunkSize += 1;
4162
10.5M
                }
4163
4164
21.1M
                inSpace = 1;
4165
4166
21.1M
                if ((c == 0xD) && (NXT(1) == 0xA))
4167
716
                    CUR_PTR++;
4168
21.1M
            }
4169
4170
150M
            NEXTL(1);
4171
150M
        } else if (NXT(1) == '#') {
4172
72.4k
            int val;
4173
4174
72.4k
            if (chunkSize > 0) {
4175
60.4k
                xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4176
60.4k
                chunkSize = 0;
4177
60.4k
            }
4178
4179
72.4k
            val = xmlParseCharRef(ctxt);
4180
72.4k
            if (val == 0)
4181
1.94k
                goto error;
4182
4183
70.4k
            if ((val == '&') && (!replaceEntities)) {
4184
                /*
4185
                 * The reparsing will be done in xmlNodeParseContent()
4186
                 * called from SAX2.c
4187
                 */
4188
0
                xmlSBufAddCString(&buf, "&#38;", 5);
4189
0
                inSpace = 0;
4190
70.4k
            } else if (val == ' ') {
4191
3.91k
                if ((normalize) && (inSpace))
4192
454
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4193
3.46k
                else
4194
3.46k
                    xmlSBufAddCString(&buf, " ", 1);
4195
3.91k
                inSpace = 1;
4196
66.5k
            } else {
4197
66.5k
                xmlSBufAddChar(&buf, val);
4198
66.5k
                inSpace = 0;
4199
66.5k
            }
4200
253k
        } else {
4201
253k
            const xmlChar *name;
4202
253k
            xmlEntityPtr ent;
4203
4204
253k
            if (chunkSize > 0) {
4205
131k
                xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4206
131k
                chunkSize = 0;
4207
131k
            }
4208
4209
253k
            name = xmlParseEntityRefInternal(ctxt);
4210
253k
            if (name == NULL) {
4211
                /*
4212
                 * Probably a literal '&' which wasn't escaped.
4213
                 * TODO: Handle gracefully in recovery mode.
4214
                 */
4215
32.5k
                continue;
4216
32.5k
            }
4217
4218
220k
            ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 1);
4219
220k
            if (ent == NULL)
4220
43.3k
                continue;
4221
4222
177k
            if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
4223
48.6k
                if ((ent->content[0] == '&') && (!replaceEntities))
4224
0
                    xmlSBufAddCString(&buf, "&#38;", 5);
4225
48.6k
                else
4226
48.6k
                    xmlSBufAddString(&buf, ent->content, ent->length);
4227
48.6k
                inSpace = 0;
4228
128k
            } else if (replaceEntities) {
4229
128k
                if (xmlExpandEntityInAttValue(ctxt, &buf,
4230
128k
                        ent->content, ent, normalize, &inSpace, ctxt->inputNr,
4231
128k
                        /* check */ 1) > 0)
4232
10.9k
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4233
128k
            } else {
4234
0
                if ((ent->flags & entFlags) != entFlags)
4235
0
                    xmlCheckEntityInAttValue(ctxt, ent, ctxt->inputNr);
4236
4237
0
                if (xmlParserEntityCheck(ctxt, ent->expandedSize)) {
4238
0
                    ent->content[0] = 0;
4239
0
                    goto error;
4240
0
                }
4241
4242
                /*
4243
                 * Just output the reference
4244
                 */
4245
0
                xmlSBufAddCString(&buf, "&", 1);
4246
0
                xmlSBufAddString(&buf, ent->name, xmlStrlen(ent->name));
4247
0
                xmlSBufAddCString(&buf, ";", 1);
4248
4249
0
                inSpace = 0;
4250
0
            }
4251
177k
  }
4252
228M
    }
4253
4254
2.82M
    if ((buf.mem == NULL) && (outFlags != NULL)) {
4255
2.68M
        ret = (xmlChar *) CUR_PTR - chunkSize;
4256
4257
2.68M
        if (attlen != NULL)
4258
2.68M
            *attlen = chunkSize;
4259
2.68M
        if ((normalize) && (inSpace) && (chunkSize > 0)) {
4260
425
            attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4261
425
            *attlen -= 1;
4262
425
        }
4263
4264
        /* Report potential error */
4265
2.68M
        xmlSBufCleanup(&buf, ctxt, "AttValue length too long");
4266
2.68M
    } else {
4267
138k
        if (chunkSize > 0)
4268
71.5k
            xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4269
4270
138k
        if ((normalize) && (inSpace) && (buf.size > 0)) {
4271
940
            attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4272
940
            buf.size--;
4273
940
        }
4274
4275
138k
        ret = xmlSBufFinish(&buf, attlen, ctxt, "AttValue length too long");
4276
138k
        attvalFlags |= XML_ATTVAL_ALLOC;
4277
4278
138k
        if (ret != NULL) {
4279
138k
            if (attlen != NULL)
4280
104k
                *attlen = buf.size;
4281
138k
        }
4282
138k
    }
4283
4284
2.82M
    if (outFlags != NULL)
4285
2.79M
        *outFlags = attvalFlags;
4286
4287
2.82M
    NEXTL(1);
4288
4289
2.82M
    return(ret);
4290
4291
9.62k
error:
4292
9.62k
    xmlSBufCleanup(&buf, ctxt, "AttValue length too long");
4293
9.62k
    return(NULL);
4294
2.83M
}
4295
4296
/**
4297
 * Parse a value for an attribute
4298
 * Note: the parser won't do substitution of entities here, this
4299
 * will be handled later in #xmlStringGetNodeList
4300
 *
4301
 * @deprecated Internal function, don't use.
4302
 *
4303
 *     [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
4304
 *                       "'" ([^<&'] | Reference)* "'"
4305
 *
4306
 * 3.3.3 Attribute-Value Normalization:
4307
 *
4308
 * Before the value of an attribute is passed to the application or
4309
 * checked for validity, the XML processor must normalize it as follows:
4310
 *
4311
 * - a character reference is processed by appending the referenced
4312
 *   character to the attribute value
4313
 * - an entity reference is processed by recursively processing the
4314
 *   replacement text of the entity
4315
 * - a whitespace character (\#x20, \#xD, \#xA, \#x9) is processed by
4316
 *   appending \#x20 to the normalized value, except that only a single
4317
 *   \#x20 is appended for a "#xD#xA" sequence that is part of an external
4318
 *   parsed entity or the literal entity value of an internal parsed entity
4319
 * - other characters are processed by appending them to the normalized value
4320
 *
4321
 * If the declared value is not CDATA, then the XML processor must further
4322
 * process the normalized attribute value by discarding any leading and
4323
 * trailing space (\#x20) characters, and by replacing sequences of space
4324
 * (\#x20) characters by a single space (\#x20) character.
4325
 * All attributes for which no declaration has been read should be treated
4326
 * by a non-validating parser as if declared CDATA.
4327
 *
4328
 * @param ctxt  an XML parser context
4329
 * @returns the AttValue parsed or NULL. The value has to be freed by the
4330
 * caller.
4331
 */
4332
xmlChar *
4333
43.6k
xmlParseAttValue(xmlParserCtxt *ctxt) {
4334
43.6k
    if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
4335
43.6k
    return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0, 0));
4336
43.6k
}
4337
4338
/**
4339
 * Parse an XML Literal
4340
 *
4341
 * @deprecated Internal function, don't use.
4342
 *
4343
 *     [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
4344
 *
4345
 * @param ctxt  an XML parser context
4346
 * @returns the SystemLiteral parsed or NULL
4347
 */
4348
4349
xmlChar *
4350
10.6k
xmlParseSystemLiteral(xmlParserCtxt *ctxt) {
4351
10.6k
    xmlChar *buf = NULL;
4352
10.6k
    int len = 0;
4353
10.6k
    int size = XML_PARSER_BUFFER_SIZE;
4354
10.6k
    int cur, l;
4355
10.6k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4356
5.07k
                    XML_MAX_TEXT_LENGTH :
4357
10.6k
                    XML_MAX_NAME_LENGTH;
4358
10.6k
    xmlChar stop;
4359
4360
10.6k
    if (RAW == '"') {
4361
5.97k
        NEXT;
4362
5.97k
  stop = '"';
4363
5.97k
    } else if (RAW == '\'') {
4364
1.01k
        NEXT;
4365
1.01k
  stop = '\'';
4366
3.66k
    } else {
4367
3.66k
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
4368
3.66k
  return(NULL);
4369
3.66k
    }
4370
4371
6.99k
    buf = xmlMalloc(size);
4372
6.99k
    if (buf == NULL) {
4373
0
        xmlErrMemory(ctxt);
4374
0
  return(NULL);
4375
0
    }
4376
6.99k
    cur = xmlCurrentCharRecover(ctxt, &l);
4377
3.36M
    while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
4378
3.35M
  if (len + 5 >= size) {
4379
5.58k
      xmlChar *tmp;
4380
5.58k
            int newSize;
4381
4382
5.58k
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4383
5.58k
            if (newSize < 0) {
4384
1
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral");
4385
1
                xmlFree(buf);
4386
1
                return(NULL);
4387
1
            }
4388
5.58k
      tmp = xmlRealloc(buf, newSize);
4389
5.58k
      if (tmp == NULL) {
4390
0
          xmlFree(buf);
4391
0
    xmlErrMemory(ctxt);
4392
0
    return(NULL);
4393
0
      }
4394
5.58k
      buf = tmp;
4395
5.58k
            size = newSize;
4396
5.58k
  }
4397
3.35M
  COPY_BUF(buf, len, cur);
4398
3.35M
  NEXTL(l);
4399
3.35M
  cur = xmlCurrentCharRecover(ctxt, &l);
4400
3.35M
    }
4401
6.99k
    buf[len] = 0;
4402
6.99k
    if (!IS_CHAR(cur)) {
4403
1.73k
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
4404
5.25k
    } else {
4405
5.25k
  NEXT;
4406
5.25k
    }
4407
6.99k
    return(buf);
4408
6.99k
}
4409
4410
/**
4411
 * Parse an XML public literal
4412
 *
4413
 * @deprecated Internal function, don't use.
4414
 *
4415
 *     [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
4416
 *
4417
 * @param ctxt  an XML parser context
4418
 * @returns the PubidLiteral parsed or NULL.
4419
 */
4420
4421
xmlChar *
4422
11.2k
xmlParsePubidLiteral(xmlParserCtxt *ctxt) {
4423
11.2k
    xmlChar *buf = NULL;
4424
11.2k
    int len = 0;
4425
11.2k
    int size = XML_PARSER_BUFFER_SIZE;
4426
11.2k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4427
3.65k
                    XML_MAX_TEXT_LENGTH :
4428
11.2k
                    XML_MAX_NAME_LENGTH;
4429
11.2k
    xmlChar cur;
4430
11.2k
    xmlChar stop;
4431
4432
11.2k
    if (RAW == '"') {
4433
3.17k
        NEXT;
4434
3.17k
  stop = '"';
4435
8.11k
    } else if (RAW == '\'') {
4436
7.14k
        NEXT;
4437
7.14k
  stop = '\'';
4438
7.14k
    } else {
4439
970
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
4440
970
  return(NULL);
4441
970
    }
4442
10.3k
    buf = xmlMalloc(size);
4443
10.3k
    if (buf == NULL) {
4444
0
  xmlErrMemory(ctxt);
4445
0
  return(NULL);
4446
0
    }
4447
10.3k
    cur = CUR;
4448
15.1M
    while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop) &&
4449
15.1M
           (PARSER_STOPPED(ctxt) == 0)) { /* checked */
4450
15.1M
  if (len + 1 >= size) {
4451
2.21k
      xmlChar *tmp;
4452
2.21k
            int newSize;
4453
4454
2.21k
      newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4455
2.21k
            if (newSize < 0) {
4456
4
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID");
4457
4
                xmlFree(buf);
4458
4
                return(NULL);
4459
4
            }
4460
2.20k
      tmp = xmlRealloc(buf, newSize);
4461
2.20k
      if (tmp == NULL) {
4462
0
    xmlErrMemory(ctxt);
4463
0
    xmlFree(buf);
4464
0
    return(NULL);
4465
0
      }
4466
2.20k
      buf = tmp;
4467
2.20k
            size = newSize;
4468
2.20k
  }
4469
15.1M
  buf[len++] = cur;
4470
15.1M
  NEXT;
4471
15.1M
  cur = CUR;
4472
15.1M
    }
4473
10.3k
    buf[len] = 0;
4474
10.3k
    if (cur != stop) {
4475
5.42k
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
4476
5.42k
    } else {
4477
4.88k
  NEXTL(1);
4478
4.88k
    }
4479
10.3k
    return(buf);
4480
10.3k
}
4481
4482
static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial);
4483
4484
/*
4485
 * used for the test in the inner loop of the char data testing
4486
 */
4487
static const unsigned char test_char_data[256] = {
4488
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4489
    0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */
4490
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4491
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4492
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */
4493
    0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
4494
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
4495
    0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */
4496
    0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
4497
    0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
4498
    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
4499
    0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */
4500
    0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
4501
    0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
4502
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
4503
    0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
4504
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */
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
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4510
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4511
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4512
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4513
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4514
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4515
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4516
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4517
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4518
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4519
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4520
};
4521
4522
static void
4523
xmlCharacters(xmlParserCtxtPtr ctxt, const xmlChar *buf, int size,
4524
2.86M
              int isBlank) {
4525
2.86M
    int checkBlanks;
4526
4527
2.86M
    if ((ctxt->sax == NULL) || (ctxt->disableSAX))
4528
677k
        return;
4529
4530
2.18M
    checkBlanks = (!ctxt->keepBlanks) ||
4531
2.18M
                  (ctxt->sax->ignorableWhitespace != ctxt->sax->characters);
4532
4533
    /*
4534
     * Calling areBlanks with only parts of a text node
4535
     * is fundamentally broken, making the NOBLANKS option
4536
     * essentially unusable.
4537
     */
4538
2.18M
    if ((checkBlanks) &&
4539
0
        (areBlanks(ctxt, buf, size, isBlank))) {
4540
0
        if ((ctxt->sax->ignorableWhitespace != NULL) &&
4541
0
            (ctxt->keepBlanks))
4542
0
            ctxt->sax->ignorableWhitespace(ctxt->userData, buf, size);
4543
2.18M
    } else {
4544
2.18M
        if (ctxt->sax->characters != NULL)
4545
2.18M
            ctxt->sax->characters(ctxt->userData, buf, size);
4546
4547
        /*
4548
         * The old code used to update this value for "complex" data
4549
         * even if checkBlanks was false. This was probably a bug.
4550
         */
4551
2.18M
        if ((checkBlanks) && (*ctxt->space == -1))
4552
0
            *ctxt->space = -2;
4553
2.18M
    }
4554
2.18M
}
4555
4556
/**
4557
 * Parse character data. Always makes progress if the first char isn't
4558
 * '<' or '&'.
4559
 *
4560
 * The right angle bracket (>) may be represented using the string "&gt;",
4561
 * and must, for compatibility, be escaped using "&gt;" or a character
4562
 * reference when it appears in the string "]]>" in content, when that
4563
 * string is not marking the end of a CDATA section.
4564
 *
4565
 *     [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
4566
 * @param ctxt  an XML parser context
4567
 * @param partial  buffer may contain partial UTF-8 sequences
4568
 */
4569
static void
4570
4.94M
xmlParseCharDataInternal(xmlParserCtxtPtr ctxt, int partial) {
4571
4.94M
    const xmlChar *in;
4572
4.94M
    int line = ctxt->input->line;
4573
4.94M
    int col = ctxt->input->col;
4574
4.94M
    int ccol;
4575
4.94M
    int terminate = 0;
4576
4577
4.94M
    GROW;
4578
    /*
4579
     * Accelerated common case where input don't need to be
4580
     * modified before passing it to the handler.
4581
     */
4582
4.94M
    in = ctxt->input->cur;
4583
4.95M
    do {
4584
6.57M
get_more_space:
4585
11.4M
        while (*in == 0x20) { in++; ctxt->input->col++; }
4586
6.57M
        if (*in == 0xA) {
4587
17.2M
            do {
4588
17.2M
                ctxt->input->line++; ctxt->input->col = 1;
4589
17.2M
                in++;
4590
17.2M
            } while (*in == 0xA);
4591
1.62M
            goto get_more_space;
4592
1.62M
        }
4593
4.95M
        if (*in == '<') {
4594
1.29M
            while (in > ctxt->input->cur) {
4595
647k
                const xmlChar *tmp = ctxt->input->cur;
4596
647k
                size_t nbchar = in - tmp;
4597
4598
647k
                if (nbchar > XML_MAX_ITEMS)
4599
0
                    nbchar = XML_MAX_ITEMS;
4600
647k
                ctxt->input->cur += nbchar;
4601
4602
647k
                xmlCharacters(ctxt, tmp, nbchar, 1);
4603
647k
            }
4604
647k
            return;
4605
647k
        }
4606
4607
4.64M
get_more:
4608
4.64M
        ccol = ctxt->input->col;
4609
85.4M
        while (test_char_data[*in]) {
4610
80.8M
            in++;
4611
80.8M
            ccol++;
4612
80.8M
        }
4613
4.64M
        ctxt->input->col = ccol;
4614
4.64M
        if (*in == 0xA) {
4615
7.75M
            do {
4616
7.75M
                ctxt->input->line++; ctxt->input->col = 1;
4617
7.75M
                in++;
4618
7.75M
            } while (*in == 0xA);
4619
266k
            goto get_more;
4620
266k
        }
4621
4.37M
        if (*in == ']') {
4622
66.9k
            size_t avail = ctxt->input->end - in;
4623
4624
66.9k
            if (partial && avail < 2) {
4625
0
                terminate = 1;
4626
0
                goto invoke_callback;
4627
0
            }
4628
66.9k
            if (in[1] == ']') {
4629
50.9k
                if (partial && avail < 3) {
4630
0
                    terminate = 1;
4631
0
                    goto invoke_callback;
4632
0
                }
4633
50.9k
                if (in[2] == '>')
4634
3.22k
                    xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
4635
50.9k
            }
4636
4637
66.9k
            in++;
4638
66.9k
            ctxt->input->col++;
4639
66.9k
            goto get_more;
4640
66.9k
        }
4641
4642
4.30M
invoke_callback:
4643
6.00M
        while (in > ctxt->input->cur) {
4644
1.69M
            const xmlChar *tmp = ctxt->input->cur;
4645
1.69M
            size_t nbchar = in - tmp;
4646
4647
1.69M
            if (nbchar > XML_MAX_ITEMS)
4648
0
                nbchar = XML_MAX_ITEMS;
4649
1.69M
            ctxt->input->cur += nbchar;
4650
4651
1.69M
            xmlCharacters(ctxt, tmp, nbchar, 0);
4652
4653
1.69M
            line = ctxt->input->line;
4654
1.69M
            col = ctxt->input->col;
4655
1.69M
        }
4656
4.30M
        ctxt->input->cur = in;
4657
4.30M
        if (*in == 0xD) {
4658
44.5k
            in++;
4659
44.5k
            if (*in == 0xA) {
4660
2.27k
                ctxt->input->cur = in;
4661
2.27k
                in++;
4662
2.27k
                ctxt->input->line++; ctxt->input->col = 1;
4663
2.27k
                continue; /* while */
4664
2.27k
            }
4665
42.2k
            in--;
4666
42.2k
        }
4667
4.30M
        if (*in == '<') {
4668
502k
            return;
4669
502k
        }
4670
3.80M
        if (*in == '&') {
4671
1.02M
            return;
4672
1.02M
        }
4673
2.78M
        if (terminate) {
4674
0
            return;
4675
0
        }
4676
2.78M
        SHRINK;
4677
2.78M
        GROW;
4678
2.78M
        in = ctxt->input->cur;
4679
2.78M
    } while (((*in >= 0x20) && (*in <= 0x7F)) ||
4680
2.77M
             (*in == 0x09) || (*in == 0x0a));
4681
2.77M
    ctxt->input->line = line;
4682
2.77M
    ctxt->input->col = col;
4683
2.77M
    xmlParseCharDataComplex(ctxt, partial);
4684
2.77M
}
4685
4686
/**
4687
 * Always makes progress if the first char isn't '<' or '&'.
4688
 *
4689
 * parse a CharData section.this is the fallback function
4690
 * of #xmlParseCharData when the parsing requires handling
4691
 * of non-ASCII characters.
4692
 *
4693
 * @param ctxt  an XML parser context
4694
 * @param partial  whether the input can end with truncated UTF-8
4695
 */
4696
static void
4697
2.77M
xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial) {
4698
2.77M
    xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
4699
2.77M
    int nbchar = 0;
4700
2.77M
    int cur, l;
4701
4702
2.77M
    cur = xmlCurrentCharRecover(ctxt, &l);
4703
55.0M
    while ((cur != '<') && /* checked */
4704
54.9M
           (cur != '&') &&
4705
54.8M
     (IS_CHAR(cur))) {
4706
52.2M
        if (cur == ']') {
4707
29.7k
            size_t avail = ctxt->input->end - ctxt->input->cur;
4708
4709
29.7k
            if (partial && avail < 2)
4710
0
                break;
4711
29.7k
            if (NXT(1) == ']') {
4712
18.9k
                if (partial && avail < 3)
4713
0
                    break;
4714
18.9k
                if (NXT(2) == '>')
4715
2.59k
                    xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
4716
18.9k
            }
4717
29.7k
        }
4718
4719
52.2M
  COPY_BUF(buf, nbchar, cur);
4720
  /* move current position before possible calling of ctxt->sax->characters */
4721
52.2M
  NEXTL(l);
4722
52.2M
  if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
4723
284k
      buf[nbchar] = 0;
4724
4725
284k
            xmlCharacters(ctxt, buf, nbchar, 0);
4726
284k
      nbchar = 0;
4727
284k
            SHRINK;
4728
284k
  }
4729
52.2M
  cur = xmlCurrentCharRecover(ctxt, &l);
4730
52.2M
    }
4731
2.77M
    if (nbchar != 0) {
4732
237k
        buf[nbchar] = 0;
4733
4734
237k
        xmlCharacters(ctxt, buf, nbchar, 0);
4735
237k
    }
4736
    /*
4737
     * cur == 0 can mean
4738
     *
4739
     * - End of buffer.
4740
     * - An actual 0 character.
4741
     * - An incomplete UTF-8 sequence. This is allowed if partial is set.
4742
     */
4743
2.77M
    if (ctxt->input->cur < ctxt->input->end) {
4744
2.76M
        if ((cur == 0) && (CUR != 0)) {
4745
762
            if (partial == 0) {
4746
762
                xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4747
762
                        "Incomplete UTF-8 sequence starting with %02X\n", CUR);
4748
762
                NEXTL(1);
4749
762
            }
4750
2.76M
        } else if ((cur != '<') && (cur != '&') && (cur != ']')) {
4751
            /* Generate the error and skip the offending character */
4752
2.60M
            xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4753
2.60M
                              "PCDATA invalid Char value %d\n", cur);
4754
2.60M
            NEXTL(l);
4755
2.60M
        }
4756
2.76M
    }
4757
2.77M
}
4758
4759
/**
4760
 * @deprecated Internal function, don't use.
4761
 * @param ctxt  an XML parser context
4762
 * @param cdata  unused
4763
 */
4764
void
4765
0
xmlParseCharData(xmlParserCtxt *ctxt, ATTRIBUTE_UNUSED int cdata) {
4766
0
    xmlParseCharDataInternal(ctxt, 0);
4767
0
}
4768
4769
/**
4770
 * Parse an External ID or a Public ID
4771
 *
4772
 * @deprecated Internal function, don't use.
4773
 *
4774
 * NOTE: Productions [75] and [83] interact badly since [75] can generate
4775
 * `'PUBLIC' S PubidLiteral S SystemLiteral`
4776
 *
4777
 *     [75] ExternalID ::= 'SYSTEM' S SystemLiteral
4778
 *                       | 'PUBLIC' S PubidLiteral S SystemLiteral
4779
 *
4780
 *     [83] PublicID ::= 'PUBLIC' S PubidLiteral
4781
 *
4782
 * @param ctxt  an XML parser context
4783
 * @param publicId  a xmlChar** receiving PubidLiteral
4784
 * @param strict  indicate whether we should restrict parsing to only
4785
 *          production [75], see NOTE below
4786
 * @returns the function returns SystemLiteral and in the second
4787
 *                case publicID receives PubidLiteral, is strict is off
4788
 *                it is possible to return NULL and have publicID set.
4789
 */
4790
4791
xmlChar *
4792
42.0k
xmlParseExternalID(xmlParserCtxt *ctxt, xmlChar **publicId, int strict) {
4793
42.0k
    xmlChar *URI = NULL;
4794
4795
42.0k
    *publicId = NULL;
4796
42.0k
    if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
4797
5.69k
        SKIP(6);
4798
5.69k
  if (SKIP_BLANKS == 0) {
4799
4.37k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4800
4.37k
                     "Space required after 'SYSTEM'\n");
4801
4.37k
  }
4802
5.69k
  URI = xmlParseSystemLiteral(ctxt);
4803
5.69k
  if (URI == NULL) {
4804
685
      xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
4805
685
        }
4806
36.3k
    } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
4807
11.2k
        SKIP(6);
4808
11.2k
  if (SKIP_BLANKS == 0) {
4809
4.15k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4810
4.15k
        "Space required after 'PUBLIC'\n");
4811
4.15k
  }
4812
11.2k
  *publicId = xmlParsePubidLiteral(ctxt);
4813
11.2k
  if (*publicId == NULL) {
4814
974
      xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
4815
974
  }
4816
11.2k
  if (strict) {
4817
      /*
4818
       * We don't handle [83] so "S SystemLiteral" is required.
4819
       */
4820
4.57k
      if (SKIP_BLANKS == 0) {
4821
3.43k
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4822
3.43k
      "Space required after the Public Identifier\n");
4823
3.43k
      }
4824
6.71k
  } else {
4825
      /*
4826
       * We handle [83] so we return immediately, if
4827
       * "S SystemLiteral" is not detected. We skip blanks if no
4828
             * system literal was found, but this is harmless since we must
4829
             * be at the end of a NotationDecl.
4830
       */
4831
6.71k
      if (SKIP_BLANKS == 0) return(NULL);
4832
2.58k
      if ((CUR != '\'') && (CUR != '"')) return(NULL);
4833
2.58k
  }
4834
4.96k
  URI = xmlParseSystemLiteral(ctxt);
4835
4.96k
  if (URI == NULL) {
4836
2.98k
      xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
4837
2.98k
        }
4838
4.96k
    }
4839
35.7k
    return(URI);
4840
42.0k
}
4841
4842
/**
4843
 * Skip an XML (SGML) comment <!-- .... -->
4844
 *  The spec says that "For compatibility, the string "--" (double-hyphen)
4845
 *  must not occur within comments. "
4846
 * This is the slow routine in case the accelerator for ascii didn't work
4847
 *
4848
 *     [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
4849
 * @param ctxt  an XML parser context
4850
 * @param buf  the already parsed part of the buffer
4851
 * @param len  number of bytes in the buffer
4852
 * @param size  allocated size of the buffer
4853
 */
4854
static void
4855
xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
4856
17.0k
                       size_t len, size_t size) {
4857
17.0k
    int q, ql;
4858
17.0k
    int r, rl;
4859
17.0k
    int cur, l;
4860
17.0k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4861
8.33k
                    XML_MAX_HUGE_LENGTH :
4862
17.0k
                    XML_MAX_TEXT_LENGTH;
4863
4864
17.0k
    if (buf == NULL) {
4865
6.52k
        len = 0;
4866
6.52k
  size = XML_PARSER_BUFFER_SIZE;
4867
6.52k
  buf = xmlMalloc(size);
4868
6.52k
  if (buf == NULL) {
4869
0
      xmlErrMemory(ctxt);
4870
0
      return;
4871
0
  }
4872
6.52k
    }
4873
17.0k
    q = xmlCurrentCharRecover(ctxt, &ql);
4874
17.0k
    if (q == 0)
4875
2.42k
        goto not_terminated;
4876
14.5k
    if (!IS_CHAR(q)) {
4877
2.10k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4878
2.10k
                          "xmlParseComment: invalid xmlChar value %d\n",
4879
2.10k
                    q);
4880
2.10k
  xmlFree (buf);
4881
2.10k
  return;
4882
2.10k
    }
4883
12.4k
    NEXTL(ql);
4884
12.4k
    r = xmlCurrentCharRecover(ctxt, &rl);
4885
12.4k
    if (r == 0)
4886
1.09k
        goto not_terminated;
4887
11.4k
    if (!IS_CHAR(r)) {
4888
1.08k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4889
1.08k
                          "xmlParseComment: invalid xmlChar value %d\n",
4890
1.08k
                    r);
4891
1.08k
  xmlFree (buf);
4892
1.08k
  return;
4893
1.08k
    }
4894
10.3k
    NEXTL(rl);
4895
10.3k
    cur = xmlCurrentCharRecover(ctxt, &l);
4896
10.3k
    if (cur == 0)
4897
1.11k
        goto not_terminated;
4898
3.30M
    while (IS_CHAR(cur) && /* checked */
4899
3.29M
           ((cur != '>') ||
4900
3.29M
      (r != '-') || (q != '-'))) {
4901
3.29M
  if ((r == '-') && (q == '-')) {
4902
4.48k
      xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
4903
4.48k
  }
4904
3.29M
  if (len + 5 >= size) {
4905
3.04k
      xmlChar *tmp;
4906
3.04k
            int newSize;
4907
4908
3.04k
      newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4909
3.04k
            if (newSize < 0) {
4910
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4911
0
                             "Comment too big found", NULL);
4912
0
                xmlFree (buf);
4913
0
                return;
4914
0
            }
4915
3.04k
      tmp = xmlRealloc(buf, newSize);
4916
3.04k
      if (tmp == NULL) {
4917
0
    xmlErrMemory(ctxt);
4918
0
    xmlFree(buf);
4919
0
    return;
4920
0
      }
4921
3.04k
      buf = tmp;
4922
3.04k
            size = newSize;
4923
3.04k
  }
4924
3.29M
  COPY_BUF(buf, len, q);
4925
4926
3.29M
  q = r;
4927
3.29M
  ql = rl;
4928
3.29M
  r = cur;
4929
3.29M
  rl = l;
4930
4931
3.29M
  NEXTL(l);
4932
3.29M
  cur = xmlCurrentCharRecover(ctxt, &l);
4933
4934
3.29M
    }
4935
9.20k
    buf[len] = 0;
4936
9.20k
    if (cur == 0) {
4937
2.96k
  xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4938
2.96k
                       "Comment not terminated \n<!--%.50s\n", buf);
4939
6.24k
    } else if (!IS_CHAR(cur)) {
4940
2.95k
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4941
2.95k
                          "xmlParseComment: invalid xmlChar value %d\n",
4942
2.95k
                    cur);
4943
3.28k
    } else {
4944
3.28k
        NEXT;
4945
3.28k
  if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
4946
3.28k
      (!ctxt->disableSAX))
4947
1.12k
      ctxt->sax->comment(ctxt->userData, buf);
4948
3.28k
    }
4949
9.20k
    xmlFree(buf);
4950
9.20k
    return;
4951
4.62k
not_terminated:
4952
4.62k
    xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4953
4.62k
       "Comment not terminated\n", NULL);
4954
4.62k
    xmlFree(buf);
4955
4.62k
}
4956
4957
/**
4958
 * Parse an XML (SGML) comment. Always consumes '<!'.
4959
 *
4960
 * @deprecated Internal function, don't use.
4961
 *
4962
 *  The spec says that "For compatibility, the string "--" (double-hyphen)
4963
 *  must not occur within comments. "
4964
 *
4965
 *     [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
4966
 * @param ctxt  an XML parser context
4967
 */
4968
void
4969
27.2k
xmlParseComment(xmlParserCtxt *ctxt) {
4970
27.2k
    xmlChar *buf = NULL;
4971
27.2k
    size_t size = XML_PARSER_BUFFER_SIZE;
4972
27.2k
    size_t len = 0;
4973
27.2k
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4974
11.7k
                       XML_MAX_HUGE_LENGTH :
4975
27.2k
                       XML_MAX_TEXT_LENGTH;
4976
27.2k
    const xmlChar *in;
4977
27.2k
    size_t nbchar = 0;
4978
27.2k
    int ccol;
4979
4980
    /*
4981
     * Check that there is a comment right here.
4982
     */
4983
27.2k
    if ((RAW != '<') || (NXT(1) != '!'))
4984
0
        return;
4985
27.2k
    SKIP(2);
4986
27.2k
    if ((RAW != '-') || (NXT(1) != '-'))
4987
16
        return;
4988
27.2k
    SKIP(2);
4989
27.2k
    GROW;
4990
4991
    /*
4992
     * Accelerated common case where input don't need to be
4993
     * modified before passing it to the handler.
4994
     */
4995
27.2k
    in = ctxt->input->cur;
4996
27.9k
    do {
4997
27.9k
  if (*in == 0xA) {
4998
12.6k
      do {
4999
12.6k
    ctxt->input->line++; ctxt->input->col = 1;
5000
12.6k
    in++;
5001
12.6k
      } while (*in == 0xA);
5002
1.39k
  }
5003
172k
get_more:
5004
172k
        ccol = ctxt->input->col;
5005
4.63M
  while (((*in > '-') && (*in <= 0x7F)) ||
5006
2.94M
         ((*in >= 0x20) && (*in < '-')) ||
5007
4.46M
         (*in == 0x09)) {
5008
4.46M
        in++;
5009
4.46M
        ccol++;
5010
4.46M
  }
5011
172k
  ctxt->input->col = ccol;
5012
172k
  if (*in == 0xA) {
5013
122k
      do {
5014
122k
    ctxt->input->line++; ctxt->input->col = 1;
5015
122k
    in++;
5016
122k
      } while (*in == 0xA);
5017
11.7k
      goto get_more;
5018
11.7k
  }
5019
161k
  nbchar = in - ctxt->input->cur;
5020
  /*
5021
   * save current set of data
5022
   */
5023
161k
  if (nbchar > 0) {
5024
146k
            if (nbchar > maxLength - len) {
5025
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
5026
0
                                  "Comment too big found", NULL);
5027
0
                xmlFree(buf);
5028
0
                return;
5029
0
            }
5030
146k
            if (buf == NULL) {
5031
16.9k
                if ((*in == '-') && (in[1] == '-'))
5032
7.32k
                    size = nbchar + 1;
5033
9.59k
                else
5034
9.59k
                    size = XML_PARSER_BUFFER_SIZE + nbchar;
5035
16.9k
                buf = xmlMalloc(size);
5036
16.9k
                if (buf == NULL) {
5037
0
                    xmlErrMemory(ctxt);
5038
0
                    return;
5039
0
                }
5040
16.9k
                len = 0;
5041
129k
            } else if (len + nbchar + 1 >= size) {
5042
2.81k
                xmlChar *new_buf;
5043
2.81k
                size += len + nbchar + XML_PARSER_BUFFER_SIZE;
5044
2.81k
                new_buf = xmlRealloc(buf, size);
5045
2.81k
                if (new_buf == NULL) {
5046
0
                    xmlErrMemory(ctxt);
5047
0
                    xmlFree(buf);
5048
0
                    return;
5049
0
                }
5050
2.81k
                buf = new_buf;
5051
2.81k
            }
5052
146k
            memcpy(&buf[len], ctxt->input->cur, nbchar);
5053
146k
            len += nbchar;
5054
146k
            buf[len] = 0;
5055
146k
  }
5056
161k
  ctxt->input->cur = in;
5057
161k
  if (*in == 0xA) {
5058
0
      in++;
5059
0
      ctxt->input->line++; ctxt->input->col = 1;
5060
0
  }
5061
161k
  if (*in == 0xD) {
5062
7.73k
      in++;
5063
7.73k
      if (*in == 0xA) {
5064
5.65k
    ctxt->input->cur = in;
5065
5.65k
    in++;
5066
5.65k
    ctxt->input->line++; ctxt->input->col = 1;
5067
5.65k
    goto get_more;
5068
5.65k
      }
5069
2.08k
      in--;
5070
2.08k
  }
5071
155k
  SHRINK;
5072
155k
  GROW;
5073
155k
  in = ctxt->input->cur;
5074
155k
  if (*in == '-') {
5075
137k
      if (in[1] == '-') {
5076
130k
          if (in[2] == '>') {
5077
10.2k
        SKIP(3);
5078
10.2k
        if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
5079
10.2k
            (!ctxt->disableSAX)) {
5080
4.37k
      if (buf != NULL)
5081
2.11k
          ctxt->sax->comment(ctxt->userData, buf);
5082
2.26k
      else
5083
2.26k
          ctxt->sax->comment(ctxt->userData, BAD_CAST "");
5084
4.37k
        }
5085
10.2k
        if (buf != NULL)
5086
6.42k
            xmlFree(buf);
5087
10.2k
        return;
5088
10.2k
    }
5089
120k
    if (buf != NULL) {
5090
118k
        xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
5091
118k
                          "Double hyphen within comment: "
5092
118k
                                      "<!--%.50s\n",
5093
118k
              buf);
5094
118k
    } else
5095
1.07k
        xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
5096
1.07k
                          "Double hyphen within comment\n", NULL);
5097
120k
    in++;
5098
120k
    ctxt->input->col++;
5099
120k
      }
5100
127k
      in++;
5101
127k
      ctxt->input->col++;
5102
127k
      goto get_more;
5103
137k
  }
5104
155k
    } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09) || (*in == 0x0a));
5105
17.0k
    xmlParseCommentComplex(ctxt, buf, len, size);
5106
17.0k
}
5107
5108
5109
/**
5110
 * Parse the name of a PI
5111
 *
5112
 * @deprecated Internal function, don't use.
5113
 *
5114
 *     [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
5115
 *
5116
 * @param ctxt  an XML parser context
5117
 * @returns the PITarget name or NULL
5118
 */
5119
5120
const xmlChar *
5121
256k
xmlParsePITarget(xmlParserCtxt *ctxt) {
5122
256k
    const xmlChar *name;
5123
5124
256k
    name = xmlParseName(ctxt);
5125
256k
    if ((name != NULL) &&
5126
249k
        ((name[0] == 'x') || (name[0] == 'X')) &&
5127
85.1k
        ((name[1] == 'm') || (name[1] == 'M')) &&
5128
15.8k
        ((name[2] == 'l') || (name[2] == 'L'))) {
5129
12.7k
  int i;
5130
12.7k
  if ((name[0] == 'x') && (name[1] == 'm') &&
5131
11.1k
      (name[2] == 'l') && (name[3] == 0)) {
5132
5.13k
      xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
5133
5.13k
     "XML declaration allowed only at the start of the document\n");
5134
5.13k
      return(name);
5135
7.56k
  } else if (name[3] == 0) {
5136
2.59k
      xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
5137
2.59k
      return(name);
5138
2.59k
  }
5139
14.4k
  for (i = 0;;i++) {
5140
14.4k
      if (xmlW3CPIs[i] == NULL) break;
5141
9.88k
      if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
5142
450
          return(name);
5143
9.88k
  }
5144
4.52k
  xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
5145
4.52k
          "xmlParsePITarget: invalid name prefix 'xml'\n",
5146
4.52k
          NULL, NULL);
5147
4.52k
    }
5148
248k
    if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) {
5149
2.00k
  xmlNsErr(ctxt, XML_NS_ERR_COLON,
5150
2.00k
     "colons are forbidden from PI names '%s'\n", name, NULL, NULL);
5151
2.00k
    }
5152
248k
    return(name);
5153
256k
}
5154
5155
#ifdef LIBXML_CATALOG_ENABLED
5156
/**
5157
 * Parse an XML Catalog Processing Instruction.
5158
 *
5159
 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
5160
 *
5161
 * Occurs only if allowed by the user and if happening in the Misc
5162
 * part of the document before any doctype information
5163
 * This will add the given catalog to the parsing context in order
5164
 * to be used if there is a resolution need further down in the document
5165
 *
5166
 * @param ctxt  an XML parser context
5167
 * @param catalog  the PI value string
5168
 */
5169
5170
static void
5171
0
xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
5172
0
    xmlChar *URL = NULL;
5173
0
    const xmlChar *tmp, *base;
5174
0
    xmlChar marker;
5175
5176
0
    tmp = catalog;
5177
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5178
0
    if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
5179
0
  goto error;
5180
0
    tmp += 7;
5181
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5182
0
    if (*tmp != '=') {
5183
0
  return;
5184
0
    }
5185
0
    tmp++;
5186
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5187
0
    marker = *tmp;
5188
0
    if ((marker != '\'') && (marker != '"'))
5189
0
  goto error;
5190
0
    tmp++;
5191
0
    base = tmp;
5192
0
    while ((*tmp != 0) && (*tmp != marker)) tmp++;
5193
0
    if (*tmp == 0)
5194
0
  goto error;
5195
0
    URL = xmlStrndup(base, tmp - base);
5196
0
    tmp++;
5197
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5198
0
    if (*tmp != 0)
5199
0
  goto error;
5200
5201
0
    if (URL != NULL) {
5202
        /*
5203
         * Unfortunately, the catalog API doesn't report OOM errors.
5204
         * xmlGetLastError isn't very helpful since we don't know
5205
         * where the last error came from. We'd have to reset it
5206
         * before this call and restore it afterwards.
5207
         */
5208
0
  ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
5209
0
  xmlFree(URL);
5210
0
    }
5211
0
    return;
5212
5213
0
error:
5214
0
    xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
5215
0
            "Catalog PI syntax error: %s\n",
5216
0
      catalog, NULL);
5217
0
    if (URL != NULL)
5218
0
  xmlFree(URL);
5219
0
}
5220
#endif
5221
5222
/**
5223
 * Parse an XML Processing Instruction.
5224
 *
5225
 * @deprecated Internal function, don't use.
5226
 *
5227
 *     [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
5228
 *
5229
 * The processing is transferred to SAX once parsed.
5230
 *
5231
 * @param ctxt  an XML parser context
5232
 */
5233
5234
void
5235
256k
xmlParsePI(xmlParserCtxt *ctxt) {
5236
256k
    xmlChar *buf = NULL;
5237
256k
    size_t len = 0;
5238
256k
    size_t size = XML_PARSER_BUFFER_SIZE;
5239
256k
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
5240
84.1k
                       XML_MAX_HUGE_LENGTH :
5241
256k
                       XML_MAX_TEXT_LENGTH;
5242
256k
    int cur, l;
5243
256k
    const xmlChar *target;
5244
5245
256k
    if ((RAW == '<') && (NXT(1) == '?')) {
5246
  /*
5247
   * this is a Processing Instruction.
5248
   */
5249
256k
  SKIP(2);
5250
5251
  /*
5252
   * Parse the target name and check for special support like
5253
   * namespace.
5254
   */
5255
256k
        target = xmlParsePITarget(ctxt);
5256
256k
  if (target != NULL) {
5257
249k
      if ((RAW == '?') && (NXT(1) == '>')) {
5258
92.7k
    SKIP(2);
5259
5260
    /*
5261
     * SAX: PI detected.
5262
     */
5263
92.7k
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
5264
87.7k
        (ctxt->sax->processingInstruction != NULL))
5265
87.7k
        ctxt->sax->processingInstruction(ctxt->userData,
5266
87.7k
                                         target, NULL);
5267
92.7k
    return;
5268
92.7k
      }
5269
156k
      buf = xmlMalloc(size);
5270
156k
      if (buf == NULL) {
5271
0
    xmlErrMemory(ctxt);
5272
0
    return;
5273
0
      }
5274
156k
      if (SKIP_BLANKS == 0) {
5275
15.9k
    xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
5276
15.9k
        "ParsePI: PI %s space expected\n", target);
5277
15.9k
      }
5278
156k
      cur = xmlCurrentCharRecover(ctxt, &l);
5279
26.9M
      while (IS_CHAR(cur) && /* checked */
5280
26.8M
       ((cur != '?') || (NXT(1) != '>'))) {
5281
26.7M
    if (len + 5 >= size) {
5282
18.4k
        xmlChar *tmp;
5283
18.4k
                    int newSize;
5284
5285
18.4k
                    newSize = xmlGrowCapacity(size, 1, 1, maxLength);
5286
18.4k
                    if (newSize < 0) {
5287
0
                        xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
5288
0
                                          "PI %s too big found", target);
5289
0
                        xmlFree(buf);
5290
0
                        return;
5291
0
                    }
5292
18.4k
        tmp = xmlRealloc(buf, newSize);
5293
18.4k
        if (tmp == NULL) {
5294
0
      xmlErrMemory(ctxt);
5295
0
      xmlFree(buf);
5296
0
      return;
5297
0
        }
5298
18.4k
        buf = tmp;
5299
18.4k
                    size = newSize;
5300
18.4k
    }
5301
26.7M
    COPY_BUF(buf, len, cur);
5302
26.7M
    NEXTL(l);
5303
26.7M
    cur = xmlCurrentCharRecover(ctxt, &l);
5304
26.7M
      }
5305
156k
      buf[len] = 0;
5306
156k
      if (cur != '?') {
5307
12.8k
    xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
5308
12.8k
          "ParsePI: PI %s never end ...\n", target);
5309
143k
      } else {
5310
143k
    SKIP(2);
5311
5312
143k
#ifdef LIBXML_CATALOG_ENABLED
5313
143k
    if ((ctxt->inSubset == 0) &&
5314
140k
        (xmlStrEqual(target, XML_CATALOG_PI))) {
5315
3.16k
        xmlCatalogAllow allow = xmlCatalogGetDefaults();
5316
5317
3.16k
        if ((ctxt->options & XML_PARSE_CATALOG_PI) &&
5318
0
                        ((allow == XML_CATA_ALLOW_DOCUMENT) ||
5319
0
       (allow == XML_CATA_ALLOW_ALL)))
5320
0
      xmlParseCatalogPI(ctxt, buf);
5321
3.16k
    }
5322
143k
#endif
5323
5324
    /*
5325
     * SAX: PI detected.
5326
     */
5327
143k
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
5328
129k
        (ctxt->sax->processingInstruction != NULL))
5329
129k
        ctxt->sax->processingInstruction(ctxt->userData,
5330
129k
                                         target, buf);
5331
143k
      }
5332
156k
      xmlFree(buf);
5333
156k
  } else {
5334
7.03k
      xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
5335
7.03k
  }
5336
256k
    }
5337
256k
}
5338
5339
/**
5340
 * Parse a notation declaration. Always consumes '<!'.
5341
 *
5342
 * @deprecated Internal function, don't use.
5343
 *
5344
 *     [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID |  PublicID)
5345
 *                           S? '>'
5346
 *
5347
 * Hence there is actually 3 choices:
5348
 *
5349
 *     'PUBLIC' S PubidLiteral
5350
 *     'PUBLIC' S PubidLiteral S SystemLiteral
5351
 *     'SYSTEM' S SystemLiteral
5352
 *
5353
 * See the NOTE on #xmlParseExternalID.
5354
 *
5355
 * @param ctxt  an XML parser context
5356
 */
5357
5358
void
5359
11.9k
xmlParseNotationDecl(xmlParserCtxt *ctxt) {
5360
11.9k
    const xmlChar *name;
5361
11.9k
    xmlChar *Pubid;
5362
11.9k
    xmlChar *Systemid;
5363
5364
11.9k
    if ((CUR != '<') || (NXT(1) != '!'))
5365
0
        return;
5366
11.9k
    SKIP(2);
5367
5368
11.9k
    if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
5369
11.8k
#ifdef LIBXML_VALID_ENABLED
5370
11.8k
  int oldInputNr = ctxt->inputNr;
5371
11.8k
#endif
5372
5373
11.8k
  SKIP(8);
5374
11.8k
  if (SKIP_BLANKS_PE == 0) {
5375
598
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5376
598
         "Space required after '<!NOTATION'\n");
5377
598
      return;
5378
598
  }
5379
5380
11.2k
        name = xmlParseName(ctxt);
5381
11.2k
  if (name == NULL) {
5382
1.69k
      xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
5383
1.69k
      return;
5384
1.69k
  }
5385
9.57k
  if (xmlStrchr(name, ':') != NULL) {
5386
546
      xmlNsErr(ctxt, XML_NS_ERR_COLON,
5387
546
         "colons are forbidden from notation names '%s'\n",
5388
546
         name, NULL, NULL);
5389
546
  }
5390
9.57k
  if (SKIP_BLANKS_PE == 0) {
5391
566
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5392
566
         "Space required after the NOTATION name'\n");
5393
566
      return;
5394
566
  }
5395
5396
  /*
5397
   * Parse the IDs.
5398
   */
5399
9.00k
  Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
5400
9.00k
  SKIP_BLANKS_PE;
5401
5402
9.00k
  if (RAW == '>') {
5403
1.48k
#ifdef LIBXML_VALID_ENABLED
5404
1.48k
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
5405
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5406
0
                           "Notation declaration doesn't start and stop"
5407
0
                                 " in the same entity\n",
5408
0
                                 NULL, NULL);
5409
0
      }
5410
1.48k
#endif
5411
1.48k
      NEXT;
5412
1.48k
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5413
654
    (ctxt->sax->notationDecl != NULL))
5414
654
    ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
5415
7.52k
  } else {
5416
7.52k
      xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
5417
7.52k
  }
5418
9.00k
  if (Systemid != NULL) xmlFree(Systemid);
5419
9.00k
  if (Pubid != NULL) xmlFree(Pubid);
5420
9.00k
    }
5421
11.9k
}
5422
5423
/**
5424
 * Parse an entity declaration. Always consumes '<!'.
5425
 *
5426
 * @deprecated Internal function, don't use.
5427
 *
5428
 *     [70] EntityDecl ::= GEDecl | PEDecl
5429
 *
5430
 *     [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
5431
 *
5432
 *     [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
5433
 *
5434
 *     [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
5435
 *
5436
 *     [74] PEDef ::= EntityValue | ExternalID
5437
 *
5438
 *     [76] NDataDecl ::= S 'NDATA' S Name
5439
 *
5440
 * [ VC: Notation Declared ]
5441
 * The Name must match the declared name of a notation.
5442
 *
5443
 * @param ctxt  an XML parser context
5444
 */
5445
5446
void
5447
58.0k
xmlParseEntityDecl(xmlParserCtxt *ctxt) {
5448
58.0k
    const xmlChar *name = NULL;
5449
58.0k
    xmlChar *value = NULL;
5450
58.0k
    xmlChar *URI = NULL, *literal = NULL;
5451
58.0k
    const xmlChar *ndata = NULL;
5452
58.0k
    int isParameter = 0;
5453
58.0k
    xmlChar *orig = NULL;
5454
5455
58.0k
    if ((CUR != '<') || (NXT(1) != '!'))
5456
0
        return;
5457
58.0k
    SKIP(2);
5458
5459
    /* GROW; done in the caller */
5460
58.0k
    if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
5461
57.9k
#ifdef LIBXML_VALID_ENABLED
5462
57.9k
  int oldInputNr = ctxt->inputNr;
5463
57.9k
#endif
5464
5465
57.9k
  SKIP(6);
5466
57.9k
  if (SKIP_BLANKS_PE == 0) {
5467
45.0k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5468
45.0k
         "Space required after '<!ENTITY'\n");
5469
45.0k
  }
5470
5471
57.9k
  if (RAW == '%') {
5472
7.67k
      NEXT;
5473
7.67k
      if (SKIP_BLANKS_PE == 0) {
5474
4.16k
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5475
4.16k
             "Space required after '%%'\n");
5476
4.16k
      }
5477
7.67k
      isParameter = 1;
5478
7.67k
  }
5479
5480
57.9k
        name = xmlParseName(ctxt);
5481
57.9k
  if (name == NULL) {
5482
2.10k
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5483
2.10k
                     "xmlParseEntityDecl: no name\n");
5484
2.10k
            return;
5485
2.10k
  }
5486
55.8k
  if (xmlStrchr(name, ':') != NULL) {
5487
3.31k
      xmlNsErr(ctxt, XML_NS_ERR_COLON,
5488
3.31k
         "colons are forbidden from entities names '%s'\n",
5489
3.31k
         name, NULL, NULL);
5490
3.31k
  }
5491
55.8k
  if (SKIP_BLANKS_PE == 0) {
5492
37.0k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5493
37.0k
         "Space required after the entity name\n");
5494
37.0k
  }
5495
5496
  /*
5497
   * handle the various case of definitions...
5498
   */
5499
55.8k
  if (isParameter) {
5500
6.65k
      if ((RAW == '"') || (RAW == '\'')) {
5501
4.92k
          value = xmlParseEntityValue(ctxt, &orig);
5502
4.92k
    if (value) {
5503
4.63k
        if ((ctxt->sax != NULL) &&
5504
4.63k
      (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5505
2.68k
      ctxt->sax->entityDecl(ctxt->userData, name,
5506
2.68k
                        XML_INTERNAL_PARAMETER_ENTITY,
5507
2.68k
            NULL, NULL, value);
5508
4.63k
    }
5509
4.92k
      } else {
5510
1.72k
          URI = xmlParseExternalID(ctxt, &literal, 1);
5511
1.72k
    if ((URI == NULL) && (literal == NULL)) {
5512
530
        xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
5513
530
    }
5514
1.72k
    if (URI) {
5515
924
                    if (xmlStrchr(URI, '#')) {
5516
432
                        xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
5517
492
                    } else {
5518
492
                        if ((ctxt->sax != NULL) &&
5519
492
                            (!ctxt->disableSAX) &&
5520
83
                            (ctxt->sax->entityDecl != NULL))
5521
83
                            ctxt->sax->entityDecl(ctxt->userData, name,
5522
83
                                        XML_EXTERNAL_PARAMETER_ENTITY,
5523
83
                                        literal, URI, NULL);
5524
492
                    }
5525
924
    }
5526
1.72k
      }
5527
49.2k
  } else {
5528
49.2k
      if ((RAW == '"') || (RAW == '\'')) {
5529
35.0k
          value = xmlParseEntityValue(ctxt, &orig);
5530
35.0k
    if ((ctxt->sax != NULL) &&
5531
35.0k
        (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5532
1.97k
        ctxt->sax->entityDecl(ctxt->userData, name,
5533
1.97k
        XML_INTERNAL_GENERAL_ENTITY,
5534
1.97k
        NULL, NULL, value);
5535
    /*
5536
     * For expat compatibility in SAX mode.
5537
     */
5538
35.0k
    if ((ctxt->myDoc == NULL) ||
5539
31.3k
        (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
5540
16.5k
        if (ctxt->myDoc == NULL) {
5541
3.64k
      ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
5542
3.64k
      if (ctxt->myDoc == NULL) {
5543
0
          xmlErrMemory(ctxt);
5544
0
          goto done;
5545
0
      }
5546
3.64k
      ctxt->myDoc->properties = XML_DOC_INTERNAL;
5547
3.64k
        }
5548
16.5k
        if (ctxt->myDoc->intSubset == NULL) {
5549
3.64k
      ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
5550
3.64k
              BAD_CAST "fake", NULL, NULL);
5551
3.64k
                        if (ctxt->myDoc->intSubset == NULL) {
5552
0
                            xmlErrMemory(ctxt);
5553
0
                            goto done;
5554
0
                        }
5555
3.64k
                    }
5556
5557
16.5k
        xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
5558
16.5k
                    NULL, NULL, value);
5559
16.5k
    }
5560
35.0k
      } else {
5561
14.2k
          URI = xmlParseExternalID(ctxt, &literal, 1);
5562
14.2k
    if ((URI == NULL) && (literal == NULL)) {
5563
7.34k
        xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
5564
7.34k
    }
5565
14.2k
    if (URI) {
5566
4.85k
                    if (xmlStrchr(URI, '#')) {
5567
913
                        xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
5568
913
                    }
5569
4.85k
    }
5570
14.2k
    if ((RAW != '>') && (SKIP_BLANKS_PE == 0)) {
5571
12.5k
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5572
12.5k
           "Space required before 'NDATA'\n");
5573
12.5k
    }
5574
14.2k
    if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
5575
782
        SKIP(5);
5576
782
        if (SKIP_BLANKS_PE == 0) {
5577
473
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5578
473
               "Space required after 'NDATA'\n");
5579
473
        }
5580
782
        ndata = xmlParseName(ctxt);
5581
782
        if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5582
209
            (ctxt->sax->unparsedEntityDecl != NULL))
5583
209
      ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
5584
209
            literal, URI, ndata);
5585
13.4k
    } else {
5586
13.4k
        if ((ctxt->sax != NULL) &&
5587
13.4k
            (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5588
432
      ctxt->sax->entityDecl(ctxt->userData, name,
5589
432
            XML_EXTERNAL_GENERAL_PARSED_ENTITY,
5590
432
            literal, URI, NULL);
5591
        /*
5592
         * For expat compatibility in SAX mode.
5593
         * assuming the entity replacement was asked for
5594
         */
5595
13.4k
        if ((ctxt->replaceEntities != 0) &&
5596
13.4k
      ((ctxt->myDoc == NULL) ||
5597
13.0k
      (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
5598
4.51k
      if (ctxt->myDoc == NULL) {
5599
374
          ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
5600
374
          if (ctxt->myDoc == NULL) {
5601
0
              xmlErrMemory(ctxt);
5602
0
        goto done;
5603
0
          }
5604
374
          ctxt->myDoc->properties = XML_DOC_INTERNAL;
5605
374
      }
5606
5607
4.51k
      if (ctxt->myDoc->intSubset == NULL) {
5608
374
          ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
5609
374
            BAD_CAST "fake", NULL, NULL);
5610
374
                            if (ctxt->myDoc->intSubset == NULL) {
5611
0
                                xmlErrMemory(ctxt);
5612
0
                                goto done;
5613
0
                            }
5614
374
                        }
5615
4.51k
      xmlSAX2EntityDecl(ctxt, name,
5616
4.51k
                  XML_EXTERNAL_GENERAL_PARSED_ENTITY,
5617
4.51k
                  literal, URI, NULL);
5618
4.51k
        }
5619
13.4k
    }
5620
14.2k
      }
5621
49.2k
  }
5622
55.8k
  SKIP_BLANKS_PE;
5623
55.8k
  if (RAW != '>') {
5624
49.7k
      xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
5625
49.7k
              "xmlParseEntityDecl: entity %s not terminated\n", name);
5626
49.7k
  } else {
5627
6.12k
#ifdef LIBXML_VALID_ENABLED
5628
6.12k
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
5629
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5630
0
                           "Entity declaration doesn't start and stop in"
5631
0
                                 " the same entity\n",
5632
0
                                 NULL, NULL);
5633
0
      }
5634
6.12k
#endif
5635
6.12k
      NEXT;
5636
6.12k
  }
5637
55.8k
  if (orig != NULL) {
5638
      /*
5639
       * Ugly mechanism to save the raw entity value.
5640
       */
5641
38.7k
      xmlEntityPtr cur = NULL;
5642
5643
38.7k
      if (isParameter) {
5644
4.63k
          if ((ctxt->sax != NULL) &&
5645
4.63k
        (ctxt->sax->getParameterEntity != NULL))
5646
4.63k
        cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
5647
34.1k
      } else {
5648
34.1k
          if ((ctxt->sax != NULL) &&
5649
34.1k
        (ctxt->sax->getEntity != NULL))
5650
34.1k
        cur = ctxt->sax->getEntity(ctxt->userData, name);
5651
34.1k
    if ((cur == NULL) && (ctxt->userData==ctxt)) {
5652
15.2k
        cur = xmlSAX2GetEntity(ctxt, name);
5653
15.2k
    }
5654
34.1k
      }
5655
38.7k
            if ((cur != NULL) && (cur->orig == NULL)) {
5656
9.56k
    cur->orig = orig;
5657
9.56k
                orig = NULL;
5658
9.56k
      }
5659
38.7k
  }
5660
5661
55.8k
done:
5662
55.8k
  if (value != NULL) xmlFree(value);
5663
55.8k
  if (URI != NULL) xmlFree(URI);
5664
55.8k
  if (literal != NULL) xmlFree(literal);
5665
55.8k
        if (orig != NULL) xmlFree(orig);
5666
55.8k
    }
5667
58.0k
}
5668
5669
/**
5670
 * Parse an attribute default declaration
5671
 *
5672
 * @deprecated Internal function, don't use.
5673
 *
5674
 *     [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
5675
 *
5676
 * [ VC: Required Attribute ]
5677
 * if the default declaration is the keyword \#REQUIRED, then the
5678
 * attribute must be specified for all elements of the type in the
5679
 * attribute-list declaration.
5680
 *
5681
 * [ VC: Attribute Default Legal ]
5682
 * The declared default value must meet the lexical constraints of
5683
 * the declared attribute type c.f. #xmlValidateAttributeDecl
5684
 *
5685
 * [ VC: Fixed Attribute Default ]
5686
 * if an attribute has a default value declared with the \#FIXED
5687
 * keyword, instances of that attribute must match the default value.
5688
 *
5689
 * [ WFC: No < in Attribute Values ]
5690
 * handled in #xmlParseAttValue
5691
 *
5692
 * @param ctxt  an XML parser context
5693
 * @param value  Receive a possible fixed default value for the attribute
5694
 * @returns XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
5695
 *          or XML_ATTRIBUTE_FIXED.
5696
 */
5697
5698
int
5699
49.7k
xmlParseDefaultDecl(xmlParserCtxt *ctxt, xmlChar **value) {
5700
49.7k
    int val;
5701
49.7k
    xmlChar *ret;
5702
5703
49.7k
    *value = NULL;
5704
49.7k
    if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
5705
1.18k
  SKIP(9);
5706
1.18k
  return(XML_ATTRIBUTE_REQUIRED);
5707
1.18k
    }
5708
48.5k
    if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
5709
4.91k
  SKIP(8);
5710
4.91k
  return(XML_ATTRIBUTE_IMPLIED);
5711
4.91k
    }
5712
43.6k
    val = XML_ATTRIBUTE_NONE;
5713
43.6k
    if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
5714
830
  SKIP(6);
5715
830
  val = XML_ATTRIBUTE_FIXED;
5716
830
  if (SKIP_BLANKS_PE == 0) {
5717
626
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5718
626
         "Space required after '#FIXED'\n");
5719
626
  }
5720
830
    }
5721
43.6k
    ret = xmlParseAttValue(ctxt);
5722
43.6k
    if (ret == NULL) {
5723
9.51k
  xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
5724
9.51k
           "Attribute default value declaration error\n");
5725
9.51k
    } else
5726
34.1k
        *value = ret;
5727
43.6k
    return(val);
5728
48.5k
}
5729
5730
/**
5731
 * Parse an Notation attribute type.
5732
 *
5733
 * @deprecated Internal function, don't use.
5734
 *
5735
 * Note: the leading 'NOTATION' S part has already being parsed...
5736
 *
5737
 *     [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
5738
 *
5739
 * [ VC: Notation Attributes ]
5740
 * Values of this type must match one of the notation names included
5741
 * in the declaration; all notation names in the declaration must be declared.
5742
 *
5743
 * @param ctxt  an XML parser context
5744
 * @returns the notation attribute tree built while parsing
5745
 */
5746
5747
xmlEnumeration *
5748
2.45k
xmlParseNotationType(xmlParserCtxt *ctxt) {
5749
2.45k
    const xmlChar *name;
5750
2.45k
    xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
5751
5752
2.45k
    if (RAW != '(') {
5753
473
  xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
5754
473
  return(NULL);
5755
473
    }
5756
2.96k
    do {
5757
2.96k
        NEXT;
5758
2.96k
  SKIP_BLANKS_PE;
5759
2.96k
        name = xmlParseName(ctxt);
5760
2.96k
  if (name == NULL) {
5761
602
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5762
602
         "Name expected in NOTATION declaration\n");
5763
602
            xmlFreeEnumeration(ret);
5764
602
      return(NULL);
5765
602
  }
5766
2.35k
        tmp = NULL;
5767
2.35k
#ifdef LIBXML_VALID_ENABLED
5768
2.35k
        if (ctxt->validate) {
5769
0
            tmp = ret;
5770
0
            while (tmp != NULL) {
5771
0
                if (xmlStrEqual(name, tmp->name)) {
5772
0
                    xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
5773
0
              "standalone: attribute notation value token %s duplicated\n",
5774
0
                                     name, NULL);
5775
0
                    if (!xmlDictOwns(ctxt->dict, name))
5776
0
                        xmlFree((xmlChar *) name);
5777
0
                    break;
5778
0
                }
5779
0
                tmp = tmp->next;
5780
0
            }
5781
0
        }
5782
2.35k
#endif /* LIBXML_VALID_ENABLED */
5783
2.35k
  if (tmp == NULL) {
5784
2.35k
      cur = xmlCreateEnumeration(name);
5785
2.35k
      if (cur == NULL) {
5786
0
                xmlErrMemory(ctxt);
5787
0
                xmlFreeEnumeration(ret);
5788
0
                return(NULL);
5789
0
            }
5790
2.35k
      if (last == NULL) ret = last = cur;
5791
969
      else {
5792
969
    last->next = cur;
5793
969
    last = cur;
5794
969
      }
5795
2.35k
  }
5796
2.35k
  SKIP_BLANKS_PE;
5797
2.35k
    } while (RAW == '|');
5798
1.38k
    if (RAW != ')') {
5799
487
  xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
5800
487
        xmlFreeEnumeration(ret);
5801
487
  return(NULL);
5802
487
    }
5803
894
    NEXT;
5804
894
    return(ret);
5805
1.38k
}
5806
5807
/**
5808
 * Parse an Enumeration attribute type.
5809
 *
5810
 * @deprecated Internal function, don't use.
5811
 *
5812
 *     [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
5813
 *
5814
 * [ VC: Enumeration ]
5815
 * Values of this type must match one of the Nmtoken tokens in
5816
 * the declaration
5817
 *
5818
 * @param ctxt  an XML parser context
5819
 * @returns the enumeration attribute tree built while parsing
5820
 */
5821
5822
xmlEnumeration *
5823
11.8k
xmlParseEnumerationType(xmlParserCtxt *ctxt) {
5824
11.8k
    xmlChar *name;
5825
11.8k
    xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
5826
5827
11.8k
    if (RAW != '(') {
5828
6.12k
  xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
5829
6.12k
  return(NULL);
5830
6.12k
    }
5831
7.81k
    do {
5832
7.81k
        NEXT;
5833
7.81k
  SKIP_BLANKS_PE;
5834
7.81k
        name = xmlParseNmtoken(ctxt);
5835
7.81k
  if (name == NULL) {
5836
750
      xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
5837
750
      return(ret);
5838
750
  }
5839
7.06k
        tmp = NULL;
5840
7.06k
#ifdef LIBXML_VALID_ENABLED
5841
7.06k
        if (ctxt->validate) {
5842
0
            tmp = ret;
5843
0
            while (tmp != NULL) {
5844
0
                if (xmlStrEqual(name, tmp->name)) {
5845
0
                    xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
5846
0
              "standalone: attribute enumeration value token %s duplicated\n",
5847
0
                                     name, NULL);
5848
0
                    if (!xmlDictOwns(ctxt->dict, name))
5849
0
                        xmlFree(name);
5850
0
                    break;
5851
0
                }
5852
0
                tmp = tmp->next;
5853
0
            }
5854
0
        }
5855
7.06k
#endif /* LIBXML_VALID_ENABLED */
5856
7.06k
  if (tmp == NULL) {
5857
7.06k
      cur = xmlCreateEnumeration(name);
5858
7.06k
      if (!xmlDictOwns(ctxt->dict, name))
5859
7.06k
    xmlFree(name);
5860
7.06k
      if (cur == NULL) {
5861
0
                xmlErrMemory(ctxt);
5862
0
                xmlFreeEnumeration(ret);
5863
0
                return(NULL);
5864
0
            }
5865
7.06k
      if (last == NULL) ret = last = cur;
5866
2.13k
      else {
5867
2.13k
    last->next = cur;
5868
2.13k
    last = cur;
5869
2.13k
      }
5870
7.06k
  }
5871
7.06k
  SKIP_BLANKS_PE;
5872
7.06k
    } while (RAW == '|');
5873
4.92k
    if (RAW != ')') {
5874
1.77k
  xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
5875
1.77k
  return(ret);
5876
1.77k
    }
5877
3.15k
    NEXT;
5878
3.15k
    return(ret);
5879
4.92k
}
5880
5881
/**
5882
 * Parse an Enumerated attribute type.
5883
 *
5884
 * @deprecated Internal function, don't use.
5885
 *
5886
 *     [57] EnumeratedType ::= NotationType | Enumeration
5887
 *
5888
 *     [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
5889
 *
5890
 * @param ctxt  an XML parser context
5891
 * @param tree  the enumeration tree built while parsing
5892
 * @returns XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
5893
 */
5894
5895
int
5896
14.7k
xmlParseEnumeratedType(xmlParserCtxt *ctxt, xmlEnumeration **tree) {
5897
14.7k
    if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
5898
2.98k
  SKIP(8);
5899
2.98k
  if (SKIP_BLANKS_PE == 0) {
5900
532
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5901
532
         "Space required after 'NOTATION'\n");
5902
532
      return(0);
5903
532
  }
5904
2.45k
  *tree = xmlParseNotationType(ctxt);
5905
2.45k
  if (*tree == NULL) return(0);
5906
894
  return(XML_ATTRIBUTE_NOTATION);
5907
2.45k
    }
5908
11.8k
    *tree = xmlParseEnumerationType(ctxt);
5909
11.8k
    if (*tree == NULL) return(0);
5910
4.93k
    return(XML_ATTRIBUTE_ENUMERATION);
5911
11.8k
}
5912
5913
/**
5914
 * Parse the Attribute list def for an element
5915
 *
5916
 * @deprecated Internal function, don't use.
5917
 *
5918
 *     [54] AttType ::= StringType | TokenizedType | EnumeratedType
5919
 *
5920
 *     [55] StringType ::= 'CDATA'
5921
 *
5922
 *     [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
5923
 *                            'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
5924
 *
5925
 * Validity constraints for attribute values syntax are checked in
5926
 * #xmlValidateAttributeValue
5927
 *
5928
 * [ VC: ID ]
5929
 * Values of type ID must match the Name production. A name must not
5930
 * appear more than once in an XML document as a value of this type;
5931
 * i.e., ID values must uniquely identify the elements which bear them.
5932
 *
5933
 * [ VC: One ID per Element Type ]
5934
 * No element type may have more than one ID attribute specified.
5935
 *
5936
 * [ VC: ID Attribute Default ]
5937
 * An ID attribute must have a declared default of \#IMPLIED or \#REQUIRED.
5938
 *
5939
 * [ VC: IDREF ]
5940
 * Values of type IDREF must match the Name production, and values
5941
 * of type IDREFS must match Names; each IDREF Name must match the value
5942
 * of an ID attribute on some element in the XML document; i.e. IDREF
5943
 * values must match the value of some ID attribute.
5944
 *
5945
 * [ VC: Entity Name ]
5946
 * Values of type ENTITY must match the Name production, values
5947
 * of type ENTITIES must match Names; each Entity Name must match the
5948
 * name of an unparsed entity declared in the DTD.
5949
 *
5950
 * [ VC: Name Token ]
5951
 * Values of type NMTOKEN must match the Nmtoken production; values
5952
 * of type NMTOKENS must match Nmtokens.
5953
 *
5954
 * @param ctxt  an XML parser context
5955
 * @param tree  the enumeration tree built while parsing
5956
 * @returns the attribute type
5957
 */
5958
int
5959
63.6k
xmlParseAttributeType(xmlParserCtxt *ctxt, xmlEnumeration **tree) {
5960
63.6k
    if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
5961
5.06k
  SKIP(5);
5962
5.06k
  return(XML_ATTRIBUTE_CDATA);
5963
58.6k
     } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
5964
495
  SKIP(6);
5965
495
  return(XML_ATTRIBUTE_IDREFS);
5966
58.1k
     } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
5967
774
  SKIP(5);
5968
774
  return(XML_ATTRIBUTE_IDREF);
5969
57.3k
     } else if ((RAW == 'I') && (NXT(1) == 'D')) {
5970
34.0k
        SKIP(2);
5971
34.0k
  return(XML_ATTRIBUTE_ID);
5972
34.0k
     } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
5973
1.43k
  SKIP(6);
5974
1.43k
  return(XML_ATTRIBUTE_ENTITY);
5975
21.8k
     } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
5976
5.64k
  SKIP(8);
5977
5.64k
  return(XML_ATTRIBUTE_ENTITIES);
5978
16.1k
     } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
5979
457
  SKIP(8);
5980
457
  return(XML_ATTRIBUTE_NMTOKENS);
5981
15.7k
     } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
5982
933
  SKIP(7);
5983
933
  return(XML_ATTRIBUTE_NMTOKEN);
5984
933
     }
5985
14.7k
     return(xmlParseEnumeratedType(ctxt, tree));
5986
63.6k
}
5987
5988
/**
5989
 * Parse an attribute list declaration for an element. Always consumes '<!'.
5990
 *
5991
 * @deprecated Internal function, don't use.
5992
 *
5993
 *     [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
5994
 *
5995
 *     [53] AttDef ::= S Name S AttType S DefaultDecl
5996
 * @param ctxt  an XML parser context
5997
 */
5998
void
5999
39.9k
xmlParseAttributeListDecl(xmlParserCtxt *ctxt) {
6000
39.9k
    const xmlChar *elemName;
6001
39.9k
    const xmlChar *attrName;
6002
39.9k
    xmlEnumerationPtr tree;
6003
6004
39.9k
    if ((CUR != '<') || (NXT(1) != '!'))
6005
0
        return;
6006
39.9k
    SKIP(2);
6007
6008
39.9k
    if (CMP7(CUR_PTR, 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
6009
39.8k
#ifdef LIBXML_VALID_ENABLED
6010
39.8k
  int oldInputNr = ctxt->inputNr;
6011
39.8k
#endif
6012
6013
39.8k
  SKIP(7);
6014
39.8k
  if (SKIP_BLANKS_PE == 0) {
6015
21.3k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6016
21.3k
                     "Space required after '<!ATTLIST'\n");
6017
21.3k
  }
6018
39.8k
        elemName = xmlParseName(ctxt);
6019
39.8k
  if (elemName == NULL) {
6020
1.98k
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6021
1.98k
         "ATTLIST: no name for Element\n");
6022
1.98k
      return;
6023
1.98k
  }
6024
37.8k
  SKIP_BLANKS_PE;
6025
37.8k
  GROW;
6026
78.0k
  while ((RAW != '>') && (PARSER_STOPPED(ctxt) == 0)) {
6027
70.6k
      int type;
6028
70.6k
      int def;
6029
70.6k
      xmlChar *defaultValue = NULL;
6030
6031
70.6k
      GROW;
6032
70.6k
            tree = NULL;
6033
70.6k
      attrName = xmlParseName(ctxt);
6034
70.6k
      if (attrName == NULL) {
6035
5.28k
    xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6036
5.28k
             "ATTLIST: no name for Attribute\n");
6037
5.28k
    break;
6038
5.28k
      }
6039
65.3k
      GROW;
6040
65.3k
      if (SKIP_BLANKS_PE == 0) {
6041
1.64k
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6042
1.64k
            "Space required after the attribute name\n");
6043
1.64k
    break;
6044
1.64k
      }
6045
6046
63.6k
      type = xmlParseAttributeType(ctxt, &tree);
6047
63.6k
      if (type <= 0) {
6048
8.96k
          break;
6049
8.96k
      }
6050
6051
54.7k
      GROW;
6052
54.7k
      if (SKIP_BLANKS_PE == 0) {
6053
4.95k
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6054
4.95k
             "Space required after the attribute type\n");
6055
4.95k
          if (tree != NULL)
6056
2.55k
        xmlFreeEnumeration(tree);
6057
4.95k
    break;
6058
4.95k
      }
6059
6060
49.7k
      def = xmlParseDefaultDecl(ctxt, &defaultValue);
6061
49.7k
      if (def <= 0) {
6062
0
                if (defaultValue != NULL)
6063
0
        xmlFree(defaultValue);
6064
0
          if (tree != NULL)
6065
0
        xmlFreeEnumeration(tree);
6066
0
          break;
6067
0
      }
6068
49.7k
      if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL))
6069
29.3k
          xmlAttrNormalizeSpace(defaultValue, defaultValue);
6070
6071
49.7k
      GROW;
6072
49.7k
            if (RAW != '>') {
6073
43.0k
    if (SKIP_BLANKS_PE == 0) {
6074
9.59k
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6075
9.59k
      "Space required after the attribute default value\n");
6076
9.59k
        if (defaultValue != NULL)
6077
708
      xmlFree(defaultValue);
6078
9.59k
        if (tree != NULL)
6079
1.56k
      xmlFreeEnumeration(tree);
6080
9.59k
        break;
6081
9.59k
    }
6082
43.0k
      }
6083
40.1k
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6084
15.9k
    (ctxt->sax->attributeDecl != NULL))
6085
15.9k
    ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
6086
15.9k
                          type, def, defaultValue, tree);
6087
24.2k
      else if (tree != NULL)
6088
1.11k
    xmlFreeEnumeration(tree);
6089
6090
40.1k
      if ((ctxt->sax2) && (defaultValue != NULL) &&
6091
33.4k
          (def != XML_ATTRIBUTE_IMPLIED) &&
6092
33.4k
    (def != XML_ATTRIBUTE_REQUIRED)) {
6093
33.4k
    xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
6094
33.4k
      }
6095
40.1k
      if (ctxt->sax2) {
6096
40.1k
    xmlAddSpecialAttr(ctxt, elemName, attrName, type);
6097
40.1k
      }
6098
40.1k
      if (defaultValue != NULL)
6099
33.4k
          xmlFree(defaultValue);
6100
40.1k
      GROW;
6101
40.1k
  }
6102
37.8k
  if (RAW == '>') {
6103
7.62k
#ifdef LIBXML_VALID_ENABLED
6104
7.62k
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
6105
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6106
0
                                 "Attribute list declaration doesn't start and"
6107
0
                                 " stop in the same entity\n",
6108
0
                                 NULL, NULL);
6109
0
      }
6110
7.62k
#endif
6111
7.62k
      NEXT;
6112
7.62k
  }
6113
37.8k
    }
6114
39.9k
}
6115
6116
/**
6117
 * Handle PEs and check that we don't pop the entity that started
6118
 * a balanced group.
6119
 *
6120
 * @param ctxt  parser context
6121
 * @param openInputNr  input nr of the entity with opening '('
6122
 */
6123
static void
6124
1.21M
xmlSkipBlankCharsPEBalanced(xmlParserCtxt *ctxt, int openInputNr) {
6125
1.21M
    SKIP_BLANKS;
6126
1.21M
    GROW;
6127
6128
1.21M
    (void) openInputNr;
6129
6130
1.21M
    if (!PARSER_EXTERNAL(ctxt) && !PARSER_IN_PE(ctxt))
6131
1.15M
        return;
6132
6133
67.4k
    while (!PARSER_STOPPED(ctxt)) {
6134
67.1k
        if (ctxt->input->cur >= ctxt->input->end) {
6135
3.70k
#ifdef LIBXML_VALID_ENABLED
6136
3.70k
            if ((ctxt->validate) && (ctxt->inputNr <= openInputNr)) {
6137
0
                xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6138
0
                                 "Element content declaration doesn't start "
6139
0
                                 "and stop in the same entity\n",
6140
0
                                 NULL, NULL);
6141
0
            }
6142
3.70k
#endif
6143
3.70k
            if (PARSER_IN_PE(ctxt))
6144
3.51k
                xmlPopPE(ctxt);
6145
186
            else
6146
186
                break;
6147
63.4k
        } else if (RAW == '%') {
6148
4.02k
            xmlParsePERefInternal(ctxt, 0);
6149
59.4k
        } else {
6150
59.4k
            break;
6151
59.4k
        }
6152
6153
7.54k
        SKIP_BLANKS;
6154
7.54k
        GROW;
6155
7.54k
    }
6156
59.8k
}
6157
6158
/**
6159
 * Parse the declaration for a Mixed Element content
6160
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6161
 *
6162
 * @deprecated Internal function, don't use.
6163
 *
6164
 *     [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
6165
 *                    '(' S? '#PCDATA' S? ')'
6166
 *
6167
 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
6168
 *
6169
 * [ VC: No Duplicate Types ]
6170
 * The same name must not appear more than once in a single
6171
 * mixed-content declaration.
6172
 *
6173
 * @param ctxt  an XML parser context
6174
 * @param openInputNr  the input used for the current entity, needed for
6175
 * boundary checks
6176
 * @returns the list of the xmlElementContent describing the element choices
6177
 */
6178
xmlElementContent *
6179
2.27k
xmlParseElementMixedContentDecl(xmlParserCtxt *ctxt, int openInputNr) {
6180
2.27k
    xmlElementContentPtr ret = NULL, cur = NULL, n;
6181
2.27k
    const xmlChar *elem = NULL;
6182
6183
2.27k
    GROW;
6184
2.27k
    if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
6185
2.27k
  SKIP(7);
6186
2.27k
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6187
2.27k
  if (RAW == ')') {
6188
910
#ifdef LIBXML_VALID_ENABLED
6189
910
      if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6190
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6191
0
                                 "Element content declaration doesn't start "
6192
0
                                 "and stop in the same entity\n",
6193
0
                                 NULL, NULL);
6194
0
      }
6195
910
#endif
6196
910
      NEXT;
6197
910
      ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
6198
910
      if (ret == NULL)
6199
0
                goto mem_error;
6200
910
      if (RAW == '*') {
6201
228
    ret->ocur = XML_ELEMENT_CONTENT_MULT;
6202
228
    NEXT;
6203
228
      }
6204
910
      return(ret);
6205
910
  }
6206
1.36k
  if ((RAW == '(') || (RAW == '|')) {
6207
742
      ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
6208
742
      if (ret == NULL)
6209
0
                goto mem_error;
6210
742
  }
6211
2.40k
  while ((RAW == '|') && (PARSER_STOPPED(ctxt) == 0)) {
6212
1.32k
      NEXT;
6213
1.32k
            n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
6214
1.32k
            if (n == NULL)
6215
0
                goto mem_error;
6216
1.32k
      if (elem == NULL) {
6217
739
    n->c1 = cur;
6218
739
    if (cur != NULL)
6219
739
        cur->parent = n;
6220
739
    ret = cur = n;
6221
739
      } else {
6222
588
          cur->c2 = n;
6223
588
    n->parent = cur;
6224
588
    n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6225
588
                if (n->c1 == NULL)
6226
0
                    goto mem_error;
6227
588
    n->c1->parent = n;
6228
588
    cur = n;
6229
588
      }
6230
1.32k
            xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6231
1.32k
      elem = xmlParseName(ctxt);
6232
1.32k
      if (elem == NULL) {
6233
284
    xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6234
284
      "xmlParseElementMixedContentDecl : Name expected\n");
6235
284
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6236
284
    return(NULL);
6237
284
      }
6238
1.04k
            xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6239
1.04k
  }
6240
1.07k
  if ((RAW == ')') && (NXT(1) == '*')) {
6241
401
      if (elem != NULL) {
6242
401
    cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem,
6243
401
                                   XML_ELEMENT_CONTENT_ELEMENT);
6244
401
    if (cur->c2 == NULL)
6245
0
                    goto mem_error;
6246
401
    cur->c2->parent = cur;
6247
401
            }
6248
401
            if (ret != NULL)
6249
401
                ret->ocur = XML_ELEMENT_CONTENT_MULT;
6250
401
#ifdef LIBXML_VALID_ENABLED
6251
401
      if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6252
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6253
0
                                 "Element content declaration doesn't start "
6254
0
                                 "and stop in the same entity\n",
6255
0
                                 NULL, NULL);
6256
0
      }
6257
401
#endif
6258
401
      SKIP(2);
6259
675
  } else {
6260
675
      xmlFreeDocElementContent(ctxt->myDoc, ret);
6261
675
      xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
6262
675
      return(NULL);
6263
675
  }
6264
6265
1.07k
    } else {
6266
0
  xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
6267
0
    }
6268
401
    return(ret);
6269
6270
0
mem_error:
6271
0
    xmlErrMemory(ctxt);
6272
0
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6273
0
    return(NULL);
6274
2.27k
}
6275
6276
/**
6277
 * Parse the declaration for a Mixed Element content
6278
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6279
 *
6280
 *     [47] children ::= (choice | seq) ('?' | '*' | '+')?
6281
 *
6282
 *     [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
6283
 *
6284
 *     [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
6285
 *
6286
 *     [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
6287
 *
6288
 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
6289
 * TODO Parameter-entity replacement text must be properly nested
6290
 *  with parenthesized groups. That is to say, if either of the
6291
 *  opening or closing parentheses in a choice, seq, or Mixed
6292
 *  construct is contained in the replacement text for a parameter
6293
 *  entity, both must be contained in the same replacement text. For
6294
 *  interoperability, if a parameter-entity reference appears in a
6295
 *  choice, seq, or Mixed construct, its replacement text should not
6296
 *  be empty, and neither the first nor last non-blank character of
6297
 *  the replacement text should be a connector (| or ,).
6298
 *
6299
 * @param ctxt  an XML parser context
6300
 * @param openInputNr  the input used for the current entity, needed for
6301
 * boundary checks
6302
 * @param depth  the level of recursion
6303
 * @returns the tree of xmlElementContent describing the element
6304
 *          hierarchy.
6305
 */
6306
static xmlElementContentPtr
6307
xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int openInputNr,
6308
93.6k
                                       int depth) {
6309
93.6k
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
6310
93.6k
    xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
6311
93.6k
    const xmlChar *elem;
6312
93.6k
    xmlChar type = 0;
6313
6314
93.6k
    if (depth > maxDepth) {
6315
3
        xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
6316
3
                "xmlParseElementChildrenContentDecl : depth %d too deep, "
6317
3
                "use XML_PARSE_HUGE\n", depth);
6318
3
  return(NULL);
6319
3
    }
6320
93.6k
    xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6321
93.6k
    if (RAW == '(') {
6322
78.1k
        int newInputNr = ctxt->inputNr;
6323
6324
        /* Recurse on first child */
6325
78.1k
  NEXT;
6326
78.1k
        cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, newInputNr,
6327
78.1k
                                                           depth + 1);
6328
78.1k
        if (cur == NULL)
6329
70.0k
            return(NULL);
6330
78.1k
    } else {
6331
15.5k
  elem = xmlParseName(ctxt);
6332
15.5k
  if (elem == NULL) {
6333
1.74k
      xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
6334
1.74k
      return(NULL);
6335
1.74k
  }
6336
13.8k
        cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6337
13.8k
  if (cur == NULL) {
6338
0
      xmlErrMemory(ctxt);
6339
0
      return(NULL);
6340
0
  }
6341
13.8k
  GROW;
6342
13.8k
  if (RAW == '?') {
6343
3.35k
      cur->ocur = XML_ELEMENT_CONTENT_OPT;
6344
3.35k
      NEXT;
6345
10.4k
  } else if (RAW == '*') {
6346
1.38k
      cur->ocur = XML_ELEMENT_CONTENT_MULT;
6347
1.38k
      NEXT;
6348
9.06k
  } else if (RAW == '+') {
6349
767
      cur->ocur = XML_ELEMENT_CONTENT_PLUS;
6350
767
      NEXT;
6351
8.30k
  } else {
6352
8.30k
      cur->ocur = XML_ELEMENT_CONTENT_ONCE;
6353
8.30k
  }
6354
13.8k
  GROW;
6355
13.8k
    }
6356
564k
    while (!PARSER_STOPPED(ctxt)) {
6357
564k
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6358
564k
        if (RAW == ')')
6359
14.0k
            break;
6360
        /*
6361
   * Each loop we parse one separator and one element.
6362
   */
6363
550k
        if (RAW == ',') {
6364
526k
      if (type == 0) type = CUR;
6365
6366
      /*
6367
       * Detect "Name | Name , Name" error
6368
       */
6369
525k
      else if (type != CUR) {
6370
2
    xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
6371
2
        "xmlParseElementChildrenContentDecl : '%c' expected\n",
6372
2
                      type);
6373
2
    if ((last != NULL) && (last != ret))
6374
2
        xmlFreeDocElementContent(ctxt->myDoc, last);
6375
2
    if (ret != NULL)
6376
2
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6377
2
    return(NULL);
6378
2
      }
6379
526k
      NEXT;
6380
6381
526k
      op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ);
6382
526k
      if (op == NULL) {
6383
0
                xmlErrMemory(ctxt);
6384
0
    if ((last != NULL) && (last != ret))
6385
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6386
0
          xmlFreeDocElementContent(ctxt->myDoc, ret);
6387
0
    return(NULL);
6388
0
      }
6389
526k
      if (last == NULL) {
6390
1.38k
    op->c1 = ret;
6391
1.38k
    if (ret != NULL)
6392
1.38k
        ret->parent = op;
6393
1.38k
    ret = cur = op;
6394
525k
      } else {
6395
525k
          cur->c2 = op;
6396
525k
    if (op != NULL)
6397
525k
        op->parent = cur;
6398
525k
    op->c1 = last;
6399
525k
    if (last != NULL)
6400
525k
        last->parent = op;
6401
525k
    cur =op;
6402
525k
    last = NULL;
6403
525k
      }
6404
526k
  } else if (RAW == '|') {
6405
21.1k
      if (type == 0) type = CUR;
6406
6407
      /*
6408
       * Detect "Name , Name | Name" error
6409
       */
6410
10.2k
      else if (type != CUR) {
6411
7
    xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
6412
7
        "xmlParseElementChildrenContentDecl : '%c' expected\n",
6413
7
          type);
6414
7
    if ((last != NULL) && (last != ret))
6415
7
        xmlFreeDocElementContent(ctxt->myDoc, last);
6416
7
    if (ret != NULL)
6417
7
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6418
7
    return(NULL);
6419
7
      }
6420
21.1k
      NEXT;
6421
6422
21.1k
      op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
6423
21.1k
      if (op == NULL) {
6424
0
                xmlErrMemory(ctxt);
6425
0
    if ((last != NULL) && (last != ret))
6426
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6427
0
    if (ret != NULL)
6428
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6429
0
    return(NULL);
6430
0
      }
6431
21.1k
      if (last == NULL) {
6432
10.9k
    op->c1 = ret;
6433
10.9k
    if (ret != NULL)
6434
10.9k
        ret->parent = op;
6435
10.9k
    ret = cur = op;
6436
10.9k
      } else {
6437
10.2k
          cur->c2 = op;
6438
10.2k
    if (op != NULL)
6439
10.2k
        op->parent = cur;
6440
10.2k
    op->c1 = last;
6441
10.2k
    if (last != NULL)
6442
10.2k
        last->parent = op;
6443
10.2k
    cur =op;
6444
10.2k
    last = NULL;
6445
10.2k
      }
6446
21.1k
  } else {
6447
2.16k
      xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
6448
2.16k
      if ((last != NULL) && (last != ret))
6449
898
          xmlFreeDocElementContent(ctxt->myDoc, last);
6450
2.16k
      if (ret != NULL)
6451
2.16k
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6452
2.16k
      return(NULL);
6453
2.16k
  }
6454
548k
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6455
548k
        if (RAW == '(') {
6456
9.33k
            int newInputNr = ctxt->inputNr;
6457
6458
      /* Recurse on second child */
6459
9.33k
      NEXT;
6460
9.33k
      last = xmlParseElementChildrenContentDeclPriv(ctxt, newInputNr,
6461
9.33k
                                                          depth + 1);
6462
9.33k
            if (last == NULL) {
6463
4.56k
    if (ret != NULL)
6464
4.56k
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6465
4.56k
    return(NULL);
6466
4.56k
            }
6467
538k
  } else {
6468
538k
      elem = xmlParseName(ctxt);
6469
538k
      if (elem == NULL) {
6470
682
    xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
6471
682
    if (ret != NULL)
6472
682
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6473
682
    return(NULL);
6474
682
      }
6475
538k
      last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6476
538k
      if (last == NULL) {
6477
0
                xmlErrMemory(ctxt);
6478
0
    if (ret != NULL)
6479
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6480
0
    return(NULL);
6481
0
      }
6482
538k
      if (RAW == '?') {
6483
1.57k
    last->ocur = XML_ELEMENT_CONTENT_OPT;
6484
1.57k
    NEXT;
6485
536k
      } else if (RAW == '*') {
6486
1.80k
    last->ocur = XML_ELEMENT_CONTENT_MULT;
6487
1.80k
    NEXT;
6488
534k
      } else if (RAW == '+') {
6489
963
    last->ocur = XML_ELEMENT_CONTENT_PLUS;
6490
963
    NEXT;
6491
533k
      } else {
6492
533k
    last->ocur = XML_ELEMENT_CONTENT_ONCE;
6493
533k
      }
6494
538k
  }
6495
548k
    }
6496
14.4k
    if ((cur != NULL) && (last != NULL)) {
6497
6.20k
        cur->c2 = last;
6498
6.20k
  if (last != NULL)
6499
6.20k
      last->parent = cur;
6500
6.20k
    }
6501
14.4k
#ifdef LIBXML_VALID_ENABLED
6502
14.4k
    if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6503
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6504
0
                         "Element content declaration doesn't start "
6505
0
                         "and stop in the same entity\n",
6506
0
                         NULL, NULL);
6507
0
    }
6508
14.4k
#endif
6509
14.4k
    NEXT;
6510
14.4k
    if (RAW == '?') {
6511
2.19k
  if (ret != NULL) {
6512
2.19k
      if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
6513
1.94k
          (ret->ocur == XML_ELEMENT_CONTENT_MULT))
6514
990
          ret->ocur = XML_ELEMENT_CONTENT_MULT;
6515
1.20k
      else
6516
1.20k
          ret->ocur = XML_ELEMENT_CONTENT_OPT;
6517
2.19k
  }
6518
2.19k
  NEXT;
6519
12.2k
    } else if (RAW == '*') {
6520
2.32k
  if (ret != NULL) {
6521
2.32k
      ret->ocur = XML_ELEMENT_CONTENT_MULT;
6522
2.32k
      cur = ret;
6523
      /*
6524
       * Some normalization:
6525
       * (a | b* | c?)* == (a | b | c)*
6526
       */
6527
9.67k
      while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
6528
7.34k
    if ((cur->c1 != NULL) &&
6529
7.34k
              ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
6530
6.13k
         (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
6531
2.70k
        cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
6532
7.34k
    if ((cur->c2 != NULL) &&
6533
7.34k
              ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
6534
7.04k
         (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
6535
727
        cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
6536
7.34k
    cur = cur->c2;
6537
7.34k
      }
6538
2.32k
  }
6539
2.32k
  NEXT;
6540
9.93k
    } else if (RAW == '+') {
6541
3.82k
  if (ret != NULL) {
6542
3.82k
      int found = 0;
6543
6544
3.82k
      if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
6545
3.46k
          (ret->ocur == XML_ELEMENT_CONTENT_MULT))
6546
1.34k
          ret->ocur = XML_ELEMENT_CONTENT_MULT;
6547
2.47k
      else
6548
2.47k
          ret->ocur = XML_ELEMENT_CONTENT_PLUS;
6549
      /*
6550
       * Some normalization:
6551
       * (a | b*)+ == (a | b)*
6552
       * (a | b?)+ == (a | b)*
6553
       */
6554
21.1k
      while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
6555
17.3k
    if ((cur->c1 != NULL) &&
6556
17.3k
              ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
6557
15.0k
         (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
6558
2.99k
        cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
6559
2.99k
        found = 1;
6560
2.99k
    }
6561
17.3k
    if ((cur->c2 != NULL) &&
6562
17.3k
              ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
6563
16.8k
         (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
6564
2.03k
        cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
6565
2.03k
        found = 1;
6566
2.03k
    }
6567
17.3k
    cur = cur->c2;
6568
17.3k
      }
6569
3.82k
      if (found)
6570
2.34k
    ret->ocur = XML_ELEMENT_CONTENT_MULT;
6571
3.82k
  }
6572
3.82k
  NEXT;
6573
3.82k
    }
6574
14.4k
    return(ret);
6575
21.8k
}
6576
6577
/**
6578
 * Parse the declaration for a Mixed Element content
6579
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6580
 *
6581
 * @deprecated Internal function, don't use.
6582
 *
6583
 *     [47] children ::= (choice | seq) ('?' | '*' | '+')?
6584
 *
6585
 *     [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
6586
 *
6587
 *     [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
6588
 *
6589
 *     [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
6590
 *
6591
 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
6592
 * TODO Parameter-entity replacement text must be properly nested
6593
 *  with parenthesized groups. That is to say, if either of the
6594
 *  opening or closing parentheses in a choice, seq, or Mixed
6595
 *  construct is contained in the replacement text for a parameter
6596
 *  entity, both must be contained in the same replacement text. For
6597
 *  interoperability, if a parameter-entity reference appears in a
6598
 *  choice, seq, or Mixed construct, its replacement text should not
6599
 *  be empty, and neither the first nor last non-blank character of
6600
 *  the replacement text should be a connector (| or ,).
6601
 *
6602
 * @param ctxt  an XML parser context
6603
 * @param inputchk  the input used for the current entity, needed for boundary checks
6604
 * @returns the tree of xmlElementContent describing the element
6605
 *          hierarchy.
6606
 */
6607
xmlElementContent *
6608
0
xmlParseElementChildrenContentDecl(xmlParserCtxt *ctxt, int inputchk) {
6609
    /* stub left for API/ABI compat */
6610
0
    return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1));
6611
0
}
6612
6613
/**
6614
 * Parse the declaration for an Element content either Mixed or Children,
6615
 * the cases EMPTY and ANY are handled directly in #xmlParseElementDecl
6616
 *
6617
 * @deprecated Internal function, don't use.
6618
 *
6619
 *     [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
6620
 *
6621
 * @param ctxt  an XML parser context
6622
 * @param name  the name of the element being defined.
6623
 * @param result  the Element Content pointer will be stored here if any
6624
 * @returns an xmlElementTypeVal value or -1 on error
6625
 */
6626
6627
int
6628
xmlParseElementContentDecl(xmlParserCtxt *ctxt, const xmlChar *name,
6629
8.48k
                           xmlElementContent **result) {
6630
6631
8.48k
    xmlElementContentPtr tree = NULL;
6632
8.48k
    int openInputNr = ctxt->inputNr;
6633
8.48k
    int res;
6634
6635
8.48k
    *result = NULL;
6636
6637
8.48k
    if (RAW != '(') {
6638
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
6639
0
    "xmlParseElementContentDecl : %s '(' expected\n", name);
6640
0
  return(-1);
6641
0
    }
6642
8.48k
    NEXT;
6643
8.48k
    xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6644
8.48k
    if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
6645
2.27k
        tree = xmlParseElementMixedContentDecl(ctxt, openInputNr);
6646
2.27k
  res = XML_ELEMENT_TYPE_MIXED;
6647
6.21k
    } else {
6648
6.21k
        tree = xmlParseElementChildrenContentDeclPriv(ctxt, openInputNr, 1);
6649
6.21k
  res = XML_ELEMENT_TYPE_ELEMENT;
6650
6.21k
    }
6651
8.48k
    if (tree == NULL)
6652
5.55k
        return(-1);
6653
2.93k
    SKIP_BLANKS_PE;
6654
2.93k
    *result = tree;
6655
2.93k
    return(res);
6656
8.48k
}
6657
6658
/**
6659
 * Parse an element declaration. Always consumes '<!'.
6660
 *
6661
 * @deprecated Internal function, don't use.
6662
 *
6663
 *     [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
6664
 *
6665
 * [ VC: Unique Element Type Declaration ]
6666
 * No element type may be declared more than once
6667
 *
6668
 * @param ctxt  an XML parser context
6669
 * @returns the type of the element, or -1 in case of error
6670
 */
6671
int
6672
14.1k
xmlParseElementDecl(xmlParserCtxt *ctxt) {
6673
14.1k
    const xmlChar *name;
6674
14.1k
    int ret = -1;
6675
14.1k
    xmlElementContentPtr content  = NULL;
6676
6677
14.1k
    if ((CUR != '<') || (NXT(1) != '!'))
6678
0
        return(ret);
6679
14.1k
    SKIP(2);
6680
6681
    /* GROW; done in the caller */
6682
14.1k
    if (CMP7(CUR_PTR, 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
6683
14.0k
#ifdef LIBXML_VALID_ENABLED
6684
14.0k
  int oldInputNr = ctxt->inputNr;
6685
14.0k
#endif
6686
6687
14.0k
  SKIP(7);
6688
14.0k
  if (SKIP_BLANKS_PE == 0) {
6689
527
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6690
527
               "Space required after 'ELEMENT'\n");
6691
527
      return(-1);
6692
527
  }
6693
13.5k
        name = xmlParseName(ctxt);
6694
13.5k
  if (name == NULL) {
6695
631
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6696
631
         "xmlParseElementDecl: no name for Element\n");
6697
631
      return(-1);
6698
631
  }
6699
12.9k
  if (SKIP_BLANKS_PE == 0) {
6700
7.92k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6701
7.92k
         "Space required after the element name\n");
6702
7.92k
  }
6703
12.9k
  if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
6704
1.90k
      SKIP(5);
6705
      /*
6706
       * Element must always be empty.
6707
       */
6708
1.90k
      ret = XML_ELEMENT_TYPE_EMPTY;
6709
11.0k
  } else if ((RAW == 'A') && (NXT(1) == 'N') &&
6710
1.43k
             (NXT(2) == 'Y')) {
6711
1.43k
      SKIP(3);
6712
      /*
6713
       * Element is a generic container.
6714
       */
6715
1.43k
      ret = XML_ELEMENT_TYPE_ANY;
6716
9.58k
  } else if (RAW == '(') {
6717
8.48k
      ret = xmlParseElementContentDecl(ctxt, name, &content);
6718
8.48k
            if (ret <= 0)
6719
5.55k
                return(-1);
6720
8.48k
  } else {
6721
      /*
6722
       * [ WFC: PEs in Internal Subset ] error handling.
6723
       */
6724
1.09k
            xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
6725
1.09k
                  "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
6726
1.09k
      return(-1);
6727
1.09k
  }
6728
6729
6.27k
  SKIP_BLANKS_PE;
6730
6731
6.27k
  if (RAW != '>') {
6732
2.57k
      xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
6733
2.57k
      if (content != NULL) {
6734
1.79k
    xmlFreeDocElementContent(ctxt->myDoc, content);
6735
1.79k
      }
6736
3.70k
  } else {
6737
3.70k
#ifdef LIBXML_VALID_ENABLED
6738
3.70k
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
6739
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6740
0
                                 "Element declaration doesn't start and stop in"
6741
0
                                 " the same entity\n",
6742
0
                                 NULL, NULL);
6743
0
      }
6744
3.70k
#endif
6745
6746
3.70k
      NEXT;
6747
3.70k
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6748
2.89k
    (ctxt->sax->elementDecl != NULL)) {
6749
2.89k
    if (content != NULL)
6750
754
        content->parent = NULL;
6751
2.89k
          ctxt->sax->elementDecl(ctxt->userData, name, ret,
6752
2.89k
                           content);
6753
2.89k
    if ((content != NULL) && (content->parent == NULL)) {
6754
        /*
6755
         * this is a trick: if xmlAddElementDecl is called,
6756
         * instead of copying the full tree it is plugged directly
6757
         * if called from the parser. Avoid duplicating the
6758
         * interfaces or change the API/ABI
6759
         */
6760
537
        xmlFreeDocElementContent(ctxt->myDoc, content);
6761
537
    }
6762
2.89k
      } else if (content != NULL) {
6763
382
    xmlFreeDocElementContent(ctxt->myDoc, content);
6764
382
      }
6765
3.70k
  }
6766
6.27k
    }
6767
6.35k
    return(ret);
6768
14.1k
}
6769
6770
/**
6771
 * Parse a conditional section. Always consumes '<!['.
6772
 *
6773
 *     [61] conditionalSect ::= includeSect | ignoreSect
6774
 *     [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
6775
 *     [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
6776
 *     [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>'
6777
 *                                 Ignore)*
6778
 *     [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
6779
 * @param ctxt  an XML parser context
6780
 */
6781
6782
static void
6783
0
xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
6784
0
    size_t depth = 0;
6785
0
    int isFreshPE = 0;
6786
0
    int oldInputNr = ctxt->inputNr;
6787
0
    int declInputNr = ctxt->inputNr;
6788
6789
0
    while (!PARSER_STOPPED(ctxt)) {
6790
0
        if (ctxt->input->cur >= ctxt->input->end) {
6791
0
            if (ctxt->inputNr <= oldInputNr) {
6792
0
                xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
6793
0
                return;
6794
0
            }
6795
6796
0
            xmlPopPE(ctxt);
6797
0
            declInputNr = ctxt->inputNr;
6798
0
        } else if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
6799
0
            SKIP(3);
6800
0
            SKIP_BLANKS_PE;
6801
6802
0
            isFreshPE = 0;
6803
6804
0
            if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
6805
0
                SKIP(7);
6806
0
                SKIP_BLANKS_PE;
6807
0
                if (RAW != '[') {
6808
0
                    xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
6809
0
                    return;
6810
0
                }
6811
0
#ifdef LIBXML_VALID_ENABLED
6812
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6813
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6814
0
                                     "All markup of the conditional section is"
6815
0
                                     " not in the same entity\n",
6816
0
                                     NULL, NULL);
6817
0
                }
6818
0
#endif
6819
0
                NEXT;
6820
6821
0
                depth++;
6822
0
            } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
6823
0
                size_t ignoreDepth = 0;
6824
6825
0
                SKIP(6);
6826
0
                SKIP_BLANKS_PE;
6827
0
                if (RAW != '[') {
6828
0
                    xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
6829
0
                    return;
6830
0
                }
6831
0
#ifdef LIBXML_VALID_ENABLED
6832
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6833
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6834
0
                                     "All markup of the conditional section is"
6835
0
                                     " not in the same entity\n",
6836
0
                                     NULL, NULL);
6837
0
                }
6838
0
#endif
6839
0
                NEXT;
6840
6841
0
                while (PARSER_STOPPED(ctxt) == 0) {
6842
0
                    if (RAW == 0) {
6843
0
                        xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
6844
0
                        return;
6845
0
                    }
6846
0
                    if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
6847
0
                        SKIP(3);
6848
0
                        ignoreDepth++;
6849
                        /* Check for integer overflow */
6850
0
                        if (ignoreDepth == 0) {
6851
0
                            xmlErrMemory(ctxt);
6852
0
                            return;
6853
0
                        }
6854
0
                    } else if ((RAW == ']') && (NXT(1) == ']') &&
6855
0
                               (NXT(2) == '>')) {
6856
0
                        SKIP(3);
6857
0
                        if (ignoreDepth == 0)
6858
0
                            break;
6859
0
                        ignoreDepth--;
6860
0
                    } else {
6861
0
                        NEXT;
6862
0
                    }
6863
0
                }
6864
6865
0
#ifdef LIBXML_VALID_ENABLED
6866
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6867
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6868
0
                                     "All markup of the conditional section is"
6869
0
                                     " not in the same entity\n",
6870
0
                                     NULL, NULL);
6871
0
                }
6872
0
#endif
6873
0
            } else {
6874
0
                xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
6875
0
                return;
6876
0
            }
6877
0
        } else if ((depth > 0) &&
6878
0
                   (RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
6879
0
            if (isFreshPE) {
6880
0
                xmlFatalErrMsg(ctxt, XML_ERR_CONDSEC_INVALID,
6881
0
                               "Parameter entity must match "
6882
0
                               "extSubsetDecl\n");
6883
0
                return;
6884
0
            }
6885
6886
0
            depth--;
6887
0
#ifdef LIBXML_VALID_ENABLED
6888
0
            if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6889
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6890
0
                                 "All markup of the conditional section is not"
6891
0
                                 " in the same entity\n",
6892
0
                                 NULL, NULL);
6893
0
            }
6894
0
#endif
6895
0
            SKIP(3);
6896
0
        } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
6897
0
            isFreshPE = 0;
6898
0
            xmlParseMarkupDecl(ctxt);
6899
0
        } else if (RAW == '%') {
6900
0
            xmlParsePERefInternal(ctxt, 1);
6901
0
            if (ctxt->inputNr > declInputNr) {
6902
0
                isFreshPE = 1;
6903
0
                declInputNr = ctxt->inputNr;
6904
0
            }
6905
0
        } else {
6906
0
            xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
6907
0
            return;
6908
0
        }
6909
6910
0
        if (depth == 0)
6911
0
            break;
6912
6913
0
        SKIP_BLANKS;
6914
0
        SHRINK;
6915
0
        GROW;
6916
0
    }
6917
0
}
6918
6919
/**
6920
 * Parse markup declarations. Always consumes '<!' or '<?'.
6921
 *
6922
 * @deprecated Internal function, don't use.
6923
 *
6924
 *     [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
6925
 *                         NotationDecl | PI | Comment
6926
 *
6927
 * [ VC: Proper Declaration/PE Nesting ]
6928
 * Parameter-entity replacement text must be properly nested with
6929
 * markup declarations. That is to say, if either the first character
6930
 * or the last character of a markup declaration (markupdecl above) is
6931
 * contained in the replacement text for a parameter-entity reference,
6932
 * both must be contained in the same replacement text.
6933
 *
6934
 * [ WFC: PEs in Internal Subset ]
6935
 * In the internal DTD subset, parameter-entity references can occur
6936
 * only where markup declarations can occur, not within markup declarations.
6937
 * (This does not apply to references that occur in external parameter
6938
 * entities or to the external subset.)
6939
 *
6940
 * @param ctxt  an XML parser context
6941
 */
6942
void
6943
145k
xmlParseMarkupDecl(xmlParserCtxt *ctxt) {
6944
145k
    GROW;
6945
145k
    if (CUR == '<') {
6946
145k
        if (NXT(1) == '!') {
6947
132k
      switch (NXT(2)) {
6948
72.2k
          case 'E':
6949
72.2k
        if (NXT(3) == 'L')
6950
14.1k
      xmlParseElementDecl(ctxt);
6951
58.1k
        else if (NXT(3) == 'N')
6952
58.0k
      xmlParseEntityDecl(ctxt);
6953
60
                    else
6954
60
                        SKIP(2);
6955
72.2k
        break;
6956
39.9k
          case 'A':
6957
39.9k
        xmlParseAttributeListDecl(ctxt);
6958
39.9k
        break;
6959
11.9k
          case 'N':
6960
11.9k
        xmlParseNotationDecl(ctxt);
6961
11.9k
        break;
6962
5.26k
          case '-':
6963
5.26k
        xmlParseComment(ctxt);
6964
5.26k
        break;
6965
3.48k
    default:
6966
3.48k
                    xmlFatalErr(ctxt,
6967
3.48k
                                ctxt->inSubset == 2 ?
6968
0
                                    XML_ERR_EXT_SUBSET_NOT_FINISHED :
6969
3.48k
                                    XML_ERR_INT_SUBSET_NOT_FINISHED,
6970
3.48k
                                NULL);
6971
3.48k
                    SKIP(2);
6972
3.48k
        break;
6973
132k
      }
6974
132k
  } else if (NXT(1) == '?') {
6975
12.3k
      xmlParsePI(ctxt);
6976
12.3k
  }
6977
145k
    }
6978
145k
}
6979
6980
/**
6981
 * Parse an XML declaration header for external entities
6982
 *
6983
 * @deprecated Internal function, don't use.
6984
 *
6985
 *     [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
6986
 * @param ctxt  an XML parser context
6987
 */
6988
6989
void
6990
0
xmlParseTextDecl(xmlParserCtxt *ctxt) {
6991
0
    xmlChar *version;
6992
6993
    /*
6994
     * We know that '<?xml' is here.
6995
     */
6996
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
6997
0
  SKIP(5);
6998
0
    } else {
6999
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
7000
0
  return;
7001
0
    }
7002
7003
0
    if (SKIP_BLANKS == 0) {
7004
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7005
0
           "Space needed after '<?xml'\n");
7006
0
    }
7007
7008
    /*
7009
     * We may have the VersionInfo here.
7010
     */
7011
0
    version = xmlParseVersionInfo(ctxt);
7012
0
    if (version == NULL) {
7013
0
  version = xmlCharStrdup(XML_DEFAULT_VERSION);
7014
0
        if (version == NULL) {
7015
0
            xmlErrMemory(ctxt);
7016
0
            return;
7017
0
        }
7018
0
    } else {
7019
0
  if (SKIP_BLANKS == 0) {
7020
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7021
0
               "Space needed here\n");
7022
0
  }
7023
0
    }
7024
0
    ctxt->input->version = version;
7025
7026
    /*
7027
     * We must have the encoding declaration
7028
     */
7029
0
    xmlParseEncodingDecl(ctxt);
7030
7031
0
    SKIP_BLANKS;
7032
0
    if ((RAW == '?') && (NXT(1) == '>')) {
7033
0
        SKIP(2);
7034
0
    } else if (RAW == '>') {
7035
        /* Deprecated old WD ... */
7036
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
7037
0
  NEXT;
7038
0
    } else {
7039
0
        int c;
7040
7041
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
7042
0
        while ((PARSER_STOPPED(ctxt) == 0) && ((c = CUR) != 0)) {
7043
0
            NEXT;
7044
0
            if (c == '>')
7045
0
                break;
7046
0
        }
7047
0
    }
7048
0
}
7049
7050
/**
7051
 * Parse Markup declarations from an external subset
7052
 *
7053
 * @deprecated Internal function, don't use.
7054
 *
7055
 *     [30] extSubset ::= textDecl? extSubsetDecl
7056
 *
7057
 *     [31] extSubsetDecl ::= (markupdecl | conditionalSect |
7058
 *                             PEReference | S) *
7059
 * @param ctxt  an XML parser context
7060
 * @param publicId  the public identifier
7061
 * @param systemId  the system identifier (URL)
7062
 */
7063
void
7064
xmlParseExternalSubset(xmlParserCtxt *ctxt, const xmlChar *publicId,
7065
0
                       const xmlChar *systemId) {
7066
0
    int oldInputNr;
7067
7068
0
    xmlCtxtInitializeLate(ctxt);
7069
7070
0
    xmlDetectEncoding(ctxt);
7071
7072
0
    if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
7073
0
  xmlParseTextDecl(ctxt);
7074
0
    }
7075
0
    if (ctxt->myDoc == NULL) {
7076
0
        ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
7077
0
  if (ctxt->myDoc == NULL) {
7078
0
      xmlErrMemory(ctxt);
7079
0
      return;
7080
0
  }
7081
0
  ctxt->myDoc->properties = XML_DOC_INTERNAL;
7082
0
    }
7083
0
    if ((ctxt->myDoc->intSubset == NULL) &&
7084
0
        (xmlCreateIntSubset(ctxt->myDoc, NULL, publicId, systemId) == NULL)) {
7085
0
        xmlErrMemory(ctxt);
7086
0
    }
7087
7088
0
    ctxt->inSubset = 2;
7089
0
    oldInputNr = ctxt->inputNr;
7090
7091
0
    SKIP_BLANKS;
7092
0
    while (!PARSER_STOPPED(ctxt)) {
7093
0
        if (ctxt->input->cur >= ctxt->input->end) {
7094
0
            if (ctxt->inputNr <= oldInputNr) {
7095
0
                xmlParserCheckEOF(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED);
7096
0
                break;
7097
0
            }
7098
7099
0
            xmlPopPE(ctxt);
7100
0
        } else if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
7101
0
            xmlParseConditionalSections(ctxt);
7102
0
        } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
7103
0
            xmlParseMarkupDecl(ctxt);
7104
0
        } else if (RAW == '%') {
7105
0
            xmlParsePERefInternal(ctxt, 1);
7106
0
        } else {
7107
0
            xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
7108
7109
0
            while (ctxt->inputNr > oldInputNr)
7110
0
                xmlPopPE(ctxt);
7111
0
            break;
7112
0
        }
7113
0
        SKIP_BLANKS;
7114
0
        SHRINK;
7115
0
        GROW;
7116
0
    }
7117
0
}
7118
7119
/**
7120
 * Parse and handle entity references in content, depending on the SAX
7121
 * interface, this may end-up in a call to character() if this is a
7122
 * CharRef, a predefined entity, if there is no reference() callback.
7123
 * or if the parser was asked to switch to that mode.
7124
 *
7125
 * @deprecated Internal function, don't use.
7126
 *
7127
 * Always consumes '&'.
7128
 *
7129
 *     [67] Reference ::= EntityRef | CharRef
7130
 * @param ctxt  an XML parser context
7131
 */
7132
void
7133
22.0M
xmlParseReference(xmlParserCtxt *ctxt) {
7134
22.0M
    xmlEntityPtr ent = NULL;
7135
22.0M
    const xmlChar *name;
7136
22.0M
    xmlChar *val;
7137
7138
22.0M
    if (RAW != '&')
7139
0
        return;
7140
7141
    /*
7142
     * Simple case of a CharRef
7143
     */
7144
22.0M
    if (NXT(1) == '#') {
7145
53.7k
  int i = 0;
7146
53.7k
  xmlChar out[16];
7147
53.7k
  int value = xmlParseCharRef(ctxt);
7148
7149
53.7k
  if (value == 0)
7150
20.6k
      return;
7151
7152
        /*
7153
         * Just encode the value in UTF-8
7154
         */
7155
33.0k
        COPY_BUF(out, i, value);
7156
33.0k
        out[i] = 0;
7157
33.0k
        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
7158
33.0k
            (!ctxt->disableSAX))
7159
11.3k
            ctxt->sax->characters(ctxt->userData, out, i);
7160
33.0k
  return;
7161
53.7k
    }
7162
7163
    /*
7164
     * We are seeing an entity reference
7165
     */
7166
21.9M
    name = xmlParseEntityRefInternal(ctxt);
7167
21.9M
    if (name == NULL)
7168
52.7k
        return;
7169
21.9M
    ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 0);
7170
21.9M
    if (ent == NULL) {
7171
        /*
7172
         * Create a reference for undeclared entities.
7173
         */
7174
52.1k
        if ((ctxt->replaceEntities == 0) &&
7175
0
            (ctxt->sax != NULL) &&
7176
0
            (ctxt->disableSAX == 0) &&
7177
0
            (ctxt->sax->reference != NULL)) {
7178
0
            ctxt->sax->reference(ctxt->userData, name);
7179
0
        }
7180
52.1k
        return;
7181
52.1k
    }
7182
21.8M
    if (!ctxt->wellFormed)
7183
109k
  return;
7184
7185
    /* special case of predefined entities */
7186
21.7M
    if ((ent->name == NULL) ||
7187
21.7M
        (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
7188
21.7M
  val = ent->content;
7189
21.7M
  if (val == NULL) return;
7190
  /*
7191
   * inline the entity.
7192
   */
7193
21.7M
  if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
7194
21.7M
      (!ctxt->disableSAX))
7195
21.7M
      ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
7196
21.7M
  return;
7197
21.7M
    }
7198
7199
    /*
7200
     * Some users try to parse entities on their own and used to set
7201
     * the renamed "checked" member. Fix the flags to cover this
7202
     * case.
7203
     */
7204
28.2k
    if (((ent->flags & XML_ENT_PARSED) == 0) && (ent->children != NULL))
7205
0
        ent->flags |= XML_ENT_PARSED;
7206
7207
    /*
7208
     * The first reference to the entity trigger a parsing phase
7209
     * where the ent->children is filled with the result from
7210
     * the parsing.
7211
     * Note: external parsed entities will not be loaded, it is not
7212
     * required for a non-validating parser, unless the parsing option
7213
     * of validating, or substituting entities were given. Doing so is
7214
     * far more secure as the parser will only process data coming from
7215
     * the document entity by default.
7216
     *
7217
     * FIXME: This doesn't work correctly since entities can be
7218
     * expanded with different namespace declarations in scope.
7219
     * For example:
7220
     *
7221
     * <!DOCTYPE doc [
7222
     *   <!ENTITY ent "<ns:elem/>">
7223
     * ]>
7224
     * <doc>
7225
     *   <decl1 xmlns:ns="urn:ns1">
7226
     *     &ent;
7227
     *   </decl1>
7228
     *   <decl2 xmlns:ns="urn:ns2">
7229
     *     &ent;
7230
     *   </decl2>
7231
     * </doc>
7232
     *
7233
     * Proposed fix:
7234
     *
7235
     * - Ignore current namespace declarations when parsing the
7236
     *   entity. If a prefix can't be resolved, don't report an error
7237
     *   but mark it as unresolved.
7238
     * - Try to resolve these prefixes when expanding the entity.
7239
     *   This will require a specialized version of xmlStaticCopyNode
7240
     *   which can also make use of the namespace hash table to avoid
7241
     *   quadratic behavior.
7242
     *
7243
     * Alternatively, we could simply reparse the entity on each
7244
     * expansion like we already do with custom SAX callbacks.
7245
     * External entity content should be cached in this case.
7246
     */
7247
28.2k
    if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
7248
194
        (((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
7249
0
         ((ctxt->replaceEntities) ||
7250
28.0k
          (ctxt->validate)))) {
7251
28.0k
        if ((ent->flags & XML_ENT_PARSED) == 0) {
7252
1.20k
            xmlCtxtParseEntity(ctxt, ent);
7253
26.8k
        } else if (ent->children == NULL) {
7254
            /*
7255
             * Probably running in SAX mode and the callbacks don't
7256
             * build the entity content. Parse the entity again.
7257
             *
7258
             * This will also be triggered in normal tree builder mode
7259
             * if an entity happens to be empty, causing unnecessary
7260
             * reloads. It's hard to come up with a reliable check in
7261
             * which mode we're running.
7262
             */
7263
11.3k
            xmlCtxtParseEntity(ctxt, ent);
7264
11.3k
        }
7265
28.0k
    }
7266
7267
    /*
7268
     * We also check for amplification if entities aren't substituted.
7269
     * They might be expanded later.
7270
     */
7271
28.2k
    if (xmlParserEntityCheck(ctxt, ent->expandedSize))
7272
66
        return;
7273
7274
28.2k
    if ((ctxt->sax == NULL) || (ctxt->disableSAX))
7275
236
        return;
7276
7277
27.9k
    if (ctxt->replaceEntities == 0) {
7278
  /*
7279
   * Create a reference
7280
   */
7281
0
        if (ctxt->sax->reference != NULL)
7282
0
      ctxt->sax->reference(ctxt->userData, ent->name);
7283
27.9k
    } else if ((ent->children != NULL) && (ctxt->node != NULL)) {
7284
16.4k
        xmlNodePtr copy, cur;
7285
7286
        /*
7287
         * Seems we are generating the DOM content, copy the tree
7288
   */
7289
16.4k
        cur = ent->children;
7290
7291
        /*
7292
         * Handle first text node with SAX to coalesce text efficiently
7293
         */
7294
16.4k
        if ((cur->type == XML_TEXT_NODE) ||
7295
14.7k
            (cur->type == XML_CDATA_SECTION_NODE)) {
7296
14.7k
            int len = xmlStrlen(cur->content);
7297
7298
14.7k
            if ((cur->type == XML_TEXT_NODE) ||
7299
14.4k
                (ctxt->options & XML_PARSE_NOCDATA)) {
7300
14.4k
                if (ctxt->sax->characters != NULL)
7301
14.4k
                    ctxt->sax->characters(ctxt->userData, cur->content, len);
7302
14.4k
            } else {
7303
302
                if (ctxt->sax->cdataBlock != NULL)
7304
302
                    ctxt->sax->cdataBlock(ctxt->userData, cur->content, len);
7305
302
            }
7306
7307
14.7k
            cur = cur->next;
7308
14.7k
        }
7309
7310
24.2k
        while (cur != NULL) {
7311
9.29k
            xmlNodePtr last;
7312
7313
            /*
7314
             * Handle last text node with SAX to coalesce text efficiently
7315
             */
7316
9.29k
            if ((cur->next == NULL) &&
7317
3.30k
                ((cur->type == XML_TEXT_NODE) ||
7318
2.26k
                 (cur->type == XML_CDATA_SECTION_NODE))) {
7319
1.40k
                int len = xmlStrlen(cur->content);
7320
7321
1.40k
                if ((cur->type == XML_TEXT_NODE) ||
7322
1.04k
                    (ctxt->options & XML_PARSE_NOCDATA)) {
7323
1.04k
                    if (ctxt->sax->characters != NULL)
7324
1.04k
                        ctxt->sax->characters(ctxt->userData, cur->content,
7325
1.04k
                                              len);
7326
1.04k
                } else {
7327
368
                    if (ctxt->sax->cdataBlock != NULL)
7328
368
                        ctxt->sax->cdataBlock(ctxt->userData, cur->content,
7329
368
                                              len);
7330
368
                }
7331
7332
1.40k
                break;
7333
1.40k
            }
7334
7335
            /*
7336
             * Reset coalesce buffer stats only for non-text nodes.
7337
             */
7338
7.88k
            ctxt->nodemem = 0;
7339
7.88k
            ctxt->nodelen = 0;
7340
7341
7.88k
            copy = xmlDocCopyNode(cur, ctxt->myDoc, 1);
7342
7343
7.88k
            if (copy == NULL) {
7344
0
                xmlErrMemory(ctxt);
7345
0
                break;
7346
0
            }
7347
7348
7.88k
            if (ctxt->parseMode == XML_PARSE_READER) {
7349
                /* Needed for reader */
7350
0
                copy->extra = cur->extra;
7351
                /* Maybe needed for reader */
7352
0
                copy->_private = cur->_private;
7353
0
            }
7354
7355
7.88k
            copy->parent = ctxt->node;
7356
7.88k
            last = ctxt->node->last;
7357
7.88k
            if (last == NULL) {
7358
497
                ctxt->node->children = copy;
7359
7.38k
            } else {
7360
7.38k
                last->next = copy;
7361
7.38k
                copy->prev = last;
7362
7.38k
            }
7363
7.88k
            ctxt->node->last = copy;
7364
7365
7.88k
            cur = cur->next;
7366
7.88k
        }
7367
16.4k
    }
7368
27.9k
}
7369
7370
static void
7371
144k
xmlHandleUndeclaredEntity(xmlParserCtxtPtr ctxt, const xmlChar *name) {
7372
    /*
7373
     * [ WFC: Entity Declared ]
7374
     * In a document without any DTD, a document with only an
7375
     * internal DTD subset which contains no parameter entity
7376
     * references, or a document with "standalone='yes'", the
7377
     * Name given in the entity reference must match that in an
7378
     * entity declaration, except that well-formed documents
7379
     * need not declare any of the following entities: amp, lt,
7380
     * gt, apos, quot.
7381
     * The declaration of a parameter entity must precede any
7382
     * reference to it.
7383
     * Similarly, the declaration of a general entity must
7384
     * precede any reference to it which appears in a default
7385
     * value in an attribute-list declaration. Note that if
7386
     * entities are declared in the external subset or in
7387
     * external parameter entities, a non-validating processor
7388
     * is not obligated to read and process their declarations;
7389
     * for such documents, the rule that an entity must be
7390
     * declared is a well-formedness constraint only if
7391
     * standalone='yes'.
7392
     */
7393
144k
    if ((ctxt->standalone == 1) ||
7394
143k
        ((ctxt->hasExternalSubset == 0) &&
7395
138k
         (ctxt->hasPErefs == 0))) {
7396
127k
        xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
7397
127k
                          "Entity '%s' not defined\n", name);
7398
127k
#ifdef LIBXML_VALID_ENABLED
7399
127k
    } else if (ctxt->validate) {
7400
        /*
7401
         * [ VC: Entity Declared ]
7402
         * In a document with an external subset or external
7403
         * parameter entities with "standalone='no'", ...
7404
         * ... The declaration of a parameter entity must
7405
         * precede any reference to it...
7406
         */
7407
0
        xmlValidityError(ctxt, XML_ERR_UNDECLARED_ENTITY,
7408
0
                         "Entity '%s' not defined\n", name, NULL);
7409
0
#endif
7410
16.7k
    } else if ((ctxt->loadsubset & ~XML_SKIP_IDS) ||
7411
1.24k
               ((ctxt->replaceEntities) &&
7412
16.7k
                ((ctxt->options & XML_PARSE_NO_XXE) == 0))) {
7413
        /*
7414
         * Also raise a non-fatal error
7415
         *
7416
         * - if the external subset is loaded and all entity declarations
7417
         *   should be available, or
7418
         * - entity substition was requested without restricting
7419
         *   external entity access.
7420
         */
7421
16.7k
        xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
7422
16.7k
                     "Entity '%s' not defined\n", name);
7423
16.7k
    } else {
7424
0
        xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7425
0
                      "Entity '%s' not defined\n", name, NULL);
7426
0
    }
7427
7428
144k
    ctxt->valid = 0;
7429
144k
}
7430
7431
static xmlEntityPtr
7432
22.9M
xmlLookupGeneralEntity(xmlParserCtxtPtr ctxt, const xmlChar *name, int inAttr) {
7433
22.9M
    xmlEntityPtr ent = NULL;
7434
7435
    /*
7436
     * Predefined entities override any extra definition
7437
     */
7438
22.9M
    if ((ctxt->options & XML_PARSE_OLDSAX) == 0) {
7439
22.9M
        ent = xmlGetPredefinedEntity(name);
7440
22.9M
        if (ent != NULL)
7441
21.9M
            return(ent);
7442
22.9M
    }
7443
7444
    /*
7445
     * Ask first SAX for entity resolution, otherwise try the
7446
     * entities which may have stored in the parser context.
7447
     */
7448
993k
    if (ctxt->sax != NULL) {
7449
993k
  if (ctxt->sax->getEntity != NULL)
7450
993k
      ent = ctxt->sax->getEntity(ctxt->userData, name);
7451
993k
  if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
7452
6.92k
      (ctxt->options & XML_PARSE_OLDSAX))
7453
0
      ent = xmlGetPredefinedEntity(name);
7454
993k
  if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
7455
6.92k
      (ctxt->userData==ctxt)) {
7456
6.92k
      ent = xmlSAX2GetEntity(ctxt, name);
7457
6.92k
  }
7458
993k
    }
7459
7460
993k
    if (ent == NULL) {
7461
141k
        xmlHandleUndeclaredEntity(ctxt, name);
7462
141k
    }
7463
7464
    /*
7465
     * [ WFC: Parsed Entity ]
7466
     * An entity reference must not contain the name of an
7467
     * unparsed entity
7468
     */
7469
852k
    else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
7470
194
  xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
7471
194
     "Entity reference to unparsed entity %s\n", name);
7472
194
        ent = NULL;
7473
194
    }
7474
7475
    /*
7476
     * [ WFC: No External Entity References ]
7477
     * Attribute values cannot contain direct or indirect
7478
     * entity references to external entities.
7479
     */
7480
852k
    else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
7481
968
        if (inAttr) {
7482
513
            xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
7483
513
                 "Attribute references external entity '%s'\n", name);
7484
513
            ent = NULL;
7485
513
        }
7486
968
    }
7487
7488
993k
    return(ent);
7489
22.9M
}
7490
7491
/**
7492
 * Parse an entity reference. Always consumes '&'.
7493
 *
7494
 *     [68] EntityRef ::= '&' Name ';'
7495
 *
7496
 * @param ctxt  an XML parser context
7497
 * @returns the name, or NULL in case of error.
7498
 */
7499
static const xmlChar *
7500
22.2M
xmlParseEntityRefInternal(xmlParserCtxtPtr ctxt) {
7501
22.2M
    const xmlChar *name;
7502
7503
22.2M
    GROW;
7504
7505
22.2M
    if (RAW != '&')
7506
0
        return(NULL);
7507
22.2M
    NEXT;
7508
22.2M
    name = xmlParseName(ctxt);
7509
22.2M
    if (name == NULL) {
7510
47.8k
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7511
47.8k
           "xmlParseEntityRef: no name\n");
7512
47.8k
        return(NULL);
7513
47.8k
    }
7514
22.1M
    if (RAW != ';') {
7515
37.3k
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7516
37.3k
  return(NULL);
7517
37.3k
    }
7518
22.1M
    NEXT;
7519
7520
22.1M
    return(name);
7521
22.1M
}
7522
7523
/**
7524
 * @deprecated Internal function, don't use.
7525
 *
7526
 * @param ctxt  an XML parser context
7527
 * @returns the xmlEntity if found, or NULL otherwise.
7528
 */
7529
xmlEntity *
7530
0
xmlParseEntityRef(xmlParserCtxt *ctxt) {
7531
0
    const xmlChar *name;
7532
7533
0
    if (ctxt == NULL)
7534
0
        return(NULL);
7535
7536
0
    name = xmlParseEntityRefInternal(ctxt);
7537
0
    if (name == NULL)
7538
0
        return(NULL);
7539
7540
0
    return(xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 0));
7541
0
}
7542
7543
/**
7544
 * Parse ENTITY references declarations, but this version parses it from
7545
 * a string value.
7546
 *
7547
 *     [68] EntityRef ::= '&' Name ';'
7548
 *
7549
 * [ WFC: Entity Declared ]
7550
 * In a document without any DTD, a document with only an internal DTD
7551
 * subset which contains no parameter entity references, or a document
7552
 * with "standalone='yes'", the Name given in the entity reference
7553
 * must match that in an entity declaration, except that well-formed
7554
 * documents need not declare any of the following entities: amp, lt,
7555
 * gt, apos, quot.  The declaration of a parameter entity must precede
7556
 * any reference to it.  Similarly, the declaration of a general entity
7557
 * must precede any reference to it which appears in a default value in an
7558
 * attribute-list declaration. Note that if entities are declared in the
7559
 * external subset or in external parameter entities, a non-validating
7560
 * processor is not obligated to read and process their declarations;
7561
 * for such documents, the rule that an entity must be declared is a
7562
 * well-formedness constraint only if standalone='yes'.
7563
 *
7564
 * [ WFC: Parsed Entity ]
7565
 * An entity reference must not contain the name of an unparsed entity
7566
 *
7567
 * @param ctxt  an XML parser context
7568
 * @param str  a pointer to an index in the string
7569
 * @returns the xmlEntity if found, or NULL otherwise. The str pointer
7570
 * is updated to the current location in the string.
7571
 */
7572
static xmlChar *
7573
834k
xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
7574
834k
    xmlChar *name;
7575
834k
    const xmlChar *ptr;
7576
834k
    xmlChar cur;
7577
7578
834k
    if ((str == NULL) || (*str == NULL))
7579
0
        return(NULL);
7580
834k
    ptr = *str;
7581
834k
    cur = *ptr;
7582
834k
    if (cur != '&')
7583
0
  return(NULL);
7584
7585
834k
    ptr++;
7586
834k
    name = xmlParseStringName(ctxt, &ptr);
7587
834k
    if (name == NULL) {
7588
8
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7589
8
           "xmlParseStringEntityRef: no name\n");
7590
8
  *str = ptr;
7591
8
  return(NULL);
7592
8
    }
7593
834k
    if (*ptr != ';') {
7594
12
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7595
12
        xmlFree(name);
7596
12
  *str = ptr;
7597
12
  return(NULL);
7598
12
    }
7599
834k
    ptr++;
7600
7601
834k
    *str = ptr;
7602
834k
    return(name);
7603
834k
}
7604
7605
/**
7606
 * Parse a parameter entity reference. Always consumes '%'.
7607
 *
7608
 * The entity content is handled directly by pushing it's content as
7609
 * a new input stream.
7610
 *
7611
 *     [69] PEReference ::= '%' Name ';'
7612
 *
7613
 * [ WFC: No Recursion ]
7614
 * A parsed entity must not contain a recursive
7615
 * reference to itself, either directly or indirectly.
7616
 *
7617
 * [ WFC: Entity Declared ]
7618
 * In a document without any DTD, a document with only an internal DTD
7619
 * subset which contains no parameter entity references, or a document
7620
 * with "standalone='yes'", ...  ... The declaration of a parameter
7621
 * entity must precede any reference to it...
7622
 *
7623
 * [ VC: Entity Declared ]
7624
 * In a document with an external subset or external parameter entities
7625
 * with "standalone='no'", ...  ... The declaration of a parameter entity
7626
 * must precede any reference to it...
7627
 *
7628
 * [ WFC: In DTD ]
7629
 * Parameter-entity references may only appear in the DTD.
7630
 * NOTE: misleading but this is handled.
7631
 *
7632
 * @param ctxt  an XML parser context
7633
 * @param markupDecl  whether the PERef starts a markup declaration
7634
 */
7635
static void
7636
70.0k
xmlParsePERefInternal(xmlParserCtxt *ctxt, int markupDecl) {
7637
70.0k
    const xmlChar *name;
7638
70.0k
    xmlEntityPtr entity = NULL;
7639
70.0k
    xmlParserInputPtr input;
7640
7641
70.0k
    if (RAW != '%')
7642
0
        return;
7643
70.0k
    NEXT;
7644
70.0k
    name = xmlParseName(ctxt);
7645
70.0k
    if (name == NULL) {
7646
25.7k
  xmlFatalErrMsg(ctxt, XML_ERR_PEREF_NO_NAME, "PEReference: no name\n");
7647
25.7k
  return;
7648
25.7k
    }
7649
44.3k
    if (RAW != ';') {
7650
3.39k
  xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
7651
3.39k
        return;
7652
3.39k
    }
7653
7654
40.9k
    NEXT;
7655
7656
    /* Must be set before xmlHandleUndeclaredEntity */
7657
40.9k
    ctxt->hasPErefs = 1;
7658
7659
    /*
7660
     * Request the entity from SAX
7661
     */
7662
40.9k
    if ((ctxt->sax != NULL) &&
7663
40.9k
  (ctxt->sax->getParameterEntity != NULL))
7664
40.9k
  entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
7665
7666
40.9k
    if (entity == NULL) {
7667
1.84k
        xmlHandleUndeclaredEntity(ctxt, name);
7668
39.1k
    } else {
7669
  /*
7670
   * Internal checking in case the entity quest barfed
7671
   */
7672
39.1k
  if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
7673
197
      (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
7674
0
      xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7675
0
      "Internal: %%%s; is not a parameter entity\n",
7676
0
        name, NULL);
7677
39.1k
  } else {
7678
39.1k
      if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
7679
197
                ((ctxt->options & XML_PARSE_NO_XXE) ||
7680
0
     (((ctxt->loadsubset & ~XML_SKIP_IDS) == 0) &&
7681
0
      (ctxt->replaceEntities == 0) &&
7682
0
      (ctxt->validate == 0))))
7683
197
    return;
7684
7685
38.9k
            if (entity->flags & XML_ENT_EXPANDING) {
7686
23
                xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
7687
23
                return;
7688
23
            }
7689
7690
38.8k
      input = xmlNewEntityInputStream(ctxt, entity);
7691
38.8k
      if (xmlCtxtPushInput(ctxt, input) < 0) {
7692
0
                xmlFreeInputStream(input);
7693
0
    return;
7694
0
            }
7695
7696
38.8k
            entity->flags |= XML_ENT_EXPANDING;
7697
7698
38.8k
            if (markupDecl)
7699
35.3k
                input->flags |= XML_INPUT_MARKUP_DECL;
7700
7701
38.8k
            GROW;
7702
7703
38.8k
      if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
7704
0
                xmlDetectEncoding(ctxt);
7705
7706
0
                if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
7707
0
                    (IS_BLANK_CH(NXT(5)))) {
7708
0
                    xmlParseTextDecl(ctxt);
7709
0
                }
7710
0
            }
7711
38.8k
  }
7712
39.1k
    }
7713
40.9k
}
7714
7715
/**
7716
 * Parse a parameter entity reference.
7717
 *
7718
 * @deprecated Internal function, don't use.
7719
 *
7720
 * @param ctxt  an XML parser context
7721
 */
7722
void
7723
0
xmlParsePEReference(xmlParserCtxt *ctxt) {
7724
0
    xmlParsePERefInternal(ctxt, 0);
7725
0
}
7726
7727
/**
7728
 * Load the content of an entity.
7729
 *
7730
 * @param ctxt  an XML parser context
7731
 * @param entity  an unloaded system entity
7732
 * @returns 0 in case of success and -1 in case of failure
7733
 */
7734
static int
7735
0
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
7736
0
    xmlParserInputPtr oldinput, input = NULL;
7737
0
    xmlParserInputPtr *oldinputTab;
7738
0
    xmlChar *oldencoding;
7739
0
    xmlChar *content = NULL;
7740
0
    xmlResourceType rtype;
7741
0
    size_t length, i;
7742
0
    int oldinputNr, oldinputMax;
7743
0
    int ret = -1;
7744
0
    int res;
7745
7746
0
    if ((ctxt == NULL) || (entity == NULL) ||
7747
0
        ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) &&
7748
0
   (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) ||
7749
0
  (entity->content != NULL)) {
7750
0
  xmlFatalErr(ctxt, XML_ERR_ARGUMENT,
7751
0
              "xmlLoadEntityContent parameter error");
7752
0
        return(-1);
7753
0
    }
7754
7755
0
    if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)
7756
0
        rtype = XML_RESOURCE_PARAMETER_ENTITY;
7757
0
    else
7758
0
        rtype = XML_RESOURCE_GENERAL_ENTITY;
7759
7760
0
    input = xmlLoadResource(ctxt, (char *) entity->URI,
7761
0
                            (char *) entity->ExternalID, rtype);
7762
0
    if (input == NULL)
7763
0
        return(-1);
7764
7765
0
    oldinput = ctxt->input;
7766
0
    oldinputNr = ctxt->inputNr;
7767
0
    oldinputMax = ctxt->inputMax;
7768
0
    oldinputTab = ctxt->inputTab;
7769
0
    oldencoding = ctxt->encoding;
7770
7771
0
    ctxt->input = NULL;
7772
0
    ctxt->inputNr = 0;
7773
0
    ctxt->inputMax = 1;
7774
0
    ctxt->encoding = NULL;
7775
0
    ctxt->inputTab = xmlMalloc(sizeof(xmlParserInputPtr));
7776
0
    if (ctxt->inputTab == NULL) {
7777
0
        xmlErrMemory(ctxt);
7778
0
        xmlFreeInputStream(input);
7779
0
        goto error;
7780
0
    }
7781
7782
0
    xmlBufResetInput(input->buf->buffer, input);
7783
7784
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
7785
0
        xmlFreeInputStream(input);
7786
0
        goto error;
7787
0
    }
7788
7789
0
    xmlDetectEncoding(ctxt);
7790
7791
    /*
7792
     * Parse a possible text declaration first
7793
     */
7794
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
7795
0
  xmlParseTextDecl(ctxt);
7796
        /*
7797
         * An XML-1.0 document can't reference an entity not XML-1.0
7798
         */
7799
0
        if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
7800
0
            (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
7801
0
            xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
7802
0
                           "Version mismatch between document and entity\n");
7803
0
        }
7804
0
    }
7805
7806
0
    length = input->cur - input->base;
7807
0
    xmlBufShrink(input->buf->buffer, length);
7808
0
    xmlSaturatedAdd(&ctxt->sizeentities, length);
7809
7810
0
    while ((res = xmlParserInputBufferGrow(input->buf, 4096)) > 0)
7811
0
        ;
7812
7813
0
    xmlBufResetInput(input->buf->buffer, input);
7814
7815
0
    if (res < 0) {
7816
0
        xmlCtxtErrIO(ctxt, input->buf->error, NULL);
7817
0
        goto error;
7818
0
    }
7819
7820
0
    length = xmlBufUse(input->buf->buffer);
7821
0
    if (length > INT_MAX) {
7822
0
        xmlErrMemory(ctxt);
7823
0
        goto error;
7824
0
    }
7825
7826
0
    content = xmlStrndup(xmlBufContent(input->buf->buffer), length);
7827
0
    if (content == NULL) {
7828
0
        xmlErrMemory(ctxt);
7829
0
        goto error;
7830
0
    }
7831
7832
0
    for (i = 0; i < length; ) {
7833
0
        int clen = length - i;
7834
0
        int c = xmlGetUTF8Char(content + i, &clen);
7835
7836
0
        if ((c < 0) || (!IS_CHAR(c))) {
7837
0
            xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
7838
0
                              "xmlLoadEntityContent: invalid char value %d\n",
7839
0
                              content[i]);
7840
0
            goto error;
7841
0
        }
7842
0
        i += clen;
7843
0
    }
7844
7845
0
    xmlSaturatedAdd(&ctxt->sizeentities, length);
7846
0
    entity->content = content;
7847
0
    entity->length = length;
7848
0
    content = NULL;
7849
0
    ret = 0;
7850
7851
0
error:
7852
0
    while (ctxt->inputNr > 0)
7853
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
7854
0
    xmlFree(ctxt->inputTab);
7855
0
    xmlFree(ctxt->encoding);
7856
7857
0
    ctxt->input = oldinput;
7858
0
    ctxt->inputNr = oldinputNr;
7859
0
    ctxt->inputMax = oldinputMax;
7860
0
    ctxt->inputTab = oldinputTab;
7861
0
    ctxt->encoding = oldencoding;
7862
7863
0
    xmlFree(content);
7864
7865
0
    return(ret);
7866
0
}
7867
7868
/**
7869
 * Parse PEReference declarations
7870
 *
7871
 *     [69] PEReference ::= '%' Name ';'
7872
 *
7873
 * [ WFC: No Recursion ]
7874
 * A parsed entity must not contain a recursive
7875
 * reference to itself, either directly or indirectly.
7876
 *
7877
 * [ WFC: Entity Declared ]
7878
 * In a document without any DTD, a document with only an internal DTD
7879
 * subset which contains no parameter entity references, or a document
7880
 * with "standalone='yes'", ...  ... The declaration of a parameter
7881
 * entity must precede any reference to it...
7882
 *
7883
 * [ VC: Entity Declared ]
7884
 * In a document with an external subset or external parameter entities
7885
 * with "standalone='no'", ...  ... The declaration of a parameter entity
7886
 * must precede any reference to it...
7887
 *
7888
 * [ WFC: In DTD ]
7889
 * Parameter-entity references may only appear in the DTD.
7890
 * NOTE: misleading but this is handled.
7891
 *
7892
 * @param ctxt  an XML parser context
7893
 * @param str  a pointer to an index in the string
7894
 * @returns the string of the entity content.
7895
 *         str is updated to the current value of the index
7896
 */
7897
static xmlEntityPtr
7898
5.88k
xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
7899
5.88k
    const xmlChar *ptr;
7900
5.88k
    xmlChar cur;
7901
5.88k
    xmlChar *name;
7902
5.88k
    xmlEntityPtr entity = NULL;
7903
7904
5.88k
    if ((str == NULL) || (*str == NULL)) return(NULL);
7905
5.88k
    ptr = *str;
7906
5.88k
    cur = *ptr;
7907
5.88k
    if (cur != '%')
7908
0
        return(NULL);
7909
5.88k
    ptr++;
7910
5.88k
    name = xmlParseStringName(ctxt, &ptr);
7911
5.88k
    if (name == NULL) {
7912
2.16k
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7913
2.16k
           "xmlParseStringPEReference: no name\n");
7914
2.16k
  *str = ptr;
7915
2.16k
  return(NULL);
7916
2.16k
    }
7917
3.72k
    cur = *ptr;
7918
3.72k
    if (cur != ';') {
7919
2.08k
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7920
2.08k
  xmlFree(name);
7921
2.08k
  *str = ptr;
7922
2.08k
  return(NULL);
7923
2.08k
    }
7924
1.63k
    ptr++;
7925
7926
    /* Must be set before xmlHandleUndeclaredEntity */
7927
1.63k
    ctxt->hasPErefs = 1;
7928
7929
    /*
7930
     * Request the entity from SAX
7931
     */
7932
1.63k
    if ((ctxt->sax != NULL) &&
7933
1.63k
  (ctxt->sax->getParameterEntity != NULL))
7934
1.63k
  entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
7935
7936
1.63k
    if (entity == NULL) {
7937
1.01k
        xmlHandleUndeclaredEntity(ctxt, name);
7938
1.01k
    } else {
7939
  /*
7940
   * Internal checking in case the entity quest barfed
7941
   */
7942
628
  if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
7943
194
      (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
7944
0
      xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7945
0
        "%%%s; is not a parameter entity\n",
7946
0
        name, NULL);
7947
0
  }
7948
628
    }
7949
7950
1.63k
    xmlFree(name);
7951
1.63k
    *str = ptr;
7952
1.63k
    return(entity);
7953
3.72k
}
7954
7955
/**
7956
 * Parse a DOCTYPE declaration
7957
 *
7958
 * @deprecated Internal function, don't use.
7959
 *
7960
 *     [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
7961
 *                          ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
7962
 *
7963
 * [ VC: Root Element Type ]
7964
 * The Name in the document type declaration must match the element
7965
 * type of the root element.
7966
 *
7967
 * @param ctxt  an XML parser context
7968
 */
7969
7970
void
7971
17.1k
xmlParseDocTypeDecl(xmlParserCtxt *ctxt) {
7972
17.1k
    const xmlChar *name = NULL;
7973
17.1k
    xmlChar *publicId = NULL;
7974
17.1k
    xmlChar *URI = NULL;
7975
7976
    /*
7977
     * We know that '<!DOCTYPE' has been detected.
7978
     */
7979
17.1k
    SKIP(9);
7980
7981
17.1k
    if (SKIP_BLANKS == 0) {
7982
6.69k
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7983
6.69k
                       "Space required after 'DOCTYPE'\n");
7984
6.69k
    }
7985
7986
    /*
7987
     * Parse the DOCTYPE name.
7988
     */
7989
17.1k
    name = xmlParseName(ctxt);
7990
17.1k
    if (name == NULL) {
7991
5.68k
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7992
5.68k
           "xmlParseDocTypeDecl : no DOCTYPE name !\n");
7993
5.68k
    }
7994
17.1k
    ctxt->intSubName = name;
7995
7996
17.1k
    SKIP_BLANKS;
7997
7998
    /*
7999
     * Check for public and system identifier (URI)
8000
     */
8001
17.1k
    URI = xmlParseExternalID(ctxt, &publicId, 1);
8002
8003
17.1k
    if ((URI != NULL) || (publicId != NULL)) {
8004
458
        ctxt->hasExternalSubset = 1;
8005
458
    }
8006
17.1k
    ctxt->extSubURI = URI;
8007
17.1k
    ctxt->extSubSystem = publicId;
8008
8009
17.1k
    SKIP_BLANKS;
8010
8011
    /*
8012
     * Create and update the internal subset.
8013
     */
8014
17.1k
    if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
8015
17.1k
  (!ctxt->disableSAX))
8016
7.31k
  ctxt->sax->internalSubset(ctxt->userData, name, publicId, URI);
8017
8018
17.1k
    if ((RAW != '[') && (RAW != '>')) {
8019
471
  xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
8020
471
    }
8021
17.1k
}
8022
8023
/**
8024
 * Parse the internal subset declaration
8025
 *
8026
 *     [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
8027
 * @param ctxt  an XML parser context
8028
 */
8029
8030
static void
8031
16.4k
xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
8032
    /*
8033
     * Is there any DTD definition ?
8034
     */
8035
16.4k
    if (RAW == '[') {
8036
16.4k
        int oldInputNr = ctxt->inputNr;
8037
8038
16.4k
        NEXT;
8039
  /*
8040
   * Parse the succession of Markup declarations and
8041
   * PEReferences.
8042
   * Subsequence (markupdecl | PEReference | S)*
8043
   */
8044
16.4k
  SKIP_BLANKS;
8045
262k
        while (1) {
8046
262k
            if (PARSER_STOPPED(ctxt)) {
8047
42
                return;
8048
262k
            } else if (ctxt->input->cur >= ctxt->input->end) {
8049
39.1k
                if (ctxt->inputNr <= oldInputNr) {
8050
4.81k
                xmlFatalErr(ctxt, XML_ERR_INT_SUBSET_NOT_FINISHED, NULL);
8051
4.81k
                    return;
8052
4.81k
                }
8053
34.3k
                xmlPopPE(ctxt);
8054
222k
            } else if ((RAW == ']') && (ctxt->inputNr <= oldInputNr)) {
8055
3.65k
                NEXT;
8056
3.65k
                SKIP_BLANKS;
8057
3.65k
                break;
8058
219k
            } else if ((PARSER_EXTERNAL(ctxt)) &&
8059
0
                       (RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
8060
                /*
8061
                 * Conditional sections are allowed in external entities
8062
                 * included by PE References in the internal subset.
8063
                 */
8064
0
                xmlParseConditionalSections(ctxt);
8065
219k
            } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
8066
145k
                xmlParseMarkupDecl(ctxt);
8067
145k
            } else if (RAW == '%') {
8068
66.0k
                xmlParsePERefInternal(ctxt, 1);
8069
66.0k
            } else {
8070
7.97k
                xmlFatalErr(ctxt, XML_ERR_INT_SUBSET_NOT_FINISHED, NULL);
8071
8072
8.07k
                while (ctxt->inputNr > oldInputNr)
8073
96
                    xmlPopPE(ctxt);
8074
7.97k
                return;
8075
7.97k
            }
8076
245k
            SKIP_BLANKS;
8077
245k
            SHRINK;
8078
245k
            GROW;
8079
245k
        }
8080
16.4k
    }
8081
8082
    /*
8083
     * We should be at the end of the DOCTYPE declaration.
8084
     */
8085
3.65k
    if (RAW != '>') {
8086
37
        xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
8087
37
        return;
8088
37
    }
8089
3.61k
    NEXT;
8090
3.61k
}
8091
8092
#ifdef LIBXML_SAX1_ENABLED
8093
/**
8094
 * Parse an attribute
8095
 *
8096
 * @deprecated Internal function, don't use.
8097
 *
8098
 *     [41] Attribute ::= Name Eq AttValue
8099
 *
8100
 * [ WFC: No External Entity References ]
8101
 * Attribute values cannot contain direct or indirect entity references
8102
 * to external entities.
8103
 *
8104
 * [ WFC: No < in Attribute Values ]
8105
 * The replacement text of any entity referred to directly or indirectly in
8106
 * an attribute value (other than "&lt;") must not contain a <.
8107
 *
8108
 * [ VC: Attribute Value Type ]
8109
 * The attribute must have been declared; the value must be of the type
8110
 * declared for it.
8111
 *
8112
 *     [25] Eq ::= S? '=' S?
8113
 *
8114
 * With namespace:
8115
 *
8116
 *     [NS 11] Attribute ::= QName Eq AttValue
8117
 *
8118
 * Also the case QName == xmlns:??? is handled independently as a namespace
8119
 * definition.
8120
 *
8121
 * @param ctxt  an XML parser context
8122
 * @param value  a xmlChar ** used to store the value of the attribute
8123
 * @returns the attribute name, and the value in *value.
8124
 */
8125
8126
const xmlChar *
8127
0
xmlParseAttribute(xmlParserCtxt *ctxt, xmlChar **value) {
8128
0
    const xmlChar *name;
8129
0
    xmlChar *val;
8130
8131
0
    *value = NULL;
8132
0
    GROW;
8133
0
    name = xmlParseName(ctxt);
8134
0
    if (name == NULL) {
8135
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8136
0
                 "error parsing attribute name\n");
8137
0
        return(NULL);
8138
0
    }
8139
8140
    /*
8141
     * read the value
8142
     */
8143
0
    SKIP_BLANKS;
8144
0
    if (RAW == '=') {
8145
0
        NEXT;
8146
0
  SKIP_BLANKS;
8147
0
  val = xmlParseAttValue(ctxt);
8148
0
    } else {
8149
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
8150
0
         "Specification mandates value for attribute %s\n", name);
8151
0
  return(name);
8152
0
    }
8153
8154
    /*
8155
     * Check that xml:lang conforms to the specification
8156
     * No more registered as an error, just generate a warning now
8157
     * since this was deprecated in XML second edition
8158
     */
8159
0
    if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
8160
0
  if (!xmlCheckLanguageID(val)) {
8161
0
      xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
8162
0
              "Malformed value for xml:lang : %s\n",
8163
0
        val, NULL);
8164
0
  }
8165
0
    }
8166
8167
    /*
8168
     * Check that xml:space conforms to the specification
8169
     */
8170
0
    if (xmlStrEqual(name, BAD_CAST "xml:space")) {
8171
0
  if (xmlStrEqual(val, BAD_CAST "default"))
8172
0
      *(ctxt->space) = 0;
8173
0
  else if (xmlStrEqual(val, BAD_CAST "preserve"))
8174
0
      *(ctxt->space) = 1;
8175
0
  else {
8176
0
    xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
8177
0
"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
8178
0
                                 val, NULL);
8179
0
  }
8180
0
    }
8181
8182
0
    *value = val;
8183
0
    return(name);
8184
0
}
8185
8186
/**
8187
 * Parse a start tag. Always consumes '<'.
8188
 *
8189
 * @deprecated Internal function, don't use.
8190
 *
8191
 *     [40] STag ::= '<' Name (S Attribute)* S? '>'
8192
 *
8193
 * [ WFC: Unique Att Spec ]
8194
 * No attribute name may appear more than once in the same start-tag or
8195
 * empty-element tag.
8196
 *
8197
 *     [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
8198
 *
8199
 * [ WFC: Unique Att Spec ]
8200
 * No attribute name may appear more than once in the same start-tag or
8201
 * empty-element tag.
8202
 *
8203
 * With namespace:
8204
 *
8205
 *     [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
8206
 *
8207
 *     [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
8208
 *
8209
 * @param ctxt  an XML parser context
8210
 * @returns the element name parsed
8211
 */
8212
8213
const xmlChar *
8214
0
xmlParseStartTag(xmlParserCtxt *ctxt) {
8215
0
    const xmlChar *name;
8216
0
    const xmlChar *attname;
8217
0
    xmlChar *attvalue;
8218
0
    const xmlChar **atts = ctxt->atts;
8219
0
    int nbatts = 0;
8220
0
    int maxatts = ctxt->maxatts;
8221
0
    int i;
8222
8223
0
    if (RAW != '<') return(NULL);
8224
0
    NEXT1;
8225
8226
0
    name = xmlParseName(ctxt);
8227
0
    if (name == NULL) {
8228
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8229
0
       "xmlParseStartTag: invalid element name\n");
8230
0
        return(NULL);
8231
0
    }
8232
8233
    /*
8234
     * Now parse the attributes, it ends up with the ending
8235
     *
8236
     * (S Attribute)* S?
8237
     */
8238
0
    SKIP_BLANKS;
8239
0
    GROW;
8240
8241
0
    while (((RAW != '>') &&
8242
0
     ((RAW != '/') || (NXT(1) != '>')) &&
8243
0
     (IS_BYTE_CHAR(RAW))) && (PARSER_STOPPED(ctxt) == 0)) {
8244
0
  attname = xmlParseAttribute(ctxt, &attvalue);
8245
0
        if (attname == NULL)
8246
0
      break;
8247
0
        if (attvalue != NULL) {
8248
      /*
8249
       * [ WFC: Unique Att Spec ]
8250
       * No attribute name may appear more than once in the same
8251
       * start-tag or empty-element tag.
8252
       */
8253
0
      for (i = 0; i < nbatts;i += 2) {
8254
0
          if (xmlStrEqual(atts[i], attname)) {
8255
0
        xmlErrAttributeDup(ctxt, NULL, attname);
8256
0
        goto failed;
8257
0
    }
8258
0
      }
8259
      /*
8260
       * Add the pair to atts
8261
       */
8262
0
      if (nbatts + 4 > maxatts) {
8263
0
          const xmlChar **n;
8264
0
                int newSize;
8265
8266
0
                newSize = xmlGrowCapacity(maxatts, sizeof(n[0]) * 2,
8267
0
                                          11, XML_MAX_ATTRS);
8268
0
                if (newSize < 0) {
8269
0
        xmlErrMemory(ctxt);
8270
0
        goto failed;
8271
0
    }
8272
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
8273
0
                if (newSize < 2)
8274
0
                    newSize = 2;
8275
0
#endif
8276
0
          n = xmlRealloc(atts, newSize * sizeof(n[0]) * 2);
8277
0
    if (n == NULL) {
8278
0
        xmlErrMemory(ctxt);
8279
0
        goto failed;
8280
0
    }
8281
0
    atts = n;
8282
0
                maxatts = newSize * 2;
8283
0
    ctxt->atts = atts;
8284
0
    ctxt->maxatts = maxatts;
8285
0
      }
8286
8287
0
      atts[nbatts++] = attname;
8288
0
      atts[nbatts++] = attvalue;
8289
0
      atts[nbatts] = NULL;
8290
0
      atts[nbatts + 1] = NULL;
8291
8292
0
            attvalue = NULL;
8293
0
  }
8294
8295
0
failed:
8296
8297
0
        if (attvalue != NULL)
8298
0
            xmlFree(attvalue);
8299
8300
0
  GROW
8301
0
  if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
8302
0
      break;
8303
0
  if (SKIP_BLANKS == 0) {
8304
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8305
0
         "attributes construct error\n");
8306
0
  }
8307
0
  SHRINK;
8308
0
        GROW;
8309
0
    }
8310
8311
    /*
8312
     * SAX: Start of Element !
8313
     */
8314
0
    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
8315
0
  (!ctxt->disableSAX)) {
8316
0
  if (nbatts > 0)
8317
0
      ctxt->sax->startElement(ctxt->userData, name, atts);
8318
0
  else
8319
0
      ctxt->sax->startElement(ctxt->userData, name, NULL);
8320
0
    }
8321
8322
0
    if (atts != NULL) {
8323
        /* Free only the content strings */
8324
0
        for (i = 1;i < nbatts;i+=2)
8325
0
      if (atts[i] != NULL)
8326
0
         xmlFree((xmlChar *) atts[i]);
8327
0
    }
8328
0
    return(name);
8329
0
}
8330
8331
/**
8332
 * Parse an end tag. Always consumes '</'.
8333
 *
8334
 *     [42] ETag ::= '</' Name S? '>'
8335
 *
8336
 * With namespace
8337
 *
8338
 *     [NS 9] ETag ::= '</' QName S? '>'
8339
 * @param ctxt  an XML parser context
8340
 * @param line  line of the start tag
8341
 */
8342
8343
static void
8344
0
xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
8345
0
    const xmlChar *name;
8346
8347
0
    GROW;
8348
0
    if ((RAW != '<') || (NXT(1) != '/')) {
8349
0
  xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
8350
0
           "xmlParseEndTag: '</' not found\n");
8351
0
  return;
8352
0
    }
8353
0
    SKIP(2);
8354
8355
0
    name = xmlParseNameAndCompare(ctxt,ctxt->name);
8356
8357
    /*
8358
     * We should definitely be at the ending "S? '>'" part
8359
     */
8360
0
    GROW;
8361
0
    SKIP_BLANKS;
8362
0
    if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
8363
0
  xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
8364
0
    } else
8365
0
  NEXT1;
8366
8367
    /*
8368
     * [ WFC: Element Type Match ]
8369
     * The Name in an element's end-tag must match the element type in the
8370
     * start-tag.
8371
     *
8372
     */
8373
0
    if (name != (xmlChar*)1) {
8374
0
        if (name == NULL) name = BAD_CAST "unparsable";
8375
0
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
8376
0
         "Opening and ending tag mismatch: %s line %d and %s\n",
8377
0
                    ctxt->name, line, name);
8378
0
    }
8379
8380
    /*
8381
     * SAX: End of Tag
8382
     */
8383
0
    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
8384
0
  (!ctxt->disableSAX))
8385
0
        ctxt->sax->endElement(ctxt->userData, ctxt->name);
8386
8387
0
    namePop(ctxt);
8388
0
    spacePop(ctxt);
8389
0
}
8390
8391
/**
8392
 * Parse an end of tag
8393
 *
8394
 * @deprecated Internal function, don't use.
8395
 *
8396
 *     [42] ETag ::= '</' Name S? '>'
8397
 *
8398
 * With namespace
8399
 *
8400
 *     [NS 9] ETag ::= '</' QName S? '>'
8401
 * @param ctxt  an XML parser context
8402
 */
8403
8404
void
8405
0
xmlParseEndTag(xmlParserCtxt *ctxt) {
8406
0
    xmlParseEndTag1(ctxt, 0);
8407
0
}
8408
#endif /* LIBXML_SAX1_ENABLED */
8409
8410
/************************************************************************
8411
 *                  *
8412
 *          SAX 2 specific operations       *
8413
 *                  *
8414
 ************************************************************************/
8415
8416
/**
8417
 * Parse an XML Namespace QName
8418
 *
8419
 *     [6]  QName  ::= (Prefix ':')? LocalPart
8420
 *     [7]  Prefix  ::= NCName
8421
 *     [8]  LocalPart  ::= NCName
8422
 *
8423
 * @param ctxt  an XML parser context
8424
 * @param prefix  pointer to store the prefix part
8425
 * @returns the Name parsed or NULL
8426
 */
8427
8428
static xmlHashedString
8429
7.54M
xmlParseQNameHashed(xmlParserCtxtPtr ctxt, xmlHashedString *prefix) {
8430
7.54M
    xmlHashedString l, p;
8431
7.54M
    int start, isNCName = 0;
8432
8433
7.54M
    l.name = NULL;
8434
7.54M
    p.name = NULL;
8435
8436
7.54M
    GROW;
8437
7.54M
    start = CUR_PTR - BASE_PTR;
8438
8439
7.54M
    l = xmlParseNCName(ctxt);
8440
7.54M
    if (l.name != NULL) {
8441
4.47M
        isNCName = 1;
8442
4.47M
        if (CUR == ':') {
8443
641k
            NEXT;
8444
641k
            p = l;
8445
641k
            l = xmlParseNCName(ctxt);
8446
641k
        }
8447
4.47M
    }
8448
7.54M
    if ((l.name == NULL) || (CUR == ':')) {
8449
3.11M
        xmlChar *tmp;
8450
8451
3.11M
        l.name = NULL;
8452
3.11M
        p.name = NULL;
8453
3.11M
        if ((isNCName == 0) && (CUR != ':'))
8454
2.68M
            return(l);
8455
423k
        tmp = xmlParseNmtoken(ctxt);
8456
423k
        if (tmp != NULL)
8457
403k
            xmlFree(tmp);
8458
423k
        l = xmlDictLookupHashed(ctxt->dict, BASE_PTR + start,
8459
423k
                                CUR_PTR - (BASE_PTR + start));
8460
423k
        if (l.name == NULL) {
8461
0
            xmlErrMemory(ctxt);
8462
0
            return(l);
8463
0
        }
8464
423k
        xmlNsErr(ctxt, XML_NS_ERR_QNAME,
8465
423k
                 "Failed to parse QName '%s'\n", l.name, NULL, NULL);
8466
423k
    }
8467
8468
4.85M
    *prefix = p;
8469
4.85M
    return(l);
8470
7.54M
}
8471
8472
/**
8473
 * Parse an XML Namespace QName
8474
 *
8475
 *     [6]  QName  ::= (Prefix ':')? LocalPart
8476
 *     [7]  Prefix  ::= NCName
8477
 *     [8]  LocalPart  ::= NCName
8478
 *
8479
 * @param ctxt  an XML parser context
8480
 * @param prefix  pointer to store the prefix part
8481
 * @returns the Name parsed or NULL
8482
 */
8483
8484
static const xmlChar *
8485
7.09k
xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
8486
7.09k
    xmlHashedString n, p;
8487
8488
7.09k
    n = xmlParseQNameHashed(ctxt, &p);
8489
7.09k
    if (n.name == NULL)
8490
1.29k
        return(NULL);
8491
5.80k
    *prefix = p.name;
8492
5.80k
    return(n.name);
8493
7.09k
}
8494
8495
/**
8496
 * Parse an XML name and compares for match
8497
 * (specialized for endtag parsing)
8498
 *
8499
 * @param ctxt  an XML parser context
8500
 * @param name  the localname
8501
 * @param prefix  the prefix, if any.
8502
 * @returns NULL for an illegal name, (xmlChar*) 1 for success
8503
 * and the name for mismatch
8504
 */
8505
8506
static const xmlChar *
8507
xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
8508
109k
                        xmlChar const *prefix) {
8509
109k
    const xmlChar *cmp;
8510
109k
    const xmlChar *in;
8511
109k
    const xmlChar *ret;
8512
109k
    const xmlChar *prefix2;
8513
8514
109k
    if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
8515
8516
109k
    GROW;
8517
109k
    in = ctxt->input->cur;
8518
8519
109k
    cmp = prefix;
8520
649k
    while (*in != 0 && *in == *cmp) {
8521
540k
  ++in;
8522
540k
  ++cmp;
8523
540k
    }
8524
109k
    if ((*cmp == 0) && (*in == ':')) {
8525
105k
        in++;
8526
105k
  cmp = name;
8527
997k
  while (*in != 0 && *in == *cmp) {
8528
892k
      ++in;
8529
892k
      ++cmp;
8530
892k
  }
8531
105k
  if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
8532
      /* success */
8533
102k
            ctxt->input->col += in - ctxt->input->cur;
8534
102k
      ctxt->input->cur = in;
8535
102k
      return((const xmlChar*) 1);
8536
102k
  }
8537
105k
    }
8538
    /*
8539
     * all strings coms from the dictionary, equality can be done directly
8540
     */
8541
7.09k
    ret = xmlParseQName (ctxt, &prefix2);
8542
7.09k
    if (ret == NULL)
8543
1.29k
        return(NULL);
8544
5.80k
    if ((ret == name) && (prefix == prefix2))
8545
1.73k
  return((const xmlChar*) 1);
8546
4.07k
    return ret;
8547
5.80k
}
8548
8549
/**
8550
 * Parse an attribute in the new SAX2 framework.
8551
 *
8552
 * @param ctxt  an XML parser context
8553
 * @param pref  the element prefix
8554
 * @param elem  the element name
8555
 * @param hprefix  resulting attribute prefix
8556
 * @param value  resulting value of the attribute
8557
 * @param len  resulting length of the attribute
8558
 * @param alloc  resulting indicator if the attribute was allocated
8559
 * @returns the attribute name, and the value in *value, .
8560
 */
8561
8562
static xmlHashedString
8563
xmlParseAttribute2(xmlParserCtxtPtr ctxt,
8564
                   const xmlChar * pref, const xmlChar * elem,
8565
                   xmlHashedString * hprefix, xmlChar ** value,
8566
                   int *len, int *alloc)
8567
2.97M
{
8568
2.97M
    xmlHashedString hname;
8569
2.97M
    const xmlChar *prefix, *name;
8570
2.97M
    xmlChar *val = NULL, *internal_val = NULL;
8571
2.97M
    int special = 0;
8572
2.97M
    int isNamespace;
8573
2.97M
    int flags;
8574
8575
2.97M
    *value = NULL;
8576
2.97M
    GROW;
8577
2.97M
    hname = xmlParseQNameHashed(ctxt, hprefix);
8578
2.97M
    if (hname.name == NULL) {
8579
143k
        xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8580
143k
                       "error parsing attribute name\n");
8581
143k
        return(hname);
8582
143k
    }
8583
2.83M
    name = hname.name;
8584
2.83M
    prefix = hprefix->name;
8585
8586
    /*
8587
     * get the type if needed
8588
     */
8589
2.83M
    if (ctxt->attsSpecial != NULL) {
8590
223k
        special = XML_PTR_TO_INT(xmlHashQLookup2(ctxt->attsSpecial, pref, elem,
8591
223k
                                              prefix, name));
8592
223k
    }
8593
8594
    /*
8595
     * read the value
8596
     */
8597
2.83M
    SKIP_BLANKS;
8598
2.83M
    if (RAW != '=') {
8599
32.3k
        xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
8600
32.3k
                          "Specification mandates value for attribute %s\n",
8601
32.3k
                          name);
8602
32.3k
        goto error;
8603
32.3k
    }
8604
8605
8606
2.80M
    NEXT;
8607
2.80M
    SKIP_BLANKS;
8608
2.80M
    flags = 0;
8609
2.80M
    isNamespace = (((prefix == NULL) && (name == ctxt->str_xmlns)) ||
8610
2.63M
                   (prefix == ctxt->str_xmlns));
8611
2.80M
    val = xmlParseAttValueInternal(ctxt, len, &flags, special,
8612
2.80M
                                   isNamespace);
8613
2.80M
    if (val == NULL)
8614
9.05k
        goto error;
8615
8616
2.79M
    *alloc = (flags & XML_ATTVAL_ALLOC) != 0;
8617
8618
2.79M
#ifdef LIBXML_VALID_ENABLED
8619
2.79M
    if ((ctxt->validate) &&
8620
0
        (ctxt->standalone == 1) &&
8621
0
        (special & XML_SPECIAL_EXTERNAL) &&
8622
0
        (flags & XML_ATTVAL_NORM_CHANGE)) {
8623
0
        xmlValidityError(ctxt, XML_DTD_NOT_STANDALONE,
8624
0
                         "standalone: normalization of attribute %s on %s "
8625
0
                         "by external subset declaration\n",
8626
0
                         name, elem);
8627
0
    }
8628
2.79M
#endif
8629
8630
2.79M
    if (prefix == ctxt->str_xml) {
8631
        /*
8632
         * Check that xml:lang conforms to the specification
8633
         * No more registered as an error, just generate a warning now
8634
         * since this was deprecated in XML second edition
8635
         */
8636
239k
        if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) {
8637
0
            internal_val = xmlStrndup(val, *len);
8638
0
            if (internal_val == NULL)
8639
0
                goto mem_error;
8640
0
            if (!xmlCheckLanguageID(internal_val)) {
8641
0
                xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
8642
0
                              "Malformed value for xml:lang : %s\n",
8643
0
                              internal_val, NULL);
8644
0
            }
8645
0
        }
8646
8647
        /*
8648
         * Check that xml:space conforms to the specification
8649
         */
8650
239k
        if (xmlStrEqual(name, BAD_CAST "space")) {
8651
2.79k
            internal_val = xmlStrndup(val, *len);
8652
2.79k
            if (internal_val == NULL)
8653
0
                goto mem_error;
8654
2.79k
            if (xmlStrEqual(internal_val, BAD_CAST "default"))
8655
884
                *(ctxt->space) = 0;
8656
1.91k
            else if (xmlStrEqual(internal_val, BAD_CAST "preserve"))
8657
1.01k
                *(ctxt->space) = 1;
8658
901
            else {
8659
901
                xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
8660
901
                              "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
8661
901
                              internal_val, NULL);
8662
901
            }
8663
2.79k
        }
8664
239k
        if (internal_val) {
8665
2.79k
            xmlFree(internal_val);
8666
2.79k
        }
8667
239k
    }
8668
8669
2.79M
    *value = val;
8670
2.79M
    return (hname);
8671
8672
0
mem_error:
8673
0
    xmlErrMemory(ctxt);
8674
41.4k
error:
8675
41.4k
    if ((val != NULL) && (*alloc != 0))
8676
0
        xmlFree(val);
8677
41.4k
    return(hname);
8678
0
}
8679
8680
/**
8681
 * Inserts a new attribute into the hash table.
8682
 *
8683
 * @param ctxt  parser context
8684
 * @param size  size of the hash table
8685
 * @param name  attribute name
8686
 * @param uri  namespace uri
8687
 * @param hashValue  combined hash value of name and uri
8688
 * @param aindex  attribute index (this is a multiple of 5)
8689
 * @returns INT_MAX if no existing attribute was found, the attribute
8690
 * index if an attribute was found, -1 if a memory allocation failed.
8691
 */
8692
static int
8693
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, unsigned size, const xmlChar *name,
8694
2.12M
                  const xmlChar *uri, unsigned hashValue, int aindex) {
8695
2.12M
    xmlAttrHashBucket *table = ctxt->attrHash;
8696
2.12M
    xmlAttrHashBucket *bucket;
8697
2.12M
    unsigned hindex;
8698
8699
2.12M
    hindex = hashValue & (size - 1);
8700
2.12M
    bucket = &table[hindex];
8701
8702
2.78M
    while (bucket->index >= 0) {
8703
962k
        const xmlChar **atts = &ctxt->atts[bucket->index];
8704
8705
962k
        if (name == atts[0]) {
8706
308k
            int nsIndex = XML_PTR_TO_INT(atts[2]);
8707
8708
308k
            if ((nsIndex == NS_INDEX_EMPTY) ? (uri == NULL) :
8709
308k
                (nsIndex == NS_INDEX_XML) ? (uri == ctxt->str_xml_ns) :
8710
31.2k
                (uri == ctxt->nsTab[nsIndex * 2 + 1]))
8711
302k
                return(bucket->index);
8712
308k
        }
8713
8714
660k
        hindex++;
8715
660k
        bucket++;
8716
660k
        if (hindex >= size) {
8717
437k
            hindex = 0;
8718
437k
            bucket = table;
8719
437k
        }
8720
660k
    }
8721
8722
1.82M
    bucket->index = aindex;
8723
8724
1.82M
    return(INT_MAX);
8725
2.12M
}
8726
8727
static int
8728
xmlAttrHashInsertQName(xmlParserCtxtPtr ctxt, unsigned size,
8729
                       const xmlChar *name, const xmlChar *prefix,
8730
14.7k
                       unsigned hashValue, int aindex) {
8731
14.7k
    xmlAttrHashBucket *table = ctxt->attrHash;
8732
14.7k
    xmlAttrHashBucket *bucket;
8733
14.7k
    unsigned hindex;
8734
8735
14.7k
    hindex = hashValue & (size - 1);
8736
14.7k
    bucket = &table[hindex];
8737
8738
21.4k
    while (bucket->index >= 0) {
8739
15.7k
        const xmlChar **atts = &ctxt->atts[bucket->index];
8740
8741
15.7k
        if ((name == atts[0]) && (prefix == atts[1]))
8742
8.98k
            return(bucket->index);
8743
8744
6.73k
        hindex++;
8745
6.73k
        bucket++;
8746
6.73k
        if (hindex >= size) {
8747
1.61k
            hindex = 0;
8748
1.61k
            bucket = table;
8749
1.61k
        }
8750
6.73k
    }
8751
8752
5.75k
    bucket->index = aindex;
8753
8754
5.75k
    return(INT_MAX);
8755
14.7k
}
8756
/**
8757
 * Parse a start tag. Always consumes '<'.
8758
 *
8759
 * This routine is called when running SAX2 parsing
8760
 *
8761
 *     [40] STag ::= '<' Name (S Attribute)* S? '>'
8762
 *
8763
 * [ WFC: Unique Att Spec ]
8764
 * No attribute name may appear more than once in the same start-tag or
8765
 * empty-element tag.
8766
 *
8767
 *     [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
8768
 *
8769
 * [ WFC: Unique Att Spec ]
8770
 * No attribute name may appear more than once in the same start-tag or
8771
 * empty-element tag.
8772
 *
8773
 * With namespace:
8774
 *
8775
 *     [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
8776
 *
8777
 *     [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
8778
 *
8779
 * @param ctxt  an XML parser context
8780
 * @param pref  resulting namespace prefix
8781
 * @param URI  resulting namespace URI
8782
 * @param nbNsPtr  resulting number of namespace declarations
8783
 * @returns the element name parsed
8784
 */
8785
8786
static const xmlChar *
8787
xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
8788
4.55M
                  const xmlChar **URI, int *nbNsPtr) {
8789
4.55M
    xmlHashedString hlocalname;
8790
4.55M
    xmlHashedString hprefix;
8791
4.55M
    xmlHashedString hattname;
8792
4.55M
    xmlHashedString haprefix;
8793
4.55M
    const xmlChar *localname;
8794
4.55M
    const xmlChar *prefix;
8795
4.55M
    const xmlChar *attname;
8796
4.55M
    const xmlChar *aprefix;
8797
4.55M
    const xmlChar *uri;
8798
4.55M
    xmlChar *attvalue = NULL;
8799
4.55M
    const xmlChar **atts = ctxt->atts;
8800
4.55M
    unsigned attrHashSize = 0;
8801
4.55M
    int maxatts = ctxt->maxatts;
8802
4.55M
    int nratts, nbatts, nbdef;
8803
4.55M
    int i, j, nbNs, nbTotalDef, attval, nsIndex, maxAtts;
8804
4.55M
    int alloc = 0;
8805
4.55M
    int numNsErr = 0;
8806
4.55M
    int numDupErr = 0;
8807
8808
4.55M
    if (RAW != '<') return(NULL);
8809
4.55M
    NEXT1;
8810
8811
4.55M
    nbatts = 0;
8812
4.55M
    nratts = 0;
8813
4.55M
    nbdef = 0;
8814
4.55M
    nbNs = 0;
8815
4.55M
    nbTotalDef = 0;
8816
4.55M
    attval = 0;
8817
8818
4.55M
    if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
8819
0
        xmlErrMemory(ctxt);
8820
0
        return(NULL);
8821
0
    }
8822
8823
4.55M
    hlocalname = xmlParseQNameHashed(ctxt, &hprefix);
8824
4.55M
    if (hlocalname.name == NULL) {
8825
2.54M
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8826
2.54M
           "StartTag: invalid element name\n");
8827
2.54M
        return(NULL);
8828
2.54M
    }
8829
2.01M
    localname = hlocalname.name;
8830
2.01M
    prefix = hprefix.name;
8831
8832
    /*
8833
     * Now parse the attributes, it ends up with the ending
8834
     *
8835
     * (S Attribute)* S?
8836
     */
8837
2.01M
    SKIP_BLANKS;
8838
2.01M
    GROW;
8839
8840
    /*
8841
     * The ctxt->atts array will be ultimately passed to the SAX callback
8842
     * containing five xmlChar pointers for each attribute:
8843
     *
8844
     * [0] attribute name
8845
     * [1] attribute prefix
8846
     * [2] namespace URI
8847
     * [3] attribute value
8848
     * [4] end of attribute value
8849
     *
8850
     * To save memory, we reuse this array temporarily and store integers
8851
     * in these pointer variables.
8852
     *
8853
     * [0] attribute name
8854
     * [1] attribute prefix
8855
     * [2] hash value of attribute prefix, and later namespace index
8856
     * [3] for non-allocated values: ptrdiff_t offset into input buffer
8857
     * [4] for non-allocated values: ptrdiff_t offset into input buffer
8858
     *
8859
     * The ctxt->attallocs array contains an additional unsigned int for
8860
     * each attribute, containing the hash value of the attribute name
8861
     * and the alloc flag in bit 31.
8862
     */
8863
8864
3.48M
    while (((RAW != '>') &&
8865
3.09M
     ((RAW != '/') || (NXT(1) != '>')) &&
8866
2.99M
     (IS_BYTE_CHAR(RAW))) && (PARSER_STOPPED(ctxt) == 0)) {
8867
2.97M
  int len = -1;
8868
8869
2.97M
  hattname = xmlParseAttribute2(ctxt, prefix, localname,
8870
2.97M
                                          &haprefix, &attvalue, &len,
8871
2.97M
                                          &alloc);
8872
2.97M
        if (hattname.name == NULL)
8873
143k
      break;
8874
2.83M
        if (attvalue == NULL)
8875
41.4k
            goto next_attr;
8876
2.79M
        attname = hattname.name;
8877
2.79M
        aprefix = haprefix.name;
8878
2.79M
  if (len < 0) len = xmlStrlen(attvalue);
8879
8880
2.79M
        if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
8881
165k
            xmlHashedString huri;
8882
165k
            xmlURIPtr parsedUri;
8883
8884
165k
            huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
8885
165k
            uri = huri.name;
8886
165k
            if (uri == NULL) {
8887
0
                xmlErrMemory(ctxt);
8888
0
                goto next_attr;
8889
0
            }
8890
165k
            if (*uri != 0) {
8891
150k
                if (xmlParseURISafe((const char *) uri, &parsedUri) < 0) {
8892
0
                    xmlErrMemory(ctxt);
8893
0
                    goto next_attr;
8894
0
                }
8895
150k
                if (parsedUri == NULL) {
8896
48.0k
                    xmlNsErr(ctxt, XML_WAR_NS_URI,
8897
48.0k
                             "xmlns: '%s' is not a valid URI\n",
8898
48.0k
                                       uri, NULL, NULL);
8899
101k
                } else {
8900
101k
                    if (parsedUri->scheme == NULL) {
8901
22.2k
                        xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
8902
22.2k
                                  "xmlns: URI %s is not absolute\n",
8903
22.2k
                                  uri, NULL, NULL);
8904
22.2k
                    }
8905
101k
                    xmlFreeURI(parsedUri);
8906
101k
                }
8907
150k
                if (uri == ctxt->str_xml_ns) {
8908
620
                    if (attname != ctxt->str_xml) {
8909
620
                        xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8910
620
                     "xml namespace URI cannot be the default namespace\n",
8911
620
                                 NULL, NULL, NULL);
8912
620
                    }
8913
620
                    goto next_attr;
8914
620
                }
8915
149k
                if ((len == 29) &&
8916
1.45k
                    (xmlStrEqual(uri,
8917
1.45k
                             BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
8918
390
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8919
390
                         "reuse of the xmlns namespace name is forbidden\n",
8920
390
                             NULL, NULL, NULL);
8921
390
                    goto next_attr;
8922
390
                }
8923
149k
            }
8924
8925
164k
            if (xmlParserNsPush(ctxt, NULL, &huri, NULL, 0) > 0)
8926
122k
                nbNs++;
8927
2.62M
        } else if (aprefix == ctxt->str_xmlns) {
8928
157k
            xmlHashedString huri;
8929
157k
            xmlURIPtr parsedUri;
8930
8931
157k
            huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
8932
157k
            uri = huri.name;
8933
157k
            if (uri == NULL) {
8934
0
                xmlErrMemory(ctxt);
8935
0
                goto next_attr;
8936
0
            }
8937
8938
157k
            if (attname == ctxt->str_xml) {
8939
598
                if (uri != ctxt->str_xml_ns) {
8940
404
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8941
404
                             "xml namespace prefix mapped to wrong URI\n",
8942
404
                             NULL, NULL, NULL);
8943
404
                }
8944
                /*
8945
                 * Do not keep a namespace definition node
8946
                 */
8947
598
                goto next_attr;
8948
598
            }
8949
157k
            if (uri == ctxt->str_xml_ns) {
8950
248
                if (attname != ctxt->str_xml) {
8951
248
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8952
248
                             "xml namespace URI mapped to wrong prefix\n",
8953
248
                             NULL, NULL, NULL);
8954
248
                }
8955
248
                goto next_attr;
8956
248
            }
8957
156k
            if (attname == ctxt->str_xmlns) {
8958
815
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8959
815
                         "redefinition of the xmlns prefix is forbidden\n",
8960
815
                         NULL, NULL, NULL);
8961
815
                goto next_attr;
8962
815
            }
8963
156k
            if ((len == 29) &&
8964
1.08k
                (xmlStrEqual(uri,
8965
1.08k
                             BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
8966
388
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8967
388
                         "reuse of the xmlns namespace name is forbidden\n",
8968
388
                         NULL, NULL, NULL);
8969
388
                goto next_attr;
8970
388
            }
8971
155k
            if ((uri == NULL) || (uri[0] == 0)) {
8972
727
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8973
727
                         "xmlns:%s: Empty XML namespace is not allowed\n",
8974
727
                              attname, NULL, NULL);
8975
727
                goto next_attr;
8976
155k
            } else {
8977
155k
                if (xmlParseURISafe((const char *) uri, &parsedUri) < 0) {
8978
0
                    xmlErrMemory(ctxt);
8979
0
                    goto next_attr;
8980
0
                }
8981
155k
                if (parsedUri == NULL) {
8982
6.66k
                    xmlNsErr(ctxt, XML_WAR_NS_URI,
8983
6.66k
                         "xmlns:%s: '%s' is not a valid URI\n",
8984
6.66k
                                       attname, uri, NULL);
8985
148k
                } else {
8986
148k
                    if ((ctxt->pedantic) && (parsedUri->scheme == NULL)) {
8987
0
                        xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
8988
0
                                  "xmlns:%s: URI %s is not absolute\n",
8989
0
                                  attname, uri, NULL);
8990
0
                    }
8991
148k
                    xmlFreeURI(parsedUri);
8992
148k
                }
8993
155k
            }
8994
8995
155k
            if (xmlParserNsPush(ctxt, &hattname, &huri, NULL, 0) > 0)
8996
153k
                nbNs++;
8997
2.47M
        } else {
8998
            /*
8999
             * Populate attributes array, see above for repurposing
9000
             * of xmlChar pointers.
9001
             */
9002
2.47M
            if ((atts == NULL) || (nbatts + 5 > maxatts)) {
9003
52.2k
                int res = xmlCtxtGrowAttrs(ctxt);
9004
9005
52.2k
                maxatts = ctxt->maxatts;
9006
52.2k
                atts = ctxt->atts;
9007
9008
52.2k
                if (res < 0)
9009
0
                    goto next_attr;
9010
52.2k
            }
9011
2.47M
            ctxt->attallocs[nratts++] = (hattname.hashValue & 0x7FFFFFFF) |
9012
2.47M
                                        ((unsigned) alloc << 31);
9013
2.47M
            atts[nbatts++] = attname;
9014
2.47M
            atts[nbatts++] = aprefix;
9015
2.47M
            atts[nbatts++] = XML_INT_TO_PTR(haprefix.hashValue);
9016
2.47M
            if (alloc) {
9017
53.3k
                atts[nbatts++] = attvalue;
9018
53.3k
                attvalue += len;
9019
53.3k
                atts[nbatts++] = attvalue;
9020
2.41M
            } else {
9021
                /*
9022
                 * attvalue points into the input buffer which can be
9023
                 * reallocated. Store differences to input->base instead.
9024
                 * The pointers will be reconstructed later.
9025
                 */
9026
2.41M
                atts[nbatts++] = XML_INT_TO_PTR(attvalue - BASE_PTR);
9027
2.41M
                attvalue += len;
9028
2.41M
                atts[nbatts++] = XML_INT_TO_PTR(attvalue - BASE_PTR);
9029
2.41M
            }
9030
            /*
9031
             * tag if some deallocation is needed
9032
             */
9033
2.47M
            if (alloc != 0) attval = 1;
9034
2.47M
            attvalue = NULL; /* moved into atts */
9035
2.47M
        }
9036
9037
2.83M
next_attr:
9038
2.83M
        if ((attvalue != NULL) && (alloc != 0)) {
9039
51.2k
            xmlFree(attvalue);
9040
51.2k
            attvalue = NULL;
9041
51.2k
        }
9042
9043
2.83M
  GROW
9044
2.83M
  if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
9045
1.29M
      break;
9046
1.54M
  if (SKIP_BLANKS == 0) {
9047
68.0k
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
9048
68.0k
         "attributes construct error\n");
9049
68.0k
      break;
9050
68.0k
  }
9051
1.47M
        GROW;
9052
1.47M
    }
9053
9054
    /*
9055
     * Namespaces from default attributes
9056
     */
9057
2.01M
    if (ctxt->attsDefault != NULL) {
9058
207k
        xmlDefAttrsPtr defaults;
9059
9060
207k
  defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
9061
207k
  if (defaults != NULL) {
9062
2.20M
      for (i = 0; i < defaults->nbAttrs; i++) {
9063
2.05M
                xmlDefAttr *attr = &defaults->attrs[i];
9064
9065
2.05M
          attname = attr->name.name;
9066
2.05M
    aprefix = attr->prefix.name;
9067
9068
2.05M
    if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
9069
19.3k
                    xmlParserEntityCheck(ctxt, attr->expandedSize);
9070
9071
19.3k
                    if (xmlParserNsPush(ctxt, NULL, &attr->value, NULL, 1) > 0)
9072
18.9k
                        nbNs++;
9073
2.03M
    } else if (aprefix == ctxt->str_xmlns) {
9074
1.71M
                    xmlParserEntityCheck(ctxt, attr->expandedSize);
9075
9076
1.71M
                    if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
9077
1.71M
                                      NULL, 1) > 0)
9078
1.71M
                        nbNs++;
9079
1.71M
    } else {
9080
322k
                    if (nratts + nbTotalDef >= XML_MAX_ATTRS) {
9081
0
                        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
9082
0
                                    "Maximum number of attributes exceeded");
9083
0
                        break;
9084
0
                    }
9085
322k
                    nbTotalDef += 1;
9086
322k
                }
9087
2.05M
      }
9088
142k
  }
9089
207k
    }
9090
9091
    /*
9092
     * Resolve attribute namespaces
9093
     */
9094
4.48M
    for (i = 0; i < nbatts; i += 5) {
9095
2.47M
        attname = atts[i];
9096
2.47M
        aprefix = atts[i+1];
9097
9098
        /*
9099
  * The default namespace does not apply to attribute names.
9100
  */
9101
2.47M
  if (aprefix == NULL) {
9102
2.17M
            nsIndex = NS_INDEX_EMPTY;
9103
2.17M
        } else if (aprefix == ctxt->str_xml) {
9104
239k
            nsIndex = NS_INDEX_XML;
9105
239k
        } else {
9106
55.2k
            haprefix.name = aprefix;
9107
55.2k
            haprefix.hashValue = (size_t) atts[i+2];
9108
55.2k
            nsIndex = xmlParserNsLookup(ctxt, &haprefix, NULL);
9109
9110
55.2k
      if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex)) {
9111
41.9k
                xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9112
41.9k
        "Namespace prefix %s for %s on %s is not defined\n",
9113
41.9k
        aprefix, attname, localname);
9114
41.9k
                nsIndex = NS_INDEX_EMPTY;
9115
41.9k
            }
9116
55.2k
        }
9117
9118
2.47M
        atts[i+2] = XML_INT_TO_PTR(nsIndex);
9119
2.47M
    }
9120
9121
    /*
9122
     * Maximum number of attributes including default attributes.
9123
     */
9124
2.01M
    maxAtts = nratts + nbTotalDef;
9125
9126
    /*
9127
     * Verify that attribute names are unique.
9128
     */
9129
2.01M
    if (maxAtts > 1) {
9130
763k
        attrHashSize = 4;
9131
1.04M
        while (attrHashSize / 2 < (unsigned) maxAtts)
9132
285k
            attrHashSize *= 2;
9133
9134
763k
        if (attrHashSize > ctxt->attrHashMax) {
9135
21.9k
            xmlAttrHashBucket *tmp;
9136
9137
21.9k
            tmp = xmlRealloc(ctxt->attrHash, attrHashSize * sizeof(tmp[0]));
9138
21.9k
            if (tmp == NULL) {
9139
0
                xmlErrMemory(ctxt);
9140
0
                goto done;
9141
0
            }
9142
9143
21.9k
            ctxt->attrHash = tmp;
9144
21.9k
            ctxt->attrHashMax = attrHashSize;
9145
21.9k
        }
9146
9147
763k
        memset(ctxt->attrHash, -1, attrHashSize * sizeof(ctxt->attrHash[0]));
9148
9149
2.65M
        for (i = 0, j = 0; j < nratts; i += 5, j++) {
9150
1.88M
            const xmlChar *nsuri;
9151
1.88M
            unsigned hashValue, nameHashValue, uriHashValue;
9152
1.88M
            int res;
9153
9154
1.88M
            attname = atts[i];
9155
1.88M
            aprefix = atts[i+1];
9156
1.88M
            nsIndex = XML_PTR_TO_INT(atts[i+2]);
9157
            /* Hash values always have bit 31 set, see dict.c */
9158
1.88M
            nameHashValue = ctxt->attallocs[j] | 0x80000000;
9159
9160
1.88M
            if (nsIndex == NS_INDEX_EMPTY) {
9161
                /*
9162
                 * Prefix with empty namespace means an undeclared
9163
                 * prefix which was already reported above.
9164
                 */
9165
1.68M
                if (aprefix != NULL)
9166
36.0k
                    continue;
9167
1.64M
                nsuri = NULL;
9168
1.64M
                uriHashValue = URI_HASH_EMPTY;
9169
1.64M
            } else if (nsIndex == NS_INDEX_XML) {
9170
196k
                nsuri = ctxt->str_xml_ns;
9171
196k
                uriHashValue = URI_HASH_XML;
9172
196k
            } else {
9173
12.7k
                nsuri = ctxt->nsTab[nsIndex * 2 + 1];
9174
12.7k
                uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
9175
12.7k
            }
9176
9177
1.85M
            hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
9178
1.85M
            res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
9179
1.85M
                                    hashValue, i);
9180
1.85M
            if (res < 0)
9181
0
                continue;
9182
9183
            /*
9184
             * [ WFC: Unique Att Spec ]
9185
             * No attribute name may appear more than once in the same
9186
             * start-tag or empty-element tag.
9187
             * As extended by the Namespace in XML REC.
9188
             */
9189
1.85M
            if (res < INT_MAX) {
9190
270k
                if (aprefix == atts[res+1]) {
9191
263k
                    xmlErrAttributeDup(ctxt, aprefix, attname);
9192
263k
                    numDupErr += 1;
9193
263k
                } else {
9194
7.47k
                    xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
9195
7.47k
                             "Namespaced Attribute %s in '%s' redefined\n",
9196
7.47k
                             attname, nsuri, NULL);
9197
7.47k
                    numNsErr += 1;
9198
7.47k
                }
9199
270k
            }
9200
1.85M
        }
9201
763k
    }
9202
9203
    /*
9204
     * Default attributes
9205
     */
9206
2.01M
    if (ctxt->attsDefault != NULL) {
9207
207k
        xmlDefAttrsPtr defaults;
9208
9209
207k
  defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
9210
207k
  if (defaults != NULL) {
9211
2.20M
      for (i = 0; i < defaults->nbAttrs; i++) {
9212
2.05M
                xmlDefAttr *attr = &defaults->attrs[i];
9213
2.05M
                const xmlChar *nsuri = NULL;
9214
2.05M
                unsigned hashValue, uriHashValue = 0;
9215
2.05M
                int res;
9216
9217
2.05M
          attname = attr->name.name;
9218
2.05M
    aprefix = attr->prefix.name;
9219
9220
2.05M
    if ((attname == ctxt->str_xmlns) && (aprefix == NULL))
9221
19.3k
                    continue;
9222
2.03M
    if (aprefix == ctxt->str_xmlns)
9223
1.71M
                    continue;
9224
9225
322k
                if (aprefix == NULL) {
9226
96.9k
                    nsIndex = NS_INDEX_EMPTY;
9227
96.9k
                    nsuri = NULL;
9228
96.9k
                    uriHashValue = URI_HASH_EMPTY;
9229
225k
                } else if (aprefix == ctxt->str_xml) {
9230
13.0k
                    nsIndex = NS_INDEX_XML;
9231
13.0k
                    nsuri = ctxt->str_xml_ns;
9232
13.0k
                    uriHashValue = URI_HASH_XML;
9233
212k
                } else {
9234
212k
                    nsIndex = xmlParserNsLookup(ctxt, &attr->prefix, NULL);
9235
212k
                    if ((nsIndex == INT_MAX) ||
9236
159k
                        (nsIndex < ctxt->nsdb->minNsIndex)) {
9237
159k
                        xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9238
159k
                                 "Namespace prefix %s for %s on %s is not "
9239
159k
                                 "defined\n",
9240
159k
                                 aprefix, attname, localname);
9241
159k
                        nsIndex = NS_INDEX_EMPTY;
9242
159k
                        nsuri = NULL;
9243
159k
                        uriHashValue = URI_HASH_EMPTY;
9244
159k
                    } else {
9245
52.8k
                        nsuri = ctxt->nsTab[nsIndex * 2 + 1];
9246
52.8k
                        uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
9247
52.8k
                    }
9248
212k
                }
9249
9250
                /*
9251
                 * Check whether the attribute exists
9252
                 */
9253
322k
                if (maxAtts > 1) {
9254
271k
                    hashValue = xmlDictCombineHash(attr->name.hashValue,
9255
271k
                                                   uriHashValue);
9256
271k
                    res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
9257
271k
                                            hashValue, nbatts);
9258
271k
                    if (res < 0)
9259
0
                        continue;
9260
271k
                    if (res < INT_MAX) {
9261
31.3k
                        if (aprefix == atts[res+1])
9262
2.76k
                            continue;
9263
28.5k
                        xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
9264
28.5k
                                 "Namespaced Attribute %s in '%s' redefined\n",
9265
28.5k
                                 attname, nsuri, NULL);
9266
28.5k
                    }
9267
271k
                }
9268
9269
319k
                xmlParserEntityCheck(ctxt, attr->expandedSize);
9270
9271
319k
                if ((atts == NULL) || (nbatts + 5 > maxatts)) {
9272
2.48k
                    res = xmlCtxtGrowAttrs(ctxt);
9273
9274
2.48k
                    maxatts = ctxt->maxatts;
9275
2.48k
                    atts = ctxt->atts;
9276
9277
2.48k
                    if (res < 0) {
9278
0
                        localname = NULL;
9279
0
                        goto done;
9280
0
                    }
9281
2.48k
                }
9282
9283
319k
                atts[nbatts++] = attname;
9284
319k
                atts[nbatts++] = aprefix;
9285
319k
                atts[nbatts++] = XML_INT_TO_PTR(nsIndex);
9286
319k
                atts[nbatts++] = attr->value.name;
9287
319k
                atts[nbatts++] = attr->valueEnd;
9288
9289
319k
#ifdef LIBXML_VALID_ENABLED
9290
                /*
9291
                 * This should be moved to valid.c, but we don't keep track
9292
                 * whether an attribute was defaulted.
9293
                 */
9294
319k
                if ((ctxt->validate) &&
9295
0
                    (ctxt->standalone == 1) &&
9296
0
                    (attr->external != 0)) {
9297
0
                    xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED,
9298
0
                            "standalone: attribute %s on %s defaulted "
9299
0
                            "from external subset\n",
9300
0
                            attname, localname);
9301
0
                }
9302
319k
#endif
9303
319k
                nbdef++;
9304
319k
      }
9305
142k
  }
9306
207k
    }
9307
9308
    /*
9309
     * Using a single hash table for nsUri/localName pairs cannot
9310
     * detect duplicate QNames reliably. The following example will
9311
     * only result in two namespace errors.
9312
     *
9313
     * <doc xmlns:a="a" xmlns:b="a">
9314
     *   <elem a:a="" b:a="" b:a=""/>
9315
     * </doc>
9316
     *
9317
     * If we saw more than one namespace error but no duplicate QNames
9318
     * were found, we have to scan for duplicate QNames.
9319
     */
9320
2.01M
    if ((numDupErr == 0) && (numNsErr > 1)) {
9321
1.35k
        memset(ctxt->attrHash, -1,
9322
1.35k
               attrHashSize * sizeof(ctxt->attrHash[0]));
9323
9324
16.7k
        for (i = 0, j = 0; j < nratts; i += 5, j++) {
9325
15.3k
            unsigned hashValue, nameHashValue, prefixHashValue;
9326
15.3k
            int res;
9327
9328
15.3k
            aprefix = atts[i+1];
9329
15.3k
            if (aprefix == NULL)
9330
651
                continue;
9331
9332
14.7k
            attname = atts[i];
9333
            /* Hash values always have bit 31 set, see dict.c */
9334
14.7k
            nameHashValue = ctxt->attallocs[j] | 0x80000000;
9335
14.7k
            prefixHashValue = xmlDictComputeHash(ctxt->dict, aprefix);
9336
9337
14.7k
            hashValue = xmlDictCombineHash(nameHashValue, prefixHashValue);
9338
14.7k
            res = xmlAttrHashInsertQName(ctxt, attrHashSize, attname,
9339
14.7k
                                         aprefix, hashValue, i);
9340
14.7k
            if (res < INT_MAX)
9341
8.98k
                xmlErrAttributeDup(ctxt, aprefix, attname);
9342
14.7k
        }
9343
1.35k
    }
9344
9345
    /*
9346
     * Reconstruct attribute pointers
9347
     */
9348
4.80M
    for (i = 0, j = 0; i < nbatts; i += 5, j++) {
9349
        /* namespace URI */
9350
2.79M
        nsIndex = XML_PTR_TO_INT(atts[i+2]);
9351
2.79M
        if (nsIndex == INT_MAX)
9352
2.47M
            atts[i+2] = NULL;
9353
318k
        else if (nsIndex == INT_MAX - 1)
9354
252k
            atts[i+2] = ctxt->str_xml_ns;
9355
65.9k
        else
9356
65.9k
            atts[i+2] = ctxt->nsTab[nsIndex * 2 + 1];
9357
9358
2.79M
        if ((j < nratts) && (ctxt->attallocs[j] & 0x80000000) == 0) {
9359
2.41M
            atts[i+3] = BASE_PTR + XML_PTR_TO_INT(atts[i+3]);  /* value */
9360
2.41M
            atts[i+4] = BASE_PTR + XML_PTR_TO_INT(atts[i+4]);  /* valuend */
9361
2.41M
        }
9362
2.79M
    }
9363
9364
2.01M
    uri = xmlParserNsLookupUri(ctxt, &hprefix);
9365
2.01M
    if ((prefix != NULL) && (uri == NULL)) {
9366
120k
  xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9367
120k
           "Namespace prefix %s on %s is not defined\n",
9368
120k
     prefix, localname, NULL);
9369
120k
    }
9370
2.01M
    *pref = prefix;
9371
2.01M
    *URI = uri;
9372
9373
    /*
9374
     * SAX callback
9375
     */
9376
2.01M
    if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
9377
2.01M
  (!ctxt->disableSAX)) {
9378
1.56M
  if (nbNs > 0)
9379
180k
      ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
9380
180k
                          nbNs, ctxt->nsTab + 2 * (ctxt->nsNr - nbNs),
9381
180k
        nbatts / 5, nbdef, atts);
9382
1.38M
  else
9383
1.38M
      ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
9384
1.38M
                          0, NULL, nbatts / 5, nbdef, atts);
9385
1.56M
    }
9386
9387
2.01M
done:
9388
    /*
9389
     * Free allocated attribute values
9390
     */
9391
2.01M
    if (attval != 0) {
9392
317k
  for (i = 0, j = 0; j < nratts; i += 5, j++)
9393
285k
      if (ctxt->attallocs[j] & 0x80000000)
9394
53.3k
          xmlFree((xmlChar *) atts[i+3]);
9395
31.9k
    }
9396
9397
2.01M
    *nbNsPtr = nbNs;
9398
2.01M
    return(localname);
9399
2.01M
}
9400
9401
/**
9402
 * Parse an end tag. Always consumes '</'.
9403
 *
9404
 *     [42] ETag ::= '</' Name S? '>'
9405
 *
9406
 * With namespace
9407
 *
9408
 *     [NS 9] ETag ::= '</' QName S? '>'
9409
 * @param ctxt  an XML parser context
9410
 * @param tag  the corresponding start tag
9411
 */
9412
9413
static void
9414
1.40M
xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlStartTag *tag) {
9415
1.40M
    const xmlChar *name;
9416
9417
1.40M
    GROW;
9418
1.40M
    if ((RAW != '<') || (NXT(1) != '/')) {
9419
251
  xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
9420
251
  return;
9421
251
    }
9422
1.40M
    SKIP(2);
9423
9424
1.40M
    if (tag->prefix == NULL)
9425
1.29M
        name = xmlParseNameAndCompare(ctxt, ctxt->name);
9426
109k
    else
9427
109k
        name = xmlParseQNameAndCompare(ctxt, ctxt->name, tag->prefix);
9428
9429
    /*
9430
     * We should definitely be at the ending "S? '>'" part
9431
     */
9432
1.40M
    GROW;
9433
1.40M
    SKIP_BLANKS;
9434
1.40M
    if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
9435
20.0k
  xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
9436
20.0k
    } else
9437
1.38M
  NEXT1;
9438
9439
    /*
9440
     * [ WFC: Element Type Match ]
9441
     * The Name in an element's end-tag must match the element type in the
9442
     * start-tag.
9443
     *
9444
     */
9445
1.40M
    if (name != (xmlChar*)1) {
9446
16.4k
        if (name == NULL) name = BAD_CAST "unparsable";
9447
16.4k
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
9448
16.4k
         "Opening and ending tag mismatch: %s line %d and %s\n",
9449
16.4k
                    ctxt->name, tag->line, name);
9450
16.4k
    }
9451
9452
    /*
9453
     * SAX: End of Tag
9454
     */
9455
1.40M
    if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
9456
1.40M
  (!ctxt->disableSAX))
9457
1.37M
  ctxt->sax->endElementNs(ctxt->userData, ctxt->name, tag->prefix,
9458
1.37M
                                tag->URI);
9459
9460
1.40M
    spacePop(ctxt);
9461
1.40M
    if (tag->nsNr != 0)
9462
144k
  xmlParserNsPop(ctxt, tag->nsNr);
9463
1.40M
}
9464
9465
/**
9466
 * Parse escaped pure raw content. Always consumes '<!['.
9467
 *
9468
 * @deprecated Internal function, don't use.
9469
 *
9470
 *     [18] CDSect ::= CDStart CData CDEnd
9471
 *
9472
 *     [19] CDStart ::= '<![CDATA['
9473
 *
9474
 *     [20] Data ::= (Char* - (Char* ']]>' Char*))
9475
 *
9476
 *     [21] CDEnd ::= ']]>'
9477
 * @param ctxt  an XML parser context
9478
 */
9479
void
9480
16.3k
xmlParseCDSect(xmlParserCtxt *ctxt) {
9481
16.3k
    xmlChar *buf = NULL;
9482
16.3k
    int len = 0;
9483
16.3k
    int size = XML_PARSER_BUFFER_SIZE;
9484
16.3k
    int r, rl;
9485
16.3k
    int s, sl;
9486
16.3k
    int cur, l;
9487
16.3k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9488
6.17k
                    XML_MAX_HUGE_LENGTH :
9489
16.3k
                    XML_MAX_TEXT_LENGTH;
9490
9491
16.3k
    if ((CUR != '<') || (NXT(1) != '!') || (NXT(2) != '['))
9492
0
        return;
9493
16.3k
    SKIP(3);
9494
9495
16.3k
    if (!CMP6(CUR_PTR, 'C', 'D', 'A', 'T', 'A', '['))
9496
0
        return;
9497
16.3k
    SKIP(6);
9498
9499
16.3k
    r = xmlCurrentCharRecover(ctxt, &rl);
9500
16.3k
    if (!IS_CHAR(r)) {
9501
3.83k
  xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
9502
3.83k
        goto out;
9503
3.83k
    }
9504
12.5k
    NEXTL(rl);
9505
12.5k
    s = xmlCurrentCharRecover(ctxt, &sl);
9506
12.5k
    if (!IS_CHAR(s)) {
9507
1.70k
  xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
9508
1.70k
        goto out;
9509
1.70k
    }
9510
10.8k
    NEXTL(sl);
9511
10.8k
    cur = xmlCurrentCharRecover(ctxt, &l);
9512
10.8k
    buf = xmlMalloc(size);
9513
10.8k
    if (buf == NULL) {
9514
0
  xmlErrMemory(ctxt);
9515
0
        goto out;
9516
0
    }
9517
6.24M
    while (IS_CHAR(cur) &&
9518
6.23M
           ((r != ']') || (s != ']') || (cur != '>'))) {
9519
6.23M
  if (len + 5 >= size) {
9520
6.27k
      xmlChar *tmp;
9521
6.27k
            int newSize;
9522
9523
6.27k
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
9524
6.27k
            if (newSize < 0) {
9525
0
                xmlFatalErrMsg(ctxt, XML_ERR_CDATA_NOT_FINISHED,
9526
0
                               "CData section too big found\n");
9527
0
                goto out;
9528
0
            }
9529
6.27k
      tmp = xmlRealloc(buf, newSize);
9530
6.27k
      if (tmp == NULL) {
9531
0
    xmlErrMemory(ctxt);
9532
0
                goto out;
9533
0
      }
9534
6.27k
      buf = tmp;
9535
6.27k
      size = newSize;
9536
6.27k
  }
9537
6.23M
  COPY_BUF(buf, len, r);
9538
6.23M
  r = s;
9539
6.23M
  rl = sl;
9540
6.23M
  s = cur;
9541
6.23M
  sl = l;
9542
6.23M
  NEXTL(l);
9543
6.23M
  cur = xmlCurrentCharRecover(ctxt, &l);
9544
6.23M
    }
9545
10.8k
    buf[len] = 0;
9546
10.8k
    if (cur != '>') {
9547
5.81k
  xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
9548
5.81k
                       "CData section not finished\n%.50s\n", buf);
9549
5.81k
        goto out;
9550
5.81k
    }
9551
5.01k
    NEXTL(l);
9552
9553
    /*
9554
     * OK the buffer is to be consumed as cdata.
9555
     */
9556
5.01k
    if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9557
4.16k
        if ((ctxt->sax->cdataBlock != NULL) &&
9558
4.16k
            ((ctxt->options & XML_PARSE_NOCDATA) == 0)) {
9559
4.16k
            ctxt->sax->cdataBlock(ctxt->userData, buf, len);
9560
4.16k
        } else if (ctxt->sax->characters != NULL) {
9561
0
            ctxt->sax->characters(ctxt->userData, buf, len);
9562
0
        }
9563
4.16k
    }
9564
9565
16.3k
out:
9566
16.3k
    xmlFree(buf);
9567
16.3k
}
9568
9569
/**
9570
 * Parse a content sequence. Stops at EOF or '</'. Leaves checking of
9571
 * unexpected EOF to the caller.
9572
 *
9573
 * @param ctxt  an XML parser context
9574
 */
9575
9576
static void
9577
46.3k
xmlParseContentInternal(xmlParserCtxtPtr ctxt) {
9578
46.3k
    int oldNameNr = ctxt->nameNr;
9579
46.3k
    int oldSpaceNr = ctxt->spaceNr;
9580
46.3k
    int oldNodeNr = ctxt->nodeNr;
9581
9582
46.3k
    GROW;
9583
33.1M
    while ((ctxt->input->cur < ctxt->input->end) &&
9584
33.1M
     (PARSER_STOPPED(ctxt) == 0)) {
9585
33.1M
  const xmlChar *cur = ctxt->input->cur;
9586
9587
  /*
9588
   * First case : a Processing Instruction.
9589
   */
9590
33.1M
  if ((*cur == '<') && (cur[1] == '?')) {
9591
171k
      xmlParsePI(ctxt);
9592
171k
  }
9593
9594
  /*
9595
   * Second case : a CDSection
9596
   */
9597
  /* 2.6.0 test was *cur not RAW */
9598
32.9M
  else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
9599
16.3k
      xmlParseCDSect(ctxt);
9600
16.3k
  }
9601
9602
  /*
9603
   * Third case :  a comment
9604
   */
9605
32.9M
  else if ((*cur == '<') && (NXT(1) == '!') &&
9606
48.6k
     (NXT(2) == '-') && (NXT(3) == '-')) {
9607
19.4k
      xmlParseComment(ctxt);
9608
19.4k
  }
9609
9610
  /*
9611
   * Fourth case :  a sub-element.
9612
   */
9613
32.8M
  else if (*cur == '<') {
9614
5.92M
            if (NXT(1) == '/') {
9615
1.40M
                if (ctxt->nameNr <= oldNameNr)
9616
21.5k
                    break;
9617
1.38M
          xmlParseElementEnd(ctxt);
9618
4.51M
            } else {
9619
4.51M
          xmlParseElementStart(ctxt);
9620
4.51M
            }
9621
5.92M
  }
9622
9623
  /*
9624
   * Fifth case : a reference. If if has not been resolved,
9625
   *    parsing returns it's Name, create the node
9626
   */
9627
9628
26.9M
  else if (*cur == '&') {
9629
22.0M
      xmlParseReference(ctxt);
9630
22.0M
  }
9631
9632
  /*
9633
   * Last case, text. Note that References are handled directly.
9634
   */
9635
4.94M
  else {
9636
4.94M
      xmlParseCharDataInternal(ctxt, 0);
9637
4.94M
  }
9638
9639
33.0M
  SHRINK;
9640
33.0M
  GROW;
9641
33.0M
    }
9642
9643
46.3k
    if ((ctxt->nameNr > oldNameNr) &&
9644
4.99k
        (ctxt->input->cur >= ctxt->input->end) &&
9645
4.78k
        (ctxt->wellFormed)) {
9646
576
        const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
9647
576
        int line = ctxt->pushTab[ctxt->nameNr - 1].line;
9648
576
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
9649
576
                "Premature end of data in tag %s line %d\n",
9650
576
                name, line, NULL);
9651
576
    }
9652
9653
    /*
9654
     * Clean up in error case
9655
     */
9656
9657
129k
    while (ctxt->nodeNr > oldNodeNr)
9658
83.3k
        nodePop(ctxt);
9659
9660
298k
    while (ctxt->nameNr > oldNameNr) {
9661
252k
        xmlStartTag *tag = &ctxt->pushTab[ctxt->nameNr - 1];
9662
9663
252k
        if (tag->nsNr != 0)
9664
111k
            xmlParserNsPop(ctxt, tag->nsNr);
9665
9666
252k
        namePop(ctxt);
9667
252k
    }
9668
9669
298k
    while (ctxt->spaceNr > oldSpaceNr)
9670
252k
        spacePop(ctxt);
9671
46.3k
}
9672
9673
/**
9674
 * Parse XML element content. This is useful if you're only interested
9675
 * in custom SAX callbacks. If you want a node list, use
9676
 * #xmlCtxtParseContent.
9677
 *
9678
 * @param ctxt  an XML parser context
9679
 */
9680
void
9681
0
xmlParseContent(xmlParserCtxt *ctxt) {
9682
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
9683
0
        return;
9684
9685
0
    xmlCtxtInitializeLate(ctxt);
9686
9687
0
    xmlParseContentInternal(ctxt);
9688
9689
0
    xmlParserCheckEOF(ctxt, XML_ERR_NOT_WELL_BALANCED);
9690
0
}
9691
9692
/**
9693
 * Parse an XML element
9694
 *
9695
 * @deprecated Internal function, don't use.
9696
 *
9697
 *     [39] element ::= EmptyElemTag | STag content ETag
9698
 *
9699
 * [ WFC: Element Type Match ]
9700
 * The Name in an element's end-tag must match the element type in the
9701
 * start-tag.
9702
 *
9703
 * @param ctxt  an XML parser context
9704
 */
9705
9706
void
9707
45.0k
xmlParseElement(xmlParserCtxt *ctxt) {
9708
45.0k
    if (xmlParseElementStart(ctxt) != 0)
9709
11.1k
        return;
9710
9711
33.8k
    xmlParseContentInternal(ctxt);
9712
9713
33.8k
    if (ctxt->input->cur >= ctxt->input->end) {
9714
12.0k
        if (ctxt->wellFormed) {
9715
1.40k
            const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
9716
1.40k
            int line = ctxt->pushTab[ctxt->nameNr - 1].line;
9717
1.40k
            xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
9718
1.40k
                    "Premature end of data in tag %s line %d\n",
9719
1.40k
                    name, line, NULL);
9720
1.40k
        }
9721
12.0k
        return;
9722
12.0k
    }
9723
9724
21.8k
    xmlParseElementEnd(ctxt);
9725
21.8k
}
9726
9727
/**
9728
 * Parse the start of an XML element. Returns -1 in case of error, 0 if an
9729
 * opening tag was parsed, 1 if an empty element was parsed.
9730
 *
9731
 * Always consumes '<'.
9732
 *
9733
 * @param ctxt  an XML parser context
9734
 */
9735
static int
9736
4.55M
xmlParseElementStart(xmlParserCtxtPtr ctxt) {
9737
4.55M
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
9738
4.55M
    const xmlChar *name;
9739
4.55M
    const xmlChar *prefix = NULL;
9740
4.55M
    const xmlChar *URI = NULL;
9741
4.55M
    xmlParserNodeInfo node_info;
9742
4.55M
    int line;
9743
4.55M
    xmlNodePtr cur;
9744
4.55M
    int nbNs = 0;
9745
9746
4.55M
    if (ctxt->nameNr > maxDepth) {
9747
21
        xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
9748
21
                "Excessive depth in document: %d use XML_PARSE_HUGE option\n",
9749
21
                ctxt->nameNr);
9750
21
  return(-1);
9751
21
    }
9752
9753
    /* Capture start position */
9754
4.55M
    if (ctxt->record_info) {
9755
0
        node_info.begin_pos = ctxt->input->consumed +
9756
0
                          (CUR_PTR - ctxt->input->base);
9757
0
  node_info.begin_line = ctxt->input->line;
9758
0
    }
9759
9760
4.55M
    if (ctxt->spaceNr == 0)
9761
0
  spacePush(ctxt, -1);
9762
4.55M
    else if (*ctxt->space == -2)
9763
0
  spacePush(ctxt, -1);
9764
4.55M
    else
9765
4.55M
  spacePush(ctxt, *ctxt->space);
9766
9767
4.55M
    line = ctxt->input->line;
9768
4.55M
#ifdef LIBXML_SAX1_ENABLED
9769
4.55M
    if (ctxt->sax2)
9770
4.55M
#endif /* LIBXML_SAX1_ENABLED */
9771
4.55M
        name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
9772
0
#ifdef LIBXML_SAX1_ENABLED
9773
0
    else
9774
0
  name = xmlParseStartTag(ctxt);
9775
4.55M
#endif /* LIBXML_SAX1_ENABLED */
9776
4.55M
    if (name == NULL) {
9777
2.54M
  spacePop(ctxt);
9778
2.54M
        return(-1);
9779
2.54M
    }
9780
2.01M
    nameNsPush(ctxt, name, prefix, URI, line, nbNs);
9781
2.01M
    cur = ctxt->node;
9782
9783
2.01M
#ifdef LIBXML_VALID_ENABLED
9784
    /*
9785
     * [ VC: Root Element Type ]
9786
     * The Name in the document type declaration must match the element
9787
     * type of the root element.
9788
     */
9789
2.01M
    if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9790
0
        ctxt->node && (ctxt->node == ctxt->myDoc->children))
9791
0
        ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
9792
2.01M
#endif /* LIBXML_VALID_ENABLED */
9793
9794
    /*
9795
     * Check for an Empty Element.
9796
     */
9797
2.01M
    if ((RAW == '/') && (NXT(1) == '>')) {
9798
115k
        SKIP(2);
9799
115k
  if (ctxt->sax2) {
9800
115k
      if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
9801
115k
    (!ctxt->disableSAX))
9802
91.7k
    ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
9803
115k
#ifdef LIBXML_SAX1_ENABLED
9804
115k
  } else {
9805
0
      if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
9806
0
    (!ctxt->disableSAX))
9807
0
    ctxt->sax->endElement(ctxt->userData, name);
9808
0
#endif /* LIBXML_SAX1_ENABLED */
9809
0
  }
9810
115k
  namePop(ctxt);
9811
115k
  spacePop(ctxt);
9812
115k
  if (nbNs > 0)
9813
11.0k
      xmlParserNsPop(ctxt, nbNs);
9814
115k
  if (cur != NULL && ctxt->record_info) {
9815
0
            node_info.node = cur;
9816
0
            node_info.end_pos = ctxt->input->consumed +
9817
0
                                (CUR_PTR - ctxt->input->base);
9818
0
            node_info.end_line = ctxt->input->line;
9819
0
            xmlParserAddNodeInfo(ctxt, &node_info);
9820
0
  }
9821
115k
  return(1);
9822
115k
    }
9823
1.89M
    if (RAW == '>') {
9824
1.67M
        NEXT1;
9825
1.67M
        if (cur != NULL && ctxt->record_info) {
9826
0
            node_info.node = cur;
9827
0
            node_info.end_pos = 0;
9828
0
            node_info.end_line = 0;
9829
0
            xmlParserAddNodeInfo(ctxt, &node_info);
9830
0
        }
9831
1.67M
    } else {
9832
223k
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
9833
223k
         "Couldn't find end of Start Tag %s line %d\n",
9834
223k
                    name, line, NULL);
9835
9836
  /*
9837
   * end of parsing of this node.
9838
   */
9839
223k
  nodePop(ctxt);
9840
223k
  namePop(ctxt);
9841
223k
  spacePop(ctxt);
9842
223k
  if (nbNs > 0)
9843
63.6k
      xmlParserNsPop(ctxt, nbNs);
9844
223k
  return(-1);
9845
223k
    }
9846
9847
1.67M
    return(0);
9848
1.89M
}
9849
9850
/**
9851
 * Parse the end of an XML element. Always consumes '</'.
9852
 *
9853
 * @param ctxt  an XML parser context
9854
 */
9855
static void
9856
1.40M
xmlParseElementEnd(xmlParserCtxtPtr ctxt) {
9857
1.40M
    xmlNodePtr cur = ctxt->node;
9858
9859
1.40M
    if (ctxt->nameNr <= 0) {
9860
0
        if ((RAW == '<') && (NXT(1) == '/'))
9861
0
            SKIP(2);
9862
0
        return;
9863
0
    }
9864
9865
    /*
9866
     * parse the end of tag: '</' should be here.
9867
     */
9868
1.40M
    if (ctxt->sax2) {
9869
1.40M
  xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
9870
1.40M
  namePop(ctxt);
9871
1.40M
    }
9872
0
#ifdef LIBXML_SAX1_ENABLED
9873
0
    else
9874
0
  xmlParseEndTag1(ctxt, 0);
9875
1.40M
#endif /* LIBXML_SAX1_ENABLED */
9876
9877
    /*
9878
     * Capture end position
9879
     */
9880
1.40M
    if (cur != NULL && ctxt->record_info) {
9881
0
        xmlParserNodeInfoPtr node_info;
9882
9883
0
        node_info = (xmlParserNodeInfoPtr) xmlParserFindNodeInfo(ctxt, cur);
9884
0
        if (node_info != NULL) {
9885
0
            node_info->end_pos = ctxt->input->consumed +
9886
0
                                 (CUR_PTR - ctxt->input->base);
9887
0
            node_info->end_line = ctxt->input->line;
9888
0
        }
9889
0
    }
9890
1.40M
}
9891
9892
/**
9893
 * Parse the XML version value.
9894
 *
9895
 * @deprecated Internal function, don't use.
9896
 *
9897
 *     [26] VersionNum ::= '1.' [0-9]+
9898
 *
9899
 * In practice allow [0-9].[0-9]+ at that level
9900
 *
9901
 * @param ctxt  an XML parser context
9902
 * @returns the string giving the XML version number, or NULL
9903
 */
9904
xmlChar *
9905
2.47k
xmlParseVersionNum(xmlParserCtxt *ctxt) {
9906
2.47k
    xmlChar *buf = NULL;
9907
2.47k
    int len = 0;
9908
2.47k
    int size = 10;
9909
2.47k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9910
686
                    XML_MAX_TEXT_LENGTH :
9911
2.47k
                    XML_MAX_NAME_LENGTH;
9912
2.47k
    xmlChar cur;
9913
9914
2.47k
    buf = xmlMalloc(size);
9915
2.47k
    if (buf == NULL) {
9916
0
  xmlErrMemory(ctxt);
9917
0
  return(NULL);
9918
0
    }
9919
2.47k
    cur = CUR;
9920
2.47k
    if (!((cur >= '0') && (cur <= '9'))) {
9921
57
  xmlFree(buf);
9922
57
  return(NULL);
9923
57
    }
9924
2.41k
    buf[len++] = cur;
9925
2.41k
    NEXT;
9926
2.41k
    cur=CUR;
9927
2.41k
    if (cur != '.') {
9928
36
  xmlFree(buf);
9929
36
  return(NULL);
9930
36
    }
9931
2.37k
    buf[len++] = cur;
9932
2.37k
    NEXT;
9933
2.37k
    cur=CUR;
9934
2.30M
    while ((cur >= '0') && (cur <= '9')) {
9935
2.30M
  if (len + 1 >= size) {
9936
566
      xmlChar *tmp;
9937
566
            int newSize;
9938
9939
566
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
9940
566
            if (newSize < 0) {
9941
1
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "VersionNum");
9942
1
                xmlFree(buf);
9943
1
                return(NULL);
9944
1
            }
9945
565
      tmp = xmlRealloc(buf, newSize);
9946
565
      if (tmp == NULL) {
9947
0
    xmlErrMemory(ctxt);
9948
0
          xmlFree(buf);
9949
0
    return(NULL);
9950
0
      }
9951
565
      buf = tmp;
9952
565
            size = newSize;
9953
565
  }
9954
2.30M
  buf[len++] = cur;
9955
2.30M
  NEXT;
9956
2.30M
  cur=CUR;
9957
2.30M
    }
9958
2.37k
    buf[len] = 0;
9959
2.37k
    return(buf);
9960
2.37k
}
9961
9962
/**
9963
 * Parse the XML version.
9964
 *
9965
 * @deprecated Internal function, don't use.
9966
 *
9967
 *     [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
9968
 *
9969
 *     [25] Eq ::= S? '=' S?
9970
 *
9971
 * @param ctxt  an XML parser context
9972
 * @returns the version string, e.g. "1.0"
9973
 */
9974
9975
xmlChar *
9976
8.88k
xmlParseVersionInfo(xmlParserCtxt *ctxt) {
9977
8.88k
    xmlChar *version = NULL;
9978
9979
8.88k
    if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
9980
2.68k
  SKIP(7);
9981
2.68k
  SKIP_BLANKS;
9982
2.68k
  if (RAW != '=') {
9983
143
      xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
9984
143
      return(NULL);
9985
143
        }
9986
2.53k
  NEXT;
9987
2.53k
  SKIP_BLANKS;
9988
2.53k
  if (RAW == '"') {
9989
1.84k
      NEXT;
9990
1.84k
      version = xmlParseVersionNum(ctxt);
9991
1.84k
      if (RAW != '"') {
9992
151
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
9993
151
      } else
9994
1.69k
          NEXT;
9995
1.84k
  } else if (RAW == '\''){
9996
625
      NEXT;
9997
625
      version = xmlParseVersionNum(ctxt);
9998
625
      if (RAW != '\'') {
9999
34
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10000
34
      } else
10001
591
          NEXT;
10002
625
  } else {
10003
69
      xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10004
69
  }
10005
2.53k
    }
10006
8.74k
    return(version);
10007
8.88k
}
10008
10009
/**
10010
 * Parse the XML encoding name
10011
 *
10012
 * @deprecated Internal function, don't use.
10013
 *
10014
 *     [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
10015
 *
10016
 * @param ctxt  an XML parser context
10017
 * @returns the encoding name value or NULL
10018
 */
10019
xmlChar *
10020
4.01k
xmlParseEncName(xmlParserCtxt *ctxt) {
10021
4.01k
    xmlChar *buf = NULL;
10022
4.01k
    int len = 0;
10023
4.01k
    int size = 10;
10024
4.01k
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
10025
1.63k
                    XML_MAX_TEXT_LENGTH :
10026
4.01k
                    XML_MAX_NAME_LENGTH;
10027
4.01k
    xmlChar cur;
10028
10029
4.01k
    cur = CUR;
10030
4.01k
    if (((cur >= 'a') && (cur <= 'z')) ||
10031
3.97k
        ((cur >= 'A') && (cur <= 'Z'))) {
10032
3.97k
  buf = xmlMalloc(size);
10033
3.97k
  if (buf == NULL) {
10034
0
      xmlErrMemory(ctxt);
10035
0
      return(NULL);
10036
0
  }
10037
10038
3.97k
  buf[len++] = cur;
10039
3.97k
  NEXT;
10040
3.97k
  cur = CUR;
10041
2.27M
  while (((cur >= 'a') && (cur <= 'z')) ||
10042
427k
         ((cur >= 'A') && (cur <= 'Z')) ||
10043
17.1k
         ((cur >= '0') && (cur <= '9')) ||
10044
9.53k
         (cur == '.') || (cur == '_') ||
10045
2.27M
         (cur == '-')) {
10046
2.27M
      if (len + 1 >= size) {
10047
994
          xmlChar *tmp;
10048
994
                int newSize;
10049
10050
994
                newSize = xmlGrowCapacity(size, 1, 1, maxLength);
10051
994
                if (newSize < 0) {
10052
1
                    xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "EncName");
10053
1
                    xmlFree(buf);
10054
1
                    return(NULL);
10055
1
                }
10056
993
    tmp = xmlRealloc(buf, newSize);
10057
993
    if (tmp == NULL) {
10058
0
        xmlErrMemory(ctxt);
10059
0
        xmlFree(buf);
10060
0
        return(NULL);
10061
0
    }
10062
993
    buf = tmp;
10063
993
                size = newSize;
10064
993
      }
10065
2.27M
      buf[len++] = cur;
10066
2.27M
      NEXT;
10067
2.27M
      cur = CUR;
10068
2.27M
        }
10069
3.97k
  buf[len] = 0;
10070
3.97k
    } else {
10071
39
  xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
10072
39
    }
10073
4.01k
    return(buf);
10074
4.01k
}
10075
10076
/**
10077
 * Parse the XML encoding declaration
10078
 *
10079
 * @deprecated Internal function, don't use.
10080
 *
10081
 *     [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | 
10082
 *                           "'" EncName "'")
10083
 *
10084
 * this setups the conversion filters.
10085
 *
10086
 * @param ctxt  an XML parser context
10087
 * @returns the encoding value or NULL
10088
 */
10089
10090
const xmlChar *
10091
8.86k
xmlParseEncodingDecl(xmlParserCtxt *ctxt) {
10092
8.86k
    xmlChar *encoding = NULL;
10093
10094
8.86k
    SKIP_BLANKS;
10095
8.86k
    if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g') == 0)
10096
4.82k
        return(NULL);
10097
10098
4.04k
    SKIP(8);
10099
4.04k
    SKIP_BLANKS;
10100
4.04k
    if (RAW != '=') {
10101
12
        xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
10102
12
        return(NULL);
10103
12
    }
10104
4.03k
    NEXT;
10105
4.03k
    SKIP_BLANKS;
10106
4.03k
    if (RAW == '"') {
10107
3.29k
        NEXT;
10108
3.29k
        encoding = xmlParseEncName(ctxt);
10109
3.29k
        if (RAW != '"') {
10110
149
            xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10111
149
            xmlFree(encoding);
10112
149
            return(NULL);
10113
149
        } else
10114
3.14k
            NEXT;
10115
3.29k
    } else if (RAW == '\''){
10116
714
        NEXT;
10117
714
        encoding = xmlParseEncName(ctxt);
10118
714
        if (RAW != '\'') {
10119
43
            xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10120
43
            xmlFree(encoding);
10121
43
            return(NULL);
10122
43
        } else
10123
671
            NEXT;
10124
714
    } else {
10125
20
        xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10126
20
    }
10127
10128
3.83k
    if (encoding == NULL)
10129
24
        return(NULL);
10130
10131
3.81k
    xmlSetDeclaredEncoding(ctxt, encoding);
10132
10133
3.81k
    return(ctxt->encoding);
10134
3.83k
}
10135
10136
/**
10137
 * Parse the XML standalone declaration
10138
 *
10139
 * @deprecated Internal function, don't use.
10140
 *
10141
 *     [32] SDDecl ::= S 'standalone' Eq
10142
 *                     (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
10143
 *
10144
 * [ VC: Standalone Document Declaration ]
10145
 * TODO The standalone document declaration must have the value "no"
10146
 * if any external markup declarations contain declarations of:
10147
 *  - attributes with default values, if elements to which these
10148
 *    attributes apply appear in the document without specifications
10149
 *    of values for these attributes, or
10150
 *  - entities (other than amp, lt, gt, apos, quot), if references
10151
 *    to those entities appear in the document, or
10152
 *  - attributes with values subject to normalization, where the
10153
 *    attribute appears in the document with a value which will change
10154
 *    as a result of normalization, or
10155
 *  - element types with element content, if white space occurs directly
10156
 *    within any instance of those types.
10157
 *
10158
 * @param ctxt  an XML parser context
10159
 * @returns
10160
 *   1 if standalone="yes"
10161
 *   0 if standalone="no"
10162
 *  -2 if standalone attribute is missing or invalid
10163
 *    (A standalone value of -2 means that the XML declaration was found,
10164
 *     but no value was specified for the standalone attribute).
10165
 */
10166
10167
int
10168
6.98k
xmlParseSDDecl(xmlParserCtxt *ctxt) {
10169
6.98k
    int standalone = -2;
10170
10171
6.98k
    SKIP_BLANKS;
10172
6.98k
    if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
10173
289
  SKIP(10);
10174
289
        SKIP_BLANKS;
10175
289
  if (RAW != '=') {
10176
12
      xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
10177
12
      return(standalone);
10178
12
        }
10179
277
  NEXT;
10180
277
  SKIP_BLANKS;
10181
277
        if (RAW == '\''){
10182
215
      NEXT;
10183
215
      if ((RAW == 'n') && (NXT(1) == 'o')) {
10184
177
          standalone = 0;
10185
177
                SKIP(2);
10186
177
      } else if ((RAW == 'y') && (NXT(1) == 'e') &&
10187
16
                 (NXT(2) == 's')) {
10188
4
          standalone = 1;
10189
4
    SKIP(3);
10190
34
            } else {
10191
34
    xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
10192
34
      }
10193
215
      if (RAW != '\'') {
10194
40
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10195
40
      } else
10196
175
          NEXT;
10197
215
  } else if (RAW == '"'){
10198
55
      NEXT;
10199
55
      if ((RAW == 'n') && (NXT(1) == 'o')) {
10200
4
          standalone = 0;
10201
4
    SKIP(2);
10202
51
      } else if ((RAW == 'y') && (NXT(1) == 'e') &&
10203
39
                 (NXT(2) == 's')) {
10204
37
          standalone = 1;
10205
37
                SKIP(3);
10206
37
            } else {
10207
14
    xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
10208
14
      }
10209
55
      if (RAW != '"') {
10210
33
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10211
33
      } else
10212
22
          NEXT;
10213
55
  } else {
10214
7
      xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10215
7
        }
10216
277
    }
10217
6.97k
    return(standalone);
10218
6.98k
}
10219
10220
/**
10221
 * Parse an XML declaration header
10222
 *
10223
 * @deprecated Internal function, don't use.
10224
 *
10225
 *     [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
10226
 * @param ctxt  an XML parser context
10227
 */
10228
10229
void
10230
8.88k
xmlParseXMLDecl(xmlParserCtxt *ctxt) {
10231
8.88k
    xmlChar *version;
10232
10233
    /*
10234
     * This value for standalone indicates that the document has an
10235
     * XML declaration but it does not have a standalone attribute.
10236
     * It will be overwritten later if a standalone attribute is found.
10237
     */
10238
10239
8.88k
    ctxt->standalone = -2;
10240
10241
    /*
10242
     * We know that '<?xml' is here.
10243
     */
10244
8.88k
    SKIP(5);
10245
10246
8.88k
    if (!IS_BLANK_CH(RAW)) {
10247
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
10248
0
                 "Blank needed after '<?xml'\n");
10249
0
    }
10250
8.88k
    SKIP_BLANKS;
10251
10252
    /*
10253
     * We must have the VersionInfo here.
10254
     */
10255
8.88k
    version = xmlParseVersionInfo(ctxt);
10256
8.88k
    if (version == NULL) {
10257
6.51k
  xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
10258
6.51k
    } else {
10259
2.37k
  if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
10260
      /*
10261
       * Changed here for XML-1.0 5th edition
10262
       */
10263
603
      if (ctxt->options & XML_PARSE_OLD10) {
10264
0
    xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
10265
0
                "Unsupported version '%s'\n",
10266
0
                version);
10267
603
      } else {
10268
603
          if ((version[0] == '1') && ((version[1] == '.'))) {
10269
537
        xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
10270
537
                      "Unsupported version '%s'\n",
10271
537
          version, NULL);
10272
537
    } else {
10273
66
        xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
10274
66
              "Unsupported version '%s'\n",
10275
66
              version);
10276
66
    }
10277
603
      }
10278
603
  }
10279
2.37k
  if (ctxt->version != NULL)
10280
0
      xmlFree(ctxt->version);
10281
2.37k
  ctxt->version = version;
10282
2.37k
    }
10283
10284
    /*
10285
     * We may have the encoding declaration
10286
     */
10287
8.88k
    if (!IS_BLANK_CH(RAW)) {
10288
6.58k
        if ((RAW == '?') && (NXT(1) == '>')) {
10289
22
      SKIP(2);
10290
22
      return;
10291
22
  }
10292
6.55k
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
10293
6.55k
    }
10294
8.86k
    xmlParseEncodingDecl(ctxt);
10295
10296
    /*
10297
     * We may have the standalone status.
10298
     */
10299
8.86k
    if ((ctxt->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
10300
3.50k
        if ((RAW == '?') && (NXT(1) == '>')) {
10301
1.88k
      SKIP(2);
10302
1.88k
      return;
10303
1.88k
  }
10304
1.62k
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
10305
1.62k
    }
10306
10307
    /*
10308
     * We can grow the input buffer freely at that point
10309
     */
10310
6.98k
    GROW;
10311
10312
6.98k
    SKIP_BLANKS;
10313
6.98k
    ctxt->standalone = xmlParseSDDecl(ctxt);
10314
10315
6.98k
    SKIP_BLANKS;
10316
6.98k
    if ((RAW == '?') && (NXT(1) == '>')) {
10317
212
        SKIP(2);
10318
6.77k
    } else if (RAW == '>') {
10319
        /* Deprecated old WD ... */
10320
2.70k
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
10321
2.70k
  NEXT;
10322
4.06k
    } else {
10323
4.06k
        int c;
10324
10325
4.06k
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
10326
231k
        while ((PARSER_STOPPED(ctxt) == 0) &&
10327
231k
               ((c = CUR) != 0)) {
10328
229k
            NEXT;
10329
229k
            if (c == '>')
10330
2.90k
                break;
10331
229k
        }
10332
4.06k
    }
10333
6.98k
}
10334
10335
/**
10336
 * @since 2.14.0
10337
 *
10338
 * @param ctxt  parser context
10339
 * @returns the version from the XML declaration.
10340
 */
10341
const xmlChar *
10342
0
xmlCtxtGetVersion(xmlParserCtxt *ctxt) {
10343
0
    if (ctxt == NULL)
10344
0
        return(NULL);
10345
10346
0
    return(ctxt->version);
10347
0
}
10348
10349
/**
10350
 * @since 2.14.0
10351
 *
10352
 * @param ctxt  parser context
10353
 * @returns the value from the standalone document declaration.
10354
 */
10355
int
10356
0
xmlCtxtGetStandalone(xmlParserCtxt *ctxt) {
10357
0
    if (ctxt == NULL)
10358
0
        return(0);
10359
10360
0
    return(ctxt->standalone);
10361
0
}
10362
10363
/**
10364
 * Parse an XML Misc* optional field.
10365
 *
10366
 * @deprecated Internal function, don't use.
10367
 *
10368
 *     [27] Misc ::= Comment | PI |  S
10369
 * @param ctxt  an XML parser context
10370
 */
10371
10372
void
10373
120k
xmlParseMisc(xmlParserCtxt *ctxt) {
10374
195k
    while (PARSER_STOPPED(ctxt) == 0) {
10375
194k
        SKIP_BLANKS;
10376
194k
        GROW;
10377
194k
        if ((RAW == '<') && (NXT(1) == '?')) {
10378
72.1k
      xmlParsePI(ctxt);
10379
122k
        } else if (CMP4(CUR_PTR, '<', '!', '-', '-')) {
10380
2.55k
      xmlParseComment(ctxt);
10381
119k
        } else {
10382
119k
            break;
10383
119k
        }
10384
194k
    }
10385
120k
}
10386
10387
static void
10388
58.2k
xmlFinishDocument(xmlParserCtxtPtr ctxt) {
10389
58.2k
    xmlDocPtr doc;
10390
10391
    /*
10392
     * SAX: end of the document processing.
10393
     */
10394
58.2k
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
10395
58.2k
        ctxt->sax->endDocument(ctxt->userData);
10396
10397
    /*
10398
     * Remove locally kept entity definitions if the tree was not built
10399
     */
10400
58.2k
    doc = ctxt->myDoc;
10401
58.2k
    if ((doc != NULL) &&
10402
55.3k
        (xmlStrEqual(doc->version, SAX_COMPAT_MODE))) {
10403
4.02k
        xmlFreeDoc(doc);
10404
4.02k
        ctxt->myDoc = NULL;
10405
4.02k
    }
10406
58.2k
}
10407
10408
/**
10409
 * Parse an XML document and invoke the SAX handlers. This is useful
10410
 * if you're only interested in custom SAX callbacks. If you want a
10411
 * document tree, use #xmlCtxtParseDocument.
10412
 *
10413
 * @param ctxt  an XML parser context
10414
 * @returns 0, -1 in case of error.
10415
 */
10416
10417
int
10418
58.3k
xmlParseDocument(xmlParserCtxt *ctxt) {
10419
58.3k
    if ((ctxt == NULL) || (ctxt->input == NULL))
10420
0
        return(-1);
10421
10422
58.3k
    GROW;
10423
10424
    /*
10425
     * SAX: detecting the level.
10426
     */
10427
58.3k
    xmlCtxtInitializeLate(ctxt);
10428
10429
58.3k
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10430
58.3k
        ctxt->sax->setDocumentLocator(ctxt->userData,
10431
58.3k
                (xmlSAXLocator *) &xmlDefaultSAXLocator);
10432
58.3k
    }
10433
10434
58.3k
    xmlDetectEncoding(ctxt);
10435
10436
58.3k
    if (CUR == 0) {
10437
124
  xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
10438
124
  return(-1);
10439
124
    }
10440
10441
58.2k
    GROW;
10442
58.2k
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
10443
10444
  /*
10445
   * Note that we will switch encoding on the fly.
10446
   */
10447
8.88k
  xmlParseXMLDecl(ctxt);
10448
8.88k
  SKIP_BLANKS;
10449
49.3k
    } else {
10450
49.3k
  ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10451
49.3k
        if (ctxt->version == NULL) {
10452
0
            xmlErrMemory(ctxt);
10453
0
            return(-1);
10454
0
        }
10455
49.3k
    }
10456
58.2k
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
10457
51.3k
        ctxt->sax->startDocument(ctxt->userData);
10458
58.2k
    if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) &&
10459
51.3k
        (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) {
10460
0
  ctxt->myDoc->compression = ctxt->input->buf->compressed;
10461
0
    }
10462
10463
    /*
10464
     * The Misc part of the Prolog
10465
     */
10466
58.2k
    xmlParseMisc(ctxt);
10467
10468
    /*
10469
     * Then possibly doc type declaration(s) and more Misc
10470
     * (doctypedecl Misc*)?
10471
     */
10472
58.2k
    GROW;
10473
58.2k
    if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
10474
10475
17.1k
  ctxt->inSubset = 1;
10476
17.1k
  xmlParseDocTypeDecl(ctxt);
10477
17.1k
  if (RAW == '[') {
10478
16.4k
      xmlParseInternalSubset(ctxt);
10479
16.4k
  } else if (RAW == '>') {
10480
157
            NEXT;
10481
157
        }
10482
10483
  /*
10484
   * Create and update the external subset.
10485
   */
10486
17.1k
  ctxt->inSubset = 2;
10487
17.1k
  if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
10488
17.1k
      (!ctxt->disableSAX))
10489
3.30k
      ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
10490
3.30k
                                ctxt->extSubSystem, ctxt->extSubURI);
10491
17.1k
  ctxt->inSubset = 0;
10492
10493
17.1k
        xmlCleanSpecialAttr(ctxt);
10494
10495
17.1k
  xmlParseMisc(ctxt);
10496
17.1k
    }
10497
10498
    /*
10499
     * Time to start parsing the tree itself
10500
     */
10501
58.2k
    GROW;
10502
58.2k
    if (RAW != '<') {
10503
13.1k
        if (ctxt->wellFormed)
10504
774
            xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
10505
774
                           "Start tag expected, '<' not found\n");
10506
45.0k
    } else {
10507
45.0k
  xmlParseElement(ctxt);
10508
10509
  /*
10510
   * The Misc part at the end
10511
   */
10512
45.0k
  xmlParseMisc(ctxt);
10513
10514
45.0k
        xmlParserCheckEOF(ctxt, XML_ERR_DOCUMENT_END);
10515
45.0k
    }
10516
10517
58.2k
    ctxt->instate = XML_PARSER_EOF;
10518
58.2k
    xmlFinishDocument(ctxt);
10519
10520
58.2k
    if (! ctxt->wellFormed) {
10521
37.5k
  ctxt->valid = 0;
10522
37.5k
  return(-1);
10523
37.5k
    }
10524
10525
20.6k
    return(0);
10526
58.2k
}
10527
10528
/**
10529
 * Parse a general parsed entity
10530
 * An external general parsed entity is well-formed if it matches the
10531
 * production labeled extParsedEnt.
10532
 *
10533
 * @deprecated Internal function, don't use.
10534
 *
10535
 *     [78] extParsedEnt ::= TextDecl? content
10536
 *
10537
 * @param ctxt  an XML parser context
10538
 * @returns 0, -1 in case of error. the parser context is augmented
10539
 *                as a result of the parsing.
10540
 */
10541
10542
int
10543
0
xmlParseExtParsedEnt(xmlParserCtxt *ctxt) {
10544
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
10545
0
        return(-1);
10546
10547
0
    xmlCtxtInitializeLate(ctxt);
10548
10549
0
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10550
0
        ctxt->sax->setDocumentLocator(ctxt->userData,
10551
0
                (xmlSAXLocator *) &xmlDefaultSAXLocator);
10552
0
    }
10553
10554
0
    xmlDetectEncoding(ctxt);
10555
10556
0
    if (CUR == 0) {
10557
0
  xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
10558
0
    }
10559
10560
    /*
10561
     * Check for the XMLDecl in the Prolog.
10562
     */
10563
0
    GROW;
10564
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
10565
10566
  /*
10567
   * Note that we will switch encoding on the fly.
10568
   */
10569
0
  xmlParseXMLDecl(ctxt);
10570
0
  SKIP_BLANKS;
10571
0
    } else {
10572
0
  ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10573
0
    }
10574
0
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
10575
0
        ctxt->sax->startDocument(ctxt->userData);
10576
10577
    /*
10578
     * Doing validity checking on chunk doesn't make sense
10579
     */
10580
0
    ctxt->options &= ~XML_PARSE_DTDVALID;
10581
0
    ctxt->validate = 0;
10582
0
    ctxt->depth = 0;
10583
10584
0
    xmlParseContentInternal(ctxt);
10585
10586
0
    if (ctxt->input->cur < ctxt->input->end)
10587
0
  xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
10588
10589
    /*
10590
     * SAX: end of the document processing.
10591
     */
10592
0
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
10593
0
        ctxt->sax->endDocument(ctxt->userData);
10594
10595
0
    if (! ctxt->wellFormed) return(-1);
10596
0
    return(0);
10597
0
}
10598
10599
#ifdef LIBXML_PUSH_ENABLED
10600
/************************************************************************
10601
 *                  *
10602
 *    Progressive parsing interfaces        *
10603
 *                  *
10604
 ************************************************************************/
10605
10606
/**
10607
 * Check whether the input buffer contains a character.
10608
 *
10609
 * @param ctxt  an XML parser context
10610
 * @param c  character
10611
 */
10612
static int
10613
0
xmlParseLookupChar(xmlParserCtxtPtr ctxt, int c) {
10614
0
    const xmlChar *cur;
10615
10616
0
    if (ctxt->checkIndex == 0) {
10617
0
        cur = ctxt->input->cur + 1;
10618
0
    } else {
10619
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10620
0
    }
10621
10622
0
    if (memchr(cur, c, ctxt->input->end - cur) == NULL) {
10623
0
        size_t index = ctxt->input->end - ctxt->input->cur;
10624
10625
0
        if (index > LONG_MAX) {
10626
0
            ctxt->checkIndex = 0;
10627
0
            return(1);
10628
0
        }
10629
0
        ctxt->checkIndex = index;
10630
0
        return(0);
10631
0
    } else {
10632
0
        ctxt->checkIndex = 0;
10633
0
        return(1);
10634
0
    }
10635
0
}
10636
10637
/**
10638
 * Check whether the input buffer contains a string.
10639
 *
10640
 * @param ctxt  an XML parser context
10641
 * @param startDelta  delta to apply at the start
10642
 * @param str  string
10643
 * @param strLen  length of string
10644
 */
10645
static const xmlChar *
10646
xmlParseLookupString(xmlParserCtxtPtr ctxt, size_t startDelta,
10647
0
                     const char *str, size_t strLen) {
10648
0
    const xmlChar *cur, *term;
10649
10650
0
    if (ctxt->checkIndex == 0) {
10651
0
        cur = ctxt->input->cur + startDelta;
10652
0
    } else {
10653
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10654
0
    }
10655
10656
0
    term = BAD_CAST strstr((const char *) cur, str);
10657
0
    if (term == NULL) {
10658
0
        const xmlChar *end = ctxt->input->end;
10659
0
        size_t index;
10660
10661
        /* Rescan (strLen - 1) characters. */
10662
0
        if ((size_t) (end - cur) < strLen)
10663
0
            end = cur;
10664
0
        else
10665
0
            end -= strLen - 1;
10666
0
        index = end - ctxt->input->cur;
10667
0
        if (index > LONG_MAX) {
10668
0
            ctxt->checkIndex = 0;
10669
0
            return(ctxt->input->end - strLen);
10670
0
        }
10671
0
        ctxt->checkIndex = index;
10672
0
    } else {
10673
0
        ctxt->checkIndex = 0;
10674
0
    }
10675
10676
0
    return(term);
10677
0
}
10678
10679
/**
10680
 * Check whether the input buffer contains terminated char data.
10681
 *
10682
 * @param ctxt  an XML parser context
10683
 */
10684
static int
10685
0
xmlParseLookupCharData(xmlParserCtxtPtr ctxt) {
10686
0
    const xmlChar *cur = ctxt->input->cur + ctxt->checkIndex;
10687
0
    const xmlChar *end = ctxt->input->end;
10688
0
    size_t index;
10689
10690
0
    while (cur < end) {
10691
0
        if ((*cur == '<') || (*cur == '&')) {
10692
0
            ctxt->checkIndex = 0;
10693
0
            return(1);
10694
0
        }
10695
0
        cur++;
10696
0
    }
10697
10698
0
    index = cur - ctxt->input->cur;
10699
0
    if (index > LONG_MAX) {
10700
0
        ctxt->checkIndex = 0;
10701
0
        return(1);
10702
0
    }
10703
0
    ctxt->checkIndex = index;
10704
0
    return(0);
10705
0
}
10706
10707
/**
10708
 * Check whether there's enough data in the input buffer to finish parsing
10709
 * a start tag. This has to take quotes into account.
10710
 *
10711
 * @param ctxt  an XML parser context
10712
 */
10713
static int
10714
0
xmlParseLookupGt(xmlParserCtxtPtr ctxt) {
10715
0
    const xmlChar *cur;
10716
0
    const xmlChar *end = ctxt->input->end;
10717
0
    int state = ctxt->endCheckState;
10718
0
    size_t index;
10719
10720
0
    if (ctxt->checkIndex == 0)
10721
0
        cur = ctxt->input->cur + 1;
10722
0
    else
10723
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10724
10725
0
    while (cur < end) {
10726
0
        if (state) {
10727
0
            if (*cur == state)
10728
0
                state = 0;
10729
0
        } else if (*cur == '\'' || *cur == '"') {
10730
0
            state = *cur;
10731
0
        } else if (*cur == '>') {
10732
0
            ctxt->checkIndex = 0;
10733
0
            ctxt->endCheckState = 0;
10734
0
            return(1);
10735
0
        }
10736
0
        cur++;
10737
0
    }
10738
10739
0
    index = cur - ctxt->input->cur;
10740
0
    if (index > LONG_MAX) {
10741
0
        ctxt->checkIndex = 0;
10742
0
        ctxt->endCheckState = 0;
10743
0
        return(1);
10744
0
    }
10745
0
    ctxt->checkIndex = index;
10746
0
    ctxt->endCheckState = state;
10747
0
    return(0);
10748
0
}
10749
10750
/**
10751
 * Check whether there's enough data in the input buffer to finish parsing
10752
 * the internal subset.
10753
 *
10754
 * @param ctxt  an XML parser context
10755
 */
10756
static int
10757
0
xmlParseLookupInternalSubset(xmlParserCtxtPtr ctxt) {
10758
    /*
10759
     * Sorry, but progressive parsing of the internal subset is not
10760
     * supported. We first check that the full content of the internal
10761
     * subset is available and parsing is launched only at that point.
10762
     * Internal subset ends with "']' S? '>'" in an unescaped section and
10763
     * not in a ']]>' sequence which are conditional sections.
10764
     */
10765
0
    const xmlChar *cur, *start;
10766
0
    const xmlChar *end = ctxt->input->end;
10767
0
    int state = ctxt->endCheckState;
10768
0
    size_t index;
10769
10770
0
    if (ctxt->checkIndex == 0) {
10771
0
        cur = ctxt->input->cur + 1;
10772
0
    } else {
10773
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10774
0
    }
10775
0
    start = cur;
10776
10777
0
    while (cur < end) {
10778
0
        if (state == '-') {
10779
0
            if ((*cur == '-') &&
10780
0
                (cur[1] == '-') &&
10781
0
                (cur[2] == '>')) {
10782
0
                state = 0;
10783
0
                cur += 3;
10784
0
                start = cur;
10785
0
                continue;
10786
0
            }
10787
0
        }
10788
0
        else if (state == ']') {
10789
0
            if (*cur == '>') {
10790
0
                ctxt->checkIndex = 0;
10791
0
                ctxt->endCheckState = 0;
10792
0
                return(1);
10793
0
            }
10794
0
            if (IS_BLANK_CH(*cur)) {
10795
0
                state = ' ';
10796
0
            } else if (*cur != ']') {
10797
0
                state = 0;
10798
0
                start = cur;
10799
0
                continue;
10800
0
            }
10801
0
        }
10802
0
        else if (state == ' ') {
10803
0
            if (*cur == '>') {
10804
0
                ctxt->checkIndex = 0;
10805
0
                ctxt->endCheckState = 0;
10806
0
                return(1);
10807
0
            }
10808
0
            if (!IS_BLANK_CH(*cur)) {
10809
0
                state = 0;
10810
0
                start = cur;
10811
0
                continue;
10812
0
            }
10813
0
        }
10814
0
        else if (state != 0) {
10815
0
            if (*cur == state) {
10816
0
                state = 0;
10817
0
                start = cur + 1;
10818
0
            }
10819
0
        }
10820
0
        else if (*cur == '<') {
10821
0
            if ((cur[1] == '!') &&
10822
0
                (cur[2] == '-') &&
10823
0
                (cur[3] == '-')) {
10824
0
                state = '-';
10825
0
                cur += 4;
10826
                /* Don't treat <!--> as comment */
10827
0
                start = cur;
10828
0
                continue;
10829
0
            }
10830
0
        }
10831
0
        else if ((*cur == '"') || (*cur == '\'') || (*cur == ']')) {
10832
0
            state = *cur;
10833
0
        }
10834
10835
0
        cur++;
10836
0
    }
10837
10838
    /*
10839
     * Rescan the three last characters to detect "<!--" and "-->"
10840
     * split across chunks.
10841
     */
10842
0
    if ((state == 0) || (state == '-')) {
10843
0
        if (cur - start < 3)
10844
0
            cur = start;
10845
0
        else
10846
0
            cur -= 3;
10847
0
    }
10848
0
    index = cur - ctxt->input->cur;
10849
0
    if (index > LONG_MAX) {
10850
0
        ctxt->checkIndex = 0;
10851
0
        ctxt->endCheckState = 0;
10852
0
        return(1);
10853
0
    }
10854
0
    ctxt->checkIndex = index;
10855
0
    ctxt->endCheckState = state;
10856
0
    return(0);
10857
0
}
10858
10859
/**
10860
 * Try to progress on parsing
10861
 *
10862
 * @param ctxt  an XML parser context
10863
 * @param terminate  last chunk indicator
10864
 * @returns zero if no parsing was possible
10865
 */
10866
static int
10867
0
xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
10868
0
    int ret = 0;
10869
0
    size_t avail;
10870
0
    xmlChar cur, next;
10871
10872
0
    if (ctxt->input == NULL)
10873
0
        return(0);
10874
10875
0
    if ((ctxt->input != NULL) &&
10876
0
        (ctxt->input->cur - ctxt->input->base > 4096)) {
10877
0
        xmlParserShrink(ctxt);
10878
0
    }
10879
10880
0
    while (ctxt->disableSAX == 0) {
10881
0
        avail = ctxt->input->end - ctxt->input->cur;
10882
0
        if (avail < 1)
10883
0
      goto done;
10884
0
        switch (ctxt->instate) {
10885
0
            case XML_PARSER_EOF:
10886
          /*
10887
     * Document parsing is done !
10888
     */
10889
0
          goto done;
10890
0
            case XML_PARSER_START:
10891
                /*
10892
                 * Very first chars read from the document flow.
10893
                 */
10894
0
                if ((!terminate) && (avail < 4))
10895
0
                    goto done;
10896
10897
                /*
10898
                 * We need more bytes to detect EBCDIC code pages.
10899
                 * See xmlDetectEBCDIC.
10900
                 */
10901
0
                if ((CMP4(CUR_PTR, 0x4C, 0x6F, 0xA7, 0x94)) &&
10902
0
                    (!terminate) && (avail < 200))
10903
0
                    goto done;
10904
10905
0
                xmlDetectEncoding(ctxt);
10906
0
                ctxt->instate = XML_PARSER_XML_DECL;
10907
0
    break;
10908
10909
0
            case XML_PARSER_XML_DECL:
10910
0
    if ((!terminate) && (avail < 2))
10911
0
        goto done;
10912
0
    cur = ctxt->input->cur[0];
10913
0
    next = ctxt->input->cur[1];
10914
0
          if ((cur == '<') && (next == '?')) {
10915
        /* PI or XML decl */
10916
0
        if ((!terminate) &&
10917
0
                        (!xmlParseLookupString(ctxt, 2, "?>", 2)))
10918
0
      goto done;
10919
0
        if ((ctxt->input->cur[2] == 'x') &&
10920
0
      (ctxt->input->cur[3] == 'm') &&
10921
0
      (ctxt->input->cur[4] == 'l') &&
10922
0
      (IS_BLANK_CH(ctxt->input->cur[5]))) {
10923
0
      ret += 5;
10924
0
      xmlParseXMLDecl(ctxt);
10925
0
        } else {
10926
0
      ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10927
0
                        if (ctxt->version == NULL) {
10928
0
                            xmlErrMemory(ctxt);
10929
0
                            break;
10930
0
                        }
10931
0
        }
10932
0
    } else {
10933
0
        ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10934
0
        if (ctxt->version == NULL) {
10935
0
            xmlErrMemory(ctxt);
10936
0
      break;
10937
0
        }
10938
0
    }
10939
0
                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10940
0
                    ctxt->sax->setDocumentLocator(ctxt->userData,
10941
0
                            (xmlSAXLocator *) &xmlDefaultSAXLocator);
10942
0
                }
10943
0
                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
10944
0
                    (!ctxt->disableSAX))
10945
0
                    ctxt->sax->startDocument(ctxt->userData);
10946
0
                ctxt->instate = XML_PARSER_MISC;
10947
0
    break;
10948
0
            case XML_PARSER_START_TAG: {
10949
0
          const xmlChar *name;
10950
0
    const xmlChar *prefix = NULL;
10951
0
    const xmlChar *URI = NULL;
10952
0
                int line = ctxt->input->line;
10953
0
    int nbNs = 0;
10954
10955
0
    if ((!terminate) && (avail < 2))
10956
0
        goto done;
10957
0
    cur = ctxt->input->cur[0];
10958
0
          if (cur != '<') {
10959
0
        xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
10960
0
                                   "Start tag expected, '<' not found");
10961
0
                    ctxt->instate = XML_PARSER_EOF;
10962
0
                    xmlFinishDocument(ctxt);
10963
0
        goto done;
10964
0
    }
10965
0
    if ((!terminate) && (!xmlParseLookupGt(ctxt)))
10966
0
                    goto done;
10967
0
    if (ctxt->spaceNr == 0)
10968
0
        spacePush(ctxt, -1);
10969
0
    else if (*ctxt->space == -2)
10970
0
        spacePush(ctxt, -1);
10971
0
    else
10972
0
        spacePush(ctxt, *ctxt->space);
10973
0
#ifdef LIBXML_SAX1_ENABLED
10974
0
    if (ctxt->sax2)
10975
0
#endif /* LIBXML_SAX1_ENABLED */
10976
0
        name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
10977
0
#ifdef LIBXML_SAX1_ENABLED
10978
0
    else
10979
0
        name = xmlParseStartTag(ctxt);
10980
0
#endif /* LIBXML_SAX1_ENABLED */
10981
0
    if (name == NULL) {
10982
0
        spacePop(ctxt);
10983
0
                    ctxt->instate = XML_PARSER_EOF;
10984
0
                    xmlFinishDocument(ctxt);
10985
0
        goto done;
10986
0
    }
10987
0
#ifdef LIBXML_VALID_ENABLED
10988
    /*
10989
     * [ VC: Root Element Type ]
10990
     * The Name in the document type declaration must match
10991
     * the element type of the root element.
10992
     */
10993
0
    if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
10994
0
        ctxt->node && (ctxt->node == ctxt->myDoc->children))
10995
0
        ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
10996
0
#endif /* LIBXML_VALID_ENABLED */
10997
10998
    /*
10999
     * Check for an Empty Element.
11000
     */
11001
0
    if ((RAW == '/') && (NXT(1) == '>')) {
11002
0
        SKIP(2);
11003
11004
0
        if (ctxt->sax2) {
11005
0
      if ((ctxt->sax != NULL) &&
11006
0
          (ctxt->sax->endElementNs != NULL) &&
11007
0
          (!ctxt->disableSAX))
11008
0
          ctxt->sax->endElementNs(ctxt->userData, name,
11009
0
                                  prefix, URI);
11010
0
      if (nbNs > 0)
11011
0
          xmlParserNsPop(ctxt, nbNs);
11012
0
#ifdef LIBXML_SAX1_ENABLED
11013
0
        } else {
11014
0
      if ((ctxt->sax != NULL) &&
11015
0
          (ctxt->sax->endElement != NULL) &&
11016
0
          (!ctxt->disableSAX))
11017
0
          ctxt->sax->endElement(ctxt->userData, name);
11018
0
#endif /* LIBXML_SAX1_ENABLED */
11019
0
        }
11020
0
        spacePop(ctxt);
11021
0
    } else if (RAW == '>') {
11022
0
        NEXT;
11023
0
                    nameNsPush(ctxt, name, prefix, URI, line, nbNs);
11024
0
    } else {
11025
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
11026
0
           "Couldn't find end of Start Tag %s\n",
11027
0
           name);
11028
0
        nodePop(ctxt);
11029
0
        spacePop(ctxt);
11030
0
                    if (nbNs > 0)
11031
0
                        xmlParserNsPop(ctxt, nbNs);
11032
0
    }
11033
11034
0
                if (ctxt->nameNr == 0)
11035
0
                    ctxt->instate = XML_PARSER_EPILOG;
11036
0
                else
11037
0
                    ctxt->instate = XML_PARSER_CONTENT;
11038
0
                break;
11039
0
      }
11040
0
            case XML_PARSER_CONTENT: {
11041
0
    cur = ctxt->input->cur[0];
11042
11043
0
    if (cur == '<') {
11044
0
                    if ((!terminate) && (avail < 2))
11045
0
                        goto done;
11046
0
        next = ctxt->input->cur[1];
11047
11048
0
                    if (next == '/') {
11049
0
                        ctxt->instate = XML_PARSER_END_TAG;
11050
0
                        break;
11051
0
                    } else if (next == '?') {
11052
0
                        if ((!terminate) &&
11053
0
                            (!xmlParseLookupString(ctxt, 2, "?>", 2)))
11054
0
                            goto done;
11055
0
                        xmlParsePI(ctxt);
11056
0
                        ctxt->instate = XML_PARSER_CONTENT;
11057
0
                        break;
11058
0
                    } else if (next == '!') {
11059
0
                        if ((!terminate) && (avail < 3))
11060
0
                            goto done;
11061
0
                        next = ctxt->input->cur[2];
11062
11063
0
                        if (next == '-') {
11064
0
                            if ((!terminate) && (avail < 4))
11065
0
                                goto done;
11066
0
                            if (ctxt->input->cur[3] == '-') {
11067
0
                                if ((!terminate) &&
11068
0
                                    (!xmlParseLookupString(ctxt, 4, "-->", 3)))
11069
0
                                    goto done;
11070
0
                                xmlParseComment(ctxt);
11071
0
                                ctxt->instate = XML_PARSER_CONTENT;
11072
0
                                break;
11073
0
                            }
11074
0
                        } else if (next == '[') {
11075
0
                            if ((!terminate) && (avail < 9))
11076
0
                                goto done;
11077
0
                            if ((ctxt->input->cur[2] == '[') &&
11078
0
                                (ctxt->input->cur[3] == 'C') &&
11079
0
                                (ctxt->input->cur[4] == 'D') &&
11080
0
                                (ctxt->input->cur[5] == 'A') &&
11081
0
                                (ctxt->input->cur[6] == 'T') &&
11082
0
                                (ctxt->input->cur[7] == 'A') &&
11083
0
                                (ctxt->input->cur[8] == '[')) {
11084
0
                                if ((!terminate) &&
11085
0
                                    (!xmlParseLookupString(ctxt, 9, "]]>", 3)))
11086
0
                                    goto done;
11087
0
                                ctxt->instate = XML_PARSER_CDATA_SECTION;
11088
0
                                xmlParseCDSect(ctxt);
11089
0
                                ctxt->instate = XML_PARSER_CONTENT;
11090
0
                                break;
11091
0
                            }
11092
0
                        }
11093
0
                    }
11094
0
    } else if (cur == '&') {
11095
0
        if ((!terminate) && (!xmlParseLookupChar(ctxt, ';')))
11096
0
      goto done;
11097
0
        xmlParseReference(ctxt);
11098
0
                    break;
11099
0
    } else {
11100
        /* TODO Avoid the extra copy, handle directly !!! */
11101
        /*
11102
         * Goal of the following test is:
11103
         *  - minimize calls to the SAX 'character' callback
11104
         *    when they are mergeable
11105
         *  - handle an problem for isBlank when we only parse
11106
         *    a sequence of blank chars and the next one is
11107
         *    not available to check against '<' presence.
11108
         *  - tries to homogenize the differences in SAX
11109
         *    callbacks between the push and pull versions
11110
         *    of the parser.
11111
         */
11112
0
        if (avail < XML_PARSER_BIG_BUFFER_SIZE) {
11113
0
      if ((!terminate) && (!xmlParseLookupCharData(ctxt)))
11114
0
          goto done;
11115
0
                    }
11116
0
                    ctxt->checkIndex = 0;
11117
0
        xmlParseCharDataInternal(ctxt, !terminate);
11118
0
                    break;
11119
0
    }
11120
11121
0
                ctxt->instate = XML_PARSER_START_TAG;
11122
0
    break;
11123
0
      }
11124
0
            case XML_PARSER_END_TAG:
11125
0
    if ((!terminate) && (!xmlParseLookupChar(ctxt, '>')))
11126
0
        goto done;
11127
0
    if (ctxt->sax2) {
11128
0
              xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
11129
0
        nameNsPop(ctxt);
11130
0
    }
11131
0
#ifdef LIBXML_SAX1_ENABLED
11132
0
      else
11133
0
        xmlParseEndTag1(ctxt, 0);
11134
0
#endif /* LIBXML_SAX1_ENABLED */
11135
0
    if (ctxt->nameNr == 0) {
11136
0
        ctxt->instate = XML_PARSER_EPILOG;
11137
0
    } else {
11138
0
        ctxt->instate = XML_PARSER_CONTENT;
11139
0
    }
11140
0
    break;
11141
0
            case XML_PARSER_MISC:
11142
0
            case XML_PARSER_PROLOG:
11143
0
            case XML_PARSER_EPILOG:
11144
0
    SKIP_BLANKS;
11145
0
                avail = ctxt->input->end - ctxt->input->cur;
11146
0
    if (avail < 1)
11147
0
        goto done;
11148
0
    if (ctxt->input->cur[0] == '<') {
11149
0
                    if ((!terminate) && (avail < 2))
11150
0
                        goto done;
11151
0
                    next = ctxt->input->cur[1];
11152
0
                    if (next == '?') {
11153
0
                        if ((!terminate) &&
11154
0
                            (!xmlParseLookupString(ctxt, 2, "?>", 2)))
11155
0
                            goto done;
11156
0
                        xmlParsePI(ctxt);
11157
0
                        break;
11158
0
                    } else if (next == '!') {
11159
0
                        if ((!terminate) && (avail < 3))
11160
0
                            goto done;
11161
11162
0
                        if (ctxt->input->cur[2] == '-') {
11163
0
                            if ((!terminate) && (avail < 4))
11164
0
                                goto done;
11165
0
                            if (ctxt->input->cur[3] == '-') {
11166
0
                                if ((!terminate) &&
11167
0
                                    (!xmlParseLookupString(ctxt, 4, "-->", 3)))
11168
0
                                    goto done;
11169
0
                                xmlParseComment(ctxt);
11170
0
                                break;
11171
0
                            }
11172
0
                        } else if (ctxt->instate == XML_PARSER_MISC) {
11173
0
                            if ((!terminate) && (avail < 9))
11174
0
                                goto done;
11175
0
                            if ((ctxt->input->cur[2] == 'D') &&
11176
0
                                (ctxt->input->cur[3] == 'O') &&
11177
0
                                (ctxt->input->cur[4] == 'C') &&
11178
0
                                (ctxt->input->cur[5] == 'T') &&
11179
0
                                (ctxt->input->cur[6] == 'Y') &&
11180
0
                                (ctxt->input->cur[7] == 'P') &&
11181
0
                                (ctxt->input->cur[8] == 'E')) {
11182
0
                                if ((!terminate) && (!xmlParseLookupGt(ctxt)))
11183
0
                                    goto done;
11184
0
                                ctxt->inSubset = 1;
11185
0
                                xmlParseDocTypeDecl(ctxt);
11186
0
                                if (RAW == '[') {
11187
0
                                    ctxt->instate = XML_PARSER_DTD;
11188
0
                                } else {
11189
0
                                    if (RAW == '>')
11190
0
                                        NEXT;
11191
                                    /*
11192
                                     * Create and update the external subset.
11193
                                     */
11194
0
                                    ctxt->inSubset = 2;
11195
0
                                    if ((ctxt->sax != NULL) &&
11196
0
                                        (!ctxt->disableSAX) &&
11197
0
                                        (ctxt->sax->externalSubset != NULL))
11198
0
                                        ctxt->sax->externalSubset(
11199
0
                                                ctxt->userData,
11200
0
                                                ctxt->intSubName,
11201
0
                                                ctxt->extSubSystem,
11202
0
                                                ctxt->extSubURI);
11203
0
                                    ctxt->inSubset = 0;
11204
0
                                    xmlCleanSpecialAttr(ctxt);
11205
0
                                    ctxt->instate = XML_PARSER_PROLOG;
11206
0
                                }
11207
0
                                break;
11208
0
                            }
11209
0
                        }
11210
0
                    }
11211
0
                }
11212
11213
0
                if (ctxt->instate == XML_PARSER_EPILOG) {
11214
0
                    if (ctxt->errNo == XML_ERR_OK)
11215
0
                        xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
11216
0
        ctxt->instate = XML_PARSER_EOF;
11217
0
                    xmlFinishDocument(ctxt);
11218
0
                } else {
11219
0
        ctxt->instate = XML_PARSER_START_TAG;
11220
0
    }
11221
0
    break;
11222
0
            case XML_PARSER_DTD: {
11223
0
                if ((!terminate) && (!xmlParseLookupInternalSubset(ctxt)))
11224
0
                    goto done;
11225
0
    xmlParseInternalSubset(ctxt);
11226
0
    ctxt->inSubset = 2;
11227
0
    if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
11228
0
        (ctxt->sax->externalSubset != NULL))
11229
0
        ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
11230
0
          ctxt->extSubSystem, ctxt->extSubURI);
11231
0
    ctxt->inSubset = 0;
11232
0
    xmlCleanSpecialAttr(ctxt);
11233
0
    ctxt->instate = XML_PARSER_PROLOG;
11234
0
                break;
11235
0
      }
11236
0
            default:
11237
0
                xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
11238
0
      "PP: internal error\n");
11239
0
    ctxt->instate = XML_PARSER_EOF;
11240
0
    break;
11241
0
  }
11242
0
    }
11243
0
done:
11244
0
    return(ret);
11245
0
}
11246
11247
/**
11248
 * Parse a chunk of memory in push parser mode.
11249
 *
11250
 * Assumes that the parser context was initialized with
11251
 * #xmlCreatePushParserCtxt.
11252
 *
11253
 * The last chunk, which will often be empty, must be marked with
11254
 * the `terminate` flag. With the default SAX callbacks, the resulting
11255
 * document will be available in ctxt->myDoc. This pointer will not
11256
 * be freed when calling #xmlFreeParserCtxt and must be freed by the
11257
 * caller. If the document isn't well-formed, it will still be returned
11258
 * in ctxt->myDoc.
11259
 *
11260
 * As an exception, #xmlCtxtResetPush will free the document in
11261
 * ctxt->myDoc. So ctxt->myDoc should be set to NULL after extracting
11262
 * the document.
11263
 *
11264
 * Since 2.14.0, #xmlCtxtGetDocument can be used to retrieve the
11265
 * result document.
11266
 *
11267
 * @param ctxt  an XML parser context
11268
 * @param chunk  chunk of memory
11269
 * @param size  size of chunk in bytes
11270
 * @param terminate  last chunk indicator
11271
 * @returns an xmlParserErrors code (0 on success).
11272
 */
11273
int
11274
xmlParseChunk(xmlParserCtxt *ctxt, const char *chunk, int size,
11275
0
              int terminate) {
11276
0
    size_t curBase;
11277
0
    size_t maxLength;
11278
0
    size_t pos;
11279
0
    int end_in_lf = 0;
11280
0
    int res;
11281
11282
0
    if ((ctxt == NULL) || (size < 0))
11283
0
        return(XML_ERR_ARGUMENT);
11284
0
    if ((chunk == NULL) && (size > 0))
11285
0
        return(XML_ERR_ARGUMENT);
11286
0
    if ((ctxt->input == NULL) || (ctxt->input->buf == NULL))
11287
0
        return(XML_ERR_ARGUMENT);
11288
0
    if (ctxt->disableSAX != 0)
11289
0
        return(ctxt->errNo);
11290
11291
0
    ctxt->input->flags |= XML_INPUT_PROGRESSIVE;
11292
0
    if (ctxt->instate == XML_PARSER_START)
11293
0
        xmlCtxtInitializeLate(ctxt);
11294
0
    if ((size > 0) && (chunk != NULL) && (!terminate) &&
11295
0
        (chunk[size - 1] == '\r')) {
11296
0
  end_in_lf = 1;
11297
0
  size--;
11298
0
    }
11299
11300
    /*
11301
     * Also push an empty chunk to make sure that the raw buffer
11302
     * will be flushed if there is an encoder.
11303
     */
11304
0
    pos = ctxt->input->cur - ctxt->input->base;
11305
0
    res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
11306
0
    xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
11307
0
    if (res < 0) {
11308
0
        xmlCtxtErrIO(ctxt, ctxt->input->buf->error, NULL);
11309
0
        return(ctxt->errNo);
11310
0
    }
11311
11312
0
    xmlParseTryOrFinish(ctxt, terminate);
11313
11314
0
    curBase = ctxt->input->cur - ctxt->input->base;
11315
0
    maxLength = (ctxt->options & XML_PARSE_HUGE) ?
11316
0
                XML_MAX_HUGE_LENGTH :
11317
0
                XML_MAX_LOOKUP_LIMIT;
11318
0
    if (curBase > maxLength) {
11319
0
        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
11320
0
                    "Buffer size limit exceeded, try XML_PARSE_HUGE\n");
11321
0
    }
11322
11323
0
    if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX != 0))
11324
0
        return(ctxt->errNo);
11325
11326
0
    if (end_in_lf == 1) {
11327
0
  pos = ctxt->input->cur - ctxt->input->base;
11328
0
  res = xmlParserInputBufferPush(ctxt->input->buf, 1, "\r");
11329
0
  xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
11330
0
        if (res < 0) {
11331
0
            xmlCtxtErrIO(ctxt, ctxt->input->buf->error, NULL);
11332
0
            return(ctxt->errNo);
11333
0
        }
11334
0
    }
11335
0
    if (terminate) {
11336
  /*
11337
   * Check for termination
11338
   */
11339
0
        if ((ctxt->instate != XML_PARSER_EOF) &&
11340
0
            (ctxt->instate != XML_PARSER_EPILOG)) {
11341
0
            if (ctxt->nameNr > 0) {
11342
0
                const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
11343
0
                int line = ctxt->pushTab[ctxt->nameNr - 1].line;
11344
0
                xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
11345
0
                        "Premature end of data in tag %s line %d\n",
11346
0
                        name, line, NULL);
11347
0
            } else if (ctxt->instate == XML_PARSER_START) {
11348
0
                xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
11349
0
            } else {
11350
0
                xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
11351
0
                               "Start tag expected, '<' not found\n");
11352
0
            }
11353
0
        } else {
11354
0
            xmlParserCheckEOF(ctxt, XML_ERR_DOCUMENT_END);
11355
0
        }
11356
0
  if (ctxt->instate != XML_PARSER_EOF) {
11357
0
            ctxt->instate = XML_PARSER_EOF;
11358
0
            xmlFinishDocument(ctxt);
11359
0
  }
11360
0
    }
11361
0
    if (ctxt->wellFormed == 0)
11362
0
  return((xmlParserErrors) ctxt->errNo);
11363
0
    else
11364
0
        return(0);
11365
0
}
11366
11367
/************************************************************************
11368
 *                  *
11369
 *    I/O front end functions to the parser     *
11370
 *                  *
11371
 ************************************************************************/
11372
11373
/**
11374
 * Create a parser context for using the XML parser in push mode.
11375
 * See #xmlParseChunk.
11376
 *
11377
 * Passing an initial chunk is useless and deprecated.
11378
 *
11379
 * The push parser doesn't support recovery mode or the
11380
 * XML_PARSE_NOBLANKS option.
11381
 *
11382
 * `filename` is used as base URI to fetch external entities and for
11383
 * error reports.
11384
 *
11385
 * @param sax  a SAX handler (optional)
11386
 * @param user_data  user data for SAX callbacks (optional)
11387
 * @param chunk  initial chunk (optional, deprecated)
11388
 * @param size  size of initial chunk in bytes
11389
 * @param filename  file name or URI (optional)
11390
 * @returns the new parser context or NULL if a memory allocation
11391
 * failed.
11392
 */
11393
11394
xmlParserCtxt *
11395
xmlCreatePushParserCtxt(xmlSAXHandler *sax, void *user_data,
11396
0
                        const char *chunk, int size, const char *filename) {
11397
0
    xmlParserCtxtPtr ctxt;
11398
0
    xmlParserInputPtr input;
11399
11400
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11401
0
    if (ctxt == NULL)
11402
0
  return(NULL);
11403
11404
0
    ctxt->options &= ~XML_PARSE_NODICT;
11405
0
    ctxt->dictNames = 1;
11406
11407
0
    input = xmlNewPushInput(filename, chunk, size);
11408
0
    if (input == NULL) {
11409
0
  xmlFreeParserCtxt(ctxt);
11410
0
  return(NULL);
11411
0
    }
11412
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11413
0
        xmlFreeInputStream(input);
11414
0
        xmlFreeParserCtxt(ctxt);
11415
0
        return(NULL);
11416
0
    }
11417
11418
0
    return(ctxt);
11419
0
}
11420
#endif /* LIBXML_PUSH_ENABLED */
11421
11422
/**
11423
 * Blocks further parser processing
11424
 *
11425
 * @param ctxt  an XML parser context
11426
 */
11427
void
11428
0
xmlStopParser(xmlParserCtxt *ctxt) {
11429
0
    if (ctxt == NULL)
11430
0
        return;
11431
11432
    /* This stops the parser */
11433
0
    ctxt->disableSAX = 2;
11434
11435
    /*
11436
     * xmlStopParser is often called from error handlers,
11437
     * so we can't raise an error here to avoid infinite
11438
     * loops. Just make sure that an error condition is
11439
     * reported.
11440
     */
11441
0
    if (ctxt->errNo == XML_ERR_OK) {
11442
0
        ctxt->errNo = XML_ERR_USER_STOP;
11443
0
        ctxt->lastError.code = XML_ERR_USER_STOP;
11444
0
        ctxt->wellFormed = 0;
11445
0
    }
11446
0
}
11447
11448
/**
11449
 * Create a parser context for using the XML parser with an existing
11450
 * I/O stream
11451
 *
11452
 * @param sax  a SAX handler (optional)
11453
 * @param user_data  user data for SAX callbacks (optional)
11454
 * @param ioread  an I/O read function
11455
 * @param ioclose  an I/O close function (optional)
11456
 * @param ioctx  an I/O handler
11457
 * @param enc  the charset encoding if known (deprecated)
11458
 * @returns the new parser context or NULL
11459
 */
11460
xmlParserCtxt *
11461
xmlCreateIOParserCtxt(xmlSAXHandler *sax, void *user_data,
11462
                      xmlInputReadCallback ioread,
11463
                      xmlInputCloseCallback ioclose,
11464
0
                      void *ioctx, xmlCharEncoding enc) {
11465
0
    xmlParserCtxtPtr ctxt;
11466
0
    xmlParserInputPtr input;
11467
0
    const char *encoding;
11468
11469
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11470
0
    if (ctxt == NULL)
11471
0
  return(NULL);
11472
11473
0
    encoding = xmlGetCharEncodingName(enc);
11474
0
    input = xmlCtxtNewInputFromIO(ctxt, NULL, ioread, ioclose, ioctx,
11475
0
                                  encoding, 0);
11476
0
    if (input == NULL) {
11477
0
  xmlFreeParserCtxt(ctxt);
11478
0
        return (NULL);
11479
0
    }
11480
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11481
0
        xmlFreeInputStream(input);
11482
0
        xmlFreeParserCtxt(ctxt);
11483
0
        return(NULL);
11484
0
    }
11485
11486
0
    return(ctxt);
11487
0
}
11488
11489
#ifdef LIBXML_VALID_ENABLED
11490
/************************************************************************
11491
 *                  *
11492
 *    Front ends when parsing a DTD       *
11493
 *                  *
11494
 ************************************************************************/
11495
11496
/**
11497
 * Parse a DTD.
11498
 *
11499
 * Option XML_PARSE_DTDLOAD should be enabled in the parser context
11500
 * to make external entities work.
11501
 *
11502
 * @since 2.14.0
11503
 *
11504
 * @param ctxt  a parser context
11505
 * @param input  a parser input
11506
 * @param publicId  public ID of the DTD (optional)
11507
 * @param systemId  system ID of the DTD (optional)
11508
 * @returns the resulting xmlDtd or NULL in case of error.
11509
 * `input` will be freed by the function in any case.
11510
 */
11511
xmlDtd *
11512
xmlCtxtParseDtd(xmlParserCtxt *ctxt, xmlParserInput *input,
11513
0
                const xmlChar *publicId, const xmlChar *systemId) {
11514
0
    xmlDtdPtr ret = NULL;
11515
11516
0
    if ((ctxt == NULL) || (input == NULL)) {
11517
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
11518
0
        xmlFreeInputStream(input);
11519
0
        return(NULL);
11520
0
    }
11521
11522
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11523
0
        xmlFreeInputStream(input);
11524
0
        return(NULL);
11525
0
    }
11526
11527
0
    if (publicId == NULL)
11528
0
        publicId = BAD_CAST "none";
11529
0
    if (systemId == NULL)
11530
0
        systemId = BAD_CAST "none";
11531
11532
0
    ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
11533
0
    if (ctxt->myDoc == NULL) {
11534
0
        xmlErrMemory(ctxt);
11535
0
        goto error;
11536
0
    }
11537
0
    ctxt->myDoc->properties = XML_DOC_INTERNAL;
11538
0
    ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
11539
0
                                       publicId, systemId);
11540
0
    if (ctxt->myDoc->extSubset == NULL) {
11541
0
        xmlErrMemory(ctxt);
11542
0
        xmlFreeDoc(ctxt->myDoc);
11543
0
        goto error;
11544
0
    }
11545
11546
0
    xmlParseExternalSubset(ctxt, publicId, systemId);
11547
11548
0
    if (ctxt->wellFormed) {
11549
0
        ret = ctxt->myDoc->extSubset;
11550
0
        ctxt->myDoc->extSubset = NULL;
11551
0
        if (ret != NULL) {
11552
0
            xmlNodePtr tmp;
11553
11554
0
            ret->doc = NULL;
11555
0
            tmp = ret->children;
11556
0
            while (tmp != NULL) {
11557
0
                tmp->doc = NULL;
11558
0
                tmp = tmp->next;
11559
0
            }
11560
0
        }
11561
0
    } else {
11562
0
        ret = NULL;
11563
0
    }
11564
0
    xmlFreeDoc(ctxt->myDoc);
11565
0
    ctxt->myDoc = NULL;
11566
11567
0
error:
11568
0
    xmlFreeInputStream(xmlCtxtPopInput(ctxt));
11569
11570
0
    return(ret);
11571
0
}
11572
11573
/**
11574
 * Load and parse a DTD
11575
 *
11576
 * @deprecated Use #xmlCtxtParseDtd.
11577
 *
11578
 * @param sax  the SAX handler block or NULL
11579
 * @param input  an Input Buffer
11580
 * @param enc  the charset encoding if known
11581
 * @returns the resulting xmlDtd or NULL in case of error.
11582
 * `input` will be freed by the function in any case.
11583
 */
11584
11585
xmlDtd *
11586
xmlIOParseDTD(xmlSAXHandler *sax, xmlParserInputBuffer *input,
11587
0
        xmlCharEncoding enc) {
11588
0
    xmlDtdPtr ret = NULL;
11589
0
    xmlParserCtxtPtr ctxt;
11590
0
    xmlParserInputPtr pinput = NULL;
11591
11592
0
    if (input == NULL)
11593
0
  return(NULL);
11594
11595
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
11596
0
    if (ctxt == NULL) {
11597
0
        xmlFreeParserInputBuffer(input);
11598
0
  return(NULL);
11599
0
    }
11600
0
    xmlCtxtSetOptions(ctxt, XML_PARSE_DTDLOAD);
11601
11602
    /*
11603
     * generate a parser input from the I/O handler
11604
     */
11605
11606
0
    pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
11607
0
    if (pinput == NULL) {
11608
0
  xmlFreeParserCtxt(ctxt);
11609
0
  return(NULL);
11610
0
    }
11611
11612
0
    if (enc != XML_CHAR_ENCODING_NONE) {
11613
0
        xmlSwitchEncoding(ctxt, enc);
11614
0
    }
11615
11616
0
    ret = xmlCtxtParseDtd(ctxt, pinput, NULL, NULL);
11617
11618
0
    xmlFreeParserCtxt(ctxt);
11619
0
    return(ret);
11620
0
}
11621
11622
/**
11623
 * Load and parse an external subset.
11624
 *
11625
 * @deprecated Use #xmlCtxtParseDtd.
11626
 *
11627
 * @param sax  the SAX handler block
11628
 * @param publicId  public identifier of the DTD (optional)
11629
 * @param systemId  system identifier (URL) of the DTD
11630
 * @returns the resulting xmlDtd or NULL in case of error.
11631
 */
11632
11633
xmlDtd *
11634
xmlSAXParseDTD(xmlSAXHandler *sax, const xmlChar *publicId,
11635
0
               const xmlChar *systemId) {
11636
0
    xmlDtdPtr ret = NULL;
11637
0
    xmlParserCtxtPtr ctxt;
11638
0
    xmlParserInputPtr input = NULL;
11639
0
    xmlChar* systemIdCanonic;
11640
11641
0
    if ((publicId == NULL) && (systemId == NULL)) return(NULL);
11642
11643
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
11644
0
    if (ctxt == NULL) {
11645
0
  return(NULL);
11646
0
    }
11647
0
    xmlCtxtSetOptions(ctxt, XML_PARSE_DTDLOAD);
11648
11649
    /*
11650
     * Canonicalise the system ID
11651
     */
11652
0
    systemIdCanonic = xmlCanonicPath(systemId);
11653
0
    if ((systemId != NULL) && (systemIdCanonic == NULL)) {
11654
0
  xmlFreeParserCtxt(ctxt);
11655
0
  return(NULL);
11656
0
    }
11657
11658
    /*
11659
     * Ask the Entity resolver to load the damn thing
11660
     */
11661
11662
0
    if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
11663
0
  input = ctxt->sax->resolveEntity(ctxt->userData, publicId,
11664
0
                                   systemIdCanonic);
11665
0
    if (input == NULL) {
11666
0
  xmlFreeParserCtxt(ctxt);
11667
0
  if (systemIdCanonic != NULL)
11668
0
      xmlFree(systemIdCanonic);
11669
0
  return(NULL);
11670
0
    }
11671
11672
0
    if (input->filename == NULL)
11673
0
  input->filename = (char *) systemIdCanonic;
11674
0
    else
11675
0
  xmlFree(systemIdCanonic);
11676
11677
0
    ret = xmlCtxtParseDtd(ctxt, input, publicId, systemId);
11678
11679
0
    xmlFreeParserCtxt(ctxt);
11680
0
    return(ret);
11681
0
}
11682
11683
11684
/**
11685
 * Load and parse an external subset.
11686
 *
11687
 * @param publicId  public identifier of the DTD (optional)
11688
 * @param systemId  system identifier (URL) of the DTD
11689
 * @returns the resulting xmlDtd or NULL in case of error.
11690
 */
11691
11692
xmlDtd *
11693
0
xmlParseDTD(const xmlChar *publicId, const xmlChar *systemId) {
11694
0
    return(xmlSAXParseDTD(NULL, publicId, systemId));
11695
0
}
11696
#endif /* LIBXML_VALID_ENABLED */
11697
11698
/************************************************************************
11699
 *                  *
11700
 *    Front ends when parsing an Entity     *
11701
 *                  *
11702
 ************************************************************************/
11703
11704
static xmlNodePtr
11705
xmlCtxtParseContentInternal(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
11706
12.4k
                            int hasTextDecl, int buildTree) {
11707
12.4k
    xmlNodePtr root = NULL;
11708
12.4k
    xmlNodePtr list = NULL;
11709
12.4k
    xmlChar *rootName = BAD_CAST "#root";
11710
12.4k
    int result;
11711
11712
12.4k
    if (buildTree) {
11713
12.4k
        root = xmlNewDocNode(ctxt->myDoc, NULL, rootName, NULL);
11714
12.4k
        if (root == NULL) {
11715
0
            xmlErrMemory(ctxt);
11716
0
            goto error;
11717
0
        }
11718
12.4k
    }
11719
11720
12.4k
    if (xmlCtxtPushInput(ctxt, input) < 0)
11721
0
        goto error;
11722
11723
12.4k
    nameNsPush(ctxt, rootName, NULL, NULL, 0, 0);
11724
12.4k
    spacePush(ctxt, -1);
11725
11726
12.4k
    if (buildTree)
11727
12.4k
        nodePush(ctxt, root);
11728
11729
12.4k
    if (hasTextDecl) {
11730
0
        xmlDetectEncoding(ctxt);
11731
11732
        /*
11733
         * Parse a possible text declaration first
11734
         */
11735
0
        if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
11736
0
            (IS_BLANK_CH(NXT(5)))) {
11737
0
            xmlParseTextDecl(ctxt);
11738
            /*
11739
             * An XML-1.0 document can't reference an entity not XML-1.0
11740
             */
11741
0
            if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
11742
0
                (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
11743
0
                xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
11744
0
                               "Version mismatch between document and "
11745
0
                               "entity\n");
11746
0
            }
11747
0
        }
11748
0
    }
11749
11750
12.4k
    xmlParseContentInternal(ctxt);
11751
11752
12.4k
    if (ctxt->input->cur < ctxt->input->end)
11753
48
  xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11754
11755
12.4k
    if ((ctxt->wellFormed) ||
11756
12.2k
        ((ctxt->recovery) && (!xmlCtxtIsCatastrophicError(ctxt)))) {
11757
12.2k
        if (root != NULL) {
11758
12.2k
            xmlNodePtr cur;
11759
11760
            /*
11761
             * Unlink newly created node list.
11762
             */
11763
12.2k
            list = root->children;
11764
12.2k
            root->children = NULL;
11765
12.2k
            root->last = NULL;
11766
14.7k
            for (cur = list; cur != NULL; cur = cur->next)
11767
2.46k
                cur->parent = NULL;
11768
12.2k
        }
11769
12.2k
    }
11770
11771
    /*
11772
     * Read the rest of the stream in case of errors. We want
11773
     * to account for the whole entity size.
11774
     */
11775
12.4k
    do {
11776
12.4k
        ctxt->input->cur = ctxt->input->end;
11777
12.4k
        xmlParserShrink(ctxt);
11778
12.4k
        result = xmlParserGrow(ctxt);
11779
12.4k
    } while (result > 0);
11780
11781
12.4k
    if (buildTree)
11782
12.4k
        nodePop(ctxt);
11783
11784
12.4k
    namePop(ctxt);
11785
12.4k
    spacePop(ctxt);
11786
11787
12.4k
    xmlCtxtPopInput(ctxt);
11788
11789
12.4k
error:
11790
12.4k
    xmlFreeNode(root);
11791
11792
12.4k
    return(list);
11793
12.4k
}
11794
11795
static void
11796
12.5k
xmlCtxtParseEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr ent) {
11797
12.5k
    xmlParserInputPtr input;
11798
12.5k
    xmlNodePtr list;
11799
12.5k
    unsigned long consumed;
11800
12.5k
    int isExternal;
11801
12.5k
    int buildTree;
11802
12.5k
    int oldMinNsIndex;
11803
12.5k
    int oldNodelen, oldNodemem;
11804
11805
12.5k
    isExternal = (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY);
11806
12.5k
    buildTree = (ctxt->node != NULL);
11807
11808
    /*
11809
     * Recursion check
11810
     */
11811
12.5k
    if (ent->flags & XML_ENT_EXPANDING) {
11812
11
        xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
11813
11
        goto error;
11814
11
    }
11815
11816
    /*
11817
     * Load entity
11818
     */
11819
12.4k
    input = xmlNewEntityInputStream(ctxt, ent);
11820
12.4k
    if (input == NULL)
11821
0
        goto error;
11822
11823
    /*
11824
     * When building a tree, we need to limit the scope of namespace
11825
     * declarations, so that entities don't reference xmlNs structs
11826
     * from the parent of a reference.
11827
     */
11828
12.4k
    oldMinNsIndex = ctxt->nsdb->minNsIndex;
11829
12.4k
    if (buildTree)
11830
12.4k
        ctxt->nsdb->minNsIndex = ctxt->nsNr;
11831
11832
12.4k
    oldNodelen = ctxt->nodelen;
11833
12.4k
    oldNodemem = ctxt->nodemem;
11834
12.4k
    ctxt->nodelen = 0;
11835
12.4k
    ctxt->nodemem = 0;
11836
11837
    /*
11838
     * Parse content
11839
     *
11840
     * This initiates a recursive call chain:
11841
     *
11842
     * - xmlCtxtParseContentInternal
11843
     * - xmlParseContentInternal
11844
     * - xmlParseReference
11845
     * - xmlCtxtParseEntity
11846
     *
11847
     * The nesting depth is limited by the maximum number of inputs,
11848
     * see xmlCtxtPushInput.
11849
     *
11850
     * It's possible to make this non-recursive (minNsIndex must be
11851
     * stored in the input struct) at the expense of code readability.
11852
     */
11853
11854
12.4k
    ent->flags |= XML_ENT_EXPANDING;
11855
11856
12.4k
    list = xmlCtxtParseContentInternal(ctxt, input, isExternal, buildTree);
11857
11858
12.4k
    ent->flags &= ~XML_ENT_EXPANDING;
11859
11860
12.4k
    ctxt->nsdb->minNsIndex = oldMinNsIndex;
11861
12.4k
    ctxt->nodelen = oldNodelen;
11862
12.4k
    ctxt->nodemem = oldNodemem;
11863
11864
    /*
11865
     * Entity size accounting
11866
     */
11867
12.4k
    consumed = input->consumed;
11868
12.4k
    xmlSaturatedAddSizeT(&consumed, input->end - input->base);
11869
11870
12.4k
    if ((ent->flags & XML_ENT_CHECKED) == 0)
11871
1.17k
        xmlSaturatedAdd(&ent->expandedSize, consumed);
11872
11873
12.4k
    if ((ent->flags & XML_ENT_PARSED) == 0) {
11874
1.17k
        if (isExternal)
11875
0
            xmlSaturatedAdd(&ctxt->sizeentities, consumed);
11876
11877
1.17k
        ent->children = list;
11878
11879
3.64k
        while (list != NULL) {
11880
2.46k
            list->parent = (xmlNodePtr) ent;
11881
11882
            /*
11883
             * Downstream code like the nginx xslt module can set
11884
             * ctxt->myDoc->extSubset to a separate DTD, so the entity
11885
             * might have a different or a NULL document.
11886
             */
11887
2.46k
            if (list->doc != ent->doc)
11888
0
                xmlSetTreeDoc(list, ent->doc);
11889
11890
2.46k
            if (list->next == NULL)
11891
843
                ent->last = list;
11892
2.46k
            list = list->next;
11893
2.46k
        }
11894
11.3k
    } else {
11895
11.3k
        xmlFreeNodeList(list);
11896
11.3k
    }
11897
11898
12.4k
    xmlFreeInputStream(input);
11899
11900
12.5k
error:
11901
12.5k
    ent->flags |= XML_ENT_PARSED | XML_ENT_CHECKED;
11902
12.5k
}
11903
11904
/**
11905
 * Parse an external general entity within an existing parsing context
11906
 * An external general parsed entity is well-formed if it matches the
11907
 * production labeled extParsedEnt.
11908
 *
11909
 *     [78] extParsedEnt ::= TextDecl? content
11910
 *
11911
 * @param ctxt  the existing parsing context
11912
 * @param URL  the URL for the entity to load
11913
 * @param ID  the System ID for the entity to load
11914
 * @param listOut  the return value for the set of parsed nodes
11915
 * @returns 0 if the entity is well formed, -1 in case of args problem and
11916
 *    the parser error code otherwise
11917
 */
11918
11919
int
11920
xmlParseCtxtExternalEntity(xmlParserCtxt *ctxt, const xmlChar *URL,
11921
0
                           const xmlChar *ID, xmlNode **listOut) {
11922
0
    xmlParserInputPtr input;
11923
0
    xmlNodePtr list;
11924
11925
0
    if (listOut != NULL)
11926
0
        *listOut = NULL;
11927
11928
0
    if (ctxt == NULL)
11929
0
        return(XML_ERR_ARGUMENT);
11930
11931
0
    input = xmlLoadResource(ctxt, (char *) URL, (char *) ID,
11932
0
                            XML_RESOURCE_GENERAL_ENTITY);
11933
0
    if (input == NULL)
11934
0
        return(ctxt->errNo);
11935
11936
0
    xmlCtxtInitializeLate(ctxt);
11937
11938
0
    list = xmlCtxtParseContentInternal(ctxt, input, /* hasTextDecl */ 1, 1);
11939
0
    if (listOut != NULL)
11940
0
        *listOut = list;
11941
0
    else
11942
0
        xmlFreeNodeList(list);
11943
11944
0
    xmlFreeInputStream(input);
11945
0
    return(ctxt->errNo);
11946
0
}
11947
11948
#ifdef LIBXML_SAX1_ENABLED
11949
/**
11950
 * Parse an external general entity
11951
 * An external general parsed entity is well-formed if it matches the
11952
 * production labeled extParsedEnt.
11953
 *
11954
 * This function uses deprecated global variables to set parser options
11955
 * which default to XML_PARSE_NODICT.
11956
 *
11957
 * @deprecated Use #xmlParseCtxtExternalEntity.
11958
 *
11959
 *     [78] extParsedEnt ::= TextDecl? content
11960
 *
11961
 * @param doc  the document the chunk pertains to
11962
 * @param sax  the SAX handler block (possibly NULL)
11963
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
11964
 * @param depth  Used for loop detection, use 0
11965
 * @param URL  the URL for the entity to load
11966
 * @param ID  the System ID for the entity to load
11967
 * @param list  the return value for the set of parsed nodes
11968
 * @returns 0 if the entity is well formed, -1 in case of args problem and
11969
 *    the parser error code otherwise
11970
 */
11971
11972
int
11973
xmlParseExternalEntity(xmlDoc *doc, xmlSAXHandler *sax, void *user_data,
11974
0
    int depth, const xmlChar *URL, const xmlChar *ID, xmlNode **list) {
11975
0
    xmlParserCtxtPtr ctxt;
11976
0
    int ret;
11977
11978
0
    if (list != NULL)
11979
0
        *list = NULL;
11980
11981
0
    if (doc == NULL)
11982
0
        return(XML_ERR_ARGUMENT);
11983
11984
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11985
0
    if (ctxt == NULL)
11986
0
        return(XML_ERR_NO_MEMORY);
11987
11988
0
    ctxt->depth = depth;
11989
0
    ctxt->myDoc = doc;
11990
0
    ret = xmlParseCtxtExternalEntity(ctxt, URL, ID, list);
11991
11992
0
    xmlFreeParserCtxt(ctxt);
11993
0
    return(ret);
11994
0
}
11995
11996
/**
11997
 * Parse a well-balanced chunk of an XML document
11998
 * called by the parser
11999
 * The allowed sequence for the Well Balanced Chunk is the one defined by
12000
 * the content production in the XML grammar:
12001
 *
12002
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12003
 *                       Comment)*
12004
 *
12005
 * This function uses deprecated global variables to set parser options
12006
 * which default to XML_PARSE_NODICT.
12007
 *
12008
 * @param doc  the document the chunk pertains to (must not be NULL)
12009
 * @param sax  the SAX handler block (possibly NULL)
12010
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
12011
 * @param depth  Used for loop detection, use 0
12012
 * @param string  the input string in UTF8 or ISO-Latin (zero terminated)
12013
 * @param lst  the return value for the set of parsed nodes
12014
 * @returns 0 if the chunk is well balanced, -1 in case of args problem and
12015
 *    the parser error code otherwise
12016
 */
12017
12018
int
12019
xmlParseBalancedChunkMemory(xmlDoc *doc, xmlSAXHandler *sax,
12020
0
     void *user_data, int depth, const xmlChar *string, xmlNode **lst) {
12021
0
    return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
12022
0
                                                depth, string, lst, 0 );
12023
0
}
12024
#endif /* LIBXML_SAX1_ENABLED */
12025
12026
/**
12027
 * Parse a well-balanced chunk of XML matching the 'content' production.
12028
 *
12029
 * Namespaces in scope of `node` and entities of `node`'s document are
12030
 * recognized. When validating, the DTD of `node`'s document is used.
12031
 *
12032
 * Always consumes `input` even in error case.
12033
 *
12034
 * @since 2.14.0
12035
 *
12036
 * @param ctxt  parser context
12037
 * @param input  parser input
12038
 * @param node  target node or document
12039
 * @param hasTextDecl  whether to parse text declaration
12040
 * @returns a node list or NULL in case of error.
12041
 */
12042
xmlNode *
12043
xmlCtxtParseContent(xmlParserCtxt *ctxt, xmlParserInput *input,
12044
0
                    xmlNode *node, int hasTextDecl) {
12045
0
    xmlDocPtr doc;
12046
0
    xmlNodePtr cur, list = NULL;
12047
0
    int nsnr = 0;
12048
0
    xmlDictPtr oldDict;
12049
0
    int oldOptions, oldDictNames, oldLoadSubset;
12050
12051
0
    if ((ctxt == NULL) || (input == NULL) || (node == NULL)) {
12052
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12053
0
        goto exit;
12054
0
    }
12055
12056
0
    doc = node->doc;
12057
0
    if (doc == NULL) {
12058
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12059
0
        goto exit;
12060
0
    }
12061
12062
0
    switch (node->type) {
12063
0
        case XML_ELEMENT_NODE:
12064
0
        case XML_DOCUMENT_NODE:
12065
0
        case XML_HTML_DOCUMENT_NODE:
12066
0
            break;
12067
12068
0
        case XML_ATTRIBUTE_NODE:
12069
0
        case XML_TEXT_NODE:
12070
0
        case XML_CDATA_SECTION_NODE:
12071
0
        case XML_ENTITY_REF_NODE:
12072
0
        case XML_PI_NODE:
12073
0
        case XML_COMMENT_NODE:
12074
0
            for (cur = node->parent; cur != NULL; cur = cur->parent) {
12075
0
                if ((cur->type == XML_ELEMENT_NODE) ||
12076
0
                    (cur->type == XML_DOCUMENT_NODE) ||
12077
0
                    (cur->type == XML_HTML_DOCUMENT_NODE)) {
12078
0
                    node = cur;
12079
0
                    break;
12080
0
                }
12081
0
            }
12082
0
            break;
12083
12084
0
        default:
12085
0
            xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12086
0
            goto exit;
12087
0
    }
12088
12089
0
    xmlCtxtReset(ctxt);
12090
12091
0
    oldDict = ctxt->dict;
12092
0
    oldOptions = ctxt->options;
12093
0
    oldDictNames = ctxt->dictNames;
12094
0
    oldLoadSubset = ctxt->loadsubset;
12095
12096
    /*
12097
     * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
12098
     */
12099
0
    if (doc->dict != NULL) {
12100
0
        ctxt->dict = doc->dict;
12101
0
    } else {
12102
0
        ctxt->options |= XML_PARSE_NODICT;
12103
0
        ctxt->dictNames = 0;
12104
0
    }
12105
12106
    /*
12107
     * Disable IDs
12108
     */
12109
0
    ctxt->loadsubset |= XML_SKIP_IDS;
12110
0
    ctxt->options |= XML_PARSE_SKIP_IDS;
12111
12112
0
    ctxt->myDoc = doc;
12113
12114
0
#ifdef LIBXML_HTML_ENABLED
12115
0
    if (ctxt->html) {
12116
        /*
12117
         * When parsing in context, it makes no sense to add implied
12118
         * elements like html/body/etc...
12119
         */
12120
0
        ctxt->options |= HTML_PARSE_NOIMPLIED;
12121
12122
0
        list = htmlCtxtParseContentInternal(ctxt, input);
12123
0
    } else
12124
0
#endif
12125
0
    {
12126
0
        xmlCtxtInitializeLate(ctxt);
12127
12128
        /*
12129
         * initialize the SAX2 namespaces stack
12130
         */
12131
0
        cur = node;
12132
0
        while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
12133
0
            xmlNsPtr ns = cur->nsDef;
12134
0
            xmlHashedString hprefix, huri;
12135
12136
0
            while (ns != NULL) {
12137
0
                hprefix = xmlDictLookupHashed(ctxt->dict, ns->prefix, -1);
12138
0
                huri = xmlDictLookupHashed(ctxt->dict, ns->href, -1);
12139
0
                if (xmlParserNsPush(ctxt, &hprefix, &huri, ns, 1) > 0)
12140
0
                    nsnr++;
12141
0
                ns = ns->next;
12142
0
            }
12143
0
            cur = cur->parent;
12144
0
        }
12145
12146
0
        list = xmlCtxtParseContentInternal(ctxt, input, hasTextDecl, 1);
12147
12148
0
        if (nsnr > 0)
12149
0
            xmlParserNsPop(ctxt, nsnr);
12150
0
    }
12151
12152
0
    ctxt->dict = oldDict;
12153
0
    ctxt->options = oldOptions;
12154
0
    ctxt->dictNames = oldDictNames;
12155
0
    ctxt->loadsubset = oldLoadSubset;
12156
0
    ctxt->myDoc = NULL;
12157
0
    ctxt->node = NULL;
12158
12159
0
exit:
12160
0
    xmlFreeInputStream(input);
12161
0
    return(list);
12162
0
}
12163
12164
/**
12165
 * Parse a well-balanced chunk of an XML document
12166
 * within the context (DTD, namespaces, etc ...) of the given node.
12167
 *
12168
 * The allowed sequence for the data is a Well Balanced Chunk defined by
12169
 * the content production in the XML grammar:
12170
 *
12171
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12172
 *                       Comment)*
12173
 *
12174
 * This function assumes the encoding of `node`'s document which is
12175
 * typically not what you want. A better alternative is
12176
 * #xmlCtxtParseContent.
12177
 *
12178
 * @param node  the context node
12179
 * @param data  the input string
12180
 * @param datalen  the input string length in bytes
12181
 * @param options  a combination of xmlParserOption
12182
 * @param listOut  the return value for the set of parsed nodes
12183
 * @returns XML_ERR_OK if the chunk is well balanced, and the parser
12184
 * error code otherwise
12185
 */
12186
xmlParserErrors
12187
xmlParseInNodeContext(xmlNode *node, const char *data, int datalen,
12188
0
                      int options, xmlNode **listOut) {
12189
0
    xmlParserCtxtPtr ctxt;
12190
0
    xmlParserInputPtr input;
12191
0
    xmlDocPtr doc;
12192
0
    xmlNodePtr list;
12193
0
    xmlParserErrors ret;
12194
12195
0
    if (listOut == NULL)
12196
0
        return(XML_ERR_INTERNAL_ERROR);
12197
0
    *listOut = NULL;
12198
12199
0
    if ((node == NULL) || (data == NULL) || (datalen < 0))
12200
0
        return(XML_ERR_INTERNAL_ERROR);
12201
12202
0
    doc = node->doc;
12203
0
    if (doc == NULL)
12204
0
        return(XML_ERR_INTERNAL_ERROR);
12205
12206
0
#ifdef LIBXML_HTML_ENABLED
12207
0
    if (doc->type == XML_HTML_DOCUMENT_NODE) {
12208
0
        ctxt = htmlNewParserCtxt();
12209
0
    }
12210
0
    else
12211
0
#endif
12212
0
        ctxt = xmlNewParserCtxt();
12213
12214
0
    if (ctxt == NULL)
12215
0
        return(XML_ERR_NO_MEMORY);
12216
12217
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, data, datalen,
12218
0
                                      (const char *) doc->encoding,
12219
0
                                      XML_INPUT_BUF_STATIC);
12220
0
    if (input == NULL) {
12221
0
        xmlFreeParserCtxt(ctxt);
12222
0
        return(XML_ERR_NO_MEMORY);
12223
0
    }
12224
12225
0
    xmlCtxtUseOptions(ctxt, options);
12226
12227
0
    list = xmlCtxtParseContent(ctxt, input, node, /* hasTextDecl */ 0);
12228
12229
0
    if (list == NULL) {
12230
0
        ret = ctxt->errNo;
12231
0
        if (ret == XML_ERR_ARGUMENT)
12232
0
            ret = XML_ERR_INTERNAL_ERROR;
12233
0
    } else {
12234
0
        ret = XML_ERR_OK;
12235
0
        *listOut = list;
12236
0
    }
12237
12238
0
    xmlFreeParserCtxt(ctxt);
12239
12240
0
    return(ret);
12241
0
}
12242
12243
#ifdef LIBXML_SAX1_ENABLED
12244
/**
12245
 * Parse a well-balanced chunk of an XML document
12246
 *
12247
 * The allowed sequence for the Well Balanced Chunk is the one defined by
12248
 * the content production in the XML grammar:
12249
 *
12250
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12251
 *                       Comment)*
12252
 *
12253
 * In case recover is set to 1, the nodelist will not be empty even if
12254
 * the parsed chunk is not well balanced, assuming the parsing succeeded to
12255
 * some extent.
12256
 *
12257
 * This function uses deprecated global variables to set parser options
12258
 * which default to XML_PARSE_NODICT.
12259
 *
12260
 * @param doc  the document the chunk pertains to (must not be NULL)
12261
 * @param sax  the SAX handler block (possibly NULL)
12262
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
12263
 * @param depth  Used for loop detection, use 0
12264
 * @param string  the input string in UTF8 or ISO-Latin (zero terminated)
12265
 * @param listOut  the return value for the set of parsed nodes
12266
 * @param recover  return nodes even if the data is broken (use 0)
12267
 * @returns 0 if the chunk is well balanced, or thehe parser error code
12268
 * otherwise.
12269
 */
12270
int
12271
xmlParseBalancedChunkMemoryRecover(xmlDoc *doc, xmlSAXHandler *sax,
12272
     void *user_data, int depth, const xmlChar *string, xmlNode **listOut,
12273
0
     int recover) {
12274
0
    xmlParserCtxtPtr ctxt;
12275
0
    xmlParserInputPtr input;
12276
0
    xmlNodePtr list;
12277
0
    int ret;
12278
12279
0
    if (listOut != NULL)
12280
0
        *listOut = NULL;
12281
12282
0
    if (string == NULL)
12283
0
        return(XML_ERR_ARGUMENT);
12284
12285
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
12286
0
    if (ctxt == NULL)
12287
0
        return(XML_ERR_NO_MEMORY);
12288
12289
0
    xmlCtxtInitializeLate(ctxt);
12290
12291
0
    ctxt->depth = depth;
12292
0
    ctxt->myDoc = doc;
12293
0
    if (recover) {
12294
0
        ctxt->options |= XML_PARSE_RECOVER;
12295
0
        ctxt->recovery = 1;
12296
0
    }
12297
12298
0
    input = xmlNewStringInputStream(ctxt, string);
12299
0
    if (input == NULL) {
12300
0
        ret = ctxt->errNo;
12301
0
        goto error;
12302
0
    }
12303
12304
0
    list = xmlCtxtParseContentInternal(ctxt, input, /* hasTextDecl */ 0, 1);
12305
0
    if (listOut != NULL)
12306
0
        *listOut = list;
12307
0
    else
12308
0
        xmlFreeNodeList(list);
12309
12310
0
    if (!ctxt->wellFormed)
12311
0
        ret = ctxt->errNo;
12312
0
    else
12313
0
        ret = XML_ERR_OK;
12314
12315
0
error:
12316
0
    xmlFreeInputStream(input);
12317
0
    xmlFreeParserCtxt(ctxt);
12318
0
    return(ret);
12319
0
}
12320
12321
/**
12322
 * Parse an XML external entity out of context and build a tree.
12323
 * It use the given SAX function block to handle the parsing callback.
12324
 * If sax is NULL, fallback to the default DOM tree building routines.
12325
 *
12326
 * @deprecated Don't use.
12327
 *
12328
 *     [78] extParsedEnt ::= TextDecl? content
12329
 *
12330
 * This correspond to a "Well Balanced" chunk
12331
 *
12332
 * This function uses deprecated global variables to set parser options
12333
 * which default to XML_PARSE_NODICT.
12334
 *
12335
 * @param sax  the SAX handler block
12336
 * @param filename  the filename
12337
 * @returns the resulting document tree
12338
 */
12339
12340
xmlDoc *
12341
0
xmlSAXParseEntity(xmlSAXHandler *sax, const char *filename) {
12342
0
    xmlDocPtr ret;
12343
0
    xmlParserCtxtPtr ctxt;
12344
12345
0
    ctxt = xmlCreateFileParserCtxt(filename);
12346
0
    if (ctxt == NULL) {
12347
0
  return(NULL);
12348
0
    }
12349
0
    if (sax != NULL) {
12350
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12351
0
            *ctxt->sax = *sax;
12352
0
        } else {
12353
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12354
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12355
0
        }
12356
0
        ctxt->userData = NULL;
12357
0
    }
12358
12359
0
    xmlParseExtParsedEnt(ctxt);
12360
12361
0
    if (ctxt->wellFormed) {
12362
0
  ret = ctxt->myDoc;
12363
0
    } else {
12364
0
        ret = NULL;
12365
0
        xmlFreeDoc(ctxt->myDoc);
12366
0
    }
12367
12368
0
    xmlFreeParserCtxt(ctxt);
12369
12370
0
    return(ret);
12371
0
}
12372
12373
/**
12374
 * Parse an XML external entity out of context and build a tree.
12375
 *
12376
 *     [78] extParsedEnt ::= TextDecl? content
12377
 *
12378
 * This correspond to a "Well Balanced" chunk
12379
 *
12380
 * This function uses deprecated global variables to set parser options
12381
 * which default to XML_PARSE_NODICT.
12382
 *
12383
 * @deprecated Don't use.
12384
 *
12385
 * @param filename  the filename
12386
 * @returns the resulting document tree
12387
 */
12388
12389
xmlDoc *
12390
0
xmlParseEntity(const char *filename) {
12391
0
    return(xmlSAXParseEntity(NULL, filename));
12392
0
}
12393
#endif /* LIBXML_SAX1_ENABLED */
12394
12395
/**
12396
 * Create a parser context for an external entity
12397
 * Automatic support for ZLIB/Compress compressed document is provided
12398
 * by default if found at compile-time.
12399
 *
12400
 * @deprecated Don't use.
12401
 *
12402
 * @param URL  the entity URL
12403
 * @param ID  the entity PUBLIC ID
12404
 * @param base  a possible base for the target URI
12405
 * @returns the new parser context or NULL
12406
 */
12407
xmlParserCtxt *
12408
xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
12409
0
                    const xmlChar *base) {
12410
0
    xmlParserCtxtPtr ctxt;
12411
0
    xmlParserInputPtr input;
12412
0
    xmlChar *uri = NULL;
12413
12414
0
    ctxt = xmlNewParserCtxt();
12415
0
    if (ctxt == NULL)
12416
0
  return(NULL);
12417
12418
0
    if (base != NULL) {
12419
0
        if (xmlBuildURISafe(URL, base, &uri) < 0)
12420
0
            goto error;
12421
0
        if (uri != NULL)
12422
0
            URL = uri;
12423
0
    }
12424
12425
0
    input = xmlLoadResource(ctxt, (char *) URL, (char *) ID,
12426
0
                            XML_RESOURCE_UNKNOWN);
12427
0
    if (input == NULL)
12428
0
        goto error;
12429
12430
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12431
0
        xmlFreeInputStream(input);
12432
0
        goto error;
12433
0
    }
12434
12435
0
    xmlFree(uri);
12436
0
    return(ctxt);
12437
12438
0
error:
12439
0
    xmlFree(uri);
12440
0
    xmlFreeParserCtxt(ctxt);
12441
0
    return(NULL);
12442
0
}
12443
12444
/************************************************************************
12445
 *                  *
12446
 *    Front ends when parsing from a file     *
12447
 *                  *
12448
 ************************************************************************/
12449
12450
/**
12451
 * Create a parser context for a file or URL content.
12452
 * Automatic support for ZLIB/Compress compressed document is provided
12453
 * by default if found at compile-time and for file accesses
12454
 *
12455
 * @deprecated Use #xmlNewParserCtxt and #xmlCtxtReadFile.
12456
 *
12457
 * @param filename  the filename or URL
12458
 * @param options  a combination of xmlParserOption
12459
 * @returns the new parser context or NULL
12460
 */
12461
xmlParserCtxt *
12462
xmlCreateURLParserCtxt(const char *filename, int options)
12463
0
{
12464
0
    xmlParserCtxtPtr ctxt;
12465
0
    xmlParserInputPtr input;
12466
12467
0
    ctxt = xmlNewParserCtxt();
12468
0
    if (ctxt == NULL)
12469
0
  return(NULL);
12470
12471
0
    xmlCtxtUseOptions(ctxt, options);
12472
12473
0
    input = xmlLoadResource(ctxt, filename, NULL, XML_RESOURCE_MAIN_DOCUMENT);
12474
0
    if (input == NULL) {
12475
0
  xmlFreeParserCtxt(ctxt);
12476
0
  return(NULL);
12477
0
    }
12478
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12479
0
        xmlFreeInputStream(input);
12480
0
        xmlFreeParserCtxt(ctxt);
12481
0
        return(NULL);
12482
0
    }
12483
12484
0
    return(ctxt);
12485
0
}
12486
12487
/**
12488
 * Create a parser context for a file content.
12489
 * Automatic support for ZLIB/Compress compressed document is provided
12490
 * by default if found at compile-time.
12491
 *
12492
 * @deprecated Use #xmlNewParserCtxt and #xmlCtxtReadFile.
12493
 *
12494
 * @param filename  the filename
12495
 * @returns the new parser context or NULL
12496
 */
12497
xmlParserCtxt *
12498
xmlCreateFileParserCtxt(const char *filename)
12499
0
{
12500
0
    return(xmlCreateURLParserCtxt(filename, 0));
12501
0
}
12502
12503
#ifdef LIBXML_SAX1_ENABLED
12504
/**
12505
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12506
 * compressed document is provided by default if found at compile-time.
12507
 * It use the given SAX function block to handle the parsing callback.
12508
 * If sax is NULL, fallback to the default DOM tree building routines.
12509
 *
12510
 * This function uses deprecated global variables to set parser options
12511
 * which default to XML_PARSE_NODICT.
12512
 *
12513
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12514
 *
12515
 * User data (void *) is stored within the parser context in the
12516
 * context's _private member, so it is available nearly everywhere in libxml
12517
 *
12518
 * @param sax  the SAX handler block
12519
 * @param filename  the filename
12520
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12521
 *             documents
12522
 * @param data  the userdata
12523
 * @returns the resulting document tree
12524
 */
12525
12526
xmlDoc *
12527
xmlSAXParseFileWithData(xmlSAXHandler *sax, const char *filename,
12528
0
                        int recovery, void *data) {
12529
0
    xmlDocPtr ret = NULL;
12530
0
    xmlParserCtxtPtr ctxt;
12531
0
    xmlParserInputPtr input;
12532
12533
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
12534
0
    if (ctxt == NULL)
12535
0
  return(NULL);
12536
12537
0
    if (data != NULL)
12538
0
  ctxt->_private = data;
12539
12540
0
    if (recovery) {
12541
0
        ctxt->options |= XML_PARSE_RECOVER;
12542
0
        ctxt->recovery = 1;
12543
0
    }
12544
12545
0
    if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0))
12546
0
        input = xmlCtxtNewInputFromFd(ctxt, filename, STDIN_FILENO, NULL, 0);
12547
0
    else
12548
0
        input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, NULL, 0);
12549
12550
0
    if (input != NULL)
12551
0
        ret = xmlCtxtParseDocument(ctxt, input);
12552
12553
0
    xmlFreeParserCtxt(ctxt);
12554
0
    return(ret);
12555
0
}
12556
12557
/**
12558
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12559
 * compressed document is provided by default if found at compile-time.
12560
 * It use the given SAX function block to handle the parsing callback.
12561
 * If sax is NULL, fallback to the default DOM tree building routines.
12562
 *
12563
 * This function uses deprecated global variables to set parser options
12564
 * which default to XML_PARSE_NODICT.
12565
 *
12566
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12567
 *
12568
 * @param sax  the SAX handler block
12569
 * @param filename  the filename
12570
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12571
 *             documents
12572
 * @returns the resulting document tree
12573
 */
12574
12575
xmlDoc *
12576
xmlSAXParseFile(xmlSAXHandler *sax, const char *filename,
12577
0
                          int recovery) {
12578
0
    return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
12579
0
}
12580
12581
/**
12582
 * Parse an XML in-memory document and build a tree.
12583
 * In the case the document is not Well Formed, a attempt to build a
12584
 * tree is tried anyway
12585
 *
12586
 * This function uses deprecated global variables to set parser options
12587
 * which default to XML_PARSE_NODICT | XML_PARSE_RECOVER.
12588
 *
12589
 * @deprecated Use #xmlReadDoc with XML_PARSE_RECOVER.
12590
 *
12591
 * @param cur  a pointer to an array of xmlChar
12592
 * @returns the resulting document tree or NULL in case of failure
12593
 */
12594
12595
xmlDoc *
12596
0
xmlRecoverDoc(const xmlChar *cur) {
12597
0
    return(xmlSAXParseDoc(NULL, cur, 1));
12598
0
}
12599
12600
/**
12601
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12602
 * compressed document is provided by default if found at compile-time.
12603
 *
12604
 * This function uses deprecated global variables to set parser options
12605
 * which default to XML_PARSE_NODICT.
12606
 *
12607
 * @deprecated Use #xmlReadFile.
12608
 *
12609
 * @param filename  the filename
12610
 * @returns the resulting document tree if the file was wellformed,
12611
 * NULL otherwise.
12612
 */
12613
12614
xmlDoc *
12615
0
xmlParseFile(const char *filename) {
12616
0
    return(xmlSAXParseFile(NULL, filename, 0));
12617
0
}
12618
12619
/**
12620
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12621
 * compressed document is provided by default if found at compile-time.
12622
 * In the case the document is not Well Formed, it attempts to build
12623
 * a tree anyway
12624
 *
12625
 * This function uses deprecated global variables to set parser options
12626
 * which default to XML_PARSE_NODICT | XML_PARSE_RECOVER.
12627
 *
12628
 * @deprecated Use #xmlReadFile with XML_PARSE_RECOVER.
12629
 *
12630
 * @param filename  the filename
12631
 * @returns the resulting document tree or NULL in case of failure
12632
 */
12633
12634
xmlDoc *
12635
0
xmlRecoverFile(const char *filename) {
12636
0
    return(xmlSAXParseFile(NULL, filename, 1));
12637
0
}
12638
12639
12640
/**
12641
 * Setup the parser context to parse a new buffer; Clears any prior
12642
 * contents from the parser context. The buffer parameter must not be
12643
 * NULL, but the filename parameter can be
12644
 *
12645
 * @deprecated Don't use.
12646
 *
12647
 * @param ctxt  an XML parser context
12648
 * @param buffer  a xmlChar * buffer
12649
 * @param filename  a file name
12650
 */
12651
void
12652
xmlSetupParserForBuffer(xmlParserCtxt *ctxt, const xmlChar* buffer,
12653
                             const char* filename)
12654
0
{
12655
0
    xmlParserInputPtr input;
12656
12657
0
    if ((ctxt == NULL) || (buffer == NULL))
12658
0
        return;
12659
12660
0
    xmlCtxtReset(ctxt);
12661
12662
0
    input = xmlCtxtNewInputFromString(ctxt, filename, (const char *) buffer,
12663
0
                                      NULL, 0);
12664
0
    if (input == NULL)
12665
0
        return;
12666
0
    if (xmlCtxtPushInput(ctxt, input) < 0)
12667
0
        xmlFreeInputStream(input);
12668
0
}
12669
12670
/**
12671
 * Parse an XML file and call the given SAX handler routines.
12672
 * Automatic support for ZLIB/Compress compressed document is provided
12673
 *
12674
 * This function uses deprecated global variables to set parser options
12675
 * which default to XML_PARSE_NODICT.
12676
 *
12677
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12678
 *
12679
 * @param sax  a SAX handler
12680
 * @param user_data  The user data returned on SAX callbacks
12681
 * @param filename  a file name
12682
 * @returns 0 in case of success or a error number otherwise
12683
 */
12684
int
12685
xmlSAXUserParseFile(xmlSAXHandler *sax, void *user_data,
12686
0
                    const char *filename) {
12687
0
    int ret = 0;
12688
0
    xmlParserCtxtPtr ctxt;
12689
12690
0
    ctxt = xmlCreateFileParserCtxt(filename);
12691
0
    if (ctxt == NULL) return -1;
12692
0
    if (sax != NULL) {
12693
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12694
0
            *ctxt->sax = *sax;
12695
0
        } else {
12696
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12697
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12698
0
        }
12699
0
  ctxt->userData = user_data;
12700
0
    }
12701
12702
0
    xmlParseDocument(ctxt);
12703
12704
0
    if (ctxt->wellFormed)
12705
0
  ret = 0;
12706
0
    else {
12707
0
        if (ctxt->errNo != 0)
12708
0
      ret = ctxt->errNo;
12709
0
  else
12710
0
      ret = -1;
12711
0
    }
12712
0
    if (ctxt->myDoc != NULL) {
12713
0
        xmlFreeDoc(ctxt->myDoc);
12714
0
  ctxt->myDoc = NULL;
12715
0
    }
12716
0
    xmlFreeParserCtxt(ctxt);
12717
12718
0
    return ret;
12719
0
}
12720
#endif /* LIBXML_SAX1_ENABLED */
12721
12722
/************************************************************************
12723
 *                  *
12724
 *    Front ends when parsing from memory     *
12725
 *                  *
12726
 ************************************************************************/
12727
12728
/**
12729
 * Create a parser context for an XML in-memory document. The input buffer
12730
 * must not contain a terminating null byte.
12731
 *
12732
 * @param buffer  a pointer to a char array
12733
 * @param size  the size of the array
12734
 * @returns the new parser context or NULL
12735
 */
12736
xmlParserCtxt *
12737
20.6k
xmlCreateMemoryParserCtxt(const char *buffer, int size) {
12738
20.6k
    xmlParserCtxtPtr ctxt;
12739
20.6k
    xmlParserInputPtr input;
12740
12741
20.6k
    if (size < 0)
12742
0
  return(NULL);
12743
12744
20.6k
    ctxt = xmlNewParserCtxt();
12745
20.6k
    if (ctxt == NULL)
12746
0
  return(NULL);
12747
12748
20.6k
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, buffer, size, NULL, 0);
12749
20.6k
    if (input == NULL) {
12750
0
  xmlFreeParserCtxt(ctxt);
12751
0
  return(NULL);
12752
0
    }
12753
20.6k
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12754
0
        xmlFreeInputStream(input);
12755
0
        xmlFreeParserCtxt(ctxt);
12756
0
        return(NULL);
12757
0
    }
12758
12759
20.6k
    return(ctxt);
12760
20.6k
}
12761
12762
#ifdef LIBXML_SAX1_ENABLED
12763
/**
12764
 * Parse an XML in-memory block and use the given SAX function block
12765
 * to handle the parsing callback. If sax is NULL, fallback to the default
12766
 * DOM tree building routines.
12767
 *
12768
 * This function uses deprecated global variables to set parser options
12769
 * which default to XML_PARSE_NODICT.
12770
 *
12771
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12772
 *
12773
 * User data (void *) is stored within the parser context in the
12774
 * context's _private member, so it is available nearly everywhere in libxml
12775
 *
12776
 * @param sax  the SAX handler block
12777
 * @param buffer  an pointer to a char array
12778
 * @param size  the size of the array
12779
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12780
 *             documents
12781
 * @param data  the userdata
12782
 * @returns the resulting document tree
12783
 */
12784
12785
xmlDoc *
12786
xmlSAXParseMemoryWithData(xmlSAXHandler *sax, const char *buffer,
12787
0
                          int size, int recovery, void *data) {
12788
0
    xmlDocPtr ret = NULL;
12789
0
    xmlParserCtxtPtr ctxt;
12790
0
    xmlParserInputPtr input;
12791
12792
0
    if (size < 0)
12793
0
        return(NULL);
12794
12795
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
12796
0
    if (ctxt == NULL)
12797
0
        return(NULL);
12798
12799
0
    if (data != NULL)
12800
0
  ctxt->_private=data;
12801
12802
0
    if (recovery) {
12803
0
        ctxt->options |= XML_PARSE_RECOVER;
12804
0
        ctxt->recovery = 1;
12805
0
    }
12806
12807
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, buffer, size, NULL,
12808
0
                                      XML_INPUT_BUF_STATIC);
12809
12810
0
    if (input != NULL)
12811
0
        ret = xmlCtxtParseDocument(ctxt, input);
12812
12813
0
    xmlFreeParserCtxt(ctxt);
12814
0
    return(ret);
12815
0
}
12816
12817
/**
12818
 * Parse an XML in-memory block and use the given SAX function block
12819
 * to handle the parsing callback. If sax is NULL, fallback to the default
12820
 * DOM tree building routines.
12821
 *
12822
 * This function uses deprecated global variables to set parser options
12823
 * which default to XML_PARSE_NODICT.
12824
 *
12825
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12826
 *
12827
 * @param sax  the SAX handler block
12828
 * @param buffer  an pointer to a char array
12829
 * @param size  the size of the array
12830
 * @param recovery  work in recovery mode, i.e. tries to read not Well Formed
12831
 *             documents
12832
 * @returns the resulting document tree
12833
 */
12834
xmlDoc *
12835
xmlSAXParseMemory(xmlSAXHandler *sax, const char *buffer,
12836
0
            int size, int recovery) {
12837
0
    return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
12838
0
}
12839
12840
/**
12841
 * Parse an XML in-memory block and build a tree.
12842
 *
12843
 * This function uses deprecated global variables to set parser options
12844
 * which default to XML_PARSE_NODICT.
12845
 *
12846
 * @deprecated Use #xmlReadMemory.
12847
 *
12848
 * @param buffer  an pointer to a char array
12849
 * @param size  the size of the array
12850
 * @returns the resulting document tree
12851
 */
12852
12853
0
xmlDoc *xmlParseMemory(const char *buffer, int size) {
12854
0
   return(xmlSAXParseMemory(NULL, buffer, size, 0));
12855
0
}
12856
12857
/**
12858
 * Parse an XML in-memory block and build a tree.
12859
 * In the case the document is not Well Formed, an attempt to
12860
 * build a tree is tried anyway
12861
 *
12862
 * This function uses deprecated global variables to set parser options
12863
 * which default to XML_PARSE_NODICT | XML_PARSE_RECOVER.
12864
 *
12865
 * @deprecated Use #xmlReadMemory with XML_PARSE_RECOVER.
12866
 *
12867
 * @param buffer  an pointer to a char array
12868
 * @param size  the size of the array
12869
 * @returns the resulting document tree or NULL in case of error
12870
 */
12871
12872
0
xmlDoc *xmlRecoverMemory(const char *buffer, int size) {
12873
0
   return(xmlSAXParseMemory(NULL, buffer, size, 1));
12874
0
}
12875
12876
/**
12877
 * Parse an XML in-memory buffer and call the given SAX handler routines.
12878
 *
12879
 * This function uses deprecated global variables to set parser options
12880
 * which default to XML_PARSE_NODICT.
12881
 *
12882
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12883
 *
12884
 * @param sax  a SAX handler
12885
 * @param user_data  The user data returned on SAX callbacks
12886
 * @param buffer  an in-memory XML document input
12887
 * @param size  the length of the XML document in bytes
12888
 * @returns 0 in case of success or a error number otherwise
12889
 */
12890
int xmlSAXUserParseMemory(xmlSAXHandler *sax, void *user_data,
12891
0
        const char *buffer, int size) {
12892
0
    int ret = 0;
12893
0
    xmlParserCtxtPtr ctxt;
12894
12895
0
    ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12896
0
    if (ctxt == NULL) return -1;
12897
0
    if (sax != NULL) {
12898
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12899
0
            *ctxt->sax = *sax;
12900
0
        } else {
12901
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12902
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12903
0
        }
12904
0
  ctxt->userData = user_data;
12905
0
    }
12906
12907
0
    xmlParseDocument(ctxt);
12908
12909
0
    if (ctxt->wellFormed)
12910
0
  ret = 0;
12911
0
    else {
12912
0
        if (ctxt->errNo != 0)
12913
0
      ret = ctxt->errNo;
12914
0
  else
12915
0
      ret = -1;
12916
0
    }
12917
0
    if (ctxt->myDoc != NULL) {
12918
0
        xmlFreeDoc(ctxt->myDoc);
12919
0
  ctxt->myDoc = NULL;
12920
0
    }
12921
0
    xmlFreeParserCtxt(ctxt);
12922
12923
0
    return ret;
12924
0
}
12925
#endif /* LIBXML_SAX1_ENABLED */
12926
12927
/**
12928
 * Creates a parser context for an XML in-memory document.
12929
 *
12930
 * @param str  a pointer to an array of xmlChar
12931
 * @returns the new parser context or NULL
12932
 */
12933
xmlParserCtxt *
12934
0
xmlCreateDocParserCtxt(const xmlChar *str) {
12935
0
    xmlParserCtxtPtr ctxt;
12936
0
    xmlParserInputPtr input;
12937
12938
0
    ctxt = xmlNewParserCtxt();
12939
0
    if (ctxt == NULL)
12940
0
  return(NULL);
12941
12942
0
    input = xmlCtxtNewInputFromString(ctxt, NULL, (const char *) str, NULL, 0);
12943
0
    if (input == NULL) {
12944
0
  xmlFreeParserCtxt(ctxt);
12945
0
  return(NULL);
12946
0
    }
12947
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12948
0
        xmlFreeInputStream(input);
12949
0
        xmlFreeParserCtxt(ctxt);
12950
0
        return(NULL);
12951
0
    }
12952
12953
0
    return(ctxt);
12954
0
}
12955
12956
#ifdef LIBXML_SAX1_ENABLED
12957
/**
12958
 * Parse an XML in-memory document and build a tree.
12959
 * It use the given SAX function block to handle the parsing callback.
12960
 * If sax is NULL, fallback to the default DOM tree building routines.
12961
 *
12962
 * This function uses deprecated global variables to set parser options
12963
 * which default to XML_PARSE_NODICT.
12964
 *
12965
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadDoc.
12966
 *
12967
 * @param sax  the SAX handler block
12968
 * @param cur  a pointer to an array of xmlChar
12969
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12970
 *             documents
12971
 * @returns the resulting document tree
12972
 */
12973
12974
xmlDoc *
12975
0
xmlSAXParseDoc(xmlSAXHandler *sax, const xmlChar *cur, int recovery) {
12976
0
    xmlDocPtr ret;
12977
0
    xmlParserCtxtPtr ctxt;
12978
0
    xmlSAXHandlerPtr oldsax = NULL;
12979
12980
0
    if (cur == NULL) return(NULL);
12981
12982
12983
0
    ctxt = xmlCreateDocParserCtxt(cur);
12984
0
    if (ctxt == NULL) return(NULL);
12985
0
    if (sax != NULL) {
12986
0
        oldsax = ctxt->sax;
12987
0
        ctxt->sax = sax;
12988
0
        ctxt->userData = NULL;
12989
0
    }
12990
12991
0
    xmlParseDocument(ctxt);
12992
0
    if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
12993
0
    else {
12994
0
       ret = NULL;
12995
0
       xmlFreeDoc(ctxt->myDoc);
12996
0
       ctxt->myDoc = NULL;
12997
0
    }
12998
0
    if (sax != NULL)
12999
0
  ctxt->sax = oldsax;
13000
0
    xmlFreeParserCtxt(ctxt);
13001
13002
0
    return(ret);
13003
0
}
13004
13005
/**
13006
 * Parse an XML in-memory document and build a tree.
13007
 *
13008
 * This function uses deprecated global variables to set parser options
13009
 * which default to XML_PARSE_NODICT.
13010
 *
13011
 * @deprecated Use #xmlReadDoc.
13012
 *
13013
 * @param cur  a pointer to an array of xmlChar
13014
 * @returns the resulting document tree
13015
 */
13016
13017
xmlDoc *
13018
0
xmlParseDoc(const xmlChar *cur) {
13019
0
    return(xmlSAXParseDoc(NULL, cur, 0));
13020
0
}
13021
#endif /* LIBXML_SAX1_ENABLED */
13022
13023
/************************************************************************
13024
 *                  *
13025
 *  New set (2.6.0) of simpler and more flexible APIs   *
13026
 *                  *
13027
 ************************************************************************/
13028
13029
/**
13030
 * Reset a parser context
13031
 *
13032
 * @param ctxt  an XML parser context
13033
 */
13034
void
13035
xmlCtxtReset(xmlParserCtxt *ctxt)
13036
0
{
13037
0
    xmlParserInputPtr input;
13038
13039
0
    if (ctxt == NULL)
13040
0
        return;
13041
13042
0
    while ((input = xmlCtxtPopInput(ctxt)) != NULL) { /* Non consuming */
13043
0
        xmlFreeInputStream(input);
13044
0
    }
13045
0
    ctxt->inputNr = 0;
13046
0
    ctxt->input = NULL;
13047
13048
0
    ctxt->spaceNr = 0;
13049
0
    if (ctxt->spaceTab != NULL) {
13050
0
  ctxt->spaceTab[0] = -1;
13051
0
  ctxt->space = &ctxt->spaceTab[0];
13052
0
    } else {
13053
0
        ctxt->space = NULL;
13054
0
    }
13055
13056
13057
0
    ctxt->nodeNr = 0;
13058
0
    ctxt->node = NULL;
13059
13060
0
    ctxt->nameNr = 0;
13061
0
    ctxt->name = NULL;
13062
13063
0
    ctxt->nsNr = 0;
13064
0
    xmlParserNsReset(ctxt->nsdb);
13065
13066
0
    if (ctxt->version != NULL) {
13067
0
        xmlFree(ctxt->version);
13068
0
        ctxt->version = NULL;
13069
0
    }
13070
0
    if (ctxt->encoding != NULL) {
13071
0
        xmlFree(ctxt->encoding);
13072
0
        ctxt->encoding = NULL;
13073
0
    }
13074
0
    if (ctxt->extSubURI != NULL) {
13075
0
        xmlFree(ctxt->extSubURI);
13076
0
        ctxt->extSubURI = NULL;
13077
0
    }
13078
0
    if (ctxt->extSubSystem != NULL) {
13079
0
        xmlFree(ctxt->extSubSystem);
13080
0
        ctxt->extSubSystem = NULL;
13081
0
    }
13082
0
    if (ctxt->directory != NULL) {
13083
0
        xmlFree(ctxt->directory);
13084
0
        ctxt->directory = NULL;
13085
0
    }
13086
13087
0
    if (ctxt->myDoc != NULL)
13088
0
        xmlFreeDoc(ctxt->myDoc);
13089
0
    ctxt->myDoc = NULL;
13090
13091
0
    ctxt->standalone = -1;
13092
0
    ctxt->hasExternalSubset = 0;
13093
0
    ctxt->hasPErefs = 0;
13094
0
    ctxt->html = ctxt->html ? 1 : 0;
13095
0
    ctxt->instate = XML_PARSER_START;
13096
13097
0
    ctxt->wellFormed = 1;
13098
0
    ctxt->nsWellFormed = 1;
13099
0
    ctxt->disableSAX = 0;
13100
0
    ctxt->valid = 1;
13101
0
    ctxt->record_info = 0;
13102
0
    ctxt->checkIndex = 0;
13103
0
    ctxt->endCheckState = 0;
13104
0
    ctxt->inSubset = 0;
13105
0
    ctxt->errNo = XML_ERR_OK;
13106
0
    ctxt->depth = 0;
13107
0
    ctxt->catalogs = NULL;
13108
0
    ctxt->sizeentities = 0;
13109
0
    ctxt->sizeentcopy = 0;
13110
0
    xmlInitNodeInfoSeq(&ctxt->node_seq);
13111
13112
0
    if (ctxt->attsDefault != NULL) {
13113
0
        xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
13114
0
        ctxt->attsDefault = NULL;
13115
0
    }
13116
0
    if (ctxt->attsSpecial != NULL) {
13117
0
        xmlHashFree(ctxt->attsSpecial, NULL);
13118
0
        ctxt->attsSpecial = NULL;
13119
0
    }
13120
13121
0
#ifdef LIBXML_CATALOG_ENABLED
13122
0
    if (ctxt->catalogs != NULL)
13123
0
  xmlCatalogFreeLocal(ctxt->catalogs);
13124
0
#endif
13125
0
    ctxt->nbErrors = 0;
13126
0
    ctxt->nbWarnings = 0;
13127
0
    if (ctxt->lastError.code != XML_ERR_OK)
13128
0
        xmlResetError(&ctxt->lastError);
13129
0
}
13130
13131
/**
13132
 * Reset a push parser context
13133
 *
13134
 * @param ctxt  an XML parser context
13135
 * @param chunk  a pointer to an array of chars
13136
 * @param size  number of chars in the array
13137
 * @param filename  an optional file name or URI
13138
 * @param encoding  the document encoding, or NULL
13139
 * @returns 0 in case of success and 1 in case of error
13140
 */
13141
int
13142
xmlCtxtResetPush(xmlParserCtxt *ctxt, const char *chunk,
13143
                 int size, const char *filename, const char *encoding)
13144
0
{
13145
0
    xmlParserInputPtr input;
13146
13147
0
    if (ctxt == NULL)
13148
0
        return(1);
13149
13150
0
    xmlCtxtReset(ctxt);
13151
13152
0
    input = xmlNewPushInput(filename, chunk, size);
13153
0
    if (input == NULL)
13154
0
        return(1);
13155
13156
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
13157
0
        xmlFreeInputStream(input);
13158
0
        return(1);
13159
0
    }
13160
13161
0
    if (encoding != NULL)
13162
0
        xmlSwitchEncodingName(ctxt, encoding);
13163
13164
0
    return(0);
13165
0
}
13166
13167
static int
13168
xmlCtxtSetOptionsInternal(xmlParserCtxtPtr ctxt, int options, int keepMask)
13169
78.9k
{
13170
78.9k
    int allMask;
13171
13172
78.9k
    if (ctxt == NULL)
13173
0
        return(-1);
13174
13175
    /*
13176
     * XInclude options aren't handled by the parser.
13177
     *
13178
     * XML_PARSE_XINCLUDE
13179
     * XML_PARSE_NOXINCNODE
13180
     * XML_PARSE_NOBASEFIX
13181
     */
13182
78.9k
    allMask = XML_PARSE_RECOVER |
13183
78.9k
              XML_PARSE_NOENT |
13184
78.9k
              XML_PARSE_DTDLOAD |
13185
78.9k
              XML_PARSE_DTDATTR |
13186
78.9k
              XML_PARSE_DTDVALID |
13187
78.9k
              XML_PARSE_NOERROR |
13188
78.9k
              XML_PARSE_NOWARNING |
13189
78.9k
              XML_PARSE_PEDANTIC |
13190
78.9k
              XML_PARSE_NOBLANKS |
13191
78.9k
#ifdef LIBXML_SAX1_ENABLED
13192
78.9k
              XML_PARSE_SAX1 |
13193
78.9k
#endif
13194
78.9k
              XML_PARSE_NONET |
13195
78.9k
              XML_PARSE_NODICT |
13196
78.9k
              XML_PARSE_NSCLEAN |
13197
78.9k
              XML_PARSE_NOCDATA |
13198
78.9k
              XML_PARSE_COMPACT |
13199
78.9k
              XML_PARSE_OLD10 |
13200
78.9k
              XML_PARSE_HUGE |
13201
78.9k
              XML_PARSE_OLDSAX |
13202
78.9k
              XML_PARSE_IGNORE_ENC |
13203
78.9k
              XML_PARSE_BIG_LINES |
13204
78.9k
              XML_PARSE_NO_XXE |
13205
78.9k
              XML_PARSE_UNZIP |
13206
78.9k
              XML_PARSE_NO_SYS_CATALOG |
13207
78.9k
              XML_PARSE_CATALOG_PI;
13208
13209
78.9k
    ctxt->options = (ctxt->options & keepMask) | (options & allMask);
13210
13211
    /*
13212
     * For some options, struct members are historically the source
13213
     * of truth. The values are initalized from global variables and
13214
     * old code could also modify them directly. Several older API
13215
     * functions that don't take an options argument rely on these
13216
     * deprecated mechanisms.
13217
     *
13218
     * Once public access to struct members and the globals are
13219
     * disabled, we can use the options bitmask as source of
13220
     * truth, making all these struct members obsolete.
13221
     *
13222
     * The XML_DETECT_IDS flags is misnamed. It simply enables
13223
     * loading of the external subset.
13224
     */
13225
78.9k
    ctxt->recovery = (options & XML_PARSE_RECOVER) ? 1 : 0;
13226
78.9k
    ctxt->replaceEntities = (options & XML_PARSE_NOENT) ? 1 : 0;
13227
78.9k
    ctxt->loadsubset = (options & XML_PARSE_DTDLOAD) ? XML_DETECT_IDS : 0;
13228
78.9k
    ctxt->loadsubset |= (options & XML_PARSE_DTDATTR) ? XML_COMPLETE_ATTRS : 0;
13229
78.9k
    ctxt->loadsubset |= (options & XML_PARSE_SKIP_IDS) ? XML_SKIP_IDS : 0;
13230
78.9k
    ctxt->validate = (options & XML_PARSE_DTDVALID) ? 1 : 0;
13231
78.9k
    ctxt->pedantic = (options & XML_PARSE_PEDANTIC) ? 1 : 0;
13232
78.9k
    ctxt->keepBlanks = (options & XML_PARSE_NOBLANKS) ? 0 : 1;
13233
78.9k
    ctxt->dictNames = (options & XML_PARSE_NODICT) ? 0 : 1;
13234
13235
78.9k
    return(options & ~allMask);
13236
78.9k
}
13237
13238
/**
13239
 * Applies the options to the parser context. Unset options are
13240
 * cleared.
13241
 *
13242
 * @since 2.13.0
13243
 *
13244
 * With older versions, you can use #xmlCtxtUseOptions.
13245
 *
13246
 * @param ctxt  an XML parser context
13247
 * @param options  a bitmask of xmlParserOption values
13248
 * @returns 0 in case of success, the set of unknown or unimplemented options
13249
 *         in case of error.
13250
 */
13251
int
13252
xmlCtxtSetOptions(xmlParserCtxt *ctxt, int options)
13253
20.6k
{
13254
20.6k
#ifdef LIBXML_HTML_ENABLED
13255
20.6k
    if ((ctxt != NULL) && (ctxt->html))
13256
0
        return(htmlCtxtSetOptions(ctxt, options));
13257
20.6k
#endif
13258
13259
20.6k
    return(xmlCtxtSetOptionsInternal(ctxt, options, 0));
13260
20.6k
}
13261
13262
/**
13263
 * Get the current options of the parser context.
13264
 *
13265
 * @since 2.14.0
13266
 *
13267
 * @param ctxt  an XML parser context
13268
 * @returns the current options set in the parser context, or -1 if ctxt is NULL.
13269
 */
13270
int
13271
xmlCtxtGetOptions(xmlParserCtxt *ctxt)
13272
0
{
13273
0
    if (ctxt == NULL)
13274
0
        return(-1);
13275
13276
0
    return(ctxt->options);
13277
0
}
13278
13279
/**
13280
 * Applies the options to the parser context. The following options
13281
 * are never cleared and can only be enabled:
13282
 *
13283
 * - XML_PARSE_NOERROR
13284
 * - XML_PARSE_NOWARNING
13285
 * - XML_PARSE_NONET
13286
 * - XML_PARSE_NSCLEAN
13287
 * - XML_PARSE_NOCDATA
13288
 * - XML_PARSE_COMPACT
13289
 * - XML_PARSE_OLD10
13290
 * - XML_PARSE_HUGE
13291
 * - XML_PARSE_OLDSAX
13292
 * - XML_PARSE_IGNORE_ENC
13293
 * - XML_PARSE_BIG_LINES
13294
 *
13295
 * @deprecated Use #xmlCtxtSetOptions.
13296
 *
13297
 * @param ctxt  an XML parser context
13298
 * @param options  a combination of xmlParserOption
13299
 * @returns 0 in case of success, the set of unknown or unimplemented options
13300
 *         in case of error.
13301
 */
13302
int
13303
xmlCtxtUseOptions(xmlParserCtxt *ctxt, int options)
13304
58.3k
{
13305
58.3k
    int keepMask;
13306
13307
58.3k
#ifdef LIBXML_HTML_ENABLED
13308
58.3k
    if ((ctxt != NULL) && (ctxt->html))
13309
0
        return(htmlCtxtUseOptions(ctxt, options));
13310
58.3k
#endif
13311
13312
    /*
13313
     * For historic reasons, some options can only be enabled.
13314
     */
13315
58.3k
    keepMask = XML_PARSE_NOERROR |
13316
58.3k
               XML_PARSE_NOWARNING |
13317
58.3k
               XML_PARSE_NONET |
13318
58.3k
               XML_PARSE_NSCLEAN |
13319
58.3k
               XML_PARSE_NOCDATA |
13320
58.3k
               XML_PARSE_COMPACT |
13321
58.3k
               XML_PARSE_OLD10 |
13322
58.3k
               XML_PARSE_HUGE |
13323
58.3k
               XML_PARSE_OLDSAX |
13324
58.3k
               XML_PARSE_IGNORE_ENC |
13325
58.3k
               XML_PARSE_BIG_LINES;
13326
13327
58.3k
    return(xmlCtxtSetOptionsInternal(ctxt, options, keepMask));
13328
58.3k
}
13329
13330
/**
13331
 * To protect against exponential entity expansion ("billion laughs"), the
13332
 * size of serialized output is (roughly) limited to the input size
13333
 * multiplied by this factor. The default value is 5.
13334
 *
13335
 * When working with documents making heavy use of entity expansion, it can
13336
 * be necessary to increase the value. For security reasons, this should only
13337
 * be considered when processing trusted input.
13338
 *
13339
 * @param ctxt  an XML parser context
13340
 * @param maxAmpl  maximum amplification factor
13341
 */
13342
void
13343
xmlCtxtSetMaxAmplification(xmlParserCtxt *ctxt, unsigned maxAmpl)
13344
0
{
13345
0
    if (ctxt == NULL)
13346
0
        return;
13347
0
    if (maxAmpl == 0)
13348
0
        return;
13349
0
    ctxt->maxAmpl = maxAmpl;
13350
0
}
13351
13352
/**
13353
 * Parse an XML document and return the resulting document tree.
13354
 * Takes ownership of the input object.
13355
 *
13356
 * @since 2.13.0
13357
 *
13358
 * @param ctxt  an XML parser context
13359
 * @param input  parser input
13360
 * @returns the resulting document tree or NULL
13361
 */
13362
xmlDoc *
13363
xmlCtxtParseDocument(xmlParserCtxt *ctxt, xmlParserInput *input)
13364
37.6k
{
13365
37.6k
    xmlDocPtr ret = NULL;
13366
13367
37.6k
    if ((ctxt == NULL) || (input == NULL)) {
13368
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
13369
0
        xmlFreeInputStream(input);
13370
0
        return(NULL);
13371
0
    }
13372
13373
    /* assert(ctxt->inputNr == 0); */
13374
37.6k
    while (ctxt->inputNr > 0)
13375
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
13376
13377
37.6k
    if (xmlCtxtPushInput(ctxt, input) < 0) {
13378
0
        xmlFreeInputStream(input);
13379
0
        return(NULL);
13380
0
    }
13381
13382
37.6k
    xmlParseDocument(ctxt);
13383
13384
37.6k
    ret = xmlCtxtGetDocument(ctxt);
13385
13386
    /* assert(ctxt->inputNr == 1); */
13387
75.3k
    while (ctxt->inputNr > 0)
13388
37.6k
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
13389
13390
37.6k
    return(ret);
13391
37.6k
}
13392
13393
/**
13394
 * Convenience function to parse an XML document from a
13395
 * zero-terminated string.
13396
 *
13397
 * See #xmlCtxtReadDoc for details.
13398
 *
13399
 * @param cur  a pointer to a zero terminated string
13400
 * @param URL  base URL (optional)
13401
 * @param encoding  the document encoding (optional)
13402
 * @param options  a combination of xmlParserOption
13403
 * @returns the resulting document tree
13404
 */
13405
xmlDoc *
13406
xmlReadDoc(const xmlChar *cur, const char *URL, const char *encoding,
13407
           int options)
13408
0
{
13409
0
    xmlParserCtxtPtr ctxt;
13410
0
    xmlParserInputPtr input;
13411
0
    xmlDocPtr doc = NULL;
13412
13413
0
    ctxt = xmlNewParserCtxt();
13414
0
    if (ctxt == NULL)
13415
0
        return(NULL);
13416
13417
0
    xmlCtxtUseOptions(ctxt, options);
13418
13419
0
    input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) cur, encoding,
13420
0
                                      XML_INPUT_BUF_STATIC);
13421
13422
0
    if (input != NULL)
13423
0
        doc = xmlCtxtParseDocument(ctxt, input);
13424
13425
0
    xmlFreeParserCtxt(ctxt);
13426
0
    return(doc);
13427
0
}
13428
13429
/**
13430
 * Convenience function to parse an XML file from the filesystem
13431
 * or a global, user-defined resource loader.
13432
 *
13433
 * If a "-" filename is passed, the function will read from stdin.
13434
 * This feature is potentially insecure and might be removed from
13435
 * later versions.
13436
 *
13437
 * See #xmlCtxtReadFile for details.
13438
 *
13439
 * @param filename  a file or URL
13440
 * @param encoding  the document encoding (optional)
13441
 * @param options  a combination of xmlParserOption
13442
 * @returns the resulting document tree
13443
 */
13444
xmlDoc *
13445
xmlReadFile(const char *filename, const char *encoding, int options)
13446
0
{
13447
0
    xmlParserCtxtPtr ctxt;
13448
0
    xmlParserInputPtr input;
13449
0
    xmlDocPtr doc = NULL;
13450
13451
0
    ctxt = xmlNewParserCtxt();
13452
0
    if (ctxt == NULL)
13453
0
        return(NULL);
13454
13455
0
    xmlCtxtUseOptions(ctxt, options);
13456
13457
    /*
13458
     * Backward compatibility for users of command line utilities like
13459
     * xmlstarlet expecting "-" to mean stdin. This is dangerous and
13460
     * should be removed at some point.
13461
     */
13462
0
    if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0))
13463
0
        input = xmlCtxtNewInputFromFd(ctxt, filename, STDIN_FILENO,
13464
0
                                      encoding, 0);
13465
0
    else
13466
0
        input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0);
13467
13468
0
    if (input != NULL)
13469
0
        doc = xmlCtxtParseDocument(ctxt, input);
13470
13471
0
    xmlFreeParserCtxt(ctxt);
13472
0
    return(doc);
13473
0
}
13474
13475
/**
13476
 * Parse an XML in-memory document and build a tree. The input buffer must
13477
 * not contain a terminating null byte.
13478
 *
13479
 * See #xmlCtxtReadMemory for details.
13480
 *
13481
 * @param buffer  a pointer to a char array
13482
 * @param size  the size of the array
13483
 * @param url  base URL (optional)
13484
 * @param encoding  the document encoding (optional)
13485
 * @param options  a combination of xmlParserOption
13486
 * @returns the resulting document tree
13487
 */
13488
xmlDoc *
13489
xmlReadMemory(const char *buffer, int size, const char *url,
13490
              const char *encoding, int options)
13491
37.6k
{
13492
37.6k
    xmlParserCtxtPtr ctxt;
13493
37.6k
    xmlParserInputPtr input;
13494
37.6k
    xmlDocPtr doc = NULL;
13495
13496
37.6k
    if (size < 0)
13497
0
  return(NULL);
13498
13499
37.6k
    ctxt = xmlNewParserCtxt();
13500
37.6k
    if (ctxt == NULL)
13501
0
        return(NULL);
13502
13503
37.6k
    xmlCtxtUseOptions(ctxt, options);
13504
13505
37.6k
    input = xmlCtxtNewInputFromMemory(ctxt, url, buffer, size, encoding,
13506
37.6k
                                      XML_INPUT_BUF_STATIC);
13507
13508
37.6k
    if (input != NULL)
13509
37.6k
        doc = xmlCtxtParseDocument(ctxt, input);
13510
13511
37.6k
    xmlFreeParserCtxt(ctxt);
13512
37.6k
    return(doc);
13513
37.6k
}
13514
13515
/**
13516
 * Parse an XML from a file descriptor and build a tree.
13517
 *
13518
 * See #xmlCtxtReadFd for details.
13519
 *
13520
 * NOTE that the file descriptor will not be closed when the
13521
 * context is freed or reset.
13522
 *
13523
 * @param fd  an open file descriptor
13524
 * @param URL  base URL (optional)
13525
 * @param encoding  the document encoding (optional)
13526
 * @param options  a combination of xmlParserOption
13527
 * @returns the resulting document tree
13528
 */
13529
xmlDoc *
13530
xmlReadFd(int fd, const char *URL, const char *encoding, int options)
13531
0
{
13532
0
    xmlParserCtxtPtr ctxt;
13533
0
    xmlParserInputPtr input;
13534
0
    xmlDocPtr doc = NULL;
13535
13536
0
    ctxt = xmlNewParserCtxt();
13537
0
    if (ctxt == NULL)
13538
0
        return(NULL);
13539
13540
0
    xmlCtxtUseOptions(ctxt, options);
13541
13542
0
    input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0);
13543
13544
0
    if (input != NULL)
13545
0
        doc = xmlCtxtParseDocument(ctxt, input);
13546
13547
0
    xmlFreeParserCtxt(ctxt);
13548
0
    return(doc);
13549
0
}
13550
13551
/**
13552
 * Parse an XML document from I/O functions and context and build a tree.
13553
 *
13554
 * See #xmlCtxtReadIO for details.
13555
 *
13556
 * @param ioread  an I/O read function
13557
 * @param ioclose  an I/O close function (optional)
13558
 * @param ioctx  an I/O handler
13559
 * @param URL  base URL (optional)
13560
 * @param encoding  the document encoding (optional)
13561
 * @param options  a combination of xmlParserOption
13562
 * @returns the resulting document tree
13563
 */
13564
xmlDoc *
13565
xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
13566
          void *ioctx, const char *URL, const char *encoding, int options)
13567
0
{
13568
0
    xmlParserCtxtPtr ctxt;
13569
0
    xmlParserInputPtr input;
13570
0
    xmlDocPtr doc = NULL;
13571
13572
0
    ctxt = xmlNewParserCtxt();
13573
0
    if (ctxt == NULL)
13574
0
        return(NULL);
13575
13576
0
    xmlCtxtUseOptions(ctxt, options);
13577
13578
0
    input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx,
13579
0
                                  encoding, 0);
13580
13581
0
    if (input != NULL)
13582
0
        doc = xmlCtxtParseDocument(ctxt, input);
13583
13584
0
    xmlFreeParserCtxt(ctxt);
13585
0
    return(doc);
13586
0
}
13587
13588
/**
13589
 * Parse an XML in-memory document and build a tree.
13590
 *
13591
 * `URL` is used as base to resolve external entities and for error
13592
 * reporting.
13593
 *
13594
 * @param ctxt  an XML parser context
13595
 * @param str  a pointer to a zero terminated string
13596
 * @param URL  base URL (optional)
13597
 * @param encoding  the document encoding (optional)
13598
 * @param options  a combination of xmlParserOption
13599
 * @returns the resulting document tree
13600
 */
13601
xmlDoc *
13602
xmlCtxtReadDoc(xmlParserCtxt *ctxt, const xmlChar *str,
13603
               const char *URL, const char *encoding, int options)
13604
0
{
13605
0
    xmlParserInputPtr input;
13606
13607
0
    if (ctxt == NULL)
13608
0
        return(NULL);
13609
13610
0
    xmlCtxtReset(ctxt);
13611
0
    xmlCtxtUseOptions(ctxt, options);
13612
13613
0
    input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) str, encoding,
13614
0
                                      XML_INPUT_BUF_STATIC);
13615
0
    if (input == NULL)
13616
0
        return(NULL);
13617
13618
0
    return(xmlCtxtParseDocument(ctxt, input));
13619
0
}
13620
13621
/**
13622
 * Parse an XML file from the filesystem or a global, user-defined
13623
 * resource loader.
13624
 *
13625
 * @param ctxt  an XML parser context
13626
 * @param filename  a file or URL
13627
 * @param encoding  the document encoding (optional)
13628
 * @param options  a combination of xmlParserOption
13629
 * @returns the resulting document tree
13630
 */
13631
xmlDoc *
13632
xmlCtxtReadFile(xmlParserCtxt *ctxt, const char *filename,
13633
                const char *encoding, int options)
13634
0
{
13635
0
    xmlParserInputPtr input;
13636
13637
0
    if (ctxt == NULL)
13638
0
        return(NULL);
13639
13640
0
    xmlCtxtReset(ctxt);
13641
0
    xmlCtxtUseOptions(ctxt, options);
13642
13643
0
    input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0);
13644
0
    if (input == NULL)
13645
0
        return(NULL);
13646
13647
0
    return(xmlCtxtParseDocument(ctxt, input));
13648
0
}
13649
13650
/**
13651
 * Parse an XML in-memory document and build a tree. The input buffer must
13652
 * not contain a terminating null byte.
13653
 *
13654
 * `URL` is used as base to resolve external entities and for error
13655
 * reporting.
13656
 *
13657
 * @param ctxt  an XML parser context
13658
 * @param buffer  a pointer to a char array
13659
 * @param size  the size of the array
13660
 * @param URL  base URL (optional)
13661
 * @param encoding  the document encoding (optional)
13662
 * @param options  a combination of xmlParserOption
13663
 * @returns the resulting document tree
13664
 */
13665
xmlDoc *
13666
xmlCtxtReadMemory(xmlParserCtxt *ctxt, const char *buffer, int size,
13667
                  const char *URL, const char *encoding, int options)
13668
0
{
13669
0
    xmlParserInputPtr input;
13670
13671
0
    if ((ctxt == NULL) || (size < 0))
13672
0
        return(NULL);
13673
13674
0
    xmlCtxtReset(ctxt);
13675
0
    xmlCtxtUseOptions(ctxt, options);
13676
13677
0
    input = xmlCtxtNewInputFromMemory(ctxt, URL, buffer, size, encoding,
13678
0
                                      XML_INPUT_BUF_STATIC);
13679
0
    if (input == NULL)
13680
0
        return(NULL);
13681
13682
0
    return(xmlCtxtParseDocument(ctxt, input));
13683
0
}
13684
13685
/**
13686
 * Parse an XML document from a file descriptor and build a tree.
13687
 *
13688
 * NOTE that the file descriptor will not be closed when the
13689
 * context is freed or reset.
13690
 *
13691
 * `URL` is used as base to resolve external entities and for error
13692
 * reporting.
13693
 *
13694
 * @param ctxt  an XML parser context
13695
 * @param fd  an open file descriptor
13696
 * @param URL  base URL (optional)
13697
 * @param encoding  the document encoding (optional)
13698
 * @param options  a combination of xmlParserOption
13699
 * @returns the resulting document tree
13700
 */
13701
xmlDoc *
13702
xmlCtxtReadFd(xmlParserCtxt *ctxt, int fd,
13703
              const char *URL, const char *encoding, int options)
13704
0
{
13705
0
    xmlParserInputPtr input;
13706
13707
0
    if (ctxt == NULL)
13708
0
        return(NULL);
13709
13710
0
    xmlCtxtReset(ctxt);
13711
0
    xmlCtxtUseOptions(ctxt, options);
13712
13713
0
    input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0);
13714
0
    if (input == NULL)
13715
0
        return(NULL);
13716
13717
0
    return(xmlCtxtParseDocument(ctxt, input));
13718
0
}
13719
13720
/**
13721
 * Parse an XML document from I/O functions and source and build a tree.
13722
 * This reuses the existing `ctxt` parser context
13723
 *
13724
 * `URL` is used as base to resolve external entities and for error
13725
 * reporting.
13726
 *
13727
 * @param ctxt  an XML parser context
13728
 * @param ioread  an I/O read function
13729
 * @param ioclose  an I/O close function
13730
 * @param ioctx  an I/O handler
13731
 * @param URL  the base URL to use for the document
13732
 * @param encoding  the document encoding, or NULL
13733
 * @param options  a combination of xmlParserOption
13734
 * @returns the resulting document tree
13735
 */
13736
xmlDoc *
13737
xmlCtxtReadIO(xmlParserCtxt *ctxt, xmlInputReadCallback ioread,
13738
              xmlInputCloseCallback ioclose, void *ioctx,
13739
        const char *URL,
13740
              const char *encoding, int options)
13741
0
{
13742
0
    xmlParserInputPtr input;
13743
13744
0
    if (ctxt == NULL)
13745
0
        return(NULL);
13746
13747
0
    xmlCtxtReset(ctxt);
13748
0
    xmlCtxtUseOptions(ctxt, options);
13749
13750
0
    input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx,
13751
0
                                  encoding, 0);
13752
0
    if (input == NULL)
13753
0
        return(NULL);
13754
13755
0
    return(xmlCtxtParseDocument(ctxt, input));
13756
0
}
13757