Coverage Report

Created: 2023-06-07 06:14

/src/libxml2/parserInternals.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * parserInternals.c : Internal routines (and obsolete ones) needed for the
3
 *                     XML and HTML parsers.
4
 *
5
 * See Copyright for the status of this software.
6
 *
7
 * daniel@veillard.com
8
 */
9
10
#define IN_LIBXML
11
#include "libxml.h"
12
13
#if defined(_WIN32)
14
#define XML_DIR_SEP '\\'
15
#else
16
#define XML_DIR_SEP '/'
17
#endif
18
19
#include <string.h>
20
#include <ctype.h>
21
#include <stdlib.h>
22
23
#include <libxml/xmlmemory.h>
24
#include <libxml/tree.h>
25
#include <libxml/parser.h>
26
#include <libxml/parserInternals.h>
27
#include <libxml/valid.h>
28
#include <libxml/entities.h>
29
#include <libxml/xmlerror.h>
30
#include <libxml/encoding.h>
31
#include <libxml/valid.h>
32
#include <libxml/xmlIO.h>
33
#include <libxml/uri.h>
34
#include <libxml/dict.h>
35
#include <libxml/SAX.h>
36
#ifdef LIBXML_CATALOG_ENABLED
37
#include <libxml/catalog.h>
38
#endif
39
#include <libxml/globals.h>
40
#include <libxml/chvalid.h>
41
42
5.33M
#define CUR(ctxt) ctxt->input->cur
43
5.33M
#define END(ctxt) ctxt->input->end
44
5.33M
#define VALID_CTXT(ctxt) (CUR(ctxt) <= END(ctxt))
45
46
#include "private/buf.h"
47
#include "private/enc.h"
48
#include "private/error.h"
49
#include "private/io.h"
50
#include "private/parser.h"
51
52
/*
53
 * Various global defaults for parsing
54
 */
55
56
/**
57
 * xmlCheckVersion:
58
 * @version: the include version number
59
 *
60
 * check the compiled lib version against the include one.
61
 * This can warn or immediately kill the application
62
 */
63
void
64
0
xmlCheckVersion(int version) {
65
0
    int myversion = LIBXML_VERSION;
66
67
0
    xmlInitParser();
68
69
0
    if ((myversion / 10000) != (version / 10000)) {
70
0
  xmlGenericError(xmlGenericErrorContext,
71
0
    "Fatal: program compiled against libxml %d using libxml %d\n",
72
0
    (version / 10000), (myversion / 10000));
73
0
  fprintf(stderr,
74
0
    "Fatal: program compiled against libxml %d using libxml %d\n",
75
0
    (version / 10000), (myversion / 10000));
76
0
    }
77
0
    if ((myversion / 100) < (version / 100)) {
78
0
  xmlGenericError(xmlGenericErrorContext,
79
0
    "Warning: program compiled against libxml %d using older %d\n",
80
0
    (version / 100), (myversion / 100));
81
0
    }
82
0
}
83
84
85
/************************************************************************
86
 *                  *
87
 *    Some factorized error routines        *
88
 *                  *
89
 ************************************************************************/
90
91
92
/**
93
 * xmlErrMemory:
94
 * @ctxt:  an XML parser context
95
 * @extra:  extra information
96
 *
97
 * Handle a redefinition of attribute error
98
 */
99
void
100
xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
101
0
{
102
0
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
103
0
        (ctxt->instate == XML_PARSER_EOF))
104
0
  return;
105
0
    if (ctxt != NULL) {
106
0
        ctxt->errNo = XML_ERR_NO_MEMORY;
107
0
        ctxt->instate = XML_PARSER_EOF;
108
0
        ctxt->disableSAX = 1;
109
0
    }
110
0
    if (extra)
111
0
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
112
0
                        XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
113
0
                        NULL, NULL, 0, 0,
114
0
                        "Memory allocation failed : %s\n", extra);
115
0
    else
116
0
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
117
0
                        XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
118
0
                        NULL, NULL, 0, 0, "Memory allocation failed\n");
119
0
}
120
121
/**
122
 * __xmlErrEncoding:
123
 * @ctxt:  an XML parser context
124
 * @xmlerr:  the error number
125
 * @msg:  the error message
126
 * @str1:  an string info
127
 * @str2:  an string info
128
 *
129
 * Handle an encoding error
130
 */
131
void
132
__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
133
                 const char *msg, const xmlChar * str1, const xmlChar * str2)
134
235
{
135
235
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
136
235
        (ctxt->instate == XML_PARSER_EOF))
137
0
  return;
138
235
    if (ctxt != NULL)
139
235
        ctxt->errNo = xmlerr;
140
235
    __xmlRaiseError(NULL, NULL, NULL,
141
235
                    ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
142
235
                    NULL, 0, (const char *) str1, (const char *) str2,
143
235
                    NULL, 0, 0, msg, str1, str2);
144
235
    if (ctxt != NULL) {
145
235
        ctxt->wellFormed = 0;
146
235
        if (ctxt->recovery == 0)
147
0
            ctxt->disableSAX = 1;
148
235
    }
149
235
}
150
151
/**
152
 * xmlErrInternal:
153
 * @ctxt:  an XML parser context
154
 * @msg:  the error message
155
 * @str:  error information
156
 *
157
 * Handle an internal error
158
 */
159
static void LIBXML_ATTR_FORMAT(2,0)
160
xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
161
0
{
162
0
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
163
0
        (ctxt->instate == XML_PARSER_EOF))
164
0
  return;
165
0
    if (ctxt != NULL)
166
0
        ctxt->errNo = XML_ERR_INTERNAL_ERROR;
167
0
    __xmlRaiseError(NULL, NULL, NULL,
168
0
                    ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
169
0
                    XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
170
0
                    0, 0, msg, str);
171
0
    if (ctxt != NULL) {
172
0
        ctxt->wellFormed = 0;
173
0
        if (ctxt->recovery == 0)
174
0
            ctxt->disableSAX = 1;
175
0
    }
176
0
}
177
178
/**
179
 * xmlFatalErr:
180
 * @ctxt:  an XML parser context
181
 * @error:  the error number
182
 * @extra:  extra information string
183
 *
184
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
185
 */
186
void
187
xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
188
1.33M
{
189
1.33M
    const char *errmsg;
190
191
1.33M
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
192
1.33M
        (ctxt->instate == XML_PARSER_EOF))
193
41
  return;
194
1.33M
    switch (error) {
195
5.13k
        case XML_ERR_INVALID_HEX_CHARREF:
196
5.13k
            errmsg = "CharRef: invalid hexadecimal value";
197
5.13k
            break;
198
1.98k
        case XML_ERR_INVALID_DEC_CHARREF:
199
1.98k
            errmsg = "CharRef: invalid decimal value";
200
1.98k
            break;
201
0
        case XML_ERR_INVALID_CHARREF:
202
0
            errmsg = "CharRef: invalid value";
203
0
            break;
204
1.31M
        case XML_ERR_INTERNAL_ERROR:
205
1.31M
            errmsg = "internal error";
206
1.31M
            break;
207
0
        case XML_ERR_PEREF_AT_EOF:
208
0
            errmsg = "PEReference at end of document";
209
0
            break;
210
0
        case XML_ERR_PEREF_IN_PROLOG:
211
0
            errmsg = "PEReference in prolog";
212
0
            break;
213
0
        case XML_ERR_PEREF_IN_EPILOG:
214
0
            errmsg = "PEReference in epilog";
215
0
            break;
216
0
        case XML_ERR_PEREF_NO_NAME:
217
0
            errmsg = "PEReference: no name";
218
0
            break;
219
0
        case XML_ERR_PEREF_SEMICOL_MISSING:
220
0
            errmsg = "PEReference: expecting ';'";
221
0
            break;
222
0
        case XML_ERR_ENTITY_LOOP:
223
0
            errmsg = "Detected an entity reference loop";
224
0
            break;
225
0
        case XML_ERR_ENTITY_NOT_STARTED:
226
0
            errmsg = "EntityValue: \" or ' expected";
227
0
            break;
228
0
        case XML_ERR_ENTITY_PE_INTERNAL:
229
0
            errmsg = "PEReferences forbidden in internal subset";
230
0
            break;
231
10
        case XML_ERR_ENTITY_NOT_FINISHED:
232
10
            errmsg = "EntityValue: \" or ' expected";
233
10
            break;
234
1.55k
        case XML_ERR_ATTRIBUTE_NOT_STARTED:
235
1.55k
            errmsg = "AttValue: \" or ' expected";
236
1.55k
            break;
237
5.49k
        case XML_ERR_LT_IN_ATTRIBUTE:
238
5.49k
            errmsg = "Unescaped '<' not allowed in attributes values";
239
5.49k
            break;
240
57
        case XML_ERR_LITERAL_NOT_STARTED:
241
57
            errmsg = "SystemLiteral \" or ' expected";
242
57
            break;
243
1
        case XML_ERR_LITERAL_NOT_FINISHED:
244
1
            errmsg = "Unfinished System or Public ID \" or ' expected";
245
1
            break;
246
287
        case XML_ERR_MISPLACED_CDATA_END:
247
287
            errmsg = "Sequence ']]>' not allowed in content";
248
287
            break;
249
57
        case XML_ERR_URI_REQUIRED:
250
57
            errmsg = "SYSTEM or PUBLIC, the URI is missing";
251
57
            break;
252
2
        case XML_ERR_PUBID_REQUIRED:
253
2
            errmsg = "PUBLIC, the Public Identifier is missing";
254
2
            break;
255
290
        case XML_ERR_HYPHEN_IN_COMMENT:
256
290
            errmsg = "Comment must not contain '--' (double-hyphen)";
257
290
            break;
258
511
        case XML_ERR_PI_NOT_STARTED:
259
511
            errmsg = "xmlParsePI : no target name";
260
511
            break;
261
1.40k
        case XML_ERR_RESERVED_XML_NAME:
262
1.40k
            errmsg = "Invalid PI name";
263
1.40k
            break;
264
0
        case XML_ERR_NOTATION_NOT_STARTED:
265
0
            errmsg = "NOTATION: Name expected here";
266
0
            break;
267
0
        case XML_ERR_NOTATION_NOT_FINISHED:
268
0
            errmsg = "'>' required to close NOTATION declaration";
269
0
            break;
270
0
        case XML_ERR_VALUE_REQUIRED:
271
0
            errmsg = "Entity value required";
272
0
            break;
273
919
        case XML_ERR_URI_FRAGMENT:
274
919
            errmsg = "Fragment not allowed";
275
919
            break;
276
2
        case XML_ERR_ATTLIST_NOT_STARTED:
277
2
            errmsg = "'(' required to start ATTLIST enumeration";
278
2
            break;
279
0
        case XML_ERR_NMTOKEN_REQUIRED:
280
0
            errmsg = "NmToken expected in ATTLIST enumeration";
281
0
            break;
282
264
        case XML_ERR_ATTLIST_NOT_FINISHED:
283
264
            errmsg = "')' required to finish ATTLIST enumeration";
284
264
            break;
285
0
        case XML_ERR_MIXED_NOT_STARTED:
286
0
            errmsg = "MixedContentDecl : '|' or ')*' expected";
287
0
            break;
288
0
        case XML_ERR_PCDATA_REQUIRED:
289
0
            errmsg = "MixedContentDecl : '#PCDATA' expected";
290
0
            break;
291
0
        case XML_ERR_ELEMCONTENT_NOT_STARTED:
292
0
            errmsg = "ContentDecl : Name or '(' expected";
293
0
            break;
294
0
        case XML_ERR_ELEMCONTENT_NOT_FINISHED:
295
0
            errmsg = "ContentDecl : ',' '|' or ')' expected";
296
0
            break;
297
0
        case XML_ERR_PEREF_IN_INT_SUBSET:
298
0
            errmsg =
299
0
                "PEReference: forbidden within markup decl in internal subset";
300
0
            break;
301
1.82k
        case XML_ERR_GT_REQUIRED:
302
1.82k
            errmsg = "expected '>'";
303
1.82k
            break;
304
0
        case XML_ERR_CONDSEC_INVALID:
305
0
            errmsg = "XML conditional section '[' expected";
306
0
            break;
307
0
        case XML_ERR_EXT_SUBSET_NOT_FINISHED:
308
0
            errmsg = "Content error in the external subset";
309
0
            break;
310
0
        case XML_ERR_CONDSEC_INVALID_KEYWORD:
311
0
            errmsg =
312
0
                "conditional section INCLUDE or IGNORE keyword expected";
313
0
            break;
314
0
        case XML_ERR_CONDSEC_NOT_FINISHED:
315
0
            errmsg = "XML conditional section not closed";
316
0
            break;
317
0
        case XML_ERR_XMLDECL_NOT_STARTED:
318
0
            errmsg = "Text declaration '<?xml' required";
319
0
            break;
320
96
        case XML_ERR_XMLDECL_NOT_FINISHED:
321
96
            errmsg = "parsing XML declaration: '?>' expected";
322
96
            break;
323
0
        case XML_ERR_EXT_ENTITY_STANDALONE:
324
0
            errmsg = "external parsed entities cannot be standalone";
325
0
            break;
326
4.29k
        case XML_ERR_ENTITYREF_SEMICOL_MISSING:
327
4.29k
            errmsg = "EntityRef: expecting ';'";
328
4.29k
            break;
329
2
        case XML_ERR_DOCTYPE_NOT_FINISHED:
330
2
            errmsg = "DOCTYPE improperly terminated";
331
2
            break;
332
0
        case XML_ERR_LTSLASH_REQUIRED:
333
0
            errmsg = "EndTag: '</' not found";
334
0
            break;
335
0
        case XML_ERR_EQUAL_REQUIRED:
336
0
            errmsg = "expected '='";
337
0
            break;
338
6
        case XML_ERR_STRING_NOT_CLOSED:
339
6
            errmsg = "String not closed expecting \" or '";
340
6
            break;
341
0
        case XML_ERR_STRING_NOT_STARTED:
342
0
            errmsg = "String not started expecting ' or \"";
343
0
            break;
344
0
        case XML_ERR_ENCODING_NAME:
345
0
            errmsg = "Invalid XML encoding name";
346
0
            break;
347
0
        case XML_ERR_STANDALONE_VALUE:
348
0
            errmsg = "standalone accepts only 'yes' or 'no'";
349
0
            break;
350
1
        case XML_ERR_DOCUMENT_EMPTY:
351
1
            errmsg = "Document is empty";
352
1
            break;
353
1
        case XML_ERR_DOCUMENT_END:
354
1
            errmsg = "Extra content at the end of the document";
355
1
            break;
356
2
        case XML_ERR_NOT_WELL_BALANCED:
357
2
            errmsg = "chunk is not well balanced";
358
2
            break;
359
0
        case XML_ERR_EXTRA_CONTENT:
360
0
            errmsg = "extra content at the end of well balanced chunk";
361
0
            break;
362
91
        case XML_ERR_VERSION_MISSING:
363
91
            errmsg = "Malformed declaration expecting version";
364
91
            break;
365
142
        case XML_ERR_NAME_TOO_LONG:
366
142
            errmsg = "Name too long";
367
142
            break;
368
19
        case XML_ERR_INVALID_ENCODING:
369
19
            errmsg = "Invalid bytes in character encoding";
370
19
            break;
371
0
        case XML_IO_UNKNOWN:
372
0
            errmsg = "I/O error";
373
0
            break;
374
#if 0
375
        case:
376
            errmsg = "";
377
            break;
378
#endif
379
4
        default:
380
4
            errmsg = "Unregistered error message";
381
1.33M
    }
382
1.33M
    if (ctxt != NULL)
383
1.33M
  ctxt->errNo = error;
384
1.33M
    if (info == NULL) {
385
24.3k
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
386
24.3k
                        XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, "%s\n",
387
24.3k
                        errmsg);
388
1.31M
    } else {
389
1.31M
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
390
1.31M
                        XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, "%s: %s\n",
391
1.31M
                        errmsg, info);
392
1.31M
    }
393
1.33M
    if (ctxt != NULL) {
394
1.33M
  ctxt->wellFormed = 0;
395
1.33M
  if (ctxt->recovery == 0)
396
19
      ctxt->disableSAX = 1;
397
1.33M
    }
398
1.33M
}
399
400
/**
401
 * xmlErrEncodingInt:
402
 * @ctxt:  an XML parser context
403
 * @error:  the error number
404
 * @msg:  the error message
405
 * @val:  an integer value
406
 *
407
 * n encoding error
408
 */
409
static void LIBXML_ATTR_FORMAT(3,0)
410
xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
411
                  const char *msg, int val)
412
41.8k
{
413
41.8k
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
414
41.8k
        (ctxt->instate == XML_PARSER_EOF))
415
0
  return;
416
41.8k
    if (ctxt != NULL)
417
41.8k
        ctxt->errNo = error;
418
41.8k
    __xmlRaiseError(NULL, NULL, NULL,
419
41.8k
                    ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
420
41.8k
                    NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
421
41.8k
    if (ctxt != NULL) {
422
41.8k
        ctxt->wellFormed = 0;
423
41.8k
        if (ctxt->recovery == 0)
424
0
            ctxt->disableSAX = 1;
425
41.8k
    }
426
41.8k
}
427
428
/**
429
 * xmlIsLetter:
430
 * @c:  an unicode character (int)
431
 *
432
 * Check whether the character is allowed by the production
433
 * [84] Letter ::= BaseChar | Ideographic
434
 *
435
 * Returns 0 if not, non-zero otherwise
436
 */
437
int
438
0
xmlIsLetter(int c) {
439
0
    return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
440
0
}
441
442
/************************************************************************
443
 *                  *
444
 *    Input handling functions for progressive parsing  *
445
 *                  *
446
 ************************************************************************/
447
448
/* #define DEBUG_INPUT */
449
/* #define DEBUG_STACK */
450
/* #define DEBUG_PUSH */
451
452
453
/* we need to keep enough input to show errors in context */
454
120
#define LINE_LEN        80
455
456
#ifdef DEBUG_INPUT
457
#define CHECK_BUFFER(in) check_buffer(in)
458
459
static
460
void check_buffer(xmlParserInputPtr in) {
461
    if (in->base != xmlBufContent(in->buf->buffer)) {
462
        xmlGenericError(xmlGenericErrorContext,
463
    "xmlParserInput: base mismatch problem\n");
464
    }
465
    if (in->cur < in->base) {
466
        xmlGenericError(xmlGenericErrorContext,
467
    "xmlParserInput: cur < base problem\n");
468
    }
469
    if (in->cur > in->base + xmlBufUse(in->buf->buffer)) {
470
        xmlGenericError(xmlGenericErrorContext,
471
    "xmlParserInput: cur > base + use problem\n");
472
    }
473
    xmlGenericError(xmlGenericErrorContext,"buffer %p : content %x, cur %d, use %d\n",
474
            (void *) in, (int) xmlBufContent(in->buf->buffer),
475
            in->cur - in->base, xmlBufUse(in->buf->buffer));
476
}
477
478
#else
479
#define CHECK_BUFFER(in)
480
#endif
481
482
483
/**
484
 * xmlHaltParser:
485
 * @ctxt:  an XML parser context
486
 *
487
 * Blocks further parser processing don't override error
488
 * for internal use
489
 */
490
void
491
82
xmlHaltParser(xmlParserCtxtPtr ctxt) {
492
82
    if (ctxt == NULL)
493
0
        return;
494
82
    ctxt->instate = XML_PARSER_EOF;
495
82
    ctxt->disableSAX = 1;
496
82
    while (ctxt->inputNr > 1)
497
0
        xmlFreeInputStream(inputPop(ctxt));
498
82
    if (ctxt->input != NULL) {
499
        /*
500
   * in case there was a specific allocation deallocate before
501
   * overriding base
502
   */
503
82
        if (ctxt->input->free != NULL) {
504
0
      ctxt->input->free((xmlChar *) ctxt->input->base);
505
0
      ctxt->input->free = NULL;
506
0
  }
507
82
        if (ctxt->input->buf != NULL) {
508
82
            xmlFreeParserInputBuffer(ctxt->input->buf);
509
82
            ctxt->input->buf = NULL;
510
82
        }
511
82
  ctxt->input->cur = BAD_CAST"";
512
82
        ctxt->input->length = 0;
513
82
  ctxt->input->base = ctxt->input->cur;
514
82
        ctxt->input->end = ctxt->input->cur;
515
82
    }
516
82
}
517
518
/**
519
 * xmlParserInputRead:
520
 * @in:  an XML parser input
521
 * @len:  an indicative size for the lookahead
522
 *
523
 * DEPRECATED: This function was internal and is deprecated.
524
 *
525
 * Returns -1 as this is an error to use it.
526
 */
527
int
528
0
xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) {
529
0
    return(-1);
530
0
}
531
532
/**
533
 * xmlParserGrow:
534
 * @ctxt:  an XML parser context
535
 */
536
int
537
46.1k
xmlParserGrow(xmlParserCtxtPtr ctxt) {
538
46.1k
    xmlParserInputPtr in = ctxt->input;
539
46.1k
    xmlParserInputBufferPtr buf = in->buf;
540
46.1k
    ptrdiff_t curEnd = in->end - in->cur;
541
46.1k
    ptrdiff_t curBase = in->cur - in->base;
542
46.1k
    int ret;
543
544
46.1k
    if (buf == NULL)
545
101
        return(0);
546
    /* Don't grow push parser buffer. */
547
46.0k
    if (ctxt->progressive)
548
0
        return(0);
549
    /* Don't grow memory buffers. */
550
46.0k
    if ((buf->encoder == NULL) && (buf->readcallback == NULL))
551
38.1k
        return(0);
552
553
7.86k
    if (((curEnd > XML_MAX_LOOKUP_LIMIT) ||
554
7.86k
         (curBase > XML_MAX_LOOKUP_LIMIT)) &&
555
7.86k
        ((ctxt->options & XML_PARSE_HUGE) == 0)) {
556
0
        xmlErrMemory(ctxt, "Huge input lookup");
557
0
        xmlHaltParser(ctxt);
558
0
  return(-1);
559
0
    }
560
561
7.86k
    if (curEnd >= INPUT_CHUNK)
562
1
        return(0);
563
564
7.86k
    ret = xmlParserInputBufferGrow(buf, INPUT_CHUNK);
565
7.86k
    xmlBufSetInputBaseCur(buf->buffer, in, 0, curBase);
566
567
7.86k
    if (ret < 0) {
568
19
        xmlFatalErr(ctxt, buf->error, NULL);
569
19
        xmlHaltParser(ctxt);
570
19
    }
571
572
7.86k
    return(ret);
573
7.86k
}
574
575
/**
576
 * xmlParserInputGrow:
577
 * @in:  an XML parser input
578
 * @len:  an indicative size for the lookahead
579
 *
580
 * DEPRECATED: Don't use.
581
 *
582
 * This function increase the input for the parser. It tries to
583
 * preserve pointers to the input buffer, and keep already read data
584
 *
585
 * Returns the amount of char read, or -1 in case of error, 0 indicate the
586
 * end of this entity
587
 */
588
int
589
0
xmlParserInputGrow(xmlParserInputPtr in, int len) {
590
0
    int ret;
591
0
    size_t indx;
592
593
0
    if ((in == NULL) || (len < 0)) return(-1);
594
#ifdef DEBUG_INPUT
595
    xmlGenericError(xmlGenericErrorContext, "Grow\n");
596
#endif
597
0
    if (in->buf == NULL) return(-1);
598
0
    if (in->base == NULL) return(-1);
599
0
    if (in->cur == NULL) return(-1);
600
0
    if (in->buf->buffer == NULL) return(-1);
601
602
    /* Don't grow memory buffers. */
603
0
    if ((in->buf->encoder == NULL) && (in->buf->readcallback == NULL))
604
0
        return(0);
605
606
0
    CHECK_BUFFER(in);
607
608
0
    indx = in->cur - in->base;
609
0
    if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) {
610
611
0
  CHECK_BUFFER(in);
612
613
0
        return(0);
614
0
    }
615
0
    ret = xmlParserInputBufferGrow(in->buf, len);
616
617
0
    in->base = xmlBufContent(in->buf->buffer);
618
0
    if (in->base == NULL) {
619
0
        in->base = BAD_CAST "";
620
0
        in->cur = in->base;
621
0
        in->end = in->base;
622
0
        return(-1);
623
0
    }
624
0
    in->cur = in->base + indx;
625
0
    in->end = xmlBufEnd(in->buf->buffer);
626
627
0
    CHECK_BUFFER(in);
628
629
0
    return(ret);
630
0
}
631
632
/**
633
 * xmlParserShrink:
634
 * @ctxt:  an XML parser context
635
 */
636
void
637
4.52k
xmlParserShrink(xmlParserCtxtPtr ctxt) {
638
4.52k
    xmlParserInputPtr in = ctxt->input;
639
4.52k
    xmlParserInputBufferPtr buf = in->buf;
640
4.52k
    size_t used;
641
642
    /* Don't shrink pull parser memory buffers. */
643
4.52k
    if ((buf == NULL) ||
644
4.52k
        ((ctxt->progressive == 0) &&
645
4.52k
         (buf->encoder == NULL) && (buf->readcallback == NULL)))
646
4.40k
        return;
647
648
120
    used = in->cur - in->base;
649
    /*
650
     * Do not shrink on large buffers whose only a tiny fraction
651
     * was consumed
652
     */
653
120
    if (used > INPUT_CHUNK) {
654
120
  size_t res = xmlBufShrink(buf->buffer, used - LINE_LEN);
655
656
120
  if (res > 0) {
657
120
            used -= res;
658
120
            if ((res > ULONG_MAX) ||
659
120
                (in->consumed > ULONG_MAX - (unsigned long)res))
660
0
                in->consumed = ULONG_MAX;
661
120
            else
662
120
                in->consumed += res;
663
120
  }
664
120
    }
665
666
120
    xmlBufSetInputBaseCur(buf->buffer, in, 0, used);
667
120
}
668
669
/**
670
 * xmlParserInputShrink:
671
 * @in:  an XML parser input
672
 *
673
 * DEPRECATED: Don't use.
674
 *
675
 * This function removes used input for the parser.
676
 */
677
void
678
0
xmlParserInputShrink(xmlParserInputPtr in) {
679
0
    size_t used;
680
0
    size_t ret;
681
682
#ifdef DEBUG_INPUT
683
    xmlGenericError(xmlGenericErrorContext, "Shrink\n");
684
#endif
685
0
    if (in == NULL) return;
686
0
    if (in->buf == NULL) return;
687
0
    if (in->base == NULL) return;
688
0
    if (in->cur == NULL) return;
689
0
    if (in->buf->buffer == NULL) return;
690
691
0
    CHECK_BUFFER(in);
692
693
0
    used = in->cur - in->base;
694
    /*
695
     * Do not shrink on large buffers whose only a tiny fraction
696
     * was consumed
697
     */
698
0
    if (used > INPUT_CHUNK) {
699
0
  ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN);
700
0
  if (ret > 0) {
701
0
            used -= ret;
702
0
            if ((ret > ULONG_MAX) ||
703
0
                (in->consumed > ULONG_MAX - (unsigned long)ret))
704
0
                in->consumed = ULONG_MAX;
705
0
            else
706
0
                in->consumed += ret;
707
0
  }
708
0
    }
709
710
0
    if (xmlBufUse(in->buf->buffer) <= INPUT_CHUNK) {
711
0
        xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
712
0
    }
713
714
0
    in->base = xmlBufContent(in->buf->buffer);
715
0
    if (in->base == NULL) {
716
        /* TODO: raise error */
717
0
        in->base = BAD_CAST "";
718
0
        in->cur = in->base;
719
0
        in->end = in->base;
720
0
        return;
721
0
    }
722
0
    in->cur = in->base + used;
723
0
    in->end = xmlBufEnd(in->buf->buffer);
724
725
0
    CHECK_BUFFER(in);
726
0
}
727
728
/************************************************************************
729
 *                  *
730
 *    UTF8 character input and related functions    *
731
 *                  *
732
 ************************************************************************/
733
734
/**
735
 * xmlNextChar:
736
 * @ctxt:  the XML parser context
737
 *
738
 * DEPRECATED: Internal function, do not use.
739
 *
740
 * Skip to the next char input char.
741
 */
742
743
void
744
xmlNextChar(xmlParserCtxtPtr ctxt)
745
5.33M
{
746
5.33M
    if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
747
5.33M
        (ctxt->input == NULL))
748
0
        return;
749
750
5.33M
    if (!(VALID_CTXT(ctxt))) {
751
0
        xmlErrInternal(ctxt, "Parser input data memory error\n", NULL);
752
0
  ctxt->errNo = XML_ERR_INTERNAL_ERROR;
753
0
        xmlStopParser(ctxt);
754
0
  return;
755
0
    }
756
757
5.33M
    if (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK) {
758
1.57k
        if (xmlParserGrow(ctxt) < 0)
759
6
            return;
760
1.56k
        if (ctxt->input->cur >= ctxt->input->end)
761
0
            return;
762
1.56k
    }
763
764
5.33M
    if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
765
4.63M
        const unsigned char *cur;
766
4.63M
        unsigned char c;
767
768
        /*
769
         *   2.11 End-of-Line Handling
770
         *   the literal two-character sequence "#xD#xA" or a standalone
771
         *   literal #xD, an XML processor must pass to the application
772
         *   the single character #xA.
773
         */
774
4.63M
        if (*(ctxt->input->cur) == '\n') {
775
76
            ctxt->input->line++; ctxt->input->col = 1;
776
76
        } else
777
4.63M
            ctxt->input->col++;
778
779
        /*
780
         * We are supposed to handle UTF8, check it's valid
781
         * From rfc2044: encoding of the Unicode values on UTF-8:
782
         *
783
         * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
784
         * 0000 0000-0000 007F   0xxxxxxx
785
         * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
786
         * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
787
         *
788
         * Check for the 0x110000 limit too
789
         */
790
4.63M
        cur = ctxt->input->cur;
791
792
4.63M
        c = *cur;
793
4.63M
        if (c & 0x80) {
794
3.72M
            size_t avail;
795
796
3.72M
            if (c == 0xC0)
797
0
          goto encoding_error;
798
799
3.72M
            avail = ctxt->input->end - ctxt->input->cur;
800
801
3.72M
            if ((avail < 2) || (cur[1] & 0xc0) != 0x80)
802
5
                goto encoding_error;
803
3.72M
            if ((c & 0xe0) == 0xe0) {
804
3.72M
                unsigned int val;
805
806
3.72M
                if ((avail < 3) || (cur[2] & 0xc0) != 0x80)
807
0
                    goto encoding_error;
808
3.72M
                if ((c & 0xf0) == 0xf0) {
809
0
                    if (((c & 0xf8) != 0xf0) ||
810
0
                        (avail < 4) || ((cur[3] & 0xc0) != 0x80))
811
0
                        goto encoding_error;
812
                    /* 4-byte code */
813
0
                    ctxt->input->cur += 4;
814
0
                    val = (cur[0] & 0x7) << 18;
815
0
                    val |= (cur[1] & 0x3f) << 12;
816
0
                    val |= (cur[2] & 0x3f) << 6;
817
0
                    val |= cur[3] & 0x3f;
818
3.72M
                } else {
819
                    /* 3-byte code */
820
3.72M
                    ctxt->input->cur += 3;
821
3.72M
                    val = (cur[0] & 0xf) << 12;
822
3.72M
                    val |= (cur[1] & 0x3f) << 6;
823
3.72M
                    val |= cur[2] & 0x3f;
824
3.72M
                }
825
3.72M
                if (((val > 0xd7ff) && (val < 0xe000)) ||
826
3.72M
                    ((val > 0xfffd) && (val < 0x10000)) ||
827
3.72M
                    (val >= 0x110000)) {
828
0
    xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
829
0
          "Char 0x%X out of allowed range\n",
830
0
          val);
831
0
                }
832
3.72M
            } else
833
                /* 2-byte code */
834
760
                ctxt->input->cur += 2;
835
3.72M
        } else
836
            /* 1-byte code */
837
912k
            ctxt->input->cur++;
838
4.63M
    } else {
839
        /*
840
         * Assume it's a fixed length encoding (1) with
841
         * a compatible encoding for the ASCII set, since
842
         * XML constructs only use < 128 chars
843
         */
844
845
700k
        if (*(ctxt->input->cur) == '\n') {
846
21.1k
            ctxt->input->line++; ctxt->input->col = 1;
847
21.1k
        } else
848
679k
            ctxt->input->col++;
849
700k
        ctxt->input->cur++;
850
700k
    }
851
5.33M
    return;
852
5.33M
encoding_error:
853
    /*
854
     * If we detect an UTF8 error that probably mean that the
855
     * input encoding didn't get properly advertised in the
856
     * declaration header. Report the error and switch the encoding
857
     * to ISO-Latin-1 (if you don't like this policy, just declare the
858
     * encoding !)
859
     */
860
5
    if ((ctxt == NULL) || (ctxt->input == NULL) ||
861
5
        (ctxt->input->end - ctxt->input->cur < 4)) {
862
0
  __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
863
0
         "Input is not proper UTF-8, indicate encoding !\n",
864
0
         NULL, NULL);
865
5
    } else {
866
5
        char buffer[150];
867
868
5
  snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
869
5
      ctxt->input->cur[0], ctxt->input->cur[1],
870
5
      ctxt->input->cur[2], ctxt->input->cur[3]);
871
5
  __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
872
5
         "Input is not proper UTF-8, indicate encoding !\n%s",
873
5
         BAD_CAST buffer, NULL);
874
5
    }
875
5
    ctxt->charset = XML_CHAR_ENCODING_8859_1;
876
5
    ctxt->input->cur++;
877
5
    return;
878
5.33M
}
879
880
/**
881
 * xmlCurrentChar:
882
 * @ctxt:  the XML parser context
883
 * @len:  pointer to the length of the char read
884
 *
885
 * DEPRECATED: Internal function, do not use.
886
 *
887
 * The current char value, if using UTF-8 this may actually span multiple
888
 * bytes in the input buffer. Implement the end of line normalization:
889
 * 2.11 End-of-Line Handling
890
 * Wherever an external parsed entity or the literal entity value
891
 * of an internal parsed entity contains either the literal two-character
892
 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
893
 * must pass to the application the single character #xA.
894
 * This behavior can conveniently be produced by normalizing all
895
 * line breaks to #xA on input, before parsing.)
896
 *
897
 * Returns the current char value and its length
898
 */
899
900
int
901
58.1M
xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
902
58.1M
    if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
903
58.1M
    if (ctxt->instate == XML_PARSER_EOF)
904
0
  return(0);
905
906
58.1M
    if ((ctxt->input->end - ctxt->input->cur < INPUT_CHUNK) &&
907
58.1M
        (xmlParserGrow(ctxt) < 0))
908
13
        return(0);
909
910
58.1M
    if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
911
20.6M
      *len = 1;
912
20.6M
      return(*ctxt->input->cur);
913
20.6M
    }
914
37.4M
    if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
915
  /*
916
   * We are supposed to handle UTF8, check it's valid
917
   * From rfc2044: encoding of the Unicode values on UTF-8:
918
   *
919
   * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
920
   * 0000 0000-0000 007F   0xxxxxxx
921
   * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
922
   * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
923
   *
924
   * Check for the 0x110000 limit too
925
   */
926
22.9M
  const unsigned char *cur = ctxt->input->cur;
927
22.9M
  unsigned char c;
928
22.9M
  unsigned int val;
929
930
22.9M
  c = *cur;
931
22.9M
  if (c & 0x80) {
932
22.9M
            size_t avail;
933
934
22.9M
      if (((c & 0x40) == 0) || (c == 0xC0))
935
37
    goto encoding_error;
936
937
22.9M
            avail = ctxt->input->end - ctxt->input->cur;
938
939
22.9M
            if (avail < 2)
940
0
                goto incomplete_sequence;
941
22.9M
      if ((cur[1] & 0xc0) != 0x80)
942
192
    goto encoding_error;
943
22.9M
      if ((c & 0xe0) == 0xe0) {
944
16.3M
                if (avail < 3)
945
0
                    goto incomplete_sequence;
946
16.3M
    if ((cur[2] & 0xc0) != 0x80)
947
1
        goto encoding_error;
948
16.3M
    if ((c & 0xf0) == 0xf0) {
949
2
                    if (avail < 4)
950
0
                        goto incomplete_sequence;
951
2
        if (((c & 0xf8) != 0xf0) ||
952
2
      ((cur[3] & 0xc0) != 0x80))
953
0
      goto encoding_error;
954
        /* 4-byte code */
955
2
        *len = 4;
956
2
        val = (cur[0] & 0x7) << 18;
957
2
        val |= (cur[1] & 0x3f) << 12;
958
2
        val |= (cur[2] & 0x3f) << 6;
959
2
        val |= cur[3] & 0x3f;
960
2
        if (val < 0x10000)
961
0
      goto encoding_error;
962
16.3M
    } else {
963
      /* 3-byte code */
964
16.3M
        *len = 3;
965
16.3M
        val = (cur[0] & 0xf) << 12;
966
16.3M
        val |= (cur[1] & 0x3f) << 6;
967
16.3M
        val |= cur[2] & 0x3f;
968
16.3M
        if (val < 0x800)
969
0
      goto encoding_error;
970
16.3M
    }
971
16.3M
      } else {
972
        /* 2-byte code */
973
6.54M
    *len = 2;
974
6.54M
    val = (cur[0] & 0x1f) << 6;
975
6.54M
    val |= cur[1] & 0x3f;
976
6.54M
    if (val < 0x80)
977
0
        goto encoding_error;
978
6.54M
      }
979
22.9M
      if (!IS_CHAR(val)) {
980
40.6k
          xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
981
40.6k
          "Char 0x%X out of allowed range\n", val);
982
40.6k
      }
983
22.9M
      return(val);
984
22.9M
  } else {
985
      /* 1-byte code */
986
58.6k
      *len = 1;
987
58.6k
      if ((*ctxt->input->cur == 0) &&
988
58.6k
          (ctxt->input->end > ctxt->input->cur)) {
989
1.16k
          xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
990
1.16k
          "Char 0x0 out of allowed range\n", 0);
991
1.16k
      }
992
58.6k
      if (*ctxt->input->cur == 0xD) {
993
3.47k
    if (ctxt->input->cur[1] == 0xA) {
994
3
        ctxt->input->cur++;
995
3
    }
996
3.47k
    return(0xA);
997
3.47k
      }
998
55.2k
      return(*ctxt->input->cur);
999
58.6k
  }
1000
22.9M
    }
1001
    /*
1002
     * Assume it's a fixed length encoding (1) with
1003
     * a compatible encoding for the ASCII set, since
1004
     * XML constructs only use < 128 chars
1005
     */
1006
14.5M
    *len = 1;
1007
14.5M
    if (*ctxt->input->cur == 0xD) {
1008
3.84k
  if (ctxt->input->cur[1] == 0xA) {
1009
28
      ctxt->input->cur++;
1010
28
  }
1011
3.84k
  return(0xA);
1012
3.84k
    }
1013
14.4M
    return(*ctxt->input->cur);
1014
1015
230
encoding_error:
1016
    /*
1017
     * If we detect an UTF8 error that probably mean that the
1018
     * input encoding didn't get properly advertised in the
1019
     * declaration header. Report the error and switch the encoding
1020
     * to ISO-Latin-1 (if you don't like this policy, just declare the
1021
     * encoding !)
1022
     */
1023
230
    if (ctxt->input->end - ctxt->input->cur < 4) {
1024
0
  __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
1025
0
         "Input is not proper UTF-8, indicate encoding !\n",
1026
0
         NULL, NULL);
1027
230
    } else {
1028
230
        char buffer[150];
1029
1030
230
  snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
1031
230
      ctxt->input->cur[0], ctxt->input->cur[1],
1032
230
      ctxt->input->cur[2], ctxt->input->cur[3]);
1033
230
  __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
1034
230
         "Input is not proper UTF-8, indicate encoding !\n%s",
1035
230
         BAD_CAST buffer, NULL);
1036
230
    }
1037
230
    ctxt->charset = XML_CHAR_ENCODING_8859_1;
1038
230
    *len = 1;
1039
230
    return(*ctxt->input->cur);
1040
1041
0
incomplete_sequence:
1042
    /*
1043
     * An encoding problem may arise from a truncated input buffer
1044
     * splitting a character in the middle. In that case do not raise
1045
     * an error but return 0. This should only happen when push parsing
1046
     * char data.
1047
     */
1048
0
    *len = 0;
1049
0
    return(0);
1050
14.5M
}
1051
1052
/**
1053
 * xmlStringCurrentChar:
1054
 * @ctxt:  the XML parser context
1055
 * @cur:  pointer to the beginning of the char
1056
 * @len:  pointer to the length of the char read
1057
 *
1058
 * DEPRECATED: Internal function, do not use.
1059
 *
1060
 * The current char value, if using UTF-8 this may actually span multiple
1061
 * bytes in the input buffer.
1062
 *
1063
 * Returns the current char value and its length
1064
 */
1065
1066
int
1067
xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
1068
34.9M
{
1069
34.9M
    if ((len == NULL) || (cur == NULL)) return(0);
1070
34.9M
    if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
1071
        /*
1072
         * We are supposed to handle UTF8, check it's valid
1073
         * From rfc2044: encoding of the Unicode values on UTF-8:
1074
         *
1075
         * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
1076
         * 0000 0000-0000 007F   0xxxxxxx
1077
         * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
1078
         * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
1079
         *
1080
         * Check for the 0x110000 limit too
1081
         */
1082
20.6M
        unsigned char c;
1083
20.6M
        unsigned int val;
1084
1085
20.6M
        c = *cur;
1086
20.6M
        if (c & 0x80) {
1087
1.43k
            if ((cur[1] & 0xc0) != 0x80)
1088
810
                goto encoding_error;
1089
623
            if ((c & 0xe0) == 0xe0) {
1090
1091
408
                if ((cur[2] & 0xc0) != 0x80)
1092
0
                    goto encoding_error;
1093
408
                if ((c & 0xf0) == 0xf0) {
1094
1
                    if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
1095
0
                        goto encoding_error;
1096
                    /* 4-byte code */
1097
1
                    *len = 4;
1098
1
                    val = (cur[0] & 0x7) << 18;
1099
1
                    val |= (cur[1] & 0x3f) << 12;
1100
1
                    val |= (cur[2] & 0x3f) << 6;
1101
1
                    val |= cur[3] & 0x3f;
1102
407
                } else {
1103
                    /* 3-byte code */
1104
407
                    *len = 3;
1105
407
                    val = (cur[0] & 0xf) << 12;
1106
407
                    val |= (cur[1] & 0x3f) << 6;
1107
407
                    val |= cur[2] & 0x3f;
1108
407
                }
1109
408
            } else {
1110
                /* 2-byte code */
1111
215
                *len = 2;
1112
215
                val = (cur[0] & 0x1f) << 6;
1113
215
                val |= cur[1] & 0x3f;
1114
215
            }
1115
623
            if (!IS_CHAR(val)) {
1116
0
          xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
1117
0
          "Char 0x%X out of allowed range\n", val);
1118
0
            }
1119
623
            return (val);
1120
20.6M
        } else {
1121
            /* 1-byte code */
1122
20.6M
            *len = 1;
1123
20.6M
            return (*cur);
1124
20.6M
        }
1125
20.6M
    }
1126
    /*
1127
     * Assume it's a fixed length encoding (1) with
1128
     * a compatible encoding for the ASCII set, since
1129
     * XML constructs only use < 128 chars
1130
     */
1131
14.3M
    *len = 1;
1132
14.3M
    return (*cur);
1133
810
encoding_error:
1134
1135
    /*
1136
     * An encoding problem may arise from a truncated input buffer
1137
     * splitting a character in the middle. In that case do not raise
1138
     * an error but return 0 to indicate an end of stream problem
1139
     */
1140
810
    if ((ctxt == NULL) || (ctxt->input == NULL) ||
1141
810
        (ctxt->input->end - ctxt->input->cur < 4)) {
1142
810
  *len = 0;
1143
810
  return(0);
1144
810
    }
1145
    /*
1146
     * If we detect an UTF8 error that probably mean that the
1147
     * input encoding didn't get properly advertised in the
1148
     * declaration header. Report the error and switch the encoding
1149
     * to ISO-Latin-1 (if you don't like this policy, just declare the
1150
     * encoding !)
1151
     */
1152
0
    {
1153
0
        char buffer[150];
1154
1155
0
  snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
1156
0
      ctxt->input->cur[0], ctxt->input->cur[1],
1157
0
      ctxt->input->cur[2], ctxt->input->cur[3]);
1158
0
  __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
1159
0
         "Input is not proper UTF-8, indicate encoding !\n%s",
1160
0
         BAD_CAST buffer, NULL);
1161
0
    }
1162
0
    *len = 1;
1163
0
    return (*cur);
1164
810
}
1165
1166
/**
1167
 * xmlCopyCharMultiByte:
1168
 * @out:  pointer to an array of xmlChar
1169
 * @val:  the char value
1170
 *
1171
 * append the char value in the array
1172
 *
1173
 * Returns the number of xmlChar written
1174
 */
1175
int
1176
18.8M
xmlCopyCharMultiByte(xmlChar *out, int val) {
1177
18.8M
    if ((out == NULL) || (val < 0)) return(0);
1178
    /*
1179
     * We are supposed to handle UTF8, check it's valid
1180
     * From rfc2044: encoding of the Unicode values on UTF-8:
1181
     *
1182
     * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
1183
     * 0000 0000-0000 007F   0xxxxxxx
1184
     * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
1185
     * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
1186
     */
1187
18.8M
    if  (val >= 0x80) {
1188
18.8M
  xmlChar *savedout = out;
1189
18.8M
  int bits;
1190
18.8M
  if (val <   0x800) { *out++= (val >>  6) | 0xC0;  bits=  0; }
1191
15.3M
  else if (val < 0x10000) { *out++= (val >> 12) | 0xE0;  bits=  6;}
1192
3.33k
  else if (val < 0x110000)  { *out++= (val >> 18) | 0xF0;  bits=  12; }
1193
0
  else {
1194
0
      xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
1195
0
        "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
1196
0
            val);
1197
0
      return(0);
1198
0
  }
1199
52.9M
  for ( ; bits >= 0; bits-= 6)
1200
34.1M
      *out++= ((val >> bits) & 0x3F) | 0x80 ;
1201
18.8M
  return (out - savedout);
1202
18.8M
    }
1203
43.0k
    *out = val;
1204
43.0k
    return 1;
1205
18.8M
}
1206
1207
/**
1208
 * xmlCopyChar:
1209
 * @len:  Ignored, compatibility
1210
 * @out:  pointer to an array of xmlChar
1211
 * @val:  the char value
1212
 *
1213
 * append the char value in the array
1214
 *
1215
 * Returns the number of xmlChar written
1216
 */
1217
1218
int
1219
1.60k
xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
1220
1.60k
    if ((out == NULL) || (val < 0)) return(0);
1221
    /* the len parameter is ignored */
1222
1.60k
    if  (val >= 0x80) {
1223
991
  return(xmlCopyCharMultiByte (out, val));
1224
991
    }
1225
610
    *out = val;
1226
610
    return 1;
1227
1.60k
}
1228
1229
/************************************************************************
1230
 *                  *
1231
 *    Commodity functions to switch encodings     *
1232
 *                  *
1233
 ************************************************************************/
1234
1235
static xmlCharEncodingHandlerPtr
1236
0
xmlDetectEBCDIC(xmlParserInputPtr input) {
1237
0
    xmlChar out[200];
1238
0
    xmlCharEncodingHandlerPtr handler;
1239
0
    int inlen, outlen, res, i;
1240
1241
    /*
1242
     * To detect the EBCDIC code page, we convert the first 200 bytes
1243
     * to EBCDIC-US and try to find the encoding declaration.
1244
     */
1245
0
    handler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_EBCDIC);
1246
0
    if (handler == NULL)
1247
0
        return(NULL);
1248
0
    outlen = sizeof(out) - 1;
1249
0
    inlen = input->end - input->cur;
1250
0
    res = xmlEncInputChunk(handler, out, &outlen, input->cur, &inlen, 0);
1251
0
    if (res < 0)
1252
0
        return(handler);
1253
0
    out[outlen] = 0;
1254
1255
0
    for (i = 0; i < outlen; i++) {
1256
0
        if (out[i] == '>')
1257
0
            break;
1258
0
        if ((out[i] == 'e') &&
1259
0
            (xmlStrncmp(out + i, BAD_CAST "encoding", 8) == 0)) {
1260
0
            int start, cur, quote;
1261
1262
0
            i += 8;
1263
0
            while (IS_BLANK_CH(out[i]))
1264
0
                i += 1;
1265
0
            if (out[i++] != '=')
1266
0
                break;
1267
0
            while (IS_BLANK_CH(out[i]))
1268
0
                i += 1;
1269
0
            quote = out[i++];
1270
0
            if ((quote != '\'') && (quote != '"'))
1271
0
                break;
1272
0
            start = i;
1273
0
            cur = out[i];
1274
0
            while (((cur >= 'a') && (cur <= 'z')) ||
1275
0
                   ((cur >= 'A') && (cur <= 'Z')) ||
1276
0
                   ((cur >= '0') && (cur <= '9')) ||
1277
0
                   (cur == '.') || (cur == '_') ||
1278
0
                   (cur == '-'))
1279
0
                cur = out[++i];
1280
0
            if (cur != quote)
1281
0
                break;
1282
0
            out[i] = 0;
1283
0
            xmlCharEncCloseFunc(handler);
1284
0
            handler = xmlFindCharEncodingHandler((char *) out + start);
1285
0
            break;
1286
0
        }
1287
0
    }
1288
1289
0
    return(handler);
1290
0
}
1291
1292
/**
1293
 * xmlSwitchEncoding:
1294
 * @ctxt:  the parser context
1295
 * @enc:  the encoding value (number)
1296
 *
1297
 * change the input functions when discovering the character encoding
1298
 * of a given entity.
1299
 *
1300
 * Returns 0 in case of success, -1 otherwise
1301
 */
1302
int
1303
xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
1304
149
{
1305
149
    xmlCharEncodingHandlerPtr handler;
1306
149
    int ret;
1307
1308
149
    if (ctxt == NULL) return(-1);
1309
149
    switch (enc) {
1310
0
  case XML_CHAR_ENCODING_ERROR:
1311
0
      __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
1312
0
                     "encoding unknown\n", NULL, NULL);
1313
0
      return(-1);
1314
0
  case XML_CHAR_ENCODING_NONE:
1315
      /* let's assume it's UTF-8 without the XML decl */
1316
0
      ctxt->charset = XML_CHAR_ENCODING_UTF8;
1317
0
      return(0);
1318
98
  case XML_CHAR_ENCODING_UTF8:
1319
      /* default encoding, no conversion should be needed */
1320
98
      ctxt->charset = XML_CHAR_ENCODING_UTF8;
1321
1322
      /*
1323
       * Errata on XML-1.0 June 20 2001
1324
       * Specific handling of the Byte Order Mark for
1325
       * UTF-8
1326
       */
1327
98
      if ((ctxt->input != NULL) &&
1328
98
    (ctxt->input->cur[0] == 0xEF) &&
1329
98
    (ctxt->input->cur[1] == 0xBB) &&
1330
98
    (ctxt->input->cur[2] == 0xBF)) {
1331
0
    ctxt->input->cur += 3;
1332
0
      }
1333
98
      return(0);
1334
0
        case XML_CHAR_ENCODING_EBCDIC:
1335
0
            handler = xmlDetectEBCDIC(ctxt->input);
1336
0
            break;
1337
51
        default:
1338
51
            handler = xmlGetCharEncodingHandler(enc);
1339
51
            break;
1340
149
    }
1341
51
    if (handler == NULL) {
1342
  /*
1343
   * Default handlers.
1344
   */
1345
0
  switch (enc) {
1346
0
      case XML_CHAR_ENCODING_ASCII:
1347
    /* default encoding, no conversion should be needed */
1348
0
    ctxt->charset = XML_CHAR_ENCODING_UTF8;
1349
0
    return(0);
1350
0
      case XML_CHAR_ENCODING_8859_1:
1351
0
    if ((ctxt->inputNr == 1) &&
1352
0
        (ctxt->encoding == NULL) &&
1353
0
        (ctxt->input != NULL) &&
1354
0
        (ctxt->input->encoding != NULL)) {
1355
0
        ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1356
0
    }
1357
0
    ctxt->charset = enc;
1358
0
    return(0);
1359
0
      default:
1360
0
    __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1361
0
                        "encoding not supported: %s\n",
1362
0
      BAD_CAST xmlGetCharEncodingName(enc), NULL);
1363
                /*
1364
                 * TODO: We could recover from errors in external entities
1365
                 * if we didn't stop the parser. But most callers of this
1366
                 * function don't check the return value.
1367
                 */
1368
0
                xmlStopParser(ctxt);
1369
0
                return(-1);
1370
0
        }
1371
0
    }
1372
51
    ret = xmlSwitchInputEncoding(ctxt, ctxt->input, handler);
1373
51
    if ((ret < 0) || (ctxt->errNo == XML_I18N_CONV_FAILED)) {
1374
        /*
1375
   * on encoding conversion errors, stop the parser
1376
   */
1377
0
        xmlStopParser(ctxt);
1378
0
  ctxt->errNo = XML_I18N_CONV_FAILED;
1379
0
    }
1380
51
    return(ret);
1381
51
}
1382
1383
/**
1384
 * xmlSwitchInputEncoding:
1385
 * @ctxt:  the parser context
1386
 * @input:  the input stream
1387
 * @handler:  the encoding handler
1388
 *
1389
 * change the input functions when discovering the character encoding
1390
 * of a given entity.
1391
 *
1392
 * Returns 0 in case of success, -1 otherwise
1393
 */
1394
int
1395
xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1396
                       xmlCharEncodingHandlerPtr handler)
1397
139
{
1398
139
    int nbchars;
1399
139
    xmlParserInputBufferPtr in;
1400
1401
139
    if (handler == NULL)
1402
0
        return (-1);
1403
139
    if (input == NULL)
1404
0
        return (-1);
1405
139
    in = input->buf;
1406
139
    if (in == NULL) {
1407
0
  xmlErrInternal(ctxt,
1408
0
                "static memory buffer doesn't support encoding\n", NULL);
1409
        /*
1410
         * Callers assume that the input buffer takes ownership of the
1411
         * encoding handler. xmlCharEncCloseFunc frees unregistered
1412
         * handlers and avoids a memory leak.
1413
         */
1414
0
        xmlCharEncCloseFunc(handler);
1415
0
  return (-1);
1416
0
    }
1417
1418
139
    if (in->encoder != NULL) {
1419
0
        if (in->encoder == handler)
1420
0
            return (0);
1421
1422
        /*
1423
         * Switching encodings during parsing is a really bad idea,
1424
         * but WebKit/Chromium switches from ISO-8859-1 to UTF-16 as soon as
1425
         * it finds Unicode characters with code points larger than 255.
1426
         *
1427
         * TODO: We should check whether the "raw" input buffer is empty and
1428
         * convert the old content using the old encoder.
1429
         */
1430
1431
0
        xmlCharEncCloseFunc(in->encoder);
1432
0
        in->encoder = handler;
1433
0
        return (0);
1434
0
    }
1435
1436
139
    ctxt->charset = XML_CHAR_ENCODING_UTF8;
1437
139
    in->encoder = handler;
1438
1439
    /*
1440
     * Is there already some content down the pipe to convert ?
1441
     */
1442
139
    if (xmlBufIsEmpty(in->buffer) == 0) {
1443
139
        size_t processed, use, consumed;
1444
1445
        /*
1446
         * Specific handling of the Byte Order Mark for
1447
         * UTF-16
1448
         */
1449
139
        if ((handler->name != NULL) &&
1450
139
            (!strcmp(handler->name, "UTF-16LE") ||
1451
139
             !strcmp(handler->name, "UTF-16")) &&
1452
139
            (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1453
8
            input->cur += 2;
1454
8
        }
1455
139
        if ((handler->name != NULL) &&
1456
139
            (!strcmp(handler->name, "UTF-16BE")) &&
1457
139
            (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1458
24
            input->cur += 2;
1459
24
        }
1460
        /*
1461
         * Errata on XML-1.0 June 20 2001
1462
         * Specific handling of the Byte Order Mark for
1463
         * UTF-8
1464
         */
1465
139
        if ((handler->name != NULL) &&
1466
139
            (!strcmp(handler->name, "UTF-8")) &&
1467
139
            (input->cur[0] == 0xEF) &&
1468
139
            (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1469
0
            input->cur += 3;
1470
0
        }
1471
1472
        /*
1473
         * Shrink the current input buffer.
1474
         * Move it as the raw buffer and create a new input buffer
1475
         */
1476
139
        processed = input->cur - input->base;
1477
139
        xmlBufShrink(in->buffer, processed);
1478
139
        input->consumed += processed;
1479
139
        in->raw = in->buffer;
1480
139
        in->buffer = xmlBufCreate();
1481
139
        in->rawconsumed = processed;
1482
139
        use = xmlBufUse(in->raw);
1483
1484
        /*
1485
         * TODO: We must flush and decode the whole buffer to make functions
1486
         * like xmlReadMemory work with a user-provided encoding. If the
1487
         * encoding is specified directly, we should probably set
1488
         * XML_PARSE_IGNORE_ENC in xmlDoRead to avoid switching encodings
1489
         * twice. Then we could set "flush" to false which should save
1490
         * a considerable amount of memory when parsing from memory.
1491
         * It's probably even possible to remove this whole if-block
1492
         * completely.
1493
         */
1494
139
        nbchars = xmlCharEncInput(in, 1);
1495
139
        xmlBufResetInput(in->buffer, input);
1496
139
        if (nbchars < 0) {
1497
            /* TODO: This could be an out of memory or an encoding error. */
1498
0
            xmlErrInternal(ctxt,
1499
0
                           "switching encoding: encoder error\n",
1500
0
                           NULL);
1501
0
            xmlHaltParser(ctxt);
1502
0
            return (-1);
1503
0
        }
1504
139
        consumed = use - xmlBufUse(in->raw);
1505
139
        if ((consumed > ULONG_MAX) ||
1506
139
            (in->rawconsumed > ULONG_MAX - (unsigned long)consumed))
1507
0
            in->rawconsumed = ULONG_MAX;
1508
139
        else
1509
139
      in->rawconsumed += consumed;
1510
139
    }
1511
139
    return (0);
1512
139
}
1513
1514
/**
1515
 * xmlSwitchToEncoding:
1516
 * @ctxt:  the parser context
1517
 * @handler:  the encoding handler
1518
 *
1519
 * change the input functions when discovering the character encoding
1520
 * of a given entity.
1521
 *
1522
 * Returns 0 in case of success, -1 otherwise
1523
 */
1524
int
1525
xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1526
88
{
1527
88
    if (ctxt == NULL)
1528
0
        return(-1);
1529
88
    return(xmlSwitchInputEncoding(ctxt, ctxt->input, handler));
1530
88
}
1531
1532
/************************************************************************
1533
 *                  *
1534
 *  Commodity functions to handle entities processing   *
1535
 *                  *
1536
 ************************************************************************/
1537
1538
/**
1539
 * xmlFreeInputStream:
1540
 * @input:  an xmlParserInputPtr
1541
 *
1542
 * Free up an input stream.
1543
 */
1544
void
1545
410
xmlFreeInputStream(xmlParserInputPtr input) {
1546
410
    if (input == NULL) return;
1547
1548
410
    if (input->filename != NULL) xmlFree((char *) input->filename);
1549
410
    if (input->directory != NULL) xmlFree((char *) input->directory);
1550
410
    if (input->encoding != NULL) xmlFree((char *) input->encoding);
1551
410
    if (input->version != NULL) xmlFree((char *) input->version);
1552
410
    if ((input->free != NULL) && (input->base != NULL))
1553
0
        input->free((xmlChar *) input->base);
1554
410
    if (input->buf != NULL)
1555
328
        xmlFreeParserInputBuffer(input->buf);
1556
410
    xmlFree(input);
1557
410
}
1558
1559
/**
1560
 * xmlNewInputStream:
1561
 * @ctxt:  an XML parser context
1562
 *
1563
 * Create a new input stream structure.
1564
 *
1565
 * Returns the new input stream or NULL
1566
 */
1567
xmlParserInputPtr
1568
410
xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1569
410
    xmlParserInputPtr input;
1570
1571
410
    input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1572
410
    if (input == NULL) {
1573
0
        xmlErrMemory(ctxt,  "couldn't allocate a new input stream\n");
1574
0
  return(NULL);
1575
0
    }
1576
410
    memset(input, 0, sizeof(xmlParserInput));
1577
410
    input->line = 1;
1578
410
    input->col = 1;
1579
410
    input->standalone = -1;
1580
1581
    /*
1582
     * If the context is NULL the id cannot be initialized, but that
1583
     * should not happen while parsing which is the situation where
1584
     * the id is actually needed.
1585
     */
1586
410
    if (ctxt != NULL) {
1587
410
        if (input->id >= INT_MAX) {
1588
0
            xmlErrMemory(ctxt, "Input ID overflow\n");
1589
0
            return(NULL);
1590
0
        }
1591
410
        input->id = ctxt->input_id++;
1592
410
    }
1593
1594
410
    return(input);
1595
410
}
1596
1597
/**
1598
 * xmlNewIOInputStream:
1599
 * @ctxt:  an XML parser context
1600
 * @input:  an I/O Input
1601
 * @enc:  the charset encoding if known
1602
 *
1603
 * Create a new input stream structure encapsulating the @input into
1604
 * a stream suitable for the parser.
1605
 *
1606
 * Returns the new input stream or NULL
1607
 */
1608
xmlParserInputPtr
1609
xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1610
0
              xmlCharEncoding enc) {
1611
0
    xmlParserInputPtr inputStream;
1612
1613
0
    if (input == NULL) return(NULL);
1614
0
    if (xmlParserDebugEntities)
1615
0
  xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1616
0
    inputStream = xmlNewInputStream(ctxt);
1617
0
    if (inputStream == NULL) {
1618
0
  return(NULL);
1619
0
    }
1620
0
    inputStream->filename = NULL;
1621
0
    inputStream->buf = input;
1622
0
    xmlBufResetInput(inputStream->buf->buffer, inputStream);
1623
1624
0
    if (enc != XML_CHAR_ENCODING_NONE) {
1625
0
        xmlSwitchEncoding(ctxt, enc);
1626
0
    }
1627
1628
0
    return(inputStream);
1629
0
}
1630
1631
/**
1632
 * xmlNewEntityInputStream:
1633
 * @ctxt:  an XML parser context
1634
 * @entity:  an Entity pointer
1635
 *
1636
 * DEPRECATED: Internal function, do not use.
1637
 *
1638
 * Create a new input stream based on an xmlEntityPtr
1639
 *
1640
 * Returns the new input stream or NULL
1641
 */
1642
xmlParserInputPtr
1643
0
xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1644
0
    xmlParserInputPtr input;
1645
1646
0
    if (entity == NULL) {
1647
0
        xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1648
0
                 NULL);
1649
0
  return(NULL);
1650
0
    }
1651
0
    if (xmlParserDebugEntities)
1652
0
  xmlGenericError(xmlGenericErrorContext,
1653
0
    "new input from entity: %s\n", entity->name);
1654
0
    if (entity->content == NULL) {
1655
0
  switch (entity->etype) {
1656
0
            case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1657
0
          xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1658
0
                   entity->name);
1659
0
                break;
1660
0
            case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1661
0
            case XML_EXTERNAL_PARAMETER_ENTITY:
1662
0
    input = xmlLoadExternalEntity((char *) entity->URI,
1663
0
           (char *) entity->ExternalID, ctxt);
1664
0
                if (input != NULL)
1665
0
                    input->entity = entity;
1666
0
                return(input);
1667
0
            case XML_INTERNAL_GENERAL_ENTITY:
1668
0
          xmlErrInternal(ctxt,
1669
0
          "Internal entity %s without content !\n",
1670
0
                   entity->name);
1671
0
                break;
1672
0
            case XML_INTERNAL_PARAMETER_ENTITY:
1673
0
          xmlErrInternal(ctxt,
1674
0
          "Internal parameter entity %s without content !\n",
1675
0
                   entity->name);
1676
0
                break;
1677
0
            case XML_INTERNAL_PREDEFINED_ENTITY:
1678
0
          xmlErrInternal(ctxt,
1679
0
          "Predefined entity %s without content !\n",
1680
0
                   entity->name);
1681
0
                break;
1682
0
  }
1683
0
  return(NULL);
1684
0
    }
1685
0
    input = xmlNewInputStream(ctxt);
1686
0
    if (input == NULL) {
1687
0
  return(NULL);
1688
0
    }
1689
0
    if (entity->URI != NULL)
1690
0
  input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
1691
0
    input->base = entity->content;
1692
0
    if (entity->length == 0)
1693
0
        entity->length = xmlStrlen(entity->content);
1694
0
    input->cur = entity->content;
1695
0
    input->length = entity->length;
1696
0
    input->end = &entity->content[input->length];
1697
0
    input->entity = entity;
1698
0
    return(input);
1699
0
}
1700
1701
/**
1702
 * xmlNewStringInputStream:
1703
 * @ctxt:  an XML parser context
1704
 * @buffer:  an memory buffer
1705
 *
1706
 * Create a new input stream based on a memory buffer.
1707
 * Returns the new input stream
1708
 */
1709
xmlParserInputPtr
1710
0
xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1711
0
    xmlParserInputPtr input;
1712
0
    xmlParserInputBufferPtr buf;
1713
1714
0
    if (buffer == NULL) {
1715
0
        xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1716
0
                 NULL);
1717
0
  return(NULL);
1718
0
    }
1719
0
    if (xmlParserDebugEntities)
1720
0
  xmlGenericError(xmlGenericErrorContext,
1721
0
    "new fixed input: %.30s\n", buffer);
1722
0
    buf = xmlParserInputBufferCreateMem((const char *) buffer,
1723
0
                                        xmlStrlen(buffer),
1724
0
                                        XML_CHAR_ENCODING_NONE);
1725
0
    if (buf == NULL) {
1726
0
  xmlErrMemory(ctxt, NULL);
1727
0
        return(NULL);
1728
0
    }
1729
0
    input = xmlNewInputStream(ctxt);
1730
0
    if (input == NULL) {
1731
0
        xmlErrMemory(ctxt,  "couldn't allocate a new input stream\n");
1732
0
  xmlFreeParserInputBuffer(buf);
1733
0
  return(NULL);
1734
0
    }
1735
0
    input->buf = buf;
1736
0
    xmlBufResetInput(input->buf->buffer, input);
1737
0
    return(input);
1738
0
}
1739
1740
/**
1741
 * xmlNewInputFromFile:
1742
 * @ctxt:  an XML parser context
1743
 * @filename:  the filename to use as entity
1744
 *
1745
 * Create a new input stream based on a file or an URL.
1746
 *
1747
 * Returns the new input stream or NULL in case of error
1748
 */
1749
xmlParserInputPtr
1750
0
xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1751
0
    xmlParserInputBufferPtr buf;
1752
0
    xmlParserInputPtr inputStream;
1753
0
    char *directory = NULL;
1754
0
    xmlChar *URI = NULL;
1755
1756
0
    if (xmlParserDebugEntities)
1757
0
  xmlGenericError(xmlGenericErrorContext,
1758
0
    "new input from file: %s\n", filename);
1759
0
    if (ctxt == NULL) return(NULL);
1760
0
    buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1761
0
    if (buf == NULL) {
1762
0
  if (filename == NULL)
1763
0
      __xmlLoaderErr(ctxt,
1764
0
                     "failed to load external entity: NULL filename \n",
1765
0
         NULL);
1766
0
  else
1767
0
      __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1768
0
         (const char *) filename);
1769
0
  return(NULL);
1770
0
    }
1771
1772
0
    inputStream = xmlNewInputStream(ctxt);
1773
0
    if (inputStream == NULL) {
1774
0
  xmlFreeParserInputBuffer(buf);
1775
0
  return(NULL);
1776
0
    }
1777
1778
0
    inputStream->buf = buf;
1779
0
    inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1780
0
    if (inputStream == NULL)
1781
0
        return(NULL);
1782
1783
0
    if (inputStream->filename == NULL)
1784
0
  URI = xmlStrdup((xmlChar *) filename);
1785
0
    else
1786
0
  URI = xmlStrdup((xmlChar *) inputStream->filename);
1787
0
    directory = xmlParserGetDirectory((const char *) URI);
1788
0
    if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
1789
0
    inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
1790
0
    if (URI != NULL) xmlFree((char *) URI);
1791
0
    inputStream->directory = directory;
1792
1793
0
    xmlBufResetInput(inputStream->buf->buffer, inputStream);
1794
0
    if ((ctxt->directory == NULL) && (directory != NULL))
1795
0
        ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1796
0
    return(inputStream);
1797
0
}
1798
1799
/************************************************************************
1800
 *                  *
1801
 *    Commodity functions to handle parser contexts   *
1802
 *                  *
1803
 ************************************************************************/
1804
1805
/**
1806
 * xmlInitSAXParserCtxt:
1807
 * @ctxt:  XML parser context
1808
 * @sax:  SAX handlert
1809
 * @userData:  user data
1810
 *
1811
 * Initialize a SAX parser context
1812
 *
1813
 * Returns 0 in case of success and -1 in case of error
1814
 */
1815
1816
static int
1817
xmlInitSAXParserCtxt(xmlParserCtxtPtr ctxt, const xmlSAXHandler *sax,
1818
                     void *userData)
1819
410
{
1820
410
    xmlParserInputPtr input;
1821
1822
410
    if(ctxt==NULL) {
1823
0
        xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
1824
0
        return(-1);
1825
0
    }
1826
1827
410
    xmlInitParser();
1828
1829
410
    if (ctxt->dict == NULL)
1830
410
  ctxt->dict = xmlDictCreate();
1831
410
    if (ctxt->dict == NULL) {
1832
0
        xmlErrMemory(NULL, "cannot initialize parser context\n");
1833
0
  return(-1);
1834
0
    }
1835
410
    xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT);
1836
1837
410
    if (ctxt->sax == NULL)
1838
410
  ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1839
410
    if (ctxt->sax == NULL) {
1840
0
        xmlErrMemory(NULL, "cannot initialize parser context\n");
1841
0
  return(-1);
1842
0
    }
1843
410
    if (sax == NULL) {
1844
410
  memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
1845
410
        xmlSAXVersion(ctxt->sax, 2);
1846
410
        ctxt->userData = ctxt;
1847
410
    } else {
1848
0
  if (sax->initialized == XML_SAX2_MAGIC) {
1849
0
      memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
1850
0
        } else {
1851
0
      memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
1852
0
      memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
1853
0
        }
1854
0
        ctxt->userData = userData ? userData : ctxt;
1855
0
    }
1856
1857
410
    ctxt->maxatts = 0;
1858
410
    ctxt->atts = NULL;
1859
    /* Allocate the Input stack */
1860
410
    if (ctxt->inputTab == NULL) {
1861
410
  ctxt->inputTab = (xmlParserInputPtr *)
1862
410
        xmlMalloc(5 * sizeof(xmlParserInputPtr));
1863
410
  ctxt->inputMax = 5;
1864
410
    }
1865
410
    if (ctxt->inputTab == NULL) {
1866
0
        xmlErrMemory(NULL, "cannot initialize parser context\n");
1867
0
  ctxt->inputNr = 0;
1868
0
  ctxt->inputMax = 0;
1869
0
  ctxt->input = NULL;
1870
0
  return(-1);
1871
0
    }
1872
410
    while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1873
0
        xmlFreeInputStream(input);
1874
0
    }
1875
410
    ctxt->inputNr = 0;
1876
410
    ctxt->input = NULL;
1877
1878
410
    ctxt->version = NULL;
1879
410
    ctxt->encoding = NULL;
1880
410
    ctxt->standalone = -1;
1881
410
    ctxt->hasExternalSubset = 0;
1882
410
    ctxt->hasPErefs = 0;
1883
410
    ctxt->html = 0;
1884
410
    ctxt->external = 0;
1885
410
    ctxt->instate = XML_PARSER_START;
1886
410
    ctxt->token = 0;
1887
410
    ctxt->directory = NULL;
1888
1889
    /* Allocate the Node stack */
1890
410
    if (ctxt->nodeTab == NULL) {
1891
410
  ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1892
410
  ctxt->nodeMax = 10;
1893
410
    }
1894
410
    if (ctxt->nodeTab == NULL) {
1895
0
        xmlErrMemory(NULL, "cannot initialize parser context\n");
1896
0
  ctxt->nodeNr = 0;
1897
0
  ctxt->nodeMax = 0;
1898
0
  ctxt->node = NULL;
1899
0
  ctxt->inputNr = 0;
1900
0
  ctxt->inputMax = 0;
1901
0
  ctxt->input = NULL;
1902
0
  return(-1);
1903
0
    }
1904
410
    ctxt->nodeNr = 0;
1905
410
    ctxt->node = NULL;
1906
1907
    /* Allocate the Name stack */
1908
410
    if (ctxt->nameTab == NULL) {
1909
410
  ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1910
410
  ctxt->nameMax = 10;
1911
410
    }
1912
410
    if (ctxt->nameTab == NULL) {
1913
0
        xmlErrMemory(NULL, "cannot initialize parser context\n");
1914
0
  ctxt->nodeNr = 0;
1915
0
  ctxt->nodeMax = 0;
1916
0
  ctxt->node = NULL;
1917
0
  ctxt->inputNr = 0;
1918
0
  ctxt->inputMax = 0;
1919
0
  ctxt->input = NULL;
1920
0
  ctxt->nameNr = 0;
1921
0
  ctxt->nameMax = 0;
1922
0
  ctxt->name = NULL;
1923
0
  return(-1);
1924
0
    }
1925
410
    ctxt->nameNr = 0;
1926
410
    ctxt->name = NULL;
1927
1928
    /* Allocate the space stack */
1929
410
    if (ctxt->spaceTab == NULL) {
1930
410
  ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1931
410
  ctxt->spaceMax = 10;
1932
410
    }
1933
410
    if (ctxt->spaceTab == NULL) {
1934
0
        xmlErrMemory(NULL, "cannot initialize parser context\n");
1935
0
  ctxt->nodeNr = 0;
1936
0
  ctxt->nodeMax = 0;
1937
0
  ctxt->node = NULL;
1938
0
  ctxt->inputNr = 0;
1939
0
  ctxt->inputMax = 0;
1940
0
  ctxt->input = NULL;
1941
0
  ctxt->nameNr = 0;
1942
0
  ctxt->nameMax = 0;
1943
0
  ctxt->name = NULL;
1944
0
  ctxt->spaceNr = 0;
1945
0
  ctxt->spaceMax = 0;
1946
0
  ctxt->space = NULL;
1947
0
  return(-1);
1948
0
    }
1949
410
    ctxt->spaceNr = 1;
1950
410
    ctxt->spaceMax = 10;
1951
410
    ctxt->spaceTab[0] = -1;
1952
410
    ctxt->space = &ctxt->spaceTab[0];
1953
410
    ctxt->myDoc = NULL;
1954
410
    ctxt->wellFormed = 1;
1955
410
    ctxt->nsWellFormed = 1;
1956
410
    ctxt->valid = 1;
1957
410
    ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1958
410
    if (ctxt->loadsubset) {
1959
0
        ctxt->options |= XML_PARSE_DTDLOAD;
1960
0
    }
1961
410
    ctxt->validate = xmlDoValidityCheckingDefaultValue;
1962
410
    ctxt->pedantic = xmlPedanticParserDefaultValue;
1963
410
    if (ctxt->pedantic) {
1964
0
        ctxt->options |= XML_PARSE_PEDANTIC;
1965
0
    }
1966
410
    ctxt->linenumbers = xmlLineNumbersDefaultValue;
1967
410
    ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
1968
410
    if (ctxt->keepBlanks == 0) {
1969
0
  ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
1970
0
  ctxt->options |= XML_PARSE_NOBLANKS;
1971
0
    }
1972
1973
410
    ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT;
1974
410
    ctxt->vctxt.userData = ctxt;
1975
410
    ctxt->vctxt.error = xmlParserValidityError;
1976
410
    ctxt->vctxt.warning = xmlParserValidityWarning;
1977
410
    if (ctxt->validate) {
1978
0
  if (xmlGetWarningsDefaultValue == 0)
1979
0
      ctxt->vctxt.warning = NULL;
1980
0
  else
1981
0
      ctxt->vctxt.warning = xmlParserValidityWarning;
1982
0
  ctxt->vctxt.nodeMax = 0;
1983
0
        ctxt->options |= XML_PARSE_DTDVALID;
1984
0
    }
1985
410
    ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1986
410
    if (ctxt->replaceEntities) {
1987
0
        ctxt->options |= XML_PARSE_NOENT;
1988
0
    }
1989
410
    ctxt->record_info = 0;
1990
410
    ctxt->checkIndex = 0;
1991
410
    ctxt->inSubset = 0;
1992
410
    ctxt->errNo = XML_ERR_OK;
1993
410
    ctxt->depth = 0;
1994
410
    ctxt->charset = XML_CHAR_ENCODING_UTF8;
1995
410
    ctxt->catalogs = NULL;
1996
410
    ctxt->sizeentities = 0;
1997
410
    ctxt->sizeentcopy = 0;
1998
410
    ctxt->input_id = 1;
1999
410
    xmlInitNodeInfoSeq(&ctxt->node_seq);
2000
410
    return(0);
2001
410
}
2002
2003
/**
2004
 * xmlInitParserCtxt:
2005
 * @ctxt:  an XML parser context
2006
 *
2007
 * DEPRECATED: Internal function which will be made private in a future
2008
 * version.
2009
 *
2010
 * Initialize a parser context
2011
 *
2012
 * Returns 0 in case of success and -1 in case of error
2013
 */
2014
2015
int
2016
xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
2017
0
{
2018
0
    return(xmlInitSAXParserCtxt(ctxt, NULL, NULL));
2019
0
}
2020
2021
/**
2022
 * xmlFreeParserCtxt:
2023
 * @ctxt:  an XML parser context
2024
 *
2025
 * Free all the memory used by a parser context. However the parsed
2026
 * document in ctxt->myDoc is not freed.
2027
 */
2028
2029
void
2030
xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
2031
410
{
2032
410
    xmlParserInputPtr input;
2033
2034
410
    if (ctxt == NULL) return;
2035
2036
820
    while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
2037
410
        xmlFreeInputStream(input);
2038
410
    }
2039
410
    if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
2040
410
    if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
2041
410
    if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
2042
410
    if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
2043
410
    if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
2044
410
    if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
2045
410
    if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
2046
410
    if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
2047
410
    if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
2048
#ifdef LIBXML_SAX1_ENABLED
2049
    if ((ctxt->sax != NULL) &&
2050
        (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
2051
#else
2052
410
    if (ctxt->sax != NULL)
2053
410
#endif /* LIBXML_SAX1_ENABLED */
2054
410
        xmlFree(ctxt->sax);
2055
410
    if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
2056
410
    if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
2057
410
    if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
2058
410
    if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
2059
410
    if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
2060
410
    if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
2061
410
    if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
2062
410
    if (ctxt->attsDefault != NULL)
2063
14
        xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
2064
410
    if (ctxt->attsSpecial != NULL)
2065
14
        xmlHashFree(ctxt->attsSpecial, NULL);
2066
410
    if (ctxt->freeElems != NULL) {
2067
0
        xmlNodePtr cur, next;
2068
2069
0
  cur = ctxt->freeElems;
2070
0
  while (cur != NULL) {
2071
0
      next = cur->next;
2072
0
      xmlFree(cur);
2073
0
      cur = next;
2074
0
  }
2075
0
    }
2076
410
    if (ctxt->freeAttrs != NULL) {
2077
0
        xmlAttrPtr cur, next;
2078
2079
0
  cur = ctxt->freeAttrs;
2080
0
  while (cur != NULL) {
2081
0
      next = cur->next;
2082
0
      xmlFree(cur);
2083
0
      cur = next;
2084
0
  }
2085
0
    }
2086
    /*
2087
     * cleanup the error strings
2088
     */
2089
410
    if (ctxt->lastError.message != NULL)
2090
409
        xmlFree(ctxt->lastError.message);
2091
410
    if (ctxt->lastError.file != NULL)
2092
0
        xmlFree(ctxt->lastError.file);
2093
410
    if (ctxt->lastError.str1 != NULL)
2094
254
        xmlFree(ctxt->lastError.str1);
2095
410
    if (ctxt->lastError.str2 != NULL)
2096
3
        xmlFree(ctxt->lastError.str2);
2097
410
    if (ctxt->lastError.str3 != NULL)
2098
0
        xmlFree(ctxt->lastError.str3);
2099
2100
410
#ifdef LIBXML_CATALOG_ENABLED
2101
410
    if (ctxt->catalogs != NULL)
2102
0
  xmlCatalogFreeLocal(ctxt->catalogs);
2103
410
#endif
2104
410
    xmlFree(ctxt);
2105
410
}
2106
2107
/**
2108
 * xmlNewParserCtxt:
2109
 *
2110
 * Allocate and initialize a new parser context.
2111
 *
2112
 * Returns the xmlParserCtxtPtr or NULL
2113
 */
2114
2115
xmlParserCtxtPtr
2116
xmlNewParserCtxt(void)
2117
410
{
2118
410
    return(xmlNewSAXParserCtxt(NULL, NULL));
2119
410
}
2120
2121
/**
2122
 * xmlNewSAXParserCtxt:
2123
 * @sax:  SAX handler
2124
 * @userData:  user data
2125
 *
2126
 * Allocate and initialize a new SAX parser context. If userData is NULL,
2127
 * the parser context will be passed as user data.
2128
 *
2129
 * Returns the xmlParserCtxtPtr or NULL if memory allocation failed.
2130
 */
2131
2132
xmlParserCtxtPtr
2133
xmlNewSAXParserCtxt(const xmlSAXHandler *sax, void *userData)
2134
410
{
2135
410
    xmlParserCtxtPtr ctxt;
2136
2137
410
    ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
2138
410
    if (ctxt == NULL) {
2139
0
  xmlErrMemory(NULL, "cannot allocate parser context\n");
2140
0
  return(NULL);
2141
0
    }
2142
410
    memset(ctxt, 0, sizeof(xmlParserCtxt));
2143
410
    if (xmlInitSAXParserCtxt(ctxt, sax, userData) < 0) {
2144
0
        xmlFreeParserCtxt(ctxt);
2145
0
  return(NULL);
2146
0
    }
2147
410
    return(ctxt);
2148
410
}
2149
2150
/************************************************************************
2151
 *                  *
2152
 *    Handling of node information        *
2153
 *                  *
2154
 ************************************************************************/
2155
2156
/**
2157
 * xmlClearParserCtxt:
2158
 * @ctxt:  an XML parser context
2159
 *
2160
 * Clear (release owned resources) and reinitialize a parser context
2161
 */
2162
2163
void
2164
xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
2165
0
{
2166
0
  if (ctxt==NULL)
2167
0
    return;
2168
0
  xmlClearNodeInfoSeq(&ctxt->node_seq);
2169
0
  xmlCtxtReset(ctxt);
2170
0
}
2171
2172
2173
/**
2174
 * xmlParserFindNodeInfo:
2175
 * @ctx:  an XML parser context
2176
 * @node:  an XML node within the tree
2177
 *
2178
 * DEPRECATED: Don't use.
2179
 *
2180
 * Find the parser node info struct for a given node
2181
 *
2182
 * Returns an xmlParserNodeInfo block pointer or NULL
2183
 */
2184
const xmlParserNodeInfo *
2185
xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
2186
0
{
2187
0
    unsigned long pos;
2188
2189
0
    if ((ctx == NULL) || (node == NULL))
2190
0
        return (NULL);
2191
    /* Find position where node should be at */
2192
0
    pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
2193
0
    if (pos < ctx->node_seq.length
2194
0
        && ctx->node_seq.buffer[pos].node == node)
2195
0
        return &ctx->node_seq.buffer[pos];
2196
0
    else
2197
0
        return NULL;
2198
0
}
2199
2200
2201
/**
2202
 * xmlInitNodeInfoSeq:
2203
 * @seq:  a node info sequence pointer
2204
 *
2205
 * DEPRECATED: Don't use.
2206
 *
2207
 * -- Initialize (set to initial state) node info sequence
2208
 */
2209
void
2210
xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
2211
410
{
2212
410
    if (seq == NULL)
2213
0
        return;
2214
410
    seq->length = 0;
2215
410
    seq->maximum = 0;
2216
410
    seq->buffer = NULL;
2217
410
}
2218
2219
/**
2220
 * xmlClearNodeInfoSeq:
2221
 * @seq:  a node info sequence pointer
2222
 *
2223
 * DEPRECATED: Don't use.
2224
 *
2225
 * -- Clear (release memory and reinitialize) node
2226
 *   info sequence
2227
 */
2228
void
2229
xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
2230
0
{
2231
0
    if (seq == NULL)
2232
0
        return;
2233
0
    if (seq->buffer != NULL)
2234
0
        xmlFree(seq->buffer);
2235
0
    xmlInitNodeInfoSeq(seq);
2236
0
}
2237
2238
/**
2239
 * xmlParserFindNodeInfoIndex:
2240
 * @seq:  a node info sequence pointer
2241
 * @node:  an XML node pointer
2242
 *
2243
 * DEPRECATED: Don't use.
2244
 *
2245
 * xmlParserFindNodeInfoIndex : Find the index that the info record for
2246
 *   the given node is or should be at in a sorted sequence
2247
 *
2248
 * Returns a long indicating the position of the record
2249
 */
2250
unsigned long
2251
xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
2252
                           const xmlNodePtr node)
2253
0
{
2254
0
    unsigned long upper, lower, middle;
2255
0
    int found = 0;
2256
2257
0
    if ((seq == NULL) || (node == NULL))
2258
0
        return ((unsigned long) -1);
2259
2260
    /* Do a binary search for the key */
2261
0
    lower = 1;
2262
0
    upper = seq->length;
2263
0
    middle = 0;
2264
0
    while (lower <= upper && !found) {
2265
0
        middle = lower + (upper - lower) / 2;
2266
0
        if (node == seq->buffer[middle - 1].node)
2267
0
            found = 1;
2268
0
        else if (node < seq->buffer[middle - 1].node)
2269
0
            upper = middle - 1;
2270
0
        else
2271
0
            lower = middle + 1;
2272
0
    }
2273
2274
    /* Return position */
2275
0
    if (middle == 0 || seq->buffer[middle - 1].node < node)
2276
0
        return middle;
2277
0
    else
2278
0
        return middle - 1;
2279
0
}
2280
2281
2282
/**
2283
 * xmlParserAddNodeInfo:
2284
 * @ctxt:  an XML parser context
2285
 * @info:  a node info sequence pointer
2286
 *
2287
 * DEPRECATED: Don't use.
2288
 *
2289
 * Insert node info record into the sorted sequence
2290
 */
2291
void
2292
xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
2293
                     const xmlParserNodeInfoPtr info)
2294
0
{
2295
0
    unsigned long pos;
2296
2297
0
    if ((ctxt == NULL) || (info == NULL)) return;
2298
2299
    /* Find pos and check to see if node is already in the sequence */
2300
0
    pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
2301
0
                                     info->node);
2302
2303
0
    if ((pos < ctxt->node_seq.length) &&
2304
0
        (ctxt->node_seq.buffer != NULL) &&
2305
0
        (ctxt->node_seq.buffer[pos].node == info->node)) {
2306
0
        ctxt->node_seq.buffer[pos] = *info;
2307
0
    }
2308
2309
    /* Otherwise, we need to add new node to buffer */
2310
0
    else {
2311
0
        if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) ||
2312
0
      (ctxt->node_seq.buffer == NULL)) {
2313
0
            xmlParserNodeInfo *tmp_buffer;
2314
0
            unsigned int byte_size;
2315
2316
0
            if (ctxt->node_seq.maximum == 0)
2317
0
                ctxt->node_seq.maximum = 2;
2318
0
            byte_size = (sizeof(*ctxt->node_seq.buffer) *
2319
0
      (2 * ctxt->node_seq.maximum));
2320
2321
0
            if (ctxt->node_seq.buffer == NULL)
2322
0
                tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
2323
0
            else
2324
0
                tmp_buffer =
2325
0
                    (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2326
0
                                                     byte_size);
2327
2328
0
            if (tmp_buffer == NULL) {
2329
0
    xmlErrMemory(ctxt, "failed to allocate buffer\n");
2330
0
                return;
2331
0
            }
2332
0
            ctxt->node_seq.buffer = tmp_buffer;
2333
0
            ctxt->node_seq.maximum *= 2;
2334
0
        }
2335
2336
        /* If position is not at end, move elements out of the way */
2337
0
        if (pos != ctxt->node_seq.length) {
2338
0
            unsigned long i;
2339
2340
0
            for (i = ctxt->node_seq.length; i > pos; i--)
2341
0
                ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2342
0
        }
2343
2344
        /* Copy element and increase length */
2345
0
        ctxt->node_seq.buffer[pos] = *info;
2346
0
        ctxt->node_seq.length++;
2347
0
    }
2348
0
}
2349
2350
/************************************************************************
2351
 *                  *
2352
 *    Defaults settings         *
2353
 *                  *
2354
 ************************************************************************/
2355
/**
2356
 * xmlPedanticParserDefault:
2357
 * @val:  int 0 or 1
2358
 *
2359
 * DEPRECATED: Use the modern options API with XML_PARSE_PEDANTIC.
2360
 *
2361
 * Set and return the previous value for enabling pedantic warnings.
2362
 *
2363
 * Returns the last value for 0 for no substitution, 1 for substitution.
2364
 */
2365
2366
int
2367
0
xmlPedanticParserDefault(int val) {
2368
0
    int old = xmlPedanticParserDefaultValue;
2369
2370
0
    xmlPedanticParserDefaultValue = val;
2371
0
    return(old);
2372
0
}
2373
2374
/**
2375
 * xmlLineNumbersDefault:
2376
 * @val:  int 0 or 1
2377
 *
2378
 * DEPRECATED: The modern options API always enables line numbers.
2379
 *
2380
 * Set and return the previous value for enabling line numbers in elements
2381
 * contents. This may break on old application and is turned off by default.
2382
 *
2383
 * Returns the last value for 0 for no substitution, 1 for substitution.
2384
 */
2385
2386
int
2387
0
xmlLineNumbersDefault(int val) {
2388
0
    int old = xmlLineNumbersDefaultValue;
2389
2390
0
    xmlLineNumbersDefaultValue = val;
2391
0
    return(old);
2392
0
}
2393
2394
/**
2395
 * xmlSubstituteEntitiesDefault:
2396
 * @val:  int 0 or 1
2397
 *
2398
 * DEPRECATED: Use the modern options API with XML_PARSE_NOENT.
2399
 *
2400
 * Set and return the previous value for default entity support.
2401
 * Initially the parser always keep entity references instead of substituting
2402
 * entity values in the output. This function has to be used to change the
2403
 * default parser behavior
2404
 * SAX::substituteEntities() has to be used for changing that on a file by
2405
 * file basis.
2406
 *
2407
 * Returns the last value for 0 for no substitution, 1 for substitution.
2408
 */
2409
2410
int
2411
0
xmlSubstituteEntitiesDefault(int val) {
2412
0
    int old = xmlSubstituteEntitiesDefaultValue;
2413
2414
0
    xmlSubstituteEntitiesDefaultValue = val;
2415
0
    return(old);
2416
0
}
2417
2418
/**
2419
 * xmlKeepBlanksDefault:
2420
 * @val:  int 0 or 1
2421
 *
2422
 * DEPRECATED: Use the modern options API with XML_PARSE_NOBLANKS.
2423
 *
2424
 * Set and return the previous value for default blanks text nodes support.
2425
 * The 1.x version of the parser used an heuristic to try to detect
2426
 * ignorable white spaces. As a result the SAX callback was generating
2427
 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
2428
 * using the DOM output text nodes containing those blanks were not generated.
2429
 * The 2.x and later version will switch to the XML standard way and
2430
 * ignorableWhitespace() are only generated when running the parser in
2431
 * validating mode and when the current element doesn't allow CDATA or
2432
 * mixed content.
2433
 * This function is provided as a way to force the standard behavior
2434
 * on 1.X libs and to switch back to the old mode for compatibility when
2435
 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2436
 * by using xmlIsBlankNode() commodity function to detect the "empty"
2437
 * nodes generated.
2438
 * This value also affect autogeneration of indentation when saving code
2439
 * if blanks sections are kept, indentation is not generated.
2440
 *
2441
 * Returns the last value for 0 for no substitution, 1 for substitution.
2442
 */
2443
2444
int
2445
0
xmlKeepBlanksDefault(int val) {
2446
0
    int old = xmlKeepBlanksDefaultValue;
2447
2448
0
    xmlKeepBlanksDefaultValue = val;
2449
0
    if (!val) xmlIndentTreeOutput = 1;
2450
0
    return(old);
2451
0
}
2452