Coverage Report

Created: 2025-07-18 06:55

/src/libxml2/parser.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * parser.c : an XML 1.0 parser, namespaces and validity support are mostly
3
 *            implemented on top of the SAX interfaces
4
 *
5
 * References:
6
 *   The XML specification:
7
 *     http://www.w3.org/TR/REC-xml
8
 *   Original 1.0 version:
9
 *     http://www.w3.org/TR/1998/REC-xml-19980210
10
 *   XML second edition working draft
11
 *     http://www.w3.org/TR/2000/WD-xml-2e-20000814
12
 *
13
 * Okay this is a big file, the parser core is around 7000 lines, then it
14
 * is followed by the progressive parser top routines, then the various
15
 * high level APIs to call the parser and a few miscellaneous functions.
16
 * A number of helper functions and deprecated ones have been moved to
17
 * parserInternals.c to reduce this file size.
18
 * As much as possible the functions are associated with their relative
19
 * production in the XML specification. A few productions defining the
20
 * different ranges of character are actually implanted either in
21
 * parserInternals.h or parserInternals.c
22
 * The DOM tree build is realized from the default SAX callbacks in
23
 * the module SAX2.c.
24
 * The routines doing the validation checks are in valid.c and called either
25
 * from the SAX callbacks or as standalone functions using a preparsed
26
 * document.
27
 *
28
 * See Copyright for the status of this software.
29
 *
30
 * Author: Daniel Veillard
31
 */
32
33
/* To avoid EBCDIC trouble when parsing on zOS */
34
#if defined(__MVS__)
35
#pragma convert("ISO8859-1")
36
#endif
37
38
#define IN_LIBXML
39
#include "libxml.h"
40
41
#if defined(_WIN32)
42
#define XML_DIR_SEP '\\'
43
#else
44
#define XML_DIR_SEP '/'
45
#endif
46
47
#include <stdlib.h>
48
#include <limits.h>
49
#include <string.h>
50
#include <stdarg.h>
51
#include <stddef.h>
52
#include <ctype.h>
53
#include <stdlib.h>
54
#include <libxml/parser.h>
55
#include <libxml/xmlmemory.h>
56
#include <libxml/tree.h>
57
#include <libxml/parserInternals.h>
58
#include <libxml/valid.h>
59
#include <libxml/entities.h>
60
#include <libxml/xmlerror.h>
61
#include <libxml/encoding.h>
62
#include <libxml/xmlIO.h>
63
#include <libxml/uri.h>
64
#include <libxml/SAX2.h>
65
#include <libxml/HTMLparser.h>
66
#ifdef LIBXML_CATALOG_ENABLED
67
#include <libxml/catalog.h>
68
#endif
69
70
#include "private/buf.h"
71
#include "private/dict.h"
72
#include "private/entities.h"
73
#include "private/error.h"
74
#include "private/html.h"
75
#include "private/io.h"
76
#include "private/memory.h"
77
#include "private/parser.h"
78
#include "private/tree.h"
79
80
0
#define NS_INDEX_EMPTY  INT_MAX
81
0
#define NS_INDEX_XML    (INT_MAX - 1)
82
0
#define URI_HASH_EMPTY  0xD943A04E
83
0
#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
0
#define XML_MAX_ATTRS 100000000 /* 100 million */
94
95
0
#define XML_SPECIAL_EXTERNAL    (1 << 20)
96
0
#define XML_SPECIAL_TYPE_MASK   (XML_SPECIAL_EXTERNAL - 1)
97
98
0
#define XML_ATTVAL_ALLOC        (1 << 0)
99
0
#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
0
#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
0
#define XML_ENT_FIXED_COST 20
170
171
0
#define XML_PARSER_BIG_BUFFER_SIZE 300
172
0
#define XML_PARSER_BUFFER_SIZE 100
173
0
#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
84
xmlErrMemory(xmlParserCtxtPtr ctxt) {
225
84
    xmlCtxtErrMemory(ctxt);
226
84
}
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
0
{
239
0
    if (prefix == NULL)
240
0
        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
241
0
                   XML_ERR_FATAL, localname, NULL, NULL, 0,
242
0
                   "Attribute %s redefined\n", localname);
243
0
    else
244
0
        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
245
0
                   XML_ERR_FATAL, prefix, localname, NULL, 0,
246
0
                   "Attribute %s:%s redefined\n", prefix, localname);
247
0
}
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
0
{
260
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
261
0
               NULL, NULL, NULL, 0, "%s", msg);
262
0
}
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
0
{
277
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
278
0
               str1, str2, NULL, 0, msg, str1, str2);
279
0
}
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
50
{
314
50
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
315
50
               NULL, NULL, NULL, val, msg, val);
316
50
}
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
0
{
333
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
334
0
               str1, str2, NULL, val, msg, str1, val, str2);
335
0
}
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
0
{
349
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
350
0
               val, NULL, NULL, 0, msg, val);
351
0
}
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
0
{
365
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_ERROR,
366
0
               val, NULL, NULL, 0, msg, val);
367
0
}
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
0
{
385
0
    ctxt->nsWellFormed = 0;
386
387
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
388
0
               info1, info2, info3, 0, msg, info1, info2, info3);
389
0
}
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
0
{
407
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
408
0
               info1, info2, info3, 0, msg, info1, info2, info3);
409
0
}
410
411
static void
412
0
xmlSaturatedAdd(unsigned long *dst, unsigned long val) {
413
0
    if (val > ULONG_MAX - *dst)
414
0
        *dst = ULONG_MAX;
415
0
    else
416
0
        *dst += val;
417
0
}
418
419
static void
420
0
xmlSaturatedAddSizeT(unsigned long *dst, unsigned long val) {
421
0
    if (val > ULONG_MAX - *dst)
422
0
        *dst = ULONG_MAX;
423
0
    else
424
0
        *dst += val;
425
0
}
426
427
/**
428
 * Check for non-linear entity expansion behaviour.
429
 *
430
 * In some cases like xmlExpandEntityInAttValue, this function is called
431
 * for each, possibly nested entity and its unexpanded content length.
432
 *
433
 * In other cases like #xmlParseReference, it's only called for each
434
 * top-level entity with its unexpanded content length plus the sum of
435
 * the unexpanded content lengths (plus fixed cost) of all nested
436
 * entities.
437
 *
438
 * Summing the unexpanded lengths also adds the length of the reference.
439
 * This is by design. Taking the length of the entity name into account
440
 * discourages attacks that try to waste CPU time with abusively long
441
 * entity names. See test/recurse/lol6.xml for example. Each call also
442
 * adds some fixed cost XML_ENT_FIXED_COST to discourage attacks with
443
 * short entities.
444
 *
445
 * @param ctxt  parser context
446
 * @param extra  sum of unexpanded entity sizes
447
 * @returns 1 on error, 0 on success.
448
 */
449
static int
450
xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long extra)
451
0
{
452
0
    unsigned long consumed;
453
0
    unsigned long *expandedSize;
454
0
    xmlParserInputPtr input = ctxt->input;
455
0
    xmlEntityPtr entity = input->entity;
456
457
0
    if ((entity) && (entity->flags & XML_ENT_CHECKED))
458
0
        return(0);
459
460
    /*
461
     * Compute total consumed bytes so far, including input streams of
462
     * external entities.
463
     */
464
0
    consumed = input->consumed;
465
0
    xmlSaturatedAddSizeT(&consumed, input->cur - input->base);
466
0
    xmlSaturatedAdd(&consumed, ctxt->sizeentities);
467
468
0
    if (entity)
469
0
        expandedSize = &entity->expandedSize;
470
0
    else
471
0
        expandedSize = &ctxt->sizeentcopy;
472
473
    /*
474
     * Add extra cost and some fixed cost.
475
     */
476
0
    xmlSaturatedAdd(expandedSize, extra);
477
0
    xmlSaturatedAdd(expandedSize, XML_ENT_FIXED_COST);
478
479
    /*
480
     * It's important to always use saturation arithmetic when tracking
481
     * entity sizes to make the size checks reliable. If "sizeentcopy"
482
     * overflows, we have to abort.
483
     */
484
0
    if ((*expandedSize > XML_PARSER_ALLOWED_EXPANSION) &&
485
0
        ((*expandedSize >= ULONG_MAX) ||
486
0
         (*expandedSize / ctxt->maxAmpl > consumed))) {
487
0
        xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
488
0
                       "Maximum entity amplification factor exceeded, see "
489
0
                       "xmlCtxtSetMaxAmplification.\n");
490
0
        xmlHaltParser(ctxt);
491
0
        return(1);
492
0
    }
493
494
0
    return(0);
495
0
}
496
497
/************************************************************************
498
 *                  *
499
 *    Library wide options          *
500
 *                  *
501
 ************************************************************************/
502
503
/**
504
 * Examines if the library has been compiled with a given feature.
505
 *
506
 * @param feature  the feature to be examined
507
 * @returns zero (0) if the feature does not exist or an unknown
508
 * feature is requested, non-zero otherwise.
509
 */
510
int
511
xmlHasFeature(xmlFeature feature)
512
0
{
513
0
    switch (feature) {
514
0
  case XML_WITH_THREAD:
515
0
#ifdef LIBXML_THREAD_ENABLED
516
0
      return(1);
517
#else
518
      return(0);
519
#endif
520
0
        case XML_WITH_TREE:
521
0
            return(1);
522
0
        case XML_WITH_OUTPUT:
523
0
#ifdef LIBXML_OUTPUT_ENABLED
524
0
            return(1);
525
#else
526
            return(0);
527
#endif
528
0
        case XML_WITH_PUSH:
529
0
#ifdef LIBXML_PUSH_ENABLED
530
0
            return(1);
531
#else
532
            return(0);
533
#endif
534
0
        case XML_WITH_READER:
535
0
#ifdef LIBXML_READER_ENABLED
536
0
            return(1);
537
#else
538
            return(0);
539
#endif
540
0
        case XML_WITH_PATTERN:
541
0
#ifdef LIBXML_PATTERN_ENABLED
542
0
            return(1);
543
#else
544
            return(0);
545
#endif
546
0
        case XML_WITH_WRITER:
547
0
#ifdef LIBXML_WRITER_ENABLED
548
0
            return(1);
549
#else
550
            return(0);
551
#endif
552
0
        case XML_WITH_SAX1:
553
0
#ifdef LIBXML_SAX1_ENABLED
554
0
            return(1);
555
#else
556
            return(0);
557
#endif
558
0
        case XML_WITH_HTTP:
559
0
            return(0);
560
0
        case XML_WITH_VALID:
561
0
#ifdef LIBXML_VALID_ENABLED
562
0
            return(1);
563
#else
564
            return(0);
565
#endif
566
0
        case XML_WITH_HTML:
567
0
#ifdef LIBXML_HTML_ENABLED
568
0
            return(1);
569
#else
570
            return(0);
571
#endif
572
0
        case XML_WITH_LEGACY:
573
0
            return(0);
574
0
        case XML_WITH_C14N:
575
0
#ifdef LIBXML_C14N_ENABLED
576
0
            return(1);
577
#else
578
            return(0);
579
#endif
580
0
        case XML_WITH_CATALOG:
581
0
#ifdef LIBXML_CATALOG_ENABLED
582
0
            return(1);
583
#else
584
            return(0);
585
#endif
586
0
        case XML_WITH_XPATH:
587
0
#ifdef LIBXML_XPATH_ENABLED
588
0
            return(1);
589
#else
590
            return(0);
591
#endif
592
0
        case XML_WITH_XPTR:
593
0
#ifdef LIBXML_XPTR_ENABLED
594
0
            return(1);
595
#else
596
            return(0);
597
#endif
598
0
        case XML_WITH_XINCLUDE:
599
0
#ifdef LIBXML_XINCLUDE_ENABLED
600
0
            return(1);
601
#else
602
            return(0);
603
#endif
604
0
        case XML_WITH_ICONV:
605
0
#ifdef LIBXML_ICONV_ENABLED
606
0
            return(1);
607
#else
608
            return(0);
609
#endif
610
0
        case XML_WITH_ISO8859X:
611
0
#ifdef LIBXML_ISO8859X_ENABLED
612
0
            return(1);
613
#else
614
            return(0);
615
#endif
616
0
        case XML_WITH_UNICODE:
617
0
            return(0);
618
0
        case XML_WITH_REGEXP:
619
0
#ifdef LIBXML_REGEXP_ENABLED
620
0
            return(1);
621
#else
622
            return(0);
623
#endif
624
0
        case XML_WITH_AUTOMATA:
625
0
#ifdef LIBXML_REGEXP_ENABLED
626
0
            return(1);
627
#else
628
            return(0);
629
#endif
630
0
        case XML_WITH_EXPR:
631
0
            return(0);
632
0
        case XML_WITH_RELAXNG:
633
0
#ifdef LIBXML_RELAXNG_ENABLED
634
0
            return(1);
635
#else
636
            return(0);
637
#endif
638
0
        case XML_WITH_SCHEMAS:
639
0
#ifdef LIBXML_SCHEMAS_ENABLED
640
0
            return(1);
641
#else
642
            return(0);
643
#endif
644
0
        case XML_WITH_SCHEMATRON:
645
#ifdef LIBXML_SCHEMATRON_ENABLED
646
            return(1);
647
#else
648
0
            return(0);
649
0
#endif
650
0
        case XML_WITH_MODULES:
651
0
#ifdef LIBXML_MODULES_ENABLED
652
0
            return(1);
653
#else
654
            return(0);
655
#endif
656
0
        case XML_WITH_DEBUG:
657
#ifdef LIBXML_DEBUG_ENABLED
658
            return(1);
659
#else
660
0
            return(0);
661
0
#endif
662
0
        case XML_WITH_DEBUG_MEM:
663
0
            return(0);
664
0
        case XML_WITH_ZLIB:
665
0
#ifdef LIBXML_ZLIB_ENABLED
666
0
            return(1);
667
#else
668
            return(0);
669
#endif
670
0
        case XML_WITH_LZMA:
671
0
#ifdef LIBXML_LZMA_ENABLED
672
0
            return(1);
673
#else
674
            return(0);
675
#endif
676
0
        case XML_WITH_ICU:
677
#ifdef LIBXML_ICU_ENABLED
678
            return(1);
679
#else
680
0
            return(0);
681
0
#endif
682
0
        default:
683
0
      break;
684
0
     }
685
0
     return(0);
686
0
}
687
688
/************************************************************************
689
 *                  *
690
 *      Simple string buffer        *
691
 *                  *
692
 ************************************************************************/
693
694
typedef struct {
695
    xmlChar *mem;
696
    unsigned size;
697
    unsigned cap; /* size < cap */
698
    unsigned max; /* size <= max */
699
    xmlParserErrors code;
700
} xmlSBuf;
701
702
static void
703
0
xmlSBufInit(xmlSBuf *buf, unsigned max) {
704
0
    buf->mem = NULL;
705
0
    buf->size = 0;
706
0
    buf->cap = 0;
707
0
    buf->max = max;
708
0
    buf->code = XML_ERR_OK;
709
0
}
710
711
static int
712
0
xmlSBufGrow(xmlSBuf *buf, unsigned len) {
713
0
    xmlChar *mem;
714
0
    unsigned cap;
715
716
0
    if (len >= UINT_MAX / 2 - buf->size) {
717
0
        if (buf->code == XML_ERR_OK)
718
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
719
0
        return(-1);
720
0
    }
721
722
0
    cap = (buf->size + len) * 2;
723
0
    if (cap < 240)
724
0
        cap = 240;
725
726
0
    mem = xmlRealloc(buf->mem, cap);
727
0
    if (mem == NULL) {
728
0
        buf->code = XML_ERR_NO_MEMORY;
729
0
        return(-1);
730
0
    }
731
732
0
    buf->mem = mem;
733
0
    buf->cap = cap;
734
735
0
    return(0);
736
0
}
737
738
static void
739
0
xmlSBufAddString(xmlSBuf *buf, const xmlChar *str, unsigned len) {
740
0
    if (buf->max - buf->size < len) {
741
0
        if (buf->code == XML_ERR_OK)
742
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
743
0
        return;
744
0
    }
745
746
0
    if (buf->cap - buf->size <= len) {
747
0
        if (xmlSBufGrow(buf, len) < 0)
748
0
            return;
749
0
    }
750
751
0
    if (len > 0)
752
0
        memcpy(buf->mem + buf->size, str, len);
753
0
    buf->size += len;
754
0
}
755
756
static void
757
0
xmlSBufAddCString(xmlSBuf *buf, const char *str, unsigned len) {
758
0
    xmlSBufAddString(buf, (const xmlChar *) str, len);
759
0
}
760
761
static void
762
0
xmlSBufAddChar(xmlSBuf *buf, int c) {
763
0
    xmlChar *end;
764
765
0
    if (buf->max - buf->size < 4) {
766
0
        if (buf->code == XML_ERR_OK)
767
0
            buf->code = XML_ERR_RESOURCE_LIMIT;
768
0
        return;
769
0
    }
770
771
0
    if (buf->cap - buf->size <= 4) {
772
0
        if (xmlSBufGrow(buf, 4) < 0)
773
0
            return;
774
0
    }
775
776
0
    end = buf->mem + buf->size;
777
778
0
    if (c < 0x80) {
779
0
        *end = (xmlChar) c;
780
0
        buf->size += 1;
781
0
    } else {
782
0
        buf->size += xmlCopyCharMultiByte(end, c);
783
0
    }
784
0
}
785
786
static void
787
0
xmlSBufAddReplChar(xmlSBuf *buf) {
788
0
    xmlSBufAddCString(buf, "\xEF\xBF\xBD", 3);
789
0
}
790
791
static void
792
0
xmlSBufReportError(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
793
0
    if (buf->code == XML_ERR_NO_MEMORY)
794
0
        xmlCtxtErrMemory(ctxt);
795
0
    else
796
0
        xmlFatalErr(ctxt, buf->code, errMsg);
797
0
}
798
799
static xmlChar *
800
xmlSBufFinish(xmlSBuf *buf, int *sizeOut, xmlParserCtxtPtr ctxt,
801
0
              const char *errMsg) {
802
0
    if (buf->mem == NULL) {
803
0
        buf->mem = xmlMalloc(1);
804
0
        if (buf->mem == NULL) {
805
0
            buf->code = XML_ERR_NO_MEMORY;
806
0
        } else {
807
0
            buf->mem[0] = 0;
808
0
        }
809
0
    } else {
810
0
        buf->mem[buf->size] = 0;
811
0
    }
812
813
0
    if (buf->code == XML_ERR_OK) {
814
0
        if (sizeOut != NULL)
815
0
            *sizeOut = buf->size;
816
0
        return(buf->mem);
817
0
    }
818
819
0
    xmlSBufReportError(buf, ctxt, errMsg);
820
821
0
    xmlFree(buf->mem);
822
823
0
    if (sizeOut != NULL)
824
0
        *sizeOut = 0;
825
0
    return(NULL);
826
0
}
827
828
static void
829
0
xmlSBufCleanup(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
830
0
    if (buf->code != XML_ERR_OK)
831
0
        xmlSBufReportError(buf, ctxt, errMsg);
832
833
0
    xmlFree(buf->mem);
834
0
}
835
836
static int
837
xmlUTF8MultibyteLen(xmlParserCtxtPtr ctxt, const xmlChar *str,
838
0
                    const char *errMsg) {
839
0
    int c = str[0];
840
0
    int c1 = str[1];
841
842
0
    if ((c1 & 0xC0) != 0x80)
843
0
        goto encoding_error;
844
845
0
    if (c < 0xE0) {
846
        /* 2-byte sequence */
847
0
        if (c < 0xC2)
848
0
            goto encoding_error;
849
850
0
        return(2);
851
0
    } else {
852
0
        int c2 = str[2];
853
854
0
        if ((c2 & 0xC0) != 0x80)
855
0
            goto encoding_error;
856
857
0
        if (c < 0xF0) {
858
            /* 3-byte sequence */
859
0
            if (c == 0xE0) {
860
                /* overlong */
861
0
                if (c1 < 0xA0)
862
0
                    goto encoding_error;
863
0
            } else if (c == 0xED) {
864
                /* surrogate */
865
0
                if (c1 >= 0xA0)
866
0
                    goto encoding_error;
867
0
            } else if (c == 0xEF) {
868
                /* U+FFFE and U+FFFF are invalid Chars */
869
0
                if ((c1 == 0xBF) && (c2 >= 0xBE))
870
0
                    xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, errMsg);
871
0
            }
872
873
0
            return(3);
874
0
        } else {
875
            /* 4-byte sequence */
876
0
            if ((str[3] & 0xC0) != 0x80)
877
0
                goto encoding_error;
878
0
            if (c == 0xF0) {
879
                /* overlong */
880
0
                if (c1 < 0x90)
881
0
                    goto encoding_error;
882
0
            } else if (c >= 0xF4) {
883
                /* greater than 0x10FFFF */
884
0
                if ((c > 0xF4) || (c1 >= 0x90))
885
0
                    goto encoding_error;
886
0
            }
887
888
0
            return(4);
889
0
        }
890
0
    }
891
892
0
encoding_error:
893
    /* Only report the first error */
894
0
    if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
895
0
        xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
896
0
        ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
897
0
    }
898
899
0
    return(0);
900
0
}
901
902
/************************************************************************
903
 *                  *
904
 *    SAX2 defaulted attributes handling      *
905
 *                  *
906
 ************************************************************************/
907
908
/**
909
 * Final initialization of the parser context before starting to parse.
910
 *
911
 * This accounts for users modifying struct members of parser context
912
 * directly.
913
 *
914
 * @param ctxt  an XML parser context
915
 */
916
static void
917
0
xmlCtxtInitializeLate(xmlParserCtxtPtr ctxt) {
918
0
    xmlSAXHandlerPtr sax;
919
920
    /* Avoid unused variable warning if features are disabled. */
921
0
    (void) sax;
922
923
    /*
924
     * Changing the SAX struct directly is still widespread practice
925
     * in internal and external code.
926
     */
927
0
    if (ctxt == NULL) return;
928
0
    sax = ctxt->sax;
929
0
#ifdef LIBXML_SAX1_ENABLED
930
    /*
931
     * Only enable SAX2 if there SAX2 element handlers, except when there
932
     * are no element handlers at all.
933
     */
934
0
    if (((ctxt->options & XML_PARSE_SAX1) == 0) &&
935
0
        (sax) &&
936
0
        (sax->initialized == XML_SAX2_MAGIC) &&
937
0
        ((sax->startElementNs != NULL) ||
938
0
         (sax->endElementNs != NULL) ||
939
0
         ((sax->startElement == NULL) && (sax->endElement == NULL))))
940
0
        ctxt->sax2 = 1;
941
#else
942
    ctxt->sax2 = 1;
943
#endif /* LIBXML_SAX1_ENABLED */
944
945
    /*
946
     * Some users replace the dictionary directly in the context struct.
947
     * We really need an API function to do that cleanly.
948
     */
949
0
    ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
950
0
    ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
951
0
    ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
952
0
    if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
953
0
    (ctxt->str_xml_ns == NULL)) {
954
0
        xmlErrMemory(ctxt);
955
0
    }
956
957
0
    xmlDictSetLimit(ctxt->dict,
958
0
                    (ctxt->options & XML_PARSE_HUGE) ?
959
0
                        0 :
960
0
                        XML_MAX_DICTIONARY_LIMIT);
961
962
0
#ifdef LIBXML_VALID_ENABLED
963
0
    if (ctxt->validate)
964
0
        ctxt->vctxt.flags |= XML_VCTXT_VALIDATE;
965
0
    else
966
0
        ctxt->vctxt.flags &= ~XML_VCTXT_VALIDATE;
967
0
#endif /* LIBXML_VALID_ENABLED */
968
0
}
969
970
typedef struct {
971
    xmlHashedString prefix;
972
    xmlHashedString name;
973
    xmlHashedString value;
974
    const xmlChar *valueEnd;
975
    int external;
976
    int expandedSize;
977
} xmlDefAttr;
978
979
typedef struct _xmlDefAttrs xmlDefAttrs;
980
typedef xmlDefAttrs *xmlDefAttrsPtr;
981
struct _xmlDefAttrs {
982
    int nbAttrs;  /* number of defaulted attributes on that element */
983
    int maxAttrs;       /* the size of the array */
984
#if __STDC_VERSION__ >= 199901L
985
    /* Using a C99 flexible array member avoids UBSan errors. */
986
    xmlDefAttr attrs[] ATTRIBUTE_COUNTED_BY(maxAttrs);
987
#else
988
    xmlDefAttr attrs[1];
989
#endif
990
};
991
992
/**
993
 * Normalize the space in non CDATA attribute values:
994
 * If the attribute type is not CDATA, then the XML processor MUST further
995
 * process the normalized attribute value by discarding any leading and
996
 * trailing space (\#x20) characters, and by replacing sequences of space
997
 * (\#x20) characters by a single space (\#x20) character.
998
 * Note that the size of dst need to be at least src, and if one doesn't need
999
 * to preserve dst (and it doesn't come from a dictionary or read-only) then
1000
 * passing src as dst is just fine.
1001
 *
1002
 * @param src  the source string
1003
 * @param dst  the target string
1004
 * @returns a pointer to the normalized value (dst) or NULL if no conversion
1005
 *         is needed.
1006
 */
1007
static xmlChar *
1008
xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst)
1009
0
{
1010
0
    if ((src == NULL) || (dst == NULL))
1011
0
        return(NULL);
1012
1013
0
    while (*src == 0x20) src++;
1014
0
    while (*src != 0) {
1015
0
  if (*src == 0x20) {
1016
0
      while (*src == 0x20) src++;
1017
0
      if (*src != 0)
1018
0
    *dst++ = 0x20;
1019
0
  } else {
1020
0
      *dst++ = *src++;
1021
0
  }
1022
0
    }
1023
0
    *dst = 0;
1024
0
    if (dst == src)
1025
0
       return(NULL);
1026
0
    return(dst);
1027
0
}
1028
1029
/**
1030
 * Add a defaulted attribute for an element
1031
 *
1032
 * @param ctxt  an XML parser context
1033
 * @param fullname  the element fullname
1034
 * @param fullattr  the attribute fullname
1035
 * @param value  the attribute value
1036
 */
1037
static void
1038
xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
1039
               const xmlChar *fullname,
1040
               const xmlChar *fullattr,
1041
0
               const xmlChar *value) {
1042
0
    xmlDefAttrsPtr defaults;
1043
0
    xmlDefAttr *attr;
1044
0
    int len, expandedSize;
1045
0
    xmlHashedString name;
1046
0
    xmlHashedString prefix;
1047
0
    xmlHashedString hvalue;
1048
0
    const xmlChar *localname;
1049
1050
    /*
1051
     * Allows to detect attribute redefinitions
1052
     */
1053
0
    if (ctxt->attsSpecial != NULL) {
1054
0
        if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL)
1055
0
      return;
1056
0
    }
1057
1058
0
    if (ctxt->attsDefault == NULL) {
1059
0
        ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict);
1060
0
  if (ctxt->attsDefault == NULL)
1061
0
      goto mem_error;
1062
0
    }
1063
1064
    /*
1065
     * split the element name into prefix:localname , the string found
1066
     * are within the DTD and then not associated to namespace names.
1067
     */
1068
0
    localname = xmlSplitQName3(fullname, &len);
1069
0
    if (localname == NULL) {
1070
0
        name = xmlDictLookupHashed(ctxt->dict, fullname, -1);
1071
0
  prefix.name = NULL;
1072
0
    } else {
1073
0
        name = xmlDictLookupHashed(ctxt->dict, localname, -1);
1074
0
  prefix = xmlDictLookupHashed(ctxt->dict, fullname, len);
1075
0
        if (prefix.name == NULL)
1076
0
            goto mem_error;
1077
0
    }
1078
0
    if (name.name == NULL)
1079
0
        goto mem_error;
1080
1081
    /*
1082
     * make sure there is some storage
1083
     */
1084
0
    defaults = xmlHashLookup2(ctxt->attsDefault, name.name, prefix.name);
1085
0
    if ((defaults == NULL) ||
1086
0
        (defaults->nbAttrs >= defaults->maxAttrs)) {
1087
0
        xmlDefAttrsPtr temp;
1088
0
        int newSize;
1089
1090
0
        if (defaults == NULL) {
1091
0
            newSize = 4;
1092
0
        } else {
1093
0
            if ((defaults->maxAttrs >= XML_MAX_ATTRS) ||
1094
0
                ((size_t) defaults->maxAttrs >
1095
0
                     SIZE_MAX / 2 / sizeof(temp[0]) - sizeof(*defaults)))
1096
0
                goto mem_error;
1097
1098
0
            if (defaults->maxAttrs > XML_MAX_ATTRS / 2)
1099
0
                newSize = XML_MAX_ATTRS;
1100
0
            else
1101
0
                newSize = defaults->maxAttrs * 2;
1102
0
        }
1103
0
        temp = xmlRealloc(defaults,
1104
0
                          sizeof(*defaults) + newSize * sizeof(xmlDefAttr));
1105
0
  if (temp == NULL)
1106
0
      goto mem_error;
1107
0
        if (defaults == NULL)
1108
0
            temp->nbAttrs = 0;
1109
0
  temp->maxAttrs = newSize;
1110
0
        defaults = temp;
1111
0
  if (xmlHashUpdateEntry2(ctxt->attsDefault, name.name, prefix.name,
1112
0
                          defaults, NULL) < 0) {
1113
0
      xmlFree(defaults);
1114
0
      goto mem_error;
1115
0
  }
1116
0
    }
1117
1118
    /*
1119
     * Split the attribute name into prefix:localname , the string found
1120
     * are within the DTD and hen not associated to namespace names.
1121
     */
1122
0
    localname = xmlSplitQName3(fullattr, &len);
1123
0
    if (localname == NULL) {
1124
0
        name = xmlDictLookupHashed(ctxt->dict, fullattr, -1);
1125
0
  prefix.name = NULL;
1126
0
    } else {
1127
0
        name = xmlDictLookupHashed(ctxt->dict, localname, -1);
1128
0
  prefix = xmlDictLookupHashed(ctxt->dict, fullattr, len);
1129
0
        if (prefix.name == NULL)
1130
0
            goto mem_error;
1131
0
    }
1132
0
    if (name.name == NULL)
1133
0
        goto mem_error;
1134
1135
    /* intern the string and precompute the end */
1136
0
    len = strlen((const char *) value);
1137
0
    hvalue = xmlDictLookupHashed(ctxt->dict, value, len);
1138
0
    if (hvalue.name == NULL)
1139
0
        goto mem_error;
1140
1141
0
    expandedSize = strlen((const char *) name.name);
1142
0
    if (prefix.name != NULL)
1143
0
        expandedSize += strlen((const char *) prefix.name);
1144
0
    expandedSize += len;
1145
1146
0
    attr = &defaults->attrs[defaults->nbAttrs++];
1147
0
    attr->name = name;
1148
0
    attr->prefix = prefix;
1149
0
    attr->value = hvalue;
1150
0
    attr->valueEnd = hvalue.name + len;
1151
0
    attr->external = PARSER_EXTERNAL(ctxt);
1152
0
    attr->expandedSize = expandedSize;
1153
1154
0
    return;
1155
1156
0
mem_error:
1157
0
    xmlErrMemory(ctxt);
1158
0
}
1159
1160
/**
1161
 * Register this attribute type
1162
 *
1163
 * @param ctxt  an XML parser context
1164
 * @param fullname  the element fullname
1165
 * @param fullattr  the attribute fullname
1166
 * @param type  the attribute type
1167
 */
1168
static void
1169
xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
1170
      const xmlChar *fullname,
1171
      const xmlChar *fullattr,
1172
      int type)
1173
0
{
1174
0
    if (ctxt->attsSpecial == NULL) {
1175
0
        ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict);
1176
0
  if (ctxt->attsSpecial == NULL)
1177
0
      goto mem_error;
1178
0
    }
1179
1180
0
    if (PARSER_EXTERNAL(ctxt))
1181
0
        type |= XML_SPECIAL_EXTERNAL;
1182
1183
0
    if (xmlHashAdd2(ctxt->attsSpecial, fullname, fullattr,
1184
0
                    XML_INT_TO_PTR(type)) < 0)
1185
0
        goto mem_error;
1186
0
    return;
1187
1188
0
mem_error:
1189
0
    xmlErrMemory(ctxt);
1190
0
}
1191
1192
/**
1193
 * Removes CDATA attributes from the special attribute table
1194
 */
1195
static void
1196
xmlCleanSpecialAttrCallback(void *payload, void *data,
1197
                            const xmlChar *fullname, const xmlChar *fullattr,
1198
0
                            const xmlChar *unused ATTRIBUTE_UNUSED) {
1199
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data;
1200
1201
0
    if (XML_PTR_TO_INT(payload) == XML_ATTRIBUTE_CDATA) {
1202
0
        xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL);
1203
0
    }
1204
0
}
1205
1206
/**
1207
 * Trim the list of attributes defined to remove all those of type
1208
 * CDATA as they are not special. This call should be done when finishing
1209
 * to parse the DTD and before starting to parse the document root.
1210
 *
1211
 * @param ctxt  an XML parser context
1212
 */
1213
static void
1214
xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt)
1215
0
{
1216
0
    if (ctxt->attsSpecial == NULL)
1217
0
        return;
1218
1219
0
    xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt);
1220
1221
0
    if (xmlHashSize(ctxt->attsSpecial) == 0) {
1222
0
        xmlHashFree(ctxt->attsSpecial, NULL);
1223
0
        ctxt->attsSpecial = NULL;
1224
0
    }
1225
0
}
1226
1227
/**
1228
 * Checks that the value conforms to the LanguageID production:
1229
 *
1230
 * @deprecated Internal function, do not use.
1231
 *
1232
 * NOTE: this is somewhat deprecated, those productions were removed from
1233
 * the XML Second edition.
1234
 *
1235
 *     [33] LanguageID ::= Langcode ('-' Subcode)*
1236
 *     [34] Langcode ::= ISO639Code |  IanaCode |  UserCode
1237
 *     [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
1238
 *     [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
1239
 *     [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
1240
 *     [38] Subcode ::= ([a-z] | [A-Z])+
1241
 *
1242
 * The current REC reference the successors of RFC 1766, currently 5646
1243
 *
1244
 * http://www.rfc-editor.org/rfc/rfc5646.txt
1245
 *
1246
 *     langtag       = language
1247
 *                     ["-" script]
1248
 *                     ["-" region]
1249
 *                     *("-" variant)
1250
 *                     *("-" extension)
1251
 *                     ["-" privateuse]
1252
 *     language      = 2*3ALPHA            ; shortest ISO 639 code
1253
 *                     ["-" extlang]       ; sometimes followed by
1254
 *                                         ; extended language subtags
1255
 *                   / 4ALPHA              ; or reserved for future use
1256
 *                   / 5*8ALPHA            ; or registered language subtag
1257
 *
1258
 *     extlang       = 3ALPHA              ; selected ISO 639 codes
1259
 *                     *2("-" 3ALPHA)      ; permanently reserved
1260
 *
1261
 *     script        = 4ALPHA              ; ISO 15924 code
1262
 *
1263
 *     region        = 2ALPHA              ; ISO 3166-1 code
1264
 *                   / 3DIGIT              ; UN M.49 code
1265
 *
1266
 *     variant       = 5*8alphanum         ; registered variants
1267
 *                   / (DIGIT 3alphanum)
1268
 *
1269
 *     extension     = singleton 1*("-" (2*8alphanum))
1270
 *
1271
 *                                         ; Single alphanumerics
1272
 *                                         ; "x" reserved for private use
1273
 *     singleton     = DIGIT               ; 0 - 9
1274
 *                   / %x41-57             ; A - W
1275
 *                   / %x59-5A             ; Y - Z
1276
 *                   / %x61-77             ; a - w
1277
 *                   / %x79-7A             ; y - z
1278
 *
1279
 * it sounds right to still allow Irregular i-xxx IANA and user codes too
1280
 * The parser below doesn't try to cope with extension or privateuse
1281
 * that could be added but that's not interoperable anyway
1282
 *
1283
 * @param lang  pointer to the string value
1284
 * @returns 1 if correct 0 otherwise
1285
 **/
1286
int
1287
xmlCheckLanguageID(const xmlChar * lang)
1288
0
{
1289
0
    const xmlChar *cur = lang, *nxt;
1290
1291
0
    if (cur == NULL)
1292
0
        return (0);
1293
0
    if (((cur[0] == 'i') && (cur[1] == '-')) ||
1294
0
        ((cur[0] == 'I') && (cur[1] == '-')) ||
1295
0
        ((cur[0] == 'x') && (cur[1] == '-')) ||
1296
0
        ((cur[0] == 'X') && (cur[1] == '-'))) {
1297
        /*
1298
         * Still allow IANA code and user code which were coming
1299
         * from the previous version of the XML-1.0 specification
1300
         * it's deprecated but we should not fail
1301
         */
1302
0
        cur += 2;
1303
0
        while (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
1304
0
               ((cur[0] >= 'a') && (cur[0] <= 'z')))
1305
0
            cur++;
1306
0
        return(cur[0] == 0);
1307
0
    }
1308
0
    nxt = cur;
1309
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1310
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1311
0
           nxt++;
1312
0
    if (nxt - cur >= 4) {
1313
        /*
1314
         * Reserved
1315
         */
1316
0
        if ((nxt - cur > 8) || (nxt[0] != 0))
1317
0
            return(0);
1318
0
        return(1);
1319
0
    }
1320
0
    if (nxt - cur < 2)
1321
0
        return(0);
1322
    /* we got an ISO 639 code */
1323
0
    if (nxt[0] == 0)
1324
0
        return(1);
1325
0
    if (nxt[0] != '-')
1326
0
        return(0);
1327
1328
0
    nxt++;
1329
0
    cur = nxt;
1330
    /* now we can have extlang or script or region or variant */
1331
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1332
0
        goto region_m49;
1333
1334
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1335
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1336
0
           nxt++;
1337
0
    if (nxt - cur == 4)
1338
0
        goto script;
1339
0
    if (nxt - cur == 2)
1340
0
        goto region;
1341
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1342
0
        goto variant;
1343
0
    if (nxt - cur != 3)
1344
0
        return(0);
1345
    /* we parsed an extlang */
1346
0
    if (nxt[0] == 0)
1347
0
        return(1);
1348
0
    if (nxt[0] != '-')
1349
0
        return(0);
1350
1351
0
    nxt++;
1352
0
    cur = nxt;
1353
    /* now we can have script or region or variant */
1354
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1355
0
        goto region_m49;
1356
1357
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1358
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1359
0
           nxt++;
1360
0
    if (nxt - cur == 2)
1361
0
        goto region;
1362
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1363
0
        goto variant;
1364
0
    if (nxt - cur != 4)
1365
0
        return(0);
1366
    /* we parsed a script */
1367
0
script:
1368
0
    if (nxt[0] == 0)
1369
0
        return(1);
1370
0
    if (nxt[0] != '-')
1371
0
        return(0);
1372
1373
0
    nxt++;
1374
0
    cur = nxt;
1375
    /* now we can have region or variant */
1376
0
    if ((nxt[0] >= '0') && (nxt[0] <= '9'))
1377
0
        goto region_m49;
1378
1379
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1380
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1381
0
           nxt++;
1382
1383
0
    if ((nxt - cur >= 5) && (nxt - cur <= 8))
1384
0
        goto variant;
1385
0
    if (nxt - cur != 2)
1386
0
        return(0);
1387
    /* we parsed a region */
1388
0
region:
1389
0
    if (nxt[0] == 0)
1390
0
        return(1);
1391
0
    if (nxt[0] != '-')
1392
0
        return(0);
1393
1394
0
    nxt++;
1395
0
    cur = nxt;
1396
    /* now we can just have a variant */
1397
0
    while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) ||
1398
0
           ((nxt[0] >= 'a') && (nxt[0] <= 'z')))
1399
0
           nxt++;
1400
1401
0
    if ((nxt - cur < 5) || (nxt - cur > 8))
1402
0
        return(0);
1403
1404
    /* we parsed a variant */
1405
0
variant:
1406
0
    if (nxt[0] == 0)
1407
0
        return(1);
1408
0
    if (nxt[0] != '-')
1409
0
        return(0);
1410
    /* extensions and private use subtags not checked */
1411
0
    return (1);
1412
1413
0
region_m49:
1414
0
    if (((nxt[1] >= '0') && (nxt[1] <= '9')) &&
1415
0
        ((nxt[2] >= '0') && (nxt[2] <= '9'))) {
1416
0
        nxt += 3;
1417
0
        goto region;
1418
0
    }
1419
0
    return(0);
1420
0
}
1421
1422
/************************************************************************
1423
 *                  *
1424
 *    Parser stacks related functions and macros    *
1425
 *                  *
1426
 ************************************************************************/
1427
1428
static xmlChar *
1429
xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar **str);
1430
1431
/**
1432
 * Create a new namespace database.
1433
 *
1434
 * @returns the new obejct.
1435
 */
1436
xmlParserNsData *
1437
0
xmlParserNsCreate(void) {
1438
0
    xmlParserNsData *nsdb = xmlMalloc(sizeof(*nsdb));
1439
1440
0
    if (nsdb == NULL)
1441
0
        return(NULL);
1442
0
    memset(nsdb, 0, sizeof(*nsdb));
1443
0
    nsdb->defaultNsIndex = INT_MAX;
1444
1445
0
    return(nsdb);
1446
0
}
1447
1448
/**
1449
 * Free a namespace database.
1450
 *
1451
 * @param nsdb  namespace database
1452
 */
1453
void
1454
0
xmlParserNsFree(xmlParserNsData *nsdb) {
1455
0
    if (nsdb == NULL)
1456
0
        return;
1457
1458
0
    xmlFree(nsdb->extra);
1459
0
    xmlFree(nsdb->hash);
1460
0
    xmlFree(nsdb);
1461
0
}
1462
1463
/**
1464
 * Reset a namespace database.
1465
 *
1466
 * @param nsdb  namespace database
1467
 */
1468
static void
1469
9.87k
xmlParserNsReset(xmlParserNsData *nsdb) {
1470
9.87k
    if (nsdb == NULL)
1471
9.87k
        return;
1472
1473
0
    nsdb->hashElems = 0;
1474
0
    nsdb->elementId = 0;
1475
0
    nsdb->defaultNsIndex = INT_MAX;
1476
1477
0
    if (nsdb->hash)
1478
0
        memset(nsdb->hash, 0, nsdb->hashSize * sizeof(nsdb->hash[0]));
1479
0
}
1480
1481
/**
1482
 * Signal that a new element has started.
1483
 *
1484
 * @param nsdb  namespace database
1485
 * @returns 0 on success, -1 if the element counter overflowed.
1486
 */
1487
static int
1488
0
xmlParserNsStartElement(xmlParserNsData *nsdb) {
1489
0
    if (nsdb->elementId == UINT_MAX)
1490
0
        return(-1);
1491
0
    nsdb->elementId++;
1492
1493
0
    return(0);
1494
0
}
1495
1496
/**
1497
 * Lookup namespace with given prefix. If `bucketPtr` is non-NULL, it will
1498
 * be set to the matching bucket, or the first empty bucket if no match
1499
 * was found.
1500
 *
1501
 * @param ctxt  parser context
1502
 * @param prefix  namespace prefix
1503
 * @param bucketPtr  optional bucket (return value)
1504
 * @returns the namespace index on success, INT_MAX if no namespace was
1505
 * found.
1506
 */
1507
static int
1508
xmlParserNsLookup(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
1509
0
                  xmlParserNsBucket **bucketPtr) {
1510
0
    xmlParserNsBucket *bucket, *tombstone;
1511
0
    unsigned index, hashValue;
1512
1513
0
    if (prefix->name == NULL)
1514
0
        return(ctxt->nsdb->defaultNsIndex);
1515
1516
0
    if (ctxt->nsdb->hashSize == 0)
1517
0
        return(INT_MAX);
1518
1519
0
    hashValue = prefix->hashValue;
1520
0
    index = hashValue & (ctxt->nsdb->hashSize - 1);
1521
0
    bucket = &ctxt->nsdb->hash[index];
1522
0
    tombstone = NULL;
1523
1524
0
    while (bucket->hashValue) {
1525
0
        if (bucket->index == INT_MAX) {
1526
0
            if (tombstone == NULL)
1527
0
                tombstone = bucket;
1528
0
        } else if (bucket->hashValue == hashValue) {
1529
0
            if (ctxt->nsTab[bucket->index * 2] == prefix->name) {
1530
0
                if (bucketPtr != NULL)
1531
0
                    *bucketPtr = bucket;
1532
0
                return(bucket->index);
1533
0
            }
1534
0
        }
1535
1536
0
        index++;
1537
0
        bucket++;
1538
0
        if (index == ctxt->nsdb->hashSize) {
1539
0
            index = 0;
1540
0
            bucket = ctxt->nsdb->hash;
1541
0
        }
1542
0
    }
1543
1544
0
    if (bucketPtr != NULL)
1545
0
        *bucketPtr = tombstone ? tombstone : bucket;
1546
0
    return(INT_MAX);
1547
0
}
1548
1549
/**
1550
 * Lookup namespace URI with given prefix.
1551
 *
1552
 * @param ctxt  parser context
1553
 * @param prefix  namespace prefix
1554
 * @returns the namespace URI on success, NULL if no namespace was found.
1555
 */
1556
static const xmlChar *
1557
0
xmlParserNsLookupUri(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix) {
1558
0
    const xmlChar *ret;
1559
0
    int nsIndex;
1560
1561
0
    if (prefix->name == ctxt->str_xml)
1562
0
        return(ctxt->str_xml_ns);
1563
1564
    /*
1565
     * minNsIndex is used when building an entity tree. We must
1566
     * ignore namespaces declared outside the entity.
1567
     */
1568
0
    nsIndex = xmlParserNsLookup(ctxt, prefix, NULL);
1569
0
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1570
0
        return(NULL);
1571
1572
0
    ret = ctxt->nsTab[nsIndex * 2 + 1];
1573
0
    if (ret[0] == 0)
1574
0
        ret = NULL;
1575
0
    return(ret);
1576
0
}
1577
1578
/**
1579
 * Lookup extra data for the given prefix. This returns data stored
1580
 * with xmlParserNsUdpateSax.
1581
 *
1582
 * @param ctxt  parser context
1583
 * @param prefix  namespace prefix
1584
 * @returns the data on success, NULL if no namespace was found.
1585
 */
1586
void *
1587
0
xmlParserNsLookupSax(xmlParserCtxt *ctxt, const xmlChar *prefix) {
1588
0
    xmlHashedString hprefix;
1589
0
    int nsIndex;
1590
1591
0
    if (prefix == ctxt->str_xml)
1592
0
        return(NULL);
1593
1594
0
    hprefix.name = prefix;
1595
0
    if (prefix != NULL)
1596
0
        hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
1597
0
    else
1598
0
        hprefix.hashValue = 0;
1599
0
    nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
1600
0
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1601
0
        return(NULL);
1602
1603
0
    return(ctxt->nsdb->extra[nsIndex].saxData);
1604
0
}
1605
1606
/**
1607
 * Sets or updates extra data for the given prefix. This value will be
1608
 * returned by xmlParserNsLookupSax as long as the namespace with the
1609
 * given prefix is in scope.
1610
 *
1611
 * @param ctxt  parser context
1612
 * @param prefix  namespace prefix
1613
 * @param saxData  extra data for SAX handler
1614
 * @returns the data on success, NULL if no namespace was found.
1615
 */
1616
int
1617
xmlParserNsUpdateSax(xmlParserCtxt *ctxt, const xmlChar *prefix,
1618
0
                     void *saxData) {
1619
0
    xmlHashedString hprefix;
1620
0
    int nsIndex;
1621
1622
0
    if (prefix == ctxt->str_xml)
1623
0
        return(-1);
1624
1625
0
    hprefix.name = prefix;
1626
0
    if (prefix != NULL)
1627
0
        hprefix.hashValue = xmlDictComputeHash(ctxt->dict, prefix);
1628
0
    else
1629
0
        hprefix.hashValue = 0;
1630
0
    nsIndex = xmlParserNsLookup(ctxt, &hprefix, NULL);
1631
0
    if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex))
1632
0
        return(-1);
1633
1634
0
    ctxt->nsdb->extra[nsIndex].saxData = saxData;
1635
0
    return(0);
1636
0
}
1637
1638
/**
1639
 * Grows the namespace tables.
1640
 *
1641
 * @param ctxt  parser context
1642
 * @returns 0 on success, -1 if a memory allocation failed.
1643
 */
1644
static int
1645
0
xmlParserNsGrow(xmlParserCtxtPtr ctxt) {
1646
0
    const xmlChar **table;
1647
0
    xmlParserNsExtra *extra;
1648
0
    int newSize;
1649
1650
0
    newSize = xmlGrowCapacity(ctxt->nsMax,
1651
0
                              sizeof(table[0]) + sizeof(extra[0]),
1652
0
                              16, XML_MAX_ITEMS);
1653
0
    if (newSize < 0)
1654
0
        goto error;
1655
1656
0
    table = xmlRealloc(ctxt->nsTab, 2 * newSize * sizeof(table[0]));
1657
0
    if (table == NULL)
1658
0
        goto error;
1659
0
    ctxt->nsTab = table;
1660
1661
0
    extra = xmlRealloc(ctxt->nsdb->extra, newSize * sizeof(extra[0]));
1662
0
    if (extra == NULL)
1663
0
        goto error;
1664
0
    ctxt->nsdb->extra = extra;
1665
1666
0
    ctxt->nsMax = newSize;
1667
0
    return(0);
1668
1669
0
error:
1670
0
    xmlErrMemory(ctxt);
1671
0
    return(-1);
1672
0
}
1673
1674
/**
1675
 * Push a new namespace on the table.
1676
 *
1677
 * @param ctxt  parser context
1678
 * @param prefix  prefix with hash value
1679
 * @param uri  uri with hash value
1680
 * @param saxData  extra data for SAX handler
1681
 * @param defAttr  whether the namespace comes from a default attribute
1682
 * @returns 1 if the namespace was pushed, 0 if the namespace was ignored,
1683
 * -1 if a memory allocation failed.
1684
 */
1685
static int
1686
xmlParserNsPush(xmlParserCtxtPtr ctxt, const xmlHashedString *prefix,
1687
0
                const xmlHashedString *uri, void *saxData, int defAttr) {
1688
0
    xmlParserNsBucket *bucket = NULL;
1689
0
    xmlParserNsExtra *extra;
1690
0
    const xmlChar **ns;
1691
0
    unsigned hashValue, nsIndex, oldIndex;
1692
1693
0
    if ((prefix != NULL) && (prefix->name == ctxt->str_xml))
1694
0
        return(0);
1695
1696
0
    if ((ctxt->nsNr >= ctxt->nsMax) && (xmlParserNsGrow(ctxt) < 0)) {
1697
0
        xmlErrMemory(ctxt);
1698
0
        return(-1);
1699
0
    }
1700
1701
    /*
1702
     * Default namespace and 'xml' namespace
1703
     */
1704
0
    if ((prefix == NULL) || (prefix->name == NULL)) {
1705
0
        oldIndex = ctxt->nsdb->defaultNsIndex;
1706
1707
0
        if (oldIndex != INT_MAX) {
1708
0
            extra = &ctxt->nsdb->extra[oldIndex];
1709
1710
0
            if (extra->elementId == ctxt->nsdb->elementId) {
1711
0
                if (defAttr == 0)
1712
0
                    xmlErrAttributeDup(ctxt, NULL, BAD_CAST "xmlns");
1713
0
                return(0);
1714
0
            }
1715
1716
0
            if ((ctxt->options & XML_PARSE_NSCLEAN) &&
1717
0
                (uri->name == ctxt->nsTab[oldIndex * 2 + 1]))
1718
0
                return(0);
1719
0
        }
1720
1721
0
        ctxt->nsdb->defaultNsIndex = ctxt->nsNr;
1722
0
        goto populate_entry;
1723
0
    }
1724
1725
    /*
1726
     * Hash table lookup
1727
     */
1728
0
    oldIndex = xmlParserNsLookup(ctxt, prefix, &bucket);
1729
0
    if (oldIndex != INT_MAX) {
1730
0
        extra = &ctxt->nsdb->extra[oldIndex];
1731
1732
        /*
1733
         * Check for duplicate definitions on the same element.
1734
         */
1735
0
        if (extra->elementId == ctxt->nsdb->elementId) {
1736
0
            if (defAttr == 0)
1737
0
                xmlErrAttributeDup(ctxt, BAD_CAST "xmlns", prefix->name);
1738
0
            return(0);
1739
0
        }
1740
1741
0
        if ((ctxt->options & XML_PARSE_NSCLEAN) &&
1742
0
            (uri->name == ctxt->nsTab[bucket->index * 2 + 1]))
1743
0
            return(0);
1744
1745
0
        bucket->index = ctxt->nsNr;
1746
0
        goto populate_entry;
1747
0
    }
1748
1749
    /*
1750
     * Insert new bucket
1751
     */
1752
1753
0
    hashValue = prefix->hashValue;
1754
1755
    /*
1756
     * Grow hash table, 50% fill factor
1757
     */
1758
0
    if (ctxt->nsdb->hashElems + 1 > ctxt->nsdb->hashSize / 2) {
1759
0
        xmlParserNsBucket *newHash;
1760
0
        unsigned newSize, i, index;
1761
1762
0
        if (ctxt->nsdb->hashSize > UINT_MAX / 2) {
1763
0
            xmlErrMemory(ctxt);
1764
0
            return(-1);
1765
0
        }
1766
0
        newSize = ctxt->nsdb->hashSize ? ctxt->nsdb->hashSize * 2 : 16;
1767
0
        newHash = xmlMalloc(newSize * sizeof(newHash[0]));
1768
0
        if (newHash == NULL) {
1769
0
            xmlErrMemory(ctxt);
1770
0
            return(-1);
1771
0
        }
1772
0
        memset(newHash, 0, newSize * sizeof(newHash[0]));
1773
1774
0
        for (i = 0; i < ctxt->nsdb->hashSize; i++) {
1775
0
            unsigned hv = ctxt->nsdb->hash[i].hashValue;
1776
0
            unsigned newIndex;
1777
1778
0
            if ((hv == 0) || (ctxt->nsdb->hash[i].index == INT_MAX))
1779
0
                continue;
1780
0
            newIndex = hv & (newSize - 1);
1781
1782
0
            while (newHash[newIndex].hashValue != 0) {
1783
0
                newIndex++;
1784
0
                if (newIndex == newSize)
1785
0
                    newIndex = 0;
1786
0
            }
1787
1788
0
            newHash[newIndex] = ctxt->nsdb->hash[i];
1789
0
        }
1790
1791
0
        xmlFree(ctxt->nsdb->hash);
1792
0
        ctxt->nsdb->hash = newHash;
1793
0
        ctxt->nsdb->hashSize = newSize;
1794
1795
        /*
1796
         * Relookup
1797
         */
1798
0
        index = hashValue & (newSize - 1);
1799
1800
0
        while (newHash[index].hashValue != 0) {
1801
0
            index++;
1802
0
            if (index == newSize)
1803
0
                index = 0;
1804
0
        }
1805
1806
0
        bucket = &newHash[index];
1807
0
    }
1808
1809
0
    bucket->hashValue = hashValue;
1810
0
    bucket->index = ctxt->nsNr;
1811
0
    ctxt->nsdb->hashElems++;
1812
0
    oldIndex = INT_MAX;
1813
1814
0
populate_entry:
1815
0
    nsIndex = ctxt->nsNr;
1816
1817
0
    ns = &ctxt->nsTab[nsIndex * 2];
1818
0
    ns[0] = prefix ? prefix->name : NULL;
1819
0
    ns[1] = uri->name;
1820
1821
0
    extra = &ctxt->nsdb->extra[nsIndex];
1822
0
    extra->saxData = saxData;
1823
0
    extra->prefixHashValue = prefix ? prefix->hashValue : 0;
1824
0
    extra->uriHashValue = uri->hashValue;
1825
0
    extra->elementId = ctxt->nsdb->elementId;
1826
0
    extra->oldIndex = oldIndex;
1827
1828
0
    ctxt->nsNr++;
1829
1830
0
    return(1);
1831
0
}
1832
1833
/**
1834
 * Pops the top `nr` namespaces and restores the hash table.
1835
 *
1836
 * @param ctxt  an XML parser context
1837
 * @param nr  the number to pop
1838
 * @returns the number of namespaces popped.
1839
 */
1840
static int
1841
xmlParserNsPop(xmlParserCtxtPtr ctxt, int nr)
1842
0
{
1843
0
    int i;
1844
1845
    /* assert(nr <= ctxt->nsNr); */
1846
1847
0
    for (i = ctxt->nsNr - 1; i >= ctxt->nsNr - nr; i--) {
1848
0
        const xmlChar *prefix = ctxt->nsTab[i * 2];
1849
0
        xmlParserNsExtra *extra = &ctxt->nsdb->extra[i];
1850
1851
0
        if (prefix == NULL) {
1852
0
            ctxt->nsdb->defaultNsIndex = extra->oldIndex;
1853
0
        } else {
1854
0
            xmlHashedString hprefix;
1855
0
            xmlParserNsBucket *bucket = NULL;
1856
1857
0
            hprefix.name = prefix;
1858
0
            hprefix.hashValue = extra->prefixHashValue;
1859
0
            xmlParserNsLookup(ctxt, &hprefix, &bucket);
1860
            /* assert(bucket && bucket->hashValue); */
1861
0
            bucket->index = extra->oldIndex;
1862
0
        }
1863
0
    }
1864
1865
0
    ctxt->nsNr -= nr;
1866
0
    return(nr);
1867
0
}
1868
1869
static int
1870
0
xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt) {
1871
0
    const xmlChar **atts;
1872
0
    unsigned *attallocs;
1873
0
    int newSize;
1874
1875
0
    newSize = xmlGrowCapacity(ctxt->maxatts / 5,
1876
0
                              sizeof(atts[0]) * 5 + sizeof(attallocs[0]),
1877
0
                              10, XML_MAX_ATTRS);
1878
0
    if (newSize < 0) {
1879
0
        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
1880
0
                    "Maximum number of attributes exceeded");
1881
0
        return(-1);
1882
0
    }
1883
1884
0
    atts = xmlRealloc(ctxt->atts, newSize * sizeof(atts[0]) * 5);
1885
0
    if (atts == NULL)
1886
0
        goto mem_error;
1887
0
    ctxt->atts = atts;
1888
1889
0
    attallocs = xmlRealloc(ctxt->attallocs,
1890
0
                           newSize * sizeof(attallocs[0]));
1891
0
    if (attallocs == NULL)
1892
0
        goto mem_error;
1893
0
    ctxt->attallocs = attallocs;
1894
1895
0
    ctxt->maxatts = newSize * 5;
1896
1897
0
    return(0);
1898
1899
0
mem_error:
1900
0
    xmlErrMemory(ctxt);
1901
0
    return(-1);
1902
0
}
1903
1904
/**
1905
 * Pushes a new parser input on top of the input stack
1906
 *
1907
 * @param ctxt  an XML parser context
1908
 * @param value  the parser input
1909
 * @returns -1 in case of error, the index in the stack otherwise
1910
 */
1911
int
1912
xmlCtxtPushInput(xmlParserCtxt *ctxt, xmlParserInput *value)
1913
19.7k
{
1914
19.7k
    char *directory = NULL;
1915
19.7k
    int maxDepth;
1916
1917
19.7k
    if ((ctxt == NULL) || (value == NULL))
1918
0
        return(-1);
1919
1920
19.7k
    maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
1921
1922
19.7k
    if (ctxt->inputNr >= ctxt->inputMax) {
1923
0
        xmlParserInputPtr *tmp;
1924
0
        int newSize;
1925
1926
0
        newSize = xmlGrowCapacity(ctxt->inputMax, sizeof(tmp[0]),
1927
0
                                  5, maxDepth);
1928
0
        if (newSize < 0) {
1929
0
            xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
1930
0
                           "Maximum entity nesting depth exceeded");
1931
0
            xmlHaltParser(ctxt);
1932
0
            return(-1);
1933
0
        }
1934
0
        tmp = xmlRealloc(ctxt->inputTab, newSize * sizeof(tmp[0]));
1935
0
        if (tmp == NULL) {
1936
0
            xmlErrMemory(ctxt);
1937
0
            return(-1);
1938
0
        }
1939
0
        ctxt->inputTab = tmp;
1940
0
        ctxt->inputMax = newSize;
1941
0
    }
1942
1943
19.7k
    if ((ctxt->inputNr == 0) && (value->filename != NULL)) {
1944
0
        directory = xmlParserGetDirectory(value->filename);
1945
0
        if (directory == NULL) {
1946
0
            xmlErrMemory(ctxt);
1947
0
            return(-1);
1948
0
        }
1949
0
    }
1950
1951
19.7k
    if (ctxt->input_id >= INT_MAX) {
1952
0
        xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT, "Input ID overflow\n");
1953
0
        return(-1);
1954
0
    }
1955
1956
19.7k
    ctxt->inputTab[ctxt->inputNr] = value;
1957
19.7k
    ctxt->input = value;
1958
1959
19.7k
    if (ctxt->inputNr == 0) {
1960
19.7k
        xmlFree(ctxt->directory);
1961
19.7k
        ctxt->directory = directory;
1962
19.7k
    }
1963
1964
    /*
1965
     * The input ID is unused internally, but there are entity
1966
     * loaders in downstream code that detect the main document
1967
     * by checking for "input_id == 1".
1968
     */
1969
19.7k
    value->id = ctxt->input_id++;
1970
1971
19.7k
    return(ctxt->inputNr++);
1972
19.7k
}
1973
1974
/**
1975
 * Pops the top parser input from the input stack
1976
 *
1977
 * @param ctxt  an XML parser context
1978
 * @returns the input just removed
1979
 */
1980
xmlParserInput *
1981
xmlCtxtPopInput(xmlParserCtxt *ctxt)
1982
49.3k
{
1983
49.3k
    xmlParserInputPtr ret;
1984
1985
49.3k
    if (ctxt == NULL)
1986
0
        return(NULL);
1987
49.3k
    if (ctxt->inputNr <= 0)
1988
29.6k
        return (NULL);
1989
19.7k
    ctxt->inputNr--;
1990
19.7k
    if (ctxt->inputNr > 0)
1991
0
        ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
1992
19.7k
    else
1993
19.7k
        ctxt->input = NULL;
1994
19.7k
    ret = ctxt->inputTab[ctxt->inputNr];
1995
19.7k
    ctxt->inputTab[ctxt->inputNr] = NULL;
1996
19.7k
    return (ret);
1997
49.3k
}
1998
1999
/**
2000
 * Pushes a new element node on top of the node stack
2001
 *
2002
 * @deprecated Internal function, do not use.
2003
 *
2004
 * @param ctxt  an XML parser context
2005
 * @param value  the element node
2006
 * @returns -1 in case of error, the index in the stack otherwise
2007
 */
2008
int
2009
nodePush(xmlParserCtxt *ctxt, xmlNode *value)
2010
322k
{
2011
322k
    if (ctxt == NULL)
2012
0
        return(0);
2013
2014
322k
    if (ctxt->nodeNr >= ctxt->nodeMax) {
2015
28.7k
        int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
2016
28.7k
        xmlNodePtr *tmp;
2017
28.7k
        int newSize;
2018
2019
28.7k
        newSize = xmlGrowCapacity(ctxt->nodeMax, sizeof(tmp[0]),
2020
28.7k
                                  10, maxDepth);
2021
28.7k
        if (newSize < 0) {
2022
50
            xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
2023
50
                    "Excessive depth in document: %d,"
2024
50
                    " use XML_PARSE_HUGE option\n",
2025
50
                    ctxt->nodeNr);
2026
50
            xmlHaltParser(ctxt);
2027
50
            return(-1);
2028
50
        }
2029
2030
28.7k
  tmp = xmlRealloc(ctxt->nodeTab, newSize * sizeof(tmp[0]));
2031
28.7k
        if (tmp == NULL) {
2032
84
            xmlErrMemory(ctxt);
2033
84
            return (-1);
2034
84
        }
2035
28.6k
        ctxt->nodeTab = tmp;
2036
28.6k
  ctxt->nodeMax = newSize;
2037
28.6k
    }
2038
2039
322k
    ctxt->nodeTab[ctxt->nodeNr] = value;
2040
322k
    ctxt->node = value;
2041
322k
    return (ctxt->nodeNr++);
2042
322k
}
2043
2044
/**
2045
 * Pops the top element node from the node stack
2046
 *
2047
 * @deprecated Internal function, do not use.
2048
 *
2049
 * @param ctxt  an XML parser context
2050
 * @returns the node just removed
2051
 */
2052
xmlNode *
2053
nodePop(xmlParserCtxt *ctxt)
2054
256k
{
2055
256k
    xmlNodePtr ret;
2056
2057
256k
    if (ctxt == NULL) return(NULL);
2058
256k
    if (ctxt->nodeNr <= 0)
2059
1.85k
        return (NULL);
2060
254k
    ctxt->nodeNr--;
2061
254k
    if (ctxt->nodeNr > 0)
2062
239k
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
2063
15.1k
    else
2064
15.1k
        ctxt->node = NULL;
2065
254k
    ret = ctxt->nodeTab[ctxt->nodeNr];
2066
254k
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
2067
254k
    return (ret);
2068
256k
}
2069
2070
/**
2071
 * Pushes a new element name/prefix/URL on top of the name stack
2072
 *
2073
 * @param ctxt  an XML parser context
2074
 * @param value  the element name
2075
 * @param prefix  the element prefix
2076
 * @param URI  the element namespace name
2077
 * @param line  the current line number for error messages
2078
 * @param nsNr  the number of namespaces pushed on the namespace table
2079
 * @returns -1 in case of error, the index in the stack otherwise
2080
 */
2081
static int
2082
nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
2083
           const xmlChar *prefix, const xmlChar *URI, int line, int nsNr)
2084
0
{
2085
0
    xmlStartTag *tag;
2086
2087
0
    if (ctxt->nameNr >= ctxt->nameMax) {
2088
0
        const xmlChar **tmp;
2089
0
        xmlStartTag *tmp2;
2090
0
        int newSize;
2091
2092
0
        newSize = xmlGrowCapacity(ctxt->nameMax,
2093
0
                                  sizeof(tmp[0]) + sizeof(tmp2[0]),
2094
0
                                  10, XML_MAX_ITEMS);
2095
0
        if (newSize < 0)
2096
0
            goto mem_error;
2097
2098
0
        tmp = xmlRealloc(ctxt->nameTab, newSize * sizeof(tmp[0]));
2099
0
        if (tmp == NULL)
2100
0
      goto mem_error;
2101
0
  ctxt->nameTab = tmp;
2102
2103
0
        tmp2 = xmlRealloc(ctxt->pushTab, newSize * sizeof(tmp2[0]));
2104
0
        if (tmp2 == NULL)
2105
0
      goto mem_error;
2106
0
  ctxt->pushTab = tmp2;
2107
2108
0
        ctxt->nameMax = newSize;
2109
0
    } else if (ctxt->pushTab == NULL) {
2110
0
        ctxt->pushTab = xmlMalloc(ctxt->nameMax * sizeof(ctxt->pushTab[0]));
2111
0
        if (ctxt->pushTab == NULL)
2112
0
            goto mem_error;
2113
0
    }
2114
0
    ctxt->nameTab[ctxt->nameNr] = value;
2115
0
    ctxt->name = value;
2116
0
    tag = &ctxt->pushTab[ctxt->nameNr];
2117
0
    tag->prefix = prefix;
2118
0
    tag->URI = URI;
2119
0
    tag->line = line;
2120
0
    tag->nsNr = nsNr;
2121
0
    return (ctxt->nameNr++);
2122
0
mem_error:
2123
0
    xmlErrMemory(ctxt);
2124
0
    return (-1);
2125
0
}
2126
#ifdef LIBXML_PUSH_ENABLED
2127
/**
2128
 * Pops the top element/prefix/URI name from the name stack
2129
 *
2130
 * @param ctxt  an XML parser context
2131
 * @returns the name just removed
2132
 */
2133
static const xmlChar *
2134
nameNsPop(xmlParserCtxtPtr ctxt)
2135
0
{
2136
0
    const xmlChar *ret;
2137
2138
0
    if (ctxt->nameNr <= 0)
2139
0
        return (NULL);
2140
0
    ctxt->nameNr--;
2141
0
    if (ctxt->nameNr > 0)
2142
0
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
2143
0
    else
2144
0
        ctxt->name = NULL;
2145
0
    ret = ctxt->nameTab[ctxt->nameNr];
2146
0
    ctxt->nameTab[ctxt->nameNr] = NULL;
2147
0
    return (ret);
2148
0
}
2149
#endif /* LIBXML_PUSH_ENABLED */
2150
2151
/**
2152
 * Pops the top element name from the name stack
2153
 *
2154
 * @deprecated Internal function, do not use.
2155
 *
2156
 * @param ctxt  an XML parser context
2157
 * @returns the name just removed
2158
 */
2159
static const xmlChar *
2160
namePop(xmlParserCtxtPtr ctxt)
2161
0
{
2162
0
    const xmlChar *ret;
2163
2164
0
    if ((ctxt == NULL) || (ctxt->nameNr <= 0))
2165
0
        return (NULL);
2166
0
    ctxt->nameNr--;
2167
0
    if (ctxt->nameNr > 0)
2168
0
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
2169
0
    else
2170
0
        ctxt->name = NULL;
2171
0
    ret = ctxt->nameTab[ctxt->nameNr];
2172
0
    ctxt->nameTab[ctxt->nameNr] = NULL;
2173
0
    return (ret);
2174
0
}
2175
2176
0
static int spacePush(xmlParserCtxtPtr ctxt, int val) {
2177
0
    if (ctxt->spaceNr >= ctxt->spaceMax) {
2178
0
        int *tmp;
2179
0
        int newSize;
2180
2181
0
        newSize = xmlGrowCapacity(ctxt->spaceMax, sizeof(tmp[0]),
2182
0
                                  10, XML_MAX_ITEMS);
2183
0
        if (newSize < 0) {
2184
0
      xmlErrMemory(ctxt);
2185
0
      return(-1);
2186
0
        }
2187
2188
0
        tmp = xmlRealloc(ctxt->spaceTab, newSize * sizeof(tmp[0]));
2189
0
        if (tmp == NULL) {
2190
0
      xmlErrMemory(ctxt);
2191
0
      return(-1);
2192
0
  }
2193
0
  ctxt->spaceTab = tmp;
2194
2195
0
        ctxt->spaceMax = newSize;
2196
0
    }
2197
0
    ctxt->spaceTab[ctxt->spaceNr] = val;
2198
0
    ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
2199
0
    return(ctxt->spaceNr++);
2200
0
}
2201
2202
0
static int spacePop(xmlParserCtxtPtr ctxt) {
2203
0
    int ret;
2204
0
    if (ctxt->spaceNr <= 0) return(0);
2205
0
    ctxt->spaceNr--;
2206
0
    if (ctxt->spaceNr > 0)
2207
0
  ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
2208
0
    else
2209
0
        ctxt->space = &ctxt->spaceTab[0];
2210
0
    ret = ctxt->spaceTab[ctxt->spaceNr];
2211
0
    ctxt->spaceTab[ctxt->spaceNr] = -1;
2212
0
    return(ret);
2213
0
}
2214
2215
/*
2216
 * Macros for accessing the content. Those should be used only by the parser,
2217
 * and not exported.
2218
 *
2219
 * Dirty macros, i.e. one often need to make assumption on the context to
2220
 * use them
2221
 *
2222
 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
2223
 *           To be used with extreme caution since operations consuming
2224
 *           characters may move the input buffer to a different location !
2225
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
2226
 *           This should be used internally by the parser
2227
 *           only to compare to ASCII values otherwise it would break when
2228
 *           running with UTF-8 encoding.
2229
 *   RAW     same as CUR but in the input buffer, bypass any token
2230
 *           extraction that may have been done
2231
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
2232
 *           to compare on ASCII based substring.
2233
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
2234
 *           strings without newlines within the parser.
2235
 *   NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
2236
 *           defined char within the parser.
2237
 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
2238
 *
2239
 *   NEXT    Skip to the next character, this does the proper decoding
2240
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
2241
 *   NEXTL(l) Skip the current unicode character of l xmlChars long.
2242
 *   COPY_BUF  copy the current unicode char to the target buffer, increment
2243
 *            the index
2244
 *   GROW, SHRINK  handling of input buffers
2245
 */
2246
2247
0
#define RAW (*ctxt->input->cur)
2248
0
#define CUR (*ctxt->input->cur)
2249
0
#define NXT(val) ctxt->input->cur[(val)]
2250
0
#define CUR_PTR ctxt->input->cur
2251
0
#define BASE_PTR ctxt->input->base
2252
2253
#define CMP4( s, c1, c2, c3, c4 ) \
2254
0
  ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
2255
0
    ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
2256
#define CMP5( s, c1, c2, c3, c4, c5 ) \
2257
0
  ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
2258
#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
2259
0
  ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
2260
#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
2261
0
  ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
2262
#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
2263
0
  ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
2264
#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
2265
0
  ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
2266
0
    ((unsigned char *) s)[ 8 ] == c9 )
2267
#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
2268
0
  ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
2269
0
    ((unsigned char *) s)[ 9 ] == c10 )
2270
2271
0
#define SKIP(val) do {             \
2272
0
    ctxt->input->cur += (val),ctxt->input->col+=(val);      \
2273
0
    if (*ctxt->input->cur == 0)           \
2274
0
        xmlParserGrow(ctxt);           \
2275
0
  } while (0)
2276
2277
#define SKIPL(val) do {             \
2278
    int skipl;                \
2279
    for(skipl=0; skipl<val; skipl++) {          \
2280
  if (*(ctxt->input->cur) == '\n') {        \
2281
  ctxt->input->line++; ctxt->input->col = 1;      \
2282
  } else ctxt->input->col++;          \
2283
  ctxt->input->cur++;           \
2284
    }                 \
2285
    if (*ctxt->input->cur == 0)           \
2286
        xmlParserGrow(ctxt);            \
2287
  } while (0)
2288
2289
#define SHRINK \
2290
0
    if (!PARSER_PROGRESSIVE(ctxt)) \
2291
0
  xmlParserShrink(ctxt);
2292
2293
#define GROW \
2294
0
    if ((!PARSER_PROGRESSIVE(ctxt)) && \
2295
0
        (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
2296
0
  xmlParserGrow(ctxt);
2297
2298
0
#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
2299
2300
0
#define SKIP_BLANKS_PE xmlSkipBlankCharsPE(ctxt)
2301
2302
0
#define NEXT xmlNextChar(ctxt)
2303
2304
0
#define NEXT1 {               \
2305
0
  ctxt->input->col++;           \
2306
0
  ctxt->input->cur++;           \
2307
0
  if (*ctxt->input->cur == 0)         \
2308
0
      xmlParserGrow(ctxt);           \
2309
0
    }
2310
2311
0
#define NEXTL(l) do {             \
2312
0
    if (*(ctxt->input->cur) == '\n') {         \
2313
0
  ctxt->input->line++; ctxt->input->col = 1;      \
2314
0
    } else ctxt->input->col++;           \
2315
0
    ctxt->input->cur += l;        \
2316
0
  } while (0)
2317
2318
#define COPY_BUF(b, i, v)           \
2319
0
    if (v < 0x80) b[i++] = v;           \
2320
0
    else i += xmlCopyCharMultiByte(&b[i],v)
2321
2322
static int
2323
0
xmlCurrentCharRecover(xmlParserCtxtPtr ctxt, int *len) {
2324
0
    int c = xmlCurrentChar(ctxt, len);
2325
2326
0
    if (c == XML_INVALID_CHAR)
2327
0
        c = 0xFFFD; /* replacement character */
2328
2329
0
    return(c);
2330
0
}
2331
2332
/**
2333
 * Skip whitespace in the input stream.
2334
 *
2335
 * @deprecated Internal function, do not use.
2336
 *
2337
 * @param ctxt  the XML parser context
2338
 * @returns the number of space chars skipped
2339
 */
2340
int
2341
0
xmlSkipBlankChars(xmlParserCtxt *ctxt) {
2342
0
    const xmlChar *cur;
2343
0
    int res = 0;
2344
2345
0
    cur = ctxt->input->cur;
2346
0
    while (IS_BLANK_CH(*cur)) {
2347
0
        if (*cur == '\n') {
2348
0
            ctxt->input->line++; ctxt->input->col = 1;
2349
0
        } else {
2350
0
            ctxt->input->col++;
2351
0
        }
2352
0
        cur++;
2353
0
        if (res < INT_MAX)
2354
0
            res++;
2355
0
        if (*cur == 0) {
2356
0
            ctxt->input->cur = cur;
2357
0
            xmlParserGrow(ctxt);
2358
0
            cur = ctxt->input->cur;
2359
0
        }
2360
0
    }
2361
0
    ctxt->input->cur = cur;
2362
2363
0
    if (res > 4)
2364
0
        GROW;
2365
2366
0
    return(res);
2367
0
}
2368
2369
static void
2370
0
xmlPopPE(xmlParserCtxtPtr ctxt) {
2371
0
    unsigned long consumed;
2372
0
    xmlEntityPtr ent;
2373
2374
0
    ent = ctxt->input->entity;
2375
2376
0
    ent->flags &= ~XML_ENT_EXPANDING;
2377
2378
0
    if ((ent->flags & XML_ENT_CHECKED) == 0) {
2379
0
        int result;
2380
2381
        /*
2382
         * Read the rest of the stream in case of errors. We want
2383
         * to account for the whole entity size.
2384
         */
2385
0
        do {
2386
0
            ctxt->input->cur = ctxt->input->end;
2387
0
            xmlParserShrink(ctxt);
2388
0
            result = xmlParserGrow(ctxt);
2389
0
        } while (result > 0);
2390
2391
0
        consumed = ctxt->input->consumed;
2392
0
        xmlSaturatedAddSizeT(&consumed,
2393
0
                             ctxt->input->end - ctxt->input->base);
2394
2395
0
        xmlSaturatedAdd(&ent->expandedSize, consumed);
2396
2397
        /*
2398
         * Add to sizeentities when parsing an external entity
2399
         * for the first time.
2400
         */
2401
0
        if (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
2402
0
            xmlSaturatedAdd(&ctxt->sizeentities, consumed);
2403
0
        }
2404
2405
0
        ent->flags |= XML_ENT_CHECKED;
2406
0
    }
2407
2408
0
    xmlFreeInputStream(xmlCtxtPopInput(ctxt));
2409
2410
0
    xmlParserEntityCheck(ctxt, ent->expandedSize);
2411
2412
0
    GROW;
2413
0
}
2414
2415
/**
2416
 * Skip whitespace in the input stream, also handling parameter
2417
 * entities.
2418
 *
2419
 * @param ctxt  the XML parser context
2420
 * @returns the number of space chars skipped
2421
 */
2422
static int
2423
0
xmlSkipBlankCharsPE(xmlParserCtxtPtr ctxt) {
2424
0
    int res = 0;
2425
0
    int inParam;
2426
0
    int expandParam;
2427
2428
0
    inParam = PARSER_IN_PE(ctxt);
2429
0
    expandParam = PARSER_EXTERNAL(ctxt);
2430
2431
0
    if (!inParam && !expandParam)
2432
0
        return(xmlSkipBlankChars(ctxt));
2433
2434
    /*
2435
     * It's Okay to use CUR/NEXT here since all the blanks are on
2436
     * the ASCII range.
2437
     */
2438
0
    while (PARSER_STOPPED(ctxt) == 0) {
2439
0
        if (IS_BLANK_CH(CUR)) { /* CHECKED tstblanks.xml */
2440
0
            NEXT;
2441
0
        } else if (CUR == '%') {
2442
0
            if ((expandParam == 0) ||
2443
0
                (IS_BLANK_CH(NXT(1))) || (NXT(1) == 0))
2444
0
                break;
2445
2446
            /*
2447
             * Expand parameter entity. We continue to consume
2448
             * whitespace at the start of the entity and possible
2449
             * even consume the whole entity and pop it. We might
2450
             * even pop multiple PEs in this loop.
2451
             */
2452
0
            xmlParsePERefInternal(ctxt, 0);
2453
2454
0
            inParam = PARSER_IN_PE(ctxt);
2455
0
            expandParam = PARSER_EXTERNAL(ctxt);
2456
0
        } else if (CUR == 0) {
2457
0
            if (inParam == 0)
2458
0
                break;
2459
2460
            /*
2461
             * Don't pop parameter entities that start a markup
2462
             * declaration to detect Well-formedness constraint:
2463
             * PE Between Declarations.
2464
             */
2465
0
            if (ctxt->input->flags & XML_INPUT_MARKUP_DECL)
2466
0
                break;
2467
2468
0
            xmlPopPE(ctxt);
2469
2470
0
            inParam = PARSER_IN_PE(ctxt);
2471
0
            expandParam = PARSER_EXTERNAL(ctxt);
2472
0
        } else {
2473
0
            break;
2474
0
        }
2475
2476
        /*
2477
         * Also increase the counter when entering or exiting a PERef.
2478
         * The spec says: "When a parameter-entity reference is recognized
2479
         * in the DTD and included, its replacement text MUST be enlarged
2480
         * by the attachment of one leading and one following space (#x20)
2481
         * character."
2482
         */
2483
0
        if (res < INT_MAX)
2484
0
            res++;
2485
0
    }
2486
2487
0
    return(res);
2488
0
}
2489
2490
/************************************************************************
2491
 *                  *
2492
 *    Commodity functions to handle entities      *
2493
 *                  *
2494
 ************************************************************************/
2495
2496
/**
2497
 * @deprecated Internal function, don't use.
2498
 *
2499
 * @param ctxt  an XML parser context
2500
 * @returns the current xmlChar in the parser context
2501
 */
2502
xmlChar
2503
0
xmlPopInput(xmlParserCtxt *ctxt) {
2504
0
    xmlParserInputPtr input;
2505
2506
0
    if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0);
2507
0
    input = xmlCtxtPopInput(ctxt);
2508
0
    xmlFreeInputStream(input);
2509
0
    if (*ctxt->input->cur == 0)
2510
0
        xmlParserGrow(ctxt);
2511
0
    return(CUR);
2512
0
}
2513
2514
/**
2515
 * Push an input stream onto the stack.
2516
 *
2517
 * @deprecated Internal function, don't use.
2518
 *
2519
 * @param ctxt  an XML parser context
2520
 * @param input  an XML parser input fragment (entity, XML fragment ...).
2521
 * @returns -1 in case of error or the index in the input stack
2522
 */
2523
int
2524
0
xmlPushInput(xmlParserCtxt *ctxt, xmlParserInput *input) {
2525
0
    int ret;
2526
2527
0
    if ((ctxt == NULL) || (input == NULL))
2528
0
        return(-1);
2529
2530
0
    ret = xmlCtxtPushInput(ctxt, input);
2531
0
    if (ret >= 0)
2532
0
        GROW;
2533
0
    return(ret);
2534
0
}
2535
2536
/**
2537
 * Parse a numeric character reference. Always consumes '&'.
2538
 *
2539
 * @deprecated Internal function, don't use.
2540
 *
2541
 *     [66] CharRef ::= '&#' [0-9]+ ';' |
2542
 *                      '&#x' [0-9a-fA-F]+ ';'
2543
 *
2544
 * [ WFC: Legal Character ]
2545
 * Characters referred to using character references must match the
2546
 * production for Char.
2547
 *
2548
 * @param ctxt  an XML parser context
2549
 * @returns the value parsed (as an int), 0 in case of error
2550
 */
2551
int
2552
0
xmlParseCharRef(xmlParserCtxt *ctxt) {
2553
0
    int val = 0;
2554
0
    int count = 0;
2555
2556
    /*
2557
     * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
2558
     */
2559
0
    if ((RAW == '&') && (NXT(1) == '#') &&
2560
0
        (NXT(2) == 'x')) {
2561
0
  SKIP(3);
2562
0
  GROW;
2563
0
  while ((RAW != ';') && (PARSER_STOPPED(ctxt) == 0)) {
2564
0
      if (count++ > 20) {
2565
0
    count = 0;
2566
0
    GROW;
2567
0
      }
2568
0
      if ((RAW >= '0') && (RAW <= '9'))
2569
0
          val = val * 16 + (CUR - '0');
2570
0
      else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
2571
0
          val = val * 16 + (CUR - 'a') + 10;
2572
0
      else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
2573
0
          val = val * 16 + (CUR - 'A') + 10;
2574
0
      else {
2575
0
    xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
2576
0
    val = 0;
2577
0
    break;
2578
0
      }
2579
0
      if (val > 0x110000)
2580
0
          val = 0x110000;
2581
2582
0
      NEXT;
2583
0
      count++;
2584
0
  }
2585
0
  if (RAW == ';') {
2586
      /* on purpose to avoid reentrancy problems with NEXT and SKIP */
2587
0
      ctxt->input->col++;
2588
0
      ctxt->input->cur++;
2589
0
  }
2590
0
    } else if  ((RAW == '&') && (NXT(1) == '#')) {
2591
0
  SKIP(2);
2592
0
  GROW;
2593
0
  while (RAW != ';') { /* loop blocked by count */
2594
0
      if (count++ > 20) {
2595
0
    count = 0;
2596
0
    GROW;
2597
0
      }
2598
0
      if ((RAW >= '0') && (RAW <= '9'))
2599
0
          val = val * 10 + (CUR - '0');
2600
0
      else {
2601
0
    xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
2602
0
    val = 0;
2603
0
    break;
2604
0
      }
2605
0
      if (val > 0x110000)
2606
0
          val = 0x110000;
2607
2608
0
      NEXT;
2609
0
      count++;
2610
0
  }
2611
0
  if (RAW == ';') {
2612
      /* on purpose to avoid reentrancy problems with NEXT and SKIP */
2613
0
      ctxt->input->col++;
2614
0
      ctxt->input->cur++;
2615
0
  }
2616
0
    } else {
2617
0
        if (RAW == '&')
2618
0
            SKIP(1);
2619
0
        xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
2620
0
    }
2621
2622
    /*
2623
     * [ WFC: Legal Character ]
2624
     * Characters referred to using character references must match the
2625
     * production for Char.
2626
     */
2627
0
    if (val >= 0x110000) {
2628
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2629
0
                "xmlParseCharRef: character reference out of bounds\n",
2630
0
          val);
2631
0
        val = 0xFFFD;
2632
0
    } else if (!IS_CHAR(val)) {
2633
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2634
0
                          "xmlParseCharRef: invalid xmlChar value %d\n",
2635
0
                    val);
2636
0
    }
2637
0
    return(val);
2638
0
}
2639
2640
/**
2641
 * Parse Reference declarations, variant parsing from a string rather
2642
 * than an an input flow.
2643
 *
2644
 *     [66] CharRef ::= '&#' [0-9]+ ';' |
2645
 *                      '&#x' [0-9a-fA-F]+ ';'
2646
 *
2647
 * [ WFC: Legal Character ]
2648
 * Characters referred to using character references must match the
2649
 * production for Char.
2650
 *
2651
 * @param ctxt  an XML parser context
2652
 * @param str  a pointer to an index in the string
2653
 * @returns the value parsed (as an int), 0 in case of error, str will be
2654
 *         updated to the current value of the index
2655
 */
2656
static int
2657
0
xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
2658
0
    const xmlChar *ptr;
2659
0
    xmlChar cur;
2660
0
    int val = 0;
2661
2662
0
    if ((str == NULL) || (*str == NULL)) return(0);
2663
0
    ptr = *str;
2664
0
    cur = *ptr;
2665
0
    if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
2666
0
  ptr += 3;
2667
0
  cur = *ptr;
2668
0
  while (cur != ';') { /* Non input consuming loop */
2669
0
      if ((cur >= '0') && (cur <= '9'))
2670
0
          val = val * 16 + (cur - '0');
2671
0
      else if ((cur >= 'a') && (cur <= 'f'))
2672
0
          val = val * 16 + (cur - 'a') + 10;
2673
0
      else if ((cur >= 'A') && (cur <= 'F'))
2674
0
          val = val * 16 + (cur - 'A') + 10;
2675
0
      else {
2676
0
    xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
2677
0
    val = 0;
2678
0
    break;
2679
0
      }
2680
0
      if (val > 0x110000)
2681
0
          val = 0x110000;
2682
2683
0
      ptr++;
2684
0
      cur = *ptr;
2685
0
  }
2686
0
  if (cur == ';')
2687
0
      ptr++;
2688
0
    } else if  ((cur == '&') && (ptr[1] == '#')){
2689
0
  ptr += 2;
2690
0
  cur = *ptr;
2691
0
  while (cur != ';') { /* Non input consuming loops */
2692
0
      if ((cur >= '0') && (cur <= '9'))
2693
0
          val = val * 10 + (cur - '0');
2694
0
      else {
2695
0
    xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
2696
0
    val = 0;
2697
0
    break;
2698
0
      }
2699
0
      if (val > 0x110000)
2700
0
          val = 0x110000;
2701
2702
0
      ptr++;
2703
0
      cur = *ptr;
2704
0
  }
2705
0
  if (cur == ';')
2706
0
      ptr++;
2707
0
    } else {
2708
0
  xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
2709
0
  return(0);
2710
0
    }
2711
0
    *str = ptr;
2712
2713
    /*
2714
     * [ WFC: Legal Character ]
2715
     * Characters referred to using character references must match the
2716
     * production for Char.
2717
     */
2718
0
    if (val >= 0x110000) {
2719
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2720
0
                "xmlParseStringCharRef: character reference out of bounds\n",
2721
0
                val);
2722
0
    } else if (IS_CHAR(val)) {
2723
0
        return(val);
2724
0
    } else {
2725
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
2726
0
        "xmlParseStringCharRef: invalid xmlChar value %d\n",
2727
0
        val);
2728
0
    }
2729
0
    return(0);
2730
0
}
2731
2732
/**
2733
 *     [69] PEReference ::= '%' Name ';'
2734
 *
2735
 * @deprecated Internal function, do not use.
2736
 *
2737
 * [ WFC: No Recursion ]
2738
 * A parsed entity must not contain a recursive
2739
 * reference to itself, either directly or indirectly.
2740
 *
2741
 * [ WFC: Entity Declared ]
2742
 * In a document without any DTD, a document with only an internal DTD
2743
 * subset which contains no parameter entity references, or a document
2744
 * with "standalone='yes'", ...  ... The declaration of a parameter
2745
 * entity must precede any reference to it...
2746
 *
2747
 * [ VC: Entity Declared ]
2748
 * In a document with an external subset or external parameter entities
2749
 * with "standalone='no'", ...  ... The declaration of a parameter entity
2750
 * must precede any reference to it...
2751
 *
2752
 * [ WFC: In DTD ]
2753
 * Parameter-entity references may only appear in the DTD.
2754
 * NOTE: misleading but this is handled.
2755
 *
2756
 * A PEReference may have been detected in the current input stream
2757
 * the handling is done accordingly to
2758
 *      http://www.w3.org/TR/REC-xml#entproc
2759
 * i.e.
2760
 *   - Included in literal in entity values
2761
 *   - Included as Parameter Entity reference within DTDs
2762
 * @param ctxt  the parser context
2763
 */
2764
void
2765
0
xmlParserHandlePEReference(xmlParserCtxt *ctxt) {
2766
0
    xmlParsePERefInternal(ctxt, 0);
2767
0
}
2768
2769
/**
2770
 * @deprecated Internal function, don't use.
2771
 *
2772
 * @param ctxt  the parser context
2773
 * @param str  the input string
2774
 * @param len  the string length
2775
 * @param what  combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2776
 * @param end  an end marker xmlChar, 0 if none
2777
 * @param end2  an end marker xmlChar, 0 if none
2778
 * @param end3  an end marker xmlChar, 0 if none
2779
 * @returns A newly allocated string with the substitution done. The caller
2780
 *      must deallocate it !
2781
 */
2782
xmlChar *
2783
xmlStringLenDecodeEntities(xmlParserCtxt *ctxt, const xmlChar *str, int len,
2784
                           int what ATTRIBUTE_UNUSED,
2785
0
                           xmlChar end, xmlChar end2, xmlChar end3) {
2786
0
    if ((ctxt == NULL) || (str == NULL) || (len < 0))
2787
0
        return(NULL);
2788
2789
0
    if ((str[len] != 0) ||
2790
0
        (end != 0) || (end2 != 0) || (end3 != 0))
2791
0
        return(NULL);
2792
2793
0
    return(xmlExpandEntitiesInAttValue(ctxt, str, 0));
2794
0
}
2795
2796
/**
2797
 * @deprecated Internal function, don't use.
2798
 *
2799
 * @param ctxt  the parser context
2800
 * @param str  the input string
2801
 * @param what  combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2802
 * @param end  an end marker xmlChar, 0 if none
2803
 * @param end2  an end marker xmlChar, 0 if none
2804
 * @param end3  an end marker xmlChar, 0 if none
2805
 * @returns A newly allocated string with the substitution done. The caller
2806
 *      must deallocate it !
2807
 */
2808
xmlChar *
2809
xmlStringDecodeEntities(xmlParserCtxt *ctxt, const xmlChar *str,
2810
                        int what ATTRIBUTE_UNUSED,
2811
0
            xmlChar end, xmlChar  end2, xmlChar end3) {
2812
0
    if ((ctxt == NULL) || (str == NULL))
2813
0
        return(NULL);
2814
2815
0
    if ((end != 0) || (end2 != 0) || (end3 != 0))
2816
0
        return(NULL);
2817
2818
0
    return(xmlExpandEntitiesInAttValue(ctxt, str, 0));
2819
0
}
2820
2821
/************************************************************************
2822
 *                  *
2823
 *    Commodity functions, cleanup needed ?     *
2824
 *                  *
2825
 ************************************************************************/
2826
2827
/**
2828
 * Is this a sequence of blank chars that one can ignore ?
2829
 *
2830
 * @param ctxt  an XML parser context
2831
 * @param str  a xmlChar *
2832
 * @param len  the size of `str`
2833
 * @param blank_chars  we know the chars are blanks
2834
 * @returns 1 if ignorable 0 otherwise.
2835
 */
2836
2837
static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2838
0
                     int blank_chars) {
2839
0
    int i;
2840
0
    xmlNodePtr lastChild;
2841
2842
    /*
2843
     * Check for xml:space value.
2844
     */
2845
0
    if ((ctxt->space == NULL) || (*(ctxt->space) == 1) ||
2846
0
        (*(ctxt->space) == -2))
2847
0
  return(0);
2848
2849
    /*
2850
     * Check that the string is made of blanks
2851
     */
2852
0
    if (blank_chars == 0) {
2853
0
  for (i = 0;i < len;i++)
2854
0
      if (!(IS_BLANK_CH(str[i]))) return(0);
2855
0
    }
2856
2857
    /*
2858
     * Look if the element is mixed content in the DTD if available
2859
     */
2860
0
    if (ctxt->node == NULL) return(0);
2861
0
    if (ctxt->myDoc != NULL) {
2862
0
        xmlElementPtr elemDecl = NULL;
2863
0
        xmlDocPtr doc = ctxt->myDoc;
2864
0
        const xmlChar *prefix = NULL;
2865
2866
0
        if (ctxt->node->ns)
2867
0
            prefix = ctxt->node->ns->prefix;
2868
0
        if (doc->intSubset != NULL)
2869
0
            elemDecl = xmlHashLookup2(doc->intSubset->elements, ctxt->node->name,
2870
0
                                      prefix);
2871
0
        if ((elemDecl == NULL) && (doc->extSubset != NULL))
2872
0
            elemDecl = xmlHashLookup2(doc->extSubset->elements, ctxt->node->name,
2873
0
                                      prefix);
2874
0
        if (elemDecl != NULL) {
2875
0
            if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)
2876
0
                return(1);
2877
0
            if ((elemDecl->etype == XML_ELEMENT_TYPE_ANY) ||
2878
0
                (elemDecl->etype == XML_ELEMENT_TYPE_MIXED))
2879
0
                return(0);
2880
0
        }
2881
0
    }
2882
2883
    /*
2884
     * Otherwise, heuristic :-\
2885
     *
2886
     * When push parsing, we could be at the end of a chunk.
2887
     * This makes the look-ahead and consequently the NOBLANKS
2888
     * option unreliable.
2889
     */
2890
0
    if ((RAW != '<') && (RAW != 0xD)) return(0);
2891
0
    if ((ctxt->node->children == NULL) &&
2892
0
  (RAW == '<') && (NXT(1) == '/')) return(0);
2893
2894
0
    lastChild = xmlGetLastChild(ctxt->node);
2895
0
    if (lastChild == NULL) {
2896
0
        if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2897
0
            (ctxt->node->content != NULL)) return(0);
2898
0
    } else if (xmlNodeIsText(lastChild))
2899
0
        return(0);
2900
0
    else if ((ctxt->node->children != NULL) &&
2901
0
             (xmlNodeIsText(ctxt->node->children)))
2902
0
        return(0);
2903
0
    return(1);
2904
0
}
2905
2906
/************************************************************************
2907
 *                  *
2908
 *    Extra stuff for namespace support     *
2909
 *  Relates to http://www.w3.org/TR/WD-xml-names      *
2910
 *                  *
2911
 ************************************************************************/
2912
2913
/**
2914
 * Parse an UTF8 encoded XML qualified name string
2915
 *
2916
 * @deprecated Don't use.
2917
 *
2918
 * @param ctxt  an XML parser context
2919
 * @param name  an XML parser context
2920
 * @param prefixOut  a xmlChar **
2921
 * @returns the local part, and prefix is updated
2922
 *   to get the Prefix if any.
2923
 */
2924
2925
xmlChar *
2926
0
xmlSplitQName(xmlParserCtxt *ctxt, const xmlChar *name, xmlChar **prefixOut) {
2927
0
    xmlChar *ret;
2928
0
    const xmlChar *localname;
2929
2930
0
    localname = xmlSplitQName4(name, prefixOut);
2931
0
    if (localname == NULL) {
2932
0
        xmlCtxtErrMemory(ctxt);
2933
0
        return(NULL);
2934
0
    }
2935
2936
0
    ret = xmlStrdup(localname);
2937
0
    if (ret == NULL) {
2938
0
        xmlCtxtErrMemory(ctxt);
2939
0
        xmlFree(*prefixOut);
2940
0
    }
2941
2942
0
    return(ret);
2943
0
}
2944
2945
/************************************************************************
2946
 *                  *
2947
 *      The parser itself       *
2948
 *  Relates to http://www.w3.org/TR/REC-xml       *
2949
 *                  *
2950
 ************************************************************************/
2951
2952
/************************************************************************
2953
 *                  *
2954
 *  Routines to parse Name, NCName and NmToken      *
2955
 *                  *
2956
 ************************************************************************/
2957
2958
/*
2959
 * The two following functions are related to the change of accepted
2960
 * characters for Name and NmToken in the Revision 5 of XML-1.0
2961
 * They correspond to the modified production [4] and the new production [4a]
2962
 * changes in that revision. Also note that the macros used for the
2963
 * productions Letter, Digit, CombiningChar and Extender are not needed
2964
 * anymore.
2965
 * We still keep compatibility to pre-revision5 parsing semantic if the
2966
 * new XML_PARSE_OLD10 option is given to the parser.
2967
 */
2968
2969
static int
2970
0
xmlIsNameStartCharNew(int c) {
2971
    /*
2972
     * Use the new checks of production [4] [4a] amd [5] of the
2973
     * Update 5 of XML-1.0
2974
     */
2975
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
2976
0
        (((c >= 'a') && (c <= 'z')) ||
2977
0
         ((c >= 'A') && (c <= 'Z')) ||
2978
0
         (c == '_') || (c == ':') ||
2979
0
         ((c >= 0xC0) && (c <= 0xD6)) ||
2980
0
         ((c >= 0xD8) && (c <= 0xF6)) ||
2981
0
         ((c >= 0xF8) && (c <= 0x2FF)) ||
2982
0
         ((c >= 0x370) && (c <= 0x37D)) ||
2983
0
         ((c >= 0x37F) && (c <= 0x1FFF)) ||
2984
0
         ((c >= 0x200C) && (c <= 0x200D)) ||
2985
0
         ((c >= 0x2070) && (c <= 0x218F)) ||
2986
0
         ((c >= 0x2C00) && (c <= 0x2FEF)) ||
2987
0
         ((c >= 0x3001) && (c <= 0xD7FF)) ||
2988
0
         ((c >= 0xF900) && (c <= 0xFDCF)) ||
2989
0
         ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
2990
0
         ((c >= 0x10000) && (c <= 0xEFFFF))))
2991
0
        return(1);
2992
0
    return(0);
2993
0
}
2994
2995
static int
2996
0
xmlIsNameCharNew(int c) {
2997
    /*
2998
     * Use the new checks of production [4] [4a] amd [5] of the
2999
     * Update 5 of XML-1.0
3000
     */
3001
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3002
0
        (((c >= 'a') && (c <= 'z')) ||
3003
0
         ((c >= 'A') && (c <= 'Z')) ||
3004
0
         ((c >= '0') && (c <= '9')) || /* !start */
3005
0
         (c == '_') || (c == ':') ||
3006
0
         (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3007
0
         ((c >= 0xC0) && (c <= 0xD6)) ||
3008
0
         ((c >= 0xD8) && (c <= 0xF6)) ||
3009
0
         ((c >= 0xF8) && (c <= 0x2FF)) ||
3010
0
         ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3011
0
         ((c >= 0x370) && (c <= 0x37D)) ||
3012
0
         ((c >= 0x37F) && (c <= 0x1FFF)) ||
3013
0
         ((c >= 0x200C) && (c <= 0x200D)) ||
3014
0
         ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3015
0
         ((c >= 0x2070) && (c <= 0x218F)) ||
3016
0
         ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3017
0
         ((c >= 0x3001) && (c <= 0xD7FF)) ||
3018
0
         ((c >= 0xF900) && (c <= 0xFDCF)) ||
3019
0
         ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3020
0
         ((c >= 0x10000) && (c <= 0xEFFFF))))
3021
0
         return(1);
3022
0
    return(0);
3023
0
}
3024
3025
static int
3026
0
xmlIsNameStartCharOld(int c) {
3027
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3028
0
        ((IS_LETTER(c) || (c == '_') || (c == ':'))))
3029
0
        return(1);
3030
0
    return(0);
3031
0
}
3032
3033
static int
3034
0
xmlIsNameCharOld(int c) {
3035
0
    if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */
3036
0
        ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3037
0
         (c == '.') || (c == '-') ||
3038
0
         (c == '_') || (c == ':') ||
3039
0
         (IS_COMBINING(c)) ||
3040
0
         (IS_EXTENDER(c))))
3041
0
        return(1);
3042
0
    return(0);
3043
0
}
3044
3045
static int
3046
0
xmlIsNameStartChar(int c, int old10) {
3047
0
    if (!old10)
3048
0
        return(xmlIsNameStartCharNew(c));
3049
0
    else
3050
0
        return(xmlIsNameStartCharOld(c));
3051
0
}
3052
3053
static int
3054
0
xmlIsNameChar(int c, int old10) {
3055
0
    if (!old10)
3056
0
        return(xmlIsNameCharNew(c));
3057
0
    else
3058
0
        return(xmlIsNameCharOld(c));
3059
0
}
3060
3061
/*
3062
 * Scan an XML Name, NCName or Nmtoken.
3063
 *
3064
 * Returns a pointer to the end of the name on success. If the
3065
 * name is invalid, returns `ptr`. If the name is longer than
3066
 * `maxSize` bytes, returns NULL.
3067
 *
3068
 * @param ptr  pointer to the start of the name
3069
 * @param maxSize  maximum size in bytes
3070
 * @param flags  XML_SCAN_* flags
3071
 * @returns a pointer to the end of the name or NULL
3072
 */
3073
const xmlChar *
3074
0
xmlScanName(const xmlChar *ptr, size_t maxSize, int flags) {
3075
0
    int stop = flags & XML_SCAN_NC ? ':' : 0;
3076
0
    int old10 = flags & XML_SCAN_OLD10 ? 1 : 0;
3077
3078
0
    while (1) {
3079
0
        int c, len;
3080
3081
0
        c = *ptr;
3082
0
        if (c < 0x80) {
3083
0
            if (c == stop)
3084
0
                break;
3085
0
            len = 1;
3086
0
        } else {
3087
0
            len = 4;
3088
0
            c = xmlGetUTF8Char(ptr, &len);
3089
0
            if (c < 0)
3090
0
                break;
3091
0
        }
3092
3093
0
        if (flags & XML_SCAN_NMTOKEN ?
3094
0
                !xmlIsNameChar(c, old10) :
3095
0
                !xmlIsNameStartChar(c, old10))
3096
0
            break;
3097
3098
0
        if ((size_t) len > maxSize)
3099
0
            return(NULL);
3100
0
        ptr += len;
3101
0
        maxSize -= len;
3102
0
        flags |= XML_SCAN_NMTOKEN;
3103
0
    }
3104
3105
0
    return(ptr);
3106
0
}
3107
3108
static const xmlChar *
3109
0
xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
3110
0
    const xmlChar *ret;
3111
0
    int len = 0, l;
3112
0
    int c;
3113
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3114
0
                    XML_MAX_TEXT_LENGTH :
3115
0
                    XML_MAX_NAME_LENGTH;
3116
0
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3117
3118
    /*
3119
     * Handler for more complex cases
3120
     */
3121
0
    c = xmlCurrentChar(ctxt, &l);
3122
0
    if (!xmlIsNameStartChar(c, old10))
3123
0
        return(NULL);
3124
0
    len += l;
3125
0
    NEXTL(l);
3126
0
    c = xmlCurrentChar(ctxt, &l);
3127
0
    while (xmlIsNameChar(c, old10)) {
3128
0
        if (len <= INT_MAX - l)
3129
0
            len += l;
3130
0
        NEXTL(l);
3131
0
        c = xmlCurrentChar(ctxt, &l);
3132
0
    }
3133
0
    if (len > maxLength) {
3134
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
3135
0
        return(NULL);
3136
0
    }
3137
0
    if (ctxt->input->cur - ctxt->input->base < len) {
3138
        /*
3139
         * There were a couple of bugs where PERefs lead to to a change
3140
         * of the buffer. Check the buffer size to avoid passing an invalid
3141
         * pointer to xmlDictLookup.
3142
         */
3143
0
        xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
3144
0
                    "unexpected change of input buffer");
3145
0
        return (NULL);
3146
0
    }
3147
0
    if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r'))
3148
0
        ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len);
3149
0
    else
3150
0
        ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len);
3151
0
    if (ret == NULL)
3152
0
        xmlErrMemory(ctxt);
3153
0
    return(ret);
3154
0
}
3155
3156
/**
3157
 * Parse an XML name.
3158
 *
3159
 * @deprecated Internal function, don't use.
3160
 *
3161
 *     [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
3162
 *                      CombiningChar | Extender
3163
 *
3164
 *     [5] Name ::= (Letter | '_' | ':') (NameChar)*
3165
 *
3166
 *     [6] Names ::= Name (#x20 Name)*
3167
 *
3168
 * @param ctxt  an XML parser context
3169
 * @returns the Name parsed or NULL
3170
 */
3171
3172
const xmlChar *
3173
0
xmlParseName(xmlParserCtxt *ctxt) {
3174
0
    const xmlChar *in;
3175
0
    const xmlChar *ret;
3176
0
    size_t count = 0;
3177
0
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3178
0
                       XML_MAX_TEXT_LENGTH :
3179
0
                       XML_MAX_NAME_LENGTH;
3180
3181
0
    GROW;
3182
3183
    /*
3184
     * Accelerator for simple ASCII names
3185
     */
3186
0
    in = ctxt->input->cur;
3187
0
    if (((*in >= 0x61) && (*in <= 0x7A)) ||
3188
0
  ((*in >= 0x41) && (*in <= 0x5A)) ||
3189
0
  (*in == '_') || (*in == ':')) {
3190
0
  in++;
3191
0
  while (((*in >= 0x61) && (*in <= 0x7A)) ||
3192
0
         ((*in >= 0x41) && (*in <= 0x5A)) ||
3193
0
         ((*in >= 0x30) && (*in <= 0x39)) ||
3194
0
         (*in == '_') || (*in == '-') ||
3195
0
         (*in == ':') || (*in == '.'))
3196
0
      in++;
3197
0
  if ((*in > 0) && (*in < 0x80)) {
3198
0
      count = in - ctxt->input->cur;
3199
0
            if (count > maxLength) {
3200
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name");
3201
0
                return(NULL);
3202
0
            }
3203
0
      ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
3204
0
      ctxt->input->cur = in;
3205
0
      ctxt->input->col += count;
3206
0
      if (ret == NULL)
3207
0
          xmlErrMemory(ctxt);
3208
0
      return(ret);
3209
0
  }
3210
0
    }
3211
    /* accelerator for special cases */
3212
0
    return(xmlParseNameComplex(ctxt));
3213
0
}
3214
3215
static xmlHashedString
3216
0
xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
3217
0
    xmlHashedString ret;
3218
0
    int len = 0, l;
3219
0
    int c;
3220
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3221
0
                    XML_MAX_TEXT_LENGTH :
3222
0
                    XML_MAX_NAME_LENGTH;
3223
0
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3224
0
    size_t startPosition = 0;
3225
3226
0
    ret.name = NULL;
3227
0
    ret.hashValue = 0;
3228
3229
    /*
3230
     * Handler for more complex cases
3231
     */
3232
0
    startPosition = CUR_PTR - BASE_PTR;
3233
0
    c = xmlCurrentChar(ctxt, &l);
3234
0
    if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
3235
0
  (!xmlIsNameStartChar(c, old10) || (c == ':'))) {
3236
0
  return(ret);
3237
0
    }
3238
3239
0
    while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
3240
0
     (xmlIsNameChar(c, old10) && (c != ':'))) {
3241
0
        if (len <= INT_MAX - l)
3242
0
      len += l;
3243
0
  NEXTL(l);
3244
0
  c = xmlCurrentChar(ctxt, &l);
3245
0
    }
3246
0
    if (len > maxLength) {
3247
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3248
0
        return(ret);
3249
0
    }
3250
0
    ret = xmlDictLookupHashed(ctxt->dict, (BASE_PTR + startPosition), len);
3251
0
    if (ret.name == NULL)
3252
0
        xmlErrMemory(ctxt);
3253
0
    return(ret);
3254
0
}
3255
3256
/**
3257
 * Parse an XML name.
3258
 *
3259
 *     [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
3260
 *                          CombiningChar | Extender
3261
 *
3262
 *     [5NS] NCName ::= (Letter | '_') (NCNameChar)*
3263
 *
3264
 * @param ctxt  an XML parser context
3265
 * @returns the Name parsed or NULL
3266
 */
3267
3268
static xmlHashedString
3269
0
xmlParseNCName(xmlParserCtxtPtr ctxt) {
3270
0
    const xmlChar *in, *e;
3271
0
    xmlHashedString ret;
3272
0
    size_t count = 0;
3273
0
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3274
0
                       XML_MAX_TEXT_LENGTH :
3275
0
                       XML_MAX_NAME_LENGTH;
3276
3277
0
    ret.name = NULL;
3278
3279
    /*
3280
     * Accelerator for simple ASCII names
3281
     */
3282
0
    in = ctxt->input->cur;
3283
0
    e = ctxt->input->end;
3284
0
    if ((((*in >= 0x61) && (*in <= 0x7A)) ||
3285
0
   ((*in >= 0x41) && (*in <= 0x5A)) ||
3286
0
   (*in == '_')) && (in < e)) {
3287
0
  in++;
3288
0
  while ((((*in >= 0x61) && (*in <= 0x7A)) ||
3289
0
          ((*in >= 0x41) && (*in <= 0x5A)) ||
3290
0
          ((*in >= 0x30) && (*in <= 0x39)) ||
3291
0
          (*in == '_') || (*in == '-') ||
3292
0
          (*in == '.')) && (in < e))
3293
0
      in++;
3294
0
  if (in >= e)
3295
0
      goto complex;
3296
0
  if ((*in > 0) && (*in < 0x80)) {
3297
0
      count = in - ctxt->input->cur;
3298
0
            if (count > maxLength) {
3299
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3300
0
                return(ret);
3301
0
            }
3302
0
      ret = xmlDictLookupHashed(ctxt->dict, ctxt->input->cur, count);
3303
0
      ctxt->input->cur = in;
3304
0
      ctxt->input->col += count;
3305
0
      if (ret.name == NULL) {
3306
0
          xmlErrMemory(ctxt);
3307
0
      }
3308
0
      return(ret);
3309
0
  }
3310
0
    }
3311
0
complex:
3312
0
    return(xmlParseNCNameComplex(ctxt));
3313
0
}
3314
3315
/**
3316
 * Parse an XML name and compares for match
3317
 * (specialized for endtag parsing)
3318
 *
3319
 * @param ctxt  an XML parser context
3320
 * @param other  the name to compare with
3321
 * @returns NULL for an illegal name, (xmlChar*) 1 for success
3322
 * and the name for mismatch
3323
 */
3324
3325
static const xmlChar *
3326
0
xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
3327
0
    register const xmlChar *cmp = other;
3328
0
    register const xmlChar *in;
3329
0
    const xmlChar *ret;
3330
3331
0
    GROW;
3332
3333
0
    in = ctxt->input->cur;
3334
0
    while (*in != 0 && *in == *cmp) {
3335
0
  ++in;
3336
0
  ++cmp;
3337
0
    }
3338
0
    if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
3339
  /* success */
3340
0
  ctxt->input->col += in - ctxt->input->cur;
3341
0
  ctxt->input->cur = in;
3342
0
  return (const xmlChar*) 1;
3343
0
    }
3344
    /* failure (or end of input buffer), check with full function */
3345
0
    ret = xmlParseName (ctxt);
3346
    /* strings coming from the dictionary direct compare possible */
3347
0
    if (ret == other) {
3348
0
  return (const xmlChar*) 1;
3349
0
    }
3350
0
    return ret;
3351
0
}
3352
3353
/**
3354
 * Parse an XML name.
3355
 *
3356
 * @param ctxt  an XML parser context
3357
 * @param str  a pointer to the string pointer (IN/OUT)
3358
 * @returns the Name parsed or NULL. The `str` pointer
3359
 * is updated to the current location in the string.
3360
 */
3361
3362
static xmlChar *
3363
0
xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
3364
0
    xmlChar *ret;
3365
0
    const xmlChar *cur = *str;
3366
0
    int flags = 0;
3367
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3368
0
                    XML_MAX_TEXT_LENGTH :
3369
0
                    XML_MAX_NAME_LENGTH;
3370
3371
0
    if (ctxt->options & XML_PARSE_OLD10)
3372
0
        flags |= XML_SCAN_OLD10;
3373
3374
0
    cur = xmlScanName(*str, maxLength, flags);
3375
0
    if (cur == NULL) {
3376
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName");
3377
0
        return(NULL);
3378
0
    }
3379
0
    if (cur == *str)
3380
0
        return(NULL);
3381
3382
0
    ret = xmlStrndup(*str, cur - *str);
3383
0
    if (ret == NULL)
3384
0
        xmlErrMemory(ctxt);
3385
0
    *str = cur;
3386
0
    return(ret);
3387
0
}
3388
3389
/**
3390
 * Parse an XML Nmtoken.
3391
 *
3392
 * @deprecated Internal function, don't use.
3393
 *
3394
 *     [7] Nmtoken ::= (NameChar)+
3395
 *
3396
 *     [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
3397
 *
3398
 * @param ctxt  an XML parser context
3399
 * @returns the Nmtoken parsed or NULL
3400
 */
3401
3402
xmlChar *
3403
0
xmlParseNmtoken(xmlParserCtxt *ctxt) {
3404
0
    xmlChar buf[XML_MAX_NAMELEN + 5];
3405
0
    xmlChar *ret;
3406
0
    int len = 0, l;
3407
0
    int c;
3408
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3409
0
                    XML_MAX_TEXT_LENGTH :
3410
0
                    XML_MAX_NAME_LENGTH;
3411
0
    int old10 = (ctxt->options & XML_PARSE_OLD10) ? 1 : 0;
3412
3413
0
    c = xmlCurrentChar(ctxt, &l);
3414
3415
0
    while (xmlIsNameChar(c, old10)) {
3416
0
  COPY_BUF(buf, len, c);
3417
0
  NEXTL(l);
3418
0
  c = xmlCurrentChar(ctxt, &l);
3419
0
  if (len >= XML_MAX_NAMELEN) {
3420
      /*
3421
       * Okay someone managed to make a huge token, so he's ready to pay
3422
       * for the processing speed.
3423
       */
3424
0
      xmlChar *buffer;
3425
0
      int max = len * 2;
3426
3427
0
      buffer = xmlMalloc(max);
3428
0
      if (buffer == NULL) {
3429
0
          xmlErrMemory(ctxt);
3430
0
    return(NULL);
3431
0
      }
3432
0
      memcpy(buffer, buf, len);
3433
0
      while (xmlIsNameChar(c, old10)) {
3434
0
    if (len + 10 > max) {
3435
0
        xmlChar *tmp;
3436
0
                    int newSize;
3437
3438
0
                    newSize = xmlGrowCapacity(max, 1, 1, maxLength);
3439
0
                    if (newSize < 0) {
3440
0
                        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
3441
0
                        xmlFree(buffer);
3442
0
                        return(NULL);
3443
0
                    }
3444
0
        tmp = xmlRealloc(buffer, newSize);
3445
0
        if (tmp == NULL) {
3446
0
      xmlErrMemory(ctxt);
3447
0
      xmlFree(buffer);
3448
0
      return(NULL);
3449
0
        }
3450
0
        buffer = tmp;
3451
0
                    max = newSize;
3452
0
    }
3453
0
    COPY_BUF(buffer, len, c);
3454
0
    NEXTL(l);
3455
0
    c = xmlCurrentChar(ctxt, &l);
3456
0
      }
3457
0
      buffer[len] = 0;
3458
0
      return(buffer);
3459
0
  }
3460
0
    }
3461
0
    if (len == 0)
3462
0
        return(NULL);
3463
0
    if (len > maxLength) {
3464
0
        xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken");
3465
0
        return(NULL);
3466
0
    }
3467
0
    ret = xmlStrndup(buf, len);
3468
0
    if (ret == NULL)
3469
0
        xmlErrMemory(ctxt);
3470
0
    return(ret);
3471
0
}
3472
3473
/**
3474
 * Validate an entity value and expand parameter entities.
3475
 *
3476
 * @param ctxt  parser context
3477
 * @param buf  string buffer
3478
 * @param str  entity value
3479
 * @param length  size of entity value
3480
 * @param depth  nesting depth
3481
 */
3482
static void
3483
xmlExpandPEsInEntityValue(xmlParserCtxtPtr ctxt, xmlSBuf *buf,
3484
0
                          const xmlChar *str, int length, int depth) {
3485
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3486
0
    const xmlChar *end, *chunk;
3487
0
    int c, l;
3488
3489
0
    if (str == NULL)
3490
0
        return;
3491
3492
0
    depth += 1;
3493
0
    if (depth > maxDepth) {
3494
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3495
0
                       "Maximum entity nesting depth exceeded");
3496
0
  return;
3497
0
    }
3498
3499
0
    end = str + length;
3500
0
    chunk = str;
3501
3502
0
    while ((str < end) && (!PARSER_STOPPED(ctxt))) {
3503
0
        c = *str;
3504
3505
0
        if (c >= 0x80) {
3506
0
            l = xmlUTF8MultibyteLen(ctxt, str,
3507
0
                    "invalid character in entity value\n");
3508
0
            if (l == 0) {
3509
0
                if (chunk < str)
3510
0
                    xmlSBufAddString(buf, chunk, str - chunk);
3511
0
                xmlSBufAddReplChar(buf);
3512
0
                str += 1;
3513
0
                chunk = str;
3514
0
            } else {
3515
0
                str += l;
3516
0
            }
3517
0
        } else if (c == '&') {
3518
0
            if (str[1] == '#') {
3519
0
                if (chunk < str)
3520
0
                    xmlSBufAddString(buf, chunk, str - chunk);
3521
3522
0
                c = xmlParseStringCharRef(ctxt, &str);
3523
0
                if (c == 0)
3524
0
                    return;
3525
3526
0
                xmlSBufAddChar(buf, c);
3527
3528
0
                chunk = str;
3529
0
            } else {
3530
0
                xmlChar *name;
3531
3532
                /*
3533
                 * General entity references are checked for
3534
                 * syntactic validity.
3535
                 */
3536
0
                str++;
3537
0
                name = xmlParseStringName(ctxt, &str);
3538
3539
0
                if ((name == NULL) || (*str++ != ';')) {
3540
0
                    xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
3541
0
                            "EntityValue: '&' forbidden except for entities "
3542
0
                            "references\n");
3543
0
                    xmlFree(name);
3544
0
                    return;
3545
0
                }
3546
3547
0
                xmlFree(name);
3548
0
            }
3549
0
        } else if (c == '%') {
3550
0
            xmlEntityPtr ent;
3551
3552
0
            if (chunk < str)
3553
0
                xmlSBufAddString(buf, chunk, str - chunk);
3554
3555
0
            ent = xmlParseStringPEReference(ctxt, &str);
3556
0
            if (ent == NULL)
3557
0
                return;
3558
3559
0
            if (!PARSER_EXTERNAL(ctxt)) {
3560
0
                xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
3561
0
                return;
3562
0
            }
3563
3564
0
            if (ent->content == NULL) {
3565
                /*
3566
                 * Note: external parsed entities will not be loaded,
3567
                 * it is not required for a non-validating parser to
3568
                 * complete external PEReferences coming from the
3569
                 * internal subset
3570
                 */
3571
0
                if (((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
3572
0
                    ((ctxt->replaceEntities) ||
3573
0
                     (ctxt->validate))) {
3574
0
                    xmlLoadEntityContent(ctxt, ent);
3575
0
                } else {
3576
0
                    xmlWarningMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
3577
0
                                  "not validating will not read content for "
3578
0
                                  "PE entity %s\n", ent->name, NULL);
3579
0
                }
3580
0
            }
3581
3582
            /*
3583
             * TODO: Skip if ent->content is still NULL.
3584
             */
3585
3586
0
            if (xmlParserEntityCheck(ctxt, ent->length))
3587
0
                return;
3588
3589
0
            if (ent->flags & XML_ENT_EXPANDING) {
3590
0
                xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3591
0
                xmlHaltParser(ctxt);
3592
0
                return;
3593
0
            }
3594
3595
0
            ent->flags |= XML_ENT_EXPANDING;
3596
0
            xmlExpandPEsInEntityValue(ctxt, buf, ent->content, ent->length,
3597
0
                                      depth);
3598
0
            ent->flags &= ~XML_ENT_EXPANDING;
3599
3600
0
            chunk = str;
3601
0
        } else {
3602
            /* Normal ASCII char */
3603
0
            if (!IS_BYTE_CHAR(c)) {
3604
0
                xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
3605
0
                        "invalid character in entity value\n");
3606
0
                if (chunk < str)
3607
0
                    xmlSBufAddString(buf, chunk, str - chunk);
3608
0
                xmlSBufAddReplChar(buf);
3609
0
                str += 1;
3610
0
                chunk = str;
3611
0
            } else {
3612
0
                str += 1;
3613
0
            }
3614
0
        }
3615
0
    }
3616
3617
0
    if (chunk < str)
3618
0
        xmlSBufAddString(buf, chunk, str - chunk);
3619
0
}
3620
3621
/**
3622
 * Parse a value for ENTITY declarations
3623
 *
3624
 * @deprecated Internal function, don't use.
3625
 *
3626
 *     [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
3627
 *                         "'" ([^%&'] | PEReference | Reference)* "'"
3628
 *
3629
 * @param ctxt  an XML parser context
3630
 * @param orig  if non-NULL store a copy of the original entity value
3631
 * @returns the EntityValue parsed with reference substituted or NULL
3632
 */
3633
xmlChar *
3634
0
xmlParseEntityValue(xmlParserCtxt *ctxt, xmlChar **orig) {
3635
0
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3636
0
                         XML_MAX_HUGE_LENGTH :
3637
0
                         XML_MAX_TEXT_LENGTH;
3638
0
    xmlSBuf buf;
3639
0
    const xmlChar *start;
3640
0
    int quote, length;
3641
3642
0
    xmlSBufInit(&buf, maxLength);
3643
3644
0
    GROW;
3645
3646
0
    quote = CUR;
3647
0
    if ((quote != '"') && (quote != '\'')) {
3648
0
  xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
3649
0
  return(NULL);
3650
0
    }
3651
0
    CUR_PTR++;
3652
3653
0
    length = 0;
3654
3655
    /*
3656
     * Copy raw content of the entity into a buffer
3657
     */
3658
0
    while (1) {
3659
0
        int c;
3660
3661
0
        if (PARSER_STOPPED(ctxt))
3662
0
            goto error;
3663
3664
0
        if (CUR_PTR >= ctxt->input->end) {
3665
0
            xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
3666
0
            goto error;
3667
0
        }
3668
3669
0
        c = CUR;
3670
3671
0
        if (c == 0) {
3672
0
            xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
3673
0
                    "invalid character in entity value\n");
3674
0
            goto error;
3675
0
        }
3676
0
        if (c == quote)
3677
0
            break;
3678
0
        NEXTL(1);
3679
0
        length += 1;
3680
3681
        /*
3682
         * TODO: Check growth threshold
3683
         */
3684
0
        if (ctxt->input->end - CUR_PTR < 10)
3685
0
            GROW;
3686
0
    }
3687
3688
0
    start = CUR_PTR - length;
3689
3690
0
    if (orig != NULL) {
3691
0
        *orig = xmlStrndup(start, length);
3692
0
        if (*orig == NULL)
3693
0
            xmlErrMemory(ctxt);
3694
0
    }
3695
3696
0
    xmlExpandPEsInEntityValue(ctxt, &buf, start, length, ctxt->inputNr);
3697
3698
0
    NEXTL(1);
3699
3700
0
    return(xmlSBufFinish(&buf, NULL, ctxt, "entity length too long"));
3701
3702
0
error:
3703
0
    xmlSBufCleanup(&buf, ctxt, "entity length too long");
3704
0
    return(NULL);
3705
0
}
3706
3707
/**
3708
 * Check an entity reference in an attribute value for validity
3709
 * without expanding it.
3710
 *
3711
 * @param ctxt  parser context
3712
 * @param pent  entity
3713
 * @param depth  nesting depth
3714
 */
3715
static void
3716
0
xmlCheckEntityInAttValue(xmlParserCtxtPtr ctxt, xmlEntityPtr pent, int depth) {
3717
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3718
0
    const xmlChar *str;
3719
0
    unsigned long expandedSize = pent->length;
3720
0
    int c, flags;
3721
3722
0
    depth += 1;
3723
0
    if (depth > maxDepth) {
3724
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3725
0
                       "Maximum entity nesting depth exceeded");
3726
0
  return;
3727
0
    }
3728
3729
0
    if (pent->flags & XML_ENT_EXPANDING) {
3730
0
        xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3731
0
        xmlHaltParser(ctxt);
3732
0
        return;
3733
0
    }
3734
3735
    /*
3736
     * If we're parsing a default attribute value in DTD content,
3737
     * the entity might reference other entities which weren't
3738
     * defined yet, so the check isn't reliable.
3739
     */
3740
0
    if (ctxt->inSubset == 0)
3741
0
        flags = XML_ENT_CHECKED | XML_ENT_VALIDATED;
3742
0
    else
3743
0
        flags = XML_ENT_VALIDATED;
3744
3745
0
    str = pent->content;
3746
0
    if (str == NULL)
3747
0
        goto done;
3748
3749
    /*
3750
     * Note that entity values are already validated. We only check
3751
     * for illegal less-than signs and compute the expanded size
3752
     * of the entity. No special handling for multi-byte characters
3753
     * is needed.
3754
     */
3755
0
    while (!PARSER_STOPPED(ctxt)) {
3756
0
        c = *str;
3757
3758
0
  if (c != '&') {
3759
0
            if (c == 0)
3760
0
                break;
3761
3762
0
            if (c == '<')
3763
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
3764
0
                        "'<' in entity '%s' is not allowed in attributes "
3765
0
                        "values\n", pent->name);
3766
3767
0
            str += 1;
3768
0
        } else if (str[1] == '#') {
3769
0
            int val;
3770
3771
0
      val = xmlParseStringCharRef(ctxt, &str);
3772
0
      if (val == 0) {
3773
0
                pent->content[0] = 0;
3774
0
                break;
3775
0
            }
3776
0
  } else {
3777
0
            xmlChar *name;
3778
0
            xmlEntityPtr ent;
3779
3780
0
      name = xmlParseStringEntityRef(ctxt, &str);
3781
0
      if (name == NULL) {
3782
0
                pent->content[0] = 0;
3783
0
                break;
3784
0
            }
3785
3786
0
            ent = xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 1);
3787
0
            xmlFree(name);
3788
3789
0
            if ((ent != NULL) &&
3790
0
                (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
3791
0
                if ((ent->flags & flags) != flags) {
3792
0
                    pent->flags |= XML_ENT_EXPANDING;
3793
0
                    xmlCheckEntityInAttValue(ctxt, ent, depth);
3794
0
                    pent->flags &= ~XML_ENT_EXPANDING;
3795
0
                }
3796
3797
0
                xmlSaturatedAdd(&expandedSize, ent->expandedSize);
3798
0
                xmlSaturatedAdd(&expandedSize, XML_ENT_FIXED_COST);
3799
0
            }
3800
0
        }
3801
0
    }
3802
3803
0
done:
3804
0
    if (ctxt->inSubset == 0)
3805
0
        pent->expandedSize = expandedSize;
3806
3807
0
    pent->flags |= flags;
3808
0
}
3809
3810
/**
3811
 * Expand general entity references in an entity or attribute value.
3812
 * Perform attribute value normalization.
3813
 *
3814
 * @param ctxt  parser context
3815
 * @param buf  string buffer
3816
 * @param str  entity or attribute value
3817
 * @param pent  entity for entity value, NULL for attribute values
3818
 * @param normalize  whether to collapse whitespace
3819
 * @param inSpace  whitespace state
3820
 * @param depth  nesting depth
3821
 * @param check  whether to check for amplification
3822
 * @returns  whether there was a normalization change
3823
 */
3824
static int
3825
xmlExpandEntityInAttValue(xmlParserCtxtPtr ctxt, xmlSBuf *buf,
3826
                          const xmlChar *str, xmlEntityPtr pent, int normalize,
3827
0
                          int *inSpace, int depth, int check) {
3828
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 40 : 20;
3829
0
    int c, chunkSize;
3830
0
    int normChange = 0;
3831
3832
0
    if (str == NULL)
3833
0
        return(0);
3834
3835
0
    depth += 1;
3836
0
    if (depth > maxDepth) {
3837
0
  xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
3838
0
                       "Maximum entity nesting depth exceeded");
3839
0
  return(0);
3840
0
    }
3841
3842
0
    if (pent != NULL) {
3843
0
        if (pent->flags & XML_ENT_EXPANDING) {
3844
0
            xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
3845
0
            xmlHaltParser(ctxt);
3846
0
            return(0);
3847
0
        }
3848
3849
0
        if (check) {
3850
0
            if (xmlParserEntityCheck(ctxt, pent->length))
3851
0
                return(0);
3852
0
        }
3853
0
    }
3854
3855
0
    chunkSize = 0;
3856
3857
    /*
3858
     * Note that entity values are already validated. No special
3859
     * handling for multi-byte characters is needed.
3860
     */
3861
0
    while (!PARSER_STOPPED(ctxt)) {
3862
0
        c = *str;
3863
3864
0
  if (c != '&') {
3865
0
            if (c == 0)
3866
0
                break;
3867
3868
            /*
3869
             * If this function is called without an entity, it is used to
3870
             * expand entities in an attribute content where less-than was
3871
             * already unscaped and is allowed.
3872
             */
3873
0
            if ((pent != NULL) && (c == '<')) {
3874
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
3875
0
                        "'<' in entity '%s' is not allowed in attributes "
3876
0
                        "values\n", pent->name);
3877
0
                break;
3878
0
            }
3879
3880
0
            if (c <= 0x20) {
3881
0
                if ((normalize) && (*inSpace)) {
3882
                    /* Skip char */
3883
0
                    if (chunkSize > 0) {
3884
0
                        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3885
0
                        chunkSize = 0;
3886
0
                    }
3887
0
                    normChange = 1;
3888
0
                } else if (c < 0x20) {
3889
0
                    if (chunkSize > 0) {
3890
0
                        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3891
0
                        chunkSize = 0;
3892
0
                    }
3893
3894
0
                    xmlSBufAddCString(buf, " ", 1);
3895
0
                } else {
3896
0
                    chunkSize += 1;
3897
0
                }
3898
3899
0
                *inSpace = 1;
3900
0
            } else {
3901
0
                chunkSize += 1;
3902
0
                *inSpace = 0;
3903
0
            }
3904
3905
0
            str += 1;
3906
0
        } else if (str[1] == '#') {
3907
0
            int val;
3908
3909
0
            if (chunkSize > 0) {
3910
0
                xmlSBufAddString(buf, str - chunkSize, chunkSize);
3911
0
                chunkSize = 0;
3912
0
            }
3913
3914
0
      val = xmlParseStringCharRef(ctxt, &str);
3915
0
      if (val == 0) {
3916
0
                if (pent != NULL)
3917
0
                    pent->content[0] = 0;
3918
0
                break;
3919
0
            }
3920
3921
0
            if (val == ' ') {
3922
0
                if ((normalize) && (*inSpace))
3923
0
                    normChange = 1;
3924
0
                else
3925
0
                    xmlSBufAddCString(buf, " ", 1);
3926
0
                *inSpace = 1;
3927
0
            } else {
3928
0
                xmlSBufAddChar(buf, val);
3929
0
                *inSpace = 0;
3930
0
            }
3931
0
  } else {
3932
0
            xmlChar *name;
3933
0
            xmlEntityPtr ent;
3934
3935
0
            if (chunkSize > 0) {
3936
0
                xmlSBufAddString(buf, str - chunkSize, chunkSize);
3937
0
                chunkSize = 0;
3938
0
            }
3939
3940
0
      name = xmlParseStringEntityRef(ctxt, &str);
3941
0
            if (name == NULL) {
3942
0
                if (pent != NULL)
3943
0
                    pent->content[0] = 0;
3944
0
                break;
3945
0
            }
3946
3947
0
            ent = xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 1);
3948
0
            xmlFree(name);
3949
3950
0
      if ((ent != NULL) &&
3951
0
    (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
3952
0
    if (ent->content == NULL) {
3953
0
        xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
3954
0
          "predefined entity has no content\n");
3955
0
                    break;
3956
0
                }
3957
3958
0
                xmlSBufAddString(buf, ent->content, ent->length);
3959
3960
0
                *inSpace = 0;
3961
0
      } else if ((ent != NULL) && (ent->content != NULL)) {
3962
0
                if (pent != NULL)
3963
0
                    pent->flags |= XML_ENT_EXPANDING;
3964
0
    normChange |= xmlExpandEntityInAttValue(ctxt, buf,
3965
0
                        ent->content, ent, normalize, inSpace, depth, check);
3966
0
                if (pent != NULL)
3967
0
                    pent->flags &= ~XML_ENT_EXPANDING;
3968
0
      }
3969
0
        }
3970
0
    }
3971
3972
0
    if (chunkSize > 0)
3973
0
        xmlSBufAddString(buf, str - chunkSize, chunkSize);
3974
3975
0
    return(normChange);
3976
0
}
3977
3978
/**
3979
 * Expand general entity references in an entity or attribute value.
3980
 * Perform attribute value normalization.
3981
 *
3982
 * @param ctxt  parser context
3983
 * @param str  entity or attribute value
3984
 * @param normalize  whether to collapse whitespace
3985
 * @returns the expanded attribtue value.
3986
 */
3987
xmlChar *
3988
xmlExpandEntitiesInAttValue(xmlParserCtxt *ctxt, const xmlChar *str,
3989
0
                            int normalize) {
3990
0
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
3991
0
                         XML_MAX_HUGE_LENGTH :
3992
0
                         XML_MAX_TEXT_LENGTH;
3993
0
    xmlSBuf buf;
3994
0
    int inSpace = 1;
3995
3996
0
    xmlSBufInit(&buf, maxLength);
3997
3998
0
    xmlExpandEntityInAttValue(ctxt, &buf, str, NULL, normalize, &inSpace,
3999
0
                              ctxt->inputNr, /* check */ 0);
4000
4001
0
    if ((normalize) && (inSpace) && (buf.size > 0))
4002
0
        buf.size--;
4003
4004
0
    return(xmlSBufFinish(&buf, NULL, ctxt, "AttValue length too long"));
4005
0
}
4006
4007
/**
4008
 * Parse a value for an attribute.
4009
 *
4010
 * NOTE: if no normalization is needed, the routine will return pointers
4011
 * directly from the data buffer.
4012
 *
4013
 * 3.3.3 Attribute-Value Normalization:
4014
 *
4015
 * Before the value of an attribute is passed to the application or
4016
 * checked for validity, the XML processor must normalize it as follows:
4017
 *
4018
 * - a character reference is processed by appending the referenced
4019
 *   character to the attribute value
4020
 * - an entity reference is processed by recursively processing the
4021
 *   replacement text of the entity
4022
 * - a whitespace character (\#x20, \#xD, \#xA, \#x9) is processed by
4023
 *   appending \#x20 to the normalized value, except that only a single
4024
 *   \#x20 is appended for a "#xD#xA" sequence that is part of an external
4025
 *   parsed entity or the literal entity value of an internal parsed entity
4026
 * - other characters are processed by appending them to the normalized value
4027
 *
4028
 * If the declared value is not CDATA, then the XML processor must further
4029
 * process the normalized attribute value by discarding any leading and
4030
 * trailing space (\#x20) characters, and by replacing sequences of space
4031
 * (\#x20) characters by a single space (\#x20) character.
4032
 * All attributes for which no declaration has been read should be treated
4033
 * by a non-validating parser as if declared CDATA.
4034
 *
4035
 * @param ctxt  an XML parser context
4036
 * @param attlen  attribute len result
4037
 * @param outFlags  resulting XML_ATTVAL_* flags
4038
 * @param special  value from attsSpecial
4039
 * @param isNamespace  whether this is a namespace declaration
4040
 * @returns the AttValue parsed or NULL. The value has to be freed by the
4041
 *     caller if it was copied, this can be detected by val[*len] == 0.
4042
 */
4043
static xmlChar *
4044
xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *attlen, int *outFlags,
4045
0
                         int special, int isNamespace) {
4046
0
    unsigned maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4047
0
                         XML_MAX_HUGE_LENGTH :
4048
0
                         XML_MAX_TEXT_LENGTH;
4049
0
    xmlSBuf buf;
4050
0
    xmlChar *ret;
4051
0
    int c, l, quote, entFlags, chunkSize;
4052
0
    int inSpace = 1;
4053
0
    int replaceEntities;
4054
0
    int normalize = (special & XML_SPECIAL_TYPE_MASK) != 0;
4055
0
    int attvalFlags = 0;
4056
4057
    /* Always expand namespace URIs */
4058
0
    replaceEntities = (ctxt->replaceEntities) || (isNamespace);
4059
4060
0
    xmlSBufInit(&buf, maxLength);
4061
4062
0
    GROW;
4063
4064
0
    quote = CUR;
4065
0
    if ((quote != '"') && (quote != '\'')) {
4066
0
  xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
4067
0
  return(NULL);
4068
0
    }
4069
0
    NEXTL(1);
4070
4071
0
    if (ctxt->inSubset == 0)
4072
0
        entFlags = XML_ENT_CHECKED | XML_ENT_VALIDATED;
4073
0
    else
4074
0
        entFlags = XML_ENT_VALIDATED;
4075
4076
0
    inSpace = 1;
4077
0
    chunkSize = 0;
4078
4079
0
    while (1) {
4080
0
        if (PARSER_STOPPED(ctxt))
4081
0
            goto error;
4082
4083
0
        if (CUR_PTR >= ctxt->input->end) {
4084
0
            xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
4085
0
                           "AttValue: ' expected\n");
4086
0
            goto error;
4087
0
        }
4088
4089
        /*
4090
         * TODO: Check growth threshold
4091
         */
4092
0
        if (ctxt->input->end - CUR_PTR < 10)
4093
0
            GROW;
4094
4095
0
        c = CUR;
4096
4097
0
        if (c >= 0x80) {
4098
0
            l = xmlUTF8MultibyteLen(ctxt, CUR_PTR,
4099
0
                    "invalid character in attribute value\n");
4100
0
            if (l == 0) {
4101
0
                if (chunkSize > 0) {
4102
0
                    xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4103
0
                    chunkSize = 0;
4104
0
                }
4105
0
                xmlSBufAddReplChar(&buf);
4106
0
                NEXTL(1);
4107
0
            } else {
4108
0
                chunkSize += l;
4109
0
                NEXTL(l);
4110
0
            }
4111
4112
0
            inSpace = 0;
4113
0
        } else if (c != '&') {
4114
0
            if (c > 0x20) {
4115
0
                if (c == quote)
4116
0
                    break;
4117
4118
0
                if (c == '<')
4119
0
                    xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
4120
4121
0
                chunkSize += 1;
4122
0
                inSpace = 0;
4123
0
            } else if (!IS_BYTE_CHAR(c)) {
4124
0
                xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR,
4125
0
                        "invalid character in attribute value\n");
4126
0
                if (chunkSize > 0) {
4127
0
                    xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4128
0
                    chunkSize = 0;
4129
0
                }
4130
0
                xmlSBufAddReplChar(&buf);
4131
0
                inSpace = 0;
4132
0
            } else {
4133
                /* Whitespace */
4134
0
                if ((normalize) && (inSpace)) {
4135
                    /* Skip char */
4136
0
                    if (chunkSize > 0) {
4137
0
                        xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4138
0
                        chunkSize = 0;
4139
0
                    }
4140
0
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4141
0
                } else if (c < 0x20) {
4142
                    /* Convert to space */
4143
0
                    if (chunkSize > 0) {
4144
0
                        xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4145
0
                        chunkSize = 0;
4146
0
                    }
4147
4148
0
                    xmlSBufAddCString(&buf, " ", 1);
4149
0
                } else {
4150
0
                    chunkSize += 1;
4151
0
                }
4152
4153
0
                inSpace = 1;
4154
4155
0
                if ((c == 0xD) && (NXT(1) == 0xA))
4156
0
                    CUR_PTR++;
4157
0
            }
4158
4159
0
            NEXTL(1);
4160
0
        } else if (NXT(1) == '#') {
4161
0
            int val;
4162
4163
0
            if (chunkSize > 0) {
4164
0
                xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4165
0
                chunkSize = 0;
4166
0
            }
4167
4168
0
            val = xmlParseCharRef(ctxt);
4169
0
            if (val == 0)
4170
0
                goto error;
4171
4172
0
            if ((val == '&') && (!replaceEntities)) {
4173
                /*
4174
                 * The reparsing will be done in xmlNodeParseContent()
4175
                 * called from SAX2.c
4176
                 */
4177
0
                xmlSBufAddCString(&buf, "&#38;", 5);
4178
0
                inSpace = 0;
4179
0
            } else if (val == ' ') {
4180
0
                if ((normalize) && (inSpace))
4181
0
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4182
0
                else
4183
0
                    xmlSBufAddCString(&buf, " ", 1);
4184
0
                inSpace = 1;
4185
0
            } else {
4186
0
                xmlSBufAddChar(&buf, val);
4187
0
                inSpace = 0;
4188
0
            }
4189
0
        } else {
4190
0
            const xmlChar *name;
4191
0
            xmlEntityPtr ent;
4192
4193
0
            if (chunkSize > 0) {
4194
0
                xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4195
0
                chunkSize = 0;
4196
0
            }
4197
4198
0
            name = xmlParseEntityRefInternal(ctxt);
4199
0
            if (name == NULL) {
4200
                /*
4201
                 * Probably a literal '&' which wasn't escaped.
4202
                 * TODO: Handle gracefully in recovery mode.
4203
                 */
4204
0
                continue;
4205
0
            }
4206
4207
0
            ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 1);
4208
0
            if (ent == NULL)
4209
0
                continue;
4210
4211
0
            if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
4212
0
                if ((ent->content[0] == '&') && (!replaceEntities))
4213
0
                    xmlSBufAddCString(&buf, "&#38;", 5);
4214
0
                else
4215
0
                    xmlSBufAddString(&buf, ent->content, ent->length);
4216
0
                inSpace = 0;
4217
0
            } else if (replaceEntities) {
4218
0
                if (xmlExpandEntityInAttValue(ctxt, &buf,
4219
0
                        ent->content, ent, normalize, &inSpace, ctxt->inputNr,
4220
0
                        /* check */ 1) > 0)
4221
0
                    attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4222
0
            } else {
4223
0
                if ((ent->flags & entFlags) != entFlags)
4224
0
                    xmlCheckEntityInAttValue(ctxt, ent, ctxt->inputNr);
4225
4226
0
                if (xmlParserEntityCheck(ctxt, ent->expandedSize)) {
4227
0
                    ent->content[0] = 0;
4228
0
                    goto error;
4229
0
                }
4230
4231
                /*
4232
                 * Just output the reference
4233
                 */
4234
0
                xmlSBufAddCString(&buf, "&", 1);
4235
0
                xmlSBufAddString(&buf, ent->name, xmlStrlen(ent->name));
4236
0
                xmlSBufAddCString(&buf, ";", 1);
4237
4238
0
                inSpace = 0;
4239
0
            }
4240
0
  }
4241
0
    }
4242
4243
0
    if ((buf.mem == NULL) && (outFlags != NULL)) {
4244
0
        ret = (xmlChar *) CUR_PTR - chunkSize;
4245
4246
0
        if (attlen != NULL)
4247
0
            *attlen = chunkSize;
4248
0
        if ((normalize) && (inSpace) && (chunkSize > 0)) {
4249
0
            attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4250
0
            *attlen -= 1;
4251
0
        }
4252
4253
        /* Report potential error */
4254
0
        xmlSBufCleanup(&buf, ctxt, "AttValue length too long");
4255
0
    } else {
4256
0
        if (chunkSize > 0)
4257
0
            xmlSBufAddString(&buf, CUR_PTR - chunkSize, chunkSize);
4258
4259
0
        if ((normalize) && (inSpace) && (buf.size > 0)) {
4260
0
            attvalFlags |= XML_ATTVAL_NORM_CHANGE;
4261
0
            buf.size--;
4262
0
        }
4263
4264
0
        ret = xmlSBufFinish(&buf, attlen, ctxt, "AttValue length too long");
4265
0
        attvalFlags |= XML_ATTVAL_ALLOC;
4266
4267
0
        if (ret != NULL) {
4268
0
            if (attlen != NULL)
4269
0
                *attlen = buf.size;
4270
0
        }
4271
0
    }
4272
4273
0
    if (outFlags != NULL)
4274
0
        *outFlags = attvalFlags;
4275
4276
0
    NEXTL(1);
4277
4278
0
    return(ret);
4279
4280
0
error:
4281
0
    xmlSBufCleanup(&buf, ctxt, "AttValue length too long");
4282
0
    return(NULL);
4283
0
}
4284
4285
/**
4286
 * Parse a value for an attribute
4287
 * Note: the parser won't do substitution of entities here, this
4288
 * will be handled later in #xmlStringGetNodeList
4289
 *
4290
 * @deprecated Internal function, don't use.
4291
 *
4292
 *     [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
4293
 *                       "'" ([^<&'] | Reference)* "'"
4294
 *
4295
 * 3.3.3 Attribute-Value Normalization:
4296
 *
4297
 * Before the value of an attribute is passed to the application or
4298
 * checked for validity, the XML processor must normalize it as follows:
4299
 *
4300
 * - a character reference is processed by appending the referenced
4301
 *   character to the attribute value
4302
 * - an entity reference is processed by recursively processing the
4303
 *   replacement text of the entity
4304
 * - a whitespace character (\#x20, \#xD, \#xA, \#x9) is processed by
4305
 *   appending \#x20 to the normalized value, except that only a single
4306
 *   \#x20 is appended for a "#xD#xA" sequence that is part of an external
4307
 *   parsed entity or the literal entity value of an internal parsed entity
4308
 * - other characters are processed by appending them to the normalized value
4309
 *
4310
 * If the declared value is not CDATA, then the XML processor must further
4311
 * process the normalized attribute value by discarding any leading and
4312
 * trailing space (\#x20) characters, and by replacing sequences of space
4313
 * (\#x20) characters by a single space (\#x20) character.
4314
 * All attributes for which no declaration has been read should be treated
4315
 * by a non-validating parser as if declared CDATA.
4316
 *
4317
 * @param ctxt  an XML parser context
4318
 * @returns the AttValue parsed or NULL. The value has to be freed by the
4319
 * caller.
4320
 */
4321
xmlChar *
4322
0
xmlParseAttValue(xmlParserCtxt *ctxt) {
4323
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
4324
0
    return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0, 0));
4325
0
}
4326
4327
/**
4328
 * Parse an XML Literal
4329
 *
4330
 * @deprecated Internal function, don't use.
4331
 *
4332
 *     [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
4333
 *
4334
 * @param ctxt  an XML parser context
4335
 * @returns the SystemLiteral parsed or NULL
4336
 */
4337
4338
xmlChar *
4339
0
xmlParseSystemLiteral(xmlParserCtxt *ctxt) {
4340
0
    xmlChar *buf = NULL;
4341
0
    int len = 0;
4342
0
    int size = XML_PARSER_BUFFER_SIZE;
4343
0
    int cur, l;
4344
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4345
0
                    XML_MAX_TEXT_LENGTH :
4346
0
                    XML_MAX_NAME_LENGTH;
4347
0
    xmlChar stop;
4348
4349
0
    if (RAW == '"') {
4350
0
        NEXT;
4351
0
  stop = '"';
4352
0
    } else if (RAW == '\'') {
4353
0
        NEXT;
4354
0
  stop = '\'';
4355
0
    } else {
4356
0
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
4357
0
  return(NULL);
4358
0
    }
4359
4360
0
    buf = xmlMalloc(size);
4361
0
    if (buf == NULL) {
4362
0
        xmlErrMemory(ctxt);
4363
0
  return(NULL);
4364
0
    }
4365
0
    cur = xmlCurrentCharRecover(ctxt, &l);
4366
0
    while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
4367
0
  if (len + 5 >= size) {
4368
0
      xmlChar *tmp;
4369
0
            int newSize;
4370
4371
0
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4372
0
            if (newSize < 0) {
4373
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral");
4374
0
                xmlFree(buf);
4375
0
                return(NULL);
4376
0
            }
4377
0
      tmp = xmlRealloc(buf, newSize);
4378
0
      if (tmp == NULL) {
4379
0
          xmlFree(buf);
4380
0
    xmlErrMemory(ctxt);
4381
0
    return(NULL);
4382
0
      }
4383
0
      buf = tmp;
4384
0
            size = newSize;
4385
0
  }
4386
0
  COPY_BUF(buf, len, cur);
4387
0
  NEXTL(l);
4388
0
  cur = xmlCurrentCharRecover(ctxt, &l);
4389
0
    }
4390
0
    buf[len] = 0;
4391
0
    if (!IS_CHAR(cur)) {
4392
0
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
4393
0
    } else {
4394
0
  NEXT;
4395
0
    }
4396
0
    return(buf);
4397
0
}
4398
4399
/**
4400
 * Parse an XML public literal
4401
 *
4402
 * @deprecated Internal function, don't use.
4403
 *
4404
 *     [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
4405
 *
4406
 * @param ctxt  an XML parser context
4407
 * @returns the PubidLiteral parsed or NULL.
4408
 */
4409
4410
xmlChar *
4411
0
xmlParsePubidLiteral(xmlParserCtxt *ctxt) {
4412
0
    xmlChar *buf = NULL;
4413
0
    int len = 0;
4414
0
    int size = XML_PARSER_BUFFER_SIZE;
4415
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4416
0
                    XML_MAX_TEXT_LENGTH :
4417
0
                    XML_MAX_NAME_LENGTH;
4418
0
    xmlChar cur;
4419
0
    xmlChar stop;
4420
4421
0
    if (RAW == '"') {
4422
0
        NEXT;
4423
0
  stop = '"';
4424
0
    } else if (RAW == '\'') {
4425
0
        NEXT;
4426
0
  stop = '\'';
4427
0
    } else {
4428
0
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
4429
0
  return(NULL);
4430
0
    }
4431
0
    buf = xmlMalloc(size);
4432
0
    if (buf == NULL) {
4433
0
  xmlErrMemory(ctxt);
4434
0
  return(NULL);
4435
0
    }
4436
0
    cur = CUR;
4437
0
    while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop) &&
4438
0
           (PARSER_STOPPED(ctxt) == 0)) { /* checked */
4439
0
  if (len + 1 >= size) {
4440
0
      xmlChar *tmp;
4441
0
            int newSize;
4442
4443
0
      newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4444
0
            if (newSize < 0) {
4445
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID");
4446
0
                xmlFree(buf);
4447
0
                return(NULL);
4448
0
            }
4449
0
      tmp = xmlRealloc(buf, newSize);
4450
0
      if (tmp == NULL) {
4451
0
    xmlErrMemory(ctxt);
4452
0
    xmlFree(buf);
4453
0
    return(NULL);
4454
0
      }
4455
0
      buf = tmp;
4456
0
            size = newSize;
4457
0
  }
4458
0
  buf[len++] = cur;
4459
0
  NEXT;
4460
0
  cur = CUR;
4461
0
    }
4462
0
    buf[len] = 0;
4463
0
    if (cur != stop) {
4464
0
  xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
4465
0
    } else {
4466
0
  NEXTL(1);
4467
0
    }
4468
0
    return(buf);
4469
0
}
4470
4471
static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial);
4472
4473
/*
4474
 * used for the test in the inner loop of the char data testing
4475
 */
4476
static const unsigned char test_char_data[256] = {
4477
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4478
    0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */
4479
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4480
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4481
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */
4482
    0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
4483
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
4484
    0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */
4485
    0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
4486
    0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
4487
    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
4488
    0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */
4489
    0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
4490
    0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
4491
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
4492
    0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
4493
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */
4494
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4495
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4496
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4497
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4498
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4499
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4500
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4501
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4502
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4503
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4504
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4505
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4506
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4507
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4508
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4509
};
4510
4511
static void
4512
xmlCharacters(xmlParserCtxtPtr ctxt, const xmlChar *buf, int size,
4513
0
              int isBlank) {
4514
0
    int checkBlanks;
4515
4516
0
    if ((ctxt->sax == NULL) || (ctxt->disableSAX))
4517
0
        return;
4518
4519
0
    checkBlanks = (!ctxt->keepBlanks) ||
4520
0
                  (ctxt->sax->ignorableWhitespace != ctxt->sax->characters);
4521
4522
    /*
4523
     * Calling areBlanks with only parts of a text node
4524
     * is fundamentally broken, making the NOBLANKS option
4525
     * essentially unusable.
4526
     */
4527
0
    if ((checkBlanks) &&
4528
0
        (areBlanks(ctxt, buf, size, isBlank))) {
4529
0
        if ((ctxt->sax->ignorableWhitespace != NULL) &&
4530
0
            (ctxt->keepBlanks))
4531
0
            ctxt->sax->ignorableWhitespace(ctxt->userData, buf, size);
4532
0
    } else {
4533
0
        if (ctxt->sax->characters != NULL)
4534
0
            ctxt->sax->characters(ctxt->userData, buf, size);
4535
4536
        /*
4537
         * The old code used to update this value for "complex" data
4538
         * even if checkBlanks was false. This was probably a bug.
4539
         */
4540
0
        if ((checkBlanks) && (*ctxt->space == -1))
4541
0
            *ctxt->space = -2;
4542
0
    }
4543
0
}
4544
4545
/**
4546
 * Parse character data. Always makes progress if the first char isn't
4547
 * '<' or '&'.
4548
 *
4549
 * The right angle bracket (>) may be represented using the string "&gt;",
4550
 * and must, for compatibility, be escaped using "&gt;" or a character
4551
 * reference when it appears in the string "]]>" in content, when that
4552
 * string is not marking the end of a CDATA section.
4553
 *
4554
 *     [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
4555
 * @param ctxt  an XML parser context
4556
 * @param partial  buffer may contain partial UTF-8 sequences
4557
 */
4558
static void
4559
0
xmlParseCharDataInternal(xmlParserCtxtPtr ctxt, int partial) {
4560
0
    const xmlChar *in;
4561
0
    int line = ctxt->input->line;
4562
0
    int col = ctxt->input->col;
4563
0
    int ccol;
4564
0
    int terminate = 0;
4565
4566
0
    GROW;
4567
    /*
4568
     * Accelerated common case where input don't need to be
4569
     * modified before passing it to the handler.
4570
     */
4571
0
    in = ctxt->input->cur;
4572
0
    do {
4573
0
get_more_space:
4574
0
        while (*in == 0x20) { in++; ctxt->input->col++; }
4575
0
        if (*in == 0xA) {
4576
0
            do {
4577
0
                ctxt->input->line++; ctxt->input->col = 1;
4578
0
                in++;
4579
0
            } while (*in == 0xA);
4580
0
            goto get_more_space;
4581
0
        }
4582
0
        if (*in == '<') {
4583
0
            while (in > ctxt->input->cur) {
4584
0
                const xmlChar *tmp = ctxt->input->cur;
4585
0
                size_t nbchar = in - tmp;
4586
4587
0
                if (nbchar > XML_MAX_ITEMS)
4588
0
                    nbchar = XML_MAX_ITEMS;
4589
0
                ctxt->input->cur += nbchar;
4590
4591
0
                xmlCharacters(ctxt, tmp, nbchar, 1);
4592
0
            }
4593
0
            return;
4594
0
        }
4595
4596
0
get_more:
4597
0
        ccol = ctxt->input->col;
4598
0
        while (test_char_data[*in]) {
4599
0
            in++;
4600
0
            ccol++;
4601
0
        }
4602
0
        ctxt->input->col = ccol;
4603
0
        if (*in == 0xA) {
4604
0
            do {
4605
0
                ctxt->input->line++; ctxt->input->col = 1;
4606
0
                in++;
4607
0
            } while (*in == 0xA);
4608
0
            goto get_more;
4609
0
        }
4610
0
        if (*in == ']') {
4611
0
            size_t avail = ctxt->input->end - in;
4612
4613
0
            if (partial && avail < 2) {
4614
0
                terminate = 1;
4615
0
                goto invoke_callback;
4616
0
            }
4617
0
            if (in[1] == ']') {
4618
0
                if (partial && avail < 3) {
4619
0
                    terminate = 1;
4620
0
                    goto invoke_callback;
4621
0
                }
4622
0
                if (in[2] == '>')
4623
0
                    xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
4624
0
            }
4625
4626
0
            in++;
4627
0
            ctxt->input->col++;
4628
0
            goto get_more;
4629
0
        }
4630
4631
0
invoke_callback:
4632
0
        while (in > ctxt->input->cur) {
4633
0
            const xmlChar *tmp = ctxt->input->cur;
4634
0
            size_t nbchar = in - tmp;
4635
4636
0
            if (nbchar > XML_MAX_ITEMS)
4637
0
                nbchar = XML_MAX_ITEMS;
4638
0
            ctxt->input->cur += nbchar;
4639
4640
0
            xmlCharacters(ctxt, tmp, nbchar, 0);
4641
4642
0
            line = ctxt->input->line;
4643
0
            col = ctxt->input->col;
4644
0
        }
4645
0
        ctxt->input->cur = in;
4646
0
        if (*in == 0xD) {
4647
0
            in++;
4648
0
            if (*in == 0xA) {
4649
0
                ctxt->input->cur = in;
4650
0
                in++;
4651
0
                ctxt->input->line++; ctxt->input->col = 1;
4652
0
                continue; /* while */
4653
0
            }
4654
0
            in--;
4655
0
        }
4656
0
        if (*in == '<') {
4657
0
            return;
4658
0
        }
4659
0
        if (*in == '&') {
4660
0
            return;
4661
0
        }
4662
0
        if (terminate) {
4663
0
            return;
4664
0
        }
4665
0
        SHRINK;
4666
0
        GROW;
4667
0
        in = ctxt->input->cur;
4668
0
    } while (((*in >= 0x20) && (*in <= 0x7F)) ||
4669
0
             (*in == 0x09) || (*in == 0x0a));
4670
0
    ctxt->input->line = line;
4671
0
    ctxt->input->col = col;
4672
0
    xmlParseCharDataComplex(ctxt, partial);
4673
0
}
4674
4675
/**
4676
 * Always makes progress if the first char isn't '<' or '&'.
4677
 *
4678
 * parse a CharData section.this is the fallback function
4679
 * of #xmlParseCharData when the parsing requires handling
4680
 * of non-ASCII characters.
4681
 *
4682
 * @param ctxt  an XML parser context
4683
 * @param partial  whether the input can end with truncated UTF-8
4684
 */
4685
static void
4686
0
xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int partial) {
4687
0
    xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
4688
0
    int nbchar = 0;
4689
0
    int cur, l;
4690
4691
0
    cur = xmlCurrentCharRecover(ctxt, &l);
4692
0
    while ((cur != '<') && /* checked */
4693
0
           (cur != '&') &&
4694
0
     (IS_CHAR(cur))) {
4695
0
        if (cur == ']') {
4696
0
            size_t avail = ctxt->input->end - ctxt->input->cur;
4697
4698
0
            if (partial && avail < 2)
4699
0
                break;
4700
0
            if (NXT(1) == ']') {
4701
0
                if (partial && avail < 3)
4702
0
                    break;
4703
0
                if (NXT(2) == '>')
4704
0
                    xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
4705
0
            }
4706
0
        }
4707
4708
0
  COPY_BUF(buf, nbchar, cur);
4709
  /* move current position before possible calling of ctxt->sax->characters */
4710
0
  NEXTL(l);
4711
0
  if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
4712
0
      buf[nbchar] = 0;
4713
4714
0
            xmlCharacters(ctxt, buf, nbchar, 0);
4715
0
      nbchar = 0;
4716
0
            SHRINK;
4717
0
  }
4718
0
  cur = xmlCurrentCharRecover(ctxt, &l);
4719
0
    }
4720
0
    if (nbchar != 0) {
4721
0
        buf[nbchar] = 0;
4722
4723
0
        xmlCharacters(ctxt, buf, nbchar, 0);
4724
0
    }
4725
    /*
4726
     * cur == 0 can mean
4727
     *
4728
     * - End of buffer.
4729
     * - An actual 0 character.
4730
     * - An incomplete UTF-8 sequence. This is allowed if partial is set.
4731
     */
4732
0
    if (ctxt->input->cur < ctxt->input->end) {
4733
0
        if ((cur == 0) && (CUR != 0)) {
4734
0
            if (partial == 0) {
4735
0
                xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4736
0
                        "Incomplete UTF-8 sequence starting with %02X\n", CUR);
4737
0
                NEXTL(1);
4738
0
            }
4739
0
        } else if ((cur != '<') && (cur != '&') && (cur != ']')) {
4740
            /* Generate the error and skip the offending character */
4741
0
            xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4742
0
                              "PCDATA invalid Char value %d\n", cur);
4743
0
            NEXTL(l);
4744
0
        }
4745
0
    }
4746
0
}
4747
4748
/**
4749
 * @deprecated Internal function, don't use.
4750
 * @param ctxt  an XML parser context
4751
 * @param cdata  unused
4752
 */
4753
void
4754
0
xmlParseCharData(xmlParserCtxt *ctxt, ATTRIBUTE_UNUSED int cdata) {
4755
0
    xmlParseCharDataInternal(ctxt, 0);
4756
0
}
4757
4758
/**
4759
 * Parse an External ID or a Public ID
4760
 *
4761
 * @deprecated Internal function, don't use.
4762
 *
4763
 * NOTE: Productions [75] and [83] interact badly since [75] can generate
4764
 * `'PUBLIC' S PubidLiteral S SystemLiteral`
4765
 *
4766
 *     [75] ExternalID ::= 'SYSTEM' S SystemLiteral
4767
 *                       | 'PUBLIC' S PubidLiteral S SystemLiteral
4768
 *
4769
 *     [83] PublicID ::= 'PUBLIC' S PubidLiteral
4770
 *
4771
 * @param ctxt  an XML parser context
4772
 * @param publicId  a xmlChar** receiving PubidLiteral
4773
 * @param strict  indicate whether we should restrict parsing to only
4774
 *          production [75], see NOTE below
4775
 * @returns the function returns SystemLiteral and in the second
4776
 *                case publicID receives PubidLiteral, is strict is off
4777
 *                it is possible to return NULL and have publicID set.
4778
 */
4779
4780
xmlChar *
4781
0
xmlParseExternalID(xmlParserCtxt *ctxt, xmlChar **publicId, int strict) {
4782
0
    xmlChar *URI = NULL;
4783
4784
0
    *publicId = NULL;
4785
0
    if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
4786
0
        SKIP(6);
4787
0
  if (SKIP_BLANKS == 0) {
4788
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4789
0
                     "Space required after 'SYSTEM'\n");
4790
0
  }
4791
0
  URI = xmlParseSystemLiteral(ctxt);
4792
0
  if (URI == NULL) {
4793
0
      xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
4794
0
        }
4795
0
    } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
4796
0
        SKIP(6);
4797
0
  if (SKIP_BLANKS == 0) {
4798
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4799
0
        "Space required after 'PUBLIC'\n");
4800
0
  }
4801
0
  *publicId = xmlParsePubidLiteral(ctxt);
4802
0
  if (*publicId == NULL) {
4803
0
      xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
4804
0
  }
4805
0
  if (strict) {
4806
      /*
4807
       * We don't handle [83] so "S SystemLiteral" is required.
4808
       */
4809
0
      if (SKIP_BLANKS == 0) {
4810
0
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4811
0
      "Space required after the Public Identifier\n");
4812
0
      }
4813
0
  } else {
4814
      /*
4815
       * We handle [83] so we return immediately, if
4816
       * "S SystemLiteral" is not detected. We skip blanks if no
4817
             * system literal was found, but this is harmless since we must
4818
             * be at the end of a NotationDecl.
4819
       */
4820
0
      if (SKIP_BLANKS == 0) return(NULL);
4821
0
      if ((CUR != '\'') && (CUR != '"')) return(NULL);
4822
0
  }
4823
0
  URI = xmlParseSystemLiteral(ctxt);
4824
0
  if (URI == NULL) {
4825
0
      xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
4826
0
        }
4827
0
    }
4828
0
    return(URI);
4829
0
}
4830
4831
/**
4832
 * Skip an XML (SGML) comment <!-- .... -->
4833
 *  The spec says that "For compatibility, the string "--" (double-hyphen)
4834
 *  must not occur within comments. "
4835
 * This is the slow routine in case the accelerator for ascii didn't work
4836
 *
4837
 *     [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
4838
 * @param ctxt  an XML parser context
4839
 * @param buf  the already parsed part of the buffer
4840
 * @param len  number of bytes in the buffer
4841
 * @param size  allocated size of the buffer
4842
 */
4843
static void
4844
xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf,
4845
0
                       size_t len, size_t size) {
4846
0
    int q, ql;
4847
0
    int r, rl;
4848
0
    int cur, l;
4849
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4850
0
                    XML_MAX_HUGE_LENGTH :
4851
0
                    XML_MAX_TEXT_LENGTH;
4852
4853
0
    if (buf == NULL) {
4854
0
        len = 0;
4855
0
  size = XML_PARSER_BUFFER_SIZE;
4856
0
  buf = xmlMalloc(size);
4857
0
  if (buf == NULL) {
4858
0
      xmlErrMemory(ctxt);
4859
0
      return;
4860
0
  }
4861
0
    }
4862
0
    q = xmlCurrentCharRecover(ctxt, &ql);
4863
0
    if (q == 0)
4864
0
        goto not_terminated;
4865
0
    if (!IS_CHAR(q)) {
4866
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4867
0
                          "xmlParseComment: invalid xmlChar value %d\n",
4868
0
                    q);
4869
0
  xmlFree (buf);
4870
0
  return;
4871
0
    }
4872
0
    NEXTL(ql);
4873
0
    r = xmlCurrentCharRecover(ctxt, &rl);
4874
0
    if (r == 0)
4875
0
        goto not_terminated;
4876
0
    if (!IS_CHAR(r)) {
4877
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4878
0
                          "xmlParseComment: invalid xmlChar value %d\n",
4879
0
                    r);
4880
0
  xmlFree (buf);
4881
0
  return;
4882
0
    }
4883
0
    NEXTL(rl);
4884
0
    cur = xmlCurrentCharRecover(ctxt, &l);
4885
0
    if (cur == 0)
4886
0
        goto not_terminated;
4887
0
    while (IS_CHAR(cur) && /* checked */
4888
0
           ((cur != '>') ||
4889
0
      (r != '-') || (q != '-'))) {
4890
0
  if ((r == '-') && (q == '-')) {
4891
0
      xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
4892
0
  }
4893
0
  if (len + 5 >= size) {
4894
0
      xmlChar *tmp;
4895
0
            int newSize;
4896
4897
0
      newSize = xmlGrowCapacity(size, 1, 1, maxLength);
4898
0
            if (newSize < 0) {
4899
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4900
0
                             "Comment too big found", NULL);
4901
0
                xmlFree (buf);
4902
0
                return;
4903
0
            }
4904
0
      tmp = xmlRealloc(buf, newSize);
4905
0
      if (tmp == NULL) {
4906
0
    xmlErrMemory(ctxt);
4907
0
    xmlFree(buf);
4908
0
    return;
4909
0
      }
4910
0
      buf = tmp;
4911
0
            size = newSize;
4912
0
  }
4913
0
  COPY_BUF(buf, len, q);
4914
4915
0
  q = r;
4916
0
  ql = rl;
4917
0
  r = cur;
4918
0
  rl = l;
4919
4920
0
  NEXTL(l);
4921
0
  cur = xmlCurrentCharRecover(ctxt, &l);
4922
4923
0
    }
4924
0
    buf[len] = 0;
4925
0
    if (cur == 0) {
4926
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4927
0
                       "Comment not terminated \n<!--%.50s\n", buf);
4928
0
    } else if (!IS_CHAR(cur)) {
4929
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
4930
0
                          "xmlParseComment: invalid xmlChar value %d\n",
4931
0
                    cur);
4932
0
    } else {
4933
0
        NEXT;
4934
0
  if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
4935
0
      (!ctxt->disableSAX))
4936
0
      ctxt->sax->comment(ctxt->userData, buf);
4937
0
    }
4938
0
    xmlFree(buf);
4939
0
    return;
4940
0
not_terminated:
4941
0
    xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
4942
0
       "Comment not terminated\n", NULL);
4943
0
    xmlFree(buf);
4944
0
}
4945
4946
/**
4947
 * Parse an XML (SGML) comment. Always consumes '<!'.
4948
 *
4949
 * @deprecated Internal function, don't use.
4950
 *
4951
 *  The spec says that "For compatibility, the string "--" (double-hyphen)
4952
 *  must not occur within comments. "
4953
 *
4954
 *     [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
4955
 * @param ctxt  an XML parser context
4956
 */
4957
void
4958
0
xmlParseComment(xmlParserCtxt *ctxt) {
4959
0
    xmlChar *buf = NULL;
4960
0
    size_t size = XML_PARSER_BUFFER_SIZE;
4961
0
    size_t len = 0;
4962
0
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
4963
0
                       XML_MAX_HUGE_LENGTH :
4964
0
                       XML_MAX_TEXT_LENGTH;
4965
0
    const xmlChar *in;
4966
0
    size_t nbchar = 0;
4967
0
    int ccol;
4968
4969
    /*
4970
     * Check that there is a comment right here.
4971
     */
4972
0
    if ((RAW != '<') || (NXT(1) != '!'))
4973
0
        return;
4974
0
    SKIP(2);
4975
0
    if ((RAW != '-') || (NXT(1) != '-'))
4976
0
        return;
4977
0
    SKIP(2);
4978
0
    GROW;
4979
4980
    /*
4981
     * Accelerated common case where input don't need to be
4982
     * modified before passing it to the handler.
4983
     */
4984
0
    in = ctxt->input->cur;
4985
0
    do {
4986
0
  if (*in == 0xA) {
4987
0
      do {
4988
0
    ctxt->input->line++; ctxt->input->col = 1;
4989
0
    in++;
4990
0
      } while (*in == 0xA);
4991
0
  }
4992
0
get_more:
4993
0
        ccol = ctxt->input->col;
4994
0
  while (((*in > '-') && (*in <= 0x7F)) ||
4995
0
         ((*in >= 0x20) && (*in < '-')) ||
4996
0
         (*in == 0x09)) {
4997
0
        in++;
4998
0
        ccol++;
4999
0
  }
5000
0
  ctxt->input->col = ccol;
5001
0
  if (*in == 0xA) {
5002
0
      do {
5003
0
    ctxt->input->line++; ctxt->input->col = 1;
5004
0
    in++;
5005
0
      } while (*in == 0xA);
5006
0
      goto get_more;
5007
0
  }
5008
0
  nbchar = in - ctxt->input->cur;
5009
  /*
5010
   * save current set of data
5011
   */
5012
0
  if (nbchar > 0) {
5013
0
            if (nbchar > maxLength - len) {
5014
0
                xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
5015
0
                                  "Comment too big found", NULL);
5016
0
                xmlFree(buf);
5017
0
                return;
5018
0
            }
5019
0
            if (buf == NULL) {
5020
0
                if ((*in == '-') && (in[1] == '-'))
5021
0
                    size = nbchar + 1;
5022
0
                else
5023
0
                    size = XML_PARSER_BUFFER_SIZE + nbchar;
5024
0
                buf = xmlMalloc(size);
5025
0
                if (buf == NULL) {
5026
0
                    xmlErrMemory(ctxt);
5027
0
                    return;
5028
0
                }
5029
0
                len = 0;
5030
0
            } else if (len + nbchar + 1 >= size) {
5031
0
                xmlChar *new_buf;
5032
0
                size += len + nbchar + XML_PARSER_BUFFER_SIZE;
5033
0
                new_buf = xmlRealloc(buf, size);
5034
0
                if (new_buf == NULL) {
5035
0
                    xmlErrMemory(ctxt);
5036
0
                    xmlFree(buf);
5037
0
                    return;
5038
0
                }
5039
0
                buf = new_buf;
5040
0
            }
5041
0
            memcpy(&buf[len], ctxt->input->cur, nbchar);
5042
0
            len += nbchar;
5043
0
            buf[len] = 0;
5044
0
  }
5045
0
  ctxt->input->cur = in;
5046
0
  if (*in == 0xA) {
5047
0
      in++;
5048
0
      ctxt->input->line++; ctxt->input->col = 1;
5049
0
  }
5050
0
  if (*in == 0xD) {
5051
0
      in++;
5052
0
      if (*in == 0xA) {
5053
0
    ctxt->input->cur = in;
5054
0
    in++;
5055
0
    ctxt->input->line++; ctxt->input->col = 1;
5056
0
    goto get_more;
5057
0
      }
5058
0
      in--;
5059
0
  }
5060
0
  SHRINK;
5061
0
  GROW;
5062
0
  in = ctxt->input->cur;
5063
0
  if (*in == '-') {
5064
0
      if (in[1] == '-') {
5065
0
          if (in[2] == '>') {
5066
0
        SKIP(3);
5067
0
        if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
5068
0
            (!ctxt->disableSAX)) {
5069
0
      if (buf != NULL)
5070
0
          ctxt->sax->comment(ctxt->userData, buf);
5071
0
      else
5072
0
          ctxt->sax->comment(ctxt->userData, BAD_CAST "");
5073
0
        }
5074
0
        if (buf != NULL)
5075
0
            xmlFree(buf);
5076
0
        return;
5077
0
    }
5078
0
    if (buf != NULL) {
5079
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
5080
0
                          "Double hyphen within comment: "
5081
0
                                      "<!--%.50s\n",
5082
0
              buf);
5083
0
    } else
5084
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT,
5085
0
                          "Double hyphen within comment\n", NULL);
5086
0
    in++;
5087
0
    ctxt->input->col++;
5088
0
      }
5089
0
      in++;
5090
0
      ctxt->input->col++;
5091
0
      goto get_more;
5092
0
  }
5093
0
    } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09) || (*in == 0x0a));
5094
0
    xmlParseCommentComplex(ctxt, buf, len, size);
5095
0
}
5096
5097
5098
/**
5099
 * Parse the name of a PI
5100
 *
5101
 * @deprecated Internal function, don't use.
5102
 *
5103
 *     [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
5104
 *
5105
 * @param ctxt  an XML parser context
5106
 * @returns the PITarget name or NULL
5107
 */
5108
5109
const xmlChar *
5110
0
xmlParsePITarget(xmlParserCtxt *ctxt) {
5111
0
    const xmlChar *name;
5112
5113
0
    name = xmlParseName(ctxt);
5114
0
    if ((name != NULL) &&
5115
0
        ((name[0] == 'x') || (name[0] == 'X')) &&
5116
0
        ((name[1] == 'm') || (name[1] == 'M')) &&
5117
0
        ((name[2] == 'l') || (name[2] == 'L'))) {
5118
0
  int i;
5119
0
  if ((name[0] == 'x') && (name[1] == 'm') &&
5120
0
      (name[2] == 'l') && (name[3] == 0)) {
5121
0
      xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
5122
0
     "XML declaration allowed only at the start of the document\n");
5123
0
      return(name);
5124
0
  } else if (name[3] == 0) {
5125
0
      xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
5126
0
      return(name);
5127
0
  }
5128
0
  for (i = 0;;i++) {
5129
0
      if (xmlW3CPIs[i] == NULL) break;
5130
0
      if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
5131
0
          return(name);
5132
0
  }
5133
0
  xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
5134
0
          "xmlParsePITarget: invalid name prefix 'xml'\n",
5135
0
          NULL, NULL);
5136
0
    }
5137
0
    if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) {
5138
0
  xmlNsErr(ctxt, XML_NS_ERR_COLON,
5139
0
     "colons are forbidden from PI names '%s'\n", name, NULL, NULL);
5140
0
    }
5141
0
    return(name);
5142
0
}
5143
5144
#ifdef LIBXML_CATALOG_ENABLED
5145
/**
5146
 * Parse an XML Catalog Processing Instruction.
5147
 *
5148
 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
5149
 *
5150
 * Occurs only if allowed by the user and if happening in the Misc
5151
 * part of the document before any doctype information
5152
 * This will add the given catalog to the parsing context in order
5153
 * to be used if there is a resolution need further down in the document
5154
 *
5155
 * @param ctxt  an XML parser context
5156
 * @param catalog  the PI value string
5157
 */
5158
5159
static void
5160
0
xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
5161
0
    xmlChar *URL = NULL;
5162
0
    const xmlChar *tmp, *base;
5163
0
    xmlChar marker;
5164
5165
0
    tmp = catalog;
5166
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5167
0
    if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
5168
0
  goto error;
5169
0
    tmp += 7;
5170
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5171
0
    if (*tmp != '=') {
5172
0
  return;
5173
0
    }
5174
0
    tmp++;
5175
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5176
0
    marker = *tmp;
5177
0
    if ((marker != '\'') && (marker != '"'))
5178
0
  goto error;
5179
0
    tmp++;
5180
0
    base = tmp;
5181
0
    while ((*tmp != 0) && (*tmp != marker)) tmp++;
5182
0
    if (*tmp == 0)
5183
0
  goto error;
5184
0
    URL = xmlStrndup(base, tmp - base);
5185
0
    tmp++;
5186
0
    while (IS_BLANK_CH(*tmp)) tmp++;
5187
0
    if (*tmp != 0)
5188
0
  goto error;
5189
5190
0
    if (URL != NULL) {
5191
        /*
5192
         * Unfortunately, the catalog API doesn't report OOM errors.
5193
         * xmlGetLastError isn't very helpful since we don't know
5194
         * where the last error came from. We'd have to reset it
5195
         * before this call and restore it afterwards.
5196
         */
5197
0
  ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
5198
0
  xmlFree(URL);
5199
0
    }
5200
0
    return;
5201
5202
0
error:
5203
0
    xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
5204
0
            "Catalog PI syntax error: %s\n",
5205
0
      catalog, NULL);
5206
0
    if (URL != NULL)
5207
0
  xmlFree(URL);
5208
0
}
5209
#endif
5210
5211
/**
5212
 * Parse an XML Processing Instruction.
5213
 *
5214
 * @deprecated Internal function, don't use.
5215
 *
5216
 *     [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
5217
 *
5218
 * The processing is transferred to SAX once parsed.
5219
 *
5220
 * @param ctxt  an XML parser context
5221
 */
5222
5223
void
5224
0
xmlParsePI(xmlParserCtxt *ctxt) {
5225
0
    xmlChar *buf = NULL;
5226
0
    size_t len = 0;
5227
0
    size_t size = XML_PARSER_BUFFER_SIZE;
5228
0
    size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
5229
0
                       XML_MAX_HUGE_LENGTH :
5230
0
                       XML_MAX_TEXT_LENGTH;
5231
0
    int cur, l;
5232
0
    const xmlChar *target;
5233
5234
0
    if ((RAW == '<') && (NXT(1) == '?')) {
5235
  /*
5236
   * this is a Processing Instruction.
5237
   */
5238
0
  SKIP(2);
5239
5240
  /*
5241
   * Parse the target name and check for special support like
5242
   * namespace.
5243
   */
5244
0
        target = xmlParsePITarget(ctxt);
5245
0
  if (target != NULL) {
5246
0
      if ((RAW == '?') && (NXT(1) == '>')) {
5247
0
    SKIP(2);
5248
5249
    /*
5250
     * SAX: PI detected.
5251
     */
5252
0
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
5253
0
        (ctxt->sax->processingInstruction != NULL))
5254
0
        ctxt->sax->processingInstruction(ctxt->userData,
5255
0
                                         target, NULL);
5256
0
    return;
5257
0
      }
5258
0
      buf = xmlMalloc(size);
5259
0
      if (buf == NULL) {
5260
0
    xmlErrMemory(ctxt);
5261
0
    return;
5262
0
      }
5263
0
      if (SKIP_BLANKS == 0) {
5264
0
    xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
5265
0
        "ParsePI: PI %s space expected\n", target);
5266
0
      }
5267
0
      cur = xmlCurrentCharRecover(ctxt, &l);
5268
0
      while (IS_CHAR(cur) && /* checked */
5269
0
       ((cur != '?') || (NXT(1) != '>'))) {
5270
0
    if (len + 5 >= size) {
5271
0
        xmlChar *tmp;
5272
0
                    int newSize;
5273
5274
0
                    newSize = xmlGrowCapacity(size, 1, 1, maxLength);
5275
0
                    if (newSize < 0) {
5276
0
                        xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
5277
0
                                          "PI %s too big found", target);
5278
0
                        xmlFree(buf);
5279
0
                        return;
5280
0
                    }
5281
0
        tmp = xmlRealloc(buf, newSize);
5282
0
        if (tmp == NULL) {
5283
0
      xmlErrMemory(ctxt);
5284
0
      xmlFree(buf);
5285
0
      return;
5286
0
        }
5287
0
        buf = tmp;
5288
0
                    size = newSize;
5289
0
    }
5290
0
    COPY_BUF(buf, len, cur);
5291
0
    NEXTL(l);
5292
0
    cur = xmlCurrentCharRecover(ctxt, &l);
5293
0
      }
5294
0
      buf[len] = 0;
5295
0
      if (cur != '?') {
5296
0
    xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
5297
0
          "ParsePI: PI %s never end ...\n", target);
5298
0
      } else {
5299
0
    SKIP(2);
5300
5301
0
#ifdef LIBXML_CATALOG_ENABLED
5302
0
    if ((ctxt->inSubset == 0) &&
5303
0
        (xmlStrEqual(target, XML_CATALOG_PI))) {
5304
0
        xmlCatalogAllow allow = xmlCatalogGetDefaults();
5305
5306
0
        if ((ctxt->options & XML_PARSE_CATALOG_PI) &&
5307
0
                        ((allow == XML_CATA_ALLOW_DOCUMENT) ||
5308
0
       (allow == XML_CATA_ALLOW_ALL)))
5309
0
      xmlParseCatalogPI(ctxt, buf);
5310
0
    }
5311
0
#endif
5312
5313
    /*
5314
     * SAX: PI detected.
5315
     */
5316
0
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
5317
0
        (ctxt->sax->processingInstruction != NULL))
5318
0
        ctxt->sax->processingInstruction(ctxt->userData,
5319
0
                                         target, buf);
5320
0
      }
5321
0
      xmlFree(buf);
5322
0
  } else {
5323
0
      xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
5324
0
  }
5325
0
    }
5326
0
}
5327
5328
/**
5329
 * Parse a notation declaration. Always consumes '<!'.
5330
 *
5331
 * @deprecated Internal function, don't use.
5332
 *
5333
 *     [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID |  PublicID)
5334
 *                           S? '>'
5335
 *
5336
 * Hence there is actually 3 choices:
5337
 *
5338
 *     'PUBLIC' S PubidLiteral
5339
 *     'PUBLIC' S PubidLiteral S SystemLiteral
5340
 *     'SYSTEM' S SystemLiteral
5341
 *
5342
 * See the NOTE on #xmlParseExternalID.
5343
 *
5344
 * @param ctxt  an XML parser context
5345
 */
5346
5347
void
5348
0
xmlParseNotationDecl(xmlParserCtxt *ctxt) {
5349
0
    const xmlChar *name;
5350
0
    xmlChar *Pubid;
5351
0
    xmlChar *Systemid;
5352
5353
0
    if ((CUR != '<') || (NXT(1) != '!'))
5354
0
        return;
5355
0
    SKIP(2);
5356
5357
0
    if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
5358
0
#ifdef LIBXML_VALID_ENABLED
5359
0
  int oldInputNr = ctxt->inputNr;
5360
0
#endif
5361
5362
0
  SKIP(8);
5363
0
  if (SKIP_BLANKS_PE == 0) {
5364
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5365
0
         "Space required after '<!NOTATION'\n");
5366
0
      return;
5367
0
  }
5368
5369
0
        name = xmlParseName(ctxt);
5370
0
  if (name == NULL) {
5371
0
      xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
5372
0
      return;
5373
0
  }
5374
0
  if (xmlStrchr(name, ':') != NULL) {
5375
0
      xmlNsErr(ctxt, XML_NS_ERR_COLON,
5376
0
         "colons are forbidden from notation names '%s'\n",
5377
0
         name, NULL, NULL);
5378
0
  }
5379
0
  if (SKIP_BLANKS_PE == 0) {
5380
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5381
0
         "Space required after the NOTATION name'\n");
5382
0
      return;
5383
0
  }
5384
5385
  /*
5386
   * Parse the IDs.
5387
   */
5388
0
  Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
5389
0
  SKIP_BLANKS_PE;
5390
5391
0
  if (RAW == '>') {
5392
0
#ifdef LIBXML_VALID_ENABLED
5393
0
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
5394
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5395
0
                           "Notation declaration doesn't start and stop"
5396
0
                                 " in the same entity\n",
5397
0
                                 NULL, NULL);
5398
0
      }
5399
0
#endif
5400
0
      NEXT;
5401
0
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5402
0
    (ctxt->sax->notationDecl != NULL))
5403
0
    ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
5404
0
  } else {
5405
0
      xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
5406
0
  }
5407
0
  if (Systemid != NULL) xmlFree(Systemid);
5408
0
  if (Pubid != NULL) xmlFree(Pubid);
5409
0
    }
5410
0
}
5411
5412
/**
5413
 * Parse an entity declaration. Always consumes '<!'.
5414
 *
5415
 * @deprecated Internal function, don't use.
5416
 *
5417
 *     [70] EntityDecl ::= GEDecl | PEDecl
5418
 *
5419
 *     [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
5420
 *
5421
 *     [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
5422
 *
5423
 *     [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
5424
 *
5425
 *     [74] PEDef ::= EntityValue | ExternalID
5426
 *
5427
 *     [76] NDataDecl ::= S 'NDATA' S Name
5428
 *
5429
 * [ VC: Notation Declared ]
5430
 * The Name must match the declared name of a notation.
5431
 *
5432
 * @param ctxt  an XML parser context
5433
 */
5434
5435
void
5436
0
xmlParseEntityDecl(xmlParserCtxt *ctxt) {
5437
0
    const xmlChar *name = NULL;
5438
0
    xmlChar *value = NULL;
5439
0
    xmlChar *URI = NULL, *literal = NULL;
5440
0
    const xmlChar *ndata = NULL;
5441
0
    int isParameter = 0;
5442
0
    xmlChar *orig = NULL;
5443
5444
0
    if ((CUR != '<') || (NXT(1) != '!'))
5445
0
        return;
5446
0
    SKIP(2);
5447
5448
    /* GROW; done in the caller */
5449
0
    if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
5450
0
#ifdef LIBXML_VALID_ENABLED
5451
0
  int oldInputNr = ctxt->inputNr;
5452
0
#endif
5453
5454
0
  SKIP(6);
5455
0
  if (SKIP_BLANKS_PE == 0) {
5456
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5457
0
         "Space required after '<!ENTITY'\n");
5458
0
  }
5459
5460
0
  if (RAW == '%') {
5461
0
      NEXT;
5462
0
      if (SKIP_BLANKS_PE == 0) {
5463
0
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5464
0
             "Space required after '%%'\n");
5465
0
      }
5466
0
      isParameter = 1;
5467
0
  }
5468
5469
0
        name = xmlParseName(ctxt);
5470
0
  if (name == NULL) {
5471
0
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5472
0
                     "xmlParseEntityDecl: no name\n");
5473
0
            return;
5474
0
  }
5475
0
  if (xmlStrchr(name, ':') != NULL) {
5476
0
      xmlNsErr(ctxt, XML_NS_ERR_COLON,
5477
0
         "colons are forbidden from entities names '%s'\n",
5478
0
         name, NULL, NULL);
5479
0
  }
5480
0
  if (SKIP_BLANKS_PE == 0) {
5481
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5482
0
         "Space required after the entity name\n");
5483
0
  }
5484
5485
  /*
5486
   * handle the various case of definitions...
5487
   */
5488
0
  if (isParameter) {
5489
0
      if ((RAW == '"') || (RAW == '\'')) {
5490
0
          value = xmlParseEntityValue(ctxt, &orig);
5491
0
    if (value) {
5492
0
        if ((ctxt->sax != NULL) &&
5493
0
      (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5494
0
      ctxt->sax->entityDecl(ctxt->userData, name,
5495
0
                        XML_INTERNAL_PARAMETER_ENTITY,
5496
0
            NULL, NULL, value);
5497
0
    }
5498
0
      } else {
5499
0
          URI = xmlParseExternalID(ctxt, &literal, 1);
5500
0
    if ((URI == NULL) && (literal == NULL)) {
5501
0
        xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
5502
0
    }
5503
0
    if (URI) {
5504
0
                    if (xmlStrchr(URI, '#')) {
5505
0
                        xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
5506
0
                    } else {
5507
0
                        if ((ctxt->sax != NULL) &&
5508
0
                            (!ctxt->disableSAX) &&
5509
0
                            (ctxt->sax->entityDecl != NULL))
5510
0
                            ctxt->sax->entityDecl(ctxt->userData, name,
5511
0
                                        XML_EXTERNAL_PARAMETER_ENTITY,
5512
0
                                        literal, URI, NULL);
5513
0
                    }
5514
0
    }
5515
0
      }
5516
0
  } else {
5517
0
      if ((RAW == '"') || (RAW == '\'')) {
5518
0
          value = xmlParseEntityValue(ctxt, &orig);
5519
0
    if ((ctxt->sax != NULL) &&
5520
0
        (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5521
0
        ctxt->sax->entityDecl(ctxt->userData, name,
5522
0
        XML_INTERNAL_GENERAL_ENTITY,
5523
0
        NULL, NULL, value);
5524
    /*
5525
     * For expat compatibility in SAX mode.
5526
     */
5527
0
    if ((ctxt->myDoc == NULL) ||
5528
0
        (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
5529
0
        if (ctxt->myDoc == NULL) {
5530
0
      ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
5531
0
      if (ctxt->myDoc == NULL) {
5532
0
          xmlErrMemory(ctxt);
5533
0
          goto done;
5534
0
      }
5535
0
      ctxt->myDoc->properties = XML_DOC_INTERNAL;
5536
0
        }
5537
0
        if (ctxt->myDoc->intSubset == NULL) {
5538
0
      ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
5539
0
              BAD_CAST "fake", NULL, NULL);
5540
0
                        if (ctxt->myDoc->intSubset == NULL) {
5541
0
                            xmlErrMemory(ctxt);
5542
0
                            goto done;
5543
0
                        }
5544
0
                    }
5545
5546
0
        xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
5547
0
                    NULL, NULL, value);
5548
0
    }
5549
0
      } else {
5550
0
          URI = xmlParseExternalID(ctxt, &literal, 1);
5551
0
    if ((URI == NULL) && (literal == NULL)) {
5552
0
        xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
5553
0
    }
5554
0
    if (URI) {
5555
0
                    if (xmlStrchr(URI, '#')) {
5556
0
                        xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
5557
0
                    }
5558
0
    }
5559
0
    if ((RAW != '>') && (SKIP_BLANKS_PE == 0)) {
5560
0
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5561
0
           "Space required before 'NDATA'\n");
5562
0
    }
5563
0
    if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
5564
0
        SKIP(5);
5565
0
        if (SKIP_BLANKS_PE == 0) {
5566
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5567
0
               "Space required after 'NDATA'\n");
5568
0
        }
5569
0
        ndata = xmlParseName(ctxt);
5570
0
        if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5571
0
            (ctxt->sax->unparsedEntityDecl != NULL))
5572
0
      ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
5573
0
            literal, URI, ndata);
5574
0
    } else {
5575
0
        if ((ctxt->sax != NULL) &&
5576
0
            (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
5577
0
      ctxt->sax->entityDecl(ctxt->userData, name,
5578
0
            XML_EXTERNAL_GENERAL_PARSED_ENTITY,
5579
0
            literal, URI, NULL);
5580
        /*
5581
         * For expat compatibility in SAX mode.
5582
         * assuming the entity replacement was asked for
5583
         */
5584
0
        if ((ctxt->replaceEntities != 0) &&
5585
0
      ((ctxt->myDoc == NULL) ||
5586
0
      (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
5587
0
      if (ctxt->myDoc == NULL) {
5588
0
          ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
5589
0
          if (ctxt->myDoc == NULL) {
5590
0
              xmlErrMemory(ctxt);
5591
0
        goto done;
5592
0
          }
5593
0
          ctxt->myDoc->properties = XML_DOC_INTERNAL;
5594
0
      }
5595
5596
0
      if (ctxt->myDoc->intSubset == NULL) {
5597
0
          ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
5598
0
            BAD_CAST "fake", NULL, NULL);
5599
0
                            if (ctxt->myDoc->intSubset == NULL) {
5600
0
                                xmlErrMemory(ctxt);
5601
0
                                goto done;
5602
0
                            }
5603
0
                        }
5604
0
      xmlSAX2EntityDecl(ctxt, name,
5605
0
                  XML_EXTERNAL_GENERAL_PARSED_ENTITY,
5606
0
                  literal, URI, NULL);
5607
0
        }
5608
0
    }
5609
0
      }
5610
0
  }
5611
0
  SKIP_BLANKS_PE;
5612
0
  if (RAW != '>') {
5613
0
      xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
5614
0
              "xmlParseEntityDecl: entity %s not terminated\n", name);
5615
0
      xmlHaltParser(ctxt);
5616
0
  } else {
5617
0
#ifdef LIBXML_VALID_ENABLED
5618
0
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
5619
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5620
0
                           "Entity declaration doesn't start and stop in"
5621
0
                                 " the same entity\n",
5622
0
                                 NULL, NULL);
5623
0
      }
5624
0
#endif
5625
0
      NEXT;
5626
0
  }
5627
0
  if (orig != NULL) {
5628
      /*
5629
       * Ugly mechanism to save the raw entity value.
5630
       */
5631
0
      xmlEntityPtr cur = NULL;
5632
5633
0
      if (isParameter) {
5634
0
          if ((ctxt->sax != NULL) &&
5635
0
        (ctxt->sax->getParameterEntity != NULL))
5636
0
        cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
5637
0
      } else {
5638
0
          if ((ctxt->sax != NULL) &&
5639
0
        (ctxt->sax->getEntity != NULL))
5640
0
        cur = ctxt->sax->getEntity(ctxt->userData, name);
5641
0
    if ((cur == NULL) && (ctxt->userData==ctxt)) {
5642
0
        cur = xmlSAX2GetEntity(ctxt, name);
5643
0
    }
5644
0
      }
5645
0
            if ((cur != NULL) && (cur->orig == NULL)) {
5646
0
    cur->orig = orig;
5647
0
                orig = NULL;
5648
0
      }
5649
0
  }
5650
5651
0
done:
5652
0
  if (value != NULL) xmlFree(value);
5653
0
  if (URI != NULL) xmlFree(URI);
5654
0
  if (literal != NULL) xmlFree(literal);
5655
0
        if (orig != NULL) xmlFree(orig);
5656
0
    }
5657
0
}
5658
5659
/**
5660
 * Parse an attribute default declaration
5661
 *
5662
 * @deprecated Internal function, don't use.
5663
 *
5664
 *     [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
5665
 *
5666
 * [ VC: Required Attribute ]
5667
 * if the default declaration is the keyword \#REQUIRED, then the
5668
 * attribute must be specified for all elements of the type in the
5669
 * attribute-list declaration.
5670
 *
5671
 * [ VC: Attribute Default Legal ]
5672
 * The declared default value must meet the lexical constraints of
5673
 * the declared attribute type c.f. #xmlValidateAttributeDecl
5674
 *
5675
 * [ VC: Fixed Attribute Default ]
5676
 * if an attribute has a default value declared with the \#FIXED
5677
 * keyword, instances of that attribute must match the default value.
5678
 *
5679
 * [ WFC: No < in Attribute Values ]
5680
 * handled in #xmlParseAttValue
5681
 *
5682
 * @param ctxt  an XML parser context
5683
 * @param value  Receive a possible fixed default value for the attribute
5684
 * @returns XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
5685
 *          or XML_ATTRIBUTE_FIXED.
5686
 */
5687
5688
int
5689
0
xmlParseDefaultDecl(xmlParserCtxt *ctxt, xmlChar **value) {
5690
0
    int val;
5691
0
    xmlChar *ret;
5692
5693
0
    *value = NULL;
5694
0
    if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
5695
0
  SKIP(9);
5696
0
  return(XML_ATTRIBUTE_REQUIRED);
5697
0
    }
5698
0
    if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
5699
0
  SKIP(8);
5700
0
  return(XML_ATTRIBUTE_IMPLIED);
5701
0
    }
5702
0
    val = XML_ATTRIBUTE_NONE;
5703
0
    if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
5704
0
  SKIP(6);
5705
0
  val = XML_ATTRIBUTE_FIXED;
5706
0
  if (SKIP_BLANKS_PE == 0) {
5707
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5708
0
         "Space required after '#FIXED'\n");
5709
0
  }
5710
0
    }
5711
0
    ret = xmlParseAttValue(ctxt);
5712
0
    if (ret == NULL) {
5713
0
  xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
5714
0
           "Attribute default value declaration error\n");
5715
0
    } else
5716
0
        *value = ret;
5717
0
    return(val);
5718
0
}
5719
5720
/**
5721
 * Parse an Notation attribute type.
5722
 *
5723
 * @deprecated Internal function, don't use.
5724
 *
5725
 * Note: the leading 'NOTATION' S part has already being parsed...
5726
 *
5727
 *     [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
5728
 *
5729
 * [ VC: Notation Attributes ]
5730
 * Values of this type must match one of the notation names included
5731
 * in the declaration; all notation names in the declaration must be declared.
5732
 *
5733
 * @param ctxt  an XML parser context
5734
 * @returns the notation attribute tree built while parsing
5735
 */
5736
5737
xmlEnumeration *
5738
0
xmlParseNotationType(xmlParserCtxt *ctxt) {
5739
0
    const xmlChar *name;
5740
0
    xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
5741
5742
0
    if (RAW != '(') {
5743
0
  xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
5744
0
  return(NULL);
5745
0
    }
5746
0
    do {
5747
0
        NEXT;
5748
0
  SKIP_BLANKS_PE;
5749
0
        name = xmlParseName(ctxt);
5750
0
  if (name == NULL) {
5751
0
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5752
0
         "Name expected in NOTATION declaration\n");
5753
0
            xmlFreeEnumeration(ret);
5754
0
      return(NULL);
5755
0
  }
5756
0
        tmp = NULL;
5757
0
#ifdef LIBXML_VALID_ENABLED
5758
0
        if (ctxt->validate) {
5759
0
            tmp = ret;
5760
0
            while (tmp != NULL) {
5761
0
                if (xmlStrEqual(name, tmp->name)) {
5762
0
                    xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
5763
0
              "standalone: attribute notation value token %s duplicated\n",
5764
0
                                     name, NULL);
5765
0
                    if (!xmlDictOwns(ctxt->dict, name))
5766
0
                        xmlFree((xmlChar *) name);
5767
0
                    break;
5768
0
                }
5769
0
                tmp = tmp->next;
5770
0
            }
5771
0
        }
5772
0
#endif /* LIBXML_VALID_ENABLED */
5773
0
  if (tmp == NULL) {
5774
0
      cur = xmlCreateEnumeration(name);
5775
0
      if (cur == NULL) {
5776
0
                xmlErrMemory(ctxt);
5777
0
                xmlFreeEnumeration(ret);
5778
0
                return(NULL);
5779
0
            }
5780
0
      if (last == NULL) ret = last = cur;
5781
0
      else {
5782
0
    last->next = cur;
5783
0
    last = cur;
5784
0
      }
5785
0
  }
5786
0
  SKIP_BLANKS_PE;
5787
0
    } while (RAW == '|');
5788
0
    if (RAW != ')') {
5789
0
  xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
5790
0
        xmlFreeEnumeration(ret);
5791
0
  return(NULL);
5792
0
    }
5793
0
    NEXT;
5794
0
    return(ret);
5795
0
}
5796
5797
/**
5798
 * Parse an Enumeration attribute type.
5799
 *
5800
 * @deprecated Internal function, don't use.
5801
 *
5802
 *     [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
5803
 *
5804
 * [ VC: Enumeration ]
5805
 * Values of this type must match one of the Nmtoken tokens in
5806
 * the declaration
5807
 *
5808
 * @param ctxt  an XML parser context
5809
 * @returns the enumeration attribute tree built while parsing
5810
 */
5811
5812
xmlEnumeration *
5813
0
xmlParseEnumerationType(xmlParserCtxt *ctxt) {
5814
0
    xmlChar *name;
5815
0
    xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp;
5816
5817
0
    if (RAW != '(') {
5818
0
  xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
5819
0
  return(NULL);
5820
0
    }
5821
0
    do {
5822
0
        NEXT;
5823
0
  SKIP_BLANKS_PE;
5824
0
        name = xmlParseNmtoken(ctxt);
5825
0
  if (name == NULL) {
5826
0
      xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
5827
0
      return(ret);
5828
0
  }
5829
0
        tmp = NULL;
5830
0
#ifdef LIBXML_VALID_ENABLED
5831
0
        if (ctxt->validate) {
5832
0
            tmp = ret;
5833
0
            while (tmp != NULL) {
5834
0
                if (xmlStrEqual(name, tmp->name)) {
5835
0
                    xmlValidityError(ctxt, XML_DTD_DUP_TOKEN,
5836
0
              "standalone: attribute enumeration value token %s duplicated\n",
5837
0
                                     name, NULL);
5838
0
                    if (!xmlDictOwns(ctxt->dict, name))
5839
0
                        xmlFree(name);
5840
0
                    break;
5841
0
                }
5842
0
                tmp = tmp->next;
5843
0
            }
5844
0
        }
5845
0
#endif /* LIBXML_VALID_ENABLED */
5846
0
  if (tmp == NULL) {
5847
0
      cur = xmlCreateEnumeration(name);
5848
0
      if (!xmlDictOwns(ctxt->dict, name))
5849
0
    xmlFree(name);
5850
0
      if (cur == NULL) {
5851
0
                xmlErrMemory(ctxt);
5852
0
                xmlFreeEnumeration(ret);
5853
0
                return(NULL);
5854
0
            }
5855
0
      if (last == NULL) ret = last = cur;
5856
0
      else {
5857
0
    last->next = cur;
5858
0
    last = cur;
5859
0
      }
5860
0
  }
5861
0
  SKIP_BLANKS_PE;
5862
0
    } while (RAW == '|');
5863
0
    if (RAW != ')') {
5864
0
  xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
5865
0
  return(ret);
5866
0
    }
5867
0
    NEXT;
5868
0
    return(ret);
5869
0
}
5870
5871
/**
5872
 * Parse an Enumerated attribute type.
5873
 *
5874
 * @deprecated Internal function, don't use.
5875
 *
5876
 *     [57] EnumeratedType ::= NotationType | Enumeration
5877
 *
5878
 *     [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
5879
 *
5880
 * @param ctxt  an XML parser context
5881
 * @param tree  the enumeration tree built while parsing
5882
 * @returns XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
5883
 */
5884
5885
int
5886
0
xmlParseEnumeratedType(xmlParserCtxt *ctxt, xmlEnumeration **tree) {
5887
0
    if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
5888
0
  SKIP(8);
5889
0
  if (SKIP_BLANKS_PE == 0) {
5890
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5891
0
         "Space required after 'NOTATION'\n");
5892
0
      return(0);
5893
0
  }
5894
0
  *tree = xmlParseNotationType(ctxt);
5895
0
  if (*tree == NULL) return(0);
5896
0
  return(XML_ATTRIBUTE_NOTATION);
5897
0
    }
5898
0
    *tree = xmlParseEnumerationType(ctxt);
5899
0
    if (*tree == NULL) return(0);
5900
0
    return(XML_ATTRIBUTE_ENUMERATION);
5901
0
}
5902
5903
/**
5904
 * Parse the Attribute list def for an element
5905
 *
5906
 * @deprecated Internal function, don't use.
5907
 *
5908
 *     [54] AttType ::= StringType | TokenizedType | EnumeratedType
5909
 *
5910
 *     [55] StringType ::= 'CDATA'
5911
 *
5912
 *     [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
5913
 *                            'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
5914
 *
5915
 * Validity constraints for attribute values syntax are checked in
5916
 * #xmlValidateAttributeValue
5917
 *
5918
 * [ VC: ID ]
5919
 * Values of type ID must match the Name production. A name must not
5920
 * appear more than once in an XML document as a value of this type;
5921
 * i.e., ID values must uniquely identify the elements which bear them.
5922
 *
5923
 * [ VC: One ID per Element Type ]
5924
 * No element type may have more than one ID attribute specified.
5925
 *
5926
 * [ VC: ID Attribute Default ]
5927
 * An ID attribute must have a declared default of \#IMPLIED or \#REQUIRED.
5928
 *
5929
 * [ VC: IDREF ]
5930
 * Values of type IDREF must match the Name production, and values
5931
 * of type IDREFS must match Names; each IDREF Name must match the value
5932
 * of an ID attribute on some element in the XML document; i.e. IDREF
5933
 * values must match the value of some ID attribute.
5934
 *
5935
 * [ VC: Entity Name ]
5936
 * Values of type ENTITY must match the Name production, values
5937
 * of type ENTITIES must match Names; each Entity Name must match the
5938
 * name of an unparsed entity declared in the DTD.
5939
 *
5940
 * [ VC: Name Token ]
5941
 * Values of type NMTOKEN must match the Nmtoken production; values
5942
 * of type NMTOKENS must match Nmtokens.
5943
 *
5944
 * @param ctxt  an XML parser context
5945
 * @param tree  the enumeration tree built while parsing
5946
 * @returns the attribute type
5947
 */
5948
int
5949
0
xmlParseAttributeType(xmlParserCtxt *ctxt, xmlEnumeration **tree) {
5950
0
    if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
5951
0
  SKIP(5);
5952
0
  return(XML_ATTRIBUTE_CDATA);
5953
0
     } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
5954
0
  SKIP(6);
5955
0
  return(XML_ATTRIBUTE_IDREFS);
5956
0
     } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
5957
0
  SKIP(5);
5958
0
  return(XML_ATTRIBUTE_IDREF);
5959
0
     } else if ((RAW == 'I') && (NXT(1) == 'D')) {
5960
0
        SKIP(2);
5961
0
  return(XML_ATTRIBUTE_ID);
5962
0
     } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
5963
0
  SKIP(6);
5964
0
  return(XML_ATTRIBUTE_ENTITY);
5965
0
     } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
5966
0
  SKIP(8);
5967
0
  return(XML_ATTRIBUTE_ENTITIES);
5968
0
     } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
5969
0
  SKIP(8);
5970
0
  return(XML_ATTRIBUTE_NMTOKENS);
5971
0
     } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
5972
0
  SKIP(7);
5973
0
  return(XML_ATTRIBUTE_NMTOKEN);
5974
0
     }
5975
0
     return(xmlParseEnumeratedType(ctxt, tree));
5976
0
}
5977
5978
/**
5979
 * Parse an attribute list declaration for an element. Always consumes '<!'.
5980
 *
5981
 * @deprecated Internal function, don't use.
5982
 *
5983
 *     [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
5984
 *
5985
 *     [53] AttDef ::= S Name S AttType S DefaultDecl
5986
 * @param ctxt  an XML parser context
5987
 */
5988
void
5989
0
xmlParseAttributeListDecl(xmlParserCtxt *ctxt) {
5990
0
    const xmlChar *elemName;
5991
0
    const xmlChar *attrName;
5992
0
    xmlEnumerationPtr tree;
5993
5994
0
    if ((CUR != '<') || (NXT(1) != '!'))
5995
0
        return;
5996
0
    SKIP(2);
5997
5998
0
    if (CMP7(CUR_PTR, 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
5999
0
#ifdef LIBXML_VALID_ENABLED
6000
0
  int oldInputNr = ctxt->inputNr;
6001
0
#endif
6002
6003
0
  SKIP(7);
6004
0
  if (SKIP_BLANKS_PE == 0) {
6005
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6006
0
                     "Space required after '<!ATTLIST'\n");
6007
0
  }
6008
0
        elemName = xmlParseName(ctxt);
6009
0
  if (elemName == NULL) {
6010
0
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6011
0
         "ATTLIST: no name for Element\n");
6012
0
      return;
6013
0
  }
6014
0
  SKIP_BLANKS_PE;
6015
0
  GROW;
6016
0
  while ((RAW != '>') && (PARSER_STOPPED(ctxt) == 0)) {
6017
0
      int type;
6018
0
      int def;
6019
0
      xmlChar *defaultValue = NULL;
6020
6021
0
      GROW;
6022
0
            tree = NULL;
6023
0
      attrName = xmlParseName(ctxt);
6024
0
      if (attrName == NULL) {
6025
0
    xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6026
0
             "ATTLIST: no name for Attribute\n");
6027
0
    break;
6028
0
      }
6029
0
      GROW;
6030
0
      if (SKIP_BLANKS_PE == 0) {
6031
0
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6032
0
            "Space required after the attribute name\n");
6033
0
    break;
6034
0
      }
6035
6036
0
      type = xmlParseAttributeType(ctxt, &tree);
6037
0
      if (type <= 0) {
6038
0
          break;
6039
0
      }
6040
6041
0
      GROW;
6042
0
      if (SKIP_BLANKS_PE == 0) {
6043
0
    xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6044
0
             "Space required after the attribute type\n");
6045
0
          if (tree != NULL)
6046
0
        xmlFreeEnumeration(tree);
6047
0
    break;
6048
0
      }
6049
6050
0
      def = xmlParseDefaultDecl(ctxt, &defaultValue);
6051
0
      if (def <= 0) {
6052
0
                if (defaultValue != NULL)
6053
0
        xmlFree(defaultValue);
6054
0
          if (tree != NULL)
6055
0
        xmlFreeEnumeration(tree);
6056
0
          break;
6057
0
      }
6058
0
      if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL))
6059
0
          xmlAttrNormalizeSpace(defaultValue, defaultValue);
6060
6061
0
      GROW;
6062
0
            if (RAW != '>') {
6063
0
    if (SKIP_BLANKS_PE == 0) {
6064
0
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6065
0
      "Space required after the attribute default value\n");
6066
0
        if (defaultValue != NULL)
6067
0
      xmlFree(defaultValue);
6068
0
        if (tree != NULL)
6069
0
      xmlFreeEnumeration(tree);
6070
0
        break;
6071
0
    }
6072
0
      }
6073
0
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6074
0
    (ctxt->sax->attributeDecl != NULL))
6075
0
    ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
6076
0
                          type, def, defaultValue, tree);
6077
0
      else if (tree != NULL)
6078
0
    xmlFreeEnumeration(tree);
6079
6080
0
      if ((ctxt->sax2) && (defaultValue != NULL) &&
6081
0
          (def != XML_ATTRIBUTE_IMPLIED) &&
6082
0
    (def != XML_ATTRIBUTE_REQUIRED)) {
6083
0
    xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
6084
0
      }
6085
0
      if (ctxt->sax2) {
6086
0
    xmlAddSpecialAttr(ctxt, elemName, attrName, type);
6087
0
      }
6088
0
      if (defaultValue != NULL)
6089
0
          xmlFree(defaultValue);
6090
0
      GROW;
6091
0
  }
6092
0
  if (RAW == '>') {
6093
0
#ifdef LIBXML_VALID_ENABLED
6094
0
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
6095
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6096
0
                                 "Attribute list declaration doesn't start and"
6097
0
                                 " stop in the same entity\n",
6098
0
                                 NULL, NULL);
6099
0
      }
6100
0
#endif
6101
0
      NEXT;
6102
0
  }
6103
0
    }
6104
0
}
6105
6106
/**
6107
 * Handle PEs and check that we don't pop the entity that started
6108
 * a balanced group.
6109
 *
6110
 * @param ctxt  parser context
6111
 * @param openInputNr  input nr of the entity with opening '('
6112
 */
6113
static void
6114
0
xmlSkipBlankCharsPEBalanced(xmlParserCtxt *ctxt, int openInputNr) {
6115
0
    SKIP_BLANKS;
6116
0
    GROW;
6117
6118
0
    (void) openInputNr;
6119
6120
0
    if (!PARSER_EXTERNAL(ctxt) && !PARSER_IN_PE(ctxt))
6121
0
        return;
6122
6123
0
    while (!PARSER_STOPPED(ctxt)) {
6124
0
        if (ctxt->input->cur >= ctxt->input->end) {
6125
0
#ifdef LIBXML_VALID_ENABLED
6126
0
            if ((ctxt->validate) && (ctxt->inputNr <= openInputNr)) {
6127
0
                xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6128
0
                                 "Element content declaration doesn't start "
6129
0
                                 "and stop in the same entity\n",
6130
0
                                 NULL, NULL);
6131
0
            }
6132
0
#endif
6133
0
            if (PARSER_IN_PE(ctxt))
6134
0
                xmlPopPE(ctxt);
6135
0
            else
6136
0
                break;
6137
0
        } else if (RAW == '%') {
6138
0
            xmlParsePERefInternal(ctxt, 0);
6139
0
        } else {
6140
0
            break;
6141
0
        }
6142
6143
0
        SKIP_BLANKS;
6144
0
        GROW;
6145
0
    }
6146
0
}
6147
6148
/**
6149
 * Parse the declaration for a Mixed Element content
6150
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6151
 *
6152
 * @deprecated Internal function, don't use.
6153
 *
6154
 *     [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
6155
 *                    '(' S? '#PCDATA' S? ')'
6156
 *
6157
 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
6158
 *
6159
 * [ VC: No Duplicate Types ]
6160
 * The same name must not appear more than once in a single
6161
 * mixed-content declaration.
6162
 *
6163
 * @param ctxt  an XML parser context
6164
 * @param openInputNr  the input used for the current entity, needed for
6165
 * boundary checks
6166
 * @returns the list of the xmlElementContent describing the element choices
6167
 */
6168
xmlElementContent *
6169
0
xmlParseElementMixedContentDecl(xmlParserCtxt *ctxt, int openInputNr) {
6170
0
    xmlElementContentPtr ret = NULL, cur = NULL, n;
6171
0
    const xmlChar *elem = NULL;
6172
6173
0
    GROW;
6174
0
    if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
6175
0
  SKIP(7);
6176
0
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6177
0
  if (RAW == ')') {
6178
0
#ifdef LIBXML_VALID_ENABLED
6179
0
      if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6180
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6181
0
                                 "Element content declaration doesn't start "
6182
0
                                 "and stop in the same entity\n",
6183
0
                                 NULL, NULL);
6184
0
      }
6185
0
#endif
6186
0
      NEXT;
6187
0
      ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
6188
0
      if (ret == NULL)
6189
0
                goto mem_error;
6190
0
      if (RAW == '*') {
6191
0
    ret->ocur = XML_ELEMENT_CONTENT_MULT;
6192
0
    NEXT;
6193
0
      }
6194
0
      return(ret);
6195
0
  }
6196
0
  if ((RAW == '(') || (RAW == '|')) {
6197
0
      ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
6198
0
      if (ret == NULL)
6199
0
                goto mem_error;
6200
0
  }
6201
0
  while ((RAW == '|') && (PARSER_STOPPED(ctxt) == 0)) {
6202
0
      NEXT;
6203
0
            n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
6204
0
            if (n == NULL)
6205
0
                goto mem_error;
6206
0
      if (elem == NULL) {
6207
0
    n->c1 = cur;
6208
0
    if (cur != NULL)
6209
0
        cur->parent = n;
6210
0
    ret = cur = n;
6211
0
      } else {
6212
0
          cur->c2 = n;
6213
0
    n->parent = cur;
6214
0
    n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6215
0
                if (n->c1 == NULL)
6216
0
                    goto mem_error;
6217
0
    n->c1->parent = n;
6218
0
    cur = n;
6219
0
      }
6220
0
            xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6221
0
      elem = xmlParseName(ctxt);
6222
0
      if (elem == NULL) {
6223
0
    xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6224
0
      "xmlParseElementMixedContentDecl : Name expected\n");
6225
0
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6226
0
    return(NULL);
6227
0
      }
6228
0
            xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6229
0
  }
6230
0
  if ((RAW == ')') && (NXT(1) == '*')) {
6231
0
      if (elem != NULL) {
6232
0
    cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem,
6233
0
                                   XML_ELEMENT_CONTENT_ELEMENT);
6234
0
    if (cur->c2 == NULL)
6235
0
                    goto mem_error;
6236
0
    cur->c2->parent = cur;
6237
0
            }
6238
0
            if (ret != NULL)
6239
0
                ret->ocur = XML_ELEMENT_CONTENT_MULT;
6240
0
#ifdef LIBXML_VALID_ENABLED
6241
0
      if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6242
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6243
0
                                 "Element content declaration doesn't start "
6244
0
                                 "and stop in the same entity\n",
6245
0
                                 NULL, NULL);
6246
0
      }
6247
0
#endif
6248
0
      SKIP(2);
6249
0
  } else {
6250
0
      xmlFreeDocElementContent(ctxt->myDoc, ret);
6251
0
      xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
6252
0
      return(NULL);
6253
0
  }
6254
6255
0
    } else {
6256
0
  xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
6257
0
    }
6258
0
    return(ret);
6259
6260
0
mem_error:
6261
0
    xmlErrMemory(ctxt);
6262
0
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6263
0
    return(NULL);
6264
0
}
6265
6266
/**
6267
 * Parse the declaration for a Mixed Element content
6268
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6269
 *
6270
 *     [47] children ::= (choice | seq) ('?' | '*' | '+')?
6271
 *
6272
 *     [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
6273
 *
6274
 *     [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
6275
 *
6276
 *     [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
6277
 *
6278
 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
6279
 * TODO Parameter-entity replacement text must be properly nested
6280
 *  with parenthesized groups. That is to say, if either of the
6281
 *  opening or closing parentheses in a choice, seq, or Mixed
6282
 *  construct is contained in the replacement text for a parameter
6283
 *  entity, both must be contained in the same replacement text. For
6284
 *  interoperability, if a parameter-entity reference appears in a
6285
 *  choice, seq, or Mixed construct, its replacement text should not
6286
 *  be empty, and neither the first nor last non-blank character of
6287
 *  the replacement text should be a connector (| or ,).
6288
 *
6289
 * @param ctxt  an XML parser context
6290
 * @param openInputNr  the input used for the current entity, needed for
6291
 * boundary checks
6292
 * @param depth  the level of recursion
6293
 * @returns the tree of xmlElementContent describing the element
6294
 *          hierarchy.
6295
 */
6296
static xmlElementContentPtr
6297
xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int openInputNr,
6298
0
                                       int depth) {
6299
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
6300
0
    xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
6301
0
    const xmlChar *elem;
6302
0
    xmlChar type = 0;
6303
6304
0
    if (depth > maxDepth) {
6305
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
6306
0
                "xmlParseElementChildrenContentDecl : depth %d too deep, "
6307
0
                "use XML_PARSE_HUGE\n", depth);
6308
0
  return(NULL);
6309
0
    }
6310
0
    xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6311
0
    if (RAW == '(') {
6312
0
        int newInputNr = ctxt->inputNr;
6313
6314
        /* Recurse on first child */
6315
0
  NEXT;
6316
0
        cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, newInputNr,
6317
0
                                                           depth + 1);
6318
0
        if (cur == NULL)
6319
0
            return(NULL);
6320
0
    } else {
6321
0
  elem = xmlParseName(ctxt);
6322
0
  if (elem == NULL) {
6323
0
      xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
6324
0
      return(NULL);
6325
0
  }
6326
0
        cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6327
0
  if (cur == NULL) {
6328
0
      xmlErrMemory(ctxt);
6329
0
      return(NULL);
6330
0
  }
6331
0
  GROW;
6332
0
  if (RAW == '?') {
6333
0
      cur->ocur = XML_ELEMENT_CONTENT_OPT;
6334
0
      NEXT;
6335
0
  } else if (RAW == '*') {
6336
0
      cur->ocur = XML_ELEMENT_CONTENT_MULT;
6337
0
      NEXT;
6338
0
  } else if (RAW == '+') {
6339
0
      cur->ocur = XML_ELEMENT_CONTENT_PLUS;
6340
0
      NEXT;
6341
0
  } else {
6342
0
      cur->ocur = XML_ELEMENT_CONTENT_ONCE;
6343
0
  }
6344
0
  GROW;
6345
0
    }
6346
0
    while (!PARSER_STOPPED(ctxt)) {
6347
0
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6348
0
        if (RAW == ')')
6349
0
            break;
6350
        /*
6351
   * Each loop we parse one separator and one element.
6352
   */
6353
0
        if (RAW == ',') {
6354
0
      if (type == 0) type = CUR;
6355
6356
      /*
6357
       * Detect "Name | Name , Name" error
6358
       */
6359
0
      else if (type != CUR) {
6360
0
    xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
6361
0
        "xmlParseElementChildrenContentDecl : '%c' expected\n",
6362
0
                      type);
6363
0
    if ((last != NULL) && (last != ret))
6364
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6365
0
    if (ret != NULL)
6366
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6367
0
    return(NULL);
6368
0
      }
6369
0
      NEXT;
6370
6371
0
      op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ);
6372
0
      if (op == NULL) {
6373
0
                xmlErrMemory(ctxt);
6374
0
    if ((last != NULL) && (last != ret))
6375
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6376
0
          xmlFreeDocElementContent(ctxt->myDoc, ret);
6377
0
    return(NULL);
6378
0
      }
6379
0
      if (last == NULL) {
6380
0
    op->c1 = ret;
6381
0
    if (ret != NULL)
6382
0
        ret->parent = op;
6383
0
    ret = cur = op;
6384
0
      } else {
6385
0
          cur->c2 = op;
6386
0
    if (op != NULL)
6387
0
        op->parent = cur;
6388
0
    op->c1 = last;
6389
0
    if (last != NULL)
6390
0
        last->parent = op;
6391
0
    cur =op;
6392
0
    last = NULL;
6393
0
      }
6394
0
  } else if (RAW == '|') {
6395
0
      if (type == 0) type = CUR;
6396
6397
      /*
6398
       * Detect "Name , Name | Name" error
6399
       */
6400
0
      else if (type != CUR) {
6401
0
    xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
6402
0
        "xmlParseElementChildrenContentDecl : '%c' expected\n",
6403
0
          type);
6404
0
    if ((last != NULL) && (last != ret))
6405
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6406
0
    if (ret != NULL)
6407
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6408
0
    return(NULL);
6409
0
      }
6410
0
      NEXT;
6411
6412
0
      op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
6413
0
      if (op == NULL) {
6414
0
                xmlErrMemory(ctxt);
6415
0
    if ((last != NULL) && (last != ret))
6416
0
        xmlFreeDocElementContent(ctxt->myDoc, last);
6417
0
    if (ret != NULL)
6418
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6419
0
    return(NULL);
6420
0
      }
6421
0
      if (last == NULL) {
6422
0
    op->c1 = ret;
6423
0
    if (ret != NULL)
6424
0
        ret->parent = op;
6425
0
    ret = cur = op;
6426
0
      } else {
6427
0
          cur->c2 = op;
6428
0
    if (op != NULL)
6429
0
        op->parent = cur;
6430
0
    op->c1 = last;
6431
0
    if (last != NULL)
6432
0
        last->parent = op;
6433
0
    cur =op;
6434
0
    last = NULL;
6435
0
      }
6436
0
  } else {
6437
0
      xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
6438
0
      if ((last != NULL) && (last != ret))
6439
0
          xmlFreeDocElementContent(ctxt->myDoc, last);
6440
0
      if (ret != NULL)
6441
0
    xmlFreeDocElementContent(ctxt->myDoc, ret);
6442
0
      return(NULL);
6443
0
  }
6444
0
        xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6445
0
        if (RAW == '(') {
6446
0
            int newInputNr = ctxt->inputNr;
6447
6448
      /* Recurse on second child */
6449
0
      NEXT;
6450
0
      last = xmlParseElementChildrenContentDeclPriv(ctxt, newInputNr,
6451
0
                                                          depth + 1);
6452
0
            if (last == NULL) {
6453
0
    if (ret != NULL)
6454
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6455
0
    return(NULL);
6456
0
            }
6457
0
  } else {
6458
0
      elem = xmlParseName(ctxt);
6459
0
      if (elem == NULL) {
6460
0
    xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
6461
0
    if (ret != NULL)
6462
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6463
0
    return(NULL);
6464
0
      }
6465
0
      last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
6466
0
      if (last == NULL) {
6467
0
                xmlErrMemory(ctxt);
6468
0
    if (ret != NULL)
6469
0
        xmlFreeDocElementContent(ctxt->myDoc, ret);
6470
0
    return(NULL);
6471
0
      }
6472
0
      if (RAW == '?') {
6473
0
    last->ocur = XML_ELEMENT_CONTENT_OPT;
6474
0
    NEXT;
6475
0
      } else if (RAW == '*') {
6476
0
    last->ocur = XML_ELEMENT_CONTENT_MULT;
6477
0
    NEXT;
6478
0
      } else if (RAW == '+') {
6479
0
    last->ocur = XML_ELEMENT_CONTENT_PLUS;
6480
0
    NEXT;
6481
0
      } else {
6482
0
    last->ocur = XML_ELEMENT_CONTENT_ONCE;
6483
0
      }
6484
0
  }
6485
0
    }
6486
0
    if ((cur != NULL) && (last != NULL)) {
6487
0
        cur->c2 = last;
6488
0
  if (last != NULL)
6489
0
      last->parent = cur;
6490
0
    }
6491
0
#ifdef LIBXML_VALID_ENABLED
6492
0
    if ((ctxt->validate) && (ctxt->inputNr > openInputNr)) {
6493
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6494
0
                         "Element content declaration doesn't start "
6495
0
                         "and stop in the same entity\n",
6496
0
                         NULL, NULL);
6497
0
    }
6498
0
#endif
6499
0
    NEXT;
6500
0
    if (RAW == '?') {
6501
0
  if (ret != NULL) {
6502
0
      if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
6503
0
          (ret->ocur == XML_ELEMENT_CONTENT_MULT))
6504
0
          ret->ocur = XML_ELEMENT_CONTENT_MULT;
6505
0
      else
6506
0
          ret->ocur = XML_ELEMENT_CONTENT_OPT;
6507
0
  }
6508
0
  NEXT;
6509
0
    } else if (RAW == '*') {
6510
0
  if (ret != NULL) {
6511
0
      ret->ocur = XML_ELEMENT_CONTENT_MULT;
6512
0
      cur = ret;
6513
      /*
6514
       * Some normalization:
6515
       * (a | b* | c?)* == (a | b | c)*
6516
       */
6517
0
      while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
6518
0
    if ((cur->c1 != NULL) &&
6519
0
              ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
6520
0
         (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
6521
0
        cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
6522
0
    if ((cur->c2 != NULL) &&
6523
0
              ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
6524
0
         (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
6525
0
        cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
6526
0
    cur = cur->c2;
6527
0
      }
6528
0
  }
6529
0
  NEXT;
6530
0
    } else if (RAW == '+') {
6531
0
  if (ret != NULL) {
6532
0
      int found = 0;
6533
6534
0
      if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
6535
0
          (ret->ocur == XML_ELEMENT_CONTENT_MULT))
6536
0
          ret->ocur = XML_ELEMENT_CONTENT_MULT;
6537
0
      else
6538
0
          ret->ocur = XML_ELEMENT_CONTENT_PLUS;
6539
      /*
6540
       * Some normalization:
6541
       * (a | b*)+ == (a | b)*
6542
       * (a | b?)+ == (a | b)*
6543
       */
6544
0
      while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) {
6545
0
    if ((cur->c1 != NULL) &&
6546
0
              ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
6547
0
         (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
6548
0
        cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
6549
0
        found = 1;
6550
0
    }
6551
0
    if ((cur->c2 != NULL) &&
6552
0
              ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
6553
0
         (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
6554
0
        cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
6555
0
        found = 1;
6556
0
    }
6557
0
    cur = cur->c2;
6558
0
      }
6559
0
      if (found)
6560
0
    ret->ocur = XML_ELEMENT_CONTENT_MULT;
6561
0
  }
6562
0
  NEXT;
6563
0
    }
6564
0
    return(ret);
6565
0
}
6566
6567
/**
6568
 * Parse the declaration for a Mixed Element content
6569
 * The leading '(' and spaces have been skipped in #xmlParseElementContentDecl
6570
 *
6571
 * @deprecated Internal function, don't use.
6572
 *
6573
 *     [47] children ::= (choice | seq) ('?' | '*' | '+')?
6574
 *
6575
 *     [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
6576
 *
6577
 *     [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
6578
 *
6579
 *     [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
6580
 *
6581
 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
6582
 * TODO Parameter-entity replacement text must be properly nested
6583
 *  with parenthesized groups. That is to say, if either of the
6584
 *  opening or closing parentheses in a choice, seq, or Mixed
6585
 *  construct is contained in the replacement text for a parameter
6586
 *  entity, both must be contained in the same replacement text. For
6587
 *  interoperability, if a parameter-entity reference appears in a
6588
 *  choice, seq, or Mixed construct, its replacement text should not
6589
 *  be empty, and neither the first nor last non-blank character of
6590
 *  the replacement text should be a connector (| or ,).
6591
 *
6592
 * @param ctxt  an XML parser context
6593
 * @param inputchk  the input used for the current entity, needed for boundary checks
6594
 * @returns the tree of xmlElementContent describing the element
6595
 *          hierarchy.
6596
 */
6597
xmlElementContent *
6598
0
xmlParseElementChildrenContentDecl(xmlParserCtxt *ctxt, int inputchk) {
6599
    /* stub left for API/ABI compat */
6600
0
    return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1));
6601
0
}
6602
6603
/**
6604
 * Parse the declaration for an Element content either Mixed or Children,
6605
 * the cases EMPTY and ANY are handled directly in #xmlParseElementDecl
6606
 *
6607
 * @deprecated Internal function, don't use.
6608
 *
6609
 *     [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
6610
 *
6611
 * @param ctxt  an XML parser context
6612
 * @param name  the name of the element being defined.
6613
 * @param result  the Element Content pointer will be stored here if any
6614
 * @returns an xmlElementTypeVal value or -1 on error
6615
 */
6616
6617
int
6618
xmlParseElementContentDecl(xmlParserCtxt *ctxt, const xmlChar *name,
6619
0
                           xmlElementContent **result) {
6620
6621
0
    xmlElementContentPtr tree = NULL;
6622
0
    int openInputNr = ctxt->inputNr;
6623
0
    int res;
6624
6625
0
    *result = NULL;
6626
6627
0
    if (RAW != '(') {
6628
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
6629
0
    "xmlParseElementContentDecl : %s '(' expected\n", name);
6630
0
  return(-1);
6631
0
    }
6632
0
    NEXT;
6633
0
    xmlSkipBlankCharsPEBalanced(ctxt, openInputNr);
6634
0
    if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
6635
0
        tree = xmlParseElementMixedContentDecl(ctxt, openInputNr);
6636
0
  res = XML_ELEMENT_TYPE_MIXED;
6637
0
    } else {
6638
0
        tree = xmlParseElementChildrenContentDeclPriv(ctxt, openInputNr, 1);
6639
0
  res = XML_ELEMENT_TYPE_ELEMENT;
6640
0
    }
6641
0
    if (tree == NULL)
6642
0
        return(-1);
6643
0
    SKIP_BLANKS_PE;
6644
0
    *result = tree;
6645
0
    return(res);
6646
0
}
6647
6648
/**
6649
 * Parse an element declaration. Always consumes '<!'.
6650
 *
6651
 * @deprecated Internal function, don't use.
6652
 *
6653
 *     [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
6654
 *
6655
 * [ VC: Unique Element Type Declaration ]
6656
 * No element type may be declared more than once
6657
 *
6658
 * @param ctxt  an XML parser context
6659
 * @returns the type of the element, or -1 in case of error
6660
 */
6661
int
6662
0
xmlParseElementDecl(xmlParserCtxt *ctxt) {
6663
0
    const xmlChar *name;
6664
0
    int ret = -1;
6665
0
    xmlElementContentPtr content  = NULL;
6666
6667
0
    if ((CUR != '<') || (NXT(1) != '!'))
6668
0
        return(ret);
6669
0
    SKIP(2);
6670
6671
    /* GROW; done in the caller */
6672
0
    if (CMP7(CUR_PTR, 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
6673
0
#ifdef LIBXML_VALID_ENABLED
6674
0
  int oldInputNr = ctxt->inputNr;
6675
0
#endif
6676
6677
0
  SKIP(7);
6678
0
  if (SKIP_BLANKS_PE == 0) {
6679
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6680
0
               "Space required after 'ELEMENT'\n");
6681
0
      return(-1);
6682
0
  }
6683
0
        name = xmlParseName(ctxt);
6684
0
  if (name == NULL) {
6685
0
      xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6686
0
         "xmlParseElementDecl: no name for Element\n");
6687
0
      return(-1);
6688
0
  }
6689
0
  if (SKIP_BLANKS_PE == 0) {
6690
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6691
0
         "Space required after the element name\n");
6692
0
  }
6693
0
  if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
6694
0
      SKIP(5);
6695
      /*
6696
       * Element must always be empty.
6697
       */
6698
0
      ret = XML_ELEMENT_TYPE_EMPTY;
6699
0
  } else if ((RAW == 'A') && (NXT(1) == 'N') &&
6700
0
             (NXT(2) == 'Y')) {
6701
0
      SKIP(3);
6702
      /*
6703
       * Element is a generic container.
6704
       */
6705
0
      ret = XML_ELEMENT_TYPE_ANY;
6706
0
  } else if (RAW == '(') {
6707
0
      ret = xmlParseElementContentDecl(ctxt, name, &content);
6708
0
            if (ret <= 0)
6709
0
                return(-1);
6710
0
  } else {
6711
      /*
6712
       * [ WFC: PEs in Internal Subset ] error handling.
6713
       */
6714
0
            xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
6715
0
                  "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
6716
0
      return(-1);
6717
0
  }
6718
6719
0
  SKIP_BLANKS_PE;
6720
6721
0
  if (RAW != '>') {
6722
0
      xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
6723
0
      if (content != NULL) {
6724
0
    xmlFreeDocElementContent(ctxt->myDoc, content);
6725
0
      }
6726
0
  } else {
6727
0
#ifdef LIBXML_VALID_ENABLED
6728
0
      if ((ctxt->validate) && (ctxt->inputNr > oldInputNr)) {
6729
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6730
0
                                 "Element declaration doesn't start and stop in"
6731
0
                                 " the same entity\n",
6732
0
                                 NULL, NULL);
6733
0
      }
6734
0
#endif
6735
6736
0
      NEXT;
6737
0
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6738
0
    (ctxt->sax->elementDecl != NULL)) {
6739
0
    if (content != NULL)
6740
0
        content->parent = NULL;
6741
0
          ctxt->sax->elementDecl(ctxt->userData, name, ret,
6742
0
                           content);
6743
0
    if ((content != NULL) && (content->parent == NULL)) {
6744
        /*
6745
         * this is a trick: if xmlAddElementDecl is called,
6746
         * instead of copying the full tree it is plugged directly
6747
         * if called from the parser. Avoid duplicating the
6748
         * interfaces or change the API/ABI
6749
         */
6750
0
        xmlFreeDocElementContent(ctxt->myDoc, content);
6751
0
    }
6752
0
      } else if (content != NULL) {
6753
0
    xmlFreeDocElementContent(ctxt->myDoc, content);
6754
0
      }
6755
0
  }
6756
0
    }
6757
0
    return(ret);
6758
0
}
6759
6760
/**
6761
 * Parse a conditional section. Always consumes '<!['.
6762
 *
6763
 *     [61] conditionalSect ::= includeSect | ignoreSect
6764
 *     [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
6765
 *     [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
6766
 *     [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>'
6767
 *                                 Ignore)*
6768
 *     [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
6769
 * @param ctxt  an XML parser context
6770
 */
6771
6772
static void
6773
0
xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
6774
0
    size_t depth = 0;
6775
0
    int isFreshPE = 0;
6776
0
    int oldInputNr = ctxt->inputNr;
6777
0
    int declInputNr = ctxt->inputNr;
6778
6779
0
    while (!PARSER_STOPPED(ctxt)) {
6780
0
        if (ctxt->input->cur >= ctxt->input->end) {
6781
0
            if (ctxt->inputNr <= oldInputNr) {
6782
0
                xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
6783
0
                return;
6784
0
            }
6785
6786
0
            xmlPopPE(ctxt);
6787
0
            declInputNr = ctxt->inputNr;
6788
0
        } else if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
6789
0
            SKIP(3);
6790
0
            SKIP_BLANKS_PE;
6791
6792
0
            isFreshPE = 0;
6793
6794
0
            if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
6795
0
                SKIP(7);
6796
0
                SKIP_BLANKS_PE;
6797
0
                if (RAW != '[') {
6798
0
                    xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
6799
0
                    return;
6800
0
                }
6801
0
#ifdef LIBXML_VALID_ENABLED
6802
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6803
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6804
0
                                     "All markup of the conditional section is"
6805
0
                                     " not in the same entity\n",
6806
0
                                     NULL, NULL);
6807
0
                }
6808
0
#endif
6809
0
                NEXT;
6810
6811
0
                depth++;
6812
0
            } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
6813
0
                size_t ignoreDepth = 0;
6814
6815
0
                SKIP(6);
6816
0
                SKIP_BLANKS_PE;
6817
0
                if (RAW != '[') {
6818
0
                    xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
6819
0
                    return;
6820
0
                }
6821
0
#ifdef LIBXML_VALID_ENABLED
6822
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6823
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6824
0
                                     "All markup of the conditional section is"
6825
0
                                     " not in the same entity\n",
6826
0
                                     NULL, NULL);
6827
0
                }
6828
0
#endif
6829
0
                NEXT;
6830
6831
0
                while (PARSER_STOPPED(ctxt) == 0) {
6832
0
                    if (RAW == 0) {
6833
0
                        xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
6834
0
                        return;
6835
0
                    }
6836
0
                    if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
6837
0
                        SKIP(3);
6838
0
                        ignoreDepth++;
6839
                        /* Check for integer overflow */
6840
0
                        if (ignoreDepth == 0) {
6841
0
                            xmlErrMemory(ctxt);
6842
0
                            return;
6843
0
                        }
6844
0
                    } else if ((RAW == ']') && (NXT(1) == ']') &&
6845
0
                               (NXT(2) == '>')) {
6846
0
                        SKIP(3);
6847
0
                        if (ignoreDepth == 0)
6848
0
                            break;
6849
0
                        ignoreDepth--;
6850
0
                    } else {
6851
0
                        NEXT;
6852
0
                    }
6853
0
                }
6854
6855
0
#ifdef LIBXML_VALID_ENABLED
6856
0
                if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6857
0
        xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6858
0
                                     "All markup of the conditional section is"
6859
0
                                     " not in the same entity\n",
6860
0
                                     NULL, NULL);
6861
0
                }
6862
0
#endif
6863
0
            } else {
6864
0
                xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
6865
0
                return;
6866
0
            }
6867
0
        } else if ((depth > 0) &&
6868
0
                   (RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
6869
0
            if (isFreshPE) {
6870
0
                xmlFatalErrMsg(ctxt, XML_ERR_CONDSEC_INVALID,
6871
0
                               "Parameter entity must match "
6872
0
                               "extSubsetDecl\n");
6873
0
                return;
6874
0
            }
6875
6876
0
            depth--;
6877
0
#ifdef LIBXML_VALID_ENABLED
6878
0
            if ((ctxt->validate) && (ctxt->inputNr > declInputNr)) {
6879
0
    xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
6880
0
                                 "All markup of the conditional section is not"
6881
0
                                 " in the same entity\n",
6882
0
                                 NULL, NULL);
6883
0
            }
6884
0
#endif
6885
0
            SKIP(3);
6886
0
        } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
6887
0
            isFreshPE = 0;
6888
0
            xmlParseMarkupDecl(ctxt);
6889
0
        } else if (RAW == '%') {
6890
0
            xmlParsePERefInternal(ctxt, 1);
6891
0
            if (ctxt->inputNr > declInputNr) {
6892
0
                isFreshPE = 1;
6893
0
                declInputNr = ctxt->inputNr;
6894
0
            }
6895
0
        } else {
6896
0
            xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
6897
0
            return;
6898
0
        }
6899
6900
0
        if (depth == 0)
6901
0
            break;
6902
6903
0
        SKIP_BLANKS;
6904
0
        SHRINK;
6905
0
        GROW;
6906
0
    }
6907
0
}
6908
6909
/**
6910
 * Parse markup declarations. Always consumes '<!' or '<?'.
6911
 *
6912
 * @deprecated Internal function, don't use.
6913
 *
6914
 *     [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
6915
 *                         NotationDecl | PI | Comment
6916
 *
6917
 * [ VC: Proper Declaration/PE Nesting ]
6918
 * Parameter-entity replacement text must be properly nested with
6919
 * markup declarations. That is to say, if either the first character
6920
 * or the last character of a markup declaration (markupdecl above) is
6921
 * contained in the replacement text for a parameter-entity reference,
6922
 * both must be contained in the same replacement text.
6923
 *
6924
 * [ WFC: PEs in Internal Subset ]
6925
 * In the internal DTD subset, parameter-entity references can occur
6926
 * only where markup declarations can occur, not within markup declarations.
6927
 * (This does not apply to references that occur in external parameter
6928
 * entities or to the external subset.)
6929
 *
6930
 * @param ctxt  an XML parser context
6931
 */
6932
void
6933
0
xmlParseMarkupDecl(xmlParserCtxt *ctxt) {
6934
0
    GROW;
6935
0
    if (CUR == '<') {
6936
0
        if (NXT(1) == '!') {
6937
0
      switch (NXT(2)) {
6938
0
          case 'E':
6939
0
        if (NXT(3) == 'L')
6940
0
      xmlParseElementDecl(ctxt);
6941
0
        else if (NXT(3) == 'N')
6942
0
      xmlParseEntityDecl(ctxt);
6943
0
                    else
6944
0
                        SKIP(2);
6945
0
        break;
6946
0
          case 'A':
6947
0
        xmlParseAttributeListDecl(ctxt);
6948
0
        break;
6949
0
          case 'N':
6950
0
        xmlParseNotationDecl(ctxt);
6951
0
        break;
6952
0
          case '-':
6953
0
        xmlParseComment(ctxt);
6954
0
        break;
6955
0
    default:
6956
0
                    xmlFatalErr(ctxt,
6957
0
                                ctxt->inSubset == 2 ?
6958
0
                                    XML_ERR_EXT_SUBSET_NOT_FINISHED :
6959
0
                                    XML_ERR_INT_SUBSET_NOT_FINISHED,
6960
0
                                NULL);
6961
0
                    SKIP(2);
6962
0
        break;
6963
0
      }
6964
0
  } else if (NXT(1) == '?') {
6965
0
      xmlParsePI(ctxt);
6966
0
  }
6967
0
    }
6968
0
}
6969
6970
/**
6971
 * Parse an XML declaration header for external entities
6972
 *
6973
 * @deprecated Internal function, don't use.
6974
 *
6975
 *     [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
6976
 * @param ctxt  an XML parser context
6977
 */
6978
6979
void
6980
0
xmlParseTextDecl(xmlParserCtxt *ctxt) {
6981
0
    xmlChar *version;
6982
6983
    /*
6984
     * We know that '<?xml' is here.
6985
     */
6986
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
6987
0
  SKIP(5);
6988
0
    } else {
6989
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
6990
0
  return;
6991
0
    }
6992
6993
0
    if (SKIP_BLANKS == 0) {
6994
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6995
0
           "Space needed after '<?xml'\n");
6996
0
    }
6997
6998
    /*
6999
     * We may have the VersionInfo here.
7000
     */
7001
0
    version = xmlParseVersionInfo(ctxt);
7002
0
    if (version == NULL) {
7003
0
  version = xmlCharStrdup(XML_DEFAULT_VERSION);
7004
0
        if (version == NULL) {
7005
0
            xmlErrMemory(ctxt);
7006
0
            return;
7007
0
        }
7008
0
    } else {
7009
0
  if (SKIP_BLANKS == 0) {
7010
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7011
0
               "Space needed here\n");
7012
0
  }
7013
0
    }
7014
0
    ctxt->input->version = version;
7015
7016
    /*
7017
     * We must have the encoding declaration
7018
     */
7019
0
    xmlParseEncodingDecl(ctxt);
7020
7021
0
    SKIP_BLANKS;
7022
0
    if ((RAW == '?') && (NXT(1) == '>')) {
7023
0
        SKIP(2);
7024
0
    } else if (RAW == '>') {
7025
        /* Deprecated old WD ... */
7026
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
7027
0
  NEXT;
7028
0
    } else {
7029
0
        int c;
7030
7031
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
7032
0
        while ((PARSER_STOPPED(ctxt) == 0) && ((c = CUR) != 0)) {
7033
0
            NEXT;
7034
0
            if (c == '>')
7035
0
                break;
7036
0
        }
7037
0
    }
7038
0
}
7039
7040
/**
7041
 * Parse Markup declarations from an external subset
7042
 *
7043
 * @deprecated Internal function, don't use.
7044
 *
7045
 *     [30] extSubset ::= textDecl? extSubsetDecl
7046
 *
7047
 *     [31] extSubsetDecl ::= (markupdecl | conditionalSect |
7048
 *                             PEReference | S) *
7049
 * @param ctxt  an XML parser context
7050
 * @param publicId  the public identifier
7051
 * @param systemId  the system identifier (URL)
7052
 */
7053
void
7054
xmlParseExternalSubset(xmlParserCtxt *ctxt, const xmlChar *publicId,
7055
0
                       const xmlChar *systemId) {
7056
0
    int oldInputNr;
7057
7058
0
    xmlCtxtInitializeLate(ctxt);
7059
7060
0
    xmlDetectEncoding(ctxt);
7061
7062
0
    if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
7063
0
  xmlParseTextDecl(ctxt);
7064
0
    }
7065
0
    if (ctxt->myDoc == NULL) {
7066
0
        ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
7067
0
  if (ctxt->myDoc == NULL) {
7068
0
      xmlErrMemory(ctxt);
7069
0
      return;
7070
0
  }
7071
0
  ctxt->myDoc->properties = XML_DOC_INTERNAL;
7072
0
    }
7073
0
    if ((ctxt->myDoc->intSubset == NULL) &&
7074
0
        (xmlCreateIntSubset(ctxt->myDoc, NULL, publicId, systemId) == NULL)) {
7075
0
        xmlErrMemory(ctxt);
7076
0
    }
7077
7078
0
    ctxt->inSubset = 2;
7079
0
    oldInputNr = ctxt->inputNr;
7080
7081
0
    SKIP_BLANKS;
7082
0
    while (!PARSER_STOPPED(ctxt)) {
7083
0
        if (ctxt->input->cur >= ctxt->input->end) {
7084
0
            if (ctxt->inputNr <= oldInputNr) {
7085
0
                xmlParserCheckEOF(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED);
7086
0
                break;
7087
0
            }
7088
7089
0
            xmlPopPE(ctxt);
7090
0
        } else if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
7091
0
            xmlParseConditionalSections(ctxt);
7092
0
        } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
7093
0
            xmlParseMarkupDecl(ctxt);
7094
0
        } else if (RAW == '%') {
7095
0
            xmlParsePERefInternal(ctxt, 1);
7096
0
        } else {
7097
0
            xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
7098
7099
0
            while (ctxt->inputNr > oldInputNr)
7100
0
                xmlPopPE(ctxt);
7101
0
            break;
7102
0
        }
7103
0
        SKIP_BLANKS;
7104
0
        SHRINK;
7105
0
        GROW;
7106
0
    }
7107
0
}
7108
7109
/**
7110
 * Parse and handle entity references in content, depending on the SAX
7111
 * interface, this may end-up in a call to character() if this is a
7112
 * CharRef, a predefined entity, if there is no reference() callback.
7113
 * or if the parser was asked to switch to that mode.
7114
 *
7115
 * @deprecated Internal function, don't use.
7116
 *
7117
 * Always consumes '&'.
7118
 *
7119
 *     [67] Reference ::= EntityRef | CharRef
7120
 * @param ctxt  an XML parser context
7121
 */
7122
void
7123
0
xmlParseReference(xmlParserCtxt *ctxt) {
7124
0
    xmlEntityPtr ent = NULL;
7125
0
    const xmlChar *name;
7126
0
    xmlChar *val;
7127
7128
0
    if (RAW != '&')
7129
0
        return;
7130
7131
    /*
7132
     * Simple case of a CharRef
7133
     */
7134
0
    if (NXT(1) == '#') {
7135
0
  int i = 0;
7136
0
  xmlChar out[16];
7137
0
  int value = xmlParseCharRef(ctxt);
7138
7139
0
  if (value == 0)
7140
0
      return;
7141
7142
        /*
7143
         * Just encode the value in UTF-8
7144
         */
7145
0
        COPY_BUF(out, i, value);
7146
0
        out[i] = 0;
7147
0
        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
7148
0
            (!ctxt->disableSAX))
7149
0
            ctxt->sax->characters(ctxt->userData, out, i);
7150
0
  return;
7151
0
    }
7152
7153
    /*
7154
     * We are seeing an entity reference
7155
     */
7156
0
    name = xmlParseEntityRefInternal(ctxt);
7157
0
    if (name == NULL)
7158
0
        return;
7159
0
    ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 0);
7160
0
    if (ent == NULL) {
7161
        /*
7162
         * Create a reference for undeclared entities.
7163
         */
7164
0
        if ((ctxt->replaceEntities == 0) &&
7165
0
            (ctxt->sax != NULL) &&
7166
0
            (ctxt->disableSAX == 0) &&
7167
0
            (ctxt->sax->reference != NULL)) {
7168
0
            ctxt->sax->reference(ctxt->userData, name);
7169
0
        }
7170
0
        return;
7171
0
    }
7172
0
    if (!ctxt->wellFormed)
7173
0
  return;
7174
7175
    /* special case of predefined entities */
7176
0
    if ((ent->name == NULL) ||
7177
0
        (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
7178
0
  val = ent->content;
7179
0
  if (val == NULL) return;
7180
  /*
7181
   * inline the entity.
7182
   */
7183
0
  if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
7184
0
      (!ctxt->disableSAX))
7185
0
      ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
7186
0
  return;
7187
0
    }
7188
7189
    /*
7190
     * Some users try to parse entities on their own and used to set
7191
     * the renamed "checked" member. Fix the flags to cover this
7192
     * case.
7193
     */
7194
0
    if (((ent->flags & XML_ENT_PARSED) == 0) && (ent->children != NULL))
7195
0
        ent->flags |= XML_ENT_PARSED;
7196
7197
    /*
7198
     * The first reference to the entity trigger a parsing phase
7199
     * where the ent->children is filled with the result from
7200
     * the parsing.
7201
     * Note: external parsed entities will not be loaded, it is not
7202
     * required for a non-validating parser, unless the parsing option
7203
     * of validating, or substituting entities were given. Doing so is
7204
     * far more secure as the parser will only process data coming from
7205
     * the document entity by default.
7206
     *
7207
     * FIXME: This doesn't work correctly since entities can be
7208
     * expanded with different namespace declarations in scope.
7209
     * For example:
7210
     *
7211
     * <!DOCTYPE doc [
7212
     *   <!ENTITY ent "<ns:elem/>">
7213
     * ]>
7214
     * <doc>
7215
     *   <decl1 xmlns:ns="urn:ns1">
7216
     *     &ent;
7217
     *   </decl1>
7218
     *   <decl2 xmlns:ns="urn:ns2">
7219
     *     &ent;
7220
     *   </decl2>
7221
     * </doc>
7222
     *
7223
     * Proposed fix:
7224
     *
7225
     * - Ignore current namespace declarations when parsing the
7226
     *   entity. If a prefix can't be resolved, don't report an error
7227
     *   but mark it as unresolved.
7228
     * - Try to resolve these prefixes when expanding the entity.
7229
     *   This will require a specialized version of xmlStaticCopyNode
7230
     *   which can also make use of the namespace hash table to avoid
7231
     *   quadratic behavior.
7232
     *
7233
     * Alternatively, we could simply reparse the entity on each
7234
     * expansion like we already do with custom SAX callbacks.
7235
     * External entity content should be cached in this case.
7236
     */
7237
0
    if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
7238
0
        (((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
7239
0
         ((ctxt->replaceEntities) ||
7240
0
          (ctxt->validate)))) {
7241
0
        if ((ent->flags & XML_ENT_PARSED) == 0) {
7242
0
            xmlCtxtParseEntity(ctxt, ent);
7243
0
        } else if (ent->children == NULL) {
7244
            /*
7245
             * Probably running in SAX mode and the callbacks don't
7246
             * build the entity content. Parse the entity again.
7247
             *
7248
             * This will also be triggered in normal tree builder mode
7249
             * if an entity happens to be empty, causing unnecessary
7250
             * reloads. It's hard to come up with a reliable check in
7251
             * which mode we're running.
7252
             */
7253
0
            xmlCtxtParseEntity(ctxt, ent);
7254
0
        }
7255
0
    }
7256
7257
    /*
7258
     * We also check for amplification if entities aren't substituted.
7259
     * They might be expanded later.
7260
     */
7261
0
    if (xmlParserEntityCheck(ctxt, ent->expandedSize))
7262
0
        return;
7263
7264
0
    if ((ctxt->sax == NULL) || (ctxt->disableSAX))
7265
0
        return;
7266
7267
0
    if (ctxt->replaceEntities == 0) {
7268
  /*
7269
   * Create a reference
7270
   */
7271
0
        if (ctxt->sax->reference != NULL)
7272
0
      ctxt->sax->reference(ctxt->userData, ent->name);
7273
0
    } else if ((ent->children != NULL) && (ctxt->node != NULL)) {
7274
0
        xmlNodePtr copy, cur;
7275
7276
        /*
7277
         * Seems we are generating the DOM content, copy the tree
7278
   */
7279
0
        cur = ent->children;
7280
7281
        /*
7282
         * Handle first text node with SAX to coalesce text efficiently
7283
         */
7284
0
        if ((cur->type == XML_TEXT_NODE) ||
7285
0
            (cur->type == XML_CDATA_SECTION_NODE)) {
7286
0
            int len = xmlStrlen(cur->content);
7287
7288
0
            if ((cur->type == XML_TEXT_NODE) ||
7289
0
                (ctxt->options & XML_PARSE_NOCDATA)) {
7290
0
                if (ctxt->sax->characters != NULL)
7291
0
                    ctxt->sax->characters(ctxt, cur->content, len);
7292
0
            } else {
7293
0
                if (ctxt->sax->cdataBlock != NULL)
7294
0
                    ctxt->sax->cdataBlock(ctxt, cur->content, len);
7295
0
            }
7296
7297
0
            cur = cur->next;
7298
0
        }
7299
7300
0
        while (cur != NULL) {
7301
0
            xmlNodePtr last;
7302
7303
            /*
7304
             * Handle last text node with SAX to coalesce text efficiently
7305
             */
7306
0
            if ((cur->next == NULL) &&
7307
0
                ((cur->type == XML_TEXT_NODE) ||
7308
0
                 (cur->type == XML_CDATA_SECTION_NODE))) {
7309
0
                int len = xmlStrlen(cur->content);
7310
7311
0
                if ((cur->type == XML_TEXT_NODE) ||
7312
0
                    (ctxt->options & XML_PARSE_NOCDATA)) {
7313
0
                    if (ctxt->sax->characters != NULL)
7314
0
                        ctxt->sax->characters(ctxt, cur->content, len);
7315
0
                } else {
7316
0
                    if (ctxt->sax->cdataBlock != NULL)
7317
0
                        ctxt->sax->cdataBlock(ctxt, cur->content, len);
7318
0
                }
7319
7320
0
                break;
7321
0
            }
7322
7323
            /*
7324
             * Reset coalesce buffer stats only for non-text nodes.
7325
             */
7326
0
            ctxt->nodemem = 0;
7327
0
            ctxt->nodelen = 0;
7328
7329
0
            copy = xmlDocCopyNode(cur, ctxt->myDoc, 1);
7330
7331
0
            if (copy == NULL) {
7332
0
                xmlErrMemory(ctxt);
7333
0
                break;
7334
0
            }
7335
7336
0
            if (ctxt->parseMode == XML_PARSE_READER) {
7337
                /* Needed for reader */
7338
0
                copy->extra = cur->extra;
7339
                /* Maybe needed for reader */
7340
0
                copy->_private = cur->_private;
7341
0
            }
7342
7343
0
            copy->parent = ctxt->node;
7344
0
            last = ctxt->node->last;
7345
0
            if (last == NULL) {
7346
0
                ctxt->node->children = copy;
7347
0
            } else {
7348
0
                last->next = copy;
7349
0
                copy->prev = last;
7350
0
            }
7351
0
            ctxt->node->last = copy;
7352
7353
0
            cur = cur->next;
7354
0
        }
7355
0
    }
7356
0
}
7357
7358
static void
7359
0
xmlHandleUndeclaredEntity(xmlParserCtxtPtr ctxt, const xmlChar *name) {
7360
    /*
7361
     * [ WFC: Entity Declared ]
7362
     * In a document without any DTD, a document with only an
7363
     * internal DTD subset which contains no parameter entity
7364
     * references, or a document with "standalone='yes'", the
7365
     * Name given in the entity reference must match that in an
7366
     * entity declaration, except that well-formed documents
7367
     * need not declare any of the following entities: amp, lt,
7368
     * gt, apos, quot.
7369
     * The declaration of a parameter entity must precede any
7370
     * reference to it.
7371
     * Similarly, the declaration of a general entity must
7372
     * precede any reference to it which appears in a default
7373
     * value in an attribute-list declaration. Note that if
7374
     * entities are declared in the external subset or in
7375
     * external parameter entities, a non-validating processor
7376
     * is not obligated to read and process their declarations;
7377
     * for such documents, the rule that an entity must be
7378
     * declared is a well-formedness constraint only if
7379
     * standalone='yes'.
7380
     */
7381
0
    if ((ctxt->standalone == 1) ||
7382
0
        ((ctxt->hasExternalSubset == 0) &&
7383
0
         (ctxt->hasPErefs == 0))) {
7384
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
7385
0
                          "Entity '%s' not defined\n", name);
7386
0
#ifdef LIBXML_VALID_ENABLED
7387
0
    } else if (ctxt->validate) {
7388
        /*
7389
         * [ VC: Entity Declared ]
7390
         * In a document with an external subset or external
7391
         * parameter entities with "standalone='no'", ...
7392
         * ... The declaration of a parameter entity must
7393
         * precede any reference to it...
7394
         */
7395
0
        xmlValidityError(ctxt, XML_ERR_UNDECLARED_ENTITY,
7396
0
                         "Entity '%s' not defined\n", name, NULL);
7397
0
#endif
7398
0
    } else if ((ctxt->loadsubset & ~XML_SKIP_IDS) ||
7399
0
               ((ctxt->replaceEntities) &&
7400
0
                ((ctxt->options & XML_PARSE_NO_XXE) == 0))) {
7401
        /*
7402
         * Also raise a non-fatal error
7403
         *
7404
         * - if the external subset is loaded and all entity declarations
7405
         *   should be available, or
7406
         * - entity substition was requested without restricting
7407
         *   external entity access.
7408
         */
7409
0
        xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
7410
0
                     "Entity '%s' not defined\n", name);
7411
0
    } else {
7412
0
        xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7413
0
                      "Entity '%s' not defined\n", name, NULL);
7414
0
    }
7415
7416
0
    ctxt->valid = 0;
7417
0
}
7418
7419
static xmlEntityPtr
7420
0
xmlLookupGeneralEntity(xmlParserCtxtPtr ctxt, const xmlChar *name, int inAttr) {
7421
0
    xmlEntityPtr ent = NULL;
7422
7423
    /*
7424
     * Predefined entities override any extra definition
7425
     */
7426
0
    if ((ctxt->options & XML_PARSE_OLDSAX) == 0) {
7427
0
        ent = xmlGetPredefinedEntity(name);
7428
0
        if (ent != NULL)
7429
0
            return(ent);
7430
0
    }
7431
7432
    /*
7433
     * Ask first SAX for entity resolution, otherwise try the
7434
     * entities which may have stored in the parser context.
7435
     */
7436
0
    if (ctxt->sax != NULL) {
7437
0
  if (ctxt->sax->getEntity != NULL)
7438
0
      ent = ctxt->sax->getEntity(ctxt->userData, name);
7439
0
  if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
7440
0
      (ctxt->options & XML_PARSE_OLDSAX))
7441
0
      ent = xmlGetPredefinedEntity(name);
7442
0
  if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
7443
0
      (ctxt->userData==ctxt)) {
7444
0
      ent = xmlSAX2GetEntity(ctxt, name);
7445
0
  }
7446
0
    }
7447
7448
0
    if (ent == NULL) {
7449
0
        xmlHandleUndeclaredEntity(ctxt, name);
7450
0
    }
7451
7452
    /*
7453
     * [ WFC: Parsed Entity ]
7454
     * An entity reference must not contain the name of an
7455
     * unparsed entity
7456
     */
7457
0
    else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
7458
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
7459
0
     "Entity reference to unparsed entity %s\n", name);
7460
0
        ent = NULL;
7461
0
    }
7462
7463
    /*
7464
     * [ WFC: No External Entity References ]
7465
     * Attribute values cannot contain direct or indirect
7466
     * entity references to external entities.
7467
     */
7468
0
    else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
7469
0
        if (inAttr) {
7470
0
            xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
7471
0
                 "Attribute references external entity '%s'\n", name);
7472
0
            ent = NULL;
7473
0
        }
7474
0
    }
7475
7476
0
    return(ent);
7477
0
}
7478
7479
/**
7480
 * Parse an entity reference. Always consumes '&'.
7481
 *
7482
 *     [68] EntityRef ::= '&' Name ';'
7483
 *
7484
 * @param ctxt  an XML parser context
7485
 * @returns the name, or NULL in case of error.
7486
 */
7487
static const xmlChar *
7488
0
xmlParseEntityRefInternal(xmlParserCtxtPtr ctxt) {
7489
0
    const xmlChar *name;
7490
7491
0
    GROW;
7492
7493
0
    if (RAW != '&')
7494
0
        return(NULL);
7495
0
    NEXT;
7496
0
    name = xmlParseName(ctxt);
7497
0
    if (name == NULL) {
7498
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7499
0
           "xmlParseEntityRef: no name\n");
7500
0
        return(NULL);
7501
0
    }
7502
0
    if (RAW != ';') {
7503
0
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7504
0
  return(NULL);
7505
0
    }
7506
0
    NEXT;
7507
7508
0
    return(name);
7509
0
}
7510
7511
/**
7512
 * @deprecated Internal function, don't use.
7513
 *
7514
 * @param ctxt  an XML parser context
7515
 * @returns the xmlEntity if found, or NULL otherwise.
7516
 */
7517
xmlEntity *
7518
0
xmlParseEntityRef(xmlParserCtxt *ctxt) {
7519
0
    const xmlChar *name;
7520
7521
0
    if (ctxt == NULL)
7522
0
        return(NULL);
7523
7524
0
    name = xmlParseEntityRefInternal(ctxt);
7525
0
    if (name == NULL)
7526
0
        return(NULL);
7527
7528
0
    return(xmlLookupGeneralEntity(ctxt, name, /* inAttr */ 0));
7529
0
}
7530
7531
/**
7532
 * Parse ENTITY references declarations, but this version parses it from
7533
 * a string value.
7534
 *
7535
 *     [68] EntityRef ::= '&' Name ';'
7536
 *
7537
 * [ WFC: Entity Declared ]
7538
 * In a document without any DTD, a document with only an internal DTD
7539
 * subset which contains no parameter entity references, or a document
7540
 * with "standalone='yes'", the Name given in the entity reference
7541
 * must match that in an entity declaration, except that well-formed
7542
 * documents need not declare any of the following entities: amp, lt,
7543
 * gt, apos, quot.  The declaration of a parameter entity must precede
7544
 * any reference to it.  Similarly, the declaration of a general entity
7545
 * must precede any reference to it which appears in a default value in an
7546
 * attribute-list declaration. Note that if entities are declared in the
7547
 * external subset or in external parameter entities, a non-validating
7548
 * processor is not obligated to read and process their declarations;
7549
 * for such documents, the rule that an entity must be declared is a
7550
 * well-formedness constraint only if standalone='yes'.
7551
 *
7552
 * [ WFC: Parsed Entity ]
7553
 * An entity reference must not contain the name of an unparsed entity
7554
 *
7555
 * @param ctxt  an XML parser context
7556
 * @param str  a pointer to an index in the string
7557
 * @returns the xmlEntity if found, or NULL otherwise. The str pointer
7558
 * is updated to the current location in the string.
7559
 */
7560
static xmlChar *
7561
0
xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
7562
0
    xmlChar *name;
7563
0
    const xmlChar *ptr;
7564
0
    xmlChar cur;
7565
7566
0
    if ((str == NULL) || (*str == NULL))
7567
0
        return(NULL);
7568
0
    ptr = *str;
7569
0
    cur = *ptr;
7570
0
    if (cur != '&')
7571
0
  return(NULL);
7572
7573
0
    ptr++;
7574
0
    name = xmlParseStringName(ctxt, &ptr);
7575
0
    if (name == NULL) {
7576
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7577
0
           "xmlParseStringEntityRef: no name\n");
7578
0
  *str = ptr;
7579
0
  return(NULL);
7580
0
    }
7581
0
    if (*ptr != ';') {
7582
0
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7583
0
        xmlFree(name);
7584
0
  *str = ptr;
7585
0
  return(NULL);
7586
0
    }
7587
0
    ptr++;
7588
7589
0
    *str = ptr;
7590
0
    return(name);
7591
0
}
7592
7593
/**
7594
 * Parse a parameter entity reference. Always consumes '%'.
7595
 *
7596
 * The entity content is handled directly by pushing it's content as
7597
 * a new input stream.
7598
 *
7599
 *     [69] PEReference ::= '%' Name ';'
7600
 *
7601
 * [ WFC: No Recursion ]
7602
 * A parsed entity must not contain a recursive
7603
 * reference to itself, either directly or indirectly.
7604
 *
7605
 * [ WFC: Entity Declared ]
7606
 * In a document without any DTD, a document with only an internal DTD
7607
 * subset which contains no parameter entity references, or a document
7608
 * with "standalone='yes'", ...  ... The declaration of a parameter
7609
 * entity must precede any reference to it...
7610
 *
7611
 * [ VC: Entity Declared ]
7612
 * In a document with an external subset or external parameter entities
7613
 * with "standalone='no'", ...  ... The declaration of a parameter entity
7614
 * must precede any reference to it...
7615
 *
7616
 * [ WFC: In DTD ]
7617
 * Parameter-entity references may only appear in the DTD.
7618
 * NOTE: misleading but this is handled.
7619
 *
7620
 * @param ctxt  an XML parser context
7621
 * @param markupDecl  whether the PERef starts a markup declaration
7622
 */
7623
static void
7624
0
xmlParsePERefInternal(xmlParserCtxt *ctxt, int markupDecl) {
7625
0
    const xmlChar *name;
7626
0
    xmlEntityPtr entity = NULL;
7627
0
    xmlParserInputPtr input;
7628
7629
0
    if (RAW != '%')
7630
0
        return;
7631
0
    NEXT;
7632
0
    name = xmlParseName(ctxt);
7633
0
    if (name == NULL) {
7634
0
  xmlFatalErrMsg(ctxt, XML_ERR_PEREF_NO_NAME, "PEReference: no name\n");
7635
0
  return;
7636
0
    }
7637
0
    if (RAW != ';') {
7638
0
  xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
7639
0
        return;
7640
0
    }
7641
7642
0
    NEXT;
7643
7644
    /* Must be set before xmlHandleUndeclaredEntity */
7645
0
    ctxt->hasPErefs = 1;
7646
7647
    /*
7648
     * Request the entity from SAX
7649
     */
7650
0
    if ((ctxt->sax != NULL) &&
7651
0
  (ctxt->sax->getParameterEntity != NULL))
7652
0
  entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
7653
7654
0
    if (entity == NULL) {
7655
0
        xmlHandleUndeclaredEntity(ctxt, name);
7656
0
    } else {
7657
  /*
7658
   * Internal checking in case the entity quest barfed
7659
   */
7660
0
  if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
7661
0
      (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
7662
0
      xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7663
0
      "Internal: %%%s; is not a parameter entity\n",
7664
0
        name, NULL);
7665
0
  } else {
7666
0
      if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
7667
0
                ((ctxt->options & XML_PARSE_NO_XXE) ||
7668
0
     (((ctxt->loadsubset & ~XML_SKIP_IDS) == 0) &&
7669
0
      (ctxt->replaceEntities == 0) &&
7670
0
      (ctxt->validate == 0))))
7671
0
    return;
7672
7673
0
            if (entity->flags & XML_ENT_EXPANDING) {
7674
0
                xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
7675
0
                xmlHaltParser(ctxt);
7676
0
                return;
7677
0
            }
7678
7679
0
      input = xmlNewEntityInputStream(ctxt, entity);
7680
0
      if (xmlCtxtPushInput(ctxt, input) < 0) {
7681
0
                xmlFreeInputStream(input);
7682
0
    return;
7683
0
            }
7684
7685
0
            entity->flags |= XML_ENT_EXPANDING;
7686
7687
0
            if (markupDecl)
7688
0
                input->flags |= XML_INPUT_MARKUP_DECL;
7689
7690
0
            GROW;
7691
7692
0
      if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
7693
0
                xmlDetectEncoding(ctxt);
7694
7695
0
                if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
7696
0
                    (IS_BLANK_CH(NXT(5)))) {
7697
0
                    xmlParseTextDecl(ctxt);
7698
0
                }
7699
0
            }
7700
0
  }
7701
0
    }
7702
0
}
7703
7704
/**
7705
 * Parse a parameter entity reference.
7706
 *
7707
 * @deprecated Internal function, don't use.
7708
 *
7709
 * @param ctxt  an XML parser context
7710
 */
7711
void
7712
0
xmlParsePEReference(xmlParserCtxt *ctxt) {
7713
0
    xmlParsePERefInternal(ctxt, 0);
7714
0
}
7715
7716
/**
7717
 * Load the content of an entity.
7718
 *
7719
 * @param ctxt  an XML parser context
7720
 * @param entity  an unloaded system entity
7721
 * @returns 0 in case of success and -1 in case of failure
7722
 */
7723
static int
7724
0
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
7725
0
    xmlParserInputPtr oldinput, input = NULL;
7726
0
    xmlParserInputPtr *oldinputTab;
7727
0
    xmlChar *oldencoding;
7728
0
    xmlChar *content = NULL;
7729
0
    xmlResourceType rtype;
7730
0
    size_t length, i;
7731
0
    int oldinputNr, oldinputMax;
7732
0
    int ret = -1;
7733
0
    int res;
7734
7735
0
    if ((ctxt == NULL) || (entity == NULL) ||
7736
0
        ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) &&
7737
0
   (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) ||
7738
0
  (entity->content != NULL)) {
7739
0
  xmlFatalErr(ctxt, XML_ERR_ARGUMENT,
7740
0
              "xmlLoadEntityContent parameter error");
7741
0
        return(-1);
7742
0
    }
7743
7744
0
    if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)
7745
0
        rtype = XML_RESOURCE_PARAMETER_ENTITY;
7746
0
    else
7747
0
        rtype = XML_RESOURCE_GENERAL_ENTITY;
7748
7749
0
    input = xmlLoadResource(ctxt, (char *) entity->URI,
7750
0
                            (char *) entity->ExternalID, rtype);
7751
0
    if (input == NULL)
7752
0
        return(-1);
7753
7754
0
    oldinput = ctxt->input;
7755
0
    oldinputNr = ctxt->inputNr;
7756
0
    oldinputMax = ctxt->inputMax;
7757
0
    oldinputTab = ctxt->inputTab;
7758
0
    oldencoding = ctxt->encoding;
7759
7760
0
    ctxt->input = NULL;
7761
0
    ctxt->inputNr = 0;
7762
0
    ctxt->inputMax = 1;
7763
0
    ctxt->encoding = NULL;
7764
0
    ctxt->inputTab = xmlMalloc(sizeof(xmlParserInputPtr));
7765
0
    if (ctxt->inputTab == NULL) {
7766
0
        xmlErrMemory(ctxt);
7767
0
        xmlFreeInputStream(input);
7768
0
        goto error;
7769
0
    }
7770
7771
0
    xmlBufResetInput(input->buf->buffer, input);
7772
7773
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
7774
0
        xmlFreeInputStream(input);
7775
0
        goto error;
7776
0
    }
7777
7778
0
    xmlDetectEncoding(ctxt);
7779
7780
    /*
7781
     * Parse a possible text declaration first
7782
     */
7783
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
7784
0
  xmlParseTextDecl(ctxt);
7785
        /*
7786
         * An XML-1.0 document can't reference an entity not XML-1.0
7787
         */
7788
0
        if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
7789
0
            (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
7790
0
            xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
7791
0
                           "Version mismatch between document and entity\n");
7792
0
        }
7793
0
    }
7794
7795
0
    length = input->cur - input->base;
7796
0
    xmlBufShrink(input->buf->buffer, length);
7797
0
    xmlSaturatedAdd(&ctxt->sizeentities, length);
7798
7799
0
    while ((res = xmlParserInputBufferGrow(input->buf, 4096)) > 0)
7800
0
        ;
7801
7802
0
    xmlBufResetInput(input->buf->buffer, input);
7803
7804
0
    if (res < 0) {
7805
0
        xmlCtxtErrIO(ctxt, input->buf->error, NULL);
7806
0
        goto error;
7807
0
    }
7808
7809
0
    length = xmlBufUse(input->buf->buffer);
7810
0
    if (length > INT_MAX) {
7811
0
        xmlErrMemory(ctxt);
7812
0
        goto error;
7813
0
    }
7814
7815
0
    content = xmlStrndup(xmlBufContent(input->buf->buffer), length);
7816
0
    if (content == NULL) {
7817
0
        xmlErrMemory(ctxt);
7818
0
        goto error;
7819
0
    }
7820
7821
0
    for (i = 0; i < length; ) {
7822
0
        int clen = length - i;
7823
0
        int c = xmlGetUTF8Char(content + i, &clen);
7824
7825
0
        if ((c < 0) || (!IS_CHAR(c))) {
7826
0
            xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
7827
0
                              "xmlLoadEntityContent: invalid char value %d\n",
7828
0
                              content[i]);
7829
0
            goto error;
7830
0
        }
7831
0
        i += clen;
7832
0
    }
7833
7834
0
    xmlSaturatedAdd(&ctxt->sizeentities, length);
7835
0
    entity->content = content;
7836
0
    entity->length = length;
7837
0
    content = NULL;
7838
0
    ret = 0;
7839
7840
0
error:
7841
0
    while (ctxt->inputNr > 0)
7842
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
7843
0
    xmlFree(ctxt->inputTab);
7844
0
    xmlFree(ctxt->encoding);
7845
7846
0
    ctxt->input = oldinput;
7847
0
    ctxt->inputNr = oldinputNr;
7848
0
    ctxt->inputMax = oldinputMax;
7849
0
    ctxt->inputTab = oldinputTab;
7850
0
    ctxt->encoding = oldencoding;
7851
7852
0
    xmlFree(content);
7853
7854
0
    return(ret);
7855
0
}
7856
7857
/**
7858
 * Parse PEReference declarations
7859
 *
7860
 *     [69] PEReference ::= '%' Name ';'
7861
 *
7862
 * [ WFC: No Recursion ]
7863
 * A parsed entity must not contain a recursive
7864
 * reference to itself, either directly or indirectly.
7865
 *
7866
 * [ WFC: Entity Declared ]
7867
 * In a document without any DTD, a document with only an internal DTD
7868
 * subset which contains no parameter entity references, or a document
7869
 * with "standalone='yes'", ...  ... The declaration of a parameter
7870
 * entity must precede any reference to it...
7871
 *
7872
 * [ VC: Entity Declared ]
7873
 * In a document with an external subset or external parameter entities
7874
 * with "standalone='no'", ...  ... The declaration of a parameter entity
7875
 * must precede any reference to it...
7876
 *
7877
 * [ WFC: In DTD ]
7878
 * Parameter-entity references may only appear in the DTD.
7879
 * NOTE: misleading but this is handled.
7880
 *
7881
 * @param ctxt  an XML parser context
7882
 * @param str  a pointer to an index in the string
7883
 * @returns the string of the entity content.
7884
 *         str is updated to the current value of the index
7885
 */
7886
static xmlEntityPtr
7887
0
xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
7888
0
    const xmlChar *ptr;
7889
0
    xmlChar cur;
7890
0
    xmlChar *name;
7891
0
    xmlEntityPtr entity = NULL;
7892
7893
0
    if ((str == NULL) || (*str == NULL)) return(NULL);
7894
0
    ptr = *str;
7895
0
    cur = *ptr;
7896
0
    if (cur != '%')
7897
0
        return(NULL);
7898
0
    ptr++;
7899
0
    name = xmlParseStringName(ctxt, &ptr);
7900
0
    if (name == NULL) {
7901
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7902
0
           "xmlParseStringPEReference: no name\n");
7903
0
  *str = ptr;
7904
0
  return(NULL);
7905
0
    }
7906
0
    cur = *ptr;
7907
0
    if (cur != ';') {
7908
0
  xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
7909
0
  xmlFree(name);
7910
0
  *str = ptr;
7911
0
  return(NULL);
7912
0
    }
7913
0
    ptr++;
7914
7915
    /* Must be set before xmlHandleUndeclaredEntity */
7916
0
    ctxt->hasPErefs = 1;
7917
7918
    /*
7919
     * Request the entity from SAX
7920
     */
7921
0
    if ((ctxt->sax != NULL) &&
7922
0
  (ctxt->sax->getParameterEntity != NULL))
7923
0
  entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
7924
7925
0
    if (entity == NULL) {
7926
0
        xmlHandleUndeclaredEntity(ctxt, name);
7927
0
    } else {
7928
  /*
7929
   * Internal checking in case the entity quest barfed
7930
   */
7931
0
  if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
7932
0
      (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
7933
0
      xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
7934
0
        "%%%s; is not a parameter entity\n",
7935
0
        name, NULL);
7936
0
  }
7937
0
    }
7938
7939
0
    xmlFree(name);
7940
0
    *str = ptr;
7941
0
    return(entity);
7942
0
}
7943
7944
/**
7945
 * Parse a DOCTYPE declaration
7946
 *
7947
 * @deprecated Internal function, don't use.
7948
 *
7949
 *     [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
7950
 *                          ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
7951
 *
7952
 * [ VC: Root Element Type ]
7953
 * The Name in the document type declaration must match the element
7954
 * type of the root element.
7955
 *
7956
 * @param ctxt  an XML parser context
7957
 */
7958
7959
void
7960
0
xmlParseDocTypeDecl(xmlParserCtxt *ctxt) {
7961
0
    const xmlChar *name = NULL;
7962
0
    xmlChar *publicId = NULL;
7963
0
    xmlChar *URI = NULL;
7964
7965
    /*
7966
     * We know that '<!DOCTYPE' has been detected.
7967
     */
7968
0
    SKIP(9);
7969
7970
0
    if (SKIP_BLANKS == 0) {
7971
0
        xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7972
0
                       "Space required after 'DOCTYPE'\n");
7973
0
    }
7974
7975
    /*
7976
     * Parse the DOCTYPE name.
7977
     */
7978
0
    name = xmlParseName(ctxt);
7979
0
    if (name == NULL) {
7980
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7981
0
           "xmlParseDocTypeDecl : no DOCTYPE name !\n");
7982
0
    }
7983
0
    ctxt->intSubName = name;
7984
7985
0
    SKIP_BLANKS;
7986
7987
    /*
7988
     * Check for public and system identifier (URI)
7989
     */
7990
0
    URI = xmlParseExternalID(ctxt, &publicId, 1);
7991
7992
0
    if ((URI != NULL) || (publicId != NULL)) {
7993
0
        ctxt->hasExternalSubset = 1;
7994
0
    }
7995
0
    ctxt->extSubURI = URI;
7996
0
    ctxt->extSubSystem = publicId;
7997
7998
0
    SKIP_BLANKS;
7999
8000
    /*
8001
     * Create and update the internal subset.
8002
     */
8003
0
    if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
8004
0
  (!ctxt->disableSAX))
8005
0
  ctxt->sax->internalSubset(ctxt->userData, name, publicId, URI);
8006
8007
0
    if ((RAW != '[') && (RAW != '>')) {
8008
0
  xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
8009
0
    }
8010
0
}
8011
8012
/**
8013
 * Parse the internal subset declaration
8014
 *
8015
 *     [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
8016
 * @param ctxt  an XML parser context
8017
 */
8018
8019
static void
8020
0
xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
8021
    /*
8022
     * Is there any DTD definition ?
8023
     */
8024
0
    if (RAW == '[') {
8025
0
        int oldInputNr = ctxt->inputNr;
8026
8027
0
        NEXT;
8028
  /*
8029
   * Parse the succession of Markup declarations and
8030
   * PEReferences.
8031
   * Subsequence (markupdecl | PEReference | S)*
8032
   */
8033
0
  SKIP_BLANKS;
8034
0
        while (1) {
8035
0
            if (PARSER_STOPPED(ctxt)) {
8036
0
                return;
8037
0
            } else if (ctxt->input->cur >= ctxt->input->end) {
8038
0
                if (ctxt->inputNr <= oldInputNr) {
8039
0
                xmlFatalErr(ctxt, XML_ERR_INT_SUBSET_NOT_FINISHED, NULL);
8040
0
                    return;
8041
0
                }
8042
0
                xmlPopPE(ctxt);
8043
0
            } else if ((RAW == ']') && (ctxt->inputNr <= oldInputNr)) {
8044
0
                NEXT;
8045
0
                SKIP_BLANKS;
8046
0
                break;
8047
0
            } else if ((PARSER_EXTERNAL(ctxt)) &&
8048
0
                       (RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
8049
                /*
8050
                 * Conditional sections are allowed in external entities
8051
                 * included by PE References in the internal subset.
8052
                 */
8053
0
                xmlParseConditionalSections(ctxt);
8054
0
            } else if ((RAW == '<') && ((NXT(1) == '!') || (NXT(1) == '?'))) {
8055
0
                xmlParseMarkupDecl(ctxt);
8056
0
            } else if (RAW == '%') {
8057
0
                xmlParsePERefInternal(ctxt, 1);
8058
0
            } else {
8059
0
                xmlFatalErr(ctxt, XML_ERR_INT_SUBSET_NOT_FINISHED, NULL);
8060
8061
0
                while (ctxt->inputNr > oldInputNr)
8062
0
                    xmlPopPE(ctxt);
8063
0
                return;
8064
0
            }
8065
0
            SKIP_BLANKS;
8066
0
            SHRINK;
8067
0
            GROW;
8068
0
        }
8069
0
    }
8070
8071
    /*
8072
     * We should be at the end of the DOCTYPE declaration.
8073
     */
8074
0
    if (RAW != '>') {
8075
0
        xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
8076
0
        return;
8077
0
    }
8078
0
    NEXT;
8079
0
}
8080
8081
#ifdef LIBXML_SAX1_ENABLED
8082
/**
8083
 * Parse an attribute
8084
 *
8085
 * @deprecated Internal function, don't use.
8086
 *
8087
 *     [41] Attribute ::= Name Eq AttValue
8088
 *
8089
 * [ WFC: No External Entity References ]
8090
 * Attribute values cannot contain direct or indirect entity references
8091
 * to external entities.
8092
 *
8093
 * [ WFC: No < in Attribute Values ]
8094
 * The replacement text of any entity referred to directly or indirectly in
8095
 * an attribute value (other than "&lt;") must not contain a <.
8096
 *
8097
 * [ VC: Attribute Value Type ]
8098
 * The attribute must have been declared; the value must be of the type
8099
 * declared for it.
8100
 *
8101
 *     [25] Eq ::= S? '=' S?
8102
 *
8103
 * With namespace:
8104
 *
8105
 *     [NS 11] Attribute ::= QName Eq AttValue
8106
 *
8107
 * Also the case QName == xmlns:??? is handled independently as a namespace
8108
 * definition.
8109
 *
8110
 * @param ctxt  an XML parser context
8111
 * @param value  a xmlChar ** used to store the value of the attribute
8112
 * @returns the attribute name, and the value in *value.
8113
 */
8114
8115
const xmlChar *
8116
0
xmlParseAttribute(xmlParserCtxt *ctxt, xmlChar **value) {
8117
0
    const xmlChar *name;
8118
0
    xmlChar *val;
8119
8120
0
    *value = NULL;
8121
0
    GROW;
8122
0
    name = xmlParseName(ctxt);
8123
0
    if (name == NULL) {
8124
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8125
0
                 "error parsing attribute name\n");
8126
0
        return(NULL);
8127
0
    }
8128
8129
    /*
8130
     * read the value
8131
     */
8132
0
    SKIP_BLANKS;
8133
0
    if (RAW == '=') {
8134
0
        NEXT;
8135
0
  SKIP_BLANKS;
8136
0
  val = xmlParseAttValue(ctxt);
8137
0
    } else {
8138
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
8139
0
         "Specification mandates value for attribute %s\n", name);
8140
0
  return(name);
8141
0
    }
8142
8143
    /*
8144
     * Check that xml:lang conforms to the specification
8145
     * No more registered as an error, just generate a warning now
8146
     * since this was deprecated in XML second edition
8147
     */
8148
0
    if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
8149
0
  if (!xmlCheckLanguageID(val)) {
8150
0
      xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
8151
0
              "Malformed value for xml:lang : %s\n",
8152
0
        val, NULL);
8153
0
  }
8154
0
    }
8155
8156
    /*
8157
     * Check that xml:space conforms to the specification
8158
     */
8159
0
    if (xmlStrEqual(name, BAD_CAST "xml:space")) {
8160
0
  if (xmlStrEqual(val, BAD_CAST "default"))
8161
0
      *(ctxt->space) = 0;
8162
0
  else if (xmlStrEqual(val, BAD_CAST "preserve"))
8163
0
      *(ctxt->space) = 1;
8164
0
  else {
8165
0
    xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
8166
0
"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
8167
0
                                 val, NULL);
8168
0
  }
8169
0
    }
8170
8171
0
    *value = val;
8172
0
    return(name);
8173
0
}
8174
8175
/**
8176
 * Parse a start tag. Always consumes '<'.
8177
 *
8178
 * @deprecated Internal function, don't use.
8179
 *
8180
 *     [40] STag ::= '<' Name (S Attribute)* S? '>'
8181
 *
8182
 * [ WFC: Unique Att Spec ]
8183
 * No attribute name may appear more than once in the same start-tag or
8184
 * empty-element tag.
8185
 *
8186
 *     [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
8187
 *
8188
 * [ WFC: Unique Att Spec ]
8189
 * No attribute name may appear more than once in the same start-tag or
8190
 * empty-element tag.
8191
 *
8192
 * With namespace:
8193
 *
8194
 *     [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
8195
 *
8196
 *     [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
8197
 *
8198
 * @param ctxt  an XML parser context
8199
 * @returns the element name parsed
8200
 */
8201
8202
const xmlChar *
8203
0
xmlParseStartTag(xmlParserCtxt *ctxt) {
8204
0
    const xmlChar *name;
8205
0
    const xmlChar *attname;
8206
0
    xmlChar *attvalue;
8207
0
    const xmlChar **atts = ctxt->atts;
8208
0
    int nbatts = 0;
8209
0
    int maxatts = ctxt->maxatts;
8210
0
    int i;
8211
8212
0
    if (RAW != '<') return(NULL);
8213
0
    NEXT1;
8214
8215
0
    name = xmlParseName(ctxt);
8216
0
    if (name == NULL) {
8217
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8218
0
       "xmlParseStartTag: invalid element name\n");
8219
0
        return(NULL);
8220
0
    }
8221
8222
    /*
8223
     * Now parse the attributes, it ends up with the ending
8224
     *
8225
     * (S Attribute)* S?
8226
     */
8227
0
    SKIP_BLANKS;
8228
0
    GROW;
8229
8230
0
    while (((RAW != '>') &&
8231
0
     ((RAW != '/') || (NXT(1) != '>')) &&
8232
0
     (IS_BYTE_CHAR(RAW))) && (PARSER_STOPPED(ctxt) == 0)) {
8233
0
  attname = xmlParseAttribute(ctxt, &attvalue);
8234
0
        if (attname == NULL)
8235
0
      break;
8236
0
        if (attvalue != NULL) {
8237
      /*
8238
       * [ WFC: Unique Att Spec ]
8239
       * No attribute name may appear more than once in the same
8240
       * start-tag or empty-element tag.
8241
       */
8242
0
      for (i = 0; i < nbatts;i += 2) {
8243
0
          if (xmlStrEqual(atts[i], attname)) {
8244
0
        xmlErrAttributeDup(ctxt, NULL, attname);
8245
0
        goto failed;
8246
0
    }
8247
0
      }
8248
      /*
8249
       * Add the pair to atts
8250
       */
8251
0
      if (nbatts + 4 > maxatts) {
8252
0
          const xmlChar **n;
8253
0
                int newSize;
8254
8255
0
                newSize = xmlGrowCapacity(maxatts, sizeof(n[0]) * 2,
8256
0
                                          11, XML_MAX_ATTRS);
8257
0
                if (newSize < 0) {
8258
0
        xmlErrMemory(ctxt);
8259
0
        goto failed;
8260
0
    }
8261
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
8262
0
                if (newSize < 2)
8263
0
                    newSize = 2;
8264
0
#endif
8265
0
          n = xmlRealloc(atts, newSize * sizeof(n[0]) * 2);
8266
0
    if (n == NULL) {
8267
0
        xmlErrMemory(ctxt);
8268
0
        goto failed;
8269
0
    }
8270
0
    atts = n;
8271
0
                maxatts = newSize * 2;
8272
0
    ctxt->atts = atts;
8273
0
    ctxt->maxatts = maxatts;
8274
0
      }
8275
8276
0
      atts[nbatts++] = attname;
8277
0
      atts[nbatts++] = attvalue;
8278
0
      atts[nbatts] = NULL;
8279
0
      atts[nbatts + 1] = NULL;
8280
8281
0
            attvalue = NULL;
8282
0
  }
8283
8284
0
failed:
8285
8286
0
        if (attvalue != NULL)
8287
0
            xmlFree(attvalue);
8288
8289
0
  GROW
8290
0
  if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
8291
0
      break;
8292
0
  if (SKIP_BLANKS == 0) {
8293
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8294
0
         "attributes construct error\n");
8295
0
  }
8296
0
  SHRINK;
8297
0
        GROW;
8298
0
    }
8299
8300
    /*
8301
     * SAX: Start of Element !
8302
     */
8303
0
    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
8304
0
  (!ctxt->disableSAX)) {
8305
0
  if (nbatts > 0)
8306
0
      ctxt->sax->startElement(ctxt->userData, name, atts);
8307
0
  else
8308
0
      ctxt->sax->startElement(ctxt->userData, name, NULL);
8309
0
    }
8310
8311
0
    if (atts != NULL) {
8312
        /* Free only the content strings */
8313
0
        for (i = 1;i < nbatts;i+=2)
8314
0
      if (atts[i] != NULL)
8315
0
         xmlFree((xmlChar *) atts[i]);
8316
0
    }
8317
0
    return(name);
8318
0
}
8319
8320
/**
8321
 * Parse an end tag. Always consumes '</'.
8322
 *
8323
 *     [42] ETag ::= '</' Name S? '>'
8324
 *
8325
 * With namespace
8326
 *
8327
 *     [NS 9] ETag ::= '</' QName S? '>'
8328
 * @param ctxt  an XML parser context
8329
 * @param line  line of the start tag
8330
 */
8331
8332
static void
8333
0
xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
8334
0
    const xmlChar *name;
8335
8336
0
    GROW;
8337
0
    if ((RAW != '<') || (NXT(1) != '/')) {
8338
0
  xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
8339
0
           "xmlParseEndTag: '</' not found\n");
8340
0
  return;
8341
0
    }
8342
0
    SKIP(2);
8343
8344
0
    name = xmlParseNameAndCompare(ctxt,ctxt->name);
8345
8346
    /*
8347
     * We should definitely be at the ending "S? '>'" part
8348
     */
8349
0
    GROW;
8350
0
    SKIP_BLANKS;
8351
0
    if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
8352
0
  xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
8353
0
    } else
8354
0
  NEXT1;
8355
8356
    /*
8357
     * [ WFC: Element Type Match ]
8358
     * The Name in an element's end-tag must match the element type in the
8359
     * start-tag.
8360
     *
8361
     */
8362
0
    if (name != (xmlChar*)1) {
8363
0
        if (name == NULL) name = BAD_CAST "unparsable";
8364
0
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
8365
0
         "Opening and ending tag mismatch: %s line %d and %s\n",
8366
0
                    ctxt->name, line, name);
8367
0
    }
8368
8369
    /*
8370
     * SAX: End of Tag
8371
     */
8372
0
    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
8373
0
  (!ctxt->disableSAX))
8374
0
        ctxt->sax->endElement(ctxt->userData, ctxt->name);
8375
8376
0
    namePop(ctxt);
8377
0
    spacePop(ctxt);
8378
0
}
8379
8380
/**
8381
 * Parse an end of tag
8382
 *
8383
 * @deprecated Internal function, don't use.
8384
 *
8385
 *     [42] ETag ::= '</' Name S? '>'
8386
 *
8387
 * With namespace
8388
 *
8389
 *     [NS 9] ETag ::= '</' QName S? '>'
8390
 * @param ctxt  an XML parser context
8391
 */
8392
8393
void
8394
0
xmlParseEndTag(xmlParserCtxt *ctxt) {
8395
0
    xmlParseEndTag1(ctxt, 0);
8396
0
}
8397
#endif /* LIBXML_SAX1_ENABLED */
8398
8399
/************************************************************************
8400
 *                  *
8401
 *          SAX 2 specific operations       *
8402
 *                  *
8403
 ************************************************************************/
8404
8405
/**
8406
 * Parse an XML Namespace QName
8407
 *
8408
 *     [6]  QName  ::= (Prefix ':')? LocalPart
8409
 *     [7]  Prefix  ::= NCName
8410
 *     [8]  LocalPart  ::= NCName
8411
 *
8412
 * @param ctxt  an XML parser context
8413
 * @param prefix  pointer to store the prefix part
8414
 * @returns the Name parsed or NULL
8415
 */
8416
8417
static xmlHashedString
8418
0
xmlParseQNameHashed(xmlParserCtxtPtr ctxt, xmlHashedString *prefix) {
8419
0
    xmlHashedString l, p;
8420
0
    int start, isNCName = 0;
8421
8422
0
    l.name = NULL;
8423
0
    p.name = NULL;
8424
8425
0
    GROW;
8426
0
    start = CUR_PTR - BASE_PTR;
8427
8428
0
    l = xmlParseNCName(ctxt);
8429
0
    if (l.name != NULL) {
8430
0
        isNCName = 1;
8431
0
        if (CUR == ':') {
8432
0
            NEXT;
8433
0
            p = l;
8434
0
            l = xmlParseNCName(ctxt);
8435
0
        }
8436
0
    }
8437
0
    if ((l.name == NULL) || (CUR == ':')) {
8438
0
        xmlChar *tmp;
8439
8440
0
        l.name = NULL;
8441
0
        p.name = NULL;
8442
0
        if ((isNCName == 0) && (CUR != ':'))
8443
0
            return(l);
8444
0
        tmp = xmlParseNmtoken(ctxt);
8445
0
        if (tmp != NULL)
8446
0
            xmlFree(tmp);
8447
0
        l = xmlDictLookupHashed(ctxt->dict, BASE_PTR + start,
8448
0
                                CUR_PTR - (BASE_PTR + start));
8449
0
        if (l.name == NULL) {
8450
0
            xmlErrMemory(ctxt);
8451
0
            return(l);
8452
0
        }
8453
0
        xmlNsErr(ctxt, XML_NS_ERR_QNAME,
8454
0
                 "Failed to parse QName '%s'\n", l.name, NULL, NULL);
8455
0
    }
8456
8457
0
    *prefix = p;
8458
0
    return(l);
8459
0
}
8460
8461
/**
8462
 * Parse an XML Namespace QName
8463
 *
8464
 *     [6]  QName  ::= (Prefix ':')? LocalPart
8465
 *     [7]  Prefix  ::= NCName
8466
 *     [8]  LocalPart  ::= NCName
8467
 *
8468
 * @param ctxt  an XML parser context
8469
 * @param prefix  pointer to store the prefix part
8470
 * @returns the Name parsed or NULL
8471
 */
8472
8473
static const xmlChar *
8474
0
xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
8475
0
    xmlHashedString n, p;
8476
8477
0
    n = xmlParseQNameHashed(ctxt, &p);
8478
0
    if (n.name == NULL)
8479
0
        return(NULL);
8480
0
    *prefix = p.name;
8481
0
    return(n.name);
8482
0
}
8483
8484
/**
8485
 * Parse an XML name and compares for match
8486
 * (specialized for endtag parsing)
8487
 *
8488
 * @param ctxt  an XML parser context
8489
 * @param name  the localname
8490
 * @param prefix  the prefix, if any.
8491
 * @returns NULL for an illegal name, (xmlChar*) 1 for success
8492
 * and the name for mismatch
8493
 */
8494
8495
static const xmlChar *
8496
xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
8497
0
                        xmlChar const *prefix) {
8498
0
    const xmlChar *cmp;
8499
0
    const xmlChar *in;
8500
0
    const xmlChar *ret;
8501
0
    const xmlChar *prefix2;
8502
8503
0
    if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
8504
8505
0
    GROW;
8506
0
    in = ctxt->input->cur;
8507
8508
0
    cmp = prefix;
8509
0
    while (*in != 0 && *in == *cmp) {
8510
0
  ++in;
8511
0
  ++cmp;
8512
0
    }
8513
0
    if ((*cmp == 0) && (*in == ':')) {
8514
0
        in++;
8515
0
  cmp = name;
8516
0
  while (*in != 0 && *in == *cmp) {
8517
0
      ++in;
8518
0
      ++cmp;
8519
0
  }
8520
0
  if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
8521
      /* success */
8522
0
            ctxt->input->col += in - ctxt->input->cur;
8523
0
      ctxt->input->cur = in;
8524
0
      return((const xmlChar*) 1);
8525
0
  }
8526
0
    }
8527
    /*
8528
     * all strings coms from the dictionary, equality can be done directly
8529
     */
8530
0
    ret = xmlParseQName (ctxt, &prefix2);
8531
0
    if (ret == NULL)
8532
0
        return(NULL);
8533
0
    if ((ret == name) && (prefix == prefix2))
8534
0
  return((const xmlChar*) 1);
8535
0
    return ret;
8536
0
}
8537
8538
/**
8539
 * Parse an attribute in the new SAX2 framework.
8540
 *
8541
 * @param ctxt  an XML parser context
8542
 * @param pref  the element prefix
8543
 * @param elem  the element name
8544
 * @param hprefix  resulting attribute prefix
8545
 * @param value  resulting value of the attribute
8546
 * @param len  resulting length of the attribute
8547
 * @param alloc  resulting indicator if the attribute was allocated
8548
 * @returns the attribute name, and the value in *value, .
8549
 */
8550
8551
static xmlHashedString
8552
xmlParseAttribute2(xmlParserCtxtPtr ctxt,
8553
                   const xmlChar * pref, const xmlChar * elem,
8554
                   xmlHashedString * hprefix, xmlChar ** value,
8555
                   int *len, int *alloc)
8556
0
{
8557
0
    xmlHashedString hname;
8558
0
    const xmlChar *prefix, *name;
8559
0
    xmlChar *val = NULL, *internal_val = NULL;
8560
0
    int special = 0;
8561
0
    int isNamespace;
8562
0
    int flags;
8563
8564
0
    *value = NULL;
8565
0
    GROW;
8566
0
    hname = xmlParseQNameHashed(ctxt, hprefix);
8567
0
    if (hname.name == NULL) {
8568
0
        xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8569
0
                       "error parsing attribute name\n");
8570
0
        return(hname);
8571
0
    }
8572
0
    name = hname.name;
8573
0
    prefix = hprefix->name;
8574
8575
    /*
8576
     * get the type if needed
8577
     */
8578
0
    if (ctxt->attsSpecial != NULL) {
8579
0
        special = XML_PTR_TO_INT(xmlHashQLookup2(ctxt->attsSpecial, pref, elem,
8580
0
                                              prefix, name));
8581
0
    }
8582
8583
    /*
8584
     * read the value
8585
     */
8586
0
    SKIP_BLANKS;
8587
0
    if (RAW != '=') {
8588
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
8589
0
                          "Specification mandates value for attribute %s\n",
8590
0
                          name);
8591
0
        goto error;
8592
0
    }
8593
8594
8595
0
    NEXT;
8596
0
    SKIP_BLANKS;
8597
0
    flags = 0;
8598
0
    isNamespace = (((prefix == NULL) && (name == ctxt->str_xmlns)) ||
8599
0
                   (prefix == ctxt->str_xmlns));
8600
0
    val = xmlParseAttValueInternal(ctxt, len, &flags, special,
8601
0
                                   isNamespace);
8602
0
    if (val == NULL)
8603
0
        goto error;
8604
8605
0
    *alloc = (flags & XML_ATTVAL_ALLOC) != 0;
8606
8607
0
#ifdef LIBXML_VALID_ENABLED
8608
0
    if ((ctxt->validate) &&
8609
0
        (ctxt->standalone) &&
8610
0
        (special & XML_SPECIAL_EXTERNAL) &&
8611
0
        (flags & XML_ATTVAL_NORM_CHANGE)) {
8612
0
        xmlValidityError(ctxt, XML_DTD_NOT_STANDALONE,
8613
0
                         "standalone: normalization of attribute %s on %s "
8614
0
                         "by external subset declaration\n",
8615
0
                         name, elem);
8616
0
    }
8617
0
#endif
8618
8619
0
    if (prefix == ctxt->str_xml) {
8620
        /*
8621
         * Check that xml:lang conforms to the specification
8622
         * No more registered as an error, just generate a warning now
8623
         * since this was deprecated in XML second edition
8624
         */
8625
0
        if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) {
8626
0
            internal_val = xmlStrndup(val, *len);
8627
0
            if (internal_val == NULL)
8628
0
                goto mem_error;
8629
0
            if (!xmlCheckLanguageID(internal_val)) {
8630
0
                xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
8631
0
                              "Malformed value for xml:lang : %s\n",
8632
0
                              internal_val, NULL);
8633
0
            }
8634
0
        }
8635
8636
        /*
8637
         * Check that xml:space conforms to the specification
8638
         */
8639
0
        if (xmlStrEqual(name, BAD_CAST "space")) {
8640
0
            internal_val = xmlStrndup(val, *len);
8641
0
            if (internal_val == NULL)
8642
0
                goto mem_error;
8643
0
            if (xmlStrEqual(internal_val, BAD_CAST "default"))
8644
0
                *(ctxt->space) = 0;
8645
0
            else if (xmlStrEqual(internal_val, BAD_CAST "preserve"))
8646
0
                *(ctxt->space) = 1;
8647
0
            else {
8648
0
                xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
8649
0
                              "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
8650
0
                              internal_val, NULL);
8651
0
            }
8652
0
        }
8653
0
        if (internal_val) {
8654
0
            xmlFree(internal_val);
8655
0
        }
8656
0
    }
8657
8658
0
    *value = val;
8659
0
    return (hname);
8660
8661
0
mem_error:
8662
0
    xmlErrMemory(ctxt);
8663
0
error:
8664
0
    if ((val != NULL) && (*alloc != 0))
8665
0
        xmlFree(val);
8666
0
    return(hname);
8667
0
}
8668
8669
/**
8670
 * Inserts a new attribute into the hash table.
8671
 *
8672
 * @param ctxt  parser context
8673
 * @param size  size of the hash table
8674
 * @param name  attribute name
8675
 * @param uri  namespace uri
8676
 * @param hashValue  combined hash value of name and uri
8677
 * @param aindex  attribute index (this is a multiple of 5)
8678
 * @returns INT_MAX if no existing attribute was found, the attribute
8679
 * index if an attribute was found, -1 if a memory allocation failed.
8680
 */
8681
static int
8682
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, unsigned size, const xmlChar *name,
8683
0
                  const xmlChar *uri, unsigned hashValue, int aindex) {
8684
0
    xmlAttrHashBucket *table = ctxt->attrHash;
8685
0
    xmlAttrHashBucket *bucket;
8686
0
    unsigned hindex;
8687
8688
0
    hindex = hashValue & (size - 1);
8689
0
    bucket = &table[hindex];
8690
8691
0
    while (bucket->index >= 0) {
8692
0
        const xmlChar **atts = &ctxt->atts[bucket->index];
8693
8694
0
        if (name == atts[0]) {
8695
0
            int nsIndex = XML_PTR_TO_INT(atts[2]);
8696
8697
0
            if ((nsIndex == NS_INDEX_EMPTY) ? (uri == NULL) :
8698
0
                (nsIndex == NS_INDEX_XML) ? (uri == ctxt->str_xml_ns) :
8699
0
                (uri == ctxt->nsTab[nsIndex * 2 + 1]))
8700
0
                return(bucket->index);
8701
0
        }
8702
8703
0
        hindex++;
8704
0
        bucket++;
8705
0
        if (hindex >= size) {
8706
0
            hindex = 0;
8707
0
            bucket = table;
8708
0
        }
8709
0
    }
8710
8711
0
    bucket->index = aindex;
8712
8713
0
    return(INT_MAX);
8714
0
}
8715
8716
static int
8717
xmlAttrHashInsertQName(xmlParserCtxtPtr ctxt, unsigned size,
8718
                       const xmlChar *name, const xmlChar *prefix,
8719
0
                       unsigned hashValue, int aindex) {
8720
0
    xmlAttrHashBucket *table = ctxt->attrHash;
8721
0
    xmlAttrHashBucket *bucket;
8722
0
    unsigned hindex;
8723
8724
0
    hindex = hashValue & (size - 1);
8725
0
    bucket = &table[hindex];
8726
8727
0
    while (bucket->index >= 0) {
8728
0
        const xmlChar **atts = &ctxt->atts[bucket->index];
8729
8730
0
        if ((name == atts[0]) && (prefix == atts[1]))
8731
0
            return(bucket->index);
8732
8733
0
        hindex++;
8734
0
        bucket++;
8735
0
        if (hindex >= size) {
8736
0
            hindex = 0;
8737
0
            bucket = table;
8738
0
        }
8739
0
    }
8740
8741
0
    bucket->index = aindex;
8742
8743
0
    return(INT_MAX);
8744
0
}
8745
/**
8746
 * Parse a start tag. Always consumes '<'.
8747
 *
8748
 * This routine is called when running SAX2 parsing
8749
 *
8750
 *     [40] STag ::= '<' Name (S Attribute)* S? '>'
8751
 *
8752
 * [ WFC: Unique Att Spec ]
8753
 * No attribute name may appear more than once in the same start-tag or
8754
 * empty-element tag.
8755
 *
8756
 *     [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
8757
 *
8758
 * [ WFC: Unique Att Spec ]
8759
 * No attribute name may appear more than once in the same start-tag or
8760
 * empty-element tag.
8761
 *
8762
 * With namespace:
8763
 *
8764
 *     [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
8765
 *
8766
 *     [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
8767
 *
8768
 * @param ctxt  an XML parser context
8769
 * @param pref  resulting namespace prefix
8770
 * @param URI  resulting namespace URI
8771
 * @param nbNsPtr  resulting number of namespace declarations
8772
 * @returns the element name parsed
8773
 */
8774
8775
static const xmlChar *
8776
xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
8777
0
                  const xmlChar **URI, int *nbNsPtr) {
8778
0
    xmlHashedString hlocalname;
8779
0
    xmlHashedString hprefix;
8780
0
    xmlHashedString hattname;
8781
0
    xmlHashedString haprefix;
8782
0
    const xmlChar *localname;
8783
0
    const xmlChar *prefix;
8784
0
    const xmlChar *attname;
8785
0
    const xmlChar *aprefix;
8786
0
    const xmlChar *uri;
8787
0
    xmlChar *attvalue = NULL;
8788
0
    const xmlChar **atts = ctxt->atts;
8789
0
    unsigned attrHashSize = 0;
8790
0
    int maxatts = ctxt->maxatts;
8791
0
    int nratts, nbatts, nbdef;
8792
0
    int i, j, nbNs, nbTotalDef, attval, nsIndex, maxAtts;
8793
0
    int alloc = 0;
8794
0
    int numNsErr = 0;
8795
0
    int numDupErr = 0;
8796
8797
0
    if (RAW != '<') return(NULL);
8798
0
    NEXT1;
8799
8800
0
    nbatts = 0;
8801
0
    nratts = 0;
8802
0
    nbdef = 0;
8803
0
    nbNs = 0;
8804
0
    nbTotalDef = 0;
8805
0
    attval = 0;
8806
8807
0
    if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
8808
0
        xmlErrMemory(ctxt);
8809
0
        return(NULL);
8810
0
    }
8811
8812
0
    hlocalname = xmlParseQNameHashed(ctxt, &hprefix);
8813
0
    if (hlocalname.name == NULL) {
8814
0
  xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
8815
0
           "StartTag: invalid element name\n");
8816
0
        return(NULL);
8817
0
    }
8818
0
    localname = hlocalname.name;
8819
0
    prefix = hprefix.name;
8820
8821
    /*
8822
     * Now parse the attributes, it ends up with the ending
8823
     *
8824
     * (S Attribute)* S?
8825
     */
8826
0
    SKIP_BLANKS;
8827
0
    GROW;
8828
8829
    /*
8830
     * The ctxt->atts array will be ultimately passed to the SAX callback
8831
     * containing five xmlChar pointers for each attribute:
8832
     *
8833
     * [0] attribute name
8834
     * [1] attribute prefix
8835
     * [2] namespace URI
8836
     * [3] attribute value
8837
     * [4] end of attribute value
8838
     *
8839
     * To save memory, we reuse this array temporarily and store integers
8840
     * in these pointer variables.
8841
     *
8842
     * [0] attribute name
8843
     * [1] attribute prefix
8844
     * [2] hash value of attribute prefix, and later namespace index
8845
     * [3] for non-allocated values: ptrdiff_t offset into input buffer
8846
     * [4] for non-allocated values: ptrdiff_t offset into input buffer
8847
     *
8848
     * The ctxt->attallocs array contains an additional unsigned int for
8849
     * each attribute, containing the hash value of the attribute name
8850
     * and the alloc flag in bit 31.
8851
     */
8852
8853
0
    while (((RAW != '>') &&
8854
0
     ((RAW != '/') || (NXT(1) != '>')) &&
8855
0
     (IS_BYTE_CHAR(RAW))) && (PARSER_STOPPED(ctxt) == 0)) {
8856
0
  int len = -1;
8857
8858
0
  hattname = xmlParseAttribute2(ctxt, prefix, localname,
8859
0
                                          &haprefix, &attvalue, &len,
8860
0
                                          &alloc);
8861
0
        if (hattname.name == NULL)
8862
0
      break;
8863
0
        if (attvalue == NULL)
8864
0
            goto next_attr;
8865
0
        attname = hattname.name;
8866
0
        aprefix = haprefix.name;
8867
0
  if (len < 0) len = xmlStrlen(attvalue);
8868
8869
0
        if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
8870
0
            xmlHashedString huri;
8871
0
            xmlURIPtr parsedUri;
8872
8873
0
            huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
8874
0
            uri = huri.name;
8875
0
            if (uri == NULL) {
8876
0
                xmlErrMemory(ctxt);
8877
0
                goto next_attr;
8878
0
            }
8879
0
            if (*uri != 0) {
8880
0
                if (xmlParseURISafe((const char *) uri, &parsedUri) < 0) {
8881
0
                    xmlErrMemory(ctxt);
8882
0
                    goto next_attr;
8883
0
                }
8884
0
                if (parsedUri == NULL) {
8885
0
                    xmlNsErr(ctxt, XML_WAR_NS_URI,
8886
0
                             "xmlns: '%s' is not a valid URI\n",
8887
0
                                       uri, NULL, NULL);
8888
0
                } else {
8889
0
                    if (parsedUri->scheme == NULL) {
8890
0
                        xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
8891
0
                                  "xmlns: URI %s is not absolute\n",
8892
0
                                  uri, NULL, NULL);
8893
0
                    }
8894
0
                    xmlFreeURI(parsedUri);
8895
0
                }
8896
0
                if (uri == ctxt->str_xml_ns) {
8897
0
                    if (attname != ctxt->str_xml) {
8898
0
                        xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8899
0
                     "xml namespace URI cannot be the default namespace\n",
8900
0
                                 NULL, NULL, NULL);
8901
0
                    }
8902
0
                    goto next_attr;
8903
0
                }
8904
0
                if ((len == 29) &&
8905
0
                    (xmlStrEqual(uri,
8906
0
                             BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
8907
0
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8908
0
                         "reuse of the xmlns namespace name is forbidden\n",
8909
0
                             NULL, NULL, NULL);
8910
0
                    goto next_attr;
8911
0
                }
8912
0
            }
8913
8914
0
            if (xmlParserNsPush(ctxt, NULL, &huri, NULL, 0) > 0)
8915
0
                nbNs++;
8916
0
        } else if (aprefix == ctxt->str_xmlns) {
8917
0
            xmlHashedString huri;
8918
0
            xmlURIPtr parsedUri;
8919
8920
0
            huri = xmlDictLookupHashed(ctxt->dict, attvalue, len);
8921
0
            uri = huri.name;
8922
0
            if (uri == NULL) {
8923
0
                xmlErrMemory(ctxt);
8924
0
                goto next_attr;
8925
0
            }
8926
8927
0
            if (attname == ctxt->str_xml) {
8928
0
                if (uri != ctxt->str_xml_ns) {
8929
0
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8930
0
                             "xml namespace prefix mapped to wrong URI\n",
8931
0
                             NULL, NULL, NULL);
8932
0
                }
8933
                /*
8934
                 * Do not keep a namespace definition node
8935
                 */
8936
0
                goto next_attr;
8937
0
            }
8938
0
            if (uri == ctxt->str_xml_ns) {
8939
0
                if (attname != ctxt->str_xml) {
8940
0
                    xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8941
0
                             "xml namespace URI mapped to wrong prefix\n",
8942
0
                             NULL, NULL, NULL);
8943
0
                }
8944
0
                goto next_attr;
8945
0
            }
8946
0
            if (attname == ctxt->str_xmlns) {
8947
0
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8948
0
                         "redefinition of the xmlns prefix is forbidden\n",
8949
0
                         NULL, NULL, NULL);
8950
0
                goto next_attr;
8951
0
            }
8952
0
            if ((len == 29) &&
8953
0
                (xmlStrEqual(uri,
8954
0
                             BAD_CAST "http://www.w3.org/2000/xmlns/"))) {
8955
0
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8956
0
                         "reuse of the xmlns namespace name is forbidden\n",
8957
0
                         NULL, NULL, NULL);
8958
0
                goto next_attr;
8959
0
            }
8960
0
            if ((uri == NULL) || (uri[0] == 0)) {
8961
0
                xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
8962
0
                         "xmlns:%s: Empty XML namespace is not allowed\n",
8963
0
                              attname, NULL, NULL);
8964
0
                goto next_attr;
8965
0
            } else {
8966
0
                if (xmlParseURISafe((const char *) uri, &parsedUri) < 0) {
8967
0
                    xmlErrMemory(ctxt);
8968
0
                    goto next_attr;
8969
0
                }
8970
0
                if (parsedUri == NULL) {
8971
0
                    xmlNsErr(ctxt, XML_WAR_NS_URI,
8972
0
                         "xmlns:%s: '%s' is not a valid URI\n",
8973
0
                                       attname, uri, NULL);
8974
0
                } else {
8975
0
                    if ((ctxt->pedantic) && (parsedUri->scheme == NULL)) {
8976
0
                        xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE,
8977
0
                                  "xmlns:%s: URI %s is not absolute\n",
8978
0
                                  attname, uri, NULL);
8979
0
                    }
8980
0
                    xmlFreeURI(parsedUri);
8981
0
                }
8982
0
            }
8983
8984
0
            if (xmlParserNsPush(ctxt, &hattname, &huri, NULL, 0) > 0)
8985
0
                nbNs++;
8986
0
        } else {
8987
            /*
8988
             * Populate attributes array, see above for repurposing
8989
             * of xmlChar pointers.
8990
             */
8991
0
            if ((atts == NULL) || (nbatts + 5 > maxatts)) {
8992
0
                int res = xmlCtxtGrowAttrs(ctxt);
8993
8994
0
                maxatts = ctxt->maxatts;
8995
0
                atts = ctxt->atts;
8996
8997
0
                if (res < 0)
8998
0
                    goto next_attr;
8999
0
            }
9000
0
            ctxt->attallocs[nratts++] = (hattname.hashValue & 0x7FFFFFFF) |
9001
0
                                        ((unsigned) alloc << 31);
9002
0
            atts[nbatts++] = attname;
9003
0
            atts[nbatts++] = aprefix;
9004
0
            atts[nbatts++] = XML_INT_TO_PTR(haprefix.hashValue);
9005
0
            if (alloc) {
9006
0
                atts[nbatts++] = attvalue;
9007
0
                attvalue += len;
9008
0
                atts[nbatts++] = attvalue;
9009
0
            } else {
9010
                /*
9011
                 * attvalue points into the input buffer which can be
9012
                 * reallocated. Store differences to input->base instead.
9013
                 * The pointers will be reconstructed later.
9014
                 */
9015
0
                atts[nbatts++] = XML_INT_TO_PTR(attvalue - BASE_PTR);
9016
0
                attvalue += len;
9017
0
                atts[nbatts++] = XML_INT_TO_PTR(attvalue - BASE_PTR);
9018
0
            }
9019
            /*
9020
             * tag if some deallocation is needed
9021
             */
9022
0
            if (alloc != 0) attval = 1;
9023
0
            attvalue = NULL; /* moved into atts */
9024
0
        }
9025
9026
0
next_attr:
9027
0
        if ((attvalue != NULL) && (alloc != 0)) {
9028
0
            xmlFree(attvalue);
9029
0
            attvalue = NULL;
9030
0
        }
9031
9032
0
  GROW
9033
0
  if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
9034
0
      break;
9035
0
  if (SKIP_BLANKS == 0) {
9036
0
      xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
9037
0
         "attributes construct error\n");
9038
0
      break;
9039
0
  }
9040
0
        GROW;
9041
0
    }
9042
9043
    /*
9044
     * Namespaces from default attributes
9045
     */
9046
0
    if (ctxt->attsDefault != NULL) {
9047
0
        xmlDefAttrsPtr defaults;
9048
9049
0
  defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
9050
0
  if (defaults != NULL) {
9051
0
      for (i = 0; i < defaults->nbAttrs; i++) {
9052
0
                xmlDefAttr *attr = &defaults->attrs[i];
9053
9054
0
          attname = attr->name.name;
9055
0
    aprefix = attr->prefix.name;
9056
9057
0
    if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
9058
0
                    xmlParserEntityCheck(ctxt, attr->expandedSize);
9059
9060
0
                    if (xmlParserNsPush(ctxt, NULL, &attr->value, NULL, 1) > 0)
9061
0
                        nbNs++;
9062
0
    } else if (aprefix == ctxt->str_xmlns) {
9063
0
                    xmlParserEntityCheck(ctxt, attr->expandedSize);
9064
9065
0
                    if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
9066
0
                                      NULL, 1) > 0)
9067
0
                        nbNs++;
9068
0
    } else {
9069
0
                    if (nratts + nbTotalDef >= XML_MAX_ATTRS) {
9070
0
                        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
9071
0
                                    "Maximum number of attributes exceeded");
9072
0
                        break;
9073
0
                    }
9074
0
                    nbTotalDef += 1;
9075
0
                }
9076
0
      }
9077
0
  }
9078
0
    }
9079
9080
    /*
9081
     * Resolve attribute namespaces
9082
     */
9083
0
    for (i = 0; i < nbatts; i += 5) {
9084
0
        attname = atts[i];
9085
0
        aprefix = atts[i+1];
9086
9087
        /*
9088
  * The default namespace does not apply to attribute names.
9089
  */
9090
0
  if (aprefix == NULL) {
9091
0
            nsIndex = NS_INDEX_EMPTY;
9092
0
        } else if (aprefix == ctxt->str_xml) {
9093
0
            nsIndex = NS_INDEX_XML;
9094
0
        } else {
9095
0
            haprefix.name = aprefix;
9096
0
            haprefix.hashValue = (size_t) atts[i+2];
9097
0
            nsIndex = xmlParserNsLookup(ctxt, &haprefix, NULL);
9098
9099
0
      if ((nsIndex == INT_MAX) || (nsIndex < ctxt->nsdb->minNsIndex)) {
9100
0
                xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9101
0
        "Namespace prefix %s for %s on %s is not defined\n",
9102
0
        aprefix, attname, localname);
9103
0
                nsIndex = NS_INDEX_EMPTY;
9104
0
            }
9105
0
        }
9106
9107
0
        atts[i+2] = XML_INT_TO_PTR(nsIndex);
9108
0
    }
9109
9110
    /*
9111
     * Maximum number of attributes including default attributes.
9112
     */
9113
0
    maxAtts = nratts + nbTotalDef;
9114
9115
    /*
9116
     * Verify that attribute names are unique.
9117
     */
9118
0
    if (maxAtts > 1) {
9119
0
        attrHashSize = 4;
9120
0
        while (attrHashSize / 2 < (unsigned) maxAtts)
9121
0
            attrHashSize *= 2;
9122
9123
0
        if (attrHashSize > ctxt->attrHashMax) {
9124
0
            xmlAttrHashBucket *tmp;
9125
9126
0
            tmp = xmlRealloc(ctxt->attrHash, attrHashSize * sizeof(tmp[0]));
9127
0
            if (tmp == NULL) {
9128
0
                xmlErrMemory(ctxt);
9129
0
                goto done;
9130
0
            }
9131
9132
0
            ctxt->attrHash = tmp;
9133
0
            ctxt->attrHashMax = attrHashSize;
9134
0
        }
9135
9136
0
        memset(ctxt->attrHash, -1, attrHashSize * sizeof(ctxt->attrHash[0]));
9137
9138
0
        for (i = 0, j = 0; j < nratts; i += 5, j++) {
9139
0
            const xmlChar *nsuri;
9140
0
            unsigned hashValue, nameHashValue, uriHashValue;
9141
0
            int res;
9142
9143
0
            attname = atts[i];
9144
0
            aprefix = atts[i+1];
9145
0
            nsIndex = XML_PTR_TO_INT(atts[i+2]);
9146
            /* Hash values always have bit 31 set, see dict.c */
9147
0
            nameHashValue = ctxt->attallocs[j] | 0x80000000;
9148
9149
0
            if (nsIndex == NS_INDEX_EMPTY) {
9150
                /*
9151
                 * Prefix with empty namespace means an undeclared
9152
                 * prefix which was already reported above.
9153
                 */
9154
0
                if (aprefix != NULL)
9155
0
                    continue;
9156
0
                nsuri = NULL;
9157
0
                uriHashValue = URI_HASH_EMPTY;
9158
0
            } else if (nsIndex == NS_INDEX_XML) {
9159
0
                nsuri = ctxt->str_xml_ns;
9160
0
                uriHashValue = URI_HASH_XML;
9161
0
            } else {
9162
0
                nsuri = ctxt->nsTab[nsIndex * 2 + 1];
9163
0
                uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
9164
0
            }
9165
9166
0
            hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
9167
0
            res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
9168
0
                                    hashValue, i);
9169
0
            if (res < 0)
9170
0
                continue;
9171
9172
            /*
9173
             * [ WFC: Unique Att Spec ]
9174
             * No attribute name may appear more than once in the same
9175
             * start-tag or empty-element tag.
9176
             * As extended by the Namespace in XML REC.
9177
             */
9178
0
            if (res < INT_MAX) {
9179
0
                if (aprefix == atts[res+1]) {
9180
0
                    xmlErrAttributeDup(ctxt, aprefix, attname);
9181
0
                    numDupErr += 1;
9182
0
                } else {
9183
0
                    xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
9184
0
                             "Namespaced Attribute %s in '%s' redefined\n",
9185
0
                             attname, nsuri, NULL);
9186
0
                    numNsErr += 1;
9187
0
                }
9188
0
            }
9189
0
        }
9190
0
    }
9191
9192
    /*
9193
     * Default attributes
9194
     */
9195
0
    if (ctxt->attsDefault != NULL) {
9196
0
        xmlDefAttrsPtr defaults;
9197
9198
0
  defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
9199
0
  if (defaults != NULL) {
9200
0
      for (i = 0; i < defaults->nbAttrs; i++) {
9201
0
                xmlDefAttr *attr = &defaults->attrs[i];
9202
0
                const xmlChar *nsuri = NULL;
9203
0
                unsigned hashValue, uriHashValue = 0;
9204
0
                int res;
9205
9206
0
          attname = attr->name.name;
9207
0
    aprefix = attr->prefix.name;
9208
9209
0
    if ((attname == ctxt->str_xmlns) && (aprefix == NULL))
9210
0
                    continue;
9211
0
    if (aprefix == ctxt->str_xmlns)
9212
0
                    continue;
9213
9214
0
                if (aprefix == NULL) {
9215
0
                    nsIndex = NS_INDEX_EMPTY;
9216
0
                    nsuri = NULL;
9217
0
                    uriHashValue = URI_HASH_EMPTY;
9218
0
                } else if (aprefix == ctxt->str_xml) {
9219
0
                    nsIndex = NS_INDEX_XML;
9220
0
                    nsuri = ctxt->str_xml_ns;
9221
0
                    uriHashValue = URI_HASH_XML;
9222
0
                } else {
9223
0
                    nsIndex = xmlParserNsLookup(ctxt, &attr->prefix, NULL);
9224
0
                    if ((nsIndex == INT_MAX) ||
9225
0
                        (nsIndex < ctxt->nsdb->minNsIndex)) {
9226
0
                        xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9227
0
                                 "Namespace prefix %s for %s on %s is not "
9228
0
                                 "defined\n",
9229
0
                                 aprefix, attname, localname);
9230
0
                        nsIndex = NS_INDEX_EMPTY;
9231
0
                        nsuri = NULL;
9232
0
                        uriHashValue = URI_HASH_EMPTY;
9233
0
                    } else {
9234
0
                        nsuri = ctxt->nsTab[nsIndex * 2 + 1];
9235
0
                        uriHashValue = ctxt->nsdb->extra[nsIndex].uriHashValue;
9236
0
                    }
9237
0
                }
9238
9239
                /*
9240
                 * Check whether the attribute exists
9241
                 */
9242
0
                if (maxAtts > 1) {
9243
0
                    hashValue = xmlDictCombineHash(attr->name.hashValue,
9244
0
                                                   uriHashValue);
9245
0
                    res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
9246
0
                                            hashValue, nbatts);
9247
0
                    if (res < 0)
9248
0
                        continue;
9249
0
                    if (res < INT_MAX) {
9250
0
                        if (aprefix == atts[res+1])
9251
0
                            continue;
9252
0
                        xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
9253
0
                                 "Namespaced Attribute %s in '%s' redefined\n",
9254
0
                                 attname, nsuri, NULL);
9255
0
                    }
9256
0
                }
9257
9258
0
                xmlParserEntityCheck(ctxt, attr->expandedSize);
9259
9260
0
                if ((atts == NULL) || (nbatts + 5 > maxatts)) {
9261
0
                    res = xmlCtxtGrowAttrs(ctxt);
9262
9263
0
                    maxatts = ctxt->maxatts;
9264
0
                    atts = ctxt->atts;
9265
9266
0
                    if (res < 0) {
9267
0
                        localname = NULL;
9268
0
                        goto done;
9269
0
                    }
9270
0
                }
9271
9272
0
                atts[nbatts++] = attname;
9273
0
                atts[nbatts++] = aprefix;
9274
0
                atts[nbatts++] = XML_INT_TO_PTR(nsIndex);
9275
0
                atts[nbatts++] = attr->value.name;
9276
0
                atts[nbatts++] = attr->valueEnd;
9277
9278
0
#ifdef LIBXML_VALID_ENABLED
9279
                /*
9280
                 * This should be moved to valid.c, but we don't keep track
9281
                 * whether an attribute was defaulted.
9282
                 */
9283
0
                if ((ctxt->validate) &&
9284
0
                    (ctxt->standalone == 1) &&
9285
0
                    (attr->external != 0)) {
9286
0
                    xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED,
9287
0
                            "standalone: attribute %s on %s defaulted "
9288
0
                            "from external subset\n",
9289
0
                            attname, localname);
9290
0
                }
9291
0
#endif
9292
0
                nbdef++;
9293
0
      }
9294
0
  }
9295
0
    }
9296
9297
    /*
9298
     * Using a single hash table for nsUri/localName pairs cannot
9299
     * detect duplicate QNames reliably. The following example will
9300
     * only result in two namespace errors.
9301
     *
9302
     * <doc xmlns:a="a" xmlns:b="a">
9303
     *   <elem a:a="" b:a="" b:a=""/>
9304
     * </doc>
9305
     *
9306
     * If we saw more than one namespace error but no duplicate QNames
9307
     * were found, we have to scan for duplicate QNames.
9308
     */
9309
0
    if ((numDupErr == 0) && (numNsErr > 1)) {
9310
0
        memset(ctxt->attrHash, -1,
9311
0
               attrHashSize * sizeof(ctxt->attrHash[0]));
9312
9313
0
        for (i = 0, j = 0; j < nratts; i += 5, j++) {
9314
0
            unsigned hashValue, nameHashValue, prefixHashValue;
9315
0
            int res;
9316
9317
0
            aprefix = atts[i+1];
9318
0
            if (aprefix == NULL)
9319
0
                continue;
9320
9321
0
            attname = atts[i];
9322
            /* Hash values always have bit 31 set, see dict.c */
9323
0
            nameHashValue = ctxt->attallocs[j] | 0x80000000;
9324
0
            prefixHashValue = xmlDictComputeHash(ctxt->dict, aprefix);
9325
9326
0
            hashValue = xmlDictCombineHash(nameHashValue, prefixHashValue);
9327
0
            res = xmlAttrHashInsertQName(ctxt, attrHashSize, attname,
9328
0
                                         aprefix, hashValue, i);
9329
0
            if (res < INT_MAX)
9330
0
                xmlErrAttributeDup(ctxt, aprefix, attname);
9331
0
        }
9332
0
    }
9333
9334
    /*
9335
     * Reconstruct attribute pointers
9336
     */
9337
0
    for (i = 0, j = 0; i < nbatts; i += 5, j++) {
9338
        /* namespace URI */
9339
0
        nsIndex = XML_PTR_TO_INT(atts[i+2]);
9340
0
        if (nsIndex == INT_MAX)
9341
0
            atts[i+2] = NULL;
9342
0
        else if (nsIndex == INT_MAX - 1)
9343
0
            atts[i+2] = ctxt->str_xml_ns;
9344
0
        else
9345
0
            atts[i+2] = ctxt->nsTab[nsIndex * 2 + 1];
9346
9347
0
        if ((j < nratts) && (ctxt->attallocs[j] & 0x80000000) == 0) {
9348
0
            atts[i+3] = BASE_PTR + XML_PTR_TO_INT(atts[i+3]);  /* value */
9349
0
            atts[i+4] = BASE_PTR + XML_PTR_TO_INT(atts[i+4]);  /* valuend */
9350
0
        }
9351
0
    }
9352
9353
0
    uri = xmlParserNsLookupUri(ctxt, &hprefix);
9354
0
    if ((prefix != NULL) && (uri == NULL)) {
9355
0
  xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
9356
0
           "Namespace prefix %s on %s is not defined\n",
9357
0
     prefix, localname, NULL);
9358
0
    }
9359
0
    *pref = prefix;
9360
0
    *URI = uri;
9361
9362
    /*
9363
     * SAX callback
9364
     */
9365
0
    if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
9366
0
  (!ctxt->disableSAX)) {
9367
0
  if (nbNs > 0)
9368
0
      ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
9369
0
                          nbNs, ctxt->nsTab + 2 * (ctxt->nsNr - nbNs),
9370
0
        nbatts / 5, nbdef, atts);
9371
0
  else
9372
0
      ctxt->sax->startElementNs(ctxt->userData, localname, prefix, uri,
9373
0
                          0, NULL, nbatts / 5, nbdef, atts);
9374
0
    }
9375
9376
0
done:
9377
    /*
9378
     * Free allocated attribute values
9379
     */
9380
0
    if (attval != 0) {
9381
0
  for (i = 0, j = 0; j < nratts; i += 5, j++)
9382
0
      if (ctxt->attallocs[j] & 0x80000000)
9383
0
          xmlFree((xmlChar *) atts[i+3]);
9384
0
    }
9385
9386
0
    *nbNsPtr = nbNs;
9387
0
    return(localname);
9388
0
}
9389
9390
/**
9391
 * Parse an end tag. Always consumes '</'.
9392
 *
9393
 *     [42] ETag ::= '</' Name S? '>'
9394
 *
9395
 * With namespace
9396
 *
9397
 *     [NS 9] ETag ::= '</' QName S? '>'
9398
 * @param ctxt  an XML parser context
9399
 * @param tag  the corresponding start tag
9400
 */
9401
9402
static void
9403
0
xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlStartTag *tag) {
9404
0
    const xmlChar *name;
9405
9406
0
    GROW;
9407
0
    if ((RAW != '<') || (NXT(1) != '/')) {
9408
0
  xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
9409
0
  return;
9410
0
    }
9411
0
    SKIP(2);
9412
9413
0
    if (tag->prefix == NULL)
9414
0
        name = xmlParseNameAndCompare(ctxt, ctxt->name);
9415
0
    else
9416
0
        name = xmlParseQNameAndCompare(ctxt, ctxt->name, tag->prefix);
9417
9418
    /*
9419
     * We should definitely be at the ending "S? '>'" part
9420
     */
9421
0
    GROW;
9422
0
    SKIP_BLANKS;
9423
0
    if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
9424
0
  xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
9425
0
    } else
9426
0
  NEXT1;
9427
9428
    /*
9429
     * [ WFC: Element Type Match ]
9430
     * The Name in an element's end-tag must match the element type in the
9431
     * start-tag.
9432
     *
9433
     */
9434
0
    if (name != (xmlChar*)1) {
9435
0
        if (name == NULL) name = BAD_CAST "unparsable";
9436
0
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
9437
0
         "Opening and ending tag mismatch: %s line %d and %s\n",
9438
0
                    ctxt->name, tag->line, name);
9439
0
    }
9440
9441
    /*
9442
     * SAX: End of Tag
9443
     */
9444
0
    if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
9445
0
  (!ctxt->disableSAX))
9446
0
  ctxt->sax->endElementNs(ctxt->userData, ctxt->name, tag->prefix,
9447
0
                                tag->URI);
9448
9449
0
    spacePop(ctxt);
9450
0
    if (tag->nsNr != 0)
9451
0
  xmlParserNsPop(ctxt, tag->nsNr);
9452
0
}
9453
9454
/**
9455
 * Parse escaped pure raw content. Always consumes '<!['.
9456
 *
9457
 * @deprecated Internal function, don't use.
9458
 *
9459
 *     [18] CDSect ::= CDStart CData CDEnd
9460
 *
9461
 *     [19] CDStart ::= '<![CDATA['
9462
 *
9463
 *     [20] Data ::= (Char* - (Char* ']]>' Char*))
9464
 *
9465
 *     [21] CDEnd ::= ']]>'
9466
 * @param ctxt  an XML parser context
9467
 */
9468
void
9469
0
xmlParseCDSect(xmlParserCtxt *ctxt) {
9470
0
    xmlChar *buf = NULL;
9471
0
    int len = 0;
9472
0
    int size = XML_PARSER_BUFFER_SIZE;
9473
0
    int r, rl;
9474
0
    int s, sl;
9475
0
    int cur, l;
9476
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9477
0
                    XML_MAX_HUGE_LENGTH :
9478
0
                    XML_MAX_TEXT_LENGTH;
9479
9480
0
    if ((CUR != '<') || (NXT(1) != '!') || (NXT(2) != '['))
9481
0
        return;
9482
0
    SKIP(3);
9483
9484
0
    if (!CMP6(CUR_PTR, 'C', 'D', 'A', 'T', 'A', '['))
9485
0
        return;
9486
0
    SKIP(6);
9487
9488
0
    r = xmlCurrentCharRecover(ctxt, &rl);
9489
0
    if (!IS_CHAR(r)) {
9490
0
  xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
9491
0
        goto out;
9492
0
    }
9493
0
    NEXTL(rl);
9494
0
    s = xmlCurrentCharRecover(ctxt, &sl);
9495
0
    if (!IS_CHAR(s)) {
9496
0
  xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
9497
0
        goto out;
9498
0
    }
9499
0
    NEXTL(sl);
9500
0
    cur = xmlCurrentCharRecover(ctxt, &l);
9501
0
    buf = xmlMalloc(size);
9502
0
    if (buf == NULL) {
9503
0
  xmlErrMemory(ctxt);
9504
0
        goto out;
9505
0
    }
9506
0
    while (IS_CHAR(cur) &&
9507
0
           ((r != ']') || (s != ']') || (cur != '>'))) {
9508
0
  if (len + 5 >= size) {
9509
0
      xmlChar *tmp;
9510
0
            int newSize;
9511
9512
0
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
9513
0
            if (newSize < 0) {
9514
0
                xmlFatalErrMsg(ctxt, XML_ERR_CDATA_NOT_FINISHED,
9515
0
                               "CData section too big found\n");
9516
0
                goto out;
9517
0
            }
9518
0
      tmp = xmlRealloc(buf, newSize);
9519
0
      if (tmp == NULL) {
9520
0
    xmlErrMemory(ctxt);
9521
0
                goto out;
9522
0
      }
9523
0
      buf = tmp;
9524
0
      size = newSize;
9525
0
  }
9526
0
  COPY_BUF(buf, len, r);
9527
0
  r = s;
9528
0
  rl = sl;
9529
0
  s = cur;
9530
0
  sl = l;
9531
0
  NEXTL(l);
9532
0
  cur = xmlCurrentCharRecover(ctxt, &l);
9533
0
    }
9534
0
    buf[len] = 0;
9535
0
    if (cur != '>') {
9536
0
  xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
9537
0
                       "CData section not finished\n%.50s\n", buf);
9538
0
        goto out;
9539
0
    }
9540
0
    NEXTL(l);
9541
9542
    /*
9543
     * OK the buffer is to be consumed as cdata.
9544
     */
9545
0
    if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9546
0
        if ((ctxt->sax->cdataBlock != NULL) &&
9547
0
            ((ctxt->options & XML_PARSE_NOCDATA) == 0)) {
9548
0
            ctxt->sax->cdataBlock(ctxt->userData, buf, len);
9549
0
        } else if (ctxt->sax->characters != NULL) {
9550
0
            ctxt->sax->characters(ctxt->userData, buf, len);
9551
0
        }
9552
0
    }
9553
9554
0
out:
9555
0
    xmlFree(buf);
9556
0
}
9557
9558
/**
9559
 * Parse a content sequence. Stops at EOF or '</'. Leaves checking of
9560
 * unexpected EOF to the caller.
9561
 *
9562
 * @param ctxt  an XML parser context
9563
 */
9564
9565
static void
9566
0
xmlParseContentInternal(xmlParserCtxtPtr ctxt) {
9567
0
    int oldNameNr = ctxt->nameNr;
9568
0
    int oldSpaceNr = ctxt->spaceNr;
9569
0
    int oldNodeNr = ctxt->nodeNr;
9570
9571
0
    GROW;
9572
0
    while ((ctxt->input->cur < ctxt->input->end) &&
9573
0
     (PARSER_STOPPED(ctxt) == 0)) {
9574
0
  const xmlChar *cur = ctxt->input->cur;
9575
9576
  /*
9577
   * First case : a Processing Instruction.
9578
   */
9579
0
  if ((*cur == '<') && (cur[1] == '?')) {
9580
0
      xmlParsePI(ctxt);
9581
0
  }
9582
9583
  /*
9584
   * Second case : a CDSection
9585
   */
9586
  /* 2.6.0 test was *cur not RAW */
9587
0
  else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
9588
0
      xmlParseCDSect(ctxt);
9589
0
  }
9590
9591
  /*
9592
   * Third case :  a comment
9593
   */
9594
0
  else if ((*cur == '<') && (NXT(1) == '!') &&
9595
0
     (NXT(2) == '-') && (NXT(3) == '-')) {
9596
0
      xmlParseComment(ctxt);
9597
0
  }
9598
9599
  /*
9600
   * Fourth case :  a sub-element.
9601
   */
9602
0
  else if (*cur == '<') {
9603
0
            if (NXT(1) == '/') {
9604
0
                if (ctxt->nameNr <= oldNameNr)
9605
0
                    break;
9606
0
          xmlParseElementEnd(ctxt);
9607
0
            } else {
9608
0
          xmlParseElementStart(ctxt);
9609
0
            }
9610
0
  }
9611
9612
  /*
9613
   * Fifth case : a reference. If if has not been resolved,
9614
   *    parsing returns it's Name, create the node
9615
   */
9616
9617
0
  else if (*cur == '&') {
9618
0
      xmlParseReference(ctxt);
9619
0
  }
9620
9621
  /*
9622
   * Last case, text. Note that References are handled directly.
9623
   */
9624
0
  else {
9625
0
      xmlParseCharDataInternal(ctxt, 0);
9626
0
  }
9627
9628
0
  SHRINK;
9629
0
  GROW;
9630
0
    }
9631
9632
0
    if ((ctxt->nameNr > oldNameNr) &&
9633
0
        (ctxt->input->cur >= ctxt->input->end) &&
9634
0
        (ctxt->wellFormed)) {
9635
0
        const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
9636
0
        int line = ctxt->pushTab[ctxt->nameNr - 1].line;
9637
0
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
9638
0
                "Premature end of data in tag %s line %d\n",
9639
0
                name, line, NULL);
9640
0
    }
9641
9642
    /*
9643
     * Clean up in error case
9644
     */
9645
9646
0
    while (ctxt->nodeNr > oldNodeNr)
9647
0
        nodePop(ctxt);
9648
9649
0
    while (ctxt->nameNr > oldNameNr) {
9650
0
        xmlStartTag *tag = &ctxt->pushTab[ctxt->nameNr - 1];
9651
9652
0
        if (tag->nsNr != 0)
9653
0
            xmlParserNsPop(ctxt, tag->nsNr);
9654
9655
0
        namePop(ctxt);
9656
0
    }
9657
9658
0
    while (ctxt->spaceNr > oldSpaceNr)
9659
0
        spacePop(ctxt);
9660
0
}
9661
9662
/**
9663
 * Parse XML element content. This is useful if you're only interested
9664
 * in custom SAX callbacks. If you want a node list, use
9665
 * #xmlCtxtParseContent.
9666
 *
9667
 * @param ctxt  an XML parser context
9668
 */
9669
void
9670
0
xmlParseContent(xmlParserCtxt *ctxt) {
9671
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
9672
0
        return;
9673
9674
0
    xmlCtxtInitializeLate(ctxt);
9675
9676
0
    xmlParseContentInternal(ctxt);
9677
9678
0
    xmlParserCheckEOF(ctxt, XML_ERR_NOT_WELL_BALANCED);
9679
0
}
9680
9681
/**
9682
 * Parse an XML element
9683
 *
9684
 * @deprecated Internal function, don't use.
9685
 *
9686
 *     [39] element ::= EmptyElemTag | STag content ETag
9687
 *
9688
 * [ WFC: Element Type Match ]
9689
 * The Name in an element's end-tag must match the element type in the
9690
 * start-tag.
9691
 *
9692
 * @param ctxt  an XML parser context
9693
 */
9694
9695
void
9696
0
xmlParseElement(xmlParserCtxt *ctxt) {
9697
0
    if (xmlParseElementStart(ctxt) != 0)
9698
0
        return;
9699
9700
0
    xmlParseContentInternal(ctxt);
9701
9702
0
    if (ctxt->input->cur >= ctxt->input->end) {
9703
0
        if (ctxt->wellFormed) {
9704
0
            const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
9705
0
            int line = ctxt->pushTab[ctxt->nameNr - 1].line;
9706
0
            xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
9707
0
                    "Premature end of data in tag %s line %d\n",
9708
0
                    name, line, NULL);
9709
0
        }
9710
0
        return;
9711
0
    }
9712
9713
0
    xmlParseElementEnd(ctxt);
9714
0
}
9715
9716
/**
9717
 * Parse the start of an XML element. Returns -1 in case of error, 0 if an
9718
 * opening tag was parsed, 1 if an empty element was parsed.
9719
 *
9720
 * Always consumes '<'.
9721
 *
9722
 * @param ctxt  an XML parser context
9723
 */
9724
static int
9725
0
xmlParseElementStart(xmlParserCtxtPtr ctxt) {
9726
0
    int maxDepth = (ctxt->options & XML_PARSE_HUGE) ? 2048 : 256;
9727
0
    const xmlChar *name;
9728
0
    const xmlChar *prefix = NULL;
9729
0
    const xmlChar *URI = NULL;
9730
0
    xmlParserNodeInfo node_info;
9731
0
    int line;
9732
0
    xmlNodePtr cur;
9733
0
    int nbNs = 0;
9734
9735
0
    if (ctxt->nameNr > maxDepth) {
9736
0
        xmlFatalErrMsgInt(ctxt, XML_ERR_RESOURCE_LIMIT,
9737
0
                "Excessive depth in document: %d use XML_PARSE_HUGE option\n",
9738
0
                ctxt->nameNr);
9739
0
  xmlHaltParser(ctxt);
9740
0
  return(-1);
9741
0
    }
9742
9743
    /* Capture start position */
9744
0
    if (ctxt->record_info) {
9745
0
        node_info.begin_pos = ctxt->input->consumed +
9746
0
                          (CUR_PTR - ctxt->input->base);
9747
0
  node_info.begin_line = ctxt->input->line;
9748
0
    }
9749
9750
0
    if (ctxt->spaceNr == 0)
9751
0
  spacePush(ctxt, -1);
9752
0
    else if (*ctxt->space == -2)
9753
0
  spacePush(ctxt, -1);
9754
0
    else
9755
0
  spacePush(ctxt, *ctxt->space);
9756
9757
0
    line = ctxt->input->line;
9758
0
#ifdef LIBXML_SAX1_ENABLED
9759
0
    if (ctxt->sax2)
9760
0
#endif /* LIBXML_SAX1_ENABLED */
9761
0
        name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
9762
0
#ifdef LIBXML_SAX1_ENABLED
9763
0
    else
9764
0
  name = xmlParseStartTag(ctxt);
9765
0
#endif /* LIBXML_SAX1_ENABLED */
9766
0
    if (name == NULL) {
9767
0
  spacePop(ctxt);
9768
0
        return(-1);
9769
0
    }
9770
0
    nameNsPush(ctxt, name, prefix, URI, line, nbNs);
9771
0
    cur = ctxt->node;
9772
9773
0
#ifdef LIBXML_VALID_ENABLED
9774
    /*
9775
     * [ VC: Root Element Type ]
9776
     * The Name in the document type declaration must match the element
9777
     * type of the root element.
9778
     */
9779
0
    if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9780
0
        ctxt->node && (ctxt->node == ctxt->myDoc->children))
9781
0
        ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
9782
0
#endif /* LIBXML_VALID_ENABLED */
9783
9784
    /*
9785
     * Check for an Empty Element.
9786
     */
9787
0
    if ((RAW == '/') && (NXT(1) == '>')) {
9788
0
        SKIP(2);
9789
0
  if (ctxt->sax2) {
9790
0
      if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
9791
0
    (!ctxt->disableSAX))
9792
0
    ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
9793
0
#ifdef LIBXML_SAX1_ENABLED
9794
0
  } else {
9795
0
      if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
9796
0
    (!ctxt->disableSAX))
9797
0
    ctxt->sax->endElement(ctxt->userData, name);
9798
0
#endif /* LIBXML_SAX1_ENABLED */
9799
0
  }
9800
0
  namePop(ctxt);
9801
0
  spacePop(ctxt);
9802
0
  if (nbNs > 0)
9803
0
      xmlParserNsPop(ctxt, nbNs);
9804
0
  if (cur != NULL && ctxt->record_info) {
9805
0
            node_info.node = cur;
9806
0
            node_info.end_pos = ctxt->input->consumed +
9807
0
                                (CUR_PTR - ctxt->input->base);
9808
0
            node_info.end_line = ctxt->input->line;
9809
0
            xmlParserAddNodeInfo(ctxt, &node_info);
9810
0
  }
9811
0
  return(1);
9812
0
    }
9813
0
    if (RAW == '>') {
9814
0
        NEXT1;
9815
0
        if (cur != NULL && ctxt->record_info) {
9816
0
            node_info.node = cur;
9817
0
            node_info.end_pos = 0;
9818
0
            node_info.end_line = 0;
9819
0
            xmlParserAddNodeInfo(ctxt, &node_info);
9820
0
        }
9821
0
    } else {
9822
0
        xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
9823
0
         "Couldn't find end of Start Tag %s line %d\n",
9824
0
                    name, line, NULL);
9825
9826
  /*
9827
   * end of parsing of this node.
9828
   */
9829
0
  nodePop(ctxt);
9830
0
  namePop(ctxt);
9831
0
  spacePop(ctxt);
9832
0
  if (nbNs > 0)
9833
0
      xmlParserNsPop(ctxt, nbNs);
9834
0
  return(-1);
9835
0
    }
9836
9837
0
    return(0);
9838
0
}
9839
9840
/**
9841
 * Parse the end of an XML element. Always consumes '</'.
9842
 *
9843
 * @param ctxt  an XML parser context
9844
 */
9845
static void
9846
0
xmlParseElementEnd(xmlParserCtxtPtr ctxt) {
9847
0
    xmlNodePtr cur = ctxt->node;
9848
9849
0
    if (ctxt->nameNr <= 0) {
9850
0
        if ((RAW == '<') && (NXT(1) == '/'))
9851
0
            SKIP(2);
9852
0
        return;
9853
0
    }
9854
9855
    /*
9856
     * parse the end of tag: '</' should be here.
9857
     */
9858
0
    if (ctxt->sax2) {
9859
0
  xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
9860
0
  namePop(ctxt);
9861
0
    }
9862
0
#ifdef LIBXML_SAX1_ENABLED
9863
0
    else
9864
0
  xmlParseEndTag1(ctxt, 0);
9865
0
#endif /* LIBXML_SAX1_ENABLED */
9866
9867
    /*
9868
     * Capture end position
9869
     */
9870
0
    if (cur != NULL && ctxt->record_info) {
9871
0
        xmlParserNodeInfoPtr node_info;
9872
9873
0
        node_info = (xmlParserNodeInfoPtr) xmlParserFindNodeInfo(ctxt, cur);
9874
0
        if (node_info != NULL) {
9875
0
            node_info->end_pos = ctxt->input->consumed +
9876
0
                                 (CUR_PTR - ctxt->input->base);
9877
0
            node_info->end_line = ctxt->input->line;
9878
0
        }
9879
0
    }
9880
0
}
9881
9882
/**
9883
 * Parse the XML version value.
9884
 *
9885
 * @deprecated Internal function, don't use.
9886
 *
9887
 *     [26] VersionNum ::= '1.' [0-9]+
9888
 *
9889
 * In practice allow [0-9].[0-9]+ at that level
9890
 *
9891
 * @param ctxt  an XML parser context
9892
 * @returns the string giving the XML version number, or NULL
9893
 */
9894
xmlChar *
9895
0
xmlParseVersionNum(xmlParserCtxt *ctxt) {
9896
0
    xmlChar *buf = NULL;
9897
0
    int len = 0;
9898
0
    int size = 10;
9899
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
9900
0
                    XML_MAX_TEXT_LENGTH :
9901
0
                    XML_MAX_NAME_LENGTH;
9902
0
    xmlChar cur;
9903
9904
0
    buf = xmlMalloc(size);
9905
0
    if (buf == NULL) {
9906
0
  xmlErrMemory(ctxt);
9907
0
  return(NULL);
9908
0
    }
9909
0
    cur = CUR;
9910
0
    if (!((cur >= '0') && (cur <= '9'))) {
9911
0
  xmlFree(buf);
9912
0
  return(NULL);
9913
0
    }
9914
0
    buf[len++] = cur;
9915
0
    NEXT;
9916
0
    cur=CUR;
9917
0
    if (cur != '.') {
9918
0
  xmlFree(buf);
9919
0
  return(NULL);
9920
0
    }
9921
0
    buf[len++] = cur;
9922
0
    NEXT;
9923
0
    cur=CUR;
9924
0
    while ((cur >= '0') && (cur <= '9')) {
9925
0
  if (len + 1 >= size) {
9926
0
      xmlChar *tmp;
9927
0
            int newSize;
9928
9929
0
            newSize = xmlGrowCapacity(size, 1, 1, maxLength);
9930
0
            if (newSize < 0) {
9931
0
                xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "VersionNum");
9932
0
                xmlFree(buf);
9933
0
                return(NULL);
9934
0
            }
9935
0
      tmp = xmlRealloc(buf, newSize);
9936
0
      if (tmp == NULL) {
9937
0
    xmlErrMemory(ctxt);
9938
0
          xmlFree(buf);
9939
0
    return(NULL);
9940
0
      }
9941
0
      buf = tmp;
9942
0
            size = newSize;
9943
0
  }
9944
0
  buf[len++] = cur;
9945
0
  NEXT;
9946
0
  cur=CUR;
9947
0
    }
9948
0
    buf[len] = 0;
9949
0
    return(buf);
9950
0
}
9951
9952
/**
9953
 * Parse the XML version.
9954
 *
9955
 * @deprecated Internal function, don't use.
9956
 *
9957
 *     [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
9958
 *
9959
 *     [25] Eq ::= S? '=' S?
9960
 *
9961
 * @param ctxt  an XML parser context
9962
 * @returns the version string, e.g. "1.0"
9963
 */
9964
9965
xmlChar *
9966
0
xmlParseVersionInfo(xmlParserCtxt *ctxt) {
9967
0
    xmlChar *version = NULL;
9968
9969
0
    if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
9970
0
  SKIP(7);
9971
0
  SKIP_BLANKS;
9972
0
  if (RAW != '=') {
9973
0
      xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
9974
0
      return(NULL);
9975
0
        }
9976
0
  NEXT;
9977
0
  SKIP_BLANKS;
9978
0
  if (RAW == '"') {
9979
0
      NEXT;
9980
0
      version = xmlParseVersionNum(ctxt);
9981
0
      if (RAW != '"') {
9982
0
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
9983
0
      } else
9984
0
          NEXT;
9985
0
  } else if (RAW == '\''){
9986
0
      NEXT;
9987
0
      version = xmlParseVersionNum(ctxt);
9988
0
      if (RAW != '\'') {
9989
0
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
9990
0
      } else
9991
0
          NEXT;
9992
0
  } else {
9993
0
      xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
9994
0
  }
9995
0
    }
9996
0
    return(version);
9997
0
}
9998
9999
/**
10000
 * Parse the XML encoding name
10001
 *
10002
 * @deprecated Internal function, don't use.
10003
 *
10004
 *     [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
10005
 *
10006
 * @param ctxt  an XML parser context
10007
 * @returns the encoding name value or NULL
10008
 */
10009
xmlChar *
10010
0
xmlParseEncName(xmlParserCtxt *ctxt) {
10011
0
    xmlChar *buf = NULL;
10012
0
    int len = 0;
10013
0
    int size = 10;
10014
0
    int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
10015
0
                    XML_MAX_TEXT_LENGTH :
10016
0
                    XML_MAX_NAME_LENGTH;
10017
0
    xmlChar cur;
10018
10019
0
    cur = CUR;
10020
0
    if (((cur >= 'a') && (cur <= 'z')) ||
10021
0
        ((cur >= 'A') && (cur <= 'Z'))) {
10022
0
  buf = xmlMalloc(size);
10023
0
  if (buf == NULL) {
10024
0
      xmlErrMemory(ctxt);
10025
0
      return(NULL);
10026
0
  }
10027
10028
0
  buf[len++] = cur;
10029
0
  NEXT;
10030
0
  cur = CUR;
10031
0
  while (((cur >= 'a') && (cur <= 'z')) ||
10032
0
         ((cur >= 'A') && (cur <= 'Z')) ||
10033
0
         ((cur >= '0') && (cur <= '9')) ||
10034
0
         (cur == '.') || (cur == '_') ||
10035
0
         (cur == '-')) {
10036
0
      if (len + 1 >= size) {
10037
0
          xmlChar *tmp;
10038
0
                int newSize;
10039
10040
0
                newSize = xmlGrowCapacity(size, 1, 1, maxLength);
10041
0
                if (newSize < 0) {
10042
0
                    xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "EncName");
10043
0
                    xmlFree(buf);
10044
0
                    return(NULL);
10045
0
                }
10046
0
    tmp = xmlRealloc(buf, newSize);
10047
0
    if (tmp == NULL) {
10048
0
        xmlErrMemory(ctxt);
10049
0
        xmlFree(buf);
10050
0
        return(NULL);
10051
0
    }
10052
0
    buf = tmp;
10053
0
                size = newSize;
10054
0
      }
10055
0
      buf[len++] = cur;
10056
0
      NEXT;
10057
0
      cur = CUR;
10058
0
        }
10059
0
  buf[len] = 0;
10060
0
    } else {
10061
0
  xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
10062
0
    }
10063
0
    return(buf);
10064
0
}
10065
10066
/**
10067
 * Parse the XML encoding declaration
10068
 *
10069
 * @deprecated Internal function, don't use.
10070
 *
10071
 *     [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | 
10072
 *                           "'" EncName "'")
10073
 *
10074
 * this setups the conversion filters.
10075
 *
10076
 * @param ctxt  an XML parser context
10077
 * @returns the encoding value or NULL
10078
 */
10079
10080
const xmlChar *
10081
0
xmlParseEncodingDecl(xmlParserCtxt *ctxt) {
10082
0
    xmlChar *encoding = NULL;
10083
10084
0
    SKIP_BLANKS;
10085
0
    if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g') == 0)
10086
0
        return(NULL);
10087
10088
0
    SKIP(8);
10089
0
    SKIP_BLANKS;
10090
0
    if (RAW != '=') {
10091
0
        xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
10092
0
        return(NULL);
10093
0
    }
10094
0
    NEXT;
10095
0
    SKIP_BLANKS;
10096
0
    if (RAW == '"') {
10097
0
        NEXT;
10098
0
        encoding = xmlParseEncName(ctxt);
10099
0
        if (RAW != '"') {
10100
0
            xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10101
0
            xmlFree(encoding);
10102
0
            return(NULL);
10103
0
        } else
10104
0
            NEXT;
10105
0
    } else if (RAW == '\''){
10106
0
        NEXT;
10107
0
        encoding = xmlParseEncName(ctxt);
10108
0
        if (RAW != '\'') {
10109
0
            xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10110
0
            xmlFree(encoding);
10111
0
            return(NULL);
10112
0
        } else
10113
0
            NEXT;
10114
0
    } else {
10115
0
        xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10116
0
    }
10117
10118
0
    if (encoding == NULL)
10119
0
        return(NULL);
10120
10121
0
    xmlSetDeclaredEncoding(ctxt, encoding);
10122
10123
0
    return(ctxt->encoding);
10124
0
}
10125
10126
/**
10127
 * Parse the XML standalone declaration
10128
 *
10129
 * @deprecated Internal function, don't use.
10130
 *
10131
 *     [32] SDDecl ::= S 'standalone' Eq
10132
 *                     (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
10133
 *
10134
 * [ VC: Standalone Document Declaration ]
10135
 * TODO The standalone document declaration must have the value "no"
10136
 * if any external markup declarations contain declarations of:
10137
 *  - attributes with default values, if elements to which these
10138
 *    attributes apply appear in the document without specifications
10139
 *    of values for these attributes, or
10140
 *  - entities (other than amp, lt, gt, apos, quot), if references
10141
 *    to those entities appear in the document, or
10142
 *  - attributes with values subject to normalization, where the
10143
 *    attribute appears in the document with a value which will change
10144
 *    as a result of normalization, or
10145
 *  - element types with element content, if white space occurs directly
10146
 *    within any instance of those types.
10147
 *
10148
 * @param ctxt  an XML parser context
10149
 * @returns
10150
 *   1 if standalone="yes"
10151
 *   0 if standalone="no"
10152
 *  -2 if standalone attribute is missing or invalid
10153
 *    (A standalone value of -2 means that the XML declaration was found,
10154
 *     but no value was specified for the standalone attribute).
10155
 */
10156
10157
int
10158
0
xmlParseSDDecl(xmlParserCtxt *ctxt) {
10159
0
    int standalone = -2;
10160
10161
0
    SKIP_BLANKS;
10162
0
    if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
10163
0
  SKIP(10);
10164
0
        SKIP_BLANKS;
10165
0
  if (RAW != '=') {
10166
0
      xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
10167
0
      return(standalone);
10168
0
        }
10169
0
  NEXT;
10170
0
  SKIP_BLANKS;
10171
0
        if (RAW == '\''){
10172
0
      NEXT;
10173
0
      if ((RAW == 'n') && (NXT(1) == 'o')) {
10174
0
          standalone = 0;
10175
0
                SKIP(2);
10176
0
      } else if ((RAW == 'y') && (NXT(1) == 'e') &&
10177
0
                 (NXT(2) == 's')) {
10178
0
          standalone = 1;
10179
0
    SKIP(3);
10180
0
            } else {
10181
0
    xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
10182
0
      }
10183
0
      if (RAW != '\'') {
10184
0
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10185
0
      } else
10186
0
          NEXT;
10187
0
  } else if (RAW == '"'){
10188
0
      NEXT;
10189
0
      if ((RAW == 'n') && (NXT(1) == 'o')) {
10190
0
          standalone = 0;
10191
0
    SKIP(2);
10192
0
      } else if ((RAW == 'y') && (NXT(1) == 'e') &&
10193
0
                 (NXT(2) == 's')) {
10194
0
          standalone = 1;
10195
0
                SKIP(3);
10196
0
            } else {
10197
0
    xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
10198
0
      }
10199
0
      if (RAW != '"') {
10200
0
    xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
10201
0
      } else
10202
0
          NEXT;
10203
0
  } else {
10204
0
      xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
10205
0
        }
10206
0
    }
10207
0
    return(standalone);
10208
0
}
10209
10210
/**
10211
 * Parse an XML declaration header
10212
 *
10213
 * @deprecated Internal function, don't use.
10214
 *
10215
 *     [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
10216
 * @param ctxt  an XML parser context
10217
 */
10218
10219
void
10220
0
xmlParseXMLDecl(xmlParserCtxt *ctxt) {
10221
0
    xmlChar *version;
10222
10223
    /*
10224
     * This value for standalone indicates that the document has an
10225
     * XML declaration but it does not have a standalone attribute.
10226
     * It will be overwritten later if a standalone attribute is found.
10227
     */
10228
10229
0
    ctxt->standalone = -2;
10230
10231
    /*
10232
     * We know that '<?xml' is here.
10233
     */
10234
0
    SKIP(5);
10235
10236
0
    if (!IS_BLANK_CH(RAW)) {
10237
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
10238
0
                 "Blank needed after '<?xml'\n");
10239
0
    }
10240
0
    SKIP_BLANKS;
10241
10242
    /*
10243
     * We must have the VersionInfo here.
10244
     */
10245
0
    version = xmlParseVersionInfo(ctxt);
10246
0
    if (version == NULL) {
10247
0
  xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
10248
0
    } else {
10249
0
  if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
10250
      /*
10251
       * Changed here for XML-1.0 5th edition
10252
       */
10253
0
      if (ctxt->options & XML_PARSE_OLD10) {
10254
0
    xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
10255
0
                "Unsupported version '%s'\n",
10256
0
                version);
10257
0
      } else {
10258
0
          if ((version[0] == '1') && ((version[1] == '.'))) {
10259
0
        xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
10260
0
                      "Unsupported version '%s'\n",
10261
0
          version, NULL);
10262
0
    } else {
10263
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION,
10264
0
              "Unsupported version '%s'\n",
10265
0
              version);
10266
0
    }
10267
0
      }
10268
0
  }
10269
0
  if (ctxt->version != NULL)
10270
0
      xmlFree(ctxt->version);
10271
0
  ctxt->version = version;
10272
0
    }
10273
10274
    /*
10275
     * We may have the encoding declaration
10276
     */
10277
0
    if (!IS_BLANK_CH(RAW)) {
10278
0
        if ((RAW == '?') && (NXT(1) == '>')) {
10279
0
      SKIP(2);
10280
0
      return;
10281
0
  }
10282
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
10283
0
    }
10284
0
    xmlParseEncodingDecl(ctxt);
10285
10286
    /*
10287
     * We may have the standalone status.
10288
     */
10289
0
    if ((ctxt->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
10290
0
        if ((RAW == '?') && (NXT(1) == '>')) {
10291
0
      SKIP(2);
10292
0
      return;
10293
0
  }
10294
0
  xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
10295
0
    }
10296
10297
    /*
10298
     * We can grow the input buffer freely at that point
10299
     */
10300
0
    GROW;
10301
10302
0
    SKIP_BLANKS;
10303
0
    ctxt->standalone = xmlParseSDDecl(ctxt);
10304
10305
0
    SKIP_BLANKS;
10306
0
    if ((RAW == '?') && (NXT(1) == '>')) {
10307
0
        SKIP(2);
10308
0
    } else if (RAW == '>') {
10309
        /* Deprecated old WD ... */
10310
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
10311
0
  NEXT;
10312
0
    } else {
10313
0
        int c;
10314
10315
0
  xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
10316
0
        while ((PARSER_STOPPED(ctxt) == 0) &&
10317
0
               ((c = CUR) != 0)) {
10318
0
            NEXT;
10319
0
            if (c == '>')
10320
0
                break;
10321
0
        }
10322
0
    }
10323
0
}
10324
10325
/**
10326
 * @since 2.14.0
10327
 *
10328
 * @param ctxt  parser context
10329
 * @returns the version from the XML declaration.
10330
 */
10331
const xmlChar *
10332
0
xmlCtxtGetVersion(xmlParserCtxt *ctxt) {
10333
0
    if (ctxt == NULL)
10334
0
        return(NULL);
10335
10336
0
    return(ctxt->version);
10337
0
}
10338
10339
/**
10340
 * @since 2.14.0
10341
 *
10342
 * @param ctxt  parser context
10343
 * @returns the value from the standalone document declaration.
10344
 */
10345
int
10346
0
xmlCtxtGetStandalone(xmlParserCtxt *ctxt) {
10347
0
    if (ctxt == NULL)
10348
0
        return(0);
10349
10350
0
    return(ctxt->standalone);
10351
0
}
10352
10353
/**
10354
 * Parse an XML Misc* optional field.
10355
 *
10356
 * @deprecated Internal function, don't use.
10357
 *
10358
 *     [27] Misc ::= Comment | PI |  S
10359
 * @param ctxt  an XML parser context
10360
 */
10361
10362
void
10363
0
xmlParseMisc(xmlParserCtxt *ctxt) {
10364
0
    while (PARSER_STOPPED(ctxt) == 0) {
10365
0
        SKIP_BLANKS;
10366
0
        GROW;
10367
0
        if ((RAW == '<') && (NXT(1) == '?')) {
10368
0
      xmlParsePI(ctxt);
10369
0
        } else if (CMP4(CUR_PTR, '<', '!', '-', '-')) {
10370
0
      xmlParseComment(ctxt);
10371
0
        } else {
10372
0
            break;
10373
0
        }
10374
0
    }
10375
0
}
10376
10377
static void
10378
0
xmlFinishDocument(xmlParserCtxtPtr ctxt) {
10379
0
    xmlDocPtr doc;
10380
10381
    /*
10382
     * SAX: end of the document processing.
10383
     */
10384
0
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
10385
0
        ctxt->sax->endDocument(ctxt->userData);
10386
10387
    /*
10388
     * Remove locally kept entity definitions if the tree was not built
10389
     */
10390
0
    doc = ctxt->myDoc;
10391
0
    if ((doc != NULL) &&
10392
0
        (xmlStrEqual(doc->version, SAX_COMPAT_MODE))) {
10393
0
        xmlFreeDoc(doc);
10394
0
        ctxt->myDoc = NULL;
10395
0
    }
10396
0
}
10397
10398
/**
10399
 * Parse an XML document and invoke the SAX handlers. This is useful
10400
 * if you're only interested in custom SAX callbacks. If you want a
10401
 * document tree, use #xmlCtxtParseDocument.
10402
 *
10403
 * @param ctxt  an XML parser context
10404
 * @returns 0, -1 in case of error.
10405
 */
10406
10407
int
10408
0
xmlParseDocument(xmlParserCtxt *ctxt) {
10409
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
10410
0
        return(-1);
10411
10412
0
    GROW;
10413
10414
    /*
10415
     * SAX: detecting the level.
10416
     */
10417
0
    xmlCtxtInitializeLate(ctxt);
10418
10419
0
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10420
0
        ctxt->sax->setDocumentLocator(ctxt->userData,
10421
0
                (xmlSAXLocator *) &xmlDefaultSAXLocator);
10422
0
    }
10423
10424
0
    xmlDetectEncoding(ctxt);
10425
10426
0
    if (CUR == 0) {
10427
0
  xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
10428
0
  return(-1);
10429
0
    }
10430
10431
0
    GROW;
10432
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
10433
10434
  /*
10435
   * Note that we will switch encoding on the fly.
10436
   */
10437
0
  xmlParseXMLDecl(ctxt);
10438
0
  SKIP_BLANKS;
10439
0
    } else {
10440
0
  ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10441
0
        if (ctxt->version == NULL) {
10442
0
            xmlErrMemory(ctxt);
10443
0
            return(-1);
10444
0
        }
10445
0
    }
10446
0
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
10447
0
        ctxt->sax->startDocument(ctxt->userData);
10448
0
    if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) &&
10449
0
        (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) {
10450
0
  ctxt->myDoc->compression = ctxt->input->buf->compressed;
10451
0
    }
10452
10453
    /*
10454
     * The Misc part of the Prolog
10455
     */
10456
0
    xmlParseMisc(ctxt);
10457
10458
    /*
10459
     * Then possibly doc type declaration(s) and more Misc
10460
     * (doctypedecl Misc*)?
10461
     */
10462
0
    GROW;
10463
0
    if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
10464
10465
0
  ctxt->inSubset = 1;
10466
0
  xmlParseDocTypeDecl(ctxt);
10467
0
  if (RAW == '[') {
10468
0
      xmlParseInternalSubset(ctxt);
10469
0
  } else if (RAW == '>') {
10470
0
            NEXT;
10471
0
        }
10472
10473
  /*
10474
   * Create and update the external subset.
10475
   */
10476
0
  ctxt->inSubset = 2;
10477
0
  if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
10478
0
      (!ctxt->disableSAX))
10479
0
      ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
10480
0
                                ctxt->extSubSystem, ctxt->extSubURI);
10481
0
  ctxt->inSubset = 0;
10482
10483
0
        xmlCleanSpecialAttr(ctxt);
10484
10485
0
  xmlParseMisc(ctxt);
10486
0
    }
10487
10488
    /*
10489
     * Time to start parsing the tree itself
10490
     */
10491
0
    GROW;
10492
0
    if (RAW != '<') {
10493
0
        if (ctxt->wellFormed)
10494
0
            xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
10495
0
                           "Start tag expected, '<' not found\n");
10496
0
    } else {
10497
0
  xmlParseElement(ctxt);
10498
10499
  /*
10500
   * The Misc part at the end
10501
   */
10502
0
  xmlParseMisc(ctxt);
10503
10504
0
        xmlParserCheckEOF(ctxt, XML_ERR_DOCUMENT_END);
10505
0
    }
10506
10507
0
    ctxt->instate = XML_PARSER_EOF;
10508
0
    xmlFinishDocument(ctxt);
10509
10510
0
    if (! ctxt->wellFormed) {
10511
0
  ctxt->valid = 0;
10512
0
  return(-1);
10513
0
    }
10514
10515
0
    return(0);
10516
0
}
10517
10518
/**
10519
 * Parse a general parsed entity
10520
 * An external general parsed entity is well-formed if it matches the
10521
 * production labeled extParsedEnt.
10522
 *
10523
 * @deprecated Internal function, don't use.
10524
 *
10525
 *     [78] extParsedEnt ::= TextDecl? content
10526
 *
10527
 * @param ctxt  an XML parser context
10528
 * @returns 0, -1 in case of error. the parser context is augmented
10529
 *                as a result of the parsing.
10530
 */
10531
10532
int
10533
0
xmlParseExtParsedEnt(xmlParserCtxt *ctxt) {
10534
0
    if ((ctxt == NULL) || (ctxt->input == NULL))
10535
0
        return(-1);
10536
10537
0
    xmlCtxtInitializeLate(ctxt);
10538
10539
0
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10540
0
        ctxt->sax->setDocumentLocator(ctxt->userData,
10541
0
                (xmlSAXLocator *) &xmlDefaultSAXLocator);
10542
0
    }
10543
10544
0
    xmlDetectEncoding(ctxt);
10545
10546
0
    if (CUR == 0) {
10547
0
  xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
10548
0
    }
10549
10550
    /*
10551
     * Check for the XMLDecl in the Prolog.
10552
     */
10553
0
    GROW;
10554
0
    if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
10555
10556
  /*
10557
   * Note that we will switch encoding on the fly.
10558
   */
10559
0
  xmlParseXMLDecl(ctxt);
10560
0
  SKIP_BLANKS;
10561
0
    } else {
10562
0
  ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10563
0
    }
10564
0
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
10565
0
        ctxt->sax->startDocument(ctxt->userData);
10566
10567
    /*
10568
     * Doing validity checking on chunk doesn't make sense
10569
     */
10570
0
    ctxt->options &= ~XML_PARSE_DTDVALID;
10571
0
    ctxt->validate = 0;
10572
0
    ctxt->depth = 0;
10573
10574
0
    xmlParseContentInternal(ctxt);
10575
10576
0
    if (ctxt->input->cur < ctxt->input->end)
10577
0
  xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
10578
10579
    /*
10580
     * SAX: end of the document processing.
10581
     */
10582
0
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
10583
0
        ctxt->sax->endDocument(ctxt->userData);
10584
10585
0
    if (! ctxt->wellFormed) return(-1);
10586
0
    return(0);
10587
0
}
10588
10589
#ifdef LIBXML_PUSH_ENABLED
10590
/************************************************************************
10591
 *                  *
10592
 *    Progressive parsing interfaces        *
10593
 *                  *
10594
 ************************************************************************/
10595
10596
/**
10597
 * Check whether the input buffer contains a character.
10598
 *
10599
 * @param ctxt  an XML parser context
10600
 * @param c  character
10601
 */
10602
static int
10603
0
xmlParseLookupChar(xmlParserCtxtPtr ctxt, int c) {
10604
0
    const xmlChar *cur;
10605
10606
0
    if (ctxt->checkIndex == 0) {
10607
0
        cur = ctxt->input->cur + 1;
10608
0
    } else {
10609
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10610
0
    }
10611
10612
0
    if (memchr(cur, c, ctxt->input->end - cur) == NULL) {
10613
0
        size_t index = ctxt->input->end - ctxt->input->cur;
10614
10615
0
        if (index > LONG_MAX) {
10616
0
            ctxt->checkIndex = 0;
10617
0
            return(1);
10618
0
        }
10619
0
        ctxt->checkIndex = index;
10620
0
        return(0);
10621
0
    } else {
10622
0
        ctxt->checkIndex = 0;
10623
0
        return(1);
10624
0
    }
10625
0
}
10626
10627
/**
10628
 * Check whether the input buffer contains a string.
10629
 *
10630
 * @param ctxt  an XML parser context
10631
 * @param startDelta  delta to apply at the start
10632
 * @param str  string
10633
 * @param strLen  length of string
10634
 */
10635
static const xmlChar *
10636
xmlParseLookupString(xmlParserCtxtPtr ctxt, size_t startDelta,
10637
0
                     const char *str, size_t strLen) {
10638
0
    const xmlChar *cur, *term;
10639
10640
0
    if (ctxt->checkIndex == 0) {
10641
0
        cur = ctxt->input->cur + startDelta;
10642
0
    } else {
10643
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10644
0
    }
10645
10646
0
    term = BAD_CAST strstr((const char *) cur, str);
10647
0
    if (term == NULL) {
10648
0
        const xmlChar *end = ctxt->input->end;
10649
0
        size_t index;
10650
10651
        /* Rescan (strLen - 1) characters. */
10652
0
        if ((size_t) (end - cur) < strLen)
10653
0
            end = cur;
10654
0
        else
10655
0
            end -= strLen - 1;
10656
0
        index = end - ctxt->input->cur;
10657
0
        if (index > LONG_MAX) {
10658
0
            ctxt->checkIndex = 0;
10659
0
            return(ctxt->input->end - strLen);
10660
0
        }
10661
0
        ctxt->checkIndex = index;
10662
0
    } else {
10663
0
        ctxt->checkIndex = 0;
10664
0
    }
10665
10666
0
    return(term);
10667
0
}
10668
10669
/**
10670
 * Check whether the input buffer contains terminated char data.
10671
 *
10672
 * @param ctxt  an XML parser context
10673
 */
10674
static int
10675
0
xmlParseLookupCharData(xmlParserCtxtPtr ctxt) {
10676
0
    const xmlChar *cur = ctxt->input->cur + ctxt->checkIndex;
10677
0
    const xmlChar *end = ctxt->input->end;
10678
0
    size_t index;
10679
10680
0
    while (cur < end) {
10681
0
        if ((*cur == '<') || (*cur == '&')) {
10682
0
            ctxt->checkIndex = 0;
10683
0
            return(1);
10684
0
        }
10685
0
        cur++;
10686
0
    }
10687
10688
0
    index = cur - ctxt->input->cur;
10689
0
    if (index > LONG_MAX) {
10690
0
        ctxt->checkIndex = 0;
10691
0
        return(1);
10692
0
    }
10693
0
    ctxt->checkIndex = index;
10694
0
    return(0);
10695
0
}
10696
10697
/**
10698
 * Check whether there's enough data in the input buffer to finish parsing
10699
 * a start tag. This has to take quotes into account.
10700
 *
10701
 * @param ctxt  an XML parser context
10702
 */
10703
static int
10704
0
xmlParseLookupGt(xmlParserCtxtPtr ctxt) {
10705
0
    const xmlChar *cur;
10706
0
    const xmlChar *end = ctxt->input->end;
10707
0
    int state = ctxt->endCheckState;
10708
0
    size_t index;
10709
10710
0
    if (ctxt->checkIndex == 0)
10711
0
        cur = ctxt->input->cur + 1;
10712
0
    else
10713
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10714
10715
0
    while (cur < end) {
10716
0
        if (state) {
10717
0
            if (*cur == state)
10718
0
                state = 0;
10719
0
        } else if (*cur == '\'' || *cur == '"') {
10720
0
            state = *cur;
10721
0
        } else if (*cur == '>') {
10722
0
            ctxt->checkIndex = 0;
10723
0
            ctxt->endCheckState = 0;
10724
0
            return(1);
10725
0
        }
10726
0
        cur++;
10727
0
    }
10728
10729
0
    index = cur - ctxt->input->cur;
10730
0
    if (index > LONG_MAX) {
10731
0
        ctxt->checkIndex = 0;
10732
0
        ctxt->endCheckState = 0;
10733
0
        return(1);
10734
0
    }
10735
0
    ctxt->checkIndex = index;
10736
0
    ctxt->endCheckState = state;
10737
0
    return(0);
10738
0
}
10739
10740
/**
10741
 * Check whether there's enough data in the input buffer to finish parsing
10742
 * the internal subset.
10743
 *
10744
 * @param ctxt  an XML parser context
10745
 */
10746
static int
10747
0
xmlParseLookupInternalSubset(xmlParserCtxtPtr ctxt) {
10748
    /*
10749
     * Sorry, but progressive parsing of the internal subset is not
10750
     * supported. We first check that the full content of the internal
10751
     * subset is available and parsing is launched only at that point.
10752
     * Internal subset ends with "']' S? '>'" in an unescaped section and
10753
     * not in a ']]>' sequence which are conditional sections.
10754
     */
10755
0
    const xmlChar *cur, *start;
10756
0
    const xmlChar *end = ctxt->input->end;
10757
0
    int state = ctxt->endCheckState;
10758
0
    size_t index;
10759
10760
0
    if (ctxt->checkIndex == 0) {
10761
0
        cur = ctxt->input->cur + 1;
10762
0
    } else {
10763
0
        cur = ctxt->input->cur + ctxt->checkIndex;
10764
0
    }
10765
0
    start = cur;
10766
10767
0
    while (cur < end) {
10768
0
        if (state == '-') {
10769
0
            if ((*cur == '-') &&
10770
0
                (cur[1] == '-') &&
10771
0
                (cur[2] == '>')) {
10772
0
                state = 0;
10773
0
                cur += 3;
10774
0
                start = cur;
10775
0
                continue;
10776
0
            }
10777
0
        }
10778
0
        else if (state == ']') {
10779
0
            if (*cur == '>') {
10780
0
                ctxt->checkIndex = 0;
10781
0
                ctxt->endCheckState = 0;
10782
0
                return(1);
10783
0
            }
10784
0
            if (IS_BLANK_CH(*cur)) {
10785
0
                state = ' ';
10786
0
            } else if (*cur != ']') {
10787
0
                state = 0;
10788
0
                start = cur;
10789
0
                continue;
10790
0
            }
10791
0
        }
10792
0
        else if (state == ' ') {
10793
0
            if (*cur == '>') {
10794
0
                ctxt->checkIndex = 0;
10795
0
                ctxt->endCheckState = 0;
10796
0
                return(1);
10797
0
            }
10798
0
            if (!IS_BLANK_CH(*cur)) {
10799
0
                state = 0;
10800
0
                start = cur;
10801
0
                continue;
10802
0
            }
10803
0
        }
10804
0
        else if (state != 0) {
10805
0
            if (*cur == state) {
10806
0
                state = 0;
10807
0
                start = cur + 1;
10808
0
            }
10809
0
        }
10810
0
        else if (*cur == '<') {
10811
0
            if ((cur[1] == '!') &&
10812
0
                (cur[2] == '-') &&
10813
0
                (cur[3] == '-')) {
10814
0
                state = '-';
10815
0
                cur += 4;
10816
                /* Don't treat <!--> as comment */
10817
0
                start = cur;
10818
0
                continue;
10819
0
            }
10820
0
        }
10821
0
        else if ((*cur == '"') || (*cur == '\'') || (*cur == ']')) {
10822
0
            state = *cur;
10823
0
        }
10824
10825
0
        cur++;
10826
0
    }
10827
10828
    /*
10829
     * Rescan the three last characters to detect "<!--" and "-->"
10830
     * split across chunks.
10831
     */
10832
0
    if ((state == 0) || (state == '-')) {
10833
0
        if (cur - start < 3)
10834
0
            cur = start;
10835
0
        else
10836
0
            cur -= 3;
10837
0
    }
10838
0
    index = cur - ctxt->input->cur;
10839
0
    if (index > LONG_MAX) {
10840
0
        ctxt->checkIndex = 0;
10841
0
        ctxt->endCheckState = 0;
10842
0
        return(1);
10843
0
    }
10844
0
    ctxt->checkIndex = index;
10845
0
    ctxt->endCheckState = state;
10846
0
    return(0);
10847
0
}
10848
10849
/**
10850
 * Try to progress on parsing
10851
 *
10852
 * @param ctxt  an XML parser context
10853
 * @param terminate  last chunk indicator
10854
 * @returns zero if no parsing was possible
10855
 */
10856
static int
10857
0
xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
10858
0
    int ret = 0;
10859
0
    size_t avail;
10860
0
    xmlChar cur, next;
10861
10862
0
    if (ctxt->input == NULL)
10863
0
        return(0);
10864
10865
0
    if ((ctxt->input != NULL) &&
10866
0
        (ctxt->input->cur - ctxt->input->base > 4096)) {
10867
0
        xmlParserShrink(ctxt);
10868
0
    }
10869
10870
0
    while (ctxt->disableSAX == 0) {
10871
0
        avail = ctxt->input->end - ctxt->input->cur;
10872
0
        if (avail < 1)
10873
0
      goto done;
10874
0
        switch (ctxt->instate) {
10875
0
            case XML_PARSER_EOF:
10876
          /*
10877
     * Document parsing is done !
10878
     */
10879
0
          goto done;
10880
0
            case XML_PARSER_START:
10881
                /*
10882
                 * Very first chars read from the document flow.
10883
                 */
10884
0
                if ((!terminate) && (avail < 4))
10885
0
                    goto done;
10886
10887
                /*
10888
                 * We need more bytes to detect EBCDIC code pages.
10889
                 * See xmlDetectEBCDIC.
10890
                 */
10891
0
                if ((CMP4(CUR_PTR, 0x4C, 0x6F, 0xA7, 0x94)) &&
10892
0
                    (!terminate) && (avail < 200))
10893
0
                    goto done;
10894
10895
0
                xmlDetectEncoding(ctxt);
10896
0
                ctxt->instate = XML_PARSER_XML_DECL;
10897
0
    break;
10898
10899
0
            case XML_PARSER_XML_DECL:
10900
0
    if ((!terminate) && (avail < 2))
10901
0
        goto done;
10902
0
    cur = ctxt->input->cur[0];
10903
0
    next = ctxt->input->cur[1];
10904
0
          if ((cur == '<') && (next == '?')) {
10905
        /* PI or XML decl */
10906
0
        if ((!terminate) &&
10907
0
                        (!xmlParseLookupString(ctxt, 2, "?>", 2)))
10908
0
      goto done;
10909
0
        if ((ctxt->input->cur[2] == 'x') &&
10910
0
      (ctxt->input->cur[3] == 'm') &&
10911
0
      (ctxt->input->cur[4] == 'l') &&
10912
0
      (IS_BLANK_CH(ctxt->input->cur[5]))) {
10913
0
      ret += 5;
10914
0
      xmlParseXMLDecl(ctxt);
10915
0
        } else {
10916
0
      ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10917
0
                        if (ctxt->version == NULL) {
10918
0
                            xmlErrMemory(ctxt);
10919
0
                            break;
10920
0
                        }
10921
0
        }
10922
0
    } else {
10923
0
        ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
10924
0
        if (ctxt->version == NULL) {
10925
0
            xmlErrMemory(ctxt);
10926
0
      break;
10927
0
        }
10928
0
    }
10929
0
                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) {
10930
0
                    ctxt->sax->setDocumentLocator(ctxt->userData,
10931
0
                            (xmlSAXLocator *) &xmlDefaultSAXLocator);
10932
0
                }
10933
0
                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
10934
0
                    (!ctxt->disableSAX))
10935
0
                    ctxt->sax->startDocument(ctxt->userData);
10936
0
                ctxt->instate = XML_PARSER_MISC;
10937
0
    break;
10938
0
            case XML_PARSER_START_TAG: {
10939
0
          const xmlChar *name;
10940
0
    const xmlChar *prefix = NULL;
10941
0
    const xmlChar *URI = NULL;
10942
0
                int line = ctxt->input->line;
10943
0
    int nbNs = 0;
10944
10945
0
    if ((!terminate) && (avail < 2))
10946
0
        goto done;
10947
0
    cur = ctxt->input->cur[0];
10948
0
          if (cur != '<') {
10949
0
        xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
10950
0
                                   "Start tag expected, '<' not found");
10951
0
                    ctxt->instate = XML_PARSER_EOF;
10952
0
                    xmlFinishDocument(ctxt);
10953
0
        goto done;
10954
0
    }
10955
0
    if ((!terminate) && (!xmlParseLookupGt(ctxt)))
10956
0
                    goto done;
10957
0
    if (ctxt->spaceNr == 0)
10958
0
        spacePush(ctxt, -1);
10959
0
    else if (*ctxt->space == -2)
10960
0
        spacePush(ctxt, -1);
10961
0
    else
10962
0
        spacePush(ctxt, *ctxt->space);
10963
0
#ifdef LIBXML_SAX1_ENABLED
10964
0
    if (ctxt->sax2)
10965
0
#endif /* LIBXML_SAX1_ENABLED */
10966
0
        name = xmlParseStartTag2(ctxt, &prefix, &URI, &nbNs);
10967
0
#ifdef LIBXML_SAX1_ENABLED
10968
0
    else
10969
0
        name = xmlParseStartTag(ctxt);
10970
0
#endif /* LIBXML_SAX1_ENABLED */
10971
0
    if (name == NULL) {
10972
0
        spacePop(ctxt);
10973
0
                    ctxt->instate = XML_PARSER_EOF;
10974
0
                    xmlFinishDocument(ctxt);
10975
0
        goto done;
10976
0
    }
10977
0
#ifdef LIBXML_VALID_ENABLED
10978
    /*
10979
     * [ VC: Root Element Type ]
10980
     * The Name in the document type declaration must match
10981
     * the element type of the root element.
10982
     */
10983
0
    if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
10984
0
        ctxt->node && (ctxt->node == ctxt->myDoc->children))
10985
0
        ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
10986
0
#endif /* LIBXML_VALID_ENABLED */
10987
10988
    /*
10989
     * Check for an Empty Element.
10990
     */
10991
0
    if ((RAW == '/') && (NXT(1) == '>')) {
10992
0
        SKIP(2);
10993
10994
0
        if (ctxt->sax2) {
10995
0
      if ((ctxt->sax != NULL) &&
10996
0
          (ctxt->sax->endElementNs != NULL) &&
10997
0
          (!ctxt->disableSAX))
10998
0
          ctxt->sax->endElementNs(ctxt->userData, name,
10999
0
                                  prefix, URI);
11000
0
      if (nbNs > 0)
11001
0
          xmlParserNsPop(ctxt, nbNs);
11002
0
#ifdef LIBXML_SAX1_ENABLED
11003
0
        } else {
11004
0
      if ((ctxt->sax != NULL) &&
11005
0
          (ctxt->sax->endElement != NULL) &&
11006
0
          (!ctxt->disableSAX))
11007
0
          ctxt->sax->endElement(ctxt->userData, name);
11008
0
#endif /* LIBXML_SAX1_ENABLED */
11009
0
        }
11010
0
        spacePop(ctxt);
11011
0
    } else if (RAW == '>') {
11012
0
        NEXT;
11013
0
                    nameNsPush(ctxt, name, prefix, URI, line, nbNs);
11014
0
    } else {
11015
0
        xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
11016
0
           "Couldn't find end of Start Tag %s\n",
11017
0
           name);
11018
0
        nodePop(ctxt);
11019
0
        spacePop(ctxt);
11020
0
                    if (nbNs > 0)
11021
0
                        xmlParserNsPop(ctxt, nbNs);
11022
0
    }
11023
11024
0
                if (ctxt->nameNr == 0)
11025
0
                    ctxt->instate = XML_PARSER_EPILOG;
11026
0
                else
11027
0
                    ctxt->instate = XML_PARSER_CONTENT;
11028
0
                break;
11029
0
      }
11030
0
            case XML_PARSER_CONTENT: {
11031
0
    cur = ctxt->input->cur[0];
11032
11033
0
    if (cur == '<') {
11034
0
                    if ((!terminate) && (avail < 2))
11035
0
                        goto done;
11036
0
        next = ctxt->input->cur[1];
11037
11038
0
                    if (next == '/') {
11039
0
                        ctxt->instate = XML_PARSER_END_TAG;
11040
0
                        break;
11041
0
                    } else if (next == '?') {
11042
0
                        if ((!terminate) &&
11043
0
                            (!xmlParseLookupString(ctxt, 2, "?>", 2)))
11044
0
                            goto done;
11045
0
                        xmlParsePI(ctxt);
11046
0
                        ctxt->instate = XML_PARSER_CONTENT;
11047
0
                        break;
11048
0
                    } else if (next == '!') {
11049
0
                        if ((!terminate) && (avail < 3))
11050
0
                            goto done;
11051
0
                        next = ctxt->input->cur[2];
11052
11053
0
                        if (next == '-') {
11054
0
                            if ((!terminate) && (avail < 4))
11055
0
                                goto done;
11056
0
                            if (ctxt->input->cur[3] == '-') {
11057
0
                                if ((!terminate) &&
11058
0
                                    (!xmlParseLookupString(ctxt, 4, "-->", 3)))
11059
0
                                    goto done;
11060
0
                                xmlParseComment(ctxt);
11061
0
                                ctxt->instate = XML_PARSER_CONTENT;
11062
0
                                break;
11063
0
                            }
11064
0
                        } else if (next == '[') {
11065
0
                            if ((!terminate) && (avail < 9))
11066
0
                                goto done;
11067
0
                            if ((ctxt->input->cur[2] == '[') &&
11068
0
                                (ctxt->input->cur[3] == 'C') &&
11069
0
                                (ctxt->input->cur[4] == 'D') &&
11070
0
                                (ctxt->input->cur[5] == 'A') &&
11071
0
                                (ctxt->input->cur[6] == 'T') &&
11072
0
                                (ctxt->input->cur[7] == 'A') &&
11073
0
                                (ctxt->input->cur[8] == '[')) {
11074
0
                                if ((!terminate) &&
11075
0
                                    (!xmlParseLookupString(ctxt, 9, "]]>", 3)))
11076
0
                                    goto done;
11077
0
                                ctxt->instate = XML_PARSER_CDATA_SECTION;
11078
0
                                xmlParseCDSect(ctxt);
11079
0
                                ctxt->instate = XML_PARSER_CONTENT;
11080
0
                                break;
11081
0
                            }
11082
0
                        }
11083
0
                    }
11084
0
    } else if (cur == '&') {
11085
0
        if ((!terminate) && (!xmlParseLookupChar(ctxt, ';')))
11086
0
      goto done;
11087
0
        xmlParseReference(ctxt);
11088
0
                    break;
11089
0
    } else {
11090
        /* TODO Avoid the extra copy, handle directly !!! */
11091
        /*
11092
         * Goal of the following test is:
11093
         *  - minimize calls to the SAX 'character' callback
11094
         *    when they are mergeable
11095
         *  - handle an problem for isBlank when we only parse
11096
         *    a sequence of blank chars and the next one is
11097
         *    not available to check against '<' presence.
11098
         *  - tries to homogenize the differences in SAX
11099
         *    callbacks between the push and pull versions
11100
         *    of the parser.
11101
         */
11102
0
        if (avail < XML_PARSER_BIG_BUFFER_SIZE) {
11103
0
      if ((!terminate) && (!xmlParseLookupCharData(ctxt)))
11104
0
          goto done;
11105
0
                    }
11106
0
                    ctxt->checkIndex = 0;
11107
0
        xmlParseCharDataInternal(ctxt, !terminate);
11108
0
                    break;
11109
0
    }
11110
11111
0
                ctxt->instate = XML_PARSER_START_TAG;
11112
0
    break;
11113
0
      }
11114
0
            case XML_PARSER_END_TAG:
11115
0
    if ((!terminate) && (!xmlParseLookupChar(ctxt, '>')))
11116
0
        goto done;
11117
0
    if (ctxt->sax2) {
11118
0
              xmlParseEndTag2(ctxt, &ctxt->pushTab[ctxt->nameNr - 1]);
11119
0
        nameNsPop(ctxt);
11120
0
    }
11121
0
#ifdef LIBXML_SAX1_ENABLED
11122
0
      else
11123
0
        xmlParseEndTag1(ctxt, 0);
11124
0
#endif /* LIBXML_SAX1_ENABLED */
11125
0
    if (ctxt->nameNr == 0) {
11126
0
        ctxt->instate = XML_PARSER_EPILOG;
11127
0
    } else {
11128
0
        ctxt->instate = XML_PARSER_CONTENT;
11129
0
    }
11130
0
    break;
11131
0
            case XML_PARSER_MISC:
11132
0
            case XML_PARSER_PROLOG:
11133
0
            case XML_PARSER_EPILOG:
11134
0
    SKIP_BLANKS;
11135
0
                avail = ctxt->input->end - ctxt->input->cur;
11136
0
    if (avail < 1)
11137
0
        goto done;
11138
0
    if (ctxt->input->cur[0] == '<') {
11139
0
                    if ((!terminate) && (avail < 2))
11140
0
                        goto done;
11141
0
                    next = ctxt->input->cur[1];
11142
0
                    if (next == '?') {
11143
0
                        if ((!terminate) &&
11144
0
                            (!xmlParseLookupString(ctxt, 2, "?>", 2)))
11145
0
                            goto done;
11146
0
                        xmlParsePI(ctxt);
11147
0
                        break;
11148
0
                    } else if (next == '!') {
11149
0
                        if ((!terminate) && (avail < 3))
11150
0
                            goto done;
11151
11152
0
                        if (ctxt->input->cur[2] == '-') {
11153
0
                            if ((!terminate) && (avail < 4))
11154
0
                                goto done;
11155
0
                            if (ctxt->input->cur[3] == '-') {
11156
0
                                if ((!terminate) &&
11157
0
                                    (!xmlParseLookupString(ctxt, 4, "-->", 3)))
11158
0
                                    goto done;
11159
0
                                xmlParseComment(ctxt);
11160
0
                                break;
11161
0
                            }
11162
0
                        } else if (ctxt->instate == XML_PARSER_MISC) {
11163
0
                            if ((!terminate) && (avail < 9))
11164
0
                                goto done;
11165
0
                            if ((ctxt->input->cur[2] == 'D') &&
11166
0
                                (ctxt->input->cur[3] == 'O') &&
11167
0
                                (ctxt->input->cur[4] == 'C') &&
11168
0
                                (ctxt->input->cur[5] == 'T') &&
11169
0
                                (ctxt->input->cur[6] == 'Y') &&
11170
0
                                (ctxt->input->cur[7] == 'P') &&
11171
0
                                (ctxt->input->cur[8] == 'E')) {
11172
0
                                if ((!terminate) && (!xmlParseLookupGt(ctxt)))
11173
0
                                    goto done;
11174
0
                                ctxt->inSubset = 1;
11175
0
                                xmlParseDocTypeDecl(ctxt);
11176
0
                                if (RAW == '[') {
11177
0
                                    ctxt->instate = XML_PARSER_DTD;
11178
0
                                } else {
11179
0
                                    if (RAW == '>')
11180
0
                                        NEXT;
11181
                                    /*
11182
                                     * Create and update the external subset.
11183
                                     */
11184
0
                                    ctxt->inSubset = 2;
11185
0
                                    if ((ctxt->sax != NULL) &&
11186
0
                                        (!ctxt->disableSAX) &&
11187
0
                                        (ctxt->sax->externalSubset != NULL))
11188
0
                                        ctxt->sax->externalSubset(
11189
0
                                                ctxt->userData,
11190
0
                                                ctxt->intSubName,
11191
0
                                                ctxt->extSubSystem,
11192
0
                                                ctxt->extSubURI);
11193
0
                                    ctxt->inSubset = 0;
11194
0
                                    xmlCleanSpecialAttr(ctxt);
11195
0
                                    ctxt->instate = XML_PARSER_PROLOG;
11196
0
                                }
11197
0
                                break;
11198
0
                            }
11199
0
                        }
11200
0
                    }
11201
0
                }
11202
11203
0
                if (ctxt->instate == XML_PARSER_EPILOG) {
11204
0
                    if (ctxt->errNo == XML_ERR_OK)
11205
0
                        xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
11206
0
        ctxt->instate = XML_PARSER_EOF;
11207
0
                    xmlFinishDocument(ctxt);
11208
0
                } else {
11209
0
        ctxt->instate = XML_PARSER_START_TAG;
11210
0
    }
11211
0
    break;
11212
0
            case XML_PARSER_DTD: {
11213
0
                if ((!terminate) && (!xmlParseLookupInternalSubset(ctxt)))
11214
0
                    goto done;
11215
0
    xmlParseInternalSubset(ctxt);
11216
0
    ctxt->inSubset = 2;
11217
0
    if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
11218
0
        (ctxt->sax->externalSubset != NULL))
11219
0
        ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
11220
0
          ctxt->extSubSystem, ctxt->extSubURI);
11221
0
    ctxt->inSubset = 0;
11222
0
    xmlCleanSpecialAttr(ctxt);
11223
0
    ctxt->instate = XML_PARSER_PROLOG;
11224
0
                break;
11225
0
      }
11226
0
            default:
11227
0
                xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
11228
0
      "PP: internal error\n");
11229
0
    ctxt->instate = XML_PARSER_EOF;
11230
0
    break;
11231
0
  }
11232
0
    }
11233
0
done:
11234
0
    return(ret);
11235
0
}
11236
11237
/**
11238
 * Parse a chunk of memory in push parser mode.
11239
 *
11240
 * Assumes that the parser context was initialized with
11241
 * #xmlCreatePushParserCtxt.
11242
 *
11243
 * The last chunk, which will often be empty, must be marked with
11244
 * the `terminate` flag. With the default SAX callbacks, the resulting
11245
 * document will be available in ctxt->myDoc. This pointer will not
11246
 * be freed when calling #xmlFreeParserCtxt and must be freed by the
11247
 * caller. If the document isn't well-formed, it will still be returned
11248
 * in ctxt->myDoc.
11249
 *
11250
 * As an exception, #xmlCtxtResetPush will free the document in
11251
 * ctxt->myDoc. So ctxt->myDoc should be set to NULL after extracting
11252
 * the document.
11253
 *
11254
 * Since 2.14.0, #xmlCtxtGetDocument can be used to retrieve the
11255
 * result document.
11256
 *
11257
 * @param ctxt  an XML parser context
11258
 * @param chunk  chunk of memory
11259
 * @param size  size of chunk in bytes
11260
 * @param terminate  last chunk indicator
11261
 * @returns an xmlParserErrors code (0 on success).
11262
 */
11263
int
11264
xmlParseChunk(xmlParserCtxt *ctxt, const char *chunk, int size,
11265
0
              int terminate) {
11266
0
    size_t curBase;
11267
0
    size_t maxLength;
11268
0
    size_t pos;
11269
0
    int end_in_lf = 0;
11270
0
    int res;
11271
11272
0
    if ((ctxt == NULL) || (size < 0))
11273
0
        return(XML_ERR_ARGUMENT);
11274
0
    if ((chunk == NULL) && (size > 0))
11275
0
        return(XML_ERR_ARGUMENT);
11276
0
    if ((ctxt->input == NULL) || (ctxt->input->buf == NULL))
11277
0
        return(XML_ERR_ARGUMENT);
11278
0
    if (ctxt->disableSAX != 0)
11279
0
        return(ctxt->errNo);
11280
11281
0
    ctxt->input->flags |= XML_INPUT_PROGRESSIVE;
11282
0
    if (ctxt->instate == XML_PARSER_START)
11283
0
        xmlCtxtInitializeLate(ctxt);
11284
0
    if ((size > 0) && (chunk != NULL) && (!terminate) &&
11285
0
        (chunk[size - 1] == '\r')) {
11286
0
  end_in_lf = 1;
11287
0
  size--;
11288
0
    }
11289
11290
    /*
11291
     * Also push an empty chunk to make sure that the raw buffer
11292
     * will be flushed if there is an encoder.
11293
     */
11294
0
    pos = ctxt->input->cur - ctxt->input->base;
11295
0
    res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
11296
0
    xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
11297
0
    if (res < 0) {
11298
0
        xmlCtxtErrIO(ctxt, ctxt->input->buf->error, NULL);
11299
0
        xmlHaltParser(ctxt);
11300
0
        return(ctxt->errNo);
11301
0
    }
11302
11303
0
    xmlParseTryOrFinish(ctxt, terminate);
11304
11305
0
    curBase = ctxt->input->cur - ctxt->input->base;
11306
0
    maxLength = (ctxt->options & XML_PARSE_HUGE) ?
11307
0
                XML_MAX_HUGE_LENGTH :
11308
0
                XML_MAX_LOOKUP_LIMIT;
11309
0
    if (curBase > maxLength) {
11310
0
        xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
11311
0
                    "Buffer size limit exceeded, try XML_PARSE_HUGE\n");
11312
0
        xmlHaltParser(ctxt);
11313
0
    }
11314
11315
0
    if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX != 0))
11316
0
        return(ctxt->errNo);
11317
11318
0
    if (end_in_lf == 1) {
11319
0
  pos = ctxt->input->cur - ctxt->input->base;
11320
0
  res = xmlParserInputBufferPush(ctxt->input->buf, 1, "\r");
11321
0
  xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
11322
0
        if (res < 0) {
11323
0
            xmlCtxtErrIO(ctxt, ctxt->input->buf->error, NULL);
11324
0
            xmlHaltParser(ctxt);
11325
0
            return(ctxt->errNo);
11326
0
        }
11327
0
    }
11328
0
    if (terminate) {
11329
  /*
11330
   * Check for termination
11331
   */
11332
0
        if ((ctxt->instate != XML_PARSER_EOF) &&
11333
0
            (ctxt->instate != XML_PARSER_EPILOG)) {
11334
0
            if (ctxt->nameNr > 0) {
11335
0
                const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1];
11336
0
                int line = ctxt->pushTab[ctxt->nameNr - 1].line;
11337
0
                xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
11338
0
                        "Premature end of data in tag %s line %d\n",
11339
0
                        name, line, NULL);
11340
0
            } else if (ctxt->instate == XML_PARSER_START) {
11341
0
                xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
11342
0
            } else {
11343
0
                xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
11344
0
                               "Start tag expected, '<' not found\n");
11345
0
            }
11346
0
        } else {
11347
0
            xmlParserCheckEOF(ctxt, XML_ERR_DOCUMENT_END);
11348
0
        }
11349
0
  if (ctxt->instate != XML_PARSER_EOF) {
11350
0
            ctxt->instate = XML_PARSER_EOF;
11351
0
            xmlFinishDocument(ctxt);
11352
0
  }
11353
0
    }
11354
0
    if (ctxt->wellFormed == 0)
11355
0
  return((xmlParserErrors) ctxt->errNo);
11356
0
    else
11357
0
        return(0);
11358
0
}
11359
11360
/************************************************************************
11361
 *                  *
11362
 *    I/O front end functions to the parser     *
11363
 *                  *
11364
 ************************************************************************/
11365
11366
/**
11367
 * Create a parser context for using the XML parser in push mode.
11368
 * See #xmlParseChunk.
11369
 *
11370
 * Passing an initial chunk is useless and deprecated.
11371
 *
11372
 * The push parser doesn't support recovery mode or the
11373
 * XML_PARSE_NOBLANKS option.
11374
 *
11375
 * `filename` is used as base URI to fetch external entities and for
11376
 * error reports.
11377
 *
11378
 * @param sax  a SAX handler (optional)
11379
 * @param user_data  user data for SAX callbacks (optional)
11380
 * @param chunk  initial chunk (optional, deprecated)
11381
 * @param size  size of initial chunk in bytes
11382
 * @param filename  file name or URI (optional)
11383
 * @returns the new parser context or NULL if a memory allocation
11384
 * failed.
11385
 */
11386
11387
xmlParserCtxt *
11388
xmlCreatePushParserCtxt(xmlSAXHandler *sax, void *user_data,
11389
0
                        const char *chunk, int size, const char *filename) {
11390
0
    xmlParserCtxtPtr ctxt;
11391
0
    xmlParserInputPtr input;
11392
11393
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11394
0
    if (ctxt == NULL)
11395
0
  return(NULL);
11396
11397
0
    ctxt->options &= ~XML_PARSE_NODICT;
11398
0
    ctxt->dictNames = 1;
11399
11400
0
    input = xmlNewPushInput(filename, chunk, size);
11401
0
    if (input == NULL) {
11402
0
  xmlFreeParserCtxt(ctxt);
11403
0
  return(NULL);
11404
0
    }
11405
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11406
0
        xmlFreeInputStream(input);
11407
0
        xmlFreeParserCtxt(ctxt);
11408
0
        return(NULL);
11409
0
    }
11410
11411
0
    return(ctxt);
11412
0
}
11413
#endif /* LIBXML_PUSH_ENABLED */
11414
11415
/**
11416
 * Blocks further parser processing
11417
 *
11418
 * @param ctxt  an XML parser context
11419
 */
11420
void
11421
0
xmlStopParser(xmlParserCtxt *ctxt) {
11422
0
    if (ctxt == NULL)
11423
0
        return;
11424
0
    xmlHaltParser(ctxt);
11425
    /*
11426
     * TODO: Update ctxt->lastError and ctxt->wellFormed?
11427
     */
11428
0
    if (ctxt->errNo != XML_ERR_NO_MEMORY)
11429
0
        ctxt->errNo = XML_ERR_USER_STOP;
11430
0
}
11431
11432
/**
11433
 * Create a parser context for using the XML parser with an existing
11434
 * I/O stream
11435
 *
11436
 * @param sax  a SAX handler (optional)
11437
 * @param user_data  user data for SAX callbacks (optional)
11438
 * @param ioread  an I/O read function
11439
 * @param ioclose  an I/O close function (optional)
11440
 * @param ioctx  an I/O handler
11441
 * @param enc  the charset encoding if known (deprecated)
11442
 * @returns the new parser context or NULL
11443
 */
11444
xmlParserCtxt *
11445
xmlCreateIOParserCtxt(xmlSAXHandler *sax, void *user_data,
11446
                      xmlInputReadCallback ioread,
11447
                      xmlInputCloseCallback ioclose,
11448
0
                      void *ioctx, xmlCharEncoding enc) {
11449
0
    xmlParserCtxtPtr ctxt;
11450
0
    xmlParserInputPtr input;
11451
0
    const char *encoding;
11452
11453
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11454
0
    if (ctxt == NULL)
11455
0
  return(NULL);
11456
11457
0
    encoding = xmlGetCharEncodingName(enc);
11458
0
    input = xmlCtxtNewInputFromIO(ctxt, NULL, ioread, ioclose, ioctx,
11459
0
                                  encoding, 0);
11460
0
    if (input == NULL) {
11461
0
  xmlFreeParserCtxt(ctxt);
11462
0
        return (NULL);
11463
0
    }
11464
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11465
0
        xmlFreeInputStream(input);
11466
0
        xmlFreeParserCtxt(ctxt);
11467
0
        return(NULL);
11468
0
    }
11469
11470
0
    return(ctxt);
11471
0
}
11472
11473
#ifdef LIBXML_VALID_ENABLED
11474
/************************************************************************
11475
 *                  *
11476
 *    Front ends when parsing a DTD       *
11477
 *                  *
11478
 ************************************************************************/
11479
11480
/**
11481
 * Parse a DTD.
11482
 *
11483
 * Option XML_PARSE_DTDLOAD should be enabled in the parser context
11484
 * to make external entities work.
11485
 *
11486
 * @since 2.14.0
11487
 *
11488
 * @param ctxt  a parser context
11489
 * @param input  a parser input
11490
 * @param publicId  public ID of the DTD (optional)
11491
 * @param systemId  system ID of the DTD (optional)
11492
 * @returns the resulting xmlDtd or NULL in case of error.
11493
 * `input` will be freed by the function in any case.
11494
 */
11495
xmlDtd *
11496
xmlCtxtParseDtd(xmlParserCtxt *ctxt, xmlParserInput *input,
11497
0
                const xmlChar *publicId, const xmlChar *systemId) {
11498
0
    xmlDtdPtr ret = NULL;
11499
11500
0
    if ((ctxt == NULL) || (input == NULL)) {
11501
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
11502
0
        xmlFreeInputStream(input);
11503
0
        return(NULL);
11504
0
    }
11505
11506
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
11507
0
        xmlFreeInputStream(input);
11508
0
        return(NULL);
11509
0
    }
11510
11511
0
    if (publicId == NULL)
11512
0
        publicId = BAD_CAST "none";
11513
0
    if (systemId == NULL)
11514
0
        systemId = BAD_CAST "none";
11515
11516
0
    ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
11517
0
    if (ctxt->myDoc == NULL) {
11518
0
        xmlErrMemory(ctxt);
11519
0
        goto error;
11520
0
    }
11521
0
    ctxt->myDoc->properties = XML_DOC_INTERNAL;
11522
0
    ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
11523
0
                                       publicId, systemId);
11524
0
    if (ctxt->myDoc->extSubset == NULL) {
11525
0
        xmlErrMemory(ctxt);
11526
0
        xmlFreeDoc(ctxt->myDoc);
11527
0
        goto error;
11528
0
    }
11529
11530
0
    xmlParseExternalSubset(ctxt, publicId, systemId);
11531
11532
0
    if (ctxt->wellFormed) {
11533
0
        ret = ctxt->myDoc->extSubset;
11534
0
        ctxt->myDoc->extSubset = NULL;
11535
0
        if (ret != NULL) {
11536
0
            xmlNodePtr tmp;
11537
11538
0
            ret->doc = NULL;
11539
0
            tmp = ret->children;
11540
0
            while (tmp != NULL) {
11541
0
                tmp->doc = NULL;
11542
0
                tmp = tmp->next;
11543
0
            }
11544
0
        }
11545
0
    } else {
11546
0
        ret = NULL;
11547
0
    }
11548
0
    xmlFreeDoc(ctxt->myDoc);
11549
0
    ctxt->myDoc = NULL;
11550
11551
0
error:
11552
0
    xmlFreeInputStream(xmlCtxtPopInput(ctxt));
11553
11554
0
    return(ret);
11555
0
}
11556
11557
/**
11558
 * Load and parse a DTD
11559
 *
11560
 * @deprecated Use #xmlCtxtParseDtd.
11561
 *
11562
 * @param sax  the SAX handler block or NULL
11563
 * @param input  an Input Buffer
11564
 * @param enc  the charset encoding if known
11565
 * @returns the resulting xmlDtd or NULL in case of error.
11566
 * `input` will be freed by the function in any case.
11567
 */
11568
11569
xmlDtd *
11570
xmlIOParseDTD(xmlSAXHandler *sax, xmlParserInputBuffer *input,
11571
0
        xmlCharEncoding enc) {
11572
0
    xmlDtdPtr ret = NULL;
11573
0
    xmlParserCtxtPtr ctxt;
11574
0
    xmlParserInputPtr pinput = NULL;
11575
11576
0
    if (input == NULL)
11577
0
  return(NULL);
11578
11579
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
11580
0
    if (ctxt == NULL) {
11581
0
        xmlFreeParserInputBuffer(input);
11582
0
  return(NULL);
11583
0
    }
11584
0
    xmlCtxtSetOptions(ctxt, XML_PARSE_DTDLOAD);
11585
11586
    /*
11587
     * generate a parser input from the I/O handler
11588
     */
11589
11590
0
    pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
11591
0
    if (pinput == NULL) {
11592
0
        xmlFreeParserInputBuffer(input);
11593
0
  xmlFreeParserCtxt(ctxt);
11594
0
  return(NULL);
11595
0
    }
11596
11597
0
    if (enc != XML_CHAR_ENCODING_NONE) {
11598
0
        xmlSwitchEncoding(ctxt, enc);
11599
0
    }
11600
11601
0
    ret = xmlCtxtParseDtd(ctxt, pinput, NULL, NULL);
11602
11603
0
    xmlFreeParserCtxt(ctxt);
11604
0
    return(ret);
11605
0
}
11606
11607
/**
11608
 * Load and parse an external subset.
11609
 *
11610
 * @deprecated Use #xmlCtxtParseDtd.
11611
 *
11612
 * @param sax  the SAX handler block
11613
 * @param publicId  public identifier of the DTD (optional)
11614
 * @param systemId  system identifier (URL) of the DTD
11615
 * @returns the resulting xmlDtd or NULL in case of error.
11616
 */
11617
11618
xmlDtd *
11619
xmlSAXParseDTD(xmlSAXHandler *sax, const xmlChar *publicId,
11620
0
               const xmlChar *systemId) {
11621
0
    xmlDtdPtr ret = NULL;
11622
0
    xmlParserCtxtPtr ctxt;
11623
0
    xmlParserInputPtr input = NULL;
11624
0
    xmlChar* systemIdCanonic;
11625
11626
0
    if ((publicId == NULL) && (systemId == NULL)) return(NULL);
11627
11628
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
11629
0
    if (ctxt == NULL) {
11630
0
  return(NULL);
11631
0
    }
11632
0
    xmlCtxtSetOptions(ctxt, XML_PARSE_DTDLOAD);
11633
11634
    /*
11635
     * Canonicalise the system ID
11636
     */
11637
0
    systemIdCanonic = xmlCanonicPath(systemId);
11638
0
    if ((systemId != NULL) && (systemIdCanonic == NULL)) {
11639
0
  xmlFreeParserCtxt(ctxt);
11640
0
  return(NULL);
11641
0
    }
11642
11643
    /*
11644
     * Ask the Entity resolver to load the damn thing
11645
     */
11646
11647
0
    if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
11648
0
  input = ctxt->sax->resolveEntity(ctxt->userData, publicId,
11649
0
                                   systemIdCanonic);
11650
0
    if (input == NULL) {
11651
0
  xmlFreeParserCtxt(ctxt);
11652
0
  if (systemIdCanonic != NULL)
11653
0
      xmlFree(systemIdCanonic);
11654
0
  return(NULL);
11655
0
    }
11656
11657
0
    if (input->filename == NULL)
11658
0
  input->filename = (char *) systemIdCanonic;
11659
0
    else
11660
0
  xmlFree(systemIdCanonic);
11661
11662
0
    ret = xmlCtxtParseDtd(ctxt, input, publicId, systemId);
11663
11664
0
    xmlFreeParserCtxt(ctxt);
11665
0
    return(ret);
11666
0
}
11667
11668
11669
/**
11670
 * Load and parse an external subset.
11671
 *
11672
 * @param publicId  public identifier of the DTD (optional)
11673
 * @param systemId  system identifier (URL) of the DTD
11674
 * @returns the resulting xmlDtd or NULL in case of error.
11675
 */
11676
11677
xmlDtd *
11678
0
xmlParseDTD(const xmlChar *publicId, const xmlChar *systemId) {
11679
0
    return(xmlSAXParseDTD(NULL, publicId, systemId));
11680
0
}
11681
#endif /* LIBXML_VALID_ENABLED */
11682
11683
/************************************************************************
11684
 *                  *
11685
 *    Front ends when parsing an Entity     *
11686
 *                  *
11687
 ************************************************************************/
11688
11689
static xmlNodePtr
11690
xmlCtxtParseContentInternal(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
11691
0
                            int hasTextDecl, int buildTree) {
11692
0
    xmlNodePtr root = NULL;
11693
0
    xmlNodePtr list = NULL;
11694
0
    xmlChar *rootName = BAD_CAST "#root";
11695
0
    int result;
11696
11697
0
    if (buildTree) {
11698
0
        root = xmlNewDocNode(ctxt->myDoc, NULL, rootName, NULL);
11699
0
        if (root == NULL) {
11700
0
            xmlErrMemory(ctxt);
11701
0
            goto error;
11702
0
        }
11703
0
    }
11704
11705
0
    if (xmlCtxtPushInput(ctxt, input) < 0)
11706
0
        goto error;
11707
11708
0
    nameNsPush(ctxt, rootName, NULL, NULL, 0, 0);
11709
0
    spacePush(ctxt, -1);
11710
11711
0
    if (buildTree)
11712
0
        nodePush(ctxt, root);
11713
11714
0
    if (hasTextDecl) {
11715
0
        xmlDetectEncoding(ctxt);
11716
11717
        /*
11718
         * Parse a possible text declaration first
11719
         */
11720
0
        if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
11721
0
            (IS_BLANK_CH(NXT(5)))) {
11722
0
            xmlParseTextDecl(ctxt);
11723
            /*
11724
             * An XML-1.0 document can't reference an entity not XML-1.0
11725
             */
11726
0
            if ((xmlStrEqual(ctxt->version, BAD_CAST "1.0")) &&
11727
0
                (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) {
11728
0
                xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH,
11729
0
                               "Version mismatch between document and "
11730
0
                               "entity\n");
11731
0
            }
11732
0
        }
11733
0
    }
11734
11735
0
    xmlParseContentInternal(ctxt);
11736
11737
0
    if (ctxt->input->cur < ctxt->input->end)
11738
0
  xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11739
11740
0
    if ((ctxt->wellFormed) ||
11741
0
        ((ctxt->recovery) && (!xmlCtxtIsCatastrophicError(ctxt)))) {
11742
0
        if (root != NULL) {
11743
0
            xmlNodePtr cur;
11744
11745
            /*
11746
             * Unlink newly created node list.
11747
             */
11748
0
            list = root->children;
11749
0
            root->children = NULL;
11750
0
            root->last = NULL;
11751
0
            for (cur = list; cur != NULL; cur = cur->next)
11752
0
                cur->parent = NULL;
11753
0
        }
11754
0
    }
11755
11756
    /*
11757
     * Read the rest of the stream in case of errors. We want
11758
     * to account for the whole entity size.
11759
     */
11760
0
    do {
11761
0
        ctxt->input->cur = ctxt->input->end;
11762
0
        xmlParserShrink(ctxt);
11763
0
        result = xmlParserGrow(ctxt);
11764
0
    } while (result > 0);
11765
11766
0
    if (buildTree)
11767
0
        nodePop(ctxt);
11768
11769
0
    namePop(ctxt);
11770
0
    spacePop(ctxt);
11771
11772
0
    xmlCtxtPopInput(ctxt);
11773
11774
0
error:
11775
0
    xmlFreeNode(root);
11776
11777
0
    return(list);
11778
0
}
11779
11780
static void
11781
0
xmlCtxtParseEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr ent) {
11782
0
    xmlParserInputPtr input;
11783
0
    xmlNodePtr list;
11784
0
    unsigned long consumed;
11785
0
    int isExternal;
11786
0
    int buildTree;
11787
0
    int oldMinNsIndex;
11788
0
    int oldNodelen, oldNodemem;
11789
11790
0
    isExternal = (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY);
11791
0
    buildTree = (ctxt->node != NULL);
11792
11793
    /*
11794
     * Recursion check
11795
     */
11796
0
    if (ent->flags & XML_ENT_EXPANDING) {
11797
0
        xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
11798
0
        xmlHaltParser(ctxt);
11799
0
        goto error;
11800
0
    }
11801
11802
    /*
11803
     * Load entity
11804
     */
11805
0
    input = xmlNewEntityInputStream(ctxt, ent);
11806
0
    if (input == NULL)
11807
0
        goto error;
11808
11809
    /*
11810
     * When building a tree, we need to limit the scope of namespace
11811
     * declarations, so that entities don't reference xmlNs structs
11812
     * from the parent of a reference.
11813
     */
11814
0
    oldMinNsIndex = ctxt->nsdb->minNsIndex;
11815
0
    if (buildTree)
11816
0
        ctxt->nsdb->minNsIndex = ctxt->nsNr;
11817
11818
0
    oldNodelen = ctxt->nodelen;
11819
0
    oldNodemem = ctxt->nodemem;
11820
0
    ctxt->nodelen = 0;
11821
0
    ctxt->nodemem = 0;
11822
11823
    /*
11824
     * Parse content
11825
     *
11826
     * This initiates a recursive call chain:
11827
     *
11828
     * - xmlCtxtParseContentInternal
11829
     * - xmlParseContentInternal
11830
     * - xmlParseReference
11831
     * - xmlCtxtParseEntity
11832
     *
11833
     * The nesting depth is limited by the maximum number of inputs,
11834
     * see xmlCtxtPushInput.
11835
     *
11836
     * It's possible to make this non-recursive (minNsIndex must be
11837
     * stored in the input struct) at the expense of code readability.
11838
     */
11839
11840
0
    ent->flags |= XML_ENT_EXPANDING;
11841
11842
0
    list = xmlCtxtParseContentInternal(ctxt, input, isExternal, buildTree);
11843
11844
0
    ent->flags &= ~XML_ENT_EXPANDING;
11845
11846
0
    ctxt->nsdb->minNsIndex = oldMinNsIndex;
11847
0
    ctxt->nodelen = oldNodelen;
11848
0
    ctxt->nodemem = oldNodemem;
11849
11850
    /*
11851
     * Entity size accounting
11852
     */
11853
0
    consumed = input->consumed;
11854
0
    xmlSaturatedAddSizeT(&consumed, input->end - input->base);
11855
11856
0
    if ((ent->flags & XML_ENT_CHECKED) == 0)
11857
0
        xmlSaturatedAdd(&ent->expandedSize, consumed);
11858
11859
0
    if ((ent->flags & XML_ENT_PARSED) == 0) {
11860
0
        if (isExternal)
11861
0
            xmlSaturatedAdd(&ctxt->sizeentities, consumed);
11862
11863
0
        ent->children = list;
11864
11865
0
        while (list != NULL) {
11866
0
            list->parent = (xmlNodePtr) ent;
11867
11868
            /*
11869
             * Downstream code like the nginx xslt module can set
11870
             * ctxt->myDoc->extSubset to a separate DTD, so the entity
11871
             * might have a different or a NULL document.
11872
             */
11873
0
            if (list->doc != ent->doc)
11874
0
                xmlSetTreeDoc(list, ent->doc);
11875
11876
0
            if (list->next == NULL)
11877
0
                ent->last = list;
11878
0
            list = list->next;
11879
0
        }
11880
0
    } else {
11881
0
        xmlFreeNodeList(list);
11882
0
    }
11883
11884
0
    xmlFreeInputStream(input);
11885
11886
0
error:
11887
0
    ent->flags |= XML_ENT_PARSED | XML_ENT_CHECKED;
11888
0
}
11889
11890
/**
11891
 * Parse an external general entity within an existing parsing context
11892
 * An external general parsed entity is well-formed if it matches the
11893
 * production labeled extParsedEnt.
11894
 *
11895
 *     [78] extParsedEnt ::= TextDecl? content
11896
 *
11897
 * @param ctxt  the existing parsing context
11898
 * @param URL  the URL for the entity to load
11899
 * @param ID  the System ID for the entity to load
11900
 * @param listOut  the return value for the set of parsed nodes
11901
 * @returns 0 if the entity is well formed, -1 in case of args problem and
11902
 *    the parser error code otherwise
11903
 */
11904
11905
int
11906
xmlParseCtxtExternalEntity(xmlParserCtxt *ctxt, const xmlChar *URL,
11907
0
                           const xmlChar *ID, xmlNode **listOut) {
11908
0
    xmlParserInputPtr input;
11909
0
    xmlNodePtr list;
11910
11911
0
    if (listOut != NULL)
11912
0
        *listOut = NULL;
11913
11914
0
    if (ctxt == NULL)
11915
0
        return(XML_ERR_ARGUMENT);
11916
11917
0
    input = xmlLoadResource(ctxt, (char *) URL, (char *) ID,
11918
0
                            XML_RESOURCE_GENERAL_ENTITY);
11919
0
    if (input == NULL)
11920
0
        return(ctxt->errNo);
11921
11922
0
    xmlCtxtInitializeLate(ctxt);
11923
11924
0
    list = xmlCtxtParseContentInternal(ctxt, input, /* hasTextDecl */ 1, 1);
11925
0
    if (listOut != NULL)
11926
0
        *listOut = list;
11927
0
    else
11928
0
        xmlFreeNodeList(list);
11929
11930
0
    xmlFreeInputStream(input);
11931
0
    return(ctxt->errNo);
11932
0
}
11933
11934
#ifdef LIBXML_SAX1_ENABLED
11935
/**
11936
 * Parse an external general entity
11937
 * An external general parsed entity is well-formed if it matches the
11938
 * production labeled extParsedEnt.
11939
 *
11940
 * @deprecated Use #xmlParseCtxtExternalEntity.
11941
 *
11942
 *     [78] extParsedEnt ::= TextDecl? content
11943
 *
11944
 * @param doc  the document the chunk pertains to
11945
 * @param sax  the SAX handler block (possibly NULL)
11946
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
11947
 * @param depth  Used for loop detection, use 0
11948
 * @param URL  the URL for the entity to load
11949
 * @param ID  the System ID for the entity to load
11950
 * @param list  the return value for the set of parsed nodes
11951
 * @returns 0 if the entity is well formed, -1 in case of args problem and
11952
 *    the parser error code otherwise
11953
 */
11954
11955
int
11956
xmlParseExternalEntity(xmlDoc *doc, xmlSAXHandler *sax, void *user_data,
11957
0
    int depth, const xmlChar *URL, const xmlChar *ID, xmlNode **list) {
11958
0
    xmlParserCtxtPtr ctxt;
11959
0
    int ret;
11960
11961
0
    if (list != NULL)
11962
0
        *list = NULL;
11963
11964
0
    if (doc == NULL)
11965
0
        return(XML_ERR_ARGUMENT);
11966
11967
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
11968
0
    if (ctxt == NULL)
11969
0
        return(XML_ERR_NO_MEMORY);
11970
11971
0
    ctxt->depth = depth;
11972
0
    ctxt->myDoc = doc;
11973
0
    ret = xmlParseCtxtExternalEntity(ctxt, URL, ID, list);
11974
11975
0
    xmlFreeParserCtxt(ctxt);
11976
0
    return(ret);
11977
0
}
11978
11979
/**
11980
 * Parse a well-balanced chunk of an XML document
11981
 * called by the parser
11982
 * The allowed sequence for the Well Balanced Chunk is the one defined by
11983
 * the content production in the XML grammar:
11984
 *
11985
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
11986
 *                       Comment)*
11987
 *
11988
 * @param doc  the document the chunk pertains to (must not be NULL)
11989
 * @param sax  the SAX handler block (possibly NULL)
11990
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
11991
 * @param depth  Used for loop detection, use 0
11992
 * @param string  the input string in UTF8 or ISO-Latin (zero terminated)
11993
 * @param lst  the return value for the set of parsed nodes
11994
 * @returns 0 if the chunk is well balanced, -1 in case of args problem and
11995
 *    the parser error code otherwise
11996
 */
11997
11998
int
11999
xmlParseBalancedChunkMemory(xmlDoc *doc, xmlSAXHandler *sax,
12000
0
     void *user_data, int depth, const xmlChar *string, xmlNode **lst) {
12001
0
    return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
12002
0
                                                depth, string, lst, 0 );
12003
0
}
12004
#endif /* LIBXML_SAX1_ENABLED */
12005
12006
/**
12007
 * Parse a well-balanced chunk of XML matching the 'content' production.
12008
 *
12009
 * Namespaces in scope of `node` and entities of `node`'s document are
12010
 * recognized. When validating, the DTD of `node`'s document is used.
12011
 *
12012
 * Always consumes `input` even in error case.
12013
 *
12014
 * @since 2.14.0
12015
 *
12016
 * @param ctxt  parser context
12017
 * @param input  parser input
12018
 * @param node  target node or document
12019
 * @param hasTextDecl  whether to parse text declaration
12020
 * @returns a node list or NULL in case of error.
12021
 */
12022
xmlNode *
12023
xmlCtxtParseContent(xmlParserCtxt *ctxt, xmlParserInput *input,
12024
0
                    xmlNode *node, int hasTextDecl) {
12025
0
    xmlDocPtr doc;
12026
0
    xmlNodePtr cur, list = NULL;
12027
0
    int nsnr = 0;
12028
0
    xmlDictPtr oldDict;
12029
0
    int oldOptions, oldDictNames, oldLoadSubset;
12030
12031
0
    if ((ctxt == NULL) || (input == NULL) || (node == NULL)) {
12032
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12033
0
        goto exit;
12034
0
    }
12035
12036
0
    doc = node->doc;
12037
0
    if (doc == NULL) {
12038
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12039
0
        goto exit;
12040
0
    }
12041
12042
0
    switch (node->type) {
12043
0
        case XML_ELEMENT_NODE:
12044
0
        case XML_DOCUMENT_NODE:
12045
0
        case XML_HTML_DOCUMENT_NODE:
12046
0
            break;
12047
12048
0
        case XML_ATTRIBUTE_NODE:
12049
0
        case XML_TEXT_NODE:
12050
0
        case XML_CDATA_SECTION_NODE:
12051
0
        case XML_ENTITY_REF_NODE:
12052
0
        case XML_PI_NODE:
12053
0
        case XML_COMMENT_NODE:
12054
0
            for (cur = node->parent; cur != NULL; cur = node->parent) {
12055
0
                if ((cur->type == XML_ELEMENT_NODE) ||
12056
0
                    (cur->type == XML_DOCUMENT_NODE) ||
12057
0
                    (cur->type == XML_HTML_DOCUMENT_NODE)) {
12058
0
                    node = cur;
12059
0
                    break;
12060
0
                }
12061
0
            }
12062
0
            break;
12063
12064
0
        default:
12065
0
            xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
12066
0
            goto exit;
12067
0
    }
12068
12069
0
    xmlCtxtReset(ctxt);
12070
12071
0
    oldDict = ctxt->dict;
12072
0
    oldOptions = ctxt->options;
12073
0
    oldDictNames = ctxt->dictNames;
12074
0
    oldLoadSubset = ctxt->loadsubset;
12075
12076
    /*
12077
     * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
12078
     */
12079
0
    if (doc->dict != NULL) {
12080
0
        ctxt->dict = doc->dict;
12081
0
    } else {
12082
0
        ctxt->options |= XML_PARSE_NODICT;
12083
0
        ctxt->dictNames = 0;
12084
0
    }
12085
12086
    /*
12087
     * Disable IDs
12088
     */
12089
0
    ctxt->loadsubset |= XML_SKIP_IDS;
12090
0
    ctxt->options |= XML_PARSE_SKIP_IDS;
12091
12092
0
    ctxt->myDoc = doc;
12093
12094
0
#ifdef LIBXML_HTML_ENABLED
12095
0
    if (ctxt->html) {
12096
        /*
12097
         * When parsing in context, it makes no sense to add implied
12098
         * elements like html/body/etc...
12099
         */
12100
0
        ctxt->options |= HTML_PARSE_NOIMPLIED;
12101
12102
0
        list = htmlCtxtParseContentInternal(ctxt, input);
12103
0
    } else
12104
0
#endif
12105
0
    {
12106
0
        xmlCtxtInitializeLate(ctxt);
12107
12108
        /*
12109
         * initialize the SAX2 namespaces stack
12110
         */
12111
0
        cur = node;
12112
0
        while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
12113
0
            xmlNsPtr ns = cur->nsDef;
12114
0
            xmlHashedString hprefix, huri;
12115
12116
0
            while (ns != NULL) {
12117
0
                hprefix = xmlDictLookupHashed(ctxt->dict, ns->prefix, -1);
12118
0
                huri = xmlDictLookupHashed(ctxt->dict, ns->href, -1);
12119
0
                if (xmlParserNsPush(ctxt, &hprefix, &huri, ns, 1) > 0)
12120
0
                    nsnr++;
12121
0
                ns = ns->next;
12122
0
            }
12123
0
            cur = cur->parent;
12124
0
        }
12125
12126
0
        list = xmlCtxtParseContentInternal(ctxt, input, hasTextDecl, 1);
12127
12128
0
        if (nsnr > 0)
12129
0
            xmlParserNsPop(ctxt, nsnr);
12130
0
    }
12131
12132
0
    ctxt->dict = oldDict;
12133
0
    ctxt->options = oldOptions;
12134
0
    ctxt->dictNames = oldDictNames;
12135
0
    ctxt->loadsubset = oldLoadSubset;
12136
0
    ctxt->myDoc = NULL;
12137
0
    ctxt->node = NULL;
12138
12139
0
exit:
12140
0
    xmlFreeInputStream(input);
12141
0
    return(list);
12142
0
}
12143
12144
/**
12145
 * Parse a well-balanced chunk of an XML document
12146
 * within the context (DTD, namespaces, etc ...) of the given node.
12147
 *
12148
 * The allowed sequence for the data is a Well Balanced Chunk defined by
12149
 * the content production in the XML grammar:
12150
 *
12151
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12152
 *                       Comment)*
12153
 *
12154
 * This function assumes the encoding of `node`'s document which is
12155
 * typically not what you want. A better alternative is
12156
 * #xmlCtxtParseContent.
12157
 *
12158
 * @param node  the context node
12159
 * @param data  the input string
12160
 * @param datalen  the input string length in bytes
12161
 * @param options  a combination of xmlParserOption
12162
 * @param listOut  the return value for the set of parsed nodes
12163
 * @returns XML_ERR_OK if the chunk is well balanced, and the parser
12164
 * error code otherwise
12165
 */
12166
xmlParserErrors
12167
xmlParseInNodeContext(xmlNode *node, const char *data, int datalen,
12168
0
                      int options, xmlNode **listOut) {
12169
0
    xmlParserCtxtPtr ctxt;
12170
0
    xmlParserInputPtr input;
12171
0
    xmlDocPtr doc;
12172
0
    xmlNodePtr list;
12173
0
    xmlParserErrors ret;
12174
12175
0
    if (listOut == NULL)
12176
0
        return(XML_ERR_INTERNAL_ERROR);
12177
0
    *listOut = NULL;
12178
12179
0
    if ((node == NULL) || (data == NULL) || (datalen < 0))
12180
0
        return(XML_ERR_INTERNAL_ERROR);
12181
12182
0
    doc = node->doc;
12183
0
    if (doc == NULL)
12184
0
        return(XML_ERR_INTERNAL_ERROR);
12185
12186
0
#ifdef LIBXML_HTML_ENABLED
12187
0
    if (doc->type == XML_HTML_DOCUMENT_NODE) {
12188
0
        ctxt = htmlNewParserCtxt();
12189
0
    }
12190
0
    else
12191
0
#endif
12192
0
        ctxt = xmlNewParserCtxt();
12193
12194
0
    if (ctxt == NULL)
12195
0
        return(XML_ERR_NO_MEMORY);
12196
12197
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, data, datalen,
12198
0
                                      (const char *) doc->encoding,
12199
0
                                      XML_INPUT_BUF_STATIC);
12200
0
    if (input == NULL) {
12201
0
        xmlFreeParserCtxt(ctxt);
12202
0
        return(XML_ERR_NO_MEMORY);
12203
0
    }
12204
12205
0
    xmlCtxtUseOptions(ctxt, options);
12206
12207
0
    list = xmlCtxtParseContent(ctxt, input, node, /* hasTextDecl */ 0);
12208
12209
0
    if (list == NULL) {
12210
0
        ret = ctxt->errNo;
12211
0
        if (ret == XML_ERR_ARGUMENT)
12212
0
            ret = XML_ERR_INTERNAL_ERROR;
12213
0
    } else {
12214
0
        ret = XML_ERR_OK;
12215
0
        *listOut = list;
12216
0
    }
12217
12218
0
    xmlFreeParserCtxt(ctxt);
12219
12220
0
    return(ret);
12221
0
}
12222
12223
#ifdef LIBXML_SAX1_ENABLED
12224
/**
12225
 * Parse a well-balanced chunk of an XML document
12226
 *
12227
 * The allowed sequence for the Well Balanced Chunk is the one defined by
12228
 * the content production in the XML grammar:
12229
 *
12230
 *     [43] content ::= (element | CharData | Reference | CDSect | PI |
12231
 *                       Comment)*
12232
 *
12233
 * In case recover is set to 1, the nodelist will not be empty even if
12234
 * the parsed chunk is not well balanced, assuming the parsing succeeded to
12235
 * some extent.
12236
 *
12237
 * @param doc  the document the chunk pertains to (must not be NULL)
12238
 * @param sax  the SAX handler block (possibly NULL)
12239
 * @param user_data  The user data returned on SAX callbacks (possibly NULL)
12240
 * @param depth  Used for loop detection, use 0
12241
 * @param string  the input string in UTF8 or ISO-Latin (zero terminated)
12242
 * @param listOut  the return value for the set of parsed nodes
12243
 * @param recover  return nodes even if the data is broken (use 0)
12244
 * @returns 0 if the chunk is well balanced, or thehe parser error code
12245
 * otherwise.
12246
 */
12247
int
12248
xmlParseBalancedChunkMemoryRecover(xmlDoc *doc, xmlSAXHandler *sax,
12249
     void *user_data, int depth, const xmlChar *string, xmlNode **listOut,
12250
0
     int recover) {
12251
0
    xmlParserCtxtPtr ctxt;
12252
0
    xmlParserInputPtr input;
12253
0
    xmlNodePtr list;
12254
0
    int ret;
12255
12256
0
    if (listOut != NULL)
12257
0
        *listOut = NULL;
12258
12259
0
    if (string == NULL)
12260
0
        return(XML_ERR_ARGUMENT);
12261
12262
0
    ctxt = xmlNewSAXParserCtxt(sax, user_data);
12263
0
    if (ctxt == NULL)
12264
0
        return(XML_ERR_NO_MEMORY);
12265
12266
0
    xmlCtxtInitializeLate(ctxt);
12267
12268
0
    ctxt->depth = depth;
12269
0
    ctxt->myDoc = doc;
12270
0
    if (recover) {
12271
0
        ctxt->options |= XML_PARSE_RECOVER;
12272
0
        ctxt->recovery = 1;
12273
0
    }
12274
12275
0
    input = xmlNewStringInputStream(ctxt, string);
12276
0
    if (input == NULL) {
12277
0
        ret = ctxt->errNo;
12278
0
        goto error;
12279
0
    }
12280
12281
0
    list = xmlCtxtParseContentInternal(ctxt, input, /* hasTextDecl */ 0, 1);
12282
0
    if (listOut != NULL)
12283
0
        *listOut = list;
12284
0
    else
12285
0
        xmlFreeNodeList(list);
12286
12287
0
    if (!ctxt->wellFormed)
12288
0
        ret = ctxt->errNo;
12289
0
    else
12290
0
        ret = XML_ERR_OK;
12291
12292
0
error:
12293
0
    xmlFreeInputStream(input);
12294
0
    xmlFreeParserCtxt(ctxt);
12295
0
    return(ret);
12296
0
}
12297
12298
/**
12299
 * Parse an XML external entity out of context and build a tree.
12300
 * It use the given SAX function block to handle the parsing callback.
12301
 * If sax is NULL, fallback to the default DOM tree building routines.
12302
 *
12303
 * @deprecated Don't use.
12304
 *
12305
 *     [78] extParsedEnt ::= TextDecl? content
12306
 *
12307
 * This correspond to a "Well Balanced" chunk
12308
 *
12309
 * @param sax  the SAX handler block
12310
 * @param filename  the filename
12311
 * @returns the resulting document tree
12312
 */
12313
12314
xmlDoc *
12315
0
xmlSAXParseEntity(xmlSAXHandler *sax, const char *filename) {
12316
0
    xmlDocPtr ret;
12317
0
    xmlParserCtxtPtr ctxt;
12318
12319
0
    ctxt = xmlCreateFileParserCtxt(filename);
12320
0
    if (ctxt == NULL) {
12321
0
  return(NULL);
12322
0
    }
12323
0
    if (sax != NULL) {
12324
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12325
0
            *ctxt->sax = *sax;
12326
0
        } else {
12327
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12328
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12329
0
        }
12330
0
        ctxt->userData = NULL;
12331
0
    }
12332
12333
0
    xmlParseExtParsedEnt(ctxt);
12334
12335
0
    if (ctxt->wellFormed) {
12336
0
  ret = ctxt->myDoc;
12337
0
    } else {
12338
0
        ret = NULL;
12339
0
        xmlFreeDoc(ctxt->myDoc);
12340
0
    }
12341
12342
0
    xmlFreeParserCtxt(ctxt);
12343
12344
0
    return(ret);
12345
0
}
12346
12347
/**
12348
 * Parse an XML external entity out of context and build a tree.
12349
 *
12350
 *     [78] extParsedEnt ::= TextDecl? content
12351
 *
12352
 * This correspond to a "Well Balanced" chunk
12353
 *
12354
 * @param filename  the filename
12355
 * @returns the resulting document tree
12356
 */
12357
12358
xmlDoc *
12359
0
xmlParseEntity(const char *filename) {
12360
0
    return(xmlSAXParseEntity(NULL, filename));
12361
0
}
12362
#endif /* LIBXML_SAX1_ENABLED */
12363
12364
/**
12365
 * Create a parser context for an external entity
12366
 * Automatic support for ZLIB/Compress compressed document is provided
12367
 * by default if found at compile-time.
12368
 *
12369
 * @deprecated Don't use.
12370
 *
12371
 * @param URL  the entity URL
12372
 * @param ID  the entity PUBLIC ID
12373
 * @param base  a possible base for the target URI
12374
 * @returns the new parser context or NULL
12375
 */
12376
xmlParserCtxt *
12377
xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
12378
0
                    const xmlChar *base) {
12379
0
    xmlParserCtxtPtr ctxt;
12380
0
    xmlParserInputPtr input;
12381
0
    xmlChar *uri = NULL;
12382
12383
0
    ctxt = xmlNewParserCtxt();
12384
0
    if (ctxt == NULL)
12385
0
  return(NULL);
12386
12387
0
    if (base != NULL) {
12388
0
        if (xmlBuildURISafe(URL, base, &uri) < 0)
12389
0
            goto error;
12390
0
        if (uri != NULL)
12391
0
            URL = uri;
12392
0
    }
12393
12394
0
    input = xmlLoadResource(ctxt, (char *) URL, (char *) ID,
12395
0
                            XML_RESOURCE_UNKNOWN);
12396
0
    if (input == NULL)
12397
0
        goto error;
12398
12399
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12400
0
        xmlFreeInputStream(input);
12401
0
        goto error;
12402
0
    }
12403
12404
0
    xmlFree(uri);
12405
0
    return(ctxt);
12406
12407
0
error:
12408
0
    xmlFree(uri);
12409
0
    xmlFreeParserCtxt(ctxt);
12410
0
    return(NULL);
12411
0
}
12412
12413
/************************************************************************
12414
 *                  *
12415
 *    Front ends when parsing from a file     *
12416
 *                  *
12417
 ************************************************************************/
12418
12419
/**
12420
 * Create a parser context for a file or URL content.
12421
 * Automatic support for ZLIB/Compress compressed document is provided
12422
 * by default if found at compile-time and for file accesses
12423
 *
12424
 * @deprecated Use #xmlNewParserCtxt and #xmlCtxtReadFile.
12425
 *
12426
 * @param filename  the filename or URL
12427
 * @param options  a combination of xmlParserOption
12428
 * @returns the new parser context or NULL
12429
 */
12430
xmlParserCtxt *
12431
xmlCreateURLParserCtxt(const char *filename, int options)
12432
0
{
12433
0
    xmlParserCtxtPtr ctxt;
12434
0
    xmlParserInputPtr input;
12435
12436
0
    ctxt = xmlNewParserCtxt();
12437
0
    if (ctxt == NULL)
12438
0
  return(NULL);
12439
12440
0
    options |= XML_PARSE_UNZIP;
12441
12442
0
    xmlCtxtUseOptions(ctxt, options);
12443
12444
0
    input = xmlLoadResource(ctxt, filename, NULL, XML_RESOURCE_MAIN_DOCUMENT);
12445
0
    if (input == NULL) {
12446
0
  xmlFreeParserCtxt(ctxt);
12447
0
  return(NULL);
12448
0
    }
12449
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12450
0
        xmlFreeInputStream(input);
12451
0
        xmlFreeParserCtxt(ctxt);
12452
0
        return(NULL);
12453
0
    }
12454
12455
0
    return(ctxt);
12456
0
}
12457
12458
/**
12459
 * Create a parser context for a file content.
12460
 * Automatic support for ZLIB/Compress compressed document is provided
12461
 * by default if found at compile-time.
12462
 *
12463
 * @deprecated Use #xmlNewParserCtxt and #xmlCtxtReadFile.
12464
 *
12465
 * @param filename  the filename
12466
 * @returns the new parser context or NULL
12467
 */
12468
xmlParserCtxt *
12469
xmlCreateFileParserCtxt(const char *filename)
12470
0
{
12471
0
    return(xmlCreateURLParserCtxt(filename, 0));
12472
0
}
12473
12474
#ifdef LIBXML_SAX1_ENABLED
12475
/**
12476
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12477
 * compressed document is provided by default if found at compile-time.
12478
 * It use the given SAX function block to handle the parsing callback.
12479
 * If sax is NULL, fallback to the default DOM tree building routines.
12480
 *
12481
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12482
 *
12483
 * User data (void *) is stored within the parser context in the
12484
 * context's _private member, so it is available nearly everywhere in libxml
12485
 *
12486
 * @param sax  the SAX handler block
12487
 * @param filename  the filename
12488
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12489
 *             documents
12490
 * @param data  the userdata
12491
 * @returns the resulting document tree
12492
 */
12493
12494
xmlDoc *
12495
xmlSAXParseFileWithData(xmlSAXHandler *sax, const char *filename,
12496
0
                        int recovery, void *data) {
12497
0
    xmlDocPtr ret = NULL;
12498
0
    xmlParserCtxtPtr ctxt;
12499
0
    xmlParserInputPtr input;
12500
12501
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
12502
0
    if (ctxt == NULL)
12503
0
  return(NULL);
12504
12505
0
    if (data != NULL)
12506
0
  ctxt->_private = data;
12507
12508
0
    if (recovery) {
12509
0
        ctxt->options |= XML_PARSE_RECOVER;
12510
0
        ctxt->recovery = 1;
12511
0
    }
12512
12513
0
    if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0))
12514
0
        input = xmlCtxtNewInputFromFd(ctxt, filename, STDIN_FILENO, NULL, 0);
12515
0
    else
12516
0
        input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, NULL, 0);
12517
12518
0
    if (input != NULL)
12519
0
        ret = xmlCtxtParseDocument(ctxt, input);
12520
12521
0
    xmlFreeParserCtxt(ctxt);
12522
0
    return(ret);
12523
0
}
12524
12525
/**
12526
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12527
 * compressed document is provided by default if found at compile-time.
12528
 * It use the given SAX function block to handle the parsing callback.
12529
 * If sax is NULL, fallback to the default DOM tree building routines.
12530
 *
12531
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12532
 *
12533
 * @param sax  the SAX handler block
12534
 * @param filename  the filename
12535
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12536
 *             documents
12537
 * @returns the resulting document tree
12538
 */
12539
12540
xmlDoc *
12541
xmlSAXParseFile(xmlSAXHandler *sax, const char *filename,
12542
0
                          int recovery) {
12543
0
    return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
12544
0
}
12545
12546
/**
12547
 * Parse an XML in-memory document and build a tree.
12548
 * In the case the document is not Well Formed, a attempt to build a
12549
 * tree is tried anyway
12550
 *
12551
 * @deprecated Use #xmlReadDoc with XML_PARSE_RECOVER.
12552
 *
12553
 * @param cur  a pointer to an array of xmlChar
12554
 * @returns the resulting document tree or NULL in case of failure
12555
 */
12556
12557
xmlDoc *
12558
0
xmlRecoverDoc(const xmlChar *cur) {
12559
0
    return(xmlSAXParseDoc(NULL, cur, 1));
12560
0
}
12561
12562
/**
12563
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12564
 * compressed document is provided by default if found at compile-time.
12565
 *
12566
 * @deprecated Use #xmlReadFile.
12567
 *
12568
 * @param filename  the filename
12569
 * @returns the resulting document tree if the file was wellformed,
12570
 * NULL otherwise.
12571
 */
12572
12573
xmlDoc *
12574
0
xmlParseFile(const char *filename) {
12575
0
    return(xmlSAXParseFile(NULL, filename, 0));
12576
0
}
12577
12578
/**
12579
 * Parse an XML file and build a tree. Automatic support for ZLIB/Compress
12580
 * compressed document is provided by default if found at compile-time.
12581
 * In the case the document is not Well Formed, it attempts to build
12582
 * a tree anyway
12583
 *
12584
 * @deprecated Use #xmlReadFile with XML_PARSE_RECOVER.
12585
 *
12586
 * @param filename  the filename
12587
 * @returns the resulting document tree or NULL in case of failure
12588
 */
12589
12590
xmlDoc *
12591
0
xmlRecoverFile(const char *filename) {
12592
0
    return(xmlSAXParseFile(NULL, filename, 1));
12593
0
}
12594
12595
12596
/**
12597
 * Setup the parser context to parse a new buffer; Clears any prior
12598
 * contents from the parser context. The buffer parameter must not be
12599
 * NULL, but the filename parameter can be
12600
 *
12601
 * @deprecated Don't use.
12602
 *
12603
 * @param ctxt  an XML parser context
12604
 * @param buffer  a xmlChar * buffer
12605
 * @param filename  a file name
12606
 */
12607
void
12608
xmlSetupParserForBuffer(xmlParserCtxt *ctxt, const xmlChar* buffer,
12609
                             const char* filename)
12610
0
{
12611
0
    xmlParserInputPtr input;
12612
12613
0
    if ((ctxt == NULL) || (buffer == NULL))
12614
0
        return;
12615
12616
0
    xmlCtxtReset(ctxt);
12617
12618
0
    input = xmlCtxtNewInputFromString(ctxt, filename, (const char *) buffer,
12619
0
                                      NULL, 0);
12620
0
    if (input == NULL)
12621
0
        return;
12622
0
    if (xmlCtxtPushInput(ctxt, input) < 0)
12623
0
        xmlFreeInputStream(input);
12624
0
}
12625
12626
/**
12627
 * Parse an XML file and call the given SAX handler routines.
12628
 * Automatic support for ZLIB/Compress compressed document is provided
12629
 *
12630
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadFile.
12631
 *
12632
 * @param sax  a SAX handler
12633
 * @param user_data  The user data returned on SAX callbacks
12634
 * @param filename  a file name
12635
 * @returns 0 in case of success or a error number otherwise
12636
 */
12637
int
12638
xmlSAXUserParseFile(xmlSAXHandler *sax, void *user_data,
12639
0
                    const char *filename) {
12640
0
    int ret = 0;
12641
0
    xmlParserCtxtPtr ctxt;
12642
12643
0
    ctxt = xmlCreateFileParserCtxt(filename);
12644
0
    if (ctxt == NULL) return -1;
12645
0
    if (sax != NULL) {
12646
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12647
0
            *ctxt->sax = *sax;
12648
0
        } else {
12649
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12650
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12651
0
        }
12652
0
  ctxt->userData = user_data;
12653
0
    }
12654
12655
0
    xmlParseDocument(ctxt);
12656
12657
0
    if (ctxt->wellFormed)
12658
0
  ret = 0;
12659
0
    else {
12660
0
        if (ctxt->errNo != 0)
12661
0
      ret = ctxt->errNo;
12662
0
  else
12663
0
      ret = -1;
12664
0
    }
12665
0
    if (ctxt->myDoc != NULL) {
12666
0
        xmlFreeDoc(ctxt->myDoc);
12667
0
  ctxt->myDoc = NULL;
12668
0
    }
12669
0
    xmlFreeParserCtxt(ctxt);
12670
12671
0
    return ret;
12672
0
}
12673
#endif /* LIBXML_SAX1_ENABLED */
12674
12675
/************************************************************************
12676
 *                  *
12677
 *    Front ends when parsing from memory     *
12678
 *                  *
12679
 ************************************************************************/
12680
12681
/**
12682
 * Create a parser context for an XML in-memory document. The input buffer
12683
 * must not contain a terminating null byte.
12684
 *
12685
 * @param buffer  a pointer to a char array
12686
 * @param size  the size of the array
12687
 * @returns the new parser context or NULL
12688
 */
12689
xmlParserCtxt *
12690
0
xmlCreateMemoryParserCtxt(const char *buffer, int size) {
12691
0
    xmlParserCtxtPtr ctxt;
12692
0
    xmlParserInputPtr input;
12693
12694
0
    if (size < 0)
12695
0
  return(NULL);
12696
12697
0
    ctxt = xmlNewParserCtxt();
12698
0
    if (ctxt == NULL)
12699
0
  return(NULL);
12700
12701
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, buffer, size, NULL, 0);
12702
0
    if (input == NULL) {
12703
0
  xmlFreeParserCtxt(ctxt);
12704
0
  return(NULL);
12705
0
    }
12706
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12707
0
        xmlFreeInputStream(input);
12708
0
        xmlFreeParserCtxt(ctxt);
12709
0
        return(NULL);
12710
0
    }
12711
12712
0
    return(ctxt);
12713
0
}
12714
12715
#ifdef LIBXML_SAX1_ENABLED
12716
/**
12717
 * Parse an XML in-memory block and use the given SAX function block
12718
 * to handle the parsing callback. If sax is NULL, fallback to the default
12719
 * DOM tree building routines.
12720
 *
12721
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12722
 *
12723
 * User data (void *) is stored within the parser context in the
12724
 * context's _private member, so it is available nearly everywhere in libxml
12725
 *
12726
 * @param sax  the SAX handler block
12727
 * @param buffer  an pointer to a char array
12728
 * @param size  the size of the array
12729
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12730
 *             documents
12731
 * @param data  the userdata
12732
 * @returns the resulting document tree
12733
 */
12734
12735
xmlDoc *
12736
xmlSAXParseMemoryWithData(xmlSAXHandler *sax, const char *buffer,
12737
0
                          int size, int recovery, void *data) {
12738
0
    xmlDocPtr ret = NULL;
12739
0
    xmlParserCtxtPtr ctxt;
12740
0
    xmlParserInputPtr input;
12741
12742
0
    if (size < 0)
12743
0
        return(NULL);
12744
12745
0
    ctxt = xmlNewSAXParserCtxt(sax, NULL);
12746
0
    if (ctxt == NULL)
12747
0
        return(NULL);
12748
12749
0
    if (data != NULL)
12750
0
  ctxt->_private=data;
12751
12752
0
    if (recovery) {
12753
0
        ctxt->options |= XML_PARSE_RECOVER;
12754
0
        ctxt->recovery = 1;
12755
0
    }
12756
12757
0
    input = xmlCtxtNewInputFromMemory(ctxt, NULL, buffer, size, NULL,
12758
0
                                      XML_INPUT_BUF_STATIC);
12759
12760
0
    if (input != NULL)
12761
0
        ret = xmlCtxtParseDocument(ctxt, input);
12762
12763
0
    xmlFreeParserCtxt(ctxt);
12764
0
    return(ret);
12765
0
}
12766
12767
/**
12768
 * Parse an XML in-memory block and use the given SAX function block
12769
 * to handle the parsing callback. If sax is NULL, fallback to the default
12770
 * DOM tree building routines.
12771
 *
12772
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12773
 *
12774
 * @param sax  the SAX handler block
12775
 * @param buffer  an pointer to a char array
12776
 * @param size  the size of the array
12777
 * @param recovery  work in recovery mode, i.e. tries to read not Well Formed
12778
 *             documents
12779
 * @returns the resulting document tree
12780
 */
12781
xmlDoc *
12782
xmlSAXParseMemory(xmlSAXHandler *sax, const char *buffer,
12783
0
            int size, int recovery) {
12784
0
    return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
12785
0
}
12786
12787
/**
12788
 * Parse an XML in-memory block and build a tree.
12789
 *
12790
 * @deprecated Use #xmlReadMemory.
12791
 *
12792
 * @param buffer  an pointer to a char array
12793
 * @param size  the size of the array
12794
 * @returns the resulting document tree
12795
 */
12796
12797
0
xmlDoc *xmlParseMemory(const char *buffer, int size) {
12798
0
   return(xmlSAXParseMemory(NULL, buffer, size, 0));
12799
0
}
12800
12801
/**
12802
 * Parse an XML in-memory block and build a tree.
12803
 * In the case the document is not Well Formed, an attempt to
12804
 * build a tree is tried anyway
12805
 *
12806
 * @deprecated Use #xmlReadMemory with XML_PARSE_RECOVER.
12807
 *
12808
 * @param buffer  an pointer to a char array
12809
 * @param size  the size of the array
12810
 * @returns the resulting document tree or NULL in case of error
12811
 */
12812
12813
0
xmlDoc *xmlRecoverMemory(const char *buffer, int size) {
12814
0
   return(xmlSAXParseMemory(NULL, buffer, size, 1));
12815
0
}
12816
12817
/**
12818
 * Parse an XML in-memory buffer and call the given SAX handler routines.
12819
 *
12820
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadMemory.
12821
 *
12822
 * @param sax  a SAX handler
12823
 * @param user_data  The user data returned on SAX callbacks
12824
 * @param buffer  an in-memory XML document input
12825
 * @param size  the length of the XML document in bytes
12826
 * @returns 0 in case of success or a error number otherwise
12827
 */
12828
int xmlSAXUserParseMemory(xmlSAXHandler *sax, void *user_data,
12829
0
        const char *buffer, int size) {
12830
0
    int ret = 0;
12831
0
    xmlParserCtxtPtr ctxt;
12832
12833
0
    ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12834
0
    if (ctxt == NULL) return -1;
12835
0
    if (sax != NULL) {
12836
0
        if (sax->initialized == XML_SAX2_MAGIC) {
12837
0
            *ctxt->sax = *sax;
12838
0
        } else {
12839
0
            memset(ctxt->sax, 0, sizeof(*ctxt->sax));
12840
0
            memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
12841
0
        }
12842
0
  ctxt->userData = user_data;
12843
0
    }
12844
12845
0
    xmlParseDocument(ctxt);
12846
12847
0
    if (ctxt->wellFormed)
12848
0
  ret = 0;
12849
0
    else {
12850
0
        if (ctxt->errNo != 0)
12851
0
      ret = ctxt->errNo;
12852
0
  else
12853
0
      ret = -1;
12854
0
    }
12855
0
    if (ctxt->myDoc != NULL) {
12856
0
        xmlFreeDoc(ctxt->myDoc);
12857
0
  ctxt->myDoc = NULL;
12858
0
    }
12859
0
    xmlFreeParserCtxt(ctxt);
12860
12861
0
    return ret;
12862
0
}
12863
#endif /* LIBXML_SAX1_ENABLED */
12864
12865
/**
12866
 * Creates a parser context for an XML in-memory document.
12867
 *
12868
 * @param str  a pointer to an array of xmlChar
12869
 * @returns the new parser context or NULL
12870
 */
12871
xmlParserCtxt *
12872
0
xmlCreateDocParserCtxt(const xmlChar *str) {
12873
0
    xmlParserCtxtPtr ctxt;
12874
0
    xmlParserInputPtr input;
12875
12876
0
    ctxt = xmlNewParserCtxt();
12877
0
    if (ctxt == NULL)
12878
0
  return(NULL);
12879
12880
0
    input = xmlCtxtNewInputFromString(ctxt, NULL, (const char *) str, NULL, 0);
12881
0
    if (input == NULL) {
12882
0
  xmlFreeParserCtxt(ctxt);
12883
0
  return(NULL);
12884
0
    }
12885
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
12886
0
        xmlFreeInputStream(input);
12887
0
        xmlFreeParserCtxt(ctxt);
12888
0
        return(NULL);
12889
0
    }
12890
12891
0
    return(ctxt);
12892
0
}
12893
12894
#ifdef LIBXML_SAX1_ENABLED
12895
/**
12896
 * Parse an XML in-memory document and build a tree.
12897
 * It use the given SAX function block to handle the parsing callback.
12898
 * If sax is NULL, fallback to the default DOM tree building routines.
12899
 *
12900
 * @deprecated Use #xmlNewSAXParserCtxt and #xmlCtxtReadDoc.
12901
 *
12902
 * @param sax  the SAX handler block
12903
 * @param cur  a pointer to an array of xmlChar
12904
 * @param recovery  work in recovery mode, i.e. tries to read no Well Formed
12905
 *             documents
12906
 * @returns the resulting document tree
12907
 */
12908
12909
xmlDoc *
12910
0
xmlSAXParseDoc(xmlSAXHandler *sax, const xmlChar *cur, int recovery) {
12911
0
    xmlDocPtr ret;
12912
0
    xmlParserCtxtPtr ctxt;
12913
0
    xmlSAXHandlerPtr oldsax = NULL;
12914
12915
0
    if (cur == NULL) return(NULL);
12916
12917
12918
0
    ctxt = xmlCreateDocParserCtxt(cur);
12919
0
    if (ctxt == NULL) return(NULL);
12920
0
    if (sax != NULL) {
12921
0
        oldsax = ctxt->sax;
12922
0
        ctxt->sax = sax;
12923
0
        ctxt->userData = NULL;
12924
0
    }
12925
12926
0
    xmlParseDocument(ctxt);
12927
0
    if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
12928
0
    else {
12929
0
       ret = NULL;
12930
0
       xmlFreeDoc(ctxt->myDoc);
12931
0
       ctxt->myDoc = NULL;
12932
0
    }
12933
0
    if (sax != NULL)
12934
0
  ctxt->sax = oldsax;
12935
0
    xmlFreeParserCtxt(ctxt);
12936
12937
0
    return(ret);
12938
0
}
12939
12940
/**
12941
 * Parse an XML in-memory document and build a tree.
12942
 *
12943
 * @deprecated Use #xmlReadDoc.
12944
 *
12945
 * @param cur  a pointer to an array of xmlChar
12946
 * @returns the resulting document tree
12947
 */
12948
12949
xmlDoc *
12950
0
xmlParseDoc(const xmlChar *cur) {
12951
0
    return(xmlSAXParseDoc(NULL, cur, 0));
12952
0
}
12953
#endif /* LIBXML_SAX1_ENABLED */
12954
12955
/************************************************************************
12956
 *                  *
12957
 *  New set (2.6.0) of simpler and more flexible APIs   *
12958
 *                  *
12959
 ************************************************************************/
12960
12961
/**
12962
 * Reset a parser context
12963
 *
12964
 * @param ctxt  an XML parser context
12965
 */
12966
void
12967
xmlCtxtReset(xmlParserCtxt *ctxt)
12968
9.87k
{
12969
9.87k
    xmlParserInputPtr input;
12970
12971
9.87k
    if (ctxt == NULL)
12972
0
        return;
12973
12974
9.87k
    while ((input = xmlCtxtPopInput(ctxt)) != NULL) { /* Non consuming */
12975
0
        xmlFreeInputStream(input);
12976
0
    }
12977
9.87k
    ctxt->inputNr = 0;
12978
9.87k
    ctxt->input = NULL;
12979
12980
9.87k
    ctxt->spaceNr = 0;
12981
9.87k
    if (ctxt->spaceTab != NULL) {
12982
0
  ctxt->spaceTab[0] = -1;
12983
0
  ctxt->space = &ctxt->spaceTab[0];
12984
9.87k
    } else {
12985
9.87k
        ctxt->space = NULL;
12986
9.87k
    }
12987
12988
12989
9.87k
    ctxt->nodeNr = 0;
12990
9.87k
    ctxt->node = NULL;
12991
12992
9.87k
    ctxt->nameNr = 0;
12993
9.87k
    ctxt->name = NULL;
12994
12995
9.87k
    ctxt->nsNr = 0;
12996
9.87k
    xmlParserNsReset(ctxt->nsdb);
12997
12998
9.87k
    if (ctxt->version != NULL) {
12999
0
        xmlFree(ctxt->version);
13000
0
        ctxt->version = NULL;
13001
0
    }
13002
9.87k
    if (ctxt->encoding != NULL) {
13003
0
        xmlFree(ctxt->encoding);
13004
0
        ctxt->encoding = NULL;
13005
0
    }
13006
9.87k
    if (ctxt->extSubURI != NULL) {
13007
0
        xmlFree(ctxt->extSubURI);
13008
0
        ctxt->extSubURI = NULL;
13009
0
    }
13010
9.87k
    if (ctxt->extSubSystem != NULL) {
13011
0
        xmlFree(ctxt->extSubSystem);
13012
0
        ctxt->extSubSystem = NULL;
13013
0
    }
13014
9.87k
    if (ctxt->directory != NULL) {
13015
0
        xmlFree(ctxt->directory);
13016
0
        ctxt->directory = NULL;
13017
0
    }
13018
13019
9.87k
    if (ctxt->myDoc != NULL)
13020
0
        xmlFreeDoc(ctxt->myDoc);
13021
9.87k
    ctxt->myDoc = NULL;
13022
13023
9.87k
    ctxt->standalone = -1;
13024
9.87k
    ctxt->hasExternalSubset = 0;
13025
9.87k
    ctxt->hasPErefs = 0;
13026
9.87k
    ctxt->html = ctxt->html ? 1 : 0;
13027
9.87k
    ctxt->instate = XML_PARSER_START;
13028
13029
9.87k
    ctxt->wellFormed = 1;
13030
9.87k
    ctxt->nsWellFormed = 1;
13031
9.87k
    ctxt->disableSAX = 0;
13032
9.87k
    ctxt->valid = 1;
13033
9.87k
    ctxt->record_info = 0;
13034
9.87k
    ctxt->checkIndex = 0;
13035
9.87k
    ctxt->endCheckState = 0;
13036
9.87k
    ctxt->inSubset = 0;
13037
9.87k
    ctxt->errNo = XML_ERR_OK;
13038
9.87k
    ctxt->depth = 0;
13039
9.87k
    ctxt->catalogs = NULL;
13040
9.87k
    ctxt->sizeentities = 0;
13041
9.87k
    ctxt->sizeentcopy = 0;
13042
9.87k
    xmlInitNodeInfoSeq(&ctxt->node_seq);
13043
13044
9.87k
    if (ctxt->attsDefault != NULL) {
13045
0
        xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
13046
0
        ctxt->attsDefault = NULL;
13047
0
    }
13048
9.87k
    if (ctxt->attsSpecial != NULL) {
13049
0
        xmlHashFree(ctxt->attsSpecial, NULL);
13050
0
        ctxt->attsSpecial = NULL;
13051
0
    }
13052
13053
9.87k
#ifdef LIBXML_CATALOG_ENABLED
13054
9.87k
    if (ctxt->catalogs != NULL)
13055
0
  xmlCatalogFreeLocal(ctxt->catalogs);
13056
9.87k
#endif
13057
9.87k
    ctxt->nbErrors = 0;
13058
9.87k
    ctxt->nbWarnings = 0;
13059
9.87k
    if (ctxt->lastError.code != XML_ERR_OK)
13060
0
        xmlResetError(&ctxt->lastError);
13061
9.87k
}
13062
13063
/**
13064
 * Reset a push parser context
13065
 *
13066
 * @param ctxt  an XML parser context
13067
 * @param chunk  a pointer to an array of chars
13068
 * @param size  number of chars in the array
13069
 * @param filename  an optional file name or URI
13070
 * @param encoding  the document encoding, or NULL
13071
 * @returns 0 in case of success and 1 in case of error
13072
 */
13073
int
13074
xmlCtxtResetPush(xmlParserCtxt *ctxt, const char *chunk,
13075
                 int size, const char *filename, const char *encoding)
13076
0
{
13077
0
    xmlParserInputPtr input;
13078
13079
0
    if (ctxt == NULL)
13080
0
        return(1);
13081
13082
0
    xmlCtxtReset(ctxt);
13083
13084
0
    input = xmlNewPushInput(filename, chunk, size);
13085
0
    if (input == NULL)
13086
0
        return(1);
13087
13088
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
13089
0
        xmlFreeInputStream(input);
13090
0
        return(1);
13091
0
    }
13092
13093
0
    if (encoding != NULL)
13094
0
        xmlSwitchEncodingName(ctxt, encoding);
13095
13096
0
    return(0);
13097
0
}
13098
13099
static int
13100
xmlCtxtSetOptionsInternal(xmlParserCtxtPtr ctxt, int options, int keepMask)
13101
0
{
13102
0
    int allMask;
13103
13104
0
    if (ctxt == NULL)
13105
0
        return(-1);
13106
13107
    /*
13108
     * XInclude options aren't handled by the parser.
13109
     *
13110
     * XML_PARSE_XINCLUDE
13111
     * XML_PARSE_NOXINCNODE
13112
     * XML_PARSE_NOBASEFIX
13113
     */
13114
0
    allMask = XML_PARSE_RECOVER |
13115
0
              XML_PARSE_NOENT |
13116
0
              XML_PARSE_DTDLOAD |
13117
0
              XML_PARSE_DTDATTR |
13118
0
              XML_PARSE_DTDVALID |
13119
0
              XML_PARSE_NOERROR |
13120
0
              XML_PARSE_NOWARNING |
13121
0
              XML_PARSE_PEDANTIC |
13122
0
              XML_PARSE_NOBLANKS |
13123
0
#ifdef LIBXML_SAX1_ENABLED
13124
0
              XML_PARSE_SAX1 |
13125
0
#endif
13126
0
              XML_PARSE_NONET |
13127
0
              XML_PARSE_NODICT |
13128
0
              XML_PARSE_NSCLEAN |
13129
0
              XML_PARSE_NOCDATA |
13130
0
              XML_PARSE_COMPACT |
13131
0
              XML_PARSE_OLD10 |
13132
0
              XML_PARSE_HUGE |
13133
0
              XML_PARSE_OLDSAX |
13134
0
              XML_PARSE_IGNORE_ENC |
13135
0
              XML_PARSE_BIG_LINES |
13136
0
              XML_PARSE_NO_XXE |
13137
0
              XML_PARSE_UNZIP |
13138
0
              XML_PARSE_NO_SYS_CATALOG |
13139
0
              XML_PARSE_CATALOG_PI;
13140
13141
0
    ctxt->options = (ctxt->options & keepMask) | (options & allMask);
13142
13143
    /*
13144
     * For some options, struct members are historically the source
13145
     * of truth. The values are initalized from global variables and
13146
     * old code could also modify them directly. Several older API
13147
     * functions that don't take an options argument rely on these
13148
     * deprecated mechanisms.
13149
     *
13150
     * Once public access to struct members and the globals are
13151
     * disabled, we can use the options bitmask as source of
13152
     * truth, making all these struct members obsolete.
13153
     *
13154
     * The XML_DETECT_IDS flags is misnamed. It simply enables
13155
     * loading of the external subset.
13156
     */
13157
0
    ctxt->recovery = (options & XML_PARSE_RECOVER) ? 1 : 0;
13158
0
    ctxt->replaceEntities = (options & XML_PARSE_NOENT) ? 1 : 0;
13159
0
    ctxt->loadsubset = (options & XML_PARSE_DTDLOAD) ? XML_DETECT_IDS : 0;
13160
0
    ctxt->loadsubset |= (options & XML_PARSE_DTDATTR) ? XML_COMPLETE_ATTRS : 0;
13161
0
    ctxt->loadsubset |= (options & XML_PARSE_SKIP_IDS) ? XML_SKIP_IDS : 0;
13162
0
    ctxt->validate = (options & XML_PARSE_DTDVALID) ? 1 : 0;
13163
0
    ctxt->pedantic = (options & XML_PARSE_PEDANTIC) ? 1 : 0;
13164
0
    ctxt->keepBlanks = (options & XML_PARSE_NOBLANKS) ? 0 : 1;
13165
0
    ctxt->dictNames = (options & XML_PARSE_NODICT) ? 0 : 1;
13166
13167
0
    return(options & ~allMask);
13168
0
}
13169
13170
/**
13171
 * Applies the options to the parser context. Unset options are
13172
 * cleared.
13173
 *
13174
 * @since 2.13.0
13175
 *
13176
 * With older versions, you can use #xmlCtxtUseOptions.
13177
 *
13178
 * @param ctxt  an XML parser context
13179
 * @param options  a bitmask of xmlParserOption values
13180
 * @returns 0 in case of success, the set of unknown or unimplemented options
13181
 *         in case of error.
13182
 */
13183
int
13184
xmlCtxtSetOptions(xmlParserCtxt *ctxt, int options)
13185
0
{
13186
0
#ifdef LIBXML_HTML_ENABLED
13187
0
    if ((ctxt != NULL) && (ctxt->html))
13188
0
        return(htmlCtxtSetOptions(ctxt, options));
13189
0
#endif
13190
13191
0
    return(xmlCtxtSetOptionsInternal(ctxt, options, 0));
13192
0
}
13193
13194
/**
13195
 * Get the current options of the parser context.
13196
 *
13197
 * @since 2.14.0
13198
 *
13199
 * @param ctxt  an XML parser context
13200
 * @returns the current options set in the parser context, or -1 if ctxt is NULL.
13201
 */
13202
int
13203
xmlCtxtGetOptions(xmlParserCtxt *ctxt)
13204
0
{
13205
0
    if (ctxt == NULL)
13206
0
        return(-1);
13207
13208
0
    return(ctxt->options);
13209
0
}
13210
13211
/**
13212
 * Applies the options to the parser context. The following options
13213
 * are never cleared and can only be enabled:
13214
 *
13215
 * - XML_PARSE_NOERROR
13216
 * - XML_PARSE_NOWARNING
13217
 * - XML_PARSE_NONET
13218
 * - XML_PARSE_NSCLEAN
13219
 * - XML_PARSE_NOCDATA
13220
 * - XML_PARSE_COMPACT
13221
 * - XML_PARSE_OLD10
13222
 * - XML_PARSE_HUGE
13223
 * - XML_PARSE_OLDSAX
13224
 * - XML_PARSE_IGNORE_ENC
13225
 * - XML_PARSE_BIG_LINES
13226
 *
13227
 * @deprecated Use #xmlCtxtSetOptions.
13228
 *
13229
 * @param ctxt  an XML parser context
13230
 * @param options  a combination of xmlParserOption
13231
 * @returns 0 in case of success, the set of unknown or unimplemented options
13232
 *         in case of error.
13233
 */
13234
int
13235
xmlCtxtUseOptions(xmlParserCtxt *ctxt, int options)
13236
0
{
13237
0
    int keepMask;
13238
13239
0
#ifdef LIBXML_HTML_ENABLED
13240
0
    if ((ctxt != NULL) && (ctxt->html))
13241
0
        return(htmlCtxtUseOptions(ctxt, options));
13242
0
#endif
13243
13244
    /*
13245
     * For historic reasons, some options can only be enabled.
13246
     */
13247
0
    keepMask = XML_PARSE_NOERROR |
13248
0
               XML_PARSE_NOWARNING |
13249
0
               XML_PARSE_NONET |
13250
0
               XML_PARSE_NSCLEAN |
13251
0
               XML_PARSE_NOCDATA |
13252
0
               XML_PARSE_COMPACT |
13253
0
               XML_PARSE_OLD10 |
13254
0
               XML_PARSE_HUGE |
13255
0
               XML_PARSE_OLDSAX |
13256
0
               XML_PARSE_IGNORE_ENC |
13257
0
               XML_PARSE_BIG_LINES;
13258
13259
0
    return(xmlCtxtSetOptionsInternal(ctxt, options, keepMask));
13260
0
}
13261
13262
/**
13263
 * To protect against exponential entity expansion ("billion laughs"), the
13264
 * size of serialized output is (roughly) limited to the input size
13265
 * multiplied by this factor. The default value is 5.
13266
 *
13267
 * When working with documents making heavy use of entity expansion, it can
13268
 * be necessary to increase the value. For security reasons, this should only
13269
 * be considered when processing trusted input.
13270
 *
13271
 * @param ctxt  an XML parser context
13272
 * @param maxAmpl  maximum amplification factor
13273
 */
13274
void
13275
xmlCtxtSetMaxAmplification(xmlParserCtxt *ctxt, unsigned maxAmpl)
13276
0
{
13277
0
    if (ctxt == NULL)
13278
0
        return;
13279
0
    ctxt->maxAmpl = maxAmpl;
13280
0
}
13281
13282
/**
13283
 * Parse an XML document and return the resulting document tree.
13284
 * Takes ownership of the input object.
13285
 *
13286
 * @since 2.13.0
13287
 *
13288
 * @param ctxt  an XML parser context
13289
 * @param input  parser input
13290
 * @returns the resulting document tree or NULL
13291
 */
13292
xmlDoc *
13293
xmlCtxtParseDocument(xmlParserCtxt *ctxt, xmlParserInput *input)
13294
0
{
13295
0
    xmlDocPtr ret = NULL;
13296
13297
0
    if ((ctxt == NULL) || (input == NULL)) {
13298
0
        xmlFatalErr(ctxt, XML_ERR_ARGUMENT, NULL);
13299
0
        xmlFreeInputStream(input);
13300
0
        return(NULL);
13301
0
    }
13302
13303
    /* assert(ctxt->inputNr == 0); */
13304
0
    while (ctxt->inputNr > 0)
13305
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
13306
13307
0
    if (xmlCtxtPushInput(ctxt, input) < 0) {
13308
0
        xmlFreeInputStream(input);
13309
0
        return(NULL);
13310
0
    }
13311
13312
0
    xmlParseDocument(ctxt);
13313
13314
0
    ret = xmlCtxtGetDocument(ctxt);
13315
13316
    /* assert(ctxt->inputNr == 1); */
13317
0
    while (ctxt->inputNr > 0)
13318
0
        xmlFreeInputStream(xmlCtxtPopInput(ctxt));
13319
13320
0
    return(ret);
13321
0
}
13322
13323
/**
13324
 * Convenience function to parse an XML document from a
13325
 * zero-terminated string.
13326
 *
13327
 * See #xmlCtxtReadDoc for details.
13328
 *
13329
 * @param cur  a pointer to a zero terminated string
13330
 * @param URL  base URL (optional)
13331
 * @param encoding  the document encoding (optional)
13332
 * @param options  a combination of xmlParserOption
13333
 * @returns the resulting document tree
13334
 */
13335
xmlDoc *
13336
xmlReadDoc(const xmlChar *cur, const char *URL, const char *encoding,
13337
           int options)
13338
0
{
13339
0
    xmlParserCtxtPtr ctxt;
13340
0
    xmlParserInputPtr input;
13341
0
    xmlDocPtr doc = NULL;
13342
13343
0
    ctxt = xmlNewParserCtxt();
13344
0
    if (ctxt == NULL)
13345
0
        return(NULL);
13346
13347
0
    xmlCtxtUseOptions(ctxt, options);
13348
13349
0
    input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) cur, encoding,
13350
0
                                      XML_INPUT_BUF_STATIC);
13351
13352
0
    if (input != NULL)
13353
0
        doc = xmlCtxtParseDocument(ctxt, input);
13354
13355
0
    xmlFreeParserCtxt(ctxt);
13356
0
    return(doc);
13357
0
}
13358
13359
/**
13360
 * Convenience function to parse an XML file from the filesystem
13361
 * or a global, user-defined resource loader.
13362
 *
13363
 * This function always enables the XML_PARSE_UNZIP option for
13364
 * backward compatibility. If a "-" filename is passed, it will
13365
 * read from stdin. Both of these features are potentially
13366
 * insecure and might be removed from later versions.
13367
 *
13368
 * See #xmlCtxtReadFile for details.
13369
 *
13370
 * @param filename  a file or URL
13371
 * @param encoding  the document encoding (optional)
13372
 * @param options  a combination of xmlParserOption
13373
 * @returns the resulting document tree
13374
 */
13375
xmlDoc *
13376
xmlReadFile(const char *filename, const char *encoding, int options)
13377
0
{
13378
0
    xmlParserCtxtPtr ctxt;
13379
0
    xmlParserInputPtr input;
13380
0
    xmlDocPtr doc = NULL;
13381
13382
0
    ctxt = xmlNewParserCtxt();
13383
0
    if (ctxt == NULL)
13384
0
        return(NULL);
13385
13386
0
    options |= XML_PARSE_UNZIP;
13387
13388
0
    xmlCtxtUseOptions(ctxt, options);
13389
13390
    /*
13391
     * Backward compatibility for users of command line utilities like
13392
     * xmlstarlet expecting "-" to mean stdin. This is dangerous and
13393
     * should be removed at some point.
13394
     */
13395
0
    if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0))
13396
0
        input = xmlCtxtNewInputFromFd(ctxt, filename, STDIN_FILENO,
13397
0
                                      encoding, 0);
13398
0
    else
13399
0
        input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0);
13400
13401
0
    if (input != NULL)
13402
0
        doc = xmlCtxtParseDocument(ctxt, input);
13403
13404
0
    xmlFreeParserCtxt(ctxt);
13405
0
    return(doc);
13406
0
}
13407
13408
/**
13409
 * Parse an XML in-memory document and build a tree. The input buffer must
13410
 * not contain a terminating null byte.
13411
 *
13412
 * See #xmlCtxtReadMemory for details.
13413
 *
13414
 * @param buffer  a pointer to a char array
13415
 * @param size  the size of the array
13416
 * @param url  base URL (optional)
13417
 * @param encoding  the document encoding (optional)
13418
 * @param options  a combination of xmlParserOption
13419
 * @returns the resulting document tree
13420
 */
13421
xmlDoc *
13422
xmlReadMemory(const char *buffer, int size, const char *url,
13423
              const char *encoding, int options)
13424
0
{
13425
0
    xmlParserCtxtPtr ctxt;
13426
0
    xmlParserInputPtr input;
13427
0
    xmlDocPtr doc = NULL;
13428
13429
0
    if (size < 0)
13430
0
  return(NULL);
13431
13432
0
    ctxt = xmlNewParserCtxt();
13433
0
    if (ctxt == NULL)
13434
0
        return(NULL);
13435
13436
0
    xmlCtxtUseOptions(ctxt, options);
13437
13438
0
    input = xmlCtxtNewInputFromMemory(ctxt, url, buffer, size, encoding,
13439
0
                                      XML_INPUT_BUF_STATIC);
13440
13441
0
    if (input != NULL)
13442
0
        doc = xmlCtxtParseDocument(ctxt, input);
13443
13444
0
    xmlFreeParserCtxt(ctxt);
13445
0
    return(doc);
13446
0
}
13447
13448
/**
13449
 * Parse an XML from a file descriptor and build a tree.
13450
 *
13451
 * See #xmlCtxtReadFd for details.
13452
 *
13453
 * NOTE that the file descriptor will not be closed when the
13454
 * context is freed or reset.
13455
 *
13456
 * @param fd  an open file descriptor
13457
 * @param URL  base URL (optional)
13458
 * @param encoding  the document encoding (optional)
13459
 * @param options  a combination of xmlParserOption
13460
 * @returns the resulting document tree
13461
 */
13462
xmlDoc *
13463
xmlReadFd(int fd, const char *URL, const char *encoding, int options)
13464
0
{
13465
0
    xmlParserCtxtPtr ctxt;
13466
0
    xmlParserInputPtr input;
13467
0
    xmlDocPtr doc = NULL;
13468
13469
0
    ctxt = xmlNewParserCtxt();
13470
0
    if (ctxt == NULL)
13471
0
        return(NULL);
13472
13473
0
    xmlCtxtUseOptions(ctxt, options);
13474
13475
0
    input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0);
13476
13477
0
    if (input != NULL)
13478
0
        doc = xmlCtxtParseDocument(ctxt, input);
13479
13480
0
    xmlFreeParserCtxt(ctxt);
13481
0
    return(doc);
13482
0
}
13483
13484
/**
13485
 * Parse an XML document from I/O functions and context and build a tree.
13486
 *
13487
 * See #xmlCtxtReadIO for details.
13488
 *
13489
 * @param ioread  an I/O read function
13490
 * @param ioclose  an I/O close function (optional)
13491
 * @param ioctx  an I/O handler
13492
 * @param URL  base URL (optional)
13493
 * @param encoding  the document encoding (optional)
13494
 * @param options  a combination of xmlParserOption
13495
 * @returns the resulting document tree
13496
 */
13497
xmlDoc *
13498
xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
13499
          void *ioctx, const char *URL, const char *encoding, int options)
13500
0
{
13501
0
    xmlParserCtxtPtr ctxt;
13502
0
    xmlParserInputPtr input;
13503
0
    xmlDocPtr doc = NULL;
13504
13505
0
    ctxt = xmlNewParserCtxt();
13506
0
    if (ctxt == NULL)
13507
0
        return(NULL);
13508
13509
0
    xmlCtxtUseOptions(ctxt, options);
13510
13511
0
    input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx,
13512
0
                                  encoding, 0);
13513
13514
0
    if (input != NULL)
13515
0
        doc = xmlCtxtParseDocument(ctxt, input);
13516
13517
0
    xmlFreeParserCtxt(ctxt);
13518
0
    return(doc);
13519
0
}
13520
13521
/**
13522
 * Parse an XML in-memory document and build a tree.
13523
 *
13524
 * `URL` is used as base to resolve external entities and for error
13525
 * reporting.
13526
 *
13527
 * @param ctxt  an XML parser context
13528
 * @param str  a pointer to a zero terminated string
13529
 * @param URL  base URL (optional)
13530
 * @param encoding  the document encoding (optional)
13531
 * @param options  a combination of xmlParserOption
13532
 * @returns the resulting document tree
13533
 */
13534
xmlDoc *
13535
xmlCtxtReadDoc(xmlParserCtxt *ctxt, const xmlChar *str,
13536
               const char *URL, const char *encoding, int options)
13537
0
{
13538
0
    xmlParserInputPtr input;
13539
13540
0
    if (ctxt == NULL)
13541
0
        return(NULL);
13542
13543
0
    xmlCtxtReset(ctxt);
13544
0
    xmlCtxtUseOptions(ctxt, options);
13545
13546
0
    input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) str, encoding,
13547
0
                                      XML_INPUT_BUF_STATIC);
13548
0
    if (input == NULL)
13549
0
        return(NULL);
13550
13551
0
    return(xmlCtxtParseDocument(ctxt, input));
13552
0
}
13553
13554
/**
13555
 * Parse an XML file from the filesystem or a global, user-defined
13556
 * resource loader.
13557
 *
13558
 * This function always enables the XML_PARSE_UNZIP option for
13559
 * backward compatibility. This feature is potentially insecure
13560
 * and might be removed from later versions.
13561
 *
13562
 * @param ctxt  an XML parser context
13563
 * @param filename  a file or URL
13564
 * @param encoding  the document encoding (optional)
13565
 * @param options  a combination of xmlParserOption
13566
 * @returns the resulting document tree
13567
 */
13568
xmlDoc *
13569
xmlCtxtReadFile(xmlParserCtxt *ctxt, const char *filename,
13570
                const char *encoding, int options)
13571
0
{
13572
0
    xmlParserInputPtr input;
13573
13574
0
    if (ctxt == NULL)
13575
0
        return(NULL);
13576
13577
0
    options |= XML_PARSE_UNZIP;
13578
13579
0
    xmlCtxtReset(ctxt);
13580
0
    xmlCtxtUseOptions(ctxt, options);
13581
13582
0
    input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0);
13583
0
    if (input == NULL)
13584
0
        return(NULL);
13585
13586
0
    return(xmlCtxtParseDocument(ctxt, input));
13587
0
}
13588
13589
/**
13590
 * Parse an XML in-memory document and build a tree. The input buffer must
13591
 * not contain a terminating null byte.
13592
 *
13593
 * `URL` is used as base to resolve external entities and for error
13594
 * reporting.
13595
 *
13596
 * @param ctxt  an XML parser context
13597
 * @param buffer  a pointer to a char array
13598
 * @param size  the size of the array
13599
 * @param URL  base URL (optional)
13600
 * @param encoding  the document encoding (optional)
13601
 * @param options  a combination of xmlParserOption
13602
 * @returns the resulting document tree
13603
 */
13604
xmlDoc *
13605
xmlCtxtReadMemory(xmlParserCtxt *ctxt, const char *buffer, int size,
13606
                  const char *URL, const char *encoding, int options)
13607
0
{
13608
0
    xmlParserInputPtr input;
13609
13610
0
    if ((ctxt == NULL) || (size < 0))
13611
0
        return(NULL);
13612
13613
0
    xmlCtxtReset(ctxt);
13614
0
    xmlCtxtUseOptions(ctxt, options);
13615
13616
0
    input = xmlCtxtNewInputFromMemory(ctxt, URL, buffer, size, encoding,
13617
0
                                      XML_INPUT_BUF_STATIC);
13618
0
    if (input == NULL)
13619
0
        return(NULL);
13620
13621
0
    return(xmlCtxtParseDocument(ctxt, input));
13622
0
}
13623
13624
/**
13625
 * Parse an XML document from a file descriptor and build a tree.
13626
 *
13627
 * NOTE that the file descriptor will not be closed when the
13628
 * context is freed or reset.
13629
 *
13630
 * `URL` is used as base to resolve external entities and for error
13631
 * reporting.
13632
 *
13633
 * @param ctxt  an XML parser context
13634
 * @param fd  an open file descriptor
13635
 * @param URL  base URL (optional)
13636
 * @param encoding  the document encoding (optional)
13637
 * @param options  a combination of xmlParserOption
13638
 * @returns the resulting document tree
13639
 */
13640
xmlDoc *
13641
xmlCtxtReadFd(xmlParserCtxt *ctxt, int fd,
13642
              const char *URL, const char *encoding, int options)
13643
0
{
13644
0
    xmlParserInputPtr input;
13645
13646
0
    if (ctxt == NULL)
13647
0
        return(NULL);
13648
13649
0
    xmlCtxtReset(ctxt);
13650
0
    xmlCtxtUseOptions(ctxt, options);
13651
13652
0
    input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0);
13653
0
    if (input == NULL)
13654
0
        return(NULL);
13655
13656
0
    return(xmlCtxtParseDocument(ctxt, input));
13657
0
}
13658
13659
/**
13660
 * Parse an XML document from I/O functions and source and build a tree.
13661
 * This reuses the existing `ctxt` parser context
13662
 *
13663
 * `URL` is used as base to resolve external entities and for error
13664
 * reporting.
13665
 *
13666
 * @param ctxt  an XML parser context
13667
 * @param ioread  an I/O read function
13668
 * @param ioclose  an I/O close function
13669
 * @param ioctx  an I/O handler
13670
 * @param URL  the base URL to use for the document
13671
 * @param encoding  the document encoding, or NULL
13672
 * @param options  a combination of xmlParserOption
13673
 * @returns the resulting document tree
13674
 */
13675
xmlDoc *
13676
xmlCtxtReadIO(xmlParserCtxt *ctxt, xmlInputReadCallback ioread,
13677
              xmlInputCloseCallback ioclose, void *ioctx,
13678
        const char *URL,
13679
              const char *encoding, int options)
13680
0
{
13681
0
    xmlParserInputPtr input;
13682
13683
0
    if (ctxt == NULL)
13684
0
        return(NULL);
13685
13686
0
    xmlCtxtReset(ctxt);
13687
0
    xmlCtxtUseOptions(ctxt, options);
13688
13689
0
    input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx,
13690
0
                                  encoding, 0);
13691
0
    if (input == NULL)
13692
0
        return(NULL);
13693
13694
0
    return(xmlCtxtParseDocument(ctxt, input));
13695
0
}
13696