Coverage Report

Created: 2023-11-19 06:13

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