Coverage Report

Created: 2025-08-26 06:41

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