Coverage Report

Created: 2024-09-06 06:20

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