Coverage Report

Created: 2026-05-28 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/tree.c
Line
Count
Source
1
/*
2
 * tree.c : implementation of access function for an XML tree.
3
 *
4
 * References:
5
 *   XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
6
 *
7
 * See Copyright for the status of this software.
8
 *
9
 * Author: Daniel Veillard
10
 *
11
 */
12
13
/* To avoid EBCDIC trouble when parsing on zOS */
14
#if defined(__MVS__)
15
#pragma convert("ISO8859-1")
16
#endif
17
18
#define IN_LIBXML
19
#include "libxml.h"
20
21
#include <string.h> /* for memset() only ! */
22
#include <stddef.h>
23
#include <limits.h>
24
#include <ctype.h>
25
#include <stdlib.h>
26
27
#ifdef LIBXML_ZLIB_ENABLED
28
#include <zlib.h>
29
#endif
30
31
#include <libxml/tree.h>
32
#include <libxml/xmlmemory.h>
33
#include <libxml/parser.h>
34
#include <libxml/uri.h>
35
#include <libxml/entities.h>
36
#include <libxml/xmlerror.h>
37
#include <libxml/parserInternals.h>
38
#ifdef LIBXML_HTML_ENABLED
39
#include <libxml/HTMLtree.h>
40
#endif
41
42
#include "private/buf.h"
43
#include "private/entities.h"
44
#include "private/error.h"
45
#include "private/memory.h"
46
#include "private/io.h"
47
#include "private/parser.h"
48
#include "private/tree.h"
49
50
#ifndef SIZE_MAX
51
  #define SIZE_MAX ((size_t) -1)
52
#endif
53
54
/*
55
 * Internal variable indicating whether a callback has been registered
56
 * for node creation/destruction. This avoids looking up thread-local
57
 * data if no callback was ever registered.
58
 */
59
int xmlRegisterCallbacks = 0;
60
61
/************************************************************************
62
 *                  *
63
 *    Forward declarations          *
64
 *                  *
65
 ************************************************************************/
66
67
static xmlNodePtr
68
xmlNewEntityRef(xmlDocPtr doc, xmlChar *name);
69
70
static xmlNsPtr
71
xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns);
72
73
static xmlAttrPtr
74
xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
75
           const xmlChar *nsName, int useDTD);
76
77
static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
78
79
static void
80
xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree);
81
82
static void
83
xmlUnlinkNodeInternal(xmlNodePtr cur);
84
85
/************************************************************************
86
 *                  *
87
 *    A few static variables and macros     *
88
 *                  *
89
 ************************************************************************/
90
/* #undef xmlStringText */
91
const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
92
/* #undef xmlStringTextNoenc */
93
const xmlChar xmlStringTextNoenc[] =
94
              { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
95
/* #undef xmlStringComment */
96
const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
97
98
static int xmlCompressMode = 0;
99
100
4.66k
#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
101
4.66k
  (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
102
103
/************************************************************************
104
 *                  *
105
 *    Functions to move to entities.c once the    *
106
 *    API freeze is smoothen and they can be made public. *
107
 *                  *
108
 ************************************************************************/
109
#include <libxml/hash.h>
110
111
/**
112
 * Do an entity lookup in the DTD entity hash table and
113
 *
114
 * @param dtd  A pointer to the DTD to search
115
 * @param name  The entity name
116
 * @returns the corresponding entity, if found.
117
 */
118
static xmlEntityPtr
119
0
xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
120
0
    xmlEntitiesTablePtr table;
121
122
0
    if((dtd != NULL) && (dtd->entities != NULL)) {
123
0
  table = (xmlEntitiesTablePtr) dtd->entities;
124
0
  return((xmlEntityPtr) xmlHashLookup(table, name));
125
  /* return(xmlGetEntityFromTable(table, name)); */
126
0
    }
127
0
    return(NULL);
128
0
}
129
/**
130
 * Do an entity lookup in the DTD parameter entity hash table and
131
 *
132
 * @param dtd  A pointer to the DTD to search
133
 * @param name  The entity name
134
 * @returns the corresponding entity, if found.
135
 */
136
static xmlEntityPtr
137
0
xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
138
0
    xmlEntitiesTablePtr table;
139
140
0
    if ((dtd != NULL) && (dtd->pentities != NULL)) {
141
0
  table = (xmlEntitiesTablePtr) dtd->pentities;
142
0
  return((xmlEntityPtr) xmlHashLookup(table, name));
143
  /* return(xmlGetEntityFromTable(table, name)); */
144
0
    }
145
0
    return(NULL);
146
0
}
147
148
/************************************************************************
149
 *                  *
150
 *      QName handling helper       *
151
 *                  *
152
 ************************************************************************/
153
154
/**
155
 * Build a QName from prefix and local name.
156
 *
157
 * Builds the QName `prefix:ncname` in `memory` if there is enough space
158
 * and prefix is not NULL nor empty, otherwise allocate a new string.
159
 * If prefix is NULL or empty it returns ncname.
160
 *
161
 * @param ncname  the Name
162
 * @param prefix  the prefix
163
 * @param memory  preallocated memory
164
 * @param len  preallocated memory length
165
 * @returns the new string which must be freed by the caller if different from
166
 *         `memory` and `ncname` or NULL in case of error
167
 */
168
xmlChar *
169
xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
170
21.1k
        xmlChar *memory, int len) {
171
21.1k
    size_t lenn, lenp;
172
21.1k
    xmlChar *ret;
173
174
21.1k
    if ((ncname == NULL) || (len < 0)) return(NULL);
175
21.1k
    if (prefix == NULL) return((xmlChar *) ncname);
176
177
21.1k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
178
    /* Make allocation more likely */
179
21.1k
    if (len > 8)
180
311
        len = 8;
181
21.1k
#endif
182
183
21.1k
    lenn = strlen((char *) ncname);
184
21.1k
    lenp = strlen((char *) prefix);
185
21.1k
    if (lenn >= SIZE_MAX - lenp - 1)
186
0
        return(NULL);
187
188
21.1k
    if ((memory == NULL) || ((size_t) len < lenn + lenp + 2)) {
189
21.0k
  ret = xmlMalloc(lenn + lenp + 2);
190
21.0k
  if (ret == NULL)
191
0
      return(NULL);
192
21.0k
    } else {
193
108
  ret = memory;
194
108
    }
195
21.1k
    memcpy(&ret[0], prefix, lenp);
196
21.1k
    ret[lenp] = ':';
197
21.1k
    memcpy(&ret[lenp + 1], ncname, lenn);
198
21.1k
    ret[lenn + lenp + 1] = 0;
199
21.1k
    return(ret);
200
21.1k
}
201
202
/**
203
 * Parse an XML qualified name.
204
 *
205
 * @deprecated This function doesn't report malloc failures.
206
 *
207
 *     [NS 5] QName ::= (Prefix ':')? LocalPart
208
 *
209
 *     [NS 6] Prefix ::= NCName
210
 *
211
 *     [NS 7] LocalPart ::= NCName
212
 *
213
 * @param name  the full QName
214
 * @param prefix  a xmlChar **
215
 * @returns NULL if the name doesn't have a prefix. Otherwise, returns the
216
 * local part, and prefix is updated to get the Prefix. Both the return value
217
 * and the prefix must be freed by the caller.
218
 */
219
xmlChar *
220
0
xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
221
0
    int len = 0;
222
0
    xmlChar *ret = NULL;
223
224
0
    if (prefix == NULL) return(NULL);
225
0
    *prefix = NULL;
226
0
    if (name == NULL) return(NULL);
227
228
    /* nasty but valid */
229
0
    if (name[0] == ':')
230
0
  return(NULL);
231
232
    /*
233
     * we are not trying to validate but just to cut, and yes it will
234
     * work even if this is as set of UTF-8 encoded chars
235
     */
236
0
    while ((name[len] != 0) && (name[len] != ':'))
237
0
  len++;
238
239
0
    if ((name[len] == 0) || (name[len+1] == 0))
240
0
  return(NULL);
241
242
0
    *prefix = xmlStrndup(name, len);
243
0
    if (*prefix == NULL)
244
0
  return(NULL);
245
0
    ret = xmlStrdup(&name[len + 1]);
246
0
    if (ret == NULL) {
247
0
  if (*prefix != NULL) {
248
0
      xmlFree(*prefix);
249
0
      *prefix = NULL;
250
0
  }
251
0
  return(NULL);
252
0
    }
253
254
0
    return(ret);
255
0
}
256
257
/**
258
 * Parse an XML qualified name.
259
 *
260
 * @param name  the full QName
261
 * @param len  an int *
262
 * @returns NULL if it is not a Qualified Name, otherwise, update len
263
 *         with the length in byte of the prefix and return a pointer
264
 *         to the start of the name without the prefix
265
 */
266
267
const xmlChar *
268
37.7k
xmlSplitQName3(const xmlChar *name, int *len) {
269
37.7k
    int l = 0;
270
271
37.7k
    if (name == NULL) return(NULL);
272
37.7k
    if (len == NULL) return(NULL);
273
274
    /* nasty but valid */
275
37.7k
    if (name[0] == ':')
276
3.80k
  return(NULL);
277
278
    /*
279
     * we are not trying to validate but just to cut, and yes it will
280
     * work even if this is as set of UTF-8 encoded chars
281
     */
282
7.23M
    while ((name[l] != 0) && (name[l] != ':'))
283
7.19M
  l++;
284
285
33.9k
    if ((name[l] == 0) || (name[l+1] == 0))
286
26.2k
  return(NULL);
287
288
7.64k
    *len = l;
289
290
7.64k
    return(&name[l+1]);
291
33.9k
}
292
293
/**
294
 * Parse a QName.
295
 *
296
 * The return value points to the start of the local
297
 * name in the input string. If the QName has a prefix, it will be
298
 * allocated and stored in `prefixPtr`. This string must be freed by
299
 * the caller. If there's no prefix, `prefixPtr` is set to NULL.
300
 *
301
 * @param name  the full QName
302
 * @param prefixPtr  pointer to resulting prefix
303
 * @returns the local name or NULL if a memory allocation failed.
304
 */
305
const xmlChar *
306
15.0k
xmlSplitQName4(const xmlChar *name, xmlChar **prefixPtr) {
307
15.0k
    xmlChar *prefix;
308
15.0k
    int l = 0;
309
310
15.0k
    if ((name == NULL) || (prefixPtr == NULL))
311
0
        return(NULL);
312
313
15.0k
    *prefixPtr = NULL;
314
315
    /* nasty but valid */
316
15.0k
    if (name[0] == ':')
317
2.77k
  return(name);
318
319
    /*
320
     * we are not trying to validate but just to cut, and yes it will
321
     * work even if this is as set of UTF-8 encoded chars
322
     */
323
34.7k
    while ((name[l] != 0) && (name[l] != ':'))
324
22.4k
  l++;
325
326
    /*
327
     * TODO: What about names with multiple colons?
328
     */
329
12.2k
    if ((name[l] == 0) || (name[l+1] == 0))
330
9.45k
  return(name);
331
332
2.82k
    prefix = xmlStrndup(name, l);
333
2.82k
    if (prefix == NULL)
334
0
        return(NULL);
335
336
2.82k
    *prefixPtr = prefix;
337
2.82k
    return(&name[l+1]);
338
2.82k
}
339
340
/************************************************************************
341
 *                  *
342
 *    Check Name, NCName and QName strings      *
343
 *                  *
344
 ************************************************************************/
345
346
/**
347
 * Check that a value conforms to the lexical space of NCName
348
 *
349
 * @param value  the value to check
350
 * @param space  allow spaces in front and end of the string
351
 * @returns 0 if this validates, a positive error code number otherwise
352
 *         and -1 in case of internal or API error.
353
 */
354
int
355
1.80k
xmlValidateNCName(const xmlChar *value, int space) {
356
1.80k
    const xmlChar *cur;
357
358
1.80k
    if (value == NULL)
359
0
        return(-1);
360
361
1.80k
    cur = value;
362
363
1.80k
    if (space) {
364
1.80k
  while (IS_BLANK_CH(*cur))
365
6.38k
            cur++;
366
1.80k
    }
367
368
1.80k
    value = cur;
369
1.80k
    cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
370
1.80k
    if ((cur == NULL) || (cur == value))
371
420
        return(1);
372
373
1.38k
    if (space) {
374
1.38k
  while (IS_BLANK_CH(*cur))
375
49.7k
            cur++;
376
1.38k
    }
377
378
1.38k
    return(*cur != 0);
379
1.80k
}
380
381
/**
382
 * Check that a value conforms to the lexical space of QName
383
 *
384
 * @param value  the value to check
385
 * @param space  allow spaces in front and end of the string
386
 * @returns 0 if this validates, a positive error code number otherwise
387
 *         and -1 in case of internal or API error.
388
 */
389
int
390
0
xmlValidateQName(const xmlChar *value, int space) {
391
0
    const xmlChar *cur;
392
393
0
    if (value == NULL)
394
0
        return(-1);
395
396
0
    cur = value;
397
398
0
    if (space) {
399
0
  while (IS_BLANK_CH(*cur))
400
0
            cur++;
401
0
    }
402
403
0
    value = cur;
404
0
    cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
405
0
    if ((cur == NULL) || (cur == value))
406
0
        return(1);
407
408
0
    if (*cur == ':') {
409
0
        cur += 1;
410
0
        value = cur;
411
0
        cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
412
0
        if ((cur == NULL) || (cur == value))
413
0
            return(1);
414
0
    }
415
416
0
    if (space) {
417
0
  while (IS_BLANK_CH(*cur))
418
0
            cur++;
419
0
    }
420
421
0
    return(*cur != 0);
422
0
}
423
424
/**
425
 * Check that a value conforms to the lexical space of Name
426
 *
427
 * @param value  the value to check
428
 * @param space  allow spaces in front and end of the string
429
 * @returns 0 if this validates, a positive error code number otherwise
430
 *         and -1 in case of internal or API error.
431
 */
432
int
433
0
xmlValidateName(const xmlChar *value, int space) {
434
0
    const xmlChar *cur;
435
436
0
    if (value == NULL)
437
0
        return(-1);
438
439
0
    cur = value;
440
441
0
    if (space) {
442
0
  while (IS_BLANK_CH(*cur))
443
0
            cur++;
444
0
    }
445
446
0
    value = cur;
447
0
    cur = xmlScanName(value, SIZE_MAX, 0);
448
0
    if ((cur == NULL) || (cur == value))
449
0
        return(1);
450
451
0
    if (space) {
452
0
  while (IS_BLANK_CH(*cur))
453
0
            cur++;
454
0
    }
455
456
0
    return(*cur != 0);
457
0
}
458
459
/**
460
 * Check that a value conforms to the lexical space of NMToken
461
 *
462
 * @param value  the value to check
463
 * @param space  allow spaces in front and end of the string
464
 * @returns 0 if this validates, a positive error code number otherwise
465
 *         and -1 in case of internal or API error.
466
 */
467
int
468
0
xmlValidateNMToken(const xmlChar *value, int space) {
469
0
    const xmlChar *cur;
470
471
0
    if (value == NULL)
472
0
        return(-1);
473
474
0
    cur = value;
475
476
0
    if (space) {
477
0
  while (IS_BLANK_CH(*cur))
478
0
            cur++;
479
0
    }
480
481
0
    value = cur;
482
0
    cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NMTOKEN);
483
0
    if ((cur == NULL) || (cur == value))
484
0
        return(1);
485
486
0
    if (space) {
487
0
  while (IS_BLANK_CH(*cur))
488
0
            cur++;
489
0
    }
490
491
0
    return(*cur != 0);
492
0
}
493
494
/************************************************************************
495
 *                  *
496
 *    Allocation and deallocation of basic structures   *
497
 *                  *
498
 ************************************************************************/
499
500
/**
501
 * Create a new namespace. For a default namespace, `prefix` should be
502
 * NULL. The namespace URI in `href` is not checked. You should make sure
503
 * to pass a valid URI.
504
 *
505
 * If `node` is provided, it must be an element node. The namespace will
506
 * be appended to the node's namespace declarations. It is an error if
507
 * the node already has a definition for the prefix or default
508
 * namespace.
509
 *
510
 * @param node  the element carrying the namespace (optional)
511
 * @param href  the URI associated
512
 * @param prefix  the prefix for the namespace (optional)
513
 * @returns a new namespace pointer or NULL if arguments are invalid,
514
 * the prefix is already in use or a memory allocation failed.
515
 */
516
xmlNs *
517
151k
xmlNewNs(xmlNode *node, const xmlChar *href, const xmlChar *prefix) {
518
151k
    xmlNsPtr cur;
519
520
151k
    if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
521
0
  return(NULL);
522
523
    /*
524
     * Allocate a new Namespace and fill the fields.
525
     */
526
151k
    cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
527
151k
    if (cur == NULL)
528
0
  return(NULL);
529
151k
    memset(cur, 0, sizeof(xmlNs));
530
151k
    cur->type = XML_LOCAL_NAMESPACE;
531
532
151k
    if (href != NULL) {
533
151k
  cur->href = xmlStrdup(href);
534
151k
        if (cur->href == NULL)
535
0
            goto error;
536
151k
    }
537
151k
    if (prefix != NULL) {
538
135k
  cur->prefix = xmlStrdup(prefix);
539
135k
        if (cur->prefix == NULL)
540
0
            goto error;
541
135k
    }
542
543
    /*
544
     * Add it at the end to preserve parsing order ...
545
     * and checks for existing use of the prefix
546
     */
547
151k
    if (node != NULL) {
548
0
  if (node->nsDef == NULL) {
549
0
      node->nsDef = cur;
550
0
  } else {
551
0
      xmlNsPtr prev = node->nsDef;
552
553
0
      if ((xmlStrEqual(prev->prefix, cur->prefix)) &&
554
0
                (prev->href != NULL))
555
0
                goto error;
556
0
      while (prev->next != NULL) {
557
0
          prev = prev->next;
558
0
    if ((xmlStrEqual(prev->prefix, cur->prefix)) &&
559
0
                    (prev->href != NULL))
560
0
                    goto error;
561
0
      }
562
0
      prev->next = cur;
563
0
  }
564
0
    }
565
151k
    return(cur);
566
567
0
error:
568
0
    xmlFreeNs(cur);
569
0
    return(NULL);
570
151k
}
571
572
/**
573
 * Set the namespace of an element or attribute node. Passing a NULL
574
 * namespace unsets the namespace.
575
 *
576
 * @param node  a node in the document
577
 * @param ns  a namespace pointer (optional)
578
 */
579
void
580
0
xmlSetNs(xmlNode *node, xmlNs *ns) {
581
0
    if (node == NULL) {
582
0
  return;
583
0
    }
584
0
    if ((node->type == XML_ELEMENT_NODE) ||
585
0
        (node->type == XML_ATTRIBUTE_NODE))
586
0
  node->ns = ns;
587
0
}
588
589
/**
590
 * Free an xmlNs object.
591
 *
592
 * @param cur  the namespace pointer
593
 */
594
void
595
151k
xmlFreeNs(xmlNs *cur) {
596
151k
    if (cur == NULL) {
597
0
  return;
598
0
    }
599
151k
    if (cur->href != NULL) xmlFree((char *) cur->href);
600
151k
    if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
601
151k
    xmlFree(cur);
602
151k
}
603
604
/**
605
 * Free a list of xmlNs objects.
606
 *
607
 * @param cur  the first namespace pointer
608
 */
609
void
610
19.4k
xmlFreeNsList(xmlNs *cur) {
611
19.4k
    xmlNsPtr next;
612
19.4k
    if (cur == NULL) {
613
0
  return;
614
0
    }
615
171k
    while (cur != NULL) {
616
151k
        next = cur->next;
617
151k
        xmlFreeNs(cur);
618
151k
  cur = next;
619
151k
    }
620
19.4k
}
621
622
/**
623
 * Create a DTD node.
624
 *
625
 * If a document is provided, it is an error if it already has an
626
 * external subset. If the document has no external subset, it
627
 * will be set to the created DTD.
628
 *
629
 * To create an internal subset, use #xmlCreateIntSubset.
630
 *
631
 * @param doc  the document pointer (optional)
632
 * @param name  the DTD name (optional)
633
 * @param publicId  public identifier of the DTD (optional)
634
 * @param systemId  system identifier (URL) of the DTD (optional)
635
 * @returns a pointer to the new DTD object or NULL if arguments are
636
 * invalid or a memory allocation failed.
637
 */
638
xmlDtd *
639
xmlNewDtd(xmlDoc *doc, const xmlChar *name, const xmlChar *publicId,
640
2.10k
          const xmlChar *systemId) {
641
2.10k
    xmlDtdPtr cur;
642
643
2.10k
    if ((doc != NULL) && (doc->extSubset != NULL)) {
644
0
  return(NULL);
645
0
    }
646
647
    /*
648
     * Allocate a new DTD and fill the fields.
649
     */
650
2.10k
    cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
651
2.10k
    if (cur == NULL)
652
0
  return(NULL);
653
2.10k
    memset(cur, 0 , sizeof(xmlDtd));
654
2.10k
    cur->type = XML_DTD_NODE;
655
656
2.10k
    if (name != NULL) {
657
2.10k
  cur->name = xmlStrdup(name);
658
2.10k
        if (cur->name == NULL)
659
0
            goto error;
660
2.10k
    }
661
2.10k
    if (publicId != NULL) {
662
0
  cur->ExternalID = xmlStrdup(publicId);
663
0
        if (cur->ExternalID == NULL)
664
0
            goto error;
665
0
    }
666
2.10k
    if (systemId != NULL) {
667
0
  cur->SystemID = xmlStrdup(systemId);
668
0
        if (cur->SystemID == NULL)
669
0
            goto error;
670
0
    }
671
2.10k
    if (doc != NULL)
672
2.10k
  doc->extSubset = cur;
673
2.10k
    cur->doc = doc;
674
675
2.10k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
676
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
677
2.10k
    return(cur);
678
679
0
error:
680
0
    xmlFreeDtd(cur);
681
0
    return(NULL);
682
2.10k
}
683
684
/**
685
 * Get the internal subset of a document.
686
 *
687
 * @param doc  the document pointer
688
 * @returns a pointer to the DTD object or NULL if not found.
689
 */
690
xmlDtd *
691
7.20k
xmlGetIntSubset(const xmlDoc *doc) {
692
7.20k
    xmlNodePtr cur;
693
694
7.20k
    if (doc == NULL)
695
0
  return(NULL);
696
7.20k
    cur = doc->children;
697
8.09k
    while (cur != NULL) {
698
890
  if (cur->type == XML_DTD_NODE)
699
0
      return((xmlDtdPtr) cur);
700
890
  cur = cur->next;
701
890
    }
702
7.20k
    return((xmlDtdPtr) doc->intSubset);
703
7.20k
}
704
705
/**
706
 * Create a DTD node.
707
 *
708
 * If a document is provided and it already has an internal subset,
709
 * the existing DTD object is returned without creating a new object.
710
 * If the document has no internal subset, it will be set to the
711
 * created DTD.
712
 *
713
 * @param doc  the document pointer (optional)
714
 * @param name  the DTD name (optional)
715
 * @param publicId  public identifier of the DTD (optional)
716
 * @param systemId  system identifier (URL) of the DTD (optional)
717
 * @returns a pointer to the new or existing DTD object or NULL if
718
 * arguments are invalid or a memory allocation failed.
719
 */
720
xmlDtd *
721
xmlCreateIntSubset(xmlDoc *doc, const xmlChar *name, const xmlChar *publicId,
722
3.60k
                   const xmlChar *systemId) {
723
3.60k
    xmlDtdPtr cur;
724
725
3.60k
    if (doc != NULL) {
726
3.60k
        cur = xmlGetIntSubset(doc);
727
3.60k
        if (cur != NULL)
728
0
            return(cur);
729
3.60k
    }
730
731
    /*
732
     * Allocate a new DTD and fill the fields.
733
     */
734
3.60k
    cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
735
3.60k
    if (cur == NULL)
736
0
  return(NULL);
737
3.60k
    memset(cur, 0, sizeof(xmlDtd));
738
3.60k
    cur->type = XML_DTD_NODE;
739
740
3.60k
    if (name != NULL) {
741
3.60k
  cur->name = xmlStrdup(name);
742
3.60k
  if (cur->name == NULL)
743
0
            goto error;
744
3.60k
    }
745
3.60k
    if (publicId != NULL) {
746
0
  cur->ExternalID = xmlStrdup(publicId);
747
0
  if (cur->ExternalID  == NULL)
748
0
            goto error;
749
0
    }
750
3.60k
    if (systemId != NULL) {
751
91
  cur->SystemID = xmlStrdup(systemId);
752
91
  if (cur->SystemID == NULL)
753
0
            goto error;
754
91
    }
755
3.60k
    if (doc != NULL) {
756
3.60k
  doc->intSubset = cur;
757
3.60k
  cur->parent = doc;
758
3.60k
  cur->doc = doc;
759
3.60k
  if (doc->children == NULL) {
760
3.56k
      doc->children = (xmlNodePtr) cur;
761
3.56k
      doc->last = (xmlNodePtr) cur;
762
3.56k
  } else {
763
42
      if (doc->type == XML_HTML_DOCUMENT_NODE) {
764
0
    xmlNodePtr prev;
765
766
0
    prev = doc->children;
767
0
    prev->prev = (xmlNodePtr) cur;
768
0
    cur->next = prev;
769
0
    doc->children = (xmlNodePtr) cur;
770
42
      } else {
771
42
    xmlNodePtr next;
772
773
42
    next = doc->children;
774
487
    while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
775
445
        next = next->next;
776
42
    if (next == NULL) {
777
42
        cur->prev = doc->last;
778
42
        cur->prev->next = (xmlNodePtr) cur;
779
42
        cur->next = NULL;
780
42
        doc->last = (xmlNodePtr) cur;
781
42
    } else {
782
0
        cur->next = next;
783
0
        cur->prev = next->prev;
784
0
        if (cur->prev == NULL)
785
0
      doc->children = (xmlNodePtr) cur;
786
0
        else
787
0
      cur->prev->next = (xmlNodePtr) cur;
788
0
        next->prev = (xmlNodePtr) cur;
789
0
    }
790
42
      }
791
42
  }
792
3.60k
    }
793
794
3.60k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
795
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
796
3.60k
    return(cur);
797
798
0
error:
799
0
    xmlFreeDtd(cur);
800
0
    return(NULL);
801
3.60k
}
802
803
/**
804
 * Free a string if it is not owned by the "dict" dictionary in the
805
 * current scope
806
 *
807
 * @param str  a string
808
 */
809
#define DICT_FREE(str)            \
810
215k
  if ((str) && ((!dict) ||       \
811
196k
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
812
196k
      xmlFree((char *)(str));
813
814
/**
815
 * Free a DTD structure.
816
 *
817
 * @param cur  the DTD structure to free up
818
 */
819
void
820
5.71k
xmlFreeDtd(xmlDtd *cur) {
821
5.71k
    xmlDictPtr dict = NULL;
822
823
5.71k
    if (cur == NULL) {
824
0
  return;
825
0
    }
826
5.71k
    if (cur->doc != NULL) dict = cur->doc->dict;
827
828
5.71k
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
829
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
830
831
5.71k
    if (cur->children != NULL) {
832
5.44k
  xmlNodePtr next, c = cur->children;
833
834
  /*
835
   * Cleanup all nodes which are not part of the specific lists
836
   * of notations, elements, attributes and entities.
837
   */
838
19.8k
        while (c != NULL) {
839
14.4k
      next = c->next;
840
14.4k
      if ((c->type != XML_ELEMENT_DECL) &&
841
14.1k
    (c->type != XML_ATTRIBUTE_DECL) &&
842
8.99k
    (c->type != XML_ENTITY_DECL)) {
843
1.35k
    xmlUnlinkNodeInternal(c);
844
1.35k
    xmlFreeNode(c);
845
1.35k
      }
846
14.4k
      c = next;
847
14.4k
  }
848
5.44k
    }
849
850
5.71k
    DICT_FREE(cur->name)
851
852
5.71k
    if (cur->SystemID != NULL)
853
91
        xmlFree(cur->SystemID);
854
5.71k
    if (cur->ExternalID != NULL)
855
0
        xmlFree(cur->ExternalID);
856
857
    /* TODO !!! */
858
5.71k
    if (cur->notations != NULL)
859
32
        xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
860
861
5.71k
    if (cur->elements != NULL)
862
753
        xmlFreeElementTable((xmlElementTablePtr) cur->elements);
863
5.71k
    if (cur->attributes != NULL)
864
681
        xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
865
5.71k
    if (cur->entities != NULL)
866
3.00k
        xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
867
5.71k
    if (cur->pentities != NULL)
868
1.88k
        xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
869
870
5.71k
    xmlFree(cur);
871
5.71k
}
872
873
/**
874
 * Creates a new XML document. If version is NULL, `"1.0"` is used.
875
 *
876
 * @param version  XML version string like `"1.0"` (optional)
877
 * @returns a new document or NULL if a memory allocation failed.
878
 */
879
xmlDoc *
880
17.9k
xmlNewDoc(const xmlChar *version) {
881
17.9k
    xmlDocPtr cur;
882
883
17.9k
    if (version == NULL)
884
0
  version = (const xmlChar *) "1.0";
885
886
    /*
887
     * Allocate a new document and fill the fields.
888
     */
889
17.9k
    cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
890
17.9k
    if (cur == NULL)
891
0
  return(NULL);
892
17.9k
    memset(cur, 0, sizeof(xmlDoc));
893
17.9k
    cur->type = XML_DOCUMENT_NODE;
894
895
17.9k
    cur->version = xmlStrdup(version);
896
17.9k
    if (cur->version == NULL) {
897
0
  xmlFree(cur);
898
0
  return(NULL);
899
0
    }
900
17.9k
    cur->standalone = -1;
901
17.9k
    cur->compression = -1; /* not initialized */
902
17.9k
    cur->doc = cur;
903
17.9k
    cur->parseFlags = 0;
904
17.9k
    cur->properties = XML_DOC_USERBUILT;
905
    /*
906
     * The in memory encoding is always UTF8
907
     * This field will never change and would
908
     * be obsolete if not for binary compatibility.
909
     */
910
17.9k
    cur->charset = XML_CHAR_ENCODING_UTF8;
911
912
17.9k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
913
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
914
17.9k
    return(cur);
915
17.9k
}
916
917
/**
918
 * Free a document including all children and associated DTDs.
919
 *
920
 * @param cur  pointer to the document
921
 */
922
void
923
17.9k
xmlFreeDoc(xmlDoc *cur) {
924
17.9k
    xmlDtdPtr extSubset, intSubset;
925
17.9k
    xmlDictPtr dict = NULL;
926
927
17.9k
    if (cur == NULL) {
928
0
  return;
929
0
    }
930
931
17.9k
    dict = cur->dict;
932
933
17.9k
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
934
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
935
936
    /*
937
     * Do this before freeing the children list to avoid ID lookups
938
     */
939
17.9k
    if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
940
17.9k
    cur->ids = NULL;
941
17.9k
    if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
942
17.9k
    cur->refs = NULL;
943
17.9k
    extSubset = cur->extSubset;
944
17.9k
    intSubset = cur->intSubset;
945
17.9k
    if (intSubset == extSubset)
946
14.3k
  extSubset = NULL;
947
17.9k
    if (extSubset != NULL) {
948
0
  xmlUnlinkNodeInternal((xmlNodePtr) cur->extSubset);
949
0
  cur->extSubset = NULL;
950
0
  xmlFreeDtd(extSubset);
951
0
    }
952
17.9k
    if (intSubset != NULL) {
953
5.71k
  xmlUnlinkNodeInternal((xmlNodePtr) cur->intSubset);
954
5.71k
  cur->intSubset = NULL;
955
5.71k
  xmlFreeDtd(intSubset);
956
5.71k
    }
957
958
17.9k
    if (cur->children != NULL) xmlFreeNodeList(cur->children);
959
17.9k
    if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
960
961
17.9k
    DICT_FREE(cur->name)
962
963
17.9k
    if (cur->version != NULL)
964
17.9k
        xmlFree(cur->version);
965
17.9k
    if (cur->encoding != NULL)
966
662
        xmlFree(cur->encoding);
967
17.9k
    if (cur->URL != NULL)
968
0
        xmlFree(cur->URL);
969
970
17.9k
    xmlFree(cur);
971
17.9k
    if (dict) xmlDictFree(dict);
972
17.9k
}
973
974
/**
975
 * Parse an attribute value and replace the node's children with
976
 * the resulting node list.
977
 *
978
 * `content` is expected to be a valid XML attribute value possibly
979
 * containing character and entity references. Syntax errors
980
 * and references to undeclared entities are ignored silently.
981
 * Only references are handled, nested elements, comments or PIs are
982
 * not.
983
 *
984
 * This function is used by the SAX parser to parse attribute
985
 * values a second time if entities aren't substituted. It's also
986
 * used to parse node content in the tree API, but this is a
987
 * historical mistake.
988
 *
989
 * @param doc  a document (optional)
990
 * @param attr  an attribute or element (optional)
991
 * @param value  an attribute value
992
 * @param len  maximum length of the attribute value
993
 * @param listPtr  pointer to the resulting node list (optional)
994
 * @returns 0 on success, -1 if a memory allocation failed.
995
 */
996
int
997
xmlNodeParseAttValue(const xmlDoc *doc, xmlAttr *attr,
998
0
                     const xmlChar *value, size_t len, xmlNode **listPtr) {
999
0
    xmlNodePtr head = NULL, last = NULL;
1000
0
    xmlNodePtr node;
1001
0
    xmlChar *val = NULL;
1002
0
    const xmlChar *cur;
1003
0
    const xmlChar *q;
1004
0
    xmlEntityPtr ent;
1005
0
    xmlBufPtr buf;
1006
0
    size_t remaining = len;
1007
1008
0
    if (listPtr != NULL)
1009
0
        *listPtr = NULL;
1010
1011
0
    if ((value == NULL) || (value[0] == 0))
1012
0
        goto done;
1013
1014
0
    cur = value;
1015
1016
0
    buf = xmlBufCreate(50);
1017
0
    if (buf == NULL)
1018
0
        return(-1);
1019
1020
0
    q = cur;
1021
0
    while ((remaining > 0) && (*cur != 0)) {
1022
0
  if (cur[0] == '&') {
1023
0
      int charval = 0;
1024
1025
      /*
1026
       * Save the current text.
1027
       */
1028
0
            if (cur != q) {
1029
0
    if (xmlBufAdd(buf, q, cur - q))
1030
0
        goto out;
1031
0
          q = cur;
1032
0
      }
1033
1034
0
      if ((remaining > 2) && (cur[1] == '#') && (cur[2] == 'x')) {
1035
0
          int tmp = 0;
1036
1037
0
    cur += 3;
1038
0
                remaining -= 3;
1039
0
    while ((remaining > 0) && ((tmp = *cur) != ';')) {
1040
0
        if ((tmp >= '0') && (tmp <= '9'))
1041
0
      charval = charval * 16 + (tmp - '0');
1042
0
        else if ((tmp >= 'a') && (tmp <= 'f'))
1043
0
      charval = charval * 16 + (tmp - 'a') + 10;
1044
0
        else if ((tmp >= 'A') && (tmp <= 'F'))
1045
0
      charval = charval * 16 + (tmp - 'A') + 10;
1046
0
        else {
1047
0
      charval = 0;
1048
0
      break;
1049
0
        }
1050
0
                    if (charval > 0x110000)
1051
0
                        charval = 0x110000;
1052
0
        cur++;
1053
0
                    remaining--;
1054
0
    }
1055
0
    if (tmp == ';') {
1056
0
        cur++;
1057
0
                    remaining--;
1058
0
                }
1059
0
    q = cur;
1060
0
      } else if ((remaining > 1) && (cur[1] == '#')) {
1061
0
          int tmp = 0;
1062
1063
0
    cur += 2;
1064
0
                remaining -= 2;
1065
0
    while ((remaining > 0) && ((tmp = *cur) != ';')) {
1066
0
        if ((tmp >= '0') && (tmp <= '9'))
1067
0
      charval = charval * 10 + (tmp - '0');
1068
0
        else {
1069
0
      charval = 0;
1070
0
      break;
1071
0
        }
1072
0
                    if (charval > 0x110000)
1073
0
                        charval = 0x110000;
1074
0
        cur++;
1075
0
                    remaining--;
1076
0
    }
1077
0
    if (tmp == ';') {
1078
0
        cur++;
1079
0
                    remaining--;
1080
0
                }
1081
0
    q = cur;
1082
0
      } else {
1083
    /*
1084
     * Read the entity string
1085
     */
1086
0
    cur++;
1087
0
                remaining--;
1088
0
    q = cur;
1089
0
    while ((remaining > 0) && (*cur != 0) && (*cur != ';')) {
1090
0
                    cur++;
1091
0
                    remaining--;
1092
0
                }
1093
0
    if ((remaining <= 0) || (*cur == 0))
1094
0
        break;
1095
0
    if (cur != q) {
1096
0
        val = xmlStrndup(q, cur - q);
1097
0
                    if (val == NULL)
1098
0
                        goto out;
1099
0
        ent = xmlGetDocEntity(doc, val);
1100
0
        if ((ent != NULL) &&
1101
0
      (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1102
                        /*
1103
                         * Predefined entities don't generate nodes
1104
                         */
1105
0
      if (xmlBufCat(buf, ent->content))
1106
0
          goto out;
1107
0
                    } else if (ent == NULL ||
1108
0
                               (ent->flags & XML_ENT_EXPANDING) == 0) {
1109
      /*
1110
       * Flush buffer so far
1111
       */
1112
0
      if (!xmlBufIsEmpty(buf)) {
1113
0
          node = xmlNewDocText(doc, NULL);
1114
0
          if (node == NULL)
1115
0
        goto out;
1116
0
          node->content = xmlBufDetach(buf);
1117
0
                            node->parent = (xmlNode *) attr;
1118
1119
0
          if (last == NULL) {
1120
0
        head = node;
1121
0
          } else {
1122
0
                                last->next = node;
1123
0
                                node->prev = last;
1124
0
          }
1125
0
                            last = node;
1126
0
      }
1127
1128
0
      if ((ent != NULL) &&
1129
0
                            ((ent->flags & XML_ENT_PARSED) == 0) &&
1130
0
                            (ent->content != NULL)) {
1131
0
                            int res;
1132
1133
0
                            ent->flags |= XML_ENT_EXPANDING;
1134
0
                            res = xmlNodeParseAttValue(doc, (xmlAttr *) ent,
1135
0
                                                       ent->content, SIZE_MAX,
1136
0
                                                       NULL);
1137
0
                            ent->flags &= ~XML_ENT_EXPANDING;
1138
0
                            if (res < 0)
1139
0
                                goto out;
1140
0
                            ent->flags |= XML_ENT_PARSED;
1141
0
      }
1142
1143
      /*
1144
       * Create a new REFERENCE_REF node
1145
       */
1146
0
      node = xmlNewEntityRef((xmlDocPtr) doc, val);
1147
0
                        val = NULL;
1148
0
      if (node == NULL)
1149
0
          goto out;
1150
0
                        node->parent = (xmlNode *) attr;
1151
0
                        node->last = (xmlNodePtr) ent;
1152
0
                        if (ent != NULL) {
1153
0
                            node->children = (xmlNodePtr) ent;
1154
0
                            node->content = ent->content;
1155
0
                        }
1156
1157
0
      if (last == NULL) {
1158
0
          head = node;
1159
0
      } else {
1160
0
                            last->next = node;
1161
0
                            node->prev = last;
1162
0
      }
1163
0
                        last = node;
1164
0
        }
1165
0
        xmlFree(val);
1166
0
                    val = NULL;
1167
0
    }
1168
0
    cur++;
1169
0
                remaining--;
1170
0
    q = cur;
1171
0
      }
1172
0
      if (charval != 0) {
1173
0
    xmlChar buffer[10];
1174
0
    int l;
1175
1176
0
                if (charval >= 0x110000)
1177
0
                    charval = 0xFFFD; /* replacement character */
1178
1179
0
    l = xmlCopyCharMultiByte(buffer, charval);
1180
0
    buffer[l] = 0;
1181
1182
0
    if (xmlBufCat(buf, buffer))
1183
0
        goto out;
1184
0
      }
1185
0
  } else {
1186
0
      cur++;
1187
0
            remaining--;
1188
0
        }
1189
0
    }
1190
1191
0
    if (cur != q) {
1192
        /*
1193
   * Handle the last piece of text.
1194
   */
1195
0
  if (xmlBufAdd(buf, q, cur - q))
1196
0
      goto out;
1197
0
    }
1198
1199
0
    if (!xmlBufIsEmpty(buf)) {
1200
0
  node = xmlNewDocText(doc, NULL);
1201
0
  if (node == NULL)
1202
0
            goto out;
1203
0
        node->parent = (xmlNode *) attr;
1204
0
  node->content = xmlBufDetach(buf);
1205
1206
0
  if (last == NULL) {
1207
0
      head = node;
1208
0
  } else {
1209
0
            last->next = node;
1210
0
            node->prev = last;
1211
0
  }
1212
0
        last = node;
1213
0
    } else if (head == NULL) {
1214
0
        head = xmlNewDocText(doc, BAD_CAST "");
1215
0
        if (head == NULL)
1216
0
            goto out;
1217
0
        head->parent = (xmlNode *) attr;
1218
0
        last = head;
1219
0
    }
1220
1221
0
    xmlBufFree(buf);
1222
1223
0
done:
1224
0
    if (attr != NULL) {
1225
0
        if (attr->children != NULL)
1226
0
            xmlFreeNodeList(attr->children);
1227
0
        attr->children = head;
1228
0
        attr->last = last;
1229
0
    }
1230
1231
0
    if (listPtr != NULL)
1232
0
        *listPtr = head;
1233
1234
0
    return(0);
1235
1236
0
out:
1237
0
    xmlBufFree(buf);
1238
0
    if (val != NULL)
1239
0
        xmlFree(val);
1240
0
    if (head != NULL)
1241
0
        xmlFreeNodeList(head);
1242
0
    return(-1);
1243
0
}
1244
1245
/**
1246
 * See #xmlStringGetNodeList.
1247
 *
1248
 * @deprecated Internal function, don't use.
1249
 *
1250
 * @param doc  a document (optional)
1251
 * @param value  an attribute value
1252
 * @param len  maximum length of the attribute value
1253
 * @returns a pointer to the first child or NULL if the value if empty
1254
 * or a memory allocation failed.
1255
 */
1256
xmlNode *
1257
0
xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
1258
0
    xmlNodePtr ret;
1259
0
    size_t maxSize = len < 0 ? SIZE_MAX : (size_t) len;
1260
1261
0
    xmlNodeParseAttValue(doc, NULL, value, maxSize, &ret);
1262
0
    return(ret);
1263
0
}
1264
1265
/**
1266
 * Parse an attribute value and build a node list with
1267
 * text and entity reference nodes. The resulting nodes will be
1268
 * associated with the document if provided. The document is also
1269
 * used to look up entities.
1270
 *
1271
 * @deprecated Internal function, don't use.
1272
 *
1273
 * The input is not validated. Syntax errors or references to
1274
 * undeclared entities will be ignored silently with unspecified
1275
 * results.
1276
 *
1277
 * @param doc  a document (optional)
1278
 * @param value  an attribute value
1279
 * @returns a pointer to the first child or NULL if the value if empty
1280
 * or a memory allocation failed.
1281
 */
1282
xmlNode *
1283
0
xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
1284
0
    xmlNodePtr ret;
1285
1286
0
    xmlNodeParseAttValue(doc, NULL, value, SIZE_MAX, &ret);
1287
0
    return(ret);
1288
0
}
1289
1290
/**
1291
 * @param node  a node list
1292
 * @param escape  whether to escape characters and keep entity refs
1293
 * @param flags  escape flags
1294
 * @returns a pointer to the string.
1295
 */
1296
xmlChar *
1297
0
xmlNodeListGetStringInternal(const xmlNode *node, int escape, int flags) {
1298
0
    xmlBufPtr buf;
1299
0
    xmlChar *ret;
1300
1301
0
    if (node == NULL)
1302
0
        return(xmlStrdup(BAD_CAST ""));
1303
1304
0
    if ((escape == 0) &&
1305
0
        ((node->type == XML_TEXT_NODE) ||
1306
0
         (node->type == XML_CDATA_SECTION_NODE)) &&
1307
0
        (node->next == NULL)) {
1308
0
        if (node->content == NULL)
1309
0
            return(xmlStrdup(BAD_CAST ""));
1310
0
        return(xmlStrdup(node->content));
1311
0
    }
1312
1313
0
    buf = xmlBufCreate(50);
1314
0
    if (buf == NULL)
1315
0
        return(NULL);
1316
1317
0
    while (node != NULL) {
1318
0
        if ((node->type == XML_TEXT_NODE) ||
1319
0
            (node->type == XML_CDATA_SECTION_NODE)) {
1320
0
            if (node->content != NULL) {
1321
0
                if (escape == 0) {
1322
0
                    xmlBufCat(buf, node->content);
1323
0
                } else {
1324
0
                    xmlChar *encoded;
1325
1326
0
                    encoded = xmlEscapeText(node->content, flags);
1327
0
                    if (encoded == NULL)
1328
0
                        goto error;
1329
0
                    xmlBufCat(buf, encoded);
1330
0
                    xmlFree(encoded);
1331
0
                }
1332
0
            }
1333
0
        } else if (node->type == XML_ENTITY_REF_NODE) {
1334
0
            if (escape == 0) {
1335
0
                xmlBufGetNodeContent(buf, node);
1336
0
            } else {
1337
0
                xmlBufAdd(buf, BAD_CAST "&", 1);
1338
0
                xmlBufCat(buf, node->name);
1339
0
                xmlBufAdd(buf, BAD_CAST ";", 1);
1340
0
            }
1341
0
        }
1342
1343
0
        node = node->next;
1344
0
    }
1345
1346
0
    ret = xmlBufDetach(buf);
1347
0
    xmlBufFree(buf);
1348
0
    return(ret);
1349
1350
0
error:
1351
0
    xmlBufFree(buf);
1352
0
    return(NULL);
1353
0
}
1354
1355
/**
1356
 * Serializes attribute children (text and entity reference nodes)
1357
 * into a string.
1358
 *
1359
 * If `inLine` is true, entity references will be substituted.
1360
 * Otherwise, entity references will be kept and special characters
1361
 * like `&` as well as non-ASCII chars will be escaped. See
1362
 * #xmlEncodeEntitiesReentrant for details. If `list` is the child
1363
 * of an attribute, escaping rules apply are adjusted.
1364
 *
1365
 * See #xmlNodeListGetRawString for an alternative option.
1366
 *
1367
 * @param doc  a document (optional)
1368
 * @param list  a node list of attribute children
1369
 * @param inLine  whether entity references are substituted
1370
 * @returns a string or NULL if a memory allocation failed.
1371
 */
1372
xmlChar *
1373
xmlNodeListGetString(xmlDoc *doc, const xmlNode *list, int inLine)
1374
0
{
1375
0
    int flags = 0;
1376
0
    int escape = 0;
1377
1378
    /* backward compatibility */
1379
0
    if (list == NULL)
1380
0
        return(NULL);
1381
1382
0
    if (!inLine) {
1383
0
        escape = 1;
1384
1385
0
        if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE))
1386
0
            flags |= XML_ESCAPE_HTML;
1387
0
        else if ((doc == NULL) || (doc->encoding == NULL))
1388
0
            flags |= XML_ESCAPE_NON_ASCII;
1389
1390
0
        if ((list->parent != NULL) &&
1391
0
            (list->parent->type == XML_ATTRIBUTE_NODE))
1392
0
            flags |= XML_ESCAPE_ATTR;
1393
0
    }
1394
1395
0
    return(xmlNodeListGetStringInternal(list, escape, flags));
1396
0
}
1397
1398
/**
1399
 * Serializes attribute children (text and entity reference nodes)
1400
 * into a string.
1401
 *
1402
 * If `inLine` is true, entity references will be substituted.
1403
 * Otherwise, entity references will be kept and special characters
1404
 * like `&` will be escaped. See #xmlEncodeSpecialChars for
1405
 * details.
1406
 *
1407
 * @param doc  a document (unused)
1408
 * @param list  a node list of attribute children
1409
 * @param inLine  whether entity references are substituted
1410
 * @returns a string or NULL if a memory allocation failed.
1411
 */
1412
xmlChar *
1413
xmlNodeListGetRawString(const xmlDoc *doc ATTRIBUTE_UNUSED,
1414
                        const xmlNode *list, int inLine)
1415
0
{
1416
0
    int escape = 0;
1417
0
    int flags = 0;
1418
1419
    /* backward compatibility */
1420
0
    if (list == NULL)
1421
0
        return(NULL);
1422
1423
0
    if (!inLine) {
1424
0
        escape = 1;
1425
0
        flags = XML_ESCAPE_QUOT;
1426
0
    }
1427
1428
0
    return(xmlNodeListGetStringInternal(list, escape, flags));
1429
0
}
1430
1431
static xmlAttrPtr
1432
xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1433
                   const xmlChar * name, const xmlChar * value,
1434
                   int eatname)
1435
0
{
1436
0
    xmlAttrPtr cur;
1437
0
    xmlDocPtr doc = NULL;
1438
1439
0
    if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1440
0
        if ((eatname == 1) &&
1441
0
      ((node->doc == NULL) || (node->doc->dict == NULL) ||
1442
0
       (!(xmlDictOwns(node->doc->dict, name)))))
1443
0
            xmlFree((xmlChar *) name);
1444
0
        return (NULL);
1445
0
    }
1446
1447
    /*
1448
     * Allocate a new property and fill the fields.
1449
     */
1450
0
    cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1451
0
    if (cur == NULL) {
1452
0
        if ((eatname == 1) &&
1453
0
      ((node == NULL) || (node->doc == NULL) ||
1454
0
             (node->doc->dict == NULL) ||
1455
0
       (!(xmlDictOwns(node->doc->dict, name)))))
1456
0
            xmlFree((xmlChar *) name);
1457
0
        return (NULL);
1458
0
    }
1459
0
    memset(cur, 0, sizeof(xmlAttr));
1460
0
    cur->type = XML_ATTRIBUTE_NODE;
1461
1462
0
    cur->parent = node;
1463
0
    if (node != NULL) {
1464
0
        doc = node->doc;
1465
0
        cur->doc = doc;
1466
0
    }
1467
0
    cur->ns = ns;
1468
1469
0
    if (eatname == 0) {
1470
0
        if ((doc != NULL) && (doc->dict != NULL))
1471
0
            cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1472
0
        else
1473
0
            cur->name = xmlStrdup(name);
1474
0
        if (cur->name == NULL)
1475
0
            goto error;
1476
0
    } else
1477
0
        cur->name = name;
1478
1479
0
    if (value != NULL) {
1480
0
        xmlNodePtr tmp;
1481
1482
0
        cur->children = xmlNewDocText(doc, value);
1483
0
        if (cur->children == NULL)
1484
0
            goto error;
1485
0
        cur->last = NULL;
1486
0
        tmp = cur->children;
1487
0
        while (tmp != NULL) {
1488
0
            tmp->parent = (xmlNodePtr) cur;
1489
0
            if (tmp->next == NULL)
1490
0
                cur->last = tmp;
1491
0
            tmp = tmp->next;
1492
0
        }
1493
1494
0
        if (doc != NULL) {
1495
0
            int res = xmlIsID(doc, node, cur);
1496
1497
0
            if (res < 0)
1498
0
                goto error;
1499
0
            if ((res == 1) && (xmlAddIDSafe(cur, value) < 0))
1500
0
                goto error;
1501
0
        }
1502
0
    }
1503
1504
    /*
1505
     * Add it at the end to preserve parsing order ...
1506
     */
1507
0
    if (node != NULL) {
1508
0
        if (node->properties == NULL) {
1509
0
            node->properties = cur;
1510
0
        } else {
1511
0
            xmlAttrPtr prev = node->properties;
1512
1513
0
            while (prev->next != NULL)
1514
0
                prev = prev->next;
1515
0
            prev->next = cur;
1516
0
            cur->prev = prev;
1517
0
        }
1518
0
    }
1519
1520
0
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1521
0
        xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1522
0
    return (cur);
1523
1524
0
error:
1525
0
    xmlFreeProp(cur);
1526
0
    return(NULL);
1527
0
}
1528
1529
/**
1530
 * Create an attribute node.
1531
 *
1532
 * If provided, `value` should be a raw, unescaped string.
1533
 *
1534
 * If `node` is provided, the created attribute will be appended without
1535
 * checking for duplicate names. It is an error if `node` is not an
1536
 * element.
1537
 *
1538
 * @param node  the parent node (optional)
1539
 * @param name  the name of the attribute
1540
 * @param value  the value of the attribute (optional)
1541
 * @returns a pointer to the attribute or NULL if arguments are invalid
1542
 * or a memory allocation failed.
1543
 */
1544
xmlAttr *
1545
0
xmlNewProp(xmlNode *node, const xmlChar *name, const xmlChar *value) {
1546
1547
0
    if (name == NULL) {
1548
0
  return(NULL);
1549
0
    }
1550
1551
0
  return xmlNewPropInternal(node, NULL, name, value, 0);
1552
0
}
1553
1554
/**
1555
 * Create an attribute node.
1556
 *
1557
 * If provided, `value` should be a raw, unescaped string.
1558
 *
1559
 * If `node` is provided, the created attribute will be appended without
1560
 * checking for duplicate names. It is an error if `node` is not an
1561
 * element.
1562
 *
1563
 * @param node  the parent node (optional)
1564
 * @param ns  the namespace (optional)
1565
 * @param name  the local name of the attribute
1566
 * @param value  the value of the attribute (optional)
1567
 * @returns a pointer to the attribute or NULL if arguments are invalid
1568
 * or a memory allocation failed.
1569
 */
1570
xmlAttr *
1571
xmlNewNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name,
1572
0
           const xmlChar *value) {
1573
1574
0
    if (name == NULL) {
1575
0
  return(NULL);
1576
0
    }
1577
1578
0
    return xmlNewPropInternal(node, ns, name, value, 0);
1579
0
}
1580
1581
/**
1582
 * Create an attribute node.
1583
 *
1584
 * Like #xmlNewNsProp, but the `name` string will be used directly
1585
 * without making a copy. Takes ownership of `name` which will also
1586
 * be freed on error.
1587
 *
1588
 * @param node  the parent node (optional)
1589
 * @param ns  the namespace (optional)
1590
 * @param name  the local name of the attribute
1591
 * @param value  the value of the attribute (optional)
1592
 * @returns a pointer to the attribute or NULL if arguments are invalid
1593
 * or a memory allocation failed.
1594
 */
1595
xmlAttr *
1596
xmlNewNsPropEatName(xmlNode *node, xmlNs *ns, xmlChar *name,
1597
0
           const xmlChar *value) {
1598
1599
0
    if (name == NULL) {
1600
0
  return(NULL);
1601
0
    }
1602
1603
0
    return xmlNewPropInternal(node, ns, name, value, 1);
1604
0
}
1605
1606
/**
1607
 * Create an attribute node.
1608
 *
1609
 * If provided, `value` is expected to be a valid XML attribute value
1610
 * possibly containing character and entity references. Syntax errors
1611
 * and references to undeclared entities are ignored silently.
1612
 * If you want to pass a raw string, see #xmlNewProp.
1613
 *
1614
 * @param doc  the target document (optional)
1615
 * @param name  the name of the attribute
1616
 * @param value  attribute value with XML references (optional)
1617
 * @returns a pointer to the attribute or NULL if arguments are invalid
1618
 * or a memory allocation failed.
1619
 */
1620
xmlAttr *
1621
1.81k
xmlNewDocProp(xmlDoc *doc, const xmlChar *name, const xmlChar *value) {
1622
1.81k
    xmlAttrPtr cur;
1623
1624
1.81k
    if (name == NULL) {
1625
0
  return(NULL);
1626
0
    }
1627
1628
    /*
1629
     * Allocate a new property and fill the fields.
1630
     */
1631
1.81k
    cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1632
1.81k
    if (cur == NULL)
1633
0
  return(NULL);
1634
1.81k
    memset(cur, 0, sizeof(xmlAttr));
1635
1.81k
    cur->type = XML_ATTRIBUTE_NODE;
1636
1637
1.81k
    if ((doc != NULL) && (doc->dict != NULL))
1638
0
  cur->name = xmlDictLookup(doc->dict, name, -1);
1639
1.81k
    else
1640
1.81k
  cur->name = xmlStrdup(name);
1641
1.81k
    if (cur->name == NULL)
1642
0
        goto error;
1643
1.81k
    cur->doc = doc;
1644
1.81k
    if (value != NULL) {
1645
        /*
1646
         * We shouldn't parse the attribute value here,
1647
         * but the API can't be changed.
1648
         */
1649
0
        if (xmlNodeParseAttValue(doc, cur, value, SIZE_MAX, NULL) < 0)
1650
0
            goto error;
1651
0
    }
1652
1653
1.81k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1654
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1655
1.81k
    return(cur);
1656
1657
0
error:
1658
0
    xmlFreeProp(cur);
1659
0
    return(NULL);
1660
1.81k
}
1661
1662
/**
1663
 * Free an attribute list including all children.
1664
 *
1665
 * @param cur  the first attribute in the list
1666
 */
1667
void
1668
24.4k
xmlFreePropList(xmlAttr *cur) {
1669
24.4k
    xmlAttrPtr next;
1670
24.4k
    if (cur == NULL) return;
1671
79.7k
    while (cur != NULL) {
1672
55.3k
        next = cur->next;
1673
55.3k
        xmlFreeProp(cur);
1674
55.3k
  cur = next;
1675
55.3k
    }
1676
24.4k
}
1677
1678
/**
1679
 * Free an attribute including all children.
1680
 *
1681
 * @param cur  an attribute
1682
 */
1683
void
1684
55.3k
xmlFreeProp(xmlAttr *cur) {
1685
55.3k
    xmlDictPtr dict = NULL;
1686
55.3k
    if (cur == NULL) return;
1687
1688
55.3k
    if (cur->doc != NULL) dict = cur->doc->dict;
1689
1690
55.3k
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1691
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1692
1693
    /* Check for ID removal -> leading to invalid references ! */
1694
55.3k
    if (cur->doc != NULL && cur->id != NULL) {
1695
34
        xmlRemoveID(cur->doc, cur);
1696
34
    }
1697
55.3k
    if (cur->children != NULL) xmlFreeNodeList(cur->children);
1698
55.3k
    DICT_FREE(cur->name)
1699
55.3k
    xmlFree(cur);
1700
55.3k
}
1701
1702
/**
1703
 * Unlink and free an attribute including all children.
1704
 *
1705
 * Note this doesn't work for namespace declarations.
1706
 *
1707
 * The attribute must have a non-NULL parent pointer.
1708
 *
1709
 * @param cur  an attribute
1710
 * @returns 0 on success or -1 if the attribute was not found or
1711
 * arguments are invalid.
1712
 */
1713
int
1714
0
xmlRemoveProp(xmlAttr *cur) {
1715
0
    xmlAttrPtr tmp;
1716
0
    if (cur == NULL) {
1717
0
  return(-1);
1718
0
    }
1719
0
    if (cur->parent == NULL) {
1720
0
  return(-1);
1721
0
    }
1722
0
    tmp = cur->parent->properties;
1723
0
    if (tmp == cur) {
1724
0
        cur->parent->properties = cur->next;
1725
0
    if (cur->next != NULL)
1726
0
      cur->next->prev = NULL;
1727
0
  xmlFreeProp(cur);
1728
0
  return(0);
1729
0
    }
1730
0
    while (tmp != NULL) {
1731
0
  if (tmp->next == cur) {
1732
0
      tmp->next = cur->next;
1733
0
      if (tmp->next != NULL)
1734
0
    tmp->next->prev = tmp;
1735
0
      xmlFreeProp(cur);
1736
0
      return(0);
1737
0
  }
1738
0
        tmp = tmp->next;
1739
0
    }
1740
0
    return(-1);
1741
0
}
1742
1743
/**
1744
 * Create a processing instruction object.
1745
 *
1746
 * @param doc  the target document (optional)
1747
 * @param name  the processing instruction target
1748
 * @param content  the PI content (optional)
1749
 * @returns a pointer to the new node object or NULL if arguments are
1750
 * invalid or a memory allocation failed.
1751
 */
1752
xmlNode *
1753
2.10k
xmlNewDocPI(xmlDoc *doc, const xmlChar *name, const xmlChar *content) {
1754
2.10k
    xmlNodePtr cur;
1755
1756
2.10k
    if (name == NULL) {
1757
0
  return(NULL);
1758
0
    }
1759
1760
    /*
1761
     * Allocate a new node and fill the fields.
1762
     */
1763
2.10k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1764
2.10k
    if (cur == NULL)
1765
0
  return(NULL);
1766
2.10k
    memset(cur, 0, sizeof(xmlNode));
1767
2.10k
    cur->type = XML_PI_NODE;
1768
2.10k
    cur->doc = doc;
1769
1770
2.10k
    if ((doc != NULL) && (doc->dict != NULL))
1771
0
        cur->name = xmlDictLookup(doc->dict, name, -1);
1772
2.10k
    else
1773
2.10k
  cur->name = xmlStrdup(name);
1774
2.10k
    if (cur->name == NULL)
1775
0
        goto error;
1776
2.10k
    if (content != NULL) {
1777
378
  cur->content = xmlStrdup(content);
1778
378
        if (cur->content == NULL)
1779
0
            goto error;
1780
378
    }
1781
1782
2.10k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1783
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1784
2.10k
    return(cur);
1785
1786
0
error:
1787
0
    xmlFreeNode(cur);
1788
0
    return(NULL);
1789
2.10k
}
1790
1791
/**
1792
 * Create a processing instruction node.
1793
 *
1794
 * Use of this function is DISCOURAGED in favor of #xmlNewDocPI.
1795
 *
1796
 * @param name  the processing instruction target
1797
 * @param content  the PI content (optional)
1798
 * @returns a pointer to the new node object or NULL if arguments are
1799
 * invalid or a memory allocation failed.
1800
 */
1801
xmlNode *
1802
0
xmlNewPI(const xmlChar *name, const xmlChar *content) {
1803
0
    return(xmlNewDocPI(NULL, name, content));
1804
0
}
1805
1806
/**
1807
 * Create an element node.
1808
 *
1809
 * Use of this function is DISCOURAGED in favor of #xmlNewDocNode.
1810
 *
1811
 * @param ns  namespace (optional)
1812
 * @param name  the node name
1813
 * @returns a pointer to the new node object or NULL if arguments are
1814
 * invalid or a memory allocation failed.
1815
 */
1816
xmlNode *
1817
0
xmlNewNode(xmlNs *ns, const xmlChar *name) {
1818
0
    return(xmlNewDocNode(NULL, ns, name, NULL));
1819
0
}
1820
1821
/**
1822
 * Create an element node.
1823
 *
1824
 * Use of this function is DISCOURAGED in favor of #xmlNewDocNodeEatName.
1825
 *
1826
 * Like #xmlNewNode, but the `name` string will be used directly
1827
 * without making a copy. Takes ownership of `name` which will also
1828
 * be freed on error.
1829
 *
1830
 * @param ns  namespace (optional)
1831
 * @param name  the node name
1832
 * @returns a pointer to the new node object or NULL if arguments are
1833
 * invalid or a memory allocation failed.
1834
 */
1835
xmlNode *
1836
0
xmlNewNodeEatName(xmlNs *ns, xmlChar *name) {
1837
0
    return(xmlNewDocNodeEatName(NULL, ns, name, NULL));
1838
0
}
1839
1840
static xmlNodePtr
1841
xmlNewElem(xmlDocPtr doc, xmlNsPtr ns, const xmlChar *name,
1842
58.1k
           const xmlChar *content) {
1843
58.1k
    xmlNodePtr cur;
1844
1845
58.1k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1846
58.1k
    if (cur == NULL)
1847
0
  return(NULL);
1848
58.1k
    memset(cur, 0, sizeof(xmlNode));
1849
1850
58.1k
    cur->type = XML_ELEMENT_NODE;
1851
58.1k
    cur->doc = doc;
1852
58.1k
    cur->name = name;
1853
58.1k
    cur->ns = ns;
1854
1855
58.1k
    if (content != NULL) {
1856
        /*
1857
         * We shouldn't parse the content as attribute value here,
1858
         * but the API can't be changed.
1859
         */
1860
0
        if (xmlNodeParseAttValue(doc, (xmlAttr *) cur, content, SIZE_MAX,
1861
0
                                 NULL) < 0) {
1862
            /* Don't free name on error */
1863
0
            xmlFree(cur);
1864
0
            return(NULL);
1865
0
        }
1866
0
    }
1867
1868
58.1k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1869
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1870
1871
58.1k
    return(cur);
1872
58.1k
}
1873
1874
/**
1875
 * Create an element node.
1876
 *
1877
 * If provided, `content` is expected to be a valid XML attribute value
1878
 * possibly containing character and entity references. Syntax errors
1879
 * and references to undeclared entities are ignored silently.
1880
 * Only references are handled, nested elements, comments or PIs are
1881
 * not. See #xmlNewDocRawNode for an alternative.
1882
 *
1883
 * General notes on object creation:
1884
 *
1885
 * Each node and all its children are associated with the same
1886
 * document. The document should be provided when creating nodes to
1887
 * avoid a performance penalty when adding the node to a document
1888
 * tree. Note that a document only owns nodes reachable from the root
1889
 * node. Unlinked subtrees must be freed manually.
1890
 *
1891
 * @param doc  the target document
1892
 * @param ns  namespace (optional)
1893
 * @param name  the node name
1894
 * @param content  text content with XML references (optional)
1895
 * @returns a pointer to the new node object or NULL if arguments are
1896
 * invalid or a memory allocation failed.
1897
 */
1898
xmlNode *
1899
xmlNewDocNode(xmlDoc *doc, xmlNs *ns,
1900
57.5k
              const xmlChar *name, const xmlChar *content) {
1901
57.5k
    xmlNodePtr cur;
1902
57.5k
    xmlChar *copy;
1903
1904
57.5k
    if (name == NULL)
1905
0
        return(NULL);
1906
1907
57.5k
    if ((doc != NULL) && (doc->dict != NULL)) {
1908
0
        const xmlChar *dictName = xmlDictLookup(doc->dict, name, -1);
1909
1910
0
        if (dictName == NULL)
1911
0
            return(NULL);
1912
0
        return(xmlNewElem(doc, ns, dictName, content));
1913
0
    }
1914
1915
57.5k
    copy = xmlStrdup(name);
1916
57.5k
    if (copy == NULL)
1917
0
        return(NULL);
1918
1919
57.5k
    cur = xmlNewElem(doc, ns, copy, content);
1920
57.5k
    if (cur == NULL) {
1921
0
        xmlFree(copy);
1922
0
        return(NULL);
1923
0
    }
1924
1925
57.5k
    return(cur);
1926
57.5k
}
1927
1928
/**
1929
 * Create an element node.
1930
 *
1931
 * Like #xmlNewDocNode, but the `name` string will be used directly
1932
 * without making a copy. Takes ownership of `name` which will also
1933
 * be freed on error.
1934
 *
1935
 * @param doc  the target document
1936
 * @param ns  namespace (optional)
1937
 * @param name  the node name
1938
 * @param content  text content with XML references (optional)
1939
 * @returns a pointer to the new node object or NULL if arguments are
1940
 * invalid or a memory allocation failed.
1941
 */
1942
xmlNode *
1943
xmlNewDocNodeEatName(xmlDoc *doc, xmlNs *ns,
1944
576
                     xmlChar *name, const xmlChar *content) {
1945
576
    xmlNodePtr cur;
1946
1947
576
    if (name == NULL)
1948
0
        return(NULL);
1949
1950
576
    cur = xmlNewElem(doc, ns, name, content);
1951
576
    if (cur == NULL) {
1952
        /* if name doesn't come from the doc dictionary free it here */
1953
0
        if ((doc == NULL) ||
1954
0
            (doc->dict == NULL) ||
1955
0
            (!xmlDictOwns(doc->dict, name)))
1956
0
            xmlFree(name);
1957
0
        return(NULL);
1958
0
    }
1959
1960
576
    return(cur);
1961
576
}
1962
1963
/**
1964
 * Create an element node.
1965
 *
1966
 * If provided, `value` should be a raw, unescaped string.
1967
 *
1968
 * @param doc  the target document
1969
 * @param ns  namespace (optional)
1970
 * @param name  the node name
1971
 * @param content  raw text content (optional)
1972
 * @returns a pointer to the new node object or NULL if arguments are
1973
 * invalid or a memory allocation failed.
1974
 */
1975
xmlNode *
1976
xmlNewDocRawNode(xmlDoc *doc, xmlNs *ns,
1977
0
                 const xmlChar *name, const xmlChar *content) {
1978
0
    xmlNodePtr cur;
1979
1980
0
    cur = xmlNewDocNode(doc, ns, name, NULL);
1981
0
    if (cur != NULL) {
1982
0
        cur->doc = doc;
1983
0
  if (content != NULL) {
1984
0
            xmlNodePtr text;
1985
1986
0
      text = xmlNewDocText(doc, content);
1987
0
            if (text == NULL) {
1988
0
                xmlFreeNode(cur);
1989
0
                return(NULL);
1990
0
            }
1991
1992
0
            cur->children = text;
1993
0
            cur->last = text;
1994
0
            text->parent = cur;
1995
0
  }
1996
0
    }
1997
0
    return(cur);
1998
0
}
1999
2000
/**
2001
 * Create a document fragment node.
2002
 *
2003
 * @param doc  the target document (optional)
2004
 * @returns a pointer to the new node object or NULL if a memory
2005
 * allocation failed.
2006
 */
2007
xmlNode *
2008
0
xmlNewDocFragment(xmlDoc *doc) {
2009
0
    xmlNodePtr cur;
2010
2011
    /*
2012
     * Allocate a new DocumentFragment node and fill the fields.
2013
     */
2014
0
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2015
0
    if (cur == NULL)
2016
0
  return(NULL);
2017
0
    memset(cur, 0, sizeof(xmlNode));
2018
0
    cur->type = XML_DOCUMENT_FRAG_NODE;
2019
2020
0
    cur->doc = doc;
2021
2022
0
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2023
0
  xmlRegisterNodeDefaultValue(cur);
2024
0
    return(cur);
2025
0
}
2026
2027
/**
2028
 * Create a text node.
2029
 *
2030
 * Use of this function is DISCOURAGED in favor of #xmlNewDocText.
2031
 *
2032
 * @param content  raw text content (optional)
2033
 * @returns a pointer to the new node object or NULL if a memory
2034
 * allocation failed.
2035
 */
2036
xmlNode *
2037
0
xmlNewText(const xmlChar *content) {
2038
0
    xmlNodePtr cur;
2039
2040
    /*
2041
     * Allocate a new node and fill the fields.
2042
     */
2043
0
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2044
0
    if (cur == NULL)
2045
0
  return(NULL);
2046
0
    memset(cur, 0, sizeof(xmlNode));
2047
0
    cur->type = XML_TEXT_NODE;
2048
2049
0
    cur->name = xmlStringText;
2050
0
    if (content != NULL) {
2051
0
  cur->content = xmlStrdup(content);
2052
0
        if (cur->content == NULL)
2053
0
            goto error;
2054
0
    }
2055
2056
0
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2057
0
  xmlRegisterNodeDefaultValue(cur);
2058
0
    return(cur);
2059
2060
0
error:
2061
0
    xmlFreeNode(cur);
2062
0
    return(NULL);
2063
0
}
2064
2065
/**
2066
 * Create a new child element and append it to a parent element.
2067
 *
2068
 * If `ns` is NULL, the newly created element inherits the namespace
2069
 * of the parent.
2070
 *
2071
 * If `content` is provided, a text node will be added to the child
2072
 * element, see #xmlNewDocRawNode.
2073
 *
2074
 * @param parent  the parent node
2075
 * @param ns  a namespace (optional)
2076
 * @param name  the name of the child
2077
 * @param content  raw text content of the child (optional)
2078
 * @returns a pointer to the new node object or NULL if arguments
2079
 * are invalid or a memory allocation failed.
2080
 */
2081
xmlNode *
2082
xmlNewTextChild(xmlNode *parent, xmlNs *ns,
2083
0
            const xmlChar *name, const xmlChar *content) {
2084
0
    xmlNodePtr cur, prev;
2085
2086
0
    if ((parent == NULL) || (name == NULL))
2087
0
  return(NULL);
2088
2089
0
    switch (parent->type) {
2090
0
        case XML_DOCUMENT_NODE:
2091
0
        case XML_HTML_DOCUMENT_NODE:
2092
0
        case XML_DOCUMENT_FRAG_NODE:
2093
0
            break;
2094
2095
0
        case XML_ELEMENT_NODE:
2096
0
            if (ns == NULL)
2097
0
                ns = parent->ns;
2098
0
            break;
2099
2100
0
        default:
2101
0
            return(NULL);
2102
0
    }
2103
2104
0
    cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2105
0
    if (cur == NULL)
2106
0
        return(NULL);
2107
2108
    /*
2109
     * add the new element at the end of the children list.
2110
     */
2111
0
    cur->parent = parent;
2112
0
    if (parent->children == NULL) {
2113
0
        parent->children = cur;
2114
0
  parent->last = cur;
2115
0
    } else {
2116
0
        prev = parent->last;
2117
0
  prev->next = cur;
2118
0
  cur->prev = prev;
2119
0
  parent->last = cur;
2120
0
    }
2121
2122
0
    return(cur);
2123
0
}
2124
2125
/**
2126
 * Create an empty entity reference node. This function doesn't attempt
2127
 * to look up the entity in `doc`.
2128
 *
2129
 * `name` is consumed.
2130
 *
2131
 * @param doc  the target document (optional)
2132
 * @param name  the entity name
2133
 * @returns a pointer to the new node object or NULL if arguments are
2134
 * invalid or a memory allocation failed.
2135
 */
2136
static xmlNodePtr
2137
0
xmlNewEntityRef(xmlDocPtr doc, xmlChar *name) {
2138
0
    xmlNodePtr cur;
2139
2140
    /*
2141
     * Allocate a new node and fill the fields.
2142
     */
2143
0
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2144
0
    if (cur == NULL) {
2145
0
        xmlFree(name);
2146
0
  return(NULL);
2147
0
    }
2148
0
    memset(cur, 0, sizeof(xmlNode));
2149
0
    cur->type = XML_ENTITY_REF_NODE;
2150
0
    cur->doc = doc;
2151
0
    cur->name = name;
2152
2153
0
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2154
0
  xmlRegisterNodeDefaultValue(cur);
2155
2156
0
    return(cur);
2157
0
}
2158
2159
/**
2160
 * Create an empty entity reference node.
2161
 *
2162
 * This function is MISNAMED. It doesn't create a character reference
2163
 * but an entity reference.
2164
 *
2165
 * This function doesn't attempt to look up the entity in `doc`.
2166
 *
2167
 * Entity names like `&entity;` are handled as well.
2168
 *
2169
 * @param doc  the target document (optional)
2170
 * @param name  the entity name
2171
 * @returns a pointer to the new node object or NULL if arguments are
2172
 * invalid or a memory allocation failed.
2173
 */
2174
xmlNode *
2175
0
xmlNewCharRef(xmlDoc *doc, const xmlChar *name) {
2176
0
    xmlChar *copy;
2177
2178
0
    if (name == NULL)
2179
0
        return(NULL);
2180
2181
0
    if (name[0] == '&') {
2182
0
        int len;
2183
0
        name++;
2184
0
  len = xmlStrlen(name);
2185
0
  if (name[len - 1] == ';')
2186
0
      copy = xmlStrndup(name, len - 1);
2187
0
  else
2188
0
      copy = xmlStrndup(name, len);
2189
0
    } else
2190
0
  copy = xmlStrdup(name);
2191
0
    if (copy == NULL)
2192
0
        return(NULL);
2193
2194
0
    return(xmlNewEntityRef(doc, copy));
2195
0
}
2196
2197
/**
2198
 * Create a new entity reference node, linking the result with the
2199
 * entity in `doc` if found.
2200
 *
2201
 * Entity names like `&entity;` are handled as well.
2202
 *
2203
 * @param doc  the target document (optional)
2204
 * @param name  the entity name
2205
 * @returns a pointer to the new node object or NULL if arguments are
2206
 * invalid or a memory allocation failed.
2207
 */
2208
xmlNode *
2209
0
xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
2210
0
    xmlNodePtr cur;
2211
0
    xmlEntityPtr ent;
2212
2213
0
    if (name == NULL)
2214
0
        return(NULL);
2215
2216
    /*
2217
     * Allocate a new node and fill the fields.
2218
     */
2219
0
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2220
0
    if (cur == NULL)
2221
0
  return(NULL);
2222
0
    memset(cur, 0, sizeof(xmlNode));
2223
0
    cur->type = XML_ENTITY_REF_NODE;
2224
2225
0
    cur->doc = (xmlDoc *)doc;
2226
0
    if (name[0] == '&') {
2227
0
        int len;
2228
0
        name++;
2229
0
  len = xmlStrlen(name);
2230
0
  if (name[len - 1] == ';')
2231
0
      cur->name = xmlStrndup(name, len - 1);
2232
0
  else
2233
0
      cur->name = xmlStrndup(name, len);
2234
0
    } else
2235
0
  cur->name = xmlStrdup(name);
2236
0
    if (cur->name == NULL)
2237
0
        goto error;
2238
2239
0
    ent = xmlGetDocEntity(doc, cur->name);
2240
0
    if (ent != NULL) {
2241
0
  cur->content = ent->content;
2242
  /*
2243
   * The parent pointer in entity is a DTD pointer and thus is NOT
2244
   * updated.  Not sure if this is 100% correct.
2245
   *  -George
2246
   */
2247
0
  cur->children = (xmlNodePtr) ent;
2248
0
  cur->last = (xmlNodePtr) ent;
2249
0
    }
2250
2251
0
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2252
0
  xmlRegisterNodeDefaultValue(cur);
2253
0
    return(cur);
2254
2255
0
error:
2256
0
    xmlFreeNode(cur);
2257
0
    return(NULL);
2258
0
}
2259
2260
/**
2261
 * Create a new text node.
2262
 *
2263
 * @param doc  the target document
2264
 * @param content  raw text content (optional)
2265
 * @returns a pointer to the new node object or NULL if a memory
2266
 * allocation failed.
2267
 */
2268
xmlNode *
2269
0
xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
2270
0
    xmlNodePtr cur;
2271
2272
0
    cur = xmlNewText(content);
2273
0
    if (cur != NULL) cur->doc = (xmlDoc *)doc;
2274
0
    return(cur);
2275
0
}
2276
2277
/**
2278
 * Create a new text node.
2279
 *
2280
 * Use of this function is DISCOURAGED in favor of #xmlNewDocTextLen.
2281
 *
2282
 * @param content  raw text content (optional)
2283
 * @param len  size of text content
2284
 * @returns a pointer to the new node object or NULL if a memory
2285
 * allocation failed.
2286
 */
2287
xmlNode *
2288
0
xmlNewTextLen(const xmlChar *content, int len) {
2289
0
    xmlNodePtr cur;
2290
2291
    /*
2292
     * Allocate a new node and fill the fields.
2293
     */
2294
0
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2295
0
    if (cur == NULL)
2296
0
  return(NULL);
2297
0
    memset(cur, 0, sizeof(xmlNode));
2298
0
    cur->type = XML_TEXT_NODE;
2299
2300
0
    cur->name = xmlStringText;
2301
0
    if (content != NULL) {
2302
0
  cur->content = xmlStrndup(content, len);
2303
0
        if (cur->content == NULL) {
2304
0
            xmlFreeNode(cur);
2305
0
            return(NULL);
2306
0
        }
2307
0
    }
2308
2309
0
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2310
0
  xmlRegisterNodeDefaultValue(cur);
2311
0
    return(cur);
2312
0
}
2313
2314
/**
2315
 * Create a new text node.
2316
 *
2317
 * @param doc  the target document
2318
 * @param content  raw text content (optional)
2319
 * @param len  size of text content
2320
 * @returns a pointer to the new node object or NULL if a memory
2321
 * allocation failed.
2322
 */
2323
xmlNode *
2324
0
xmlNewDocTextLen(xmlDoc *doc, const xmlChar *content, int len) {
2325
0
    xmlNodePtr cur;
2326
2327
0
    cur = xmlNewTextLen(content, len);
2328
0
    if (cur != NULL) cur->doc = doc;
2329
0
    return(cur);
2330
0
}
2331
2332
/**
2333
 * Use of this function is DISCOURAGED in favor of #xmlNewDocComment.
2334
 *
2335
 * Create a comment node.
2336
 *
2337
 * @param content  the comment content (optional)
2338
 * @returns a pointer to the new node object or NULL if a memory
2339
 * allocation failed.
2340
 */
2341
xmlNode *
2342
1.93k
xmlNewComment(const xmlChar *content) {
2343
1.93k
    xmlNodePtr cur;
2344
2345
    /*
2346
     * Allocate a new node and fill the fields.
2347
     */
2348
1.93k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2349
1.93k
    if (cur == NULL)
2350
0
  return(NULL);
2351
1.93k
    memset(cur, 0, sizeof(xmlNode));
2352
1.93k
    cur->type = XML_COMMENT_NODE;
2353
2354
1.93k
    cur->name = xmlStringComment;
2355
1.93k
    if (content != NULL) {
2356
1.93k
  cur->content = xmlStrdup(content);
2357
1.93k
        if (cur->content == NULL)
2358
0
            goto error;
2359
1.93k
    }
2360
2361
1.93k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2362
0
  xmlRegisterNodeDefaultValue(cur);
2363
1.93k
    return(cur);
2364
2365
0
error:
2366
0
    xmlFreeNode(cur);
2367
0
    return(NULL);
2368
1.93k
}
2369
2370
/**
2371
 * Create a CDATA section node.
2372
 *
2373
 * @param doc  the target document (optional)
2374
 * @param content  raw text content (optional)
2375
 * @param len  size of text content
2376
 * @returns a pointer to the new node object or NULL if a memory
2377
 * allocation failed.
2378
 */
2379
xmlNode *
2380
1.93k
xmlNewCDataBlock(xmlDoc *doc, const xmlChar *content, int len) {
2381
1.93k
    xmlNodePtr cur;
2382
2383
    /*
2384
     * Allocate a new node and fill the fields.
2385
     */
2386
1.93k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2387
1.93k
    if (cur == NULL)
2388
0
  return(NULL);
2389
1.93k
    memset(cur, 0, sizeof(xmlNode));
2390
1.93k
    cur->type = XML_CDATA_SECTION_NODE;
2391
1.93k
    cur->doc = doc;
2392
2393
1.93k
    if (content != NULL) {
2394
1.93k
  cur->content = xmlStrndup(content, len);
2395
1.93k
        if (cur->content == NULL) {
2396
0
            xmlFree(cur);
2397
0
            return(NULL);
2398
0
        }
2399
1.93k
    }
2400
2401
1.93k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2402
0
  xmlRegisterNodeDefaultValue(cur);
2403
1.93k
    return(cur);
2404
1.93k
}
2405
2406
/**
2407
 * Create a comment node.
2408
 *
2409
 * @param doc  the document
2410
 * @param content  the comment content
2411
 * @returns a pointer to the new node object or NULL if a memory
2412
 * allocation failed.
2413
 */
2414
xmlNode *
2415
1.93k
xmlNewDocComment(xmlDoc *doc, const xmlChar *content) {
2416
1.93k
    xmlNodePtr cur;
2417
2418
1.93k
    cur = xmlNewComment(content);
2419
1.93k
    if (cur != NULL) cur->doc = doc;
2420
1.93k
    return(cur);
2421
1.93k
}
2422
2423
static void
2424
0
xmlRemoveEntity(xmlEntityPtr ent) {
2425
0
    xmlDocPtr doc = ent->doc;
2426
0
    xmlDtdPtr intSubset, extSubset;
2427
2428
0
    if (doc == NULL)
2429
0
        return;
2430
0
    intSubset = doc->intSubset;
2431
0
    extSubset = doc->extSubset;
2432
2433
0
    if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
2434
0
        (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) ||
2435
0
        (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) {
2436
0
        if (intSubset != NULL) {
2437
0
            if (xmlHashLookup(intSubset->entities, ent->name) == ent)
2438
0
                xmlHashRemoveEntry(intSubset->entities, ent->name, NULL);
2439
0
        }
2440
0
        if (extSubset != NULL) {
2441
0
            if (xmlHashLookup(extSubset->entities, ent->name) == ent)
2442
0
                xmlHashRemoveEntry(extSubset->entities, ent->name, NULL);
2443
0
        }
2444
0
    } else if ((ent->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
2445
0
               (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
2446
0
        if (intSubset != NULL) {
2447
0
            if (xmlHashLookup(intSubset->pentities, ent->name) == ent)
2448
0
                xmlHashRemoveEntry(intSubset->pentities, ent->name, NULL);
2449
0
        }
2450
0
        if (extSubset != NULL) {
2451
0
            if (xmlHashLookup(extSubset->pentities, ent->name) == ent)
2452
0
                xmlHashRemoveEntry(extSubset->pentities, ent->name, NULL);
2453
0
        }
2454
0
    }
2455
0
}
2456
2457
static int
2458
0
xmlNodeSetDoc(xmlNodePtr node, xmlDocPtr doc) {
2459
0
    xmlDocPtr oldDoc;
2460
0
    xmlDictPtr oldDict, newDict;
2461
0
    int ret = 0;
2462
2463
    /*
2464
     * Remove name and content from old dictionary
2465
     */
2466
2467
0
    oldDoc = node->doc;
2468
0
    oldDict = oldDoc ? oldDoc->dict : NULL;
2469
0
    newDict = doc ? doc->dict : NULL;
2470
2471
0
    if ((oldDict != NULL) && (oldDict != newDict)) {
2472
0
        if ((node->name != NULL) &&
2473
0
            ((node->type == XML_ELEMENT_NODE) ||
2474
0
             (node->type == XML_ATTRIBUTE_NODE) ||
2475
0
             (node->type == XML_PI_NODE) ||
2476
0
             (node->type == XML_ENTITY_REF_NODE)) &&
2477
0
            (xmlDictOwns(oldDict, node->name))) {
2478
0
            if (newDict)
2479
0
                node->name = xmlDictLookup(newDict, node->name, -1);
2480
0
            else
2481
0
                node->name = xmlStrdup(node->name);
2482
0
            if (node->name == NULL)
2483
0
                ret = -1;
2484
0
        }
2485
2486
0
        if ((node->content != NULL) &&
2487
0
            ((node->type == XML_TEXT_NODE) ||
2488
0
             (node->type == XML_CDATA_SECTION_NODE)) &&
2489
0
            (xmlDictOwns(oldDict, node->content))) {
2490
0
            node->content = xmlStrdup(node->content);
2491
0
            if (node->content == NULL)
2492
0
                ret = -1;
2493
0
        }
2494
0
    }
2495
2496
0
    switch (node->type) {
2497
0
        case XML_ATTRIBUTE_NODE: {
2498
0
            xmlAttrPtr attr = (xmlAttrPtr) node;
2499
2500
            /*
2501
             * Handle IDs
2502
             *
2503
             * TODO: ID attributes should also be added to the new
2504
             * document, but it's not clear how to handle clashes.
2505
             */
2506
0
            if (attr->id != NULL)
2507
0
                xmlRemoveID(oldDoc, attr);
2508
2509
0
            break;
2510
0
        }
2511
2512
0
        case XML_ENTITY_REF_NODE:
2513
            /*
2514
             * Handle entity references
2515
             */
2516
0
            node->children = NULL;
2517
0
            node->last = NULL;
2518
0
            node->content = NULL;
2519
2520
0
            if ((doc != NULL) &&
2521
0
                ((doc->intSubset != NULL) || (doc->extSubset != NULL))) {
2522
0
                xmlEntityPtr ent;
2523
2524
                /*
2525
                * Assign new entity node if available
2526
                */
2527
0
                ent = xmlGetDocEntity(doc, node->name);
2528
0
                if (ent != NULL) {
2529
0
                    node->children = (xmlNodePtr) ent;
2530
0
                    node->last = (xmlNodePtr) ent;
2531
0
                    node->content = ent->content;
2532
0
                }
2533
0
            }
2534
2535
0
            break;
2536
2537
0
        case XML_DTD_NODE:
2538
0
            if (oldDoc != NULL) {
2539
0
                if (oldDoc->intSubset == (xmlDtdPtr) node)
2540
0
                    oldDoc->intSubset = NULL;
2541
0
                if (oldDoc->extSubset == (xmlDtdPtr) node)
2542
0
                    oldDoc->extSubset = NULL;
2543
0
            }
2544
2545
0
            break;
2546
2547
0
        case XML_ENTITY_DECL:
2548
0
            xmlRemoveEntity((xmlEntityPtr) node);
2549
0
            break;
2550
2551
        /*
2552
         * TODO:
2553
         * - Remove element decls from doc->elements
2554
         * - Remove attribtue decls form doc->attributes
2555
         */
2556
2557
0
        default:
2558
0
            break;
2559
0
    }
2560
2561
    /*
2562
     * Set new document
2563
     */
2564
0
    node->doc = doc;
2565
2566
0
    return(ret);
2567
0
}
2568
2569
/**
2570
 * Associate all nodes in a tree with a new document.
2571
 *
2572
 * This is an internal function which shouldn't be used. It is
2573
 * invoked by functions like #xmlAddChild, #xmlAddSibling or
2574
 * #xmlReplaceNode. `tree` must be the root node of an unlinked
2575
 * subtree.
2576
 *
2577
 * Also copy strings from the old document's dictionary and
2578
 * remove ID attributes from the old ID table.
2579
 *
2580
 * @param tree  root of a subtree
2581
 * @param doc  new document
2582
 * @returns 0 on success. If a memory allocation fails, returns -1.
2583
 * The whole tree will be updated on failure but some strings
2584
 * may be lost.
2585
 */
2586
int
2587
0
xmlSetTreeDoc(xmlNode *tree, xmlDoc *doc) {
2588
0
    int ret = 0;
2589
2590
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2591
0
  return(0);
2592
0
    if (tree->doc == doc)
2593
0
        return(0);
2594
2595
0
    if (tree->type == XML_ELEMENT_NODE) {
2596
0
        xmlAttrPtr prop = tree->properties;
2597
2598
0
        while (prop != NULL) {
2599
0
            if (prop->children != NULL) {
2600
0
                if (xmlSetListDoc(prop->children, doc) < 0)
2601
0
                    ret = -1;
2602
0
            }
2603
2604
0
            if (xmlNodeSetDoc((xmlNodePtr) prop, doc) < 0)
2605
0
                ret = -1;
2606
2607
0
            prop = prop->next;
2608
0
        }
2609
0
    }
2610
2611
0
    if ((tree->children != NULL) &&
2612
0
        (tree->type != XML_ENTITY_REF_NODE)) {
2613
0
        if (xmlSetListDoc(tree->children, doc) < 0)
2614
0
            ret = -1;
2615
0
    }
2616
2617
0
    if (xmlNodeSetDoc(tree, doc) < 0)
2618
0
        ret = -1;
2619
2620
0
    return(ret);
2621
0
}
2622
2623
/**
2624
 * Associate all subtrees in `list` with a new document.
2625
 *
2626
 * Internal function, see #xmlSetTreeDoc.
2627
 *
2628
 * @param list  a node list
2629
 * @param doc  new document
2630
 * @returns 0 on success. If a memory allocation fails, returns -1.
2631
 * All subtrees will be updated on failure but some strings
2632
 * may be lost.
2633
 */
2634
int
2635
0
xmlSetListDoc(xmlNode *list, xmlDoc *doc) {
2636
0
    xmlNodePtr cur;
2637
0
    int ret = 0;
2638
2639
0
    if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2640
0
  return(0);
2641
2642
0
    cur = list;
2643
0
    while (cur != NULL) {
2644
0
  if (cur->doc != doc) {
2645
0
      if (xmlSetTreeDoc(cur, doc) < 0)
2646
0
                ret = -1;
2647
0
        }
2648
0
  cur = cur->next;
2649
0
    }
2650
2651
0
    return(ret);
2652
0
}
2653
2654
/**
2655
 * Create a new child element and append it to a parent element.
2656
 *
2657
 * If `ns` is NULL, the newly created element inherits the namespace
2658
 * of the parent.
2659
 *
2660
 * If provided, `content` is expected to be a valid XML attribute
2661
 * value possibly containing character and entity references. Text
2662
 * and entity reference node will be added to the child element,
2663
 * see #xmlNewDocNode.
2664
 *
2665
 * @param parent  the parent node
2666
 * @param ns  a namespace (optional)
2667
 * @param name  the name of the child
2668
 * @param content  text content with XML references (optional)
2669
 * @returns a pointer to the new node object or NULL if arguments
2670
 * are invalid or a memory allocation failed.
2671
 */
2672
xmlNode *
2673
xmlNewChild(xmlNode *parent, xmlNs *ns,
2674
0
            const xmlChar *name, const xmlChar *content) {
2675
0
    xmlNodePtr cur, prev;
2676
2677
0
    if ((parent == NULL) || (name == NULL))
2678
0
  return(NULL);
2679
2680
0
    switch (parent->type) {
2681
0
        case XML_DOCUMENT_NODE:
2682
0
        case XML_HTML_DOCUMENT_NODE:
2683
0
        case XML_DOCUMENT_FRAG_NODE:
2684
0
            break;
2685
2686
0
        case XML_ELEMENT_NODE:
2687
0
            if (ns == NULL)
2688
0
                ns = parent->ns;
2689
0
            break;
2690
2691
0
        default:
2692
0
            return(NULL);
2693
0
    }
2694
2695
0
    cur = xmlNewDocNode(parent->doc, ns, name, content);
2696
0
    if (cur == NULL)
2697
0
        return(NULL);
2698
2699
    /*
2700
     * add the new element at the end of the children list.
2701
     */
2702
0
    cur->parent = parent;
2703
0
    if (parent->children == NULL) {
2704
0
        parent->children = cur;
2705
0
  parent->last = cur;
2706
0
    } else {
2707
0
        prev = parent->last;
2708
0
  prev->next = cur;
2709
0
  cur->prev = prev;
2710
0
  parent->last = cur;
2711
0
    }
2712
2713
0
    return(cur);
2714
0
}
2715
2716
static void
2717
0
xmlTextSetContent(xmlNodePtr text, xmlChar *content) {
2718
0
    if ((text->content != NULL) &&
2719
0
        (text->content != (xmlChar *) &text->properties)) {
2720
0
        xmlDocPtr doc = text->doc;
2721
2722
0
        if ((doc == NULL) ||
2723
0
            (doc->dict == NULL) ||
2724
0
            (!xmlDictOwns(doc->dict, text->content)))
2725
0
            xmlFree(text->content);
2726
0
    }
2727
2728
0
    text->content = content;
2729
0
    text->properties = NULL;
2730
0
}
2731
2732
static int
2733
0
xmlTextAddContent(xmlNodePtr text, const xmlChar *content, int len) {
2734
0
    xmlChar *merged;
2735
2736
0
    if (content == NULL)
2737
0
        return(0);
2738
2739
0
    merged = xmlStrncatNew(text->content, content, len);
2740
0
    if (merged == NULL)
2741
0
        return(-1);
2742
2743
0
    xmlTextSetContent(text, merged);
2744
0
    return(0);
2745
0
}
2746
2747
static xmlNodePtr
2748
xmlInsertProp(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent,
2749
0
              xmlNodePtr prev, xmlNodePtr next) {
2750
0
    xmlAttrPtr attr;
2751
2752
0
    if (((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)) ||
2753
0
        ((next != NULL) && (next->type != XML_ATTRIBUTE_NODE)))
2754
0
        return(NULL);
2755
2756
    /* check if an attribute with the same name exists */
2757
0
    attr = xmlGetPropNodeInternal(parent, cur->name,
2758
0
                                  cur->ns ? cur->ns->href : NULL, 0);
2759
2760
0
    xmlUnlinkNodeInternal(cur);
2761
2762
0
    if (cur->doc != doc) {
2763
0
        if (xmlSetTreeDoc(cur, doc) < 0)
2764
0
            return(NULL);
2765
0
    }
2766
2767
0
    cur->parent = parent;
2768
0
    cur->prev = prev;
2769
0
    cur->next = next;
2770
2771
0
    if (prev == NULL) {
2772
0
        if (parent != NULL)
2773
0
            parent->properties = (xmlAttrPtr) cur;
2774
0
    } else {
2775
0
        prev->next = cur;
2776
0
    }
2777
0
    if (next != NULL) {
2778
0
        next->prev = cur;
2779
0
    }
2780
2781
0
    if ((attr != NULL) && (attr != (xmlAttrPtr) cur)) {
2782
        /* different instance, destroy it (attributes must be unique) */
2783
0
        xmlRemoveProp((xmlAttrPtr) attr);
2784
0
    }
2785
2786
0
    return cur;
2787
0
}
2788
2789
static xmlNodePtr
2790
xmlInsertNode(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent,
2791
0
              xmlNodePtr prev, xmlNodePtr next, int coalesce) {
2792
0
    xmlNodePtr oldParent;
2793
2794
0
    if (cur->type == XML_ATTRIBUTE_NODE)
2795
0
  return xmlInsertProp(doc, cur, parent, prev, next);
2796
2797
    /*
2798
     * Coalesce text nodes
2799
     */
2800
0
    if ((coalesce) && (cur->type == XML_TEXT_NODE)) {
2801
0
  if ((prev != NULL) && (prev->type == XML_TEXT_NODE) &&
2802
0
            (prev->name == cur->name)) {
2803
0
            if (xmlTextAddContent(prev, cur->content, -1) < 0)
2804
0
                return(NULL);
2805
0
            xmlUnlinkNodeInternal(cur);
2806
0
      xmlFreeNode(cur);
2807
0
      return(prev);
2808
0
  }
2809
2810
0
  if ((next != NULL) && (next->type == XML_TEXT_NODE) &&
2811
0
            (next->name == cur->name)) {
2812
0
            if (cur->content != NULL) {
2813
0
          xmlChar *merged;
2814
2815
0
                merged = xmlStrncatNew(cur->content, next->content, -1);
2816
0
                if (merged == NULL)
2817
0
                    return(NULL);
2818
0
                xmlTextSetContent(next, merged);
2819
0
            }
2820
2821
0
            xmlUnlinkNodeInternal(cur);
2822
0
      xmlFreeNode(cur);
2823
0
      return(next);
2824
0
  }
2825
0
    }
2826
2827
    /* Unlink */
2828
0
    oldParent = cur->parent;
2829
0
    if (oldParent != NULL) {
2830
0
        if (oldParent->children == cur)
2831
0
            oldParent->children = cur->next;
2832
0
        if (oldParent->last == cur)
2833
0
            oldParent->last = cur->prev;
2834
0
    }
2835
0
    if (cur->next != NULL)
2836
0
        cur->next->prev = cur->prev;
2837
0
    if (cur->prev != NULL)
2838
0
        cur->prev->next = cur->next;
2839
2840
0
    if (cur->doc != doc) {
2841
0
  if (xmlSetTreeDoc(cur, doc) < 0) {
2842
            /*
2843
             * We shouldn't make any modifications to the inserted
2844
             * tree if a memory allocation fails, but that's hard to
2845
             * implement. The tree has been moved to the target
2846
             * document now but some contents are corrupted.
2847
             * Unlinking is the best we can do.
2848
             */
2849
0
            cur->parent = NULL;
2850
0
            cur->prev = NULL;
2851
0
            cur->next = NULL;
2852
0
            return(NULL);
2853
0
        }
2854
0
    }
2855
2856
0
    cur->parent = parent;
2857
0
    cur->prev = prev;
2858
0
    cur->next = next;
2859
2860
0
    if (prev == NULL) {
2861
0
        if (parent != NULL)
2862
0
            parent->children = cur;
2863
0
    } else {
2864
0
        prev->next = cur;
2865
0
    }
2866
0
    if (next == NULL) {
2867
0
        if (parent != NULL)
2868
0
            parent->last = cur;
2869
0
    } else {
2870
0
        next->prev = cur;
2871
0
    }
2872
2873
0
    return(cur);
2874
0
}
2875
2876
/**
2877
 * Unlinks `cur` and inserts it as next sibling after `prev`.
2878
 *
2879
 * Unlike #xmlAddChild this function does not merge text nodes.
2880
 *
2881
 * If `cur` is an attribute node, it is inserted after attribute
2882
 * `prev`. If the attribute list contains an attribute with a name
2883
 * matching `cur`, the old attribute is destroyed.
2884
 *
2885
 * See the notes in #xmlAddChild.
2886
 *
2887
 * @param prev  the target node
2888
 * @param cur  the new node
2889
 * @returns `cur` or a sibling if `cur` was merged. Returns NULL
2890
 * if arguments are invalid or a memory allocation failed.
2891
 */
2892
xmlNode *
2893
0
xmlAddNextSibling(xmlNode *prev, xmlNode *cur) {
2894
0
    if ((prev == NULL) || (prev->type == XML_NAMESPACE_DECL) ||
2895
0
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
2896
0
        (cur == prev))
2897
0
  return(NULL);
2898
2899
0
    if (cur == prev->next)
2900
0
        return(cur);
2901
2902
0
    return(xmlInsertNode(prev->doc, cur, prev->parent, prev, prev->next, 0));
2903
0
}
2904
2905
/**
2906
 * Unlinks `cur` and inserts it as previous sibling before `next`.
2907
 *
2908
 * Unlike #xmlAddChild this function does not merge text nodes.
2909
 *
2910
 * If `cur` is an attribute node, it is inserted before attribute
2911
 * `next`. If the attribute list contains an attribute with a name
2912
 * matching `cur`, the old attribute is destroyed.
2913
 *
2914
 * See the notes in #xmlAddChild.
2915
 *
2916
 * @param next  the target node
2917
 * @param cur  the new node
2918
 * @returns `cur` or a sibling if `cur` was merged. Returns NULL
2919
 * if arguments are invalid or a memory allocation failed.
2920
 */
2921
xmlNode *
2922
0
xmlAddPrevSibling(xmlNode *next, xmlNode *cur) {
2923
0
    if ((next == NULL) || (next->type == XML_NAMESPACE_DECL) ||
2924
0
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
2925
0
        (cur == next))
2926
0
  return(NULL);
2927
2928
0
    if (cur == next->prev)
2929
0
        return(cur);
2930
2931
0
    return(xmlInsertNode(next->doc, cur, next->parent, next->prev, next, 0));
2932
0
}
2933
2934
/**
2935
 * Unlinks `cur` and inserts it as last sibling of `node`.
2936
 *
2937
 * If `cur` is a text node, it may be merged with an adjacent text
2938
 * node and freed. In this case the text node containing the merged
2939
 * content is returned.
2940
 *
2941
 * If `cur` is an attribute node, it is appended to the attribute
2942
 * list containing `node`. If the attribute list contains an attribute
2943
 * with a name matching `cur`, the old attribute is destroyed.
2944
 *
2945
 * See the notes in #xmlAddChild.
2946
 *
2947
 * @param node  the target node
2948
 * @param cur  the new node
2949
 * @returns `cur` or a sibling if `cur` was merged. Returns NULL
2950
 * if arguments are invalid or a memory allocation failed.
2951
 */
2952
xmlNode *
2953
0
xmlAddSibling(xmlNode *node, xmlNode *cur) {
2954
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2955
0
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
2956
0
        (cur == node))
2957
0
  return(NULL);
2958
2959
    /*
2960
     * Constant time is we can rely on the ->parent->last to find
2961
     * the last sibling.
2962
     */
2963
0
    if ((node->type != XML_ATTRIBUTE_NODE) && (node->parent != NULL)) {
2964
0
        if (node->parent->last != NULL)
2965
0
      node = node->parent->last;
2966
0
    } else {
2967
0
  while (node->next != NULL)
2968
0
            node = node->next;
2969
0
    }
2970
2971
0
    if (cur == node)
2972
0
        return(cur);
2973
2974
0
    return(xmlInsertNode(node->doc, cur, node->parent, node, NULL, 1));
2975
0
}
2976
2977
/**
2978
 * Append a node list to another node.
2979
 *
2980
 * See #xmlAddChild.
2981
 *
2982
 * @param parent  the parent node
2983
 * @param cur  the first node in the list
2984
 * @returns the last child or NULL in case of error.
2985
 */
2986
xmlNode *
2987
0
xmlAddChildList(xmlNode *parent, xmlNode *cur) {
2988
0
    xmlNodePtr iter;
2989
0
    xmlNodePtr prev;
2990
0
    int oom;
2991
2992
0
    if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
2993
0
  return(NULL);
2994
0
    }
2995
2996
0
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
2997
0
  return(NULL);
2998
0
    }
2999
3000
0
    oom = 0;
3001
0
    for (iter = cur; iter != NULL; iter = iter->next) {
3002
0
  if (iter->doc != parent->doc) {
3003
0
      if (xmlSetTreeDoc(iter, parent->doc) < 0)
3004
0
                oom = 1;
3005
0
  }
3006
0
    }
3007
0
    if (oom)
3008
0
        return(NULL);
3009
3010
    /*
3011
     * add the first element at the end of the children list.
3012
     */
3013
3014
0
    if (parent->children == NULL) {
3015
0
        parent->children = cur;
3016
0
    } else {
3017
0
        prev = parent->last;
3018
3019
  /*
3020
   * If cur and parent->last both are TEXT nodes, then merge them.
3021
   */
3022
0
  if ((cur->type == XML_TEXT_NODE) &&
3023
0
      (prev->type == XML_TEXT_NODE) &&
3024
0
      (cur->name == prev->name)) {
3025
0
            xmlNodePtr next;
3026
3027
0
            if (xmlTextAddContent(prev, cur->content, -1) < 0)
3028
0
                return(NULL);
3029
0
            next = cur->next;
3030
0
      xmlFreeNode(cur);
3031
      /*
3032
       * if it's the only child, nothing more to be done.
3033
       */
3034
0
      if (next == NULL)
3035
0
    return(prev);
3036
0
      cur = next;
3037
0
  }
3038
3039
0
  prev->next = cur;
3040
0
  cur->prev = prev;
3041
0
    }
3042
0
    while (cur->next != NULL) {
3043
0
  cur->parent = parent;
3044
0
        cur = cur->next;
3045
0
    }
3046
0
    cur->parent = parent;
3047
0
    parent->last = cur;
3048
3049
0
    return(cur);
3050
0
}
3051
3052
/**
3053
 * Unlink `cur` and append it to the children of `parent`.
3054
 *
3055
 * If `cur` is a text node, it may be merged with an adjacent text
3056
 * node and freed. In this case the text node containing the merged
3057
 * content is returned.
3058
 *
3059
 * If `cur` is an attribute node, it is appended to the attributes of
3060
 * `parent`. If the attribute list contains an attribute with a name
3061
 * matching `cur`, the old attribute is destroyed.
3062
 *
3063
 * Before version 2.13, this function didn't unlink `cur` before
3064
 * moving it. Callers must unlink the node manually if it has
3065
 * siblings.
3066
 *
3067
 * General notes:
3068
 *
3069
 * Move operations like #xmlAddChild can cause element or attribute
3070
 * nodes to reference namespaces that aren't declared in one of
3071
 * their ancestors. This can lead to use-after-free errors if the
3072
 * elements containing the declarations are freed later, especially
3073
 * when moving nodes from one document to another. You should
3074
 * consider calling #xmlReconciliateNs after a move operation to
3075
 * normalize namespaces. Another option is to call
3076
 * #xmlDOMWrapAdoptNode with the target parent before moving a node.
3077
 *
3078
 * For the most part, move operations don't check whether the
3079
 * resulting tree structure is valid. Users must make sure that
3080
 * parent nodes only receive children of valid types. Inserted
3081
 * child nodes must never be an ancestor of the parent node to
3082
 * avoid cycles in the tree structure. In general, only
3083
 * document, document fragments, elements and attributes
3084
 * should be used as parent nodes.
3085
 *
3086
 * When moving a node between documents and a memory allocation
3087
 * fails, the node's content will be corrupted and it will be
3088
 * unlinked. In this case, the node must be freed manually.
3089
 *
3090
 * Moving DTDs between documents isn't supported.
3091
 *
3092
 * @param parent  the parent node
3093
 * @param cur  the child node
3094
 * @returns `cur` or a sibling if `cur` was merged. Returns NULL
3095
 * if arguments are invalid or a memory allocation failed.
3096
 */
3097
xmlNode *
3098
0
xmlAddChild(xmlNode *parent, xmlNode *cur) {
3099
0
    xmlNodePtr prev;
3100
3101
0
    if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL) ||
3102
0
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
3103
0
        (parent == cur))
3104
0
        return(NULL);
3105
3106
    /*
3107
     * If parent is a text node, call xmlTextAddContent. This
3108
     * undocumented quirk should probably be removed.
3109
     */
3110
0
    if (parent->type == XML_TEXT_NODE) {
3111
0
        if (xmlTextAddContent(parent, cur->content, -1) < 0)
3112
0
            return(NULL);
3113
0
        xmlUnlinkNodeInternal(cur);
3114
0
        xmlFreeNode(cur);
3115
0
        return(parent);
3116
0
    }
3117
3118
0
    if (cur->type == XML_ATTRIBUTE_NODE) {
3119
0
        prev = (xmlNodePtr) parent->properties;
3120
0
        if (prev != NULL) {
3121
0
            while (prev->next != NULL)
3122
0
                prev = prev->next;
3123
0
        }
3124
0
    } else {
3125
0
        prev = parent->last;
3126
0
    }
3127
3128
0
    if (cur == prev)
3129
0
        return(cur);
3130
3131
0
    return(xmlInsertNode(parent->doc, cur, parent, prev, NULL, 1));
3132
0
}
3133
3134
/**
3135
 * Find the last child of a node.
3136
 *
3137
 * @param parent  the parent node
3138
 * @returns the last child or NULL if parent has no children.
3139
 */
3140
xmlNode *
3141
0
xmlGetLastChild(const xmlNode *parent) {
3142
0
    if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3143
0
  return(NULL);
3144
0
    }
3145
0
    return(parent->last);
3146
0
}
3147
3148
/*
3149
 * 5 interfaces from DOM ElementTraversal
3150
 */
3151
3152
/**
3153
 * Count the number of child nodes which are elements.
3154
 *
3155
 * Note that entity references are not expanded.
3156
 *
3157
 * @param parent  the parent node
3158
 * @returns the number of element children or 0 if arguments are
3159
 * invalid.
3160
 */
3161
unsigned long
3162
0
xmlChildElementCount(xmlNode *parent) {
3163
0
    unsigned long ret = 0;
3164
0
    xmlNodePtr cur = NULL;
3165
3166
0
    if (parent == NULL)
3167
0
        return(0);
3168
0
    switch (parent->type) {
3169
0
        case XML_ELEMENT_NODE:
3170
0
        case XML_DOCUMENT_NODE:
3171
0
        case XML_DOCUMENT_FRAG_NODE:
3172
0
        case XML_HTML_DOCUMENT_NODE:
3173
0
        case XML_ENTITY_DECL:
3174
0
            cur = parent->children;
3175
0
            break;
3176
0
        default:
3177
0
            return(0);
3178
0
    }
3179
0
    while (cur != NULL) {
3180
0
        if (cur->type == XML_ELEMENT_NODE)
3181
0
            ret++;
3182
0
        cur = cur->next;
3183
0
    }
3184
0
    return(ret);
3185
0
}
3186
3187
/**
3188
 * Find the first child node which is an element.
3189
 *
3190
 * Note that entity references are not expanded.
3191
 *
3192
 * @param parent  the parent node
3193
 * @returns the first element or NULL if parent has no children.
3194
 */
3195
xmlNode *
3196
0
xmlFirstElementChild(xmlNode *parent) {
3197
0
    xmlNodePtr cur = NULL;
3198
3199
0
    if (parent == NULL)
3200
0
        return(NULL);
3201
0
    switch (parent->type) {
3202
0
        case XML_ELEMENT_NODE:
3203
0
        case XML_DOCUMENT_NODE:
3204
0
        case XML_DOCUMENT_FRAG_NODE:
3205
0
        case XML_HTML_DOCUMENT_NODE:
3206
0
        case XML_ENTITY_DECL:
3207
0
            cur = parent->children;
3208
0
            break;
3209
0
        default:
3210
0
            return(NULL);
3211
0
    }
3212
0
    while (cur != NULL) {
3213
0
        if (cur->type == XML_ELEMENT_NODE)
3214
0
            return(cur);
3215
0
        cur = cur->next;
3216
0
    }
3217
0
    return(NULL);
3218
0
}
3219
3220
/**
3221
 * Find the last child node which is an element.
3222
 *
3223
 * Note that entity references are not expanded.
3224
 *
3225
 * @param parent  the parent node
3226
 * @returns the last element or NULL if parent has no children.
3227
 */
3228
xmlNode *
3229
0
xmlLastElementChild(xmlNode *parent) {
3230
0
    xmlNodePtr cur = NULL;
3231
3232
0
    if (parent == NULL)
3233
0
        return(NULL);
3234
0
    switch (parent->type) {
3235
0
        case XML_ELEMENT_NODE:
3236
0
        case XML_DOCUMENT_NODE:
3237
0
        case XML_DOCUMENT_FRAG_NODE:
3238
0
        case XML_HTML_DOCUMENT_NODE:
3239
0
        case XML_ENTITY_DECL:
3240
0
            cur = parent->last;
3241
0
            break;
3242
0
        default:
3243
0
            return(NULL);
3244
0
    }
3245
0
    while (cur != NULL) {
3246
0
        if (cur->type == XML_ELEMENT_NODE)
3247
0
            return(cur);
3248
0
        cur = cur->prev;
3249
0
    }
3250
0
    return(NULL);
3251
0
}
3252
3253
/**
3254
 * Find the closest preceding sibling which is a element.
3255
 *
3256
 * Note that entity references are not expanded.
3257
 *
3258
 * @param node  the current node
3259
 * @returns the sibling or NULL if no sibling was found.
3260
 */
3261
xmlNode *
3262
0
xmlPreviousElementSibling(xmlNode *node) {
3263
0
    if (node == NULL)
3264
0
        return(NULL);
3265
0
    switch (node->type) {
3266
0
        case XML_ELEMENT_NODE:
3267
0
        case XML_TEXT_NODE:
3268
0
        case XML_CDATA_SECTION_NODE:
3269
0
        case XML_ENTITY_REF_NODE:
3270
0
        case XML_PI_NODE:
3271
0
        case XML_COMMENT_NODE:
3272
0
        case XML_XINCLUDE_START:
3273
0
        case XML_XINCLUDE_END:
3274
0
            node = node->prev;
3275
0
            break;
3276
0
        default:
3277
0
            return(NULL);
3278
0
    }
3279
0
    while (node != NULL) {
3280
0
        if (node->type == XML_ELEMENT_NODE)
3281
0
            return(node);
3282
0
        node = node->prev;
3283
0
    }
3284
0
    return(NULL);
3285
0
}
3286
3287
/**
3288
 * Find the closest following sibling which is a element.
3289
 *
3290
 * Note that entity references are not expanded.
3291
 *
3292
 * @param node  the current node
3293
 * @returns the sibling or NULL if no sibling was found.
3294
 */
3295
xmlNode *
3296
0
xmlNextElementSibling(xmlNode *node) {
3297
0
    if (node == NULL)
3298
0
        return(NULL);
3299
0
    switch (node->type) {
3300
0
        case XML_ELEMENT_NODE:
3301
0
        case XML_TEXT_NODE:
3302
0
        case XML_CDATA_SECTION_NODE:
3303
0
        case XML_ENTITY_REF_NODE:
3304
0
        case XML_PI_NODE:
3305
0
        case XML_COMMENT_NODE:
3306
0
        case XML_DTD_NODE:
3307
0
        case XML_XINCLUDE_START:
3308
0
        case XML_XINCLUDE_END:
3309
0
            node = node->next;
3310
0
            break;
3311
0
        default:
3312
0
            return(NULL);
3313
0
    }
3314
0
    while (node != NULL) {
3315
0
        if (node->type == XML_ELEMENT_NODE)
3316
0
            return(node);
3317
0
        node = node->next;
3318
0
    }
3319
0
    return(NULL);
3320
0
}
3321
3322
/**
3323
 * Free a node list including all children.
3324
 *
3325
 * @param cur  the first node in the list
3326
 */
3327
void
3328
62.4k
xmlFreeNodeList(xmlNode *cur) {
3329
62.4k
    xmlNodePtr next;
3330
62.4k
    xmlNodePtr parent;
3331
62.4k
    xmlDictPtr dict = NULL;
3332
62.4k
    size_t depth = 0;
3333
3334
62.4k
    if (cur == NULL) return;
3335
61.5k
    if (cur->type == XML_NAMESPACE_DECL) {
3336
0
  xmlFreeNsList((xmlNsPtr) cur);
3337
0
  return;
3338
0
    }
3339
61.5k
    if (cur->doc != NULL) dict = cur->doc->dict;
3340
131k
    while (1) {
3341
183k
        while ((cur->children != NULL) &&
3342
51.9k
               (cur->type != XML_DOCUMENT_NODE) &&
3343
51.9k
               (cur->type != XML_HTML_DOCUMENT_NODE) &&
3344
51.9k
               (cur->type != XML_DTD_NODE) &&
3345
51.9k
               (cur->type != XML_ENTITY_REF_NODE)) {
3346
51.9k
            cur = cur->children;
3347
51.9k
            depth += 1;
3348
51.9k
        }
3349
3350
131k
        next = cur->next;
3351
131k
        parent = cur->parent;
3352
131k
  if ((cur->type == XML_DOCUMENT_NODE) ||
3353
131k
            (cur->type == XML_HTML_DOCUMENT_NODE)) {
3354
0
            xmlFreeDoc((xmlDocPtr) cur);
3355
131k
        } else if (cur->type == XML_DTD_NODE) {
3356
            /*
3357
             * TODO: We should consider freeing the DTD if it isn't
3358
             * referenced from doc->intSubset or doc->extSubset.
3359
             */
3360
0
            cur->prev = NULL;
3361
0
            cur->next = NULL;
3362
131k
        } else {
3363
131k
      if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3364
0
    xmlDeregisterNodeDefaultValue(cur);
3365
3366
131k
      if (((cur->type == XML_ELEMENT_NODE) ||
3367
69.2k
     (cur->type == XML_XINCLUDE_START) ||
3368
69.2k
     (cur->type == XML_XINCLUDE_END)) &&
3369
61.9k
    (cur->properties != NULL))
3370
24.4k
    xmlFreePropList(cur->properties);
3371
131k
      if ((cur->type != XML_ELEMENT_NODE) &&
3372
69.2k
    (cur->type != XML_XINCLUDE_START) &&
3373
69.2k
    (cur->type != XML_XINCLUDE_END) &&
3374
69.2k
    (cur->type != XML_ENTITY_REF_NODE) &&
3375
69.2k
    (cur->content != (xmlChar *) &(cur->properties))) {
3376
69.2k
    DICT_FREE(cur->content)
3377
69.2k
      }
3378
131k
      if (((cur->type == XML_ELEMENT_NODE) ||
3379
69.2k
           (cur->type == XML_XINCLUDE_START) ||
3380
69.2k
     (cur->type == XML_XINCLUDE_END)) &&
3381
61.9k
    (cur->nsDef != NULL))
3382
19.0k
    xmlFreeNsList(cur->nsDef);
3383
3384
      /*
3385
       * When a node is a text node or a comment, it uses a global static
3386
       * variable for the name of the node.
3387
       * Otherwise the node name might come from the document's
3388
       * dictionary
3389
       */
3390
131k
      if ((cur->name != NULL) &&
3391
128k
    (cur->type != XML_TEXT_NODE) &&
3392
65.1k
    (cur->type != XML_COMMENT_NODE))
3393
63.9k
    DICT_FREE(cur->name)
3394
131k
      xmlFree(cur);
3395
131k
  }
3396
3397
131k
        if (next != NULL) {
3398
17.6k
      cur = next;
3399
113k
        } else {
3400
113k
            if ((depth == 0) || (parent == NULL))
3401
61.5k
                break;
3402
51.9k
            depth -= 1;
3403
51.9k
            cur = parent;
3404
51.9k
            cur->children = NULL;
3405
51.9k
        }
3406
131k
    }
3407
61.5k
}
3408
3409
/**
3410
 * Free a node including all the children.
3411
 *
3412
 * This doesn't unlink the node from the tree. Call #xmlUnlinkNode first
3413
 * unless `cur` is a root node.
3414
 *
3415
 * @param cur  the node
3416
 */
3417
void
3418
3.39k
xmlFreeNode(xmlNode *cur) {
3419
3.39k
    xmlDictPtr dict = NULL;
3420
3421
3.39k
    if (cur == NULL) return;
3422
3423
    /* use xmlFreeDtd for DTD nodes */
3424
3.39k
    if (cur->type == XML_DTD_NODE) {
3425
0
  xmlFreeDtd((xmlDtdPtr) cur);
3426
0
  return;
3427
0
    }
3428
3.39k
    if (cur->type == XML_NAMESPACE_DECL) {
3429
0
  xmlFreeNs((xmlNsPtr) cur);
3430
0
        return;
3431
0
    }
3432
3.39k
    if (cur->type == XML_ATTRIBUTE_NODE) {
3433
0
  xmlFreeProp((xmlAttrPtr) cur);
3434
0
  return;
3435
0
    }
3436
3.39k
    if (cur->type == XML_ENTITY_DECL) {
3437
0
        xmlFreeEntity((xmlEntityPtr) cur);
3438
0
        return;
3439
0
    }
3440
3441
3.39k
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3442
0
  xmlDeregisterNodeDefaultValue(cur);
3443
3444
3.39k
    if (cur->doc != NULL) dict = cur->doc->dict;
3445
3446
3.39k
    if ((cur->children != NULL) &&
3447
99
  (cur->type != XML_ENTITY_REF_NODE))
3448
99
  xmlFreeNodeList(cur->children);
3449
3450
3.39k
    if ((cur->type == XML_ELEMENT_NODE) ||
3451
1.35k
        (cur->type == XML_XINCLUDE_START) ||
3452
2.04k
        (cur->type == XML_XINCLUDE_END)) {
3453
2.04k
        if (cur->properties != NULL)
3454
0
            xmlFreePropList(cur->properties);
3455
2.04k
        if (cur->nsDef != NULL)
3456
4
            xmlFreeNsList(cur->nsDef);
3457
2.04k
    } else if ((cur->content != NULL) &&
3458
916
               (cur->type != XML_ENTITY_REF_NODE) &&
3459
916
               (cur->content != (xmlChar *) &(cur->properties))) {
3460
916
        DICT_FREE(cur->content)
3461
916
    }
3462
3463
    /*
3464
     * When a node is a text node or a comment, it uses a global static
3465
     * variable for the name of the node.
3466
     * Otherwise the node name might come from the document's dictionary
3467
     */
3468
3.39k
    if ((cur->name != NULL) &&
3469
3.39k
        (cur->type != XML_TEXT_NODE) &&
3470
3.39k
        (cur->type != XML_COMMENT_NODE))
3471
2.54k
  DICT_FREE(cur->name)
3472
3473
3.39k
    xmlFree(cur);
3474
3.39k
}
3475
3476
/**
3477
 * Unlink a node from its tree.
3478
 *
3479
 * This function only unlinks the node from the tree. It doesn't
3480
 * clear references to DTD nodes.
3481
 *
3482
 * @param cur  the node
3483
 */
3484
static void
3485
15.4k
xmlUnlinkNodeInternal(xmlNodePtr cur) {
3486
15.4k
    if (cur->parent != NULL) {
3487
10.4k
  xmlNodePtr parent;
3488
10.4k
  parent = cur->parent;
3489
10.4k
  if (cur->type == XML_ATTRIBUTE_NODE) {
3490
0
      if (parent->properties == (xmlAttrPtr) cur)
3491
0
    parent->properties = ((xmlAttrPtr) cur)->next;
3492
10.4k
  } else {
3493
10.4k
      if (parent->children == cur)
3494
5.23k
    parent->children = cur->next;
3495
10.4k
      if (parent->last == cur)
3496
4.05k
    parent->last = cur->prev;
3497
10.4k
  }
3498
10.4k
  cur->parent = NULL;
3499
10.4k
    }
3500
3501
15.4k
    if (cur->next != NULL)
3502
6.34k
        cur->next->prev = cur->prev;
3503
15.4k
    if (cur->prev != NULL)
3504
5.17k
        cur->prev->next = cur->next;
3505
15.4k
    cur->next = NULL;
3506
15.4k
    cur->prev = NULL;
3507
15.4k
}
3508
3509
/**
3510
 * Unlink a node from its tree.
3511
 *
3512
 * The node is not freed. Unless it is reinserted, it must be managed
3513
 * manually and freed eventually by calling #xmlFreeNode.
3514
 *
3515
 * @param cur  the node
3516
 */
3517
void
3518
8.42k
xmlUnlinkNode(xmlNode *cur) {
3519
8.42k
    if (cur == NULL)
3520
0
  return;
3521
3522
8.42k
    if (cur->type == XML_NAMESPACE_DECL)
3523
0
        return;
3524
3525
8.42k
    if (cur->type == XML_DTD_NODE) {
3526
0
  xmlDocPtr doc = cur->doc;
3527
3528
0
  if (doc != NULL) {
3529
0
      if (doc->intSubset == (xmlDtdPtr) cur)
3530
0
    doc->intSubset = NULL;
3531
0
      if (doc->extSubset == (xmlDtdPtr) cur)
3532
0
    doc->extSubset = NULL;
3533
0
  }
3534
0
    }
3535
3536
8.42k
    if (cur->type == XML_ENTITY_DECL)
3537
0
        xmlRemoveEntity((xmlEntityPtr) cur);
3538
3539
8.42k
    xmlUnlinkNodeInternal(cur);
3540
8.42k
}
3541
3542
/**
3543
 * Unlink the old node. If `cur` is provided, it is unlinked and
3544
 * inserted in place of `old`.
3545
 *
3546
 * It is an error if `old` has no parent.
3547
 *
3548
 * Unlike #xmlAddChild, this function doesn't merge text nodes or
3549
 * delete duplicate attributes.
3550
 *
3551
 * See the notes in #xmlAddChild.
3552
 *
3553
 * @param old  the old node
3554
 * @param cur  the node (optional)
3555
 * @returns `old` or NULL if arguments are invalid or a memory
3556
 * allocation failed.
3557
 */
3558
xmlNode *
3559
0
xmlReplaceNode(xmlNode *old, xmlNode *cur) {
3560
0
    if (old == cur) return(NULL);
3561
0
    if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3562
0
        (old->parent == NULL)) {
3563
0
  return(NULL);
3564
0
    }
3565
0
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3566
        /* Don't call xmlUnlinkNodeInternal to handle DTDs. */
3567
0
  xmlUnlinkNode(old);
3568
0
  return(old);
3569
0
    }
3570
0
    if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3571
0
  return(old);
3572
0
    }
3573
0
    if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3574
0
  return(old);
3575
0
    }
3576
0
    xmlUnlinkNodeInternal(cur);
3577
0
    if (xmlSetTreeDoc(cur, old->doc) < 0)
3578
0
        return(NULL);
3579
0
    cur->parent = old->parent;
3580
0
    cur->next = old->next;
3581
0
    if (cur->next != NULL)
3582
0
  cur->next->prev = cur;
3583
0
    cur->prev = old->prev;
3584
0
    if (cur->prev != NULL)
3585
0
  cur->prev->next = cur;
3586
0
    if (cur->parent != NULL) {
3587
0
  if (cur->type == XML_ATTRIBUTE_NODE) {
3588
0
      if (cur->parent->properties == (xmlAttrPtr)old)
3589
0
    cur->parent->properties = ((xmlAttrPtr) cur);
3590
0
  } else {
3591
0
      if (cur->parent->children == old)
3592
0
    cur->parent->children = cur;
3593
0
      if (cur->parent->last == old)
3594
0
    cur->parent->last = cur;
3595
0
  }
3596
0
    }
3597
0
    old->next = old->prev = NULL;
3598
0
    old->parent = NULL;
3599
0
    return(old);
3600
0
}
3601
3602
/************************************************************************
3603
 *                  *
3604
 *    Copy operations           *
3605
 *                  *
3606
 ************************************************************************/
3607
3608
/**
3609
 * Copy a namespace.
3610
 *
3611
 * @param cur  the namespace
3612
 * @returns the copied namespace or NULL if a memory allocation
3613
 * failed.
3614
 */
3615
xmlNs *
3616
1.02k
xmlCopyNamespace(xmlNs *cur) {
3617
1.02k
    xmlNsPtr ret;
3618
3619
1.02k
    if (cur == NULL) return(NULL);
3620
1.02k
    switch (cur->type) {
3621
1.02k
  case XML_LOCAL_NAMESPACE:
3622
1.02k
      ret = xmlNewNs(NULL, cur->href, cur->prefix);
3623
1.02k
      break;
3624
0
  default:
3625
0
      return(NULL);
3626
1.02k
    }
3627
1.02k
    return(ret);
3628
1.02k
}
3629
3630
/**
3631
 * Copy a namespace list.
3632
 *
3633
 * @param cur  the first namespace
3634
 * @returns the head of the copied list or NULL if a memory
3635
 * allocation failed.
3636
 */
3637
xmlNs *
3638
877
xmlCopyNamespaceList(xmlNs *cur) {
3639
877
    xmlNsPtr ret = NULL;
3640
877
    xmlNsPtr p = NULL,q;
3641
3642
1.89k
    while (cur != NULL) {
3643
1.02k
        q = xmlCopyNamespace(cur);
3644
1.02k
        if (q == NULL) {
3645
0
            xmlFreeNsList(ret);
3646
0
            return(NULL);
3647
0
        }
3648
1.02k
  if (p == NULL) {
3649
877
      ret = p = q;
3650
877
  } else {
3651
144
      p->next = q;
3652
144
      p = q;
3653
144
  }
3654
1.02k
  cur = cur->next;
3655
1.02k
    }
3656
877
    return(ret);
3657
877
}
3658
3659
static xmlAttrPtr
3660
1.81k
xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3661
1.81k
    xmlAttrPtr ret = NULL;
3662
3663
1.81k
    if (cur == NULL) return(NULL);
3664
1.81k
    if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3665
0
        return(NULL);
3666
1.81k
    if (target != NULL)
3667
1.81k
  ret = xmlNewDocProp(target->doc, cur->name, NULL);
3668
0
    else if (doc != NULL)
3669
0
  ret = xmlNewDocProp(doc, cur->name, NULL);
3670
0
    else if (cur->parent != NULL)
3671
0
  ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3672
0
    else if (cur->children != NULL)
3673
0
  ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3674
0
    else
3675
0
  ret = xmlNewDocProp(NULL, cur->name, NULL);
3676
1.81k
    if (ret == NULL) return(NULL);
3677
1.81k
    ret->parent = target;
3678
3679
1.81k
    if ((cur->ns != NULL) && (target != NULL)) {
3680
751
      xmlNsPtr ns;
3681
751
      int res;
3682
3683
751
      res = xmlSearchNsSafe(target, cur->ns->prefix, &ns);
3684
751
      if (res < 0)
3685
0
          goto error;
3686
751
      if (ns == NULL) {
3687
        /*
3688
         * Humm, we are copying an element whose namespace is defined
3689
         * out of the new tree scope. Search it in the original tree
3690
         * and add it at the top of the new tree
3691
         */
3692
0
        res = xmlSearchNsSafe(cur->parent, cur->ns->prefix, &ns);
3693
0
        if (res < 0)
3694
0
          goto error;
3695
0
        if (ns != NULL) {
3696
0
          xmlNodePtr root = target;
3697
0
          xmlNodePtr pred = NULL;
3698
3699
0
          while (root->parent != NULL) {
3700
0
            pred = root;
3701
0
            root = root->parent;
3702
0
          }
3703
0
          if (root == (xmlNodePtr) target->doc) {
3704
            /* correct possibly cycling above the document elt */
3705
0
            root = pred;
3706
0
          }
3707
0
          ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3708
0
          if (ret->ns == NULL)
3709
0
              goto error;
3710
0
        }
3711
751
      } else {
3712
        /*
3713
         * we have to find something appropriate here since
3714
         * we can't be sure, that the namespace we found is identified
3715
         * by the prefix
3716
         */
3717
751
        if (xmlStrEqual(ns->href, cur->ns->href)) {
3718
          /* this is the nice case */
3719
751
          ret->ns = ns;
3720
751
        } else {
3721
          /*
3722
           * we are in trouble: we need a new reconciled namespace.
3723
           * This is expensive
3724
           */
3725
0
          ret->ns = xmlNewReconciledNs(target, cur->ns);
3726
0
          if (ret->ns == NULL)
3727
0
              goto error;
3728
0
        }
3729
751
      }
3730
3731
751
    } else
3732
1.06k
        ret->ns = NULL;
3733
3734
1.81k
    if (cur->children != NULL) {
3735
1.81k
  xmlNodePtr tmp;
3736
3737
1.81k
  ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3738
1.81k
        if (ret->children == NULL)
3739
0
            goto error;
3740
1.81k
  ret->last = NULL;
3741
1.81k
  tmp = ret->children;
3742
3.63k
  while (tmp != NULL) {
3743
      /* tmp->parent = (xmlNodePtr)ret; */
3744
1.81k
      if (tmp->next == NULL)
3745
1.81k
          ret->last = tmp;
3746
1.81k
      tmp = tmp->next;
3747
1.81k
  }
3748
1.81k
    }
3749
    /*
3750
     * Try to handle IDs
3751
     */
3752
1.81k
    if ((target != NULL) && (cur != NULL) &&
3753
1.81k
  (target->doc != NULL) && (cur->doc != NULL) &&
3754
1.81k
        (cur->parent != NULL) &&
3755
1.81k
        (cur->children != NULL)) {
3756
1.81k
        int res = xmlIsID(cur->doc, cur->parent, cur);
3757
3758
1.81k
        if (res < 0)
3759
0
            goto error;
3760
1.81k
  if (res != 0) {
3761
339
      xmlChar *id;
3762
3763
339
      id = xmlNodeGetContent((xmlNodePtr) cur);
3764
339
      if (id == NULL)
3765
0
                goto error;
3766
339
            res = xmlAddIDSafe(ret, id);
3767
339
      xmlFree(id);
3768
339
            if (res < 0)
3769
0
                goto error;
3770
339
  }
3771
1.81k
    }
3772
1.81k
    return(ret);
3773
3774
0
error:
3775
0
    xmlFreeProp(ret);
3776
0
    return(NULL);
3777
1.81k
}
3778
3779
/**
3780
 * Create a copy of the attribute. This function sets the parent
3781
 * pointer of the copy to `target` but doesn't set the attribute on
3782
 * the target element. Users should consider to set the attribute
3783
 * by calling #xmlAddChild afterwards or reset the parent pointer to
3784
 * NULL.
3785
 *
3786
 * @param target  the element where the attribute will be grafted
3787
 * @param cur  the attribute
3788
 * @returns the copied attribute or NULL if a memory allocation
3789
 * failed.
3790
 */
3791
xmlAttr *
3792
1.81k
xmlCopyProp(xmlNode *target, xmlAttr *cur) {
3793
1.81k
  return xmlCopyPropInternal(NULL, target, cur);
3794
1.81k
}
3795
3796
/**
3797
 * Create a copy of an attribute list. This function sets the
3798
 * parent pointers of the copied attributes to `target` but doesn't
3799
 * set the attributes on the target element.
3800
 *
3801
 * @param target  the element where the attributes will be grafted
3802
 * @param cur  the first attribute
3803
 * @returns the head of the copied list or NULL if a memory
3804
 * allocation failed.
3805
 */
3806
xmlAttr *
3807
1.32k
xmlCopyPropList(xmlNode *target, xmlAttr *cur) {
3808
1.32k
    xmlAttrPtr ret = NULL;
3809
1.32k
    xmlAttrPtr p = NULL,q;
3810
3811
1.32k
    if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3812
0
        return(NULL);
3813
3.13k
    while (cur != NULL) {
3814
1.81k
        q = xmlCopyProp(target, cur);
3815
1.81k
  if (q == NULL) {
3816
0
            xmlFreePropList(ret);
3817
0
      return(NULL);
3818
0
        }
3819
1.81k
  if (p == NULL) {
3820
1.32k
      ret = p = q;
3821
1.32k
  } else {
3822
495
      p->next = q;
3823
495
      q->prev = p;
3824
495
      p = q;
3825
495
  }
3826
1.81k
  cur = cur->next;
3827
1.81k
    }
3828
1.32k
    return(ret);
3829
1.32k
}
3830
3831
/*
3832
 * NOTE about the CopyNode operations !
3833
 *
3834
 * They are split into external and internal parts for one
3835
 * tricky reason: namespaces. Doing a direct copy of a node
3836
 * say RPM:Copyright without changing the namespace pointer to
3837
 * something else can produce stale links. One way to do it is
3838
 * to keep a reference counter but this doesn't work as soon
3839
 * as one moves the element or the subtree out of the scope of
3840
 * the existing namespace. The actual solution seems to be to add
3841
 * a copy of the namespace at the top of the copied tree if
3842
 * not available in the subtree.
3843
 * Hence two functions, the public front-end call the inner ones
3844
 * The argument "recursive" normally indicates a recursive copy
3845
 * of the node with values 0 (no) and 1 (yes).  For XInclude,
3846
 * however, we allow a value of 2 to indicate copy properties and
3847
 * namespace info, but don't recurse on children.
3848
 */
3849
3850
/**
3851
 * Copy a node.
3852
 *
3853
 * @param node  source node
3854
 * @param doc  target document
3855
 * @param parent  target parent
3856
 * @param extended  flags
3857
 * @returns the copy or NULL if a memory allocation failed.
3858
 */
3859
xmlNode *
3860
xmlStaticCopyNode(xmlNode *node, xmlDoc *doc, xmlNode *parent,
3861
10.8k
                  int extended) {
3862
10.8k
    xmlNodePtr ret;
3863
3864
10.8k
    if (node == NULL) return(NULL);
3865
10.8k
    switch (node->type) {
3866
4.04k
        case XML_TEXT_NODE:
3867
4.52k
        case XML_CDATA_SECTION_NODE:
3868
10.3k
        case XML_ELEMENT_NODE:
3869
10.3k
        case XML_DOCUMENT_FRAG_NODE:
3870
10.3k
        case XML_ENTITY_REF_NODE:
3871
10.7k
        case XML_PI_NODE:
3872
10.8k
        case XML_COMMENT_NODE:
3873
10.8k
        case XML_XINCLUDE_START:
3874
10.8k
        case XML_XINCLUDE_END:
3875
10.8k
      break;
3876
0
        case XML_ATTRIBUTE_NODE:
3877
0
    return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
3878
0
        case XML_NAMESPACE_DECL:
3879
0
      return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3880
0
        case XML_DTD_NODE:
3881
0
            return((xmlNodePtr) xmlCopyDtd((xmlDtdPtr) node));
3882
0
        case XML_DOCUMENT_NODE:
3883
0
        case XML_HTML_DOCUMENT_NODE:
3884
0
      return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
3885
0
        default:
3886
0
            return(NULL);
3887
10.8k
    }
3888
3889
    /*
3890
     * Allocate a new node and fill the fields.
3891
     */
3892
10.8k
    ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3893
10.8k
    if (ret == NULL)
3894
0
  return(NULL);
3895
10.8k
    memset(ret, 0, sizeof(xmlNode));
3896
10.8k
    ret->type = node->type;
3897
3898
10.8k
    ret->doc = doc;
3899
10.8k
    ret->parent = parent;
3900
10.8k
    if (node->name == xmlStringText)
3901
4.04k
  ret->name = xmlStringText;
3902
6.84k
    else if (node->name == xmlStringTextNoenc)
3903
0
  ret->name = xmlStringTextNoenc;
3904
6.84k
    else if (node->name == xmlStringComment)
3905
157
  ret->name = xmlStringComment;
3906
6.68k
    else if (node->name != NULL) {
3907
6.20k
        if ((doc != NULL) && (doc->dict != NULL))
3908
0
      ret->name = xmlDictLookup(doc->dict, node->name, -1);
3909
6.20k
  else
3910
6.20k
      ret->name = xmlStrdup(node->name);
3911
6.20k
        if (ret->name == NULL)
3912
0
            goto error;
3913
6.20k
    }
3914
10.8k
    if ((node->type != XML_ELEMENT_NODE) &&
3915
5.04k
  (node->content != NULL) &&
3916
4.68k
  (node->type != XML_ENTITY_REF_NODE) &&
3917
4.68k
  (node->type != XML_XINCLUDE_END) &&
3918
4.68k
  (node->type != XML_XINCLUDE_START)) {
3919
4.68k
  ret->content = xmlStrdup(node->content);
3920
4.68k
        if (ret->content == NULL)
3921
0
            goto error;
3922
6.20k
    }else{
3923
6.20k
      if (node->type == XML_ELEMENT_NODE)
3924
5.83k
        ret->line = node->line;
3925
6.20k
    }
3926
3927
10.8k
    if (!extended)
3928
0
  goto out;
3929
10.8k
    if (((node->type == XML_ELEMENT_NODE) ||
3930
5.83k
         (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) {
3931
877
        ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3932
877
        if (ret->nsDef == NULL)
3933
0
            goto error;
3934
877
    }
3935
3936
10.8k
    if ((node->type == XML_ELEMENT_NODE) && (node->ns != NULL)) {
3937
1.36k
        xmlNsPtr ns = NULL;
3938
1.36k
        int res;
3939
3940
1.36k
  res = xmlSearchNsSafe(ret, node->ns->prefix, &ns);
3941
1.36k
        if (res < 0)
3942
0
            goto error;
3943
1.36k
  if (ns == NULL) {
3944
      /*
3945
       * Humm, we are copying an element whose namespace is defined
3946
       * out of the new tree scope. Search it in the original tree
3947
       * and add it at the top of the new tree.
3948
             *
3949
             * TODO: Searching the original tree seems unnecessary. We
3950
             * already have a namespace URI.
3951
       */
3952
0
      res = xmlSearchNsSafe(node, node->ns->prefix, &ns);
3953
0
            if (res < 0)
3954
0
                goto error;
3955
0
      if (ns != NULL) {
3956
0
          xmlNodePtr root = ret;
3957
3958
0
    while (root->parent != NULL) root = root->parent;
3959
0
    ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3960
0
            } else {
3961
0
                ret->ns = xmlNewReconciledNs(ret, node->ns);
3962
0
      }
3963
0
            if (ret->ns == NULL)
3964
0
                goto error;
3965
1.36k
  } else {
3966
      /*
3967
       * reference the existing namespace definition in our own tree.
3968
       */
3969
1.36k
      ret->ns = ns;
3970
1.36k
  }
3971
1.36k
    }
3972
10.8k
    if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) {
3973
1.32k
        ret->properties = xmlCopyPropList(ret, node->properties);
3974
1.32k
        if (ret->properties == NULL)
3975
0
            goto error;
3976
1.32k
    }
3977
10.8k
    if (node->type == XML_ENTITY_REF_NODE) {
3978
0
  if ((doc == NULL) || (node->doc != doc)) {
3979
      /*
3980
       * The copied node will go into a separate document, so
3981
       * to avoid dangling references to the ENTITY_DECL node
3982
       * we cannot keep the reference. Try to find it in the
3983
       * target document.
3984
       */
3985
0
      ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3986
0
  } else {
3987
0
            ret->children = node->children;
3988
0
  }
3989
0
  ret->last = ret->children;
3990
10.8k
    } else if ((node->children != NULL) && (extended != 2)) {
3991
745
        xmlNodePtr cur, insert;
3992
3993
745
        cur = node->children;
3994
745
        insert = ret;
3995
2.94k
        while (cur != NULL) {
3996
2.19k
            xmlNodePtr copy = xmlStaticCopyNode(cur, doc, insert, 2);
3997
2.19k
            if (copy == NULL)
3998
0
                goto error;
3999
4000
            /* Check for coalesced text nodes */
4001
2.19k
            if (insert->last != copy) {
4002
2.19k
                if (insert->last == NULL) {
4003
1.26k
                    insert->children = copy;
4004
1.26k
                } else {
4005
937
                    copy->prev = insert->last;
4006
937
                    insert->last->next = copy;
4007
937
                }
4008
2.19k
                insert->last = copy;
4009
2.19k
            }
4010
4011
2.19k
            if ((cur->type != XML_ENTITY_REF_NODE) &&
4012
2.19k
                (cur->children != NULL)) {
4013
517
                cur = cur->children;
4014
517
                insert = copy;
4015
517
                continue;
4016
517
            }
4017
4018
2.19k
            while (1) {
4019
2.19k
                if (cur->next != NULL) {
4020
937
                    cur = cur->next;
4021
937
                    break;
4022
937
                }
4023
4024
1.26k
                cur = cur->parent;
4025
1.26k
                insert = insert->parent;
4026
1.26k
                if (cur == node) {
4027
745
                    cur = NULL;
4028
745
                    break;
4029
745
                }
4030
1.26k
            }
4031
1.68k
        }
4032
745
    }
4033
4034
10.8k
out:
4035
10.8k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4036
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4037
10.8k
    return(ret);
4038
4039
0
error:
4040
0
    xmlFreeNode(ret);
4041
0
    return(NULL);
4042
10.8k
}
4043
4044
/**
4045
 * Copy a node list. If `parent` is provided, sets the parent pointer
4046
 * of the copied nodes, but doesn't update the children and last
4047
 * pointer of `parent`.
4048
 *
4049
 * @param node  node to copy
4050
 * @param doc  target document
4051
 * @param parent  target node (optional)
4052
 * @returns a the copy or NULL in case of error.
4053
 */
4054
xmlNode *
4055
1.81k
xmlStaticCopyNodeList(xmlNode *node, xmlDoc *doc, xmlNode *parent) {
4056
1.81k
    xmlNodePtr ret = NULL;
4057
1.81k
    xmlNodePtr p = NULL,q;
4058
1.81k
    xmlDtdPtr newSubset = NULL;
4059
1.81k
    int linkedSubset = 0;
4060
4061
3.63k
    while (node != NULL) {
4062
1.81k
        xmlNodePtr next = node->next;
4063
4064
1.81k
  if (node->type == XML_DTD_NODE ) {
4065
0
      if (doc == NULL) {
4066
0
    node = next;
4067
0
    continue;
4068
0
      }
4069
0
      if ((doc->intSubset == NULL) && (newSubset == NULL)) {
4070
0
    q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4071
0
    if (q == NULL) goto error;
4072
                /* Can't fail on DTD */
4073
0
    xmlSetTreeDoc(q, doc);
4074
0
    q->parent = parent;
4075
0
    newSubset = (xmlDtdPtr) q;
4076
0
      } else {
4077
                /*
4078
                 * We don't allow multiple internal subsets in a document,
4079
                 * so we move the DTD instead of creating a copy.
4080
                 */
4081
0
                linkedSubset = 1;
4082
0
    q = (xmlNodePtr) doc->intSubset;
4083
                /* Unlink */
4084
0
                if (q->prev == NULL) {
4085
0
                    if (q->parent != NULL)
4086
0
                        q->parent->children = q->next;
4087
0
                } else {
4088
0
                    q->prev->next = q->next;
4089
0
                }
4090
0
                if (q->next == NULL) {
4091
0
                    if (q->parent != NULL)
4092
0
                        q->parent->last = q->prev;
4093
0
                } else {
4094
0
                    q->next->prev = q->prev;
4095
0
                }
4096
0
                q->parent = parent;
4097
0
                q->next = NULL;
4098
0
                q->prev = NULL;
4099
0
      }
4100
0
  } else
4101
1.81k
      q = xmlStaticCopyNode(node, doc, parent, 1);
4102
1.81k
  if (q == NULL) goto error;
4103
1.81k
  if (ret == NULL) {
4104
1.81k
      q->prev = NULL;
4105
1.81k
      ret = p = q;
4106
1.81k
  } else if (p != q) {
4107
  /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4108
0
      p->next = q;
4109
0
      q->prev = p;
4110
0
      p = q;
4111
0
  }
4112
1.81k
  node = next;
4113
1.81k
    }
4114
1.81k
    if ((doc != NULL) && (newSubset != NULL))
4115
0
        doc->intSubset = newSubset;
4116
1.81k
    return(ret);
4117
0
error:
4118
0
    xmlFreeNodeList(ret);
4119
0
    if (newSubset != NULL)
4120
0
        xmlFreeDtd(newSubset);
4121
0
    if (linkedSubset != 0) {
4122
0
        doc->intSubset->next = NULL;
4123
0
        doc->intSubset->prev = NULL;
4124
0
    }
4125
0
    return(NULL);
4126
1.81k
}
4127
4128
/**
4129
 * Copy a node.
4130
 *
4131
 * If `extended` is 0, make a shallow copy.
4132
 *
4133
 * If `extended` is 1, make a deep copy (properties, namespaces
4134
 * and children when applicable).
4135
 *
4136
 * If `extended` is 2, make a shallow copy including properties and
4137
 * namespaces of elements.
4138
 *
4139
 * Use of this function is DISCOURAGED in favor of #xmlDocCopyNode.
4140
 *
4141
 * @param node  the node
4142
 * @param extended  mode of operation
4143
 * @returns the copied node or NULL if a memory allocation failed.
4144
 */
4145
xmlNode *
4146
0
xmlCopyNode(xmlNode *node, int extended) {
4147
0
    xmlNodePtr ret;
4148
4149
0
    ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4150
0
    return(ret);
4151
0
}
4152
4153
/**
4154
 * Copy a node into another document.
4155
 *
4156
 * If `extended` is 0, make a shallow copy.
4157
 *
4158
 * If `extended` is 1, make a deep copy (properties, namespaces
4159
 * and children when applicable).
4160
 *
4161
 * If `extended` is 2, make a shallow copy including properties and
4162
 * namespaces of elements.
4163
 *
4164
 * @param node  the node
4165
 * @param doc  the document
4166
 * @param extended  mode of operation
4167
 * @returns the copied node or NULL if a memory allocation failed.
4168
 */
4169
xmlNode *
4170
6.86k
xmlDocCopyNode(xmlNode *node, xmlDoc *doc, int extended) {
4171
6.86k
    xmlNodePtr ret;
4172
4173
6.86k
    ret = xmlStaticCopyNode(node, doc, NULL, extended);
4174
6.86k
    return(ret);
4175
6.86k
}
4176
4177
/**
4178
 * Copy a node list and all children into a new document.
4179
 *
4180
 * @param doc  the target document
4181
 * @param node  the first node in the list.
4182
 * @returns the head of the copied list or NULL if a memory
4183
 * allocation failed.
4184
 */
4185
0
xmlNode *xmlDocCopyNodeList(xmlDoc *doc, xmlNode *node) {
4186
0
    xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4187
0
    return(ret);
4188
0
}
4189
4190
/**
4191
 * Copy a node list and all children.
4192
 *
4193
 * Use of this function is DISCOURAGED in favor of #xmlDocCopyNodeList.
4194
 *
4195
 * @param node  the first node in the list.
4196
 * @returns the head of the copied list or NULL if a memory
4197
 * allocation failed.
4198
 */
4199
0
xmlNode *xmlCopyNodeList(xmlNode *node) {
4200
0
    xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4201
0
    return(ret);
4202
0
}
4203
4204
/**
4205
 * Copy a DTD.
4206
 *
4207
 * @param dtd  the DTD
4208
 * @returns the copied DTD or NULL if a memory allocation failed.
4209
 */
4210
xmlDtd *
4211
0
xmlCopyDtd(xmlDtd *dtd) {
4212
0
    xmlDtdPtr ret;
4213
0
    xmlNodePtr cur, p = NULL, q;
4214
4215
0
    if (dtd == NULL) return(NULL);
4216
0
    ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4217
0
    if (ret == NULL) return(NULL);
4218
0
    if (dtd->entities != NULL) {
4219
0
        ret->entities = (void *) xmlCopyEntitiesTable(
4220
0
                      (xmlEntitiesTablePtr) dtd->entities);
4221
0
        if (ret->entities == NULL)
4222
0
            goto error;
4223
0
    }
4224
0
    if (dtd->notations != NULL) {
4225
0
        ret->notations = (void *) xmlCopyNotationTable(
4226
0
                      (xmlNotationTablePtr) dtd->notations);
4227
0
        if (ret->notations == NULL)
4228
0
            goto error;
4229
0
    }
4230
0
    if (dtd->elements != NULL) {
4231
0
        ret->elements = (void *) xmlCopyElementTable(
4232
0
                      (xmlElementTablePtr) dtd->elements);
4233
0
        if (ret->elements == NULL)
4234
0
            goto error;
4235
0
    }
4236
0
    if (dtd->attributes != NULL) {
4237
0
        ret->attributes = (void *) xmlCopyAttributeTable(
4238
0
                      (xmlAttributeTablePtr) dtd->attributes);
4239
0
        if (ret->attributes == NULL)
4240
0
            goto error;
4241
0
    }
4242
0
    if (dtd->pentities != NULL) {
4243
0
  ret->pentities = (void *) xmlCopyEntitiesTable(
4244
0
          (xmlEntitiesTablePtr) dtd->pentities);
4245
0
        if (ret->pentities == NULL)
4246
0
            goto error;
4247
0
    }
4248
4249
0
    cur = dtd->children;
4250
0
    while (cur != NULL) {
4251
0
  q = NULL;
4252
4253
0
  if (cur->type == XML_ENTITY_DECL) {
4254
0
      xmlEntityPtr tmp = (xmlEntityPtr) cur;
4255
0
      switch (tmp->etype) {
4256
0
    case XML_INTERNAL_GENERAL_ENTITY:
4257
0
    case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4258
0
    case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4259
0
        q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4260
0
        break;
4261
0
    case XML_INTERNAL_PARAMETER_ENTITY:
4262
0
    case XML_EXTERNAL_PARAMETER_ENTITY:
4263
0
        q = (xmlNodePtr)
4264
0
      xmlGetParameterEntityFromDtd(ret, tmp->name);
4265
0
        break;
4266
0
    case XML_INTERNAL_PREDEFINED_ENTITY:
4267
0
        break;
4268
0
      }
4269
0
  } else if (cur->type == XML_ELEMENT_DECL) {
4270
0
      xmlElementPtr tmp = (xmlElementPtr) cur;
4271
0
      q = (xmlNodePtr)
4272
0
    xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4273
0
  } else if (cur->type == XML_ATTRIBUTE_DECL) {
4274
0
      xmlAttributePtr tmp = (xmlAttributePtr) cur;
4275
0
      q = (xmlNodePtr)
4276
0
    xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4277
0
  } else if (cur->type == XML_COMMENT_NODE) {
4278
0
      q = xmlCopyNode(cur, 0);
4279
0
            if (q == NULL)
4280
0
                goto error;
4281
0
  }
4282
4283
0
  if (q == NULL) {
4284
0
      cur = cur->next;
4285
0
      continue;
4286
0
  }
4287
4288
0
  if (p == NULL)
4289
0
      ret->children = q;
4290
0
  else
4291
0
      p->next = q;
4292
4293
0
  q->prev = p;
4294
0
  q->parent = (xmlNodePtr) ret;
4295
0
  q->next = NULL;
4296
0
  ret->last = q;
4297
0
  p = q;
4298
0
  cur = cur->next;
4299
0
    }
4300
4301
0
    return(ret);
4302
4303
0
error:
4304
0
    xmlFreeDtd(ret);
4305
0
    return(NULL);
4306
0
}
4307
4308
/**
4309
 * Copy a document. If recursive, the content tree will
4310
 * be copied too as well as DTD, namespaces and entities.
4311
 *
4312
 * @param doc  the document
4313
 * @param recursive  if not zero do a recursive copy.
4314
 * @returns the copied document or NULL if a memory allocation
4315
 * failed.
4316
 */
4317
xmlDoc *
4318
0
xmlCopyDoc(xmlDoc *doc, int recursive) {
4319
0
    xmlDocPtr ret;
4320
4321
0
    if (doc == NULL) return(NULL);
4322
0
    ret = xmlNewDoc(doc->version);
4323
0
    if (ret == NULL) return(NULL);
4324
0
    ret->type = doc->type;
4325
0
    if (doc->name != NULL) {
4326
0
        ret->name = xmlMemStrdup(doc->name);
4327
0
        if (ret->name == NULL)
4328
0
            goto error;
4329
0
    }
4330
0
    if (doc->encoding != NULL) {
4331
0
        ret->encoding = xmlStrdup(doc->encoding);
4332
0
        if (ret->encoding == NULL)
4333
0
            goto error;
4334
0
    }
4335
0
    if (doc->URL != NULL) {
4336
0
        ret->URL = xmlStrdup(doc->URL);
4337
0
        if (ret->URL == NULL)
4338
0
            goto error;
4339
0
    }
4340
0
    ret->charset = doc->charset;
4341
0
    ret->compression = doc->compression;
4342
0
    ret->standalone = doc->standalone;
4343
0
    if (!recursive) return(ret);
4344
4345
0
    ret->last = NULL;
4346
0
    ret->children = NULL;
4347
0
    if (doc->intSubset != NULL) {
4348
0
        ret->intSubset = xmlCopyDtd(doc->intSubset);
4349
0
  if (ret->intSubset == NULL)
4350
0
            goto error;
4351
        /* Can't fail on DTD */
4352
0
  xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4353
0
    }
4354
0
    if (doc->oldNs != NULL) {
4355
0
        ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4356
0
        if (ret->oldNs == NULL)
4357
0
            goto error;
4358
0
    }
4359
0
    if (doc->children != NULL) {
4360
0
  xmlNodePtr tmp;
4361
4362
0
  ret->children = xmlStaticCopyNodeList(doc->children, ret,
4363
0
                                   (xmlNodePtr)ret);
4364
0
        if (ret->children == NULL)
4365
0
            goto error;
4366
0
  ret->last = NULL;
4367
0
  tmp = ret->children;
4368
0
  while (tmp != NULL) {
4369
0
      if (tmp->next == NULL)
4370
0
          ret->last = tmp;
4371
0
      tmp = tmp->next;
4372
0
  }
4373
0
    }
4374
0
    return(ret);
4375
4376
0
error:
4377
0
    xmlFreeDoc(ret);
4378
0
    return(NULL);
4379
0
}
4380
4381
/************************************************************************
4382
 *                  *
4383
 *    Content access functions        *
4384
 *                  *
4385
 ************************************************************************/
4386
4387
/**
4388
 * Get line number of `node`. Try to work around the limitation of
4389
 * line numbers being stored as 16 bits ints. Requires xmlParserOption
4390
 * XML_PARSE_BIG_LINES to be set when parsing.
4391
 *
4392
 * @param node  valid node
4393
 * @param depth  used to limit any risk of recursion
4394
 * @returns the line number if successful, -1 otherwise
4395
 */
4396
static long
4397
xmlGetLineNoInternal(const xmlNode *node, int depth)
4398
17.9k
{
4399
17.9k
    long result = -1;
4400
4401
17.9k
    if (depth >= 5)
4402
2.00k
        return(-1);
4403
4404
15.9k
    if (!node)
4405
0
        return result;
4406
15.9k
    if ((node->type == XML_ELEMENT_NODE) ||
4407
4.31k
        (node->type == XML_TEXT_NODE) ||
4408
1.56k
  (node->type == XML_COMMENT_NODE) ||
4409
15.4k
  (node->type == XML_PI_NODE)) {
4410
15.4k
  if (node->line == 65535) {
4411
13.0k
      if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4412
0
          result = XML_PTR_TO_INT(node->psvi);
4413
13.0k
      else if ((node->type == XML_ELEMENT_NODE) &&
4414
9.27k
               (node->children != NULL))
4415
182
          result = xmlGetLineNoInternal(node->children, depth + 1);
4416
12.8k
      else if (node->next != NULL)
4417
3.83k
          result = xmlGetLineNoInternal(node->next, depth + 1);
4418
9.05k
      else if (node->prev != NULL)
4419
5.82k
          result = xmlGetLineNoInternal(node->prev, depth + 1);
4420
13.0k
  }
4421
15.4k
  if ((result == -1) || (result == 65535))
4422
15.2k
      result = node->line;
4423
15.4k
    } else if ((node->prev != NULL) &&
4424
376
             ((node->prev->type == XML_ELEMENT_NODE) ||
4425
342
        (node->prev->type == XML_TEXT_NODE) ||
4426
247
        (node->prev->type == XML_COMMENT_NODE) ||
4427
220
        (node->prev->type == XML_PI_NODE)))
4428
181
        result = xmlGetLineNoInternal(node->prev, depth + 1);
4429
297
    else if ((node->parent != NULL) &&
4430
297
             (node->parent->type == XML_ELEMENT_NODE))
4431
283
        result = xmlGetLineNoInternal(node->parent, depth + 1);
4432
4433
15.9k
    return result;
4434
15.9k
}
4435
4436
/**
4437
 * Get line number of `node`. Try to work around the limitation of
4438
 * line numbers being stored as 16 bits ints. Requires xmlParserOption
4439
 * XML_PARSE_BIG_LINES to be set when parsing.
4440
 *
4441
 * @param node  valid node
4442
 * @returns the line number if successful, -1 otherwise
4443
 */
4444
long
4445
xmlGetLineNo(const xmlNode *node)
4446
7.60k
{
4447
7.60k
    return(xmlGetLineNoInternal(node, 0));
4448
7.60k
}
4449
4450
/**
4451
 * Build a structure based Path for the given node
4452
 *
4453
 * @param node  a node
4454
 * @returns the new path or NULL in case of error. The caller must free
4455
 *     the returned string
4456
 */
4457
xmlChar *
4458
xmlGetNodePath(const xmlNode *node)
4459
0
{
4460
0
    const xmlNode *cur, *tmp;
4461
0
    const xmlNode **nodes = NULL;
4462
0
    xmlChar *ret = NULL;
4463
0
    xmlBuf *buf;
4464
0
    size_t numNodes, i;
4465
4466
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4467
0
        return(NULL);
4468
4469
0
    buf = xmlBufCreate(50);
4470
0
    if (buf == NULL)
4471
0
        return(NULL);
4472
4473
    /*
4474
     * Get list of ancestors
4475
     */
4476
0
    numNodes = 0;
4477
0
    for (cur = node; cur != NULL; cur = cur->parent)
4478
0
        numNodes += 1;
4479
0
    if (numNodes > SIZE_MAX / sizeof(nodes[0]))
4480
0
        goto error;
4481
0
    nodes = xmlMalloc(numNodes * sizeof(nodes[0]));
4482
0
    if (nodes == NULL)
4483
0
        goto error;
4484
0
    i = 0;
4485
0
    for (cur = node; cur != NULL && i < numNodes; cur = cur->parent)
4486
0
        nodes[i++] = cur;
4487
4488
    /*
4489
     * Iterate in reverse to start at root
4490
     */
4491
0
    while (i > 0) {
4492
0
        int occur = 0;
4493
4494
0
        i -= 1;
4495
0
        cur = nodes[i];
4496
4497
0
        if ((cur->type == XML_DOCUMENT_NODE) ||
4498
0
            (cur->type == XML_HTML_DOCUMENT_NODE)) {
4499
0
            if (i == 0)
4500
0
                xmlBufCat(buf, BAD_CAST "/");
4501
0
        } else if (cur->type == XML_ELEMENT_NODE) {
4502
0
            int generic = 0;
4503
4504
0
            xmlBufCat(buf, BAD_CAST "/");
4505
4506
0
            if (cur->ns) {
4507
0
    if (cur->ns->prefix != NULL) {
4508
0
                    xmlBufCat(buf, cur->ns->prefix);
4509
0
                    xmlBufCat(buf, BAD_CAST ":");
4510
0
                    xmlBufCat(buf, cur->name);
4511
0
    } else {
4512
        /*
4513
        * We cannot express named elements in the default
4514
        * namespace, so use "*".
4515
        */
4516
0
        generic = 1;
4517
0
                    xmlBufCat(buf, BAD_CAST "*");
4518
0
    }
4519
0
            } else {
4520
0
                xmlBufCat(buf, cur->name);
4521
0
            }
4522
4523
            /*
4524
             * Thumbler index computation
4525
       * TODO: the occurrence test seems bogus for namespaced names
4526
             */
4527
0
            tmp = cur->prev;
4528
0
            while (tmp != NULL) {
4529
0
                if ((tmp->type == XML_ELEMENT_NODE) &&
4530
0
        (generic ||
4531
0
         (xmlStrEqual(cur->name, tmp->name) &&
4532
0
         ((tmp->ns == cur->ns) ||
4533
0
          ((tmp->ns != NULL) && (cur->ns != NULL) &&
4534
0
           (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4535
0
                    occur++;
4536
0
                tmp = tmp->prev;
4537
0
            }
4538
0
            if (occur == 0) {
4539
0
                tmp = cur->next;
4540
0
                while (tmp != NULL && occur == 0) {
4541
0
                    if ((tmp->type == XML_ELEMENT_NODE) &&
4542
0
      (generic ||
4543
0
       (xmlStrEqual(cur->name, tmp->name) &&
4544
0
       ((tmp->ns == cur->ns) ||
4545
0
        ((tmp->ns != NULL) && (cur->ns != NULL) &&
4546
0
         (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4547
0
                        occur++;
4548
0
                    tmp = tmp->next;
4549
0
                }
4550
0
                if (occur != 0)
4551
0
                    occur = 1;
4552
0
            } else
4553
0
                occur++;
4554
0
        } else if (cur->type == XML_COMMENT_NODE) {
4555
0
            xmlBufCat(buf, BAD_CAST "/comment()");
4556
4557
            /*
4558
             * Thumbler index computation
4559
             */
4560
0
            tmp = cur->prev;
4561
0
            while (tmp != NULL) {
4562
0
                if (tmp->type == XML_COMMENT_NODE)
4563
0
        occur++;
4564
0
                tmp = tmp->prev;
4565
0
            }
4566
0
            if (occur == 0) {
4567
0
                tmp = cur->next;
4568
0
                while (tmp != NULL && occur == 0) {
4569
0
        if (tmp->type == XML_COMMENT_NODE)
4570
0
            occur++;
4571
0
                    tmp = tmp->next;
4572
0
                }
4573
0
                if (occur != 0)
4574
0
                    occur = 1;
4575
0
            } else
4576
0
                occur++;
4577
0
        } else if ((cur->type == XML_TEXT_NODE) ||
4578
0
                   (cur->type == XML_CDATA_SECTION_NODE)) {
4579
0
            xmlBufCat(buf, BAD_CAST "/text()");
4580
4581
            /*
4582
             * Thumbler index computation
4583
             */
4584
0
            tmp = cur->prev;
4585
0
            while (tmp != NULL) {
4586
0
                if ((tmp->type == XML_TEXT_NODE) ||
4587
0
        (tmp->type == XML_CDATA_SECTION_NODE))
4588
0
        occur++;
4589
0
                tmp = tmp->prev;
4590
0
            }
4591
      /*
4592
      * Evaluate if this is the only text- or CDATA-section-node;
4593
      * if yes, then we'll get "text()", otherwise "text()[1]".
4594
      */
4595
0
            if (occur == 0) {
4596
0
                tmp = cur->next;
4597
0
                while (tmp != NULL) {
4598
0
        if ((tmp->type == XML_TEXT_NODE) ||
4599
0
      (tmp->type == XML_CDATA_SECTION_NODE))
4600
0
        {
4601
0
      occur = 1;
4602
0
      break;
4603
0
        }
4604
0
        tmp = tmp->next;
4605
0
    }
4606
0
            } else
4607
0
                occur++;
4608
0
        } else if (cur->type == XML_PI_NODE) {
4609
0
            xmlBufCat(buf, BAD_CAST "/processing-instruction('");
4610
0
            xmlBufCat(buf, cur->name);
4611
0
            xmlBufCat(buf, BAD_CAST "')");
4612
4613
            /*
4614
             * Thumbler index computation
4615
             */
4616
0
            tmp = cur->prev;
4617
0
            while (tmp != NULL) {
4618
0
                if ((tmp->type == XML_PI_NODE) &&
4619
0
        (xmlStrEqual(cur->name, tmp->name)))
4620
0
                    occur++;
4621
0
                tmp = tmp->prev;
4622
0
            }
4623
0
            if (occur == 0) {
4624
0
                tmp = cur->next;
4625
0
                while (tmp != NULL && occur == 0) {
4626
0
                    if ((tmp->type == XML_PI_NODE) &&
4627
0
      (xmlStrEqual(cur->name, tmp->name)))
4628
0
                        occur++;
4629
0
                    tmp = tmp->next;
4630
0
                }
4631
0
                if (occur != 0)
4632
0
                    occur = 1;
4633
0
            } else
4634
0
                occur++;
4635
4636
0
        } else if (cur->type == XML_ATTRIBUTE_NODE) {
4637
0
            xmlBufCat(buf, BAD_CAST "/@");
4638
0
            if (cur->ns && cur->ns->prefix != NULL) {
4639
0
                xmlBufCat(buf, cur->ns->prefix);
4640
0
                xmlBufCat(buf, BAD_CAST ":");
4641
0
            }
4642
0
            xmlBufCat(buf, cur->name);
4643
0
        } else {
4644
0
            goto error;
4645
0
        }
4646
4647
0
        if (occur > 0) {
4648
0
            char tmpbuf[30];
4649
4650
0
            snprintf(tmpbuf, sizeof(tmpbuf), "[%d]", occur);
4651
0
            xmlBufCat(buf, BAD_CAST tmpbuf);
4652
0
        }
4653
0
    }
4654
4655
0
    ret = xmlBufDetach(buf);
4656
4657
0
error:
4658
0
    xmlBufFree(buf);
4659
0
    xmlFree(nodes);
4660
0
    return(ret);
4661
0
}
4662
4663
/**
4664
 * Get the root element of the document.
4665
 *
4666
 * Searches the document's children for the root element. The first
4667
 * child is not necessarily the root element, but could also be a
4668
 * DTD, comment or PI.
4669
 *
4670
 * @param doc  the document
4671
 * @returns the root element or NULL if no element was found.
4672
 */
4673
xmlNode *
4674
0
xmlDocGetRootElement(const xmlDoc *doc) {
4675
0
    xmlNodePtr ret;
4676
4677
0
    if (doc == NULL) return(NULL);
4678
0
    ret = doc->children;
4679
0
    while (ret != NULL) {
4680
0
  if (ret->type == XML_ELEMENT_NODE)
4681
0
      return(ret);
4682
0
        ret = ret->next;
4683
0
    }
4684
0
    return(ret);
4685
0
}
4686
4687
/**
4688
 * If `root` is NULL no action is taken. To remove a node from a
4689
 * document, use #xmlUnlinkNode instead.
4690
 *
4691
 * Set the root element of the document (`doc->children` is a list
4692
 * containing possibly comments, PIs, etc ...).
4693
 *
4694
 * `root` must be an element node. It is unlinked before insertion.
4695
 *
4696
 * @param doc  the document
4697
 * @param root  the new document root element
4698
 * @returns the unlinked old root element or NULL if the document
4699
 * didn't have a root element or a memory allocation failed.
4700
 */
4701
xmlNode *
4702
0
xmlDocSetRootElement(xmlDoc *doc, xmlNode *root) {
4703
0
    xmlNodePtr old = NULL;
4704
4705
0
    if (doc == NULL) return(NULL);
4706
0
    if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4707
0
  return(NULL);
4708
0
    old = doc->children;
4709
0
    while (old != NULL) {
4710
0
  if (old->type == XML_ELEMENT_NODE)
4711
0
      break;
4712
0
        old = old->next;
4713
0
    }
4714
0
    if (old == root)
4715
0
        return(old);
4716
0
    xmlUnlinkNodeInternal(root);
4717
0
    if (xmlSetTreeDoc(root, doc) < 0)
4718
0
        return(NULL);
4719
0
    root->parent = (xmlNodePtr) doc;
4720
0
    if (old == NULL) {
4721
0
  if (doc->children == NULL) {
4722
0
      doc->children = root;
4723
0
      doc->last = root;
4724
0
  } else {
4725
0
      xmlAddSibling(doc->children, root);
4726
0
  }
4727
0
    } else {
4728
0
  xmlReplaceNode(old, root);
4729
0
    }
4730
0
    return(old);
4731
0
}
4732
4733
/**
4734
 * Set the `xml:lang` attribute of a node.
4735
 *
4736
 * @param cur  the node being changed
4737
 * @param lang  the language description
4738
 * @returns 0 on success, 1 if arguments are invalid, -1 if a
4739
 * memory allocation failed.
4740
 */
4741
int
4742
0
xmlNodeSetLang(xmlNode *cur, const xmlChar *lang) {
4743
0
    xmlNsPtr ns;
4744
0
    xmlAttrPtr attr;
4745
0
    int res;
4746
4747
0
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
4748
0
        return(1);
4749
4750
0
    res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns);
4751
0
    if (res != 0)
4752
0
        return(res);
4753
0
    attr = xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4754
0
    if (attr == NULL)
4755
0
        return(-1);
4756
4757
0
    return(0);
4758
0
}
4759
4760
/**
4761
 * Find the `xml:lang` of a node.
4762
 *
4763
 * Look up the value of the `xml:lang` attribute or the one carried
4764
 * by the nearest ancestor.
4765
 *
4766
 * @param cur  the node being checked
4767
 * @returns a pointer to the lang value, or NULL if not found
4768
 *     It's up to the caller to free the memory with #xmlFree.
4769
 */
4770
xmlChar *
4771
0
xmlNodeGetLang(const xmlNode *cur) {
4772
0
    xmlChar *lang;
4773
0
    int res;
4774
4775
0
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
4776
0
        return(NULL);
4777
4778
0
    while (cur != NULL) {
4779
0
        res = xmlNodeGetAttrValue(cur, BAD_CAST "lang", XML_XML_NAMESPACE,
4780
0
                                  &lang);
4781
0
        if (res < 0)
4782
0
            return(NULL);
4783
0
  if (lang != NULL)
4784
0
      return(lang);
4785
4786
0
  cur = cur->parent;
4787
0
    }
4788
4789
0
    return(NULL);
4790
0
}
4791
4792
4793
/**
4794
 * Set the `xml:space` attribute of a node.
4795
 *
4796
 * @param cur  the node being changed
4797
 * @param val  the xml:space value ("0": default, 1: "preserve")
4798
 * @returns 0 on success, 1 if arguments are invalid, -1 if a
4799
 * memory allocation failed.
4800
 */
4801
int
4802
0
xmlNodeSetSpacePreserve(xmlNode *cur, int val) {
4803
0
    xmlNsPtr ns;
4804
0
    xmlAttrPtr attr;
4805
0
    const char *string;
4806
0
    int res;
4807
4808
0
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
4809
0
        return(1);
4810
4811
0
    res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns);
4812
0
    if (res != 0)
4813
0
  return(res);
4814
4815
0
    if (val == 0)
4816
0
        string = "default";
4817
0
    else
4818
0
        string = "preserve";
4819
4820
0
    attr = xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST string);
4821
0
    if (attr == NULL)
4822
0
        return(-1);
4823
4824
0
    return(0);
4825
0
}
4826
4827
/**
4828
 * Find the `xml:space` of a node.
4829
 *
4830
 * Look up the value of the `xml:space` attribute or the one carried
4831
 * by the nearest ancestor.
4832
 *
4833
 * @param cur  the node being checked
4834
 * @returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
4835
 */
4836
int
4837
0
xmlNodeGetSpacePreserve(const xmlNode *cur) {
4838
0
    xmlChar *space;
4839
0
        int res;
4840
4841
0
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
4842
0
        return(-1);
4843
4844
0
    while (cur != NULL) {
4845
0
  res = xmlNodeGetAttrValue(cur, BAD_CAST "space", XML_XML_NAMESPACE,
4846
0
                                  &space);
4847
0
        if (res < 0)
4848
0
            return(-1);
4849
0
  if (space != NULL) {
4850
0
      if (xmlStrEqual(space, BAD_CAST "preserve")) {
4851
0
    xmlFree(space);
4852
0
    return(1);
4853
0
      }
4854
0
      if (xmlStrEqual(space, BAD_CAST "default")) {
4855
0
    xmlFree(space);
4856
0
    return(0);
4857
0
      }
4858
0
      xmlFree(space);
4859
0
  }
4860
4861
0
  cur = cur->parent;
4862
0
    }
4863
4864
0
    return(-1);
4865
0
}
4866
4867
/**
4868
 * Set (or reset) the name of a node.
4869
 *
4870
 * @param cur  the node being changed
4871
 * @param name  the new tag name
4872
 */
4873
void
4874
0
xmlNodeSetName(xmlNode *cur, const xmlChar *name) {
4875
0
    xmlDocPtr doc;
4876
0
    xmlDictPtr dict;
4877
0
    const xmlChar *copy;
4878
0
    const xmlChar *oldName;
4879
4880
0
    if (cur == NULL) return;
4881
0
    if (name == NULL) return;
4882
0
    switch(cur->type) {
4883
0
        case XML_ELEMENT_NODE:
4884
0
        case XML_ATTRIBUTE_NODE:
4885
0
        case XML_PI_NODE:
4886
0
        case XML_ENTITY_REF_NODE:
4887
0
      break;
4888
0
        default:
4889
0
            return;
4890
0
    }
4891
4892
0
    doc = cur->doc;
4893
0
    if (doc != NULL)
4894
0
  dict = doc->dict;
4895
0
    else
4896
0
        dict = NULL;
4897
4898
0
    if (dict != NULL)
4899
0
        copy = xmlDictLookup(dict, name, -1);
4900
0
    else
4901
0
        copy = xmlStrdup(name);
4902
0
    if (copy == NULL)
4903
0
        return;
4904
4905
0
    oldName = cur->name;
4906
0
    cur->name = copy;
4907
0
    if ((oldName != NULL) &&
4908
0
        ((dict == NULL) || (!xmlDictOwns(dict, oldName))))
4909
0
        xmlFree((xmlChar *) oldName);
4910
0
}
4911
4912
/**
4913
 * Set (or reset) the base URI of a node, i.e. the value of the
4914
 * `xml:base` attribute.
4915
 *
4916
 * @param cur  the node being changed
4917
 * @param uri  the new base URI
4918
 * @returns 0 on success, -1 on error.
4919
 */
4920
int
4921
0
xmlNodeSetBase(xmlNode *cur, const xmlChar* uri) {
4922
0
    xmlNsPtr ns;
4923
0
    xmlChar* fixed;
4924
4925
0
    if (cur == NULL)
4926
0
        return(-1);
4927
0
    switch(cur->type) {
4928
0
        case XML_ELEMENT_NODE:
4929
0
        case XML_ATTRIBUTE_NODE:
4930
0
      break;
4931
0
        case XML_DOCUMENT_NODE:
4932
0
        case XML_HTML_DOCUMENT_NODE: {
4933
0
      xmlDocPtr doc = (xmlDocPtr) cur;
4934
4935
0
      if (doc->URL != NULL)
4936
0
    xmlFree(doc->URL);
4937
0
      if (uri == NULL) {
4938
0
    doc->URL = NULL;
4939
0
            } else {
4940
0
    doc->URL = xmlPathToURI(uri);
4941
0
                if (doc->URL == NULL)
4942
0
                    return(-1);
4943
0
            }
4944
0
      return(0);
4945
0
  }
4946
0
        default:
4947
0
      return(-1);
4948
0
    }
4949
4950
0
    xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns);
4951
0
    if (ns == NULL)
4952
0
  return(-1);
4953
0
    fixed = xmlPathToURI(uri);
4954
0
    if (fixed == NULL)
4955
0
        return(-1);
4956
0
    if (xmlSetNsProp(cur, ns, BAD_CAST "base", fixed) == NULL) {
4957
0
        xmlFree(fixed);
4958
0
        return(-1);
4959
0
    }
4960
0
    xmlFree(fixed);
4961
4962
0
    return(0);
4963
0
}
4964
4965
/**
4966
 * Searches for the base URI. The code should work on both XML
4967
 * and HTML document even if base mechanisms are completely different.
4968
 * It returns the base as defined in RFC 2396 sections "5.1.1. Base
4969
 * URI within Document Content" and "5.1.2. Base URI from the
4970
 * Encapsulating Entity". However it does not return the document base
4971
 * (5.1.3), use `doc->URL` in this case.
4972
 *
4973
 * @since 2.13.0
4974
 *
4975
 * @param doc  the document the node pertains to
4976
 * @param cur  the node being checked
4977
 * @param baseOut  pointer to base
4978
 * @returns 0 in case of success, 1 if a URI or argument is invalid, -1 if a
4979
 * memory allocation failed.
4980
 */
4981
int
4982
0
xmlNodeGetBaseSafe(const xmlDoc *doc, const xmlNode *cur, xmlChar **baseOut) {
4983
0
    xmlChar *ret = NULL;
4984
0
    xmlChar *base, *newbase;
4985
0
    int res;
4986
4987
0
    if (baseOut == NULL)
4988
0
        return(1);
4989
0
    *baseOut = NULL;
4990
0
    if ((cur == NULL) && (doc == NULL))
4991
0
        return(1);
4992
0
    if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
4993
0
        return(1);
4994
0
    if (doc == NULL)
4995
0
        doc = cur->doc;
4996
4997
0
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4998
0
        cur = doc->children;
4999
0
  while ((cur != NULL) && (cur->name != NULL)) {
5000
0
      if (cur->type != XML_ELEMENT_NODE) {
5001
0
          cur = cur->next;
5002
0
    continue;
5003
0
      }
5004
0
      if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5005
0
          cur = cur->children;
5006
0
    continue;
5007
0
      }
5008
0
      if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5009
0
          cur = cur->children;
5010
0
    continue;
5011
0
      }
5012
0
      if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5013
0
                if (xmlNodeGetAttrValue(cur, BAD_CAST "href", NULL, &ret) < 0)
5014
0
                    return(-1);
5015
0
                if (ret == NULL)
5016
0
                    return(1);
5017
0
                goto found;
5018
0
      }
5019
0
      cur = cur->next;
5020
0
  }
5021
0
  return(0);
5022
0
    }
5023
5024
0
    while (cur != NULL) {
5025
0
  if (cur->type == XML_ENTITY_DECL) {
5026
0
      xmlEntityPtr ent = (xmlEntityPtr) cur;
5027
5028
0
            if (ent->URI == NULL)
5029
0
                break;
5030
0
            xmlFree(ret);
5031
0
      ret = xmlStrdup(ent->URI);
5032
0
            if (ret == NULL)
5033
0
                return(-1);
5034
0
            goto found;
5035
0
  }
5036
0
  if (cur->type == XML_ELEMENT_NODE) {
5037
0
      if (xmlNodeGetAttrValue(cur, BAD_CAST "base", XML_XML_NAMESPACE,
5038
0
                                    &base) < 0) {
5039
0
                xmlFree(ret);
5040
0
                return(-1);
5041
0
            }
5042
0
      if (base != NULL) {
5043
0
    if (ret != NULL) {
5044
0
        res = xmlBuildURISafe(ret, base, &newbase);
5045
0
                    xmlFree(ret);
5046
0
                    xmlFree(base);
5047
0
                    if (res != 0)
5048
0
                        return(res);
5049
0
        ret = newbase;
5050
0
    } else {
5051
0
        ret = base;
5052
0
    }
5053
0
    if ((!xmlStrncmp(ret, BAD_CAST "http://", 7)) ||
5054
0
        (!xmlStrncmp(ret, BAD_CAST "ftp://", 6)) ||
5055
0
        (!xmlStrncmp(ret, BAD_CAST "urn:", 4)))
5056
0
                    goto found;
5057
0
      }
5058
0
  }
5059
0
  cur = cur->parent;
5060
0
    }
5061
5062
0
    if ((doc != NULL) && (doc->URL != NULL)) {
5063
0
  if (ret == NULL) {
5064
0
      ret = xmlStrdup(doc->URL);
5065
0
            if (ret == NULL)
5066
0
                return(-1);
5067
0
        } else {
5068
0
            res = xmlBuildURISafe(ret, doc->URL, &newbase);
5069
0
            xmlFree(ret);
5070
0
            if (res != 0)
5071
0
                return(res);
5072
0
            ret = newbase;
5073
0
        }
5074
0
    }
5075
5076
0
found:
5077
0
    *baseOut = ret;
5078
0
    return(0);
5079
0
}
5080
5081
/**
5082
 * See #xmlNodeGetBaseSafe. This function doesn't allow to distinguish
5083
 * memory allocation failures from a non-existing base.
5084
 *
5085
 * @param doc  the document the node pertains to
5086
 * @param cur  the node being checked
5087
 * @returns a pointer to the base URL, or NULL if not found
5088
 *     It's up to the caller to free the memory with #xmlFree.
5089
 */
5090
xmlChar *
5091
0
xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
5092
0
    xmlChar *base;
5093
5094
0
    xmlNodeGetBaseSafe(doc, cur, &base);
5095
0
    return(base);
5096
0
}
5097
5098
/**
5099
 * Append the string value of a node to `buffer`. For text nodes,
5100
 * the string value is the text content. Otherwise, the string value
5101
 * is the concatenation of the string values of the node's
5102
 * descendants.
5103
 *
5104
 * Entity references are substituted.
5105
 *
5106
 * @param buffer  a buffer
5107
 * @param cur  the node being read
5108
 * @returns 0 in case of success and -1 in case of error.
5109
 */
5110
int
5111
xmlNodeBufGetContent(xmlBuffer *buffer, const xmlNode *cur)
5112
0
{
5113
0
    xmlBufPtr buf;
5114
0
    int ret1, ret2;
5115
5116
0
    if ((cur == NULL) || (buffer == NULL)) return(-1);
5117
0
    buf = xmlBufFromBuffer(buffer);
5118
0
    ret1 = xmlBufGetNodeContent(buf, cur);
5119
0
    ret2 = xmlBufBackToBuffer(buf, buffer);
5120
0
    if ((ret1 < 0) || (ret2 < 0))
5121
0
        return(-1);
5122
0
    return(0);
5123
0
}
5124
5125
static void
5126
0
xmlBufGetEntityRefContent(xmlBufPtr buf, const xmlNode *ref) {
5127
0
    xmlEntityPtr ent;
5128
5129
0
    if (ref->children != NULL) {
5130
0
        ent = (xmlEntityPtr) ref->children;
5131
0
    } else {
5132
        /* lookup entity declaration */
5133
0
        ent = xmlGetDocEntity(ref->doc, ref->name);
5134
0
        if (ent == NULL)
5135
0
            return;
5136
0
    }
5137
5138
    /*
5139
     * The parser should always expand predefined entities but it's
5140
     * possible to create references to predefined entities using
5141
     * the tree API.
5142
     */
5143
0
    if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
5144
0
        xmlBufCat(buf, ent->content);
5145
0
        return;
5146
0
    }
5147
5148
0
    if (ent->flags & XML_ENT_EXPANDING)
5149
0
        return;
5150
5151
0
    ent->flags |= XML_ENT_EXPANDING;
5152
0
    xmlBufGetChildContent(buf, (xmlNodePtr) ent);
5153
0
    ent->flags &= ~XML_ENT_EXPANDING;
5154
0
}
5155
5156
static void
5157
0
xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree) {
5158
0
    const xmlNode *cur = tree->children;
5159
5160
0
    while (cur != NULL) {
5161
0
        switch (cur->type) {
5162
0
            case XML_TEXT_NODE:
5163
0
            case XML_CDATA_SECTION_NODE:
5164
0
                xmlBufCat(buf, cur->content);
5165
0
                break;
5166
5167
0
            case XML_ENTITY_REF_NODE:
5168
0
                xmlBufGetEntityRefContent(buf, cur);
5169
0
                break;
5170
5171
0
            default:
5172
0
                if (cur->children != NULL) {
5173
0
                    cur = cur->children;
5174
0
                    continue;
5175
0
                }
5176
0
                break;
5177
0
        }
5178
5179
0
        while (cur->next == NULL) {
5180
0
            cur = cur->parent;
5181
0
            if (cur == tree)
5182
0
                return;
5183
0
        }
5184
0
        cur = cur->next;
5185
0
    }
5186
0
}
5187
5188
/**
5189
 * Append the string value of a node to `buf`. For text nodes,
5190
 * the string value is the text content. Otherwise, the string value
5191
 * is the concatenation of the string values of the node's
5192
 * descendants.
5193
 *
5194
 * Entity references are substituted.
5195
 *
5196
 * @param buf  a buffer xmlBuf
5197
 * @param cur  the node being read
5198
 * @returns 0 in case of success and -1 in case of error.
5199
 */
5200
int
5201
xmlBufGetNodeContent(xmlBuf *buf, const xmlNode *cur)
5202
0
{
5203
0
    if ((cur == NULL) || (buf == NULL))
5204
0
        return(-1);
5205
5206
0
    switch (cur->type) {
5207
0
        case XML_DOCUMENT_NODE:
5208
0
        case XML_HTML_DOCUMENT_NODE:
5209
0
        case XML_DOCUMENT_FRAG_NODE:
5210
0
        case XML_ELEMENT_NODE:
5211
0
        case XML_ATTRIBUTE_NODE:
5212
0
        case XML_ENTITY_DECL:
5213
0
            xmlBufGetChildContent(buf, cur);
5214
0
            break;
5215
5216
0
        case XML_CDATA_SECTION_NODE:
5217
0
        case XML_TEXT_NODE:
5218
0
        case XML_COMMENT_NODE:
5219
0
        case XML_PI_NODE:
5220
0
      xmlBufCat(buf, cur->content);
5221
0
            break;
5222
5223
0
        case XML_ENTITY_REF_NODE:
5224
0
            xmlBufGetEntityRefContent(buf, cur);
5225
0
            break;
5226
5227
0
        case XML_NAMESPACE_DECL:
5228
0
      xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5229
0
      break;
5230
5231
0
        default:
5232
0
            break;
5233
0
    }
5234
5235
0
    return(0);
5236
0
}
5237
5238
/**
5239
 * Returns the string value of a node. For text nodes, the string
5240
 * value is the text content. Otherwise, the string value is the
5241
 * concatenation of the string values of the node's descendants.
5242
 *
5243
 * Entity references are substituted.
5244
 *
5245
 * It's up to the caller to free the result with #xmlFree.
5246
 *
5247
 * @param cur  the node being read
5248
 * @returns a new string or NULL if arguments are invalid or a
5249
 * memory allocation failed.
5250
 */
5251
xmlChar *
5252
xmlNodeGetContent(const xmlNode *cur)
5253
339
{
5254
339
    xmlBufPtr buf;
5255
339
    xmlChar *ret;
5256
5257
339
    if (cur == NULL)
5258
0
        return (NULL);
5259
5260
339
    switch (cur->type) {
5261
0
        case XML_DOCUMENT_NODE:
5262
0
        case XML_HTML_DOCUMENT_NODE:
5263
0
        case XML_ENTITY_REF_NODE:
5264
0
            break;
5265
5266
0
        case XML_DOCUMENT_FRAG_NODE:
5267
0
        case XML_ELEMENT_NODE:
5268
339
        case XML_ATTRIBUTE_NODE:
5269
339
        case XML_ENTITY_DECL: {
5270
339
            xmlNodePtr children = cur->children;
5271
5272
339
            if (children == NULL)
5273
0
                return(xmlStrdup(BAD_CAST ""));
5274
5275
            /* Optimization for single text children */
5276
339
            if (((children->type == XML_TEXT_NODE) ||
5277
0
                 (children->type == XML_CDATA_SECTION_NODE)) &&
5278
339
                (children->next == NULL)) {
5279
339
                if (children->content == NULL)
5280
0
                    return(xmlStrdup(BAD_CAST ""));
5281
339
                return(xmlStrdup(children->content));
5282
339
            }
5283
5284
0
            break;
5285
339
        }
5286
5287
0
        case XML_CDATA_SECTION_NODE:
5288
0
        case XML_TEXT_NODE:
5289
0
        case XML_COMMENT_NODE:
5290
0
        case XML_PI_NODE:
5291
0
            if (cur->content != NULL)
5292
0
                return(xmlStrdup(cur->content));
5293
0
            else
5294
0
                return(xmlStrdup(BAD_CAST ""));
5295
5296
0
        case XML_NAMESPACE_DECL:
5297
0
      return(xmlStrdup(((xmlNsPtr) cur)->href));
5298
5299
0
        default:
5300
0
            return(NULL);
5301
339
    }
5302
5303
0
    buf = xmlBufCreate(50);
5304
0
    if (buf == NULL)
5305
0
        return (NULL);
5306
0
    xmlBufGetNodeContent(buf, cur);
5307
0
    ret = xmlBufDetach(buf);
5308
0
    xmlBufFree(buf);
5309
5310
0
    return(ret);
5311
0
}
5312
5313
static int
5314
0
xmlNodeSetContentInternal(xmlNodePtr cur, const xmlChar *content, int len) {
5315
0
    if (cur == NULL) {
5316
0
  return(1);
5317
0
    }
5318
0
    switch (cur->type) {
5319
0
        case XML_DOCUMENT_FRAG_NODE:
5320
0
        case XML_ELEMENT_NODE:
5321
0
        case XML_ATTRIBUTE_NODE: {
5322
0
            size_t maxSize = len < 0 ? SIZE_MAX : (size_t) len;
5323
5324
            /*
5325
             * We shouldn't parse the content as attribute value here,
5326
             * but the API can't be changed.
5327
             */
5328
0
            if (xmlNodeParseAttValue(cur->doc, (xmlAttr *) cur,
5329
0
                                     content, maxSize, NULL) < 0)
5330
0
                return(-1);
5331
0
      break;
5332
0
        }
5333
5334
0
        case XML_TEXT_NODE:
5335
0
        case XML_CDATA_SECTION_NODE:
5336
0
        case XML_PI_NODE:
5337
0
        case XML_COMMENT_NODE: {
5338
0
            xmlChar *copy = NULL;
5339
5340
0
      if (content != NULL) {
5341
0
                if (len < 0)
5342
0
                    copy = xmlStrdup(content);
5343
0
                else
5344
0
        copy = xmlStrndup(content, len);
5345
0
                if (copy == NULL)
5346
0
                    return(-1);
5347
0
      }
5348
5349
0
            xmlTextSetContent(cur, copy);
5350
0
      break;
5351
0
        }
5352
5353
0
        default:
5354
0
            break;
5355
0
    }
5356
5357
0
    return(0);
5358
0
}
5359
5360
/**
5361
 * Replace the text content of a node.
5362
 *
5363
 * Sets the raw text content of text, CDATA, comment or PI nodes.
5364
 *
5365
 * For element and attribute nodes, removes all children and
5366
 * replaces them by parsing `content` which is expected to be a
5367
 * valid XML attribute value possibly containing character and
5368
 * entity references. Syntax errors and references to undeclared
5369
 * entities are ignored silently. Unfortunately, there isn't an
5370
 * API to pass raw content directly. An inefficient work-around
5371
 * is to escape the content with #xmlEncodeSpecialChars before
5372
 * passing it. A better trick is clearing the old content
5373
 * with `xmlNodeSetContent(node, NULL)` first and then calling
5374
 * `xmlNodeAddContent(node, content)`. Unlike this function,
5375
 * #xmlNodeAddContent accepts raw text.
5376
 *
5377
 * @param cur  the node being modified
5378
 * @param content  the new value of the content
5379
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5380
 */
5381
int
5382
0
xmlNodeSetContent(xmlNode *cur, const xmlChar *content) {
5383
0
    return(xmlNodeSetContentInternal(cur, content, -1));
5384
0
}
5385
5386
/**
5387
 * See #xmlNodeSetContent.
5388
 *
5389
 * @param cur  the node being modified
5390
 * @param content  the new value of the content
5391
 * @param len  the size of `content`
5392
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5393
 */
5394
int
5395
0
xmlNodeSetContentLen(xmlNode *cur, const xmlChar *content, int len) {
5396
0
    return(xmlNodeSetContentInternal(cur, content, len));
5397
0
}
5398
5399
/**
5400
 * Append the extra substring to the node content.
5401
 *
5402
 * NOTE: In contrast to #xmlNodeSetContentLen, `content` is supposed
5403
 * to be raw text, so unescaped XML special chars are allowed, entity
5404
 * references are not supported.
5405
 *
5406
 * This doesn't work on attributes before version 2.15.
5407
 *
5408
 * @param cur  the node being modified
5409
 * @param content  extra content
5410
 * @param len  the size of `content`
5411
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5412
 */
5413
int
5414
0
xmlNodeAddContentLen(xmlNode *cur, const xmlChar *content, int len) {
5415
0
    if (cur == NULL)
5416
0
  return(1);
5417
0
    if ((content == NULL) || (len <= 0))
5418
0
        return(0);
5419
5420
0
    switch (cur->type) {
5421
0
        case XML_DOCUMENT_FRAG_NODE:
5422
0
        case XML_ELEMENT_NODE:
5423
0
        case XML_ATTRIBUTE_NODE: {
5424
0
      xmlNodePtr newNode, tmp;
5425
5426
0
      newNode = xmlNewDocTextLen(cur->doc, content, len);
5427
0
      if (newNode == NULL)
5428
0
                return(-1);
5429
0
            tmp = xmlAddChild(cur, newNode);
5430
0
            if (tmp == NULL) {
5431
0
                xmlFreeNode(newNode);
5432
0
                return(-1);
5433
0
            }
5434
0
      break;
5435
0
  }
5436
0
      break;
5437
0
        case XML_TEXT_NODE:
5438
0
        case XML_CDATA_SECTION_NODE:
5439
0
        case XML_PI_NODE:
5440
0
        case XML_COMMENT_NODE:
5441
0
            return(xmlTextAddContent(cur, content, len));
5442
0
        default:
5443
0
            break;
5444
0
    }
5445
5446
0
    return(0);
5447
0
}
5448
5449
/**
5450
 * Append the extra substring to the node content.
5451
 *
5452
 * NOTE: In contrast to #xmlNodeSetContent, `content` is supposed
5453
 * to be raw text, so unescaped XML special chars are allowed, entity
5454
 * references are not supported.
5455
 *
5456
 * This doesn't work on attributes before version 2.15.
5457
 *
5458
 * @param cur  the node being modified
5459
 * @param content  extra content
5460
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5461
 */
5462
int
5463
0
xmlNodeAddContent(xmlNode *cur, const xmlChar *content) {
5464
0
    return(xmlNodeAddContentLen(cur, content, xmlStrlen(content)));
5465
0
}
5466
5467
/**
5468
 * Merge the second text node into the first. If `first` is NULL,
5469
 * `second` is returned. Otherwise, the second node is unlinked and
5470
 * freed.
5471
 *
5472
 * @param first  the first text node
5473
 * @param second  the second text node being merged
5474
 * @returns the first text node augmented or NULL in case of error.
5475
 */
5476
xmlNode *
5477
0
xmlTextMerge(xmlNode *first, xmlNode *second) {
5478
0
    if (first == NULL)
5479
0
        return(second);
5480
0
    if (second == NULL)
5481
0
        return(first);
5482
5483
0
    if ((first->type != XML_TEXT_NODE) ||
5484
0
        (second->type != XML_TEXT_NODE) ||
5485
0
        (first == second) ||
5486
0
        (first->name != second->name))
5487
0
  return(NULL);
5488
5489
0
    if (xmlTextAddContent(first, second->content, -1) < 0)
5490
0
        return(NULL);
5491
5492
0
    xmlUnlinkNodeInternal(second);
5493
0
    xmlFreeNode(second);
5494
0
    return(first);
5495
0
}
5496
5497
/**
5498
 * Find all in-scope namespaces of a node. `out` returns a NULL
5499
 * terminated array of namespace pointers that must be freed by
5500
 * the caller.
5501
 *
5502
 * @since 2.13.0
5503
 *
5504
 * @param doc  the document
5505
 * @param node  the current node
5506
 * @param out  the returned namespace array
5507
 * @returns 0 on success, 1 if no namespaces were found, -1 if a
5508
 * memory allocation failed.
5509
 */
5510
int
5511
xmlGetNsListSafe(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node,
5512
                 xmlNs ***out)
5513
0
{
5514
0
    xmlNsPtr cur;
5515
0
    xmlNsPtr *namespaces = NULL;
5516
0
    int nbns = 0;
5517
0
    int maxns = 0;
5518
0
    int i;
5519
5520
0
    if (out == NULL)
5521
0
        return(1);
5522
0
    *out = NULL;
5523
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5524
0
        return(1);
5525
5526
0
    while (node != NULL) {
5527
0
        if (node->type == XML_ELEMENT_NODE) {
5528
0
            cur = node->nsDef;
5529
0
            while (cur != NULL) {
5530
0
                for (i = 0; i < nbns; i++) {
5531
0
                    if ((cur->prefix == namespaces[i]->prefix) ||
5532
0
                        (xmlStrEqual(cur->prefix, namespaces[i]->prefix)))
5533
0
                        break;
5534
0
                }
5535
0
                if (i >= nbns) {
5536
0
                    if (nbns >= maxns) {
5537
0
                        xmlNsPtr *tmp;
5538
0
                        int newSize;
5539
5540
0
                        newSize = xmlGrowCapacity(maxns, sizeof(tmp[0]),
5541
0
                                                  10, XML_MAX_ITEMS);
5542
0
                        if (newSize < 0) {
5543
0
                            xmlFree(namespaces);
5544
0
                            return(-1);
5545
0
                        }
5546
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
5547
0
                        if (newSize < 2)
5548
0
                            newSize = 2;
5549
0
#endif
5550
0
                        tmp = xmlRealloc(namespaces,
5551
0
                                         (newSize + 1) * sizeof(tmp[0]));
5552
0
                        if (tmp == NULL) {
5553
0
                            xmlFree(namespaces);
5554
0
                            return(-1);
5555
0
                        }
5556
0
                        namespaces = tmp;
5557
0
                        maxns = newSize;
5558
0
                    }
5559
0
                    namespaces[nbns++] = cur;
5560
0
                    namespaces[nbns] = NULL;
5561
0
                }
5562
5563
0
                cur = cur->next;
5564
0
            }
5565
0
        }
5566
0
        node = node->parent;
5567
0
    }
5568
5569
0
    *out = namespaces;
5570
0
    return((namespaces == NULL) ? 1 : 0);
5571
0
}
5572
5573
/**
5574
 * Find all in-scope namespaces of a node.
5575
 *
5576
 * Use #xmlGetNsListSafe for better error reporting.
5577
 *
5578
 * @param doc  the document
5579
 * @param node  the current node
5580
 * @returns a NULL terminated array of namespace pointers that must
5581
 * be freed by the caller or NULL if no namespaces were found or
5582
 * a memory allocation failed.
5583
 */
5584
xmlNs **
5585
xmlGetNsList(const xmlDoc *doc, const xmlNode *node)
5586
0
{
5587
0
    xmlNsPtr *ret;
5588
5589
0
    xmlGetNsListSafe(doc, node, &ret);
5590
0
    return(ret);
5591
0
}
5592
5593
static xmlNsPtr
5594
370
xmlNewXmlNs(void) {
5595
370
    xmlNsPtr ns;
5596
5597
370
    ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5598
370
    if (ns == NULL)
5599
0
        return(NULL);
5600
370
    memset(ns, 0, sizeof(xmlNs));
5601
370
    ns->type = XML_LOCAL_NAMESPACE;
5602
370
    ns->href = xmlStrdup(XML_XML_NAMESPACE);
5603
370
    if (ns->href == NULL) {
5604
0
        xmlFreeNs(ns);
5605
0
        return(NULL);
5606
0
    }
5607
370
    ns->prefix = xmlStrdup(BAD_CAST "xml");
5608
370
    if (ns->prefix == NULL) {
5609
0
        xmlFreeNs(ns);
5610
0
        return(NULL);
5611
0
    }
5612
5613
370
    return(ns);
5614
370
}
5615
5616
/*
5617
* xmlTreeEnsureXMLDecl:
5618
* @doc: the doc
5619
*
5620
* Ensures that there is an XML namespace declaration on the doc.
5621
*
5622
* Returns the XML ns-struct or NULL if a memory allocation failed.
5623
*/
5624
static xmlNsPtr
5625
xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5626
3.10k
{
5627
3.10k
    xmlNsPtr ns;
5628
5629
3.10k
    ns = doc->oldNs;
5630
3.10k
    if (ns != NULL)
5631
2.73k
  return (ns);
5632
5633
370
    ns = xmlNewXmlNs();
5634
370
    doc->oldNs = ns;
5635
5636
370
    return(ns);
5637
3.10k
}
5638
5639
/**
5640
 * Search for a namespace with `prefix` in scope of `node`.
5641
 *
5642
 * @param node  a node
5643
 * @param prefix  a namespace prefix
5644
 * @param out  pointer to resulting namespace
5645
 * @returns 0 on success, -1 if a memory allocation failed, 1 on
5646
 * other errors.
5647
 */
5648
int
5649
xmlSearchNsSafe(xmlNode *node, const xmlChar *prefix,
5650
4.66k
                xmlNs **out) {
5651
4.66k
    xmlNsPtr cur;
5652
4.66k
    xmlDocPtr doc;
5653
4.66k
    xmlNodePtr orig = node;
5654
4.66k
    xmlNodePtr parent;
5655
5656
4.66k
    if (out == NULL)
5657
0
        return(1);
5658
4.66k
    *out = NULL;
5659
4.66k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5660
0
        return(1);
5661
5662
4.66k
    doc = node->doc;
5663
5664
4.66k
    if ((doc != NULL) && (IS_STR_XML(prefix))) {
5665
3.10k
        cur = xmlTreeEnsureXMLDecl(doc);
5666
3.10k
        if (cur == NULL)
5667
0
            return(-1);
5668
3.10k
        *out = cur;
5669
3.10k
        return(0);
5670
3.10k
    }
5671
5672
1.55k
    while (node->type != XML_ELEMENT_NODE) {
5673
0
        node = node->parent;
5674
0
        if (node == NULL)
5675
0
            return(0);
5676
0
    }
5677
5678
1.55k
    parent = node;
5679
5680
3.10k
    while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
5681
3.10k
        cur = node->nsDef;
5682
3.24k
        while (cur != NULL) {
5683
1.35k
            if ((xmlStrEqual(cur->prefix, prefix)) &&
5684
1.20k
                (cur->href != NULL)) {
5685
1.20k
                *out = cur;
5686
1.20k
                return(0);
5687
1.20k
            }
5688
146
            cur = cur->next;
5689
146
        }
5690
1.89k
        if (orig != node) {
5691
948
            cur = node->ns;
5692
948
            if ((cur != NULL) &&
5693
498
                (xmlStrEqual(cur->prefix, prefix)) &&
5694
348
                (cur->href != NULL)) {
5695
348
                *out = cur;
5696
348
                return(0);
5697
348
            }
5698
948
        }
5699
5700
1.54k
  node = node->parent;
5701
1.54k
    }
5702
5703
    /*
5704
     * The XML-1.0 namespace is normally held on the document
5705
     * element. In this case exceptionally create it on the
5706
     * node element.
5707
     */
5708
0
    if ((doc == NULL) && (IS_STR_XML(prefix))) {
5709
0
        cur = xmlNewXmlNs();
5710
0
        if (cur == NULL)
5711
0
            return(-1);
5712
0
        cur->next = parent->nsDef;
5713
0
        parent->nsDef = cur;
5714
0
        *out = cur;
5715
0
    }
5716
5717
0
    return(0);
5718
0
}
5719
5720
/**
5721
 * Search for a namespace with `prefix` in scope of `node`.
5722
 *
5723
 * Recurse on the parents until it finds the defined namespace
5724
 * or return NULL otherwise.
5725
 *
5726
 * If `nameSpace` is NULL, the default namespace is looked up.
5727
 *
5728
 * Namespace search doesn't cross entity boundaries.
5729
 *
5730
 * @param doc  the document
5731
 * @param node  the current node
5732
 * @param nameSpace  the namespace prefix
5733
 * @returns the namespace pointer or NULL if no namespace was found or
5734
 * a memory allocation failed. Allocations can only fail if the `xml`
5735
 * namespace is queried.
5736
 */
5737
xmlNs *
5738
xmlSearchNs(xmlDoc *doc ATTRIBUTE_UNUSED, xmlNode *node,
5739
0
            const xmlChar *nameSpace) {
5740
0
    xmlNsPtr cur;
5741
5742
0
    xmlSearchNsSafe(node, nameSpace, &cur);
5743
0
    return(cur);
5744
0
}
5745
5746
/**
5747
 * Verify that the given namespace held on `ancestor` is still in scope
5748
 * on node.
5749
 *
5750
 * @param doc  the document
5751
 * @param node  the current node
5752
 * @param ancestor  the ancestor carrying the namespace
5753
 * @param prefix  the namespace prefix
5754
 * @returns 1 if true, 0 if false and -1 in case of error.
5755
 */
5756
static int
5757
xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5758
             xmlNodePtr ancestor, const xmlChar * prefix)
5759
0
{
5760
0
    xmlNsPtr tst;
5761
5762
0
    while ((node != NULL) && (node != ancestor)) {
5763
0
        if ((node->type == XML_ENTITY_REF_NODE) ||
5764
0
            (node->type == XML_ENTITY_DECL))
5765
0
            return (-1);
5766
0
        if (node->type == XML_ELEMENT_NODE) {
5767
0
            tst = node->nsDef;
5768
0
            while (tst != NULL) {
5769
0
                if ((tst->prefix == NULL)
5770
0
                    && (prefix == NULL))
5771
0
                    return (0);
5772
0
                if ((tst->prefix != NULL)
5773
0
                    && (prefix != NULL)
5774
0
                    && (xmlStrEqual(tst->prefix, prefix)))
5775
0
                    return (0);
5776
0
                tst = tst->next;
5777
0
            }
5778
0
        }
5779
0
        node = node->parent;
5780
0
    }
5781
0
    if (node != ancestor)
5782
0
        return (-1);
5783
0
    return (1);
5784
0
}
5785
5786
/**
5787
 * Search for a namespace matching `URI` in scope of `node`.
5788
 *
5789
 * @param node  a node
5790
 * @param href  a namespace URI
5791
 * @param out  pointer to resulting namespace
5792
 * @returns 0 on success, -1 if a memory allocation failed, 1 on
5793
 * other errors.
5794
 */
5795
int
5796
xmlSearchNsByHrefSafe(xmlNode *node, const xmlChar *href,
5797
0
                      xmlNs **out) {
5798
0
    xmlNsPtr cur;
5799
0
    xmlDocPtr doc;
5800
0
    xmlNodePtr orig = node;
5801
0
    xmlNodePtr parent;
5802
0
    int is_attr;
5803
5804
0
    if (out == NULL)
5805
0
        return(1);
5806
0
    *out = NULL;
5807
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5808
0
        return(1);
5809
5810
0
    doc = node->doc;
5811
5812
0
    if ((doc != NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) {
5813
0
        cur = xmlTreeEnsureXMLDecl(doc);
5814
0
        if (cur == NULL)
5815
0
            return(-1);
5816
0
        *out = cur;
5817
0
        return(0);
5818
0
    }
5819
5820
0
    is_attr = (node->type == XML_ATTRIBUTE_NODE);
5821
5822
0
    while (node->type != XML_ELEMENT_NODE) {
5823
0
        node = node->parent;
5824
0
        if (node == NULL)
5825
0
            return(0);
5826
0
    }
5827
5828
0
    parent = node;
5829
5830
0
    while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
5831
0
        cur = node->nsDef;
5832
0
        while (cur != NULL) {
5833
0
            if (xmlStrEqual(cur->href, href)) {
5834
0
                if (((!is_attr) || (cur->prefix != NULL)) &&
5835
0
                    (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) {
5836
0
                    *out = cur;
5837
0
                    return(0);
5838
0
                }
5839
0
            }
5840
0
            cur = cur->next;
5841
0
        }
5842
0
        if (orig != node) {
5843
0
            cur = node->ns;
5844
0
            if (cur != NULL) {
5845
0
                if (xmlStrEqual(cur->href, href)) {
5846
0
                    if (((!is_attr) || (cur->prefix != NULL)) &&
5847
0
                        (xmlNsInScope(doc, orig, node,
5848
0
                                      cur->prefix) == 1)) {
5849
0
                        *out = cur;
5850
0
                        return(0);
5851
0
                    }
5852
0
                }
5853
0
            }
5854
0
        }
5855
5856
0
        node = node->parent;
5857
0
    }
5858
5859
    /*
5860
     * The XML-1.0 namespace is normally held on the document
5861
     * element. In this case exceptionally create it on the
5862
     * node element.
5863
     */
5864
0
    if ((doc == NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) {
5865
0
        cur = xmlNewXmlNs();
5866
0
        if (cur == NULL)
5867
0
            return(-1);
5868
0
        cur->next = parent->nsDef;
5869
0
        parent->nsDef = cur;
5870
0
        *out = cur;
5871
0
    }
5872
5873
0
    return(0);
5874
0
}
5875
5876
/**
5877
 * Search for a namespace matching `URI` in scope of `node`.
5878
 *
5879
 * @param doc  the document
5880
 * @param node  the current node
5881
 * @param href  the namespace value
5882
 * @returns the namespace pointer or NULL if no namespace was found or
5883
 * a memory allocation failed. Allocations can only fail if the `xml`
5884
 * namespace is queried.
5885
 */
5886
xmlNs *
5887
xmlSearchNsByHref(xmlDoc *doc ATTRIBUTE_UNUSED, xmlNode *node,
5888
0
                  const xmlChar * href) {
5889
0
    xmlNsPtr cur;
5890
5891
0
    xmlSearchNsByHrefSafe(node, href, &cur);
5892
0
    return(cur);
5893
0
}
5894
5895
/**
5896
 * Fix up namespace declarations.
5897
 *
5898
 * This function tries to locate a namespace definition in a tree
5899
 * ancestors, or create a new namespace definition node similar to
5900
 * `ns` trying to reuse the same prefix. However if the given prefix is
5901
 * null (default namespace) or reused within the subtree defined by
5902
 * `tree` or on one of its ancestors then a new prefix is generated.
5903
 *
5904
 * @param tree  a node expected to hold the new namespace
5905
 * @param ns  the original namespace
5906
 * @returns the (new) namespace definition or NULL in case of error
5907
 */
5908
static xmlNsPtr
5909
0
xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns) {
5910
0
    xmlNsPtr def;
5911
0
    xmlChar prefix[50];
5912
0
    int counter = 1;
5913
0
    int res;
5914
5915
0
    if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
5916
0
  return(NULL);
5917
0
    }
5918
0
    if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
5919
0
  return(NULL);
5920
0
    }
5921
    /*
5922
     * Search an existing namespace definition inherited.
5923
     */
5924
0
    res = xmlSearchNsByHrefSafe(tree, ns->href, &def);
5925
0
    if (res < 0)
5926
0
        return(NULL);
5927
0
    if (def != NULL)
5928
0
        return(def);
5929
5930
    /*
5931
     * Find a close prefix which is not already in use.
5932
     * Let's strip namespace prefixes longer than 20 chars !
5933
     */
5934
0
    if (ns->prefix == NULL)
5935
0
  snprintf((char *) prefix, sizeof(prefix), "default");
5936
0
    else
5937
0
  snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
5938
5939
0
    res = xmlSearchNsSafe(tree, prefix, &def);
5940
0
    if (res < 0)
5941
0
        return(NULL);
5942
0
    while (def != NULL) {
5943
0
        if (counter > 1000) return(NULL);
5944
0
  if (ns->prefix == NULL)
5945
0
      snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
5946
0
  else
5947
0
      snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5948
0
    (char *)ns->prefix, counter++);
5949
0
  res = xmlSearchNsSafe(tree, prefix, &def);
5950
0
        if (res < 0)
5951
0
            return(NULL);
5952
0
    }
5953
5954
    /*
5955
     * OK, now we are ready to create a new one.
5956
     */
5957
0
    def = xmlNewNs(tree, ns->href, prefix);
5958
0
    return(def);
5959
0
}
5960
5961
typedef struct {
5962
    xmlNsPtr oldNs;
5963
    xmlNsPtr newNs;
5964
} xmlNsCache;
5965
5966
static int
5967
0
xmlGrowNsCache(xmlNsCache **cache, int *capacity) {
5968
0
    xmlNsCache *tmp;
5969
0
    int newSize;
5970
5971
0
    newSize = xmlGrowCapacity(*capacity, sizeof(tmp[0]),
5972
0
                              10, XML_MAX_ITEMS);
5973
0
    if (newSize < 0)
5974
0
        return(-1);
5975
0
    tmp = xmlRealloc(*cache, newSize * sizeof(tmp[0]));
5976
0
    if (tmp == NULL)
5977
0
        return(-1);
5978
0
    *cache = tmp;
5979
0
    *capacity = newSize;
5980
5981
0
    return(0);
5982
0
}
5983
5984
/**
5985
 * This function checks that all the namespaces declared within the given
5986
 * tree are properly declared. This is needed for example after copy or cut
5987
 * and then paste operations. The subtree may still hold pointers to
5988
 * namespace declarations outside the subtree or invalid/masked. As much
5989
 * as possible the function tries to reuse the existing namespaces found in
5990
 * the new environment. If not possible the new namespaces are redeclared
5991
 * on `tree` at the top of the given subtree.
5992
 *
5993
 * @param doc  the document
5994
 * @param tree  a node defining the subtree to reconciliate
5995
 * @returns 0 on success or -1 in case of error.
5996
 */
5997
int
5998
0
xmlReconciliateNs(xmlDoc *doc, xmlNode *tree) {
5999
0
    xmlNsCache *cache = NULL;
6000
0
    int sizeCache = 0;
6001
0
    int nbCache = 0;
6002
6003
0
    xmlNsPtr n;
6004
0
    xmlNodePtr node = tree;
6005
0
    xmlAttrPtr attr;
6006
0
    int ret = 0, i;
6007
6008
0
    if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6009
0
    if (node->doc != doc) return(-1);
6010
0
    while (node != NULL) {
6011
        /*
6012
   * Reconciliate the node namespace
6013
   */
6014
0
  if (node->ns != NULL) {
6015
0
      for (i = 0; i < nbCache; i++) {
6016
0
          if (cache[i].oldNs == node->ns) {
6017
0
        node->ns = cache[i].newNs;
6018
0
        break;
6019
0
    }
6020
0
      }
6021
0
      if (i == nbCache) {
6022
          /*
6023
     * OK we need to recreate a new namespace definition
6024
     */
6025
0
    n = xmlNewReconciledNs(tree, node->ns);
6026
0
    if (n == NULL) {
6027
0
                    ret = -1;
6028
0
                } else {
6029
        /*
6030
         * check if we need to grow the cache buffers.
6031
         */
6032
0
        if ((sizeCache <= nbCache) &&
6033
0
                        (xmlGrowNsCache(&cache, &sizeCache) < 0)) {
6034
0
                        ret = -1;
6035
0
        } else {
6036
0
                        cache[nbCache].newNs = n;
6037
0
                        cache[nbCache++].oldNs = node->ns;
6038
0
                    }
6039
0
                }
6040
0
    node->ns = n;
6041
0
      }
6042
0
  }
6043
  /*
6044
   * now check for namespace held by attributes on the node.
6045
   */
6046
0
  if (node->type == XML_ELEMENT_NODE) {
6047
0
      attr = node->properties;
6048
0
      while (attr != NULL) {
6049
0
    if (attr->ns != NULL) {
6050
0
        for (i = 0; i < nbCache; i++) {
6051
0
      if (cache[i].oldNs == attr->ns) {
6052
0
          attr->ns = cache[i].newNs;
6053
0
          break;
6054
0
      }
6055
0
        }
6056
0
        if (i == nbCache) {
6057
      /*
6058
       * OK we need to recreate a new namespace definition
6059
       */
6060
0
      n = xmlNewReconciledNs(tree, attr->ns);
6061
0
      if (n == NULL) {
6062
0
                            ret = -1;
6063
0
                        } else {
6064
          /*
6065
           * check if we need to grow the cache buffers.
6066
           */
6067
0
                            if ((sizeCache <= nbCache) &&
6068
0
                                (xmlGrowNsCache(&cache, &sizeCache) < 0)) {
6069
0
                                ret = -1;
6070
0
                            } else {
6071
0
                                cache[nbCache].newNs = n;
6072
0
                                cache[nbCache++].oldNs = attr->ns;
6073
0
          }
6074
0
      }
6075
0
      attr->ns = n;
6076
0
        }
6077
0
    }
6078
0
    attr = attr->next;
6079
0
      }
6080
0
  }
6081
6082
  /*
6083
   * Browse the full subtree, deep first
6084
   */
6085
0
        if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6086
      /* deep first */
6087
0
      node = node->children;
6088
0
  } else if ((node != tree) && (node->next != NULL)) {
6089
      /* then siblings */
6090
0
      node = node->next;
6091
0
  } else if (node != tree) {
6092
      /* go up to parents->next if needed */
6093
0
      while (node != tree) {
6094
0
          if (node->parent != NULL)
6095
0
        node = node->parent;
6096
0
    if ((node != tree) && (node->next != NULL)) {
6097
0
        node = node->next;
6098
0
        break;
6099
0
    }
6100
0
    if (node->parent == NULL) {
6101
0
        node = NULL;
6102
0
        break;
6103
0
    }
6104
0
      }
6105
      /* exit condition */
6106
0
      if (node == tree)
6107
0
          node = NULL;
6108
0
  } else
6109
0
      break;
6110
0
    }
6111
0
    if (cache != NULL)
6112
0
  xmlFree(cache);
6113
0
    return(ret);
6114
0
}
6115
6116
static xmlAttrPtr
6117
xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
6118
           const xmlChar *nsName, int useDTD)
6119
0
{
6120
0
    xmlAttrPtr prop;
6121
6122
    /* Avoid unused variable warning if features are disabled. */
6123
0
    (void) useDTD;
6124
6125
0
    if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6126
0
  return(NULL);
6127
6128
0
    if (node->properties != NULL) {
6129
0
  prop = node->properties;
6130
0
  if (nsName == NULL) {
6131
      /*
6132
      * We want the attr to be in no namespace.
6133
      */
6134
0
      do {
6135
0
    if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6136
0
        return(prop);
6137
0
    }
6138
0
    prop = prop->next;
6139
0
      } while (prop != NULL);
6140
0
  } else {
6141
      /*
6142
      * We want the attr to be in the specified namespace.
6143
      */
6144
0
      do {
6145
0
    if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6146
0
        ((prop->ns->href == nsName) ||
6147
0
         xmlStrEqual(prop->ns->href, nsName)))
6148
0
    {
6149
0
        return(prop);
6150
0
    }
6151
0
    prop = prop->next;
6152
0
      } while (prop != NULL);
6153
0
  }
6154
0
    }
6155
6156
0
    if (! useDTD)
6157
0
  return(NULL);
6158
    /*
6159
     * Check if there is a default/fixed attribute declaration in
6160
     * the internal or external subset.
6161
     */
6162
0
    if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6163
0
  xmlDocPtr doc = node->doc;
6164
0
  xmlAttributePtr attrDecl = NULL;
6165
0
  xmlChar *elemQName, *tmpstr = NULL;
6166
6167
  /*
6168
  * We need the QName of the element for the DTD-lookup.
6169
  */
6170
0
  if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6171
0
      tmpstr = xmlStrdup(node->ns->prefix);
6172
0
      if (tmpstr == NULL)
6173
0
    return(NULL);
6174
0
      tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6175
0
      if (tmpstr == NULL)
6176
0
    return(NULL);
6177
0
      tmpstr = xmlStrcat(tmpstr, node->name);
6178
0
      if (tmpstr == NULL)
6179
0
    return(NULL);
6180
0
      elemQName = tmpstr;
6181
0
  } else
6182
0
      elemQName = (xmlChar *) node->name;
6183
0
  if (nsName == NULL) {
6184
      /*
6185
      * The common and nice case: Attr in no namespace.
6186
      */
6187
0
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6188
0
    elemQName, name, NULL);
6189
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6190
0
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6191
0
        elemQName, name, NULL);
6192
0
      }
6193
0
        } else if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
6194
      /*
6195
      * The XML namespace must be bound to prefix 'xml'.
6196
      */
6197
0
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6198
0
    elemQName, name, BAD_CAST "xml");
6199
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6200
0
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6201
0
        elemQName, name, BAD_CAST "xml");
6202
0
      }
6203
0
  } else {
6204
0
      xmlNsPtr *nsList, *cur;
6205
6206
      /*
6207
      * The ugly case: Search using the prefixes of in-scope
6208
      * ns-decls corresponding to @nsName.
6209
      */
6210
0
      nsList = xmlGetNsList(node->doc, node);
6211
0
      if (nsList == NULL) {
6212
0
    if (tmpstr != NULL)
6213
0
        xmlFree(tmpstr);
6214
0
    return(NULL);
6215
0
      }
6216
0
      cur = nsList;
6217
0
      while (*cur != NULL) {
6218
0
    if (xmlStrEqual((*cur)->href, nsName)) {
6219
0
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6220
0
      name, (*cur)->prefix);
6221
0
        if (attrDecl)
6222
0
      break;
6223
0
        if (doc->extSubset != NULL) {
6224
0
      attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6225
0
          name, (*cur)->prefix);
6226
0
      if (attrDecl)
6227
0
          break;
6228
0
        }
6229
0
    }
6230
0
    cur++;
6231
0
      }
6232
0
      xmlFree(nsList);
6233
0
  }
6234
0
  if (tmpstr != NULL)
6235
0
      xmlFree(tmpstr);
6236
  /*
6237
  * Only default/fixed attrs are relevant.
6238
  */
6239
0
  if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6240
0
      return((xmlAttrPtr) attrDecl);
6241
0
    }
6242
6243
0
    return(NULL);
6244
0
}
6245
6246
static xmlChar*
6247
xmlGetPropNodeValueInternal(const xmlAttr *prop)
6248
0
{
6249
0
    if (prop == NULL)
6250
0
  return(NULL);
6251
0
    if (prop->type == XML_ATTRIBUTE_NODE) {
6252
0
  return(xmlNodeGetContent((xmlNodePtr) prop));
6253
0
    } else if (prop->type == XML_ATTRIBUTE_DECL) {
6254
0
  return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6255
0
    }
6256
0
    return(NULL);
6257
0
}
6258
6259
/**
6260
 * Search for an attribute of an element.
6261
 *
6262
 * This function also looks in DTD attribute declaration for \#FIXED or
6263
 * default declaration values.
6264
 *
6265
 * @param node  the element
6266
 * @param name  the attribute name
6267
 * @returns the attribute or the attribute declaration or NULL if
6268
 * neither was found. Also returns NULL if a memory allocation failed,
6269
 * making this function unreliable.
6270
 */
6271
xmlAttr *
6272
0
xmlHasProp(const xmlNode *node, const xmlChar *name) {
6273
0
    xmlAttrPtr prop;
6274
0
    xmlDocPtr doc;
6275
6276
0
    if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6277
0
        return(NULL);
6278
    /*
6279
     * Check on the properties attached to the node
6280
     */
6281
0
    prop = node->properties;
6282
0
    while (prop != NULL) {
6283
0
        if (xmlStrEqual(prop->name, name))  {
6284
0
      return(prop);
6285
0
        }
6286
0
  prop = prop->next;
6287
0
    }
6288
6289
    /*
6290
     * Check if there is a default declaration in the internal
6291
     * or external subsets
6292
     */
6293
0
    doc =  node->doc;
6294
0
    if (doc != NULL) {
6295
0
        xmlAttributePtr attrDecl;
6296
0
        if (doc->intSubset != NULL) {
6297
0
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6298
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
6299
0
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6300
0
            if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6301
              /* return attribute declaration only if a default value is given
6302
                 (that includes #FIXED declarations) */
6303
0
    return((xmlAttrPtr) attrDecl);
6304
0
  }
6305
0
    }
6306
0
    return(NULL);
6307
0
}
6308
6309
/**
6310
 * Search for an attribute of an element.
6311
 *
6312
 * The attribute has to match the specified namespace. A namespace of
6313
 * NULL means that the attribute must have no namespace.
6314
 *
6315
 * This function also looks in DTD attribute declaration for \#FIXED or
6316
 * default declaration values.
6317
 *
6318
 * @param node  the element
6319
 * @param name  the attribute name
6320
 * @param nameSpace  the URI of the namespace
6321
 * @returns the attribute or the attribute declaration or NULL if
6322
 * neither was found. Also returns NULL if a memory allocation failed
6323
 * making this function unreliable.
6324
 */
6325
xmlAttr *
6326
0
xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6327
6328
0
    return(xmlGetPropNodeInternal(node, name, nameSpace, 1));
6329
0
}
6330
6331
/**
6332
 * Look up the value of an element's attribute.
6333
 *
6334
 * The attribute has to match the specified namespace. A namespace of
6335
 * NULL means that the attribute must have no namespace.
6336
 *
6337
 * Entities are substituted. The returned value must be freed by the
6338
 * caller.
6339
 *
6340
 * @since 2.13.0
6341
 *
6342
 * @param node  the element
6343
 * @param name  the attribute name
6344
 * @param nsUri  the URI of the namespace
6345
 * @param out  the returned string
6346
 * @returns 0 on success, 1 if no attribute was found, -1 if a
6347
 * memory allocation failed.
6348
 */
6349
int
6350
xmlNodeGetAttrValue(const xmlNode *node, const xmlChar *name,
6351
0
                    const xmlChar *nsUri, xmlChar **out) {
6352
0
    xmlAttrPtr prop;
6353
6354
0
    if (out == NULL)
6355
0
        return(1);
6356
0
    *out = NULL;
6357
6358
0
    prop = xmlGetPropNodeInternal(node, name, nsUri, 0);
6359
0
    if (prop == NULL)
6360
0
  return(1);
6361
6362
0
    *out = xmlGetPropNodeValueInternal(prop);
6363
0
    if (*out == NULL)
6364
0
        return(-1);
6365
0
    return(0);
6366
0
}
6367
6368
/**
6369
 * Look up the value of an element's attribute.
6370
 *
6371
 * Entities are substituted. The returned value must be freed by the
6372
 * caller.
6373
 *
6374
 * This function looks in DTD attribute declarations for \#FIXED or
6375
 * default declaration values.
6376
 *
6377
 * NOTE: This function is ignores namespaces. Use #xmlGetNsProp or
6378
 * #xmlGetNoNsProp for namespace aware processing.
6379
 *
6380
 * NOTE: This function doesn't allow to distinguish malloc failures from
6381
 * missing attributes.
6382
 *
6383
 * @param node  the element
6384
 * @param name  the attribute name
6385
 * @returns the attribute value or NULL if not found or a memory allocation
6386
 * failed.
6387
 */
6388
xmlChar *
6389
0
xmlGetProp(const xmlNode *node, const xmlChar *name) {
6390
0
    xmlAttrPtr prop;
6391
6392
0
    prop = xmlHasProp(node, name);
6393
0
    if (prop == NULL)
6394
0
  return(NULL);
6395
0
    return(xmlGetPropNodeValueInternal(prop));
6396
0
}
6397
6398
/**
6399
 * Look up the value of an element's attribute.
6400
 *
6401
 * Entities are substituted. The returned value must be freed by the
6402
 * caller.
6403
 *
6404
 * This function looks in DTD attribute declarations for \#FIXED or
6405
 * default declaration values.
6406
 *
6407
 * This function is similar to #xmlGetProp except it will accept only
6408
 * an attribute in no namespace.
6409
 *
6410
 * NOTE: This function doesn't allow to distinguish malloc failures from
6411
 * missing attributes. It's more robust to use #xmlNodeGetAttrValue.
6412
 *
6413
 * @param node  the element
6414
 * @param name  the attribute name
6415
 * @returns the attribute value or NULL if not found or a memory allocation
6416
 * failed.
6417
 */
6418
xmlChar *
6419
0
xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
6420
0
    xmlAttrPtr prop;
6421
6422
0
    prop = xmlGetPropNodeInternal(node, name, NULL, 1);
6423
0
    if (prop == NULL)
6424
0
  return(NULL);
6425
0
    return(xmlGetPropNodeValueInternal(prop));
6426
0
}
6427
6428
/**
6429
 * Look up the value of an element's attribute.
6430
 *
6431
 * The attribute has to match the specified namespace. A namespace of
6432
 * NULL means that the attribute must have no namespace.
6433
 *
6434
 * Entities are substituted. The returned value must be freed by the
6435
 * caller.
6436
 *
6437
 * This function looks in DTD attribute declaration for \#FIXED or
6438
 * default declaration values.
6439
 *
6440
 * NOTE: This function doesn't allow to distinguish malloc failures from
6441
 * missing attributes. It's more robust to use #xmlNodeGetAttrValue.
6442
 *
6443
 * @param node  the element
6444
 * @param name  the attribute name
6445
 * @param nameSpace  the URI of the namespace
6446
 * @returns the attribute value or NULL if not found or a memory allocation
6447
 * failed.
6448
 */
6449
xmlChar *
6450
0
xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6451
0
    xmlAttrPtr prop;
6452
6453
0
    prop = xmlGetPropNodeInternal(node, name, nameSpace, 1);
6454
0
    if (prop == NULL)
6455
0
  return(NULL);
6456
0
    return(xmlGetPropNodeValueInternal(prop));
6457
0
}
6458
6459
/**
6460
 * Remove an attribute of an element.
6461
 * This handles only attributes in no namespace.
6462
 *
6463
 * @param node  the element
6464
 * @param name  the attribute name
6465
 * @returns 0 if successful, -1 if not found
6466
 */
6467
int
6468
0
xmlUnsetProp(xmlNode *node, const xmlChar *name) {
6469
0
    xmlAttrPtr prop;
6470
6471
0
    prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6472
0
    if (prop == NULL)
6473
0
  return(-1);
6474
0
    xmlUnlinkNodeInternal((xmlNodePtr) prop);
6475
0
    xmlFreeProp(prop);
6476
0
    return(0);
6477
0
}
6478
6479
/**
6480
 * Remove an attribute of an element.
6481
 *
6482
 * @param node  the element
6483
 * @param ns  the namespace definition
6484
 * @param name  the attribute name
6485
 * @returns 0 if successful, -1 if not found
6486
 */
6487
int
6488
0
xmlUnsetNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name) {
6489
0
    xmlAttrPtr prop;
6490
6491
0
    prop = xmlGetPropNodeInternal(node, name,
6492
0
                                  (ns != NULL) ? ns->href : NULL, 0);
6493
0
    if (prop == NULL)
6494
0
  return(-1);
6495
0
    xmlUnlinkNodeInternal((xmlNodePtr) prop);
6496
0
    xmlFreeProp(prop);
6497
0
    return(0);
6498
0
}
6499
6500
/**
6501
 * Set (or reset) an element's attribute. If `name` has a prefix,
6502
 * the corresponding namespace will be used. It is an error if
6503
 * there's no such binding for the prefix in scope.
6504
 *
6505
 * @param node  the node
6506
 * @param name  the attribute name (a QName)
6507
 * @param value  the attribute value
6508
 * @returns the attribute pointer.
6509
 */
6510
xmlAttr *
6511
0
xmlSetProp(xmlNode *node, const xmlChar *name, const xmlChar *value) {
6512
0
    xmlNsPtr ns = NULL;
6513
0
    const xmlChar *localname;
6514
0
    xmlChar *prefix;
6515
0
    int res;
6516
6517
0
    if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6518
0
  return(NULL);
6519
6520
    /*
6521
     * handle QNames
6522
     */
6523
0
    localname = xmlSplitQName4(name, &prefix);
6524
0
    if (localname == NULL)
6525
0
        return(NULL);
6526
6527
0
    if (prefix != NULL) {
6528
0
  res = xmlSearchNsSafe(node, prefix, &ns);
6529
0
  xmlFree(prefix);
6530
0
        if (res < 0)
6531
0
            return(NULL);
6532
0
        if (ns != NULL)
6533
0
            return(xmlSetNsProp(node, ns, localname, value));
6534
0
    }
6535
6536
0
    return(xmlSetNsProp(node, NULL, name, value));
6537
0
}
6538
6539
/**
6540
 * Set (or reset) an element's attribute.
6541
 *
6542
 * The namespace must be in scope.
6543
 *
6544
 * @param node  the node
6545
 * @param ns  the namespace definition
6546
 * @param name  the attribute name
6547
 * @param value  the attribute value
6548
 * @returns the attribute pointer.
6549
 */
6550
xmlAttr *
6551
xmlSetNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name,
6552
       const xmlChar *value)
6553
0
{
6554
0
    xmlAttrPtr prop;
6555
6556
0
    if (ns && (ns->href == NULL))
6557
0
  return(NULL);
6558
0
    if (name == NULL)
6559
0
        return(NULL);
6560
0
    prop = xmlGetPropNodeInternal(node, name,
6561
0
                                  (ns != NULL) ? ns->href : NULL, 0);
6562
0
    if (prop != NULL) {
6563
0
        xmlNodePtr children = NULL;
6564
6565
  /*
6566
  * Modify the attribute's value.
6567
  */
6568
0
        if (value != NULL) {
6569
0
      children = xmlNewDocText(node->doc, value);
6570
0
            if (children == NULL)
6571
0
                return(NULL);
6572
0
        }
6573
6574
0
  if (prop->id != NULL) {
6575
0
      xmlRemoveID(node->doc, prop);
6576
0
      prop->atype = XML_ATTRIBUTE_ID;
6577
0
  }
6578
0
  if (prop->children != NULL)
6579
0
      xmlFreeNodeList(prop->children);
6580
0
  prop->children = NULL;
6581
0
  prop->last = NULL;
6582
0
  prop->ns = ns;
6583
0
  if (value != NULL) {
6584
0
      xmlNodePtr tmp;
6585
6586
0
      prop->children = children;
6587
0
      prop->last = NULL;
6588
0
      tmp = prop->children;
6589
0
      while (tmp != NULL) {
6590
0
    tmp->parent = (xmlNodePtr) prop;
6591
0
    if (tmp->next == NULL)
6592
0
        prop->last = tmp;
6593
0
    tmp = tmp->next;
6594
0
      }
6595
0
  }
6596
0
  if ((prop->atype == XML_ATTRIBUTE_ID) &&
6597
0
      (xmlAddIDSafe(prop, value) < 0)) {
6598
0
            return(NULL);
6599
0
        }
6600
0
  return(prop);
6601
0
    }
6602
    /*
6603
    * No equal attr found; create a new one.
6604
    */
6605
0
    return(xmlNewPropInternal(node, ns, name, value, 0));
6606
0
}
6607
6608
/**
6609
 * Check whether the node is a text node.
6610
 *
6611
 * @param node  the node
6612
 * @returns 1 if yes, 0 if no
6613
 */
6614
int
6615
0
xmlNodeIsText(const xmlNode *node) {
6616
0
    if (node == NULL) return(0);
6617
6618
0
    if (node->type == XML_TEXT_NODE) return(1);
6619
0
    return(0);
6620
0
}
6621
6622
/**
6623
 * Checks whether this node is an empty or whitespace-only
6624
 * text node.
6625
 *
6626
 * @param node  the node
6627
 * @returns 1 if yes, 0 if no
6628
 */
6629
int
6630
0
xmlIsBlankNode(const xmlNode *node) {
6631
0
    const xmlChar *cur;
6632
0
    if (node == NULL) return(0);
6633
6634
0
    if ((node->type != XML_TEXT_NODE) &&
6635
0
        (node->type != XML_CDATA_SECTION_NODE))
6636
0
  return(0);
6637
0
    if (node->content == NULL) return(1);
6638
0
    cur = node->content;
6639
0
    while (*cur != 0) {
6640
0
  if (!IS_BLANK_CH(*cur)) return(0);
6641
0
  cur++;
6642
0
    }
6643
6644
0
    return(1);
6645
0
}
6646
6647
/**
6648
 * Concat the given string at the end of the existing node content.
6649
 *
6650
 * If `len` is -1, the string length will be calculated.
6651
 *
6652
 * @param node  the node
6653
 * @param content  the content
6654
 * @param len  `content` length
6655
 * @returns -1 in case of error, 0 otherwise
6656
 */
6657
6658
int
6659
0
xmlTextConcat(xmlNode *node, const xmlChar *content, int len) {
6660
0
    if (node == NULL)
6661
0
        return(-1);
6662
6663
0
    if ((node->type != XML_TEXT_NODE) &&
6664
0
        (node->type != XML_CDATA_SECTION_NODE) &&
6665
0
  (node->type != XML_COMMENT_NODE) &&
6666
0
  (node->type != XML_PI_NODE))
6667
0
        return(-1);
6668
6669
0
    return(xmlTextAddContent(node, content, len));
6670
0
}
6671
6672
/**
6673
 * Get the compression level of a document, ZLIB based.
6674
 *
6675
 * @param doc  the document
6676
 * @returns 0 (uncompressed) to 9 (max compression)
6677
 */
6678
int
6679
0
xmlGetDocCompressMode (const xmlDoc *doc) {
6680
0
    if (doc == NULL) return(-1);
6681
0
    return(doc->compression);
6682
0
}
6683
6684
/**
6685
 * Set the compression level of a document, ZLIB based.
6686
 *
6687
 * Correct values: 0 (uncompressed) to 9 (max compression)
6688
 *
6689
 * @param doc  the document
6690
 * @param mode  the compression ratio
6691
 */
6692
void
6693
0
xmlSetDocCompressMode (xmlDoc *doc, int mode) {
6694
0
    if (doc == NULL) return;
6695
0
    if (mode < 0) doc->compression = 0;
6696
0
    else if (mode > 9) doc->compression = 9;
6697
0
    else doc->compression = mode;
6698
0
}
6699
6700
/**
6701
 * Get the global compression level, ZLIB based.
6702
 *
6703
 * @deprecated Use #xmlGetDocCompressMode
6704
 *
6705
 * @returns 0 (uncompressed) to 9 (max compression)
6706
 */
6707
int
6708
xmlGetCompressMode(void)
6709
0
{
6710
0
    return (xmlCompressMode);
6711
0
}
6712
6713
/**
6714
 * Set the global compression level, ZLIB based.
6715
 *
6716
 * @deprecated Use #xmlSetDocCompressMode
6717
 *
6718
 * Correct values: 0 (uncompressed) to 9 (max compression)
6719
 *
6720
 * @param mode  the compression ratio
6721
 */
6722
void
6723
0
xmlSetCompressMode(int mode) {
6724
0
    if (mode < 0) xmlCompressMode = 0;
6725
0
    else if (mode > 9) xmlCompressMode = 9;
6726
0
    else xmlCompressMode = mode;
6727
0
}
6728
6729
0
#define XML_TREE_NSMAP_PARENT -1
6730
#define XML_TREE_NSMAP_XML -2
6731
0
#define XML_TREE_NSMAP_DOC -3
6732
0
#define XML_TREE_NSMAP_CUSTOM -4
6733
6734
typedef struct xmlNsMapItem *xmlNsMapItemPtr;
6735
struct xmlNsMapItem {
6736
    xmlNsMapItemPtr next;
6737
    xmlNsMapItemPtr prev;
6738
    xmlNsPtr oldNs; /* old ns decl reference */
6739
    xmlNsPtr newNs; /* new ns decl reference */
6740
    int shadowDepth; /* Shadowed at this depth */
6741
    /*
6742
    * depth:
6743
    * >= 0 == @node's ns-decls
6744
    * -1   == @parent's ns-decls
6745
    * -2   == the doc->oldNs XML ns-decl
6746
    * -3   == the doc->oldNs storage ns-decls
6747
    * -4   == ns-decls provided via custom ns-handling
6748
    */
6749
    int depth;
6750
};
6751
6752
typedef struct xmlNsMap *xmlNsMapPtr;
6753
struct xmlNsMap {
6754
    xmlNsMapItemPtr first;
6755
    xmlNsMapItemPtr last;
6756
    xmlNsMapItemPtr pool;
6757
};
6758
6759
0
#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
6760
0
#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
6761
#define XML_NSMAP_POP(m, i) \
6762
0
    i = (m)->last; \
6763
0
    (m)->last = (i)->prev; \
6764
0
    if ((m)->last == NULL) \
6765
0
  (m)->first = NULL; \
6766
0
    else \
6767
0
  (m)->last->next = NULL; \
6768
0
    (i)->next = (m)->pool; \
6769
0
    (m)->pool = i;
6770
6771
/**
6772
 * Frees the ns-map
6773
 *
6774
 * @param nsmap  the ns-map
6775
 */
6776
static void
6777
xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
6778
0
{
6779
0
    xmlNsMapItemPtr cur, tmp;
6780
6781
0
    if (nsmap == NULL)
6782
0
  return;
6783
0
    cur = nsmap->pool;
6784
0
    while (cur != NULL) {
6785
0
  tmp = cur;
6786
0
  cur = cur->next;
6787
0
  xmlFree(tmp);
6788
0
    }
6789
0
    cur = nsmap->first;
6790
0
    while (cur != NULL) {
6791
0
  tmp = cur;
6792
0
  cur = cur->next;
6793
0
  xmlFree(tmp);
6794
0
    }
6795
0
    xmlFree(nsmap);
6796
0
}
6797
6798
/**
6799
 * Adds an ns-mapping item.
6800
 *
6801
 * @param nsmap  the ns-map
6802
 * @param position  position
6803
 * @param oldNs  the old ns-struct
6804
 * @param newNs  the new ns-struct
6805
 * @param depth  depth and ns-kind information
6806
 * @returns the added item.
6807
 */
6808
static xmlNsMapItemPtr
6809
xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
6810
           xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
6811
0
{
6812
0
    xmlNsMapItemPtr ret;
6813
0
    xmlNsMapPtr map;
6814
6815
0
    if (nsmap == NULL)
6816
0
  return(NULL);
6817
0
    if ((position != -1) && (position != 0))
6818
0
  return(NULL);
6819
0
    map = *nsmap;
6820
6821
0
    if (map == NULL) {
6822
  /*
6823
  * Create the ns-map.
6824
  */
6825
0
  map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
6826
0
  if (map == NULL)
6827
0
      return(NULL);
6828
0
  memset(map, 0, sizeof(struct xmlNsMap));
6829
0
  *nsmap = map;
6830
0
    }
6831
6832
0
    if (map->pool != NULL) {
6833
  /*
6834
  * Reuse an item from the pool.
6835
  */
6836
0
  ret = map->pool;
6837
0
  map->pool = ret->next;
6838
0
  memset(ret, 0, sizeof(struct xmlNsMapItem));
6839
0
    } else {
6840
  /*
6841
  * Create a new item.
6842
  */
6843
0
  ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
6844
0
  if (ret == NULL)
6845
0
      return(NULL);
6846
0
  memset(ret, 0, sizeof(struct xmlNsMapItem));
6847
0
    }
6848
6849
0
    if (map->first == NULL) {
6850
  /*
6851
  * First ever.
6852
  */
6853
0
  map->first = ret;
6854
0
  map->last = ret;
6855
0
    } else if (position == -1) {
6856
  /*
6857
  * Append.
6858
  */
6859
0
  ret->prev = map->last;
6860
0
  map->last->next = ret;
6861
0
  map->last = ret;
6862
0
    } else if (position == 0) {
6863
  /*
6864
  * Set on first position.
6865
  */
6866
0
  map->first->prev = ret;
6867
0
  ret->next = map->first;
6868
0
  map->first = ret;
6869
0
    }
6870
6871
0
    ret->oldNs = oldNs;
6872
0
    ret->newNs = newNs;
6873
0
    ret->shadowDepth = -1;
6874
0
    ret->depth = depth;
6875
0
    return (ret);
6876
0
}
6877
6878
/**
6879
 * Creates or reuses an xmlNs struct on doc->oldNs with
6880
 * the given prefix and namespace name.
6881
 *
6882
 * @param doc  the doc
6883
 * @param nsName  the namespace name
6884
 * @param prefix  the prefix
6885
 * @returns the acquired ns struct or NULL in case of an API
6886
 *          or internal error.
6887
 */
6888
static xmlNsPtr
6889
xmlDOMWrapStoreNs(xmlDocPtr doc,
6890
       const xmlChar *nsName,
6891
       const xmlChar *prefix)
6892
0
{
6893
0
    xmlNsPtr ns;
6894
6895
0
    if (doc == NULL)
6896
0
  return (NULL);
6897
0
    ns = xmlTreeEnsureXMLDecl(doc);
6898
0
    if (ns == NULL)
6899
0
  return (NULL);
6900
0
    if (ns->next != NULL) {
6901
  /* Reuse. */
6902
0
  ns = ns->next;
6903
0
  while (ns != NULL) {
6904
0
      if (((ns->prefix == prefix) ||
6905
0
    xmlStrEqual(ns->prefix, prefix)) &&
6906
0
    xmlStrEqual(ns->href, nsName)) {
6907
0
    return (ns);
6908
0
      }
6909
0
      if (ns->next == NULL)
6910
0
    break;
6911
0
      ns = ns->next;
6912
0
  }
6913
0
    }
6914
    /* Create. */
6915
0
    if (ns != NULL) {
6916
0
        ns->next = xmlNewNs(NULL, nsName, prefix);
6917
0
        return (ns->next);
6918
0
    }
6919
0
    return(NULL);
6920
0
}
6921
6922
/**
6923
 * Allocates and initializes a new DOM-wrapper context.
6924
 *
6925
 * @returns the xmlDOMWrapCtxt or NULL in case of an internal error.
6926
 */
6927
xmlDOMWrapCtxt *
6928
xmlDOMWrapNewCtxt(void)
6929
0
{
6930
0
    xmlDOMWrapCtxtPtr ret;
6931
6932
0
    ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
6933
0
    if (ret == NULL)
6934
0
  return (NULL);
6935
0
    memset(ret, 0, sizeof(xmlDOMWrapCtxt));
6936
0
    return (ret);
6937
0
}
6938
6939
/**
6940
 * Frees the DOM-wrapper context.
6941
 *
6942
 * @param ctxt  the DOM-wrapper context
6943
 */
6944
void
6945
xmlDOMWrapFreeCtxt(xmlDOMWrapCtxt *ctxt)
6946
0
{
6947
0
    if (ctxt == NULL)
6948
0
  return;
6949
0
    if (ctxt->namespaceMap != NULL)
6950
0
  xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
6951
    /*
6952
    * TODO: Store the namespace map in the context.
6953
    */
6954
0
    xmlFree(ctxt);
6955
0
}
6956
6957
/**
6958
 * Searches for a ns-decl with the given prefix in `nsList`.
6959
 *
6960
 * @param nsList  a list of ns-structs
6961
 * @param prefix  the searched prefix
6962
 * @returns the ns-decl if found, NULL if not found and on
6963
 *          API errors.
6964
 */
6965
static xmlNsPtr
6966
xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
6967
0
{
6968
0
    if (nsList == NULL)
6969
0
  return (NULL);
6970
0
    {
6971
0
  xmlNsPtr ns;
6972
0
  ns = nsList;
6973
0
  do {
6974
0
      if ((prefix == ns->prefix) ||
6975
0
    xmlStrEqual(prefix, ns->prefix)) {
6976
0
    return (ns);
6977
0
      }
6978
0
      ns = ns->next;
6979
0
  } while (ns != NULL);
6980
0
    }
6981
0
    return (NULL);
6982
0
}
6983
6984
/**
6985
 * Puts in-scope namespaces into the ns-map.
6986
 *
6987
 * @param map  the namespace map
6988
 * @param node  the node to start with
6989
 * @returns 0 on success, -1 on API or internal errors.
6990
 */
6991
static int
6992
xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
6993
        xmlNodePtr node)
6994
0
{
6995
0
    xmlNodePtr cur;
6996
0
    xmlNsPtr ns;
6997
0
    xmlNsMapItemPtr mi;
6998
0
    int shadowed;
6999
7000
0
    if ((map == NULL) || (*map != NULL))
7001
0
  return (-1);
7002
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
7003
0
        return (-1);
7004
    /*
7005
    * Get in-scope ns-decls of @parent.
7006
    */
7007
0
    cur = node;
7008
0
    while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7009
0
  if (cur->type == XML_ELEMENT_NODE) {
7010
0
      if (cur->nsDef != NULL) {
7011
0
    ns = cur->nsDef;
7012
0
    do {
7013
0
        shadowed = 0;
7014
0
        if (XML_NSMAP_NOTEMPTY(*map)) {
7015
      /*
7016
      * Skip shadowed prefixes.
7017
      */
7018
0
      XML_NSMAP_FOREACH(*map, mi) {
7019
0
          if ((ns->prefix == mi->newNs->prefix) ||
7020
0
        xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7021
0
        shadowed = 1;
7022
0
        break;
7023
0
          }
7024
0
      }
7025
0
        }
7026
        /*
7027
        * Insert mapping.
7028
        */
7029
0
        mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
7030
0
      ns, XML_TREE_NSMAP_PARENT);
7031
0
        if (mi == NULL)
7032
0
      return (-1);
7033
0
        if (shadowed)
7034
0
      mi->shadowDepth = 0;
7035
0
        ns = ns->next;
7036
0
    } while (ns != NULL);
7037
0
      }
7038
0
  }
7039
0
  cur = cur->parent;
7040
0
    }
7041
0
    return (0);
7042
0
}
7043
7044
/*
7045
 * For internal use. Adds a ns-decl mapping.
7046
 *
7047
 * Returns 0 on success, -1 on internal errors.
7048
 */
7049
static int
7050
xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7051
      xmlNsPtr oldNs, xmlNsPtr newNs)
7052
0
{
7053
0
    if (*number >= *size) {
7054
0
        xmlNsPtr *tmp;
7055
0
        int newSize;
7056
7057
0
        newSize = xmlGrowCapacity(*size, 2 * sizeof(tmp[0]), 3, XML_MAX_ITEMS);
7058
0
        if (newSize < 0)
7059
0
            return(-1);
7060
0
        tmp = xmlRealloc(*list, newSize * 2 * sizeof(tmp[0]));
7061
0
        if (tmp == NULL)
7062
0
            return(-1);
7063
0
        *list = tmp;
7064
0
        *size = newSize;
7065
0
    }
7066
7067
0
    (*list)[2 * (*number)] = oldNs;
7068
0
    (*list)[2 * (*number) +1] = newNs;
7069
0
    (*number)++;
7070
0
    return (0);
7071
0
}
7072
7073
/**
7074
 * Unlinks the given node from its owner.
7075
 *
7076
 * This will substitute ns-references to node->nsDef for
7077
 * ns-references to doc->oldNs, thus ensuring the removed
7078
 * branch to be autark wrt ns-references.
7079
 *
7080
 * NOTE: This function was not intensively tested.
7081
 *
7082
 * @param ctxt  a DOM wrapper context
7083
 * @param doc  the doc
7084
 * @param node  the node to be removed.
7085
 * @param options  set of options, unused at the moment
7086
 * @returns 0 on success, 1 if the node is not supported,
7087
 *          -1 on API and internal errors.
7088
 */
7089
int
7090
xmlDOMWrapRemoveNode(xmlDOMWrapCtxt *ctxt, xmlDoc *doc,
7091
         xmlNode *node, int options ATTRIBUTE_UNUSED)
7092
0
{
7093
0
    xmlNsPtr *list = NULL;
7094
0
    int sizeList = 0, nbList = 0, ret = 0, i, j;
7095
0
    xmlNsPtr ns;
7096
7097
0
    if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7098
0
  return (-1);
7099
7100
    /* TODO: 0 or -1 ? */
7101
0
    if (node->parent == NULL)
7102
0
  return (0);
7103
7104
0
    switch (node->type) {
7105
0
  case XML_TEXT_NODE:
7106
0
  case XML_CDATA_SECTION_NODE:
7107
0
  case XML_ENTITY_REF_NODE:
7108
0
  case XML_PI_NODE:
7109
0
  case XML_COMMENT_NODE:
7110
0
      xmlUnlinkNodeInternal(node);
7111
0
      return (0);
7112
0
  case XML_ELEMENT_NODE:
7113
0
  case XML_ATTRIBUTE_NODE:
7114
0
      break;
7115
0
  default:
7116
0
      return (1);
7117
0
    }
7118
0
    xmlUnlinkNodeInternal(node);
7119
    /*
7120
    * Save out-of-scope ns-references in doc->oldNs.
7121
    */
7122
0
    do {
7123
0
  switch (node->type) {
7124
0
      case XML_ELEMENT_NODE:
7125
0
    if ((ctxt == NULL) && (node->nsDef != NULL)) {
7126
0
        ns = node->nsDef;
7127
0
        do {
7128
0
      if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7129
0
          &nbList, ns, ns) == -1)
7130
0
          ret = -1;
7131
0
      ns = ns->next;
7132
0
        } while (ns != NULL);
7133
0
    }
7134
                /* Falls through. */
7135
0
      case XML_ATTRIBUTE_NODE:
7136
0
    if (node->ns != NULL) {
7137
        /*
7138
        * Find a mapping.
7139
        */
7140
0
        if (list != NULL) {
7141
0
      for (i = 0, j = 0; i < nbList; i++, j += 2) {
7142
0
          if (node->ns == list[j]) {
7143
0
        node->ns = list[++j];
7144
0
        goto next_node;
7145
0
          }
7146
0
      }
7147
0
        }
7148
0
        ns = NULL;
7149
0
        if (ctxt != NULL) {
7150
      /*
7151
      * User defined.
7152
      */
7153
0
        } else {
7154
      /*
7155
      * Add to doc's oldNs.
7156
      */
7157
0
      ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7158
0
          node->ns->prefix);
7159
0
      if (ns == NULL)
7160
0
          ret = -1;
7161
0
        }
7162
0
        if (ns != NULL) {
7163
      /*
7164
      * Add mapping.
7165
      */
7166
0
      if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7167
0
          &nbList, node->ns, ns) == -1)
7168
0
          ret = -1;
7169
0
        }
7170
0
        node->ns = ns;
7171
0
    }
7172
0
    if ((node->type == XML_ELEMENT_NODE) &&
7173
0
        (node->properties != NULL)) {
7174
0
        node = (xmlNodePtr) node->properties;
7175
0
        continue;
7176
0
    }
7177
0
    break;
7178
0
      default:
7179
0
    goto next_sibling;
7180
0
  }
7181
0
next_node:
7182
0
  if ((node->type == XML_ELEMENT_NODE) &&
7183
0
      (node->children != NULL)) {
7184
0
      node = node->children;
7185
0
      continue;
7186
0
  }
7187
0
next_sibling:
7188
0
  if (node == NULL)
7189
0
      break;
7190
0
  if (node->next != NULL)
7191
0
      node = node->next;
7192
0
  else {
7193
0
            int type = node->type;
7194
7195
0
      node = node->parent;
7196
0
            if ((type == XML_ATTRIBUTE_NODE) &&
7197
0
                (node != NULL) &&
7198
0
                (node->children != NULL)) {
7199
0
                node = node->children;
7200
0
            } else {
7201
0
          goto next_sibling;
7202
0
            }
7203
0
  }
7204
0
    } while (node != NULL);
7205
7206
0
    if (list != NULL)
7207
0
  xmlFree(list);
7208
0
    return (ret);
7209
0
}
7210
7211
/**
7212
 * Dynamically searches for a ns-declaration which matches
7213
 * the given `nsName` in the ancestor-or-self axis of `node`.
7214
 *
7215
 * @param doc  the document
7216
 * @param node  the start node
7217
 * @param nsName  the searched namespace name
7218
 * @param retNs  the resulting ns-decl
7219
 * @param prefixed  if the found ns-decl must have a prefix
7220
 *                  (for attributes)
7221
 * @returns 1 if a ns-decl was found, 0 if not and -1 on API
7222
 *          and internal errors.
7223
 */
7224
static int
7225
xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
7226
           const xmlChar* nsName,
7227
           xmlNsPtr *retNs, int prefixed)
7228
0
{
7229
0
    xmlNodePtr cur, prev = NULL, out = NULL;
7230
0
    xmlNsPtr ns, prevns;
7231
7232
0
    if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7233
0
  return (-1);
7234
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
7235
0
        return(-1);
7236
7237
0
    *retNs = NULL;
7238
0
    if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7239
0
  *retNs = xmlTreeEnsureXMLDecl(doc);
7240
0
  if (*retNs == NULL)
7241
0
      return (-1);
7242
0
  return (1);
7243
0
    }
7244
0
    cur = node;
7245
0
    do {
7246
0
  if (cur->type == XML_ELEMENT_NODE) {
7247
0
      if (cur->nsDef != NULL) {
7248
0
    for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7249
0
        if (prefixed && (ns->prefix == NULL))
7250
0
      continue;
7251
0
        if (prev != NULL) {
7252
      /*
7253
      * Check the last level of ns-decls for a
7254
      * shadowing prefix.
7255
      */
7256
0
      prevns = prev->nsDef;
7257
0
      do {
7258
0
          if ((prevns->prefix == ns->prefix) ||
7259
0
        ((prevns->prefix != NULL) &&
7260
0
        (ns->prefix != NULL) &&
7261
0
        xmlStrEqual(prevns->prefix, ns->prefix))) {
7262
        /*
7263
        * Shadowed.
7264
        */
7265
0
        break;
7266
0
          }
7267
0
          prevns = prevns->next;
7268
0
      } while (prevns != NULL);
7269
0
      if (prevns != NULL)
7270
0
          continue;
7271
0
        }
7272
        /*
7273
        * Ns-name comparison.
7274
        */
7275
0
        if ((nsName == ns->href) ||
7276
0
      xmlStrEqual(nsName, ns->href)) {
7277
      /*
7278
      * At this point the prefix can only be shadowed,
7279
      * if we are the the (at least) 3rd level of
7280
      * ns-decls.
7281
      */
7282
0
      if (out) {
7283
0
          int ret;
7284
7285
0
          ret = xmlNsInScope(doc, node, prev, ns->prefix);
7286
0
          if (ret < 0)
7287
0
        return (-1);
7288
          /*
7289
          * TODO: Should we try to find a matching ns-name
7290
          * only once? This here keeps on searching.
7291
          * I think we should try further since, there might
7292
          * be an other matching ns-decl with an unshadowed
7293
          * prefix.
7294
          */
7295
0
          if (! ret)
7296
0
        continue;
7297
0
      }
7298
0
      *retNs = ns;
7299
0
      return (1);
7300
0
        }
7301
0
    }
7302
0
    out = prev;
7303
0
    prev = cur;
7304
0
      }
7305
0
  } else if (cur->type == XML_ENTITY_DECL)
7306
0
      return (0);
7307
0
  cur = cur->parent;
7308
0
    } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7309
0
    return (0);
7310
0
}
7311
7312
/**
7313
 * Dynamically searches for a ns-declaration which matches
7314
 * the given `nsName` in the ancestor-or-self axis of `node`.
7315
 *
7316
 * @param doc  the document
7317
 * @param node  the start node
7318
 * @param prefix  the searched namespace prefix
7319
 * @param retNs  the resulting ns-decl
7320
 * @returns 1 if a ns-decl was found, 0 if not and -1 on API
7321
 *          and internal errors.
7322
 */
7323
static int
7324
xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
7325
        const xmlChar* prefix,
7326
        xmlNsPtr *retNs)
7327
0
{
7328
0
    xmlNodePtr cur;
7329
0
    xmlNsPtr ns;
7330
7331
0
    if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
7332
0
        return(-1);
7333
7334
0
    if (retNs)
7335
0
  *retNs = NULL;
7336
0
    if (IS_STR_XML(prefix)) {
7337
0
  if (retNs) {
7338
0
      *retNs = xmlTreeEnsureXMLDecl(doc);
7339
0
      if (*retNs == NULL)
7340
0
    return (-1);
7341
0
  }
7342
0
  return (1);
7343
0
    }
7344
0
    cur = node;
7345
0
    do {
7346
0
  if (cur->type == XML_ELEMENT_NODE) {
7347
0
      if (cur->nsDef != NULL) {
7348
0
    ns = cur->nsDef;
7349
0
    do {
7350
0
        if ((prefix == ns->prefix) ||
7351
0
      xmlStrEqual(prefix, ns->prefix))
7352
0
        {
7353
      /*
7354
      * Disabled namespaces, e.g. xmlns:abc="".
7355
      */
7356
0
      if (ns->href == NULL)
7357
0
          return(0);
7358
0
      if (retNs)
7359
0
          *retNs = ns;
7360
0
      return (1);
7361
0
        }
7362
0
        ns = ns->next;
7363
0
    } while (ns != NULL);
7364
0
      }
7365
0
  } else if (cur->type == XML_ENTITY_DECL)
7366
0
      return (0);
7367
0
  cur = cur->parent;
7368
0
    } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7369
0
    return (0);
7370
0
}
7371
7372
/**
7373
 * Declares a new namespace on `elem`. It tries to use the
7374
 * given `prefix`. If a ns-decl with the given prefix is already existent
7375
 * on `elem`, it will generate an other prefix.
7376
 *
7377
 * @param doc  the doc
7378
 * @param elem  the element-node to declare on
7379
 * @param nsName  the namespace-name of the ns-decl
7380
 * @param prefix  the preferred prefix of the ns-decl
7381
 * @param checkShadow  ensure that the new ns-decl doesn't shadow
7382
 *                     ancestor ns-decls
7383
 * @returns 1 if a ns-decl was found, 0 if not and -1 on API
7384
 *          and internal errors.
7385
 */
7386
static xmlNsPtr
7387
xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7388
        xmlNodePtr elem,
7389
        const xmlChar *nsName,
7390
        const xmlChar *prefix,
7391
        int checkShadow)
7392
0
{
7393
7394
0
    xmlNsPtr ret;
7395
0
    char buf[50];
7396
0
    const xmlChar *pref;
7397
0
    int counter = 0;
7398
7399
0
    if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
7400
0
        return(NULL);
7401
    /*
7402
    * Create a ns-decl on @anchor.
7403
    */
7404
0
    pref = prefix;
7405
0
    while (1) {
7406
  /*
7407
  * Lookup whether the prefix is unused in elem's ns-decls.
7408
  */
7409
0
  if ((elem->nsDef != NULL) &&
7410
0
      (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7411
0
      goto ns_next_prefix;
7412
0
  if (checkShadow && elem->parent &&
7413
0
      ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7414
      /*
7415
      * Does it shadow ancestor ns-decls?
7416
      */
7417
0
      if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
7418
0
    goto ns_next_prefix;
7419
0
  }
7420
0
  ret = xmlNewNs(NULL, nsName, pref);
7421
0
  if (ret == NULL)
7422
0
      return (NULL);
7423
0
  if (elem->nsDef == NULL)
7424
0
      elem->nsDef = ret;
7425
0
  else {
7426
0
      xmlNsPtr ns2 = elem->nsDef;
7427
0
      while (ns2->next != NULL)
7428
0
    ns2 = ns2->next;
7429
0
      ns2->next = ret;
7430
0
  }
7431
0
  return (ret);
7432
0
ns_next_prefix:
7433
0
  counter++;
7434
0
  if (counter > 1000)
7435
0
      return (NULL);
7436
0
  if (prefix == NULL) {
7437
0
      snprintf((char *) buf, sizeof(buf),
7438
0
    "ns_%d", counter);
7439
0
  } else
7440
0
      snprintf((char *) buf, sizeof(buf),
7441
0
      "%.30s_%d", (char *)prefix, counter);
7442
0
  pref = BAD_CAST buf;
7443
0
    }
7444
0
}
7445
7446
/**
7447
 * Searches for a matching ns-name in the ns-decls of `nsMap`, if not
7448
 * found it will either declare it on `elem`, or store it in `doc->oldNs`.
7449
 * If a new ns-decl needs to be declared on `elem`, it tries to use the
7450
 * `ns->prefix` for it, if this prefix is already in use on `elem`, it will
7451
 * change the prefix or the new ns-decl.
7452
 *
7453
 * @param doc  the doc
7454
 * @param elem  the element-node to declare namespaces on
7455
 * @param ns  the ns-struct to use for the search
7456
 * @param retNs  the found/created ns-struct
7457
 * @param nsMap  the ns-map
7458
 * @param depth  the current tree depth
7459
 * @param ancestorsOnly  search in ancestor ns-decls only
7460
 * @param prefixed  if the searched ns-decl must have a prefix
7461
 *                  (for attributes)
7462
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
7463
 */
7464
static int
7465
xmlDOMWrapNSNormAcquireNormalizedNs(xmlDocPtr doc,
7466
           xmlNodePtr elem,
7467
           xmlNsPtr ns,
7468
           xmlNsPtr *retNs,
7469
           xmlNsMapPtr *nsMap,
7470
7471
           int depth,
7472
           int ancestorsOnly,
7473
           int prefixed)
7474
0
{
7475
0
    xmlNsMapItemPtr mi;
7476
7477
0
    if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
7478
0
  (nsMap == NULL))
7479
0
  return (-1);
7480
7481
0
    *retNs = NULL;
7482
    /*
7483
    * Handle XML namespace.
7484
    */
7485
0
    if (IS_STR_XML(ns->prefix)) {
7486
  /*
7487
  * Insert XML namespace mapping.
7488
  */
7489
0
  *retNs = xmlTreeEnsureXMLDecl(doc);
7490
0
  if (*retNs == NULL)
7491
0
      return (-1);
7492
0
  return (0);
7493
0
    }
7494
    /*
7495
    * If the search should be done in ancestors only and no
7496
    * @elem (the first ancestor) was specified, then skip the search.
7497
    */
7498
0
    if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
7499
0
  (! (ancestorsOnly && (elem == NULL))))
7500
0
    {
7501
  /*
7502
  * Try to find an equal ns-name in in-scope ns-decls.
7503
  */
7504
0
  XML_NSMAP_FOREACH(*nsMap, mi) {
7505
0
      if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7506
    /*
7507
    * ancestorsOnly: This should be turned on to gain speed,
7508
    * if one knows that the branch itself was already
7509
    * ns-wellformed and no stale references existed.
7510
    * I.e. it searches in the ancestor axis only.
7511
    */
7512
0
    ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
7513
    /* Skip shadowed prefixes. */
7514
0
    (mi->shadowDepth == -1) &&
7515
    /* Skip xmlns="" or xmlns:foo="". */
7516
0
    ((mi->newNs->href != NULL) &&
7517
0
    (mi->newNs->href[0] != 0)) &&
7518
    /* Ensure a prefix if wanted. */
7519
0
    ((! prefixed) || (mi->newNs->prefix != NULL)) &&
7520
    /* Equal ns name */
7521
0
    ((mi->newNs->href == ns->href) ||
7522
0
    xmlStrEqual(mi->newNs->href, ns->href))) {
7523
    /* Set the mapping. */
7524
0
    mi->oldNs = ns;
7525
0
    *retNs = mi->newNs;
7526
0
    return (0);
7527
0
      }
7528
0
  }
7529
0
    }
7530
    /*
7531
    * No luck, the namespace is out of scope or shadowed.
7532
    */
7533
0
    if (elem == NULL) {
7534
0
  xmlNsPtr tmpns;
7535
7536
  /*
7537
  * Store ns-decls in "oldNs" of the document-node.
7538
  */
7539
0
  tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
7540
0
  if (tmpns == NULL)
7541
0
      return (-1);
7542
  /*
7543
  * Insert mapping.
7544
  */
7545
0
  if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
7546
0
    tmpns, XML_TREE_NSMAP_DOC) == NULL) {
7547
0
      return (-1);
7548
0
  }
7549
0
  *retNs = tmpns;
7550
0
    } else {
7551
0
  xmlNsPtr tmpns;
7552
7553
0
  tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
7554
0
      ns->prefix, 0);
7555
0
  if (tmpns == NULL)
7556
0
      return (-1);
7557
7558
0
  if (*nsMap != NULL) {
7559
      /*
7560
      * Does it shadow ancestor ns-decls?
7561
      */
7562
0
      XML_NSMAP_FOREACH(*nsMap, mi) {
7563
0
    if ((mi->depth < depth) &&
7564
0
        (mi->shadowDepth == -1) &&
7565
0
        ((ns->prefix == mi->newNs->prefix) ||
7566
0
        xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7567
        /*
7568
        * Shadows.
7569
        */
7570
0
        mi->shadowDepth = depth;
7571
0
        break;
7572
0
    }
7573
0
      }
7574
0
  }
7575
0
  if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
7576
0
      return (-1);
7577
0
  }
7578
0
  *retNs = tmpns;
7579
0
    }
7580
0
    return (0);
7581
0
}
7582
7583
typedef enum {
7584
    XML_DOM_RECONNS_REMOVEREDUND = 1<<0
7585
} xmlDOMReconcileNSOptions;
7586
7587
/**
7588
 * Fix up namespaces.
7589
 *
7590
 * Ensures that ns-references point to ns-decls hold on element-nodes.
7591
 * Ensures that the tree is namespace wellformed by creating additional
7592
 * ns-decls where needed. Note that, since prefixes of already existent
7593
 * ns-decls can be shadowed by this process, it could break QNames in
7594
 * attribute values or element content.
7595
 *
7596
 * NOTE: This function was not intensively tested.
7597
 *
7598
 * @param ctxt  DOM wrapper context, unused at the moment
7599
 * @param elem  the element-node
7600
 * @param options  option flags
7601
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
7602
 */
7603
int
7604
xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxt *ctxt ATTRIBUTE_UNUSED,
7605
            xmlNode *elem,
7606
            int options)
7607
0
{
7608
0
    int depth = -1, adoptns = 0, parnsdone = 0;
7609
0
    xmlNsPtr ns, prevns;
7610
0
    xmlDocPtr doc;
7611
0
    xmlNodePtr cur, curElem = NULL;
7612
0
    xmlNsMapPtr nsMap = NULL;
7613
0
    xmlNsMapItemPtr /* topmi = NULL, */ mi;
7614
    /* @ancestorsOnly should be set by an option flag. */
7615
0
    int ancestorsOnly = 0;
7616
0
    int optRemoveRedundantNS =
7617
0
  ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
7618
0
    xmlNsPtr *listRedund = NULL;
7619
0
    int sizeRedund = 0, nbRedund = 0, ret = 0, i, j;
7620
7621
0
    if ((elem == NULL) || (elem->doc == NULL) ||
7622
0
  (elem->type != XML_ELEMENT_NODE))
7623
0
  return (-1);
7624
7625
0
    doc = elem->doc;
7626
0
    cur = elem;
7627
0
    do {
7628
0
  switch (cur->type) {
7629
0
      case XML_ELEMENT_NODE:
7630
0
    adoptns = 1;
7631
0
    curElem = cur;
7632
0
    depth++;
7633
    /*
7634
    * Namespace declarations.
7635
    */
7636
0
    if (cur->nsDef != NULL) {
7637
0
        prevns = NULL;
7638
0
        ns = cur->nsDef;
7639
0
        while (ns != NULL) {
7640
0
      if (! parnsdone) {
7641
0
          if ((elem->parent) &&
7642
0
        ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7643
        /*
7644
        * Gather ancestor in-scope ns-decls.
7645
        */
7646
0
        if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7647
0
            elem->parent) == -1)
7648
0
            ret = -1;
7649
0
          }
7650
0
          parnsdone = 1;
7651
0
      }
7652
7653
      /*
7654
      * Lookup the ns ancestor-axis for equal ns-decls in scope.
7655
      */
7656
0
      if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
7657
0
          XML_NSMAP_FOREACH(nsMap, mi) {
7658
0
        if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7659
0
            (mi->shadowDepth == -1) &&
7660
0
            ((ns->prefix == mi->newNs->prefix) ||
7661
0
              xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
7662
0
            ((ns->href == mi->newNs->href) ||
7663
0
              xmlStrEqual(ns->href, mi->newNs->href)))
7664
0
        {
7665
            /*
7666
            * A redundant ns-decl was found.
7667
            * Add it to the list of redundant ns-decls.
7668
            */
7669
0
            if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
7670
0
          &sizeRedund, &nbRedund, ns, mi->newNs) == -1) {
7671
0
          ret = -1;
7672
0
                                    } else {
7673
                                        /*
7674
                                        * Remove the ns-decl from the element-node.
7675
                                        */
7676
0
                                        if (prevns)
7677
0
                                            prevns->next = ns->next;
7678
0
                                        else
7679
0
                                            cur->nsDef = ns->next;
7680
0
                                        goto next_ns_decl;
7681
0
                                    }
7682
0
        }
7683
0
          }
7684
0
      }
7685
7686
      /*
7687
      * Skip ns-references handling if the referenced
7688
      * ns-decl is declared on the same element.
7689
      */
7690
0
      if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
7691
0
          adoptns = 0;
7692
      /*
7693
      * Does it shadow any ns-decl?
7694
      */
7695
0
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
7696
0
          XML_NSMAP_FOREACH(nsMap, mi) {
7697
0
        if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7698
0
            (mi->shadowDepth == -1) &&
7699
0
            ((ns->prefix == mi->newNs->prefix) ||
7700
0
            xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7701
7702
0
            mi->shadowDepth = depth;
7703
0
        }
7704
0
          }
7705
0
      }
7706
      /*
7707
      * Push mapping.
7708
      */
7709
0
      if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
7710
0
          depth) == NULL)
7711
0
          ret = -1;
7712
7713
0
      prevns = ns;
7714
0
next_ns_decl:
7715
0
      ns = ns->next;
7716
0
        }
7717
0
    }
7718
0
    if (! adoptns)
7719
0
        goto ns_end;
7720
                /* Falls through. */
7721
0
      case XML_ATTRIBUTE_NODE:
7722
    /* No ns, no fun. */
7723
0
    if (cur->ns == NULL)
7724
0
        goto ns_end;
7725
7726
0
    if (! parnsdone) {
7727
0
        if ((elem->parent) &&
7728
0
      ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7729
0
      if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7730
0
        elem->parent) == -1)
7731
0
          ret = -1;
7732
0
        }
7733
0
        parnsdone = 1;
7734
0
    }
7735
    /*
7736
    * Adjust the reference if this was a redundant ns-decl.
7737
    */
7738
0
    if (listRedund) {
7739
0
       for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
7740
0
           if (cur->ns == listRedund[j]) {
7741
0
         cur->ns = listRedund[++j];
7742
0
         break;
7743
0
           }
7744
0
       }
7745
0
    }
7746
    /*
7747
    * Adopt ns-references.
7748
    */
7749
0
    if (XML_NSMAP_NOTEMPTY(nsMap)) {
7750
        /*
7751
        * Search for a mapping.
7752
        */
7753
0
        XML_NSMAP_FOREACH(nsMap, mi) {
7754
0
      if ((mi->shadowDepth == -1) &&
7755
0
          (cur->ns == mi->oldNs)) {
7756
7757
0
          cur->ns = mi->newNs;
7758
0
          goto ns_end;
7759
0
      }
7760
0
        }
7761
0
    }
7762
    /*
7763
    * Acquire a normalized ns-decl and add it to the map.
7764
    */
7765
0
    if (xmlDOMWrapNSNormAcquireNormalizedNs(doc, curElem,
7766
0
      cur->ns, &ns,
7767
0
      &nsMap, depth,
7768
0
      ancestorsOnly,
7769
0
      (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
7770
0
        ret = -1;
7771
0
    cur->ns = ns;
7772
7773
0
ns_end:
7774
0
    if ((cur->type == XML_ELEMENT_NODE) &&
7775
0
        (cur->properties != NULL)) {
7776
        /*
7777
        * Process attributes.
7778
        */
7779
0
        cur = (xmlNodePtr) cur->properties;
7780
0
        continue;
7781
0
    }
7782
0
    break;
7783
0
      default:
7784
0
    goto next_sibling;
7785
0
  }
7786
0
into_content:
7787
0
  if ((cur->type == XML_ELEMENT_NODE) &&
7788
0
      (cur->children != NULL)) {
7789
      /*
7790
      * Process content of element-nodes only.
7791
      */
7792
0
      cur = cur->children;
7793
0
      continue;
7794
0
  }
7795
0
next_sibling:
7796
0
  if (cur == elem)
7797
0
      break;
7798
0
  if (cur->type == XML_ELEMENT_NODE) {
7799
0
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
7800
    /*
7801
    * Pop mappings.
7802
    */
7803
0
    while ((nsMap->last != NULL) &&
7804
0
        (nsMap->last->depth >= depth))
7805
0
    {
7806
0
        XML_NSMAP_POP(nsMap, mi)
7807
0
    }
7808
    /*
7809
    * Unshadow.
7810
    */
7811
0
    XML_NSMAP_FOREACH(nsMap, mi) {
7812
0
        if (mi->shadowDepth >= depth)
7813
0
      mi->shadowDepth = -1;
7814
0
    }
7815
0
      }
7816
0
      depth--;
7817
0
  }
7818
0
  if (cur->next != NULL)
7819
0
      cur = cur->next;
7820
0
  else {
7821
0
      if (cur->type == XML_ATTRIBUTE_NODE) {
7822
0
    cur = cur->parent;
7823
0
    goto into_content;
7824
0
      }
7825
0
      cur = cur->parent;
7826
0
      goto next_sibling;
7827
0
  }
7828
0
    } while (cur != NULL);
7829
7830
0
    if (listRedund) {
7831
0
  for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
7832
0
      xmlFreeNs(listRedund[j]);
7833
0
  }
7834
0
  xmlFree(listRedund);
7835
0
    }
7836
0
    if (nsMap != NULL)
7837
0
  xmlDOMWrapNsMapFree(nsMap);
7838
0
    return (ret);
7839
0
}
7840
7841
/**
7842
 * Ensures that ns-references point to `destDoc`: either to
7843
 * `elements->nsDef` entries if `destParent` is given, or to
7844
 * `destDoc->oldNs` otherwise.
7845
 *
7846
 * If `destParent` is given, it ensures that the tree is namespace
7847
 * wellformed by creating additional ns-decls where needed.
7848
 * Note that, since prefixes of already existent ns-decls can be
7849
 * shadowed by this process, it could break QNames in attribute
7850
 * values or element content.
7851
 *
7852
 * NOTE: This function was not intensively tested.
7853
 *
7854
 * @param ctxt  the optional context for custom processing
7855
 * @param sourceDoc  the optional sourceDoc
7856
 * @param node  the element-node to start with
7857
 * @param destDoc  the destination doc for adoption
7858
 * @param destParent  the optional new parent of `node` in `destDoc`
7859
 * @param options  option flags
7860
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
7861
 */
7862
static int
7863
xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
7864
          xmlDocPtr sourceDoc ATTRIBUTE_UNUSED,
7865
          xmlNodePtr node,
7866
          xmlDocPtr destDoc,
7867
          xmlNodePtr destParent,
7868
          int options ATTRIBUTE_UNUSED)
7869
0
{
7870
0
    int ret = 0;
7871
0
    xmlNodePtr cur, curElem = NULL;
7872
0
    xmlNsMapPtr nsMap = NULL;
7873
0
    xmlNsMapItemPtr mi;
7874
0
    xmlNsPtr ns = NULL;
7875
0
    int depth = -1;
7876
    /* gather @parent's ns-decls. */
7877
0
    int parnsdone;
7878
    /* @ancestorsOnly should be set per option. */
7879
0
    int ancestorsOnly = 0;
7880
7881
    /*
7882
    * Get the ns-map from the context if available.
7883
    */
7884
0
    if (ctxt)
7885
0
  nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
7886
    /*
7887
    * Disable search for ns-decls in the parent-axis of the
7888
    * destination element, if:
7889
    * 1) there's no destination parent
7890
    * 2) custom ns-reference handling is used
7891
    */
7892
0
    if ((destParent == NULL) ||
7893
0
  (ctxt && ctxt->getNsForNodeFunc))
7894
0
    {
7895
0
  parnsdone = 1;
7896
0
    } else
7897
0
  parnsdone = 0;
7898
7899
0
    cur = node;
7900
7901
0
    while (cur != NULL) {
7902
0
        if (cur->doc != destDoc) {
7903
0
            if (xmlNodeSetDoc(cur, destDoc) < 0)
7904
0
                ret = -1;
7905
0
        }
7906
7907
0
  switch (cur->type) {
7908
0
      case XML_XINCLUDE_START:
7909
0
      case XML_XINCLUDE_END:
7910
    /*
7911
    * TODO
7912
    */
7913
0
    ret = -1;
7914
0
                goto leave_node;
7915
0
      case XML_ELEMENT_NODE:
7916
0
    curElem = cur;
7917
0
    depth++;
7918
    /*
7919
    * Namespace declarations.
7920
    * - ns->href and ns->prefix are never in the dict, so
7921
    *   we need not move the values over to the destination dict.
7922
    * - Note that for custom handling of ns-references,
7923
    *   the ns-decls need not be stored in the ns-map,
7924
    *   since they won't be referenced by node->ns.
7925
    */
7926
0
    if ((cur->nsDef) &&
7927
0
        ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
7928
0
    {
7929
0
        if (! parnsdone) {
7930
      /*
7931
      * Gather @parent's in-scope ns-decls.
7932
      */
7933
0
      if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7934
0
          destParent) == -1)
7935
0
          ret = -1;
7936
0
      parnsdone = 1;
7937
0
        }
7938
0
        for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7939
      /*
7940
      * NOTE: ns->prefix and ns->href are never in the dict.
7941
      */
7942
      /*
7943
      * Does it shadow any ns-decl?
7944
      */
7945
0
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
7946
0
          XML_NSMAP_FOREACH(nsMap, mi) {
7947
0
        if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7948
0
            (mi->shadowDepth == -1) &&
7949
0
            ((ns->prefix == mi->newNs->prefix) ||
7950
0
            xmlStrEqual(ns->prefix,
7951
0
            mi->newNs->prefix))) {
7952
7953
0
            mi->shadowDepth = depth;
7954
0
        }
7955
0
          }
7956
0
      }
7957
      /*
7958
      * Push mapping.
7959
      */
7960
0
      if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
7961
0
          ns, ns, depth) == NULL)
7962
0
          ret = -1;
7963
0
        }
7964
0
    }
7965
                /* Falls through. */
7966
0
      case XML_ATTRIBUTE_NODE:
7967
    /* No namespace, no fun. */
7968
0
    if (cur->ns == NULL)
7969
0
        goto ns_end;
7970
7971
0
    if (! parnsdone) {
7972
0
        if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7973
0
      destParent) == -1)
7974
0
      ret = -1;
7975
0
        parnsdone = 1;
7976
0
    }
7977
    /*
7978
    * Adopt ns-references.
7979
    */
7980
0
    if (XML_NSMAP_NOTEMPTY(nsMap)) {
7981
        /*
7982
        * Search for a mapping.
7983
        */
7984
0
        XML_NSMAP_FOREACH(nsMap, mi) {
7985
0
      if ((mi->shadowDepth == -1) &&
7986
0
          (cur->ns == mi->oldNs)) {
7987
7988
0
          cur->ns = mi->newNs;
7989
0
          goto ns_end;
7990
0
      }
7991
0
        }
7992
0
    }
7993
    /*
7994
    * No matching namespace in scope. We need a new one.
7995
    */
7996
0
    if ((ctxt) && (ctxt->getNsForNodeFunc)) {
7997
        /*
7998
        * User-defined behaviour.
7999
        */
8000
0
        ns = ctxt->getNsForNodeFunc(ctxt, cur,
8001
0
      cur->ns->href, cur->ns->prefix);
8002
        /*
8003
        * Insert mapping if ns is available; it's the users fault
8004
        * if not.
8005
        */
8006
0
        if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8007
0
          cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8008
0
      ret = -1;
8009
0
        cur->ns = ns;
8010
0
    } else {
8011
        /*
8012
        * Acquire a normalized ns-decl and add it to the map.
8013
        */
8014
0
        if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc,
8015
      /* ns-decls on curElem or on destDoc->oldNs */
8016
0
      destParent ? curElem : NULL,
8017
0
      cur->ns, &ns,
8018
0
      &nsMap, depth,
8019
0
      ancestorsOnly,
8020
      /* ns-decls must be prefixed for attributes. */
8021
0
      (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8022
0
      ret = -1;
8023
0
        cur->ns = ns;
8024
0
    }
8025
8026
0
ns_end:
8027
0
    if (cur->type == XML_ELEMENT_NODE) {
8028
0
        cur->psvi = NULL;
8029
0
        cur->line = 0;
8030
0
        cur->extra = 0;
8031
        /*
8032
        * Walk attributes.
8033
        */
8034
0
        if (cur->properties != NULL) {
8035
      /*
8036
      * Process first attribute node.
8037
      */
8038
0
      cur = (xmlNodePtr) cur->properties;
8039
0
      continue;
8040
0
        }
8041
0
    }
8042
0
    break;
8043
0
      case XML_TEXT_NODE:
8044
0
      case XML_CDATA_SECTION_NODE:
8045
0
      case XML_PI_NODE:
8046
0
      case XML_COMMENT_NODE:
8047
0
      case XML_ENTITY_REF_NODE:
8048
0
    goto leave_node;
8049
0
      default:
8050
0
    ret = -1;
8051
0
  }
8052
  /*
8053
  * Walk the tree.
8054
  */
8055
0
  if (cur->children != NULL) {
8056
0
      cur = cur->children;
8057
0
      continue;
8058
0
  }
8059
8060
0
leave_node:
8061
0
  if (cur == node)
8062
0
      break;
8063
0
  if ((cur->type == XML_ELEMENT_NODE) ||
8064
0
      (cur->type == XML_XINCLUDE_START) ||
8065
0
      (cur->type == XML_XINCLUDE_END))
8066
0
  {
8067
      /*
8068
      * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8069
      */
8070
0
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
8071
    /*
8072
    * Pop mappings.
8073
    */
8074
0
    while ((nsMap->last != NULL) &&
8075
0
        (nsMap->last->depth >= depth))
8076
0
    {
8077
0
        XML_NSMAP_POP(nsMap, mi)
8078
0
    }
8079
    /*
8080
    * Unshadow.
8081
    */
8082
0
    XML_NSMAP_FOREACH(nsMap, mi) {
8083
0
        if (mi->shadowDepth >= depth)
8084
0
      mi->shadowDepth = -1;
8085
0
    }
8086
0
      }
8087
0
      depth--;
8088
0
  }
8089
0
  if (cur->next != NULL)
8090
0
      cur = cur->next;
8091
0
  else if ((cur->type == XML_ATTRIBUTE_NODE) &&
8092
0
      (cur->parent->children != NULL))
8093
0
  {
8094
0
      cur = cur->parent->children;
8095
0
  } else {
8096
0
      cur = cur->parent;
8097
0
      goto leave_node;
8098
0
  }
8099
0
    }
8100
8101
    /*
8102
    * Cleanup.
8103
    */
8104
0
    if (nsMap != NULL) {
8105
0
  if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8106
      /*
8107
      * Just cleanup the map but don't free.
8108
      */
8109
0
      if (nsMap->first) {
8110
0
    if (nsMap->pool)
8111
0
        nsMap->last->next = nsMap->pool;
8112
0
    nsMap->pool = nsMap->first;
8113
0
    nsMap->first = NULL;
8114
0
      }
8115
0
  } else
8116
0
      xmlDOMWrapNsMapFree(nsMap);
8117
0
    }
8118
0
    return(ret);
8119
0
}
8120
8121
/**
8122
 * Clone a node and fix namespaces.
8123
 *
8124
 * References of out-of scope ns-decls are remapped to point to `destDoc`.
8125
 * If `destParent` is given, then nsDef entries on element-nodes are used.
8126
 * If *no* `destParent` is given, then `destDoc->oldNs` entries are used.
8127
 * This is the case when you don't know already where the cloned branch
8128
 * will be added to.
8129
 *
8130
 * If `destParent` is given, it ensures that the tree is namespace
8131
 * wellformed by creating additional ns-decls where needed.
8132
 * Note that, since prefixes of already existent ns-decls can be
8133
 * shadowed by this process, it could break QNames in attribute
8134
 * values or element content.
8135
 *
8136
 * @param ctxt  the optional context for custom processing
8137
 * @param sourceDoc  the optional sourceDoc
8138
 * @param node  the node to start with
8139
 * @param resNode  the clone of the given `node`
8140
 * @param destDoc  the destination doc
8141
 * @param destParent  the optional new parent of `node` in `destDoc`
8142
 * @param deep  descend into child if set
8143
 * @param options  option flags
8144
 * @returns 0 if the operation succeeded,
8145
 *          1 if a node of unsupported (or not yet supported) type was given,
8146
 *          -1 on API/internal errors.
8147
 */
8148
int
8149
xmlDOMWrapCloneNode(xmlDOMWrapCtxt *ctxt,
8150
          xmlDoc *sourceDoc,
8151
          xmlNode *node,
8152
          xmlNode **resNode,
8153
          xmlDoc *destDoc,
8154
          xmlNode *destParent,
8155
          int deep,
8156
          int options ATTRIBUTE_UNUSED)
8157
0
{
8158
0
    int ret = 0;
8159
0
    xmlNodePtr cur, cloneElem = NULL;
8160
0
    xmlNsMapPtr nsMap = NULL;
8161
0
    xmlNsMapItemPtr mi;
8162
0
    xmlNsPtr ns;
8163
0
    int depth = -1;
8164
    /* int adoptStr = 1; */
8165
    /* gather @parent's ns-decls. */
8166
0
    int parnsdone = 0;
8167
    /*
8168
    * @ancestorsOnly:
8169
    * TODO: @ancestorsOnly should be set per option.
8170
    *
8171
    */
8172
0
    int ancestorsOnly = 0;
8173
0
    xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
8174
0
    xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
8175
0
    xmlDictPtr dict; /* The destination dict */
8176
8177
0
    if ((node == NULL) || (resNode == NULL) || (destDoc == NULL) ||
8178
0
  ((destParent != NULL) && (destParent->doc != destDoc)))
8179
0
  return(-1);
8180
    /*
8181
    * TODO: Initially we support only element-nodes.
8182
    */
8183
0
    if (node->type != XML_ELEMENT_NODE)
8184
0
  return(1);
8185
    /*
8186
    * Check node->doc sanity.
8187
    */
8188
0
    if ((node->doc != NULL) && (sourceDoc != NULL) &&
8189
0
  (node->doc != sourceDoc)) {
8190
  /*
8191
  * Might be an XIncluded node.
8192
  */
8193
0
  return (-1);
8194
0
    }
8195
0
    if (sourceDoc == NULL)
8196
0
  sourceDoc = node->doc;
8197
0
    if (sourceDoc == NULL)
8198
0
        return (-1);
8199
8200
0
    dict = destDoc->dict;
8201
    /*
8202
    * Reuse the namespace map of the context.
8203
    */
8204
0
    if (ctxt)
8205
0
  nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8206
8207
0
    *resNode = NULL;
8208
8209
0
    cur = node;
8210
0
    while (cur != NULL) {
8211
0
  if (cur->doc != sourceDoc) {
8212
      /*
8213
      * We'll assume XIncluded nodes if the doc differs.
8214
      * TODO: Do we need to reconciliate XIncluded nodes?
8215
      * TODO: This here returns -1 in this case.
8216
      */
8217
0
      goto internal_error;
8218
0
  }
8219
  /*
8220
  * Create a new node.
8221
  */
8222
0
  switch (cur->type) {
8223
0
      case XML_XINCLUDE_START:
8224
0
      case XML_XINCLUDE_END:
8225
    /*
8226
    * TODO: What to do with XInclude?
8227
    */
8228
0
    goto internal_error;
8229
0
    break;
8230
0
      case XML_ELEMENT_NODE:
8231
0
      case XML_TEXT_NODE:
8232
0
      case XML_CDATA_SECTION_NODE:
8233
0
      case XML_COMMENT_NODE:
8234
0
      case XML_PI_NODE:
8235
0
      case XML_DOCUMENT_FRAG_NODE:
8236
0
      case XML_ENTITY_REF_NODE:
8237
    /*
8238
    * Nodes of xmlNode structure.
8239
    */
8240
0
    clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
8241
0
    if (clone == NULL)
8242
0
        goto internal_error;
8243
0
    memset(clone, 0, sizeof(xmlNode));
8244
    /*
8245
    * Set hierarchical links.
8246
    */
8247
0
    if (resultClone != NULL) {
8248
0
        clone->parent = parentClone;
8249
0
        if (prevClone) {
8250
0
      prevClone->next = clone;
8251
0
      clone->prev = prevClone;
8252
0
        } else
8253
0
      parentClone->children = clone;
8254
0
                    parentClone->last = clone;
8255
0
    } else
8256
0
        resultClone = clone;
8257
8258
0
    break;
8259
0
      case XML_ATTRIBUTE_NODE:
8260
    /*
8261
    * Attributes (xmlAttr).
8262
    */
8263
0
    clone = xmlMalloc(sizeof(xmlAttr));
8264
0
    if (clone == NULL)
8265
0
        goto internal_error;
8266
0
    memset(clone, 0, sizeof(xmlAttr));
8267
    /*
8268
    * Set hierarchical links.
8269
    * TODO: Change this to add to the end of attributes.
8270
    */
8271
0
    if (resultClone != NULL) {
8272
0
        clone->parent = parentClone;
8273
0
        if (prevClone) {
8274
0
      prevClone->next = clone;
8275
0
      clone->prev = prevClone;
8276
0
        } else
8277
0
      parentClone->properties = (xmlAttrPtr) clone;
8278
0
    } else
8279
0
        resultClone = clone;
8280
0
    break;
8281
0
      default:
8282
    /*
8283
    * TODO QUESTION: Any other nodes expected?
8284
    */
8285
0
    goto internal_error;
8286
0
  }
8287
8288
0
  clone->type = cur->type;
8289
0
  clone->doc = destDoc;
8290
8291
  /*
8292
  * Clone the name of the node if any.
8293
  */
8294
0
  if (cur->name == xmlStringText)
8295
0
      clone->name = xmlStringText;
8296
0
  else if (cur->name == xmlStringTextNoenc)
8297
      /*
8298
      * NOTE: Although xmlStringTextNoenc is never assigned to a node
8299
      *   in tree.c, it might be set in Libxslt via
8300
      *   "xsl:disable-output-escaping".
8301
      */
8302
0
      clone->name = xmlStringTextNoenc;
8303
0
  else if (cur->name == xmlStringComment)
8304
0
      clone->name = xmlStringComment;
8305
0
  else if (cur->name != NULL) {
8306
0
            if (dict != NULL)
8307
0
                clone->name = xmlDictLookup(dict, cur->name, -1);
8308
0
            else
8309
0
                clone->name = xmlStrdup(cur->name);
8310
0
            if (clone->name == NULL)
8311
0
                goto internal_error;
8312
0
  }
8313
8314
0
  switch (cur->type) {
8315
0
      case XML_XINCLUDE_START:
8316
0
      case XML_XINCLUDE_END:
8317
    /*
8318
    * TODO
8319
    */
8320
0
    return (-1);
8321
0
      case XML_ELEMENT_NODE:
8322
0
    cloneElem = clone;
8323
0
    depth++;
8324
    /*
8325
    * Namespace declarations.
8326
    */
8327
0
    if (cur->nsDef != NULL) {
8328
0
        if (! parnsdone) {
8329
0
      if (destParent && (ctxt == NULL)) {
8330
          /*
8331
          * Gather @parent's in-scope ns-decls.
8332
          */
8333
0
          if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8334
0
        destParent) == -1)
8335
0
        goto internal_error;
8336
0
      }
8337
0
      parnsdone = 1;
8338
0
        }
8339
        /*
8340
        * Clone namespace declarations.
8341
        */
8342
0
        cloneNsDefSlot = &(clone->nsDef);
8343
0
        for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8344
      /*
8345
      * Create a new xmlNs.
8346
      */
8347
0
      cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
8348
0
      if (cloneNs == NULL)
8349
0
          goto internal_error;
8350
0
      memset(cloneNs, 0, sizeof(xmlNs));
8351
0
      cloneNs->type = XML_LOCAL_NAMESPACE;
8352
8353
0
      if (ns->href != NULL) {
8354
0
          cloneNs->href = xmlStrdup(ns->href);
8355
0
                            if (cloneNs->href == NULL) {
8356
0
                                xmlFreeNs(cloneNs);
8357
0
                                goto internal_error;
8358
0
                            }
8359
0
                        }
8360
0
      if (ns->prefix != NULL) {
8361
0
          cloneNs->prefix = xmlStrdup(ns->prefix);
8362
0
                            if (cloneNs->prefix == NULL) {
8363
0
                                xmlFreeNs(cloneNs);
8364
0
                                goto internal_error;
8365
0
                            }
8366
0
                        }
8367
8368
0
      *cloneNsDefSlot = cloneNs;
8369
0
      cloneNsDefSlot = &(cloneNs->next);
8370
8371
      /*
8372
      * Note that for custom handling of ns-references,
8373
      * the ns-decls need not be stored in the ns-map,
8374
      * since they won't be referenced by node->ns.
8375
      */
8376
0
      if ((ctxt == NULL) ||
8377
0
          (ctxt->getNsForNodeFunc == NULL))
8378
0
      {
8379
          /*
8380
          * Does it shadow any ns-decl?
8381
          */
8382
0
          if (XML_NSMAP_NOTEMPTY(nsMap)) {
8383
0
        XML_NSMAP_FOREACH(nsMap, mi) {
8384
0
            if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8385
0
          (mi->shadowDepth == -1) &&
8386
0
          ((ns->prefix == mi->newNs->prefix) ||
8387
0
          xmlStrEqual(ns->prefix,
8388
0
          mi->newNs->prefix))) {
8389
          /*
8390
          * Mark as shadowed at the current
8391
          * depth.
8392
          */
8393
0
          mi->shadowDepth = depth;
8394
0
            }
8395
0
        }
8396
0
          }
8397
          /*
8398
          * Push mapping.
8399
          */
8400
0
          if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8401
0
        ns, cloneNs, depth) == NULL)
8402
0
        goto internal_error;
8403
0
      }
8404
0
        }
8405
0
    }
8406
    /* cur->ns will be processed further down. */
8407
0
    break;
8408
0
      case XML_ATTRIBUTE_NODE:
8409
    /* IDs will be processed further down. */
8410
    /* cur->ns will be processed further down. */
8411
0
    break;
8412
0
      case XML_PI_NODE:
8413
0
      case XML_COMMENT_NODE:
8414
0
      case XML_TEXT_NODE:
8415
0
      case XML_CDATA_SECTION_NODE:
8416
    /*
8417
    * Note that this will also cover the values of attributes.
8418
    */
8419
0
                if (cur->content != NULL) {
8420
0
                    clone->content = xmlStrdup(cur->content);
8421
0
                    if (clone->content == NULL)
8422
0
                        goto internal_error;
8423
0
                }
8424
0
    goto leave_node;
8425
0
      case XML_ENTITY_REF_NODE:
8426
0
    if (sourceDoc != destDoc) {
8427
0
        if ((destDoc->intSubset) || (destDoc->extSubset)) {
8428
0
      xmlEntityPtr ent;
8429
      /*
8430
      * Different doc: Assign new entity-node if available.
8431
      */
8432
0
      ent = xmlGetDocEntity(destDoc, cur->name);
8433
0
      if (ent != NULL) {
8434
0
          clone->content = ent->content;
8435
0
          clone->children = (xmlNodePtr) ent;
8436
0
          clone->last = (xmlNodePtr) ent;
8437
0
      }
8438
0
        }
8439
0
    } else {
8440
        /*
8441
        * Same doc: Use the current node's entity declaration
8442
        * and value.
8443
        */
8444
0
        clone->content = cur->content;
8445
0
        clone->children = cur->children;
8446
0
        clone->last = cur->last;
8447
0
    }
8448
0
    goto leave_node;
8449
0
      default:
8450
0
    goto internal_error;
8451
0
  }
8452
8453
0
  if (cur->ns == NULL)
8454
0
      goto end_ns_reference;
8455
8456
/* handle_ns_reference: */
8457
  /*
8458
  ** The following will take care of references to ns-decls ********
8459
  ** and is intended only for element- and attribute-nodes.
8460
  **
8461
  */
8462
0
  if (! parnsdone) {
8463
0
      if (destParent && (ctxt == NULL)) {
8464
0
    if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
8465
0
        goto internal_error;
8466
0
      }
8467
0
      parnsdone = 1;
8468
0
  }
8469
  /*
8470
  * Adopt ns-references.
8471
  */
8472
0
  if (XML_NSMAP_NOTEMPTY(nsMap)) {
8473
      /*
8474
      * Search for a mapping.
8475
      */
8476
0
      XML_NSMAP_FOREACH(nsMap, mi) {
8477
0
    if ((mi->shadowDepth == -1) &&
8478
0
        (cur->ns == mi->oldNs)) {
8479
        /*
8480
        * This is the nice case: a mapping was found.
8481
        */
8482
0
        clone->ns = mi->newNs;
8483
0
        goto end_ns_reference;
8484
0
    }
8485
0
      }
8486
0
  }
8487
  /*
8488
  * No matching namespace in scope. We need a new one.
8489
  */
8490
0
  if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
8491
      /*
8492
      * User-defined behaviour.
8493
      */
8494
0
      ns = ctxt->getNsForNodeFunc(ctxt, cur,
8495
0
    cur->ns->href, cur->ns->prefix);
8496
      /*
8497
      * Add user's mapping.
8498
      */
8499
0
      if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8500
0
    cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8501
0
    goto internal_error;
8502
0
      clone->ns = ns;
8503
0
  } else {
8504
      /*
8505
      * Acquire a normalized ns-decl and add it to the map.
8506
      */
8507
0
      if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc,
8508
    /* ns-decls on cloneElem or on destDoc->oldNs */
8509
0
    destParent ? cloneElem : NULL,
8510
0
    cur->ns, &ns,
8511
0
    &nsMap, depth,
8512
    /* if we need to search only in the ancestor-axis */
8513
0
    ancestorsOnly,
8514
    /* ns-decls must be prefixed for attributes. */
8515
0
    (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8516
0
    goto internal_error;
8517
0
      clone->ns = ns;
8518
0
  }
8519
8520
0
end_ns_reference:
8521
8522
  /*
8523
  * Some post-processing.
8524
  *
8525
  * Handle ID attributes.
8526
  */
8527
0
  if ((clone->type == XML_ATTRIBUTE_NODE) &&
8528
0
      (clone->parent != NULL))
8529
0
  {
8530
0
            int res;
8531
8532
0
      res = xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone);
8533
0
            if (res < 0)
8534
0
                goto internal_error;
8535
0
            if (res == 1) {
8536
0
    xmlChar *idVal;
8537
8538
0
    idVal = xmlNodeGetContent(cur);
8539
0
                if (idVal == NULL)
8540
0
                    goto internal_error;
8541
0
                if (xmlAddIDSafe((xmlAttrPtr) cur, idVal) < 0) {
8542
0
                    xmlFree(idVal);
8543
0
                    goto internal_error;
8544
0
                }
8545
0
                xmlFree(idVal);
8546
0
      }
8547
0
  }
8548
  /*
8549
  **
8550
  ** The following will traverse the tree **************************
8551
  **
8552
  *
8553
  * Walk the element's attributes before descending into child-nodes.
8554
  */
8555
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
8556
0
      prevClone = NULL;
8557
0
      parentClone = clone;
8558
0
      cur = (xmlNodePtr) cur->properties;
8559
0
      continue;
8560
0
  }
8561
0
into_content:
8562
  /*
8563
  * Descend into child-nodes.
8564
  */
8565
0
  if (cur->children != NULL) {
8566
0
      if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
8567
0
    prevClone = NULL;
8568
0
    parentClone = clone;
8569
0
    cur = cur->children;
8570
0
    continue;
8571
0
      }
8572
0
  }
8573
8574
0
leave_node:
8575
  /*
8576
  * At this point we are done with the node, its content
8577
  * and an element-nodes's attribute-nodes.
8578
  */
8579
0
  if (cur == node)
8580
0
      break;
8581
0
  if ((cur->type == XML_ELEMENT_NODE) ||
8582
0
      (cur->type == XML_XINCLUDE_START) ||
8583
0
      (cur->type == XML_XINCLUDE_END)) {
8584
      /*
8585
      * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8586
      */
8587
0
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
8588
    /*
8589
    * Pop mappings.
8590
    */
8591
0
    while ((nsMap->last != NULL) &&
8592
0
        (nsMap->last->depth >= depth))
8593
0
    {
8594
0
        XML_NSMAP_POP(nsMap, mi)
8595
0
    }
8596
    /*
8597
    * Unshadow.
8598
    */
8599
0
    XML_NSMAP_FOREACH(nsMap, mi) {
8600
0
        if (mi->shadowDepth >= depth)
8601
0
      mi->shadowDepth = -1;
8602
0
    }
8603
0
      }
8604
0
      depth--;
8605
0
  }
8606
0
  if (cur->next != NULL) {
8607
0
      prevClone = clone;
8608
0
      cur = cur->next;
8609
0
  } else if (cur->type != XML_ATTRIBUTE_NODE) {
8610
0
      clone = clone->parent;
8611
0
      if (clone != NULL)
8612
0
    parentClone = clone->parent;
8613
      /*
8614
      * Process parent --> next;
8615
      */
8616
0
      cur = cur->parent;
8617
0
      goto leave_node;
8618
0
  } else {
8619
      /* This is for attributes only. */
8620
0
      clone = clone->parent;
8621
0
      parentClone = clone->parent;
8622
      /*
8623
      * Process parent-element --> children.
8624
      */
8625
0
      cur = cur->parent;
8626
0
      goto into_content;
8627
0
  }
8628
0
    }
8629
0
    goto exit;
8630
8631
0
internal_error:
8632
0
    ret = -1;
8633
8634
0
exit:
8635
    /*
8636
    * Cleanup.
8637
    */
8638
0
    if (nsMap != NULL) {
8639
0
  if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8640
      /*
8641
      * Just cleanup the map but don't free.
8642
      */
8643
0
      if (nsMap->first) {
8644
0
    if (nsMap->pool)
8645
0
        nsMap->last->next = nsMap->pool;
8646
0
    nsMap->pool = nsMap->first;
8647
0
    nsMap->first = NULL;
8648
0
      }
8649
0
  } else
8650
0
      xmlDOMWrapNsMapFree(nsMap);
8651
0
    }
8652
    /*
8653
    * TODO: Should we try a cleanup of the cloned node in case of a
8654
    * fatal error?
8655
    */
8656
0
    *resNode = resultClone;
8657
0
    return (ret);
8658
0
}
8659
8660
/**
8661
 * `attr` is adopted by `destDoc`.
8662
 * Ensures that ns-references point to `destDoc`: either to
8663
 * `elements->nsDef` entries if `destParent` is given, or to
8664
 * `destDoc->oldNs` otherwise.
8665
 *
8666
 * @param ctxt  the optional context for custom processing
8667
 * @param sourceDoc  unused
8668
 * @param attr  the attribute-node to be adopted
8669
 * @param destDoc  the destination doc for adoption
8670
 * @param destParent  the optional new parent of `attr` in `destDoc`
8671
 * @param options  option flags (unused)
8672
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
8673
 */
8674
static int
8675
xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
8676
        xmlDocPtr sourceDoc ATTRIBUTE_UNUSED,
8677
        xmlAttrPtr attr,
8678
        xmlDocPtr destDoc,
8679
        xmlNodePtr destParent,
8680
        int options ATTRIBUTE_UNUSED)
8681
0
{
8682
0
    int ret = 0;
8683
8684
0
    if ((attr == NULL) || (destDoc == NULL))
8685
0
  return (-1);
8686
8687
0
    if (attr->doc != destDoc) {
8688
0
        if (xmlSetTreeDoc((xmlNodePtr) attr, destDoc) < 0)
8689
0
            ret = -1;
8690
0
    }
8691
8692
0
    if (attr->ns != NULL) {
8693
0
  xmlNsPtr ns = NULL;
8694
8695
0
  if (ctxt != NULL) {
8696
      /* TODO: User defined. */
8697
0
  }
8698
  /* XML Namespace. */
8699
0
  if (IS_STR_XML(attr->ns->prefix)) {
8700
0
      ns = xmlTreeEnsureXMLDecl(destDoc);
8701
0
  } else if (destParent == NULL) {
8702
      /*
8703
      * Store in @destDoc->oldNs.
8704
      */
8705
0
      ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
8706
0
  } else {
8707
      /*
8708
      * Declare on @destParent.
8709
      */
8710
0
      if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
8711
0
    &ns, 1) == -1)
8712
0
    ret = -1;
8713
0
      if (ns == NULL) {
8714
0
    ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
8715
0
        attr->ns->href, attr->ns->prefix, 1);
8716
0
      }
8717
0
  }
8718
0
  if (ns == NULL)
8719
0
      ret = -1;
8720
0
  attr->ns = ns;
8721
0
    }
8722
8723
0
    return (ret);
8724
0
}
8725
8726
/**
8727
 * Fix up namespaces before moving a node.
8728
 *
8729
 * References of out-of scope ns-decls are remapped to point to `destDoc`:
8730
 * If `destParent` is given, then nsDef entries on element-nodes are used.
8731
 * If *no* `destParent` is given, then `destDoc->oldNs` entries are used
8732
 * This is the case when you have an unlinked node and just want to move it
8733
 * to the context of.
8734
 *
8735
 * If `destParent` is given, it ensures that the tree is namespace
8736
 * wellformed by creating additional ns-decls where needed.
8737
 * Note that, since prefixes of already existent ns-decls can be
8738
 * shadowed by this process, it could break QNames in attribute
8739
 * values or element content.
8740
 *
8741
 * NOTE: This function was not intensively tested.
8742
 *
8743
 * @param ctxt  the optional context for custom processing
8744
 * @param sourceDoc  the optional sourceDoc
8745
 * @param node  the node to start with
8746
 * @param destDoc  the destination doc
8747
 * @param destParent  the optional new parent of `node` in `destDoc`
8748
 * @param options  option flags
8749
 * @returns 0 if the operation succeeded,
8750
 *          1 if a node of unsupported type was given,
8751
 *          2 if a node of not yet supported type was given and
8752
 *          -1 on API/internal errors.
8753
 */
8754
int
8755
xmlDOMWrapAdoptNode(xmlDOMWrapCtxt *ctxt,
8756
        xmlDoc *sourceDoc,
8757
        xmlNode *node,
8758
        xmlDoc *destDoc,
8759
        xmlNode *destParent,
8760
        int options)
8761
0
{
8762
0
    int ret = 0;
8763
8764
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
8765
0
        (destDoc == NULL) ||
8766
0
  ((destParent != NULL) && (destParent->doc != destDoc)))
8767
0
  return(-1);
8768
    /*
8769
    * Check node->doc sanity.
8770
    */
8771
0
    if (sourceDoc == NULL) {
8772
0
        sourceDoc = node->doc;
8773
0
    } else if (node->doc != sourceDoc) {
8774
0
  return (-1);
8775
0
    }
8776
8777
    /*
8778
     * TODO: Shouldn't this be allowed?
8779
     */
8780
0
    if (sourceDoc == destDoc)
8781
0
  return (-1);
8782
8783
0
    switch (node->type) {
8784
0
  case XML_ELEMENT_NODE:
8785
0
  case XML_ATTRIBUTE_NODE:
8786
0
  case XML_TEXT_NODE:
8787
0
  case XML_CDATA_SECTION_NODE:
8788
0
  case XML_ENTITY_REF_NODE:
8789
0
  case XML_PI_NODE:
8790
0
  case XML_COMMENT_NODE:
8791
0
      break;
8792
0
  case XML_DOCUMENT_FRAG_NODE:
8793
      /* TODO: Support document-fragment-nodes. */
8794
0
      return (2);
8795
0
  default:
8796
0
      return (1);
8797
0
    }
8798
    /*
8799
    * Unlink only if @node was not already added to @destParent.
8800
    */
8801
0
    if ((node->parent != NULL) && (destParent != node->parent))
8802
0
  xmlUnlinkNodeInternal(node);
8803
8804
0
    if (node->type == XML_ELEMENT_NODE) {
8805
0
      return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
8806
0
        destDoc, destParent, options));
8807
0
    } else if (node->type == XML_ATTRIBUTE_NODE) {
8808
0
      return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
8809
0
    (xmlAttrPtr) node, destDoc, destParent, options));
8810
0
    } else {
8811
0
        if (node->doc != destDoc) {
8812
0
            if (xmlNodeSetDoc(node, destDoc) < 0)
8813
0
                ret = -1;
8814
0
        }
8815
0
    }
8816
0
    return (ret);
8817
0
}
8818
8819
/************************************************************************
8820
 *                  *
8821
 *      XHTML detection         *
8822
 *                  *
8823
 ************************************************************************/
8824
8825
0
#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
8826
0
   "-//W3C//DTD XHTML 1.0 Strict//EN"
8827
0
#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
8828
0
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
8829
0
#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
8830
0
   "-//W3C//DTD XHTML 1.0 Frameset//EN"
8831
0
#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
8832
0
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
8833
0
#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
8834
0
   "-//W3C//DTD XHTML 1.0 Transitional//EN"
8835
0
#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
8836
0
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
8837
8838
/**
8839
 * Try to find if the document correspond to an XHTML DTD
8840
 *
8841
 * @param systemID  the system identifier
8842
 * @param publicID  the public identifier
8843
 * @returns 1 if true, 0 if not and -1 in case of error
8844
 */
8845
int
8846
0
xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
8847
0
    if ((systemID == NULL) && (publicID == NULL))
8848
0
  return(-1);
8849
0
    if (publicID != NULL) {
8850
0
  if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
8851
0
  if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
8852
0
  if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
8853
0
    }
8854
0
    if (systemID != NULL) {
8855
0
  if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
8856
0
  if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
8857
0
  if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
8858
0
    }
8859
0
    return(0);
8860
0
}
8861
8862
/************************************************************************
8863
 *                  *
8864
 *      Node callbacks          *
8865
 *                  *
8866
 ************************************************************************/
8867
8868
/**
8869
 * Registers a callback for node creation
8870
 *
8871
 * @deprecated don't use
8872
 *
8873
 * @param func  function pointer to the new RegisterNodeFunc
8874
 * @returns the old value of the registration function
8875
 */
8876
xmlRegisterNodeFunc
8877
xmlRegisterNodeDefault(xmlRegisterNodeFunc func)
8878
0
{
8879
0
    xmlRegisterNodeFunc old = xmlRegisterNodeDefaultValue;
8880
8881
0
    xmlRegisterCallbacks = 1;
8882
0
    xmlRegisterNodeDefaultValue = func;
8883
0
    return(old);
8884
0
}
8885
8886
/**
8887
 * Registers a callback for node destruction
8888
 *
8889
 * @deprecated don't use
8890
 *
8891
 * @param func  function pointer to the new DeregisterNodeFunc
8892
 * @returns the previous value of the deregistration function
8893
 */
8894
xmlDeregisterNodeFunc
8895
xmlDeregisterNodeDefault(xmlDeregisterNodeFunc func)
8896
0
{
8897
0
    xmlDeregisterNodeFunc old = xmlDeregisterNodeDefaultValue;
8898
8899
0
    xmlRegisterCallbacks = 1;
8900
0
    xmlDeregisterNodeDefaultValue = func;
8901
0
    return(old);
8902
0
}
8903