Coverage Report

Created: 2025-07-11 06:12

/src/libxml2/SAX2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SAX2.c : Default SAX2 handler to build a tree.
3
 *
4
 * See Copyright for the status of this software.
5
 *
6
 * Author: Daniel Veillard
7
 */
8
9
10
#define IN_LIBXML
11
#include "libxml.h"
12
#include <stdlib.h>
13
#include <string.h>
14
#include <limits.h>
15
#include <stddef.h>
16
#include <libxml/SAX2.h>
17
#include <libxml/xmlmemory.h>
18
#include <libxml/tree.h>
19
#include <libxml/parser.h>
20
#include <libxml/parserInternals.h>
21
#include <libxml/valid.h>
22
#include <libxml/entities.h>
23
#include <libxml/xmlerror.h>
24
#include <libxml/xmlIO.h>
25
#include <libxml/uri.h>
26
#include <libxml/valid.h>
27
#include <libxml/HTMLtree.h>
28
29
#include "private/error.h"
30
#include "private/parser.h"
31
#include "private/tree.h"
32
33
/*
34
 * @param ctxt  an XML validation parser context
35
 * @param msg  a string to accompany the error message
36
 */
37
static void
38
0
xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt) {
39
0
    xmlCtxtErrMemory(ctxt);
40
0
}
41
42
#ifdef LIBXML_VALID_ENABLED
43
/**
44
 * Handle a validation error
45
 *
46
 * @param ctxt  an XML validation parser context
47
 * @param error  the error number
48
 * @param msg  the error message
49
 * @param str1  extra data
50
 * @param str2  extra data
51
 */
52
static void LIBXML_ATTR_FORMAT(3,0)
53
xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
54
            const char *msg, const xmlChar *str1, const xmlChar *str2)
55
{
56
    xmlCtxtErr(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR,
57
               str1, str2, NULL, 0, msg, str1, str2);
58
    if (ctxt != NULL)
59
  ctxt->valid = 0;
60
}
61
#endif /* LIBXML_VALID_ENABLED */
62
63
/**
64
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
65
 *
66
 * @param ctxt  an XML parser context
67
 * @param error  the error number
68
 * @param msg  the error message
69
 * @param str1  an error string
70
 * @param str2  an error string
71
 */
72
static void LIBXML_ATTR_FORMAT(3,0)
73
xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
74
               const char *msg, const xmlChar *str1, const xmlChar *str2)
75
0
{
76
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
77
0
               str1, str2, NULL, 0, msg, str1, str2);
78
0
}
79
80
/**
81
 * Handle an xml:id error
82
 *
83
 * @param ctxt  an XML validation parser context
84
 * @param error  the error number
85
 * @param msg  the error message
86
 * @param str1  extra data
87
 */
88
static void LIBXML_ATTR_FORMAT(3,0)
89
xmlErrId(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg,
90
         const xmlChar *str1)
91
4.59k
{
92
4.59k
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_ERROR,
93
4.59k
               str1, NULL, NULL, 0, msg, str1);
94
4.59k
}
95
96
/**
97
 * Handle a parser warning
98
 *
99
 * @param ctxt  an XML parser context
100
 * @param error  the error number
101
 * @param msg  the error message
102
 * @param str1  an error string
103
 */
104
static void LIBXML_ATTR_FORMAT(3,0)
105
xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
106
               const char *msg, const xmlChar *str1)
107
0
{
108
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
109
0
               str1, NULL, NULL, 0, msg, str1);
110
0
}
111
112
/**
113
 * Handle a namespace warning
114
 *
115
 * @param ctxt  an XML parser context
116
 * @param error  the error number
117
 * @param msg  the error message
118
 * @param str1  an error string
119
 * @param str2  an error string
120
 */
121
static void LIBXML_ATTR_FORMAT(3,0)
122
xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
123
             const char *msg, const xmlChar *str1, const xmlChar *str2)
124
0
{
125
0
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
126
0
               str1, str2, NULL, 0, msg, str1, str2);
127
0
}
128
129
/**
130
 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
131
 *
132
 * @param ctx  the user data (XML parser context)
133
 * @returns a xmlChar *
134
 */
135
const xmlChar *
136
xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
137
0
{
138
    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
139
0
    return(NULL);
140
0
}
141
142
/**
143
 * Provides the system ID, basically URL or filename e.g.
144
 * http://www.sgmlsource.com/dtds/memo.dtd
145
 *
146
 * @param ctx  the user data (XML parser context)
147
 * @returns a xmlChar *
148
 */
149
const xmlChar *
150
xmlSAX2GetSystemId(void *ctx)
151
0
{
152
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
153
0
    if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
154
0
    return((const xmlChar *) ctxt->input->filename);
155
0
}
156
157
/**
158
 * Provide the line number of the current parsing point.
159
 *
160
 * @param ctx  the user data (XML parser context)
161
 * @returns an int
162
 */
163
int
164
xmlSAX2GetLineNumber(void *ctx)
165
0
{
166
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
167
0
    if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
168
0
    return(ctxt->input->line);
169
0
}
170
171
/**
172
 * Provide the column number of the current parsing point.
173
 *
174
 * @param ctx  the user data (XML parser context)
175
 * @returns an int
176
 */
177
int
178
xmlSAX2GetColumnNumber(void *ctx)
179
0
{
180
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
181
0
    if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
182
0
    return(ctxt->input->col);
183
0
}
184
185
/**
186
 * Is this document tagged standalone ?
187
 *
188
 * @param ctx  the user data (XML parser context)
189
 * @returns 1 if true
190
 */
191
int
192
xmlSAX2IsStandalone(void *ctx)
193
0
{
194
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
195
0
    if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
196
0
    return(ctxt->myDoc->standalone == 1);
197
0
}
198
199
/**
200
 * Does this document has an internal subset
201
 *
202
 * @param ctx  the user data (XML parser context)
203
 * @returns 1 if true
204
 */
205
int
206
xmlSAX2HasInternalSubset(void *ctx)
207
0
{
208
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
209
0
    if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
210
0
    return(ctxt->myDoc->intSubset != NULL);
211
0
}
212
213
/**
214
 * Does this document has an external subset
215
 *
216
 * @param ctx  the user data (XML parser context)
217
 * @returns 1 if true
218
 */
219
int
220
xmlSAX2HasExternalSubset(void *ctx)
221
0
{
222
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
223
0
    if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
224
0
    return(ctxt->myDoc->extSubset != NULL);
225
0
}
226
227
/**
228
 * Callback on internal subset declaration.
229
 *
230
 * @param ctx  the user data (XML parser context)
231
 * @param name  the root element name
232
 * @param publicId  public identifier of the DTD (optional)
233
 * @param systemId  system identifier (URL) of the DTD
234
 */
235
void
236
xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
237
         const xmlChar *publicId, const xmlChar *systemId)
238
156
{
239
156
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
240
156
    xmlDtdPtr dtd;
241
156
    if (ctx == NULL) return;
242
243
156
    if (ctxt->myDoc == NULL)
244
0
  return;
245
156
    if ((ctxt->html) && (ctxt->instate != XML_PARSER_MISC))
246
0
        return;
247
156
    dtd = xmlGetIntSubset(ctxt->myDoc);
248
156
    if (dtd != NULL) {
249
0
  xmlUnlinkNode((xmlNodePtr) dtd);
250
0
  xmlFreeDtd(dtd);
251
0
  ctxt->myDoc->intSubset = NULL;
252
0
    }
253
156
    ctxt->myDoc->intSubset =
254
156
  xmlCreateIntSubset(ctxt->myDoc, name, publicId, systemId);
255
156
    if (ctxt->myDoc->intSubset == NULL)
256
0
        xmlSAX2ErrMemory(ctxt);
257
156
}
258
259
/**
260
 * Callback on external subset declaration.
261
 *
262
 * @param ctx  the user data (XML parser context)
263
 * @param name  the root element name
264
 * @param publicId  public identifier of the DTD (optional)
265
 * @param systemId  system identifier (URL) of the DTD
266
 */
267
void
268
xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
269
         const xmlChar *publicId, const xmlChar *systemId)
270
132
{
271
132
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
272
132
    if (ctx == NULL) return;
273
132
    if ((systemId != NULL) &&
274
132
        ((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
275
132
        (((ctxt->validate) || (ctxt->loadsubset & ~XML_SKIP_IDS)) &&
276
0
   (ctxt->wellFormed && ctxt->myDoc))) {
277
  /*
278
   * Try to fetch and parse the external subset.
279
   */
280
0
  xmlParserInputPtr oldinput;
281
0
  int oldinputNr;
282
0
  int oldinputMax;
283
0
  xmlParserInputPtr *oldinputTab;
284
0
  xmlParserInputPtr input = NULL;
285
0
  xmlChar *oldencoding;
286
0
        unsigned long consumed;
287
0
        size_t buffered;
288
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
289
0
        int inputMax = 1;
290
#else
291
        int inputMax = 5;
292
#endif
293
294
  /*
295
   * Ask the Entity resolver to load the damn thing
296
   */
297
0
  if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
298
0
      input = ctxt->sax->resolveEntity(ctxt->userData, publicId,
299
0
                                       systemId);
300
0
  if (input == NULL) {
301
0
      return;
302
0
  }
303
304
0
  if (xmlNewDtd(ctxt->myDoc, name, publicId, systemId) == NULL) {
305
0
            xmlSAX2ErrMemory(ctxt);
306
0
            xmlFreeInputStream(input);
307
0
            return;
308
0
        }
309
310
  /*
311
   * make sure we won't destroy the main document context
312
   */
313
0
  oldinput = ctxt->input;
314
0
  oldinputNr = ctxt->inputNr;
315
0
  oldinputMax = ctxt->inputMax;
316
0
  oldinputTab = ctxt->inputTab;
317
0
  oldencoding = ctxt->encoding;
318
0
  ctxt->encoding = NULL;
319
320
0
  ctxt->inputTab = xmlMalloc(inputMax * sizeof(xmlParserInputPtr));
321
0
  if (ctxt->inputTab == NULL) {
322
0
      xmlSAX2ErrMemory(ctxt);
323
0
            goto error;
324
0
  }
325
0
  ctxt->inputNr = 0;
326
0
  ctxt->inputMax = inputMax;
327
0
  ctxt->input = NULL;
328
0
  if (xmlCtxtPushInput(ctxt, input) < 0)
329
0
            goto error;
330
331
0
  if (input->filename == NULL)
332
0
      input->filename = (char *) xmlCanonicPath(systemId);
333
0
  input->line = 1;
334
0
  input->col = 1;
335
0
  input->base = ctxt->input->cur;
336
0
  input->cur = ctxt->input->cur;
337
0
  input->free = NULL;
338
339
  /*
340
   * let's parse that entity knowing it's an external subset.
341
   */
342
0
  xmlParseExternalSubset(ctxt, publicId, systemId);
343
344
        /*
345
   * Free up the external entities
346
   */
347
348
0
  while (ctxt->inputNr > 1)
349
0
      xmlFreeInputStream(xmlCtxtPopInput(ctxt));
350
351
0
        consumed = ctxt->input->consumed;
352
0
        buffered = ctxt->input->cur - ctxt->input->base;
353
0
        if (buffered > ULONG_MAX - consumed)
354
0
            consumed = ULONG_MAX;
355
0
        else
356
0
            consumed += buffered;
357
0
        if (consumed > ULONG_MAX - ctxt->sizeentities)
358
0
            ctxt->sizeentities = ULONG_MAX;
359
0
        else
360
0
            ctxt->sizeentities += consumed;
361
362
0
error:
363
0
  xmlFreeInputStream(input);
364
0
        xmlFree(ctxt->inputTab);
365
366
  /*
367
   * Restore the parsing context of the main entity
368
   */
369
0
  ctxt->input = oldinput;
370
0
  ctxt->inputNr = oldinputNr;
371
0
  ctxt->inputMax = oldinputMax;
372
0
  ctxt->inputTab = oldinputTab;
373
0
  if (ctxt->encoding != NULL)
374
0
      xmlFree(ctxt->encoding);
375
0
  ctxt->encoding = oldencoding;
376
  /* ctxt->wellFormed = oldwellFormed; */
377
0
    }
378
132
}
379
380
/**
381
 * This is only used to load DTDs. The preferred way to install
382
 * custom resolvers is #xmlCtxtSetResourceLoader.
383
 *
384
 * @param ctx  the user data (XML parser context)
385
 * @param publicId  The public ID of the entity
386
 * @param systemId  The system ID (URL) of the entity
387
 * @returns a parser input.
388
 */
389
xmlParserInput *
390
xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId,
391
                     const xmlChar *systemId)
392
0
{
393
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
394
0
    xmlParserInputPtr ret = NULL;
395
0
    xmlChar *URI = NULL;
396
397
0
    if (ctx == NULL) return(NULL);
398
399
0
    if (systemId != NULL) {
400
0
        const xmlChar *base = NULL;
401
0
        int res;
402
403
0
        if (ctxt->input != NULL)
404
0
            base = BAD_CAST ctxt->input->filename;
405
406
        /*
407
         * We don't really need the 'directory' struct member, but some
408
         * users set it manually to a base URI for memory streams.
409
         */
410
0
        if (base == NULL)
411
0
            base = BAD_CAST ctxt->directory;
412
413
0
        if ((xmlStrlen(systemId) > XML_MAX_URI_LENGTH) ||
414
0
            (xmlStrlen(base) > XML_MAX_URI_LENGTH)) {
415
0
            xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
416
0
            return(NULL);
417
0
        }
418
0
        res = xmlBuildURISafe(systemId, base, &URI);
419
0
        if (URI == NULL) {
420
0
            if (res < 0)
421
0
                xmlSAX2ErrMemory(ctxt);
422
0
            else
423
0
                xmlWarnMsg(ctxt, XML_ERR_INVALID_URI,
424
0
                           "Can't resolve URI: %s\n", systemId);
425
0
            return(NULL);
426
0
        }
427
0
        if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) {
428
0
            xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
429
0
            xmlFree(URI);
430
0
            return(NULL);
431
0
        }
432
0
    }
433
434
0
    ret = xmlLoadResource(ctxt, (const char *) URI,
435
0
                          (const char *) publicId, XML_RESOURCE_DTD);
436
437
0
    xmlFree(URI);
438
0
    return(ret);
439
0
}
440
441
/**
442
 * Get an entity by name
443
 *
444
 * @param ctx  the user data (XML parser context)
445
 * @param name  The entity name
446
 * @returns the xmlEntity if found.
447
 */
448
xmlEntity *
449
xmlSAX2GetEntity(void *ctx, const xmlChar *name)
450
42.2k
{
451
42.2k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
452
42.2k
    xmlEntityPtr ret = NULL;
453
454
42.2k
    if (ctx == NULL) return(NULL);
455
456
42.2k
    if (ctxt->inSubset == 0) {
457
37.9k
  ret = xmlGetPredefinedEntity(name);
458
37.9k
  if (ret != NULL)
459
0
      return(ret);
460
37.9k
    }
461
42.2k
    if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
462
0
  if (ctxt->inSubset == 2) {
463
0
      ctxt->myDoc->standalone = 0;
464
0
      ret = xmlGetDocEntity(ctxt->myDoc, name);
465
0
      ctxt->myDoc->standalone = 1;
466
0
  } else {
467
0
      ret = xmlGetDocEntity(ctxt->myDoc, name);
468
0
      if (ret == NULL) {
469
0
    ctxt->myDoc->standalone = 0;
470
0
    ret = xmlGetDocEntity(ctxt->myDoc, name);
471
0
    if (ret != NULL) {
472
0
        xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
473
0
   "Entity(%s) document marked standalone but requires external subset\n",
474
0
           name, NULL);
475
0
    }
476
0
    ctxt->myDoc->standalone = 1;
477
0
      }
478
0
  }
479
42.2k
    } else {
480
42.2k
  ret = xmlGetDocEntity(ctxt->myDoc, name);
481
42.2k
    }
482
42.2k
    return(ret);
483
42.2k
}
484
485
/**
486
 * Get a parameter entity by name
487
 *
488
 * @param ctx  the user data (XML parser context)
489
 * @param name  The entity name
490
 * @returns the xmlEntity if found.
491
 */
492
xmlEntity *
493
xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
494
3.42k
{
495
3.42k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
496
3.42k
    xmlEntityPtr ret;
497
498
3.42k
    if (ctx == NULL) return(NULL);
499
500
3.42k
    ret = xmlGetParameterEntity(ctxt->myDoc, name);
501
3.42k
    return(ret);
502
3.42k
}
503
504
505
/**
506
 * An entity definition has been parsed
507
 *
508
 * @param ctx  the user data (XML parser context)
509
 * @param name  the entity name
510
 * @param type  the entity type
511
 * @param publicId  The public ID of the entity
512
 * @param systemId  The system ID of the entity
513
 * @param content  the entity value (without processing).
514
 */
515
void
516
xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
517
          const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
518
7.69k
{
519
7.69k
    xmlEntityPtr ent;
520
7.69k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
521
7.69k
    int extSubset;
522
7.69k
    int res;
523
524
7.69k
    if ((ctxt == NULL) || (ctxt->myDoc == NULL))
525
0
        return;
526
527
7.69k
    extSubset = ctxt->inSubset == 2;
528
7.69k
    res = xmlAddEntity(ctxt->myDoc, extSubset, name, type, publicId, systemId,
529
7.69k
                       content, &ent);
530
7.69k
    switch (res) {
531
253
        case XML_ERR_OK:
532
253
            break;
533
0
        case XML_ERR_NO_MEMORY:
534
0
            xmlSAX2ErrMemory(ctxt);
535
0
            return;
536
7.44k
        case XML_WAR_ENTITY_REDEFINED:
537
7.44k
            if (ctxt->pedantic) {
538
0
                if (extSubset)
539
0
                    xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the"
540
0
                               " external subset\n", name);
541
0
                else
542
0
                    xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the"
543
0
                               " internal subset\n", name);
544
0
            }
545
7.44k
            return;
546
0
        case XML_ERR_REDECL_PREDEF_ENTITY:
547
            /*
548
             * Technically an error but it's a common mistake to get double
549
             * escaping according to "4.6 Predefined Entities" wrong.
550
             */
551
0
            xmlWarnMsg(ctxt, res, "Invalid redeclaration of predefined"
552
0
                       " entity '%s'", name);
553
0
            return;
554
0
        default:
555
0
            xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
556
0
                           "Unexpected error code from xmlAddEntity\n",
557
0
                           NULL, NULL);
558
0
            return;
559
7.69k
    }
560
561
253
    if ((ent->URI == NULL) && (systemId != NULL)) {
562
2
        xmlChar *URI;
563
2
        const char *base = NULL;
564
2
        int i;
565
566
4
        for (i = ctxt->inputNr - 1; i >= 0; i--) {
567
2
            if (ctxt->inputTab[i]->filename != NULL) {
568
0
                base = ctxt->inputTab[i]->filename;
569
0
                break;
570
0
            }
571
2
        }
572
573
        /*
574
         * We don't really need the 'directory' struct member, but some
575
         * users set it manually to a base URI for memory streams.
576
         */
577
2
        if (base == NULL)
578
2
            base = ctxt->directory;
579
580
2
        res = xmlBuildURISafe(systemId, (const xmlChar *) base, &URI);
581
582
2
        if (URI == NULL) {
583
0
            if (res < 0) {
584
0
                xmlSAX2ErrMemory(ctxt);
585
0
            } else {
586
0
                xmlWarnMsg(ctxt, XML_ERR_INVALID_URI,
587
0
                           "Can't resolve URI: %s\n", systemId);
588
0
            }
589
2
        } else if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) {
590
2
            xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
591
2
            xmlFree(URI);
592
2
        } else {
593
0
            ent->URI = URI;
594
0
        }
595
2
    }
596
253
}
597
598
/**
599
 * An attribute definition has been parsed
600
 *
601
 * @param ctx  the user data (XML parser context)
602
 * @param elem  the name of the element
603
 * @param fullname  the attribute name
604
 * @param type  the attribute type
605
 * @param def  the type of default value
606
 * @param defaultValue  the attribute default value
607
 * @param tree  the tree of enumerated value set
608
 */
609
void
610
xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
611
              int type, int def, const xmlChar *defaultValue,
612
        xmlEnumeration *tree)
613
871
{
614
871
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
615
871
    xmlAttributePtr attr;
616
871
    const xmlChar *name = NULL;
617
871
    xmlChar *prefix = NULL;
618
619
    /* Avoid unused variable warning if features are disabled. */
620
871
    (void) attr;
621
622
871
    if ((ctxt == NULL) || (ctxt->myDoc == NULL))
623
0
        return;
624
625
871
    if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
626
871
        (type != XML_ATTRIBUTE_ID)) {
627
0
  xmlErrId(ctxt, XML_DTD_XMLID_TYPE,
628
0
        "xml:id : attribute type should be ID\n", NULL);
629
0
    }
630
871
    name = xmlSplitQName4(fullname, &prefix);
631
871
    if (name == NULL)
632
0
        xmlSAX2ErrMemory(ctxt);
633
871
    ctxt->vctxt.valid = 1;
634
871
    if (ctxt->inSubset == 1)
635
871
  attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
636
871
         name, prefix, (xmlAttributeType) type,
637
871
         (xmlAttributeDefault) def, defaultValue, tree);
638
0
    else if (ctxt->inSubset == 2)
639
0
  attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
640
0
     name, prefix, (xmlAttributeType) type,
641
0
     (xmlAttributeDefault) def, defaultValue, tree);
642
0
    else {
643
0
        xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
644
0
       "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
645
0
                 name, NULL);
646
0
  xmlFree(prefix);
647
0
  xmlFreeEnumeration(tree);
648
0
  return;
649
0
    }
650
#ifdef LIBXML_VALID_ENABLED
651
    if (ctxt->vctxt.valid == 0)
652
  ctxt->valid = 0;
653
    if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
654
        (ctxt->myDoc->intSubset != NULL))
655
  ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
656
                                          attr);
657
#endif /* LIBXML_VALID_ENABLED */
658
871
    if (prefix != NULL)
659
1
  xmlFree(prefix);
660
871
}
661
662
/**
663
 * An element definition has been parsed
664
 *
665
 * @param ctx  the user data (XML parser context)
666
 * @param name  the element name
667
 * @param type  the element type
668
 * @param content  the element value tree
669
 */
670
void
671
xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
672
            xmlElementContent *content)
673
0
{
674
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
675
0
    xmlElementPtr elem = NULL;
676
677
    /* Avoid unused variable warning if features are disabled. */
678
0
    (void) elem;
679
680
0
    if ((ctxt == NULL) || (ctxt->myDoc == NULL))
681
0
        return;
682
683
0
    if (ctxt->inSubset == 1)
684
0
        elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
685
0
                                 name, (xmlElementTypeVal) type, content);
686
0
    else if (ctxt->inSubset == 2)
687
0
        elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
688
0
                                 name, (xmlElementTypeVal) type, content);
689
0
    else {
690
0
        xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
691
0
       "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
692
0
                 name, NULL);
693
0
        return;
694
0
    }
695
#ifdef LIBXML_VALID_ENABLED
696
    if (elem == NULL)
697
        ctxt->valid = 0;
698
    if (ctxt->validate && ctxt->wellFormed &&
699
        ctxt->myDoc && ctxt->myDoc->intSubset)
700
        ctxt->valid &=
701
            xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
702
#endif /* LIBXML_VALID_ENABLED */
703
0
}
704
705
/**
706
 * What to do when a notation declaration has been parsed.
707
 *
708
 * @param ctx  the user data (XML parser context)
709
 * @param name  The name of the notation
710
 * @param publicId  The public ID of the entity
711
 * @param systemId  The system ID of the entity
712
 */
713
void
714
xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
715
       const xmlChar *publicId, const xmlChar *systemId)
716
0
{
717
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
718
0
    xmlNotationPtr nota = NULL;
719
720
    /* Avoid unused variable warning if features are disabled. */
721
0
    (void) nota;
722
723
0
    if ((ctxt == NULL) || (ctxt->myDoc == NULL))
724
0
        return;
725
726
0
    if ((publicId == NULL) && (systemId == NULL)) {
727
0
  xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
728
0
       "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
729
0
                 name, NULL);
730
0
  return;
731
0
    } else if (ctxt->inSubset == 1)
732
0
  nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
733
0
                              publicId, systemId);
734
0
    else if (ctxt->inSubset == 2)
735
0
  nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
736
0
                              publicId, systemId);
737
0
    else {
738
0
  xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
739
0
       "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
740
0
                 name, NULL);
741
0
  return;
742
0
    }
743
#ifdef LIBXML_VALID_ENABLED
744
    if (nota == NULL) ctxt->valid = 0;
745
    if ((ctxt->validate) && (ctxt->wellFormed) &&
746
        (ctxt->myDoc->intSubset != NULL))
747
  ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
748
                                         nota);
749
#endif /* LIBXML_VALID_ENABLED */
750
0
}
751
752
/**
753
 * What to do when an unparsed entity declaration is parsed
754
 *
755
 * @param ctx  the user data (XML parser context)
756
 * @param name  The name of the entity
757
 * @param publicId  The public ID of the entity
758
 * @param systemId  The system ID of the entity
759
 * @param notationName  the name of the notation
760
 */
761
void
762
xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
763
       const xmlChar *publicId, const xmlChar *systemId,
764
       const xmlChar *notationName)
765
0
{
766
0
    xmlSAX2EntityDecl(ctx, name, XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
767
0
                      publicId, systemId, (xmlChar *) notationName);
768
0
}
769
770
/**
771
 * Receive the document locator at startup, actually xmlDefaultSAXLocator
772
 * Everything is available on the context, so this is useless in our case.
773
 *
774
 * @param ctx  the user data (XML parser context)
775
 * @param loc  A SAX Locator
776
 */
777
void
778
xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocator *loc ATTRIBUTE_UNUSED)
779
301
{
780
301
}
781
782
/**
783
 * called when the document start being processed.
784
 *
785
 * @param ctx  the user data (XML parser context)
786
 */
787
void
788
xmlSAX2StartDocument(void *ctx)
789
300
{
790
300
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
791
300
    xmlDocPtr doc;
792
793
300
    if (ctx == NULL) return;
794
795
300
#ifdef LIBXML_HTML_ENABLED
796
300
    if (ctxt->html) {
797
0
  if (ctxt->myDoc == NULL)
798
0
      ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
799
0
  if (ctxt->myDoc == NULL) {
800
0
      xmlSAX2ErrMemory(ctxt);
801
0
      return;
802
0
  }
803
0
  ctxt->myDoc->properties = XML_DOC_HTML;
804
0
  ctxt->myDoc->parseFlags = ctxt->options;
805
0
    } else
806
300
#endif
807
300
    {
808
300
  doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
809
300
  if (doc != NULL) {
810
300
      doc->properties = 0;
811
300
      if (ctxt->options & XML_PARSE_OLD10)
812
0
          doc->properties |= XML_DOC_OLD10;
813
300
      doc->parseFlags = ctxt->options;
814
300
      doc->standalone = ctxt->standalone;
815
300
  } else {
816
0
      xmlSAX2ErrMemory(ctxt);
817
0
      return;
818
0
  }
819
300
  if ((ctxt->dictNames) && (doc != NULL)) {
820
300
      doc->dict = ctxt->dict;
821
300
      xmlDictReference(doc->dict);
822
300
  }
823
300
    }
824
300
    if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
825
300
  (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
826
0
  ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
827
0
  if (ctxt->myDoc->URL == NULL)
828
0
      xmlSAX2ErrMemory(ctxt);
829
0
    }
830
300
}
831
832
/**
833
 * called when the document end has been detected.
834
 *
835
 * @param ctx  the user data (XML parser context)
836
 */
837
void
838
xmlSAX2EndDocument(void *ctx)
839
300
{
840
300
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
841
300
    xmlDocPtr doc;
842
843
300
    if (ctx == NULL) return;
844
#ifdef LIBXML_VALID_ENABLED
845
    if (ctxt->validate && ctxt->wellFormed &&
846
        ctxt->myDoc && ctxt->myDoc->intSubset)
847
  ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
848
#endif /* LIBXML_VALID_ENABLED */
849
850
300
    doc = ctxt->myDoc;
851
300
    if (doc == NULL)
852
0
        return;
853
854
300
    if (doc->encoding == NULL) {
855
300
        const xmlChar *encoding = xmlGetActualEncoding(ctxt);
856
857
300
        if (encoding != NULL) {
858
131
            doc->encoding = xmlStrdup(encoding);
859
131
            if (doc->encoding == NULL)
860
0
                xmlSAX2ErrMemory(ctxt);
861
131
        }
862
300
    }
863
864
300
#ifdef LIBXML_HTML_ENABLED
865
300
    if (ctxt->html) {
866
0
        if (((ctxt->options & HTML_PARSE_NODEFDTD) == 0) &&
867
0
            (doc->intSubset == NULL)) {
868
0
            doc->intSubset = xmlCreateIntSubset(doc, BAD_CAST "html",
869
0
                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
870
0
                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
871
0
            if (doc->intSubset == NULL)
872
0
                xmlSAX2ErrMemory(ctxt);
873
0
        }
874
0
    } else
875
300
#endif /* LIBXML_HTML_ENABLED */
876
300
    {
877
300
        if (ctxt->wellFormed) {
878
0
            doc->properties |= XML_DOC_WELLFORMED;
879
0
            if (ctxt->valid)
880
0
                doc->properties |= XML_DOC_DTDVALID;
881
0
            if (ctxt->nsWellFormed)
882
0
                doc->properties |= XML_DOC_NSVALID;
883
0
        }
884
885
300
        if (ctxt->options & XML_PARSE_OLD10)
886
0
            doc->properties |= XML_DOC_OLD10;
887
300
    }
888
300
}
889
890
static void
891
2.81M
xmlSAX2AppendChild(xmlParserCtxtPtr ctxt, xmlNodePtr node) {
892
2.81M
    xmlNodePtr parent;
893
2.81M
    xmlNodePtr last;
894
895
2.81M
    if (ctxt->inSubset == 1) {
896
40
  parent = (xmlNodePtr) ctxt->myDoc->intSubset;
897
2.81M
    } else if (ctxt->inSubset == 2) {
898
0
  parent = (xmlNodePtr) ctxt->myDoc->extSubset;
899
2.81M
    } else {
900
2.81M
        parent = ctxt->node;
901
2.81M
        if (parent == NULL)
902
290
            parent = (xmlNodePtr) ctxt->myDoc;
903
2.81M
    }
904
905
2.81M
    last = parent->last;
906
2.81M
    if (last == NULL) {
907
23.7k
        parent->children = node;
908
2.78M
    } else {
909
2.78M
        last->next = node;
910
2.78M
        node->prev = last;
911
2.78M
    }
912
913
2.81M
    parent->last = node;
914
2.81M
    node->parent = parent;
915
916
2.81M
    if ((node->type != XML_TEXT_NODE) &&
917
2.81M
  (ctxt->input != NULL)) {
918
2.52M
        if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
919
2.51M
            node->line = ctxt->input->line;
920
3.23k
        else
921
3.23k
            node->line = USHRT_MAX;
922
2.52M
    }
923
2.81M
}
924
925
#if defined(LIBXML_SAX1_ENABLED)
926
/**
927
 * Handle a namespace error
928
 *
929
 * @param ctxt  an XML parser context
930
 * @param error  the error number
931
 * @param msg  the error message
932
 * @param str1  an error string
933
 * @param str2  an error string
934
 */
935
static void LIBXML_ATTR_FORMAT(3,0)
936
xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
937
            const char *msg, const xmlChar *str1, const xmlChar *str2)
938
{
939
    xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
940
               str1, str2, NULL, 0, msg, str1, str2);
941
}
942
943
/**
944
 * Handle an attribute that has been read by the parser.
945
 *
946
 * Deprecated SAX1 interface.
947
 *
948
 * @param ctxt  the parser context
949
 * @param fullname  the attribute name, including namespace prefix
950
 * @param value  the attribute value
951
 * @param prefix  the namespace prefix
952
 */
953
static void
954
xmlSAX1Attribute(xmlParserCtxtPtr ctxt, const xmlChar *fullname,
955
                 const xmlChar *value, const xmlChar *prefix)
956
{
957
    xmlAttrPtr ret;
958
    const xmlChar *name;
959
    xmlChar *ns;
960
    xmlNsPtr namespace;
961
962
    /*
963
     * Split the full name into a namespace prefix and the tag name
964
     */
965
    name = xmlSplitQName4(fullname, &ns);
966
    if (name == NULL) {
967
        xmlSAX2ErrMemory(ctxt);
968
        return;
969
    }
970
971
    /*
972
     * Check whether it's a namespace definition
973
     */
974
    if ((ns == NULL) &&
975
        (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
976
        (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
977
  xmlNsPtr nsret;
978
  xmlChar *val;
979
980
        /* Avoid unused variable warning if features are disabled. */
981
        (void) nsret;
982
983
        if (!ctxt->replaceEntities) {
984
            /* TODO: normalize if needed */
985
      val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0);
986
      if (val == NULL) {
987
          xmlSAX2ErrMemory(ctxt);
988
    return;
989
      }
990
  } else {
991
      val = (xmlChar *) value;
992
  }
993
994
  if (val[0] != 0) {
995
      xmlURIPtr uri;
996
997
      if (xmlParseURISafe((const char *)val, &uri) < 0)
998
                xmlSAX2ErrMemory(ctxt);
999
      if (uri == NULL) {
1000
                xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
1001
                             "xmlns:%s: %s not a valid URI\n", name, value);
1002
      } else {
1003
    if (uri->scheme == NULL) {
1004
                    xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
1005
                                 "xmlns:%s: URI %s is not absolute\n",
1006
                                 name, value);
1007
    }
1008
    xmlFreeURI(uri);
1009
      }
1010
  }
1011
1012
  /* a default namespace definition */
1013
  nsret = xmlNewNs(ctxt->node, val, NULL);
1014
        if (nsret == NULL) {
1015
            xmlSAX2ErrMemory(ctxt);
1016
        }
1017
#ifdef LIBXML_VALID_ENABLED
1018
  /*
1019
   * Validate also for namespace decls, they are attributes from
1020
   * an XML-1.0 perspective
1021
   */
1022
        else if (ctxt->validate && ctxt->wellFormed &&
1023
                 ctxt->myDoc && ctxt->myDoc->intSubset) {
1024
      ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1025
             ctxt->node, prefix, nsret, val);
1026
        }
1027
#endif /* LIBXML_VALID_ENABLED */
1028
  if (val != value)
1029
      xmlFree(val);
1030
  return;
1031
    }
1032
    if ((ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1033
        (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1034
  xmlNsPtr nsret;
1035
  xmlChar *val;
1036
1037
        /* Avoid unused variable warning if features are disabled. */
1038
        (void) nsret;
1039
1040
        if (!ctxt->replaceEntities) {
1041
            /* TODO: normalize if needed */
1042
      val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0);
1043
      if (val == NULL) {
1044
          xmlSAX2ErrMemory(ctxt);
1045
          xmlFree(ns);
1046
    return;
1047
      }
1048
  } else {
1049
      val = (xmlChar *) value;
1050
  }
1051
1052
  if (val[0] == 0) {
1053
      xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1054
            "Empty namespace name for prefix %s\n", name, NULL);
1055
  }
1056
  if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1057
      xmlURIPtr uri;
1058
1059
      if (xmlParseURISafe((const char *)val, &uri) < 0)
1060
                xmlSAX2ErrMemory(ctxt);
1061
      if (uri == NULL) {
1062
          xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
1063
       "xmlns:%s: %s not a valid URI\n", name, value);
1064
      } else {
1065
    if (uri->scheme == NULL) {
1066
        xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
1067
         "xmlns:%s: URI %s is not absolute\n", name, value);
1068
    }
1069
    xmlFreeURI(uri);
1070
      }
1071
  }
1072
1073
  /* a standard namespace definition */
1074
  nsret = xmlNewNs(ctxt->node, val, name);
1075
  xmlFree(ns);
1076
1077
        if (nsret == NULL) {
1078
            xmlSAX2ErrMemory(ctxt);
1079
        }
1080
#ifdef LIBXML_VALID_ENABLED
1081
  /*
1082
   * Validate also for namespace decls, they are attributes from
1083
   * an XML-1.0 perspective
1084
   */
1085
        else if (ctxt->validate && ctxt->wellFormed &&
1086
           ctxt->myDoc && ctxt->myDoc->intSubset) {
1087
      ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1088
             ctxt->node, prefix, nsret, value);
1089
        }
1090
#endif /* LIBXML_VALID_ENABLED */
1091
  if (val != value)
1092
      xmlFree(val);
1093
  return;
1094
    }
1095
1096
    if (ns != NULL) {
1097
        int res;
1098
1099
  res = xmlSearchNsSafe(ctxt->node, ns, &namespace);
1100
        if (res < 0)
1101
            xmlSAX2ErrMemory(ctxt);
1102
1103
  if (namespace == NULL) {
1104
      xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1105
        "Namespace prefix %s of attribute %s is not defined\n",
1106
                 ns, name);
1107
  } else {
1108
            xmlAttrPtr prop;
1109
1110
            prop = ctxt->node->properties;
1111
            while (prop != NULL) {
1112
                if (prop->ns != NULL) {
1113
                    if ((xmlStrEqual(name, prop->name)) &&
1114
                        ((namespace == prop->ns) ||
1115
                         (xmlStrEqual(namespace->href, prop->ns->href)))) {
1116
                        xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER,
1117
                                   XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL,
1118
                                   name, NULL, NULL, 0,
1119
                                   "Attribute %s in %s redefined\n",
1120
                                   name, namespace->href);
1121
                        goto error;
1122
                    }
1123
                }
1124
                prop = prop->next;
1125
            }
1126
        }
1127
    } else {
1128
  namespace = NULL;
1129
    }
1130
1131
    /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1132
    ret = xmlNewNsProp(ctxt->node, namespace, name, NULL);
1133
    if (ret == NULL) {
1134
        xmlSAX2ErrMemory(ctxt);
1135
        goto error;
1136
    }
1137
1138
    if (ctxt->replaceEntities == 0) {
1139
        if (xmlNodeParseContent((xmlNodePtr) ret, value, INT_MAX) < 0)
1140
            xmlSAX2ErrMemory(ctxt);
1141
    } else if (value != NULL) {
1142
        ret->children = xmlNewDocText(ctxt->myDoc, value);
1143
        if (ret->children == NULL) {
1144
            xmlSAX2ErrMemory(ctxt);
1145
        } else {
1146
            ret->last = ret->children;
1147
            ret->children->parent = (xmlNodePtr) ret;
1148
        }
1149
    }
1150
1151
#ifdef LIBXML_VALID_ENABLED
1152
    if (ctxt->validate && ctxt->wellFormed &&
1153
        ctxt->myDoc && ctxt->myDoc->intSubset) {
1154
1155
  /*
1156
   * If we don't substitute entities, the validation should be
1157
   * done on a value with replaced entities anyway.
1158
   */
1159
        if (!ctxt->replaceEntities) {
1160
      xmlChar *val;
1161
1162
            /* TODO: normalize if needed */
1163
      val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0);
1164
1165
      if (val == NULL)
1166
    ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1167
        ctxt->myDoc, ctxt->node, ret, value);
1168
      else {
1169
    xmlChar *nvalnorm;
1170
1171
    /*
1172
     * Do the last stage of the attribute normalization
1173
     * It need to be done twice ... it's an extra burden related
1174
     * to the ability to keep xmlSAX2References in attributes
1175
     */
1176
                nvalnorm = xmlValidCtxtNormalizeAttributeValue(
1177
                                 &ctxt->vctxt, ctxt->myDoc,
1178
                                 ctxt->node, fullname, val);
1179
    if (nvalnorm != NULL) {
1180
        xmlFree(val);
1181
        val = nvalnorm;
1182
    }
1183
1184
    ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1185
              ctxt->myDoc, ctxt->node, ret, val);
1186
                xmlFree(val);
1187
      }
1188
  } else {
1189
      ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1190
                 ctxt->node, ret, value);
1191
  }
1192
    } else
1193
#endif /* LIBXML_VALID_ENABLED */
1194
           if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1195
               (ctxt->input->entity == NULL) &&
1196
               /* Don't create IDs containing entity references */
1197
               (ret->children != NULL) &&
1198
               (ret->children->type == XML_TEXT_NODE) &&
1199
               (ret->children->next == NULL)) {
1200
        xmlChar *content = ret->children->content;
1201
        /*
1202
   * when validating, the ID registration is done at the attribute
1203
   * validation level. Otherwise we have to do specific handling here.
1204
   */
1205
  if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
1206
      /*
1207
       * Add the xml:id value
1208
       *
1209
       * Open issue: normalization of the value.
1210
       */
1211
      if (xmlValidateNCName(content, 1) != 0) {
1212
          xmlErrId(ctxt, XML_DTD_XMLID_VALUE,
1213
             "xml:id : attribute value %s is not an NCName\n",
1214
             content);
1215
      }
1216
      xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
1217
  } else {
1218
            int res = xmlIsID(ctxt->myDoc, ctxt->node, ret);
1219
1220
            if (res < 0)
1221
                xmlCtxtErrMemory(ctxt);
1222
            else if (res > 0)
1223
                xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
1224
            else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1225
                xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
1226
        }
1227
    }
1228
1229
error:
1230
    if (ns != NULL)
1231
  xmlFree(ns);
1232
}
1233
1234
/*
1235
 *
1236
 * Check defaulted attributes from the DTD
1237
 *
1238
 * Deprecated SAX1 interface.
1239
 */
1240
static void
1241
xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1242
  const xmlChar *prefix, const xmlChar **atts) {
1243
    xmlElementPtr elemDecl;
1244
    const xmlChar *att;
1245
    int internal = 1;
1246
    int i;
1247
1248
    elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1249
    if (elemDecl == NULL) {
1250
  elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1251
  internal = 0;
1252
    }
1253
1254
process_external_subset:
1255
1256
    if (elemDecl != NULL) {
1257
  xmlAttributePtr attr = elemDecl->attributes;
1258
1259
#ifdef LIBXML_VALID_ENABLED
1260
        /*
1261
         * Check against defaulted attributes from the external subset
1262
         * if the document is stamped as standalone.
1263
         *
1264
         * This should be moved to valid.c, but we don't keep track
1265
         * whether an attribute was defaulted.
1266
         */
1267
  if ((ctxt->myDoc->standalone == 1) &&
1268
      (ctxt->myDoc->extSubset != NULL) &&
1269
      (ctxt->validate)) {
1270
      while (attr != NULL) {
1271
    if ((attr->defaultValue != NULL) &&
1272
        (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1273
          attr->elem, attr->name,
1274
          attr->prefix) == attr) &&
1275
        (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1276
          attr->elem, attr->name,
1277
          attr->prefix) == NULL)) {
1278
        xmlChar *fulln;
1279
1280
        if (attr->prefix != NULL) {
1281
      fulln = xmlStrdup(attr->prefix);
1282
                        if (fulln != NULL)
1283
          fulln = xmlStrcat(fulln, BAD_CAST ":");
1284
                        if (fulln != NULL)
1285
          fulln = xmlStrcat(fulln, attr->name);
1286
        } else {
1287
      fulln = xmlStrdup(attr->name);
1288
        }
1289
                    if (fulln == NULL) {
1290
                        xmlSAX2ErrMemory(ctxt);
1291
                        break;
1292
                    }
1293
1294
        /*
1295
         * Check that the attribute is not declared in the
1296
         * serialization
1297
         */
1298
        att = NULL;
1299
        if (atts != NULL) {
1300
      i = 0;
1301
      att = atts[i];
1302
      while (att != NULL) {
1303
          if (xmlStrEqual(att, fulln))
1304
        break;
1305
          i += 2;
1306
          att = atts[i];
1307
      }
1308
        }
1309
        if (att == NULL) {
1310
            xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
1311
      "standalone: attribute %s on %s defaulted from external subset\n",
1312
            fulln,
1313
            attr->elem);
1314
        }
1315
                    xmlFree(fulln);
1316
    }
1317
    attr = attr->nexth;
1318
      }
1319
  }
1320
#endif
1321
1322
  /*
1323
   * Actually insert defaulted values when needed
1324
   */
1325
  attr = elemDecl->attributes;
1326
  while (attr != NULL) {
1327
      /*
1328
       * Make sure that attributes redefinition occurring in the
1329
       * internal subset are not overridden by definitions in the
1330
       * external subset.
1331
       */
1332
      if (attr->defaultValue != NULL) {
1333
    /*
1334
     * the element should be instantiated in the tree if:
1335
     *  - this is a namespace prefix
1336
     *  - the user required for completion in the tree
1337
     *    like XSLT
1338
     *  - there isn't already an attribute definition
1339
     *    in the internal subset overriding it.
1340
     */
1341
    if (((attr->prefix != NULL) &&
1342
         (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1343
        ((attr->prefix == NULL) &&
1344
         (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1345
        (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1346
        xmlAttributePtr tst;
1347
1348
        tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1349
               attr->elem, attr->name,
1350
               attr->prefix);
1351
        if ((tst == attr) || (tst == NULL)) {
1352
            xmlChar fn[50];
1353
      xmlChar *fulln;
1354
1355
                        fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1356
      if (fulln == NULL) {
1357
          xmlSAX2ErrMemory(ctxt);
1358
          return;
1359
      }
1360
1361
      /*
1362
       * Check that the attribute is not declared in the
1363
       * serialization
1364
       */
1365
      att = NULL;
1366
      if (atts != NULL) {
1367
          i = 0;
1368
          att = atts[i];
1369
          while (att != NULL) {
1370
        if (xmlStrEqual(att, fulln))
1371
            break;
1372
        i += 2;
1373
        att = atts[i];
1374
          }
1375
      }
1376
      if (att == NULL) {
1377
          xmlSAX1Attribute(ctxt, fulln,
1378
               attr->defaultValue, prefix);
1379
      }
1380
      if ((fulln != fn) && (fulln != attr->name))
1381
          xmlFree(fulln);
1382
        }
1383
    }
1384
      }
1385
      attr = attr->nexth;
1386
  }
1387
  if (internal == 1) {
1388
      elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1389
                                 name, prefix);
1390
      internal = 0;
1391
      goto process_external_subset;
1392
  }
1393
    }
1394
}
1395
1396
/**
1397
 * called when an opening tag has been processed.
1398
 *
1399
 * Deprecated SAX1 interface.
1400
 *
1401
 * @param ctx  the user data (XML parser context)
1402
 * @param fullname  The element name, including namespace prefix
1403
 * @param atts  An array of name/value attributes pairs, NULL terminated
1404
 */
1405
static void
1406
xmlSAX1StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1407
{
1408
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1409
    xmlNodePtr ret;
1410
    xmlNodePtr parent;
1411
    xmlNsPtr ns;
1412
    const xmlChar *name;
1413
    xmlChar *prefix;
1414
    const xmlChar *att;
1415
    const xmlChar *value;
1416
    int i, res;
1417
1418
    if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
1419
1420
#ifdef LIBXML_VALID_ENABLED
1421
    /*
1422
     * First check on validity:
1423
     */
1424
    if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1425
        ((ctxt->myDoc->intSubset == NULL) ||
1426
   ((ctxt->myDoc->intSubset->notations == NULL) &&
1427
    (ctxt->myDoc->intSubset->elements == NULL) &&
1428
    (ctxt->myDoc->intSubset->attributes == NULL) &&
1429
    (ctxt->myDoc->intSubset->entities == NULL)))) {
1430
  xmlErrValid(ctxt, XML_ERR_NO_DTD,
1431
    "Validation failed: no DTD found !", NULL, NULL);
1432
  ctxt->validate = 0;
1433
    }
1434
#endif
1435
1436
    /*
1437
     * Split the full name into a namespace prefix and the tag name
1438
     */
1439
    name = xmlSplitQName4(fullname, &prefix);
1440
    if (name == NULL) {
1441
        xmlSAX2ErrMemory(ctxt);
1442
        return;
1443
    }
1444
1445
    /*
1446
     * Note : the namespace resolution is deferred until the end of the
1447
     *        attributes parsing, since local namespace can be defined as
1448
     *        an attribute at this level.
1449
     */
1450
    ret = xmlNewDocNode(ctxt->myDoc, NULL, name, NULL);
1451
    if (ret == NULL) {
1452
  xmlFree(prefix);
1453
  xmlSAX2ErrMemory(ctxt);
1454
        return;
1455
    }
1456
    ctxt->nodemem = -1;
1457
1458
    /* Initialize parent before pushing node */
1459
    parent = ctxt->node;
1460
    if (parent == NULL)
1461
        parent = (xmlNodePtr) ctxt->myDoc;
1462
1463
    /*
1464
     * Link the child element
1465
     */
1466
    xmlSAX2AppendChild(ctxt, ret);
1467
1468
    /*
1469
     * We are parsing a new node.
1470
     */
1471
    if (nodePush(ctxt, ret) < 0) {
1472
        xmlUnlinkNode(ret);
1473
        xmlFreeNode(ret);
1474
        if (prefix != NULL)
1475
            xmlFree(prefix);
1476
        return;
1477
    }
1478
1479
    /*
1480
     * Insert all the defaulted attributes from the DTD especially
1481
     * namespaces
1482
     */
1483
    if ((ctxt->myDoc->intSubset != NULL) ||
1484
        (ctxt->myDoc->extSubset != NULL)) {
1485
        xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1486
    }
1487
1488
    /*
1489
     * process all the attributes whose name start with "xmlns"
1490
     */
1491
    if (atts != NULL) {
1492
        i = 0;
1493
        att = atts[i++];
1494
        value = atts[i++];
1495
        while ((att != NULL) && (value != NULL)) {
1496
            if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1497
                (att[3] == 'n') && (att[4] == 's'))
1498
                xmlSAX1Attribute(ctxt, att, value, prefix);
1499
1500
            att = atts[i++];
1501
            value = atts[i++];
1502
        }
1503
    }
1504
1505
    /*
1506
     * Search the namespace, note that since the attributes have been
1507
     * processed, the local namespaces are available.
1508
     */
1509
    res = xmlSearchNsSafe(ret, prefix, &ns);
1510
    if (res < 0)
1511
        xmlSAX2ErrMemory(ctxt);
1512
    if ((ns == NULL) && (parent != NULL)) {
1513
        res = xmlSearchNsSafe(parent, prefix, &ns);
1514
        if (res < 0)
1515
            xmlSAX2ErrMemory(ctxt);
1516
    }
1517
    if ((prefix != NULL) && (ns == NULL)) {
1518
        xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1519
                     "Namespace prefix %s is not defined\n",
1520
                     prefix, NULL);
1521
        ns = xmlNewNs(ret, NULL, prefix);
1522
        if (ns == NULL)
1523
            xmlSAX2ErrMemory(ctxt);
1524
    }
1525
1526
    /*
1527
     * set the namespace node, making sure that if the default namespace
1528
     * is unbound on a parent we simply keep it NULL
1529
     */
1530
    if ((ns != NULL) && (ns->href != NULL) &&
1531
        ((ns->href[0] != 0) || (ns->prefix != NULL)))
1532
        xmlSetNs(ret, ns);
1533
1534
    /*
1535
     * process all the other attributes
1536
     */
1537
    if (atts != NULL) {
1538
        i = 0;
1539
  att = atts[i++];
1540
  value = atts[i++];
1541
        while ((att != NULL) && (value != NULL)) {
1542
            if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1543
                (att[3] != 'n') || (att[4] != 's'))
1544
                xmlSAX1Attribute(ctxt, att, value, NULL);
1545
1546
            /*
1547
             * Next ones
1548
             */
1549
            att = atts[i++];
1550
            value = atts[i++];
1551
        }
1552
    }
1553
1554
#ifdef LIBXML_VALID_ENABLED
1555
    /*
1556
     * If it's the Document root, finish the DTD validation and
1557
     * check the document root element for validity
1558
     */
1559
    if ((ctxt->validate) &&
1560
        ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) {
1561
  int chk;
1562
1563
  chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1564
  if (chk <= 0)
1565
      ctxt->valid = 0;
1566
  if (chk < 0)
1567
      ctxt->wellFormed = 0;
1568
  ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1569
  ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED;
1570
    }
1571
#endif /* LIBXML_VALID_ENABLED */
1572
1573
    if (prefix != NULL)
1574
  xmlFree(prefix);
1575
1576
}
1577
#endif /* LIBXML_SAX1_ENABLED */
1578
1579
#ifdef LIBXML_HTML_ENABLED
1580
static void
1581
xmlSAX2HtmlAttribute(xmlParserCtxtPtr ctxt, const xmlChar *fullname,
1582
0
                     const xmlChar *value) {
1583
0
    xmlAttrPtr ret;
1584
0
    xmlChar *nval = NULL;
1585
1586
0
    ret = xmlNewNsProp(ctxt->node, NULL, fullname, NULL);
1587
0
    if (ret == NULL) {
1588
0
        xmlSAX2ErrMemory(ctxt);
1589
0
        return;
1590
0
    }
1591
1592
0
    if ((value == NULL) && (htmlIsBooleanAttr(fullname))) {
1593
0
        nval = xmlStrdup(fullname);
1594
0
        if (nval == NULL) {
1595
0
            xmlSAX2ErrMemory(ctxt);
1596
0
            return;
1597
0
        }
1598
0
        value = nval;
1599
0
    }
1600
1601
0
    if (value != NULL) {
1602
0
        ret->children = xmlNewDocText(ctxt->myDoc, value);
1603
0
        if (ret->children == NULL) {
1604
0
            xmlSAX2ErrMemory(ctxt);
1605
0
        } else {
1606
0
            ret->last = ret->children;
1607
0
            ret->children->parent = (xmlNodePtr) ret;
1608
0
        }
1609
0
    }
1610
1611
0
    if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1612
        /*
1613
         * Don't create IDs containing entity references (should
1614
         * be always the case with HTML)
1615
         */
1616
0
        (ret->children != NULL) &&
1617
0
        (ret->children->type == XML_TEXT_NODE) &&
1618
0
        (ret->children->next == NULL)) {
1619
0
        int res = xmlIsID(ctxt->myDoc, ctxt->node, ret);
1620
1621
0
        if (res < 0)
1622
0
            xmlCtxtErrMemory(ctxt);
1623
0
        else if (res > 0)
1624
0
            xmlAddID(&ctxt->vctxt, ctxt->myDoc, ret->children->content, ret);
1625
0
    }
1626
1627
0
    if (nval != NULL)
1628
0
        xmlFree(nval);
1629
0
}
1630
1631
/**
1632
 * Called when an opening tag has been processed.
1633
 *
1634
 * @param ctxt  parser context
1635
 * @param fullname  The element name, including namespace prefix
1636
 * @param atts  An array of name/value attributes pairs, NULL terminated
1637
 */
1638
static void
1639
xmlSAX2StartHtmlElement(xmlParserCtxtPtr ctxt, const xmlChar *fullname,
1640
0
                        const xmlChar **atts) {
1641
0
    xmlNodePtr ret;
1642
0
    xmlNodePtr parent;
1643
0
    const xmlChar *att;
1644
0
    const xmlChar *value;
1645
0
    int i;
1646
1647
0
    ret = xmlNewDocNode(ctxt->myDoc, NULL, fullname, NULL);
1648
0
    if (ret == NULL) {
1649
0
  xmlSAX2ErrMemory(ctxt);
1650
0
        return;
1651
0
    }
1652
0
    ctxt->nodemem = -1;
1653
1654
    /* Initialize parent before pushing node */
1655
0
    parent = ctxt->node;
1656
0
    if (parent == NULL)
1657
0
        parent = (xmlNodePtr) ctxt->myDoc;
1658
1659
    /*
1660
     * Link the child element
1661
     */
1662
0
    xmlSAX2AppendChild(ctxt, ret);
1663
1664
    /*
1665
     * We are parsing a new node.
1666
     */
1667
0
    if (nodePush(ctxt, ret) < 0) {
1668
0
        xmlUnlinkNode(ret);
1669
0
        xmlFreeNode(ret);
1670
0
        return;
1671
0
    }
1672
1673
0
    if (atts != NULL) {
1674
0
        i = 0;
1675
0
  att = atts[i++];
1676
0
  value = atts[i++];
1677
0
        while (att != NULL) {
1678
0
            xmlSAX2HtmlAttribute(ctxt, att, value);
1679
0
            att = atts[i++];
1680
0
            value = atts[i++];
1681
0
        }
1682
0
    }
1683
0
}
1684
#endif /* LIBXML_HTML_ENABLED */
1685
1686
/**
1687
 * Called when an opening tag has been processed.
1688
 *
1689
 * @deprecated Don't call this function directly.
1690
 *
1691
 * Used for HTML and SAX1.
1692
 *
1693
 * @param ctx  the user data (XML parser context)
1694
 * @param fullname  The element name, including namespace prefix
1695
 * @param atts  An array of name/value attributes pairs, NULL terminated
1696
 */
1697
void
1698
0
xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts) {
1699
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1700
1701
0
    (void) atts;
1702
1703
0
    if ((ctxt == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL))
1704
0
        return;
1705
1706
#ifdef LIBXML_SAX1_ENABLED
1707
    if (!ctxt->html) {
1708
        xmlSAX1StartElement(ctxt, fullname, atts);
1709
        return;
1710
    }
1711
#endif
1712
1713
0
#ifdef LIBXML_HTML_ENABLED
1714
0
    if (ctxt->html) {
1715
0
        xmlSAX2StartHtmlElement(ctxt, fullname, atts);
1716
0
        return;
1717
0
    }
1718
0
#endif
1719
0
}
1720
1721
/**
1722
 * called when the end of an element has been detected.
1723
 *
1724
 * @deprecated Don't call this function directly.
1725
 *
1726
 * Used for HTML and SAX1.
1727
 *
1728
 * @param ctx  the user data (XML parser context)
1729
 * @param name  The element name
1730
 */
1731
void
1732
xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1733
0
{
1734
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1735
1736
0
    if (ctxt == NULL)
1737
0
        return;
1738
1739
#if defined(LIBXML_SAX1_ENABLED) && defined(LIBXML_VALID_ENABLED)
1740
    if (!ctxt->html && ctxt->validate && ctxt->wellFormed &&
1741
        ctxt->myDoc && ctxt->myDoc->intSubset)
1742
        ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1743
               ctxt->node);
1744
#endif /* LIBXML_VALID_ENABLED */
1745
1746
0
#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED)
1747
0
    ctxt->nodemem = -1;
1748
1749
    /*
1750
     * end of parsing of this node.
1751
     */
1752
0
    nodePop(ctxt);
1753
0
#endif
1754
0
}
1755
1756
/*
1757
 * @param ctxt  the parser context
1758
 * @param doc  the document
1759
 * @param str  the input string
1760
 * @param len  the string length
1761
 *
1762
 * Callback for a text node
1763
 *
1764
 * @returns the newly allocated string or NULL if not needed or error
1765
 */
1766
static xmlNodePtr
1767
xmlSAX2TextNode(xmlParserCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *str,
1768
315k
                int len) {
1769
315k
    xmlNodePtr ret;
1770
315k
    const xmlChar *intern = NULL;
1771
1772
    /*
1773
     * Allocate
1774
     */
1775
315k
    if (ctxt->freeElems != NULL) {
1776
0
  ret = ctxt->freeElems;
1777
0
  ctxt->freeElems = ret->next;
1778
0
  ctxt->freeElemsNr--;
1779
315k
    } else {
1780
315k
  ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1781
315k
    }
1782
315k
    if (ret == NULL) {
1783
0
        xmlCtxtErrMemory(ctxt);
1784
0
  return(NULL);
1785
0
    }
1786
315k
    memset(ret, 0, sizeof(xmlNode));
1787
    /*
1788
     * intern the formatting blanks found between tags, or the
1789
     * very short strings
1790
     */
1791
315k
    if ((!ctxt->html) && (ctxt->dictNames)) {
1792
315k
        xmlChar cur = str[len];
1793
1794
315k
  if ((len < (int) (2 * sizeof(void *))) &&
1795
315k
      (ctxt->options & XML_PARSE_COMPACT)) {
1796
      /* store the string in the node overriding properties and nsDef */
1797
0
      xmlChar *tmp = (xmlChar *) &(ret->properties);
1798
0
      memcpy(tmp, str, len);
1799
0
      tmp[len] = 0;
1800
0
      intern = tmp;
1801
315k
  } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1802
209k
      ((cur == '<') && (str[len + 1] != '!')))) {
1803
180k
      intern = xmlDictLookup(ctxt->dict, str, len);
1804
180k
            if (intern == NULL) {
1805
0
                xmlSAX2ErrMemory(ctxt);
1806
0
                xmlFree(ret);
1807
0
                return(NULL);
1808
0
            }
1809
180k
  } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
1810
135k
             (str[len + 1] != '!')) {
1811
6.91k
      int i;
1812
1813
41.7k
      for (i = 1;i < len;i++) {
1814
36.7k
    if (!IS_BLANK_CH(str[i])) goto skip;
1815
36.7k
      }
1816
4.96k
      intern = xmlDictLookup(ctxt->dict, str, len);
1817
4.96k
            if (intern == NULL) {
1818
0
                xmlSAX2ErrMemory(ctxt);
1819
0
                xmlFree(ret);
1820
0
                return(NULL);
1821
0
            }
1822
4.96k
  }
1823
315k
    }
1824
315k
skip:
1825
315k
    ret->type = XML_TEXT_NODE;
1826
315k
    ret->doc = doc;
1827
1828
315k
    ret->name = xmlStringText;
1829
315k
    if (intern == NULL) {
1830
130k
  ret->content = xmlStrndup(str, len);
1831
130k
  if (ret->content == NULL) {
1832
0
      xmlSAX2ErrMemory(ctxt);
1833
0
      xmlFree(ret);
1834
0
      return(NULL);
1835
0
  }
1836
130k
    } else
1837
185k
  ret->content = (xmlChar *) intern;
1838
1839
315k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1840
0
  xmlRegisterNodeDefaultValue(ret);
1841
315k
    return(ret);
1842
315k
}
1843
1844
#ifdef LIBXML_VALID_ENABLED
1845
/*
1846
 * @param ctxt  the parser context
1847
 * @param str  the input string
1848
 * @param len  the string length
1849
 *
1850
 * Remove the entities from an attribute value
1851
 *
1852
 * @returns the newly allocated string or NULL if not needed or error
1853
 */
1854
static xmlChar *
1855
xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1856
                          const xmlChar *end) {
1857
    const xmlChar *in;
1858
1859
    in = str;
1860
    while (in < end)
1861
        if (*in++ == '&')
1862
      goto decode;
1863
    return(NULL);
1864
decode:
1865
    /*
1866
     * If the value contains '&', we can be sure it was allocated and is
1867
     * zero-terminated.
1868
     */
1869
    /* TODO: normalize if needed */
1870
    return(xmlExpandEntitiesInAttValue(ctxt, str, /* normalize */ 0));
1871
}
1872
#endif /* LIBXML_VALID_ENABLED */
1873
1874
/**
1875
 * Handle an attribute that has been read by the parser.
1876
 * The default handling is to convert the attribute into an
1877
 * DOM subtree and past it in a new xmlAttr element added to
1878
 * the element.
1879
 *
1880
 * @param ctxt  the parser context
1881
 * @param localname  the local name of the attribute
1882
 * @param prefix  the attribute namespace prefix if available
1883
 * @param value  start of the attribute value
1884
 * @param valueend  end of the attribute value
1885
 * @returns the new attribute or NULL in case of error.
1886
 */
1887
static xmlAttrPtr
1888
xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1889
                   const xmlChar * localname,
1890
                   const xmlChar * prefix,
1891
       const xmlChar * value,
1892
       const xmlChar * valueend)
1893
13.9k
{
1894
13.9k
    xmlAttrPtr ret;
1895
13.9k
    xmlNsPtr namespace = NULL;
1896
13.9k
    xmlChar *dup = NULL;
1897
1898
    /*
1899
     * Note: if prefix == NULL, the attribute is not in the default namespace
1900
     */
1901
13.9k
    if (prefix != NULL) {
1902
6.50k
  namespace = xmlParserNsLookupSax(ctxt, prefix);
1903
6.50k
  if ((namespace == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
1904
6.38k
            int res;
1905
1906
6.38k
      res = xmlSearchNsSafe(ctxt->node, prefix, &namespace);
1907
6.38k
            if (res < 0)
1908
0
                xmlSAX2ErrMemory(ctxt);
1909
6.38k
  }
1910
6.50k
    }
1911
1912
    /*
1913
     * allocate the node
1914
     */
1915
13.9k
    if (ctxt->freeAttrs != NULL) {
1916
0
        ret = ctxt->freeAttrs;
1917
0
  ctxt->freeAttrs = ret->next;
1918
0
  ctxt->freeAttrsNr--;
1919
13.9k
    } else {
1920
13.9k
        ret = xmlMalloc(sizeof(*ret));
1921
13.9k
        if (ret == NULL) {
1922
0
            xmlSAX2ErrMemory(ctxt);
1923
0
            return(NULL);
1924
0
        }
1925
13.9k
    }
1926
1927
13.9k
    memset(ret, 0, sizeof(xmlAttr));
1928
13.9k
    ret->type = XML_ATTRIBUTE_NODE;
1929
1930
    /*
1931
     * xmlParseBalancedChunkMemoryRecover had a bug that could result in
1932
     * a mismatch between ctxt->node->doc and ctxt->myDoc. We use
1933
     * ctxt->node->doc here, but we should somehow make sure that the
1934
     * document pointers match.
1935
     */
1936
1937
    /* assert(ctxt->node->doc == ctxt->myDoc); */
1938
1939
13.9k
    ret->parent = ctxt->node;
1940
13.9k
    ret->doc = ctxt->node->doc;
1941
13.9k
    ret->ns = namespace;
1942
1943
13.9k
    if (ctxt->dictNames) {
1944
13.9k
        ret->name = localname;
1945
13.9k
    } else {
1946
0
        ret->name = xmlStrdup(localname);
1947
0
        if (ret->name == NULL)
1948
0
            xmlSAX2ErrMemory(ctxt);
1949
0
    }
1950
1951
13.9k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1952
0
        xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1953
1954
13.9k
    if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1955
13.9k
  xmlNodePtr tmp;
1956
1957
  /*
1958
   * We know that if there is an entity reference, then
1959
   * the string has been dup'ed and terminates with 0
1960
   * otherwise with ' or "
1961
   */
1962
13.9k
  if (*valueend != 0) {
1963
6.09k
      tmp = xmlSAX2TextNode(ctxt, ret->doc, value, valueend - value);
1964
6.09k
      ret->children = tmp;
1965
6.09k
      ret->last = tmp;
1966
6.09k
      if (tmp != NULL) {
1967
6.09k
    tmp->parent = (xmlNodePtr) ret;
1968
6.09k
      }
1969
7.90k
  } else if (valueend > value) {
1970
7.90k
            if (xmlNodeParseContent((xmlNodePtr) ret, value,
1971
7.90k
                                    valueend - value) < 0)
1972
0
                xmlSAX2ErrMemory(ctxt);
1973
7.90k
  }
1974
13.9k
    } else if (value != NULL) {
1975
0
  xmlNodePtr tmp;
1976
1977
0
  tmp = xmlSAX2TextNode(ctxt, ret->doc, value, valueend - value);
1978
0
  ret->children = tmp;
1979
0
  ret->last = tmp;
1980
0
  if (tmp != NULL) {
1981
0
      tmp->parent = (xmlNodePtr) ret;
1982
0
  }
1983
0
    }
1984
1985
#ifdef LIBXML_VALID_ENABLED
1986
    if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1987
        ctxt->myDoc && ctxt->myDoc->intSubset) {
1988
  /*
1989
   * If we don't substitute entities, the validation should be
1990
   * done on a value with replaced entities anyway.
1991
   */
1992
        if (!ctxt->replaceEntities) {
1993
      dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
1994
      if (dup == NULL) {
1995
          if (*valueend == 0) {
1996
        ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1997
            ctxt->myDoc, ctxt->node, ret, value);
1998
    } else {
1999
        /*
2000
         * That should already be normalized.
2001
         * cheaper to finally allocate here than duplicate
2002
         * entry points in the full validation code
2003
         */
2004
        dup = xmlStrndup(value, valueend - value);
2005
                    if (dup == NULL)
2006
                        xmlSAX2ErrMemory(ctxt);
2007
2008
        ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2009
            ctxt->myDoc, ctxt->node, ret, dup);
2010
    }
2011
      } else {
2012
          /*
2013
     * dup now contains a string of the flattened attribute
2014
     * content with entities substituted. Check if we need to
2015
     * apply an extra layer of normalization.
2016
     * It need to be done twice ... it's an extra burden related
2017
     * to the ability to keep references in attributes
2018
     */
2019
    if (ctxt->attsSpecial != NULL) {
2020
        xmlChar *nvalnorm;
2021
        xmlChar fn[50];
2022
        xmlChar *fullname;
2023
2024
        fullname = xmlBuildQName(localname, prefix, fn, 50);
2025
                    if (fullname == NULL) {
2026
                        xmlSAX2ErrMemory(ctxt);
2027
                    } else {
2028
      ctxt->vctxt.valid = 1;
2029
            nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2030
                       &ctxt->vctxt, ctxt->myDoc,
2031
           ctxt->node, fullname, dup);
2032
      if (ctxt->vctxt.valid != 1)
2033
          ctxt->valid = 0;
2034
2035
      if ((fullname != fn) && (fullname != localname))
2036
          xmlFree(fullname);
2037
      if (nvalnorm != NULL) {
2038
          xmlFree(dup);
2039
          dup = nvalnorm;
2040
      }
2041
        }
2042
    }
2043
2044
    ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2045
              ctxt->myDoc, ctxt->node, ret, dup);
2046
      }
2047
  } else {
2048
      /*
2049
       * if entities already have been substituted, then
2050
       * the attribute as passed is already normalized
2051
       */
2052
      dup = xmlStrndup(value, valueend - value);
2053
            if (dup == NULL)
2054
                xmlSAX2ErrMemory(ctxt);
2055
2056
      ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2057
                               ctxt->myDoc, ctxt->node, ret, dup);
2058
  }
2059
    } else
2060
#endif /* LIBXML_VALID_ENABLED */
2061
13.9k
           if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
2062
13.9k
               (ctxt->input->entity == NULL) &&
2063
               /* Don't create IDs containing entity references */
2064
13.9k
               (ret->children != NULL) &&
2065
13.9k
               (ret->children->type == XML_TEXT_NODE) &&
2066
13.9k
               (ret->children->next == NULL)) {
2067
13.4k
        xmlChar *content = ret->children->content;
2068
        /*
2069
   * when validating, the ID registration is done at the attribute
2070
   * validation level. Otherwise we have to do specific handling here.
2071
   */
2072
13.4k
        if ((prefix == ctxt->str_xml) &&
2073
13.4k
             (localname[0] == 'i') && (localname[1] == 'd') &&
2074
13.4k
       (localname[2] == 0)) {
2075
      /*
2076
       * Add the xml:id value
2077
       *
2078
       * Open issue: normalization of the value.
2079
       */
2080
4.60k
      if (xmlValidateNCName(content, 1) != 0) {
2081
4.59k
          xmlErrId(ctxt, XML_DTD_XMLID_VALUE,
2082
4.59k
                         "xml:id : attribute value %s is not an NCName\n",
2083
4.59k
                         content);
2084
4.59k
      }
2085
4.60k
      xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
2086
8.86k
  } else {
2087
8.86k
            int res = xmlIsID(ctxt->myDoc, ctxt->node, ret);
2088
2089
8.86k
            if (res < 0)
2090
0
                xmlCtxtErrMemory(ctxt);
2091
8.86k
            else if (res > 0)
2092
0
                xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
2093
8.86k
            else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
2094
0
                xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
2095
8.86k
  }
2096
13.4k
    }
2097
13.9k
    if (dup != NULL)
2098
0
  xmlFree(dup);
2099
2100
13.9k
    return(ret);
2101
13.9k
}
2102
2103
/**
2104
 * SAX2 callback when an element start has been detected by the parser.
2105
 * It provides the namespace information for the element, as well as
2106
 * the new namespace declarations on the element.
2107
 *
2108
 * @param ctx  the user data (XML parser context)
2109
 * @param localname  the local name of the element
2110
 * @param prefix  the element namespace prefix if available
2111
 * @param URI  the element namespace name if available
2112
 * @param nb_namespaces  number of namespace definitions on that node
2113
 * @param namespaces  pointer to the array of prefix/URI pairs namespace definitions
2114
 * @param nb_attributes  the number of attributes on that node
2115
 * @param nb_defaulted  the number of defaulted attributes.
2116
 * @param attributes  pointer to the array of (localname/prefix/URI/value/end)
2117
 *               attribute values.
2118
 */
2119
void
2120
xmlSAX2StartElementNs(void *ctx,
2121
                      const xmlChar *localname,
2122
          const xmlChar *prefix,
2123
          const xmlChar *URI,
2124
          int nb_namespaces,
2125
          const xmlChar **namespaces,
2126
          int nb_attributes,
2127
          int nb_defaulted,
2128
          const xmlChar **attributes)
2129
2.50M
{
2130
2.50M
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2131
2.50M
    xmlNodePtr ret;
2132
2.50M
    xmlNsPtr last = NULL, ns;
2133
2.50M
    const xmlChar *uri, *pref;
2134
2.50M
    xmlChar *lname = NULL;
2135
2.50M
    int i, j;
2136
2137
2.50M
    if (ctx == NULL) return;
2138
2139
#ifdef LIBXML_VALID_ENABLED
2140
    /*
2141
     * First check on validity:
2142
     */
2143
    if (ctxt->validate &&
2144
        ((ctxt->myDoc == NULL) ||
2145
         ((ctxt->myDoc->extSubset == NULL) &&
2146
          ((ctxt->myDoc->intSubset == NULL) ||
2147
     ((ctxt->myDoc->intSubset->notations == NULL) &&
2148
      (ctxt->myDoc->intSubset->elements == NULL) &&
2149
      (ctxt->myDoc->intSubset->attributes == NULL) &&
2150
      (ctxt->myDoc->intSubset->entities == NULL)))))) {
2151
  xmlErrValid(ctxt, XML_DTD_NO_DTD,
2152
    "Validation failed: no DTD found !", NULL, NULL);
2153
  ctxt->validate = 0;
2154
    }
2155
#endif /* LIBXML_VALID_ENABLED */
2156
2157
    /*
2158
     * Take care of the rare case of an undefined namespace prefix
2159
     */
2160
2.50M
    if ((prefix != NULL) && (URI == NULL)) {
2161
11.4k
        if (ctxt->dictNames) {
2162
11.4k
      const xmlChar *fullname;
2163
2164
11.4k
      fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
2165
11.4k
      if (fullname == NULL) {
2166
0
                xmlSAX2ErrMemory(ctxt);
2167
0
                return;
2168
0
            }
2169
11.4k
      localname = fullname;
2170
11.4k
  } else {
2171
0
      lname = xmlBuildQName(localname, prefix, NULL, 0);
2172
0
            if (lname == NULL) {
2173
0
                xmlSAX2ErrMemory(ctxt);
2174
0
                return;
2175
0
            }
2176
0
  }
2177
11.4k
    }
2178
    /*
2179
     * allocate the node
2180
     */
2181
2.50M
    if (ctxt->freeElems != NULL) {
2182
0
        ret = ctxt->freeElems;
2183
0
  ctxt->freeElems = ret->next;
2184
0
  ctxt->freeElemsNr--;
2185
0
  memset(ret, 0, sizeof(xmlNode));
2186
0
        ret->doc = ctxt->myDoc;
2187
0
  ret->type = XML_ELEMENT_NODE;
2188
2189
0
  if (ctxt->dictNames)
2190
0
      ret->name = localname;
2191
0
  else {
2192
0
      if (lname == NULL)
2193
0
    ret->name = xmlStrdup(localname);
2194
0
      else
2195
0
          ret->name = lname;
2196
0
      if (ret->name == NULL) {
2197
0
          xmlSAX2ErrMemory(ctxt);
2198
0
                xmlFree(ret);
2199
0
    return;
2200
0
      }
2201
0
  }
2202
0
  if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2203
0
      xmlRegisterNodeDefaultValue(ret);
2204
2.50M
    } else {
2205
2.50M
  if (ctxt->dictNames)
2206
2.50M
      ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2207
2.50M
                                 (xmlChar *) localname, NULL);
2208
0
  else if (lname == NULL)
2209
0
      ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2210
0
  else
2211
0
      ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, lname, NULL);
2212
2.50M
  if (ret == NULL) {
2213
0
      xmlSAX2ErrMemory(ctxt);
2214
0
      return;
2215
0
  }
2216
2.50M
    }
2217
2218
    /*
2219
     * Build the namespace list
2220
     */
2221
2.51M
    for (i = 0,j = 0;j < nb_namespaces;j++) {
2222
13.0k
        pref = namespaces[i++];
2223
13.0k
  uri = namespaces[i++];
2224
13.0k
  ns = xmlNewNs(NULL, uri, pref);
2225
13.0k
  if (ns != NULL) {
2226
13.0k
      if (last == NULL) {
2227
6.98k
          ret->nsDef = last = ns;
2228
6.98k
      } else {
2229
6.09k
          last->next = ns;
2230
6.09k
    last = ns;
2231
6.09k
      }
2232
13.0k
      if ((URI != NULL) && (prefix == pref))
2233
4.34k
    ret->ns = ns;
2234
13.0k
  } else {
2235
0
            xmlSAX2ErrMemory(ctxt);
2236
0
      continue;
2237
0
  }
2238
2239
13.0k
        xmlParserNsUpdateSax(ctxt, pref, ns);
2240
2241
#ifdef LIBXML_VALID_ENABLED
2242
  if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2243
      ctxt->myDoc && ctxt->myDoc->intSubset) {
2244
      ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2245
                                             ret, prefix, ns, uri);
2246
  }
2247
#endif /* LIBXML_VALID_ENABLED */
2248
13.0k
    }
2249
2.50M
    ctxt->nodemem = -1;
2250
2251
    /*
2252
     * Link the child element
2253
     */
2254
2.50M
    xmlSAX2AppendChild(ctxt, ret);
2255
2256
    /*
2257
     * We are parsing a new node.
2258
     */
2259
2.50M
    if (nodePush(ctxt, ret) < 0) {
2260
20
        xmlUnlinkNode(ret);
2261
20
        xmlFreeNode(ret);
2262
20
        return;
2263
20
    }
2264
2265
    /*
2266
     * Insert the defaulted attributes from the DTD only if requested:
2267
     */
2268
2.50M
    if ((nb_defaulted != 0) &&
2269
2.50M
        ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2270
272
  nb_attributes -= nb_defaulted;
2271
2272
    /*
2273
     * Search the namespace if it wasn't already found
2274
     * Note that, if prefix is NULL, this searches for the default Ns
2275
     */
2276
2.50M
    if ((URI != NULL) && (ret->ns == NULL)) {
2277
180k
        ret->ns = xmlParserNsLookupSax(ctxt, prefix);
2278
180k
  if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2279
0
            int res;
2280
2281
0
      res = xmlSearchNsSafe(ret, prefix, &ret->ns);
2282
0
            if (res < 0)
2283
0
                xmlSAX2ErrMemory(ctxt);
2284
0
  }
2285
180k
  if (ret->ns == NULL) {
2286
0
      ns = xmlNewNs(ret, NULL, prefix);
2287
0
      if (ns == NULL) {
2288
2289
0
          xmlSAX2ErrMemory(ctxt);
2290
0
    return;
2291
0
      }
2292
0
            if (prefix != NULL)
2293
0
                xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2294
0
                             "Namespace prefix %s was not found\n",
2295
0
                             prefix, NULL);
2296
0
            else
2297
0
                xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2298
0
                             "Namespace default prefix was not found\n",
2299
0
                             NULL, NULL);
2300
0
  }
2301
180k
    }
2302
2303
    /*
2304
     * process all the other attributes
2305
     */
2306
2.50M
    if (nb_attributes > 0) {
2307
6.01k
        xmlAttrPtr prev = NULL;
2308
2309
20.0k
        for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2310
13.9k
            xmlAttrPtr attr = NULL;
2311
2312
      /*
2313
       * Handle the rare case of an undefined attribute prefix
2314
       */
2315
13.9k
      if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
2316
1.12k
    if (ctxt->dictNames) {
2317
1.12k
        const xmlChar *fullname;
2318
2319
1.12k
        fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
2320
1.12k
                                  attributes[j]);
2321
1.12k
        if (fullname == NULL) {
2322
0
                        xmlSAX2ErrMemory(ctxt);
2323
0
                        return;
2324
0
                    }
2325
1.12k
                    attr = xmlSAX2AttributeNs(ctxt, fullname, NULL,
2326
1.12k
                                              attributes[j+3],
2327
1.12k
                                              attributes[j+4]);
2328
1.12k
                    goto have_attr;
2329
1.12k
    } else {
2330
0
        lname = xmlBuildQName(attributes[j], attributes[j+1],
2331
0
                              NULL, 0);
2332
0
        if (lname == NULL) {
2333
0
                        xmlSAX2ErrMemory(ctxt);
2334
0
                        return;
2335
0
                    }
2336
0
                    attr = xmlSAX2AttributeNs(ctxt, lname, NULL,
2337
0
                                              attributes[j+3],
2338
0
                                              attributes[j+4]);
2339
0
                    xmlFree(lname);
2340
0
                    goto have_attr;
2341
0
    }
2342
1.12k
      }
2343
12.8k
            attr = xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2344
12.8k
                                      attributes[j+3], attributes[j+4]);
2345
13.9k
have_attr:
2346
13.9k
            if (attr == NULL)
2347
0
                continue;
2348
2349
            /* link at the end to preserve order */
2350
13.9k
            if (prev == NULL) {
2351
6.01k
                ctxt->node->properties = attr;
2352
7.98k
            } else {
2353
7.98k
                prev->next = attr;
2354
7.98k
                attr->prev = prev;
2355
7.98k
            }
2356
2357
13.9k
            prev = attr;
2358
13.9k
  }
2359
6.01k
    }
2360
2361
#ifdef LIBXML_VALID_ENABLED
2362
    /*
2363
     * If it's the Document root, finish the DTD validation and
2364
     * check the document root element for validity
2365
     */
2366
    if ((ctxt->validate) &&
2367
        ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) {
2368
  int chk;
2369
2370
  chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2371
  if (chk <= 0)
2372
      ctxt->valid = 0;
2373
  if (chk < 0)
2374
      ctxt->wellFormed = 0;
2375
  ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2376
  ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED;
2377
    }
2378
#endif /* LIBXML_VALID_ENABLED */
2379
2.50M
}
2380
2381
/**
2382
 * SAX2 callback when an element end has been detected by the parser.
2383
 * It provides the namespace information for the element.
2384
 *
2385
 * @param ctx  the user data (XML parser context)
2386
 * @param localname  the local name of the element
2387
 * @param prefix  the element namespace prefix if available
2388
 * @param URI  the element namespace name if available
2389
 */
2390
void
2391
xmlSAX2EndElementNs(void *ctx,
2392
                    const xmlChar * localname ATTRIBUTE_UNUSED,
2393
                    const xmlChar * prefix ATTRIBUTE_UNUSED,
2394
        const xmlChar * URI ATTRIBUTE_UNUSED)
2395
35.0k
{
2396
35.0k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2397
2398
35.0k
    if (ctx == NULL) return;
2399
35.0k
    ctxt->nodemem = -1;
2400
2401
#ifdef LIBXML_VALID_ENABLED
2402
    if (ctxt->validate && ctxt->wellFormed &&
2403
        ctxt->myDoc && ctxt->myDoc->intSubset)
2404
        ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
2405
                                             ctxt->node);
2406
#endif /* LIBXML_VALID_ENABLED */
2407
2408
    /*
2409
     * end of parsing of this node.
2410
     */
2411
35.0k
    nodePop(ctxt);
2412
35.0k
}
2413
2414
/**
2415
 * called when an entity #xmlSAX2Reference is detected.
2416
 *
2417
 * @param ctx  the user data (XML parser context)
2418
 * @param name  The entity name
2419
 */
2420
void
2421
xmlSAX2Reference(void *ctx, const xmlChar *name)
2422
3.75k
{
2423
3.75k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2424
3.75k
    xmlNodePtr ret;
2425
2426
3.75k
    if (ctx == NULL) return;
2427
3.75k
    ret = xmlNewReference(ctxt->myDoc, name);
2428
3.75k
    if (ret == NULL) {
2429
0
        xmlSAX2ErrMemory(ctxt);
2430
0
        return;
2431
0
    }
2432
2433
3.75k
    xmlSAX2AppendChild(ctxt, ret);
2434
3.75k
}
2435
2436
/**
2437
 * Append characters.
2438
 *
2439
 * @param ctxt  the parser context
2440
 * @param ch  a xmlChar string
2441
 * @param len  the number of xmlChar
2442
 * @param type  text or cdata
2443
 */
2444
static void
2445
xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
2446
            xmlElementType type)
2447
600k
{
2448
600k
    xmlNodePtr lastChild;
2449
600k
    xmlNodePtr parent;
2450
2451
600k
    if (ctxt == NULL)
2452
0
        return;
2453
2454
600k
    parent = ctxt->node;
2455
600k
    if (parent == NULL)
2456
0
        return;
2457
600k
    lastChild = parent->last;
2458
2459
    /*
2460
     * Try to merge with previous text node using size and capacity
2461
     * stored in the parser context to avoid naive concatenation.
2462
     *
2463
     * Don't merge CDATA sections. In HTML mode, CDATA is used for
2464
     * raw text which should be merged.
2465
     */
2466
600k
    if ((lastChild == NULL) ||
2467
600k
        (lastChild->type != type) ||
2468
600k
        ((!ctxt->html) && (type != XML_TEXT_NODE))) {
2469
312k
        xmlNode *node;
2470
2471
312k
        if (type == XML_TEXT_NODE)
2472
309k
            node = xmlSAX2TextNode(ctxt, parent->doc, ch, len);
2473
3.11k
        else
2474
3.11k
            node = xmlNewCDataBlock(parent->doc, ch, len);
2475
312k
  if (node == NULL) {
2476
0
      xmlSAX2ErrMemory(ctxt);
2477
0
      return;
2478
0
  }
2479
2480
312k
        if (lastChild == NULL) {
2481
21.9k
            parent->children = node;
2482
21.9k
            parent->last = node;
2483
21.9k
            node->parent = parent;
2484
291k
        } else {
2485
291k
            xmlSAX2AppendChild(ctxt, node);
2486
291k
        }
2487
2488
312k
        ctxt->nodelen = len;
2489
312k
        ctxt->nodemem = len + 1;
2490
312k
        lastChild = node;
2491
312k
    } else {
2492
287k
        xmlChar *content;
2493
287k
        int oldSize, newSize, capacity;
2494
287k
        int maxSize = (ctxt->options & XML_PARSE_HUGE) ?
2495
0
                      XML_MAX_HUGE_LENGTH :
2496
287k
                      XML_MAX_TEXT_LENGTH;
2497
2498
287k
        content = lastChild->content;
2499
287k
        oldSize = ctxt->nodelen;
2500
287k
        capacity = ctxt->nodemem;
2501
2502
        /* Shouldn't happen */
2503
287k
        if ((content == NULL) || (capacity <= 0)) {
2504
0
            xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
2505
0
                        "xmlSAX2Text: no content");
2506
0
            return;
2507
0
        }
2508
2509
287k
        if ((len > maxSize) || (oldSize > maxSize - len)) {
2510
0
            xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
2511
0
                        "Text node too long, try XML_PARSE_HUGE");
2512
0
            xmlHaltParser(ctxt);
2513
0
            return;
2514
0
        }
2515
2516
287k
        newSize = oldSize + len;
2517
2518
287k
        if (newSize >= capacity) {
2519
57.6k
            if (newSize <= 20)
2520
23.9k
                capacity = 40;
2521
33.7k
            else
2522
33.7k
                capacity = newSize > INT_MAX / 2 ? INT_MAX : newSize * 2;
2523
2524
            /*
2525
             * If the content was stored in properties or in
2526
             * the dictionary, don't realloc.
2527
             */
2528
57.6k
            if ((content == (xmlChar *) &lastChild->properties) ||
2529
57.6k
                ((ctxt->nodemem == oldSize + 1) &&
2530
57.6k
                 (xmlDictOwns(ctxt->dict, content)))) {
2531
1.59k
                xmlChar *newContent;
2532
2533
1.59k
                newContent = xmlMalloc(capacity);
2534
1.59k
                if (newContent == NULL) {
2535
0
                    xmlSAX2ErrMemory(ctxt);
2536
0
                    return;
2537
0
                }
2538
2539
1.59k
                memcpy(newContent, content, oldSize);
2540
1.59k
                lastChild->properties = NULL;
2541
1.59k
                content = newContent;
2542
56.0k
            } else {
2543
56.0k
                content = xmlRealloc(content, capacity);
2544
56.0k
                if (content == NULL) {
2545
0
                    xmlSAX2ErrMemory(ctxt);
2546
0
                    return;
2547
0
                }
2548
56.0k
            }
2549
2550
57.6k
            ctxt->nodemem = capacity;
2551
57.6k
            lastChild->content = content;
2552
57.6k
        }
2553
2554
287k
        memcpy(&content[oldSize], ch, len);
2555
287k
        content[newSize] = 0;
2556
287k
        ctxt->nodelen = newSize;
2557
287k
    }
2558
2559
600k
    if ((lastChild != NULL) &&
2560
600k
        (type == XML_TEXT_NODE) &&
2561
600k
        (ctxt->input != NULL)) {
2562
597k
        if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
2563
587k
            lastChild->line = ctxt->input->line;
2564
9.64k
        else {
2565
9.64k
            lastChild->line = USHRT_MAX;
2566
9.64k
            if (ctxt->options & XML_PARSE_BIG_LINES)
2567
0
                lastChild->psvi = XML_INT_TO_PTR(ctxt->input->line);
2568
9.64k
        }
2569
597k
    }
2570
600k
}
2571
2572
/**
2573
 * receiving some chars from the parser.
2574
 *
2575
 * @param ctx  the user data (XML parser context)
2576
 * @param ch  a xmlChar string
2577
 * @param len  the number of xmlChar
2578
 */
2579
void
2580
xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2581
597k
{
2582
597k
    xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE);
2583
597k
}
2584
2585
/**
2586
 * receiving some ignorable whitespaces from the parser.
2587
 * UNUSED: by default the DOM building will use #xmlSAX2Characters
2588
 *
2589
 * @param ctx  the user data (XML parser context)
2590
 * @param ch  a xmlChar string
2591
 * @param len  the number of xmlChar
2592
 */
2593
void
2594
xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2595
0
{
2596
0
}
2597
2598
/**
2599
 * A processing instruction has been parsed.
2600
 *
2601
 * @param ctx  the user data (XML parser context)
2602
 * @param target  the target name
2603
 * @param data  the PI data's
2604
 */
2605
void
2606
xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2607
                      const xmlChar *data)
2608
7.70k
{
2609
7.70k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2610
7.70k
    xmlNodePtr ret;
2611
2612
7.70k
    if (ctx == NULL) return;
2613
2614
7.70k
    ret = xmlNewDocPI(ctxt->myDoc, target, data);
2615
7.70k
    if (ret == NULL) {
2616
0
        xmlSAX2ErrMemory(ctxt);
2617
0
        return;
2618
0
    }
2619
2620
7.70k
    xmlSAX2AppendChild(ctxt, ret);
2621
7.70k
}
2622
2623
/**
2624
 * A #xmlSAX2Comment has been parsed.
2625
 *
2626
 * @param ctx  the user data (XML parser context)
2627
 * @param value  the #xmlSAX2Comment content
2628
 */
2629
void
2630
xmlSAX2Comment(void *ctx, const xmlChar *value)
2631
5.60k
{
2632
5.60k
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2633
5.60k
    xmlNodePtr ret;
2634
2635
5.60k
    if (ctx == NULL) return;
2636
2637
5.60k
    ret = xmlNewDocComment(ctxt->myDoc, value);
2638
5.60k
    if (ret == NULL) {
2639
0
        xmlSAX2ErrMemory(ctxt);
2640
0
        return;
2641
0
    }
2642
2643
5.60k
    xmlSAX2AppendChild(ctxt, ret);
2644
5.60k
}
2645
2646
/**
2647
 * called when a pcdata block has been parsed
2648
 *
2649
 * @param ctx  the user data (XML parser context)
2650
 * @param value  The pcdata content
2651
 * @param len  the block length
2652
 */
2653
void
2654
xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2655
3.11k
{
2656
3.11k
    xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE);
2657
3.11k
}
2658
2659
#ifdef LIBXML_SAX1_ENABLED
2660
/**
2661
 * Has no effect.
2662
 *
2663
 * @deprecated Use parser option XML_PARSE_SAX1.
2664
 *
2665
 * @param version  the version, must be 2
2666
 * @returns 2 in case of success and -1 in case of error.
2667
 */
2668
int
2669
xmlSAXDefaultVersion(int version)
2670
{
2671
    if (version != 2)
2672
        return(-1);
2673
    return(2);
2674
}
2675
#endif /* LIBXML_SAX1_ENABLED */
2676
2677
/**
2678
 * Initialize the default XML SAX handler according to the version
2679
 *
2680
 * @param hdlr  the SAX handler
2681
 * @param version  the version, 1 or 2
2682
 * @returns 0 in case of success and -1 in case of error.
2683
 */
2684
int
2685
xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2686
302
{
2687
302
    if (hdlr == NULL) return(-1);
2688
302
    if (version == 2) {
2689
302
  hdlr->startElementNs = xmlSAX2StartElementNs;
2690
302
  hdlr->endElementNs = xmlSAX2EndElementNs;
2691
302
  hdlr->serror = NULL;
2692
302
  hdlr->initialized = XML_SAX2_MAGIC;
2693
#ifdef LIBXML_SAX1_ENABLED
2694
    } else if (version == 1) {
2695
  hdlr->initialized = 1;
2696
#endif /* LIBXML_SAX1_ENABLED */
2697
302
    } else
2698
0
        return(-1);
2699
#ifdef LIBXML_SAX1_ENABLED
2700
    hdlr->startElement = xmlSAX2StartElement;
2701
    hdlr->endElement = xmlSAX2EndElement;
2702
#else
2703
302
    hdlr->startElement = NULL;
2704
302
    hdlr->endElement = NULL;
2705
302
#endif /* LIBXML_SAX1_ENABLED */
2706
302
    hdlr->internalSubset = xmlSAX2InternalSubset;
2707
302
    hdlr->externalSubset = xmlSAX2ExternalSubset;
2708
302
    hdlr->isStandalone = xmlSAX2IsStandalone;
2709
302
    hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2710
302
    hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2711
302
    hdlr->resolveEntity = xmlSAX2ResolveEntity;
2712
302
    hdlr->getEntity = xmlSAX2GetEntity;
2713
302
    hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2714
302
    hdlr->entityDecl = xmlSAX2EntityDecl;
2715
302
    hdlr->attributeDecl = xmlSAX2AttributeDecl;
2716
302
    hdlr->elementDecl = xmlSAX2ElementDecl;
2717
302
    hdlr->notationDecl = xmlSAX2NotationDecl;
2718
302
    hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2719
302
    hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2720
302
    hdlr->startDocument = xmlSAX2StartDocument;
2721
302
    hdlr->endDocument = xmlSAX2EndDocument;
2722
302
    hdlr->reference = xmlSAX2Reference;
2723
302
    hdlr->characters = xmlSAX2Characters;
2724
302
    hdlr->cdataBlock = xmlSAX2CDataBlock;
2725
302
    hdlr->ignorableWhitespace = xmlSAX2Characters;
2726
302
    hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2727
302
    hdlr->comment = xmlSAX2Comment;
2728
302
    hdlr->warning = xmlParserWarning;
2729
302
    hdlr->error = xmlParserError;
2730
302
    hdlr->fatalError = xmlParserError;
2731
2732
302
    return(0);
2733
302
}
2734
2735
/**
2736
 * Initialize the default XML SAX2 handler
2737
 *
2738
 * @param hdlr  the SAX handler
2739
 * @param warning  flag if non-zero sets the handler warning procedure
2740
 */
2741
void
2742
xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2743
0
{
2744
0
    if ((hdlr == NULL) || (hdlr->initialized != 0))
2745
0
  return;
2746
2747
0
    xmlSAXVersion(hdlr, 2);
2748
0
    if (warning == 0)
2749
0
  hdlr->warning = NULL;
2750
0
}
2751
2752
/**
2753
 * Initialize the default SAX2 handler
2754
 *
2755
 * @deprecated This function is a no-op. Call #xmlInitParser to
2756
 * initialize the library.
2757
 *
2758
 */
2759
void
2760
xmlDefaultSAXHandlerInit(void)
2761
0
{
2762
0
}
2763
2764
#ifdef LIBXML_HTML_ENABLED
2765
2766
/**
2767
 * Initialize the default HTML SAX2 handler
2768
 *
2769
 * @param hdlr  the SAX handler
2770
 */
2771
void
2772
xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2773
0
{
2774
0
    if ((hdlr == NULL) || (hdlr->initialized != 0))
2775
0
  return;
2776
2777
0
    hdlr->internalSubset = xmlSAX2InternalSubset;
2778
0
    hdlr->externalSubset = NULL;
2779
0
    hdlr->isStandalone = NULL;
2780
0
    hdlr->hasInternalSubset = NULL;
2781
0
    hdlr->hasExternalSubset = NULL;
2782
0
    hdlr->resolveEntity = NULL;
2783
0
    hdlr->getEntity = xmlSAX2GetEntity;
2784
0
    hdlr->getParameterEntity = NULL;
2785
0
    hdlr->entityDecl = NULL;
2786
0
    hdlr->attributeDecl = NULL;
2787
0
    hdlr->elementDecl = NULL;
2788
0
    hdlr->notationDecl = NULL;
2789
0
    hdlr->unparsedEntityDecl = NULL;
2790
0
    hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2791
0
    hdlr->startDocument = xmlSAX2StartDocument;
2792
0
    hdlr->endDocument = xmlSAX2EndDocument;
2793
0
    hdlr->startElement = xmlSAX2StartElement;
2794
0
    hdlr->endElement = xmlSAX2EndElement;
2795
0
    hdlr->reference = NULL;
2796
0
    hdlr->characters = xmlSAX2Characters;
2797
0
    hdlr->cdataBlock = xmlSAX2CDataBlock;
2798
0
    hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2799
0
    hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2800
0
    hdlr->comment = xmlSAX2Comment;
2801
0
    hdlr->warning = xmlParserWarning;
2802
0
    hdlr->error = xmlParserError;
2803
0
    hdlr->fatalError = xmlParserError;
2804
2805
0
    hdlr->initialized = 1;
2806
0
}
2807
2808
/**
2809
 * @deprecated This function is a no-op. Call #xmlInitParser to
2810
 * initialize the library.
2811
 */
2812
void
2813
htmlDefaultSAXHandlerInit(void)
2814
0
{
2815
0
}
2816
2817
#endif /* LIBXML_HTML_ENABLED */