Coverage Report

Created: 2023-06-07 06:06

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