Coverage Report

Created: 2023-06-07 06:14

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