Coverage Report

Created: 2022-05-03 06:10

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