Coverage Report

Created: 2026-07-30 06:08

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, int normalize);
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
3.49M
#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
101
3.49M
  (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
12.7k
xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
120
12.7k
    xmlEntitiesTablePtr table;
121
122
12.7k
    if((dtd != NULL) && (dtd->entities != NULL)) {
123
12.7k
  table = (xmlEntitiesTablePtr) dtd->entities;
124
12.7k
  return((xmlEntityPtr) xmlHashLookup(table, name));
125
  /* return(xmlGetEntityFromTable(table, name)); */
126
12.7k
    }
127
0
    return(NULL);
128
12.7k
}
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
5.49k
xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
138
5.49k
    xmlEntitiesTablePtr table;
139
140
5.49k
    if ((dtd != NULL) && (dtd->pentities != NULL)) {
141
5.49k
  table = (xmlEntitiesTablePtr) dtd->pentities;
142
5.49k
  return((xmlEntityPtr) xmlHashLookup(table, name));
143
  /* return(xmlGetEntityFromTable(table, name)); */
144
5.49k
    }
145
0
    return(NULL);
146
5.49k
}
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
2.03M
        xmlChar *memory, int len) {
171
2.03M
    size_t lenn, lenp;
172
2.03M
    xmlChar *ret;
173
174
2.03M
    if ((ncname == NULL) || (len < 0)) return(NULL);
175
2.03M
    if (prefix == NULL) return((xmlChar *) ncname);
176
177
997k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
178
    /* Make allocation more likely */
179
997k
    if (len > 8)
180
755k
        len = 8;
181
997k
#endif
182
183
997k
    lenn = strlen((char *) ncname);
184
997k
    lenp = strlen((char *) prefix);
185
997k
    if (lenn >= SIZE_MAX - lenp - 1)
186
0
        return(NULL);
187
188
997k
    if ((memory == NULL) || ((size_t) len < lenn + lenp + 2)) {
189
447k
  ret = xmlMalloc(lenn + lenp + 2);
190
447k
  if (ret == NULL)
191
246
      return(NULL);
192
550k
    } else {
193
550k
  ret = memory;
194
550k
    }
195
997k
    memcpy(&ret[0], prefix, lenp);
196
997k
    ret[lenp] = ':';
197
997k
    memcpy(&ret[lenp + 1], ncname, lenn);
198
997k
    ret[lenn + lenp + 1] = 0;
199
997k
    return(ret);
200
997k
}
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
3.16k
xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
221
3.16k
    int len = 0;
222
3.16k
    xmlChar *ret = NULL;
223
224
3.16k
    if (prefix == NULL) return(NULL);
225
3.16k
    *prefix = NULL;
226
3.16k
    if (name == NULL) return(NULL);
227
228
    /* nasty but valid */
229
3.16k
    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
118k
    while ((name[len] != 0) && (name[len] != ':'))
237
115k
  len++;
238
239
3.16k
    if ((name[len] == 0) || (name[len+1] == 0))
240
1.09k
  return(NULL);
241
242
2.06k
    *prefix = xmlStrndup(name, len);
243
2.06k
    if (*prefix == NULL)
244
1
  return(NULL);
245
2.06k
    ret = xmlStrdup(&name[len + 1]);
246
2.06k
    if (ret == NULL) {
247
3
  if (*prefix != NULL) {
248
3
      xmlFree(*prefix);
249
3
      *prefix = NULL;
250
3
  }
251
3
  return(NULL);
252
3
    }
253
254
2.06k
    return(ret);
255
2.06k
}
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
2.49M
xmlSplitQName3(const xmlChar *name, int *len) {
269
2.49M
    int l = 0;
270
271
2.49M
    if (name == NULL) return(NULL);
272
2.49M
    if (len == NULL) return(NULL);
273
274
    /* nasty but valid */
275
2.49M
    if (name[0] == ':')
276
30.8k
  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
21.8M
    while ((name[l] != 0) && (name[l] != ':'))
283
19.4M
  l++;
284
285
2.46M
    if ((name[l] == 0) || (name[l+1] == 0))
286
1.50M
  return(NULL);
287
288
955k
    *len = l;
289
290
955k
    return(&name[l+1]);
291
2.46M
}
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
1.84M
xmlSplitQName4(const xmlChar *name, xmlChar **prefixPtr) {
307
1.84M
    xmlChar *prefix;
308
1.84M
    int l = 0;
309
310
1.84M
    if ((name == NULL) || (prefixPtr == NULL))
311
0
        return(NULL);
312
313
1.84M
    *prefixPtr = NULL;
314
315
    /* nasty but valid */
316
1.84M
    if (name[0] == ':')
317
25.0k
  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
9.26M
    while ((name[l] != 0) && (name[l] != ':'))
324
7.44M
  l++;
325
326
    /*
327
     * TODO: What about names with multiple colons?
328
     */
329
1.81M
    if ((name[l] == 0) || (name[l+1] == 0))
330
1.47M
  return(name);
331
332
349k
    prefix = xmlStrndup(name, l);
333
349k
    if (prefix == NULL)
334
286
        return(NULL);
335
336
349k
    *prefixPtr = prefix;
337
349k
    return(&name[l+1]);
338
349k
}
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
156k
xmlValidateNCName(const xmlChar *value, int space) {
356
156k
    const xmlChar *cur;
357
358
156k
    if (value == NULL)
359
7
        return(-1);
360
361
156k
    cur = value;
362
363
156k
    if (space) {
364
156k
  while (IS_BLANK_CH(*cur))
365
54.6k
            cur++;
366
156k
    }
367
368
156k
    value = cur;
369
156k
    cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
370
156k
    if ((cur == NULL) || (cur == value))
371
10.9k
        return(1);
372
373
145k
    if (space) {
374
145k
  while (IS_BLANK_CH(*cur))
375
49.5k
            cur++;
376
145k
    }
377
378
145k
    return(*cur != 0);
379
156k
}
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
160k
xmlValidateQName(const xmlChar *value, int space) {
391
160k
    const xmlChar *cur;
392
393
160k
    if (value == NULL)
394
6
        return(-1);
395
396
160k
    cur = value;
397
398
160k
    if (space) {
399
160k
  while (IS_BLANK_CH(*cur))
400
1.44k
            cur++;
401
160k
    }
402
403
160k
    value = cur;
404
160k
    cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
405
160k
    if ((cur == NULL) || (cur == value))
406
5.49k
        return(1);
407
408
154k
    if (*cur == ':') {
409
62.1k
        cur += 1;
410
62.1k
        value = cur;
411
62.1k
        cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
412
62.1k
        if ((cur == NULL) || (cur == value))
413
1.46k
            return(1);
414
62.1k
    }
415
416
153k
    if (space) {
417
153k
  while (IS_BLANK_CH(*cur))
418
810
            cur++;
419
153k
    }
420
421
153k
    return(*cur != 0);
422
154k
}
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
2.37k
xmlValidateName(const xmlChar *value, int space) {
434
2.37k
    const xmlChar *cur;
435
436
2.37k
    if (value == NULL)
437
0
        return(-1);
438
439
2.37k
    cur = value;
440
441
2.37k
    if (space) {
442
2.37k
  while (IS_BLANK_CH(*cur))
443
1.03k
            cur++;
444
2.37k
    }
445
446
2.37k
    value = cur;
447
2.37k
    cur = xmlScanName(value, SIZE_MAX, 0);
448
2.37k
    if ((cur == NULL) || (cur == value))
449
647
        return(1);
450
451
1.73k
    if (space) {
452
1.73k
  while (IS_BLANK_CH(*cur))
453
1.24k
            cur++;
454
1.73k
    }
455
456
1.73k
    return(*cur != 0);
457
2.37k
}
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
6.35k
xmlValidateNMToken(const xmlChar *value, int space) {
469
6.35k
    const xmlChar *cur;
470
471
6.35k
    if (value == NULL)
472
0
        return(-1);
473
474
6.35k
    cur = value;
475
476
6.35k
    if (space) {
477
6.35k
  while (IS_BLANK_CH(*cur))
478
820
            cur++;
479
6.35k
    }
480
481
6.35k
    value = cur;
482
6.35k
    cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NMTOKEN);
483
6.35k
    if ((cur == NULL) || (cur == value))
484
1.12k
        return(1);
485
486
5.23k
    if (space) {
487
5.23k
  while (IS_BLANK_CH(*cur))
488
738
            cur++;
489
5.23k
    }
490
491
5.23k
    return(*cur != 0);
492
6.35k
}
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
14.0M
xmlNewNs(xmlNode *node, const xmlChar *href, const xmlChar *prefix) {
518
14.0M
    xmlNsPtr cur;
519
520
14.0M
    if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
521
1.71k
  return(NULL);
522
523
    /*
524
     * Allocate a new Namespace and fill the fields.
525
     */
526
14.0M
    cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
527
14.0M
    if (cur == NULL)
528
362
  return(NULL);
529
14.0M
    memset(cur, 0, sizeof(xmlNs));
530
14.0M
    cur->type = XML_LOCAL_NAMESPACE;
531
532
14.0M
    if (href != NULL) {
533
14.0M
  cur->href = xmlStrdup(href);
534
14.0M
        if (cur->href == NULL)
535
349
            goto error;
536
14.0M
    }
537
14.0M
    if (prefix != NULL) {
538
13.4M
  cur->prefix = xmlStrdup(prefix);
539
13.4M
        if (cur->prefix == NULL)
540
277
            goto error;
541
13.4M
    }
542
543
    /*
544
     * Add it at the end to preserve parsing order ...
545
     * and checks for existing use of the prefix
546
     */
547
14.0M
    if (node != NULL) {
548
294k
  if (node->nsDef == NULL) {
549
192k
      node->nsDef = cur;
550
192k
  } else {
551
102k
      xmlNsPtr prev = node->nsDef;
552
553
102k
      if ((xmlStrEqual(prev->prefix, cur->prefix)) &&
554
2.46k
                (prev->href != NULL))
555
890
                goto error;
556
1.97M
      while (prev->next != NULL) {
557
1.87M
          prev = prev->next;
558
1.87M
    if ((xmlStrEqual(prev->prefix, cur->prefix)) &&
559
60.0k
                    (prev->href != NULL))
560
762
                    goto error;
561
1.87M
      }
562
100k
      prev->next = cur;
563
100k
  }
564
294k
    }
565
14.0M
    return(cur);
566
567
2.27k
error:
568
2.27k
    xmlFreeNs(cur);
569
2.27k
    return(NULL);
570
14.0M
}
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
161k
xmlSetNs(xmlNode *node, xmlNs *ns) {
581
161k
    if (node == NULL) {
582
823
  return;
583
823
    }
584
161k
    if ((node->type == XML_ELEMENT_NODE) ||
585
1.85k
        (node->type == XML_ATTRIBUTE_NODE))
586
160k
  node->ns = ns;
587
161k
}
588
589
/**
590
 * Free an xmlNs object.
591
 *
592
 * @param cur  the namespace pointer
593
 */
594
void
595
14.1M
xmlFreeNs(xmlNs *cur) {
596
14.1M
    if (cur == NULL) {
597
1.51k
  return;
598
1.51k
    }
599
14.1M
    if (cur->href != NULL) xmlFree((char *) cur->href);
600
14.1M
    if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
601
14.1M
    xmlFree(cur);
602
14.1M
}
603
604
/**
605
 * Free a list of xmlNs objects.
606
 *
607
 * @param cur  the first namespace pointer
608
 */
609
void
610
6.81M
xmlFreeNsList(xmlNs *cur) {
611
6.81M
    xmlNsPtr next;
612
6.81M
    if (cur == NULL) {
613
1.79k
  return;
614
1.79k
    }
615
20.9M
    while (cur != NULL) {
616
14.1M
        next = cur->next;
617
14.1M
        xmlFreeNs(cur);
618
14.1M
  cur = next;
619
14.1M
    }
620
6.81M
}
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
61.5k
          const xmlChar *systemId) {
641
61.5k
    xmlDtdPtr cur;
642
643
61.5k
    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
61.5k
    cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
651
61.5k
    if (cur == NULL)
652
108
  return(NULL);
653
61.4k
    memset(cur, 0 , sizeof(xmlDtd));
654
61.4k
    cur->type = XML_DTD_NODE;
655
656
61.4k
    if (name != NULL) {
657
55.8k
  cur->name = xmlStrdup(name);
658
55.8k
        if (cur->name == NULL)
659
90
            goto error;
660
55.8k
    }
661
61.3k
    if (publicId != NULL) {
662
19.8k
  cur->ExternalID = xmlStrdup(publicId);
663
19.8k
        if (cur->ExternalID == NULL)
664
52
            goto error;
665
19.8k
    }
666
61.3k
    if (systemId != NULL) {
667
33.9k
  cur->SystemID = xmlStrdup(systemId);
668
33.9k
        if (cur->SystemID == NULL)
669
69
            goto error;
670
33.9k
    }
671
61.2k
    if (doc != NULL)
672
20.9k
  doc->extSubset = cur;
673
61.2k
    cur->doc = doc;
674
675
61.2k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
676
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
677
61.2k
    return(cur);
678
679
211
error:
680
211
    xmlFreeDtd(cur);
681
211
    return(NULL);
682
61.3k
}
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
650k
xmlGetIntSubset(const xmlDoc *doc) {
692
650k
    xmlNodePtr cur;
693
694
650k
    if (doc == NULL)
695
52.4k
  return(NULL);
696
598k
    cur = doc->children;
697
710k
    while (cur != NULL) {
698
203k
  if (cur->type == XML_DTD_NODE)
699
91.4k
      return((xmlDtdPtr) cur);
700
112k
  cur = cur->next;
701
112k
    }
702
506k
    return((xmlDtdPtr) doc->intSubset);
703
598k
}
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
270k
                   const xmlChar *systemId) {
723
270k
    xmlDtdPtr cur;
724
725
270k
    if (doc != NULL) {
726
268k
        cur = xmlGetIntSubset(doc);
727
268k
        if (cur != NULL)
728
0
            return(cur);
729
268k
    }
730
731
    /*
732
     * Allocate a new DTD and fill the fields.
733
     */
734
270k
    cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
735
270k
    if (cur == NULL)
736
544
  return(NULL);
737
270k
    memset(cur, 0, sizeof(xmlDtd));
738
270k
    cur->type = XML_DTD_NODE;
739
740
270k
    if (name != NULL) {
741
232k
  cur->name = xmlStrdup(name);
742
232k
  if (cur->name == NULL)
743
236
            goto error;
744
232k
    }
745
269k
    if (publicId != NULL) {
746
83.8k
  cur->ExternalID = xmlStrdup(publicId);
747
83.8k
  if (cur->ExternalID  == NULL)
748
119
            goto error;
749
83.8k
    }
750
269k
    if (systemId != NULL) {
751
161k
  cur->SystemID = xmlStrdup(systemId);
752
161k
  if (cur->SystemID == NULL)
753
128
            goto error;
754
161k
    }
755
269k
    if (doc != NULL) {
756
267k
  doc->intSubset = cur;
757
267k
  cur->parent = doc;
758
267k
  cur->doc = doc;
759
267k
  if (doc->children == NULL) {
760
242k
      doc->children = (xmlNodePtr) cur;
761
242k
      doc->last = (xmlNodePtr) cur;
762
242k
  } else {
763
25.5k
      if (doc->type == XML_HTML_DOCUMENT_NODE) {
764
11.7k
    xmlNodePtr prev;
765
766
11.7k
    prev = doc->children;
767
11.7k
    prev->prev = (xmlNodePtr) cur;
768
11.7k
    cur->next = prev;
769
11.7k
    doc->children = (xmlNodePtr) cur;
770
13.7k
      } else {
771
13.7k
    xmlNodePtr next;
772
773
13.7k
    next = doc->children;
774
29.0k
    while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
775
15.2k
        next = next->next;
776
13.7k
    if (next == NULL) {
777
12.6k
        cur->prev = doc->last;
778
12.6k
        cur->prev->next = (xmlNodePtr) cur;
779
12.6k
        cur->next = NULL;
780
12.6k
        doc->last = (xmlNodePtr) cur;
781
12.6k
    } else {
782
1.05k
        cur->next = next;
783
1.05k
        cur->prev = next->prev;
784
1.05k
        if (cur->prev == NULL)
785
831
      doc->children = (xmlNodePtr) cur;
786
219
        else
787
219
      cur->prev->next = (xmlNodePtr) cur;
788
1.05k
        next->prev = (xmlNodePtr) cur;
789
1.05k
    }
790
13.7k
      }
791
25.5k
  }
792
267k
    }
793
794
269k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
795
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
796
269k
    return(cur);
797
798
483
error:
799
483
    xmlFreeDtd(cur);
800
483
    return(NULL);
801
269k
}
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
35.4M
  if ((str) && ((!dict) ||       \
811
34.4M
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
812
20.4M
      xmlFree((char *)(str));
813
814
/**
815
 * Free a DTD structure.
816
 *
817
 * @param cur  the DTD structure to free up
818
 */
819
void
820
331k
xmlFreeDtd(xmlDtd *cur) {
821
331k
    xmlDictPtr dict = NULL;
822
823
331k
    if (cur == NULL) {
824
0
  return;
825
0
    }
826
331k
    if (cur->doc != NULL) dict = cur->doc->dict;
827
828
331k
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
829
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
830
831
331k
    if (cur->children != NULL) {
832
177k
  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
1.54M
        while (c != NULL) {
839
1.37M
      next = c->next;
840
1.37M
      if ((c->type != XML_ELEMENT_DECL) &&
841
1.30M
    (c->type != XML_ATTRIBUTE_DECL) &&
842
1.07M
    (c->type != XML_ENTITY_DECL)) {
843
881k
    xmlUnlinkNodeInternal(c);
844
881k
    xmlFreeNode(c);
845
881k
      }
846
1.37M
      c = next;
847
1.37M
  }
848
177k
    }
849
850
331k
    DICT_FREE(cur->name)
851
852
331k
    if (cur->SystemID != NULL)
853
195k
        xmlFree(cur->SystemID);
854
331k
    if (cur->ExternalID != NULL)
855
103k
        xmlFree(cur->ExternalID);
856
857
    /* TODO !!! */
858
331k
    if (cur->notations != NULL)
859
6.88k
        xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
860
861
331k
    if (cur->elements != NULL)
862
92.2k
        xmlFreeElementTable((xmlElementTablePtr) cur->elements);
863
331k
    if (cur->attributes != NULL)
864
63.0k
        xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
865
331k
    if (cur->entities != NULL)
866
71.0k
        xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
867
331k
    if (cur->pentities != NULL)
868
38.3k
        xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
869
870
331k
    xmlFree(cur);
871
331k
}
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
482k
xmlNewDoc(const xmlChar *version) {
881
482k
    xmlDocPtr cur;
882
883
482k
    if (version == NULL)
884
26.3k
  version = (const xmlChar *) "1.0";
885
886
    /*
887
     * Allocate a new document and fill the fields.
888
     */
889
482k
    cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
890
482k
    if (cur == NULL)
891
373
  return(NULL);
892
482k
    memset(cur, 0, sizeof(xmlDoc));
893
482k
    cur->type = XML_DOCUMENT_NODE;
894
895
482k
    cur->version = xmlStrdup(version);
896
482k
    if (cur->version == NULL) {
897
450
  xmlFree(cur);
898
450
  return(NULL);
899
450
    }
900
481k
    cur->standalone = -1;
901
481k
    cur->compression = -1; /* not initialized */
902
481k
    cur->doc = cur;
903
481k
    cur->parseFlags = 0;
904
481k
    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
481k
    cur->charset = XML_CHAR_ENCODING_UTF8;
911
912
481k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
913
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
914
481k
    return(cur);
915
482k
}
916
917
/**
918
 * Free a document including all children and associated DTDs.
919
 *
920
 * @param cur  pointer to the document
921
 */
922
void
923
602k
xmlFreeDoc(xmlDoc *cur) {
924
602k
    xmlDtdPtr extSubset, intSubset;
925
602k
    xmlDictPtr dict = NULL;
926
927
602k
    if (cur == NULL) {
928
88.5k
  return;
929
88.5k
    }
930
931
513k
    dict = cur->dict;
932
933
513k
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
934
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
935
936
    /*
937
     * Do this before freeing the children list to avoid ID lookups
938
     */
939
513k
    if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
940
513k
    cur->ids = NULL;
941
513k
    if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
942
513k
    cur->refs = NULL;
943
513k
    extSubset = cur->extSubset;
944
513k
    intSubset = cur->intSubset;
945
513k
    if (intSubset == extSubset)
946
238k
  extSubset = NULL;
947
513k
    if (extSubset != NULL) {
948
14.7k
  xmlUnlinkNodeInternal((xmlNodePtr) cur->extSubset);
949
14.7k
  cur->extSubset = NULL;
950
14.7k
  xmlFreeDtd(extSubset);
951
14.7k
    }
952
513k
    if (intSubset != NULL) {
953
278k
  xmlUnlinkNodeInternal((xmlNodePtr) cur->intSubset);
954
278k
  cur->intSubset = NULL;
955
278k
  xmlFreeDtd(intSubset);
956
278k
    }
957
958
513k
    if (cur->children != NULL) xmlFreeNodeList(cur->children);
959
513k
    if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
960
961
513k
    DICT_FREE(cur->name)
962
963
513k
    if (cur->version != NULL)
964
445k
        xmlFree(cur->version);
965
513k
    if (cur->encoding != NULL)
966
45.5k
        xmlFree(cur->encoding);
967
513k
    if (cur->URL != NULL)
968
358k
        xmlFree(cur->URL);
969
970
513k
    xmlFree(cur);
971
513k
    if (dict) xmlDictFree(dict);
972
513k
}
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
477k
                     const xmlChar *value, size_t len, xmlNode **listPtr) {
999
477k
    xmlNodePtr head = NULL, last = NULL;
1000
477k
    xmlNodePtr node;
1001
477k
    xmlChar *val = NULL;
1002
477k
    const xmlChar *cur;
1003
477k
    const xmlChar *q;
1004
477k
    xmlEntityPtr ent;
1005
477k
    xmlBufPtr buf;
1006
477k
    size_t remaining = len;
1007
1008
477k
    if (listPtr != NULL)
1009
4.28k
        *listPtr = NULL;
1010
1011
477k
    if ((value == NULL) || (value[0] == 0))
1012
19.1k
        goto done;
1013
1014
458k
    cur = value;
1015
1016
458k
    buf = xmlBufCreate(50);
1017
458k
    if (buf == NULL)
1018
149
        return(-1);
1019
1020
458k
    q = cur;
1021
583M
    while ((remaining > 0) && (*cur != 0)) {
1022
582M
  if (cur[0] == '&') {
1023
424k
      int charval = 0;
1024
1025
      /*
1026
       * Save the current text.
1027
       */
1028
424k
            if (cur != q) {
1029
207k
    if (xmlBufAdd(buf, q, cur - q))
1030
24
        goto out;
1031
207k
          q = cur;
1032
207k
      }
1033
1034
424k
      if ((remaining > 2) && (cur[1] == '#') && (cur[2] == 'x')) {
1035
12.5k
          int tmp = 0;
1036
1037
12.5k
    cur += 3;
1038
12.5k
                remaining -= 3;
1039
55.9k
    while ((remaining > 0) && ((tmp = *cur) != ';')) {
1040
43.9k
        if ((tmp >= '0') && (tmp <= '9'))
1041
9.47k
      charval = charval * 16 + (tmp - '0');
1042
34.4k
        else if ((tmp >= 'a') && (tmp <= 'f'))
1043
11.9k
      charval = charval * 16 + (tmp - 'a') + 10;
1044
22.5k
        else if ((tmp >= 'A') && (tmp <= 'F'))
1045
22.0k
      charval = charval * 16 + (tmp - 'A') + 10;
1046
549
        else {
1047
549
      charval = 0;
1048
549
      break;
1049
549
        }
1050
43.4k
                    if (charval > 0x110000)
1051
403
                        charval = 0x110000;
1052
43.4k
        cur++;
1053
43.4k
                    remaining--;
1054
43.4k
    }
1055
12.5k
    if (tmp == ';') {
1056
11.7k
        cur++;
1057
11.7k
                    remaining--;
1058
11.7k
                }
1059
12.5k
    q = cur;
1060
411k
      } else if ((remaining > 1) && (cur[1] == '#')) {
1061
93.6k
          int tmp = 0;
1062
1063
93.6k
    cur += 2;
1064
93.6k
                remaining -= 2;
1065
286k
    while ((remaining > 0) && ((tmp = *cur) != ';')) {
1066
192k
        if ((tmp >= '0') && (tmp <= '9'))
1067
192k
      charval = charval * 10 + (tmp - '0');
1068
213
        else {
1069
213
      charval = 0;
1070
213
      break;
1071
213
        }
1072
192k
                    if (charval > 0x110000)
1073
312
                        charval = 0x110000;
1074
192k
        cur++;
1075
192k
                    remaining--;
1076
192k
    }
1077
93.6k
    if (tmp == ';') {
1078
93.2k
        cur++;
1079
93.2k
                    remaining--;
1080
93.2k
                }
1081
93.6k
    q = cur;
1082
318k
      } else {
1083
    /*
1084
     * Read the entity string
1085
     */
1086
318k
    cur++;
1087
318k
                remaining--;
1088
318k
    q = cur;
1089
1.21M
    while ((remaining > 0) && (*cur != 0) && (*cur != ';')) {
1090
893k
                    cur++;
1091
893k
                    remaining--;
1092
893k
                }
1093
318k
    if ((remaining <= 0) || (*cur == 0))
1094
782
        break;
1095
317k
    if (cur != q) {
1096
316k
        val = xmlStrndup(q, cur - q);
1097
316k
                    if (val == NULL)
1098
60
                        goto out;
1099
316k
        ent = xmlGetDocEntity(doc, val);
1100
316k
        if ((ent != NULL) &&
1101
296k
      (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1102
                        /*
1103
                         * Predefined entities don't generate nodes
1104
                         */
1105
48.8k
      if (xmlBufCat(buf, ent->content))
1106
14
          goto out;
1107
268k
                    } else if (ent == NULL ||
1108
267k
                               (ent->flags & XML_ENT_EXPANDING) == 0) {
1109
      /*
1110
       * Flush buffer so far
1111
       */
1112
267k
      if (!xmlBufIsEmpty(buf)) {
1113
85.1k
          node = xmlNewDocText(doc, NULL);
1114
85.1k
          if (node == NULL)
1115
30
        goto out;
1116
85.0k
          node->content = xmlBufDetach(buf);
1117
85.0k
                            node->parent = (xmlNode *) attr;
1118
1119
85.0k
          if (last == NULL) {
1120
7.00k
        head = node;
1121
78.0k
          } else {
1122
78.0k
                                last->next = node;
1123
78.0k
                                node->prev = last;
1124
78.0k
          }
1125
85.0k
                            last = node;
1126
85.0k
      }
1127
1128
267k
      if ((ent != NULL) &&
1129
246k
                            ((ent->flags & XML_ENT_PARSED) == 0) &&
1130
11.4k
                            (ent->content != NULL)) {
1131
7.09k
                            int res;
1132
1133
7.09k
                            ent->flags |= XML_ENT_EXPANDING;
1134
7.09k
                            res = xmlNodeParseAttValue(doc, (xmlAttr *) ent,
1135
7.09k
                                                       ent->content, SIZE_MAX,
1136
7.09k
                                                       NULL);
1137
7.09k
                            ent->flags &= ~XML_ENT_EXPANDING;
1138
7.09k
                            if (res < 0)
1139
498
                                goto out;
1140
6.60k
                            ent->flags |= XML_ENT_PARSED;
1141
6.60k
      }
1142
1143
      /*
1144
       * Create a new REFERENCE_REF node
1145
       */
1146
266k
      node = xmlNewEntityRef((xmlDocPtr) doc, val);
1147
266k
                        val = NULL;
1148
266k
      if (node == NULL)
1149
68
          goto out;
1150
266k
                        node->parent = (xmlNode *) attr;
1151
266k
                        node->last = (xmlNodePtr) ent;
1152
266k
                        if (ent != NULL) {
1153
245k
                            node->children = (xmlNodePtr) ent;
1154
245k
                            node->content = ent->content;
1155
245k
                        }
1156
1157
266k
      if (last == NULL) {
1158
9.22k
          head = node;
1159
257k
      } else {
1160
257k
                            last->next = node;
1161
257k
                            node->prev = last;
1162
257k
      }
1163
266k
                        last = node;
1164
266k
        }
1165
316k
        xmlFree(val);
1166
316k
                    val = NULL;
1167
316k
    }
1168
316k
    cur++;
1169
316k
                remaining--;
1170
316k
    q = cur;
1171
316k
      }
1172
422k
      if (charval != 0) {
1173
105k
    xmlChar buffer[10];
1174
105k
    int l;
1175
1176
105k
                if (charval >= 0x110000)
1177
292
                    charval = 0xFFFD; /* replacement character */
1178
1179
105k
    l = xmlCopyCharMultiByte(buffer, charval);
1180
105k
    buffer[l] = 0;
1181
1182
105k
    if (xmlBufCat(buf, buffer))
1183
12
        goto out;
1184
105k
      }
1185
582M
  } else {
1186
582M
      cur++;
1187
582M
            remaining--;
1188
582M
        }
1189
582M
    }
1190
1191
457k
    if (cur != q) {
1192
        /*
1193
   * Handle the last piece of text.
1194
   */
1195
444k
  if (xmlBufAdd(buf, q, cur - q))
1196
25
      goto out;
1197
444k
    }
1198
1199
457k
    if (!xmlBufIsEmpty(buf)) {
1200
448k
  node = xmlNewDocText(doc, NULL);
1201
448k
  if (node == NULL)
1202
104
            goto out;
1203
447k
        node->parent = (xmlNode *) attr;
1204
447k
  node->content = xmlBufDetach(buf);
1205
1206
447k
  if (last == NULL) {
1207
440k
      head = node;
1208
440k
  } else {
1209
7.00k
            last->next = node;
1210
7.00k
            node->prev = last;
1211
7.00k
  }
1212
447k
        last = node;
1213
447k
    } else if (head == NULL) {
1214
574
        head = xmlNewDocText(doc, BAD_CAST "");
1215
574
        if (head == NULL)
1216
3
            goto out;
1217
571
        head->parent = (xmlNode *) attr;
1218
571
        last = head;
1219
571
    }
1220
1221
457k
    xmlBufFree(buf);
1222
1223
476k
done:
1224
476k
    if (attr != NULL) {
1225
472k
        if (attr->children != NULL)
1226
2.66k
            xmlFreeNodeList(attr->children);
1227
472k
        attr->children = head;
1228
472k
        attr->last = last;
1229
472k
    }
1230
1231
476k
    if (listPtr != NULL)
1232
4.27k
        *listPtr = head;
1233
1234
476k
    return(0);
1235
1236
838
out:
1237
838
    xmlBufFree(buf);
1238
838
    if (val != NULL)
1239
542
        xmlFree(val);
1240
838
    if (head != NULL)
1241
249
        xmlFreeNodeList(head);
1242
838
    return(-1);
1243
457k
}
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
2.15k
xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
1258
2.15k
    xmlNodePtr ret;
1259
2.15k
    size_t maxSize = len < 0 ? SIZE_MAX : (size_t) len;
1260
1261
2.15k
    xmlNodeParseAttValue(doc, NULL, value, maxSize, &ret);
1262
2.15k
    return(ret);
1263
2.15k
}
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
2.12k
xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
1284
2.12k
    xmlNodePtr ret;
1285
1286
2.12k
    xmlNodeParseAttValue(doc, NULL, value, SIZE_MAX, &ret);
1287
2.12k
    return(ret);
1288
2.12k
}
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
180k
xmlNodeListGetStringInternal(const xmlNode *node, int escape, int flags) {
1298
180k
    xmlBufPtr buf;
1299
180k
    xmlChar *ret;
1300
1301
180k
    if (node == NULL)
1302
0
        return(xmlStrdup(BAD_CAST ""));
1303
1304
180k
    if ((escape == 0) &&
1305
48.4k
        ((node->type == XML_TEXT_NODE) ||
1306
1.51k
         (node->type == XML_CDATA_SECTION_NODE)) &&
1307
47.1k
        (node->next == NULL)) {
1308
46.4k
        if (node->content == NULL)
1309
217
            return(xmlStrdup(BAD_CAST ""));
1310
46.2k
        return(xmlStrdup(node->content));
1311
46.4k
    }
1312
1313
134k
    buf = xmlBufCreate(50);
1314
134k
    if (buf == NULL)
1315
36
        return(NULL);
1316
1317
284k
    while (node != NULL) {
1318
150k
        if ((node->type == XML_TEXT_NODE) ||
1319
136k
            (node->type == XML_CDATA_SECTION_NODE)) {
1320
136k
            if (node->content != NULL) {
1321
135k
                if (escape == 0) {
1322
1.50k
                    xmlBufCat(buf, node->content);
1323
133k
                } else {
1324
133k
                    xmlChar *encoded;
1325
1326
133k
                    encoded = xmlEscapeText(node->content, flags);
1327
133k
                    if (encoded == NULL)
1328
28
                        goto error;
1329
133k
                    xmlBufCat(buf, encoded);
1330
133k
                    xmlFree(encoded);
1331
133k
                }
1332
135k
            }
1333
136k
        } else if (node->type == XML_ENTITY_REF_NODE) {
1334
11.6k
            if (escape == 0) {
1335
1.45k
                xmlBufGetNodeContent(buf, node);
1336
10.2k
            } else {
1337
10.2k
                xmlBufAdd(buf, BAD_CAST "&", 1);
1338
10.2k
                xmlBufCat(buf, node->name);
1339
10.2k
                xmlBufAdd(buf, BAD_CAST ";", 1);
1340
10.2k
            }
1341
11.6k
        }
1342
1343
149k
        node = node->next;
1344
149k
    }
1345
1346
134k
    ret = xmlBufDetach(buf);
1347
134k
    xmlBufFree(buf);
1348
134k
    return(ret);
1349
1350
28
error:
1351
28
    xmlBufFree(buf);
1352
28
    return(NULL);
1353
134k
}
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
191k
{
1375
191k
    int flags = 0;
1376
191k
    int escape = 0;
1377
1378
    /* backward compatibility */
1379
191k
    if (list == NULL)
1380
12.7k
        return(NULL);
1381
1382
178k
    if (!inLine) {
1383
131k
        escape = 1;
1384
1385
131k
        if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE))
1386
20.2k
            flags |= XML_ESCAPE_HTML;
1387
111k
        else if ((doc == NULL) || (doc->encoding == NULL))
1388
109k
            flags |= XML_ESCAPE_NON_ASCII;
1389
1390
131k
        if ((list->parent != NULL) &&
1391
130k
            (list->parent->type == XML_ATTRIBUTE_NODE))
1392
130k
            flags |= XML_ESCAPE_ATTR;
1393
131k
    }
1394
1395
178k
    return(xmlNodeListGetStringInternal(list, escape, flags));
1396
191k
}
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
2.48k
{
1416
2.48k
    int escape = 0;
1417
2.48k
    int flags = 0;
1418
1419
    /* backward compatibility */
1420
2.48k
    if (list == NULL)
1421
506
        return(NULL);
1422
1423
1.97k
    if (!inLine) {
1424
857
        escape = 1;
1425
857
        flags = XML_ESCAPE_QUOT;
1426
857
    }
1427
1428
1.97k
    return(xmlNodeListGetStringInternal(list, escape, flags));
1429
2.48k
}
1430
1431
static xmlAttrPtr
1432
xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1433
                   const xmlChar * name, const xmlChar * value,
1434
                   int eatname)
1435
702k
{
1436
702k
    xmlAttrPtr cur;
1437
702k
    xmlDocPtr doc = NULL;
1438
1439
702k
    if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1440
3.21k
        if ((eatname == 1) &&
1441
866
      ((node->doc == NULL) || (node->doc->dict == NULL) ||
1442
214
       (!(xmlDictOwns(node->doc->dict, name)))))
1443
866
            xmlFree((xmlChar *) name);
1444
3.21k
        return (NULL);
1445
3.21k
    }
1446
1447
    /*
1448
     * Allocate a new property and fill the fields.
1449
     */
1450
699k
    cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1451
699k
    if (cur == NULL) {
1452
145
        if ((eatname == 1) &&
1453
5
      ((node == NULL) || (node->doc == NULL) ||
1454
2
             (node->doc->dict == NULL) ||
1455
1
       (!(xmlDictOwns(node->doc->dict, name)))))
1456
5
            xmlFree((xmlChar *) name);
1457
145
        return (NULL);
1458
145
    }
1459
699k
    memset(cur, 0, sizeof(xmlAttr));
1460
699k
    cur->type = XML_ATTRIBUTE_NODE;
1461
1462
699k
    cur->parent = node;
1463
699k
    if (node != NULL) {
1464
695k
        doc = node->doc;
1465
695k
        cur->doc = doc;
1466
695k
    }
1467
699k
    cur->ns = ns;
1468
1469
699k
    if (eatname == 0) {
1470
694k
        if ((doc != NULL) && (doc->dict != NULL))
1471
76.7k
            cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1472
617k
        else
1473
617k
            cur->name = xmlStrdup(name);
1474
694k
        if (cur->name == NULL)
1475
137
            goto error;
1476
694k
    } else
1477
4.58k
        cur->name = name;
1478
1479
699k
    if (value != NULL) {
1480
32.7k
        xmlNodePtr tmp;
1481
1482
32.7k
        cur->children = xmlNewDocText(doc, value);
1483
32.7k
        if (cur->children == NULL)
1484
27
            goto error;
1485
32.7k
        cur->last = NULL;
1486
32.7k
        tmp = cur->children;
1487
65.4k
        while (tmp != NULL) {
1488
32.7k
            tmp->parent = (xmlNodePtr) cur;
1489
32.7k
            if (tmp->next == NULL)
1490
32.7k
                cur->last = tmp;
1491
32.7k
            tmp = tmp->next;
1492
32.7k
        }
1493
1494
32.7k
        if (doc != NULL) {
1495
23.9k
            int res = xmlIsID(doc, node, cur);
1496
1497
23.9k
            if (res < 0)
1498
4
                goto error;
1499
23.8k
            if ((res == 1) && (xmlAddIDSafe(cur, value) < 0))
1500
1
                goto error;
1501
23.8k
        }
1502
32.7k
    }
1503
1504
    /*
1505
     * Add it at the end to preserve parsing order ...
1506
     */
1507
699k
    if (node != NULL) {
1508
694k
        if (node->properties == NULL) {
1509
236k
            node->properties = cur;
1510
458k
        } else {
1511
458k
            xmlAttrPtr prev = node->properties;
1512
1513
844M
            while (prev->next != NULL)
1514
844M
                prev = prev->next;
1515
458k
            prev->next = cur;
1516
458k
            cur->prev = prev;
1517
458k
        }
1518
694k
    }
1519
1520
699k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1521
0
        xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1522
699k
    return (cur);
1523
1524
169
error:
1525
169
    xmlFreeProp(cur);
1526
169
    return(NULL);
1527
699k
}
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
4.29k
xmlNewProp(xmlNode *node, const xmlChar *name, const xmlChar *value) {
1546
1547
4.29k
    if (name == NULL) {
1548
283
  return(NULL);
1549
283
    }
1550
1551
4.01k
  return xmlNewPropInternal(node, NULL, name, value, 0);
1552
4.29k
}
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
664k
           const xmlChar *value) {
1573
1574
664k
    if (name == NULL) {
1575
287
  return(NULL);
1576
287
    }
1577
1578
664k
    return xmlNewPropInternal(node, ns, name, value, 0);
1579
664k
}
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
5.76k
           const xmlChar *value) {
1598
1599
5.76k
    if (name == NULL) {
1600
307
  return(NULL);
1601
307
    }
1602
1603
5.45k
    return xmlNewPropInternal(node, ns, name, value, 1);
1604
5.76k
}
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
2.27M
xmlNewDocProp(xmlDoc *doc, const xmlChar *name, const xmlChar *value) {
1622
2.27M
    xmlAttrPtr cur;
1623
1624
2.27M
    if (name == NULL) {
1625
6.13k
  return(NULL);
1626
6.13k
    }
1627
1628
    /*
1629
     * Allocate a new property and fill the fields.
1630
     */
1631
2.27M
    cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1632
2.27M
    if (cur == NULL)
1633
185
  return(NULL);
1634
2.27M
    memset(cur, 0, sizeof(xmlAttr));
1635
2.27M
    cur->type = XML_ATTRIBUTE_NODE;
1636
1637
2.27M
    if ((doc != NULL) && (doc->dict != NULL))
1638
548k
  cur->name = xmlDictLookup(doc->dict, name, -1);
1639
1.72M
    else
1640
1.72M
  cur->name = xmlStrdup(name);
1641
2.27M
    if (cur->name == NULL)
1642
161
        goto error;
1643
2.27M
    cur->doc = doc;
1644
2.27M
    if (value != NULL) {
1645
        /*
1646
         * We shouldn't parse the attribute value here,
1647
         * but the API can't be changed.
1648
         */
1649
1.45k
        if (xmlNodeParseAttValue(doc, cur, value, SIZE_MAX, NULL) < 0)
1650
5
            goto error;
1651
1.45k
    }
1652
1653
2.27M
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1654
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1655
2.27M
    return(cur);
1656
1657
166
error:
1658
166
    xmlFreeProp(cur);
1659
166
    return(NULL);
1660
2.27M
}
1661
1662
/**
1663
 * Free an attribute list including all children.
1664
 *
1665
 * @param cur  the first attribute in the list
1666
 */
1667
void
1668
1.98M
xmlFreePropList(xmlAttr *cur) {
1669
1.98M
    xmlAttrPtr next;
1670
1.98M
    if (cur == NULL) return;
1671
6.57M
    while (cur != NULL) {
1672
4.59M
        next = cur->next;
1673
4.59M
        xmlFreeProp(cur);
1674
4.59M
  cur = next;
1675
4.59M
    }
1676
1.97M
}
1677
1678
/**
1679
 * Free an attribute including all children.
1680
 *
1681
 * @param cur  an attribute
1682
 */
1683
void
1684
4.61M
xmlFreeProp(xmlAttr *cur) {
1685
4.61M
    xmlDictPtr dict = NULL;
1686
4.61M
    if (cur == NULL) return;
1687
1688
4.61M
    if (cur->doc != NULL) dict = cur->doc->dict;
1689
1690
4.61M
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1691
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1692
1693
    /* Check for ID removal -> leading to invalid references ! */
1694
4.61M
    if (cur->doc != NULL && cur->id != NULL) {
1695
6.46k
        xmlRemoveID(cur->doc, cur);
1696
6.46k
    }
1697
4.61M
    if (cur->children != NULL) xmlFreeNodeList(cur->children);
1698
4.61M
    DICT_FREE(cur->name)
1699
4.61M
    xmlFree(cur);
1700
4.61M
}
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
4.00k
xmlRemoveProp(xmlAttr *cur) {
1715
4.00k
    xmlAttrPtr tmp;
1716
4.00k
    if (cur == NULL) {
1717
2.63k
  return(-1);
1718
2.63k
    }
1719
1.37k
    if (cur->parent == NULL) {
1720
0
  return(-1);
1721
0
    }
1722
1.37k
    tmp = cur->parent->properties;
1723
1.37k
    if (tmp == cur) {
1724
721
        cur->parent->properties = cur->next;
1725
721
    if (cur->next != NULL)
1726
285
      cur->next->prev = NULL;
1727
721
  xmlFreeProp(cur);
1728
721
  return(0);
1729
721
    }
1730
1.18k
    while (tmp != NULL) {
1731
1.18k
  if (tmp->next == cur) {
1732
653
      tmp->next = cur->next;
1733
653
      if (tmp->next != NULL)
1734
172
    tmp->next->prev = tmp;
1735
653
      xmlFreeProp(cur);
1736
653
      return(0);
1737
653
  }
1738
530
        tmp = tmp->next;
1739
530
    }
1740
0
    return(-1);
1741
653
}
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
343k
xmlNewDocPI(xmlDoc *doc, const xmlChar *name, const xmlChar *content) {
1754
343k
    xmlNodePtr cur;
1755
1756
343k
    if (name == NULL) {
1757
556
  return(NULL);
1758
556
    }
1759
1760
    /*
1761
     * Allocate a new node and fill the fields.
1762
     */
1763
343k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1764
343k
    if (cur == NULL)
1765
58
  return(NULL);
1766
343k
    memset(cur, 0, sizeof(xmlNode));
1767
343k
    cur->type = XML_PI_NODE;
1768
343k
    cur->doc = doc;
1769
1770
343k
    if ((doc != NULL) && (doc->dict != NULL))
1771
212k
        cur->name = xmlDictLookup(doc->dict, name, -1);
1772
130k
    else
1773
130k
  cur->name = xmlStrdup(name);
1774
343k
    if (cur->name == NULL)
1775
23
        goto error;
1776
343k
    if (content != NULL) {
1777
106k
  cur->content = xmlStrdup(content);
1778
106k
        if (cur->content == NULL)
1779
28
            goto error;
1780
106k
    }
1781
1782
342k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1783
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1784
342k
    return(cur);
1785
1786
51
error:
1787
51
    xmlFreeNode(cur);
1788
51
    return(NULL);
1789
343k
}
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
2.87k
xmlNewPI(const xmlChar *name, const xmlChar *content) {
1803
2.87k
    return(xmlNewDocPI(NULL, name, content));
1804
2.87k
}
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
5.76k
xmlNewNode(xmlNs *ns, const xmlChar *name) {
1818
5.76k
    return(xmlNewDocNode(NULL, ns, name, NULL));
1819
5.76k
}
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
4.93k
xmlNewNodeEatName(xmlNs *ns, xmlChar *name) {
1837
4.93k
    return(xmlNewDocNodeEatName(NULL, ns, name, NULL));
1838
4.93k
}
1839
1840
static xmlNodePtr
1841
xmlNewElem(xmlDocPtr doc, xmlNsPtr ns, const xmlChar *name,
1842
4.88M
           const xmlChar *content) {
1843
4.88M
    xmlNodePtr cur;
1844
1845
4.88M
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1846
4.88M
    if (cur == NULL)
1847
1.26k
  return(NULL);
1848
4.88M
    memset(cur, 0, sizeof(xmlNode));
1849
1850
4.88M
    cur->type = XML_ELEMENT_NODE;
1851
4.88M
    cur->doc = doc;
1852
4.88M
    cur->name = name;
1853
4.88M
    cur->ns = ns;
1854
1855
4.88M
    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
16.4k
        if (xmlNodeParseAttValue(doc, (xmlAttr *) cur, content, SIZE_MAX,
1861
16.4k
                                 NULL) < 0) {
1862
            /* Don't free name on error */
1863
12
            xmlFree(cur);
1864
12
            return(NULL);
1865
12
        }
1866
16.4k
    }
1867
1868
4.88M
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1869
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1870
1871
4.88M
    return(cur);
1872
4.88M
}
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
2.16M
              const xmlChar *name, const xmlChar *content) {
1901
2.16M
    xmlNodePtr cur;
1902
2.16M
    xmlChar *copy;
1903
1904
2.16M
    if (name == NULL)
1905
1.66k
        return(NULL);
1906
1907
2.16M
    if ((doc != NULL) && (doc->dict != NULL)) {
1908
301k
        const xmlChar *dictName = xmlDictLookup(doc->dict, name, -1);
1909
1910
301k
        if (dictName == NULL)
1911
15
            return(NULL);
1912
301k
        return(xmlNewElem(doc, ns, dictName, content));
1913
301k
    }
1914
1915
1.85M
    copy = xmlStrdup(name);
1916
1.85M
    if (copy == NULL)
1917
544
        return(NULL);
1918
1919
1.85M
    cur = xmlNewElem(doc, ns, copy, content);
1920
1.85M
    if (cur == NULL) {
1921
454
        xmlFree(copy);
1922
454
        return(NULL);
1923
454
    }
1924
1925
1.85M
    return(cur);
1926
1.85M
}
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
2.73M
                     xmlChar *name, const xmlChar *content) {
1945
2.73M
    xmlNodePtr cur;
1946
1947
2.73M
    if (name == NULL)
1948
1.13k
        return(NULL);
1949
1950
2.73M
    cur = xmlNewElem(doc, ns, name, content);
1951
2.73M
    if (cur == NULL) {
1952
        /* if name doesn't come from the doc dictionary free it here */
1953
374
        if ((doc == NULL) ||
1954
371
            (doc->dict == NULL) ||
1955
350
            (!xmlDictOwns(doc->dict, name)))
1956
25
            xmlFree(name);
1957
374
        return(NULL);
1958
374
    }
1959
1960
2.72M
    return(cur);
1961
2.73M
}
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
63.1k
                 const xmlChar *name, const xmlChar *content) {
1978
63.1k
    xmlNodePtr cur;
1979
1980
63.1k
    cur = xmlNewDocNode(doc, ns, name, NULL);
1981
63.1k
    if (cur != NULL) {
1982
62.4k
        cur->doc = doc;
1983
62.4k
  if (content != NULL) {
1984
26.1k
            xmlNodePtr text;
1985
1986
26.1k
      text = xmlNewDocText(doc, content);
1987
26.1k
            if (text == NULL) {
1988
4
                xmlFreeNode(cur);
1989
4
                return(NULL);
1990
4
            }
1991
1992
26.1k
            cur->children = text;
1993
26.1k
            cur->last = text;
1994
26.1k
            text->parent = cur;
1995
26.1k
  }
1996
62.4k
    }
1997
63.1k
    return(cur);
1998
63.1k
}
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
4.14k
xmlNewDocFragment(xmlDoc *doc) {
2009
4.14k
    xmlNodePtr cur;
2010
2011
    /*
2012
     * Allocate a new DocumentFragment node and fill the fields.
2013
     */
2014
4.14k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2015
4.14k
    if (cur == NULL)
2016
2
  return(NULL);
2017
4.14k
    memset(cur, 0, sizeof(xmlNode));
2018
4.14k
    cur->type = XML_DOCUMENT_FRAG_NODE;
2019
2020
4.14k
    cur->doc = doc;
2021
2022
4.14k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2023
0
  xmlRegisterNodeDefaultValue(cur);
2024
4.14k
    return(cur);
2025
4.14k
}
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
807k
xmlNewText(const xmlChar *content) {
2038
807k
    xmlNodePtr cur;
2039
2040
    /*
2041
     * Allocate a new node and fill the fields.
2042
     */
2043
807k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2044
807k
    if (cur == NULL)
2045
239
  return(NULL);
2046
806k
    memset(cur, 0, sizeof(xmlNode));
2047
806k
    cur->type = XML_TEXT_NODE;
2048
2049
806k
    cur->name = xmlStringText;
2050
806k
    if (content != NULL) {
2051
270k
  cur->content = xmlStrdup(content);
2052
270k
        if (cur->content == NULL)
2053
123
            goto error;
2054
270k
    }
2055
2056
806k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2057
0
  xmlRegisterNodeDefaultValue(cur);
2058
806k
    return(cur);
2059
2060
123
error:
2061
123
    xmlFreeNode(cur);
2062
123
    return(NULL);
2063
806k
}
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
51.3k
            const xmlChar *name, const xmlChar *content) {
2084
51.3k
    xmlNodePtr cur, prev;
2085
2086
51.3k
    if ((parent == NULL) || (name == NULL))
2087
1.37k
  return(NULL);
2088
2089
49.9k
    switch (parent->type) {
2090
2.94k
        case XML_DOCUMENT_NODE:
2091
10.6k
        case XML_HTML_DOCUMENT_NODE:
2092
10.9k
        case XML_DOCUMENT_FRAG_NODE:
2093
10.9k
            break;
2094
2095
38.6k
        case XML_ELEMENT_NODE:
2096
38.6k
            if (ns == NULL)
2097
24.7k
                ns = parent->ns;
2098
38.6k
            break;
2099
2100
316
        default:
2101
316
            return(NULL);
2102
49.9k
    }
2103
2104
49.6k
    cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2105
49.6k
    if (cur == NULL)
2106
8
        return(NULL);
2107
2108
    /*
2109
     * add the new element at the end of the children list.
2110
     */
2111
49.6k
    cur->parent = parent;
2112
49.6k
    if (parent->children == NULL) {
2113
28.4k
        parent->children = cur;
2114
28.4k
  parent->last = cur;
2115
28.4k
    } else {
2116
21.2k
        prev = parent->last;
2117
21.2k
  prev->next = cur;
2118
21.2k
  cur->prev = prev;
2119
21.2k
  parent->last = cur;
2120
21.2k
    }
2121
2122
49.6k
    return(cur);
2123
49.6k
}
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
269k
xmlNewEntityRef(xmlDocPtr doc, xmlChar *name) {
2138
269k
    xmlNodePtr cur;
2139
2140
    /*
2141
     * Allocate a new node and fill the fields.
2142
     */
2143
269k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2144
269k
    if (cur == NULL) {
2145
71
        xmlFree(name);
2146
71
  return(NULL);
2147
71
    }
2148
269k
    memset(cur, 0, sizeof(xmlNode));
2149
269k
    cur->type = XML_ENTITY_REF_NODE;
2150
269k
    cur->doc = doc;
2151
269k
    cur->name = name;
2152
2153
269k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2154
0
  xmlRegisterNodeDefaultValue(cur);
2155
2156
269k
    return(cur);
2157
269k
}
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
2.66k
xmlNewCharRef(xmlDoc *doc, const xmlChar *name) {
2176
2.66k
    xmlChar *copy;
2177
2178
2.66k
    if (name == NULL)
2179
252
        return(NULL);
2180
2181
2.40k
    if (name[0] == '&') {
2182
540
        int len;
2183
540
        name++;
2184
540
  len = xmlStrlen(name);
2185
540
  if (name[len - 1] == ';')
2186
296
      copy = xmlStrndup(name, len - 1);
2187
244
  else
2188
244
      copy = xmlStrndup(name, len);
2189
540
    } else
2190
1.86k
  copy = xmlStrdup(name);
2191
2.40k
    if (copy == NULL)
2192
2
        return(NULL);
2193
2194
2.40k
    return(xmlNewEntityRef(doc, copy));
2195
2.40k
}
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
133k
xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
2210
133k
    xmlNodePtr cur;
2211
133k
    xmlEntityPtr ent;
2212
2213
133k
    if (name == NULL)
2214
247
        return(NULL);
2215
2216
    /*
2217
     * Allocate a new node and fill the fields.
2218
     */
2219
133k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2220
133k
    if (cur == NULL)
2221
49
  return(NULL);
2222
133k
    memset(cur, 0, sizeof(xmlNode));
2223
133k
    cur->type = XML_ENTITY_REF_NODE;
2224
2225
133k
    cur->doc = (xmlDoc *)doc;
2226
133k
    if (name[0] == '&') {
2227
489
        int len;
2228
489
        name++;
2229
489
  len = xmlStrlen(name);
2230
489
  if (name[len - 1] == ';')
2231
288
      cur->name = xmlStrndup(name, len - 1);
2232
201
  else
2233
201
      cur->name = xmlStrndup(name, len);
2234
489
    } else
2235
132k
  cur->name = xmlStrdup(name);
2236
133k
    if (cur->name == NULL)
2237
30
        goto error;
2238
2239
133k
    ent = xmlGetDocEntity(doc, cur->name);
2240
133k
    if (ent != NULL) {
2241
26.1k
  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
26.1k
  cur->children = (xmlNodePtr) ent;
2248
26.1k
  cur->last = (xmlNodePtr) ent;
2249
26.1k
    }
2250
2251
133k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2252
0
  xmlRegisterNodeDefaultValue(cur);
2253
133k
    return(cur);
2254
2255
30
error:
2256
30
    xmlFreeNode(cur);
2257
30
    return(NULL);
2258
133k
}
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
805k
xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
2270
805k
    xmlNodePtr cur;
2271
2272
805k
    cur = xmlNewText(content);
2273
805k
    if (cur != NULL) cur->doc = (xmlDoc *)doc;
2274
805k
    return(cur);
2275
805k
}
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
30.7k
xmlNewTextLen(const xmlChar *content, int len) {
2289
30.7k
    xmlNodePtr cur;
2290
2291
    /*
2292
     * Allocate a new node and fill the fields.
2293
     */
2294
30.7k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2295
30.7k
    if (cur == NULL)
2296
7
  return(NULL);
2297
30.7k
    memset(cur, 0, sizeof(xmlNode));
2298
30.7k
    cur->type = XML_TEXT_NODE;
2299
2300
30.7k
    cur->name = xmlStringText;
2301
30.7k
    if (content != NULL) {
2302
23.6k
  cur->content = xmlStrndup(content, len);
2303
23.6k
        if (cur->content == NULL) {
2304
7
            xmlFreeNode(cur);
2305
7
            return(NULL);
2306
7
        }
2307
23.6k
    }
2308
2309
30.7k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2310
0
  xmlRegisterNodeDefaultValue(cur);
2311
30.7k
    return(cur);
2312
30.7k
}
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
26.7k
xmlNewDocTextLen(xmlDoc *doc, const xmlChar *content, int len) {
2325
26.7k
    xmlNodePtr cur;
2326
2327
26.7k
    cur = xmlNewTextLen(content, len);
2328
26.7k
    if (cur != NULL) cur->doc = doc;
2329
26.7k
    return(cur);
2330
26.7k
}
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
2.59M
xmlNewComment(const xmlChar *content) {
2343
2.59M
    xmlNodePtr cur;
2344
2345
    /*
2346
     * Allocate a new node and fill the fields.
2347
     */
2348
2.59M
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2349
2.59M
    if (cur == NULL)
2350
88
  return(NULL);
2351
2.59M
    memset(cur, 0, sizeof(xmlNode));
2352
2.59M
    cur->type = XML_COMMENT_NODE;
2353
2354
2.59M
    cur->name = xmlStringComment;
2355
2.59M
    if (content != NULL) {
2356
2.59M
  cur->content = xmlStrdup(content);
2357
2.59M
        if (cur->content == NULL)
2358
119
            goto error;
2359
2.59M
    }
2360
2361
2.59M
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2362
0
  xmlRegisterNodeDefaultValue(cur);
2363
2.59M
    return(cur);
2364
2365
119
error:
2366
119
    xmlFreeNode(cur);
2367
119
    return(NULL);
2368
2.59M
}
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
118k
xmlNewCDataBlock(xmlDoc *doc, const xmlChar *content, int len) {
2381
118k
    xmlNodePtr cur;
2382
2383
    /*
2384
     * Allocate a new node and fill the fields.
2385
     */
2386
118k
    cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2387
118k
    if (cur == NULL)
2388
64
  return(NULL);
2389
118k
    memset(cur, 0, sizeof(xmlNode));
2390
118k
    cur->type = XML_CDATA_SECTION_NODE;
2391
118k
    cur->doc = doc;
2392
2393
118k
    if (content != NULL) {
2394
118k
  cur->content = xmlStrndup(content, len);
2395
118k
        if (cur->content == NULL) {
2396
54
            xmlFree(cur);
2397
54
            return(NULL);
2398
54
        }
2399
118k
    }
2400
2401
118k
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2402
0
  xmlRegisterNodeDefaultValue(cur);
2403
118k
    return(cur);
2404
118k
}
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
2.59M
xmlNewDocComment(xmlDoc *doc, const xmlChar *content) {
2416
2.59M
    xmlNodePtr cur;
2417
2418
2.59M
    cur = xmlNewComment(content);
2419
2.59M
    if (cur != NULL) cur->doc = doc;
2420
2.59M
    return(cur);
2421
2.59M
}
2422
2423
static void
2424
13.4k
xmlRemoveEntity(xmlEntityPtr ent) {
2425
13.4k
    xmlDocPtr doc = ent->doc;
2426
13.4k
    xmlDtdPtr intSubset, extSubset;
2427
2428
13.4k
    if (doc == NULL)
2429
13.4k
        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
460k
xmlNodeSetDoc(xmlNodePtr node, xmlDocPtr doc) {
2459
460k
    xmlDocPtr oldDoc;
2460
460k
    xmlDictPtr oldDict, newDict;
2461
460k
    int ret = 0;
2462
2463
    /*
2464
     * Remove name and content from old dictionary
2465
     */
2466
2467
460k
    oldDoc = node->doc;
2468
460k
    oldDict = oldDoc ? oldDoc->dict : NULL;
2469
460k
    newDict = doc ? doc->dict : NULL;
2470
2471
460k
    if ((oldDict != NULL) && (oldDict != newDict)) {
2472
7.66k
        if ((node->name != NULL) &&
2473
7.09k
            ((node->type == XML_ELEMENT_NODE) ||
2474
3.45k
             (node->type == XML_ATTRIBUTE_NODE) ||
2475
2.59k
             (node->type == XML_PI_NODE) ||
2476
2.32k
             (node->type == XML_ENTITY_REF_NODE)) &&
2477
5.18k
            (xmlDictOwns(oldDict, node->name))) {
2478
3.17k
            if (newDict)
2479
1.51k
                node->name = xmlDictLookup(newDict, node->name, -1);
2480
1.66k
            else
2481
1.66k
                node->name = xmlStrdup(node->name);
2482
3.17k
            if (node->name == NULL)
2483
11
                ret = -1;
2484
3.17k
        }
2485
2486
7.66k
        if ((node->content != NULL) &&
2487
2.78k
            ((node->type == XML_TEXT_NODE) ||
2488
873
             (node->type == XML_CDATA_SECTION_NODE)) &&
2489
2.39k
            (xmlDictOwns(oldDict, node->content))) {
2490
361
            node->content = xmlStrdup(node->content);
2491
361
            if (node->content == NULL)
2492
4
                ret = -1;
2493
361
        }
2494
7.66k
    }
2495
2496
460k
    switch (node->type) {
2497
36.9k
        case XML_ATTRIBUTE_NODE: {
2498
36.9k
            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
36.9k
            if (attr->id != NULL)
2507
67
                xmlRemoveID(oldDoc, attr);
2508
2509
36.9k
            break;
2510
0
        }
2511
2512
3.62k
        case XML_ENTITY_REF_NODE:
2513
            /*
2514
             * Handle entity references
2515
             */
2516
3.62k
            node->children = NULL;
2517
3.62k
            node->last = NULL;
2518
3.62k
            node->content = NULL;
2519
2520
3.62k
            if ((doc != NULL) &&
2521
3.02k
                ((doc->intSubset != NULL) || (doc->extSubset != NULL))) {
2522
1.83k
                xmlEntityPtr ent;
2523
2524
                /*
2525
                * Assign new entity node if available
2526
                */
2527
1.83k
                ent = xmlGetDocEntity(doc, node->name);
2528
1.83k
                if (ent != NULL) {
2529
983
                    node->children = (xmlNodePtr) ent;
2530
983
                    node->last = (xmlNodePtr) ent;
2531
983
                    node->content = ent->content;
2532
983
                }
2533
1.83k
            }
2534
2535
3.62k
            break;
2536
2537
22.6k
        case XML_DTD_NODE:
2538
22.6k
            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
22.6k
            break;
2546
2547
13.4k
        case XML_ENTITY_DECL:
2548
13.4k
            xmlRemoveEntity((xmlEntityPtr) node);
2549
13.4k
            break;
2550
2551
        /*
2552
         * TODO:
2553
         * - Remove element decls from doc->elements
2554
         * - Remove attribtue decls form doc->attributes
2555
         */
2556
2557
383k
        default:
2558
383k
            break;
2559
460k
    }
2560
2561
    /*
2562
     * Set new document
2563
     */
2564
460k
    node->doc = doc;
2565
2566
460k
    return(ret);
2567
460k
}
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
420k
xmlSetTreeDoc(xmlNode *tree, xmlDoc *doc) {
2588
420k
    int ret = 0;
2589
2590
420k
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2591
0
  return(0);
2592
420k
    if (tree->doc == doc)
2593
11.3k
        return(0);
2594
2595
408k
    if (tree->type == XML_ELEMENT_NODE) {
2596
244k
        xmlAttrPtr prop = tree->properties;
2597
2598
270k
        while (prop != NULL) {
2599
25.4k
            if (prop->children != NULL) {
2600
24.3k
                if (xmlSetListDoc(prop->children, doc) < 0)
2601
1
                    ret = -1;
2602
24.3k
            }
2603
2604
25.4k
            if (xmlNodeSetDoc((xmlNodePtr) prop, doc) < 0)
2605
2
                ret = -1;
2606
2607
25.4k
            prop = prop->next;
2608
25.4k
        }
2609
244k
    }
2610
2611
408k
    if ((tree->children != NULL) &&
2612
69.1k
        (tree->type != XML_ENTITY_REF_NODE)) {
2613
68.8k
        if (xmlSetListDoc(tree->children, doc) < 0)
2614
235
            ret = -1;
2615
68.8k
    }
2616
2617
408k
    if (xmlNodeSetDoc(tree, doc) < 0)
2618
12
        ret = -1;
2619
2620
408k
    return(ret);
2621
420k
}
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
93.2k
xmlSetListDoc(xmlNode *list, xmlDoc *doc) {
2636
93.2k
    xmlNodePtr cur;
2637
93.2k
    int ret = 0;
2638
2639
93.2k
    if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2640
0
  return(0);
2641
2642
93.2k
    cur = list;
2643
466k
    while (cur != NULL) {
2644
373k
  if (cur->doc != doc) {
2645
373k
      if (xmlSetTreeDoc(cur, doc) < 0)
2646
236
                ret = -1;
2647
373k
        }
2648
373k
  cur = cur->next;
2649
373k
    }
2650
2651
93.2k
    return(ret);
2652
93.2k
}
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
23.4k
            const xmlChar *name, const xmlChar *content) {
2675
23.4k
    xmlNodePtr cur, prev;
2676
2677
23.4k
    if ((parent == NULL) || (name == NULL))
2678
1.10k
  return(NULL);
2679
2680
22.3k
    switch (parent->type) {
2681
1.66k
        case XML_DOCUMENT_NODE:
2682
6.26k
        case XML_HTML_DOCUMENT_NODE:
2683
6.81k
        case XML_DOCUMENT_FRAG_NODE:
2684
6.81k
            break;
2685
2686
15.2k
        case XML_ELEMENT_NODE:
2687
15.2k
            if (ns == NULL)
2688
8.85k
                ns = parent->ns;
2689
15.2k
            break;
2690
2691
303
        default:
2692
303
            return(NULL);
2693
22.3k
    }
2694
2695
22.0k
    cur = xmlNewDocNode(parent->doc, ns, name, content);
2696
22.0k
    if (cur == NULL)
2697
13
        return(NULL);
2698
2699
    /*
2700
     * add the new element at the end of the children list.
2701
     */
2702
22.0k
    cur->parent = parent;
2703
22.0k
    if (parent->children == NULL) {
2704
12.3k
        parent->children = cur;
2705
12.3k
  parent->last = cur;
2706
12.3k
    } else {
2707
9.66k
        prev = parent->last;
2708
9.66k
  prev->next = cur;
2709
9.66k
  cur->prev = prev;
2710
9.66k
  parent->last = cur;
2711
9.66k
    }
2712
2713
22.0k
    return(cur);
2714
22.0k
}
2715
2716
static void
2717
28.5k
xmlTextSetContent(xmlNodePtr text, xmlChar *content) {
2718
28.5k
    if ((text->content != NULL) &&
2719
27.3k
        (text->content != (xmlChar *) &text->properties)) {
2720
26.5k
        xmlDocPtr doc = text->doc;
2721
2722
26.5k
        if ((doc == NULL) ||
2723
22.9k
            (doc->dict == NULL) ||
2724
9.04k
            (!xmlDictOwns(doc->dict, text->content)))
2725
25.6k
            xmlFree(text->content);
2726
26.5k
    }
2727
2728
28.5k
    text->content = content;
2729
28.5k
    text->properties = NULL;
2730
28.5k
}
2731
2732
static int
2733
25.4k
xmlTextAddContent(xmlNodePtr text, const xmlChar *content, int len) {
2734
25.4k
    xmlChar *merged;
2735
2736
25.4k
    if (content == NULL)
2737
1.06k
        return(0);
2738
2739
24.4k
    merged = xmlStrncatNew(text->content, content, len);
2740
24.4k
    if (merged == NULL)
2741
11
        return(-1);
2742
2743
24.3k
    xmlTextSetContent(text, merged);
2744
24.3k
    return(0);
2745
24.4k
}
2746
2747
static xmlNodePtr
2748
xmlInsertProp(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent,
2749
6.90k
              xmlNodePtr prev, xmlNodePtr next) {
2750
6.90k
    xmlAttrPtr attr;
2751
2752
6.90k
    if (((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)) ||
2753
6.90k
        ((next != NULL) && (next->type != XML_ATTRIBUTE_NODE)))
2754
0
        return(NULL);
2755
2756
    /* check if an attribute with the same name exists */
2757
6.90k
    attr = xmlGetPropNodeInternal(parent, cur->name,
2758
6.90k
                                  cur->ns ? cur->ns->href : NULL, 0);
2759
2760
6.90k
    xmlUnlinkNodeInternal(cur);
2761
2762
6.90k
    if (cur->doc != doc) {
2763
4.36k
        if (xmlSetTreeDoc(cur, doc) < 0)
2764
1
            return(NULL);
2765
4.36k
    }
2766
2767
6.89k
    cur->parent = parent;
2768
6.89k
    cur->prev = prev;
2769
6.89k
    cur->next = next;
2770
2771
6.89k
    if (prev == NULL) {
2772
4.86k
        if (parent != NULL)
2773
4.86k
            parent->properties = (xmlAttrPtr) cur;
2774
4.86k
    } else {
2775
2.03k
        prev->next = cur;
2776
2.03k
    }
2777
6.89k
    if (next != NULL) {
2778
629
        next->prev = cur;
2779
629
    }
2780
2781
6.89k
    if ((attr != NULL) && (attr != (xmlAttrPtr) cur)) {
2782
        /* different instance, destroy it (attributes must be unique) */
2783
288
        xmlRemoveProp((xmlAttrPtr) attr);
2784
288
    }
2785
2786
6.89k
    return cur;
2787
6.90k
}
2788
2789
static xmlNodePtr
2790
xmlInsertNode(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent,
2791
352k
              xmlNodePtr prev, xmlNodePtr next, int coalesce) {
2792
352k
    xmlNodePtr oldParent;
2793
2794
352k
    if (cur->type == XML_ATTRIBUTE_NODE)
2795
6.90k
  return xmlInsertProp(doc, cur, parent, prev, next);
2796
2797
    /*
2798
     * Coalesce text nodes
2799
     */
2800
345k
    if ((coalesce) && (cur->type == XML_TEXT_NODE)) {
2801
21.5k
  if ((prev != NULL) && (prev->type == XML_TEXT_NODE) &&
2802
19.9k
            (prev->name == cur->name)) {
2803
19.9k
            if (xmlTextAddContent(prev, cur->content, -1) < 0)
2804
4
                return(NULL);
2805
19.9k
            xmlUnlinkNodeInternal(cur);
2806
19.9k
      xmlFreeNode(cur);
2807
19.9k
      return(prev);
2808
19.9k
  }
2809
2810
1.56k
  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
1.56k
    }
2826
2827
    /* Unlink */
2828
325k
    oldParent = cur->parent;
2829
325k
    if (oldParent != NULL) {
2830
3.59k
        if (oldParent->children == cur)
2831
793
            oldParent->children = cur->next;
2832
3.59k
        if (oldParent->last == cur)
2833
2.45k
            oldParent->last = cur->prev;
2834
3.59k
    }
2835
325k
    if (cur->next != NULL)
2836
264k
        cur->next->prev = cur->prev;
2837
325k
    if (cur->prev != NULL)
2838
2.55k
        cur->prev->next = cur->next;
2839
2840
325k
    if (cur->doc != doc) {
2841
3.25k
  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
10
            cur->parent = NULL;
2850
10
            cur->prev = NULL;
2851
10
            cur->next = NULL;
2852
10
            return(NULL);
2853
10
        }
2854
3.25k
    }
2855
2856
325k
    cur->parent = parent;
2857
325k
    cur->prev = prev;
2858
325k
    cur->next = next;
2859
2860
325k
    if (prev == NULL) {
2861
8.50k
        if (parent != NULL)
2862
8.50k
            parent->children = cur;
2863
317k
    } else {
2864
317k
        prev->next = cur;
2865
317k
    }
2866
325k
    if (next == NULL) {
2867
29.9k
        if (parent != NULL)
2868
29.9k
            parent->last = cur;
2869
295k
    } else {
2870
295k
        next->prev = cur;
2871
295k
    }
2872
2873
325k
    return(cur);
2874
325k
}
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
28.7k
xmlAddNextSibling(xmlNode *prev, xmlNode *cur) {
2894
28.7k
    if ((prev == NULL) || (prev->type == XML_NAMESPACE_DECL) ||
2895
28.3k
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
2896
27.9k
        (cur == prev))
2897
1.05k
  return(NULL);
2898
2899
27.7k
    if (cur == prev->next)
2900
246
        return(cur);
2901
2902
27.4k
    return(xmlInsertNode(prev->doc, cur, prev->parent, prev, prev->next, 0));
2903
27.7k
}
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
275k
xmlAddPrevSibling(xmlNode *next, xmlNode *cur) {
2923
275k
    if ((next == NULL) || (next->type == XML_NAMESPACE_DECL) ||
2924
275k
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
2925
274k
        (cur == next))
2926
1.74k
  return(NULL);
2927
2928
274k
    if (cur == next->prev)
2929
394
        return(cur);
2930
2931
273k
    return(xmlInsertNode(next->doc, cur, next->parent, next->prev, next, 0));
2932
274k
}
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
2.56k
xmlAddSibling(xmlNode *node, xmlNode *cur) {
2954
2.56k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2955
2.04k
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
2956
1.45k
        (cur == node))
2957
1.30k
  return(NULL);
2958
2959
    /*
2960
     * Constant time is we can rely on the ->parent->last to find
2961
     * the last sibling.
2962
     */
2963
1.25k
    if ((node->type != XML_ATTRIBUTE_NODE) && (node->parent != NULL)) {
2964
524
        if (node->parent->last != NULL)
2965
524
      node = node->parent->last;
2966
729
    } else {
2967
1.03k
  while (node->next != NULL)
2968
303
            node = node->next;
2969
729
    }
2970
2971
1.25k
    if (cur == node)
2972
460
        return(cur);
2973
2974
793
    return(xmlInsertNode(node->doc, cur, node->parent, node, NULL, 1));
2975
1.25k
}
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
55.4k
xmlAddChild(xmlNode *parent, xmlNode *cur) {
3099
55.4k
    xmlNodePtr prev;
3100
3101
55.4k
    if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL) ||
3102
54.7k
        (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
3103
53.1k
        (parent == cur))
3104
2.36k
        return(NULL);
3105
3106
    /*
3107
     * If parent is a text node, call xmlTextAddContent. This
3108
     * undocumented quirk should probably be removed.
3109
     */
3110
53.1k
    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
53.1k
    if (cur->type == XML_ATTRIBUTE_NODE) {
3119
7.10k
        prev = (xmlNodePtr) parent->properties;
3120
7.10k
        if (prev != NULL) {
3121
4.49k
            while (prev->next != NULL)
3122
1.80k
                prev = prev->next;
3123
2.68k
        }
3124
45.9k
    } else {
3125
45.9k
        prev = parent->last;
3126
45.9k
    }
3127
3128
53.1k
    if (cur == prev)
3129
2.46k
        return(cur);
3130
3131
50.6k
    return(xmlInsertNode(parent->doc, cur, parent, prev, NULL, 1));
3132
53.1k
}
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
65.3k
xmlGetLastChild(const xmlNode *parent) {
3142
65.3k
    if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3143
0
  return(NULL);
3144
0
    }
3145
65.3k
    return(parent->last);
3146
65.3k
}
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
2.62k
xmlChildElementCount(xmlNode *parent) {
3163
2.62k
    unsigned long ret = 0;
3164
2.62k
    xmlNodePtr cur = NULL;
3165
3166
2.62k
    if (parent == NULL)
3167
596
        return(0);
3168
2.02k
    switch (parent->type) {
3169
681
        case XML_ELEMENT_NODE:
3170
1.04k
        case XML_DOCUMENT_NODE:
3171
1.23k
        case XML_DOCUMENT_FRAG_NODE:
3172
1.53k
        case XML_HTML_DOCUMENT_NODE:
3173
1.74k
        case XML_ENTITY_DECL:
3174
1.74k
            cur = parent->children;
3175
1.74k
            break;
3176
284
        default:
3177
284
            return(0);
3178
2.02k
    }
3179
3.49k
    while (cur != NULL) {
3180
1.74k
        if (cur->type == XML_ELEMENT_NODE)
3181
656
            ret++;
3182
1.74k
        cur = cur->next;
3183
1.74k
    }
3184
1.74k
    return(ret);
3185
2.02k
}
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
5.07k
xmlFirstElementChild(xmlNode *parent) {
3197
5.07k
    xmlNodePtr cur = NULL;
3198
3199
5.07k
    if (parent == NULL)
3200
726
        return(NULL);
3201
4.34k
    switch (parent->type) {
3202
658
        case XML_ELEMENT_NODE:
3203
3.01k
        case XML_DOCUMENT_NODE:
3204
3.21k
        case XML_DOCUMENT_FRAG_NODE:
3205
3.53k
        case XML_HTML_DOCUMENT_NODE:
3206
3.73k
        case XML_ENTITY_DECL:
3207
3.73k
            cur = parent->children;
3208
3.73k
            break;
3209
619
        default:
3210
619
            return(NULL);
3211
4.34k
    }
3212
5.44k
    while (cur != NULL) {
3213
4.07k
        if (cur->type == XML_ELEMENT_NODE)
3214
2.35k
            return(cur);
3215
1.71k
        cur = cur->next;
3216
1.71k
    }
3217
1.37k
    return(NULL);
3218
3.73k
}
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
2.56k
xmlLastElementChild(xmlNode *parent) {
3230
2.56k
    xmlNodePtr cur = NULL;
3231
3232
2.56k
    if (parent == NULL)
3233
657
        return(NULL);
3234
1.91k
    switch (parent->type) {
3235
302
        case XML_ELEMENT_NODE:
3236
640
        case XML_DOCUMENT_NODE:
3237
973
        case XML_DOCUMENT_FRAG_NODE:
3238
1.42k
        case XML_HTML_DOCUMENT_NODE:
3239
1.62k
        case XML_ENTITY_DECL:
3240
1.62k
            cur = parent->last;
3241
1.62k
            break;
3242
286
        default:
3243
286
            return(NULL);
3244
1.91k
    }
3245
2.19k
    while (cur != NULL) {
3246
900
        if (cur->type == XML_ELEMENT_NODE)
3247
327
            return(cur);
3248
573
        cur = cur->prev;
3249
573
    }
3250
1.29k
    return(NULL);
3251
1.62k
}
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
2.63k
xmlPreviousElementSibling(xmlNode *node) {
3263
2.63k
    if (node == NULL)
3264
812
        return(NULL);
3265
1.82k
    switch (node->type) {
3266
303
        case XML_ELEMENT_NODE:
3267
530
        case XML_TEXT_NODE:
3268
596
        case XML_CDATA_SECTION_NODE:
3269
802
        case XML_ENTITY_REF_NODE:
3270
999
        case XML_PI_NODE:
3271
1.32k
        case XML_COMMENT_NODE:
3272
1.32k
        case XML_XINCLUDE_START:
3273
1.32k
        case XML_XINCLUDE_END:
3274
1.32k
            node = node->prev;
3275
1.32k
            break;
3276
503
        default:
3277
503
            return(NULL);
3278
1.82k
    }
3279
1.55k
    while (node != NULL) {
3280
326
        if (node->type == XML_ELEMENT_NODE)
3281
92
            return(node);
3282
234
        node = node->prev;
3283
234
    }
3284
1.22k
    return(NULL);
3285
1.32k
}
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
3.35k
xmlNextElementSibling(xmlNode *node) {
3297
3.35k
    if (node == NULL)
3298
806
        return(NULL);
3299
2.55k
    switch (node->type) {
3300
257
        case XML_ELEMENT_NODE:
3301
627
        case XML_TEXT_NODE:
3302
695
        case XML_CDATA_SECTION_NODE:
3303
909
        case XML_ENTITY_REF_NODE:
3304
1.11k
        case XML_PI_NODE:
3305
1.49k
        case XML_COMMENT_NODE:
3306
2.06k
        case XML_DTD_NODE:
3307
2.06k
        case XML_XINCLUDE_START:
3308
2.06k
        case XML_XINCLUDE_END:
3309
2.06k
            node = node->next;
3310
2.06k
            break;
3311
482
        default:
3312
482
            return(NULL);
3313
2.55k
    }
3314
2.37k
    while (node != NULL) {
3315
728
        if (node->type == XML_ELEMENT_NODE)
3316
419
            return(node);
3317
309
        node = node->next;
3318
309
    }
3319
1.64k
    return(NULL);
3320
2.06k
}
3321
3322
/**
3323
 * Free a node list including all children.
3324
 *
3325
 * @param cur  the first node in the list
3326
 */
3327
void
3328
4.39M
xmlFreeNodeList(xmlNode *cur) {
3329
4.39M
    xmlNodePtr next;
3330
4.39M
    xmlNodePtr parent;
3331
4.39M
    xmlDictPtr dict = NULL;
3332
4.39M
    size_t depth = 0;
3333
3334
4.39M
    if (cur == NULL) return;
3335
4.36M
    if (cur->type == XML_NAMESPACE_DECL) {
3336
0
  xmlFreeNsList((xmlNsPtr) cur);
3337
0
  return;
3338
0
    }
3339
4.36M
    if (cur->doc != NULL) dict = cur->doc->dict;
3340
28.2M
    while (1) {
3341
36.1M
        while ((cur->children != NULL) &&
3342
8.17M
               (cur->type != XML_DOCUMENT_NODE) &&
3343
8.16M
               (cur->type != XML_HTML_DOCUMENT_NODE) &&
3344
8.16M
               (cur->type != XML_DTD_NODE) &&
3345
8.16M
               (cur->type != XML_ENTITY_REF_NODE)) {
3346
7.87M
            cur = cur->children;
3347
7.87M
            depth += 1;
3348
7.87M
        }
3349
3350
28.2M
        next = cur->next;
3351
28.2M
        parent = cur->parent;
3352
28.2M
  if ((cur->type == XML_DOCUMENT_NODE) ||
3353
28.2M
            (cur->type == XML_HTML_DOCUMENT_NODE)) {
3354
6.15k
            xmlFreeDoc((xmlDocPtr) cur);
3355
28.2M
        } 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
2.82k
            cur->prev = NULL;
3361
2.82k
            cur->next = NULL;
3362
28.2M
        } else {
3363
28.2M
      if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3364
0
    xmlDeregisterNodeDefaultValue(cur);
3365
3366
28.2M
      if (((cur->type == XML_ELEMENT_NODE) ||
3367
16.1M
     (cur->type == XML_XINCLUDE_START) ||
3368
16.0M
     (cur->type == XML_XINCLUDE_END)) &&
3369
14.5M
    (cur->properties != NULL))
3370
1.94M
    xmlFreePropList(cur->properties);
3371
28.2M
      if ((cur->type != XML_ELEMENT_NODE) &&
3372
16.1M
    (cur->type != XML_XINCLUDE_START) &&
3373
16.0M
    (cur->type != XML_XINCLUDE_END) &&
3374
13.7M
    (cur->type != XML_ENTITY_REF_NODE) &&
3375
13.2M
    (cur->content != (xmlChar *) &(cur->properties))) {
3376
12.8M
    DICT_FREE(cur->content)
3377
12.8M
      }
3378
28.2M
      if (((cur->type == XML_ELEMENT_NODE) ||
3379
16.1M
           (cur->type == XML_XINCLUDE_START) ||
3380
16.0M
     (cur->type == XML_XINCLUDE_END)) &&
3381
14.5M
    (cur->nsDef != NULL))
3382
3.60M
    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
28.2M
      if ((cur->name != NULL) &&
3391
27.9M
    (cur->type != XML_TEXT_NODE) &&
3392
17.5M
    (cur->type != XML_COMMENT_NODE))
3393
15.4M
    DICT_FREE(cur->name)
3394
28.2M
      xmlFree(cur);
3395
28.2M
  }
3396
3397
28.2M
        if (next != NULL) {
3398
16.0M
      cur = next;
3399
16.0M
        } else {
3400
12.2M
            if ((depth == 0) || (parent == NULL))
3401
4.36M
                break;
3402
7.87M
            depth -= 1;
3403
7.87M
            cur = parent;
3404
7.87M
            cur->children = NULL;
3405
7.87M
        }
3406
28.2M
    }
3407
4.36M
}
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
1.68M
xmlFreeNode(xmlNode *cur) {
3419
1.68M
    xmlDictPtr dict = NULL;
3420
3421
1.68M
    if (cur == NULL) return;
3422
3423
    /* use xmlFreeDtd for DTD nodes */
3424
1.68M
    if (cur->type == XML_DTD_NODE) {
3425
18.6k
  xmlFreeDtd((xmlDtdPtr) cur);
3426
18.6k
  return;
3427
18.6k
    }
3428
1.66M
    if (cur->type == XML_NAMESPACE_DECL) {
3429
0
  xmlFreeNs((xmlNsPtr) cur);
3430
0
        return;
3431
0
    }
3432
1.66M
    if (cur->type == XML_ATTRIBUTE_NODE) {
3433
7.92k
  xmlFreeProp((xmlAttrPtr) cur);
3434
7.92k
  return;
3435
7.92k
    }
3436
1.65M
    if (cur->type == XML_ENTITY_DECL) {
3437
0
        xmlFreeEntity((xmlEntityPtr) cur);
3438
0
        return;
3439
0
    }
3440
3441
1.65M
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3442
0
  xmlDeregisterNodeDefaultValue(cur);
3443
3444
1.65M
    if (cur->doc != NULL) dict = cur->doc->dict;
3445
3446
1.65M
    if ((cur->children != NULL) &&
3447
31.5k
  (cur->type != XML_ENTITY_REF_NODE))
3448
29.8k
  xmlFreeNodeList(cur->children);
3449
3450
1.65M
    if ((cur->type == XML_ELEMENT_NODE) ||
3451
1.53M
        (cur->type == XML_XINCLUDE_START) ||
3452
1.53M
        (cur->type == XML_XINCLUDE_END)) {
3453
124k
        if (cur->properties != NULL)
3454
20.9k
            xmlFreePropList(cur->properties);
3455
124k
        if (cur->nsDef != NULL)
3456
23.4k
            xmlFreeNsList(cur->nsDef);
3457
1.52M
    } else if ((cur->content != NULL) &&
3458
1.47M
               (cur->type != XML_ENTITY_REF_NODE) &&
3459
1.47M
               (cur->content != (xmlChar *) &(cur->properties))) {
3460
1.47M
        DICT_FREE(cur->content)
3461
1.47M
    }
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
1.65M
    if ((cur->name != NULL) &&
3469
1.64M
        (cur->type != XML_TEXT_NODE) &&
3470
1.03M
        (cur->type != XML_COMMENT_NODE))
3471
209k
  DICT_FREE(cur->name)
3472
3473
1.65M
    xmlFree(cur);
3474
1.65M
}
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
2.48M
xmlUnlinkNodeInternal(xmlNodePtr cur) {
3486
2.48M
    if (cur->parent != NULL) {
3487
2.24M
  xmlNodePtr parent;
3488
2.24M
  parent = cur->parent;
3489
2.24M
  if (cur->type == XML_ATTRIBUTE_NODE) {
3490
17.3k
      if (parent->properties == (xmlAttrPtr) cur)
3491
14.1k
    parent->properties = ((xmlAttrPtr) cur)->next;
3492
2.22M
  } else {
3493
2.22M
      if (parent->children == cur)
3494
718k
    parent->children = cur->next;
3495
2.22M
      if (parent->last == cur)
3496
430k
    parent->last = cur->prev;
3497
2.22M
  }
3498
2.24M
  cur->parent = NULL;
3499
2.24M
    }
3500
3501
2.48M
    if (cur->next != NULL)
3502
1.79M
        cur->next->prev = cur->prev;
3503
2.48M
    if (cur->prev != NULL)
3504
1.50M
        cur->prev->next = cur->next;
3505
2.48M
    cur->next = NULL;
3506
2.48M
    cur->prev = NULL;
3507
2.48M
}
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
1.25M
xmlUnlinkNode(xmlNode *cur) {
3519
1.25M
    if (cur == NULL)
3520
1.07k
  return;
3521
3522
1.24M
    if (cur->type == XML_NAMESPACE_DECL)
3523
0
        return;
3524
3525
1.24M
    if (cur->type == XML_DTD_NODE) {
3526
18.7k
  xmlDocPtr doc = cur->doc;
3527
3528
18.7k
  if (doc != NULL) {
3529
18.5k
      if (doc->intSubset == (xmlDtdPtr) cur)
3530
17.0k
    doc->intSubset = NULL;
3531
18.5k
      if (doc->extSubset == (xmlDtdPtr) cur)
3532
1.47k
    doc->extSubset = NULL;
3533
18.5k
  }
3534
18.7k
    }
3535
3536
1.24M
    if (cur->type == XML_ENTITY_DECL)
3537
0
        xmlRemoveEntity((xmlEntityPtr) cur);
3538
3539
1.24M
    xmlUnlinkNodeInternal(cur);
3540
1.24M
}
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
6.38k
xmlReplaceNode(xmlNode *old, xmlNode *cur) {
3560
6.38k
    if (old == cur) return(NULL);
3561
5.48k
    if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3562
4.51k
        (old->parent == NULL)) {
3563
1.44k
  return(NULL);
3564
1.44k
    }
3565
4.03k
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3566
        /* Don't call xmlUnlinkNodeInternal to handle DTDs. */
3567
365
  xmlUnlinkNode(old);
3568
365
  return(old);
3569
365
    }
3570
3.67k
    if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3571
203
  return(old);
3572
203
    }
3573
3.47k
    if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3574
197
  return(old);
3575
197
    }
3576
3.27k
    xmlUnlinkNodeInternal(cur);
3577
3.27k
    if (xmlSetTreeDoc(cur, old->doc) < 0)
3578
1
        return(NULL);
3579
3.27k
    cur->parent = old->parent;
3580
3.27k
    cur->next = old->next;
3581
3.27k
    if (cur->next != NULL)
3582
206
  cur->next->prev = cur;
3583
3.27k
    cur->prev = old->prev;
3584
3.27k
    if (cur->prev != NULL)
3585
2.30k
  cur->prev->next = cur;
3586
3.27k
    if (cur->parent != NULL) {
3587
3.27k
  if (cur->type == XML_ATTRIBUTE_NODE) {
3588
172
      if (cur->parent->properties == (xmlAttrPtr)old)
3589
75
    cur->parent->properties = ((xmlAttrPtr) cur);
3590
3.10k
  } else {
3591
3.10k
      if (cur->parent->children == old)
3592
892
    cur->parent->children = cur;
3593
3.10k
      if (cur->parent->last == old)
3594
2.89k
    cur->parent->last = cur;
3595
3.10k
  }
3596
3.27k
    }
3597
3.27k
    old->next = old->prev = NULL;
3598
3.27k
    old->parent = NULL;
3599
3.27k
    return(old);
3600
3.27k
}
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
12.4M
xmlCopyNamespace(xmlNs *cur) {
3617
12.4M
    xmlNsPtr ret;
3618
3619
12.4M
    if (cur == NULL) return(NULL);
3620
12.4M
    switch (cur->type) {
3621
12.4M
  case XML_LOCAL_NAMESPACE:
3622
12.4M
      ret = xmlNewNs(NULL, cur->href, cur->prefix);
3623
12.4M
      break;
3624
0
  default:
3625
0
      return(NULL);
3626
12.4M
    }
3627
12.4M
    return(ret);
3628
12.4M
}
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
6.03M
xmlCopyNamespaceList(xmlNs *cur) {
3639
6.03M
    xmlNsPtr ret = NULL;
3640
6.03M
    xmlNsPtr p = NULL,q;
3641
3642
18.4M
    while (cur != NULL) {
3643
12.4M
        q = xmlCopyNamespace(cur);
3644
12.4M
        if (q == NULL) {
3645
312
            xmlFreeNsList(ret);
3646
312
            return(NULL);
3647
312
        }
3648
12.4M
  if (p == NULL) {
3649
6.03M
      ret = p = q;
3650
6.39M
  } else {
3651
6.39M
      p->next = q;
3652
6.39M
      p = q;
3653
6.39M
  }
3654
12.4M
  cur = cur->next;
3655
12.4M
    }
3656
6.03M
    return(ret);
3657
6.03M
}
3658
3659
static xmlAttrPtr
3660
2.27M
xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3661
2.27M
    xmlAttrPtr ret = NULL;
3662
3663
2.27M
    if (cur == NULL) return(NULL);
3664
2.27M
    if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3665
212
        return(NULL);
3666
2.27M
    if (target != NULL)
3667
2.27M
  ret = xmlNewDocProp(target->doc, cur->name, NULL);
3668
2.40k
    else if (doc != NULL)
3669
326
  ret = xmlNewDocProp(doc, cur->name, NULL);
3670
2.08k
    else if (cur->parent != NULL)
3671
877
  ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3672
1.20k
    else if (cur->children != NULL)
3673
575
  ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3674
630
    else
3675
630
  ret = xmlNewDocProp(NULL, cur->name, NULL);
3676
2.27M
    if (ret == NULL) return(NULL);
3677
2.26M
    ret->parent = target;
3678
3679
2.26M
    if ((cur->ns != NULL) && (target != NULL)) {
3680
281k
      xmlNsPtr ns;
3681
281k
      int res;
3682
3683
281k
      res = xmlSearchNsSafe(target, cur->ns->prefix, &ns);
3684
281k
      if (res < 0)
3685
4
          goto error;
3686
281k
      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
2.50k
        res = xmlSearchNsSafe(cur->parent, cur->ns->prefix, &ns);
3693
2.50k
        if (res < 0)
3694
0
          goto error;
3695
2.50k
        if (ns != NULL) {
3696
1.91k
          xmlNodePtr root = target;
3697
1.91k
          xmlNodePtr pred = NULL;
3698
3699
3.31k
          while (root->parent != NULL) {
3700
1.40k
            pred = root;
3701
1.40k
            root = root->parent;
3702
1.40k
          }
3703
1.91k
          if (root == (xmlNodePtr) target->doc) {
3704
            /* correct possibly cycling above the document elt */
3705
39
            root = pred;
3706
39
          }
3707
1.91k
          ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3708
1.91k
          if (ret->ns == NULL)
3709
423
              goto error;
3710
1.91k
        }
3711
279k
      } 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
279k
        if (xmlStrEqual(ns->href, cur->ns->href)) {
3718
          /* this is the nice case */
3719
275k
          ret->ns = ns;
3720
275k
        } else {
3721
          /*
3722
           * we are in trouble: we need a new reconciled namespace.
3723
           * This is expensive
3724
           */
3725
4.10k
          ret->ns = xmlNewReconciledNs(target, cur->ns);
3726
4.10k
          if (ret->ns == NULL)
3727
8
              goto error;
3728
4.10k
        }
3729
279k
      }
3730
3731
281k
    } else
3732
1.98M
        ret->ns = NULL;
3733
3734
2.26M
    if (cur->children != NULL) {
3735
2.09M
  xmlNodePtr tmp;
3736
3737
2.09M
  ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3738
2.09M
        if (ret->children == NULL)
3739
318
            goto error;
3740
2.09M
  ret->last = NULL;
3741
2.09M
  tmp = ret->children;
3742
4.24M
  while (tmp != NULL) {
3743
      /* tmp->parent = (xmlNodePtr)ret; */
3744
2.14M
      if (tmp->next == NULL)
3745
2.09M
          ret->last = tmp;
3746
2.14M
      tmp = tmp->next;
3747
2.14M
  }
3748
2.09M
    }
3749
    /*
3750
     * Try to handle IDs
3751
     */
3752
2.26M
    if ((target != NULL) && (cur != NULL) &&
3753
2.26M
  (target->doc != NULL) && (cur->doc != NULL) &&
3754
2.23M
        (cur->parent != NULL) &&
3755
2.23M
        (cur->children != NULL)) {
3756
2.07M
        int res = xmlIsID(cur->doc, cur->parent, cur);
3757
3758
2.07M
        if (res < 0)
3759
17
            goto error;
3760
2.07M
  if (res != 0) {
3761
54.1k
      xmlChar *id;
3762
3763
54.1k
      id = xmlNodeGetContent((xmlNodePtr) cur);
3764
54.1k
      if (id == NULL)
3765
43
                goto error;
3766
54.0k
            res = xmlAddIDSafe(ret, id);
3767
54.0k
      xmlFree(id);
3768
54.0k
            if (res < 0)
3769
103
                goto error;
3770
54.0k
  }
3771
2.07M
    }
3772
2.26M
    return(ret);
3773
3774
916
error:
3775
916
    xmlFreeProp(ret);
3776
916
    return(NULL);
3777
2.26M
}
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
2.27M
xmlCopyProp(xmlNode *target, xmlAttr *cur) {
3793
2.27M
  return xmlCopyPropInternal(NULL, target, cur);
3794
2.27M
}
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.04M
xmlCopyPropList(xmlNode *target, xmlAttr *cur) {
3808
1.04M
    xmlAttrPtr ret = NULL;
3809
1.04M
    xmlAttrPtr p = NULL,q;
3810
3811
1.04M
    if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3812
596
        return(NULL);
3813
3.31M
    while (cur != NULL) {
3814
2.27M
        q = xmlCopyProp(target, cur);
3815
2.27M
  if (q == NULL) {
3816
7.12k
            xmlFreePropList(ret);
3817
7.12k
      return(NULL);
3818
7.12k
        }
3819
2.26M
  if (p == NULL) {
3820
1.04M
      ret = p = q;
3821
1.22M
  } else {
3822
1.22M
      p->next = q;
3823
1.22M
      q->prev = p;
3824
1.22M
      p = q;
3825
1.22M
  }
3826
2.26M
  cur = cur->next;
3827
2.26M
    }
3828
1.03M
    return(ret);
3829
1.04M
}
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
29.7M
                  int extended) {
3862
29.7M
    xmlNodePtr ret;
3863
3864
29.7M
    if (node == NULL) return(NULL);
3865
29.7M
    switch (node->type) {
3866
9.30M
        case XML_TEXT_NODE:
3867
9.49M
        case XML_CDATA_SECTION_NODE:
3868
23.4M
        case XML_ELEMENT_NODE:
3869
23.4M
        case XML_DOCUMENT_FRAG_NODE:
3870
23.4M
        case XML_ENTITY_REF_NODE:
3871
23.7M
        case XML_PI_NODE:
3872
24.2M
        case XML_COMMENT_NODE:
3873
24.3M
        case XML_XINCLUDE_START:
3874
29.7M
        case XML_XINCLUDE_END:
3875
29.7M
      break;
3876
1.28k
        case XML_ATTRIBUTE_NODE:
3877
1.28k
    return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
3878
0
        case XML_NAMESPACE_DECL:
3879
0
      return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3880
9.32k
        case XML_DTD_NODE:
3881
9.32k
            return((xmlNodePtr) xmlCopyDtd((xmlDtdPtr) node));
3882
4.88k
        case XML_DOCUMENT_NODE:
3883
9.05k
        case XML_HTML_DOCUMENT_NODE:
3884
9.05k
      return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
3885
678
        default:
3886
678
            return(NULL);
3887
29.7M
    }
3888
3889
    /*
3890
     * Allocate a new node and fill the fields.
3891
     */
3892
29.7M
    ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3893
29.7M
    if (ret == NULL)
3894
1.03k
  return(NULL);
3895
29.7M
    memset(ret, 0, sizeof(xmlNode));
3896
29.7M
    ret->type = node->type;
3897
3898
29.7M
    ret->doc = doc;
3899
29.7M
    ret->parent = parent;
3900
29.7M
    if (node->name == xmlStringText)
3901
9.30M
  ret->name = xmlStringText;
3902
20.4M
    else if (node->name == xmlStringTextNoenc)
3903
0
  ret->name = xmlStringTextNoenc;
3904
20.4M
    else if (node->name == xmlStringComment)
3905
427k
  ret->name = xmlStringComment;
3906
19.9M
    else if (node->name != NULL) {
3907
19.7M
        if ((doc != NULL) && (doc->dict != NULL))
3908
17.2M
      ret->name = xmlDictLookup(doc->dict, node->name, -1);
3909
2.51M
  else
3910
2.51M
      ret->name = xmlStrdup(node->name);
3911
19.7M
        if (ret->name == NULL)
3912
424
            goto error;
3913
19.7M
    }
3914
29.7M
    if ((node->type != XML_ELEMENT_NODE) &&
3915
15.7M
  (node->content != NULL) &&
3916
9.97M
  (node->type != XML_ENTITY_REF_NODE) &&
3917
9.92M
  (node->type != XML_XINCLUDE_END) &&
3918
9.92M
  (node->type != XML_XINCLUDE_START)) {
3919
9.92M
  ret->content = xmlStrdup(node->content);
3920
9.92M
        if (ret->content == NULL)
3921
502
            goto error;
3922
19.7M
    }else{
3923
19.7M
      if (node->type == XML_ELEMENT_NODE)
3924
13.9M
        ret->line = node->line;
3925
19.7M
    }
3926
3927
29.7M
    if (!extended)
3928
13.8k
  goto out;
3929
29.6M
    if (((node->type == XML_ELEMENT_NODE) ||
3930
15.7M
         (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) {
3931
6.03M
        ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3932
6.03M
        if (ret->nsDef == NULL)
3933
278
            goto error;
3934
6.03M
    }
3935
3936
29.6M
    if ((node->type == XML_ELEMENT_NODE) && (node->ns != NULL)) {
3937
1.58M
        xmlNsPtr ns = NULL;
3938
1.58M
        int res;
3939
3940
1.58M
  res = xmlSearchNsSafe(ret, node->ns->prefix, &ns);
3941
1.58M
        if (res < 0)
3942
2
            goto error;
3943
1.58M
  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
6.47k
      res = xmlSearchNsSafe(node, node->ns->prefix, &ns);
3953
6.47k
            if (res < 0)
3954
0
                goto error;
3955
6.47k
      if (ns != NULL) {
3956
4.08k
          xmlNodePtr root = ret;
3957
3958
10.1k
    while (root->parent != NULL) root = root->parent;
3959
4.08k
    ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3960
4.08k
            } else {
3961
2.38k
                ret->ns = xmlNewReconciledNs(ret, node->ns);
3962
2.38k
      }
3963
6.47k
            if (ret->ns == NULL)
3964
273
                goto error;
3965
1.58M
  } else {
3966
      /*
3967
       * reference the existing namespace definition in our own tree.
3968
       */
3969
1.58M
      ret->ns = ns;
3970
1.58M
  }
3971
1.58M
    }
3972
29.6M
    if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) {
3973
1.04M
        ret->properties = xmlCopyPropList(ret, node->properties);
3974
1.04M
        if (ret->properties == NULL)
3975
6.63k
            goto error;
3976
1.04M
    }
3977
29.6M
    if (node->type == XML_ENTITY_REF_NODE) {
3978
84.1k
  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
71.9k
      ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3986
71.9k
  } else {
3987
12.1k
            ret->children = node->children;
3988
12.1k
  }
3989
84.1k
  ret->last = ret->children;
3990
29.6M
    } else if ((node->children != NULL) && (extended != 2)) {
3991
60.0k
        xmlNodePtr cur, insert;
3992
3993
60.0k
        cur = node->children;
3994
60.0k
        insert = ret;
3995
25.2M
        while (cur != NULL) {
3996
25.1M
            xmlNodePtr copy = xmlStaticCopyNode(cur, doc, insert, 2);
3997
25.1M
            if (copy == NULL)
3998
6.97k
                goto error;
3999
4000
            /* Check for coalesced text nodes */
4001
25.1M
            if (insert->last != copy) {
4002
25.1M
                if (insert->last == NULL) {
4003
11.5M
                    insert->children = copy;
4004
13.6M
                } else {
4005
13.6M
                    copy->prev = insert->last;
4006
13.6M
                    insert->last->next = copy;
4007
13.6M
                }
4008
25.1M
                insert->last = copy;
4009
25.1M
            }
4010
4011
25.1M
            if ((cur->type != XML_ENTITY_REF_NODE) &&
4012
25.1M
                (cur->children != NULL)) {
4013
11.4M
                cur = cur->children;
4014
11.4M
                insert = copy;
4015
11.4M
                continue;
4016
11.4M
            }
4017
4018
24.9M
            while (1) {
4019
24.9M
                if (cur->next != NULL) {
4020
13.6M
                    cur = cur->next;
4021
13.6M
                    break;
4022
13.6M
                }
4023
4024
11.2M
                cur = cur->parent;
4025
11.2M
                insert = insert->parent;
4026
11.2M
                if (cur == node) {
4027
53.1k
                    cur = NULL;
4028
53.1k
                    break;
4029
53.1k
                }
4030
11.2M
            }
4031
13.6M
        }
4032
60.0k
    }
4033
4034
29.6M
out:
4035
29.6M
    if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4036
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4037
29.6M
    return(ret);
4038
4039
15.0k
error:
4040
15.0k
    xmlFreeNode(ret);
4041
15.0k
    return(NULL);
4042
29.6M
}
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
2.15M
xmlStaticCopyNodeList(xmlNode *node, xmlDoc *doc, xmlNode *parent) {
4056
2.15M
    xmlNodePtr ret = NULL;
4057
2.15M
    xmlNodePtr p = NULL,q;
4058
2.15M
    xmlDtdPtr newSubset = NULL;
4059
2.15M
    int linkedSubset = 0;
4060
4061
4.45M
    while (node != NULL) {
4062
2.30M
        xmlNodePtr next = node->next;
4063
4064
2.30M
  if (node->type == XML_DTD_NODE ) {
4065
23.2k
      if (doc == NULL) {
4066
528
    node = next;
4067
528
    continue;
4068
528
      }
4069
22.6k
      if ((doc->intSubset == NULL) && (newSubset == NULL)) {
4070
493
    q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4071
493
    if (q == NULL) goto error;
4072
                /* Can't fail on DTD */
4073
492
    xmlSetTreeDoc(q, doc);
4074
492
    q->parent = parent;
4075
492
    newSubset = (xmlDtdPtr) q;
4076
22.1k
      } 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
22.1k
                linkedSubset = 1;
4082
22.1k
    q = (xmlNodePtr) doc->intSubset;
4083
                /* Unlink */
4084
22.1k
                if (q->prev == NULL) {
4085
22.1k
                    if (q->parent != NULL)
4086
542
                        q->parent->children = q->next;
4087
22.1k
                } else {
4088
66
                    q->prev->next = q->next;
4089
66
                }
4090
22.1k
                if (q->next == NULL) {
4091
21.8k
                    if (q->parent != NULL)
4092
302
                        q->parent->last = q->prev;
4093
21.8k
                } else {
4094
306
                    q->next->prev = q->prev;
4095
306
                }
4096
22.1k
                q->parent = parent;
4097
22.1k
                q->next = NULL;
4098
22.1k
                q->prev = NULL;
4099
22.1k
      }
4100
22.6k
  } else
4101
2.27M
      q = xmlStaticCopyNode(node, doc, parent, 1);
4102
2.30M
  if (q == NULL) goto error;
4103
2.29M
  if (ret == NULL) {
4104
2.14M
      q->prev = NULL;
4105
2.14M
      ret = p = q;
4106
2.14M
  } else if (p != q) {
4107
  /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4108
149k
      p->next = q;
4109
149k
      q->prev = p;
4110
149k
      p = q;
4111
149k
  }
4112
2.29M
  node = next;
4113
2.29M
    }
4114
2.14M
    if ((doc != NULL) && (newSubset != NULL))
4115
291
        doc->intSubset = newSubset;
4116
2.14M
    return(ret);
4117
4.35k
error:
4118
4.35k
    xmlFreeNodeList(ret);
4119
4.35k
    if (newSubset != NULL)
4120
201
        xmlFreeDtd(newSubset);
4121
4.35k
    if (linkedSubset != 0) {
4122
1.34k
        doc->intSubset->next = NULL;
4123
1.34k
        doc->intSubset->prev = NULL;
4124
1.34k
    }
4125
4.35k
    return(NULL);
4126
2.15M
}
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
17.5k
xmlCopyNode(xmlNode *node, int extended) {
4147
17.5k
    xmlNodePtr ret;
4148
4149
17.5k
    ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4150
17.5k
    return(ret);
4151
17.5k
}
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
606k
xmlDocCopyNode(xmlNode *node, xmlDoc *doc, int extended) {
4171
606k
    xmlNodePtr ret;
4172
4173
606k
    ret = xmlStaticCopyNode(node, doc, NULL, extended);
4174
606k
    return(ret);
4175
606k
}
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
8.19k
xmlNode *xmlDocCopyNodeList(xmlDoc *doc, xmlNode *node) {
4186
8.19k
    xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4187
8.19k
    return(ret);
4188
8.19k
}
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
7.43k
xmlNode *xmlCopyNodeList(xmlNode *node) {
4200
7.43k
    xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4201
7.43k
    return(ret);
4202
7.43k
}
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
37.5k
xmlCopyDtd(xmlDtd *dtd) {
4212
37.5k
    xmlDtdPtr ret;
4213
37.5k
    xmlNodePtr cur, p = NULL, q;
4214
4215
37.5k
    if (dtd == NULL) return(NULL);
4216
36.2k
    ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4217
36.2k
    if (ret == NULL) return(NULL);
4218
36.0k
    if (dtd->entities != NULL) {
4219
8.48k
        ret->entities = (void *) xmlCopyEntitiesTable(
4220
8.48k
                      (xmlEntitiesTablePtr) dtd->entities);
4221
8.48k
        if (ret->entities == NULL)
4222
150
            goto error;
4223
8.48k
    }
4224
35.9k
    if (dtd->notations != NULL) {
4225
2.05k
        ret->notations = (void *) xmlCopyNotationTable(
4226
2.05k
                      (xmlNotationTablePtr) dtd->notations);
4227
2.05k
        if (ret->notations == NULL)
4228
34
            goto error;
4229
2.05k
    }
4230
35.8k
    if (dtd->elements != NULL) {
4231
12.0k
        ret->elements = (void *) xmlCopyElementTable(
4232
12.0k
                      (xmlElementTablePtr) dtd->elements);
4233
12.0k
        if (ret->elements == NULL)
4234
242
            goto error;
4235
12.0k
    }
4236
35.6k
    if (dtd->attributes != NULL) {
4237
7.91k
        ret->attributes = (void *) xmlCopyAttributeTable(
4238
7.91k
                      (xmlAttributeTablePtr) dtd->attributes);
4239
7.91k
        if (ret->attributes == NULL)
4240
210
            goto error;
4241
7.91k
    }
4242
35.4k
    if (dtd->pentities != NULL) {
4243
3.21k
  ret->pentities = (void *) xmlCopyEntitiesTable(
4244
3.21k
          (xmlEntitiesTablePtr) dtd->pentities);
4245
3.21k
        if (ret->pentities == NULL)
4246
74
            goto error;
4247
3.21k
    }
4248
4249
35.3k
    cur = dtd->children;
4250
104k
    while (cur != NULL) {
4251
69.1k
  q = NULL;
4252
4253
69.1k
  if (cur->type == XML_ENTITY_DECL) {
4254
18.2k
      xmlEntityPtr tmp = (xmlEntityPtr) cur;
4255
18.2k
      switch (tmp->etype) {
4256
7.47k
    case XML_INTERNAL_GENERAL_ENTITY:
4257
11.9k
    case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4258
12.7k
    case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4259
12.7k
        q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4260
12.7k
        break;
4261
2.19k
    case XML_INTERNAL_PARAMETER_ENTITY:
4262
5.49k
    case XML_EXTERNAL_PARAMETER_ENTITY:
4263
5.49k
        q = (xmlNodePtr)
4264
5.49k
      xmlGetParameterEntityFromDtd(ret, tmp->name);
4265
5.49k
        break;
4266
0
    case XML_INTERNAL_PREDEFINED_ENTITY:
4267
0
        break;
4268
18.2k
      }
4269
50.9k
  } else if (cur->type == XML_ELEMENT_DECL) {
4270
10.2k
      xmlElementPtr tmp = (xmlElementPtr) cur;
4271
10.2k
      q = (xmlNodePtr)
4272
10.2k
    xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4273
40.6k
  } else if (cur->type == XML_ATTRIBUTE_DECL) {
4274
22.9k
      xmlAttributePtr tmp = (xmlAttributePtr) cur;
4275
22.9k
      q = (xmlNodePtr)
4276
22.9k
    xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4277
22.9k
  } else if (cur->type == XML_COMMENT_NODE) {
4278
13.0k
      q = xmlCopyNode(cur, 0);
4279
13.0k
            if (q == NULL)
4280
10
                goto error;
4281
13.0k
  }
4282
4283
69.1k
  if (q == NULL) {
4284
4.67k
      cur = cur->next;
4285
4.67k
      continue;
4286
4.67k
  }
4287
4288
64.5k
  if (p == NULL)
4289
19.8k
      ret->children = q;
4290
44.6k
  else
4291
44.6k
      p->next = q;
4292
4293
64.5k
  q->prev = p;
4294
64.5k
  q->parent = (xmlNodePtr) ret;
4295
64.5k
  q->next = NULL;
4296
64.5k
  ret->last = q;
4297
64.5k
  p = q;
4298
64.5k
  cur = cur->next;
4299
64.5k
    }
4300
4301
35.3k
    return(ret);
4302
4303
720
error:
4304
720
    xmlFreeDtd(ret);
4305
720
    return(NULL);
4306
35.3k
}
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
50.3k
xmlCopyDoc(xmlDoc *doc, int recursive) {
4319
50.3k
    xmlDocPtr ret;
4320
4321
50.3k
    if (doc == NULL) return(NULL);
4322
41.3k
    ret = xmlNewDoc(doc->version);
4323
41.3k
    if (ret == NULL) return(NULL);
4324
41.0k
    ret->type = doc->type;
4325
41.0k
    if (doc->name != NULL) {
4326
0
        ret->name = xmlMemStrdup(doc->name);
4327
0
        if (ret->name == NULL)
4328
0
            goto error;
4329
0
    }
4330
41.0k
    if (doc->encoding != NULL) {
4331
5.35k
        ret->encoding = xmlStrdup(doc->encoding);
4332
5.35k
        if (ret->encoding == NULL)
4333
17
            goto error;
4334
5.35k
    }
4335
41.0k
    if (doc->URL != NULL) {
4336
23.1k
        ret->URL = xmlStrdup(doc->URL);
4337
23.1k
        if (ret->URL == NULL)
4338
58
            goto error;
4339
23.1k
    }
4340
41.0k
    ret->charset = doc->charset;
4341
41.0k
    ret->compression = doc->compression;
4342
41.0k
    ret->standalone = doc->standalone;
4343
41.0k
    if (!recursive) return(ret);
4344
4345
39.0k
    ret->last = NULL;
4346
39.0k
    ret->children = NULL;
4347
39.0k
    if (doc->intSubset != NULL) {
4348
22.7k
        ret->intSubset = xmlCopyDtd(doc->intSubset);
4349
22.7k
  if (ret->intSubset == NULL)
4350
579
            goto error;
4351
        /* Can't fail on DTD */
4352
22.1k
  xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4353
22.1k
    }
4354
38.4k
    if (doc->oldNs != NULL) {
4355
3.71k
        ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4356
3.71k
        if (ret->oldNs == NULL)
4357
15
            goto error;
4358
3.71k
    }
4359
38.4k
    if (doc->children != NULL) {
4360
35.9k
  xmlNodePtr tmp;
4361
4362
35.9k
  ret->children = xmlStaticCopyNodeList(doc->children, ret,
4363
35.9k
                                   (xmlNodePtr)ret);
4364
35.9k
        if (ret->children == NULL)
4365
2.18k
            goto error;
4366
33.7k
  ret->last = NULL;
4367
33.7k
  tmp = ret->children;
4368
148k
  while (tmp != NULL) {
4369
114k
      if (tmp->next == NULL)
4370
33.7k
          ret->last = tmp;
4371
114k
      tmp = tmp->next;
4372
114k
  }
4373
33.7k
    }
4374
36.2k
    return(ret);
4375
4376
2.85k
error:
4377
2.85k
    xmlFreeDoc(ret);
4378
2.85k
    return(NULL);
4379
38.4k
}
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
584k
{
4399
584k
    long result = -1;
4400
4401
584k
    if (depth >= 5)
4402
21.2k
        return(-1);
4403
4404
563k
    if (!node)
4405
680
        return result;
4406
563k
    if ((node->type == XML_ELEMENT_NODE) ||
4407
57.1k
        (node->type == XML_TEXT_NODE) ||
4408
27.9k
  (node->type == XML_COMMENT_NODE) ||
4409
539k
  (node->type == XML_PI_NODE)) {
4410
539k
  if (node->line == 65535) {
4411
132k
      if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4412
6.49k
          result = XML_PTR_TO_INT(node->psvi);
4413
126k
      else if ((node->type == XML_ELEMENT_NODE) &&
4414
100k
               (node->children != NULL))
4415
24.3k
          result = xmlGetLineNoInternal(node->children, depth + 1);
4416
102k
      else if (node->next != NULL)
4417
42.5k
          result = xmlGetLineNoInternal(node->next, depth + 1);
4418
59.4k
      else if (node->prev != NULL)
4419
43.1k
          result = xmlGetLineNoInternal(node->prev, depth + 1);
4420
132k
  }
4421
539k
  if ((result == -1) || (result == 65535))
4422
524k
      result = node->line;
4423
539k
    } else if ((node->prev != NULL) &&
4424
7.31k
             ((node->prev->type == XML_ELEMENT_NODE) ||
4425
5.76k
        (node->prev->type == XML_TEXT_NODE) ||
4426
2.96k
        (node->prev->type == XML_COMMENT_NODE) ||
4427
1.95k
        (node->prev->type == XML_PI_NODE)))
4428
6.17k
        result = xmlGetLineNoInternal(node->prev, depth + 1);
4429
17.0k
    else if ((node->parent != NULL) &&
4430
5.24k
             (node->parent->type == XML_ELEMENT_NODE))
4431
4.71k
        result = xmlGetLineNoInternal(node->parent, depth + 1);
4432
4433
563k
    return result;
4434
563k
}
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
464k
{
4447
464k
    return(xmlGetLineNoInternal(node, 0));
4448
464k
}
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
8.71k
{
4460
8.71k
    const xmlNode *cur, *tmp;
4461
8.71k
    const xmlNode **nodes = NULL;
4462
8.71k
    xmlChar *ret = NULL;
4463
8.71k
    xmlBuf *buf;
4464
8.71k
    size_t numNodes, i;
4465
4466
8.71k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4467
396
        return(NULL);
4468
4469
8.32k
    buf = xmlBufCreate(50);
4470
8.32k
    if (buf == NULL)
4471
2
        return(NULL);
4472
4473
    /*
4474
     * Get list of ancestors
4475
     */
4476
8.32k
    numNodes = 0;
4477
36.5k
    for (cur = node; cur != NULL; cur = cur->parent)
4478
28.2k
        numNodes += 1;
4479
8.32k
    if (numNodes > SIZE_MAX / sizeof(nodes[0]))
4480
0
        goto error;
4481
8.32k
    nodes = xmlMalloc(numNodes * sizeof(nodes[0]));
4482
8.32k
    if (nodes == NULL)
4483
3
        goto error;
4484
8.31k
    i = 0;
4485
36.5k
    for (cur = node; cur != NULL && i < numNodes; cur = cur->parent)
4486
28.2k
        nodes[i++] = cur;
4487
4488
    /*
4489
     * Iterate in reverse to start at root
4490
     */
4491
36.1k
    while (i > 0) {
4492
28.1k
        int occur = 0;
4493
4494
28.1k
        i -= 1;
4495
28.1k
        cur = nodes[i];
4496
4497
28.1k
        if ((cur->type == XML_DOCUMENT_NODE) ||
4498
26.2k
            (cur->type == XML_HTML_DOCUMENT_NODE)) {
4499
5.75k
            if (i == 0)
4500
823
                xmlBufCat(buf, BAD_CAST "/");
4501
22.4k
        } else if (cur->type == XML_ELEMENT_NODE) {
4502
17.4k
            int generic = 0;
4503
4504
17.4k
            xmlBufCat(buf, BAD_CAST "/");
4505
4506
17.4k
            if (cur->ns) {
4507
6.71k
    if (cur->ns->prefix != NULL) {
4508
3.46k
                    xmlBufCat(buf, cur->ns->prefix);
4509
3.46k
                    xmlBufCat(buf, BAD_CAST ":");
4510
3.46k
                    xmlBufCat(buf, cur->name);
4511
3.46k
    } else {
4512
        /*
4513
        * We cannot express named elements in the default
4514
        * namespace, so use "*".
4515
        */
4516
3.24k
        generic = 1;
4517
3.24k
                    xmlBufCat(buf, BAD_CAST "*");
4518
3.24k
    }
4519
10.7k
            } else {
4520
10.7k
                xmlBufCat(buf, cur->name);
4521
10.7k
            }
4522
4523
            /*
4524
             * Thumbler index computation
4525
       * TODO: the occurrence test seems bogus for namespaced names
4526
             */
4527
17.4k
            tmp = cur->prev;
4528
27.5k
            while (tmp != NULL) {
4529
10.1k
                if ((tmp->type == XML_ELEMENT_NODE) &&
4530
3.05k
        (generic ||
4531
2.75k
         (xmlStrEqual(cur->name, tmp->name) &&
4532
2.20k
         ((tmp->ns == cur->ns) ||
4533
1.33k
          ((tmp->ns != NULL) && (cur->ns != NULL) &&
4534
882
           (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4535
1.61k
                    occur++;
4536
10.1k
                tmp = tmp->prev;
4537
10.1k
            }
4538
17.4k
            if (occur == 0) {
4539
16.3k
                tmp = cur->next;
4540
18.8k
                while (tmp != NULL && occur == 0) {
4541
2.40k
                    if ((tmp->type == XML_ELEMENT_NODE) &&
4542
2.16k
      (generic ||
4543
1.96k
       (xmlStrEqual(cur->name, tmp->name) &&
4544
1.53k
       ((tmp->ns == cur->ns) ||
4545
927
        ((tmp->ns != NULL) && (cur->ns != NULL) &&
4546
444
         (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4547
1.04k
                        occur++;
4548
2.40k
                    tmp = tmp->next;
4549
2.40k
                }
4550
16.3k
                if (occur != 0)
4551
1.04k
                    occur = 1;
4552
16.3k
            } else
4553
1.05k
                occur++;
4554
17.4k
        } else if (cur->type == XML_COMMENT_NODE) {
4555
1.15k
            xmlBufCat(buf, BAD_CAST "/comment()");
4556
4557
            /*
4558
             * Thumbler index computation
4559
             */
4560
1.15k
            tmp = cur->prev;
4561
2.47k
            while (tmp != NULL) {
4562
1.31k
                if (tmp->type == XML_COMMENT_NODE)
4563
322
        occur++;
4564
1.31k
                tmp = tmp->prev;
4565
1.31k
            }
4566
1.15k
            if (occur == 0) {
4567
924
                tmp = cur->next;
4568
1.94k
                while (tmp != NULL && occur == 0) {
4569
1.02k
        if (tmp->type == XML_COMMENT_NODE)
4570
325
            occur++;
4571
1.02k
                    tmp = tmp->next;
4572
1.02k
                }
4573
924
                if (occur != 0)
4574
325
                    occur = 1;
4575
924
            } else
4576
227
                occur++;
4577
3.81k
        } else if ((cur->type == XML_TEXT_NODE) ||
4578
2.88k
                   (cur->type == XML_CDATA_SECTION_NODE)) {
4579
1.13k
            xmlBufCat(buf, BAD_CAST "/text()");
4580
4581
            /*
4582
             * Thumbler index computation
4583
             */
4584
1.13k
            tmp = cur->prev;
4585
2.26k
            while (tmp != NULL) {
4586
1.13k
                if ((tmp->type == XML_TEXT_NODE) ||
4587
598
        (tmp->type == XML_CDATA_SECTION_NODE))
4588
726
        occur++;
4589
1.13k
                tmp = tmp->prev;
4590
1.13k
            }
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
1.13k
            if (occur == 0) {
4596
713
                tmp = cur->next;
4597
1.11k
                while (tmp != NULL) {
4598
645
        if ((tmp->type == XML_TEXT_NODE) ||
4599
399
      (tmp->type == XML_CDATA_SECTION_NODE))
4600
246
        {
4601
246
      occur = 1;
4602
246
      break;
4603
246
        }
4604
399
        tmp = tmp->next;
4605
399
    }
4606
713
            } else
4607
426
                occur++;
4608
2.67k
        } else if (cur->type == XML_PI_NODE) {
4609
1.15k
            xmlBufCat(buf, BAD_CAST "/processing-instruction('");
4610
1.15k
            xmlBufCat(buf, cur->name);
4611
1.15k
            xmlBufCat(buf, BAD_CAST "')");
4612
4613
            /*
4614
             * Thumbler index computation
4615
             */
4616
1.15k
            tmp = cur->prev;
4617
2.14k
            while (tmp != NULL) {
4618
996
                if ((tmp->type == XML_PI_NODE) &&
4619
463
        (xmlStrEqual(cur->name, tmp->name)))
4620
259
                    occur++;
4621
996
                tmp = tmp->prev;
4622
996
            }
4623
1.15k
            if (occur == 0) {
4624
935
                tmp = cur->next;
4625
2.14k
                while (tmp != NULL && occur == 0) {
4626
1.20k
                    if ((tmp->type == XML_PI_NODE) &&
4627
475
      (xmlStrEqual(cur->name, tmp->name)))
4628
267
                        occur++;
4629
1.20k
                    tmp = tmp->next;
4630
1.20k
                }
4631
935
                if (occur != 0)
4632
267
                    occur = 1;
4633
935
            } else
4634
216
                occur++;
4635
4636
1.52k
        } else if (cur->type == XML_ATTRIBUTE_NODE) {
4637
1.21k
            xmlBufCat(buf, BAD_CAST "/@");
4638
1.21k
            if (cur->ns && cur->ns->prefix != NULL) {
4639
355
                xmlBufCat(buf, cur->ns->prefix);
4640
355
                xmlBufCat(buf, BAD_CAST ":");
4641
355
            }
4642
1.21k
            xmlBufCat(buf, cur->name);
4643
1.21k
        } else {
4644
310
            goto error;
4645
310
        }
4646
4647
27.8k
        if (occur > 0) {
4648
3.80k
            char tmpbuf[30];
4649
4650
3.80k
            snprintf(tmpbuf, sizeof(tmpbuf), "[%d]", occur);
4651
3.80k
            xmlBufCat(buf, BAD_CAST tmpbuf);
4652
3.80k
        }
4653
27.8k
    }
4654
4655
8.00k
    ret = xmlBufDetach(buf);
4656
4657
8.32k
error:
4658
8.32k
    xmlBufFree(buf);
4659
8.32k
    xmlFree(nodes);
4660
8.32k
    return(ret);
4661
8.00k
}
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
897k
xmlDocGetRootElement(const xmlDoc *doc) {
4675
897k
    xmlNodePtr ret;
4676
4677
897k
    if (doc == NULL) return(NULL);
4678
897k
    ret = doc->children;
4679
1.20M
    while (ret != NULL) {
4680
1.17M
  if (ret->type == XML_ELEMENT_NODE)
4681
863k
      return(ret);
4682
312k
        ret = ret->next;
4683
312k
    }
4684
33.2k
    return(ret);
4685
897k
}
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
10.6k
xmlDocSetRootElement(xmlDoc *doc, xmlNode *root) {
4703
10.6k
    xmlNodePtr old = NULL;
4704
4705
10.6k
    if (doc == NULL) return(NULL);
4706
9.98k
    if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4707
579
  return(NULL);
4708
9.40k
    old = doc->children;
4709
11.8k
    while (old != NULL) {
4710
5.18k
  if (old->type == XML_ELEMENT_NODE)
4711
2.75k
      break;
4712
2.43k
        old = old->next;
4713
2.43k
    }
4714
9.40k
    if (old == root)
4715
723
        return(old);
4716
8.68k
    xmlUnlinkNodeInternal(root);
4717
8.68k
    if (xmlSetTreeDoc(root, doc) < 0)
4718
1
        return(NULL);
4719
8.68k
    root->parent = (xmlNodePtr) doc;
4720
8.68k
    if (old == NULL) {
4721
6.64k
  if (doc->children == NULL) {
4722
6.40k
      doc->children = root;
4723
6.40k
      doc->last = root;
4724
6.40k
  } else {
4725
246
      xmlAddSibling(doc->children, root);
4726
246
  }
4727
6.64k
    } else {
4728
2.03k
  xmlReplaceNode(old, root);
4729
2.03k
    }
4730
8.68k
    return(old);
4731
8.68k
}
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
6.35k
xmlNodeSetLang(xmlNode *cur, const xmlChar *lang) {
4743
6.35k
    xmlNsPtr ns;
4744
6.35k
    xmlAttrPtr attr;
4745
6.35k
    int res;
4746
4747
6.35k
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
4748
1.23k
        return(1);
4749
4750
5.12k
    res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns);
4751
5.12k
    if (res != 0)
4752
9
        return(res);
4753
5.11k
    attr = xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4754
5.11k
    if (attr == NULL)
4755
8
        return(-1);
4756
4757
5.10k
    return(0);
4758
5.11k
}
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
11.8k
xmlNodeGetLang(const xmlNode *cur) {
4772
11.8k
    xmlChar *lang;
4773
11.8k
    int res;
4774
4775
11.8k
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
4776
491
        return(NULL);
4777
4778
147k
    while (cur != NULL) {
4779
137k
        res = xmlNodeGetAttrValue(cur, BAD_CAST "lang", XML_XML_NAMESPACE,
4780
137k
                                  &lang);
4781
137k
        if (res < 0)
4782
3
            return(NULL);
4783
137k
  if (lang != NULL)
4784
1.61k
      return(lang);
4785
4786
136k
  cur = cur->parent;
4787
136k
    }
4788
4789
9.76k
    return(NULL);
4790
11.3k
}
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
4.33k
xmlNodeSetSpacePreserve(xmlNode *cur, int val) {
4803
4.33k
    xmlNsPtr ns;
4804
4.33k
    xmlAttrPtr attr;
4805
4.33k
    const char *string;
4806
4.33k
    int res;
4807
4808
4.33k
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
4809
1.02k
        return(1);
4810
4811
3.31k
    res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns);
4812
3.31k
    if (res != 0)
4813
6
  return(res);
4814
4815
3.30k
    if (val == 0)
4816
2.01k
        string = "default";
4817
1.29k
    else
4818
1.29k
        string = "preserve";
4819
4820
3.30k
    attr = xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST string);
4821
3.30k
    if (attr == NULL)
4822
9
        return(-1);
4823
4824
3.29k
    return(0);
4825
3.30k
}
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
558k
xmlNodeGetSpacePreserve(const xmlNode *cur) {
4838
558k
    xmlChar *space;
4839
558k
        int res;
4840
4841
558k
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
4842
556k
        return(-1);
4843
4844
8.21k
    while (cur != NULL) {
4845
6.35k
  res = xmlNodeGetAttrValue(cur, BAD_CAST "space", XML_XML_NAMESPACE,
4846
6.35k
                                  &space);
4847
6.35k
        if (res < 0)
4848
1
            return(-1);
4849
6.34k
  if (space != NULL) {
4850
807
      if (xmlStrEqual(space, BAD_CAST "preserve")) {
4851
207
    xmlFree(space);
4852
207
    return(1);
4853
207
      }
4854
600
      if (xmlStrEqual(space, BAD_CAST "default")) {
4855
404
    xmlFree(space);
4856
404
    return(0);
4857
404
      }
4858
196
      xmlFree(space);
4859
196
  }
4860
4861
5.73k
  cur = cur->parent;
4862
5.73k
    }
4863
4864
1.86k
    return(-1);
4865
2.47k
}
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
3.49k
xmlNodeSetName(xmlNode *cur, const xmlChar *name) {
4875
3.49k
    xmlDocPtr doc;
4876
3.49k
    xmlDictPtr dict;
4877
3.49k
    const xmlChar *copy;
4878
3.49k
    const xmlChar *oldName;
4879
4880
3.49k
    if (cur == NULL) return;
4881
2.93k
    if (name == NULL) return;
4882
2.61k
    switch(cur->type) {
4883
967
        case XML_ELEMENT_NODE:
4884
1.34k
        case XML_ATTRIBUTE_NODE:
4885
1.57k
        case XML_PI_NODE:
4886
2.06k
        case XML_ENTITY_REF_NODE:
4887
2.06k
      break;
4888
551
        default:
4889
551
            return;
4890
2.61k
    }
4891
4892
2.06k
    doc = cur->doc;
4893
2.06k
    if (doc != NULL)
4894
1.13k
  dict = doc->dict;
4895
930
    else
4896
930
        dict = NULL;
4897
4898
2.06k
    if (dict != NULL)
4899
472
        copy = xmlDictLookup(dict, name, -1);
4900
1.58k
    else
4901
1.58k
        copy = xmlStrdup(name);
4902
2.06k
    if (copy == NULL)
4903
3
        return;
4904
4905
2.05k
    oldName = cur->name;
4906
2.05k
    cur->name = copy;
4907
2.05k
    if ((oldName != NULL) &&
4908
2.04k
        ((dict == NULL) || (!xmlDictOwns(dict, oldName))))
4909
1.77k
        xmlFree((xmlChar *) oldName);
4910
2.05k
}
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
29.2k
xmlNodeSetBase(xmlNode *cur, const xmlChar* uri) {
4922
29.2k
    xmlNsPtr ns;
4923
29.2k
    xmlChar* fixed;
4924
4925
29.2k
    if (cur == NULL)
4926
600
        return(-1);
4927
28.6k
    switch(cur->type) {
4928
24.5k
        case XML_ELEMENT_NODE:
4929
25.2k
        case XML_ATTRIBUTE_NODE:
4930
25.2k
      break;
4931
504
        case XML_DOCUMENT_NODE:
4932
2.97k
        case XML_HTML_DOCUMENT_NODE: {
4933
2.97k
      xmlDocPtr doc = (xmlDocPtr) cur;
4934
4935
2.97k
      if (doc->URL != NULL)
4936
442
    xmlFree(doc->URL);
4937
2.97k
      if (uri == NULL) {
4938
355
    doc->URL = NULL;
4939
2.61k
            } else {
4940
2.61k
    doc->URL = xmlPathToURI(uri);
4941
2.61k
                if (doc->URL == NULL)
4942
1
                    return(-1);
4943
2.61k
            }
4944
2.97k
      return(0);
4945
2.97k
  }
4946
351
        default:
4947
351
      return(-1);
4948
28.6k
    }
4949
4950
25.2k
    xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns);
4951
25.2k
    if (ns == NULL)
4952
211
  return(-1);
4953
25.0k
    fixed = xmlPathToURI(uri);
4954
25.0k
    if (fixed == NULL)
4955
990
        return(-1);
4956
24.0k
    if (xmlSetNsProp(cur, ns, BAD_CAST "base", fixed) == NULL) {
4957
434
        xmlFree(fixed);
4958
434
        return(-1);
4959
434
    }
4960
23.6k
    xmlFree(fixed);
4961
4962
23.6k
    return(0);
4963
24.0k
}
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
208k
xmlNodeGetBaseSafe(const xmlDoc *doc, const xmlNode *cur, xmlChar **baseOut) {
4983
208k
    xmlChar *ret = NULL;
4984
208k
    xmlChar *base, *newbase;
4985
208k
    int res;
4986
4987
208k
    if (baseOut == NULL)
4988
0
        return(1);
4989
208k
    *baseOut = NULL;
4990
208k
    if ((cur == NULL) && (doc == NULL))
4991
949
        return(1);
4992
207k
    if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
4993
0
        return(1);
4994
207k
    if (doc == NULL)
4995
47.7k
        doc = cur->doc;
4996
4997
207k
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4998
1.60k
        cur = doc->children;
4999
4.44k
  while ((cur != NULL) && (cur->name != NULL)) {
5000
3.04k
      if (cur->type != XML_ELEMENT_NODE) {
5001
1.78k
          cur = cur->next;
5002
1.78k
    continue;
5003
1.78k
      }
5004
1.26k
      if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5005
310
          cur = cur->children;
5006
310
    continue;
5007
310
      }
5008
951
      if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5009
198
          cur = cur->children;
5010
198
    continue;
5011
198
      }
5012
753
      if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5013
195
                if (xmlNodeGetAttrValue(cur, BAD_CAST "href", NULL, &ret) < 0)
5014
0
                    return(-1);
5015
195
                if (ret == NULL)
5016
195
                    return(1);
5017
0
                goto found;
5018
195
      }
5019
558
      cur = cur->next;
5020
558
  }
5021
1.40k
  return(0);
5022
1.60k
    }
5023
5024
11.8M
    while (cur != NULL) {
5025
11.7M
  if (cur->type == XML_ENTITY_DECL) {
5026
1.06k
      xmlEntityPtr ent = (xmlEntityPtr) cur;
5027
5028
1.06k
            if (ent->URI == NULL)
5029
604
                break;
5030
456
            xmlFree(ret);
5031
456
      ret = xmlStrdup(ent->URI);
5032
456
            if (ret == NULL)
5033
2
                return(-1);
5034
454
            goto found;
5035
456
  }
5036
11.7M
  if (cur->type == XML_ELEMENT_NODE) {
5037
11.5M
      if (xmlNodeGetAttrValue(cur, BAD_CAST "base", XML_XML_NAMESPACE,
5038
11.5M
                                    &base) < 0) {
5039
190
                xmlFree(ret);
5040
190
                return(-1);
5041
190
            }
5042
11.5M
      if (base != NULL) {
5043
336k
    if (ret != NULL) {
5044
232k
        res = xmlBuildURISafe(ret, base, &newbase);
5045
232k
                    xmlFree(ret);
5046
232k
                    xmlFree(base);
5047
232k
                    if (res != 0)
5048
11.8k
                        return(res);
5049
221k
        ret = newbase;
5050
221k
    } else {
5051
103k
        ret = base;
5052
103k
    }
5053
325k
    if ((!xmlStrncmp(ret, BAD_CAST "http://", 7)) ||
5054
320k
        (!xmlStrncmp(ret, BAD_CAST "ftp://", 6)) ||
5055
318k
        (!xmlStrncmp(ret, BAD_CAST "urn:", 4)))
5056
8.33k
                    goto found;
5057
325k
      }
5058
11.5M
  }
5059
11.6M
  cur = cur->parent;
5060
11.6M
    }
5061
5062
185k
    if ((doc != NULL) && (doc->URL != NULL)) {
5063
131k
  if (ret == NULL) {
5064
82.6k
      ret = xmlStrdup(doc->URL);
5065
82.6k
            if (ret == NULL)
5066
44
                return(-1);
5067
82.6k
        } else {
5068
48.9k
            res = xmlBuildURISafe(ret, doc->URL, &newbase);
5069
48.9k
            xmlFree(ret);
5070
48.9k
            if (res != 0)
5071
8.54k
                return(res);
5072
40.4k
            ret = newbase;
5073
40.4k
        }
5074
131k
    }
5075
5076
185k
found:
5077
185k
    *baseOut = ret;
5078
185k
    return(0);
5079
185k
}
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
27.8k
xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
5092
27.8k
    xmlChar *base;
5093
5094
27.8k
    xmlNodeGetBaseSafe(doc, cur, &base);
5095
27.8k
    return(base);
5096
27.8k
}
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
5.05k
{
5113
5.05k
    xmlBufPtr buf;
5114
5.05k
    int ret1, ret2;
5115
5116
5.05k
    if ((cur == NULL) || (buffer == NULL)) return(-1);
5117
3.97k
    buf = xmlBufFromBuffer(buffer);
5118
3.97k
    ret1 = xmlBufGetNodeContent(buf, cur);
5119
3.97k
    ret2 = xmlBufBackToBuffer(buf, buffer);
5120
3.97k
    if ((ret1 < 0) || (ret2 < 0))
5121
7
        return(-1);
5122
3.96k
    return(0);
5123
3.97k
}
5124
5125
static void
5126
297k
xmlBufGetEntityRefContent(xmlBufPtr buf, const xmlNode *ref, int normalize) {
5127
297k
    xmlEntityPtr ent;
5128
5129
297k
    if (ref->children != NULL) {
5130
189k
        ent = (xmlEntityPtr) ref->children;
5131
189k
    } else {
5132
        /* lookup entity declaration */
5133
108k
        ent = xmlGetDocEntity(ref->doc, ref->name);
5134
108k
        if (ent == NULL)
5135
107k
            return;
5136
108k
    }
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
190k
    if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
5144
428
        xmlBufCat(buf, ent->content);
5145
428
        return;
5146
428
    }
5147
5148
189k
    if (ent->flags & XML_ENT_EXPANDING)
5149
215
        return;
5150
5151
189k
    ent->flags |= XML_ENT_EXPANDING;
5152
189k
    xmlBufGetChildContent(buf, (xmlNodePtr) ent, normalize);
5153
189k
    ent->flags &= ~XML_ENT_EXPANDING;
5154
189k
}
5155
5156
static void
5157
2.81M
xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree, int normalize) {
5158
2.81M
    const xmlNode *cur = tree->children;
5159
5160
200M
    while (cur != NULL) {
5161
200M
        switch (cur->type) {
5162
47.9M
            case XML_TEXT_NODE:
5163
47.9M
            case XML_CDATA_SECTION_NODE:
5164
47.9M
                if (normalize)
5165
23.2k
                    xmlBufCat(buf, xmlAttrNormalize(cur->content));
5166
47.9M
                else
5167
47.9M
                    xmlBufCat(buf, cur->content);
5168
47.9M
                break;
5169
5170
295k
            case XML_ENTITY_REF_NODE:
5171
295k
                xmlBufGetEntityRefContent(buf, cur, normalize);
5172
295k
                break;
5173
5174
152M
            default:
5175
152M
                if (cur->children != NULL) {
5176
90.1M
                    cur = cur->children;
5177
90.1M
                    continue;
5178
90.1M
                }
5179
62.0M
                break;
5180
200M
        }
5181
5182
200M
        while (cur->next == NULL) {
5183
92.9M
            cur = cur->parent;
5184
92.9M
            if (cur == tree)
5185
2.80M
                return;
5186
92.9M
        }
5187
107M
        cur = cur->next;
5188
107M
    }
5189
2.81M
}
5190
5191
/**
5192
 * Append the string value of a node to `buf`. For text nodes,
5193
 * the string value is the text content. Otherwise, the string value
5194
 * is the concatenation of the string values of the node's
5195
 * descendants.
5196
 *
5197
 * Entity references are substituted.
5198
 *
5199
 * @param buf  a buffer xmlBuf
5200
 * @param cur  the node being read
5201
 * @returns 0 in case of success and -1 in case of error.
5202
 */
5203
int
5204
xmlBufGetNodeContent(xmlBuf *buf, const xmlNode *cur)
5205
2.62M
{
5206
2.62M
    if ((cur == NULL) || (buf == NULL))
5207
6
        return(-1);
5208
5209
2.62M
    switch (cur->type) {
5210
258k
        case XML_DOCUMENT_NODE:
5211
259k
        case XML_HTML_DOCUMENT_NODE:
5212
259k
        case XML_DOCUMENT_FRAG_NODE:
5213
2.61M
        case XML_ELEMENT_NODE:
5214
2.61M
        case XML_ENTITY_DECL:
5215
2.61M
            xmlBufGetChildContent(buf, cur, 0);
5216
2.61M
            break;
5217
3.71k
        case XML_ATTRIBUTE_NODE:
5218
3.71k
            xmlBufGetChildContent(buf, cur, 1);
5219
3.71k
            break;
5220
5221
198
        case XML_CDATA_SECTION_NODE:
5222
473
        case XML_TEXT_NODE:
5223
671
        case XML_COMMENT_NODE:
5224
887
        case XML_PI_NODE:
5225
887
      xmlBufCat(buf, cur->content);
5226
887
            break;
5227
5228
2.07k
        case XML_ENTITY_REF_NODE:
5229
2.07k
            xmlBufGetEntityRefContent(buf, cur, 0);
5230
2.07k
            break;
5231
5232
0
        case XML_NAMESPACE_DECL:
5233
0
      xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5234
0
      break;
5235
5236
386
        default:
5237
386
            break;
5238
2.62M
    }
5239
5240
2.62M
    return(0);
5241
2.62M
}
5242
5243
/**
5244
 * Returns the string value of a node. For text nodes, the string
5245
 * value is the text content. Otherwise, the string value is the
5246
 * concatenation of the string values of the node's descendants.
5247
 *
5248
 * Entity references are substituted.
5249
 *
5250
 * It's up to the caller to free the result with #xmlFree.
5251
 *
5252
 * @param cur  the node being read
5253
 * @returns a new string or NULL if arguments are invalid or a
5254
 * memory allocation failed.
5255
 */
5256
xmlChar *
5257
xmlNodeGetContent(const xmlNode *cur)
5258
10.8M
{
5259
10.8M
    xmlBufPtr buf;
5260
10.8M
    xmlChar *ret;
5261
5262
10.8M
    if (cur == NULL)
5263
7.55k
        return (NULL);
5264
5265
10.8M
    switch (cur->type) {
5266
258k
        case XML_DOCUMENT_NODE:
5267
259k
        case XML_HTML_DOCUMENT_NODE:
5268
259k
        case XML_ENTITY_REF_NODE:
5269
259k
            break;
5270
5271
402
        case XML_DOCUMENT_FRAG_NODE:
5272
6.30M
        case XML_ELEMENT_NODE:
5273
7.29M
        case XML_ATTRIBUTE_NODE:
5274
7.29M
        case XML_ENTITY_DECL: {
5275
7.29M
            xmlNodePtr children = cur->children;
5276
5277
7.29M
            if (children == NULL)
5278
3.74M
                return(xmlStrdup(BAD_CAST ""));
5279
5280
            /* Optimization for single text children */
5281
3.55M
            if (((children->type == XML_TEXT_NODE) ||
5282
2.02M
                 (children->type == XML_CDATA_SECTION_NODE)) &&
5283
1.53M
                (children->next == NULL)) {
5284
1.19M
                if (children->content == NULL)
5285
196
                    return(xmlStrdup(BAD_CAST ""));
5286
1.19M
                return(xmlStrdup(children->content));
5287
1.19M
            }
5288
5289
2.35M
            break;
5290
3.55M
        }
5291
5292
2.35M
        case XML_CDATA_SECTION_NODE:
5293
3.31M
        case XML_TEXT_NODE:
5294
3.31M
        case XML_COMMENT_NODE:
5295
3.32M
        case XML_PI_NODE:
5296
3.32M
            if (cur->content != NULL)
5297
3.31M
                return(xmlStrdup(cur->content));
5298
2.49k
            else
5299
2.49k
                return(xmlStrdup(BAD_CAST ""));
5300
5301
3.22k
        case XML_NAMESPACE_DECL:
5302
3.22k
      return(xmlStrdup(((xmlNsPtr) cur)->href));
5303
5304
258
        default:
5305
258
            return(NULL);
5306
10.8M
    }
5307
5308
2.61M
    buf = xmlBufCreate(50);
5309
2.61M
    if (buf == NULL)
5310
174
        return (NULL);
5311
2.61M
    xmlBufGetNodeContent(buf, cur);
5312
2.61M
    ret = xmlBufDetach(buf);
5313
2.61M
    xmlBufFree(buf);
5314
5315
2.61M
    return(ret);
5316
2.61M
}
5317
5318
static int
5319
17.5k
xmlNodeSetContentInternal(xmlNodePtr cur, const xmlChar *content, int len) {
5320
17.5k
    if (cur == NULL) {
5321
1.37k
  return(1);
5322
1.37k
    }
5323
16.1k
    switch (cur->type) {
5324
244
        case XML_DOCUMENT_FRAG_NODE:
5325
9.39k
        case XML_ELEMENT_NODE:
5326
10.2k
        case XML_ATTRIBUTE_NODE: {
5327
10.2k
            size_t maxSize = len < 0 ? SIZE_MAX : (size_t) len;
5328
5329
            /*
5330
             * We shouldn't parse the content as attribute value here,
5331
             * but the API can't be changed.
5332
             */
5333
10.2k
            if (xmlNodeParseAttValue(cur->doc, (xmlAttr *) cur,
5334
10.2k
                                     content, maxSize, NULL) < 0)
5335
10
                return(-1);
5336
10.2k
      break;
5337
10.2k
        }
5338
5339
10.2k
        case XML_TEXT_NODE:
5340
3.40k
        case XML_CDATA_SECTION_NODE:
5341
3.79k
        case XML_PI_NODE:
5342
4.18k
        case XML_COMMENT_NODE: {
5343
4.18k
            xmlChar *copy = NULL;
5344
5345
4.18k
      if (content != NULL) {
5346
1.65k
                if (len < 0)
5347
498
                    copy = xmlStrdup(content);
5348
1.15k
                else
5349
1.15k
        copy = xmlStrndup(content, len);
5350
1.65k
                if (copy == NULL)
5351
2
                    return(-1);
5352
1.65k
      }
5353
5354
4.18k
            xmlTextSetContent(cur, copy);
5355
4.18k
      break;
5356
4.18k
        }
5357
5358
1.72k
        default:
5359
1.72k
            break;
5360
16.1k
    }
5361
5362
16.1k
    return(0);
5363
16.1k
}
5364
5365
/**
5366
 * Replace the text content of a node.
5367
 *
5368
 * Sets the raw text content of text, CDATA, comment or PI nodes.
5369
 *
5370
 * For element and attribute nodes, removes all children and
5371
 * replaces them by parsing `content` which is expected to be a
5372
 * valid XML attribute value possibly containing character and
5373
 * entity references. Syntax errors and references to undeclared
5374
 * entities are ignored silently. Unfortunately, there isn't an
5375
 * API to pass raw content directly. An inefficient work-around
5376
 * is to escape the content with #xmlEncodeSpecialChars before
5377
 * passing it. A better trick is clearing the old content
5378
 * with `xmlNodeSetContent(node, NULL)` first and then calling
5379
 * `xmlNodeAddContent(node, content)`. Unlike this function,
5380
 * #xmlNodeAddContent accepts raw text.
5381
 *
5382
 * @param cur  the node being modified
5383
 * @param content  the new value of the content
5384
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5385
 */
5386
int
5387
12.4k
xmlNodeSetContent(xmlNode *cur, const xmlChar *content) {
5388
12.4k
    return(xmlNodeSetContentInternal(cur, content, -1));
5389
12.4k
}
5390
5391
/**
5392
 * See #xmlNodeSetContent.
5393
 *
5394
 * @param cur  the node being modified
5395
 * @param content  the new value of the content
5396
 * @param len  the size of `content`
5397
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5398
 */
5399
int
5400
5.03k
xmlNodeSetContentLen(xmlNode *cur, const xmlChar *content, int len) {
5401
5.03k
    return(xmlNodeSetContentInternal(cur, content, len));
5402
5.03k
}
5403
5404
/**
5405
 * Append the extra substring to the node content.
5406
 *
5407
 * NOTE: In contrast to #xmlNodeSetContentLen, `content` is supposed
5408
 * to be raw text, so unescaped XML special chars are allowed, entity
5409
 * references are not supported.
5410
 *
5411
 * This doesn't work on attributes before version 2.15.
5412
 *
5413
 * @param cur  the node being modified
5414
 * @param content  extra content
5415
 * @param len  the size of `content`
5416
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5417
 */
5418
int
5419
27.2k
xmlNodeAddContentLen(xmlNode *cur, const xmlChar *content, int len) {
5420
27.2k
    if (cur == NULL)
5421
1.30k
  return(1);
5422
25.9k
    if ((content == NULL) || (len <= 0))
5423
2.97k
        return(0);
5424
5425
22.9k
    switch (cur->type) {
5426
300
        case XML_DOCUMENT_FRAG_NODE:
5427
3.24k
        case XML_ELEMENT_NODE:
5428
20.1k
        case XML_ATTRIBUTE_NODE: {
5429
20.1k
      xmlNodePtr newNode, tmp;
5430
5431
20.1k
      newNode = xmlNewDocTextLen(cur->doc, content, len);
5432
20.1k
      if (newNode == NULL)
5433
6
                return(-1);
5434
20.1k
            tmp = xmlAddChild(cur, newNode);
5435
20.1k
            if (tmp == NULL) {
5436
4
                xmlFreeNode(newNode);
5437
4
                return(-1);
5438
4
            }
5439
20.1k
      break;
5440
20.1k
  }
5441
20.1k
      break;
5442
20.1k
        case XML_TEXT_NODE:
5443
1.08k
        case XML_CDATA_SECTION_NODE:
5444
1.75k
        case XML_PI_NODE:
5445
2.05k
        case XML_COMMENT_NODE:
5446
2.05k
            return(xmlTextAddContent(cur, content, len));
5447
694
        default:
5448
694
            break;
5449
22.9k
    }
5450
5451
20.8k
    return(0);
5452
22.9k
}
5453
5454
/**
5455
 * Append the extra substring to the node content.
5456
 *
5457
 * NOTE: In contrast to #xmlNodeSetContent, `content` is supposed
5458
 * to be raw text, so unescaped XML special chars are allowed, entity
5459
 * references are not supported.
5460
 *
5461
 * This doesn't work on attributes before version 2.15.
5462
 *
5463
 * @param cur  the node being modified
5464
 * @param content  extra content
5465
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
5466
 */
5467
int
5468
20.1k
xmlNodeAddContent(xmlNode *cur, const xmlChar *content) {
5469
20.1k
    return(xmlNodeAddContentLen(cur, content, xmlStrlen(content)));
5470
20.1k
}
5471
5472
/**
5473
 * Merge the second text node into the first. If `first` is NULL,
5474
 * `second` is returned. Otherwise, the second node is unlinked and
5475
 * freed.
5476
 *
5477
 * @param first  the first text node
5478
 * @param second  the second text node being merged
5479
 * @returns the first text node augmented or NULL in case of error.
5480
 */
5481
xmlNode *
5482
3.95k
xmlTextMerge(xmlNode *first, xmlNode *second) {
5483
3.95k
    if (first == NULL)
5484
754
        return(second);
5485
3.19k
    if (second == NULL)
5486
805
        return(first);
5487
5488
2.39k
    if ((first->type != XML_TEXT_NODE) ||
5489
1.78k
        (second->type != XML_TEXT_NODE) ||
5490
1.56k
        (first == second) ||
5491
1.37k
        (first->name != second->name))
5492
1.02k
  return(NULL);
5493
5494
1.37k
    if (xmlTextAddContent(first, second->content, -1) < 0)
5495
1
        return(NULL);
5496
5497
1.36k
    xmlUnlinkNodeInternal(second);
5498
1.36k
    xmlFreeNode(second);
5499
1.36k
    return(first);
5500
1.37k
}
5501
5502
/**
5503
 * Find all in-scope namespaces of a node. `out` returns a NULL
5504
 * terminated array of namespace pointers that must be freed by
5505
 * the caller.
5506
 *
5507
 * @since 2.13.0
5508
 *
5509
 * @param doc  the document
5510
 * @param node  the current node
5511
 * @param out  the returned namespace array
5512
 * @returns 0 on success, 1 if no namespaces were found, -1 if a
5513
 * memory allocation failed.
5514
 */
5515
int
5516
xmlGetNsListSafe(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node,
5517
                 xmlNs ***out)
5518
551k
{
5519
551k
    xmlNsPtr cur;
5520
551k
    xmlNsPtr *namespaces = NULL;
5521
551k
    int nbns = 0;
5522
551k
    int maxns = 0;
5523
551k
    int i;
5524
5525
551k
    if (out == NULL)
5526
0
        return(1);
5527
551k
    *out = NULL;
5528
551k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5529
944
        return(1);
5530
5531
10.7M
    while (node != NULL) {
5532
10.2M
        if (node->type == XML_ELEMENT_NODE) {
5533
9.68M
            cur = node->nsDef;
5534
12.5M
            while (cur != NULL) {
5535
11.4M
                for (i = 0; i < nbns; i++) {
5536
9.17M
                    if ((cur->prefix == namespaces[i]->prefix) ||
5537
8.98M
                        (xmlStrEqual(cur->prefix, namespaces[i]->prefix)))
5538
616k
                        break;
5539
9.17M
                }
5540
2.84M
                if (i >= nbns) {
5541
2.22M
                    if (nbns >= maxns) {
5542
1.28M
                        xmlNsPtr *tmp;
5543
1.28M
                        int newSize;
5544
5545
1.28M
                        newSize = xmlGrowCapacity(maxns, sizeof(tmp[0]),
5546
1.28M
                                                  10, XML_MAX_ITEMS);
5547
1.28M
                        if (newSize < 0) {
5548
0
                            xmlFree(namespaces);
5549
0
                            return(-1);
5550
0
                        }
5551
1.28M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
5552
1.28M
                        if (newSize < 2)
5553
482k
                            newSize = 2;
5554
1.28M
#endif
5555
1.28M
                        tmp = xmlRealloc(namespaces,
5556
1.28M
                                         (newSize + 1) * sizeof(tmp[0]));
5557
1.28M
                        if (tmp == NULL) {
5558
28
                            xmlFree(namespaces);
5559
28
                            return(-1);
5560
28
                        }
5561
1.28M
                        namespaces = tmp;
5562
1.28M
                        maxns = newSize;
5563
1.28M
                    }
5564
2.22M
                    namespaces[nbns++] = cur;
5565
2.22M
                    namespaces[nbns] = NULL;
5566
2.22M
                }
5567
5568
2.84M
                cur = cur->next;
5569
2.84M
            }
5570
9.68M
        }
5571
10.2M
        node = node->parent;
5572
10.2M
    }
5573
5574
550k
    *out = namespaces;
5575
550k
    return((namespaces == NULL) ? 1 : 0);
5576
550k
}
5577
5578
/**
5579
 * Find all in-scope namespaces of a node.
5580
 *
5581
 * Use #xmlGetNsListSafe for better error reporting.
5582
 *
5583
 * @param doc  the document
5584
 * @param node  the current node
5585
 * @returns a NULL terminated array of namespace pointers that must
5586
 * be freed by the caller or NULL if no namespaces were found or
5587
 * a memory allocation failed.
5588
 */
5589
xmlNs **
5590
xmlGetNsList(const xmlDoc *doc, const xmlNode *node)
5591
31.4k
{
5592
31.4k
    xmlNsPtr *ret;
5593
5594
31.4k
    xmlGetNsListSafe(doc, node, &ret);
5595
31.4k
    return(ret);
5596
31.4k
}
5597
5598
static xmlNsPtr
5599
43.4k
xmlNewXmlNs(void) {
5600
43.4k
    xmlNsPtr ns;
5601
5602
43.4k
    ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5603
43.4k
    if (ns == NULL)
5604
82
        return(NULL);
5605
43.3k
    memset(ns, 0, sizeof(xmlNs));
5606
43.3k
    ns->type = XML_LOCAL_NAMESPACE;
5607
43.3k
    ns->href = xmlStrdup(XML_XML_NAMESPACE);
5608
43.3k
    if (ns->href == NULL) {
5609
81
        xmlFreeNs(ns);
5610
81
        return(NULL);
5611
81
    }
5612
43.2k
    ns->prefix = xmlStrdup(BAD_CAST "xml");
5613
43.2k
    if (ns->prefix == NULL) {
5614
93
        xmlFreeNs(ns);
5615
93
        return(NULL);
5616
93
    }
5617
5618
43.1k
    return(ns);
5619
43.2k
}
5620
5621
/*
5622
* xmlTreeEnsureXMLDecl:
5623
* @doc: the doc
5624
*
5625
* Ensures that there is an XML namespace declaration on the doc.
5626
*
5627
* Returns the XML ns-struct or NULL if a memory allocation failed.
5628
*/
5629
static xmlNsPtr
5630
xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5631
554k
{
5632
554k
    xmlNsPtr ns;
5633
5634
554k
    ns = doc->oldNs;
5635
554k
    if (ns != NULL)
5636
520k
  return (ns);
5637
5638
33.9k
    ns = xmlNewXmlNs();
5639
33.9k
    doc->oldNs = ns;
5640
5641
33.9k
    return(ns);
5642
554k
}
5643
5644
/**
5645
 * Search for a namespace with `prefix` in scope of `node`.
5646
 *
5647
 * @param node  a node
5648
 * @param prefix  a namespace prefix
5649
 * @param out  pointer to resulting namespace
5650
 * @returns 0 on success, -1 if a memory allocation failed, 1 on
5651
 * other errors.
5652
 */
5653
int
5654
xmlSearchNsSafe(xmlNode *node, const xmlChar *prefix,
5655
3.70M
                xmlNs **out) {
5656
3.70M
    xmlNsPtr cur;
5657
3.70M
    xmlDocPtr doc;
5658
3.70M
    xmlNodePtr orig = node;
5659
3.70M
    xmlNodePtr parent;
5660
5661
3.70M
    if (out == NULL)
5662
0
        return(1);
5663
3.70M
    *out = NULL;
5664
3.70M
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5665
538
        return(1);
5666
5667
3.70M
    doc = node->doc;
5668
5669
3.70M
    if ((doc != NULL) && (IS_STR_XML(prefix))) {
5670
499k
        cur = xmlTreeEnsureXMLDecl(doc);
5671
499k
        if (cur == NULL)
5672
224
            return(-1);
5673
499k
        *out = cur;
5674
499k
        return(0);
5675
499k
    }
5676
5677
3.20M
    while (node->type != XML_ELEMENT_NODE) {
5678
54.1k
        node = node->parent;
5679
54.1k
        if (node == NULL)
5680
49.4k
            return(0);
5681
54.1k
    }
5682
5683
3.15M
    parent = node;
5684
5685
116M
    while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
5686
115M
        cur = node->nsDef;
5687
131M
        while (cur != NULL) {
5688
16.9M
            if ((xmlStrEqual(cur->prefix, prefix)) &&
5689
1.42M
                (cur->href != NULL)) {
5690
1.19M
                *out = cur;
5691
1.19M
                return(0);
5692
1.19M
            }
5693
15.7M
            cur = cur->next;
5694
15.7M
        }
5695
114M
        if (orig != node) {
5696
111M
            cur = node->ns;
5697
111M
            if ((cur != NULL) &&
5698
2.70M
                (xmlStrEqual(cur->prefix, prefix)) &&
5699
760k
                (cur->href != NULL)) {
5700
753k
                *out = cur;
5701
753k
                return(0);
5702
753k
            }
5703
111M
        }
5704
5705
113M
  node = node->parent;
5706
113M
    }
5707
5708
    /*
5709
     * The XML-1.0 namespace is normally held on the document
5710
     * element. In this case exceptionally create it on the
5711
     * node element.
5712
     */
5713
1.20M
    if ((doc == NULL) && (IS_STR_XML(prefix))) {
5714
4.77k
        cur = xmlNewXmlNs();
5715
4.77k
        if (cur == NULL)
5716
3
            return(-1);
5717
4.77k
        cur->next = parent->nsDef;
5718
4.77k
        parent->nsDef = cur;
5719
4.77k
        *out = cur;
5720
4.77k
    }
5721
5722
1.20M
    return(0);
5723
1.20M
}
5724
5725
/**
5726
 * Search for a namespace with `prefix` in scope of `node`.
5727
 *
5728
 * Recurse on the parents until it finds the defined namespace
5729
 * or return NULL otherwise.
5730
 *
5731
 * If `nameSpace` is NULL, the default namespace is looked up.
5732
 *
5733
 * Namespace search doesn't cross entity boundaries.
5734
 *
5735
 * @param doc  the document
5736
 * @param node  the current node
5737
 * @param nameSpace  the namespace prefix
5738
 * @returns the namespace pointer or NULL if no namespace was found or
5739
 * a memory allocation failed. Allocations can only fail if the `xml`
5740
 * namespace is queried.
5741
 */
5742
xmlNs *
5743
xmlSearchNs(xmlDoc *doc ATTRIBUTE_UNUSED, xmlNode *node,
5744
132k
            const xmlChar *nameSpace) {
5745
132k
    xmlNsPtr cur;
5746
5747
132k
    xmlSearchNsSafe(node, nameSpace, &cur);
5748
132k
    return(cur);
5749
132k
}
5750
5751
/**
5752
 * Verify that the given namespace held on `ancestor` is still in scope
5753
 * on node.
5754
 *
5755
 * @param doc  the document
5756
 * @param node  the current node
5757
 * @param ancestor  the ancestor carrying the namespace
5758
 * @param prefix  the namespace prefix
5759
 * @returns 1 if true, 0 if false and -1 in case of error.
5760
 */
5761
static int
5762
xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5763
             xmlNodePtr ancestor, const xmlChar * prefix)
5764
63.4k
{
5765
63.4k
    xmlNsPtr tst;
5766
5767
147k
    while ((node != NULL) && (node != ancestor)) {
5768
110k
        if ((node->type == XML_ENTITY_REF_NODE) ||
5769
110k
            (node->type == XML_ENTITY_DECL))
5770
422
            return (-1);
5771
110k
        if (node->type == XML_ELEMENT_NODE) {
5772
110k
            tst = node->nsDef;
5773
159k
            while (tst != NULL) {
5774
75.8k
                if ((tst->prefix == NULL)
5775
19.4k
                    && (prefix == NULL))
5776
11.9k
                    return (0);
5777
63.9k
                if ((tst->prefix != NULL)
5778
56.4k
                    && (prefix != NULL)
5779
32.0k
                    && (xmlStrEqual(tst->prefix, prefix)))
5780
14.6k
                    return (0);
5781
49.2k
                tst = tst->next;
5782
49.2k
            }
5783
110k
        }
5784
83.8k
        node = node->parent;
5785
83.8k
    }
5786
36.4k
    if (node != ancestor)
5787
0
        return (-1);
5788
36.4k
    return (1);
5789
36.4k
}
5790
5791
/**
5792
 * Search for a namespace matching `URI` in scope of `node`.
5793
 *
5794
 * @param node  a node
5795
 * @param href  a namespace URI
5796
 * @param out  pointer to resulting namespace
5797
 * @returns 0 on success, -1 if a memory allocation failed, 1 on
5798
 * other errors.
5799
 */
5800
int
5801
xmlSearchNsByHrefSafe(xmlNode *node, const xmlChar *href,
5802
146k
                      xmlNs **out) {
5803
146k
    xmlNsPtr cur;
5804
146k
    xmlDocPtr doc;
5805
146k
    xmlNodePtr orig = node;
5806
146k
    xmlNodePtr parent;
5807
146k
    int is_attr;
5808
5809
146k
    if (out == NULL)
5810
0
        return(1);
5811
146k
    *out = NULL;
5812
146k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5813
290
        return(1);
5814
5815
146k
    doc = node->doc;
5816
5817
146k
    if ((doc != NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) {
5818
29.8k
        cur = xmlTreeEnsureXMLDecl(doc);
5819
29.8k
        if (cur == NULL)
5820
12
            return(-1);
5821
29.8k
        *out = cur;
5822
29.8k
        return(0);
5823
29.8k
    }
5824
5825
116k
    is_attr = (node->type == XML_ATTRIBUTE_NODE);
5826
5827
117k
    while (node->type != XML_ELEMENT_NODE) {
5828
1.43k
        node = node->parent;
5829
1.43k
        if (node == NULL)
5830
508
            return(0);
5831
1.43k
    }
5832
5833
115k
    parent = node;
5834
5835
318k
    while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
5836
238k
        cur = node->nsDef;
5837
2.43M
        while (cur != NULL) {
5838
2.23M
            if (xmlStrEqual(cur->href, href)) {
5839
45.0k
                if (((!is_attr) || (cur->prefix != NULL)) &&
5840
44.7k
                    (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) {
5841
34.5k
                    *out = cur;
5842
34.5k
                    return(0);
5843
34.5k
                }
5844
45.0k
            }
5845
2.20M
            cur = cur->next;
5846
2.20M
        }
5847
204k
        if (orig != node) {
5848
120k
            cur = node->ns;
5849
120k
            if (cur != NULL) {
5850
113k
                if (xmlStrEqual(cur->href, href)) {
5851
18.0k
                    if (((!is_attr) || (cur->prefix != NULL)) &&
5852
17.6k
                        (xmlNsInScope(doc, orig, node,
5853
17.6k
                                      cur->prefix) == 1)) {
5854
1.38k
                        *out = cur;
5855
1.38k
                        return(0);
5856
1.38k
                    }
5857
18.0k
                }
5858
113k
            }
5859
120k
        }
5860
5861
202k
        node = node->parent;
5862
202k
    }
5863
5864
    /*
5865
     * The XML-1.0 namespace is normally held on the document
5866
     * element. In this case exceptionally create it on the
5867
     * node element.
5868
     */
5869
80.0k
    if ((doc == NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) {
5870
4.72k
        cur = xmlNewXmlNs();
5871
4.72k
        if (cur == NULL)
5872
12
            return(-1);
5873
4.71k
        cur->next = parent->nsDef;
5874
4.71k
        parent->nsDef = cur;
5875
4.71k
        *out = cur;
5876
4.71k
    }
5877
5878
80.0k
    return(0);
5879
80.0k
}
5880
5881
/**
5882
 * Search for a namespace matching `URI` in scope of `node`.
5883
 *
5884
 * @param doc  the document
5885
 * @param node  the current node
5886
 * @param href  the namespace value
5887
 * @returns the namespace pointer or NULL if no namespace was found or
5888
 * a memory allocation failed. Allocations can only fail if the `xml`
5889
 * namespace is queried.
5890
 */
5891
xmlNs *
5892
xmlSearchNsByHref(xmlDoc *doc ATTRIBUTE_UNUSED, xmlNode *node,
5893
1.38k
                  const xmlChar * href) {
5894
1.38k
    xmlNsPtr cur;
5895
5896
1.38k
    xmlSearchNsByHrefSafe(node, href, &cur);
5897
1.38k
    return(cur);
5898
1.38k
}
5899
5900
/**
5901
 * Fix up namespace declarations.
5902
 *
5903
 * This function tries to locate a namespace definition in a tree
5904
 * ancestors, or create a new namespace definition node similar to
5905
 * `ns` trying to reuse the same prefix. However if the given prefix is
5906
 * null (default namespace) or reused within the subtree defined by
5907
 * `tree` or on one of its ancestors then a new prefix is generated.
5908
 *
5909
 * @param tree  a node expected to hold the new namespace
5910
 * @param ns  the original namespace
5911
 * @returns the (new) namespace definition or NULL in case of error
5912
 */
5913
static xmlNsPtr
5914
111k
xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns) {
5915
111k
    xmlNsPtr def;
5916
111k
    xmlChar prefix[50];
5917
111k
    int counter = 1;
5918
111k
    int res;
5919
5920
111k
    if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
5921
0
  return(NULL);
5922
0
    }
5923
111k
    if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
5924
0
  return(NULL);
5925
0
    }
5926
    /*
5927
     * Search an existing namespace definition inherited.
5928
     */
5929
111k
    res = xmlSearchNsByHrefSafe(tree, ns->href, &def);
5930
111k
    if (res < 0)
5931
4
        return(NULL);
5932
111k
    if (def != NULL)
5933
36.6k
        return(def);
5934
5935
    /*
5936
     * Find a close prefix which is not already in use.
5937
     * Let's strip namespace prefixes longer than 20 chars !
5938
     */
5939
74.7k
    if (ns->prefix == NULL)
5940
3.31k
  snprintf((char *) prefix, sizeof(prefix), "default");
5941
71.4k
    else
5942
71.4k
  snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
5943
5944
74.7k
    res = xmlSearchNsSafe(tree, prefix, &def);
5945
74.7k
    if (res < 0)
5946
1
        return(NULL);
5947
112k
    while (def != NULL) {
5948
37.8k
        if (counter > 1000) return(NULL);
5949
37.8k
  if (ns->prefix == NULL)
5950
274
      snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
5951
37.5k
  else
5952
37.5k
      snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5953
37.5k
    (char *)ns->prefix, counter++);
5954
37.8k
  res = xmlSearchNsSafe(tree, prefix, &def);
5955
37.8k
        if (res < 0)
5956
0
            return(NULL);
5957
37.8k
    }
5958
5959
    /*
5960
     * OK, now we are ready to create a new one.
5961
     */
5962
74.7k
    def = xmlNewNs(tree, ns->href, prefix);
5963
74.7k
    return(def);
5964
74.7k
}
5965
5966
typedef struct {
5967
    xmlNsPtr oldNs;
5968
    xmlNsPtr newNs;
5969
} xmlNsCache;
5970
5971
static int
5972
43.0k
xmlGrowNsCache(xmlNsCache **cache, int *capacity) {
5973
43.0k
    xmlNsCache *tmp;
5974
43.0k
    int newSize;
5975
5976
43.0k
    newSize = xmlGrowCapacity(*capacity, sizeof(tmp[0]),
5977
43.0k
                              10, XML_MAX_ITEMS);
5978
43.0k
    if (newSize < 0)
5979
0
        return(-1);
5980
43.0k
    tmp = xmlRealloc(*cache, newSize * sizeof(tmp[0]));
5981
43.0k
    if (tmp == NULL)
5982
22
        return(-1);
5983
43.0k
    *cache = tmp;
5984
43.0k
    *capacity = newSize;
5985
5986
43.0k
    return(0);
5987
43.0k
}
5988
5989
/**
5990
 * This function checks that all the namespaces declared within the given
5991
 * tree are properly declared. This is needed for example after copy or cut
5992
 * and then paste operations. The subtree may still hold pointers to
5993
 * namespace declarations outside the subtree or invalid/masked. As much
5994
 * as possible the function tries to reuse the existing namespaces found in
5995
 * the new environment. If not possible the new namespaces are redeclared
5996
 * on `tree` at the top of the given subtree.
5997
 *
5998
 * @param doc  the document
5999
 * @param tree  a node defining the subtree to reconciliate
6000
 * @returns 0 on success or -1 in case of error.
6001
 */
6002
int
6003
57.3k
xmlReconciliateNs(xmlDoc *doc, xmlNode *tree) {
6004
57.3k
    xmlNsCache *cache = NULL;
6005
57.3k
    int sizeCache = 0;
6006
57.3k
    int nbCache = 0;
6007
6008
57.3k
    xmlNsPtr n;
6009
57.3k
    xmlNodePtr node = tree;
6010
57.3k
    xmlAttrPtr attr;
6011
57.3k
    int ret = 0, i;
6012
6013
57.3k
    if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6014
57.3k
    if (node->doc != doc) return(-1);
6015
345k
    while (node != NULL) {
6016
        /*
6017
   * Reconciliate the node namespace
6018
   */
6019
323k
  if (node->ns != NULL) {
6020
2.67M
      for (i = 0; i < nbCache; i++) {
6021
2.58M
          if (cache[i].oldNs == node->ns) {
6022
25.6k
        node->ns = cache[i].newNs;
6023
25.6k
        break;
6024
25.6k
    }
6025
2.58M
      }
6026
119k
      if (i == nbCache) {
6027
          /*
6028
     * OK we need to recreate a new namespace definition
6029
     */
6030
93.7k
    n = xmlNewReconciledNs(tree, node->ns);
6031
93.7k
    if (n == NULL) {
6032
19
                    ret = -1;
6033
93.7k
                } else {
6034
        /*
6035
         * check if we need to grow the cache buffers.
6036
         */
6037
93.7k
        if ((sizeCache <= nbCache) &&
6038
33.1k
                        (xmlGrowNsCache(&cache, &sizeCache) < 0)) {
6039
17
                        ret = -1;
6040
93.7k
        } else {
6041
93.7k
                        cache[nbCache].newNs = n;
6042
93.7k
                        cache[nbCache++].oldNs = node->ns;
6043
93.7k
                    }
6044
93.7k
                }
6045
93.7k
    node->ns = n;
6046
93.7k
      }
6047
119k
  }
6048
  /*
6049
   * now check for namespace held by attributes on the node.
6050
   */
6051
323k
  if (node->type == XML_ELEMENT_NODE) {
6052
193k
      attr = node->properties;
6053
226k
      while (attr != NULL) {
6054
33.0k
    if (attr->ns != NULL) {
6055
134k
        for (i = 0; i < nbCache; i++) {
6056
122k
      if (cache[i].oldNs == attr->ns) {
6057
17.3k
          attr->ns = cache[i].newNs;
6058
17.3k
          break;
6059
17.3k
      }
6060
122k
        }
6061
28.6k
        if (i == nbCache) {
6062
      /*
6063
       * OK we need to recreate a new namespace definition
6064
       */
6065
11.2k
      n = xmlNewReconciledNs(tree, attr->ns);
6066
11.2k
      if (n == NULL) {
6067
2
                            ret = -1;
6068
11.2k
                        } else {
6069
          /*
6070
           * check if we need to grow the cache buffers.
6071
           */
6072
11.2k
                            if ((sizeCache <= nbCache) &&
6073
9.86k
                                (xmlGrowNsCache(&cache, &sizeCache) < 0)) {
6074
5
                                ret = -1;
6075
11.1k
                            } else {
6076
11.1k
                                cache[nbCache].newNs = n;
6077
11.1k
                                cache[nbCache++].oldNs = attr->ns;
6078
11.1k
          }
6079
11.2k
      }
6080
11.2k
      attr->ns = n;
6081
11.2k
        }
6082
28.6k
    }
6083
33.0k
    attr = attr->next;
6084
33.0k
      }
6085
193k
  }
6086
6087
  /*
6088
   * Browse the full subtree, deep first
6089
   */
6090
323k
        if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6091
      /* deep first */
6092
145k
      node = node->children;
6093
178k
  } else if ((node != tree) && (node->next != NULL)) {
6094
      /* then siblings */
6095
116k
      node = node->next;
6096
116k
  } else if (node != tree) {
6097
      /* go up to parents->next if needed */
6098
153k
      while (node != tree) {
6099
145k
          if (node->parent != NULL)
6100
145k
        node = node->parent;
6101
145k
    if ((node != tree) && (node->next != NULL)) {
6102
4.87k
        node = node->next;
6103
4.87k
        break;
6104
4.87k
    }
6105
140k
    if (node->parent == NULL) {
6106
13.5k
        node = NULL;
6107
13.5k
        break;
6108
13.5k
    }
6109
140k
      }
6110
      /* exit condition */
6111
26.4k
      if (node == tree)
6112
8.08k
          node = NULL;
6113
26.4k
  } else
6114
35.7k
      break;
6115
323k
    }
6116
57.3k
    if (cache != NULL)
6117
22.1k
  xmlFree(cache);
6118
57.3k
    return(ret);
6119
57.3k
}
6120
6121
static xmlAttrPtr
6122
xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
6123
           const xmlChar *nsName, int useDTD)
6124
13.0M
{
6125
13.0M
    xmlAttrPtr prop;
6126
6127
    /* Avoid unused variable warning if features are disabled. */
6128
13.0M
    (void) useDTD;
6129
6130
13.0M
    if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6131
89.9k
  return(NULL);
6132
6133
12.9M
    if (node->properties != NULL) {
6134
1.37M
  prop = node->properties;
6135
1.37M
  if (nsName == NULL) {
6136
      /*
6137
      * We want the attr to be in no namespace.
6138
      */
6139
793k
      do {
6140
793k
    if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6141
274k
        return(prop);
6142
274k
    }
6143
519k
    prop = prop->next;
6144
519k
      } while (prop != NULL);
6145
785k
  } else {
6146
      /*
6147
      * We want the attr to be in the specified namespace.
6148
      */
6149
983k
      do {
6150
983k
    if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6151
365k
        ((prop->ns->href == nsName) ||
6152
352k
         xmlStrEqual(prop->ns->href, nsName)))
6153
362k
    {
6154
362k
        return(prop);
6155
362k
    }
6156
621k
    prop = prop->next;
6157
621k
      } while (prop != NULL);
6158
785k
  }
6159
1.37M
    }
6160
6161
12.3M
    if (! useDTD)
6162
12.0M
  return(NULL);
6163
    /*
6164
     * Check if there is a default/fixed attribute declaration in
6165
     * the internal or external subset.
6166
     */
6167
286k
    if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6168
12.4k
  xmlDocPtr doc = node->doc;
6169
12.4k
  xmlAttributePtr attrDecl = NULL;
6170
12.4k
  xmlChar *elemQName, *tmpstr = NULL;
6171
6172
  /*
6173
  * We need the QName of the element for the DTD-lookup.
6174
  */
6175
12.4k
  if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6176
1.85k
      tmpstr = xmlStrdup(node->ns->prefix);
6177
1.85k
      if (tmpstr == NULL)
6178
3
    return(NULL);
6179
1.85k
      tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6180
1.85k
      if (tmpstr == NULL)
6181
3
    return(NULL);
6182
1.85k
      tmpstr = xmlStrcat(tmpstr, node->name);
6183
1.85k
      if (tmpstr == NULL)
6184
5
    return(NULL);
6185
1.84k
      elemQName = tmpstr;
6186
1.84k
  } else
6187
10.6k
      elemQName = (xmlChar *) node->name;
6188
12.4k
  if (nsName == NULL) {
6189
      /*
6190
      * The common and nice case: Attr in no namespace.
6191
      */
6192
5.89k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6193
5.89k
    elemQName, name, NULL);
6194
5.89k
      if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6195
203
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6196
203
        elemQName, name, NULL);
6197
203
      }
6198
6.58k
        } else if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
6199
      /*
6200
      * The XML namespace must be bound to prefix 'xml'.
6201
      */
6202
4.59k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6203
4.59k
    elemQName, name, BAD_CAST "xml");
6204
4.59k
      if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6205
252
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6206
252
        elemQName, name, BAD_CAST "xml");
6207
252
      }
6208
4.59k
  } else {
6209
1.98k
      xmlNsPtr *nsList, *cur;
6210
6211
      /*
6212
      * The ugly case: Search using the prefixes of in-scope
6213
      * ns-decls corresponding to @nsName.
6214
      */
6215
1.98k
      nsList = xmlGetNsList(node->doc, node);
6216
1.98k
      if (nsList == NULL) {
6217
453
    if (tmpstr != NULL)
6218
194
        xmlFree(tmpstr);
6219
453
    return(NULL);
6220
453
      }
6221
1.53k
      cur = nsList;
6222
3.35k
      while (*cur != NULL) {
6223
2.24k
    if (xmlStrEqual((*cur)->href, nsName)) {
6224
1.81k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6225
1.81k
      name, (*cur)->prefix);
6226
1.81k
        if (attrDecl)
6227
235
      break;
6228
1.57k
        if (doc->extSubset != NULL) {
6229
467
      attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6230
467
          name, (*cur)->prefix);
6231
467
      if (attrDecl)
6232
195
          break;
6233
467
        }
6234
1.57k
    }
6235
1.81k
    cur++;
6236
1.81k
      }
6237
1.53k
      xmlFree(nsList);
6238
1.53k
  }
6239
12.0k
  if (tmpstr != NULL)
6240
1.65k
      xmlFree(tmpstr);
6241
  /*
6242
  * Only default/fixed attrs are relevant.
6243
  */
6244
12.0k
  if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6245
1.68k
      return((xmlAttrPtr) attrDecl);
6246
12.0k
    }
6247
6248
283k
    return(NULL);
6249
286k
}
6250
6251
static xmlChar*
6252
xmlGetPropNodeValueInternal(const xmlAttr *prop)
6253
607k
{
6254
607k
    if (prop == NULL)
6255
0
  return(NULL);
6256
607k
    if (prop->type == XML_ATTRIBUTE_NODE) {
6257
606k
  return(xmlNodeGetContent((xmlNodePtr) prop));
6258
606k
    } else if (prop->type == XML_ATTRIBUTE_DECL) {
6259
920
  return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6260
920
    }
6261
0
    return(NULL);
6262
607k
}
6263
6264
/**
6265
 * Search for an attribute of an element.
6266
 *
6267
 * This function also looks in DTD attribute declaration for \#FIXED or
6268
 * default declaration values.
6269
 *
6270
 * @param node  the element
6271
 * @param name  the attribute name
6272
 * @returns the attribute or the attribute declaration or NULL if
6273
 * neither was found. Also returns NULL if a memory allocation failed,
6274
 * making this function unreliable.
6275
 */
6276
xmlAttr *
6277
15.6k
xmlHasProp(const xmlNode *node, const xmlChar *name) {
6278
15.6k
    xmlAttrPtr prop;
6279
15.6k
    xmlDocPtr doc;
6280
6281
15.6k
    if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6282
2.26k
        return(NULL);
6283
    /*
6284
     * Check on the properties attached to the node
6285
     */
6286
13.3k
    prop = node->properties;
6287
27.2k
    while (prop != NULL) {
6288
25.3k
        if (xmlStrEqual(prop->name, name))  {
6289
11.5k
      return(prop);
6290
11.5k
        }
6291
13.8k
  prop = prop->next;
6292
13.8k
    }
6293
6294
    /*
6295
     * Check if there is a default declaration in the internal
6296
     * or external subsets
6297
     */
6298
1.81k
    doc =  node->doc;
6299
1.81k
    if (doc != NULL) {
6300
1.40k
        xmlAttributePtr attrDecl;
6301
1.40k
        if (doc->intSubset != NULL) {
6302
1.20k
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6303
1.20k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
6304
266
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6305
1.20k
            if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6306
              /* return attribute declaration only if a default value is given
6307
                 (that includes #FIXED declarations) */
6308
395
    return((xmlAttrPtr) attrDecl);
6309
1.20k
  }
6310
1.40k
    }
6311
1.42k
    return(NULL);
6312
1.81k
}
6313
6314
/**
6315
 * Search for an attribute of an element.
6316
 *
6317
 * The attribute has to match the specified namespace. A namespace of
6318
 * NULL means that the attribute must have no namespace.
6319
 *
6320
 * This function also looks in DTD attribute declaration for \#FIXED or
6321
 * default declaration values.
6322
 *
6323
 * @param node  the element
6324
 * @param name  the attribute name
6325
 * @param nameSpace  the URI of the namespace
6326
 * @returns the attribute or the attribute declaration or NULL if
6327
 * neither was found. Also returns NULL if a memory allocation failed
6328
 * making this function unreliable.
6329
 */
6330
xmlAttr *
6331
54.7k
xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6332
6333
54.7k
    return(xmlGetPropNodeInternal(node, name, nameSpace, 1));
6334
54.7k
}
6335
6336
/**
6337
 * Look up the value of an element's attribute.
6338
 *
6339
 * The attribute has to match the specified namespace. A namespace of
6340
 * NULL means that the attribute must have no namespace.
6341
 *
6342
 * Entities are substituted. The returned value must be freed by the
6343
 * caller.
6344
 *
6345
 * @since 2.13.0
6346
 *
6347
 * @param node  the element
6348
 * @param name  the attribute name
6349
 * @param nsUri  the URI of the namespace
6350
 * @param out  the returned string
6351
 * @returns 0 on success, 1 if no attribute was found, -1 if a
6352
 * memory allocation failed.
6353
 */
6354
int
6355
xmlNodeGetAttrValue(const xmlNode *node, const xmlChar *name,
6356
12.4M
                    const xmlChar *nsUri, xmlChar **out) {
6357
12.4M
    xmlAttrPtr prop;
6358
6359
12.4M
    if (out == NULL)
6360
0
        return(1);
6361
12.4M
    *out = NULL;
6362
6363
12.4M
    prop = xmlGetPropNodeInternal(node, name, nsUri, 0);
6364
12.4M
    if (prop == NULL)
6365
12.0M
  return(1);
6366
6367
397k
    *out = xmlGetPropNodeValueInternal(prop);
6368
397k
    if (*out == NULL)
6369
264
        return(-1);
6370
397k
    return(0);
6371
397k
}
6372
6373
/**
6374
 * Look up the value of an element's attribute.
6375
 *
6376
 * Entities are substituted. The returned value must be freed by the
6377
 * caller.
6378
 *
6379
 * This function looks in DTD attribute declarations for \#FIXED or
6380
 * default declaration values.
6381
 *
6382
 * NOTE: This function is ignores namespaces. Use #xmlGetNsProp or
6383
 * #xmlGetNoNsProp for namespace aware processing.
6384
 *
6385
 * NOTE: This function doesn't allow to distinguish malloc failures from
6386
 * missing attributes.
6387
 *
6388
 * @param node  the element
6389
 * @param name  the attribute name
6390
 * @returns the attribute value or NULL if not found or a memory allocation
6391
 * failed.
6392
 */
6393
xmlChar *
6394
3.64k
xmlGetProp(const xmlNode *node, const xmlChar *name) {
6395
3.64k
    xmlAttrPtr prop;
6396
6397
3.64k
    prop = xmlHasProp(node, name);
6398
3.64k
    if (prop == NULL)
6399
2.47k
  return(NULL);
6400
1.16k
    return(xmlGetPropNodeValueInternal(prop));
6401
3.64k
}
6402
6403
/**
6404
 * Look up the value of an element's attribute.
6405
 *
6406
 * Entities are substituted. The returned value must be freed by the
6407
 * caller.
6408
 *
6409
 * This function looks in DTD attribute declarations for \#FIXED or
6410
 * default declaration values.
6411
 *
6412
 * This function is similar to #xmlGetProp except it will accept only
6413
 * an attribute in no namespace.
6414
 *
6415
 * NOTE: This function doesn't allow to distinguish malloc failures from
6416
 * missing attributes. It's more robust to use #xmlNodeGetAttrValue.
6417
 *
6418
 * @param node  the element
6419
 * @param name  the attribute name
6420
 * @returns the attribute value or NULL if not found or a memory allocation
6421
 * failed.
6422
 */
6423
xmlChar *
6424
467k
xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
6425
467k
    xmlAttrPtr prop;
6426
6427
467k
    prop = xmlGetPropNodeInternal(node, name, NULL, 1);
6428
467k
    if (prop == NULL)
6429
259k
  return(NULL);
6430
208k
    return(xmlGetPropNodeValueInternal(prop));
6431
467k
}
6432
6433
/**
6434
 * Look up the value of an element's attribute.
6435
 *
6436
 * The attribute has to match the specified namespace. A namespace of
6437
 * NULL means that the attribute must have no namespace.
6438
 *
6439
 * Entities are substituted. The returned value must be freed by the
6440
 * caller.
6441
 *
6442
 * This function looks in DTD attribute declaration for \#FIXED or
6443
 * default declaration values.
6444
 *
6445
 * NOTE: This function doesn't allow to distinguish malloc failures from
6446
 * missing attributes. It's more robust to use #xmlNodeGetAttrValue.
6447
 *
6448
 * @param node  the element
6449
 * @param name  the attribute name
6450
 * @param nameSpace  the URI of the namespace
6451
 * @returns the attribute value or NULL if not found or a memory allocation
6452
 * failed.
6453
 */
6454
xmlChar *
6455
1.26k
xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6456
1.26k
    xmlAttrPtr prop;
6457
6458
1.26k
    prop = xmlGetPropNodeInternal(node, name, nameSpace, 1);
6459
1.26k
    if (prop == NULL)
6460
715
  return(NULL);
6461
553
    return(xmlGetPropNodeValueInternal(prop));
6462
1.26k
}
6463
6464
/**
6465
 * Remove an attribute of an element.
6466
 * This handles only attributes in no namespace.
6467
 *
6468
 * @param node  the element
6469
 * @param name  the attribute name
6470
 * @returns 0 if successful, -1 if not found
6471
 */
6472
int
6473
5.65k
xmlUnsetProp(xmlNode *node, const xmlChar *name) {
6474
5.65k
    xmlAttrPtr prop;
6475
6476
5.65k
    prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6477
5.65k
    if (prop == NULL)
6478
3.73k
  return(-1);
6479
1.91k
    xmlUnlinkNodeInternal((xmlNodePtr) prop);
6480
1.91k
    xmlFreeProp(prop);
6481
1.91k
    return(0);
6482
5.65k
}
6483
6484
/**
6485
 * Remove an attribute of an element.
6486
 *
6487
 * @param node  the element
6488
 * @param ns  the namespace definition
6489
 * @param name  the attribute name
6490
 * @returns 0 if successful, -1 if not found
6491
 */
6492
int
6493
78.0k
xmlUnsetNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name) {
6494
78.0k
    xmlAttrPtr prop;
6495
6496
78.0k
    prop = xmlGetPropNodeInternal(node, name,
6497
78.0k
                                  (ns != NULL) ? ns->href : NULL, 0);
6498
78.0k
    if (prop == NULL)
6499
73.7k
  return(-1);
6500
4.31k
    xmlUnlinkNodeInternal((xmlNodePtr) prop);
6501
4.31k
    xmlFreeProp(prop);
6502
4.31k
    return(0);
6503
78.0k
}
6504
6505
/**
6506
 * Set (or reset) an element's attribute. If `name` has a prefix,
6507
 * the corresponding namespace will be used. It is an error if
6508
 * there's no such binding for the prefix in scope.
6509
 *
6510
 * @param node  the node
6511
 * @param name  the attribute name (a QName)
6512
 * @param value  the attribute value
6513
 * @returns the attribute pointer.
6514
 */
6515
xmlAttr *
6516
5.12k
xmlSetProp(xmlNode *node, const xmlChar *name, const xmlChar *value) {
6517
5.12k
    xmlNsPtr ns = NULL;
6518
5.12k
    const xmlChar *localname;
6519
5.12k
    xmlChar *prefix;
6520
5.12k
    int res;
6521
6522
5.12k
    if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6523
1.91k
  return(NULL);
6524
6525
    /*
6526
     * handle QNames
6527
     */
6528
3.20k
    localname = xmlSplitQName4(name, &prefix);
6529
3.20k
    if (localname == NULL)
6530
1
        return(NULL);
6531
6532
3.20k
    if (prefix != NULL) {
6533
705
  res = xmlSearchNsSafe(node, prefix, &ns);
6534
705
  xmlFree(prefix);
6535
705
        if (res < 0)
6536
0
            return(NULL);
6537
705
        if (ns != NULL)
6538
374
            return(xmlSetNsProp(node, ns, localname, value));
6539
705
    }
6540
6541
2.83k
    return(xmlSetNsProp(node, NULL, name, value));
6542
3.20k
}
6543
6544
/**
6545
 * Set (or reset) an element's attribute.
6546
 *
6547
 * The namespace must be in scope.
6548
 *
6549
 * @param node  the node
6550
 * @param ns  the namespace definition
6551
 * @param name  the attribute name
6552
 * @param value  the attribute value
6553
 * @returns the attribute pointer.
6554
 */
6555
xmlAttr *
6556
xmlSetNsProp(xmlNode *node, xmlNs *ns, const xmlChar *name,
6557
       const xmlChar *value)
6558
41.7k
{
6559
41.7k
    xmlAttrPtr prop;
6560
6561
41.7k
    if (ns && (ns->href == NULL))
6562
204
  return(NULL);
6563
41.5k
    if (name == NULL)
6564
343
        return(NULL);
6565
41.1k
    prop = xmlGetPropNodeInternal(node, name,
6566
41.1k
                                  (ns != NULL) ? ns->href : NULL, 0);
6567
41.1k
    if (prop != NULL) {
6568
12.1k
        xmlNodePtr children = NULL;
6569
6570
  /*
6571
  * Modify the attribute's value.
6572
  */
6573
12.1k
        if (value != NULL) {
6574
11.4k
      children = xmlNewDocText(node->doc, value);
6575
11.4k
            if (children == NULL)
6576
15
                return(NULL);
6577
11.4k
        }
6578
6579
12.1k
  if (prop->id != NULL) {
6580
707
      xmlRemoveID(node->doc, prop);
6581
707
      prop->atype = XML_ATTRIBUTE_ID;
6582
707
  }
6583
12.1k
  if (prop->children != NULL)
6584
11.2k
      xmlFreeNodeList(prop->children);
6585
12.1k
  prop->children = NULL;
6586
12.1k
  prop->last = NULL;
6587
12.1k
  prop->ns = ns;
6588
12.1k
  if (value != NULL) {
6589
11.4k
      xmlNodePtr tmp;
6590
6591
11.4k
      prop->children = children;
6592
11.4k
      prop->last = NULL;
6593
11.4k
      tmp = prop->children;
6594
22.8k
      while (tmp != NULL) {
6595
11.4k
    tmp->parent = (xmlNodePtr) prop;
6596
11.4k
    if (tmp->next == NULL)
6597
11.4k
        prop->last = tmp;
6598
11.4k
    tmp = tmp->next;
6599
11.4k
      }
6600
11.4k
  }
6601
12.1k
  if ((prop->atype == XML_ATTRIBUTE_ID) &&
6602
707
      (xmlAddIDSafe(prop, value) < 0)) {
6603
1
            return(NULL);
6604
1
        }
6605
12.1k
  return(prop);
6606
12.1k
    }
6607
    /*
6608
    * No equal attr found; create a new one.
6609
    */
6610
29.0k
    return(xmlNewPropInternal(node, ns, name, value, 0));
6611
41.1k
}
6612
6613
/**
6614
 * Check whether the node is a text node.
6615
 *
6616
 * @param node  the node
6617
 * @returns 1 if yes, 0 if no
6618
 */
6619
int
6620
50.2k
xmlNodeIsText(const xmlNode *node) {
6621
50.2k
    if (node == NULL) return(0);
6622
6623
49.9k
    if (node->type == XML_TEXT_NODE) return(1);
6624
39.1k
    return(0);
6625
49.9k
}
6626
6627
/**
6628
 * Checks whether this node is an empty or whitespace-only
6629
 * text node.
6630
 *
6631
 * @param node  the node
6632
 * @returns 1 if yes, 0 if no
6633
 */
6634
int
6635
105k
xmlIsBlankNode(const xmlNode *node) {
6636
105k
    const xmlChar *cur;
6637
105k
    if (node == NULL) return(0);
6638
6639
104k
    if ((node->type != XML_TEXT_NODE) &&
6640
295
        (node->type != XML_CDATA_SECTION_NODE))
6641
295
  return(0);
6642
103k
    if (node->content == NULL) return(1);
6643
103k
    cur = node->content;
6644
250k
    while (*cur != 0) {
6645
185k
  if (!IS_BLANK_CH(*cur)) return(0);
6646
147k
  cur++;
6647
147k
    }
6648
6649
65.0k
    return(1);
6650
103k
}
6651
6652
/**
6653
 * Concat the given string at the end of the existing node content.
6654
 *
6655
 * If `len` is -1, the string length will be calculated.
6656
 *
6657
 * @param node  the node
6658
 * @param content  the content
6659
 * @param len  `content` length
6660
 * @returns -1 in case of error, 0 otherwise
6661
 */
6662
6663
int
6664
3.37k
xmlTextConcat(xmlNode *node, const xmlChar *content, int len) {
6665
3.37k
    if (node == NULL)
6666
581
        return(-1);
6667
6668
2.79k
    if ((node->type != XML_TEXT_NODE) &&
6669
1.86k
        (node->type != XML_CDATA_SECTION_NODE) &&
6670
1.65k
  (node->type != XML_COMMENT_NODE) &&
6671
1.12k
  (node->type != XML_PI_NODE))
6672
698
        return(-1);
6673
6674
2.09k
    return(xmlTextAddContent(node, content, len));
6675
2.79k
}
6676
6677
/**
6678
 * Get the compression level of a document, ZLIB based.
6679
 *
6680
 * @param doc  the document
6681
 * @returns 0 (uncompressed) to 9 (max compression)
6682
 */
6683
int
6684
0
xmlGetDocCompressMode (const xmlDoc *doc) {
6685
0
    if (doc == NULL) return(-1);
6686
0
    return(doc->compression);
6687
0
}
6688
6689
/**
6690
 * Set the compression level of a document, ZLIB based.
6691
 *
6692
 * Correct values: 0 (uncompressed) to 9 (max compression)
6693
 *
6694
 * @param doc  the document
6695
 * @param mode  the compression ratio
6696
 */
6697
void
6698
0
xmlSetDocCompressMode (xmlDoc *doc, int mode) {
6699
0
    if (doc == NULL) return;
6700
0
    if (mode < 0) doc->compression = 0;
6701
0
    else if (mode > 9) doc->compression = 9;
6702
0
    else doc->compression = mode;
6703
0
}
6704
6705
/**
6706
 * Get the global compression level, ZLIB based.
6707
 *
6708
 * @deprecated Use #xmlGetDocCompressMode
6709
 *
6710
 * @returns 0 (uncompressed) to 9 (max compression)
6711
 */
6712
int
6713
xmlGetCompressMode(void)
6714
0
{
6715
0
    return (xmlCompressMode);
6716
0
}
6717
6718
/**
6719
 * Set the global compression level, ZLIB based.
6720
 *
6721
 * @deprecated Use #xmlSetDocCompressMode
6722
 *
6723
 * Correct values: 0 (uncompressed) to 9 (max compression)
6724
 *
6725
 * @param mode  the compression ratio
6726
 */
6727
void
6728
0
xmlSetCompressMode(int mode) {
6729
0
    if (mode < 0) xmlCompressMode = 0;
6730
0
    else if (mode > 9) xmlCompressMode = 9;
6731
0
    else xmlCompressMode = mode;
6732
0
}
6733
6734
965k
#define XML_TREE_NSMAP_PARENT -1
6735
#define XML_TREE_NSMAP_XML -2
6736
891
#define XML_TREE_NSMAP_DOC -3
6737
0
#define XML_TREE_NSMAP_CUSTOM -4
6738
6739
typedef struct xmlNsMapItem *xmlNsMapItemPtr;
6740
struct xmlNsMapItem {
6741
    xmlNsMapItemPtr next;
6742
    xmlNsMapItemPtr prev;
6743
    xmlNsPtr oldNs; /* old ns decl reference */
6744
    xmlNsPtr newNs; /* new ns decl reference */
6745
    int shadowDepth; /* Shadowed at this depth */
6746
    /*
6747
    * depth:
6748
    * >= 0 == @node's ns-decls
6749
    * -1   == @parent's ns-decls
6750
    * -2   == the doc->oldNs XML ns-decl
6751
    * -3   == the doc->oldNs storage ns-decls
6752
    * -4   == ns-decls provided via custom ns-handling
6753
    */
6754
    int depth;
6755
};
6756
6757
typedef struct xmlNsMap *xmlNsMapPtr;
6758
struct xmlNsMap {
6759
    xmlNsMapItemPtr first;
6760
    xmlNsMapItemPtr last;
6761
    xmlNsMapItemPtr pool;
6762
};
6763
6764
230k
#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
6765
1.67M
#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
6766
#define XML_NSMAP_POP(m, i) \
6767
39.7k
    i = (m)->last; \
6768
39.7k
    (m)->last = (i)->prev; \
6769
39.7k
    if ((m)->last == NULL) \
6770
39.7k
  (m)->first = NULL; \
6771
39.7k
    else \
6772
39.7k
  (m)->last->next = NULL; \
6773
39.7k
    (i)->next = (m)->pool; \
6774
39.7k
    (m)->pool = i;
6775
6776
/**
6777
 * Frees the ns-map
6778
 *
6779
 * @param nsmap  the ns-map
6780
 */
6781
static void
6782
xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
6783
10.9k
{
6784
10.9k
    xmlNsMapItemPtr cur, tmp;
6785
6786
10.9k
    if (nsmap == NULL)
6787
0
  return;
6788
10.9k
    cur = nsmap->pool;
6789
22.7k
    while (cur != NULL) {
6790
11.7k
  tmp = cur;
6791
11.7k
  cur = cur->next;
6792
11.7k
  xmlFree(tmp);
6793
11.7k
    }
6794
10.9k
    cur = nsmap->first;
6795
44.5k
    while (cur != NULL) {
6796
33.6k
  tmp = cur;
6797
33.6k
  cur = cur->next;
6798
33.6k
  xmlFree(tmp);
6799
33.6k
    }
6800
10.9k
    xmlFree(nsmap);
6801
10.9k
}
6802
6803
/**
6804
 * Adds an ns-mapping item.
6805
 *
6806
 * @param nsmap  the ns-map
6807
 * @param position  position
6808
 * @param oldNs  the old ns-struct
6809
 * @param newNs  the new ns-struct
6810
 * @param depth  depth and ns-kind information
6811
 * @returns the added item.
6812
 */
6813
static xmlNsMapItemPtr
6814
xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
6815
           xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
6816
73.3k
{
6817
73.3k
    xmlNsMapItemPtr ret;
6818
73.3k
    xmlNsMapPtr map;
6819
6820
73.3k
    if (nsmap == NULL)
6821
0
  return(NULL);
6822
73.3k
    if ((position != -1) && (position != 0))
6823
0
  return(NULL);
6824
73.3k
    map = *nsmap;
6825
6826
73.3k
    if (map == NULL) {
6827
  /*
6828
  * Create the ns-map.
6829
  */
6830
10.9k
  map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
6831
10.9k
  if (map == NULL)
6832
12
      return(NULL);
6833
10.9k
  memset(map, 0, sizeof(struct xmlNsMap));
6834
10.9k
  *nsmap = map;
6835
10.9k
    }
6836
6837
73.3k
    if (map->pool != NULL) {
6838
  /*
6839
  * Reuse an item from the pool.
6840
  */
6841
27.9k
  ret = map->pool;
6842
27.9k
  map->pool = ret->next;
6843
27.9k
  memset(ret, 0, sizeof(struct xmlNsMapItem));
6844
45.4k
    } else {
6845
  /*
6846
  * Create a new item.
6847
  */
6848
45.4k
  ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
6849
45.4k
  if (ret == NULL)
6850
24
      return(NULL);
6851
45.3k
  memset(ret, 0, sizeof(struct xmlNsMapItem));
6852
45.3k
    }
6853
6854
73.3k
    if (map->first == NULL) {
6855
  /*
6856
  * First ever.
6857
  */
6858
13.4k
  map->first = ret;
6859
13.4k
  map->last = ret;
6860
59.8k
    } else if (position == -1) {
6861
  /*
6862
  * Append.
6863
  */
6864
42.8k
  ret->prev = map->last;
6865
42.8k
  map->last->next = ret;
6866
42.8k
  map->last = ret;
6867
42.8k
    } else if (position == 0) {
6868
  /*
6869
  * Set on first position.
6870
  */
6871
16.9k
  map->first->prev = ret;
6872
16.9k
  ret->next = map->first;
6873
16.9k
  map->first = ret;
6874
16.9k
    }
6875
6876
73.3k
    ret->oldNs = oldNs;
6877
73.3k
    ret->newNs = newNs;
6878
73.3k
    ret->shadowDepth = -1;
6879
73.3k
    ret->depth = depth;
6880
73.3k
    return (ret);
6881
73.3k
}
6882
6883
/**
6884
 * Creates or reuses an xmlNs struct on doc->oldNs with
6885
 * the given prefix and namespace name.
6886
 *
6887
 * @param doc  the doc
6888
 * @param nsName  the namespace name
6889
 * @param prefix  the prefix
6890
 * @returns the acquired ns struct or NULL in case of an API
6891
 *          or internal error.
6892
 */
6893
static xmlNsPtr
6894
xmlDOMWrapStoreNs(xmlDocPtr doc,
6895
       const xmlChar *nsName,
6896
       const xmlChar *prefix)
6897
5.31k
{
6898
5.31k
    xmlNsPtr ns;
6899
6900
5.31k
    if (doc == NULL)
6901
0
  return (NULL);
6902
5.31k
    ns = xmlTreeEnsureXMLDecl(doc);
6903
5.31k
    if (ns == NULL)
6904
3
  return (NULL);
6905
5.31k
    if (ns->next != NULL) {
6906
  /* Reuse. */
6907
4.84k
  ns = ns->next;
6908
151k
  while (ns != NULL) {
6909
151k
      if (((ns->prefix == prefix) ||
6910
150k
    xmlStrEqual(ns->prefix, prefix)) &&
6911
6.40k
    xmlStrEqual(ns->href, nsName)) {
6912
3.66k
    return (ns);
6913
3.66k
      }
6914
148k
      if (ns->next == NULL)
6915
1.18k
    break;
6916
147k
      ns = ns->next;
6917
147k
  }
6918
4.84k
    }
6919
    /* Create. */
6920
1.65k
    if (ns != NULL) {
6921
1.65k
        ns->next = xmlNewNs(NULL, nsName, prefix);
6922
1.65k
        return (ns->next);
6923
1.65k
    }
6924
0
    return(NULL);
6925
1.65k
}
6926
6927
/**
6928
 * Allocates and initializes a new DOM-wrapper context.
6929
 *
6930
 * @returns the xmlDOMWrapCtxt or NULL in case of an internal error.
6931
 */
6932
xmlDOMWrapCtxt *
6933
xmlDOMWrapNewCtxt(void)
6934
31.1k
{
6935
31.1k
    xmlDOMWrapCtxtPtr ret;
6936
6937
31.1k
    ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
6938
31.1k
    if (ret == NULL)
6939
52
  return (NULL);
6940
31.1k
    memset(ret, 0, sizeof(xmlDOMWrapCtxt));
6941
31.1k
    return (ret);
6942
31.1k
}
6943
6944
/**
6945
 * Frees the DOM-wrapper context.
6946
 *
6947
 * @param ctxt  the DOM-wrapper context
6948
 */
6949
void
6950
xmlDOMWrapFreeCtxt(xmlDOMWrapCtxt *ctxt)
6951
31.1k
{
6952
31.1k
    if (ctxt == NULL)
6953
52
  return;
6954
31.1k
    if (ctxt->namespaceMap != NULL)
6955
0
  xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
6956
    /*
6957
    * TODO: Store the namespace map in the context.
6958
    */
6959
31.1k
    xmlFree(ctxt);
6960
31.1k
}
6961
6962
/**
6963
 * Searches for a ns-decl with the given prefix in `nsList`.
6964
 *
6965
 * @param nsList  a list of ns-structs
6966
 * @param prefix  the searched prefix
6967
 * @returns the ns-decl if found, NULL if not found and on
6968
 *          API errors.
6969
 */
6970
static xmlNsPtr
6971
xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
6972
55.2k
{
6973
55.2k
    if (nsList == NULL)
6974
0
  return (NULL);
6975
55.2k
    {
6976
55.2k
  xmlNsPtr ns;
6977
55.2k
  ns = nsList;
6978
1.92M
  do {
6979
1.92M
      if ((prefix == ns->prefix) ||
6980
1.92M
    xmlStrEqual(prefix, ns->prefix)) {
6981
51.3k
    return (ns);
6982
51.3k
      }
6983
1.87M
      ns = ns->next;
6984
1.87M
  } while (ns != NULL);
6985
55.2k
    }
6986
3.88k
    return (NULL);
6987
55.2k
}
6988
6989
/**
6990
 * Puts in-scope namespaces into the ns-map.
6991
 *
6992
 * @param map  the namespace map
6993
 * @param node  the node to start with
6994
 * @returns 0 on success, -1 on API or internal errors.
6995
 */
6996
static int
6997
xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
6998
        xmlNodePtr node)
6999
2.97k
{
7000
2.97k
    xmlNodePtr cur;
7001
2.97k
    xmlNsPtr ns;
7002
2.97k
    xmlNsMapItemPtr mi;
7003
2.97k
    int shadowed;
7004
7005
2.97k
    if ((map == NULL) || (*map != NULL))
7006
0
  return (-1);
7007
2.97k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
7008
0
        return (-1);
7009
    /*
7010
    * Get in-scope ns-decls of @parent.
7011
    */
7012
2.97k
    cur = node;
7013
23.5k
    while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7014
20.6k
  if (cur->type == XML_ELEMENT_NODE) {
7015
20.4k
      if (cur->nsDef != NULL) {
7016
17.4k
    ns = cur->nsDef;
7017
19.1k
    do {
7018
19.1k
        shadowed = 0;
7019
19.1k
        if (XML_NSMAP_NOTEMPTY(*map)) {
7020
      /*
7021
      * Skip shadowed prefixes.
7022
      */
7023
32.6k
      XML_NSMAP_FOREACH(*map, mi) {
7024
32.6k
          if ((ns->prefix == mi->newNs->prefix) ||
7025
31.7k
        xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7026
15.7k
        shadowed = 1;
7027
15.7k
        break;
7028
15.7k
          }
7029
32.6k
      }
7030
16.9k
        }
7031
        /*
7032
        * Insert mapping.
7033
        */
7034
19.1k
        mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
7035
19.1k
      ns, XML_TREE_NSMAP_PARENT);
7036
19.1k
        if (mi == NULL)
7037
8
      return (-1);
7038
19.1k
        if (shadowed)
7039
15.7k
      mi->shadowDepth = 0;
7040
19.1k
        ns = ns->next;
7041
19.1k
    } while (ns != NULL);
7042
17.4k
      }
7043
20.4k
  }
7044
20.6k
  cur = cur->parent;
7045
20.6k
    }
7046
2.96k
    return (0);
7047
2.97k
}
7048
7049
/*
7050
 * For internal use. Adds a ns-decl mapping.
7051
 *
7052
 * Returns 0 on success, -1 on internal errors.
7053
 */
7054
static int
7055
xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7056
      xmlNsPtr oldNs, xmlNsPtr newNs)
7057
153k
{
7058
153k
    if (*number >= *size) {
7059
7.60k
        xmlNsPtr *tmp;
7060
7.60k
        int newSize;
7061
7062
7.60k
        newSize = xmlGrowCapacity(*size, 2 * sizeof(tmp[0]), 3, XML_MAX_ITEMS);
7063
7.60k
        if (newSize < 0)
7064
0
            return(-1);
7065
7.60k
        tmp = xmlRealloc(*list, newSize * 2 * sizeof(tmp[0]));
7066
7.60k
        if (tmp == NULL)
7067
6
            return(-1);
7068
7.60k
        *list = tmp;
7069
7.60k
        *size = newSize;
7070
7.60k
    }
7071
7072
153k
    (*list)[2 * (*number)] = oldNs;
7073
153k
    (*list)[2 * (*number) +1] = newNs;
7074
153k
    (*number)++;
7075
153k
    return (0);
7076
153k
}
7077
7078
/**
7079
 * Unlinks the given node from its owner.
7080
 *
7081
 * This will substitute ns-references to node->nsDef for
7082
 * ns-references to doc->oldNs, thus ensuring the removed
7083
 * branch to be autark wrt ns-references.
7084
 *
7085
 * NOTE: This function was not intensively tested.
7086
 *
7087
 * @param ctxt  a DOM wrapper context
7088
 * @param doc  the doc
7089
 * @param node  the node to be removed.
7090
 * @param options  set of options, unused at the moment
7091
 * @returns 0 on success, 1 if the node is not supported,
7092
 *          -1 on API and internal errors.
7093
 */
7094
int
7095
xmlDOMWrapRemoveNode(xmlDOMWrapCtxt *ctxt, xmlDoc *doc,
7096
         xmlNode *node, int options ATTRIBUTE_UNUSED)
7097
5.46k
{
7098
5.46k
    xmlNsPtr *list = NULL;
7099
5.46k
    int sizeList = 0, nbList = 0, ret = 0, i, j;
7100
5.46k
    xmlNsPtr ns;
7101
7102
5.46k
    if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7103
1.19k
  return (-1);
7104
7105
    /* TODO: 0 or -1 ? */
7106
4.26k
    if (node->parent == NULL)
7107
670
  return (0);
7108
7109
3.59k
    switch (node->type) {
7110
200
  case XML_TEXT_NODE:
7111
276
  case XML_CDATA_SECTION_NODE:
7112
475
  case XML_ENTITY_REF_NODE:
7113
669
  case XML_PI_NODE:
7114
923
  case XML_COMMENT_NODE:
7115
923
      xmlUnlinkNodeInternal(node);
7116
923
      return (0);
7117
1.64k
  case XML_ELEMENT_NODE:
7118
2.10k
  case XML_ATTRIBUTE_NODE:
7119
2.10k
      break;
7120
562
  default:
7121
562
      return (1);
7122
3.59k
    }
7123
2.10k
    xmlUnlinkNodeInternal(node);
7124
    /*
7125
    * Save out-of-scope ns-references in doc->oldNs.
7126
    */
7127
21.3k
    do {
7128
21.3k
  switch (node->type) {
7129
11.2k
      case XML_ELEMENT_NODE:
7130
11.2k
    if ((ctxt == NULL) && (node->nsDef != NULL)) {
7131
7.68k
        ns = node->nsDef;
7132
134k
        do {
7133
134k
      if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7134
134k
          &nbList, ns, ns) == -1)
7135
3
          ret = -1;
7136
134k
      ns = ns->next;
7137
134k
        } while (ns != NULL);
7138
7.68k
    }
7139
                /* Falls through. */
7140
15.0k
      case XML_ATTRIBUTE_NODE:
7141
15.0k
    if (node->ns != NULL) {
7142
        /*
7143
        * Find a mapping.
7144
        */
7145
10.0k
        if (list != NULL) {
7146
5.31M
      for (i = 0, j = 0; i < nbList; i++, j += 2) {
7147
5.31M
          if (node->ns == list[j]) {
7148
5.86k
        node->ns = list[++j];
7149
5.86k
        goto next_node;
7150
5.86k
          }
7151
5.31M
      }
7152
9.42k
        }
7153
4.18k
        ns = NULL;
7154
4.18k
        if (ctxt != NULL) {
7155
      /*
7156
      * User defined.
7157
      */
7158
4.18k
        } else {
7159
      /*
7160
      * Add to doc's oldNs.
7161
      */
7162
4.18k
      ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7163
4.18k
          node->ns->prefix);
7164
4.18k
      if (ns == NULL)
7165
14
          ret = -1;
7166
4.18k
        }
7167
4.18k
        if (ns != NULL) {
7168
      /*
7169
      * Add mapping.
7170
      */
7171
4.17k
      if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7172
4.17k
          &nbList, node->ns, ns) == -1)
7173
2
          ret = -1;
7174
4.17k
        }
7175
4.18k
        node->ns = ns;
7176
4.18k
    }
7177
9.13k
    if ((node->type == XML_ELEMENT_NODE) &&
7178
7.49k
        (node->properties != NULL)) {
7179
2.50k
        node = (xmlNodePtr) node->properties;
7180
2.50k
        continue;
7181
2.50k
    }
7182
6.62k
    break;
7183
6.62k
      default:
7184
6.33k
    goto next_sibling;
7185
21.3k
  }
7186
12.4k
next_node:
7187
12.4k
  if ((node->type == XML_ELEMENT_NODE) &&
7188
8.72k
      (node->children != NULL)) {
7189
6.95k
      node = node->children;
7190
6.95k
      continue;
7191
6.95k
  }
7192
23.4k
next_sibling:
7193
23.4k
  if (node == NULL)
7194
2.10k
      break;
7195
21.3k
  if (node->next != NULL)
7196
8.14k
      node = node->next;
7197
13.1k
  else {
7198
13.1k
            int type = node->type;
7199
7200
13.1k
      node = node->parent;
7201
13.1k
            if ((type == XML_ATTRIBUTE_NODE) &&
7202
2.97k
                (node != NULL) &&
7203
2.50k
                (node->children != NULL)) {
7204
1.62k
                node = node->children;
7205
11.5k
            } else {
7206
11.5k
          goto next_sibling;
7207
11.5k
            }
7208
13.1k
  }
7209
21.3k
    } while (node != NULL);
7210
7211
2.10k
    if (list != NULL)
7212
1.34k
  xmlFree(list);
7213
2.10k
    return (ret);
7214
2.10k
}
7215
7216
/**
7217
 * Dynamically searches for a ns-declaration which matches
7218
 * the given `nsName` in the ancestor-or-self axis of `node`.
7219
 *
7220
 * @param doc  the document
7221
 * @param node  the start node
7222
 * @param nsName  the searched namespace name
7223
 * @param retNs  the resulting ns-decl
7224
 * @param prefixed  if the found ns-decl must have a prefix
7225
 *                  (for attributes)
7226
 * @returns 1 if a ns-decl was found, 0 if not and -1 on API
7227
 *          and internal errors.
7228
 */
7229
static int
7230
xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
7231
           const xmlChar* nsName,
7232
           xmlNsPtr *retNs, int prefixed)
7233
3.57k
{
7234
3.57k
    xmlNodePtr cur, prev = NULL, out = NULL;
7235
3.57k
    xmlNsPtr ns, prevns;
7236
7237
3.57k
    if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7238
1.29k
  return (-1);
7239
2.27k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
7240
0
        return(-1);
7241
7242
2.27k
    *retNs = NULL;
7243
2.27k
    if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7244
655
  *retNs = xmlTreeEnsureXMLDecl(doc);
7245
655
  if (*retNs == NULL)
7246
1
      return (-1);
7247
654
  return (1);
7248
655
    }
7249
1.61k
    cur = node;
7250
5.73k
    do {
7251
5.73k
  if (cur->type == XML_ELEMENT_NODE) {
7252
5.42k
      if (cur->nsDef != NULL) {
7253
9.15k
    for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7254
5.96k
        if (prefixed && (ns->prefix == NULL))
7255
2.07k
      continue;
7256
3.89k
        if (prev != NULL) {
7257
      /*
7258
      * Check the last level of ns-decls for a
7259
      * shadowing prefix.
7260
      */
7261
2.84k
      prevns = prev->nsDef;
7262
3.40k
      do {
7263
3.40k
          if ((prevns->prefix == ns->prefix) ||
7264
3.40k
        ((prevns->prefix != NULL) &&
7265
1.64k
        (ns->prefix != NULL) &&
7266
1.64k
        xmlStrEqual(prevns->prefix, ns->prefix))) {
7267
        /*
7268
        * Shadowed.
7269
        */
7270
905
        break;
7271
905
          }
7272
2.50k
          prevns = prevns->next;
7273
2.50k
      } while (prevns != NULL);
7274
2.84k
      if (prevns != NULL)
7275
905
          continue;
7276
2.84k
        }
7277
        /*
7278
        * Ns-name comparison.
7279
        */
7280
2.99k
        if ((nsName == ns->href) ||
7281
2.99k
      xmlStrEqual(nsName, ns->href)) {
7282
      /*
7283
      * At this point the prefix can only be shadowed,
7284
      * if we are the the (at least) 3rd level of
7285
      * ns-decls.
7286
      */
7287
1.79k
      if (out) {
7288
1.04k
          int ret;
7289
7290
1.04k
          ret = xmlNsInScope(doc, node, prev, ns->prefix);
7291
1.04k
          if (ret < 0)
7292
0
        return (-1);
7293
          /*
7294
          * TODO: Should we try to find a matching ns-name
7295
          * only once? This here keeps on searching.
7296
          * I think we should try further since, there might
7297
          * be an other matching ns-decl with an unshadowed
7298
          * prefix.
7299
          */
7300
1.04k
          if (! ret)
7301
575
        continue;
7302
1.04k
      }
7303
1.22k
      *retNs = ns;
7304
1.22k
      return (1);
7305
1.79k
        }
7306
2.99k
    }
7307
3.18k
    out = prev;
7308
3.18k
    prev = cur;
7309
3.18k
      }
7310
5.42k
  } else if (cur->type == XML_ENTITY_DECL)
7311
0
      return (0);
7312
4.51k
  cur = cur->parent;
7313
4.51k
    } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7314
396
    return (0);
7315
1.61k
}
7316
7317
/**
7318
 * Dynamically searches for a ns-declaration which matches
7319
 * the given `nsName` in the ancestor-or-self axis of `node`.
7320
 *
7321
 * @param doc  the document
7322
 * @param node  the start node
7323
 * @param prefix  the searched namespace prefix
7324
 * @param retNs  the resulting ns-decl
7325
 * @returns 1 if a ns-decl was found, 0 if not and -1 on API
7326
 *          and internal errors.
7327
 */
7328
static int
7329
xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
7330
        const xmlChar* prefix,
7331
        xmlNsPtr *retNs)
7332
1.55k
{
7333
1.55k
    xmlNodePtr cur;
7334
1.55k
    xmlNsPtr ns;
7335
7336
1.55k
    if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
7337
0
        return(-1);
7338
7339
1.55k
    if (retNs)
7340
0
  *retNs = NULL;
7341
1.55k
    if (IS_STR_XML(prefix)) {
7342
0
  if (retNs) {
7343
0
      *retNs = xmlTreeEnsureXMLDecl(doc);
7344
0
      if (*retNs == NULL)
7345
0
    return (-1);
7346
0
  }
7347
0
  return (1);
7348
0
    }
7349
1.55k
    cur = node;
7350
2.41k
    do {
7351
2.41k
  if (cur->type == XML_ELEMENT_NODE) {
7352
2.01k
      if (cur->nsDef != NULL) {
7353
1.08k
    ns = cur->nsDef;
7354
1.69k
    do {
7355
1.69k
        if ((prefix == ns->prefix) ||
7356
1.66k
      xmlStrEqual(prefix, ns->prefix))
7357
393
        {
7358
      /*
7359
      * Disabled namespaces, e.g. xmlns:abc="".
7360
      */
7361
393
      if (ns->href == NULL)
7362
85
          return(0);
7363
308
      if (retNs)
7364
0
          *retNs = ns;
7365
308
      return (1);
7366
393
        }
7367
1.30k
        ns = ns->next;
7368
1.30k
    } while (ns != NULL);
7369
1.08k
      }
7370
2.01k
  } else if (cur->type == XML_ENTITY_DECL)
7371
0
      return (0);
7372
2.02k
  cur = cur->parent;
7373
2.02k
    } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7374
1.16k
    return (0);
7375
1.55k
}
7376
7377
/**
7378
 * Declares a new namespace on `elem`. It tries to use the
7379
 * given `prefix`. If a ns-decl with the given prefix is already existent
7380
 * on `elem`, it will generate an other prefix.
7381
 *
7382
 * @param doc  the doc
7383
 * @param elem  the element-node to declare on
7384
 * @param nsName  the namespace-name of the ns-decl
7385
 * @param prefix  the preferred prefix of the ns-decl
7386
 * @param checkShadow  ensure that the new ns-decl doesn't shadow
7387
 *                     ancestor ns-decls
7388
 * @returns 1 if a ns-decl was found, 0 if not and -1 on API
7389
 *          and internal errors.
7390
 */
7391
static xmlNsPtr
7392
xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7393
        xmlNodePtr elem,
7394
        const xmlChar *nsName,
7395
        const xmlChar *prefix,
7396
        int checkShadow)
7397
8.98k
{
7398
7399
8.98k
    xmlNsPtr ret;
7400
8.98k
    char buf[50];
7401
8.98k
    const xmlChar *pref;
7402
8.98k
    int counter = 0;
7403
7404
8.98k
    if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
7405
0
        return(NULL);
7406
    /*
7407
    * Create a ns-decl on @anchor.
7408
    */
7409
8.98k
    pref = prefix;
7410
60.6k
    while (1) {
7411
  /*
7412
  * Lookup whether the prefix is unused in elem's ns-decls.
7413
  */
7414
60.6k
  if ((elem->nsDef != NULL) &&
7415
55.2k
      (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7416
51.3k
      goto ns_next_prefix;
7417
9.29k
  if (checkShadow && elem->parent &&
7418
1.77k
      ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7419
      /*
7420
      * Does it shadow ancestor ns-decls?
7421
      */
7422
1.55k
      if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
7423
308
    goto ns_next_prefix;
7424
1.55k
  }
7425
8.98k
  ret = xmlNewNs(NULL, nsName, pref);
7426
8.98k
  if (ret == NULL)
7427
15
      return (NULL);
7428
8.97k
  if (elem->nsDef == NULL)
7429
5.35k
      elem->nsDef = ret;
7430
3.62k
  else {
7431
3.62k
      xmlNsPtr ns2 = elem->nsDef;
7432
52.4k
      while (ns2->next != NULL)
7433
48.7k
    ns2 = ns2->next;
7434
3.62k
      ns2->next = ret;
7435
3.62k
  }
7436
8.97k
  return (ret);
7437
51.6k
ns_next_prefix:
7438
51.6k
  counter++;
7439
51.6k
  if (counter > 1000)
7440
0
      return (NULL);
7441
51.6k
  if (prefix == NULL) {
7442
3.14k
      snprintf((char *) buf, sizeof(buf),
7443
3.14k
    "ns_%d", counter);
7444
3.14k
  } else
7445
48.5k
      snprintf((char *) buf, sizeof(buf),
7446
48.5k
      "%.30s_%d", (char *)prefix, counter);
7447
51.6k
  pref = BAD_CAST buf;
7448
51.6k
    }
7449
8.98k
}
7450
7451
/**
7452
 * Searches for a matching ns-name in the ns-decls of `nsMap`, if not
7453
 * found it will either declare it on `elem`, or store it in `doc->oldNs`.
7454
 * If a new ns-decl needs to be declared on `elem`, it tries to use the
7455
 * `ns->prefix` for it, if this prefix is already in use on `elem`, it will
7456
 * change the prefix or the new ns-decl.
7457
 *
7458
 * @param doc  the doc
7459
 * @param elem  the element-node to declare namespaces on
7460
 * @param ns  the ns-struct to use for the search
7461
 * @param retNs  the found/created ns-struct
7462
 * @param nsMap  the ns-map
7463
 * @param depth  the current tree depth
7464
 * @param ancestorsOnly  search in ancestor ns-decls only
7465
 * @param prefixed  if the searched ns-decl must have a prefix
7466
 *                  (for attributes)
7467
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
7468
 */
7469
static int
7470
xmlDOMWrapNSNormAcquireNormalizedNs(xmlDocPtr doc,
7471
           xmlNodePtr elem,
7472
           xmlNsPtr ns,
7473
           xmlNsPtr *retNs,
7474
           xmlNsMapPtr *nsMap,
7475
7476
           int depth,
7477
           int ancestorsOnly,
7478
           int prefixed)
7479
28.6k
{
7480
28.6k
    xmlNsMapItemPtr mi;
7481
7482
28.6k
    if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
7483
28.6k
  (nsMap == NULL))
7484
0
  return (-1);
7485
7486
28.6k
    *retNs = NULL;
7487
    /*
7488
    * Handle XML namespace.
7489
    */
7490
28.6k
    if (IS_STR_XML(ns->prefix)) {
7491
  /*
7492
  * Insert XML namespace mapping.
7493
  */
7494
18.9k
  *retNs = xmlTreeEnsureXMLDecl(doc);
7495
18.9k
  if (*retNs == NULL)
7496
1
      return (-1);
7497
18.9k
  return (0);
7498
18.9k
    }
7499
    /*
7500
    * If the search should be done in ancestors only and no
7501
    * @elem (the first ancestor) was specified, then skip the search.
7502
    */
7503
9.75k
    if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
7504
8.30k
  (! (ancestorsOnly && (elem == NULL))))
7505
8.30k
    {
7506
  /*
7507
  * Try to find an equal ns-name in in-scope ns-decls.
7508
  */
7509
54.9k
  XML_NSMAP_FOREACH(*nsMap, mi) {
7510
54.9k
      if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7511
    /*
7512
    * ancestorsOnly: This should be turned on to gain speed,
7513
    * if one knows that the branch itself was already
7514
    * ns-wellformed and no stale references existed.
7515
    * I.e. it searches in the ancestor axis only.
7516
    */
7517
52.3k
    ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
7518
    /* Skip shadowed prefixes. */
7519
52.3k
    (mi->shadowDepth == -1) &&
7520
    /* Skip xmlns="" or xmlns:foo="". */
7521
17.8k
    ((mi->newNs->href != NULL) &&
7522
16.5k
    (mi->newNs->href[0] != 0)) &&
7523
    /* Ensure a prefix if wanted. */
7524
8.34k
    ((! prefixed) || (mi->newNs->prefix != NULL)) &&
7525
    /* Equal ns name */
7526
7.77k
    ((mi->newNs->href == ns->href) ||
7527
7.50k
    xmlStrEqual(mi->newNs->href, ns->href))) {
7528
    /* Set the mapping. */
7529
1.56k
    mi->oldNs = ns;
7530
1.56k
    *retNs = mi->newNs;
7531
1.56k
    return (0);
7532
1.56k
      }
7533
54.9k
  }
7534
8.30k
    }
7535
    /*
7536
    * No luck, the namespace is out of scope or shadowed.
7537
    */
7538
8.18k
    if (elem == NULL) {
7539
893
  xmlNsPtr tmpns;
7540
7541
  /*
7542
  * Store ns-decls in "oldNs" of the document-node.
7543
  */
7544
893
  tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
7545
893
  if (tmpns == NULL)
7546
2
      return (-1);
7547
  /*
7548
  * Insert mapping.
7549
  */
7550
891
  if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
7551
891
    tmpns, XML_TREE_NSMAP_DOC) == NULL) {
7552
4
      return (-1);
7553
4
  }
7554
887
  *retNs = tmpns;
7555
7.29k
    } else {
7556
7.29k
  xmlNsPtr tmpns;
7557
7558
7.29k
  tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
7559
7.29k
      ns->prefix, 0);
7560
7.29k
  if (tmpns == NULL)
7561
15
      return (-1);
7562
7563
7.27k
  if (*nsMap != NULL) {
7564
      /*
7565
      * Does it shadow ancestor ns-decls?
7566
      */
7567
31.8k
      XML_NSMAP_FOREACH(*nsMap, mi) {
7568
31.8k
    if ((mi->depth < depth) &&
7569
27.6k
        (mi->shadowDepth == -1) &&
7570
5.60k
        ((ns->prefix == mi->newNs->prefix) ||
7571
4.20k
        xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7572
        /*
7573
        * Shadows.
7574
        */
7575
4.20k
        mi->shadowDepth = depth;
7576
4.20k
        break;
7577
4.20k
    }
7578
31.8k
      }
7579
6.50k
  }
7580
7.27k
  if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
7581
4
      return (-1);
7582
4
  }
7583
7.27k
  *retNs = tmpns;
7584
7.27k
    }
7585
8.16k
    return (0);
7586
8.18k
}
7587
7588
typedef enum {
7589
    XML_DOM_RECONNS_REMOVEREDUND = 1<<0
7590
} xmlDOMReconcileNSOptions;
7591
7592
/**
7593
 * Fix up namespaces.
7594
 *
7595
 * Ensures that ns-references point to ns-decls hold on element-nodes.
7596
 * Ensures that the tree is namespace wellformed by creating additional
7597
 * ns-decls where needed. Note that, since prefixes of already existent
7598
 * ns-decls can be shadowed by this process, it could break QNames in
7599
 * attribute values or element content.
7600
 *
7601
 * NOTE: This function was not intensively tested.
7602
 *
7603
 * @param ctxt  DOM wrapper context, unused at the moment
7604
 * @param elem  the element-node
7605
 * @param options  option flags
7606
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
7607
 */
7608
int
7609
xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxt *ctxt ATTRIBUTE_UNUSED,
7610
            xmlNode *elem,
7611
            int options)
7612
4.45k
{
7613
4.45k
    int depth = -1, adoptns = 0, parnsdone = 0;
7614
4.45k
    xmlNsPtr ns, prevns;
7615
4.45k
    xmlDocPtr doc;
7616
4.45k
    xmlNodePtr cur, curElem = NULL;
7617
4.45k
    xmlNsMapPtr nsMap = NULL;
7618
4.45k
    xmlNsMapItemPtr /* topmi = NULL, */ mi;
7619
    /* @ancestorsOnly should be set by an option flag. */
7620
4.45k
    int ancestorsOnly = 0;
7621
4.45k
    int optRemoveRedundantNS =
7622
4.45k
  ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
7623
4.45k
    xmlNsPtr *listRedund = NULL;
7624
4.45k
    int sizeRedund = 0, nbRedund = 0, ret = 0, i, j;
7625
7626
4.45k
    if ((elem == NULL) || (elem->doc == NULL) ||
7627
3.87k
  (elem->type != XML_ELEMENT_NODE))
7628
920
  return (-1);
7629
7630
3.53k
    doc = elem->doc;
7631
3.53k
    cur = elem;
7632
23.1k
    do {
7633
23.1k
  switch (cur->type) {
7634
17.4k
      case XML_ELEMENT_NODE:
7635
17.4k
    adoptns = 1;
7636
17.4k
    curElem = cur;
7637
17.4k
    depth++;
7638
    /*
7639
    * Namespace declarations.
7640
    */
7641
17.4k
    if (cur->nsDef != NULL) {
7642
7.42k
        prevns = NULL;
7643
7.42k
        ns = cur->nsDef;
7644
27.5k
        while (ns != NULL) {
7645
20.0k
      if (! parnsdone) {
7646
2.02k
          if ((elem->parent) &&
7647
1.65k
        ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7648
        /*
7649
        * Gather ancestor in-scope ns-decls.
7650
        */
7651
1.14k
        if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7652
1.14k
            elem->parent) == -1)
7653
2
            ret = -1;
7654
1.14k
          }
7655
2.02k
          parnsdone = 1;
7656
2.02k
      }
7657
7658
      /*
7659
      * Lookup the ns ancestor-axis for equal ns-decls in scope.
7660
      */
7661
20.0k
      if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
7662
595k
          XML_NSMAP_FOREACH(nsMap, mi) {
7663
595k
        if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7664
595k
            (mi->shadowDepth == -1) &&
7665
300k
            ((ns->prefix == mi->newNs->prefix) ||
7666
297k
              xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
7667
15.2k
            ((ns->href == mi->newNs->href) ||
7668
11.8k
              xmlStrEqual(ns->href, mi->newNs->href)))
7669
14.6k
        {
7670
            /*
7671
            * A redundant ns-decl was found.
7672
            * Add it to the list of redundant ns-decls.
7673
            */
7674
14.6k
            if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
7675
14.6k
          &sizeRedund, &nbRedund, ns, mi->newNs) == -1) {
7676
1
          ret = -1;
7677
14.6k
                                    } else {
7678
                                        /*
7679
                                        * Remove the ns-decl from the element-node.
7680
                                        */
7681
14.6k
                                        if (prevns)
7682
5.63k
                                            prevns->next = ns->next;
7683
9.01k
                                        else
7684
9.01k
                                            cur->nsDef = ns->next;
7685
14.6k
                                        goto next_ns_decl;
7686
14.6k
                                    }
7687
14.6k
        }
7688
595k
          }
7689
16.4k
      }
7690
7691
      /*
7692
      * Skip ns-references handling if the referenced
7693
      * ns-decl is declared on the same element.
7694
      */
7695
5.42k
      if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
7696
880
          adoptns = 0;
7697
      /*
7698
      * Does it shadow any ns-decl?
7699
      */
7700
5.42k
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
7701
94.5k
          XML_NSMAP_FOREACH(nsMap, mi) {
7702
94.5k
        if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7703
94.5k
            (mi->shadowDepth == -1) &&
7704
45.1k
            ((ns->prefix == mi->newNs->prefix) ||
7705
44.4k
            xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7706
7707
2.65k
            mi->shadowDepth = depth;
7708
2.65k
        }
7709
94.5k
          }
7710
4.11k
      }
7711
      /*
7712
      * Push mapping.
7713
      */
7714
5.42k
      if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
7715
5.42k
          depth) == NULL)
7716
6
          ret = -1;
7717
7718
5.42k
      prevns = ns;
7719
20.0k
next_ns_decl:
7720
20.0k
      ns = ns->next;
7721
20.0k
        }
7722
7.42k
    }
7723
17.4k
    if (! adoptns)
7724
880
        goto ns_end;
7725
                /* Falls through. */
7726
18.8k
      case XML_ATTRIBUTE_NODE:
7727
    /* No ns, no fun. */
7728
18.8k
    if (cur->ns == NULL)
7729
8.95k
        goto ns_end;
7730
7731
9.85k
    if (! parnsdone) {
7732
1.19k
        if ((elem->parent) &&
7733
988
      ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7734
755
      if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7735
755
        elem->parent) == -1)
7736
3
          ret = -1;
7737
755
        }
7738
1.19k
        parnsdone = 1;
7739
1.19k
    }
7740
    /*
7741
    * Adjust the reference if this was a redundant ns-decl.
7742
    */
7743
9.85k
    if (listRedund) {
7744
174k
       for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
7745
173k
           if (cur->ns == listRedund[j]) {
7746
4.72k
         cur->ns = listRedund[++j];
7747
4.72k
         break;
7748
4.72k
           }
7749
173k
       }
7750
5.57k
    }
7751
    /*
7752
    * Adopt ns-references.
7753
    */
7754
9.85k
    if (XML_NSMAP_NOTEMPTY(nsMap)) {
7755
        /*
7756
        * Search for a mapping.
7757
        */
7758
66.3k
        XML_NSMAP_FOREACH(nsMap, mi) {
7759
66.3k
      if ((mi->shadowDepth == -1) &&
7760
26.7k
          (cur->ns == mi->oldNs)) {
7761
7762
3.80k
          cur->ns = mi->newNs;
7763
3.80k
          goto ns_end;
7764
3.80k
      }
7765
66.3k
        }
7766
8.61k
    }
7767
    /*
7768
    * Acquire a normalized ns-decl and add it to the map.
7769
    */
7770
6.05k
    if (xmlDOMWrapNSNormAcquireNormalizedNs(doc, curElem,
7771
6.05k
      cur->ns, &ns,
7772
6.05k
      &nsMap, depth,
7773
6.05k
      ancestorsOnly,
7774
6.05k
      (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
7775
9
        ret = -1;
7776
6.05k
    cur->ns = ns;
7777
7778
19.6k
ns_end:
7779
19.6k
    if ((cur->type == XML_ELEMENT_NODE) &&
7780
17.4k
        (cur->properties != NULL)) {
7781
        /*
7782
        * Process attributes.
7783
        */
7784
1.95k
        cur = (xmlNodePtr) cur->properties;
7785
1.95k
        continue;
7786
1.95k
    }
7787
17.7k
    break;
7788
17.7k
      default:
7789
3.40k
    goto next_sibling;
7790
23.1k
  }
7791
19.6k
into_content:
7792
19.6k
  if ((cur->type == XML_ELEMENT_NODE) &&
7793
17.4k
      (cur->children != NULL)) {
7794
      /*
7795
      * Process content of element-nodes only.
7796
      */
7797
11.0k
      cur = cur->children;
7798
11.0k
      continue;
7799
11.0k
  }
7800
23.1k
next_sibling:
7801
23.1k
  if (cur == elem)
7802
3.53k
      break;
7803
19.5k
  if (cur->type == XML_ELEMENT_NODE) {
7804
13.8k
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
7805
    /*
7806
    * Pop mappings.
7807
    */
7808
18.8k
    while ((nsMap->last != NULL) &&
7809
18.2k
        (nsMap->last->depth >= depth))
7810
6.86k
    {
7811
6.86k
        XML_NSMAP_POP(nsMap, mi)
7812
6.86k
    }
7813
    /*
7814
    * Unshadow.
7815
    */
7816
72.4k
    XML_NSMAP_FOREACH(nsMap, mi) {
7817
72.4k
        if (mi->shadowDepth >= depth)
7818
4.36k
      mi->shadowDepth = -1;
7819
72.4k
    }
7820
12.0k
      }
7821
13.8k
      depth--;
7822
13.8k
  }
7823
19.5k
  if (cur->next != NULL)
7824
6.61k
      cur = cur->next;
7825
12.9k
  else {
7826
12.9k
      if (cur->type == XML_ATTRIBUTE_NODE) {
7827
1.95k
    cur = cur->parent;
7828
1.95k
    goto into_content;
7829
1.95k
      }
7830
11.0k
      cur = cur->parent;
7831
11.0k
      goto next_sibling;
7832
12.9k
  }
7833
19.5k
    } while (cur != NULL);
7834
7835
3.53k
    if (listRedund) {
7836
15.4k
  for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
7837
14.6k
      xmlFreeNs(listRedund[j]);
7838
14.6k
  }
7839
792
  xmlFree(listRedund);
7840
792
    }
7841
3.53k
    if (nsMap != NULL)
7842
2.57k
  xmlDOMWrapNsMapFree(nsMap);
7843
3.53k
    return (ret);
7844
3.53k
}
7845
7846
/**
7847
 * Ensures that ns-references point to `destDoc`: either to
7848
 * `elements->nsDef` entries if `destParent` is given, or to
7849
 * `destDoc->oldNs` otherwise.
7850
 *
7851
 * If `destParent` is given, it ensures that the tree is namespace
7852
 * wellformed by creating additional ns-decls where needed.
7853
 * Note that, since prefixes of already existent ns-decls can be
7854
 * shadowed by this process, it could break QNames in attribute
7855
 * values or element content.
7856
 *
7857
 * NOTE: This function was not intensively tested.
7858
 *
7859
 * @param ctxt  the optional context for custom processing
7860
 * @param sourceDoc  the optional sourceDoc
7861
 * @param node  the element-node to start with
7862
 * @param destDoc  the destination doc for adoption
7863
 * @param destParent  the optional new parent of `node` in `destDoc`
7864
 * @param options  option flags
7865
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
7866
 */
7867
static int
7868
xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
7869
          xmlDocPtr sourceDoc ATTRIBUTE_UNUSED,
7870
          xmlNodePtr node,
7871
          xmlDocPtr destDoc,
7872
          xmlNodePtr destParent,
7873
          int options ATTRIBUTE_UNUSED)
7874
2.09k
{
7875
2.09k
    int ret = 0;
7876
2.09k
    xmlNodePtr cur, curElem = NULL;
7877
2.09k
    xmlNsMapPtr nsMap = NULL;
7878
2.09k
    xmlNsMapItemPtr mi;
7879
2.09k
    xmlNsPtr ns = NULL;
7880
2.09k
    int depth = -1;
7881
    /* gather @parent's ns-decls. */
7882
2.09k
    int parnsdone;
7883
    /* @ancestorsOnly should be set per option. */
7884
2.09k
    int ancestorsOnly = 0;
7885
7886
    /*
7887
    * Get the ns-map from the context if available.
7888
    */
7889
2.09k
    if (ctxt)
7890
2.07k
  nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
7891
    /*
7892
    * Disable search for ns-decls in the parent-axis of the
7893
    * destination element, if:
7894
    * 1) there's no destination parent
7895
    * 2) custom ns-reference handling is used
7896
    */
7897
2.09k
    if ((destParent == NULL) ||
7898
1.30k
  (ctxt && ctxt->getNsForNodeFunc))
7899
793
    {
7900
793
  parnsdone = 1;
7901
793
    } else
7902
1.30k
  parnsdone = 0;
7903
7904
2.09k
    cur = node;
7905
7906
24.4k
    while (cur != NULL) {
7907
24.4k
        if (cur->doc != destDoc) {
7908
24.4k
            if (xmlNodeSetDoc(cur, destDoc) < 0)
7909
1
                ret = -1;
7910
24.4k
        }
7911
7912
24.4k
  switch (cur->type) {
7913
0
      case XML_XINCLUDE_START:
7914
0
      case XML_XINCLUDE_END:
7915
    /*
7916
    * TODO
7917
    */
7918
0
    ret = -1;
7919
0
                goto leave_node;
7920
13.3k
      case XML_ELEMENT_NODE:
7921
13.3k
    curElem = cur;
7922
13.3k
    depth++;
7923
    /*
7924
    * Namespace declarations.
7925
    * - ns->href and ns->prefix are never in the dict, so
7926
    *   we need not move the values over to the destination dict.
7927
    * - Note that for custom handling of ns-references,
7928
    *   the ns-decls need not be stored in the ns-map,
7929
    *   since they won't be referenced by node->ns.
7930
    */
7931
13.3k
    if ((cur->nsDef) &&
7932
3.63k
        ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
7933
3.63k
    {
7934
3.63k
        if (! parnsdone) {
7935
      /*
7936
      * Gather @parent's in-scope ns-decls.
7937
      */
7938
817
      if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7939
817
          destParent) == -1)
7940
2
          ret = -1;
7941
817
      parnsdone = 1;
7942
817
        }
7943
8.89k
        for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7944
      /*
7945
      * NOTE: ns->prefix and ns->href are never in the dict.
7946
      */
7947
      /*
7948
      * Does it shadow any ns-decl?
7949
      */
7950
5.25k
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
7951
45.8k
          XML_NSMAP_FOREACH(nsMap, mi) {
7952
45.8k
        if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7953
39.6k
            (mi->shadowDepth == -1) &&
7954
8.47k
            ((ns->prefix == mi->newNs->prefix) ||
7955
7.25k
            xmlStrEqual(ns->prefix,
7956
7.25k
            mi->newNs->prefix))) {
7957
7958
3.02k
            mi->shadowDepth = depth;
7959
3.02k
        }
7960
45.8k
          }
7961
4.11k
      }
7962
      /*
7963
      * Push mapping.
7964
      */
7965
5.25k
      if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
7966
5.25k
          ns, ns, depth) == NULL)
7967
6
          ret = -1;
7968
5.25k
        }
7969
3.63k
    }
7970
                /* Falls through. */
7971
16.0k
      case XML_ATTRIBUTE_NODE:
7972
    /* No namespace, no fun. */
7973
16.0k
    if (cur->ns == NULL)
7974
7.73k
        goto ns_end;
7975
7976
8.31k
    if (! parnsdone) {
7977
234
        if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
7978
234
      destParent) == -1)
7979
1
      ret = -1;
7980
234
        parnsdone = 1;
7981
234
    }
7982
    /*
7983
    * Adopt ns-references.
7984
    */
7985
8.31k
    if (XML_NSMAP_NOTEMPTY(nsMap)) {
7986
        /*
7987
        * Search for a mapping.
7988
        */
7989
37.5k
        XML_NSMAP_FOREACH(nsMap, mi) {
7990
37.5k
      if ((mi->shadowDepth == -1) &&
7991
12.7k
          (cur->ns == mi->oldNs)) {
7992
7993
4.69k
          cur->ns = mi->newNs;
7994
4.69k
          goto ns_end;
7995
4.69k
      }
7996
37.5k
        }
7997
6.35k
    }
7998
    /*
7999
    * No matching namespace in scope. We need a new one.
8000
    */
8001
3.61k
    if ((ctxt) && (ctxt->getNsForNodeFunc)) {
8002
        /*
8003
        * User-defined behaviour.
8004
        */
8005
0
        ns = ctxt->getNsForNodeFunc(ctxt, cur,
8006
0
      cur->ns->href, cur->ns->prefix);
8007
        /*
8008
        * Insert mapping if ns is available; it's the users fault
8009
        * if not.
8010
        */
8011
0
        if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8012
0
          cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8013
0
      ret = -1;
8014
0
        cur->ns = ns;
8015
3.61k
    } else {
8016
        /*
8017
        * Acquire a normalized ns-decl and add it to the map.
8018
        */
8019
3.61k
        if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc,
8020
      /* ns-decls on curElem or on destDoc->oldNs */
8021
3.61k
      destParent ? curElem : NULL,
8022
3.61k
      cur->ns, &ns,
8023
3.61k
      &nsMap, depth,
8024
3.61k
      ancestorsOnly,
8025
      /* ns-decls must be prefixed for attributes. */
8026
3.61k
      (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8027
6
      ret = -1;
8028
3.61k
        cur->ns = ns;
8029
3.61k
    }
8030
8031
16.0k
ns_end:
8032
16.0k
    if (cur->type == XML_ELEMENT_NODE) {
8033
13.3k
        cur->psvi = NULL;
8034
13.3k
        cur->line = 0;
8035
13.3k
        cur->extra = 0;
8036
        /*
8037
        * Walk attributes.
8038
        */
8039
13.3k
        if (cur->properties != NULL) {
8040
      /*
8041
      * Process first attribute node.
8042
      */
8043
2.10k
      cur = (xmlNodePtr) cur->properties;
8044
2.10k
      continue;
8045
2.10k
        }
8046
13.3k
    }
8047
13.9k
    break;
8048
13.9k
      case XML_TEXT_NODE:
8049
6.92k
      case XML_CDATA_SECTION_NODE:
8050
7.21k
      case XML_PI_NODE:
8051
7.41k
      case XML_COMMENT_NODE:
8052
8.37k
      case XML_ENTITY_REF_NODE:
8053
8.37k
    goto leave_node;
8054
0
      default:
8055
0
    ret = -1;
8056
24.4k
  }
8057
  /*
8058
  * Walk the tree.
8059
  */
8060
13.9k
  if (cur->children != NULL) {
8061
10.5k
      cur = cur->children;
8062
10.5k
      continue;
8063
10.5k
  }
8064
8065
24.4k
leave_node:
8066
24.4k
  if (cur == node)
8067
2.09k
      break;
8068
22.3k
  if ((cur->type == XML_ELEMENT_NODE) ||
8069
11.0k
      (cur->type == XML_XINCLUDE_START) ||
8070
11.0k
      (cur->type == XML_XINCLUDE_END))
8071
11.2k
  {
8072
      /*
8073
      * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8074
      */
8075
11.2k
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
8076
    /*
8077
    * Pop mappings.
8078
    */
8079
14.0k
    while ((nsMap->last != NULL) &&
8080
13.0k
        (nsMap->last->depth >= depth))
8081
4.96k
    {
8082
4.96k
        XML_NSMAP_POP(nsMap, mi)
8083
4.96k
    }
8084
    /*
8085
    * Unshadow.
8086
    */
8087
46.5k
    XML_NSMAP_FOREACH(nsMap, mi) {
8088
46.5k
        if (mi->shadowDepth >= depth)
8089
2.62k
      mi->shadowDepth = -1;
8090
46.5k
    }
8091
9.04k
      }
8092
11.2k
      depth--;
8093
11.2k
  }
8094
22.3k
  if (cur->next != NULL)
8095
8.45k
      cur = cur->next;
8096
13.8k
  else if ((cur->type == XML_ATTRIBUTE_NODE) &&
8097
2.10k
      (cur->parent->children != NULL))
8098
1.17k
  {
8099
1.17k
      cur = cur->parent->children;
8100
12.6k
  } else {
8101
12.6k
      cur = cur->parent;
8102
12.6k
      goto leave_node;
8103
12.6k
  }
8104
22.3k
    }
8105
8106
    /*
8107
    * Cleanup.
8108
    */
8109
2.09k
    if (nsMap != NULL) {
8110
1.36k
  if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8111
      /*
8112
      * Just cleanup the map but don't free.
8113
      */
8114
0
      if (nsMap->first) {
8115
0
    if (nsMap->pool)
8116
0
        nsMap->last->next = nsMap->pool;
8117
0
    nsMap->pool = nsMap->first;
8118
0
    nsMap->first = NULL;
8119
0
      }
8120
0
  } else
8121
1.36k
      xmlDOMWrapNsMapFree(nsMap);
8122
1.36k
    }
8123
2.09k
    return(ret);
8124
2.09k
}
8125
8126
/**
8127
 * Clone a node and fix namespaces.
8128
 *
8129
 * References of out-of scope ns-decls are remapped to point to `destDoc`.
8130
 * If `destParent` is given, then nsDef entries on element-nodes are used.
8131
 * If *no* `destParent` is given, then `destDoc->oldNs` entries are used.
8132
 * This is the case when you don't know already where the cloned branch
8133
 * will be added to.
8134
 *
8135
 * If `destParent` is given, it ensures that the tree is namespace
8136
 * wellformed by creating additional ns-decls where needed.
8137
 * Note that, since prefixes of already existent ns-decls can be
8138
 * shadowed by this process, it could break QNames in attribute
8139
 * values or element content.
8140
 *
8141
 * @param ctxt  the optional context for custom processing
8142
 * @param sourceDoc  the optional sourceDoc
8143
 * @param node  the node to start with
8144
 * @param resNode  the clone of the given `node`
8145
 * @param destDoc  the destination doc
8146
 * @param destParent  the optional new parent of `node` in `destDoc`
8147
 * @param deep  descend into child if set
8148
 * @param options  option flags
8149
 * @returns 0 if the operation succeeded,
8150
 *          1 if a node of unsupported (or not yet supported) type was given,
8151
 *          -1 on API/internal errors.
8152
 */
8153
int
8154
xmlDOMWrapCloneNode(xmlDOMWrapCtxt *ctxt,
8155
          xmlDoc *sourceDoc,
8156
          xmlNode *node,
8157
          xmlNode **resNode,
8158
          xmlDoc *destDoc,
8159
          xmlNode *destParent,
8160
          int deep,
8161
          int options ATTRIBUTE_UNUSED)
8162
17.5k
{
8163
17.5k
    int ret = 0;
8164
17.5k
    xmlNodePtr cur, cloneElem = NULL;
8165
17.5k
    xmlNsMapPtr nsMap = NULL;
8166
17.5k
    xmlNsMapItemPtr mi;
8167
17.5k
    xmlNsPtr ns;
8168
17.5k
    int depth = -1;
8169
    /* int adoptStr = 1; */
8170
    /* gather @parent's ns-decls. */
8171
17.5k
    int parnsdone = 0;
8172
    /*
8173
    * @ancestorsOnly:
8174
    * TODO: @ancestorsOnly should be set per option.
8175
    *
8176
    */
8177
17.5k
    int ancestorsOnly = 0;
8178
17.5k
    xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
8179
17.5k
    xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
8180
17.5k
    xmlDictPtr dict; /* The destination dict */
8181
8182
17.5k
    if ((node == NULL) || (resNode == NULL) || (destDoc == NULL) ||
8183
14.7k
  ((destParent != NULL) && (destParent->doc != destDoc)))
8184
3.07k
  return(-1);
8185
    /*
8186
    * TODO: Initially we support only element-nodes.
8187
    */
8188
14.5k
    if (node->type != XML_ELEMENT_NODE)
8189
456
  return(1);
8190
    /*
8191
    * Check node->doc sanity.
8192
    */
8193
14.0k
    if ((node->doc != NULL) && (sourceDoc != NULL) &&
8194
12.3k
  (node->doc != sourceDoc)) {
8195
  /*
8196
  * Might be an XIncluded node.
8197
  */
8198
205
  return (-1);
8199
205
    }
8200
13.8k
    if (sourceDoc == NULL)
8201
1.65k
  sourceDoc = node->doc;
8202
13.8k
    if (sourceDoc == NULL)
8203
269
        return (-1);
8204
8205
13.5k
    dict = destDoc->dict;
8206
    /*
8207
    * Reuse the namespace map of the context.
8208
    */
8209
13.5k
    if (ctxt)
8210
13.5k
  nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8211
8212
13.5k
    *resNode = NULL;
8213
8214
13.5k
    cur = node;
8215
151k
    while (cur != NULL) {
8216
151k
  if (cur->doc != sourceDoc) {
8217
      /*
8218
      * We'll assume XIncluded nodes if the doc differs.
8219
      * TODO: Do we need to reconciliate XIncluded nodes?
8220
      * TODO: This here returns -1 in this case.
8221
      */
8222
80
      goto internal_error;
8223
80
  }
8224
  /*
8225
  * Create a new node.
8226
  */
8227
151k
  switch (cur->type) {
8228
0
      case XML_XINCLUDE_START:
8229
0
      case XML_XINCLUDE_END:
8230
    /*
8231
    * TODO: What to do with XInclude?
8232
    */
8233
0
    goto internal_error;
8234
0
    break;
8235
59.9k
      case XML_ELEMENT_NODE:
8236
107k
      case XML_TEXT_NODE:
8237
107k
      case XML_CDATA_SECTION_NODE:
8238
107k
      case XML_COMMENT_NODE:
8239
107k
      case XML_PI_NODE:
8240
107k
      case XML_DOCUMENT_FRAG_NODE:
8241
110k
      case XML_ENTITY_REF_NODE:
8242
    /*
8243
    * Nodes of xmlNode structure.
8244
    */
8245
110k
    clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
8246
110k
    if (clone == NULL)
8247
26
        goto internal_error;
8248
109k
    memset(clone, 0, sizeof(xmlNode));
8249
    /*
8250
    * Set hierarchical links.
8251
    */
8252
109k
    if (resultClone != NULL) {
8253
96.5k
        clone->parent = parentClone;
8254
96.5k
        if (prevClone) {
8255
27.4k
      prevClone->next = clone;
8256
27.4k
      clone->prev = prevClone;
8257
27.4k
        } else
8258
69.0k
      parentClone->children = clone;
8259
96.5k
                    parentClone->last = clone;
8260
96.5k
    } else
8261
13.4k
        resultClone = clone;
8262
8263
109k
    break;
8264
41.0k
      case XML_ATTRIBUTE_NODE:
8265
    /*
8266
    * Attributes (xmlAttr).
8267
    */
8268
41.0k
    clone = xmlMalloc(sizeof(xmlAttr));
8269
41.0k
    if (clone == NULL)
8270
9
        goto internal_error;
8271
41.0k
    memset(clone, 0, sizeof(xmlAttr));
8272
    /*
8273
    * Set hierarchical links.
8274
    * TODO: Change this to add to the end of attributes.
8275
    */
8276
41.0k
    if (resultClone != NULL) {
8277
41.0k
        clone->parent = parentClone;
8278
41.0k
        if (prevClone) {
8279
20.0k
      prevClone->next = clone;
8280
20.0k
      clone->prev = prevClone;
8281
20.0k
        } else
8282
21.0k
      parentClone->properties = (xmlAttrPtr) clone;
8283
41.0k
    } else
8284
0
        resultClone = clone;
8285
41.0k
    break;
8286
0
      default:
8287
    /*
8288
    * TODO QUESTION: Any other nodes expected?
8289
    */
8290
0
    goto internal_error;
8291
151k
  }
8292
8293
151k
  clone->type = cur->type;
8294
151k
  clone->doc = destDoc;
8295
8296
  /*
8297
  * Clone the name of the node if any.
8298
  */
8299
151k
  if (cur->name == xmlStringText)
8300
47.0k
      clone->name = xmlStringText;
8301
103k
  else if (cur->name == xmlStringTextNoenc)
8302
      /*
8303
      * NOTE: Although xmlStringTextNoenc is never assigned to a node
8304
      *   in tree.c, it might be set in Libxslt via
8305
      *   "xsl:disable-output-escaping".
8306
      */
8307
0
      clone->name = xmlStringTextNoenc;
8308
103k
  else if (cur->name == xmlStringComment)
8309
245
      clone->name = xmlStringComment;
8310
103k
  else if (cur->name != NULL) {
8311
102k
            if (dict != NULL)
8312
29.7k
                clone->name = xmlDictLookup(dict, cur->name, -1);
8313
72.5k
            else
8314
72.5k
                clone->name = xmlStrdup(cur->name);
8315
102k
            if (clone->name == NULL)
8316
212
                goto internal_error;
8317
102k
  }
8318
8319
150k
  switch (cur->type) {
8320
0
      case XML_XINCLUDE_START:
8321
0
      case XML_XINCLUDE_END:
8322
    /*
8323
    * TODO
8324
    */
8325
0
    return (-1);
8326
59.7k
      case XML_ELEMENT_NODE:
8327
59.7k
    cloneElem = clone;
8328
59.7k
    depth++;
8329
    /*
8330
    * Namespace declarations.
8331
    */
8332
59.7k
    if (cur->nsDef != NULL) {
8333
25.3k
        if (! parnsdone) {
8334
6.00k
      if (destParent && (ctxt == NULL)) {
8335
          /*
8336
          * Gather @parent's in-scope ns-decls.
8337
          */
8338
12
          if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8339
12
        destParent) == -1)
8340
0
        goto internal_error;
8341
12
      }
8342
6.00k
      parnsdone = 1;
8343
6.00k
        }
8344
        /*
8345
        * Clone namespace declarations.
8346
        */
8347
25.3k
        cloneNsDefSlot = &(clone->nsDef);
8348
60.7k
        for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8349
      /*
8350
      * Create a new xmlNs.
8351
      */
8352
35.3k
      cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
8353
35.3k
      if (cloneNs == NULL)
8354
11
          goto internal_error;
8355
35.3k
      memset(cloneNs, 0, sizeof(xmlNs));
8356
35.3k
      cloneNs->type = XML_LOCAL_NAMESPACE;
8357
8358
35.3k
      if (ns->href != NULL) {
8359
27.0k
          cloneNs->href = xmlStrdup(ns->href);
8360
27.0k
                            if (cloneNs->href == NULL) {
8361
9
                                xmlFreeNs(cloneNs);
8362
9
                                goto internal_error;
8363
9
                            }
8364
27.0k
                        }
8365
35.3k
      if (ns->prefix != NULL) {
8366
23.8k
          cloneNs->prefix = xmlStrdup(ns->prefix);
8367
23.8k
                            if (cloneNs->prefix == NULL) {
8368
4
                                xmlFreeNs(cloneNs);
8369
4
                                goto internal_error;
8370
4
                            }
8371
23.8k
                        }
8372
8373
35.3k
      *cloneNsDefSlot = cloneNs;
8374
35.3k
      cloneNsDefSlot = &(cloneNs->next);
8375
8376
      /*
8377
      * Note that for custom handling of ns-references,
8378
      * the ns-decls need not be stored in the ns-map,
8379
      * since they won't be referenced by node->ns.
8380
      */
8381
35.3k
      if ((ctxt == NULL) ||
8382
34.7k
          (ctxt->getNsForNodeFunc == NULL))
8383
35.3k
      {
8384
          /*
8385
          * Does it shadow any ns-decl?
8386
          */
8387
35.3k
          if (XML_NSMAP_NOTEMPTY(nsMap)) {
8388
155k
        XML_NSMAP_FOREACH(nsMap, mi) {
8389
155k
            if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8390
149k
          (mi->shadowDepth == -1) &&
8391
48.6k
          ((ns->prefix == mi->newNs->prefix) ||
8392
40.2k
          xmlStrEqual(ns->prefix,
8393
40.2k
          mi->newNs->prefix))) {
8394
          /*
8395
          * Mark as shadowed at the current
8396
          * depth.
8397
          */
8398
24.6k
          mi->shadowDepth = depth;
8399
24.6k
            }
8400
155k
        }
8401
27.9k
          }
8402
          /*
8403
          * Push mapping.
8404
          */
8405
35.3k
          if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8406
35.3k
        ns, cloneNs, depth) == NULL)
8407
8
        goto internal_error;
8408
35.3k
      }
8409
35.3k
        }
8410
25.3k
    }
8411
    /* cur->ns will be processed further down. */
8412
59.7k
    break;
8413
59.7k
      case XML_ATTRIBUTE_NODE:
8414
    /* IDs will be processed further down. */
8415
    /* cur->ns will be processed further down. */
8416
40.9k
    break;
8417
349
      case XML_PI_NODE:
8418
594
      case XML_COMMENT_NODE:
8419
47.6k
      case XML_TEXT_NODE:
8420
48.0k
      case XML_CDATA_SECTION_NODE:
8421
    /*
8422
    * Note that this will also cover the values of attributes.
8423
    */
8424
48.0k
                if (cur->content != NULL) {
8425
47.1k
                    clone->content = xmlStrdup(cur->content);
8426
47.1k
                    if (clone->content == NULL)
8427
36
                        goto internal_error;
8428
47.1k
                }
8429
48.0k
    goto leave_node;
8430
48.0k
      case XML_ENTITY_REF_NODE:
8431
2.03k
    if (sourceDoc != destDoc) {
8432
755
        if ((destDoc->intSubset) || (destDoc->extSubset)) {
8433
555
      xmlEntityPtr ent;
8434
      /*
8435
      * Different doc: Assign new entity-node if available.
8436
      */
8437
555
      ent = xmlGetDocEntity(destDoc, cur->name);
8438
555
      if (ent != NULL) {
8439
85
          clone->content = ent->content;
8440
85
          clone->children = (xmlNodePtr) ent;
8441
85
          clone->last = (xmlNodePtr) ent;
8442
85
      }
8443
555
        }
8444
1.27k
    } else {
8445
        /*
8446
        * Same doc: Use the current node's entity declaration
8447
        * and value.
8448
        */
8449
1.27k
        clone->content = cur->content;
8450
1.27k
        clone->children = cur->children;
8451
1.27k
        clone->last = cur->last;
8452
1.27k
    }
8453
2.03k
    goto leave_node;
8454
0
      default:
8455
0
    goto internal_error;
8456
150k
  }
8457
8458
100k
  if (cur->ns == NULL)
8459
51.7k
      goto end_ns_reference;
8460
8461
/* handle_ns_reference: */
8462
  /*
8463
  ** The following will take care of references to ns-decls ********
8464
  ** and is intended only for element- and attribute-nodes.
8465
  **
8466
  */
8467
48.9k
  if (! parnsdone) {
8468
2.22k
      if (destParent && (ctxt == NULL)) {
8469
6
    if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
8470
0
        goto internal_error;
8471
6
      }
8472
2.22k
      parnsdone = 1;
8473
2.22k
  }
8474
  /*
8475
  * Adopt ns-references.
8476
  */
8477
48.9k
  if (XML_NSMAP_NOTEMPTY(nsMap)) {
8478
      /*
8479
      * Search for a mapping.
8480
      */
8481
199k
      XML_NSMAP_FOREACH(nsMap, mi) {
8482
199k
    if ((mi->shadowDepth == -1) &&
8483
66.7k
        (cur->ns == mi->oldNs)) {
8484
        /*
8485
        * This is the nice case: a mapping was found.
8486
        */
8487
29.9k
        clone->ns = mi->newNs;
8488
29.9k
        goto end_ns_reference;
8489
29.9k
    }
8490
199k
      }
8491
42.8k
  }
8492
  /*
8493
  * No matching namespace in scope. We need a new one.
8494
  */
8495
19.0k
  if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
8496
      /*
8497
      * User-defined behaviour.
8498
      */
8499
0
      ns = ctxt->getNsForNodeFunc(ctxt, cur,
8500
0
    cur->ns->href, cur->ns->prefix);
8501
      /*
8502
      * Add user's mapping.
8503
      */
8504
0
      if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8505
0
    cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8506
0
    goto internal_error;
8507
0
      clone->ns = ns;
8508
19.0k
  } else {
8509
      /*
8510
      * Acquire a normalized ns-decl and add it to the map.
8511
      */
8512
19.0k
      if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc,
8513
    /* ns-decls on cloneElem or on destDoc->oldNs */
8514
19.0k
    destParent ? cloneElem : NULL,
8515
19.0k
    cur->ns, &ns,
8516
19.0k
    &nsMap, depth,
8517
    /* if we need to search only in the ancestor-axis */
8518
19.0k
    ancestorsOnly,
8519
    /* ns-decls must be prefixed for attributes. */
8520
19.0k
    (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8521
11
    goto internal_error;
8522
19.0k
      clone->ns = ns;
8523
19.0k
  }
8524
8525
100k
end_ns_reference:
8526
8527
  /*
8528
  * Some post-processing.
8529
  *
8530
  * Handle ID attributes.
8531
  */
8532
100k
  if ((clone->type == XML_ATTRIBUTE_NODE) &&
8533
40.9k
      (clone->parent != NULL))
8534
40.9k
  {
8535
40.9k
            int res;
8536
8537
40.9k
      res = xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone);
8538
40.9k
            if (res < 0)
8539
1
                goto internal_error;
8540
40.9k
            if (res == 1) {
8541
441
    xmlChar *idVal;
8542
8543
441
    idVal = xmlNodeGetContent(cur);
8544
441
                if (idVal == NULL)
8545
1
                    goto internal_error;
8546
440
                if (xmlAddIDSafe((xmlAttrPtr) cur, idVal) < 0) {
8547
1
                    xmlFree(idVal);
8548
1
                    goto internal_error;
8549
1
                }
8550
439
                xmlFree(idVal);
8551
439
      }
8552
40.9k
  }
8553
  /*
8554
  **
8555
  ** The following will traverse the tree **************************
8556
  **
8557
  *
8558
  * Walk the element's attributes before descending into child-nodes.
8559
  */
8560
100k
  if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
8561
21.0k
      prevClone = NULL;
8562
21.0k
      parentClone = clone;
8563
21.0k
      cur = (xmlNodePtr) cur->properties;
8564
21.0k
      continue;
8565
21.0k
  }
8566
100k
into_content:
8567
  /*
8568
  * Descend into child-nodes.
8569
  */
8570
100k
  if (cur->children != NULL) {
8571
70.7k
      if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
8572
69.0k
    prevClone = NULL;
8573
69.0k
    parentClone = clone;
8574
69.0k
    cur = cur->children;
8575
69.0k
    continue;
8576
69.0k
      }
8577
70.7k
  }
8578
8579
150k
leave_node:
8580
  /*
8581
  * At this point we are done with the node, its content
8582
  * and an element-nodes's attribute-nodes.
8583
  */
8584
150k
  if (cur == node)
8585
13.1k
      break;
8586
137k
  if ((cur->type == XML_ELEMENT_NODE) ||
8587
90.9k
      (cur->type == XML_XINCLUDE_START) ||
8588
90.9k
      (cur->type == XML_XINCLUDE_END)) {
8589
      /*
8590
      * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8591
      */
8592
46.1k
      if (XML_NSMAP_NOTEMPTY(nsMap)) {
8593
    /*
8594
    * Pop mappings.
8595
    */
8596
57.4k
    while ((nsMap->last != NULL) &&
8597
55.3k
        (nsMap->last->depth >= depth))
8598
27.8k
    {
8599
27.8k
        XML_NSMAP_POP(nsMap, mi)
8600
27.8k
    }
8601
    /*
8602
    * Unshadow.
8603
    */
8604
118k
    XML_NSMAP_FOREACH(nsMap, mi) {
8605
118k
        if (mi->shadowDepth >= depth)
8606
24.2k
      mi->shadowDepth = -1;
8607
118k
    }
8608
29.5k
      }
8609
46.1k
      depth--;
8610
46.1k
  }
8611
137k
  if (cur->next != NULL) {
8612
47.4k
      prevClone = clone;
8613
47.4k
      cur = cur->next;
8614
89.6k
  } else if (cur->type != XML_ATTRIBUTE_NODE) {
8615
68.7k
      clone = clone->parent;
8616
68.7k
      if (clone != NULL)
8617
68.7k
    parentClone = clone->parent;
8618
      /*
8619
      * Process parent --> next;
8620
      */
8621
68.7k
      cur = cur->parent;
8622
68.7k
      goto leave_node;
8623
68.7k
  } else {
8624
      /* This is for attributes only. */
8625
20.9k
      clone = clone->parent;
8626
20.9k
      parentClone = clone->parent;
8627
      /*
8628
      * Process parent-element --> children.
8629
      */
8630
20.9k
      cur = cur->parent;
8631
20.9k
      goto into_content;
8632
20.9k
  }
8633
137k
    }
8634
13.1k
    goto exit;
8635
8636
13.1k
internal_error:
8637
409
    ret = -1;
8638
8639
13.5k
exit:
8640
    /*
8641
    * Cleanup.
8642
    */
8643
13.5k
    if (nsMap != NULL) {
8644
7.02k
  if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8645
      /*
8646
      * Just cleanup the map but don't free.
8647
      */
8648
0
      if (nsMap->first) {
8649
0
    if (nsMap->pool)
8650
0
        nsMap->last->next = nsMap->pool;
8651
0
    nsMap->pool = nsMap->first;
8652
0
    nsMap->first = NULL;
8653
0
      }
8654
0
  } else
8655
7.02k
      xmlDOMWrapNsMapFree(nsMap);
8656
7.02k
    }
8657
    /*
8658
    * TODO: Should we try a cleanup of the cloned node in case of a
8659
    * fatal error?
8660
    */
8661
13.5k
    *resNode = resultClone;
8662
13.5k
    return (ret);
8663
409
}
8664
8665
/**
8666
 * `attr` is adopted by `destDoc`.
8667
 * Ensures that ns-references point to `destDoc`: either to
8668
 * `elements->nsDef` entries if `destParent` is given, or to
8669
 * `destDoc->oldNs` otherwise.
8670
 *
8671
 * @param ctxt  the optional context for custom processing
8672
 * @param sourceDoc  unused
8673
 * @param attr  the attribute-node to be adopted
8674
 * @param destDoc  the destination doc for adoption
8675
 * @param destParent  the optional new parent of `attr` in `destDoc`
8676
 * @param options  option flags (unused)
8677
 * @returns 0 if succeeded, -1 otherwise and on API/internal errors.
8678
 */
8679
static int
8680
xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
8681
        xmlDocPtr sourceDoc ATTRIBUTE_UNUSED,
8682
        xmlAttrPtr attr,
8683
        xmlDocPtr destDoc,
8684
        xmlNodePtr destParent,
8685
        int options ATTRIBUTE_UNUSED)
8686
4.40k
{
8687
4.40k
    int ret = 0;
8688
8689
4.40k
    if ((attr == NULL) || (destDoc == NULL))
8690
0
  return (-1);
8691
8692
4.40k
    if (attr->doc != destDoc) {
8693
4.40k
        if (xmlSetTreeDoc((xmlNodePtr) attr, destDoc) < 0)
8694
1
            ret = -1;
8695
4.40k
    }
8696
8697
4.40k
    if (attr->ns != NULL) {
8698
4.10k
  xmlNsPtr ns = NULL;
8699
8700
4.10k
  if (ctxt != NULL) {
8701
      /* TODO: User defined. */
8702
4.09k
  }
8703
  /* XML Namespace. */
8704
4.10k
  if (IS_STR_XML(attr->ns->prefix)) {
8705
289
      ns = xmlTreeEnsureXMLDecl(destDoc);
8706
3.81k
  } else if (destParent == NULL) {
8707
      /*
8708
      * Store in @destDoc->oldNs.
8709
      */
8710
239
      ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
8711
3.57k
  } else {
8712
      /*
8713
      * Declare on @destParent.
8714
      */
8715
3.57k
      if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
8716
3.57k
    &ns, 1) == -1)
8717
1.29k
    ret = -1;
8718
3.57k
      if (ns == NULL) {
8719
1.69k
    ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
8720
1.69k
        attr->ns->href, attr->ns->prefix, 1);
8721
1.69k
      }
8722
3.57k
  }
8723
4.10k
  if (ns == NULL)
8724
1
      ret = -1;
8725
4.10k
  attr->ns = ns;
8726
4.10k
    }
8727
8728
4.40k
    return (ret);
8729
4.40k
}
8730
8731
/**
8732
 * Fix up namespaces before moving a node.
8733
 *
8734
 * References of out-of scope ns-decls are remapped to point to `destDoc`:
8735
 * If `destParent` is given, then nsDef entries on element-nodes are used.
8736
 * If *no* `destParent` is given, then `destDoc->oldNs` entries are used
8737
 * This is the case when you have an unlinked node and just want to move it
8738
 * to the context of.
8739
 *
8740
 * If `destParent` is given, it ensures that the tree is namespace
8741
 * wellformed by creating additional ns-decls where needed.
8742
 * Note that, since prefixes of already existent ns-decls can be
8743
 * shadowed by this process, it could break QNames in attribute
8744
 * values or element content.
8745
 *
8746
 * NOTE: This function was not intensively tested.
8747
 *
8748
 * @param ctxt  the optional context for custom processing
8749
 * @param sourceDoc  the optional sourceDoc
8750
 * @param node  the node to start with
8751
 * @param destDoc  the destination doc
8752
 * @param destParent  the optional new parent of `node` in `destDoc`
8753
 * @param options  option flags
8754
 * @returns 0 if the operation succeeded,
8755
 *          1 if a node of unsupported type was given,
8756
 *          2 if a node of not yet supported type was given and
8757
 *          -1 on API/internal errors.
8758
 */
8759
int
8760
xmlDOMWrapAdoptNode(xmlDOMWrapCtxt *ctxt,
8761
        xmlDoc *sourceDoc,
8762
        xmlNode *node,
8763
        xmlDoc *destDoc,
8764
        xmlNode *destParent,
8765
        int options)
8766
13.5k
{
8767
13.5k
    int ret = 0;
8768
8769
13.5k
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
8770
12.7k
        (destDoc == NULL) ||
8771
11.5k
  ((destParent != NULL) && (destParent->doc != destDoc)))
8772
2.42k
  return(-1);
8773
    /*
8774
    * Check node->doc sanity.
8775
    */
8776
11.1k
    if (sourceDoc == NULL) {
8777
8.80k
        sourceDoc = node->doc;
8778
8.80k
    } else if (node->doc != sourceDoc) {
8779
475
  return (-1);
8780
475
    }
8781
8782
    /*
8783
     * TODO: Shouldn't this be allowed?
8784
     */
8785
10.6k
    if (sourceDoc == destDoc)
8786
2.45k
  return (-1);
8787
8788
8.22k
    switch (node->type) {
8789
2.09k
  case XML_ELEMENT_NODE:
8790
6.49k
  case XML_ATTRIBUTE_NODE:
8791
6.69k
  case XML_TEXT_NODE:
8792
6.88k
  case XML_CDATA_SECTION_NODE:
8793
7.09k
  case XML_ENTITY_REF_NODE:
8794
7.54k
  case XML_PI_NODE:
8795
7.75k
  case XML_COMMENT_NODE:
8796
7.75k
      break;
8797
195
  case XML_DOCUMENT_FRAG_NODE:
8798
      /* TODO: Support document-fragment-nodes. */
8799
195
      return (2);
8800
275
  default:
8801
275
      return (1);
8802
8.22k
    }
8803
    /*
8804
    * Unlink only if @node was not already added to @destParent.
8805
    */
8806
7.75k
    if ((node->parent != NULL) && (destParent != node->parent))
8807
7.11k
  xmlUnlinkNodeInternal(node);
8808
8809
7.75k
    if (node->type == XML_ELEMENT_NODE) {
8810
2.09k
      return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
8811
2.09k
        destDoc, destParent, options));
8812
5.65k
    } else if (node->type == XML_ATTRIBUTE_NODE) {
8813
4.40k
      return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
8814
4.40k
    (xmlAttrPtr) node, destDoc, destParent, options));
8815
4.40k
    } else {
8816
1.25k
        if (node->doc != destDoc) {
8817
1.25k
            if (xmlNodeSetDoc(node, destDoc) < 0)
8818
0
                ret = -1;
8819
1.25k
        }
8820
1.25k
    }
8821
1.25k
    return (ret);
8822
7.75k
}
8823
8824
/************************************************************************
8825
 *                  *
8826
 *      XHTML detection         *
8827
 *                  *
8828
 ************************************************************************/
8829
8830
51.1k
#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
8831
51.1k
   "-//W3C//DTD XHTML 1.0 Strict//EN"
8832
51.1k
#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
8833
51.1k
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
8834
39.7k
#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
8835
39.7k
   "-//W3C//DTD XHTML 1.0 Frameset//EN"
8836
40.6k
#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
8837
40.6k
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
8838
33.3k
#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
8839
33.3k
   "-//W3C//DTD XHTML 1.0 Transitional//EN"
8840
39.7k
#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
8841
39.7k
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
8842
8843
/**
8844
 * Try to find if the document correspond to an XHTML DTD
8845
 *
8846
 * @param systemID  the system identifier
8847
 * @param publicID  the public identifier
8848
 * @returns 1 if true, 0 if not and -1 in case of error
8849
 */
8850
int
8851
90.5k
xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
8852
90.5k
    if ((systemID == NULL) && (publicID == NULL))
8853
18.5k
  return(-1);
8854
72.0k
    if (publicID != NULL) {
8855
51.1k
  if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
8856
39.7k
  if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
8857
33.3k
  if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
8858
33.3k
    }
8859
52.6k
    if (systemID != NULL) {
8860
51.1k
  if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
8861
40.6k
  if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
8862
39.7k
  if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
8863
39.7k
    }
8864
40.5k
    return(0);
8865
52.6k
}
8866
8867
/************************************************************************
8868
 *                  *
8869
 *      Node callbacks          *
8870
 *                  *
8871
 ************************************************************************/
8872
8873
/**
8874
 * Registers a callback for node creation
8875
 *
8876
 * @deprecated don't use
8877
 *
8878
 * @param func  function pointer to the new RegisterNodeFunc
8879
 * @returns the old value of the registration function
8880
 */
8881
xmlRegisterNodeFunc
8882
xmlRegisterNodeDefault(xmlRegisterNodeFunc func)
8883
0
{
8884
0
    xmlRegisterNodeFunc old = xmlRegisterNodeDefaultValue;
8885
8886
0
    xmlRegisterCallbacks = 1;
8887
0
    xmlRegisterNodeDefaultValue = func;
8888
0
    return(old);
8889
0
}
8890
8891
/**
8892
 * Registers a callback for node destruction
8893
 *
8894
 * @deprecated don't use
8895
 *
8896
 * @param func  function pointer to the new DeregisterNodeFunc
8897
 * @returns the previous value of the deregistration function
8898
 */
8899
xmlDeregisterNodeFunc
8900
xmlDeregisterNodeDefault(xmlDeregisterNodeFunc func)
8901
0
{
8902
0
    xmlDeregisterNodeFunc old = xmlDeregisterNodeDefaultValue;
8903
8904
0
    xmlRegisterCallbacks = 1;
8905
0
    xmlDeregisterNodeDefaultValue = func;
8906
0
    return(old);
8907
0
}
8908